blob: f7b913cc4aa4c92d2b07f389725359f6e782fdf7 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ExternalASTSource.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Chris Lattner1b63e4f2009-06-14 01:54:56 +000021#include "clang/Basic/Builtins.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000022#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000024#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000025#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000026#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
29enum FloatingRank {
30 FloatRank, DoubleRank, LongDoubleRank
31};
32
Chris Lattner61710852008-10-05 17:34:18 +000033ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
34 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000035 IdentifierTable &idents, SelectorTable &sels,
Chris Lattner1b63e4f2009-06-14 01:54:56 +000036 Builtin::Context &builtins,
37 bool FreeMem, unsigned size_reserve) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000038 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
Douglas Gregorc29f77b2009-07-07 16:35:42 +000039 ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0),
40 SourceMgr(SM), LangOpts(LOpts),
Douglas Gregor2e222532009-07-02 17:08:52 +000041 LoadedExternalComments(false), FreeMemory(FreeMem), Target(t),
42 Idents(idents), Selectors(sels),
Chris Lattnere4f21422009-06-30 01:26:17 +000043 BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
Daniel Dunbare91593e2008-08-11 04:54:23 +000044 if (size_reserve > 0) Types.reserve(size_reserve);
Daniel Dunbare91593e2008-08-11 04:54:23 +000045 TUDecl = TranslationUnitDecl::Create(*this);
Steve Naroff14108da2009-07-10 23:34:53 +000046 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000047}
48
Reid Spencer5f016e22007-07-11 17:01:13 +000049ASTContext::~ASTContext() {
50 // Deallocate all the types.
51 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000052 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000053 Types.pop_back();
54 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000055
Nuno Lopesb74668e2008-12-17 22:30:25 +000056 {
57 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
58 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
59 while (I != E) {
60 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
61 delete R;
62 }
63 }
64
65 {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +000066 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
67 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopesb74668e2008-12-17 22:30:25 +000068 while (I != E) {
69 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
70 delete R;
71 }
72 }
73
Douglas Gregorab452ba2009-03-26 23:50:42 +000074 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000075 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
76 NNS = NestedNameSpecifiers.begin(),
77 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000078 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000079 /* Increment in loop */)
80 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000081
82 if (GlobalNestedNameSpecifier)
83 GlobalNestedNameSpecifier->Destroy(*this);
84
Eli Friedmanb26153c2008-05-27 03:08:09 +000085 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000086}
87
Douglas Gregor2cf26342009-04-09 22:27:44 +000088void
89ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
90 ExternalSource.reset(Source.take());
91}
92
Reid Spencer5f016e22007-07-11 17:01:13 +000093void ASTContext::PrintStats() const {
94 fprintf(stderr, "*** AST Context Stats:\n");
95 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +000096
Douglas Gregordbe833d2009-05-26 14:40:08 +000097 unsigned counts[] = {
98#define TYPE(Name, Parent) 0,
99#define ABSTRACT_TYPE(Name, Parent)
100#include "clang/AST/TypeNodes.def"
101 0 // Extra
102 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000103
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
105 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000106 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 }
108
Douglas Gregordbe833d2009-05-26 14:40:08 +0000109 unsigned Idx = 0;
110 unsigned TotalBytes = 0;
111#define TYPE(Name, Parent) \
112 if (counts[Idx]) \
113 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
114 TotalBytes += counts[Idx] * sizeof(Name##Type); \
115 ++Idx;
116#define ABSTRACT_TYPE(Name, Parent)
117#include "clang/AST/TypeNodes.def"
118
119 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000120
121 if (ExternalSource.get()) {
122 fprintf(stderr, "\n");
123 ExternalSource->PrintStats();
124 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000125}
126
127
128void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000129 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000130}
131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132void ASTContext::InitBuiltinTypes() {
133 assert(VoidTy.isNull() && "Context reinitialized?");
134
135 // C99 6.2.5p19.
136 InitBuiltinType(VoidTy, BuiltinType::Void);
137
138 // C99 6.2.5p2.
139 InitBuiltinType(BoolTy, BuiltinType::Bool);
140 // C99 6.2.5p3.
Eli Friedman15b91762009-06-05 07:05:05 +0000141 if (LangOpts.CharIsSigned)
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 InitBuiltinType(CharTy, BuiltinType::Char_S);
143 else
144 InitBuiltinType(CharTy, BuiltinType::Char_U);
145 // C99 6.2.5p4.
146 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
147 InitBuiltinType(ShortTy, BuiltinType::Short);
148 InitBuiltinType(IntTy, BuiltinType::Int);
149 InitBuiltinType(LongTy, BuiltinType::Long);
150 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
151
152 // C99 6.2.5p6.
153 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
154 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
155 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
156 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
157 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
158
159 // C99 6.2.5p10.
160 InitBuiltinType(FloatTy, BuiltinType::Float);
161 InitBuiltinType(DoubleTy, BuiltinType::Double);
162 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000163
Chris Lattner2df9ced2009-04-30 02:43:43 +0000164 // GNU extension, 128-bit integers.
165 InitBuiltinType(Int128Ty, BuiltinType::Int128);
166 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
167
Chris Lattner3a250322009-02-26 23:43:47 +0000168 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
169 InitBuiltinType(WCharTy, BuiltinType::WChar);
170 else // C99
171 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000172
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000173 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
174 InitBuiltinType(Char16Ty, BuiltinType::Char16);
175 else // C99
176 Char16Ty = getFromTargetType(Target.getChar16Type());
177
178 if (LangOpts.CPlusPlus) // C++0x 3.9.1p5, extension for C++
179 InitBuiltinType(Char32Ty, BuiltinType::Char32);
180 else // C99
181 Char32Ty = getFromTargetType(Target.getChar32Type());
182
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000183 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000184 InitBuiltinType(OverloadTy, BuiltinType::Overload);
185
186 // Placeholder type for type-dependent expressions whose type is
187 // completely unknown. No code should ever check a type against
188 // DependentTy and users should never see it; however, it is here to
189 // help diagnose failures to properly check for type-dependent
190 // expressions.
191 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000192
Anders Carlssone89d1592009-06-26 18:41:36 +0000193 // Placeholder type for C++0x auto declarations whose real type has
194 // not yet been deduced.
195 InitBuiltinType(UndeducedAutoTy, BuiltinType::UndeducedAuto);
196
Reid Spencer5f016e22007-07-11 17:01:13 +0000197 // C99 6.2.5p11.
198 FloatComplexTy = getComplexType(FloatTy);
199 DoubleComplexTy = getComplexType(DoubleTy);
200 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000201
Steve Naroff7e219e42007-10-15 14:41:52 +0000202 BuiltinVaListType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000203
Steve Naroffde2e22d2009-07-15 18:40:39 +0000204 // "Builtin" typedefs set by Sema::ActOnTranslationUnitScope().
205 ObjCIdTypedefType = QualType();
206 ObjCClassTypedefType = QualType();
207
208 // Builtin types for 'id' and 'Class'.
209 InitBuiltinType(ObjCBuiltinIdTy, BuiltinType::ObjCId);
210 InitBuiltinType(ObjCBuiltinClassTy, BuiltinType::ObjCClass);
Steve Naroff14108da2009-07-10 23:34:53 +0000211
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000212 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000213
214 // void * type
215 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000216
217 // nullptr type (C++0x 2.14.7)
218 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000219}
220
Douglas Gregor2e222532009-07-02 17:08:52 +0000221namespace {
222 class BeforeInTranslationUnit
223 : std::binary_function<SourceRange, SourceRange, bool> {
224 SourceManager *SourceMgr;
225
226 public:
227 explicit BeforeInTranslationUnit(SourceManager *SM) : SourceMgr(SM) { }
228
229 bool operator()(SourceRange X, SourceRange Y) {
230 return SourceMgr->isBeforeInTranslationUnit(X.getBegin(), Y.getBegin());
231 }
232 };
233}
234
235/// \brief Determine whether the given comment is a Doxygen-style comment.
236///
237/// \param Start the start of the comment text.
238///
239/// \param End the end of the comment text.
240///
241/// \param Member whether we want to check whether this is a member comment
242/// (which requires a < after the Doxygen-comment delimiter). Otherwise,
243/// we only return true when we find a non-member comment.
244static bool
245isDoxygenComment(SourceManager &SourceMgr, SourceRange Comment,
246 bool Member = false) {
247 const char *BufferStart
248 = SourceMgr.getBufferData(SourceMgr.getFileID(Comment.getBegin())).first;
249 const char *Start = BufferStart + SourceMgr.getFileOffset(Comment.getBegin());
250 const char* End = BufferStart + SourceMgr.getFileOffset(Comment.getEnd());
251
252 if (End - Start < 4)
253 return false;
254
255 assert(Start[0] == '/' && "Not a comment?");
256 if (Start[1] == '*' && !(Start[2] == '!' || Start[2] == '*'))
257 return false;
258 if (Start[1] == '/' && !(Start[2] == '!' || Start[2] == '/'))
259 return false;
260
261 return (Start[3] == '<') == Member;
262}
263
264/// \brief Retrieve the comment associated with the given declaration, if
265/// it has one.
266const char *ASTContext::getCommentForDecl(const Decl *D) {
267 if (!D)
268 return 0;
269
270 // Check whether we have cached a comment string for this declaration
271 // already.
272 llvm::DenseMap<const Decl *, std::string>::iterator Pos
273 = DeclComments.find(D);
274 if (Pos != DeclComments.end())
275 return Pos->second.c_str();
276
277 // If we have an external AST source and have not yet loaded comments from
278 // that source, do so now.
279 if (ExternalSource && !LoadedExternalComments) {
280 std::vector<SourceRange> LoadedComments;
281 ExternalSource->ReadComments(LoadedComments);
282
283 if (!LoadedComments.empty())
284 Comments.insert(Comments.begin(), LoadedComments.begin(),
285 LoadedComments.end());
286
287 LoadedExternalComments = true;
288 }
289
290 // If there are no comments anywhere, we won't find anything.
291 if (Comments.empty())
292 return 0;
293
294 // If the declaration doesn't map directly to a location in a file, we
295 // can't find the comment.
296 SourceLocation DeclStartLoc = D->getLocStart();
297 if (DeclStartLoc.isInvalid() || !DeclStartLoc.isFileID())
298 return 0;
299
300 // Find the comment that occurs just before this declaration.
301 std::vector<SourceRange>::iterator LastComment
302 = std::lower_bound(Comments.begin(), Comments.end(),
303 SourceRange(DeclStartLoc),
304 BeforeInTranslationUnit(&SourceMgr));
305
306 // Decompose the location for the start of the declaration and find the
307 // beginning of the file buffer.
308 std::pair<FileID, unsigned> DeclStartDecomp
309 = SourceMgr.getDecomposedLoc(DeclStartLoc);
310 const char *FileBufferStart
311 = SourceMgr.getBufferData(DeclStartDecomp.first).first;
312
313 // First check whether we have a comment for a member.
314 if (LastComment != Comments.end() &&
315 !isa<TagDecl>(D) && !isa<NamespaceDecl>(D) &&
316 isDoxygenComment(SourceMgr, *LastComment, true)) {
317 std::pair<FileID, unsigned> LastCommentEndDecomp
318 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
319 if (DeclStartDecomp.first == LastCommentEndDecomp.first &&
320 SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second)
321 == SourceMgr.getLineNumber(LastCommentEndDecomp.first,
322 LastCommentEndDecomp.second)) {
323 // The Doxygen member comment comes after the declaration starts and
324 // is on the same line and in the same file as the declaration. This
325 // is the comment we want.
326 std::string &Result = DeclComments[D];
327 Result.append(FileBufferStart +
328 SourceMgr.getFileOffset(LastComment->getBegin()),
329 FileBufferStart + LastCommentEndDecomp.second + 1);
330 return Result.c_str();
331 }
332 }
333
334 if (LastComment == Comments.begin())
335 return 0;
336 --LastComment;
337
338 // Decompose the end of the comment.
339 std::pair<FileID, unsigned> LastCommentEndDecomp
340 = SourceMgr.getDecomposedLoc(LastComment->getEnd());
341
342 // If the comment and the declaration aren't in the same file, then they
343 // aren't related.
344 if (DeclStartDecomp.first != LastCommentEndDecomp.first)
345 return 0;
346
347 // Check that we actually have a Doxygen comment.
348 if (!isDoxygenComment(SourceMgr, *LastComment))
349 return 0;
350
351 // Compute the starting line for the declaration and for the end of the
352 // comment (this is expensive).
353 unsigned DeclStartLine
354 = SourceMgr.getLineNumber(DeclStartDecomp.first, DeclStartDecomp.second);
355 unsigned CommentEndLine
356 = SourceMgr.getLineNumber(LastCommentEndDecomp.first,
357 LastCommentEndDecomp.second);
358
359 // If the comment does not end on the line prior to the declaration, then
360 // the comment is not associated with the declaration at all.
361 if (CommentEndLine + 1 != DeclStartLine)
362 return 0;
363
364 // We have a comment, but there may be more comments on the previous lines.
365 // Keep looking so long as the comments are still Doxygen comments and are
366 // still adjacent.
367 unsigned ExpectedLine
368 = SourceMgr.getSpellingLineNumber(LastComment->getBegin()) - 1;
369 std::vector<SourceRange>::iterator FirstComment = LastComment;
370 while (FirstComment != Comments.begin()) {
371 // Look at the previous comment
372 --FirstComment;
373 std::pair<FileID, unsigned> Decomp
374 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
375
376 // If this previous comment is in a different file, we're done.
377 if (Decomp.first != DeclStartDecomp.first) {
378 ++FirstComment;
379 break;
380 }
381
382 // If this comment is not a Doxygen comment, we're done.
383 if (!isDoxygenComment(SourceMgr, *FirstComment)) {
384 ++FirstComment;
385 break;
386 }
387
388 // If the line number is not what we expected, we're done.
389 unsigned Line = SourceMgr.getLineNumber(Decomp.first, Decomp.second);
390 if (Line != ExpectedLine) {
391 ++FirstComment;
392 break;
393 }
394
395 // Set the next expected line number.
396 ExpectedLine
397 = SourceMgr.getSpellingLineNumber(FirstComment->getBegin()) - 1;
398 }
399
400 // The iterator range [FirstComment, LastComment] contains all of the
401 // BCPL comments that, together, are associated with this declaration.
402 // Form a single comment block string for this declaration that concatenates
403 // all of these comments.
404 std::string &Result = DeclComments[D];
405 while (FirstComment != LastComment) {
406 std::pair<FileID, unsigned> DecompStart
407 = SourceMgr.getDecomposedLoc(FirstComment->getBegin());
408 std::pair<FileID, unsigned> DecompEnd
409 = SourceMgr.getDecomposedLoc(FirstComment->getEnd());
410 Result.append(FileBufferStart + DecompStart.second,
411 FileBufferStart + DecompEnd.second + 1);
412 ++FirstComment;
413 }
414
415 // Append the last comment line.
416 Result.append(FileBufferStart +
417 SourceMgr.getFileOffset(LastComment->getBegin()),
418 FileBufferStart + LastCommentEndDecomp.second + 1);
419 return Result.c_str();
420}
421
Chris Lattner464175b2007-07-18 17:52:12 +0000422//===----------------------------------------------------------------------===//
423// Type Sizing and Analysis
424//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000425
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000426/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
427/// scalar floating point type.
428const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
429 const BuiltinType *BT = T->getAsBuiltinType();
430 assert(BT && "Not a floating point type!");
431 switch (BT->getKind()) {
432 default: assert(0 && "Not a floating point type!");
433 case BuiltinType::Float: return Target.getFloatFormat();
434 case BuiltinType::Double: return Target.getDoubleFormat();
435 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
436 }
437}
438
Chris Lattneraf707ab2009-01-24 21:53:27 +0000439/// getDeclAlign - Return a conservative estimate of the alignment of the
440/// specified decl. Note that bitfields do not have a valid alignment, so
441/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000442unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000443 unsigned Align = Target.getCharWidth();
444
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000445 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
Eli Friedmandcdafb62009-02-22 02:56:25 +0000446 Align = std::max(Align, AA->getAlignment());
447
Chris Lattneraf707ab2009-01-24 21:53:27 +0000448 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
449 QualType T = VD->getType();
Ted Kremenek35366a62009-07-17 17:50:17 +0000450 if (const ReferenceType* RT = T->getAsReferenceType()) {
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000451 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000452 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000453 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
454 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000455 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
456 T = cast<ArrayType>(T)->getElementType();
457
458 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
459 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000460 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000461
462 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000463}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000464
Chris Lattnera7674d82007-07-13 22:13:22 +0000465/// getTypeSize - Return the size of the specified type, in bits. This method
466/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000467std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000468ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000469 uint64_t Width=0;
470 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000471 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000472#define TYPE(Class, Base)
473#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000474#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000475#define DEPENDENT_TYPE(Class, Base) case Type::Class:
476#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000477 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000478 break;
479
Chris Lattner692233e2007-07-13 22:27:08 +0000480 case Type::FunctionNoProto:
481 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000482 // GCC extension: alignof(function) = 32 bits
483 Width = 0;
484 Align = 32;
485 break;
486
Douglas Gregor72564e72009-02-26 23:50:07 +0000487 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000488 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000489 Width = 0;
490 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
491 break;
492
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +0000493 case Type::ConstantArrayWithExpr:
494 case Type::ConstantArrayWithoutExpr:
Steve Narofffb22d962007-08-30 01:06:46 +0000495 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000496 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000497
Chris Lattner98be4942008-03-05 18:54:05 +0000498 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000499 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000500 Align = EltInfo.second;
501 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000502 }
Nate Begeman213541a2008-04-18 23:10:10 +0000503 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000504 case Type::Vector: {
505 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000506 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000507 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000508 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000509 // If the alignment is not a power of 2, round up to the next power of 2.
510 // This happens for non-power-of-2 length vectors.
511 // FIXME: this should probably be a target property.
512 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000513 break;
514 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000515
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000516 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000517 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000518 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000519 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000520 // GCC extension: alignof(void) = 8 bits.
521 Width = 0;
522 Align = 8;
523 break;
524
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000525 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000526 Width = Target.getBoolWidth();
527 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000528 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000529 case BuiltinType::Char_S:
530 case BuiltinType::Char_U:
531 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000532 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000533 Width = Target.getCharWidth();
534 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000535 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000536 case BuiltinType::WChar:
537 Width = Target.getWCharWidth();
538 Align = Target.getWCharAlign();
539 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +0000540 case BuiltinType::Char16:
541 Width = Target.getChar16Width();
542 Align = Target.getChar16Align();
543 break;
544 case BuiltinType::Char32:
545 Width = Target.getChar32Width();
546 Align = Target.getChar32Align();
547 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000548 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000549 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000550 Width = Target.getShortWidth();
551 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000552 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000553 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000554 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000555 Width = Target.getIntWidth();
556 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000557 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000558 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000559 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000560 Width = Target.getLongWidth();
561 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000562 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000563 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000564 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000565 Width = Target.getLongLongWidth();
566 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000567 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000568 case BuiltinType::Int128:
569 case BuiltinType::UInt128:
570 Width = 128;
571 Align = 128; // int128_t is 128-bit aligned on all targets.
572 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000573 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000574 Width = Target.getFloatWidth();
575 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000576 break;
577 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000578 Width = Target.getDoubleWidth();
579 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000580 break;
581 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000582 Width = Target.getLongDoubleWidth();
583 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000584 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000585 case BuiltinType::NullPtr:
586 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
587 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000588 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000589 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000590 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000591 case Type::FixedWidthInt:
592 // FIXME: This isn't precisely correct; the width/alignment should depend
593 // on the available types for the target
594 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000595 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000596 Align = Width;
597 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000598 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000599 // FIXME: Pointers into different addr spaces could have different sizes and
600 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000601 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Steve Naroffd1b3c2d2009-06-17 22:40:22 +0000602 case Type::ObjCObjectPointer:
Chris Lattner5426bf62008-04-07 07:01:58 +0000603 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000604 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000605 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000606 case Type::BlockPointer: {
607 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
608 Width = Target.getPointerWidth(AS);
609 Align = Target.getPointerAlign(AS);
610 break;
611 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000612 case Type::Pointer: {
613 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000614 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000615 Align = Target.getPointerAlign(AS);
616 break;
617 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000618 case Type::LValueReference:
619 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000620 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000621 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000622 // FIXME: This is wrong for struct layout: a reference in a struct has
623 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000624 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000625 case Type::MemberPointer: {
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000626 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
627 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
628 // If we ever want to support other ABIs this needs to be abstracted.
629
Sebastian Redlf30208a2009-01-24 21:16:55 +0000630 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000631 std::pair<uint64_t, unsigned> PtrDiffInfo =
632 getTypeInfo(getPointerDiffType());
633 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000634 if (Pointee->isFunctionType())
635 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000636 Align = PtrDiffInfo.second;
637 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000638 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000639 case Type::Complex: {
640 // Complex types have the same alignment as their elements, but twice the
641 // size.
642 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000643 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000644 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000645 Align = EltInfo.second;
646 break;
647 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000648 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000649 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000650 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
651 Width = Layout.getSize();
652 Align = Layout.getAlignment();
653 break;
654 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000655 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000656 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000657 const TagType *TT = cast<TagType>(T);
658
659 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000660 Width = 1;
661 Align = 1;
662 break;
663 }
664
Daniel Dunbar1d751182008-11-08 05:48:37 +0000665 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000666 return getTypeInfo(ET->getDecl()->getIntegerType());
667
Daniel Dunbar1d751182008-11-08 05:48:37 +0000668 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000669 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
670 Width = Layout.getSize();
671 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000672 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000673 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000674
Douglas Gregor18857642009-04-30 17:32:17 +0000675 case Type::Typedef: {
676 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000677 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
Douglas Gregor18857642009-04-30 17:32:17 +0000678 Align = Aligned->getAlignment();
679 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
680 } else
681 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000682 break;
Chris Lattner71763312008-04-06 22:05:18 +0000683 }
Douglas Gregor18857642009-04-30 17:32:17 +0000684
685 case Type::TypeOfExpr:
686 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
687 .getTypePtr());
688
689 case Type::TypeOf:
690 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
691
Anders Carlsson395b4752009-06-24 19:06:50 +0000692 case Type::Decltype:
693 return getTypeInfo(cast<DecltypeType>(T)->getUnderlyingExpr()->getType()
694 .getTypePtr());
695
Douglas Gregor18857642009-04-30 17:32:17 +0000696 case Type::QualifiedName:
697 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
698
699 case Type::TemplateSpecialization:
700 assert(getCanonicalType(T) != T &&
701 "Cannot request the size of a dependent type");
702 // FIXME: this is likely to be wrong once we support template
703 // aliases, since a template alias could refer to a typedef that
704 // has an __aligned__ attribute on it.
705 return getTypeInfo(getCanonicalType(T));
706 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000707
Chris Lattner464175b2007-07-18 17:52:12 +0000708 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000709 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000710}
711
Chris Lattner34ebde42009-01-27 18:08:34 +0000712/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
713/// type for the current target in bits. This can be different than the ABI
714/// alignment in cases where it is beneficial for performance to overalign
715/// a data type.
716unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
717 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000718
719 // Double and long long should be naturally aligned if possible.
720 if (const ComplexType* CT = T->getAsComplexType())
721 T = CT->getElementType().getTypePtr();
722 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
723 T->isSpecificBuiltinType(BuiltinType::LongLong))
724 return std::max(ABIAlign, (unsigned)getTypeSize(T));
725
Chris Lattner34ebde42009-01-27 18:08:34 +0000726 return ABIAlign;
727}
728
729
Devang Patel8b277042008-06-04 21:22:16 +0000730/// LayoutField - Field layout.
731void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000732 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000733 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000734 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000735 uint64_t FieldOffset = IsUnion ? 0 : Size;
736 uint64_t FieldSize;
737 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000738
739 // FIXME: Should this override struct packing? Probably we want to
740 // take the minimum?
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000741 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000742 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000743
744 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
745 // TODO: Need to check this algorithm on other targets!
746 // (tested on Linux-X86)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000747 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000748
749 std::pair<uint64_t, unsigned> FieldInfo =
750 Context.getTypeInfo(FD->getType());
751 uint64_t TypeSize = FieldInfo.first;
752
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000753 // Determine the alignment of this bitfield. The packing
754 // attributes define a maximum and the alignment attribute defines
755 // a minimum.
756 // FIXME: What is the right behavior when the specified alignment
757 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000758 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000759 if (FieldPacking)
760 FieldAlign = std::min(FieldAlign, FieldPacking);
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000761 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000762 FieldAlign = std::max(FieldAlign, AA->getAlignment());
763
764 // Check if we need to add padding to give the field the correct
765 // alignment.
766 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
767 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
768
769 // Padding members don't affect overall alignment
770 if (!FD->getIdentifier())
771 FieldAlign = 1;
772 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000773 if (FD->getType()->isIncompleteArrayType()) {
774 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000775 // query getTypeInfo about these, so we figure it out here.
776 // Flexible array members don't have any size, but they
777 // have to be aligned appropriately for their element type.
778 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000779 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000780 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Ted Kremenek35366a62009-07-17 17:50:17 +0000781 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
Anders Carlsson2f1169f2009-04-10 05:31:15 +0000782 unsigned AS = RT->getPointeeType().getAddressSpace();
783 FieldSize = Context.Target.getPointerWidth(AS);
784 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patel8b277042008-06-04 21:22:16 +0000785 } else {
786 std::pair<uint64_t, unsigned> FieldInfo =
787 Context.getTypeInfo(FD->getType());
788 FieldSize = FieldInfo.first;
789 FieldAlign = FieldInfo.second;
790 }
791
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000792 // Determine the alignment of this bitfield. The packing
793 // attributes define a maximum and the alignment attribute defines
794 // a minimum. Additionally, the packing alignment must be at least
795 // a byte for non-bitfields.
796 //
797 // FIXME: What is the right behavior when the specified alignment
798 // is smaller than the specified packing?
799 if (FieldPacking)
800 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000801 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000802 FieldAlign = std::max(FieldAlign, AA->getAlignment());
803
804 // Round up the current record size to the field's alignment boundary.
805 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
806 }
807
808 // Place this field at the current location.
809 FieldOffsets[FieldNo] = FieldOffset;
810
811 // Reserve space for this field.
812 if (IsUnion) {
813 Size = std::max(Size, FieldSize);
814 } else {
815 Size = FieldOffset + FieldSize;
816 }
817
Daniel Dunbard6884a02009-05-04 05:16:21 +0000818 // Remember the next available offset.
819 NextOffset = Size;
820
Devang Patel8b277042008-06-04 21:22:16 +0000821 // Remember max struct/class alignment.
822 Alignment = std::max(Alignment, FieldAlign);
823}
824
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000825static void CollectLocalObjCIvars(ASTContext *Ctx,
826 const ObjCInterfaceDecl *OI,
827 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000828 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
829 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000830 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000831 if (!IVDecl->isInvalidDecl())
832 Fields.push_back(cast<FieldDecl>(IVDecl));
833 }
834}
835
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000836void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
837 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
838 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
839 CollectObjCIvars(SuperClass, Fields);
840 CollectLocalObjCIvars(this, OI, Fields);
841}
842
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000843/// ShallowCollectObjCIvars -
844/// Collect all ivars, including those synthesized, in the current class.
845///
846void ASTContext::ShallowCollectObjCIvars(const ObjCInterfaceDecl *OI,
847 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars,
848 bool CollectSynthesized) {
849 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
850 E = OI->ivar_end(); I != E; ++I) {
851 Ivars.push_back(*I);
852 }
853 if (CollectSynthesized)
854 CollectSynthesizedIvars(OI, Ivars);
855}
856
Fariborz Jahanian98200742009-05-12 18:14:29 +0000857void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
858 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000859 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
860 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian98200742009-05-12 18:14:29 +0000861 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
862 Ivars.push_back(Ivar);
863
864 // Also look into nested protocols.
865 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
866 E = PD->protocol_end(); P != E; ++P)
867 CollectProtocolSynthesizedIvars(*P, Ivars);
868}
869
870/// CollectSynthesizedIvars -
871/// This routine collect synthesized ivars for the designated class.
872///
873void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
874 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000875 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
876 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000877 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
878 Ivars.push_back(Ivar);
879 }
880 // Also look into interface's protocol list for properties declared
881 // in the protocol and whose ivars are synthesized.
882 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
883 PE = OI->protocol_end(); P != PE; ++P) {
884 ObjCProtocolDecl *PD = (*P);
885 CollectProtocolSynthesizedIvars(PD, Ivars);
886 }
887}
888
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000889unsigned ASTContext::CountProtocolSynthesizedIvars(const ObjCProtocolDecl *PD) {
890 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000891 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(),
892 E = PD->prop_end(); I != E; ++I)
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000893 if ((*I)->getPropertyIvarDecl())
894 ++count;
895
896 // Also look into nested protocols.
897 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
898 E = PD->protocol_end(); P != E; ++P)
899 count += CountProtocolSynthesizedIvars(*P);
900 return count;
901}
902
903unsigned ASTContext::CountSynthesizedIvars(const ObjCInterfaceDecl *OI)
904{
905 unsigned count = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000906 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
907 E = OI->prop_end(); I != E; ++I) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000908 if ((*I)->getPropertyIvarDecl())
909 ++count;
910 }
911 // Also look into interface's protocol list for properties declared
912 // in the protocol and whose ivars are synthesized.
913 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
914 PE = OI->protocol_end(); P != PE; ++P) {
915 ObjCProtocolDecl *PD = (*P);
916 count += CountProtocolSynthesizedIvars(PD);
917 }
918 return count;
919}
920
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000921/// getInterfaceLayoutImpl - Get or compute information about the
922/// layout of the given interface.
923///
924/// \param Impl - If given, also include the layout of the interface's
925/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000926const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000927ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
928 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000929 assert(!D->isForwardDecl() && "Invalid interface decl!");
930
Devang Patel44a3dde2008-06-04 21:54:36 +0000931 // Look up this layout, if already laid out, return what we have.
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000932 ObjCContainerDecl *Key =
933 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
934 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
935 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000936
Daniel Dunbar453addb2009-05-03 11:16:44 +0000937 unsigned FieldCount = D->ivar_size();
938 // Add in synthesized ivar count if laying out an implementation.
939 if (Impl) {
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000940 unsigned SynthCount = CountSynthesizedIvars(D);
941 FieldCount += SynthCount;
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000942 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000943 // entry. Note we can't cache this because we simply free all
944 // entries later; however we shouldn't look up implementations
945 // frequently.
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000946 if (SynthCount == 0)
Daniel Dunbar453addb2009-05-03 11:16:44 +0000947 return getObjCLayout(D, 0);
948 }
949
Devang Patel6a5a34c2008-06-06 02:14:01 +0000950 ASTRecordLayout *NewEntry = NULL;
Devang Patel6a5a34c2008-06-06 02:14:01 +0000951 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Devang Patel6a5a34c2008-06-06 02:14:01 +0000952 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
953 unsigned Alignment = SL.getAlignment();
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000954
Daniel Dunbar913af352009-05-07 21:58:26 +0000955 // We start laying out ivars not at the end of the superclass
956 // structure, but at the next byte following the last field.
957 uint64_t Size = llvm::RoundUpToAlignment(SL.NextOffset, 8);
Daniel Dunbard6884a02009-05-04 05:16:21 +0000958
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000959 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000960 NewEntry->InitializeLayout(FieldCount);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000961 } else {
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000962 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
Devang Patel6a5a34c2008-06-06 02:14:01 +0000963 NewEntry->InitializeLayout(FieldCount);
964 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000965
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000966 unsigned StructPacking = 0;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000967 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000968 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000969
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000970 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel44a3dde2008-06-04 21:54:36 +0000971 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
972 AA->getAlignment()));
973
974 // Layout each ivar sequentially.
975 unsigned i = 0;
Fariborz Jahanian8e6ac1d2009-06-04 01:19:09 +0000976 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
977 ShallowCollectObjCIvars(D, Ivars, Impl);
978 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
979 NewEntry->LayoutField(Ivars[k], i++, false, StructPacking, *this);
980
Devang Patel44a3dde2008-06-04 21:54:36 +0000981 // Finally, round the size of the total struct up to the alignment of the
982 // struct itself.
983 NewEntry->FinalizeLayout();
984 return *NewEntry;
985}
986
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000987const ASTRecordLayout &
988ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
989 return getObjCLayout(D, 0);
990}
991
992const ASTRecordLayout &
993ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
994 return getObjCLayout(D->getClassInterface(), D);
995}
996
Devang Patel88a981b2007-11-01 19:11:01 +0000997/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000998/// specified record (struct/union/class), which indicates its size and field
999/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +00001000const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001001 D = D->getDefinition(*this);
1002 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +00001003
Chris Lattner464175b2007-07-18 17:52:12 +00001004 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +00001005 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +00001006 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +00001007
Devang Patel88a981b2007-11-01 19:11:01 +00001008 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
1009 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
1010 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +00001011 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +00001012
Douglas Gregore267ff32008-12-11 20:41:00 +00001013 // FIXME: Avoid linear walk through the fields, if possible.
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001014 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001015 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +00001016
Daniel Dunbar3b0db902008-10-16 02:34:03 +00001017 unsigned StructPacking = 0;
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001018 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
Daniel Dunbar3b0db902008-10-16 02:34:03 +00001019 StructPacking = PA->getAlignment();
1020
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00001021 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +00001022 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
1023 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +00001024
Eli Friedman4bd998b2008-05-30 09:31:38 +00001025 // Layout each field, for now, just sequentially, respecting alignment. In
1026 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +00001027 unsigned FieldIdx = 0;
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00001028 for (RecordDecl::field_iterator Field = D->field_begin(),
1029 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00001030 Field != FieldEnd; (void)++Field, ++FieldIdx)
1031 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +00001032
1033 // Finally, round the size of the total struct up to the alignment of the
1034 // struct itself.
Sebastian Redl1590d9c2009-05-27 19:34:06 +00001035 NewEntry->FinalizeLayout(getLangOptions().CPlusPlus);
Chris Lattner5d2a6302007-07-18 18:26:58 +00001036 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +00001037}
1038
Chris Lattnera7674d82007-07-13 22:13:22 +00001039//===----------------------------------------------------------------------===//
1040// Type creation/memoization methods
1041//===----------------------------------------------------------------------===//
1042
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001043QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001044 QualType CanT = getCanonicalType(T);
1045 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +00001046 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001047
1048 // If we are composing extended qualifiers together, merge together into one
1049 // ExtQualType node.
1050 unsigned CVRQuals = T.getCVRQualifiers();
1051 QualType::GCAttrTypes GCAttr = QualType::GCNone;
1052 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +00001053
Chris Lattnerb7d25532009-02-18 22:53:11 +00001054 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
1055 // If this type already has an address space specified, it cannot get
1056 // another one.
1057 assert(EQT->getAddressSpace() == 0 &&
1058 "Type cannot be in multiple addr spaces!");
1059 GCAttr = EQT->getObjCGCAttr();
1060 TypeNode = EQT->getBaseType();
1061 }
Chris Lattnerf46699c2008-02-20 20:55:12 +00001062
Chris Lattnerb7d25532009-02-18 22:53:11 +00001063 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +00001064 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001065 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +00001066 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001067 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +00001068 return QualType(EXTQy, CVRQuals);
1069
Christopher Lambebb97e92008-02-04 02:31:56 +00001070 // If the base type isn't canonical, this won't be a canonical type either,
1071 // so fill in the canonical type field.
1072 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001073 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001074 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +00001075
Chris Lattnerb7d25532009-02-18 22:53:11 +00001076 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001077 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001078 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +00001079 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00001080 ExtQualType *New =
1081 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001082 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +00001083 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001084 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +00001085}
1086
Chris Lattnerb7d25532009-02-18 22:53:11 +00001087QualType ASTContext::getObjCGCQualType(QualType T,
1088 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001089 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001090 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001091 return T;
1092
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001093 if (T->isPointerType()) {
Ted Kremenek35366a62009-07-17 17:50:17 +00001094 QualType Pointee = T->getAsPointerType()->getPointeeType();
Steve Naroff58f9f2c2009-07-14 18:25:06 +00001095 if (Pointee->isAnyPointerType()) {
Fariborz Jahanian4027cd12009-06-03 17:15:17 +00001096 QualType ResultType = getObjCGCQualType(Pointee, GCAttr);
1097 return getPointerType(ResultType);
1098 }
1099 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00001100 // If we are composing extended qualifiers together, merge together into one
1101 // ExtQualType node.
1102 unsigned CVRQuals = T.getCVRQualifiers();
1103 Type *TypeNode = T.getTypePtr();
1104 unsigned AddressSpace = 0;
1105
1106 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
1107 // If this type already has an address space specified, it cannot get
1108 // another one.
1109 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
1110 "Type cannot be in multiple addr spaces!");
1111 AddressSpace = EQT->getAddressSpace();
1112 TypeNode = EQT->getBaseType();
1113 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001114
1115 // Check if we've already instantiated an gc qual'd type of this type.
1116 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +00001117 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001118 void *InsertPos = 0;
1119 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +00001120 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001121
1122 // If the base type isn't canonical, this won't be a canonical type either,
1123 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00001124 // FIXME: Isn't this also not canonical if the base type is a array
1125 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001126 QualType Canonical;
1127 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00001128 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001129
Chris Lattnerb7d25532009-02-18 22:53:11 +00001130 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001131 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
1132 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1133 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00001134 ExtQualType *New =
1135 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001136 ExtQualTypes.InsertNode(New, InsertPos);
1137 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +00001138 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +00001139}
Chris Lattnera7674d82007-07-13 22:13:22 +00001140
Reid Spencer5f016e22007-07-11 17:01:13 +00001141/// getComplexType - Return the uniqued reference to the type for a complex
1142/// number with the specified element type.
1143QualType ASTContext::getComplexType(QualType T) {
1144 // Unique pointers, to guarantee there is only one pointer of a particular
1145 // structure.
1146 llvm::FoldingSetNodeID ID;
1147 ComplexType::Profile(ID, T);
1148
1149 void *InsertPos = 0;
1150 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
1151 return QualType(CT, 0);
1152
1153 // If the pointee type isn't canonical, this won't be a canonical type either,
1154 // so fill in the canonical type field.
1155 QualType Canonical;
1156 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001157 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +00001158
1159 // Get the new insert position for the node we care about.
1160 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001161 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 }
Steve Narofff83820b2009-01-27 22:08:43 +00001163 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 Types.push_back(New);
1165 ComplexTypes.InsertNode(New, InsertPos);
1166 return QualType(New, 0);
1167}
1168
Eli Friedmanf98aba32009-02-13 02:31:07 +00001169QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
1170 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
1171 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
1172 FixedWidthIntType *&Entry = Map[Width];
1173 if (!Entry)
1174 Entry = new FixedWidthIntType(Width, Signed);
1175 return QualType(Entry, 0);
1176}
Reid Spencer5f016e22007-07-11 17:01:13 +00001177
1178/// getPointerType - Return the uniqued reference to the type for a pointer to
1179/// the specified type.
1180QualType ASTContext::getPointerType(QualType T) {
1181 // Unique pointers, to guarantee there is only one pointer of a particular
1182 // structure.
1183 llvm::FoldingSetNodeID ID;
1184 PointerType::Profile(ID, T);
1185
1186 void *InsertPos = 0;
1187 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1188 return QualType(PT, 0);
1189
1190 // If the pointee type isn't canonical, this won't be a canonical type either,
1191 // so fill in the canonical type field.
1192 QualType Canonical;
1193 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001194 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +00001195
1196 // Get the new insert position for the node we care about.
1197 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001198 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 }
Steve Narofff83820b2009-01-27 22:08:43 +00001200 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001201 Types.push_back(New);
1202 PointerTypes.InsertNode(New, InsertPos);
1203 return QualType(New, 0);
1204}
1205
Steve Naroff5618bd42008-08-27 16:04:49 +00001206/// getBlockPointerType - Return the uniqued reference to the type for
1207/// a pointer to the specified block.
1208QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +00001209 assert(T->isFunctionType() && "block of function types only");
1210 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +00001211 // structure.
1212 llvm::FoldingSetNodeID ID;
1213 BlockPointerType::Profile(ID, T);
1214
1215 void *InsertPos = 0;
1216 if (BlockPointerType *PT =
1217 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1218 return QualType(PT, 0);
1219
Steve Naroff296e8d52008-08-28 19:20:44 +00001220 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +00001221 // type either so fill in the canonical type field.
1222 QualType Canonical;
1223 if (!T->isCanonical()) {
1224 Canonical = getBlockPointerType(getCanonicalType(T));
1225
1226 // Get the new insert position for the node we care about.
1227 BlockPointerType *NewIP =
1228 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001229 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +00001230 }
Steve Narofff83820b2009-01-27 22:08:43 +00001231 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +00001232 Types.push_back(New);
1233 BlockPointerTypes.InsertNode(New, InsertPos);
1234 return QualType(New, 0);
1235}
1236
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001237/// getLValueReferenceType - Return the uniqued reference to the type for an
1238/// lvalue reference to the specified type.
1239QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 // Unique pointers, to guarantee there is only one pointer of a particular
1241 // structure.
1242 llvm::FoldingSetNodeID ID;
1243 ReferenceType::Profile(ID, T);
1244
1245 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001246 if (LValueReferenceType *RT =
1247 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001249
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 // If the referencee type isn't canonical, this won't be a canonical type
1251 // either, so fill in the canonical type field.
1252 QualType Canonical;
1253 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001254 Canonical = getLValueReferenceType(getCanonicalType(T));
1255
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001257 LValueReferenceType *NewIP =
1258 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001259 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 }
1261
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001262 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001264 LValueReferenceTypes.InsertNode(New, InsertPos);
1265 return QualType(New, 0);
1266}
1267
1268/// getRValueReferenceType - Return the uniqued reference to the type for an
1269/// rvalue reference to the specified type.
1270QualType ASTContext::getRValueReferenceType(QualType T) {
1271 // Unique pointers, to guarantee there is only one pointer of a particular
1272 // structure.
1273 llvm::FoldingSetNodeID ID;
1274 ReferenceType::Profile(ID, T);
1275
1276 void *InsertPos = 0;
1277 if (RValueReferenceType *RT =
1278 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1279 return QualType(RT, 0);
1280
1281 // If the referencee type isn't canonical, this won't be a canonical type
1282 // either, so fill in the canonical type field.
1283 QualType Canonical;
1284 if (!T->isCanonical()) {
1285 Canonical = getRValueReferenceType(getCanonicalType(T));
1286
1287 // Get the new insert position for the node we care about.
1288 RValueReferenceType *NewIP =
1289 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1290 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1291 }
1292
1293 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1294 Types.push_back(New);
1295 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 return QualType(New, 0);
1297}
1298
Sebastian Redlf30208a2009-01-24 21:16:55 +00001299/// getMemberPointerType - Return the uniqued reference to the type for a
1300/// member pointer to the specified type, in the specified class.
1301QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1302{
1303 // Unique pointers, to guarantee there is only one pointer of a particular
1304 // structure.
1305 llvm::FoldingSetNodeID ID;
1306 MemberPointerType::Profile(ID, T, Cls);
1307
1308 void *InsertPos = 0;
1309 if (MemberPointerType *PT =
1310 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1311 return QualType(PT, 0);
1312
1313 // If the pointee or class type isn't canonical, this won't be a canonical
1314 // type either, so fill in the canonical type field.
1315 QualType Canonical;
1316 if (!T->isCanonical()) {
1317 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1318
1319 // Get the new insert position for the node we care about.
1320 MemberPointerType *NewIP =
1321 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1322 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1323 }
Steve Narofff83820b2009-01-27 22:08:43 +00001324 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001325 Types.push_back(New);
1326 MemberPointerTypes.InsertNode(New, InsertPos);
1327 return QualType(New, 0);
1328}
1329
Steve Narofffb22d962007-08-30 01:06:46 +00001330/// getConstantArrayType - Return the unique reference to the type for an
1331/// array of the specified element type.
1332QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001333 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001334 ArrayType::ArraySizeModifier ASM,
1335 unsigned EltTypeQuals) {
Eli Friedman587cbdf2009-05-29 20:17:55 +00001336 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1337 "Constant array of VLAs is illegal!");
1338
Chris Lattner38aeec72009-05-13 04:12:56 +00001339 // Convert the array size into a canonical width matching the pointer size for
1340 // the target.
1341 llvm::APInt ArySize(ArySizeIn);
1342 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1343
Reid Spencer5f016e22007-07-11 17:01:13 +00001344 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001345 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001346
1347 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001348 if (ConstantArrayType *ATP =
1349 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001350 return QualType(ATP, 0);
1351
1352 // If the element type isn't canonical, this won't be a canonical type either,
1353 // so fill in the canonical type field.
1354 QualType Canonical;
1355 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001356 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001357 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001358 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001359 ConstantArrayType *NewIP =
1360 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001361 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001362 }
1363
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001364 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001365 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001366 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001367 Types.push_back(New);
1368 return QualType(New, 0);
1369}
1370
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001371/// getConstantArrayWithExprType - Return a reference to the type for
1372/// an array of the specified element type.
1373QualType
1374ASTContext::getConstantArrayWithExprType(QualType EltTy,
1375 const llvm::APInt &ArySizeIn,
1376 Expr *ArySizeExpr,
1377 ArrayType::ArraySizeModifier ASM,
1378 unsigned EltTypeQuals,
1379 SourceRange Brackets) {
1380 // Convert the array size into a canonical width matching the pointer
1381 // size for the target.
1382 llvm::APInt ArySize(ArySizeIn);
1383 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1384
1385 // Compute the canonical ConstantArrayType.
1386 QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
1387 ArySize, ASM, EltTypeQuals);
1388 // Since we don't unique expressions, it isn't possible to unique VLA's
1389 // that have an expression provided for their size.
1390 ConstantArrayWithExprType *New =
1391 new(*this,8)ConstantArrayWithExprType(EltTy, Canonical,
1392 ArySize, ArySizeExpr,
1393 ASM, EltTypeQuals, Brackets);
1394 Types.push_back(New);
1395 return QualType(New, 0);
1396}
1397
1398/// getConstantArrayWithoutExprType - Return a reference to the type for
1399/// an array of the specified element type.
1400QualType
1401ASTContext::getConstantArrayWithoutExprType(QualType EltTy,
1402 const llvm::APInt &ArySizeIn,
1403 ArrayType::ArraySizeModifier ASM,
1404 unsigned EltTypeQuals) {
1405 // Convert the array size into a canonical width matching the pointer
1406 // size for the target.
1407 llvm::APInt ArySize(ArySizeIn);
1408 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1409
1410 // Compute the canonical ConstantArrayType.
1411 QualType Canonical = getConstantArrayType(getCanonicalType(EltTy),
1412 ArySize, ASM, EltTypeQuals);
1413 ConstantArrayWithoutExprType *New =
1414 new(*this,8)ConstantArrayWithoutExprType(EltTy, Canonical,
1415 ArySize, ASM, EltTypeQuals);
1416 Types.push_back(New);
1417 return QualType(New, 0);
1418}
1419
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001420/// getVariableArrayType - Returns a non-unique reference to the type for a
1421/// variable array of the specified element type.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001422QualType ASTContext::getVariableArrayType(QualType EltTy,
1423 Expr *NumElts,
Steve Naroffc9406122007-08-30 18:10:14 +00001424 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001425 unsigned EltTypeQuals,
1426 SourceRange Brackets) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001427 // Since we don't unique expressions, it isn't possible to unique VLA's
1428 // that have an expression provided for their size.
1429
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001430 VariableArrayType *New =
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001431 new(*this,8)VariableArrayType(EltTy, QualType(),
1432 NumElts, ASM, EltTypeQuals, Brackets);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001433
1434 VariableArrayTypes.push_back(New);
1435 Types.push_back(New);
1436 return QualType(New, 0);
1437}
1438
Douglas Gregor898574e2008-12-05 23:32:09 +00001439/// getDependentSizedArrayType - Returns a non-unique reference to
1440/// the type for a dependently-sized array of the specified element
1441/// type. FIXME: We will need these to be uniqued, or at least
1442/// comparable, at some point.
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001443QualType ASTContext::getDependentSizedArrayType(QualType EltTy,
1444 Expr *NumElts,
Douglas Gregor898574e2008-12-05 23:32:09 +00001445 ArrayType::ArraySizeModifier ASM,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001446 unsigned EltTypeQuals,
1447 SourceRange Brackets) {
Douglas Gregor898574e2008-12-05 23:32:09 +00001448 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1449 "Size must be type- or value-dependent!");
1450
1451 // Since we don't unique expressions, it isn't possible to unique
1452 // dependently-sized array types.
1453
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001454 DependentSizedArrayType *New =
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001455 new (*this,8) DependentSizedArrayType(EltTy, QualType(),
1456 NumElts, ASM, EltTypeQuals,
1457 Brackets);
Douglas Gregor898574e2008-12-05 23:32:09 +00001458
1459 DependentSizedArrayTypes.push_back(New);
1460 Types.push_back(New);
1461 return QualType(New, 0);
1462}
1463
Eli Friedmanc5773c42008-02-15 18:16:39 +00001464QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1465 ArrayType::ArraySizeModifier ASM,
1466 unsigned EltTypeQuals) {
1467 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001468 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001469
1470 void *InsertPos = 0;
1471 if (IncompleteArrayType *ATP =
1472 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1473 return QualType(ATP, 0);
1474
1475 // If the element type isn't canonical, this won't be a canonical type
1476 // either, so fill in the canonical type field.
1477 QualType Canonical;
1478
1479 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001480 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001481 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001482
1483 // Get the new insert position for the node we care about.
1484 IncompleteArrayType *NewIP =
1485 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001486 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001487 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001488
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00001489 IncompleteArrayType *New
1490 = new (*this,8) IncompleteArrayType(EltTy, Canonical,
1491 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001492
1493 IncompleteArrayTypes.InsertNode(New, InsertPos);
1494 Types.push_back(New);
1495 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001496}
1497
Steve Naroff73322922007-07-18 18:00:27 +00001498/// getVectorType - Return the unique reference to a vector type of
1499/// the specified element type and size. VectorType must be a built-in type.
1500QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001501 BuiltinType *baseType;
1502
Chris Lattnerf52ab252008-04-06 22:59:24 +00001503 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001504 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001505
1506 // Check if we've already instantiated a vector of this type.
1507 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001508 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001509 void *InsertPos = 0;
1510 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1511 return QualType(VTP, 0);
1512
1513 // If the element type isn't canonical, this won't be a canonical type either,
1514 // so fill in the canonical type field.
1515 QualType Canonical;
1516 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001517 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001518
1519 // Get the new insert position for the node we care about.
1520 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001521 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001522 }
Steve Narofff83820b2009-01-27 22:08:43 +00001523 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001524 VectorTypes.InsertNode(New, InsertPos);
1525 Types.push_back(New);
1526 return QualType(New, 0);
1527}
1528
Nate Begeman213541a2008-04-18 23:10:10 +00001529/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001530/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001531QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001532 BuiltinType *baseType;
1533
Chris Lattnerf52ab252008-04-06 22:59:24 +00001534 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001535 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001536
1537 // Check if we've already instantiated a vector of this type.
1538 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001539 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001540 void *InsertPos = 0;
1541 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1542 return QualType(VTP, 0);
1543
1544 // If the element type isn't canonical, this won't be a canonical type either,
1545 // so fill in the canonical type field.
1546 QualType Canonical;
1547 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001548 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001549
1550 // Get the new insert position for the node we care about.
1551 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001552 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001553 }
Steve Narofff83820b2009-01-27 22:08:43 +00001554 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001555 VectorTypes.InsertNode(New, InsertPos);
1556 Types.push_back(New);
1557 return QualType(New, 0);
1558}
1559
Douglas Gregor9cdda0c2009-06-17 21:51:59 +00001560QualType ASTContext::getDependentSizedExtVectorType(QualType vecType,
1561 Expr *SizeExpr,
1562 SourceLocation AttrLoc) {
1563 DependentSizedExtVectorType *New =
1564 new (*this,8) DependentSizedExtVectorType(vecType, QualType(),
1565 SizeExpr, AttrLoc);
1566
1567 DependentSizedExtVectorTypes.push_back(New);
1568 Types.push_back(New);
1569 return QualType(New, 0);
1570}
1571
Douglas Gregor72564e72009-02-26 23:50:07 +00001572/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001573///
Douglas Gregor72564e72009-02-26 23:50:07 +00001574QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001575 // Unique functions, to guarantee there is only one function of a particular
1576 // structure.
1577 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001578 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001579
1580 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001581 if (FunctionNoProtoType *FT =
1582 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001583 return QualType(FT, 0);
1584
1585 QualType Canonical;
1586 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001587 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001588
1589 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001590 FunctionNoProtoType *NewIP =
1591 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001592 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001593 }
1594
Douglas Gregor72564e72009-02-26 23:50:07 +00001595 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001596 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001597 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001598 return QualType(New, 0);
1599}
1600
1601/// getFunctionType - Return a normal function type with a typed argument
1602/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001603QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001604 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001605 unsigned TypeQuals, bool hasExceptionSpec,
1606 bool hasAnyExceptionSpec, unsigned NumExs,
1607 const QualType *ExArray) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001608 // Unique functions, to guarantee there is only one function of a particular
1609 // structure.
1610 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001611 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001612 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1613 NumExs, ExArray);
Reid Spencer5f016e22007-07-11 17:01:13 +00001614
1615 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001616 if (FunctionProtoType *FTP =
1617 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001618 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001619
1620 // Determine whether the type being created is already canonical or not.
Reid Spencer5f016e22007-07-11 17:01:13 +00001621 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl465226e2009-05-27 22:11:52 +00001622 if (hasExceptionSpec)
1623 isCanonical = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001624 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1625 if (!ArgArray[i]->isCanonical())
1626 isCanonical = false;
1627
1628 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001629 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 QualType Canonical;
1631 if (!isCanonical) {
1632 llvm::SmallVector<QualType, 16> CanonicalArgs;
1633 CanonicalArgs.reserve(NumArgs);
1634 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001635 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001636
Chris Lattnerf52ab252008-04-06 22:59:24 +00001637 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001638 CanonicalArgs.data(), NumArgs,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001639 isVariadic, TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +00001640
Reid Spencer5f016e22007-07-11 17:01:13 +00001641 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001642 FunctionProtoType *NewIP =
1643 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001644 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001645 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001646
Douglas Gregor72564e72009-02-26 23:50:07 +00001647 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001648 // for two variable size arrays (for parameter and exception types) at the
1649 // end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001650 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001651 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1652 NumArgs*sizeof(QualType) +
1653 NumExs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001654 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001655 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1656 ExArray, NumExs, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001658 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001659 return QualType(FTP, 0);
1660}
1661
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001662/// getTypeDeclType - Return the unique reference to the type for the
1663/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001664QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001665 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001666 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1667
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001668 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001669 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001670 else if (isa<TemplateTypeParmDecl>(Decl)) {
1671 assert(false && "Template type parameter types are always available.");
1672 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001673 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001674
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001675 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001676 if (PrevDecl)
1677 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001678 else
1679 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001680 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001681 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1682 if (PrevDecl)
1683 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001684 else
1685 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001686 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001687 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001688 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001689
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001690 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001691 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001692}
1693
Reid Spencer5f016e22007-07-11 17:01:13 +00001694/// getTypedefType - Return the unique reference to the type for the
1695/// specified typename decl.
1696QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1697 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1698
Chris Lattnerf52ab252008-04-06 22:59:24 +00001699 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001700 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001701 Types.push_back(Decl->TypeForDecl);
1702 return QualType(Decl->TypeForDecl, 0);
1703}
1704
Douglas Gregorfab9d672009-02-05 23:33:38 +00001705/// \brief Retrieve the template type parameter type for a template
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001706/// parameter or parameter pack with the given depth, index, and (optionally)
1707/// name.
Douglas Gregorfab9d672009-02-05 23:33:38 +00001708QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001709 bool ParameterPack,
Douglas Gregorfab9d672009-02-05 23:33:38 +00001710 IdentifierInfo *Name) {
1711 llvm::FoldingSetNodeID ID;
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001712 TemplateTypeParmType::Profile(ID, Depth, Index, ParameterPack, Name);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001713 void *InsertPos = 0;
1714 TemplateTypeParmType *TypeParm
1715 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1716
1717 if (TypeParm)
1718 return QualType(TypeParm, 0);
1719
Anders Carlsson76e4ce42009-06-16 00:30:48 +00001720 if (Name) {
1721 QualType Canon = getTemplateTypeParmType(Depth, Index, ParameterPack);
1722 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack,
1723 Name, Canon);
1724 } else
1725 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, ParameterPack);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001726
1727 Types.push_back(TypeParm);
1728 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1729
1730 return QualType(TypeParm, 0);
1731}
1732
Douglas Gregor55f6b142009-02-09 18:46:07 +00001733QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001734ASTContext::getTemplateSpecializationType(TemplateName Template,
1735 const TemplateArgument *Args,
1736 unsigned NumArgs,
1737 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001738 if (!Canon.isNull())
1739 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001740
Douglas Gregor55f6b142009-02-09 18:46:07 +00001741 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001742 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001743
Douglas Gregor55f6b142009-02-09 18:46:07 +00001744 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001745 TemplateSpecializationType *Spec
1746 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001747
1748 if (Spec)
1749 return QualType(Spec, 0);
1750
Douglas Gregor7532dc62009-03-30 22:58:21 +00001751 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001752 sizeof(TemplateArgument) * NumArgs),
1753 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001754 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001755 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001756 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001757
1758 return QualType(Spec, 0);
1759}
1760
Douglas Gregore4e5b052009-03-19 00:18:19 +00001761QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001762ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001763 QualType NamedType) {
1764 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001765 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001766
1767 void *InsertPos = 0;
1768 QualifiedNameType *T
1769 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1770 if (T)
1771 return QualType(T, 0);
1772
Douglas Gregorab452ba2009-03-26 23:50:42 +00001773 T = new (*this) QualifiedNameType(NNS, NamedType,
1774 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001775 Types.push_back(T);
1776 QualifiedNameTypes.InsertNode(T, InsertPos);
1777 return QualType(T, 0);
1778}
1779
Douglas Gregord57959a2009-03-27 23:10:48 +00001780QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1781 const IdentifierInfo *Name,
1782 QualType Canon) {
1783 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1784
1785 if (Canon.isNull()) {
1786 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1787 if (CanonNNS != NNS)
1788 Canon = getTypenameType(CanonNNS, Name);
1789 }
1790
1791 llvm::FoldingSetNodeID ID;
1792 TypenameType::Profile(ID, NNS, Name);
1793
1794 void *InsertPos = 0;
1795 TypenameType *T
1796 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1797 if (T)
1798 return QualType(T, 0);
1799
1800 T = new (*this) TypenameType(NNS, Name, Canon);
1801 Types.push_back(T);
1802 TypenameTypes.InsertNode(T, InsertPos);
1803 return QualType(T, 0);
1804}
1805
Douglas Gregor17343172009-04-01 00:28:59 +00001806QualType
1807ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1808 const TemplateSpecializationType *TemplateId,
1809 QualType Canon) {
1810 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1811
1812 if (Canon.isNull()) {
1813 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1814 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1815 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1816 const TemplateSpecializationType *CanonTemplateId
1817 = CanonType->getAsTemplateSpecializationType();
1818 assert(CanonTemplateId &&
1819 "Canonical type must also be a template specialization type");
1820 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1821 }
1822 }
1823
1824 llvm::FoldingSetNodeID ID;
1825 TypenameType::Profile(ID, NNS, TemplateId);
1826
1827 void *InsertPos = 0;
1828 TypenameType *T
1829 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1830 if (T)
1831 return QualType(T, 0);
1832
1833 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1834 Types.push_back(T);
1835 TypenameTypes.InsertNode(T, InsertPos);
1836 return QualType(T, 0);
1837}
1838
Chris Lattner88cb27a2008-04-07 04:56:42 +00001839/// CmpProtocolNames - Comparison predicate for sorting protocols
1840/// alphabetically.
1841static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1842 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001843 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001844}
1845
1846static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1847 unsigned &NumProtocols) {
1848 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1849
1850 // Sort protocols, keyed by name.
1851 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1852
1853 // Remove duplicates.
1854 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1855 NumProtocols = ProtocolsEnd-Protocols;
1856}
1857
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001858/// getObjCObjectPointerType - Return a ObjCObjectPointerType type for
1859/// the given interface decl and the conforming protocol list.
Steve Naroff14108da2009-07-10 23:34:53 +00001860QualType ASTContext::getObjCObjectPointerType(QualType InterfaceT,
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001861 ObjCProtocolDecl **Protocols,
1862 unsigned NumProtocols) {
Steve Naroff14108da2009-07-10 23:34:53 +00001863 if (InterfaceT.isNull())
Steve Naroffde2e22d2009-07-15 18:40:39 +00001864 InterfaceT = ObjCBuiltinIdTy;
Steve Naroff14108da2009-07-10 23:34:53 +00001865
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001866 // Sort the protocol list alphabetically to canonicalize it.
1867 if (NumProtocols)
1868 SortAndUniqueProtocols(Protocols, NumProtocols);
1869
1870 llvm::FoldingSetNodeID ID;
Steve Naroff14108da2009-07-10 23:34:53 +00001871 ObjCObjectPointerType::Profile(ID, InterfaceT, Protocols, NumProtocols);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001872
1873 void *InsertPos = 0;
1874 if (ObjCObjectPointerType *QT =
1875 ObjCObjectPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1876 return QualType(QT, 0);
1877
1878 // No Match;
1879 ObjCObjectPointerType *QType =
Steve Naroff14108da2009-07-10 23:34:53 +00001880 new (*this,8) ObjCObjectPointerType(InterfaceT, Protocols, NumProtocols);
Steve Naroffd1b3c2d2009-06-17 22:40:22 +00001881
1882 Types.push_back(QType);
1883 ObjCObjectPointerTypes.InsertNode(QType, InsertPos);
1884 return QualType(QType, 0);
1885}
Chris Lattner88cb27a2008-04-07 04:56:42 +00001886
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001887/// getObjCInterfaceType - Return the unique reference to the type for the
1888/// specified ObjC interface decl. The list of protocols is optional.
1889QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl,
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001890 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001891 if (NumProtocols)
1892 // Sort the protocol list alphabetically to canonicalize it.
1893 SortAndUniqueProtocols(Protocols, NumProtocols);
Chris Lattner88cb27a2008-04-07 04:56:42 +00001894
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001895 llvm::FoldingSetNodeID ID;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001896 ObjCInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001897
1898 void *InsertPos = 0;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001899 if (ObjCInterfaceType *QT =
1900 ObjCInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001901 return QualType(QT, 0);
1902
1903 // No Match;
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001904 ObjCInterfaceType *QType =
1905 new (*this,8) ObjCInterfaceType(const_cast<ObjCInterfaceDecl*>(Decl),
1906 Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001907 Types.push_back(QType);
Steve Naroffc15cb2a2009-07-18 15:33:26 +00001908 ObjCInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001909 return QualType(QType, 0);
1910}
1911
Douglas Gregor72564e72009-02-26 23:50:07 +00001912/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1913/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001914/// multiple declarations that refer to "typeof(x)" all contain different
1915/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1916/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001917QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00001918 TypeOfExprType *toe;
1919 if (tofExpr->isTypeDependent())
1920 toe = new (*this, 8) TypeOfExprType(tofExpr);
1921 else {
1922 QualType Canonical = getCanonicalType(tofExpr->getType());
1923 toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
1924 }
Steve Naroff9752f252007-08-01 18:02:17 +00001925 Types.push_back(toe);
1926 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001927}
1928
Steve Naroff9752f252007-08-01 18:02:17 +00001929/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1930/// TypeOfType AST's. The only motivation to unique these nodes would be
1931/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1932/// an issue. This doesn't effect the type checker, since it operates
1933/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001934QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001935 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001936 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001937 Types.push_back(tot);
1938 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001939}
1940
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00001941/// getDecltypeForExpr - Given an expr, will return the decltype for that
1942/// expression, according to the rules in C++0x [dcl.type.simple]p4
1943static QualType getDecltypeForExpr(const Expr *e, ASTContext &Context) {
Anders Carlssona07c33e2009-06-25 15:00:34 +00001944 if (e->isTypeDependent())
1945 return Context.DependentTy;
1946
Anders Carlsson60a9a2a2009-06-24 21:24:56 +00001947 // If e is an id expression or a class member access, decltype(e) is defined
1948 // as the type of the entity named by e.
1949 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(e)) {
1950 if (const ValueDecl *VD = dyn_cast<ValueDecl>(DRE->getDecl()))
1951 return VD->getType();
1952 }
1953 if (const MemberExpr *ME = dyn_cast<MemberExpr>(e)) {
1954 if (const FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl()))
1955 return FD->getType();
1956 }
1957 // If e is a function call or an invocation of an overloaded operator,
1958 // (parentheses around e are ignored), decltype(e) is defined as the
1959 // return type of that function.
1960 if (const CallExpr *CE = dyn_cast<CallExpr>(e->IgnoreParens()))
1961 return CE->getCallReturnType();
1962
1963 QualType T = e->getType();
1964
1965 // Otherwise, where T is the type of e, if e is an lvalue, decltype(e) is
1966 // defined as T&, otherwise decltype(e) is defined as T.
1967 if (e->isLvalue(Context) == Expr::LV_Valid)
1968 T = Context.getLValueReferenceType(T);
1969
1970 return T;
1971}
1972
Anders Carlsson395b4752009-06-24 19:06:50 +00001973/// getDecltypeType - Unlike many "get<Type>" functions, we don't unique
1974/// DecltypeType AST's. The only motivation to unique these nodes would be
1975/// memory savings. Since decltype(t) is fairly uncommon, space shouldn't be
1976/// an issue. This doesn't effect the type checker, since it operates
1977/// on canonical type's (which are always unique).
1978QualType ASTContext::getDecltypeType(Expr *e) {
Douglas Gregordd0257c2009-07-08 00:03:05 +00001979 DecltypeType *dt;
1980 if (e->isTypeDependent()) // FIXME: canonicalize the expression
Anders Carlsson563a03b2009-07-10 19:20:26 +00001981 dt = new (*this, 8) DecltypeType(e, DependentTy);
Douglas Gregordd0257c2009-07-08 00:03:05 +00001982 else {
1983 QualType T = getDecltypeForExpr(e, *this);
Anders Carlsson563a03b2009-07-10 19:20:26 +00001984 dt = new (*this, 8) DecltypeType(e, T, getCanonicalType(T));
Douglas Gregordd0257c2009-07-08 00:03:05 +00001985 }
Anders Carlsson395b4752009-06-24 19:06:50 +00001986 Types.push_back(dt);
1987 return QualType(dt, 0);
1988}
1989
Reid Spencer5f016e22007-07-11 17:01:13 +00001990/// getTagDeclType - Return the unique reference to the type for the
1991/// specified TagDecl (struct/union/class/enum) decl.
1992QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001993 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001994 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001995}
1996
1997/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1998/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1999/// needs to agree with the definition in <stddef.h>.
2000QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002001 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00002002}
2003
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00002004/// getSignedWCharType - Return the type of "signed wchar_t".
2005/// Used when in C++, as a GCC extension.
2006QualType ASTContext::getSignedWCharType() const {
2007 // FIXME: derive from "Target" ?
2008 return WCharTy;
2009}
2010
2011/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
2012/// Used when in C++, as a GCC extension.
2013QualType ASTContext::getUnsignedWCharType() const {
2014 // FIXME: derive from "Target" ?
2015 return UnsignedIntTy;
2016}
2017
Chris Lattner8b9023b2007-07-13 03:05:23 +00002018/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
2019/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
2020QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002021 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00002022}
2023
Chris Lattnere6327742008-04-02 05:18:44 +00002024//===----------------------------------------------------------------------===//
2025// Type Operators
2026//===----------------------------------------------------------------------===//
2027
Chris Lattner77c96472008-04-06 22:41:35 +00002028/// getCanonicalType - Return the canonical (structural) type corresponding to
2029/// the specified potentially non-canonical type. The non-canonical version
2030/// of a type may have many "decorated" versions of types. Decorators can
2031/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
2032/// to be free of any of these, allowing two canonical types to be compared
2033/// for exact equality with a simple pointer comparison.
2034QualType ASTContext::getCanonicalType(QualType T) {
2035 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002036
2037 // If the result has type qualifiers, make sure to canonicalize them as well.
2038 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
2039 if (TypeQuals == 0) return CanType;
2040
2041 // If the type qualifiers are on an array type, get the canonical type of the
2042 // array with the qualifiers applied to the element type.
2043 ArrayType *AT = dyn_cast<ArrayType>(CanType);
2044 if (!AT)
2045 return CanType.getQualifiedType(TypeQuals);
2046
2047 // Get the canonical version of the element with the extra qualifiers on it.
2048 // This can recursively sink qualifiers through multiple levels of arrays.
2049 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
2050 NewEltTy = getCanonicalType(NewEltTy);
2051
2052 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2053 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
2054 CAT->getIndexTypeQualifier());
2055 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
2056 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
2057 IAT->getIndexTypeQualifier());
2058
Douglas Gregor898574e2008-12-05 23:32:09 +00002059 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002060 return getDependentSizedArrayType(NewEltTy,
2061 DSAT->getSizeExpr(),
Douglas Gregor898574e2008-12-05 23:32:09 +00002062 DSAT->getSizeModifier(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002063 DSAT->getIndexTypeQualifier(),
2064 DSAT->getBracketsRange());
Douglas Gregor898574e2008-12-05 23:32:09 +00002065
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002066 VariableArrayType *VAT = cast<VariableArrayType>(AT);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002067 return getVariableArrayType(NewEltTy,
2068 VAT->getSizeExpr(),
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002069 VAT->getSizeModifier(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002070 VAT->getIndexTypeQualifier(),
2071 VAT->getBracketsRange());
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002072}
2073
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002074TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
2075 // If this template name refers to a template, the canonical
2076 // template name merely stores the template itself.
2077 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Argyrios Kyrtzidis97fbaa22009-07-18 00:34:25 +00002078 return TemplateName(cast<TemplateDecl>(Template->getCanonicalDecl()));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00002079
2080 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
2081 assert(DTN && "Non-dependent template names must refer to template decls.");
2082 return DTN->CanonicalTemplateName;
2083}
2084
Douglas Gregord57959a2009-03-27 23:10:48 +00002085NestedNameSpecifier *
2086ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
2087 if (!NNS)
2088 return 0;
2089
2090 switch (NNS->getKind()) {
2091 case NestedNameSpecifier::Identifier:
2092 // Canonicalize the prefix but keep the identifier the same.
2093 return NestedNameSpecifier::Create(*this,
2094 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
2095 NNS->getAsIdentifier());
2096
2097 case NestedNameSpecifier::Namespace:
2098 // A namespace is canonical; build a nested-name-specifier with
2099 // this namespace and no prefix.
2100 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
2101
2102 case NestedNameSpecifier::TypeSpec:
2103 case NestedNameSpecifier::TypeSpecWithTemplate: {
2104 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
2105 NestedNameSpecifier *Prefix = 0;
2106
2107 // FIXME: This isn't the right check!
2108 if (T->isDependentType())
2109 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
2110
2111 return NestedNameSpecifier::Create(*this, Prefix,
2112 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
2113 T.getTypePtr());
2114 }
2115
2116 case NestedNameSpecifier::Global:
2117 // The global specifier is canonical and unique.
2118 return NNS;
2119 }
2120
2121 // Required to silence a GCC warning
2122 return 0;
2123}
2124
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002125
2126const ArrayType *ASTContext::getAsArrayType(QualType T) {
2127 // Handle the non-qualified case efficiently.
2128 if (T.getCVRQualifiers() == 0) {
2129 // Handle the common positive case fast.
2130 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
2131 return AT;
2132 }
2133
2134 // Handle the common negative case fast, ignoring CVR qualifiers.
2135 QualType CType = T->getCanonicalTypeInternal();
2136
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00002137 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002138 // test.
2139 if (!isa<ArrayType>(CType) &&
2140 !isa<ArrayType>(CType.getUnqualifiedType()))
2141 return 0;
2142
2143 // Apply any CVR qualifiers from the array type to the element type. This
2144 // implements C99 6.7.3p8: "If the specification of an array type includes
2145 // any type qualifiers, the element type is so qualified, not the array type."
2146
2147 // If we get here, we either have type qualifiers on the type, or we have
2148 // sugar such as a typedef in the way. If we have type qualifiers on the type
2149 // we must propagate them down into the elemeng type.
2150 unsigned CVRQuals = T.getCVRQualifiers();
2151 unsigned AddrSpace = 0;
2152 Type *Ty = T.getTypePtr();
2153
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00002154 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002155 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00002156 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
2157 AddrSpace = EXTQT->getAddressSpace();
2158 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002159 } else {
2160 T = Ty->getDesugaredType();
2161 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
2162 break;
2163 CVRQuals |= T.getCVRQualifiers();
2164 Ty = T.getTypePtr();
2165 }
2166 }
2167
2168 // If we have a simple case, just return now.
2169 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
2170 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
2171 return ATy;
2172
2173 // Otherwise, we have an array and we have qualifiers on it. Push the
2174 // qualifiers into the array element type and return a new array type.
2175 // Get the canonical version of the element with the extra qualifiers on it.
2176 // This can recursively sink qualifiers through multiple levels of arrays.
2177 QualType NewEltTy = ATy->getElementType();
2178 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00002179 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002180 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
2181
2182 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
2183 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
2184 CAT->getSizeModifier(),
2185 CAT->getIndexTypeQualifier()));
2186 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
2187 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
2188 IAT->getSizeModifier(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002189 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00002190
Douglas Gregor898574e2008-12-05 23:32:09 +00002191 if (const DependentSizedArrayType *DSAT
2192 = dyn_cast<DependentSizedArrayType>(ATy))
2193 return cast<ArrayType>(
2194 getDependentSizedArrayType(NewEltTy,
2195 DSAT->getSizeExpr(),
2196 DSAT->getSizeModifier(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002197 DSAT->getIndexTypeQualifier(),
2198 DSAT->getBracketsRange()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002199
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002200 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002201 return cast<ArrayType>(getVariableArrayType(NewEltTy,
2202 VAT->getSizeExpr(),
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002203 VAT->getSizeModifier(),
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00002204 VAT->getIndexTypeQualifier(),
2205 VAT->getBracketsRange()));
Chris Lattner77c96472008-04-06 22:41:35 +00002206}
2207
2208
Chris Lattnere6327742008-04-02 05:18:44 +00002209/// getArrayDecayedType - Return the properly qualified result of decaying the
2210/// specified array type to a pointer. This operation is non-trivial when
2211/// handling typedefs etc. The canonical type of "T" must be an array type,
2212/// this returns a pointer to a properly qualified element of the array.
2213///
2214/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
2215QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002216 // Get the element type with 'getAsArrayType' so that we don't lose any
2217 // typedefs in the element type of the array. This also handles propagation
2218 // of type qualifiers from the array type into the element type if present
2219 // (C99 6.7.3p8).
2220 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
2221 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00002222
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002223 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00002224
2225 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002226 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00002227}
2228
Daniel Dunbard786f6a2009-01-05 22:14:37 +00002229QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00002230 QualType ElemTy = VAT->getElementType();
2231
2232 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
2233 return getBaseElementType(VAT);
2234
2235 return ElemTy;
2236}
2237
Reid Spencer5f016e22007-07-11 17:01:13 +00002238/// getFloatingRank - Return a relative rank for floating point types.
2239/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00002240static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00002241 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00002242 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00002243
Daniel Dunbard786f6a2009-01-05 22:14:37 +00002244 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00002245 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00002246 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00002247 case BuiltinType::Float: return FloatRank;
2248 case BuiltinType::Double: return DoubleRank;
2249 case BuiltinType::LongDouble: return LongDoubleRank;
2250 }
2251}
2252
Steve Naroff716c7302007-08-27 01:41:48 +00002253/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
2254/// point or a complex type (based on typeDomain/typeSize).
2255/// 'typeDomain' is a real floating point or complex type.
2256/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00002257QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
2258 QualType Domain) const {
2259 FloatingRank EltRank = getFloatingRank(Size);
2260 if (Domain->isComplexType()) {
2261 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00002262 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00002263 case FloatRank: return FloatComplexTy;
2264 case DoubleRank: return DoubleComplexTy;
2265 case LongDoubleRank: return LongDoubleComplexTy;
2266 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002267 }
Chris Lattner1361b112008-04-06 23:58:54 +00002268
2269 assert(Domain->isRealFloatingType() && "Unknown domain!");
2270 switch (EltRank) {
2271 default: assert(0 && "getFloatingRank(): illegal value for rank");
2272 case FloatRank: return FloatTy;
2273 case DoubleRank: return DoubleTy;
2274 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00002275 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002276}
2277
Chris Lattner7cfeb082008-04-06 23:55:33 +00002278/// getFloatingTypeOrder - Compare the rank of the two specified floating
2279/// point types, ignoring the domain of the type (i.e. 'double' ==
2280/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
2281/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00002282int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
2283 FloatingRank LHSR = getFloatingRank(LHS);
2284 FloatingRank RHSR = getFloatingRank(RHS);
2285
2286 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002287 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00002288 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00002289 return 1;
2290 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002291}
2292
Chris Lattnerf52ab252008-04-06 22:59:24 +00002293/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
2294/// routine will assert if passed a built-in type that isn't an integer or enum,
2295/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00002296unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002297 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00002298 if (EnumType* ET = dyn_cast<EnumType>(T))
2299 T = ET->getDecl()->getIntegerType().getTypePtr();
2300
Eli Friedmana3426752009-07-05 23:44:27 +00002301 if (T->isSpecificBuiltinType(BuiltinType::WChar))
2302 T = getFromTargetType(Target.getWCharType()).getTypePtr();
2303
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002304 if (T->isSpecificBuiltinType(BuiltinType::Char16))
2305 T = getFromTargetType(Target.getChar16Type()).getTypePtr();
2306
2307 if (T->isSpecificBuiltinType(BuiltinType::Char32))
2308 T = getFromTargetType(Target.getChar32Type()).getTypePtr();
2309
Eli Friedmanf98aba32009-02-13 02:31:07 +00002310 // There are two things which impact the integer rank: the width, and
2311 // the ordering of builtins. The builtin ordering is encoded in the
2312 // bottom three bits; the width is encoded in the bits above that.
Chris Lattner1b63e4f2009-06-14 01:54:56 +00002313 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T))
Eli Friedmanf98aba32009-02-13 02:31:07 +00002314 return FWIT->getWidth() << 3;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002315
Chris Lattnerf52ab252008-04-06 22:59:24 +00002316 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00002317 default: assert(0 && "getIntegerRank(): not a built-in integer");
2318 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002319 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002320 case BuiltinType::Char_S:
2321 case BuiltinType::Char_U:
2322 case BuiltinType::SChar:
2323 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002324 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002325 case BuiltinType::Short:
2326 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002327 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002328 case BuiltinType::Int:
2329 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002330 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002331 case BuiltinType::Long:
2332 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002333 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00002334 case BuiltinType::LongLong:
2335 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00002336 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00002337 case BuiltinType::Int128:
2338 case BuiltinType::UInt128:
2339 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00002340 }
2341}
2342
Chris Lattner7cfeb082008-04-06 23:55:33 +00002343/// getIntegerTypeOrder - Returns the highest ranked integer type:
2344/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
2345/// LHS < RHS, return -1.
2346int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00002347 Type *LHSC = getCanonicalType(LHS).getTypePtr();
2348 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00002349 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00002350
Chris Lattnerf52ab252008-04-06 22:59:24 +00002351 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
2352 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00002353
Chris Lattner7cfeb082008-04-06 23:55:33 +00002354 unsigned LHSRank = getIntegerRank(LHSC);
2355 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00002356
Chris Lattner7cfeb082008-04-06 23:55:33 +00002357 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
2358 if (LHSRank == RHSRank) return 0;
2359 return LHSRank > RHSRank ? 1 : -1;
2360 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002361
Chris Lattner7cfeb082008-04-06 23:55:33 +00002362 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
2363 if (LHSUnsigned) {
2364 // If the unsigned [LHS] type is larger, return it.
2365 if (LHSRank >= RHSRank)
2366 return 1;
2367
2368 // If the signed type can represent all values of the unsigned type, it
2369 // wins. Because we are dealing with 2's complement and types that are
2370 // powers of two larger than each other, this is always safe.
2371 return -1;
2372 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002373
Chris Lattner7cfeb082008-04-06 23:55:33 +00002374 // If the unsigned [RHS] type is larger, return it.
2375 if (RHSRank >= LHSRank)
2376 return -1;
2377
2378 // If the signed type can represent all values of the unsigned type, it
2379 // wins. Because we are dealing with 2's complement and types that are
2380 // powers of two larger than each other, this is always safe.
2381 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002382}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002383
2384// getCFConstantStringType - Return the type used for constant CFStrings.
2385QualType ASTContext::getCFConstantStringType() {
2386 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002387 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002388 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002389 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002390 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00002391
2392 // const int *isa;
2393 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002394 // int flags;
2395 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002396 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002397 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00002398 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002399 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00002400
Anders Carlsson71993dd2007-08-17 05:31:46 +00002401 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002402 for (unsigned i = 0; i < 4; ++i) {
2403 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2404 SourceLocation(), 0,
2405 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002406 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002407 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002408 }
2409
2410 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002411 }
2412
2413 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002414}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002415
Douglas Gregor319ac892009-04-23 22:29:11 +00002416void ASTContext::setCFConstantStringType(QualType T) {
Ted Kremenek35366a62009-07-17 17:50:17 +00002417 const RecordType *Rec = T->getAsRecordType();
Douglas Gregor319ac892009-04-23 22:29:11 +00002418 assert(Rec && "Invalid CFConstantStringType");
2419 CFConstantStringTypeDecl = Rec->getDecl();
2420}
2421
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002422QualType ASTContext::getObjCFastEnumerationStateType()
2423{
2424 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002425 ObjCFastEnumerationStateTypeDecl =
2426 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2427 &Idents.get("__objcFastEnumerationState"));
2428
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002429 QualType FieldTypes[] = {
2430 UnsignedLongTy,
Steve Naroffde2e22d2009-07-15 18:40:39 +00002431 getPointerType(ObjCIdTypedefType),
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002432 getPointerType(UnsignedLongTy),
2433 getConstantArrayType(UnsignedLongTy,
2434 llvm::APInt(32, 5), ArrayType::Normal, 0)
2435 };
2436
Douglas Gregor44b43212008-12-11 16:49:14 +00002437 for (size_t i = 0; i < 4; ++i) {
2438 FieldDecl *Field = FieldDecl::Create(*this,
2439 ObjCFastEnumerationStateTypeDecl,
2440 SourceLocation(), 0,
2441 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002442 /*Mutable=*/false);
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002443 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002444 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002445
Douglas Gregor44b43212008-12-11 16:49:14 +00002446 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002447 }
2448
2449 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2450}
2451
Douglas Gregor319ac892009-04-23 22:29:11 +00002452void ASTContext::setObjCFastEnumerationStateType(QualType T) {
Ted Kremenek35366a62009-07-17 17:50:17 +00002453 const RecordType *Rec = T->getAsRecordType();
Douglas Gregor319ac892009-04-23 22:29:11 +00002454 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2455 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2456}
2457
Anders Carlssone8c49532007-10-29 06:33:42 +00002458// This returns true if a type has been typedefed to BOOL:
2459// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002460static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002461 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002462 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2463 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002464
2465 return false;
2466}
2467
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002468/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002469/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002470int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002471 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002472
2473 // Make all integer and enum types at least as large as an int
2474 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002475 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002476 // Treat arrays as pointers, since that's how they're passed in.
2477 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002478 sz = getTypeSize(VoidPtrTy);
2479 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002480}
2481
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002482/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002483/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002484void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002485 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002486 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002487 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002488 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002489 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002490 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002491 // Compute size of all parameters.
2492 // Start with computing size of a pointer in number of bytes.
2493 // FIXME: There might(should) be a better way of doing this computation!
2494 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002495 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002496 // The first two arguments (self and _cmd) are pointers; account for
2497 // their size.
2498 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002499 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2500 E = Decl->param_end(); PI != E; ++PI) {
2501 QualType PType = (*PI)->getType();
2502 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002503 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002504 ParmOffset += sz;
2505 }
2506 S += llvm::utostr(ParmOffset);
2507 S += "@0:";
2508 S += llvm::utostr(PtrSize);
2509
2510 // Argument types.
2511 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002512 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2513 E = Decl->param_end(); PI != E; ++PI) {
2514 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002515 QualType PType = PVDecl->getOriginalType();
2516 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002517 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2518 // Use array's original type only if it has known number of
2519 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002520 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002521 PType = PVDecl->getType();
2522 } else if (PType->isFunctionType())
2523 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002524 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002525 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002526 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002527 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002528 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002529 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002530 }
2531}
2532
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002533/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002534/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002535/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2536/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002537/// Property attributes are stored as a comma-delimited C string. The simple
2538/// attributes readonly and bycopy are encoded as single characters. The
2539/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2540/// encoded as single characters, followed by an identifier. Property types
2541/// are also encoded as a parametrized attribute. The characters used to encode
2542/// these attributes are defined by the following enumeration:
2543/// @code
2544/// enum PropertyAttributes {
2545/// kPropertyReadOnly = 'R', // property is read-only.
2546/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2547/// kPropertyByref = '&', // property is a reference to the value last assigned
2548/// kPropertyDynamic = 'D', // property is dynamic
2549/// kPropertyGetter = 'G', // followed by getter selector name
2550/// kPropertySetter = 'S', // followed by setter selector name
2551/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2552/// kPropertyType = 't' // followed by old-style type encoding.
2553/// kPropertyWeak = 'W' // 'weak' property
2554/// kPropertyStrong = 'P' // property GC'able
2555/// kPropertyNonAtomic = 'N' // property non-atomic
2556/// };
2557/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002558void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2559 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002560 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002561 // Collect information from the property implementation decl(s).
2562 bool Dynamic = false;
2563 ObjCPropertyImplDecl *SynthesizePID = 0;
2564
2565 // FIXME: Duplicated code due to poor abstraction.
2566 if (Container) {
2567 if (const ObjCCategoryImplDecl *CID =
2568 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2569 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002570 i = CID->propimpl_begin(), e = CID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002571 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002572 ObjCPropertyImplDecl *PID = *i;
2573 if (PID->getPropertyDecl() == PD) {
2574 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2575 Dynamic = true;
2576 } else {
2577 SynthesizePID = PID;
2578 }
2579 }
2580 }
2581 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002582 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002583 for (ObjCCategoryImplDecl::propimpl_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002584 i = OID->propimpl_begin(), e = OID->propimpl_end();
Douglas Gregor653f1b12009-04-23 01:02:12 +00002585 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002586 ObjCPropertyImplDecl *PID = *i;
2587 if (PID->getPropertyDecl() == PD) {
2588 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2589 Dynamic = true;
2590 } else {
2591 SynthesizePID = PID;
2592 }
2593 }
2594 }
2595 }
2596 }
2597
2598 // FIXME: This is not very efficient.
2599 S = "T";
2600
2601 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002602 // GCC has some special rules regarding encoding of properties which
2603 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002604 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002605 true /* outermost type */,
2606 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002607
2608 if (PD->isReadOnly()) {
2609 S += ",R";
2610 } else {
2611 switch (PD->getSetterKind()) {
2612 case ObjCPropertyDecl::Assign: break;
2613 case ObjCPropertyDecl::Copy: S += ",C"; break;
2614 case ObjCPropertyDecl::Retain: S += ",&"; break;
2615 }
2616 }
2617
2618 // It really isn't clear at all what this means, since properties
2619 // are "dynamic by default".
2620 if (Dynamic)
2621 S += ",D";
2622
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002623 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2624 S += ",N";
2625
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002626 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2627 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002628 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002629 }
2630
2631 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2632 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002633 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002634 }
2635
2636 if (SynthesizePID) {
2637 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2638 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002639 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002640 }
2641
2642 // FIXME: OBJCGC: weak & strong
2643}
2644
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002645/// getLegacyIntegralTypeEncoding -
2646/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002647/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002648/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2649///
2650void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2651 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2652 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002653 if (BT->getKind() == BuiltinType::ULong &&
2654 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002655 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002656 else
2657 if (BT->getKind() == BuiltinType::Long &&
2658 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002659 PointeeTy = IntTy;
2660 }
2661 }
2662}
2663
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002664void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002665 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002666 // We follow the behavior of gcc, expanding structures which are
2667 // directly pointed to, and expanding embedded structures. Note that
2668 // these rules are sufficient to prevent recursive encoding of the
2669 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002670 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2671 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002672}
2673
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002674static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002675 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002676 const Expr *E = FD->getBitWidth();
2677 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2678 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002679 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002680 S += 'b';
2681 S += llvm::utostr(N);
2682}
2683
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002684void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2685 bool ExpandPointedToStructures,
2686 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002687 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002688 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002689 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002690 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002691 if (FD && FD->isBitField())
2692 return EncodeBitField(this, S, FD);
2693 char encoding;
2694 switch (BT->getKind()) {
2695 default: assert(0 && "Unhandled builtin type kind");
2696 case BuiltinType::Void: encoding = 'v'; break;
2697 case BuiltinType::Bool: encoding = 'B'; break;
2698 case BuiltinType::Char_U:
2699 case BuiltinType::UChar: encoding = 'C'; break;
2700 case BuiltinType::UShort: encoding = 'S'; break;
2701 case BuiltinType::UInt: encoding = 'I'; break;
2702 case BuiltinType::ULong:
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002703 encoding =
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002704 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002705 break;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002706 case BuiltinType::UInt128: encoding = 'T'; break;
2707 case BuiltinType::ULongLong: encoding = 'Q'; break;
2708 case BuiltinType::Char_S:
2709 case BuiltinType::SChar: encoding = 'c'; break;
2710 case BuiltinType::Short: encoding = 's'; break;
2711 case BuiltinType::Int: encoding = 'i'; break;
2712 case BuiltinType::Long:
2713 encoding =
2714 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2715 break;
2716 case BuiltinType::LongLong: encoding = 'q'; break;
2717 case BuiltinType::Int128: encoding = 't'; break;
2718 case BuiltinType::Float: encoding = 'f'; break;
2719 case BuiltinType::Double: encoding = 'd'; break;
2720 case BuiltinType::LongDouble: encoding = 'd'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002721 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002722
2723 S += encoding;
2724 return;
2725 }
2726
2727 if (const ComplexType *CT = T->getAsComplexType()) {
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002728 S += 'j';
2729 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2730 false);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002731 return;
2732 }
2733
Ted Kremenek35366a62009-07-17 17:50:17 +00002734 if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002735 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002736 bool isReadOnly = false;
2737 // For historical/compatibility reasons, the read-only qualifier of the
2738 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2739 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2740 // Also, do not emit the 'r' for anything but the outermost type!
2741 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2742 if (OutermostType && T.isConstQualified()) {
2743 isReadOnly = true;
2744 S += 'r';
2745 }
2746 }
2747 else if (OutermostType) {
2748 QualType P = PointeeTy;
Ted Kremenek35366a62009-07-17 17:50:17 +00002749 while (P->getAsPointerType())
2750 P = P->getAsPointerType()->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002751 if (P.isConstQualified()) {
2752 isReadOnly = true;
2753 S += 'r';
2754 }
2755 }
2756 if (isReadOnly) {
2757 // Another legacy compatibility encoding. Some ObjC qualifier and type
2758 // combinations need to be rearranged.
2759 // Rewrite "in const" from "nr" to "rn"
2760 const char * s = S.c_str();
2761 int len = S.length();
2762 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2763 std::string replace = "rn";
2764 S.replace(S.end()-2, S.end(), replace);
2765 }
2766 }
Steve Naroff14108da2009-07-10 23:34:53 +00002767 if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002768 S += ':';
2769 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002770 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002771
2772 if (PointeeTy->isCharType()) {
2773 // char pointer types should be encoded as '*' unless it is a
2774 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002775 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002776 S += '*';
2777 return;
2778 }
2779 }
2780
2781 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002782 getLegacyIntegralTypeEncoding(PointeeTy);
2783
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002784 getObjCEncodingForTypeImpl(PointeeTy, S, false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002785 NULL);
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002786 return;
2787 }
2788
2789 if (const ArrayType *AT =
2790 // Ignore type qualifiers etc.
2791 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002792 if (isa<IncompleteArrayType>(AT)) {
2793 // Incomplete arrays are encoded as a pointer to the array element.
2794 S += '^';
2795
2796 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2797 false, ExpandStructures, FD);
2798 } else {
2799 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002800
Anders Carlsson559a8332009-02-22 01:38:57 +00002801 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2802 S += llvm::utostr(CAT->getSize().getZExtValue());
2803 else {
2804 //Variable length arrays are encoded as a regular array with 0 elements.
2805 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2806 S += '0';
2807 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002808
Anders Carlsson559a8332009-02-22 01:38:57 +00002809 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2810 false, ExpandStructures, FD);
2811 S += ']';
2812 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002813 return;
2814 }
2815
2816 if (T->getAsFunctionType()) {
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002817 S += '?';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002818 return;
2819 }
2820
Ted Kremenek35366a62009-07-17 17:50:17 +00002821 if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002822 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002823 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002824 // Anonymous structures print as '?'
2825 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2826 S += II->getName();
2827 } else {
2828 S += '?';
2829 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002830 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002831 S += '=';
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +00002832 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2833 FieldEnd = RDecl->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00002834 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002835 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002836 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002837 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002838 S += '"';
2839 }
2840
2841 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002842 if (Field->isBitField()) {
2843 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2844 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002845 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002846 QualType qt = Field->getType();
2847 getLegacyIntegralTypeEncoding(qt);
2848 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002849 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002850 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002851 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002852 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002853 S += RDecl->isUnion() ? ')' : '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002854 return;
2855 }
2856
2857 if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002858 if (FD && FD->isBitField())
2859 EncodeBitField(this, S, FD);
2860 else
2861 S += 'i';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002862 return;
2863 }
2864
2865 if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002866 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002867 return;
2868 }
2869
2870 if (T->isObjCInterfaceType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002871 // @encode(class_name)
2872 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2873 S += '{';
2874 const IdentifierInfo *II = OI->getIdentifier();
2875 S += II->getName();
2876 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002877 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002878 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002879 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002880 if (RecFields[i]->isBitField())
2881 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2882 RecFields[i]);
2883 else
2884 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2885 FD);
2886 }
2887 S += '}';
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002888 return;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002889 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002890
2891 if (const ObjCObjectPointerType *OPT = T->getAsObjCObjectPointerType()) {
Steve Naroff14108da2009-07-10 23:34:53 +00002892 if (OPT->isObjCIdType()) {
2893 S += '@';
2894 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002895 }
2896
2897 if (OPT->isObjCClassType()) {
Steve Naroff14108da2009-07-10 23:34:53 +00002898 S += '#';
2899 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002900 }
2901
2902 if (OPT->isObjCQualifiedIdType()) {
Steve Naroff14108da2009-07-10 23:34:53 +00002903 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2904 ExpandPointedToStructures,
2905 ExpandStructures, FD);
2906 if (FD || EncodingProperty) {
2907 // Note that we do extended encoding of protocol qualifer list
2908 // Only when doing ivar or property encoding.
2909 const ObjCObjectPointerType *QIDT = T->getAsObjCQualifiedIdType();
2910 S += '"';
2911 for (ObjCObjectPointerType::qual_iterator I = QIDT->qual_begin(),
2912 E = QIDT->qual_end(); I != E; ++I) {
2913 S += '<';
2914 S += (*I)->getNameAsString();
2915 S += '>';
2916 }
2917 S += '"';
2918 }
2919 return;
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002920 }
2921
2922 QualType PointeeTy = OPT->getPointeeType();
2923 if (!EncodingProperty &&
2924 isa<TypedefType>(PointeeTy.getTypePtr())) {
2925 // Another historical/compatibility reason.
2926 // We encode the underlying type which comes out as
2927 // {...};
2928 S += '^';
2929 getObjCEncodingForTypeImpl(PointeeTy, S,
2930 false, ExpandPointedToStructures,
2931 NULL);
Steve Naroff14108da2009-07-10 23:34:53 +00002932 return;
2933 }
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002934
2935 S += '@';
2936 if (FD || EncodingProperty) {
2937 const ObjCInterfaceType *OIT = OPT->getInterfaceType();
2938 ObjCInterfaceDecl *OI = OIT->getDecl();
2939 S += '"';
2940 S += OI->getNameAsCString();
2941 for (ObjCInterfaceType::qual_iterator I = OIT->qual_begin(),
2942 E = OIT->qual_end(); I != E; ++I) {
2943 S += '<';
2944 S += (*I)->getNameAsString();
2945 S += '>';
2946 }
2947 S += '"';
2948 }
2949 return;
2950 }
2951
2952 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002953}
2954
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002955void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002956 std::string& S) const {
2957 if (QT & Decl::OBJC_TQ_In)
2958 S += 'n';
2959 if (QT & Decl::OBJC_TQ_Inout)
2960 S += 'N';
2961 if (QT & Decl::OBJC_TQ_Out)
2962 S += 'o';
2963 if (QT & Decl::OBJC_TQ_Bycopy)
2964 S += 'O';
2965 if (QT & Decl::OBJC_TQ_Byref)
2966 S += 'R';
2967 if (QT & Decl::OBJC_TQ_Oneway)
2968 S += 'V';
2969}
2970
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002971void ASTContext::setBuiltinVaListType(QualType T) {
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002972 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2973
2974 BuiltinVaListType = T;
2975}
2976
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002977void ASTContext::setObjCIdType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00002978 ObjCIdTypedefType = T;
Steve Naroff7e219e42007-10-15 14:41:52 +00002979}
2980
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002981void ASTContext::setObjCSelType(QualType T) {
Douglas Gregor319ac892009-04-23 22:29:11 +00002982 ObjCSelType = T;
2983
2984 const TypedefType *TT = T->getAsTypedefType();
2985 if (!TT)
2986 return;
2987 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002988
2989 // typedef struct objc_selector *SEL;
Ted Kremenek35366a62009-07-17 17:50:17 +00002990 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002991 if (!ptr)
2992 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002993 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002994 if (!rec)
2995 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002996 SelStructType = rec;
2997}
2998
Chris Lattnerce7b38c2009-07-13 00:10:46 +00002999void ASTContext::setObjCProtoType(QualType QT) {
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003000 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00003001}
3002
Chris Lattnerce7b38c2009-07-13 00:10:46 +00003003void ASTContext::setObjCClassType(QualType T) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003004 ObjCClassTypedefType = T;
Anders Carlsson8baaca52007-10-31 02:53:19 +00003005}
3006
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003007void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
3008 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00003009 "'NSConstantString' type already set!");
3010
Ted Kremeneka526c5c2008-01-07 19:49:32 +00003011 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00003012}
3013
Douglas Gregor7532dc62009-03-30 22:58:21 +00003014/// \brief Retrieve the template name that represents a qualified
3015/// template name such as \c std::vector.
3016TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
3017 bool TemplateKeyword,
3018 TemplateDecl *Template) {
3019 llvm::FoldingSetNodeID ID;
3020 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
3021
3022 void *InsertPos = 0;
3023 QualifiedTemplateName *QTN =
3024 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3025 if (!QTN) {
3026 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
3027 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
3028 }
3029
3030 return TemplateName(QTN);
3031}
3032
3033/// \brief Retrieve the template name that represents a dependent
3034/// template name such as \c MetaFun::template apply.
3035TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
3036 const IdentifierInfo *Name) {
3037 assert(NNS->isDependent() && "Nested name specifier must be dependent");
3038
3039 llvm::FoldingSetNodeID ID;
3040 DependentTemplateName::Profile(ID, NNS, Name);
3041
3042 void *InsertPos = 0;
3043 DependentTemplateName *QTN =
3044 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
3045
3046 if (QTN)
3047 return TemplateName(QTN);
3048
3049 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
3050 if (CanonNNS == NNS) {
3051 QTN = new (*this,4) DependentTemplateName(NNS, Name);
3052 } else {
3053 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
3054 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
3055 }
3056
3057 DependentTemplateNames.InsertNode(QTN, InsertPos);
3058 return TemplateName(QTN);
3059}
3060
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003061/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00003062/// TargetInfo, produce the corresponding type. The unsigned @p Type
3063/// is actually a value of type @c TargetInfo::IntType.
3064QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003065 switch (Type) {
3066 case TargetInfo::NoInt: return QualType();
3067 case TargetInfo::SignedShort: return ShortTy;
3068 case TargetInfo::UnsignedShort: return UnsignedShortTy;
3069 case TargetInfo::SignedInt: return IntTy;
3070 case TargetInfo::UnsignedInt: return UnsignedIntTy;
3071 case TargetInfo::SignedLong: return LongTy;
3072 case TargetInfo::UnsignedLong: return UnsignedLongTy;
3073 case TargetInfo::SignedLongLong: return LongLongTy;
3074 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
3075 }
3076
3077 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00003078 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00003079}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00003080
3081//===----------------------------------------------------------------------===//
3082// Type Predicates.
3083//===----------------------------------------------------------------------===//
3084
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003085/// isObjCNSObjectType - Return true if this is an NSObject object using
3086/// NSObject attribute on a c-style pointer type.
3087/// FIXME - Make it work directly on types.
Steve Narofff4954562009-07-16 15:41:00 +00003088/// FIXME: Move to Type.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003089///
3090bool ASTContext::isObjCNSObjectType(QualType Ty) const {
3091 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
3092 if (TypedefDecl *TD = TDT->getDecl())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +00003093 if (TD->getAttr<ObjCNSObjectAttr>())
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00003094 return true;
3095 }
3096 return false;
3097}
3098
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003099/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
3100/// garbage collection attribute.
3101///
3102QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003103 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003104 if (getLangOptions().ObjC1 &&
3105 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00003106 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003107 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003108 // (or pointers to them) be treated as though they were declared
3109 // as __strong.
3110 if (GCAttrs == QualType::GCNone) {
Steve Narofff4954562009-07-16 15:41:00 +00003111 if (Ty->isObjCObjectPointerType())
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003112 GCAttrs = QualType::Strong;
3113 else if (Ty->isPointerType())
Ted Kremenek35366a62009-07-17 17:50:17 +00003114 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00003115 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00003116 // Non-pointers have none gc'able attribute regardless of the attribute
3117 // set on them.
Steve Narofff4954562009-07-16 15:41:00 +00003118 else if (!Ty->isAnyPointerType() && !Ty->isBlockPointerType())
Fariborz Jahanianc2112182009-04-11 00:00:54 +00003119 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003120 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00003121 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00003122}
3123
Chris Lattner6ac46a42008-04-07 06:51:04 +00003124//===----------------------------------------------------------------------===//
3125// Type Compatibility Testing
3126//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00003127
Chris Lattner6ac46a42008-04-07 06:51:04 +00003128/// areCompatVectorTypes - Return true if the two specified vector types are
3129/// compatible.
3130static bool areCompatVectorTypes(const VectorType *LHS,
3131 const VectorType *RHS) {
3132 assert(LHS->isCanonical() && RHS->isCanonical());
3133 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00003134 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00003135}
3136
Eli Friedman3d815e72008-08-22 00:56:42 +00003137/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00003138/// compatible for assignment from RHS to LHS. This handles validation of any
3139/// protocol qualifiers on the LHS or RHS.
3140///
Steve Naroff14108da2009-07-10 23:34:53 +00003141/// FIXME: Move the following to ObjCObjectPointerType/ObjCInterfaceType.
3142bool ASTContext::canAssignObjCInterfaces(const ObjCObjectPointerType *LHSOPT,
3143 const ObjCObjectPointerType *RHSOPT) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003144 // If either type represents the built-in 'id' or 'Class' types, return true.
3145 if (LHSOPT->isObjCBuiltinType() || RHSOPT->isObjCBuiltinType())
Steve Naroff14108da2009-07-10 23:34:53 +00003146 return true;
3147
3148 const ObjCInterfaceType* LHS = LHSOPT->getInterfaceType();
3149 const ObjCInterfaceType* RHS = RHSOPT->getInterfaceType();
Steve Naroffde2e22d2009-07-15 18:40:39 +00003150 if (!LHS || !RHS) {
3151 // We have qualified builtin types.
3152 // Both the right and left sides have qualifiers.
3153 for (ObjCObjectPointerType::qual_iterator I = LHSOPT->qual_begin(),
3154 E = LHSOPT->qual_end(); I != E; ++I) {
3155 bool RHSImplementsProtocol = false;
3156
3157 // when comparing an id<P> on lhs with a static type on rhs,
3158 // see if static class implements all of id's protocols, directly or
3159 // through its super class and categories.
3160 for (ObjCObjectPointerType::qual_iterator J = RHSOPT->qual_begin(),
3161 E = RHSOPT->qual_end(); J != E; ++J) {
Steve Naroff8f167562009-07-16 16:21:02 +00003162 if ((*J)->lookupProtocolNamed((*I)->getIdentifier())) {
Steve Naroffde2e22d2009-07-15 18:40:39 +00003163 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00003164 break;
3165 }
Steve Naroffde2e22d2009-07-15 18:40:39 +00003166 }
3167 if (!RHSImplementsProtocol)
3168 return false;
3169 }
3170 // The RHS implements all protocols listed on the LHS.
3171 return true;
3172 }
Steve Naroff14108da2009-07-10 23:34:53 +00003173 return canAssignObjCInterfaces(LHS, RHS);
3174}
3175
Eli Friedman3d815e72008-08-22 00:56:42 +00003176bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
3177 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00003178 // Verify that the base decls are compatible: the RHS must be a subclass of
3179 // the LHS.
3180 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
3181 return false;
3182
3183 // RHS must have a superset of the protocols in the LHS. If the LHS is not
3184 // protocol qualified at all, then we are good.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003185 if (LHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003186 return true;
3187
3188 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
3189 // isn't a superset.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003190 if (RHS->getNumProtocols() == 0)
Chris Lattner6ac46a42008-04-07 06:51:04 +00003191 return true; // FIXME: should return false!
3192
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003193 for (ObjCInterfaceType::qual_iterator LHSPI = LHS->qual_begin(),
3194 LHSPE = LHS->qual_end();
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003195 LHSPI != LHSPE; LHSPI++) {
3196 bool RHSImplementsProtocol = false;
3197
3198 // If the RHS doesn't implement the protocol on the left, the types
3199 // are incompatible.
Steve Naroffc15cb2a2009-07-18 15:33:26 +00003200 for (ObjCInterfaceType::qual_iterator RHSPI = RHS->qual_begin(),
3201 RHSPE = RHS->qual_end();
Steve Naroff8f167562009-07-16 16:21:02 +00003202 RHSPI != RHSPE; RHSPI++) {
3203 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier())) {
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003204 RHSImplementsProtocol = true;
Steve Naroff8f167562009-07-16 16:21:02 +00003205 break;
3206 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003207 }
3208 // FIXME: For better diagnostics, consider passing back the protocol name.
3209 if (!RHSImplementsProtocol)
3210 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003211 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00003212 // The RHS implements all protocols listed on the LHS.
3213 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00003214}
3215
Steve Naroff389bf462009-02-12 17:52:19 +00003216bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
3217 // get the "pointed to" types
Steve Naroff14108da2009-07-10 23:34:53 +00003218 const ObjCObjectPointerType *LHSOPT = LHS->getAsObjCObjectPointerType();
3219 const ObjCObjectPointerType *RHSOPT = RHS->getAsObjCObjectPointerType();
Steve Naroff389bf462009-02-12 17:52:19 +00003220
Steve Naroff14108da2009-07-10 23:34:53 +00003221 if (!LHSOPT || !RHSOPT)
Steve Naroff389bf462009-02-12 17:52:19 +00003222 return false;
Steve Naroff14108da2009-07-10 23:34:53 +00003223
3224 return canAssignObjCInterfaces(LHSOPT, RHSOPT) ||
3225 canAssignObjCInterfaces(RHSOPT, LHSOPT);
Steve Naroff389bf462009-02-12 17:52:19 +00003226}
3227
Steve Naroffec0550f2007-10-15 20:41:53 +00003228/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
3229/// both shall have the identically qualified version of a compatible type.
3230/// C99 6.2.7p1: Two types have compatible types if their types are the
3231/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00003232bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
3233 return !mergeTypes(LHS, RHS).isNull();
3234}
3235
3236QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
3237 const FunctionType *lbase = lhs->getAsFunctionType();
3238 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00003239 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
3240 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00003241 bool allLTypes = true;
3242 bool allRTypes = true;
3243
3244 // Check return type
3245 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
3246 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003247 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
3248 allLTypes = false;
3249 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
3250 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00003251
3252 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00003253 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
3254 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003255 unsigned lproto_nargs = lproto->getNumArgs();
3256 unsigned rproto_nargs = rproto->getNumArgs();
3257
3258 // Compatible functions must have the same number of arguments
3259 if (lproto_nargs != rproto_nargs)
3260 return QualType();
3261
3262 // Variadic and non-variadic functions aren't compatible
3263 if (lproto->isVariadic() != rproto->isVariadic())
3264 return QualType();
3265
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003266 if (lproto->getTypeQuals() != rproto->getTypeQuals())
3267 return QualType();
3268
Eli Friedman3d815e72008-08-22 00:56:42 +00003269 // Check argument compatibility
3270 llvm::SmallVector<QualType, 10> types;
3271 for (unsigned i = 0; i < lproto_nargs; i++) {
3272 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
3273 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
3274 QualType argtype = mergeTypes(largtype, rargtype);
3275 if (argtype.isNull()) return QualType();
3276 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00003277 if (getCanonicalType(argtype) != getCanonicalType(largtype))
3278 allLTypes = false;
3279 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
3280 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00003281 }
3282 if (allLTypes) return lhs;
3283 if (allRTypes) return rhs;
3284 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003285 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00003286 }
3287
3288 if (lproto) allRTypes = false;
3289 if (rproto) allLTypes = false;
3290
Douglas Gregor72564e72009-02-26 23:50:07 +00003291 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00003292 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00003293 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00003294 if (proto->isVariadic()) return QualType();
3295 // Check that the types are compatible with the types that
3296 // would result from default argument promotions (C99 6.7.5.3p15).
3297 // The only types actually affected are promotable integer
3298 // types and floats, which would be passed as a different
3299 // type depending on whether the prototype is visible.
3300 unsigned proto_nargs = proto->getNumArgs();
3301 for (unsigned i = 0; i < proto_nargs; ++i) {
3302 QualType argTy = proto->getArgType(i);
3303 if (argTy->isPromotableIntegerType() ||
3304 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
3305 return QualType();
3306 }
3307
3308 if (allLTypes) return lhs;
3309 if (allRTypes) return rhs;
3310 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00003311 proto->getNumArgs(), lproto->isVariadic(),
3312 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00003313 }
3314
3315 if (allLTypes) return lhs;
3316 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00003317 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00003318}
3319
3320QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00003321 // C++ [expr]: If an expression initially has the type "reference to T", the
3322 // type is adjusted to "T" prior to any further analysis, the expression
3323 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003324 // expression is an lvalue unless the reference is an rvalue reference and
3325 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003326 // FIXME: C++ shouldn't be going through here! The rules are different
3327 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003328 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3329 // shouldn't be going through here!
Ted Kremenek35366a62009-07-17 17:50:17 +00003330 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003331 LHS = RT->getPointeeType();
Ted Kremenek35366a62009-07-17 17:50:17 +00003332 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003333 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003334
Eli Friedman3d815e72008-08-22 00:56:42 +00003335 QualType LHSCan = getCanonicalType(LHS),
3336 RHSCan = getCanonicalType(RHS);
3337
3338 // If two types are identical, they are compatible.
3339 if (LHSCan == RHSCan)
3340 return LHS;
3341
3342 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003343 // Note that we handle extended qualifiers later, in the
3344 // case for ExtQualType.
3345 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00003346 return QualType();
3347
Eli Friedman852d63b2009-06-01 01:22:52 +00003348 Type::TypeClass LHSClass = LHSCan->getTypeClass();
3349 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003350
Chris Lattner1adb8832008-01-14 05:45:46 +00003351 // We want to consider the two function types to be the same for these
3352 // comparisons, just force one to the other.
3353 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3354 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003355
Eli Friedman07d25872009-06-02 05:28:56 +00003356 // Strip off objc_gc attributes off the top level so they can be merged.
3357 // This is a complete mess, but the attribute itself doesn't make much sense.
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003358 if (RHSClass == Type::ExtQual) {
Eli Friedman07d25872009-06-02 05:28:56 +00003359 QualType::GCAttrTypes GCAttr = RHSCan.getObjCGCAttr();
3360 if (GCAttr != QualType::GCNone) {
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003361 QualType::GCAttrTypes GCLHSAttr = LHSCan.getObjCGCAttr();
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003362 // __weak attribute must appear on both declarations.
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003363 // __strong attribue is redundant if other decl is an objective-c
3364 // object pointer (or decorated with __strong attribute); otherwise
3365 // issue error.
3366 if ((GCAttr == QualType::Weak && GCLHSAttr != GCAttr) ||
3367 (GCAttr == QualType::Strong && GCLHSAttr != GCAttr &&
Steve Naroff14108da2009-07-10 23:34:53 +00003368 !LHSCan->isObjCObjectPointerType()))
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003369 return QualType();
3370
Eli Friedman07d25872009-06-02 05:28:56 +00003371 RHS = QualType(cast<ExtQualType>(RHS.getDesugaredType())->getBaseType(),
3372 RHS.getCVRQualifiers());
3373 QualType Result = mergeTypes(LHS, RHS);
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003374 if (!Result.isNull()) {
3375 if (Result.getObjCGCAttr() == QualType::GCNone)
3376 Result = getObjCGCQualType(Result, GCAttr);
3377 else if (Result.getObjCGCAttr() != GCAttr)
3378 Result = QualType();
3379 }
Eli Friedman07d25872009-06-02 05:28:56 +00003380 return Result;
3381 }
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003382 }
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003383 if (LHSClass == Type::ExtQual) {
Eli Friedman07d25872009-06-02 05:28:56 +00003384 QualType::GCAttrTypes GCAttr = LHSCan.getObjCGCAttr();
3385 if (GCAttr != QualType::GCNone) {
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003386 QualType::GCAttrTypes GCRHSAttr = RHSCan.getObjCGCAttr();
3387 // __weak attribute must appear on both declarations. __strong
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003388 // __strong attribue is redundant if other decl is an objective-c
3389 // object pointer (or decorated with __strong attribute); otherwise
3390 // issue error.
3391 if ((GCAttr == QualType::Weak && GCRHSAttr != GCAttr) ||
3392 (GCAttr == QualType::Strong && GCRHSAttr != GCAttr &&
Steve Naroff14108da2009-07-10 23:34:53 +00003393 !RHSCan->isObjCObjectPointerType()))
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003394 return QualType();
Fariborz Jahanian86f43852009-06-02 20:58:58 +00003395
Eli Friedman07d25872009-06-02 05:28:56 +00003396 LHS = QualType(cast<ExtQualType>(LHS.getDesugaredType())->getBaseType(),
3397 LHS.getCVRQualifiers());
3398 QualType Result = mergeTypes(LHS, RHS);
Fariborz Jahanian8df7a282009-06-02 18:32:00 +00003399 if (!Result.isNull()) {
3400 if (Result.getObjCGCAttr() == QualType::GCNone)
3401 Result = getObjCGCQualType(Result, GCAttr);
3402 else if (Result.getObjCGCAttr() != GCAttr)
3403 Result = QualType();
3404 }
Eli Friedman354e53d2009-06-02 07:45:37 +00003405 return Result;
Eli Friedman07d25872009-06-02 05:28:56 +00003406 }
Fariborz Jahanian585f7b22009-06-02 01:40:22 +00003407 }
3408
Eli Friedman4c721d32008-02-12 08:23:06 +00003409 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003410 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3411 LHSClass = Type::ConstantArray;
3412 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3413 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00003414
Nate Begeman213541a2008-04-18 23:10:10 +00003415 // Canonicalize ExtVector -> Vector.
3416 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3417 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00003418
3419 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003420 if (LHSClass != RHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00003421 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3422 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003423 if (const EnumType* ETy = LHS->getAsEnumType()) {
3424 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3425 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003426 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003427 if (const EnumType* ETy = RHS->getAsEnumType()) {
3428 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3429 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003430 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003431
Eli Friedman3d815e72008-08-22 00:56:42 +00003432 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003433 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003434
Steve Naroff4a746782008-01-09 22:43:08 +00003435 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003436 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003437#define TYPE(Class, Base)
3438#define ABSTRACT_TYPE(Class, Base)
3439#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3440#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3441#include "clang/AST/TypeNodes.def"
3442 assert(false && "Non-canonical and dependent types shouldn't get here");
3443 return QualType();
3444
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003445 case Type::LValueReference:
3446 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003447 case Type::MemberPointer:
3448 assert(false && "C++ should never be in mergeTypes");
3449 return QualType();
3450
3451 case Type::IncompleteArray:
3452 case Type::VariableArray:
3453 case Type::FunctionProto:
3454 case Type::ExtVector:
Douglas Gregor72564e72009-02-26 23:50:07 +00003455 assert(false && "Types are eliminated above");
3456 return QualType();
3457
Chris Lattner1adb8832008-01-14 05:45:46 +00003458 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003459 {
3460 // Merge two pointer types, while trying to preserve typedef info
Ted Kremenek35366a62009-07-17 17:50:17 +00003461 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3462 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003463 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3464 if (ResultType.isNull()) return QualType();
Eli Friedman07d25872009-06-02 05:28:56 +00003465 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003466 return LHS;
Eli Friedman07d25872009-06-02 05:28:56 +00003467 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
Chris Lattner61710852008-10-05 17:34:18 +00003468 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003469 return getPointerType(ResultType);
3470 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003471 case Type::BlockPointer:
3472 {
3473 // Merge two block pointer types, while trying to preserve typedef info
Ted Kremenek35366a62009-07-17 17:50:17 +00003474 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3475 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
Steve Naroffc0febd52008-12-10 17:49:55 +00003476 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3477 if (ResultType.isNull()) return QualType();
3478 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3479 return LHS;
3480 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3481 return RHS;
3482 return getBlockPointerType(ResultType);
3483 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003484 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003485 {
3486 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3487 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3488 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3489 return QualType();
3490
3491 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3492 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3493 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3494 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003495 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3496 return LHS;
3497 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3498 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003499 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3500 ArrayType::ArraySizeModifier(), 0);
3501 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3502 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003503 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3504 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003505 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3506 return LHS;
3507 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3508 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003509 if (LVAT) {
3510 // FIXME: This isn't correct! But tricky to implement because
3511 // the array's size has to be the size of LHS, but the type
3512 // has to be different.
3513 return LHS;
3514 }
3515 if (RVAT) {
3516 // FIXME: This isn't correct! But tricky to implement because
3517 // the array's size has to be the size of RHS, but the type
3518 // has to be different.
3519 return RHS;
3520 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003521 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3522 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00003523 return getIncompleteArrayType(ResultType,
3524 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003525 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003526 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003527 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003528 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003529 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003530 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003531 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003532 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003533 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003534 case Type::Complex:
3535 // Distinct complex types are incompatible.
3536 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003537 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003538 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003539 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3540 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003541 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003542 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003543 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003544 // FIXME: This should be type compatibility, e.g. whether
3545 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003546 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3547 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3548 if (LHSIface && RHSIface &&
3549 canAssignObjCInterfaces(LHSIface, RHSIface))
3550 return LHS;
3551
Eli Friedman3d815e72008-08-22 00:56:42 +00003552 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003553 }
Steve Naroff14108da2009-07-10 23:34:53 +00003554 case Type::ObjCObjectPointer: {
3555 // FIXME: Incorporate tests from Sema::ObjCQualifiedIdTypesAreCompatible().
3556 if (LHS->isObjCQualifiedIdType() && RHS->isObjCQualifiedIdType())
3557 return QualType();
3558
3559 if (canAssignObjCInterfaces(LHS->getAsObjCObjectPointerType(),
3560 RHS->getAsObjCObjectPointerType()))
3561 return LHS;
3562
Steve Naroffbc76dd02008-12-10 22:14:21 +00003563 return QualType();
Steve Naroff14108da2009-07-10 23:34:53 +00003564 }
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003565 case Type::FixedWidthInt:
3566 // Distinct fixed-width integers are not compatible.
3567 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003568 case Type::ExtQual:
3569 // FIXME: ExtQual types can be compatible even if they're not
3570 // identical!
3571 return QualType();
3572 // First attempt at an implementation, but I'm not really sure it's
3573 // right...
3574#if 0
3575 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3576 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3577 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3578 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3579 return QualType();
3580 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3581 LHSBase = QualType(LQual->getBaseType(), 0);
3582 RHSBase = QualType(RQual->getBaseType(), 0);
3583 ResultType = mergeTypes(LHSBase, RHSBase);
3584 if (ResultType.isNull()) return QualType();
3585 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3586 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3587 return LHS;
3588 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3589 return RHS;
3590 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3591 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3592 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3593 return ResultType;
3594#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003595
3596 case Type::TemplateSpecialization:
3597 assert(false && "Dependent types have no size");
3598 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003599 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003600
3601 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003602}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003603
Chris Lattner5426bf62008-04-07 07:01:58 +00003604//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003605// Integer Predicates
3606//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003607
Eli Friedmanad74a752008-06-28 06:23:08 +00003608unsigned ASTContext::getIntWidth(QualType T) {
3609 if (T == BoolTy)
3610 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003611 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3612 return FWIT->getWidth();
3613 }
3614 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003615 return (unsigned)getTypeSize(T);
3616}
3617
3618QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3619 assert(T->isSignedIntegerType() && "Unexpected type");
3620 if (const EnumType* ETy = T->getAsEnumType())
3621 T = ETy->getDecl()->getIntegerType();
3622 const BuiltinType* BTy = T->getAsBuiltinType();
3623 assert (BTy && "Unexpected signed integer type");
3624 switch (BTy->getKind()) {
3625 case BuiltinType::Char_S:
3626 case BuiltinType::SChar:
3627 return UnsignedCharTy;
3628 case BuiltinType::Short:
3629 return UnsignedShortTy;
3630 case BuiltinType::Int:
3631 return UnsignedIntTy;
3632 case BuiltinType::Long:
3633 return UnsignedLongTy;
3634 case BuiltinType::LongLong:
3635 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003636 case BuiltinType::Int128:
3637 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003638 default:
3639 assert(0 && "Unexpected signed integer type");
3640 return QualType();
3641 }
3642}
3643
Douglas Gregor2cf26342009-04-09 22:27:44 +00003644ExternalASTSource::~ExternalASTSource() { }
3645
3646void ExternalASTSource::PrintStats() { }
Chris Lattner86df27b2009-06-14 00:45:47 +00003647
3648
3649//===----------------------------------------------------------------------===//
3650// Builtin Type Computation
3651//===----------------------------------------------------------------------===//
3652
3653/// DecodeTypeFromStr - This decodes one type descriptor from Str, advancing the
3654/// pointer over the consumed characters. This returns the resultant type.
3655static QualType DecodeTypeFromStr(const char *&Str, ASTContext &Context,
3656 ASTContext::GetBuiltinTypeError &Error,
3657 bool AllowTypeModifiers = true) {
3658 // Modifiers.
3659 int HowLong = 0;
3660 bool Signed = false, Unsigned = false;
3661
3662 // Read the modifiers first.
3663 bool Done = false;
3664 while (!Done) {
3665 switch (*Str++) {
3666 default: Done = true; --Str; break;
3667 case 'S':
3668 assert(!Unsigned && "Can't use both 'S' and 'U' modifiers!");
3669 assert(!Signed && "Can't use 'S' modifier multiple times!");
3670 Signed = true;
3671 break;
3672 case 'U':
3673 assert(!Signed && "Can't use both 'S' and 'U' modifiers!");
3674 assert(!Unsigned && "Can't use 'S' modifier multiple times!");
3675 Unsigned = true;
3676 break;
3677 case 'L':
3678 assert(HowLong <= 2 && "Can't have LLLL modifier");
3679 ++HowLong;
3680 break;
3681 }
3682 }
3683
3684 QualType Type;
3685
3686 // Read the base type.
3687 switch (*Str++) {
3688 default: assert(0 && "Unknown builtin type letter!");
3689 case 'v':
3690 assert(HowLong == 0 && !Signed && !Unsigned &&
3691 "Bad modifiers used with 'v'!");
3692 Type = Context.VoidTy;
3693 break;
3694 case 'f':
3695 assert(HowLong == 0 && !Signed && !Unsigned &&
3696 "Bad modifiers used with 'f'!");
3697 Type = Context.FloatTy;
3698 break;
3699 case 'd':
3700 assert(HowLong < 2 && !Signed && !Unsigned &&
3701 "Bad modifiers used with 'd'!");
3702 if (HowLong)
3703 Type = Context.LongDoubleTy;
3704 else
3705 Type = Context.DoubleTy;
3706 break;
3707 case 's':
3708 assert(HowLong == 0 && "Bad modifiers used with 's'!");
3709 if (Unsigned)
3710 Type = Context.UnsignedShortTy;
3711 else
3712 Type = Context.ShortTy;
3713 break;
3714 case 'i':
3715 if (HowLong == 3)
3716 Type = Unsigned ? Context.UnsignedInt128Ty : Context.Int128Ty;
3717 else if (HowLong == 2)
3718 Type = Unsigned ? Context.UnsignedLongLongTy : Context.LongLongTy;
3719 else if (HowLong == 1)
3720 Type = Unsigned ? Context.UnsignedLongTy : Context.LongTy;
3721 else
3722 Type = Unsigned ? Context.UnsignedIntTy : Context.IntTy;
3723 break;
3724 case 'c':
3725 assert(HowLong == 0 && "Bad modifiers used with 'c'!");
3726 if (Signed)
3727 Type = Context.SignedCharTy;
3728 else if (Unsigned)
3729 Type = Context.UnsignedCharTy;
3730 else
3731 Type = Context.CharTy;
3732 break;
3733 case 'b': // boolean
3734 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'b'!");
3735 Type = Context.BoolTy;
3736 break;
3737 case 'z': // size_t.
3738 assert(HowLong == 0 && !Signed && !Unsigned && "Bad modifiers for 'z'!");
3739 Type = Context.getSizeType();
3740 break;
3741 case 'F':
3742 Type = Context.getCFConstantStringType();
3743 break;
3744 case 'a':
3745 Type = Context.getBuiltinVaListType();
3746 assert(!Type.isNull() && "builtin va list type not initialized!");
3747 break;
3748 case 'A':
3749 // This is a "reference" to a va_list; however, what exactly
3750 // this means depends on how va_list is defined. There are two
3751 // different kinds of va_list: ones passed by value, and ones
3752 // passed by reference. An example of a by-value va_list is
3753 // x86, where va_list is a char*. An example of by-ref va_list
3754 // is x86-64, where va_list is a __va_list_tag[1]. For x86,
3755 // we want this argument to be a char*&; for x86-64, we want
3756 // it to be a __va_list_tag*.
3757 Type = Context.getBuiltinVaListType();
3758 assert(!Type.isNull() && "builtin va list type not initialized!");
3759 if (Type->isArrayType()) {
3760 Type = Context.getArrayDecayedType(Type);
3761 } else {
3762 Type = Context.getLValueReferenceType(Type);
3763 }
3764 break;
3765 case 'V': {
3766 char *End;
3767
3768 unsigned NumElements = strtoul(Str, &End, 10);
3769 assert(End != Str && "Missing vector size");
3770
3771 Str = End;
3772
3773 QualType ElementType = DecodeTypeFromStr(Str, Context, Error, false);
3774 Type = Context.getVectorType(ElementType, NumElements);
3775 break;
3776 }
3777 case 'P': {
Douglas Gregorc29f77b2009-07-07 16:35:42 +00003778 Type = Context.getFILEType();
3779 if (Type.isNull()) {
Chris Lattner86df27b2009-06-14 00:45:47 +00003780 Error = ASTContext::GE_Missing_FILE;
3781 return QualType();
Douglas Gregorc29f77b2009-07-07 16:35:42 +00003782 } else {
3783 break;
Chris Lattner86df27b2009-06-14 00:45:47 +00003784 }
3785 }
3786 }
3787
3788 if (!AllowTypeModifiers)
3789 return Type;
3790
3791 Done = false;
3792 while (!Done) {
3793 switch (*Str++) {
3794 default: Done = true; --Str; break;
3795 case '*':
3796 Type = Context.getPointerType(Type);
3797 break;
3798 case '&':
3799 Type = Context.getLValueReferenceType(Type);
3800 break;
3801 // FIXME: There's no way to have a built-in with an rvalue ref arg.
3802 case 'C':
3803 Type = Type.getQualifiedType(QualType::Const);
3804 break;
3805 }
3806 }
3807
3808 return Type;
3809}
3810
3811/// GetBuiltinType - Return the type for the specified builtin.
3812QualType ASTContext::GetBuiltinType(unsigned id,
3813 GetBuiltinTypeError &Error) {
3814 const char *TypeStr = BuiltinInfo.GetTypeString(id);
3815
3816 llvm::SmallVector<QualType, 8> ArgTypes;
3817
3818 Error = GE_None;
3819 QualType ResType = DecodeTypeFromStr(TypeStr, *this, Error);
3820 if (Error != GE_None)
3821 return QualType();
3822 while (TypeStr[0] && TypeStr[0] != '.') {
3823 QualType Ty = DecodeTypeFromStr(TypeStr, *this, Error);
3824 if (Error != GE_None)
3825 return QualType();
3826
3827 // Do array -> pointer decay. The builtin should use the decayed type.
3828 if (Ty->isArrayType())
3829 Ty = getArrayDecayedType(Ty);
3830
3831 ArgTypes.push_back(Ty);
3832 }
3833
3834 assert((TypeStr[0] != '.' || TypeStr[1] == 0) &&
3835 "'.' should only occur at end of builtin type list!");
3836
3837 // handle untyped/variadic arguments "T c99Style();" or "T cppStyle(...);".
3838 if (ArgTypes.size() == 0 && TypeStr[0] == '.')
3839 return getFunctionNoProtoType(ResType);
3840 return getFunctionType(ResType, ArgTypes.data(), ArgTypes.size(),
3841 TypeStr[0] == '.', 0);
3842}