blob: a4b9acc110d6860c3267d31a0a08f969be9b19e0 [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"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000021#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000023#include "llvm/ADT/StringExtras.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000025#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner61710852008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Douglas Gregor2deaea32009-04-22 18:49:13 +000035 bool FreeMem, unsigned size_reserve,
36 bool InitializeBuiltins) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregor2cf26342009-04-09 22:27:44 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
40 ExternalSource(0) {
Daniel Dunbare91593e2008-08-11 04:54:23 +000041 if (size_reserve > 0) Types.reserve(size_reserve);
42 InitBuiltinTypes();
Daniel Dunbare91593e2008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
Douglas Gregor7a9cbed2009-04-26 03:57:37 +000044 BuiltinInfo.InitializeTargetBuiltins(Target);
Douglas Gregor2deaea32009-04-22 18:49:13 +000045 if (InitializeBuiltins)
46 this->InitializeBuiltins(idents);
Douglas Gregord249e1d1f2009-05-29 20:38:28 +000047 PrintingPolicy.CPlusPlus = LangOpts.CPlusPlus;
Daniel Dunbare91593e2008-08-11 04:54:23 +000048}
49
Reid Spencer5f016e22007-07-11 17:01:13 +000050ASTContext::~ASTContext() {
51 // Deallocate all the types.
52 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000053 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000054 Types.pop_back();
55 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000056
Nuno Lopesb74668e2008-12-17 22:30:25 +000057 {
58 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
59 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
60 while (I != E) {
61 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
62 delete R;
63 }
64 }
65
66 {
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +000067 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
68 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopesb74668e2008-12-17 22:30:25 +000069 while (I != E) {
70 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
71 delete R;
72 }
73 }
74
Douglas Gregorab452ba2009-03-26 23:50:42 +000075 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000076 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
77 NNS = NestedNameSpecifiers.begin(),
78 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000079 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000080 /* Increment in loop */)
81 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000082
83 if (GlobalNestedNameSpecifier)
84 GlobalNestedNameSpecifier->Destroy(*this);
85
Eli Friedmanb26153c2008-05-27 03:08:09 +000086 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000087}
88
Douglas Gregor2deaea32009-04-22 18:49:13 +000089void ASTContext::InitializeBuiltins(IdentifierTable &idents) {
Douglas Gregor2deaea32009-04-22 18:49:13 +000090 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
91}
92
Douglas Gregor2cf26342009-04-09 22:27:44 +000093void
94ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
95 ExternalSource.reset(Source.take());
96}
97
Reid Spencer5f016e22007-07-11 17:01:13 +000098void ASTContext::PrintStats() const {
99 fprintf(stderr, "*** AST Context Stats:\n");
100 fprintf(stderr, " %d types total.\n", (int)Types.size());
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000101
Douglas Gregordbe833d2009-05-26 14:40:08 +0000102 unsigned counts[] = {
103#define TYPE(Name, Parent) 0,
104#define ABSTRACT_TYPE(Name, Parent)
105#include "clang/AST/TypeNodes.def"
106 0 // Extra
107 };
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000108
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
110 Type *T = Types[i];
Douglas Gregordbe833d2009-05-26 14:40:08 +0000111 counts[(unsigned)T->getTypeClass()]++;
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 }
113
Douglas Gregordbe833d2009-05-26 14:40:08 +0000114 unsigned Idx = 0;
115 unsigned TotalBytes = 0;
116#define TYPE(Name, Parent) \
117 if (counts[Idx]) \
118 fprintf(stderr, " %d %s types\n", (int)counts[Idx], #Name); \
119 TotalBytes += counts[Idx] * sizeof(Name##Type); \
120 ++Idx;
121#define ABSTRACT_TYPE(Name, Parent)
122#include "clang/AST/TypeNodes.def"
123
124 fprintf(stderr, "Total bytes = %d\n", int(TotalBytes));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000125
126 if (ExternalSource.get()) {
127 fprintf(stderr, "\n");
128 ExternalSource->PrintStats();
129 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000130}
131
132
133void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000134 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000135}
136
Reid Spencer5f016e22007-07-11 17:01:13 +0000137void ASTContext::InitBuiltinTypes() {
138 assert(VoidTy.isNull() && "Context reinitialized?");
139
140 // C99 6.2.5p19.
141 InitBuiltinType(VoidTy, BuiltinType::Void);
142
143 // C99 6.2.5p2.
144 InitBuiltinType(BoolTy, BuiltinType::Bool);
145 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000146 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000147 InitBuiltinType(CharTy, BuiltinType::Char_S);
148 else
149 InitBuiltinType(CharTy, BuiltinType::Char_U);
150 // C99 6.2.5p4.
151 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
152 InitBuiltinType(ShortTy, BuiltinType::Short);
153 InitBuiltinType(IntTy, BuiltinType::Int);
154 InitBuiltinType(LongTy, BuiltinType::Long);
155 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
156
157 // C99 6.2.5p6.
158 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
159 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
160 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
161 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
162 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
163
164 // C99 6.2.5p10.
165 InitBuiltinType(FloatTy, BuiltinType::Float);
166 InitBuiltinType(DoubleTy, BuiltinType::Double);
167 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000168
Chris Lattner2df9ced2009-04-30 02:43:43 +0000169 // GNU extension, 128-bit integers.
170 InitBuiltinType(Int128Ty, BuiltinType::Int128);
171 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
172
Chris Lattner3a250322009-02-26 23:43:47 +0000173 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
174 InitBuiltinType(WCharTy, BuiltinType::WChar);
175 else // C99
176 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000177
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000178 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000179 InitBuiltinType(OverloadTy, BuiltinType::Overload);
180
181 // Placeholder type for type-dependent expressions whose type is
182 // completely unknown. No code should ever check a type against
183 // DependentTy and users should never see it; however, it is here to
184 // help diagnose failures to properly check for type-dependent
185 // expressions.
186 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000187
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 // C99 6.2.5p11.
189 FloatComplexTy = getComplexType(FloatTy);
190 DoubleComplexTy = getComplexType(DoubleTy);
191 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000192
Steve Naroff7e219e42007-10-15 14:41:52 +0000193 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000194 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000195 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000196 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000197 ClassStructType = 0;
198
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000199 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000200
201 // void * type
202 VoidPtrTy = getPointerType(VoidTy);
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000203
204 // nullptr type (C++0x 2.14.7)
205 InitBuiltinType(NullPtrTy, BuiltinType::NullPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +0000206}
207
Chris Lattner464175b2007-07-18 17:52:12 +0000208//===----------------------------------------------------------------------===//
209// Type Sizing and Analysis
210//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000211
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000212/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
213/// scalar floating point type.
214const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
215 const BuiltinType *BT = T->getAsBuiltinType();
216 assert(BT && "Not a floating point type!");
217 switch (BT->getKind()) {
218 default: assert(0 && "Not a floating point type!");
219 case BuiltinType::Float: return Target.getFloatFormat();
220 case BuiltinType::Double: return Target.getDoubleFormat();
221 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
222 }
223}
224
Chris Lattneraf707ab2009-01-24 21:53:27 +0000225/// getDeclAlign - Return a conservative estimate of the alignment of the
226/// specified decl. Note that bitfields do not have a valid alignment, so
227/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000228unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000229 unsigned Align = Target.getCharWidth();
230
231 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
232 Align = std::max(Align, AA->getAlignment());
233
Chris Lattneraf707ab2009-01-24 21:53:27 +0000234 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
235 QualType T = VD->getType();
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000236 if (const ReferenceType* RT = T->getAsReferenceType()) {
237 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000238 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000239 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
240 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000241 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
242 T = cast<ArrayType>(T)->getElementType();
243
244 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
245 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000246 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000247
248 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000249}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000250
Chris Lattnera7674d82007-07-13 22:13:22 +0000251/// getTypeSize - Return the size of the specified type, in bits. This method
252/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000253std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000254ASTContext::getTypeInfo(const Type *T) {
Mike Stump5e301002009-02-27 18:32:39 +0000255 uint64_t Width=0;
256 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000257 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000258#define TYPE(Class, Base)
259#define ABSTRACT_TYPE(Class, Base)
Douglas Gregor18857642009-04-30 17:32:17 +0000260#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor72564e72009-02-26 23:50:07 +0000261#define DEPENDENT_TYPE(Class, Base) case Type::Class:
262#include "clang/AST/TypeNodes.def"
Douglas Gregor18857642009-04-30 17:32:17 +0000263 assert(false && "Should not see dependent types");
Douglas Gregor72564e72009-02-26 23:50:07 +0000264 break;
265
Chris Lattner692233e2007-07-13 22:27:08 +0000266 case Type::FunctionNoProto:
267 case Type::FunctionProto:
Douglas Gregor18857642009-04-30 17:32:17 +0000268 // GCC extension: alignof(function) = 32 bits
269 Width = 0;
270 Align = 32;
271 break;
272
Douglas Gregor72564e72009-02-26 23:50:07 +0000273 case Type::IncompleteArray:
Steve Narofffb22d962007-08-30 01:06:46 +0000274 case Type::VariableArray:
Douglas Gregor18857642009-04-30 17:32:17 +0000275 Width = 0;
276 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
277 break;
278
Steve Narofffb22d962007-08-30 01:06:46 +0000279 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000280 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000281
Chris Lattner98be4942008-03-05 18:54:05 +0000282 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000283 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000284 Align = EltInfo.second;
285 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000286 }
Nate Begeman213541a2008-04-18 23:10:10 +0000287 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000288 case Type::Vector: {
289 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000290 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000291 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000292 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000293 // If the alignment is not a power of 2, round up to the next power of 2.
294 // This happens for non-power-of-2 length vectors.
295 // FIXME: this should probably be a target property.
296 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000297 break;
298 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000299
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000300 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000301 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000302 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000303 case BuiltinType::Void:
Douglas Gregor18857642009-04-30 17:32:17 +0000304 // GCC extension: alignof(void) = 8 bits.
305 Width = 0;
306 Align = 8;
307 break;
308
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000309 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000310 Width = Target.getBoolWidth();
311 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000312 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000313 case BuiltinType::Char_S:
314 case BuiltinType::Char_U:
315 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000316 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000317 Width = Target.getCharWidth();
318 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000319 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000320 case BuiltinType::WChar:
321 Width = Target.getWCharWidth();
322 Align = Target.getWCharAlign();
323 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000324 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000325 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000326 Width = Target.getShortWidth();
327 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000328 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000329 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000330 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000331 Width = Target.getIntWidth();
332 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000333 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000334 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000335 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000336 Width = Target.getLongWidth();
337 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000338 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000339 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000340 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000341 Width = Target.getLongLongWidth();
342 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000343 break;
Chris Lattnerec16cb92009-04-30 02:55:13 +0000344 case BuiltinType::Int128:
345 case BuiltinType::UInt128:
346 Width = 128;
347 Align = 128; // int128_t is 128-bit aligned on all targets.
348 break;
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000349 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000350 Width = Target.getFloatWidth();
351 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000352 break;
353 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000354 Width = Target.getDoubleWidth();
355 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000356 break;
357 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000358 Width = Target.getLongDoubleWidth();
359 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000360 break;
Sebastian Redl6e8ed162009-05-10 18:38:11 +0000361 case BuiltinType::NullPtr:
362 Width = Target.getPointerWidth(0); // C++ 3.9.1p11: sizeof(nullptr_t)
363 Align = Target.getPointerAlign(0); // == sizeof(void*)
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000364 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000365 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000366 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000367 case Type::FixedWidthInt:
368 // FIXME: This isn't precisely correct; the width/alignment should depend
369 // on the available types for the target
370 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000371 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000372 Align = Width;
373 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000374 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000375 // FIXME: Pointers into different addr spaces could have different sizes and
376 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000377 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000378 case Type::ObjCQualifiedId:
Douglas Gregor72564e72009-02-26 23:50:07 +0000379 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000380 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000381 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000382 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000383 case Type::BlockPointer: {
384 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
385 Width = Target.getPointerWidth(AS);
386 Align = Target.getPointerAlign(AS);
387 break;
388 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000389 case Type::Pointer: {
390 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000391 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000392 Align = Target.getPointerAlign(AS);
393 break;
394 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000395 case Type::LValueReference:
396 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000397 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000398 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000399 // FIXME: This is wrong for struct layout: a reference in a struct has
400 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000401 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000402 case Type::MemberPointer: {
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000403 // FIXME: This is ABI dependent. We use the Itanium C++ ABI.
404 // http://www.codesourcery.com/public/cxx-abi/abi.html#member-pointers
405 // If we ever want to support other ABIs this needs to be abstracted.
406
Sebastian Redlf30208a2009-01-24 21:16:55 +0000407 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000408 std::pair<uint64_t, unsigned> PtrDiffInfo =
409 getTypeInfo(getPointerDiffType());
410 Width = PtrDiffInfo.first;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000411 if (Pointee->isFunctionType())
412 Width *= 2;
Anders Carlsson1cca74e2009-05-17 02:06:04 +0000413 Align = PtrDiffInfo.second;
414 break;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000415 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000416 case Type::Complex: {
417 // Complex types have the same alignment as their elements, but twice the
418 // size.
419 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000420 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000421 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000422 Align = EltInfo.second;
423 break;
424 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000425 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000426 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000427 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
428 Width = Layout.getSize();
429 Align = Layout.getAlignment();
430 break;
431 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000432 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000433 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000434 const TagType *TT = cast<TagType>(T);
435
436 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000437 Width = 1;
438 Align = 1;
439 break;
440 }
441
Daniel Dunbar1d751182008-11-08 05:48:37 +0000442 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000443 return getTypeInfo(ET->getDecl()->getIntegerType());
444
Daniel Dunbar1d751182008-11-08 05:48:37 +0000445 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000446 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
447 Width = Layout.getSize();
448 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000449 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000450 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000451
Douglas Gregor18857642009-04-30 17:32:17 +0000452 case Type::Typedef: {
453 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
454 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
455 Align = Aligned->getAlignment();
456 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
457 } else
458 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregor7532dc62009-03-30 22:58:21 +0000459 break;
Chris Lattner71763312008-04-06 22:05:18 +0000460 }
Douglas Gregor18857642009-04-30 17:32:17 +0000461
462 case Type::TypeOfExpr:
463 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
464 .getTypePtr());
465
466 case Type::TypeOf:
467 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
468
469 case Type::QualifiedName:
470 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
471
472 case Type::TemplateSpecialization:
473 assert(getCanonicalType(T) != T &&
474 "Cannot request the size of a dependent type");
475 // FIXME: this is likely to be wrong once we support template
476 // aliases, since a template alias could refer to a typedef that
477 // has an __aligned__ attribute on it.
478 return getTypeInfo(getCanonicalType(T));
479 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000480
Chris Lattner464175b2007-07-18 17:52:12 +0000481 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000482 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000483}
484
Chris Lattner34ebde42009-01-27 18:08:34 +0000485/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
486/// type for the current target in bits. This can be different than the ABI
487/// alignment in cases where it is beneficial for performance to overalign
488/// a data type.
489unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
490 unsigned ABIAlign = getTypeAlign(T);
Eli Friedman1eed6022009-05-25 21:27:19 +0000491
492 // Double and long long should be naturally aligned if possible.
493 if (const ComplexType* CT = T->getAsComplexType())
494 T = CT->getElementType().getTypePtr();
495 if (T->isSpecificBuiltinType(BuiltinType::Double) ||
496 T->isSpecificBuiltinType(BuiltinType::LongLong))
497 return std::max(ABIAlign, (unsigned)getTypeSize(T));
498
Chris Lattner34ebde42009-01-27 18:08:34 +0000499 return ABIAlign;
500}
501
502
Devang Patel8b277042008-06-04 21:22:16 +0000503/// LayoutField - Field layout.
504void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000505 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000506 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000507 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000508 uint64_t FieldOffset = IsUnion ? 0 : Size;
509 uint64_t FieldSize;
510 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000511
512 // FIXME: Should this override struct packing? Probably we want to
513 // take the minimum?
514 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
515 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000516
517 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
518 // TODO: Need to check this algorithm on other targets!
519 // (tested on Linux-X86)
Eli Friedman9a901bb2009-04-26 19:19:15 +0000520 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000521
522 std::pair<uint64_t, unsigned> FieldInfo =
523 Context.getTypeInfo(FD->getType());
524 uint64_t TypeSize = FieldInfo.first;
525
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000526 // Determine the alignment of this bitfield. The packing
527 // attributes define a maximum and the alignment attribute defines
528 // a minimum.
529 // FIXME: What is the right behavior when the specified alignment
530 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000531 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000532 if (FieldPacking)
533 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000534 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
535 FieldAlign = std::max(FieldAlign, AA->getAlignment());
536
537 // Check if we need to add padding to give the field the correct
538 // alignment.
539 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
540 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
541
542 // Padding members don't affect overall alignment
543 if (!FD->getIdentifier())
544 FieldAlign = 1;
545 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000546 if (FD->getType()->isIncompleteArrayType()) {
547 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000548 // query getTypeInfo about these, so we figure it out here.
549 // Flexible array members don't have any size, but they
550 // have to be aligned appropriately for their element type.
551 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000552 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000553 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson2f1169f2009-04-10 05:31:15 +0000554 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
555 unsigned AS = RT->getPointeeType().getAddressSpace();
556 FieldSize = Context.Target.getPointerWidth(AS);
557 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patel8b277042008-06-04 21:22:16 +0000558 } else {
559 std::pair<uint64_t, unsigned> FieldInfo =
560 Context.getTypeInfo(FD->getType());
561 FieldSize = FieldInfo.first;
562 FieldAlign = FieldInfo.second;
563 }
564
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000565 // Determine the alignment of this bitfield. The packing
566 // attributes define a maximum and the alignment attribute defines
567 // a minimum. Additionally, the packing alignment must be at least
568 // a byte for non-bitfields.
569 //
570 // FIXME: What is the right behavior when the specified alignment
571 // is smaller than the specified packing?
572 if (FieldPacking)
573 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000574 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
575 FieldAlign = std::max(FieldAlign, AA->getAlignment());
576
577 // Round up the current record size to the field's alignment boundary.
578 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
579 }
580
581 // Place this field at the current location.
582 FieldOffsets[FieldNo] = FieldOffset;
583
584 // Reserve space for this field.
585 if (IsUnion) {
586 Size = std::max(Size, FieldSize);
587 } else {
588 Size = FieldOffset + FieldSize;
589 }
590
Daniel Dunbard6884a02009-05-04 05:16:21 +0000591 // Remember the next available offset.
592 NextOffset = Size;
593
Devang Patel8b277042008-06-04 21:22:16 +0000594 // Remember max struct/class alignment.
595 Alignment = std::max(Alignment, FieldAlign);
596}
597
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000598static void CollectLocalObjCIvars(ASTContext *Ctx,
599 const ObjCInterfaceDecl *OI,
600 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000601 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
602 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000603 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000604 if (!IVDecl->isInvalidDecl())
605 Fields.push_back(cast<FieldDecl>(IVDecl));
606 }
607}
608
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000609void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
610 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
611 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
612 CollectObjCIvars(SuperClass, Fields);
613 CollectLocalObjCIvars(this, OI, Fields);
614}
615
Fariborz Jahanian98200742009-05-12 18:14:29 +0000616void ASTContext::CollectProtocolSynthesizedIvars(const ObjCProtocolDecl *PD,
617 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
618 for (ObjCContainerDecl::prop_iterator I = PD->prop_begin(*this),
619 E = PD->prop_end(*this); I != E; ++I)
620 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
621 Ivars.push_back(Ivar);
622
623 // Also look into nested protocols.
624 for (ObjCProtocolDecl::protocol_iterator P = PD->protocol_begin(),
625 E = PD->protocol_end(); P != E; ++P)
626 CollectProtocolSynthesizedIvars(*P, Ivars);
627}
628
629/// CollectSynthesizedIvars -
630/// This routine collect synthesized ivars for the designated class.
631///
632void ASTContext::CollectSynthesizedIvars(const ObjCInterfaceDecl *OI,
633 llvm::SmallVectorImpl<ObjCIvarDecl*> &Ivars) {
634 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
635 E = OI->prop_end(*this); I != E; ++I) {
636 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
637 Ivars.push_back(Ivar);
638 }
639 // Also look into interface's protocol list for properties declared
640 // in the protocol and whose ivars are synthesized.
641 for (ObjCInterfaceDecl::protocol_iterator P = OI->protocol_begin(),
642 PE = OI->protocol_end(); P != PE; ++P) {
643 ObjCProtocolDecl *PD = (*P);
644 CollectProtocolSynthesizedIvars(PD, Ivars);
645 }
646}
647
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000648/// getInterfaceLayoutImpl - Get or compute information about the
649/// layout of the given interface.
650///
651/// \param Impl - If given, also include the layout of the interface's
652/// implementation. This may differ by including synthesized ivars.
Devang Patel44a3dde2008-06-04 21:54:36 +0000653const ASTRecordLayout &
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000654ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
655 const ObjCImplementationDecl *Impl) {
Daniel Dunbar532d4da2009-05-03 13:15:50 +0000656 assert(!D->isForwardDecl() && "Invalid interface decl!");
657
Devang Patel44a3dde2008-06-04 21:54:36 +0000658 // Look up this layout, if already laid out, return what we have.
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000659 ObjCContainerDecl *Key =
660 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
661 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
662 return *Entry;
Devang Patel44a3dde2008-06-04 21:54:36 +0000663
Daniel Dunbar453addb2009-05-03 11:16:44 +0000664 unsigned FieldCount = D->ivar_size();
665 // Add in synthesized ivar count if laying out an implementation.
666 if (Impl) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000667 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
668 CollectSynthesizedIvars(D, Ivars);
669 FieldCount += Ivars.size();
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000670 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar453addb2009-05-03 11:16:44 +0000671 // entry. Note we can't cache this because we simply free all
672 // entries later; however we shouldn't look up implementations
673 // frequently.
674 if (FieldCount == D->ivar_size())
675 return getObjCLayout(D, 0);
676 }
677
Devang Patel6a5a34c2008-06-06 02:14:01 +0000678 ASTRecordLayout *NewEntry = NULL;
Devang Patel6a5a34c2008-06-06 02:14:01 +0000679 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Devang Patel6a5a34c2008-06-06 02:14:01 +0000680 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
681 unsigned Alignment = SL.getAlignment();
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000682
Daniel Dunbar913af352009-05-07 21:58:26 +0000683 // We start laying out ivars not at the end of the superclass
684 // structure, but at the next byte following the last field.
685 uint64_t Size = llvm::RoundUpToAlignment(SL.NextOffset, 8);
Daniel Dunbard6884a02009-05-04 05:16:21 +0000686
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000687 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000688 NewEntry->InitializeLayout(FieldCount);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000689 } else {
Daniel Dunbard8fd6ff2009-05-03 11:41:43 +0000690 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
Devang Patel6a5a34c2008-06-06 02:14:01 +0000691 NewEntry->InitializeLayout(FieldCount);
692 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000693
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000694 unsigned StructPacking = 0;
695 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
696 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000697
698 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
699 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
700 AA->getAlignment()));
701
702 // Layout each ivar sequentially.
703 unsigned i = 0;
704 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
705 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
706 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000707 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000708 }
Daniel Dunbar453addb2009-05-03 11:16:44 +0000709 // And synthesized ivars, if this is an implementation.
710 if (Impl) {
Fariborz Jahanian98200742009-05-12 18:14:29 +0000711 // FIXME. Do we need to colltect twice?
712 llvm::SmallVector<ObjCIvarDecl*, 16> Ivars;
713 CollectSynthesizedIvars(D, Ivars);
714 for (unsigned k = 0, e = Ivars.size(); k != e; ++k)
715 NewEntry->LayoutField(Ivars[k], i++, false, StructPacking, *this);
Fariborz Jahanian18191882009-03-31 18:11:23 +0000716 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000717
Devang Patel44a3dde2008-06-04 21:54:36 +0000718 // Finally, round the size of the total struct up to the alignment of the
719 // struct itself.
720 NewEntry->FinalizeLayout();
721 return *NewEntry;
722}
723
Daniel Dunbarb2dbbb92009-05-03 10:38:35 +0000724const ASTRecordLayout &
725ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
726 return getObjCLayout(D, 0);
727}
728
729const ASTRecordLayout &
730ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
731 return getObjCLayout(D->getClassInterface(), D);
732}
733
Devang Patel88a981b2007-11-01 19:11:01 +0000734/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000735/// specified record (struct/union/class), which indicates its size and field
736/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000737const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000738 D = D->getDefinition(*this);
739 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000740
Chris Lattner464175b2007-07-18 17:52:12 +0000741 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000742 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000743 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000744
Devang Patel88a981b2007-11-01 19:11:01 +0000745 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
746 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
747 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000748 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000749
Douglas Gregore267ff32008-12-11 20:41:00 +0000750 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000751 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
752 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000753 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000754
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000755 unsigned StructPacking = 0;
756 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
757 StructPacking = PA->getAlignment();
758
Eli Friedman4bd998b2008-05-30 09:31:38 +0000759 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000760 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
761 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000762
Eli Friedman4bd998b2008-05-30 09:31:38 +0000763 // Layout each field, for now, just sequentially, respecting alignment. In
764 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000765 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000766 for (RecordDecl::field_iterator Field = D->field_begin(*this),
767 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000768 Field != FieldEnd; (void)++Field, ++FieldIdx)
769 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000770
771 // Finally, round the size of the total struct up to the alignment of the
772 // struct itself.
Sebastian Redl1590d9c2009-05-27 19:34:06 +0000773 NewEntry->FinalizeLayout(getLangOptions().CPlusPlus);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000774 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000775}
776
Chris Lattnera7674d82007-07-13 22:13:22 +0000777//===----------------------------------------------------------------------===//
778// Type creation/memoization methods
779//===----------------------------------------------------------------------===//
780
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000781QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000782 QualType CanT = getCanonicalType(T);
783 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000784 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000785
786 // If we are composing extended qualifiers together, merge together into one
787 // ExtQualType node.
788 unsigned CVRQuals = T.getCVRQualifiers();
789 QualType::GCAttrTypes GCAttr = QualType::GCNone;
790 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000791
Chris Lattnerb7d25532009-02-18 22:53:11 +0000792 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
793 // If this type already has an address space specified, it cannot get
794 // another one.
795 assert(EQT->getAddressSpace() == 0 &&
796 "Type cannot be in multiple addr spaces!");
797 GCAttr = EQT->getObjCGCAttr();
798 TypeNode = EQT->getBaseType();
799 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000800
Chris Lattnerb7d25532009-02-18 22:53:11 +0000801 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000802 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000803 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000804 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000805 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000806 return QualType(EXTQy, CVRQuals);
807
Christopher Lambebb97e92008-02-04 02:31:56 +0000808 // If the base type isn't canonical, this won't be a canonical type either,
809 // so fill in the canonical type field.
810 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000811 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000812 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000813
Chris Lattnerb7d25532009-02-18 22:53:11 +0000814 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000815 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000816 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000817 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000818 ExtQualType *New =
819 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000820 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000821 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000822 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000823}
824
Chris Lattnerb7d25532009-02-18 22:53:11 +0000825QualType ASTContext::getObjCGCQualType(QualType T,
826 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000827 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000828 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000829 return T;
830
Chris Lattnerb7d25532009-02-18 22:53:11 +0000831 // If we are composing extended qualifiers together, merge together into one
832 // ExtQualType node.
833 unsigned CVRQuals = T.getCVRQualifiers();
834 Type *TypeNode = T.getTypePtr();
835 unsigned AddressSpace = 0;
836
837 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
838 // If this type already has an address space specified, it cannot get
839 // another one.
840 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
841 "Type cannot be in multiple addr spaces!");
842 AddressSpace = EQT->getAddressSpace();
843 TypeNode = EQT->getBaseType();
844 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000845
846 // Check if we've already instantiated an gc qual'd type of this type.
847 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000848 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000849 void *InsertPos = 0;
850 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000851 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000852
853 // If the base type isn't canonical, this won't be a canonical type either,
854 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000855 // FIXME: Isn't this also not canonical if the base type is a array
856 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000857 QualType Canonical;
858 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000859 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000860
Chris Lattnerb7d25532009-02-18 22:53:11 +0000861 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000862 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
863 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
864 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000865 ExtQualType *New =
866 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000867 ExtQualTypes.InsertNode(New, InsertPos);
868 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000869 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000870}
Chris Lattnera7674d82007-07-13 22:13:22 +0000871
Reid Spencer5f016e22007-07-11 17:01:13 +0000872/// getComplexType - Return the uniqued reference to the type for a complex
873/// number with the specified element type.
874QualType ASTContext::getComplexType(QualType T) {
875 // Unique pointers, to guarantee there is only one pointer of a particular
876 // structure.
877 llvm::FoldingSetNodeID ID;
878 ComplexType::Profile(ID, T);
879
880 void *InsertPos = 0;
881 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
882 return QualType(CT, 0);
883
884 // If the pointee type isn't canonical, this won't be a canonical type either,
885 // so fill in the canonical type field.
886 QualType Canonical;
887 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000888 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000889
890 // Get the new insert position for the node we care about.
891 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000892 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 }
Steve Narofff83820b2009-01-27 22:08:43 +0000894 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 Types.push_back(New);
896 ComplexTypes.InsertNode(New, InsertPos);
897 return QualType(New, 0);
898}
899
Eli Friedmanf98aba32009-02-13 02:31:07 +0000900QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
901 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
902 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
903 FixedWidthIntType *&Entry = Map[Width];
904 if (!Entry)
905 Entry = new FixedWidthIntType(Width, Signed);
906 return QualType(Entry, 0);
907}
Reid Spencer5f016e22007-07-11 17:01:13 +0000908
909/// getPointerType - Return the uniqued reference to the type for a pointer to
910/// the specified type.
911QualType ASTContext::getPointerType(QualType T) {
912 // Unique pointers, to guarantee there is only one pointer of a particular
913 // structure.
914 llvm::FoldingSetNodeID ID;
915 PointerType::Profile(ID, T);
916
917 void *InsertPos = 0;
918 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
919 return QualType(PT, 0);
920
921 // If the pointee type isn't canonical, this won't be a canonical type either,
922 // so fill in the canonical type field.
923 QualType Canonical;
924 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000925 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000926
927 // Get the new insert position for the node we care about.
928 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000929 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 }
Steve Narofff83820b2009-01-27 22:08:43 +0000931 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000932 Types.push_back(New);
933 PointerTypes.InsertNode(New, InsertPos);
934 return QualType(New, 0);
935}
936
Steve Naroff5618bd42008-08-27 16:04:49 +0000937/// getBlockPointerType - Return the uniqued reference to the type for
938/// a pointer to the specified block.
939QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000940 assert(T->isFunctionType() && "block of function types only");
941 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000942 // structure.
943 llvm::FoldingSetNodeID ID;
944 BlockPointerType::Profile(ID, T);
945
946 void *InsertPos = 0;
947 if (BlockPointerType *PT =
948 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
949 return QualType(PT, 0);
950
Steve Naroff296e8d52008-08-28 19:20:44 +0000951 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000952 // type either so fill in the canonical type field.
953 QualType Canonical;
954 if (!T->isCanonical()) {
955 Canonical = getBlockPointerType(getCanonicalType(T));
956
957 // Get the new insert position for the node we care about.
958 BlockPointerType *NewIP =
959 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000960 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000961 }
Steve Narofff83820b2009-01-27 22:08:43 +0000962 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000963 Types.push_back(New);
964 BlockPointerTypes.InsertNode(New, InsertPos);
965 return QualType(New, 0);
966}
967
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000968/// getLValueReferenceType - Return the uniqued reference to the type for an
969/// lvalue reference to the specified type.
970QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 // Unique pointers, to guarantee there is only one pointer of a particular
972 // structure.
973 llvm::FoldingSetNodeID ID;
974 ReferenceType::Profile(ID, T);
975
976 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000977 if (LValueReferenceType *RT =
978 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000980
Reid Spencer5f016e22007-07-11 17:01:13 +0000981 // If the referencee type isn't canonical, this won't be a canonical type
982 // either, so fill in the canonical type field.
983 QualType Canonical;
984 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000985 Canonical = getLValueReferenceType(getCanonicalType(T));
986
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000988 LValueReferenceType *NewIP =
989 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000990 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 }
992
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000993 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000994 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000995 LValueReferenceTypes.InsertNode(New, InsertPos);
996 return QualType(New, 0);
997}
998
999/// getRValueReferenceType - Return the uniqued reference to the type for an
1000/// rvalue reference to the specified type.
1001QualType ASTContext::getRValueReferenceType(QualType T) {
1002 // Unique pointers, to guarantee there is only one pointer of a particular
1003 // structure.
1004 llvm::FoldingSetNodeID ID;
1005 ReferenceType::Profile(ID, T);
1006
1007 void *InsertPos = 0;
1008 if (RValueReferenceType *RT =
1009 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1010 return QualType(RT, 0);
1011
1012 // If the referencee type isn't canonical, this won't be a canonical type
1013 // either, so fill in the canonical type field.
1014 QualType Canonical;
1015 if (!T->isCanonical()) {
1016 Canonical = getRValueReferenceType(getCanonicalType(T));
1017
1018 // Get the new insert position for the node we care about.
1019 RValueReferenceType *NewIP =
1020 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1021 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1022 }
1023
1024 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1025 Types.push_back(New);
1026 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001027 return QualType(New, 0);
1028}
1029
Sebastian Redlf30208a2009-01-24 21:16:55 +00001030/// getMemberPointerType - Return the uniqued reference to the type for a
1031/// member pointer to the specified type, in the specified class.
1032QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1033{
1034 // Unique pointers, to guarantee there is only one pointer of a particular
1035 // structure.
1036 llvm::FoldingSetNodeID ID;
1037 MemberPointerType::Profile(ID, T, Cls);
1038
1039 void *InsertPos = 0;
1040 if (MemberPointerType *PT =
1041 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1042 return QualType(PT, 0);
1043
1044 // If the pointee or class type isn't canonical, this won't be a canonical
1045 // type either, so fill in the canonical type field.
1046 QualType Canonical;
1047 if (!T->isCanonical()) {
1048 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1049
1050 // Get the new insert position for the node we care about.
1051 MemberPointerType *NewIP =
1052 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1053 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1054 }
Steve Narofff83820b2009-01-27 22:08:43 +00001055 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001056 Types.push_back(New);
1057 MemberPointerTypes.InsertNode(New, InsertPos);
1058 return QualType(New, 0);
1059}
1060
Steve Narofffb22d962007-08-30 01:06:46 +00001061/// getConstantArrayType - Return the unique reference to the type for an
1062/// array of the specified element type.
1063QualType ASTContext::getConstantArrayType(QualType EltTy,
Chris Lattner38aeec72009-05-13 04:12:56 +00001064 const llvm::APInt &ArySizeIn,
Steve Naroffc9406122007-08-30 18:10:14 +00001065 ArrayType::ArraySizeModifier ASM,
1066 unsigned EltTypeQuals) {
Eli Friedman587cbdf2009-05-29 20:17:55 +00001067 assert((EltTy->isDependentType() || EltTy->isConstantSizeType()) &&
1068 "Constant array of VLAs is illegal!");
1069
Chris Lattner38aeec72009-05-13 04:12:56 +00001070 // Convert the array size into a canonical width matching the pointer size for
1071 // the target.
1072 llvm::APInt ArySize(ArySizeIn);
1073 ArySize.zextOrTrunc(Target.getPointerWidth(EltTy.getAddressSpace()));
1074
Reid Spencer5f016e22007-07-11 17:01:13 +00001075 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001076 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001077
1078 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001079 if (ConstantArrayType *ATP =
1080 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001081 return QualType(ATP, 0);
1082
1083 // If the element type isn't canonical, this won't be a canonical type either,
1084 // so fill in the canonical type field.
1085 QualType Canonical;
1086 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001087 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001088 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001089 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001090 ConstantArrayType *NewIP =
1091 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001092 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 }
1094
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001095 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001096 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001097 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001098 Types.push_back(New);
1099 return QualType(New, 0);
1100}
1101
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001102/// getVariableArrayType - Returns a non-unique reference to the type for a
1103/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001104QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1105 ArrayType::ArraySizeModifier ASM,
1106 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001107 // Since we don't unique expressions, it isn't possible to unique VLA's
1108 // that have an expression provided for their size.
1109
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001110 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001111 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001112
1113 VariableArrayTypes.push_back(New);
1114 Types.push_back(New);
1115 return QualType(New, 0);
1116}
1117
Douglas Gregor898574e2008-12-05 23:32:09 +00001118/// getDependentSizedArrayType - Returns a non-unique reference to
1119/// the type for a dependently-sized array of the specified element
1120/// type. FIXME: We will need these to be uniqued, or at least
1121/// comparable, at some point.
1122QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1123 ArrayType::ArraySizeModifier ASM,
1124 unsigned EltTypeQuals) {
1125 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1126 "Size must be type- or value-dependent!");
1127
1128 // Since we don't unique expressions, it isn't possible to unique
1129 // dependently-sized array types.
1130
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001131 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001132 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1133 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001134
1135 DependentSizedArrayTypes.push_back(New);
1136 Types.push_back(New);
1137 return QualType(New, 0);
1138}
1139
Eli Friedmanc5773c42008-02-15 18:16:39 +00001140QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1141 ArrayType::ArraySizeModifier ASM,
1142 unsigned EltTypeQuals) {
1143 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001144 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001145
1146 void *InsertPos = 0;
1147 if (IncompleteArrayType *ATP =
1148 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1149 return QualType(ATP, 0);
1150
1151 // If the element type isn't canonical, this won't be a canonical type
1152 // either, so fill in the canonical type field.
1153 QualType Canonical;
1154
1155 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001156 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001157 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001158
1159 // Get the new insert position for the node we care about.
1160 IncompleteArrayType *NewIP =
1161 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001162 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001163 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001164
Steve Narofff83820b2009-01-27 22:08:43 +00001165 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001166 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001167
1168 IncompleteArrayTypes.InsertNode(New, InsertPos);
1169 Types.push_back(New);
1170 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001171}
1172
Steve Naroff73322922007-07-18 18:00:27 +00001173/// getVectorType - Return the unique reference to a vector type of
1174/// the specified element type and size. VectorType must be a built-in type.
1175QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 BuiltinType *baseType;
1177
Chris Lattnerf52ab252008-04-06 22:59:24 +00001178 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001179 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001180
1181 // Check if we've already instantiated a vector of this type.
1182 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001183 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 void *InsertPos = 0;
1185 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1186 return QualType(VTP, 0);
1187
1188 // If the element type isn't canonical, this won't be a canonical type either,
1189 // so fill in the canonical type field.
1190 QualType Canonical;
1191 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001192 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001193
1194 // Get the new insert position for the node we care about.
1195 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001196 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 }
Steve Narofff83820b2009-01-27 22:08:43 +00001198 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 VectorTypes.InsertNode(New, InsertPos);
1200 Types.push_back(New);
1201 return QualType(New, 0);
1202}
1203
Nate Begeman213541a2008-04-18 23:10:10 +00001204/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001205/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001206QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001207 BuiltinType *baseType;
1208
Chris Lattnerf52ab252008-04-06 22:59:24 +00001209 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001210 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001211
1212 // Check if we've already instantiated a vector of this type.
1213 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001214 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001215 void *InsertPos = 0;
1216 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1217 return QualType(VTP, 0);
1218
1219 // If the element type isn't canonical, this won't be a canonical type either,
1220 // so fill in the canonical type field.
1221 QualType Canonical;
1222 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001223 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001224
1225 // Get the new insert position for the node we care about.
1226 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001227 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001228 }
Steve Narofff83820b2009-01-27 22:08:43 +00001229 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001230 VectorTypes.InsertNode(New, InsertPos);
1231 Types.push_back(New);
1232 return QualType(New, 0);
1233}
1234
Douglas Gregor72564e72009-02-26 23:50:07 +00001235/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001236///
Douglas Gregor72564e72009-02-26 23:50:07 +00001237QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 // Unique functions, to guarantee there is only one function of a particular
1239 // structure.
1240 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001241 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001242
1243 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001244 if (FunctionNoProtoType *FT =
1245 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 return QualType(FT, 0);
1247
1248 QualType Canonical;
1249 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001250 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001251
1252 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001253 FunctionNoProtoType *NewIP =
1254 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001255 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 }
1257
Douglas Gregor72564e72009-02-26 23:50:07 +00001258 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001259 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001260 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 return QualType(New, 0);
1262}
1263
1264/// getFunctionType - Return a normal function type with a typed argument
1265/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001266QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001267 unsigned NumArgs, bool isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001268 unsigned TypeQuals, bool hasExceptionSpec,
1269 bool hasAnyExceptionSpec, unsigned NumExs,
1270 const QualType *ExArray) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 // Unique functions, to guarantee there is only one function of a particular
1272 // structure.
1273 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001274 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001275 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1276 NumExs, ExArray);
Reid Spencer5f016e22007-07-11 17:01:13 +00001277
1278 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001279 if (FunctionProtoType *FTP =
1280 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 return QualType(FTP, 0);
Sebastian Redl465226e2009-05-27 22:11:52 +00001282
1283 // Determine whether the type being created is already canonical or not.
Reid Spencer5f016e22007-07-11 17:01:13 +00001284 bool isCanonical = ResultTy->isCanonical();
Sebastian Redl465226e2009-05-27 22:11:52 +00001285 if (hasExceptionSpec)
1286 isCanonical = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001287 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1288 if (!ArgArray[i]->isCanonical())
1289 isCanonical = false;
1290
1291 // If this type isn't canonical, get the canonical version of it.
Sebastian Redl465226e2009-05-27 22:11:52 +00001292 // The exception spec is not part of the canonical type.
Reid Spencer5f016e22007-07-11 17:01:13 +00001293 QualType Canonical;
1294 if (!isCanonical) {
1295 llvm::SmallVector<QualType, 16> CanonicalArgs;
1296 CanonicalArgs.reserve(NumArgs);
1297 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001298 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl465226e2009-05-27 22:11:52 +00001299
Chris Lattnerf52ab252008-04-06 22:59:24 +00001300 Canonical = getFunctionType(getCanonicalType(ResultTy),
Jay Foadbeaaccd2009-05-21 09:52:38 +00001301 CanonicalArgs.data(), NumArgs,
Sebastian Redlbfa2fcb2009-05-06 23:27:55 +00001302 isVariadic, TypeQuals);
Sebastian Redl465226e2009-05-27 22:11:52 +00001303
Reid Spencer5f016e22007-07-11 17:01:13 +00001304 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001305 FunctionProtoType *NewIP =
1306 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001307 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 }
Sebastian Redl465226e2009-05-27 22:11:52 +00001309
Douglas Gregor72564e72009-02-26 23:50:07 +00001310 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl465226e2009-05-27 22:11:52 +00001311 // for two variable size arrays (for parameter and exception types) at the
1312 // end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001313 FunctionProtoType *FTP =
Sebastian Redl465226e2009-05-27 22:11:52 +00001314 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1315 NumArgs*sizeof(QualType) +
1316 NumExs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001317 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl465226e2009-05-27 22:11:52 +00001318 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1319 ExArray, NumExs, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001320 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001321 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001322 return QualType(FTP, 0);
1323}
1324
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001325/// getTypeDeclType - Return the unique reference to the type for the
1326/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001327QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001328 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001329 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1330
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001331 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001332 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001333 else if (isa<TemplateTypeParmDecl>(Decl)) {
1334 assert(false && "Template type parameter types are always available.");
1335 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001336 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001337
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001338 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001339 if (PrevDecl)
1340 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001341 else
1342 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001343 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001344 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1345 if (PrevDecl)
1346 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001347 else
1348 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001349 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001350 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001351 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001352
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001353 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001354 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001355}
1356
Reid Spencer5f016e22007-07-11 17:01:13 +00001357/// getTypedefType - Return the unique reference to the type for the
1358/// specified typename decl.
1359QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1360 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1361
Chris Lattnerf52ab252008-04-06 22:59:24 +00001362 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001363 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001364 Types.push_back(Decl->TypeForDecl);
1365 return QualType(Decl->TypeForDecl, 0);
1366}
1367
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001368/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001369/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001370QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001371 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1372
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001373 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1374 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001375 Types.push_back(Decl->TypeForDecl);
1376 return QualType(Decl->TypeForDecl, 0);
1377}
1378
Douglas Gregorfab9d672009-02-05 23:33:38 +00001379/// \brief Retrieve the template type parameter type for a template
1380/// parameter with the given depth, index, and (optionally) name.
1381QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1382 IdentifierInfo *Name) {
1383 llvm::FoldingSetNodeID ID;
1384 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1385 void *InsertPos = 0;
1386 TemplateTypeParmType *TypeParm
1387 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1388
1389 if (TypeParm)
1390 return QualType(TypeParm, 0);
1391
1392 if (Name)
1393 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1394 getTemplateTypeParmType(Depth, Index));
1395 else
1396 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1397
1398 Types.push_back(TypeParm);
1399 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1400
1401 return QualType(TypeParm, 0);
1402}
1403
Douglas Gregor55f6b142009-02-09 18:46:07 +00001404QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001405ASTContext::getTemplateSpecializationType(TemplateName Template,
1406 const TemplateArgument *Args,
1407 unsigned NumArgs,
1408 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001409 if (!Canon.isNull())
1410 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001411
Douglas Gregor55f6b142009-02-09 18:46:07 +00001412 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001413 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001414
Douglas Gregor55f6b142009-02-09 18:46:07 +00001415 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001416 TemplateSpecializationType *Spec
1417 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001418
1419 if (Spec)
1420 return QualType(Spec, 0);
1421
Douglas Gregor7532dc62009-03-30 22:58:21 +00001422 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001423 sizeof(TemplateArgument) * NumArgs),
1424 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001425 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001426 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001427 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001428
1429 return QualType(Spec, 0);
1430}
1431
Douglas Gregore4e5b052009-03-19 00:18:19 +00001432QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001433ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001434 QualType NamedType) {
1435 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001436 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001437
1438 void *InsertPos = 0;
1439 QualifiedNameType *T
1440 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1441 if (T)
1442 return QualType(T, 0);
1443
Douglas Gregorab452ba2009-03-26 23:50:42 +00001444 T = new (*this) QualifiedNameType(NNS, NamedType,
1445 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001446 Types.push_back(T);
1447 QualifiedNameTypes.InsertNode(T, InsertPos);
1448 return QualType(T, 0);
1449}
1450
Douglas Gregord57959a2009-03-27 23:10:48 +00001451QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1452 const IdentifierInfo *Name,
1453 QualType Canon) {
1454 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1455
1456 if (Canon.isNull()) {
1457 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1458 if (CanonNNS != NNS)
1459 Canon = getTypenameType(CanonNNS, Name);
1460 }
1461
1462 llvm::FoldingSetNodeID ID;
1463 TypenameType::Profile(ID, NNS, Name);
1464
1465 void *InsertPos = 0;
1466 TypenameType *T
1467 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1468 if (T)
1469 return QualType(T, 0);
1470
1471 T = new (*this) TypenameType(NNS, Name, Canon);
1472 Types.push_back(T);
1473 TypenameTypes.InsertNode(T, InsertPos);
1474 return QualType(T, 0);
1475}
1476
Douglas Gregor17343172009-04-01 00:28:59 +00001477QualType
1478ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1479 const TemplateSpecializationType *TemplateId,
1480 QualType Canon) {
1481 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1482
1483 if (Canon.isNull()) {
1484 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1485 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1486 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1487 const TemplateSpecializationType *CanonTemplateId
1488 = CanonType->getAsTemplateSpecializationType();
1489 assert(CanonTemplateId &&
1490 "Canonical type must also be a template specialization type");
1491 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1492 }
1493 }
1494
1495 llvm::FoldingSetNodeID ID;
1496 TypenameType::Profile(ID, NNS, TemplateId);
1497
1498 void *InsertPos = 0;
1499 TypenameType *T
1500 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1501 if (T)
1502 return QualType(T, 0);
1503
1504 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1505 Types.push_back(T);
1506 TypenameTypes.InsertNode(T, InsertPos);
1507 return QualType(T, 0);
1508}
1509
Chris Lattner88cb27a2008-04-07 04:56:42 +00001510/// CmpProtocolNames - Comparison predicate for sorting protocols
1511/// alphabetically.
1512static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1513 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001514 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001515}
1516
1517static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1518 unsigned &NumProtocols) {
1519 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1520
1521 // Sort protocols, keyed by name.
1522 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1523
1524 // Remove duplicates.
1525 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1526 NumProtocols = ProtocolsEnd-Protocols;
1527}
1528
1529
Chris Lattner065f0d72008-04-07 04:44:08 +00001530/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1531/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001532QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1533 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001534 // Sort the protocol list alphabetically to canonicalize it.
1535 SortAndUniqueProtocols(Protocols, NumProtocols);
1536
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001537 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001538 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001539
1540 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001541 if (ObjCQualifiedInterfaceType *QT =
1542 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001543 return QualType(QT, 0);
1544
1545 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001546 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001547 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001548
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001549 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001550 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001551 return QualType(QType, 0);
1552}
1553
Chris Lattner88cb27a2008-04-07 04:56:42 +00001554/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1555/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001556QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001557 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001558 // Sort the protocol list alphabetically to canonicalize it.
1559 SortAndUniqueProtocols(Protocols, NumProtocols);
1560
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001561 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001562 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001563
1564 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001565 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001566 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001567 return QualType(QT, 0);
1568
1569 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001570 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001571 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001572 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001573 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001574 return QualType(QType, 0);
1575}
1576
Douglas Gregor72564e72009-02-26 23:50:07 +00001577/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1578/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001579/// multiple declarations that refer to "typeof(x)" all contain different
1580/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1581/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001582QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001583 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001584 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001585 Types.push_back(toe);
1586 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001587}
1588
Steve Naroff9752f252007-08-01 18:02:17 +00001589/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1590/// TypeOfType AST's. The only motivation to unique these nodes would be
1591/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1592/// an issue. This doesn't effect the type checker, since it operates
1593/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001594QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001595 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001596 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001597 Types.push_back(tot);
1598 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001599}
1600
Reid Spencer5f016e22007-07-11 17:01:13 +00001601/// getTagDeclType - Return the unique reference to the type for the
1602/// specified TagDecl (struct/union/class/enum) decl.
1603QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001604 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001605 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001606}
1607
1608/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1609/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1610/// needs to agree with the definition in <stddef.h>.
1611QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001612 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001613}
1614
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001615/// getSignedWCharType - Return the type of "signed wchar_t".
1616/// Used when in C++, as a GCC extension.
1617QualType ASTContext::getSignedWCharType() const {
1618 // FIXME: derive from "Target" ?
1619 return WCharTy;
1620}
1621
1622/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1623/// Used when in C++, as a GCC extension.
1624QualType ASTContext::getUnsignedWCharType() const {
1625 // FIXME: derive from "Target" ?
1626 return UnsignedIntTy;
1627}
1628
Chris Lattner8b9023b2007-07-13 03:05:23 +00001629/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1630/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1631QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001632 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001633}
1634
Chris Lattnere6327742008-04-02 05:18:44 +00001635//===----------------------------------------------------------------------===//
1636// Type Operators
1637//===----------------------------------------------------------------------===//
1638
Chris Lattner77c96472008-04-06 22:41:35 +00001639/// getCanonicalType - Return the canonical (structural) type corresponding to
1640/// the specified potentially non-canonical type. The non-canonical version
1641/// of a type may have many "decorated" versions of types. Decorators can
1642/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1643/// to be free of any of these, allowing two canonical types to be compared
1644/// for exact equality with a simple pointer comparison.
1645QualType ASTContext::getCanonicalType(QualType T) {
1646 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001647
1648 // If the result has type qualifiers, make sure to canonicalize them as well.
1649 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1650 if (TypeQuals == 0) return CanType;
1651
1652 // If the type qualifiers are on an array type, get the canonical type of the
1653 // array with the qualifiers applied to the element type.
1654 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1655 if (!AT)
1656 return CanType.getQualifiedType(TypeQuals);
1657
1658 // Get the canonical version of the element with the extra qualifiers on it.
1659 // This can recursively sink qualifiers through multiple levels of arrays.
1660 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1661 NewEltTy = getCanonicalType(NewEltTy);
1662
1663 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1664 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1665 CAT->getIndexTypeQualifier());
1666 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1667 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1668 IAT->getIndexTypeQualifier());
1669
Douglas Gregor898574e2008-12-05 23:32:09 +00001670 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1671 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1672 DSAT->getSizeModifier(),
1673 DSAT->getIndexTypeQualifier());
1674
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001675 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1676 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1677 VAT->getSizeModifier(),
1678 VAT->getIndexTypeQualifier());
1679}
1680
Douglas Gregor7da97d02009-05-10 22:57:19 +00001681Decl *ASTContext::getCanonicalDecl(Decl *D) {
Douglas Gregorc4ccf012009-05-10 22:59:12 +00001682 if (!D)
1683 return 0;
1684
Douglas Gregor7da97d02009-05-10 22:57:19 +00001685 if (TagDecl *Tag = dyn_cast<TagDecl>(D)) {
1686 QualType T = getTagDeclType(Tag);
1687 return cast<TagDecl>(cast<TagType>(T.getTypePtr()->CanonicalType)
1688 ->getDecl());
1689 }
1690
1691 if (ClassTemplateDecl *Template = dyn_cast<ClassTemplateDecl>(D)) {
1692 while (Template->getPreviousDeclaration())
1693 Template = Template->getPreviousDeclaration();
1694 return Template;
1695 }
1696
1697 if (const FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) {
1698 while (Function->getPreviousDeclaration())
1699 Function = Function->getPreviousDeclaration();
1700 return const_cast<FunctionDecl *>(Function);
1701 }
1702
1703 if (const VarDecl *Var = dyn_cast<VarDecl>(D)) {
1704 while (Var->getPreviousDeclaration())
1705 Var = Var->getPreviousDeclaration();
1706 return const_cast<VarDecl *>(Var);
1707 }
1708
1709 return D;
1710}
1711
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001712TemplateName ASTContext::getCanonicalTemplateName(TemplateName Name) {
1713 // If this template name refers to a template, the canonical
1714 // template name merely stores the template itself.
1715 if (TemplateDecl *Template = Name.getAsTemplateDecl())
Douglas Gregor7da97d02009-05-10 22:57:19 +00001716 return TemplateName(cast<TemplateDecl>(getCanonicalDecl(Template)));
Douglas Gregor25a3ef72009-05-07 06:41:52 +00001717
1718 DependentTemplateName *DTN = Name.getAsDependentTemplateName();
1719 assert(DTN && "Non-dependent template names must refer to template decls.");
1720 return DTN->CanonicalTemplateName;
1721}
1722
Douglas Gregord57959a2009-03-27 23:10:48 +00001723NestedNameSpecifier *
1724ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1725 if (!NNS)
1726 return 0;
1727
1728 switch (NNS->getKind()) {
1729 case NestedNameSpecifier::Identifier:
1730 // Canonicalize the prefix but keep the identifier the same.
1731 return NestedNameSpecifier::Create(*this,
1732 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1733 NNS->getAsIdentifier());
1734
1735 case NestedNameSpecifier::Namespace:
1736 // A namespace is canonical; build a nested-name-specifier with
1737 // this namespace and no prefix.
1738 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1739
1740 case NestedNameSpecifier::TypeSpec:
1741 case NestedNameSpecifier::TypeSpecWithTemplate: {
1742 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1743 NestedNameSpecifier *Prefix = 0;
1744
1745 // FIXME: This isn't the right check!
1746 if (T->isDependentType())
1747 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1748
1749 return NestedNameSpecifier::Create(*this, Prefix,
1750 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1751 T.getTypePtr());
1752 }
1753
1754 case NestedNameSpecifier::Global:
1755 // The global specifier is canonical and unique.
1756 return NNS;
1757 }
1758
1759 // Required to silence a GCC warning
1760 return 0;
1761}
1762
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001763
1764const ArrayType *ASTContext::getAsArrayType(QualType T) {
1765 // Handle the non-qualified case efficiently.
1766 if (T.getCVRQualifiers() == 0) {
1767 // Handle the common positive case fast.
1768 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1769 return AT;
1770 }
1771
1772 // Handle the common negative case fast, ignoring CVR qualifiers.
1773 QualType CType = T->getCanonicalTypeInternal();
1774
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001775 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001776 // test.
1777 if (!isa<ArrayType>(CType) &&
1778 !isa<ArrayType>(CType.getUnqualifiedType()))
1779 return 0;
1780
1781 // Apply any CVR qualifiers from the array type to the element type. This
1782 // implements C99 6.7.3p8: "If the specification of an array type includes
1783 // any type qualifiers, the element type is so qualified, not the array type."
1784
1785 // If we get here, we either have type qualifiers on the type, or we have
1786 // sugar such as a typedef in the way. If we have type qualifiers on the type
1787 // we must propagate them down into the elemeng type.
1788 unsigned CVRQuals = T.getCVRQualifiers();
1789 unsigned AddrSpace = 0;
1790 Type *Ty = T.getTypePtr();
1791
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001792 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001793 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001794 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1795 AddrSpace = EXTQT->getAddressSpace();
1796 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001797 } else {
1798 T = Ty->getDesugaredType();
1799 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1800 break;
1801 CVRQuals |= T.getCVRQualifiers();
1802 Ty = T.getTypePtr();
1803 }
1804 }
1805
1806 // If we have a simple case, just return now.
1807 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1808 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1809 return ATy;
1810
1811 // Otherwise, we have an array and we have qualifiers on it. Push the
1812 // qualifiers into the array element type and return a new array type.
1813 // Get the canonical version of the element with the extra qualifiers on it.
1814 // This can recursively sink qualifiers through multiple levels of arrays.
1815 QualType NewEltTy = ATy->getElementType();
1816 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001817 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001818 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1819
1820 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1821 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1822 CAT->getSizeModifier(),
1823 CAT->getIndexTypeQualifier()));
1824 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1825 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1826 IAT->getSizeModifier(),
1827 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001828
Douglas Gregor898574e2008-12-05 23:32:09 +00001829 if (const DependentSizedArrayType *DSAT
1830 = dyn_cast<DependentSizedArrayType>(ATy))
1831 return cast<ArrayType>(
1832 getDependentSizedArrayType(NewEltTy,
1833 DSAT->getSizeExpr(),
1834 DSAT->getSizeModifier(),
1835 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001836
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001837 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1838 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1839 VAT->getSizeModifier(),
1840 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001841}
1842
1843
Chris Lattnere6327742008-04-02 05:18:44 +00001844/// getArrayDecayedType - Return the properly qualified result of decaying the
1845/// specified array type to a pointer. This operation is non-trivial when
1846/// handling typedefs etc. The canonical type of "T" must be an array type,
1847/// this returns a pointer to a properly qualified element of the array.
1848///
1849/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1850QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001851 // Get the element type with 'getAsArrayType' so that we don't lose any
1852 // typedefs in the element type of the array. This also handles propagation
1853 // of type qualifiers from the array type into the element type if present
1854 // (C99 6.7.3p8).
1855 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1856 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001857
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001858 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001859
1860 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001861 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001862}
1863
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001864QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001865 QualType ElemTy = VAT->getElementType();
1866
1867 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1868 return getBaseElementType(VAT);
1869
1870 return ElemTy;
1871}
1872
Reid Spencer5f016e22007-07-11 17:01:13 +00001873/// getFloatingRank - Return a relative rank for floating point types.
1874/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001875static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001876 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001877 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001878
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001879 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001880 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001881 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001882 case BuiltinType::Float: return FloatRank;
1883 case BuiltinType::Double: return DoubleRank;
1884 case BuiltinType::LongDouble: return LongDoubleRank;
1885 }
1886}
1887
Steve Naroff716c7302007-08-27 01:41:48 +00001888/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1889/// point or a complex type (based on typeDomain/typeSize).
1890/// 'typeDomain' is a real floating point or complex type.
1891/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001892QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1893 QualType Domain) const {
1894 FloatingRank EltRank = getFloatingRank(Size);
1895 if (Domain->isComplexType()) {
1896 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001897 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001898 case FloatRank: return FloatComplexTy;
1899 case DoubleRank: return DoubleComplexTy;
1900 case LongDoubleRank: return LongDoubleComplexTy;
1901 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001902 }
Chris Lattner1361b112008-04-06 23:58:54 +00001903
1904 assert(Domain->isRealFloatingType() && "Unknown domain!");
1905 switch (EltRank) {
1906 default: assert(0 && "getFloatingRank(): illegal value for rank");
1907 case FloatRank: return FloatTy;
1908 case DoubleRank: return DoubleTy;
1909 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001910 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001911}
1912
Chris Lattner7cfeb082008-04-06 23:55:33 +00001913/// getFloatingTypeOrder - Compare the rank of the two specified floating
1914/// point types, ignoring the domain of the type (i.e. 'double' ==
1915/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1916/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001917int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1918 FloatingRank LHSR = getFloatingRank(LHS);
1919 FloatingRank RHSR = getFloatingRank(RHS);
1920
1921 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001922 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001923 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001924 return 1;
1925 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001926}
1927
Chris Lattnerf52ab252008-04-06 22:59:24 +00001928/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1929/// routine will assert if passed a built-in type that isn't an integer or enum,
1930/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001931unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001932 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001933 if (EnumType* ET = dyn_cast<EnumType>(T))
1934 T = ET->getDecl()->getIntegerType().getTypePtr();
1935
1936 // There are two things which impact the integer rank: the width, and
1937 // the ordering of builtins. The builtin ordering is encoded in the
1938 // bottom three bits; the width is encoded in the bits above that.
1939 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1940 return FWIT->getWidth() << 3;
1941 }
1942
Chris Lattnerf52ab252008-04-06 22:59:24 +00001943 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001944 default: assert(0 && "getIntegerRank(): not a built-in integer");
1945 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001946 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001947 case BuiltinType::Char_S:
1948 case BuiltinType::Char_U:
1949 case BuiltinType::SChar:
1950 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001951 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001952 case BuiltinType::Short:
1953 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001954 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001955 case BuiltinType::Int:
1956 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001957 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001958 case BuiltinType::Long:
1959 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001960 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001961 case BuiltinType::LongLong:
1962 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001963 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner2df9ced2009-04-30 02:43:43 +00001964 case BuiltinType::Int128:
1965 case BuiltinType::UInt128:
1966 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001967 }
1968}
1969
Chris Lattner7cfeb082008-04-06 23:55:33 +00001970/// getIntegerTypeOrder - Returns the highest ranked integer type:
1971/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1972/// LHS < RHS, return -1.
1973int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001974 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1975 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001976 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001977
Chris Lattnerf52ab252008-04-06 22:59:24 +00001978 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1979 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001980
Chris Lattner7cfeb082008-04-06 23:55:33 +00001981 unsigned LHSRank = getIntegerRank(LHSC);
1982 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001983
Chris Lattner7cfeb082008-04-06 23:55:33 +00001984 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1985 if (LHSRank == RHSRank) return 0;
1986 return LHSRank > RHSRank ? 1 : -1;
1987 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001988
Chris Lattner7cfeb082008-04-06 23:55:33 +00001989 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1990 if (LHSUnsigned) {
1991 // If the unsigned [LHS] type is larger, return it.
1992 if (LHSRank >= RHSRank)
1993 return 1;
1994
1995 // If the signed type can represent all values of the unsigned type, it
1996 // wins. Because we are dealing with 2's complement and types that are
1997 // powers of two larger than each other, this is always safe.
1998 return -1;
1999 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00002000
Chris Lattner7cfeb082008-04-06 23:55:33 +00002001 // If the unsigned [RHS] type is larger, return it.
2002 if (RHSRank >= LHSRank)
2003 return -1;
2004
2005 // If the signed type can represent all values of the unsigned type, it
2006 // wins. Because we are dealing with 2's complement and types that are
2007 // powers of two larger than each other, this is always safe.
2008 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00002009}
Anders Carlsson71993dd2007-08-17 05:31:46 +00002010
2011// getCFConstantStringType - Return the type used for constant CFStrings.
2012QualType ASTContext::getCFConstantStringType() {
2013 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00002014 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00002015 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00002016 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002017 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00002018
2019 // const int *isa;
2020 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00002021 // int flags;
2022 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00002023 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002024 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00002025 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00002026 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00002027
Anders Carlsson71993dd2007-08-17 05:31:46 +00002028 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00002029 for (unsigned i = 0; i < 4; ++i) {
2030 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2031 SourceLocation(), 0,
2032 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002033 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002034 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002035 }
2036
2037 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00002038 }
2039
2040 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00002041}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002042
Douglas Gregor319ac892009-04-23 22:29:11 +00002043void ASTContext::setCFConstantStringType(QualType T) {
2044 const RecordType *Rec = T->getAsRecordType();
2045 assert(Rec && "Invalid CFConstantStringType");
2046 CFConstantStringTypeDecl = Rec->getDecl();
2047}
2048
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002049QualType ASTContext::getObjCFastEnumerationStateType()
2050{
2051 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002052 ObjCFastEnumerationStateTypeDecl =
2053 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2054 &Idents.get("__objcFastEnumerationState"));
2055
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002056 QualType FieldTypes[] = {
2057 UnsignedLongTy,
2058 getPointerType(ObjCIdType),
2059 getPointerType(UnsignedLongTy),
2060 getConstantArrayType(UnsignedLongTy,
2061 llvm::APInt(32, 5), ArrayType::Normal, 0)
2062 };
2063
Douglas Gregor44b43212008-12-11 16:49:14 +00002064 for (size_t i = 0; i < 4; ++i) {
2065 FieldDecl *Field = FieldDecl::Create(*this,
2066 ObjCFastEnumerationStateTypeDecl,
2067 SourceLocation(), 0,
2068 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002069 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002070 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002071 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002072
Douglas Gregor44b43212008-12-11 16:49:14 +00002073 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002074 }
2075
2076 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2077}
2078
Douglas Gregor319ac892009-04-23 22:29:11 +00002079void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2080 const RecordType *Rec = T->getAsRecordType();
2081 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2082 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2083}
2084
Anders Carlssone8c49532007-10-29 06:33:42 +00002085// This returns true if a type has been typedefed to BOOL:
2086// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002087static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002088 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002089 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2090 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002091
2092 return false;
2093}
2094
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002095/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002096/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002097int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002098 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002099
2100 // Make all integer and enum types at least as large as an int
2101 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002102 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002103 // Treat arrays as pointers, since that's how they're passed in.
2104 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002105 sz = getTypeSize(VoidPtrTy);
2106 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002107}
2108
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002109/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002110/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002111void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002112 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002113 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002114 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002115 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002116 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002117 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002118 // Compute size of all parameters.
2119 // Start with computing size of a pointer in number of bytes.
2120 // FIXME: There might(should) be a better way of doing this computation!
2121 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002122 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002123 // The first two arguments (self and _cmd) are pointers; account for
2124 // their size.
2125 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002126 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2127 E = Decl->param_end(); PI != E; ++PI) {
2128 QualType PType = (*PI)->getType();
2129 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002130 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002131 ParmOffset += sz;
2132 }
2133 S += llvm::utostr(ParmOffset);
2134 S += "@0:";
2135 S += llvm::utostr(PtrSize);
2136
2137 // Argument types.
2138 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002139 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2140 E = Decl->param_end(); PI != E; ++PI) {
2141 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002142 QualType PType = PVDecl->getOriginalType();
2143 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002144 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2145 // Use array's original type only if it has known number of
2146 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002147 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002148 PType = PVDecl->getType();
2149 } else if (PType->isFunctionType())
2150 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002151 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002152 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002153 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002154 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002155 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002156 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002157 }
2158}
2159
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002160/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002161/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002162/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2163/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002164/// Property attributes are stored as a comma-delimited C string. The simple
2165/// attributes readonly and bycopy are encoded as single characters. The
2166/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2167/// encoded as single characters, followed by an identifier. Property types
2168/// are also encoded as a parametrized attribute. The characters used to encode
2169/// these attributes are defined by the following enumeration:
2170/// @code
2171/// enum PropertyAttributes {
2172/// kPropertyReadOnly = 'R', // property is read-only.
2173/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2174/// kPropertyByref = '&', // property is a reference to the value last assigned
2175/// kPropertyDynamic = 'D', // property is dynamic
2176/// kPropertyGetter = 'G', // followed by getter selector name
2177/// kPropertySetter = 'S', // followed by setter selector name
2178/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2179/// kPropertyType = 't' // followed by old-style type encoding.
2180/// kPropertyWeak = 'W' // 'weak' property
2181/// kPropertyStrong = 'P' // property GC'able
2182/// kPropertyNonAtomic = 'N' // property non-atomic
2183/// };
2184/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002185void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2186 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002187 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002188 // Collect information from the property implementation decl(s).
2189 bool Dynamic = false;
2190 ObjCPropertyImplDecl *SynthesizePID = 0;
2191
2192 // FIXME: Duplicated code due to poor abstraction.
2193 if (Container) {
2194 if (const ObjCCategoryImplDecl *CID =
2195 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2196 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002197 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2198 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002199 ObjCPropertyImplDecl *PID = *i;
2200 if (PID->getPropertyDecl() == PD) {
2201 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2202 Dynamic = true;
2203 } else {
2204 SynthesizePID = PID;
2205 }
2206 }
2207 }
2208 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002209 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002210 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregor653f1b12009-04-23 01:02:12 +00002211 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2212 i != e; ++i) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002213 ObjCPropertyImplDecl *PID = *i;
2214 if (PID->getPropertyDecl() == PD) {
2215 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2216 Dynamic = true;
2217 } else {
2218 SynthesizePID = PID;
2219 }
2220 }
2221 }
2222 }
2223 }
2224
2225 // FIXME: This is not very efficient.
2226 S = "T";
2227
2228 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002229 // GCC has some special rules regarding encoding of properties which
2230 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002231 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002232 true /* outermost type */,
2233 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002234
2235 if (PD->isReadOnly()) {
2236 S += ",R";
2237 } else {
2238 switch (PD->getSetterKind()) {
2239 case ObjCPropertyDecl::Assign: break;
2240 case ObjCPropertyDecl::Copy: S += ",C"; break;
2241 case ObjCPropertyDecl::Retain: S += ",&"; break;
2242 }
2243 }
2244
2245 // It really isn't clear at all what this means, since properties
2246 // are "dynamic by default".
2247 if (Dynamic)
2248 S += ",D";
2249
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002250 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2251 S += ",N";
2252
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002253 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2254 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002255 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002256 }
2257
2258 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2259 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002260 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002261 }
2262
2263 if (SynthesizePID) {
2264 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2265 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002266 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002267 }
2268
2269 // FIXME: OBJCGC: weak & strong
2270}
2271
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002272/// getLegacyIntegralTypeEncoding -
2273/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002274/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002275/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2276///
2277void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2278 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2279 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002280 if (BT->getKind() == BuiltinType::ULong &&
2281 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002282 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002283 else
2284 if (BT->getKind() == BuiltinType::Long &&
2285 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002286 PointeeTy = IntTy;
2287 }
2288 }
2289}
2290
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002291void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002292 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002293 // We follow the behavior of gcc, expanding structures which are
2294 // directly pointed to, and expanding embedded structures. Note that
2295 // these rules are sufficient to prevent recursive encoding of the
2296 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002297 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2298 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002299}
2300
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002301static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002302 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002303 const Expr *E = FD->getBitWidth();
2304 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2305 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman9a901bb2009-04-26 19:19:15 +00002306 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002307 S += 'b';
2308 S += llvm::utostr(N);
2309}
2310
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002311void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2312 bool ExpandPointedToStructures,
2313 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002314 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002315 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002316 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002317 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002318 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002319 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002320 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002321 else {
2322 char encoding;
2323 switch (BT->getKind()) {
2324 default: assert(0 && "Unhandled builtin type kind");
2325 case BuiltinType::Void: encoding = 'v'; break;
2326 case BuiltinType::Bool: encoding = 'B'; break;
2327 case BuiltinType::Char_U:
2328 case BuiltinType::UChar: encoding = 'C'; break;
2329 case BuiltinType::UShort: encoding = 'S'; break;
2330 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002331 case BuiltinType::ULong:
2332 encoding =
2333 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2334 break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002335 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002336 case BuiltinType::ULongLong: encoding = 'Q'; break;
2337 case BuiltinType::Char_S:
2338 case BuiltinType::SChar: encoding = 'c'; break;
2339 case BuiltinType::Short: encoding = 's'; break;
2340 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002341 case BuiltinType::Long:
2342 encoding =
2343 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2344 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002345 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner2df9ced2009-04-30 02:43:43 +00002346 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002347 case BuiltinType::Float: encoding = 'f'; break;
2348 case BuiltinType::Double: encoding = 'd'; break;
2349 case BuiltinType::LongDouble: encoding = 'd'; break;
2350 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002351
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002352 S += encoding;
2353 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002354 } else if (const ComplexType *CT = T->getAsComplexType()) {
2355 S += 'j';
2356 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2357 false);
2358 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002359 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2360 ExpandPointedToStructures,
2361 ExpandStructures, FD);
2362 if (FD || EncodingProperty) {
2363 // Note that we do extended encoding of protocol qualifer list
2364 // Only when doing ivar or property encoding.
2365 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2366 S += '"';
Steve Naroff446ee4e2009-05-27 16:21:00 +00002367 for (ObjCQualifiedIdType::qual_iterator I = QIDT->qual_begin(),
2368 E = QIDT->qual_end(); I != E; ++I) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002369 S += '<';
Steve Naroff446ee4e2009-05-27 16:21:00 +00002370 S += (*I)->getNameAsString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002371 S += '>';
2372 }
2373 S += '"';
2374 }
2375 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002376 }
2377 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002378 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002379 bool isReadOnly = false;
2380 // For historical/compatibility reasons, the read-only qualifier of the
2381 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2382 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2383 // Also, do not emit the 'r' for anything but the outermost type!
2384 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2385 if (OutermostType && T.isConstQualified()) {
2386 isReadOnly = true;
2387 S += 'r';
2388 }
2389 }
2390 else if (OutermostType) {
2391 QualType P = PointeeTy;
2392 while (P->getAsPointerType())
2393 P = P->getAsPointerType()->getPointeeType();
2394 if (P.isConstQualified()) {
2395 isReadOnly = true;
2396 S += 'r';
2397 }
2398 }
2399 if (isReadOnly) {
2400 // Another legacy compatibility encoding. Some ObjC qualifier and type
2401 // combinations need to be rearranged.
2402 // Rewrite "in const" from "nr" to "rn"
2403 const char * s = S.c_str();
2404 int len = S.length();
2405 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2406 std::string replace = "rn";
2407 S.replace(S.end()-2, S.end(), replace);
2408 }
2409 }
Steve Naroff389bf462009-02-12 17:52:19 +00002410 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002411 S += '@';
2412 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002413 }
2414 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002415 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002416 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002417 // Another historical/compatibility reason.
2418 // We encode the underlying type which comes out as
2419 // {...};
2420 S += '^';
2421 getObjCEncodingForTypeImpl(PointeeTy, S,
2422 false, ExpandPointedToStructures,
2423 NULL);
2424 return;
2425 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002426 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002427 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002428 const ObjCInterfaceType *OIT =
2429 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002430 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002431 S += '"';
2432 S += OI->getNameAsCString();
Steve Naroff446ee4e2009-05-27 16:21:00 +00002433 for (ObjCInterfaceType::qual_iterator I = OIT->qual_begin(),
2434 E = OIT->qual_end(); I != E; ++I) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002435 S += '<';
Steve Naroff446ee4e2009-05-27 16:21:00 +00002436 S += (*I)->getNameAsString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002437 S += '>';
2438 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002439 S += '"';
2440 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002441 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002442 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002443 S += '#';
2444 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002445 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002446 S += ':';
2447 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002448 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002449
2450 if (PointeeTy->isCharType()) {
2451 // char pointer types should be encoded as '*' unless it is a
2452 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002453 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002454 S += '*';
2455 return;
2456 }
2457 }
2458
2459 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002460 getLegacyIntegralTypeEncoding(PointeeTy);
2461
2462 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002463 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002464 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002465 } else if (const ArrayType *AT =
2466 // Ignore type qualifiers etc.
2467 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002468 if (isa<IncompleteArrayType>(AT)) {
2469 // Incomplete arrays are encoded as a pointer to the array element.
2470 S += '^';
2471
2472 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2473 false, ExpandStructures, FD);
2474 } else {
2475 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002476
Anders Carlsson559a8332009-02-22 01:38:57 +00002477 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2478 S += llvm::utostr(CAT->getSize().getZExtValue());
2479 else {
2480 //Variable length arrays are encoded as a regular array with 0 elements.
2481 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2482 S += '0';
2483 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002484
Anders Carlsson559a8332009-02-22 01:38:57 +00002485 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2486 false, ExpandStructures, FD);
2487 S += ']';
2488 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002489 } else if (T->getAsFunctionType()) {
2490 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002491 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002492 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002493 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002494 // Anonymous structures print as '?'
2495 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2496 S += II->getName();
2497 } else {
2498 S += '?';
2499 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002500 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002501 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002502 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2503 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002504 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002505 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002506 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002507 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002508 S += '"';
2509 }
2510
2511 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002512 if (Field->isBitField()) {
2513 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2514 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002515 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002516 QualType qt = Field->getType();
2517 getLegacyIntegralTypeEncoding(qt);
2518 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002519 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002520 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002521 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002522 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002523 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002524 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002525 if (FD && FD->isBitField())
2526 EncodeBitField(this, S, FD);
2527 else
2528 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002529 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002530 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002531 } else if (T->isObjCInterfaceType()) {
2532 // @encode(class_name)
2533 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2534 S += '{';
2535 const IdentifierInfo *II = OI->getIdentifier();
2536 S += II->getName();
2537 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002538 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002539 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002540 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002541 if (RecFields[i]->isBitField())
2542 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2543 RecFields[i]);
2544 else
2545 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2546 FD);
2547 }
2548 S += '}';
2549 }
2550 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002551 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002552}
2553
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002554void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002555 std::string& S) const {
2556 if (QT & Decl::OBJC_TQ_In)
2557 S += 'n';
2558 if (QT & Decl::OBJC_TQ_Inout)
2559 S += 'N';
2560 if (QT & Decl::OBJC_TQ_Out)
2561 S += 'o';
2562 if (QT & Decl::OBJC_TQ_Bycopy)
2563 S += 'O';
2564 if (QT & Decl::OBJC_TQ_Byref)
2565 S += 'R';
2566 if (QT & Decl::OBJC_TQ_Oneway)
2567 S += 'V';
2568}
2569
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002570void ASTContext::setBuiltinVaListType(QualType T)
2571{
2572 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2573
2574 BuiltinVaListType = T;
2575}
2576
Douglas Gregor319ac892009-04-23 22:29:11 +00002577void ASTContext::setObjCIdType(QualType T)
Steve Naroff7e219e42007-10-15 14:41:52 +00002578{
Douglas Gregor319ac892009-04-23 22:29:11 +00002579 ObjCIdType = T;
2580
2581 const TypedefType *TT = T->getAsTypedefType();
2582 if (!TT)
2583 return;
2584
2585 TypedefDecl *TD = TT->getDecl();
Steve Naroff7e219e42007-10-15 14:41:52 +00002586
2587 // typedef struct objc_object *id;
2588 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002589 // User error - caller will issue diagnostics.
2590 if (!ptr)
2591 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002592 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002593 // User error - caller will issue diagnostics.
2594 if (!rec)
2595 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002596 IdStructType = rec;
2597}
2598
Douglas Gregor319ac892009-04-23 22:29:11 +00002599void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002600{
Douglas Gregor319ac892009-04-23 22:29:11 +00002601 ObjCSelType = T;
2602
2603 const TypedefType *TT = T->getAsTypedefType();
2604 if (!TT)
2605 return;
2606 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002607
2608 // typedef struct objc_selector *SEL;
2609 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002610 if (!ptr)
2611 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002612 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002613 if (!rec)
2614 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002615 SelStructType = rec;
2616}
2617
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002618void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002619{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002620 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002621}
2622
Douglas Gregor319ac892009-04-23 22:29:11 +00002623void ASTContext::setObjCClassType(QualType T)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002624{
Douglas Gregor319ac892009-04-23 22:29:11 +00002625 ObjCClassType = T;
2626
2627 const TypedefType *TT = T->getAsTypedefType();
2628 if (!TT)
2629 return;
2630 TypedefDecl *TD = TT->getDecl();
Anders Carlsson8baaca52007-10-31 02:53:19 +00002631
2632 // typedef struct objc_class *Class;
2633 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2634 assert(ptr && "'Class' incorrectly typed");
2635 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2636 assert(rec && "'Class' incorrectly typed");
2637 ClassStructType = rec;
2638}
2639
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002640void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2641 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002642 "'NSConstantString' type already set!");
2643
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002644 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002645}
2646
Douglas Gregor7532dc62009-03-30 22:58:21 +00002647/// \brief Retrieve the template name that represents a qualified
2648/// template name such as \c std::vector.
2649TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2650 bool TemplateKeyword,
2651 TemplateDecl *Template) {
2652 llvm::FoldingSetNodeID ID;
2653 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2654
2655 void *InsertPos = 0;
2656 QualifiedTemplateName *QTN =
2657 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2658 if (!QTN) {
2659 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2660 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2661 }
2662
2663 return TemplateName(QTN);
2664}
2665
2666/// \brief Retrieve the template name that represents a dependent
2667/// template name such as \c MetaFun::template apply.
2668TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2669 const IdentifierInfo *Name) {
2670 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2671
2672 llvm::FoldingSetNodeID ID;
2673 DependentTemplateName::Profile(ID, NNS, Name);
2674
2675 void *InsertPos = 0;
2676 DependentTemplateName *QTN =
2677 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2678
2679 if (QTN)
2680 return TemplateName(QTN);
2681
2682 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2683 if (CanonNNS == NNS) {
2684 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2685 } else {
2686 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2687 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2688 }
2689
2690 DependentTemplateNames.InsertNode(QTN, InsertPos);
2691 return TemplateName(QTN);
2692}
2693
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002694/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002695/// TargetInfo, produce the corresponding type. The unsigned @p Type
2696/// is actually a value of type @c TargetInfo::IntType.
2697QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002698 switch (Type) {
2699 case TargetInfo::NoInt: return QualType();
2700 case TargetInfo::SignedShort: return ShortTy;
2701 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2702 case TargetInfo::SignedInt: return IntTy;
2703 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2704 case TargetInfo::SignedLong: return LongTy;
2705 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2706 case TargetInfo::SignedLongLong: return LongLongTy;
2707 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2708 }
2709
2710 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002711 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002712}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002713
2714//===----------------------------------------------------------------------===//
2715// Type Predicates.
2716//===----------------------------------------------------------------------===//
2717
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002718/// isObjCNSObjectType - Return true if this is an NSObject object using
2719/// NSObject attribute on a c-style pointer type.
2720/// FIXME - Make it work directly on types.
2721///
2722bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2723 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2724 if (TypedefDecl *TD = TDT->getDecl())
2725 if (TD->getAttr<ObjCNSObjectAttr>())
2726 return true;
2727 }
2728 return false;
2729}
2730
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002731/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2732/// to an object type. This includes "id" and "Class" (two 'special' pointers
2733/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2734/// ID type).
2735bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002736 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002737 return true;
2738
Steve Naroff6ae98502008-10-21 18:24:04 +00002739 // Blocks are objects.
2740 if (Ty->isBlockPointerType())
2741 return true;
2742
2743 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002744 const PointerType *PT = Ty->getAsPointerType();
2745 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002746 return false;
2747
Chris Lattner16ede0e2009-04-12 23:51:02 +00002748 // If this a pointer to an interface (e.g. NSString*), it is ok.
2749 if (PT->getPointeeType()->isObjCInterfaceType() ||
2750 // If is has NSObject attribute, OK as well.
2751 isObjCNSObjectType(Ty))
2752 return true;
2753
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002754 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2755 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002756 // underlying type. Iteratively strip off typedefs so that we can handle
2757 // typedefs of typedefs.
2758 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2759 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2760 Ty.getUnqualifiedType() == getObjCClassType())
2761 return true;
2762
2763 Ty = TDT->getDecl()->getUnderlyingType();
2764 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002765
Chris Lattner16ede0e2009-04-12 23:51:02 +00002766 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002767}
2768
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002769/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2770/// garbage collection attribute.
2771///
2772QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002773 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002774 if (getLangOptions().ObjC1 &&
2775 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002776 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002777 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002778 // (or pointers to them) be treated as though they were declared
2779 // as __strong.
2780 if (GCAttrs == QualType::GCNone) {
2781 if (isObjCObjectPointerType(Ty))
2782 GCAttrs = QualType::Strong;
2783 else if (Ty->isPointerType())
2784 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2785 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002786 // Non-pointers have none gc'able attribute regardless of the attribute
2787 // set on them.
2788 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2789 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002790 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002791 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002792}
2793
Chris Lattner6ac46a42008-04-07 06:51:04 +00002794//===----------------------------------------------------------------------===//
2795// Type Compatibility Testing
2796//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002797
Steve Naroff1c7d0672008-09-04 15:10:53 +00002798/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002799/// block types. Types must be strictly compatible here. For example,
2800/// C unfortunately doesn't produce an error for the following:
2801///
2802/// int (*emptyArgFunc)();
2803/// int (*intArgList)(int) = emptyArgFunc;
2804///
2805/// For blocks, we will produce an error for the following (similar to C++):
2806///
2807/// int (^emptyArgBlock)();
2808/// int (^intArgBlock)(int) = emptyArgBlock;
2809///
2810/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2811///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002812bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002813 const FunctionType *lbase = lhs->getAsFunctionType();
2814 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002815 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2816 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002817 if (lproto && rproto == 0)
2818 return false;
2819 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002820}
2821
Chris Lattner6ac46a42008-04-07 06:51:04 +00002822/// areCompatVectorTypes - Return true if the two specified vector types are
2823/// compatible.
2824static bool areCompatVectorTypes(const VectorType *LHS,
2825 const VectorType *RHS) {
2826 assert(LHS->isCanonical() && RHS->isCanonical());
2827 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002828 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002829}
2830
Eli Friedman3d815e72008-08-22 00:56:42 +00002831/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002832/// compatible for assignment from RHS to LHS. This handles validation of any
2833/// protocol qualifiers on the LHS or RHS.
2834///
Eli Friedman3d815e72008-08-22 00:56:42 +00002835bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2836 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002837 // Verify that the base decls are compatible: the RHS must be a subclass of
2838 // the LHS.
2839 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2840 return false;
2841
2842 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2843 // protocol qualified at all, then we are good.
2844 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2845 return true;
2846
2847 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2848 // isn't a superset.
2849 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2850 return true; // FIXME: should return false!
2851
2852 // Finally, we must have two protocol-qualified interfaces.
2853 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2854 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002855
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002856 // All LHS protocols must have a presence on the RHS.
2857 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002858
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002859 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2860 LHSPE = LHSP->qual_end();
2861 LHSPI != LHSPE; LHSPI++) {
2862 bool RHSImplementsProtocol = false;
2863
2864 // If the RHS doesn't implement the protocol on the left, the types
2865 // are incompatible.
2866 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2867 RHSPE = RHSP->qual_end();
2868 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2869 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2870 RHSImplementsProtocol = true;
2871 }
2872 // FIXME: For better diagnostics, consider passing back the protocol name.
2873 if (!RHSImplementsProtocol)
2874 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002875 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002876 // The RHS implements all protocols listed on the LHS.
2877 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002878}
2879
Steve Naroff389bf462009-02-12 17:52:19 +00002880bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2881 // get the "pointed to" types
2882 const PointerType *LHSPT = LHS->getAsPointerType();
2883 const PointerType *RHSPT = RHS->getAsPointerType();
2884
2885 if (!LHSPT || !RHSPT)
2886 return false;
2887
2888 QualType lhptee = LHSPT->getPointeeType();
2889 QualType rhptee = RHSPT->getPointeeType();
2890 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2891 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2892 // ID acts sort of like void* for ObjC interfaces
2893 if (LHSIface && isObjCIdStructType(rhptee))
2894 return true;
2895 if (RHSIface && isObjCIdStructType(lhptee))
2896 return true;
2897 if (!LHSIface || !RHSIface)
2898 return false;
2899 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2900 canAssignObjCInterfaces(RHSIface, LHSIface);
2901}
2902
Steve Naroffec0550f2007-10-15 20:41:53 +00002903/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2904/// both shall have the identically qualified version of a compatible type.
2905/// C99 6.2.7p1: Two types have compatible types if their types are the
2906/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002907bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2908 return !mergeTypes(LHS, RHS).isNull();
2909}
2910
2911QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2912 const FunctionType *lbase = lhs->getAsFunctionType();
2913 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002914 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2915 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002916 bool allLTypes = true;
2917 bool allRTypes = true;
2918
2919 // Check return type
2920 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2921 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002922 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2923 allLTypes = false;
2924 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2925 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002926
2927 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl465226e2009-05-27 22:11:52 +00002928 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
2929 "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00002930 unsigned lproto_nargs = lproto->getNumArgs();
2931 unsigned rproto_nargs = rproto->getNumArgs();
2932
2933 // Compatible functions must have the same number of arguments
2934 if (lproto_nargs != rproto_nargs)
2935 return QualType();
2936
2937 // Variadic and non-variadic functions aren't compatible
2938 if (lproto->isVariadic() != rproto->isVariadic())
2939 return QualType();
2940
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002941 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2942 return QualType();
2943
Eli Friedman3d815e72008-08-22 00:56:42 +00002944 // Check argument compatibility
2945 llvm::SmallVector<QualType, 10> types;
2946 for (unsigned i = 0; i < lproto_nargs; i++) {
2947 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2948 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2949 QualType argtype = mergeTypes(largtype, rargtype);
2950 if (argtype.isNull()) return QualType();
2951 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002952 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2953 allLTypes = false;
2954 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2955 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002956 }
2957 if (allLTypes) return lhs;
2958 if (allRTypes) return rhs;
2959 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002960 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002961 }
2962
2963 if (lproto) allRTypes = false;
2964 if (rproto) allLTypes = false;
2965
Douglas Gregor72564e72009-02-26 23:50:07 +00002966 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002967 if (proto) {
Sebastian Redl465226e2009-05-27 22:11:52 +00002968 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman3d815e72008-08-22 00:56:42 +00002969 if (proto->isVariadic()) return QualType();
2970 // Check that the types are compatible with the types that
2971 // would result from default argument promotions (C99 6.7.5.3p15).
2972 // The only types actually affected are promotable integer
2973 // types and floats, which would be passed as a different
2974 // type depending on whether the prototype is visible.
2975 unsigned proto_nargs = proto->getNumArgs();
2976 for (unsigned i = 0; i < proto_nargs; ++i) {
2977 QualType argTy = proto->getArgType(i);
2978 if (argTy->isPromotableIntegerType() ||
2979 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2980 return QualType();
2981 }
2982
2983 if (allLTypes) return lhs;
2984 if (allRTypes) return rhs;
2985 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002986 proto->getNumArgs(), lproto->isVariadic(),
2987 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002988 }
2989
2990 if (allLTypes) return lhs;
2991 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002992 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002993}
2994
2995QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002996 // C++ [expr]: If an expression initially has the type "reference to T", the
2997 // type is adjusted to "T" prior to any further analysis, the expression
2998 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002999 // expression is an lvalue unless the reference is an rvalue reference and
3000 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00003001 // FIXME: C++ shouldn't be going through here! The rules are different
3002 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003003 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3004 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00003005 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003006 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00003007 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00003008 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00003009
Eli Friedman3d815e72008-08-22 00:56:42 +00003010 QualType LHSCan = getCanonicalType(LHS),
3011 RHSCan = getCanonicalType(RHS);
3012
3013 // If two types are identical, they are compatible.
3014 if (LHSCan == RHSCan)
3015 return LHS;
3016
3017 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003018 // Note that we handle extended qualifiers later, in the
3019 // case for ExtQualType.
3020 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00003021 return QualType();
3022
Eli Friedman852d63b2009-06-01 01:22:52 +00003023 Type::TypeClass LHSClass = LHSCan->getTypeClass();
3024 Type::TypeClass RHSClass = RHSCan->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00003025
Chris Lattner1adb8832008-01-14 05:45:46 +00003026 // We want to consider the two function types to be the same for these
3027 // comparisons, just force one to the other.
3028 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3029 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00003030
3031 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00003032 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3033 LHSClass = Type::ConstantArray;
3034 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3035 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00003036
Nate Begeman213541a2008-04-18 23:10:10 +00003037 // Canonicalize ExtVector -> Vector.
3038 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3039 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00003040
Chris Lattnerb0489812008-04-07 06:38:24 +00003041 // Consider qualified interfaces and interfaces the same.
3042 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3043 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00003044
Chris Lattnera36a61f2008-04-07 05:43:21 +00003045 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003046 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003047 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3048 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00003049
Steve Naroffd824c9c2009-04-14 15:11:46 +00003050 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3051 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003052 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00003053 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00003054 return RHS;
3055
Steve Naroffbc76dd02008-12-10 22:14:21 +00003056 // ID is compatible with all qualified id types.
3057 if (LHS->isObjCQualifiedIdType()) {
3058 if (const PointerType *PT = RHS->getAsPointerType()) {
3059 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003060 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003061 return LHS;
3062 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3063 // Unfortunately, this API is part of Sema (which we don't have access
3064 // to. Need to refactor. The following check is insufficient, since we
3065 // need to make sure the class implements the protocol.
3066 if (pType->isObjCInterfaceType())
3067 return LHS;
3068 }
3069 }
3070 if (RHS->isObjCQualifiedIdType()) {
3071 if (const PointerType *PT = LHS->getAsPointerType()) {
3072 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00003073 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00003074 return RHS;
3075 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3076 // Unfortunately, this API is part of Sema (which we don't have access
3077 // to. Need to refactor. The following check is insufficient, since we
3078 // need to make sure the class implements the protocol.
3079 if (pType->isObjCInterfaceType())
3080 return RHS;
3081 }
3082 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003083 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3084 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003085 if (const EnumType* ETy = LHS->getAsEnumType()) {
3086 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3087 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003088 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003089 if (const EnumType* ETy = RHS->getAsEnumType()) {
3090 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3091 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003092 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003093
Eli Friedman3d815e72008-08-22 00:56:42 +00003094 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003095 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003096
Steve Naroff4a746782008-01-09 22:43:08 +00003097 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003098 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003099#define TYPE(Class, Base)
3100#define ABSTRACT_TYPE(Class, Base)
3101#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3102#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3103#include "clang/AST/TypeNodes.def"
3104 assert(false && "Non-canonical and dependent types shouldn't get here");
3105 return QualType();
3106
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003107 case Type::LValueReference:
3108 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003109 case Type::MemberPointer:
3110 assert(false && "C++ should never be in mergeTypes");
3111 return QualType();
3112
3113 case Type::IncompleteArray:
3114 case Type::VariableArray:
3115 case Type::FunctionProto:
3116 case Type::ExtVector:
3117 case Type::ObjCQualifiedInterface:
3118 assert(false && "Types are eliminated above");
3119 return QualType();
3120
Chris Lattner1adb8832008-01-14 05:45:46 +00003121 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003122 {
3123 // Merge two pointer types, while trying to preserve typedef info
3124 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3125 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3126 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3127 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003128 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3129 return LHS;
3130 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3131 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003132 return getPointerType(ResultType);
3133 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003134 case Type::BlockPointer:
3135 {
3136 // Merge two block pointer types, while trying to preserve typedef info
3137 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3138 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3139 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3140 if (ResultType.isNull()) return QualType();
3141 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3142 return LHS;
3143 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3144 return RHS;
3145 return getBlockPointerType(ResultType);
3146 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003147 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003148 {
3149 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3150 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3151 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3152 return QualType();
3153
3154 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3155 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3156 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3157 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003158 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3159 return LHS;
3160 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3161 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003162 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3163 ArrayType::ArraySizeModifier(), 0);
3164 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3165 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003166 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3167 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003168 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3169 return LHS;
3170 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3171 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003172 if (LVAT) {
3173 // FIXME: This isn't correct! But tricky to implement because
3174 // the array's size has to be the size of LHS, but the type
3175 // has to be different.
3176 return LHS;
3177 }
3178 if (RVAT) {
3179 // FIXME: This isn't correct! But tricky to implement because
3180 // the array's size has to be the size of RHS, but the type
3181 // has to be different.
3182 return RHS;
3183 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003184 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3185 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003186 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003187 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003188 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003189 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003190 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003191 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003192 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003193 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3194 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003195 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003196 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003197 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003198 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003199 case Type::Complex:
3200 // Distinct complex types are incompatible.
3201 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003202 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003203 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003204 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3205 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003206 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003207 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003208 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003209 // FIXME: This should be type compatibility, e.g. whether
3210 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003211 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3212 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3213 if (LHSIface && RHSIface &&
3214 canAssignObjCInterfaces(LHSIface, RHSIface))
3215 return LHS;
3216
Eli Friedman3d815e72008-08-22 00:56:42 +00003217 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003218 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003219 case Type::ObjCQualifiedId:
3220 // Distinct qualified id's are not compatible.
3221 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003222 case Type::FixedWidthInt:
3223 // Distinct fixed-width integers are not compatible.
3224 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003225 case Type::ExtQual:
3226 // FIXME: ExtQual types can be compatible even if they're not
3227 // identical!
3228 return QualType();
3229 // First attempt at an implementation, but I'm not really sure it's
3230 // right...
3231#if 0
3232 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3233 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3234 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3235 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3236 return QualType();
3237 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3238 LHSBase = QualType(LQual->getBaseType(), 0);
3239 RHSBase = QualType(RQual->getBaseType(), 0);
3240 ResultType = mergeTypes(LHSBase, RHSBase);
3241 if (ResultType.isNull()) return QualType();
3242 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3243 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3244 return LHS;
3245 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3246 return RHS;
3247 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3248 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3249 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3250 return ResultType;
3251#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003252
3253 case Type::TemplateSpecialization:
3254 assert(false && "Dependent types have no size");
3255 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003256 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003257
3258 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003259}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003260
Chris Lattner5426bf62008-04-07 07:01:58 +00003261//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003262// Integer Predicates
3263//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003264
Eli Friedmanad74a752008-06-28 06:23:08 +00003265unsigned ASTContext::getIntWidth(QualType T) {
3266 if (T == BoolTy)
3267 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003268 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3269 return FWIT->getWidth();
3270 }
3271 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003272 return (unsigned)getTypeSize(T);
3273}
3274
3275QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3276 assert(T->isSignedIntegerType() && "Unexpected type");
3277 if (const EnumType* ETy = T->getAsEnumType())
3278 T = ETy->getDecl()->getIntegerType();
3279 const BuiltinType* BTy = T->getAsBuiltinType();
3280 assert (BTy && "Unexpected signed integer type");
3281 switch (BTy->getKind()) {
3282 case BuiltinType::Char_S:
3283 case BuiltinType::SChar:
3284 return UnsignedCharTy;
3285 case BuiltinType::Short:
3286 return UnsignedShortTy;
3287 case BuiltinType::Int:
3288 return UnsignedIntTy;
3289 case BuiltinType::Long:
3290 return UnsignedLongTy;
3291 case BuiltinType::LongLong:
3292 return UnsignedLongLongTy;
Chris Lattner2df9ced2009-04-30 02:43:43 +00003293 case BuiltinType::Int128:
3294 return UnsignedInt128Ty;
Eli Friedmanad74a752008-06-28 06:23:08 +00003295 default:
3296 assert(0 && "Unexpected signed integer type");
3297 return QualType();
3298 }
3299}
3300
Douglas Gregor2cf26342009-04-09 22:27:44 +00003301ExternalASTSource::~ExternalASTSource() { }
3302
3303void ExternalASTSource::PrintStats() { }