blob: a9f13c9518039006eb81b6232188fe3d6ae6e3fb [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000019#include "clang/AST/ExternalASTSource.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattnerb09b31d2009-03-28 03:45:20 +000021#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000023#include "llvm/ADT/StringExtras.h"
Nate Begeman7903d052009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Chris Lattnerf4fbc442009-03-28 04:27:18 +000025#include "llvm/Support/MemoryBuffer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner2fda0ed2008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Douglas Gregorda38c6c2009-04-22 18:49:13 +000035 bool FreeMem, unsigned size_reserve,
36 bool InitializeBuiltins) :
Douglas Gregor1e589cc2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregorc34897d2009-04-09 22:27:44 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
40 ExternalSource(0) {
Daniel Dunbarde300732008-08-11 04:54:23 +000041 if (size_reserve > 0) Types.reserve(size_reserve);
42 InitBuiltinTypes();
Daniel Dunbarde300732008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
Douglas Gregord37ce612009-04-26 03:57:37 +000044 BuiltinInfo.InitializeTargetBuiltins(Target);
Douglas Gregorda38c6c2009-04-22 18:49:13 +000045 if (InitializeBuiltins)
46 this->InitializeBuiltins(idents);
Daniel Dunbarde300732008-08-11 04:54:23 +000047}
48
Chris Lattner4b009652007-07-25 00:24:17 +000049ASTContext::~ASTContext() {
50 // Deallocate all the types.
51 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000052 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000053 Types.pop_back();
54 }
Eli Friedman65489b72008-05-27 03:08:09 +000055
Nuno Lopes355a8682008-12-17 22:30:25 +000056 {
57 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
58 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
59 while (I != E) {
60 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
61 delete R;
62 }
63 }
64
65 {
Daniel Dunbar1fbaef12009-05-03 10:38:35 +000066 llvm::DenseMap<const ObjCContainerDecl*, const ASTRecordLayout*>::iterator
67 I = ObjCLayouts.begin(), E = ObjCLayouts.end();
Nuno Lopes355a8682008-12-17 22:30:25 +000068 while (I != E) {
69 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
70 delete R;
71 }
72 }
73
Douglas Gregor1e589cc2009-03-26 23:50:42 +000074 // Destroy nested-name-specifiers.
Douglas Gregor3c4eae52009-03-27 23:54:10 +000075 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
76 NNS = NestedNameSpecifiers.begin(),
77 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregorbccd97c2009-03-27 23:25:45 +000078 NNS != NNSEnd;
Douglas Gregor3c4eae52009-03-27 23:54:10 +000079 /* Increment in loop */)
80 (*NNS++).Destroy(*this);
Douglas Gregor1e589cc2009-03-26 23:50:42 +000081
82 if (GlobalNestedNameSpecifier)
83 GlobalNestedNameSpecifier->Destroy(*this);
84
Eli Friedman65489b72008-05-27 03:08:09 +000085 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000086}
87
Douglas Gregorda38c6c2009-04-22 18:49:13 +000088void ASTContext::InitializeBuiltins(IdentifierTable &idents) {
Douglas Gregorda38c6c2009-04-22 18:49:13 +000089 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
90}
91
Douglas Gregorc34897d2009-04-09 22:27:44 +000092void
93ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
94 ExternalSource.reset(Source.take());
95}
96
Chris Lattner4b009652007-07-25 00:24:17 +000097void ASTContext::PrintStats() const {
98 fprintf(stderr, "*** AST Context Stats:\n");
99 fprintf(stderr, " %d types total.\n", (int)Types.size());
100 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +0000101 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000102 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
103 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
104
Chris Lattner4b009652007-07-25 00:24:17 +0000105 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000106 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
107 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000108 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000109 unsigned NumExtQual = 0;
110
Chris Lattner4b009652007-07-25 00:24:17 +0000111 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
112 Type *T = Types[i];
113 if (isa<BuiltinType>(T))
114 ++NumBuiltin;
115 else if (isa<PointerType>(T))
116 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +0000117 else if (isa<BlockPointerType>(T))
118 ++NumBlockPointer;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000119 else if (isa<LValueReferenceType>(T))
120 ++NumLValueReference;
121 else if (isa<RValueReferenceType>(T))
122 ++NumRValueReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000123 else if (isa<MemberPointerType>(T))
124 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000125 else if (isa<ComplexType>(T))
126 ++NumComplex;
127 else if (isa<ArrayType>(T))
128 ++NumArray;
129 else if (isa<VectorType>(T))
130 ++NumVector;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000131 else if (isa<FunctionNoProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000132 ++NumFunctionNP;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000133 else if (isa<FunctionProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000134 ++NumFunctionP;
135 else if (isa<TypedefType>(T))
136 ++NumTypeName;
137 else if (TagType *TT = dyn_cast<TagType>(T)) {
138 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000139 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000140 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000141 case TagDecl::TK_struct: ++NumTagStruct; break;
142 case TagDecl::TK_union: ++NumTagUnion; break;
143 case TagDecl::TK_class: ++NumTagClass; break;
144 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000145 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000146 } else if (isa<ObjCInterfaceType>(T))
147 ++NumObjCInterfaces;
148 else if (isa<ObjCQualifiedInterfaceType>(T))
149 ++NumObjCQualifiedInterfaces;
150 else if (isa<ObjCQualifiedIdType>(T))
151 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000152 else if (isa<TypeOfType>(T))
153 ++NumTypeOfTypes;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000154 else if (isa<TypeOfExprType>(T))
155 ++NumTypeOfExprTypes;
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000156 else if (isa<ExtQualType>(T))
157 ++NumExtQual;
Steve Naroff948fd372007-09-17 14:16:13 +0000158 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000159 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000160 assert(0 && "Unknown type!");
161 }
162 }
163
164 fprintf(stderr, " %d builtin types\n", NumBuiltin);
165 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000166 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000167 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
168 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000169 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000170 fprintf(stderr, " %d complex types\n", NumComplex);
171 fprintf(stderr, " %d array types\n", NumArray);
172 fprintf(stderr, " %d vector types\n", NumVector);
173 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
174 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
175 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
176 fprintf(stderr, " %d tagged types\n", NumTagged);
177 fprintf(stderr, " %d struct types\n", NumTagStruct);
178 fprintf(stderr, " %d union types\n", NumTagUnion);
179 fprintf(stderr, " %d class types\n", NumTagClass);
180 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000181 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000182 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000183 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000184 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000185 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000186 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000187 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000188 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000189
Chris Lattner4b009652007-07-25 00:24:17 +0000190 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
191 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
192 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlce6fff02009-03-16 23:22:08 +0000193 NumLValueReference*sizeof(LValueReferenceType)+
194 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000195 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor4fa58902009-02-26 23:50:07 +0000196 NumFunctionP*sizeof(FunctionProtoType)+
197 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroffe0430632008-05-21 15:59:22 +0000198 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000199 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
200 NumExtQual*sizeof(ExtQualType)));
Douglas Gregorc34897d2009-04-09 22:27:44 +0000201
202 if (ExternalSource.get()) {
203 fprintf(stderr, "\n");
204 ExternalSource->PrintStats();
205 }
Chris Lattner4b009652007-07-25 00:24:17 +0000206}
207
208
209void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000210 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000211}
212
Chris Lattner4b009652007-07-25 00:24:17 +0000213void ASTContext::InitBuiltinTypes() {
214 assert(VoidTy.isNull() && "Context reinitialized?");
215
216 // C99 6.2.5p19.
217 InitBuiltinType(VoidTy, BuiltinType::Void);
218
219 // C99 6.2.5p2.
220 InitBuiltinType(BoolTy, BuiltinType::Bool);
221 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000222 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000223 InitBuiltinType(CharTy, BuiltinType::Char_S);
224 else
225 InitBuiltinType(CharTy, BuiltinType::Char_U);
226 // C99 6.2.5p4.
227 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
228 InitBuiltinType(ShortTy, BuiltinType::Short);
229 InitBuiltinType(IntTy, BuiltinType::Int);
230 InitBuiltinType(LongTy, BuiltinType::Long);
231 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
232
233 // C99 6.2.5p6.
234 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
235 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
236 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
237 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
238 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
239
240 // C99 6.2.5p10.
241 InitBuiltinType(FloatTy, BuiltinType::Float);
242 InitBuiltinType(DoubleTy, BuiltinType::Double);
243 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000244
Chris Lattner6cc7e412009-04-30 02:43:43 +0000245 // GNU extension, 128-bit integers.
246 InitBuiltinType(Int128Ty, BuiltinType::Int128);
247 InitBuiltinType(UnsignedInt128Ty, BuiltinType::UInt128);
248
Chris Lattnere1dafe72009-02-26 23:43:47 +0000249 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
250 InitBuiltinType(WCharTy, BuiltinType::WChar);
251 else // C99
252 WCharTy = getFromTargetType(Target.getWCharType());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000253
Douglas Gregord2baafd2008-10-21 16:13:35 +0000254 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000255 InitBuiltinType(OverloadTy, BuiltinType::Overload);
256
257 // Placeholder type for type-dependent expressions whose type is
258 // completely unknown. No code should ever check a type against
259 // DependentTy and users should never see it; however, it is here to
260 // help diagnose failures to properly check for type-dependent
261 // expressions.
262 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000263
Chris Lattner4b009652007-07-25 00:24:17 +0000264 // C99 6.2.5p11.
265 FloatComplexTy = getComplexType(FloatTy);
266 DoubleComplexTy = getComplexType(DoubleTy);
267 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000268
Steve Naroff9d12c902007-10-15 14:41:52 +0000269 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000270 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000271 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000272 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000273 ClassStructType = 0;
274
Ted Kremenek42730c52008-01-07 19:49:32 +0000275 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000276
277 // void * type
278 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000279}
280
281//===----------------------------------------------------------------------===//
282// Type Sizing and Analysis
283//===----------------------------------------------------------------------===//
284
Chris Lattner2a674dc2008-06-30 18:32:54 +0000285/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
286/// scalar floating point type.
287const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
288 const BuiltinType *BT = T->getAsBuiltinType();
289 assert(BT && "Not a floating point type!");
290 switch (BT->getKind()) {
291 default: assert(0 && "Not a floating point type!");
292 case BuiltinType::Float: return Target.getFloatFormat();
293 case BuiltinType::Double: return Target.getDoubleFormat();
294 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
295 }
296}
297
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000298/// getDeclAlign - Return a conservative estimate of the alignment of the
299/// specified decl. Note that bitfields do not have a valid alignment, so
300/// this method will assert on them.
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +0000301unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman0ee57322009-02-22 02:56:25 +0000302 unsigned Align = Target.getCharWidth();
303
304 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
305 Align = std::max(Align, AA->getAlignment());
306
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000307 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
308 QualType T = VD->getType();
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000309 if (const ReferenceType* RT = T->getAsReferenceType()) {
310 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssoneeaeda32009-04-10 04:52:36 +0000311 Align = Target.getPointerAlign(AS);
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000312 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
313 // Incomplete or function types default to 1.
Eli Friedman0ee57322009-02-22 02:56:25 +0000314 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
315 T = cast<ArrayType>(T)->getElementType();
316
317 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
318 }
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000319 }
Eli Friedman0ee57322009-02-22 02:56:25 +0000320
321 return Align / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000322}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000323
Chris Lattner4b009652007-07-25 00:24:17 +0000324/// getTypeSize - Return the size of the specified type, in bits. This method
325/// does not work on incomplete types.
326std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000327ASTContext::getTypeInfo(const Type *T) {
Mike Stump44d1f402009-02-27 18:32:39 +0000328 uint64_t Width=0;
329 unsigned Align=8;
Chris Lattner4b009652007-07-25 00:24:17 +0000330 switch (T->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000331#define TYPE(Class, Base)
332#define ABSTRACT_TYPE(Class, Base)
Douglas Gregorab380272009-04-30 17:32:17 +0000333#define NON_CANONICAL_TYPE(Class, Base)
Douglas Gregor4fa58902009-02-26 23:50:07 +0000334#define DEPENDENT_TYPE(Class, Base) case Type::Class:
335#include "clang/AST/TypeNodes.def"
Douglas Gregorab380272009-04-30 17:32:17 +0000336 assert(false && "Should not see dependent types");
Douglas Gregor4fa58902009-02-26 23:50:07 +0000337 break;
338
Chris Lattner4b009652007-07-25 00:24:17 +0000339 case Type::FunctionNoProto:
340 case Type::FunctionProto:
Douglas Gregorab380272009-04-30 17:32:17 +0000341 // GCC extension: alignof(function) = 32 bits
342 Width = 0;
343 Align = 32;
344 break;
345
Douglas Gregor4fa58902009-02-26 23:50:07 +0000346 case Type::IncompleteArray:
Steve Naroff83c13012007-08-30 01:06:46 +0000347 case Type::VariableArray:
Douglas Gregorab380272009-04-30 17:32:17 +0000348 Width = 0;
349 Align = getTypeAlign(cast<ArrayType>(T)->getElementType());
350 break;
351
Steve Naroff83c13012007-08-30 01:06:46 +0000352 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000353 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000354
Chris Lattner8cd0e932008-03-05 18:54:05 +0000355 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000356 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000357 Align = EltInfo.second;
358 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000359 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000360 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000361 case Type::Vector: {
362 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000363 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000364 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000365 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000366 // If the alignment is not a power of 2, round up to the next power of 2.
367 // This happens for non-power-of-2 length vectors.
368 // FIXME: this should probably be a target property.
369 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000370 break;
371 }
372
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000373 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000374 switch (cast<BuiltinType>(T)->getKind()) {
375 default: assert(0 && "Unknown builtin type!");
376 case BuiltinType::Void:
Douglas Gregorab380272009-04-30 17:32:17 +0000377 // GCC extension: alignof(void) = 8 bits.
378 Width = 0;
379 Align = 8;
380 break;
381
Chris Lattnerb66237b2007-12-19 19:23:28 +0000382 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000383 Width = Target.getBoolWidth();
384 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000385 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000386 case BuiltinType::Char_S:
387 case BuiltinType::Char_U:
388 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000389 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000390 Width = Target.getCharWidth();
391 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000392 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000393 case BuiltinType::WChar:
394 Width = Target.getWCharWidth();
395 Align = Target.getWCharAlign();
396 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000397 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000398 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000399 Width = Target.getShortWidth();
400 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000401 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000402 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000403 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000404 Width = Target.getIntWidth();
405 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000406 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000407 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000408 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000409 Width = Target.getLongWidth();
410 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000411 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000412 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000413 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000414 Width = Target.getLongLongWidth();
415 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000416 break;
Chris Lattner4b11cc22009-04-30 02:55:13 +0000417 case BuiltinType::Int128:
418 case BuiltinType::UInt128:
419 Width = 128;
420 Align = 128; // int128_t is 128-bit aligned on all targets.
421 break;
Chris Lattnerb66237b2007-12-19 19:23:28 +0000422 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000423 Width = Target.getFloatWidth();
424 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000425 break;
426 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000427 Width = Target.getDoubleWidth();
428 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000429 break;
430 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000431 Width = Target.getLongDoubleWidth();
432 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000433 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000434 }
435 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000436 case Type::FixedWidthInt:
437 // FIXME: This isn't precisely correct; the width/alignment should depend
438 // on the available types for the target
439 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattnere9174982009-02-15 21:20:13 +0000440 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000441 Align = Width;
442 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000443 case Type::ExtQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000444 // FIXME: Pointers into different addr spaces could have different sizes and
445 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000446 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000447 case Type::ObjCQualifiedId:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000448 case Type::ObjCQualifiedInterface:
Chris Lattner1d78a862008-04-07 07:01:58 +0000449 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000450 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000451 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000452 case Type::BlockPointer: {
453 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
454 Width = Target.getPointerWidth(AS);
455 Align = Target.getPointerAlign(AS);
456 break;
457 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000458 case Type::Pointer: {
459 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000460 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000461 Align = Target.getPointerAlign(AS);
462 break;
463 }
Sebastian Redlce6fff02009-03-16 23:22:08 +0000464 case Type::LValueReference:
465 case Type::RValueReference:
Chris Lattner4b009652007-07-25 00:24:17 +0000466 // "When applied to a reference or a reference type, the result is the size
467 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000468 // FIXME: This is wrong for struct layout: a reference in a struct has
469 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000470 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000471 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000472 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-01-24 21:16:55 +0000473 // the GCC ABI, where pointers to data are one pointer large, pointers to
474 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl18cffee2009-01-24 23:29:36 +0000475 // other compilers too, we need to delegate this completely to TargetInfo
476 // or some ABI abstraction layer.
Sebastian Redl75555032009-01-24 21:16:55 +0000477 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
478 unsigned AS = Pointee.getAddressSpace();
479 Width = Target.getPointerWidth(AS);
480 if (Pointee->isFunctionType())
481 Width *= 2;
482 Align = Target.getPointerAlign(AS);
483 // GCC aligns at single pointer width.
484 }
Chris Lattner4b009652007-07-25 00:24:17 +0000485 case Type::Complex: {
486 // Complex types have the same alignment as their elements, but twice the
487 // size.
488 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000489 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000490 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000491 Align = EltInfo.second;
492 break;
493 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000494 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000495 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000496 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
497 Width = Layout.getSize();
498 Align = Layout.getAlignment();
499 break;
500 }
Douglas Gregor4fa58902009-02-26 23:50:07 +0000501 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000502 case Type::Enum: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000503 const TagType *TT = cast<TagType>(T);
504
505 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000506 Width = 1;
507 Align = 1;
508 break;
509 }
510
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000511 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000512 return getTypeInfo(ET->getDecl()->getIntegerType());
513
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000514 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000515 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
516 Width = Layout.getSize();
517 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000518 break;
519 }
Douglas Gregordd13e842009-03-30 22:58:21 +0000520
Douglas Gregorab380272009-04-30 17:32:17 +0000521 case Type::Typedef: {
522 const TypedefDecl *Typedef = cast<TypedefType>(T)->getDecl();
523 if (const AlignedAttr *Aligned = Typedef->getAttr<AlignedAttr>()) {
524 Align = Aligned->getAlignment();
525 Width = getTypeSize(Typedef->getUnderlyingType().getTypePtr());
526 } else
527 return getTypeInfo(Typedef->getUnderlyingType().getTypePtr());
Douglas Gregordd13e842009-03-30 22:58:21 +0000528 break;
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000529 }
Douglas Gregorab380272009-04-30 17:32:17 +0000530
531 case Type::TypeOfExpr:
532 return getTypeInfo(cast<TypeOfExprType>(T)->getUnderlyingExpr()->getType()
533 .getTypePtr());
534
535 case Type::TypeOf:
536 return getTypeInfo(cast<TypeOfType>(T)->getUnderlyingType().getTypePtr());
537
538 case Type::QualifiedName:
539 return getTypeInfo(cast<QualifiedNameType>(T)->getNamedType().getTypePtr());
540
541 case Type::TemplateSpecialization:
542 assert(getCanonicalType(T) != T &&
543 "Cannot request the size of a dependent type");
544 // FIXME: this is likely to be wrong once we support template
545 // aliases, since a template alias could refer to a typedef that
546 // has an __aligned__ attribute on it.
547 return getTypeInfo(getCanonicalType(T));
548 }
Chris Lattner4b009652007-07-25 00:24:17 +0000549
550 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000551 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000552}
553
Chris Lattner83165b52009-01-27 18:08:34 +0000554/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
555/// type for the current target in bits. This can be different than the ABI
556/// alignment in cases where it is beneficial for performance to overalign
557/// a data type.
558unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
559 unsigned ABIAlign = getTypeAlign(T);
560
561 // Doubles should be naturally aligned if possible.
Daniel Dunbarc61a8002009-02-18 19:59:32 +0000562 if (T->isSpecificBuiltinType(BuiltinType::Double))
563 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000564
565 return ABIAlign;
566}
567
568
Devang Patelbfe323c2008-06-04 21:22:16 +0000569/// LayoutField - Field layout.
570void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000571 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000572 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000573 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000574 uint64_t FieldOffset = IsUnion ? 0 : Size;
575 uint64_t FieldSize;
576 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000577
578 // FIXME: Should this override struct packing? Probably we want to
579 // take the minimum?
580 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
581 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000582
583 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
584 // TODO: Need to check this algorithm on other targets!
585 // (tested on Linux-X86)
Eli Friedman5255e7a2009-04-26 19:19:15 +0000586 FieldSize = BitWidthExpr->EvaluateAsInt(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000587
588 std::pair<uint64_t, unsigned> FieldInfo =
589 Context.getTypeInfo(FD->getType());
590 uint64_t TypeSize = FieldInfo.first;
591
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000592 // Determine the alignment of this bitfield. The packing
593 // attributes define a maximum and the alignment attribute defines
594 // a minimum.
595 // FIXME: What is the right behavior when the specified alignment
596 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000597 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000598 if (FieldPacking)
599 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000600 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
601 FieldAlign = std::max(FieldAlign, AA->getAlignment());
602
603 // Check if we need to add padding to give the field the correct
604 // alignment.
605 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
606 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
607
608 // Padding members don't affect overall alignment
609 if (!FD->getIdentifier())
610 FieldAlign = 1;
611 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000612 if (FD->getType()->isIncompleteArrayType()) {
613 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000614 // query getTypeInfo about these, so we figure it out here.
615 // Flexible array members don't have any size, but they
616 // have to be aligned appropriately for their element type.
617 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000618 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000619 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson0843ea52009-04-10 05:31:15 +0000620 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
621 unsigned AS = RT->getPointeeType().getAddressSpace();
622 FieldSize = Context.Target.getPointerWidth(AS);
623 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patelbfe323c2008-06-04 21:22:16 +0000624 } else {
625 std::pair<uint64_t, unsigned> FieldInfo =
626 Context.getTypeInfo(FD->getType());
627 FieldSize = FieldInfo.first;
628 FieldAlign = FieldInfo.second;
629 }
630
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000631 // Determine the alignment of this bitfield. The packing
632 // attributes define a maximum and the alignment attribute defines
633 // a minimum. Additionally, the packing alignment must be at least
634 // a byte for non-bitfields.
635 //
636 // FIXME: What is the right behavior when the specified alignment
637 // is smaller than the specified packing?
638 if (FieldPacking)
639 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000640 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
641 FieldAlign = std::max(FieldAlign, AA->getAlignment());
642
643 // Round up the current record size to the field's alignment boundary.
644 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
645 }
646
647 // Place this field at the current location.
648 FieldOffsets[FieldNo] = FieldOffset;
649
650 // Reserve space for this field.
651 if (IsUnion) {
652 Size = std::max(Size, FieldSize);
653 } else {
654 Size = FieldOffset + FieldSize;
655 }
656
Daniel Dunbar5523e862009-05-04 05:16:21 +0000657 // Remember the next available offset.
658 NextOffset = Size;
659
Devang Patelbfe323c2008-06-04 21:22:16 +0000660 // Remember max struct/class alignment.
661 Alignment = std::max(Alignment, FieldAlign);
662}
663
Daniel Dunbar1af336e2009-04-22 17:43:55 +0000664static void CollectLocalObjCIvars(ASTContext *Ctx,
665 const ObjCInterfaceDecl *OI,
666 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000667 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
668 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000669 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000670 if (!IVDecl->isInvalidDecl())
671 Fields.push_back(cast<FieldDecl>(IVDecl));
672 }
673}
674
Daniel Dunbar1af336e2009-04-22 17:43:55 +0000675void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
676 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
677 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
678 CollectObjCIvars(SuperClass, Fields);
679 CollectLocalObjCIvars(this, OI, Fields);
680}
681
Daniel Dunbar1fbaef12009-05-03 10:38:35 +0000682/// getInterfaceLayoutImpl - Get or compute information about the
683/// layout of the given interface.
684///
685/// \param Impl - If given, also include the layout of the interface's
686/// implementation. This may differ by including synthesized ivars.
Devang Patel4b6bf702008-06-04 21:54:36 +0000687const ASTRecordLayout &
Daniel Dunbar1fbaef12009-05-03 10:38:35 +0000688ASTContext::getObjCLayout(const ObjCInterfaceDecl *D,
689 const ObjCImplementationDecl *Impl) {
Daniel Dunbar94d2ede2009-05-03 13:15:50 +0000690 assert(!D->isForwardDecl() && "Invalid interface decl!");
691
Devang Patel4b6bf702008-06-04 21:54:36 +0000692 // Look up this layout, if already laid out, return what we have.
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000693 ObjCContainerDecl *Key =
694 Impl ? (ObjCContainerDecl*) Impl : (ObjCContainerDecl*) D;
695 if (const ASTRecordLayout *Entry = ObjCLayouts[Key])
696 return *Entry;
Devang Patel4b6bf702008-06-04 21:54:36 +0000697
Daniel Dunbar5b9332f2009-05-03 11:16:44 +0000698 unsigned FieldCount = D->ivar_size();
699 // Add in synthesized ivar count if laying out an implementation.
700 if (Impl) {
701 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
702 E = D->prop_end(*this); I != E; ++I)
703 if ((*I)->getPropertyIvarDecl())
704 ++FieldCount;
705
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000706 // If there aren't any sythesized ivars then reuse the interface
Daniel Dunbar5b9332f2009-05-03 11:16:44 +0000707 // entry. Note we can't cache this because we simply free all
708 // entries later; however we shouldn't look up implementations
709 // frequently.
710 if (FieldCount == D->ivar_size())
711 return getObjCLayout(D, 0);
712 }
713
Devang Patel8682d882008-06-06 02:14:01 +0000714 ASTRecordLayout *NewEntry = NULL;
Devang Patel8682d882008-06-06 02:14:01 +0000715 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
Daniel Dunbar94d2ede2009-05-03 13:15:50 +0000716 // FIXME: This increment of FieldCount is wrong, we don't actually
717 // count the super class as a member (see the field index passed
718 // to LayoutField below).
Devang Patel8682d882008-06-06 02:14:01 +0000719 FieldCount++;
720 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
721 unsigned Alignment = SL.getAlignment();
722 uint64_t Size = SL.getSize();
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000723
Daniel Dunbar5523e862009-05-04 05:16:21 +0000724 // If we are using tight interface packing, then we start laying
725 // out ivars not at the end of the superclass structure, but at
726 // the next byte following the last field.
727 if (getLangOptions().ObjCTightLayout)
728 Size = llvm::RoundUpToAlignment(SL.NextOffset, 8);
729
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000730 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout(Size, Alignment);
Devang Patel8682d882008-06-06 02:14:01 +0000731 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000732 // Super class is at the beginning of the layout.
733 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000734 } else {
Daniel Dunbarb3170af2009-05-03 11:41:43 +0000735 ObjCLayouts[Key] = NewEntry = new ASTRecordLayout();
Devang Patel8682d882008-06-06 02:14:01 +0000736 NewEntry->InitializeLayout(FieldCount);
737 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000738
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000739 unsigned StructPacking = 0;
740 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
741 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000742
743 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
744 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
745 AA->getAlignment()));
746
747 // Layout each ivar sequentially.
748 unsigned i = 0;
749 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
750 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
751 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000752 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000753 }
Daniel Dunbar5b9332f2009-05-03 11:16:44 +0000754 // And synthesized ivars, if this is an implementation.
755 if (Impl) {
756 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
757 E = D->prop_end(*this); I != E; ++I) {
758 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
759 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
760 }
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000761 }
Fariborz Jahanian84c45692009-04-01 19:37:34 +0000762
Devang Patel4b6bf702008-06-04 21:54:36 +0000763 // Finally, round the size of the total struct up to the alignment of the
764 // struct itself.
765 NewEntry->FinalizeLayout();
766 return *NewEntry;
767}
768
Daniel Dunbar1fbaef12009-05-03 10:38:35 +0000769const ASTRecordLayout &
770ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
771 return getObjCLayout(D, 0);
772}
773
774const ASTRecordLayout &
775ASTContext::getASTObjCImplementationLayout(const ObjCImplementationDecl *D) {
776 return getObjCLayout(D->getClassInterface(), D);
777}
778
Devang Patel7a78e432007-11-01 19:11:01 +0000779/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000780/// specified record (struct/union/class), which indicates its size and field
781/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000782const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000783 D = D->getDefinition(*this);
784 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000785
Chris Lattner4b009652007-07-25 00:24:17 +0000786 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000787 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000788 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000789
Devang Patel7a78e432007-11-01 19:11:01 +0000790 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
791 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
792 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000793 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000794
Douglas Gregor39677622008-12-11 20:41:00 +0000795 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000796 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
797 D->field_end(*this)));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000798 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000799
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000800 unsigned StructPacking = 0;
801 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
802 StructPacking = PA->getAlignment();
803
Eli Friedman5949a022008-05-30 09:31:38 +0000804 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000805 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
806 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000807
Eli Friedman5949a022008-05-30 09:31:38 +0000808 // Layout each field, for now, just sequentially, respecting alignment. In
809 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000810 unsigned FieldIdx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000811 for (RecordDecl::field_iterator Field = D->field_begin(*this),
812 FieldEnd = D->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000813 Field != FieldEnd; (void)++Field, ++FieldIdx)
814 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000815
816 // Finally, round the size of the total struct up to the alignment of the
817 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000818 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000819 return *NewEntry;
820}
821
Chris Lattner4b009652007-07-25 00:24:17 +0000822//===----------------------------------------------------------------------===//
823// Type creation/memoization methods
824//===----------------------------------------------------------------------===//
825
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000826QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000827 QualType CanT = getCanonicalType(T);
828 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000829 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000830
831 // If we are composing extended qualifiers together, merge together into one
832 // ExtQualType node.
833 unsigned CVRQuals = T.getCVRQualifiers();
834 QualType::GCAttrTypes GCAttr = QualType::GCNone;
835 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000836
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000837 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->getAddressSpace() == 0 &&
841 "Type cannot be in multiple addr spaces!");
842 GCAttr = EQT->getObjCGCAttr();
843 TypeNode = EQT->getBaseType();
844 }
Chris Lattner35fef522008-02-20 20:55:12 +0000845
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000846 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000847 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000848 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000849 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000850 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000851 return QualType(EXTQy, CVRQuals);
852
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000853 // If the base type isn't canonical, this won't be a canonical type either,
854 // so fill in the canonical type field.
855 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000856 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000857 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000858
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000859 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000860 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000861 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000862 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000863 ExtQualType *New =
864 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000865 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000866 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000867 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000868}
869
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000870QualType ASTContext::getObjCGCQualType(QualType T,
871 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000872 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000873 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000874 return T;
875
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000876 // If we are composing extended qualifiers together, merge together into one
877 // ExtQualType node.
878 unsigned CVRQuals = T.getCVRQualifiers();
879 Type *TypeNode = T.getTypePtr();
880 unsigned AddressSpace = 0;
881
882 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
883 // If this type already has an address space specified, it cannot get
884 // another one.
885 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
886 "Type cannot be in multiple addr spaces!");
887 AddressSpace = EQT->getAddressSpace();
888 TypeNode = EQT->getBaseType();
889 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000890
891 // Check if we've already instantiated an gc qual'd type of this type.
892 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000893 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000894 void *InsertPos = 0;
895 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000896 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000897
898 // If the base type isn't canonical, this won't be a canonical type either,
899 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +0000900 // FIXME: Isn't this also not canonical if the base type is a array
901 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000902 QualType Canonical;
903 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000904 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000905
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000906 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000907 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
908 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
909 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000910 ExtQualType *New =
911 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000912 ExtQualTypes.InsertNode(New, InsertPos);
913 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000914 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000915}
Chris Lattner4b009652007-07-25 00:24:17 +0000916
917/// getComplexType - Return the uniqued reference to the type for a complex
918/// number with the specified element type.
919QualType ASTContext::getComplexType(QualType T) {
920 // Unique pointers, to guarantee there is only one pointer of a particular
921 // structure.
922 llvm::FoldingSetNodeID ID;
923 ComplexType::Profile(ID, T);
924
925 void *InsertPos = 0;
926 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
927 return QualType(CT, 0);
928
929 // If the pointee type isn't canonical, this won't be a canonical type either,
930 // so fill in the canonical type field.
931 QualType Canonical;
932 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000933 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000934
935 // Get the new insert position for the node we care about.
936 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000937 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000938 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000939 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000940 Types.push_back(New);
941 ComplexTypes.InsertNode(New, InsertPos);
942 return QualType(New, 0);
943}
944
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000945QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
946 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
947 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
948 FixedWidthIntType *&Entry = Map[Width];
949 if (!Entry)
950 Entry = new FixedWidthIntType(Width, Signed);
951 return QualType(Entry, 0);
952}
Chris Lattner4b009652007-07-25 00:24:17 +0000953
954/// getPointerType - Return the uniqued reference to the type for a pointer to
955/// the specified type.
956QualType ASTContext::getPointerType(QualType T) {
957 // Unique pointers, to guarantee there is only one pointer of a particular
958 // structure.
959 llvm::FoldingSetNodeID ID;
960 PointerType::Profile(ID, T);
961
962 void *InsertPos = 0;
963 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
964 return QualType(PT, 0);
965
966 // If the pointee type isn't canonical, this won't be a canonical type either,
967 // so fill in the canonical type field.
968 QualType Canonical;
969 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000970 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000971
972 // Get the new insert position for the node we care about.
973 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000974 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000975 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000976 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000977 Types.push_back(New);
978 PointerTypes.InsertNode(New, InsertPos);
979 return QualType(New, 0);
980}
981
Steve Naroff7aa54752008-08-27 16:04:49 +0000982/// getBlockPointerType - Return the uniqued reference to the type for
983/// a pointer to the specified block.
984QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000985 assert(T->isFunctionType() && "block of function types only");
986 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000987 // structure.
988 llvm::FoldingSetNodeID ID;
989 BlockPointerType::Profile(ID, T);
990
991 void *InsertPos = 0;
992 if (BlockPointerType *PT =
993 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
994 return QualType(PT, 0);
995
Steve Narofffd5b19d2008-08-28 19:20:44 +0000996 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000997 // type either so fill in the canonical type field.
998 QualType Canonical;
999 if (!T->isCanonical()) {
1000 Canonical = getBlockPointerType(getCanonicalType(T));
1001
1002 // Get the new insert position for the node we care about.
1003 BlockPointerType *NewIP =
1004 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001005 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +00001006 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001007 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +00001008 Types.push_back(New);
1009 BlockPointerTypes.InsertNode(New, InsertPos);
1010 return QualType(New, 0);
1011}
1012
Sebastian Redlce6fff02009-03-16 23:22:08 +00001013/// getLValueReferenceType - Return the uniqued reference to the type for an
1014/// lvalue reference to the specified type.
1015QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +00001016 // Unique pointers, to guarantee there is only one pointer of a particular
1017 // structure.
1018 llvm::FoldingSetNodeID ID;
1019 ReferenceType::Profile(ID, T);
1020
1021 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +00001022 if (LValueReferenceType *RT =
1023 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001024 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001025
Chris Lattner4b009652007-07-25 00:24:17 +00001026 // If the referencee type isn't canonical, this won't be a canonical type
1027 // either, so fill in the canonical type field.
1028 QualType Canonical;
1029 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +00001030 Canonical = getLValueReferenceType(getCanonicalType(T));
1031
Chris Lattner4b009652007-07-25 00:24:17 +00001032 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +00001033 LValueReferenceType *NewIP =
1034 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001035 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001036 }
1037
Sebastian Redlce6fff02009-03-16 23:22:08 +00001038 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001039 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001040 LValueReferenceTypes.InsertNode(New, InsertPos);
1041 return QualType(New, 0);
1042}
1043
1044/// getRValueReferenceType - Return the uniqued reference to the type for an
1045/// rvalue reference to the specified type.
1046QualType ASTContext::getRValueReferenceType(QualType T) {
1047 // Unique pointers, to guarantee there is only one pointer of a particular
1048 // structure.
1049 llvm::FoldingSetNodeID ID;
1050 ReferenceType::Profile(ID, T);
1051
1052 void *InsertPos = 0;
1053 if (RValueReferenceType *RT =
1054 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1055 return QualType(RT, 0);
1056
1057 // If the referencee type isn't canonical, this won't be a canonical type
1058 // either, so fill in the canonical type field.
1059 QualType Canonical;
1060 if (!T->isCanonical()) {
1061 Canonical = getRValueReferenceType(getCanonicalType(T));
1062
1063 // Get the new insert position for the node we care about.
1064 RValueReferenceType *NewIP =
1065 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1066 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1067 }
1068
1069 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1070 Types.push_back(New);
1071 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001072 return QualType(New, 0);
1073}
1074
Sebastian Redl75555032009-01-24 21:16:55 +00001075/// getMemberPointerType - Return the uniqued reference to the type for a
1076/// member pointer to the specified type, in the specified class.
1077QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1078{
1079 // Unique pointers, to guarantee there is only one pointer of a particular
1080 // structure.
1081 llvm::FoldingSetNodeID ID;
1082 MemberPointerType::Profile(ID, T, Cls);
1083
1084 void *InsertPos = 0;
1085 if (MemberPointerType *PT =
1086 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1087 return QualType(PT, 0);
1088
1089 // If the pointee or class type isn't canonical, this won't be a canonical
1090 // type either, so fill in the canonical type field.
1091 QualType Canonical;
1092 if (!T->isCanonical()) {
1093 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1094
1095 // Get the new insert position for the node we care about.
1096 MemberPointerType *NewIP =
1097 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1098 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1099 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001100 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001101 Types.push_back(New);
1102 MemberPointerTypes.InsertNode(New, InsertPos);
1103 return QualType(New, 0);
1104}
1105
Steve Naroff83c13012007-08-30 01:06:46 +00001106/// getConstantArrayType - Return the unique reference to the type for an
1107/// array of the specified element type.
1108QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +00001109 const llvm::APInt &ArySize,
1110 ArrayType::ArraySizeModifier ASM,
1111 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001112 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001113 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001114
1115 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001116 if (ConstantArrayType *ATP =
1117 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001118 return QualType(ATP, 0);
1119
1120 // If the element type isn't canonical, this won't be a canonical type either,
1121 // so fill in the canonical type field.
1122 QualType Canonical;
1123 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001124 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001125 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001126 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001127 ConstantArrayType *NewIP =
1128 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001129 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001130 }
1131
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001132 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001133 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001134 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001135 Types.push_back(New);
1136 return QualType(New, 0);
1137}
1138
Steve Naroffe2579e32007-08-30 18:14:25 +00001139/// getVariableArrayType - Returns a non-unique reference to the type for a
1140/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +00001141QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1142 ArrayType::ArraySizeModifier ASM,
1143 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001144 // Since we don't unique expressions, it isn't possible to unique VLA's
1145 // that have an expression provided for their size.
1146
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001147 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001148 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001149
1150 VariableArrayTypes.push_back(New);
1151 Types.push_back(New);
1152 return QualType(New, 0);
1153}
1154
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001155/// getDependentSizedArrayType - Returns a non-unique reference to
1156/// the type for a dependently-sized array of the specified element
1157/// type. FIXME: We will need these to be uniqued, or at least
1158/// comparable, at some point.
1159QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1160 ArrayType::ArraySizeModifier ASM,
1161 unsigned EltTypeQuals) {
1162 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1163 "Size must be type- or value-dependent!");
1164
1165 // Since we don't unique expressions, it isn't possible to unique
1166 // dependently-sized array types.
1167
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001168 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001169 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1170 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001171
1172 DependentSizedArrayTypes.push_back(New);
1173 Types.push_back(New);
1174 return QualType(New, 0);
1175}
1176
Eli Friedman8ff07782008-02-15 18:16:39 +00001177QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1178 ArrayType::ArraySizeModifier ASM,
1179 unsigned EltTypeQuals) {
1180 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001181 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001182
1183 void *InsertPos = 0;
1184 if (IncompleteArrayType *ATP =
1185 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1186 return QualType(ATP, 0);
1187
1188 // If the element type isn't canonical, this won't be a canonical type
1189 // either, so fill in the canonical type field.
1190 QualType Canonical;
1191
1192 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001193 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001194 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001195
1196 // Get the new insert position for the node we care about.
1197 IncompleteArrayType *NewIP =
1198 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001199 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001200 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001201
Steve Naroff93fd2112009-01-27 22:08:43 +00001202 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001203 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001204
1205 IncompleteArrayTypes.InsertNode(New, InsertPos);
1206 Types.push_back(New);
1207 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001208}
1209
Chris Lattner4b009652007-07-25 00:24:17 +00001210/// getVectorType - Return the unique reference to a vector type of
1211/// the specified element type and size. VectorType must be a built-in type.
1212QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1213 BuiltinType *baseType;
1214
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001215 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001216 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1217
1218 // Check if we've already instantiated a vector of this type.
1219 llvm::FoldingSetNodeID ID;
1220 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1221 void *InsertPos = 0;
1222 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1223 return QualType(VTP, 0);
1224
1225 // If the element type isn't canonical, this won't be a canonical type either,
1226 // so fill in the canonical type field.
1227 QualType Canonical;
1228 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001229 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001230
1231 // Get the new insert position for the node we care about.
1232 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001233 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001234 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001235 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001236 VectorTypes.InsertNode(New, InsertPos);
1237 Types.push_back(New);
1238 return QualType(New, 0);
1239}
1240
Nate Begemanaf6ed502008-04-18 23:10:10 +00001241/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001242/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001243QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001244 BuiltinType *baseType;
1245
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001246 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001247 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001248
1249 // Check if we've already instantiated a vector of this type.
1250 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001251 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001252 void *InsertPos = 0;
1253 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1254 return QualType(VTP, 0);
1255
1256 // If the element type isn't canonical, this won't be a canonical type either,
1257 // so fill in the canonical type field.
1258 QualType Canonical;
1259 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001260 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001261
1262 // Get the new insert position for the node we care about.
1263 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001264 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001265 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001266 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001267 VectorTypes.InsertNode(New, InsertPos);
1268 Types.push_back(New);
1269 return QualType(New, 0);
1270}
1271
Douglas Gregor4fa58902009-02-26 23:50:07 +00001272/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001273///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001274QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001275 // Unique functions, to guarantee there is only one function of a particular
1276 // structure.
1277 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001278 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001279
1280 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001281 if (FunctionNoProtoType *FT =
1282 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001283 return QualType(FT, 0);
1284
1285 QualType Canonical;
1286 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001287 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001288
1289 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001290 FunctionNoProtoType *NewIP =
1291 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001292 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001293 }
1294
Douglas Gregor4fa58902009-02-26 23:50:07 +00001295 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001296 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001297 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001298 return QualType(New, 0);
1299}
1300
1301/// getFunctionType - Return a normal function type with a typed argument
1302/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001303QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001304 unsigned NumArgs, bool isVariadic,
Sebastian Redl74942cd2009-04-30 19:20:55 +00001305 unsigned TypeQuals, bool hasExceptionSpec,
1306 bool hasAnyExceptionSpec, unsigned NumExs,
1307 const QualType *ExArray) {
Chris Lattner4b009652007-07-25 00:24:17 +00001308 // Unique functions, to guarantee there is only one function of a particular
1309 // structure.
1310 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001311 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl74942cd2009-04-30 19:20:55 +00001312 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1313 NumExs, ExArray);
Chris Lattner4b009652007-07-25 00:24:17 +00001314
1315 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001316 if (FunctionProtoType *FTP =
1317 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001318 return QualType(FTP, 0);
1319
1320 // Determine whether the type being created is already canonical or not.
1321 bool isCanonical = ResultTy->isCanonical();
1322 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1323 if (!ArgArray[i]->isCanonical())
1324 isCanonical = false;
1325
1326 // If this type isn't canonical, get the canonical version of it.
1327 QualType Canonical;
1328 if (!isCanonical) {
1329 llvm::SmallVector<QualType, 16> CanonicalArgs;
1330 CanonicalArgs.reserve(NumArgs);
1331 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001332 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Sebastian Redl74942cd2009-04-30 19:20:55 +00001333 llvm::SmallVector<QualType, 2> CanonicalExs;
1334 CanonicalExs.reserve(NumExs);
1335 for (unsigned i = 0; i != NumExs; ++i)
1336 CanonicalExs.push_back(getCanonicalType(ExArray[i]));
1337
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001338 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001339 &CanonicalArgs[0], NumArgs,
Sebastian Redl74942cd2009-04-30 19:20:55 +00001340 isVariadic, TypeQuals, hasExceptionSpec,
1341 hasAnyExceptionSpec, NumExs, &CanonicalExs[0]);
1342
Chris Lattner4b009652007-07-25 00:24:17 +00001343 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001344 FunctionProtoType *NewIP =
1345 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001346 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001347 }
Sebastian Redl74942cd2009-04-30 19:20:55 +00001348
Douglas Gregor4fa58902009-02-26 23:50:07 +00001349 // FunctionProtoType objects are allocated with extra bytes after them
Sebastian Redl74942cd2009-04-30 19:20:55 +00001350 // for two variable size arrays (for parameter and exception types) at the
1351 // end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001352 FunctionProtoType *FTP =
Sebastian Redl74942cd2009-04-30 19:20:55 +00001353 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
1354 NumArgs*sizeof(QualType) +
1355 NumExs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001356 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Sebastian Redl74942cd2009-04-30 19:20:55 +00001357 TypeQuals, hasExceptionSpec, hasAnyExceptionSpec,
1358 ExArray, NumExs, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001359 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001360 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001361 return QualType(FTP, 0);
1362}
1363
Douglas Gregor1d661552008-04-13 21:07:44 +00001364/// getTypeDeclType - Return the unique reference to the type for the
1365/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001366QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001367 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001368 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1369
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001370 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001371 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001372 else if (isa<TemplateTypeParmDecl>(Decl)) {
1373 assert(false && "Template type parameter types are always available.");
1374 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001375 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001376
Douglas Gregor2e047592009-02-28 01:32:25 +00001377 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001378 if (PrevDecl)
1379 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001380 else
1381 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001382 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001383 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1384 if (PrevDecl)
1385 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001386 else
1387 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001388 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001389 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001390 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001391
Ted Kremenek46a837c2008-09-05 17:16:31 +00001392 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001393 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001394}
1395
Chris Lattner4b009652007-07-25 00:24:17 +00001396/// getTypedefType - Return the unique reference to the type for the
1397/// specified typename decl.
1398QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1399 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1400
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001401 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001402 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001403 Types.push_back(Decl->TypeForDecl);
1404 return QualType(Decl->TypeForDecl, 0);
1405}
1406
Ted Kremenek42730c52008-01-07 19:49:32 +00001407/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001408/// specified ObjC interface decl.
Daniel Dunbarbe1ff272009-04-22 04:34:53 +00001409QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001410 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1411
Daniel Dunbarbe1ff272009-04-22 04:34:53 +00001412 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1413 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001414 Types.push_back(Decl->TypeForDecl);
1415 return QualType(Decl->TypeForDecl, 0);
1416}
1417
Douglas Gregora4918772009-02-05 23:33:38 +00001418/// \brief Retrieve the template type parameter type for a template
1419/// parameter with the given depth, index, and (optionally) name.
1420QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1421 IdentifierInfo *Name) {
1422 llvm::FoldingSetNodeID ID;
1423 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1424 void *InsertPos = 0;
1425 TemplateTypeParmType *TypeParm
1426 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1427
1428 if (TypeParm)
1429 return QualType(TypeParm, 0);
1430
1431 if (Name)
1432 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1433 getTemplateTypeParmType(Depth, Index));
1434 else
1435 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1436
1437 Types.push_back(TypeParm);
1438 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1439
1440 return QualType(TypeParm, 0);
1441}
1442
Douglas Gregor8e458f42009-02-09 18:46:07 +00001443QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001444ASTContext::getTemplateSpecializationType(TemplateName Template,
1445 const TemplateArgument *Args,
1446 unsigned NumArgs,
1447 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001448 if (!Canon.isNull())
1449 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001450
Douglas Gregor8e458f42009-02-09 18:46:07 +00001451 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001452 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001453
Douglas Gregor8e458f42009-02-09 18:46:07 +00001454 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001455 TemplateSpecializationType *Spec
1456 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001457
1458 if (Spec)
1459 return QualType(Spec, 0);
1460
Douglas Gregordd13e842009-03-30 22:58:21 +00001461 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001462 sizeof(TemplateArgument) * NumArgs),
1463 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001464 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001465 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001466 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001467
1468 return QualType(Spec, 0);
1469}
1470
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001471QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001472ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001473 QualType NamedType) {
1474 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001475 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001476
1477 void *InsertPos = 0;
1478 QualifiedNameType *T
1479 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1480 if (T)
1481 return QualType(T, 0);
1482
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001483 T = new (*this) QualifiedNameType(NNS, NamedType,
1484 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001485 Types.push_back(T);
1486 QualifiedNameTypes.InsertNode(T, InsertPos);
1487 return QualType(T, 0);
1488}
1489
Douglas Gregord3022602009-03-27 23:10:48 +00001490QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1491 const IdentifierInfo *Name,
1492 QualType Canon) {
1493 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1494
1495 if (Canon.isNull()) {
1496 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1497 if (CanonNNS != NNS)
1498 Canon = getTypenameType(CanonNNS, Name);
1499 }
1500
1501 llvm::FoldingSetNodeID ID;
1502 TypenameType::Profile(ID, NNS, Name);
1503
1504 void *InsertPos = 0;
1505 TypenameType *T
1506 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1507 if (T)
1508 return QualType(T, 0);
1509
1510 T = new (*this) TypenameType(NNS, Name, Canon);
1511 Types.push_back(T);
1512 TypenameTypes.InsertNode(T, InsertPos);
1513 return QualType(T, 0);
1514}
1515
Douglas Gregor77da5802009-04-01 00:28:59 +00001516QualType
1517ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1518 const TemplateSpecializationType *TemplateId,
1519 QualType Canon) {
1520 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1521
1522 if (Canon.isNull()) {
1523 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1524 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1525 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1526 const TemplateSpecializationType *CanonTemplateId
1527 = CanonType->getAsTemplateSpecializationType();
1528 assert(CanonTemplateId &&
1529 "Canonical type must also be a template specialization type");
1530 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1531 }
1532 }
1533
1534 llvm::FoldingSetNodeID ID;
1535 TypenameType::Profile(ID, NNS, TemplateId);
1536
1537 void *InsertPos = 0;
1538 TypenameType *T
1539 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1540 if (T)
1541 return QualType(T, 0);
1542
1543 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1544 Types.push_back(T);
1545 TypenameTypes.InsertNode(T, InsertPos);
1546 return QualType(T, 0);
1547}
1548
Chris Lattnere1352302008-04-07 04:56:42 +00001549/// CmpProtocolNames - Comparison predicate for sorting protocols
1550/// alphabetically.
1551static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1552 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001553 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001554}
1555
1556static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1557 unsigned &NumProtocols) {
1558 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1559
1560 // Sort protocols, keyed by name.
1561 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1562
1563 // Remove duplicates.
1564 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1565 NumProtocols = ProtocolsEnd-Protocols;
1566}
1567
1568
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001569/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1570/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001571QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1572 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001573 // Sort the protocol list alphabetically to canonicalize it.
1574 SortAndUniqueProtocols(Protocols, NumProtocols);
1575
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001576 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001577 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001578
1579 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001580 if (ObjCQualifiedInterfaceType *QT =
1581 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001582 return QualType(QT, 0);
1583
1584 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001585 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001586 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001587
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001588 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001589 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001590 return QualType(QType, 0);
1591}
1592
Chris Lattnere1352302008-04-07 04:56:42 +00001593/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1594/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001595QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001596 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001597 // Sort the protocol list alphabetically to canonicalize it.
1598 SortAndUniqueProtocols(Protocols, NumProtocols);
1599
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001600 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001601 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001602
1603 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001604 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001605 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001606 return QualType(QT, 0);
1607
1608 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001609 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001610 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001611 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001612 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001613 return QualType(QType, 0);
1614}
1615
Douglas Gregor4fa58902009-02-26 23:50:07 +00001616/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1617/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001618/// multiple declarations that refer to "typeof(x)" all contain different
1619/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1620/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001621QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001622 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001623 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001624 Types.push_back(toe);
1625 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001626}
1627
Steve Naroff0604dd92007-08-01 18:02:17 +00001628/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1629/// TypeOfType AST's. The only motivation to unique these nodes would be
1630/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1631/// an issue. This doesn't effect the type checker, since it operates
1632/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001633QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001634 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001635 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001636 Types.push_back(tot);
1637 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001638}
1639
Chris Lattner4b009652007-07-25 00:24:17 +00001640/// getTagDeclType - Return the unique reference to the type for the
1641/// specified TagDecl (struct/union/class/enum) decl.
1642QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001643 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001644 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001645}
1646
1647/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1648/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1649/// needs to agree with the definition in <stddef.h>.
1650QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001651 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001652}
1653
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001654/// getSignedWCharType - Return the type of "signed wchar_t".
1655/// Used when in C++, as a GCC extension.
1656QualType ASTContext::getSignedWCharType() const {
1657 // FIXME: derive from "Target" ?
1658 return WCharTy;
1659}
1660
1661/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1662/// Used when in C++, as a GCC extension.
1663QualType ASTContext::getUnsignedWCharType() const {
1664 // FIXME: derive from "Target" ?
1665 return UnsignedIntTy;
1666}
1667
Chris Lattner4b009652007-07-25 00:24:17 +00001668/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1669/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1670QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001671 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001672}
1673
Chris Lattner19eb97e2008-04-02 05:18:44 +00001674//===----------------------------------------------------------------------===//
1675// Type Operators
1676//===----------------------------------------------------------------------===//
1677
Chris Lattner3dae6f42008-04-06 22:41:35 +00001678/// getCanonicalType - Return the canonical (structural) type corresponding to
1679/// the specified potentially non-canonical type. The non-canonical version
1680/// of a type may have many "decorated" versions of types. Decorators can
1681/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1682/// to be free of any of these, allowing two canonical types to be compared
1683/// for exact equality with a simple pointer comparison.
1684QualType ASTContext::getCanonicalType(QualType T) {
1685 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001686
1687 // If the result has type qualifiers, make sure to canonicalize them as well.
1688 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1689 if (TypeQuals == 0) return CanType;
1690
1691 // If the type qualifiers are on an array type, get the canonical type of the
1692 // array with the qualifiers applied to the element type.
1693 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1694 if (!AT)
1695 return CanType.getQualifiedType(TypeQuals);
1696
1697 // Get the canonical version of the element with the extra qualifiers on it.
1698 // This can recursively sink qualifiers through multiple levels of arrays.
1699 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1700 NewEltTy = getCanonicalType(NewEltTy);
1701
1702 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1703 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1704 CAT->getIndexTypeQualifier());
1705 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1706 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1707 IAT->getIndexTypeQualifier());
1708
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001709 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1710 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1711 DSAT->getSizeModifier(),
1712 DSAT->getIndexTypeQualifier());
1713
Chris Lattnera1923f62008-08-04 07:31:14 +00001714 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1715 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1716 VAT->getSizeModifier(),
1717 VAT->getIndexTypeQualifier());
1718}
1719
Douglas Gregord3022602009-03-27 23:10:48 +00001720NestedNameSpecifier *
1721ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1722 if (!NNS)
1723 return 0;
1724
1725 switch (NNS->getKind()) {
1726 case NestedNameSpecifier::Identifier:
1727 // Canonicalize the prefix but keep the identifier the same.
1728 return NestedNameSpecifier::Create(*this,
1729 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1730 NNS->getAsIdentifier());
1731
1732 case NestedNameSpecifier::Namespace:
1733 // A namespace is canonical; build a nested-name-specifier with
1734 // this namespace and no prefix.
1735 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1736
1737 case NestedNameSpecifier::TypeSpec:
1738 case NestedNameSpecifier::TypeSpecWithTemplate: {
1739 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1740 NestedNameSpecifier *Prefix = 0;
1741
1742 // FIXME: This isn't the right check!
1743 if (T->isDependentType())
1744 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1745
1746 return NestedNameSpecifier::Create(*this, Prefix,
1747 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1748 T.getTypePtr());
1749 }
1750
1751 case NestedNameSpecifier::Global:
1752 // The global specifier is canonical and unique.
1753 return NNS;
1754 }
1755
1756 // Required to silence a GCC warning
1757 return 0;
1758}
1759
Chris Lattnera1923f62008-08-04 07:31:14 +00001760
1761const ArrayType *ASTContext::getAsArrayType(QualType T) {
1762 // Handle the non-qualified case efficiently.
1763 if (T.getCVRQualifiers() == 0) {
1764 // Handle the common positive case fast.
1765 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1766 return AT;
1767 }
1768
1769 // Handle the common negative case fast, ignoring CVR qualifiers.
1770 QualType CType = T->getCanonicalTypeInternal();
1771
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001772 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001773 // test.
1774 if (!isa<ArrayType>(CType) &&
1775 !isa<ArrayType>(CType.getUnqualifiedType()))
1776 return 0;
1777
1778 // Apply any CVR qualifiers from the array type to the element type. This
1779 // implements C99 6.7.3p8: "If the specification of an array type includes
1780 // any type qualifiers, the element type is so qualified, not the array type."
1781
1782 // If we get here, we either have type qualifiers on the type, or we have
1783 // sugar such as a typedef in the way. If we have type qualifiers on the type
1784 // we must propagate them down into the elemeng type.
1785 unsigned CVRQuals = T.getCVRQualifiers();
1786 unsigned AddrSpace = 0;
1787 Type *Ty = T.getTypePtr();
1788
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001789 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001790 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001791 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1792 AddrSpace = EXTQT->getAddressSpace();
1793 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001794 } else {
1795 T = Ty->getDesugaredType();
1796 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1797 break;
1798 CVRQuals |= T.getCVRQualifiers();
1799 Ty = T.getTypePtr();
1800 }
1801 }
1802
1803 // If we have a simple case, just return now.
1804 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1805 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1806 return ATy;
1807
1808 // Otherwise, we have an array and we have qualifiers on it. Push the
1809 // qualifiers into the array element type and return a new array type.
1810 // Get the canonical version of the element with the extra qualifiers on it.
1811 // This can recursively sink qualifiers through multiple levels of arrays.
1812 QualType NewEltTy = ATy->getElementType();
1813 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001814 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001815 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1816
1817 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1818 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1819 CAT->getSizeModifier(),
1820 CAT->getIndexTypeQualifier()));
1821 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1822 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1823 IAT->getSizeModifier(),
1824 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001825
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001826 if (const DependentSizedArrayType *DSAT
1827 = dyn_cast<DependentSizedArrayType>(ATy))
1828 return cast<ArrayType>(
1829 getDependentSizedArrayType(NewEltTy,
1830 DSAT->getSizeExpr(),
1831 DSAT->getSizeModifier(),
1832 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001833
Chris Lattnera1923f62008-08-04 07:31:14 +00001834 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1835 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1836 VAT->getSizeModifier(),
1837 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001838}
1839
1840
Chris Lattner19eb97e2008-04-02 05:18:44 +00001841/// getArrayDecayedType - Return the properly qualified result of decaying the
1842/// specified array type to a pointer. This operation is non-trivial when
1843/// handling typedefs etc. The canonical type of "T" must be an array type,
1844/// this returns a pointer to a properly qualified element of the array.
1845///
1846/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1847QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001848 // Get the element type with 'getAsArrayType' so that we don't lose any
1849 // typedefs in the element type of the array. This also handles propagation
1850 // of type qualifiers from the array type into the element type if present
1851 // (C99 6.7.3p8).
1852 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1853 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001854
Chris Lattnera1923f62008-08-04 07:31:14 +00001855 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001856
1857 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001858 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001859}
1860
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001861QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001862 QualType ElemTy = VAT->getElementType();
1863
1864 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1865 return getBaseElementType(VAT);
1866
1867 return ElemTy;
1868}
1869
Chris Lattner4b009652007-07-25 00:24:17 +00001870/// getFloatingRank - Return a relative rank for floating point types.
1871/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001872static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001873 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001874 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001875
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001876 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001877 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001878 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001879 case BuiltinType::Float: return FloatRank;
1880 case BuiltinType::Double: return DoubleRank;
1881 case BuiltinType::LongDouble: return LongDoubleRank;
1882 }
1883}
1884
Steve Narofffa0c4532007-08-27 01:41:48 +00001885/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1886/// point or a complex type (based on typeDomain/typeSize).
1887/// 'typeDomain' is a real floating point or complex type.
1888/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001889QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1890 QualType Domain) const {
1891 FloatingRank EltRank = getFloatingRank(Size);
1892 if (Domain->isComplexType()) {
1893 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001894 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001895 case FloatRank: return FloatComplexTy;
1896 case DoubleRank: return DoubleComplexTy;
1897 case LongDoubleRank: return LongDoubleComplexTy;
1898 }
Chris Lattner4b009652007-07-25 00:24:17 +00001899 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001900
1901 assert(Domain->isRealFloatingType() && "Unknown domain!");
1902 switch (EltRank) {
1903 default: assert(0 && "getFloatingRank(): illegal value for rank");
1904 case FloatRank: return FloatTy;
1905 case DoubleRank: return DoubleTy;
1906 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001907 }
Chris Lattner4b009652007-07-25 00:24:17 +00001908}
1909
Chris Lattner51285d82008-04-06 23:55:33 +00001910/// getFloatingTypeOrder - Compare the rank of the two specified floating
1911/// point types, ignoring the domain of the type (i.e. 'double' ==
1912/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1913/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001914int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1915 FloatingRank LHSR = getFloatingRank(LHS);
1916 FloatingRank RHSR = getFloatingRank(RHS);
1917
1918 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001919 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001920 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001921 return 1;
1922 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001923}
1924
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001925/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1926/// routine will assert if passed a built-in type that isn't an integer or enum,
1927/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001928unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001929 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001930 if (EnumType* ET = dyn_cast<EnumType>(T))
1931 T = ET->getDecl()->getIntegerType().getTypePtr();
1932
1933 // There are two things which impact the integer rank: the width, and
1934 // the ordering of builtins. The builtin ordering is encoded in the
1935 // bottom three bits; the width is encoded in the bits above that.
1936 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1937 return FWIT->getWidth() << 3;
1938 }
1939
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001940 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001941 default: assert(0 && "getIntegerRank(): not a built-in integer");
1942 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001943 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001944 case BuiltinType::Char_S:
1945 case BuiltinType::Char_U:
1946 case BuiltinType::SChar:
1947 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001948 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001949 case BuiltinType::Short:
1950 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001951 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001952 case BuiltinType::Int:
1953 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001954 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001955 case BuiltinType::Long:
1956 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001957 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001958 case BuiltinType::LongLong:
1959 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001960 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattner6cc7e412009-04-30 02:43:43 +00001961 case BuiltinType::Int128:
1962 case BuiltinType::UInt128:
1963 return 7 + (getIntWidth(Int128Ty) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001964 }
1965}
1966
Chris Lattner51285d82008-04-06 23:55:33 +00001967/// getIntegerTypeOrder - Returns the highest ranked integer type:
1968/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1969/// LHS < RHS, return -1.
1970int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001971 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1972 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001973 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001974
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001975 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1976 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001977
Chris Lattner51285d82008-04-06 23:55:33 +00001978 unsigned LHSRank = getIntegerRank(LHSC);
1979 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001980
Chris Lattner51285d82008-04-06 23:55:33 +00001981 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1982 if (LHSRank == RHSRank) return 0;
1983 return LHSRank > RHSRank ? 1 : -1;
1984 }
Chris Lattner4b009652007-07-25 00:24:17 +00001985
Chris Lattner51285d82008-04-06 23:55:33 +00001986 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1987 if (LHSUnsigned) {
1988 // If the unsigned [LHS] type is larger, return it.
1989 if (LHSRank >= RHSRank)
1990 return 1;
1991
1992 // If the signed type can represent all values of the unsigned type, it
1993 // wins. Because we are dealing with 2's complement and types that are
1994 // powers of two larger than each other, this is always safe.
1995 return -1;
1996 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001997
Chris Lattner51285d82008-04-06 23:55:33 +00001998 // If the unsigned [RHS] type is larger, return it.
1999 if (RHSRank >= LHSRank)
2000 return -1;
2001
2002 // If the signed type can represent all values of the unsigned type, it
2003 // wins. Because we are dealing with 2's complement and types that are
2004 // powers of two larger than each other, this is always safe.
2005 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00002006}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002007
2008// getCFConstantStringType - Return the type used for constant CFStrings.
2009QualType ASTContext::getCFConstantStringType() {
2010 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00002011 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00002012 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00002013 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002014 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002015
2016 // const int *isa;
2017 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002018 // int flags;
2019 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002020 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002021 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002022 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00002023 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00002024
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002025 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00002026 for (unsigned i = 0; i < 4; ++i) {
2027 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2028 SourceLocation(), 0,
2029 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002030 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002031 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002032 }
2033
2034 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002035 }
2036
2037 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00002038}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002039
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002040void ASTContext::setCFConstantStringType(QualType T) {
2041 const RecordType *Rec = T->getAsRecordType();
2042 assert(Rec && "Invalid CFConstantStringType");
2043 CFConstantStringTypeDecl = Rec->getDecl();
2044}
2045
Anders Carlssonf58cac72008-08-30 19:34:46 +00002046QualType ASTContext::getObjCFastEnumerationStateType()
2047{
2048 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00002049 ObjCFastEnumerationStateTypeDecl =
2050 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2051 &Idents.get("__objcFastEnumerationState"));
2052
Anders Carlssonf58cac72008-08-30 19:34:46 +00002053 QualType FieldTypes[] = {
2054 UnsignedLongTy,
2055 getPointerType(ObjCIdType),
2056 getPointerType(UnsignedLongTy),
2057 getConstantArrayType(UnsignedLongTy,
2058 llvm::APInt(32, 5), ArrayType::Normal, 0)
2059 };
2060
Douglas Gregor8acb7272008-12-11 16:49:14 +00002061 for (size_t i = 0; i < 4; ++i) {
2062 FieldDecl *Field = FieldDecl::Create(*this,
2063 ObjCFastEnumerationStateTypeDecl,
2064 SourceLocation(), 0,
2065 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002066 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002067 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002068 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00002069
Douglas Gregor8acb7272008-12-11 16:49:14 +00002070 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00002071 }
2072
2073 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2074}
2075
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002076void ASTContext::setObjCFastEnumerationStateType(QualType T) {
2077 const RecordType *Rec = T->getAsRecordType();
2078 assert(Rec && "Invalid ObjCFAstEnumerationStateType");
2079 ObjCFastEnumerationStateTypeDecl = Rec->getDecl();
2080}
2081
Anders Carlssone3f02572007-10-29 06:33:42 +00002082// This returns true if a type has been typedefed to BOOL:
2083// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00002084static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002085 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00002086 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2087 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002088
2089 return false;
2090}
2091
Ted Kremenek42730c52008-01-07 19:49:32 +00002092/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002093/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00002094int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00002095 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002096
2097 // Make all integer and enum types at least as large as an int
2098 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002099 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002100 // Treat arrays as pointers, since that's how they're passed in.
2101 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002102 sz = getTypeSize(VoidPtrTy);
2103 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002104}
2105
Ted Kremenek42730c52008-01-07 19:49:32 +00002106/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002107/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002108void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00002109 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002110 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002111 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00002112 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002113 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002114 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002115 // Compute size of all parameters.
2116 // Start with computing size of a pointer in number of bytes.
2117 // FIXME: There might(should) be a better way of doing this computation!
2118 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002119 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002120 // The first two arguments (self and _cmd) are pointers; account for
2121 // their size.
2122 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002123 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2124 E = Decl->param_end(); PI != E; ++PI) {
2125 QualType PType = (*PI)->getType();
2126 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002127 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002128 ParmOffset += sz;
2129 }
2130 S += llvm::utostr(ParmOffset);
2131 S += "@0:";
2132 S += llvm::utostr(PtrSize);
2133
2134 // Argument types.
2135 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002136 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2137 E = Decl->param_end(); PI != E; ++PI) {
2138 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002139 QualType PType = PVDecl->getOriginalType();
2140 if (const ArrayType *AT =
Steve Naroff78380fb2009-04-14 00:03:58 +00002141 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2142 // Use array's original type only if it has known number of
2143 // elements.
Steve Naroff6777bf32009-04-14 00:40:09 +00002144 if (!isa<ConstantArrayType>(AT))
Steve Naroff78380fb2009-04-14 00:03:58 +00002145 PType = PVDecl->getType();
2146 } else if (PType->isFunctionType())
2147 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002148 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002149 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002150 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002151 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002152 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002153 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002154 }
2155}
2156
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002157/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002158/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002159/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2160/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002161/// Property attributes are stored as a comma-delimited C string. The simple
2162/// attributes readonly and bycopy are encoded as single characters. The
2163/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2164/// encoded as single characters, followed by an identifier. Property types
2165/// are also encoded as a parametrized attribute. The characters used to encode
2166/// these attributes are defined by the following enumeration:
2167/// @code
2168/// enum PropertyAttributes {
2169/// kPropertyReadOnly = 'R', // property is read-only.
2170/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2171/// kPropertyByref = '&', // property is a reference to the value last assigned
2172/// kPropertyDynamic = 'D', // property is dynamic
2173/// kPropertyGetter = 'G', // followed by getter selector name
2174/// kPropertySetter = 'S', // followed by setter selector name
2175/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2176/// kPropertyType = 't' // followed by old-style type encoding.
2177/// kPropertyWeak = 'W' // 'weak' property
2178/// kPropertyStrong = 'P' // property GC'able
2179/// kPropertyNonAtomic = 'N' // property non-atomic
2180/// };
2181/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002182void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2183 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002184 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002185 // Collect information from the property implementation decl(s).
2186 bool Dynamic = false;
2187 ObjCPropertyImplDecl *SynthesizePID = 0;
2188
2189 // FIXME: Duplicated code due to poor abstraction.
2190 if (Container) {
2191 if (const ObjCCategoryImplDecl *CID =
2192 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2193 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregorcd19b572009-04-23 01:02:12 +00002194 i = CID->propimpl_begin(*this), e = CID->propimpl_end(*this);
2195 i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002196 ObjCPropertyImplDecl *PID = *i;
2197 if (PID->getPropertyDecl() == PD) {
2198 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2199 Dynamic = true;
2200 } else {
2201 SynthesizePID = PID;
2202 }
2203 }
2204 }
2205 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002206 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002207 for (ObjCCategoryImplDecl::propimpl_iterator
Douglas Gregorcd19b572009-04-23 01:02:12 +00002208 i = OID->propimpl_begin(*this), e = OID->propimpl_end(*this);
2209 i != e; ++i) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002210 ObjCPropertyImplDecl *PID = *i;
2211 if (PID->getPropertyDecl() == PD) {
2212 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2213 Dynamic = true;
2214 } else {
2215 SynthesizePID = PID;
2216 }
2217 }
2218 }
2219 }
2220 }
2221
2222 // FIXME: This is not very efficient.
2223 S = "T";
2224
2225 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002226 // GCC has some special rules regarding encoding of properties which
2227 // closely resembles encoding of ivars.
Daniel Dunbar701c8502009-04-20 06:37:24 +00002228 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002229 true /* outermost type */,
2230 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002231
2232 if (PD->isReadOnly()) {
2233 S += ",R";
2234 } else {
2235 switch (PD->getSetterKind()) {
2236 case ObjCPropertyDecl::Assign: break;
2237 case ObjCPropertyDecl::Copy: S += ",C"; break;
2238 case ObjCPropertyDecl::Retain: S += ",&"; break;
2239 }
2240 }
2241
2242 // It really isn't clear at all what this means, since properties
2243 // are "dynamic by default".
2244 if (Dynamic)
2245 S += ",D";
2246
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002247 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2248 S += ",N";
2249
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002250 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2251 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002252 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002253 }
2254
2255 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2256 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002257 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002258 }
2259
2260 if (SynthesizePID) {
2261 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2262 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002263 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002264 }
2265
2266 // FIXME: OBJCGC: weak & strong
2267}
2268
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002269/// getLegacyIntegralTypeEncoding -
2270/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002271/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002272/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2273///
2274void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2275 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2276 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002277 if (BT->getKind() == BuiltinType::ULong &&
2278 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002279 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002280 else
2281 if (BT->getKind() == BuiltinType::Long &&
2282 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002283 PointeeTy = IntTy;
2284 }
2285 }
2286}
2287
Fariborz Jahanian248db262008-01-22 22:44:46 +00002288void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002289 const FieldDecl *Field) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002290 // We follow the behavior of gcc, expanding structures which are
2291 // directly pointed to, and expanding embedded structures. Note that
2292 // these rules are sufficient to prevent recursive encoding of the
2293 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002294 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2295 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002296}
2297
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002298static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002299 const FieldDecl *FD) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002300 const Expr *E = FD->getBitWidth();
2301 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2302 ASTContext *Ctx = const_cast<ASTContext*>(Context);
Eli Friedman5255e7a2009-04-26 19:19:15 +00002303 unsigned N = E->EvaluateAsInt(*Ctx).getZExtValue();
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002304 S += 'b';
2305 S += llvm::utostr(N);
2306}
2307
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002308void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2309 bool ExpandPointedToStructures,
2310 bool ExpandStructures,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002311 const FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002312 bool OutermostType,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002313 bool EncodingProperty) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002314 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002315 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002316 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002317 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002318 else {
2319 char encoding;
2320 switch (BT->getKind()) {
2321 default: assert(0 && "Unhandled builtin type kind");
2322 case BuiltinType::Void: encoding = 'v'; break;
2323 case BuiltinType::Bool: encoding = 'B'; break;
2324 case BuiltinType::Char_U:
2325 case BuiltinType::UChar: encoding = 'C'; break;
2326 case BuiltinType::UShort: encoding = 'S'; break;
2327 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002328 case BuiltinType::ULong:
2329 encoding =
2330 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2331 break;
Chris Lattner6cc7e412009-04-30 02:43:43 +00002332 case BuiltinType::UInt128: encoding = 'T'; break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002333 case BuiltinType::ULongLong: encoding = 'Q'; break;
2334 case BuiltinType::Char_S:
2335 case BuiltinType::SChar: encoding = 'c'; break;
2336 case BuiltinType::Short: encoding = 's'; break;
2337 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002338 case BuiltinType::Long:
2339 encoding =
2340 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2341 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002342 case BuiltinType::LongLong: encoding = 'q'; break;
Chris Lattner6cc7e412009-04-30 02:43:43 +00002343 case BuiltinType::Int128: encoding = 't'; break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002344 case BuiltinType::Float: encoding = 'f'; break;
2345 case BuiltinType::Double: encoding = 'd'; break;
2346 case BuiltinType::LongDouble: encoding = 'd'; break;
2347 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002348
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002349 S += encoding;
2350 }
Anders Carlsson70e16dd2009-04-09 21:55:45 +00002351 } else if (const ComplexType *CT = T->getAsComplexType()) {
2352 S += 'j';
2353 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2354 false);
2355 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002356 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2357 ExpandPointedToStructures,
2358 ExpandStructures, FD);
2359 if (FD || EncodingProperty) {
2360 // Note that we do extended encoding of protocol qualifer list
2361 // Only when doing ivar or property encoding.
2362 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2363 S += '"';
2364 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2365 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2366 S += '<';
2367 S += Proto->getNameAsString();
2368 S += '>';
2369 }
2370 S += '"';
2371 }
2372 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002373 }
2374 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002375 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002376 bool isReadOnly = false;
2377 // For historical/compatibility reasons, the read-only qualifier of the
2378 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2379 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2380 // Also, do not emit the 'r' for anything but the outermost type!
2381 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2382 if (OutermostType && T.isConstQualified()) {
2383 isReadOnly = true;
2384 S += 'r';
2385 }
2386 }
2387 else if (OutermostType) {
2388 QualType P = PointeeTy;
2389 while (P->getAsPointerType())
2390 P = P->getAsPointerType()->getPointeeType();
2391 if (P.isConstQualified()) {
2392 isReadOnly = true;
2393 S += 'r';
2394 }
2395 }
2396 if (isReadOnly) {
2397 // Another legacy compatibility encoding. Some ObjC qualifier and type
2398 // combinations need to be rearranged.
2399 // Rewrite "in const" from "nr" to "rn"
2400 const char * s = S.c_str();
2401 int len = S.length();
2402 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2403 std::string replace = "rn";
2404 S.replace(S.end()-2, S.end(), replace);
2405 }
2406 }
Steve Naroff17c03822009-02-12 17:52:19 +00002407 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002408 S += '@';
2409 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002410 }
2411 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002412 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002413 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002414 // Another historical/compatibility reason.
2415 // We encode the underlying type which comes out as
2416 // {...};
2417 S += '^';
2418 getObjCEncodingForTypeImpl(PointeeTy, S,
2419 false, ExpandPointedToStructures,
2420 NULL);
2421 return;
2422 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002423 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002424 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002425 const ObjCInterfaceType *OIT =
2426 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002427 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002428 S += '"';
2429 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002430 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2431 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2432 S += '<';
2433 S += Proto->getNameAsString();
2434 S += '>';
2435 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002436 S += '"';
2437 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002438 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002439 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002440 S += '#';
2441 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002442 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002443 S += ':';
2444 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002445 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002446
2447 if (PointeeTy->isCharType()) {
2448 // char pointer types should be encoded as '*' unless it is a
2449 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002450 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002451 S += '*';
2452 return;
2453 }
2454 }
2455
2456 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002457 getLegacyIntegralTypeEncoding(PointeeTy);
2458
2459 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002460 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002461 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002462 } else if (const ArrayType *AT =
2463 // Ignore type qualifiers etc.
2464 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002465 if (isa<IncompleteArrayType>(AT)) {
2466 // Incomplete arrays are encoded as a pointer to the array element.
2467 S += '^';
2468
2469 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2470 false, ExpandStructures, FD);
2471 } else {
2472 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002473
Anders Carlsson858c64d2009-02-22 01:38:57 +00002474 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2475 S += llvm::utostr(CAT->getSize().getZExtValue());
2476 else {
2477 //Variable length arrays are encoded as a regular array with 0 elements.
2478 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2479 S += '0';
2480 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002481
Anders Carlsson858c64d2009-02-22 01:38:57 +00002482 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2483 false, ExpandStructures, FD);
2484 S += ']';
2485 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002486 } else if (T->getAsFunctionType()) {
2487 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002488 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002489 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002490 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002491 // Anonymous structures print as '?'
2492 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2493 S += II->getName();
2494 } else {
2495 S += '?';
2496 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002497 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002498 S += '=';
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002499 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2500 FieldEnd = RDecl->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002501 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002502 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002503 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002504 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002505 S += '"';
2506 }
2507
2508 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002509 if (Field->isBitField()) {
2510 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2511 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002512 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002513 QualType qt = Field->getType();
2514 getLegacyIntegralTypeEncoding(qt);
2515 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002516 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002517 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002518 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002519 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002520 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002521 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002522 if (FD && FD->isBitField())
2523 EncodeBitField(this, S, FD);
2524 else
2525 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002526 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002527 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002528 } else if (T->isObjCInterfaceType()) {
2529 // @encode(class_name)
2530 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2531 S += '{';
2532 const IdentifierInfo *II = OI->getIdentifier();
2533 S += II->getName();
2534 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002535 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002536 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002537 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002538 if (RecFields[i]->isBitField())
2539 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2540 RecFields[i]);
2541 else
2542 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2543 FD);
2544 }
2545 S += '}';
2546 }
2547 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002548 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002549}
2550
Ted Kremenek42730c52008-01-07 19:49:32 +00002551void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002552 std::string& S) const {
2553 if (QT & Decl::OBJC_TQ_In)
2554 S += 'n';
2555 if (QT & Decl::OBJC_TQ_Inout)
2556 S += 'N';
2557 if (QT & Decl::OBJC_TQ_Out)
2558 S += 'o';
2559 if (QT & Decl::OBJC_TQ_Bycopy)
2560 S += 'O';
2561 if (QT & Decl::OBJC_TQ_Byref)
2562 S += 'R';
2563 if (QT & Decl::OBJC_TQ_Oneway)
2564 S += 'V';
2565}
2566
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002567void ASTContext::setBuiltinVaListType(QualType T)
2568{
2569 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2570
2571 BuiltinVaListType = T;
2572}
2573
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002574void ASTContext::setObjCIdType(QualType T)
Steve Naroff9d12c902007-10-15 14:41:52 +00002575{
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002576 ObjCIdType = T;
2577
2578 const TypedefType *TT = T->getAsTypedefType();
2579 if (!TT)
2580 return;
2581
2582 TypedefDecl *TD = TT->getDecl();
Steve Naroff9d12c902007-10-15 14:41:52 +00002583
2584 // typedef struct objc_object *id;
2585 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002586 // User error - caller will issue diagnostics.
2587 if (!ptr)
2588 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002589 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002590 // User error - caller will issue diagnostics.
2591 if (!rec)
2592 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002593 IdStructType = rec;
2594}
2595
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002596void ASTContext::setObjCSelType(QualType T)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002597{
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002598 ObjCSelType = T;
2599
2600 const TypedefType *TT = T->getAsTypedefType();
2601 if (!TT)
2602 return;
2603 TypedefDecl *TD = TT->getDecl();
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002604
2605 // typedef struct objc_selector *SEL;
2606 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002607 if (!ptr)
2608 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002609 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002610 if (!rec)
2611 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002612 SelStructType = rec;
2613}
2614
Ted Kremenek42730c52008-01-07 19:49:32 +00002615void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002616{
Ted Kremenek42730c52008-01-07 19:49:32 +00002617 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002618}
2619
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002620void ASTContext::setObjCClassType(QualType T)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002621{
Douglas Gregorbb21d4b2009-04-23 22:29:11 +00002622 ObjCClassType = T;
2623
2624 const TypedefType *TT = T->getAsTypedefType();
2625 if (!TT)
2626 return;
2627 TypedefDecl *TD = TT->getDecl();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002628
2629 // typedef struct objc_class *Class;
2630 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2631 assert(ptr && "'Class' incorrectly typed");
2632 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2633 assert(rec && "'Class' incorrectly typed");
2634 ClassStructType = rec;
2635}
2636
Ted Kremenek42730c52008-01-07 19:49:32 +00002637void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2638 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002639 "'NSConstantString' type already set!");
2640
Ted Kremenek42730c52008-01-07 19:49:32 +00002641 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002642}
2643
Douglas Gregordd13e842009-03-30 22:58:21 +00002644/// \brief Retrieve the template name that represents a qualified
2645/// template name such as \c std::vector.
2646TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2647 bool TemplateKeyword,
2648 TemplateDecl *Template) {
2649 llvm::FoldingSetNodeID ID;
2650 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2651
2652 void *InsertPos = 0;
2653 QualifiedTemplateName *QTN =
2654 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2655 if (!QTN) {
2656 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2657 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2658 }
2659
2660 return TemplateName(QTN);
2661}
2662
2663/// \brief Retrieve the template name that represents a dependent
2664/// template name such as \c MetaFun::template apply.
2665TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2666 const IdentifierInfo *Name) {
2667 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2668
2669 llvm::FoldingSetNodeID ID;
2670 DependentTemplateName::Profile(ID, NNS, Name);
2671
2672 void *InsertPos = 0;
2673 DependentTemplateName *QTN =
2674 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2675
2676 if (QTN)
2677 return TemplateName(QTN);
2678
2679 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2680 if (CanonNNS == NNS) {
2681 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2682 } else {
2683 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2684 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2685 }
2686
2687 DependentTemplateNames.InsertNode(QTN, InsertPos);
2688 return TemplateName(QTN);
2689}
2690
Douglas Gregorc6507e42008-11-03 14:12:49 +00002691/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002692/// TargetInfo, produce the corresponding type. The unsigned @p Type
2693/// is actually a value of type @c TargetInfo::IntType.
2694QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002695 switch (Type) {
2696 case TargetInfo::NoInt: return QualType();
2697 case TargetInfo::SignedShort: return ShortTy;
2698 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2699 case TargetInfo::SignedInt: return IntTy;
2700 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2701 case TargetInfo::SignedLong: return LongTy;
2702 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2703 case TargetInfo::SignedLongLong: return LongLongTy;
2704 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2705 }
2706
2707 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002708 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002709}
Ted Kremenek118930e2008-07-24 23:58:27 +00002710
2711//===----------------------------------------------------------------------===//
2712// Type Predicates.
2713//===----------------------------------------------------------------------===//
2714
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002715/// isObjCNSObjectType - Return true if this is an NSObject object using
2716/// NSObject attribute on a c-style pointer type.
2717/// FIXME - Make it work directly on types.
2718///
2719bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2720 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2721 if (TypedefDecl *TD = TDT->getDecl())
2722 if (TD->getAttr<ObjCNSObjectAttr>())
2723 return true;
2724 }
2725 return false;
2726}
2727
Ted Kremenek118930e2008-07-24 23:58:27 +00002728/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2729/// to an object type. This includes "id" and "Class" (two 'special' pointers
2730/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2731/// ID type).
2732bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002733 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002734 return true;
2735
Steve Naroffd9e00802008-10-21 18:24:04 +00002736 // Blocks are objects.
2737 if (Ty->isBlockPointerType())
2738 return true;
2739
2740 // All other object types are pointers.
Chris Lattnera008d172009-04-12 23:51:02 +00002741 const PointerType *PT = Ty->getAsPointerType();
2742 if (PT == 0)
Ted Kremenek118930e2008-07-24 23:58:27 +00002743 return false;
2744
Chris Lattnera008d172009-04-12 23:51:02 +00002745 // If this a pointer to an interface (e.g. NSString*), it is ok.
2746 if (PT->getPointeeType()->isObjCInterfaceType() ||
2747 // If is has NSObject attribute, OK as well.
2748 isObjCNSObjectType(Ty))
2749 return true;
2750
Ted Kremenek118930e2008-07-24 23:58:27 +00002751 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2752 // pointer types. This looks for the typedef specifically, not for the
Chris Lattnera008d172009-04-12 23:51:02 +00002753 // underlying type. Iteratively strip off typedefs so that we can handle
2754 // typedefs of typedefs.
2755 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2756 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2757 Ty.getUnqualifiedType() == getObjCClassType())
2758 return true;
2759
2760 Ty = TDT->getDecl()->getUnderlyingType();
2761 }
Ted Kremenek118930e2008-07-24 23:58:27 +00002762
Chris Lattnera008d172009-04-12 23:51:02 +00002763 return false;
Ted Kremenek118930e2008-07-24 23:58:27 +00002764}
2765
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002766/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2767/// garbage collection attribute.
2768///
2769QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002770 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002771 if (getLangOptions().ObjC1 &&
2772 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002773 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002774 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002775 // (or pointers to them) be treated as though they were declared
2776 // as __strong.
2777 if (GCAttrs == QualType::GCNone) {
2778 if (isObjCObjectPointerType(Ty))
2779 GCAttrs = QualType::Strong;
2780 else if (Ty->isPointerType())
2781 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2782 }
Fariborz Jahaniand7b01972009-04-11 00:00:54 +00002783 // Non-pointers have none gc'able attribute regardless of the attribute
2784 // set on them.
2785 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2786 return QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002787 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002788 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002789}
2790
Chris Lattner6ff358b2008-04-07 06:51:04 +00002791//===----------------------------------------------------------------------===//
2792// Type Compatibility Testing
2793//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002794
Steve Naroff3454b6c2008-09-04 15:10:53 +00002795/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002796/// block types. Types must be strictly compatible here. For example,
2797/// C unfortunately doesn't produce an error for the following:
2798///
2799/// int (*emptyArgFunc)();
2800/// int (*intArgList)(int) = emptyArgFunc;
2801///
2802/// For blocks, we will produce an error for the following (similar to C++):
2803///
2804/// int (^emptyArgBlock)();
2805/// int (^intArgBlock)(int) = emptyArgBlock;
2806///
2807/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2808///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002809bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002810 const FunctionType *lbase = lhs->getAsFunctionType();
2811 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002812 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2813 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stump896ceed2009-04-01 01:17:39 +00002814 if (lproto && rproto == 0)
2815 return false;
2816 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff3454b6c2008-09-04 15:10:53 +00002817}
2818
Chris Lattner6ff358b2008-04-07 06:51:04 +00002819/// areCompatVectorTypes - Return true if the two specified vector types are
2820/// compatible.
2821static bool areCompatVectorTypes(const VectorType *LHS,
2822 const VectorType *RHS) {
2823 assert(LHS->isCanonical() && RHS->isCanonical());
2824 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002825 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002826}
2827
Eli Friedman0d9549b2008-08-22 00:56:42 +00002828/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002829/// compatible for assignment from RHS to LHS. This handles validation of any
2830/// protocol qualifiers on the LHS or RHS.
2831///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002832bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2833 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002834 // Verify that the base decls are compatible: the RHS must be a subclass of
2835 // the LHS.
2836 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2837 return false;
2838
2839 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2840 // protocol qualified at all, then we are good.
2841 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2842 return true;
2843
2844 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2845 // isn't a superset.
2846 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2847 return true; // FIXME: should return false!
2848
2849 // Finally, we must have two protocol-qualified interfaces.
2850 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2851 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002852
Steve Naroff98e71b82009-03-01 16:12:44 +00002853 // All LHS protocols must have a presence on the RHS.
2854 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002855
Steve Naroff98e71b82009-03-01 16:12:44 +00002856 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2857 LHSPE = LHSP->qual_end();
2858 LHSPI != LHSPE; LHSPI++) {
2859 bool RHSImplementsProtocol = false;
2860
2861 // If the RHS doesn't implement the protocol on the left, the types
2862 // are incompatible.
2863 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2864 RHSPE = RHSP->qual_end();
2865 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2866 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2867 RHSImplementsProtocol = true;
2868 }
2869 // FIXME: For better diagnostics, consider passing back the protocol name.
2870 if (!RHSImplementsProtocol)
2871 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002872 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002873 // The RHS implements all protocols listed on the LHS.
2874 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002875}
2876
Steve Naroff17c03822009-02-12 17:52:19 +00002877bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2878 // get the "pointed to" types
2879 const PointerType *LHSPT = LHS->getAsPointerType();
2880 const PointerType *RHSPT = RHS->getAsPointerType();
2881
2882 if (!LHSPT || !RHSPT)
2883 return false;
2884
2885 QualType lhptee = LHSPT->getPointeeType();
2886 QualType rhptee = RHSPT->getPointeeType();
2887 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2888 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2889 // ID acts sort of like void* for ObjC interfaces
2890 if (LHSIface && isObjCIdStructType(rhptee))
2891 return true;
2892 if (RHSIface && isObjCIdStructType(lhptee))
2893 return true;
2894 if (!LHSIface || !RHSIface)
2895 return false;
2896 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2897 canAssignObjCInterfaces(RHSIface, LHSIface);
2898}
2899
Steve Naroff85f0dc52007-10-15 20:41:53 +00002900/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2901/// both shall have the identically qualified version of a compatible type.
2902/// C99 6.2.7p1: Two types have compatible types if their types are the
2903/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002904bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2905 return !mergeTypes(LHS, RHS).isNull();
2906}
2907
2908QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2909 const FunctionType *lbase = lhs->getAsFunctionType();
2910 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002911 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2912 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002913 bool allLTypes = true;
2914 bool allRTypes = true;
2915
2916 // Check return type
2917 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2918 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002919 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2920 allLTypes = false;
2921 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2922 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002923
2924 if (lproto && rproto) { // two C99 style function prototypes
Sebastian Redl74942cd2009-04-30 19:20:55 +00002925 assert(!lproto->hasExceptionSpec() && !rproto->hasExceptionSpec() &&
2926 "C++ shouldn't be here");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002927 unsigned lproto_nargs = lproto->getNumArgs();
2928 unsigned rproto_nargs = rproto->getNumArgs();
2929
2930 // Compatible functions must have the same number of arguments
2931 if (lproto_nargs != rproto_nargs)
2932 return QualType();
2933
2934 // Variadic and non-variadic functions aren't compatible
2935 if (lproto->isVariadic() != rproto->isVariadic())
2936 return QualType();
2937
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002938 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2939 return QualType();
2940
Eli Friedman0d9549b2008-08-22 00:56:42 +00002941 // Check argument compatibility
2942 llvm::SmallVector<QualType, 10> types;
2943 for (unsigned i = 0; i < lproto_nargs; i++) {
2944 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2945 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2946 QualType argtype = mergeTypes(largtype, rargtype);
2947 if (argtype.isNull()) return QualType();
2948 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002949 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2950 allLTypes = false;
2951 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2952 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002953 }
2954 if (allLTypes) return lhs;
2955 if (allRTypes) return rhs;
2956 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002957 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002958 }
2959
2960 if (lproto) allRTypes = false;
2961 if (rproto) allLTypes = false;
2962
Douglas Gregor4fa58902009-02-26 23:50:07 +00002963 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002964 if (proto) {
Sebastian Redl74942cd2009-04-30 19:20:55 +00002965 assert(!proto->hasExceptionSpec() && "C++ shouldn't be here");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002966 if (proto->isVariadic()) return QualType();
2967 // Check that the types are compatible with the types that
2968 // would result from default argument promotions (C99 6.7.5.3p15).
2969 // The only types actually affected are promotable integer
2970 // types and floats, which would be passed as a different
2971 // type depending on whether the prototype is visible.
2972 unsigned proto_nargs = proto->getNumArgs();
2973 for (unsigned i = 0; i < proto_nargs; ++i) {
2974 QualType argTy = proto->getArgType(i);
2975 if (argTy->isPromotableIntegerType() ||
2976 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2977 return QualType();
2978 }
2979
2980 if (allLTypes) return lhs;
2981 if (allRTypes) return rhs;
2982 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002983 proto->getNumArgs(), lproto->isVariadic(),
2984 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002985 }
2986
2987 if (allLTypes) return lhs;
2988 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002989 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002990}
2991
2992QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002993 // C++ [expr]: If an expression initially has the type "reference to T", the
2994 // type is adjusted to "T" prior to any further analysis, the expression
2995 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002996 // expression is an lvalue unless the reference is an rvalue reference and
2997 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002998 // FIXME: C++ shouldn't be going through here! The rules are different
2999 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00003000 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
3001 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003002 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00003003 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00003004 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00003005 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00003006
Eli Friedman0d9549b2008-08-22 00:56:42 +00003007 QualType LHSCan = getCanonicalType(LHS),
3008 RHSCan = getCanonicalType(RHS);
3009
3010 // If two types are identical, they are compatible.
3011 if (LHSCan == RHSCan)
3012 return LHS;
3013
3014 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003015 // Note that we handle extended qualifiers later, in the
3016 // case for ExtQualType.
3017 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00003018 return QualType();
3019
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00003020 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
3021 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman0d9549b2008-08-22 00:56:42 +00003022
Chris Lattnerc38d4522008-01-14 05:45:46 +00003023 // We want to consider the two function types to be the same for these
3024 // comparisons, just force one to the other.
3025 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
3026 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00003027
3028 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00003029 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
3030 LHSClass = Type::ConstantArray;
3031 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
3032 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003033
Nate Begemanaf6ed502008-04-18 23:10:10 +00003034 // Canonicalize ExtVector -> Vector.
3035 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
3036 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00003037
Chris Lattner7cdcb252008-04-07 06:38:24 +00003038 // Consider qualified interfaces and interfaces the same.
3039 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
3040 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003041
Chris Lattnerb5709e22008-04-07 05:43:21 +00003042 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003043 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003044 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3045 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00003046
Steve Naroff0773c582009-04-14 15:11:46 +00003047 // 'id' and 'Class' act sort of like void* for ObjC interfaces
3048 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00003049 return LHS;
Steve Naroff0773c582009-04-14 15:11:46 +00003050 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00003051 return RHS;
3052
Steve Naroff28ceff72008-12-10 22:14:21 +00003053 // ID is compatible with all qualified id types.
3054 if (LHS->isObjCQualifiedIdType()) {
3055 if (const PointerType *PT = RHS->getAsPointerType()) {
3056 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00003057 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00003058 return LHS;
3059 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3060 // Unfortunately, this API is part of Sema (which we don't have access
3061 // to. Need to refactor. The following check is insufficient, since we
3062 // need to make sure the class implements the protocol.
3063 if (pType->isObjCInterfaceType())
3064 return LHS;
3065 }
3066 }
3067 if (RHS->isObjCQualifiedIdType()) {
3068 if (const PointerType *PT = LHS->getAsPointerType()) {
3069 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00003070 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00003071 return RHS;
3072 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3073 // Unfortunately, this API is part of Sema (which we don't have access
3074 // to. Need to refactor. The following check is insufficient, since we
3075 // need to make sure the class implements the protocol.
3076 if (pType->isObjCInterfaceType())
3077 return RHS;
3078 }
3079 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003080 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3081 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003082 if (const EnumType* ETy = LHS->getAsEnumType()) {
3083 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3084 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003085 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003086 if (const EnumType* ETy = RHS->getAsEnumType()) {
3087 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3088 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003089 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003090
Eli Friedman0d9549b2008-08-22 00:56:42 +00003091 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003092 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003093
Steve Naroffc88babe2008-01-09 22:43:08 +00003094 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003095 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00003096#define TYPE(Class, Base)
3097#define ABSTRACT_TYPE(Class, Base)
3098#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3099#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3100#include "clang/AST/TypeNodes.def"
3101 assert(false && "Non-canonical and dependent types shouldn't get here");
3102 return QualType();
3103
Sebastian Redlce6fff02009-03-16 23:22:08 +00003104 case Type::LValueReference:
3105 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003106 case Type::MemberPointer:
3107 assert(false && "C++ should never be in mergeTypes");
3108 return QualType();
3109
3110 case Type::IncompleteArray:
3111 case Type::VariableArray:
3112 case Type::FunctionProto:
3113 case Type::ExtVector:
3114 case Type::ObjCQualifiedInterface:
3115 assert(false && "Types are eliminated above");
3116 return QualType();
3117
Chris Lattnerc38d4522008-01-14 05:45:46 +00003118 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003119 {
3120 // Merge two pointer types, while trying to preserve typedef info
3121 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3122 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3123 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3124 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003125 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3126 return LHS;
3127 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3128 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003129 return getPointerType(ResultType);
3130 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003131 case Type::BlockPointer:
3132 {
3133 // Merge two block pointer types, while trying to preserve typedef info
3134 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3135 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3136 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3137 if (ResultType.isNull()) return QualType();
3138 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3139 return LHS;
3140 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3141 return RHS;
3142 return getBlockPointerType(ResultType);
3143 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003144 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003145 {
3146 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3147 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3148 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3149 return QualType();
3150
3151 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3152 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3153 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3154 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003155 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3156 return LHS;
3157 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3158 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003159 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3160 ArrayType::ArraySizeModifier(), 0);
3161 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3162 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003163 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3164 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003165 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3166 return LHS;
3167 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3168 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003169 if (LVAT) {
3170 // FIXME: This isn't correct! But tricky to implement because
3171 // the array's size has to be the size of LHS, but the type
3172 // has to be different.
3173 return LHS;
3174 }
3175 if (RVAT) {
3176 // FIXME: This isn't correct! But tricky to implement because
3177 // the array's size has to be the size of RHS, but the type
3178 // has to be different.
3179 return RHS;
3180 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003181 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3182 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003183 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003184 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003185 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003186 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003187 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003188 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003189 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003190 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3191 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003192 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003193 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003194 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003195 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003196 case Type::Complex:
3197 // Distinct complex types are incompatible.
3198 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003199 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003200 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003201 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3202 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003203 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003204 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003205 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003206 // FIXME: This should be type compatibility, e.g. whether
3207 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003208 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3209 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3210 if (LHSIface && RHSIface &&
3211 canAssignObjCInterfaces(LHSIface, RHSIface))
3212 return LHS;
3213
Eli Friedman0d9549b2008-08-22 00:56:42 +00003214 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003215 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003216 case Type::ObjCQualifiedId:
3217 // Distinct qualified id's are not compatible.
3218 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003219 case Type::FixedWidthInt:
3220 // Distinct fixed-width integers are not compatible.
3221 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003222 case Type::ExtQual:
3223 // FIXME: ExtQual types can be compatible even if they're not
3224 // identical!
3225 return QualType();
3226 // First attempt at an implementation, but I'm not really sure it's
3227 // right...
3228#if 0
3229 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3230 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3231 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3232 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3233 return QualType();
3234 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3235 LHSBase = QualType(LQual->getBaseType(), 0);
3236 RHSBase = QualType(RQual->getBaseType(), 0);
3237 ResultType = mergeTypes(LHSBase, RHSBase);
3238 if (ResultType.isNull()) return QualType();
3239 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3240 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3241 return LHS;
3242 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3243 return RHS;
3244 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3245 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3246 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3247 return ResultType;
3248#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003249
3250 case Type::TemplateSpecialization:
3251 assert(false && "Dependent types have no size");
3252 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003253 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003254
3255 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003256}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003257
Chris Lattner1d78a862008-04-07 07:01:58 +00003258//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003259// Integer Predicates
3260//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003261
Eli Friedman0832dbc2008-06-28 06:23:08 +00003262unsigned ASTContext::getIntWidth(QualType T) {
3263 if (T == BoolTy)
3264 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003265 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3266 return FWIT->getWidth();
3267 }
3268 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003269 return (unsigned)getTypeSize(T);
3270}
3271
3272QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3273 assert(T->isSignedIntegerType() && "Unexpected type");
3274 if (const EnumType* ETy = T->getAsEnumType())
3275 T = ETy->getDecl()->getIntegerType();
3276 const BuiltinType* BTy = T->getAsBuiltinType();
3277 assert (BTy && "Unexpected signed integer type");
3278 switch (BTy->getKind()) {
3279 case BuiltinType::Char_S:
3280 case BuiltinType::SChar:
3281 return UnsignedCharTy;
3282 case BuiltinType::Short:
3283 return UnsignedShortTy;
3284 case BuiltinType::Int:
3285 return UnsignedIntTy;
3286 case BuiltinType::Long:
3287 return UnsignedLongTy;
3288 case BuiltinType::LongLong:
3289 return UnsignedLongLongTy;
Chris Lattner6cc7e412009-04-30 02:43:43 +00003290 case BuiltinType::Int128:
3291 return UnsignedInt128Ty;
Eli Friedman0832dbc2008-06-28 06:23:08 +00003292 default:
3293 assert(0 && "Unexpected signed integer type");
3294 return QualType();
3295 }
3296}
3297
Douglas Gregorc34897d2009-04-09 22:27:44 +00003298ExternalASTSource::~ExternalASTSource() { }
3299
3300void ExternalASTSource::PrintStats() { }