blob: d91a8cefd14ae374c8d82350c57acf619c76979c [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"
Ted Kremenek738e6c02007-10-31 17:10:13 +000024#include "llvm/Bitcode/Serialize.h"
25#include "llvm/Bitcode/Deserialize.h"
Nate Begeman7903d052009-01-18 06:42:49 +000026#include "llvm/Support/MathExtras.h"
Chris Lattnerf4fbc442009-03-28 04:27:18 +000027#include "llvm/Support/MemoryBuffer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000028using namespace clang;
29
30enum FloatingRank {
31 FloatRank, DoubleRank, LongDoubleRank
32};
33
Chris Lattner2fda0ed2008-10-05 17:34:18 +000034ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
35 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000036 IdentifierTable &idents, SelectorTable &sels,
Steve Naroff207b9ec2009-01-27 23:20:32 +000037 bool FreeMem, unsigned size_reserve) :
Douglas Gregor1e589cc2009-03-26 23:50:42 +000038 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
39 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregorc34897d2009-04-09 22:27:44 +000040 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
41 ExternalSource(0) {
Daniel Dunbarde300732008-08-11 04:54:23 +000042 if (size_reserve > 0) Types.reserve(size_reserve);
43 InitBuiltinTypes();
Douglas Gregor37f477e2009-04-22 04:56:28 +000044 BuiltinInfo.InitializeTargetBuiltins(Target);
45 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
Daniel Dunbarde300732008-08-11 04:54:23 +000046 TUDecl = TranslationUnitDecl::Create(*this);
47}
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 {
66 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
67 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
68 while (I != E) {
69 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
70 delete R;
71 }
72 }
73
74 {
Chris Lattner608c1e32009-03-31 09:24:30 +000075 llvm::DenseMap<const ObjCInterfaceDecl*, RecordDecl*>::iterator
Nuno Lopes355a8682008-12-17 22:30:25 +000076 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
77 while (I != E) {
Chris Lattner608c1e32009-03-31 09:24:30 +000078 RecordDecl *R = (I++)->second;
Nuno Lopes355a8682008-12-17 22:30:25 +000079 R->Destroy(*this);
80 }
81 }
82
Douglas Gregor1e589cc2009-03-26 23:50:42 +000083 // Destroy nested-name-specifiers.
Douglas Gregor3c4eae52009-03-27 23:54:10 +000084 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
85 NNS = NestedNameSpecifiers.begin(),
86 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregorbccd97c2009-03-27 23:25:45 +000087 NNS != NNSEnd;
Douglas Gregor3c4eae52009-03-27 23:54:10 +000088 /* Increment in loop */)
89 (*NNS++).Destroy(*this);
Douglas Gregor1e589cc2009-03-26 23:50:42 +000090
91 if (GlobalNestedNameSpecifier)
92 GlobalNestedNameSpecifier->Destroy(*this);
93
Eli Friedman65489b72008-05-27 03:08:09 +000094 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000095}
96
Douglas Gregorc34897d2009-04-09 22:27:44 +000097void
98ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
99 ExternalSource.reset(Source.take());
100}
101
Chris Lattner4b009652007-07-25 00:24:17 +0000102void ASTContext::PrintStats() const {
103 fprintf(stderr, "*** AST Context Stats:\n");
104 fprintf(stderr, " %d types total.\n", (int)Types.size());
105 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +0000106 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000107 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
108 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
109
Chris Lattner4b009652007-07-25 00:24:17 +0000110 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000111 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
112 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000113 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000114 unsigned NumExtQual = 0;
115
Chris Lattner4b009652007-07-25 00:24:17 +0000116 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
117 Type *T = Types[i];
118 if (isa<BuiltinType>(T))
119 ++NumBuiltin;
120 else if (isa<PointerType>(T))
121 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +0000122 else if (isa<BlockPointerType>(T))
123 ++NumBlockPointer;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000124 else if (isa<LValueReferenceType>(T))
125 ++NumLValueReference;
126 else if (isa<RValueReferenceType>(T))
127 ++NumRValueReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000128 else if (isa<MemberPointerType>(T))
129 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000130 else if (isa<ComplexType>(T))
131 ++NumComplex;
132 else if (isa<ArrayType>(T))
133 ++NumArray;
134 else if (isa<VectorType>(T))
135 ++NumVector;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000136 else if (isa<FunctionNoProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000137 ++NumFunctionNP;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000138 else if (isa<FunctionProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000139 ++NumFunctionP;
140 else if (isa<TypedefType>(T))
141 ++NumTypeName;
142 else if (TagType *TT = dyn_cast<TagType>(T)) {
143 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000144 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000145 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000146 case TagDecl::TK_struct: ++NumTagStruct; break;
147 case TagDecl::TK_union: ++NumTagUnion; break;
148 case TagDecl::TK_class: ++NumTagClass; break;
149 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000150 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000151 } else if (isa<ObjCInterfaceType>(T))
152 ++NumObjCInterfaces;
153 else if (isa<ObjCQualifiedInterfaceType>(T))
154 ++NumObjCQualifiedInterfaces;
155 else if (isa<ObjCQualifiedIdType>(T))
156 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000157 else if (isa<TypeOfType>(T))
158 ++NumTypeOfTypes;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000159 else if (isa<TypeOfExprType>(T))
160 ++NumTypeOfExprTypes;
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000161 else if (isa<ExtQualType>(T))
162 ++NumExtQual;
Steve Naroff948fd372007-09-17 14:16:13 +0000163 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000164 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000165 assert(0 && "Unknown type!");
166 }
167 }
168
169 fprintf(stderr, " %d builtin types\n", NumBuiltin);
170 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000171 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000172 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
173 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000174 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000175 fprintf(stderr, " %d complex types\n", NumComplex);
176 fprintf(stderr, " %d array types\n", NumArray);
177 fprintf(stderr, " %d vector types\n", NumVector);
178 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
179 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
180 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
181 fprintf(stderr, " %d tagged types\n", NumTagged);
182 fprintf(stderr, " %d struct types\n", NumTagStruct);
183 fprintf(stderr, " %d union types\n", NumTagUnion);
184 fprintf(stderr, " %d class types\n", NumTagClass);
185 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000186 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000187 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000188 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000189 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000190 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000191 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000192 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000193 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000194
Chris Lattner4b009652007-07-25 00:24:17 +0000195 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
196 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
197 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlce6fff02009-03-16 23:22:08 +0000198 NumLValueReference*sizeof(LValueReferenceType)+
199 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000200 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor4fa58902009-02-26 23:50:07 +0000201 NumFunctionP*sizeof(FunctionProtoType)+
202 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroffe0430632008-05-21 15:59:22 +0000203 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000204 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
205 NumExtQual*sizeof(ExtQualType)));
Douglas Gregorc34897d2009-04-09 22:27:44 +0000206
207 if (ExternalSource.get()) {
208 fprintf(stderr, "\n");
209 ExternalSource->PrintStats();
210 }
Chris Lattner4b009652007-07-25 00:24:17 +0000211}
212
213
214void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000215 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000216}
217
Chris Lattner4b009652007-07-25 00:24:17 +0000218void ASTContext::InitBuiltinTypes() {
219 assert(VoidTy.isNull() && "Context reinitialized?");
220
221 // C99 6.2.5p19.
222 InitBuiltinType(VoidTy, BuiltinType::Void);
223
224 // C99 6.2.5p2.
225 InitBuiltinType(BoolTy, BuiltinType::Bool);
226 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000227 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000228 InitBuiltinType(CharTy, BuiltinType::Char_S);
229 else
230 InitBuiltinType(CharTy, BuiltinType::Char_U);
231 // C99 6.2.5p4.
232 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
233 InitBuiltinType(ShortTy, BuiltinType::Short);
234 InitBuiltinType(IntTy, BuiltinType::Int);
235 InitBuiltinType(LongTy, BuiltinType::Long);
236 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
237
238 // C99 6.2.5p6.
239 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
240 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
241 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
242 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
243 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
244
245 // C99 6.2.5p10.
246 InitBuiltinType(FloatTy, BuiltinType::Float);
247 InitBuiltinType(DoubleTy, BuiltinType::Double);
248 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000249
Chris Lattnere1dafe72009-02-26 23:43:47 +0000250 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
251 InitBuiltinType(WCharTy, BuiltinType::WChar);
252 else // C99
253 WCharTy = getFromTargetType(Target.getWCharType());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000254
Douglas Gregord2baafd2008-10-21 16:13:35 +0000255 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000256 InitBuiltinType(OverloadTy, BuiltinType::Overload);
257
258 // Placeholder type for type-dependent expressions whose type is
259 // completely unknown. No code should ever check a type against
260 // DependentTy and users should never see it; however, it is here to
261 // help diagnose failures to properly check for type-dependent
262 // expressions.
263 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000264
Chris Lattner4b009652007-07-25 00:24:17 +0000265 // C99 6.2.5p11.
266 FloatComplexTy = getComplexType(FloatTy);
267 DoubleComplexTy = getComplexType(DoubleTy);
268 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000269
Steve Naroff9d12c902007-10-15 14:41:52 +0000270 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000271 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000272 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000273 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000274 ClassStructType = 0;
275
Ted Kremenek42730c52008-01-07 19:49:32 +0000276 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000277
278 // void * type
279 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000280}
281
282//===----------------------------------------------------------------------===//
283// Type Sizing and Analysis
284//===----------------------------------------------------------------------===//
285
Chris Lattner2a674dc2008-06-30 18:32:54 +0000286/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
287/// scalar floating point type.
288const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
289 const BuiltinType *BT = T->getAsBuiltinType();
290 assert(BT && "Not a floating point type!");
291 switch (BT->getKind()) {
292 default: assert(0 && "Not a floating point type!");
293 case BuiltinType::Float: return Target.getFloatFormat();
294 case BuiltinType::Double: return Target.getDoubleFormat();
295 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
296 }
297}
298
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000299/// getDeclAlign - Return a conservative estimate of the alignment of the
300/// specified decl. Note that bitfields do not have a valid alignment, so
301/// this method will assert on them.
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +0000302unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman0ee57322009-02-22 02:56:25 +0000303 unsigned Align = Target.getCharWidth();
304
305 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
306 Align = std::max(Align, AA->getAlignment());
307
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000308 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
309 QualType T = VD->getType();
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000310 if (const ReferenceType* RT = T->getAsReferenceType()) {
311 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssoneeaeda32009-04-10 04:52:36 +0000312 Align = Target.getPointerAlign(AS);
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000313 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
314 // Incomplete or function types default to 1.
Eli Friedman0ee57322009-02-22 02:56:25 +0000315 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
316 T = cast<ArrayType>(T)->getElementType();
317
318 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
319 }
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000320 }
Eli Friedman0ee57322009-02-22 02:56:25 +0000321
322 return Align / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000323}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000324
Chris Lattner4b009652007-07-25 00:24:17 +0000325/// getTypeSize - Return the size of the specified type, in bits. This method
326/// does not work on incomplete types.
327std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000328ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000329 T = getCanonicalType(T);
Mike Stump44d1f402009-02-27 18:32:39 +0000330 uint64_t Width=0;
331 unsigned Align=8;
Chris Lattner4b009652007-07-25 00:24:17 +0000332 switch (T->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000333#define TYPE(Class, Base)
334#define ABSTRACT_TYPE(Class, Base)
335#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
336#define DEPENDENT_TYPE(Class, Base) case Type::Class:
337#include "clang/AST/TypeNodes.def"
338 assert(false && "Should not see non-canonical or dependent types");
339 break;
340
Chris Lattner4b009652007-07-25 00:24:17 +0000341 case Type::FunctionNoProto:
342 case Type::FunctionProto:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000343 case Type::IncompleteArray:
Chris Lattner4b009652007-07-25 00:24:17 +0000344 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000345 case Type::VariableArray:
346 assert(0 && "VLAs not implemented yet!");
347 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000348 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000349
Chris Lattner8cd0e932008-03-05 18:54:05 +0000350 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000351 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000352 Align = EltInfo.second;
353 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000354 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000355 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000356 case Type::Vector: {
357 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000358 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000359 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000360 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000361 // If the alignment is not a power of 2, round up to the next power of 2.
362 // This happens for non-power-of-2 length vectors.
363 // FIXME: this should probably be a target property.
364 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000365 break;
366 }
367
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000368 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000369 switch (cast<BuiltinType>(T)->getKind()) {
370 default: assert(0 && "Unknown builtin type!");
371 case BuiltinType::Void:
372 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000373 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000374 Width = Target.getBoolWidth();
375 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000376 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000377 case BuiltinType::Char_S:
378 case BuiltinType::Char_U:
379 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000380 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000381 Width = Target.getCharWidth();
382 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000383 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000384 case BuiltinType::WChar:
385 Width = Target.getWCharWidth();
386 Align = Target.getWCharAlign();
387 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000388 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000389 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000390 Width = Target.getShortWidth();
391 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000392 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000393 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000394 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000395 Width = Target.getIntWidth();
396 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000397 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000398 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000399 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000400 Width = Target.getLongWidth();
401 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000402 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000403 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000404 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000405 Width = Target.getLongLongWidth();
406 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000407 break;
408 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000409 Width = Target.getFloatWidth();
410 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000411 break;
412 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000413 Width = Target.getDoubleWidth();
414 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000415 break;
416 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000417 Width = Target.getLongDoubleWidth();
418 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000419 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000420 }
421 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000422 case Type::FixedWidthInt:
423 // FIXME: This isn't precisely correct; the width/alignment should depend
424 // on the available types for the target
425 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattnere9174982009-02-15 21:20:13 +0000426 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000427 Align = Width;
428 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000429 case Type::ExtQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000430 // FIXME: Pointers into different addr spaces could have different sizes and
431 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000432 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000433 case Type::ObjCQualifiedId:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000434 case Type::ObjCQualifiedInterface:
Chris Lattner1d78a862008-04-07 07:01:58 +0000435 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000436 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000437 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000438 case Type::BlockPointer: {
439 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
440 Width = Target.getPointerWidth(AS);
441 Align = Target.getPointerAlign(AS);
442 break;
443 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000444 case Type::Pointer: {
445 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000446 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000447 Align = Target.getPointerAlign(AS);
448 break;
449 }
Sebastian Redlce6fff02009-03-16 23:22:08 +0000450 case Type::LValueReference:
451 case Type::RValueReference:
Chris Lattner4b009652007-07-25 00:24:17 +0000452 // "When applied to a reference or a reference type, the result is the size
453 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000454 // FIXME: This is wrong for struct layout: a reference in a struct has
455 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000456 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000457 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000458 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-01-24 21:16:55 +0000459 // the GCC ABI, where pointers to data are one pointer large, pointers to
460 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl18cffee2009-01-24 23:29:36 +0000461 // other compilers too, we need to delegate this completely to TargetInfo
462 // or some ABI abstraction layer.
Sebastian Redl75555032009-01-24 21:16:55 +0000463 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
464 unsigned AS = Pointee.getAddressSpace();
465 Width = Target.getPointerWidth(AS);
466 if (Pointee->isFunctionType())
467 Width *= 2;
468 Align = Target.getPointerAlign(AS);
469 // GCC aligns at single pointer width.
470 }
Chris Lattner4b009652007-07-25 00:24:17 +0000471 case Type::Complex: {
472 // Complex types have the same alignment as their elements, but twice the
473 // size.
474 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000475 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000476 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000477 Align = EltInfo.second;
478 break;
479 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000480 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000481 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000482 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
483 Width = Layout.getSize();
484 Align = Layout.getAlignment();
485 break;
486 }
Douglas Gregor4fa58902009-02-26 23:50:07 +0000487 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000488 case Type::Enum: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000489 const TagType *TT = cast<TagType>(T);
490
491 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000492 Width = 1;
493 Align = 1;
494 break;
495 }
496
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000497 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000498 return getTypeInfo(ET->getDecl()->getIntegerType());
499
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000500 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000501 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
502 Width = Layout.getSize();
503 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000504 break;
505 }
Douglas Gregordd13e842009-03-30 22:58:21 +0000506
507 case Type::TemplateSpecialization:
508 assert(false && "Dependent types have no size");
509 break;
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000510 }
Chris Lattner4b009652007-07-25 00:24:17 +0000511
512 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000513 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000514}
515
Chris Lattner83165b52009-01-27 18:08:34 +0000516/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
517/// type for the current target in bits. This can be different than the ABI
518/// alignment in cases where it is beneficial for performance to overalign
519/// a data type.
520unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
521 unsigned ABIAlign = getTypeAlign(T);
522
523 // Doubles should be naturally aligned if possible.
Daniel Dunbarc61a8002009-02-18 19:59:32 +0000524 if (T->isSpecificBuiltinType(BuiltinType::Double))
525 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000526
527 return ABIAlign;
528}
529
530
Devang Patelbfe323c2008-06-04 21:22:16 +0000531/// LayoutField - Field layout.
532void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000533 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000534 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000535 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000536 uint64_t FieldOffset = IsUnion ? 0 : Size;
537 uint64_t FieldSize;
538 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000539
540 // FIXME: Should this override struct packing? Probably we want to
541 // take the minimum?
542 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
543 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000544
545 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
546 // TODO: Need to check this algorithm on other targets!
547 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000548 FieldSize =
549 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000550
551 std::pair<uint64_t, unsigned> FieldInfo =
552 Context.getTypeInfo(FD->getType());
553 uint64_t TypeSize = FieldInfo.first;
554
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000555 // Determine the alignment of this bitfield. The packing
556 // attributes define a maximum and the alignment attribute defines
557 // a minimum.
558 // FIXME: What is the right behavior when the specified alignment
559 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000560 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000561 if (FieldPacking)
562 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000563 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
564 FieldAlign = std::max(FieldAlign, AA->getAlignment());
565
566 // Check if we need to add padding to give the field the correct
567 // alignment.
568 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
569 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
570
571 // Padding members don't affect overall alignment
572 if (!FD->getIdentifier())
573 FieldAlign = 1;
574 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000575 if (FD->getType()->isIncompleteArrayType()) {
576 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000577 // query getTypeInfo about these, so we figure it out here.
578 // Flexible array members don't have any size, but they
579 // have to be aligned appropriately for their element type.
580 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000581 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000582 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson0843ea52009-04-10 05:31:15 +0000583 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
584 unsigned AS = RT->getPointeeType().getAddressSpace();
585 FieldSize = Context.Target.getPointerWidth(AS);
586 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patelbfe323c2008-06-04 21:22:16 +0000587 } else {
588 std::pair<uint64_t, unsigned> FieldInfo =
589 Context.getTypeInfo(FD->getType());
590 FieldSize = FieldInfo.first;
591 FieldAlign = FieldInfo.second;
592 }
593
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000594 // Determine the alignment of this bitfield. The packing
595 // attributes define a maximum and the alignment attribute defines
596 // a minimum. Additionally, the packing alignment must be at least
597 // a byte for non-bitfields.
598 //
599 // FIXME: What is the right behavior when the specified alignment
600 // is smaller than the specified packing?
601 if (FieldPacking)
602 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000603 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
604 FieldAlign = std::max(FieldAlign, AA->getAlignment());
605
606 // Round up the current record size to the field's alignment boundary.
607 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
608 }
609
610 // Place this field at the current location.
611 FieldOffsets[FieldNo] = FieldOffset;
612
613 // Reserve space for this field.
614 if (IsUnion) {
615 Size = std::max(Size, FieldSize);
616 } else {
617 Size = FieldOffset + FieldSize;
618 }
619
620 // Remember max struct/class alignment.
621 Alignment = std::max(Alignment, FieldAlign);
622}
623
Daniel Dunbare0edb2e2009-04-22 04:39:47 +0000624void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
625 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
626 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
627 if (SuperClass)
628 CollectObjCIvars(SuperClass, Fields);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000629 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
630 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000631 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000632 if (!IVDecl->isInvalidDecl())
633 Fields.push_back(cast<FieldDecl>(IVDecl));
634 }
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000635 // look into properties.
Daniel Dunbare0edb2e2009-04-22 04:39:47 +0000636 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
637 E = OI->prop_end(*this); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000638 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000639 Fields.push_back(cast<FieldDecl>(IV));
640 }
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000641}
642
643/// addRecordToClass - produces record info. for the class for its
644/// ivars and all those inherited.
645///
Chris Lattner9329cf52009-03-31 08:48:01 +0000646const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) {
Daniel Dunbaracdae262009-04-22 10:56:29 +0000647 assert(!D->isForwardDecl() && "Invalid decl!");
648
Daniel Dunbardbc8af72009-04-21 21:41:56 +0000649 // FIXME: The only client relying on this working in the presence of
Daniel Dunbare0edb2e2009-04-22 04:39:47 +0000650 // forward declarations is IRgen, which should not need it. Fix
651 // and simplify this code.
Chris Lattner608c1e32009-03-31 09:24:30 +0000652 RecordDecl *&RD = ASTRecordForInterface[D];
Daniel Dunbaracdae262009-04-22 10:56:29 +0000653 if (RD)
654 return RD;
Chris Lattner9329cf52009-03-31 08:48:01 +0000655
656 llvm::SmallVector<FieldDecl*, 32> RecFields;
Daniel Dunbare0edb2e2009-04-22 04:39:47 +0000657 CollectObjCIvars(D, RecFields);
Chris Lattner608c1e32009-03-31 09:24:30 +0000658
Daniel Dunbaracdae262009-04-22 10:56:29 +0000659 RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(),
660 D->getIdentifier());
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000661 /// FIXME! Can do collection of ivars and adding to the record while
662 /// doing it.
Chris Lattner41b780c2009-03-31 08:58:42 +0000663 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000664 RD->addDecl(*this,
665 FieldDecl::Create(*this, RD,
Chris Lattner608c1e32009-03-31 09:24:30 +0000666 RecFields[i]->getLocation(),
667 RecFields[i]->getIdentifier(),
668 RecFields[i]->getType(),
669 RecFields[i]->getBitWidth(), false));
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000670 }
Chris Lattner9329cf52009-03-31 08:48:01 +0000671
Chris Lattner608c1e32009-03-31 09:24:30 +0000672 RD->completeDefinition(*this);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000673 return RD;
674}
Devang Patel4b6bf702008-06-04 21:54:36 +0000675
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000676/// getASTObjcInterfaceLayout - Get or compute information about the layout of
677/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000678/// position information.
679const ASTRecordLayout &
680ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
681 // Look up this layout, if already laid out, return what we have.
682 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
683 if (Entry) return *Entry;
684
685 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
686 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000687 ASTRecordLayout *NewEntry = NULL;
Fariborz Jahanianba50c332009-04-08 21:54:52 +0000688 // FIXME. Add actual count of synthesized ivars, instead of count
689 // of properties which is the upper bound, but is safe.
Daniel Dunbar998510f2009-04-08 20:18:15 +0000690 unsigned FieldCount =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000691 D->ivar_size() + std::distance(D->prop_begin(*this), D->prop_end(*this));
Devang Patel8682d882008-06-06 02:14:01 +0000692 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
693 FieldCount++;
694 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
695 unsigned Alignment = SL.getAlignment();
696 uint64_t Size = SL.getSize();
697 NewEntry = new ASTRecordLayout(Size, Alignment);
698 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000699 // Super class is at the beginning of the layout.
700 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000701 } else {
702 NewEntry = new ASTRecordLayout();
703 NewEntry->InitializeLayout(FieldCount);
704 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000705 Entry = NewEntry;
706
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000707 unsigned StructPacking = 0;
708 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
709 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000710
711 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
712 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
713 AA->getAlignment()));
714
715 // Layout each ivar sequentially.
716 unsigned i = 0;
717 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
718 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
719 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000720 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000721 }
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000722 // Also synthesized ivars
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000723 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
724 E = D->prop_end(*this); I != E; ++I) {
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000725 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
726 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
727 }
Fariborz Jahanian84c45692009-04-01 19:37:34 +0000728
Devang Patel4b6bf702008-06-04 21:54:36 +0000729 // Finally, round the size of the total struct up to the alignment of the
730 // struct itself.
731 NewEntry->FinalizeLayout();
732 return *NewEntry;
733}
734
Devang Patel7a78e432007-11-01 19:11:01 +0000735/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000736/// specified record (struct/union/class), which indicates its size and field
737/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000738const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000739 D = D->getDefinition(*this);
740 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000741
Chris Lattner4b009652007-07-25 00:24:17 +0000742 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000743 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000744 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000745
Devang Patel7a78e432007-11-01 19:11:01 +0000746 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
747 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
748 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000749 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000750
Douglas Gregor39677622008-12-11 20:41:00 +0000751 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000752 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
753 D->field_end(*this)));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000754 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000755
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000756 unsigned StructPacking = 0;
757 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
758 StructPacking = PA->getAlignment();
759
Eli Friedman5949a022008-05-30 09:31:38 +0000760 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000761 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
762 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000763
Eli Friedman5949a022008-05-30 09:31:38 +0000764 // Layout each field, for now, just sequentially, respecting alignment. In
765 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000766 unsigned FieldIdx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000767 for (RecordDecl::field_iterator Field = D->field_begin(*this),
768 FieldEnd = D->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000769 Field != FieldEnd; (void)++Field, ++FieldIdx)
770 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000771
772 // Finally, round the size of the total struct up to the alignment of the
773 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000774 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000775 return *NewEntry;
776}
777
Chris Lattner4b009652007-07-25 00:24:17 +0000778//===----------------------------------------------------------------------===//
779// Type creation/memoization methods
780//===----------------------------------------------------------------------===//
781
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000782QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000783 QualType CanT = getCanonicalType(T);
784 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000785 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000786
787 // If we are composing extended qualifiers together, merge together into one
788 // ExtQualType node.
789 unsigned CVRQuals = T.getCVRQualifiers();
790 QualType::GCAttrTypes GCAttr = QualType::GCNone;
791 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000792
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000793 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
794 // If this type already has an address space specified, it cannot get
795 // another one.
796 assert(EQT->getAddressSpace() == 0 &&
797 "Type cannot be in multiple addr spaces!");
798 GCAttr = EQT->getObjCGCAttr();
799 TypeNode = EQT->getBaseType();
800 }
Chris Lattner35fef522008-02-20 20:55:12 +0000801
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000802 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000803 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000804 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000805 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000806 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000807 return QualType(EXTQy, CVRQuals);
808
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000809 // If the base type isn't canonical, this won't be a canonical type either,
810 // so fill in the canonical type field.
811 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000812 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000813 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000814
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000815 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000816 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000817 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000818 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000819 ExtQualType *New =
820 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000821 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000822 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000823 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000824}
825
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000826QualType ASTContext::getObjCGCQualType(QualType T,
827 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000828 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000829 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000830 return T;
831
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000832 // If we are composing extended qualifiers together, merge together into one
833 // ExtQualType node.
834 unsigned CVRQuals = T.getCVRQualifiers();
835 Type *TypeNode = T.getTypePtr();
836 unsigned AddressSpace = 0;
837
838 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
839 // If this type already has an address space specified, it cannot get
840 // another one.
841 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
842 "Type cannot be in multiple addr spaces!");
843 AddressSpace = EQT->getAddressSpace();
844 TypeNode = EQT->getBaseType();
845 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000846
847 // Check if we've already instantiated an gc qual'd type of this type.
848 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000849 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000850 void *InsertPos = 0;
851 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000852 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000853
854 // If the base type isn't canonical, this won't be a canonical type either,
855 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +0000856 // FIXME: Isn't this also not canonical if the base type is a array
857 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000858 QualType Canonical;
859 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000860 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000861
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000862 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000863 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
864 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
865 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000866 ExtQualType *New =
867 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000868 ExtQualTypes.InsertNode(New, InsertPos);
869 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000870 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000871}
Chris Lattner4b009652007-07-25 00:24:17 +0000872
873/// getComplexType - Return the uniqued reference to the type for a complex
874/// number with the specified element type.
875QualType ASTContext::getComplexType(QualType T) {
876 // Unique pointers, to guarantee there is only one pointer of a particular
877 // structure.
878 llvm::FoldingSetNodeID ID;
879 ComplexType::Profile(ID, T);
880
881 void *InsertPos = 0;
882 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
883 return QualType(CT, 0);
884
885 // If the pointee type isn't canonical, this won't be a canonical type either,
886 // so fill in the canonical type field.
887 QualType Canonical;
888 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000889 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000890
891 // Get the new insert position for the node we care about.
892 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000893 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000894 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000895 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000896 Types.push_back(New);
897 ComplexTypes.InsertNode(New, InsertPos);
898 return QualType(New, 0);
899}
900
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000901QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
902 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
903 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
904 FixedWidthIntType *&Entry = Map[Width];
905 if (!Entry)
906 Entry = new FixedWidthIntType(Width, Signed);
907 return QualType(Entry, 0);
908}
Chris Lattner4b009652007-07-25 00:24:17 +0000909
910/// getPointerType - Return the uniqued reference to the type for a pointer to
911/// the specified type.
912QualType ASTContext::getPointerType(QualType T) {
913 // Unique pointers, to guarantee there is only one pointer of a particular
914 // structure.
915 llvm::FoldingSetNodeID ID;
916 PointerType::Profile(ID, T);
917
918 void *InsertPos = 0;
919 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
920 return QualType(PT, 0);
921
922 // If the pointee type isn't canonical, this won't be a canonical type either,
923 // so fill in the canonical type field.
924 QualType Canonical;
925 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000926 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000927
928 // Get the new insert position for the node we care about.
929 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000930 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000931 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000932 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000933 Types.push_back(New);
934 PointerTypes.InsertNode(New, InsertPos);
935 return QualType(New, 0);
936}
937
Steve Naroff7aa54752008-08-27 16:04:49 +0000938/// getBlockPointerType - Return the uniqued reference to the type for
939/// a pointer to the specified block.
940QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000941 assert(T->isFunctionType() && "block of function types only");
942 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000943 // structure.
944 llvm::FoldingSetNodeID ID;
945 BlockPointerType::Profile(ID, T);
946
947 void *InsertPos = 0;
948 if (BlockPointerType *PT =
949 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
950 return QualType(PT, 0);
951
Steve Narofffd5b19d2008-08-28 19:20:44 +0000952 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000953 // type either so fill in the canonical type field.
954 QualType Canonical;
955 if (!T->isCanonical()) {
956 Canonical = getBlockPointerType(getCanonicalType(T));
957
958 // Get the new insert position for the node we care about.
959 BlockPointerType *NewIP =
960 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000961 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000962 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000963 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000964 Types.push_back(New);
965 BlockPointerTypes.InsertNode(New, InsertPos);
966 return QualType(New, 0);
967}
968
Sebastian Redlce6fff02009-03-16 23:22:08 +0000969/// getLValueReferenceType - Return the uniqued reference to the type for an
970/// lvalue reference to the specified type.
971QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +0000972 // Unique pointers, to guarantee there is only one pointer of a particular
973 // structure.
974 llvm::FoldingSetNodeID ID;
975 ReferenceType::Profile(ID, T);
976
977 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000978 if (LValueReferenceType *RT =
979 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000980 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000981
Chris Lattner4b009652007-07-25 00:24:17 +0000982 // If the referencee type isn't canonical, this won't be a canonical type
983 // either, so fill in the canonical type field.
984 QualType Canonical;
985 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000986 Canonical = getLValueReferenceType(getCanonicalType(T));
987
Chris Lattner4b009652007-07-25 00:24:17 +0000988 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000989 LValueReferenceType *NewIP =
990 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000991 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000992 }
993
Sebastian Redlce6fff02009-03-16 23:22:08 +0000994 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000995 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000996 LValueReferenceTypes.InsertNode(New, InsertPos);
997 return QualType(New, 0);
998}
999
1000/// getRValueReferenceType - Return the uniqued reference to the type for an
1001/// rvalue reference to the specified type.
1002QualType ASTContext::getRValueReferenceType(QualType T) {
1003 // Unique pointers, to guarantee there is only one pointer of a particular
1004 // structure.
1005 llvm::FoldingSetNodeID ID;
1006 ReferenceType::Profile(ID, T);
1007
1008 void *InsertPos = 0;
1009 if (RValueReferenceType *RT =
1010 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1011 return QualType(RT, 0);
1012
1013 // If the referencee type isn't canonical, this won't be a canonical type
1014 // either, so fill in the canonical type field.
1015 QualType Canonical;
1016 if (!T->isCanonical()) {
1017 Canonical = getRValueReferenceType(getCanonicalType(T));
1018
1019 // Get the new insert position for the node we care about.
1020 RValueReferenceType *NewIP =
1021 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1022 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1023 }
1024
1025 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1026 Types.push_back(New);
1027 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001028 return QualType(New, 0);
1029}
1030
Sebastian Redl75555032009-01-24 21:16:55 +00001031/// getMemberPointerType - Return the uniqued reference to the type for a
1032/// member pointer to the specified type, in the specified class.
1033QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1034{
1035 // Unique pointers, to guarantee there is only one pointer of a particular
1036 // structure.
1037 llvm::FoldingSetNodeID ID;
1038 MemberPointerType::Profile(ID, T, Cls);
1039
1040 void *InsertPos = 0;
1041 if (MemberPointerType *PT =
1042 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1043 return QualType(PT, 0);
1044
1045 // If the pointee or class type isn't canonical, this won't be a canonical
1046 // type either, so fill in the canonical type field.
1047 QualType Canonical;
1048 if (!T->isCanonical()) {
1049 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1050
1051 // Get the new insert position for the node we care about.
1052 MemberPointerType *NewIP =
1053 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1054 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1055 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001056 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001057 Types.push_back(New);
1058 MemberPointerTypes.InsertNode(New, InsertPos);
1059 return QualType(New, 0);
1060}
1061
Steve Naroff83c13012007-08-30 01:06:46 +00001062/// getConstantArrayType - Return the unique reference to the type for an
1063/// array of the specified element type.
1064QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +00001065 const llvm::APInt &ArySize,
1066 ArrayType::ArraySizeModifier ASM,
1067 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001068 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001069 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001070
1071 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001072 if (ConstantArrayType *ATP =
1073 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001074 return QualType(ATP, 0);
1075
1076 // If the element type isn't canonical, this won't be a canonical type either,
1077 // so fill in the canonical type field.
1078 QualType Canonical;
1079 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001080 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001081 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001082 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001083 ConstantArrayType *NewIP =
1084 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001085 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001086 }
1087
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001088 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001089 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001090 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001091 Types.push_back(New);
1092 return QualType(New, 0);
1093}
1094
Steve Naroffe2579e32007-08-30 18:14:25 +00001095/// getVariableArrayType - Returns a non-unique reference to the type for a
1096/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +00001097QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1098 ArrayType::ArraySizeModifier ASM,
1099 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001100 // Since we don't unique expressions, it isn't possible to unique VLA's
1101 // that have an expression provided for their size.
1102
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001103 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001104 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001105
1106 VariableArrayTypes.push_back(New);
1107 Types.push_back(New);
1108 return QualType(New, 0);
1109}
1110
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001111/// getDependentSizedArrayType - Returns a non-unique reference to
1112/// the type for a dependently-sized array of the specified element
1113/// type. FIXME: We will need these to be uniqued, or at least
1114/// comparable, at some point.
1115QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1116 ArrayType::ArraySizeModifier ASM,
1117 unsigned EltTypeQuals) {
1118 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1119 "Size must be type- or value-dependent!");
1120
1121 // Since we don't unique expressions, it isn't possible to unique
1122 // dependently-sized array types.
1123
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001124 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001125 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1126 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001127
1128 DependentSizedArrayTypes.push_back(New);
1129 Types.push_back(New);
1130 return QualType(New, 0);
1131}
1132
Eli Friedman8ff07782008-02-15 18:16:39 +00001133QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1134 ArrayType::ArraySizeModifier ASM,
1135 unsigned EltTypeQuals) {
1136 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001137 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001138
1139 void *InsertPos = 0;
1140 if (IncompleteArrayType *ATP =
1141 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1142 return QualType(ATP, 0);
1143
1144 // If the element type isn't canonical, this won't be a canonical type
1145 // either, so fill in the canonical type field.
1146 QualType Canonical;
1147
1148 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001149 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001150 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001151
1152 // Get the new insert position for the node we care about.
1153 IncompleteArrayType *NewIP =
1154 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001155 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001156 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001157
Steve Naroff93fd2112009-01-27 22:08:43 +00001158 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001159 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001160
1161 IncompleteArrayTypes.InsertNode(New, InsertPos);
1162 Types.push_back(New);
1163 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001164}
1165
Chris Lattner4b009652007-07-25 00:24:17 +00001166/// getVectorType - Return the unique reference to a vector type of
1167/// the specified element type and size. VectorType must be a built-in type.
1168QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1169 BuiltinType *baseType;
1170
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001171 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001172 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1173
1174 // Check if we've already instantiated a vector of this type.
1175 llvm::FoldingSetNodeID ID;
1176 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1177 void *InsertPos = 0;
1178 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1179 return QualType(VTP, 0);
1180
1181 // If the element type isn't canonical, this won't be a canonical type either,
1182 // so fill in the canonical type field.
1183 QualType Canonical;
1184 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001185 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001186
1187 // Get the new insert position for the node we care about.
1188 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001189 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001190 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001191 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001192 VectorTypes.InsertNode(New, InsertPos);
1193 Types.push_back(New);
1194 return QualType(New, 0);
1195}
1196
Nate Begemanaf6ed502008-04-18 23:10:10 +00001197/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001198/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001199QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001200 BuiltinType *baseType;
1201
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001202 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001203 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001204
1205 // Check if we've already instantiated a vector of this type.
1206 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001207 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001208 void *InsertPos = 0;
1209 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1210 return QualType(VTP, 0);
1211
1212 // If the element type isn't canonical, this won't be a canonical type either,
1213 // so fill in the canonical type field.
1214 QualType Canonical;
1215 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001216 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001217
1218 // Get the new insert position for the node we care about.
1219 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001220 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001221 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001222 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001223 VectorTypes.InsertNode(New, InsertPos);
1224 Types.push_back(New);
1225 return QualType(New, 0);
1226}
1227
Douglas Gregor4fa58902009-02-26 23:50:07 +00001228/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001229///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001230QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001231 // Unique functions, to guarantee there is only one function of a particular
1232 // structure.
1233 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001234 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001235
1236 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001237 if (FunctionNoProtoType *FT =
1238 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001239 return QualType(FT, 0);
1240
1241 QualType Canonical;
1242 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001243 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001244
1245 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001246 FunctionNoProtoType *NewIP =
1247 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001248 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001249 }
1250
Douglas Gregor4fa58902009-02-26 23:50:07 +00001251 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001252 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001253 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001254 return QualType(New, 0);
1255}
1256
1257/// getFunctionType - Return a normal function type with a typed argument
1258/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001259QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001260 unsigned NumArgs, bool isVariadic,
1261 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001262 // Unique functions, to guarantee there is only one function of a particular
1263 // structure.
1264 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001265 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001266 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001267
1268 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001269 if (FunctionProtoType *FTP =
1270 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001271 return QualType(FTP, 0);
1272
1273 // Determine whether the type being created is already canonical or not.
1274 bool isCanonical = ResultTy->isCanonical();
1275 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1276 if (!ArgArray[i]->isCanonical())
1277 isCanonical = false;
1278
1279 // If this type isn't canonical, get the canonical version of it.
1280 QualType Canonical;
1281 if (!isCanonical) {
1282 llvm::SmallVector<QualType, 16> CanonicalArgs;
1283 CanonicalArgs.reserve(NumArgs);
1284 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001285 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001286
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001287 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001288 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001289 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001290
1291 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001292 FunctionProtoType *NewIP =
1293 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001294 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001295 }
1296
Douglas Gregor4fa58902009-02-26 23:50:07 +00001297 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001298 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001299 FunctionProtoType *FTP =
1300 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001301 NumArgs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001302 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001303 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001304 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001305 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001306 return QualType(FTP, 0);
1307}
1308
Douglas Gregor1d661552008-04-13 21:07:44 +00001309/// getTypeDeclType - Return the unique reference to the type for the
1310/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001311QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001312 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001313 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1314
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001315 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001316 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001317 else if (isa<TemplateTypeParmDecl>(Decl)) {
1318 assert(false && "Template type parameter types are always available.");
1319 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001320 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001321
Douglas Gregor2e047592009-02-28 01:32:25 +00001322 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001323 if (PrevDecl)
1324 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001325 else
1326 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001327 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001328 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1329 if (PrevDecl)
1330 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001331 else
1332 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001333 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001334 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001335 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001336
Ted Kremenek46a837c2008-09-05 17:16:31 +00001337 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001338 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001339}
1340
Chris Lattner4b009652007-07-25 00:24:17 +00001341/// getTypedefType - Return the unique reference to the type for the
1342/// specified typename decl.
1343QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1344 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1345
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001346 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001347 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001348 Types.push_back(Decl->TypeForDecl);
1349 return QualType(Decl->TypeForDecl, 0);
1350}
1351
Ted Kremenek42730c52008-01-07 19:49:32 +00001352/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001353/// specified ObjC interface decl.
Daniel Dunbarbe1ff272009-04-22 04:34:53 +00001354QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001355 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1356
Daniel Dunbarbe1ff272009-04-22 04:34:53 +00001357 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1358 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001359 Types.push_back(Decl->TypeForDecl);
1360 return QualType(Decl->TypeForDecl, 0);
1361}
1362
Douglas Gregora4918772009-02-05 23:33:38 +00001363/// \brief Retrieve the template type parameter type for a template
1364/// parameter with the given depth, index, and (optionally) name.
1365QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1366 IdentifierInfo *Name) {
1367 llvm::FoldingSetNodeID ID;
1368 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1369 void *InsertPos = 0;
1370 TemplateTypeParmType *TypeParm
1371 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1372
1373 if (TypeParm)
1374 return QualType(TypeParm, 0);
1375
1376 if (Name)
1377 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1378 getTemplateTypeParmType(Depth, Index));
1379 else
1380 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1381
1382 Types.push_back(TypeParm);
1383 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1384
1385 return QualType(TypeParm, 0);
1386}
1387
Douglas Gregor8e458f42009-02-09 18:46:07 +00001388QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001389ASTContext::getTemplateSpecializationType(TemplateName Template,
1390 const TemplateArgument *Args,
1391 unsigned NumArgs,
1392 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001393 if (!Canon.isNull())
1394 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001395
Douglas Gregor8e458f42009-02-09 18:46:07 +00001396 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001397 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001398
Douglas Gregor8e458f42009-02-09 18:46:07 +00001399 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001400 TemplateSpecializationType *Spec
1401 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001402
1403 if (Spec)
1404 return QualType(Spec, 0);
1405
Douglas Gregordd13e842009-03-30 22:58:21 +00001406 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001407 sizeof(TemplateArgument) * NumArgs),
1408 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001409 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001410 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001411 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001412
1413 return QualType(Spec, 0);
1414}
1415
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001416QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001417ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001418 QualType NamedType) {
1419 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001420 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001421
1422 void *InsertPos = 0;
1423 QualifiedNameType *T
1424 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1425 if (T)
1426 return QualType(T, 0);
1427
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001428 T = new (*this) QualifiedNameType(NNS, NamedType,
1429 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001430 Types.push_back(T);
1431 QualifiedNameTypes.InsertNode(T, InsertPos);
1432 return QualType(T, 0);
1433}
1434
Douglas Gregord3022602009-03-27 23:10:48 +00001435QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1436 const IdentifierInfo *Name,
1437 QualType Canon) {
1438 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1439
1440 if (Canon.isNull()) {
1441 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1442 if (CanonNNS != NNS)
1443 Canon = getTypenameType(CanonNNS, Name);
1444 }
1445
1446 llvm::FoldingSetNodeID ID;
1447 TypenameType::Profile(ID, NNS, Name);
1448
1449 void *InsertPos = 0;
1450 TypenameType *T
1451 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1452 if (T)
1453 return QualType(T, 0);
1454
1455 T = new (*this) TypenameType(NNS, Name, Canon);
1456 Types.push_back(T);
1457 TypenameTypes.InsertNode(T, InsertPos);
1458 return QualType(T, 0);
1459}
1460
Douglas Gregor77da5802009-04-01 00:28:59 +00001461QualType
1462ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1463 const TemplateSpecializationType *TemplateId,
1464 QualType Canon) {
1465 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1466
1467 if (Canon.isNull()) {
1468 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1469 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1470 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1471 const TemplateSpecializationType *CanonTemplateId
1472 = CanonType->getAsTemplateSpecializationType();
1473 assert(CanonTemplateId &&
1474 "Canonical type must also be a template specialization type");
1475 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1476 }
1477 }
1478
1479 llvm::FoldingSetNodeID ID;
1480 TypenameType::Profile(ID, NNS, TemplateId);
1481
1482 void *InsertPos = 0;
1483 TypenameType *T
1484 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1485 if (T)
1486 return QualType(T, 0);
1487
1488 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1489 Types.push_back(T);
1490 TypenameTypes.InsertNode(T, InsertPos);
1491 return QualType(T, 0);
1492}
1493
Chris Lattnere1352302008-04-07 04:56:42 +00001494/// CmpProtocolNames - Comparison predicate for sorting protocols
1495/// alphabetically.
1496static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1497 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001498 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001499}
1500
1501static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1502 unsigned &NumProtocols) {
1503 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1504
1505 // Sort protocols, keyed by name.
1506 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1507
1508 // Remove duplicates.
1509 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1510 NumProtocols = ProtocolsEnd-Protocols;
1511}
1512
1513
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001514/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1515/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001516QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1517 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001518 // Sort the protocol list alphabetically to canonicalize it.
1519 SortAndUniqueProtocols(Protocols, NumProtocols);
1520
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001521 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001522 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001523
1524 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001525 if (ObjCQualifiedInterfaceType *QT =
1526 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001527 return QualType(QT, 0);
1528
1529 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001530 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001531 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001532
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001533 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001534 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001535 return QualType(QType, 0);
1536}
1537
Chris Lattnere1352302008-04-07 04:56:42 +00001538/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1539/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001540QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001541 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001542 // Sort the protocol list alphabetically to canonicalize it.
1543 SortAndUniqueProtocols(Protocols, NumProtocols);
1544
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001545 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001546 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001547
1548 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001549 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001550 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001551 return QualType(QT, 0);
1552
1553 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001554 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001555 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001556 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001557 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001558 return QualType(QType, 0);
1559}
1560
Douglas Gregor4fa58902009-02-26 23:50:07 +00001561/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1562/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001563/// multiple declarations that refer to "typeof(x)" all contain different
1564/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1565/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001566QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001567 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001568 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001569 Types.push_back(toe);
1570 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001571}
1572
Steve Naroff0604dd92007-08-01 18:02:17 +00001573/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1574/// TypeOfType AST's. The only motivation to unique these nodes would be
1575/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1576/// an issue. This doesn't effect the type checker, since it operates
1577/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001578QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001579 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001580 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001581 Types.push_back(tot);
1582 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001583}
1584
Chris Lattner4b009652007-07-25 00:24:17 +00001585/// getTagDeclType - Return the unique reference to the type for the
1586/// specified TagDecl (struct/union/class/enum) decl.
1587QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001588 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001589 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001590}
1591
1592/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1593/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1594/// needs to agree with the definition in <stddef.h>.
1595QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001596 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001597}
1598
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001599/// getSignedWCharType - Return the type of "signed wchar_t".
1600/// Used when in C++, as a GCC extension.
1601QualType ASTContext::getSignedWCharType() const {
1602 // FIXME: derive from "Target" ?
1603 return WCharTy;
1604}
1605
1606/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1607/// Used when in C++, as a GCC extension.
1608QualType ASTContext::getUnsignedWCharType() const {
1609 // FIXME: derive from "Target" ?
1610 return UnsignedIntTy;
1611}
1612
Chris Lattner4b009652007-07-25 00:24:17 +00001613/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1614/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1615QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001616 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001617}
1618
Chris Lattner19eb97e2008-04-02 05:18:44 +00001619//===----------------------------------------------------------------------===//
1620// Type Operators
1621//===----------------------------------------------------------------------===//
1622
Chris Lattner3dae6f42008-04-06 22:41:35 +00001623/// getCanonicalType - Return the canonical (structural) type corresponding to
1624/// the specified potentially non-canonical type. The non-canonical version
1625/// of a type may have many "decorated" versions of types. Decorators can
1626/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1627/// to be free of any of these, allowing two canonical types to be compared
1628/// for exact equality with a simple pointer comparison.
1629QualType ASTContext::getCanonicalType(QualType T) {
1630 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001631
1632 // If the result has type qualifiers, make sure to canonicalize them as well.
1633 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1634 if (TypeQuals == 0) return CanType;
1635
1636 // If the type qualifiers are on an array type, get the canonical type of the
1637 // array with the qualifiers applied to the element type.
1638 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1639 if (!AT)
1640 return CanType.getQualifiedType(TypeQuals);
1641
1642 // Get the canonical version of the element with the extra qualifiers on it.
1643 // This can recursively sink qualifiers through multiple levels of arrays.
1644 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1645 NewEltTy = getCanonicalType(NewEltTy);
1646
1647 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1648 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1649 CAT->getIndexTypeQualifier());
1650 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1651 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1652 IAT->getIndexTypeQualifier());
1653
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001654 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1655 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1656 DSAT->getSizeModifier(),
1657 DSAT->getIndexTypeQualifier());
1658
Chris Lattnera1923f62008-08-04 07:31:14 +00001659 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1660 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1661 VAT->getSizeModifier(),
1662 VAT->getIndexTypeQualifier());
1663}
1664
Douglas Gregord3022602009-03-27 23:10:48 +00001665NestedNameSpecifier *
1666ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1667 if (!NNS)
1668 return 0;
1669
1670 switch (NNS->getKind()) {
1671 case NestedNameSpecifier::Identifier:
1672 // Canonicalize the prefix but keep the identifier the same.
1673 return NestedNameSpecifier::Create(*this,
1674 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1675 NNS->getAsIdentifier());
1676
1677 case NestedNameSpecifier::Namespace:
1678 // A namespace is canonical; build a nested-name-specifier with
1679 // this namespace and no prefix.
1680 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1681
1682 case NestedNameSpecifier::TypeSpec:
1683 case NestedNameSpecifier::TypeSpecWithTemplate: {
1684 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1685 NestedNameSpecifier *Prefix = 0;
1686
1687 // FIXME: This isn't the right check!
1688 if (T->isDependentType())
1689 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1690
1691 return NestedNameSpecifier::Create(*this, Prefix,
1692 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1693 T.getTypePtr());
1694 }
1695
1696 case NestedNameSpecifier::Global:
1697 // The global specifier is canonical and unique.
1698 return NNS;
1699 }
1700
1701 // Required to silence a GCC warning
1702 return 0;
1703}
1704
Chris Lattnera1923f62008-08-04 07:31:14 +00001705
1706const ArrayType *ASTContext::getAsArrayType(QualType T) {
1707 // Handle the non-qualified case efficiently.
1708 if (T.getCVRQualifiers() == 0) {
1709 // Handle the common positive case fast.
1710 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1711 return AT;
1712 }
1713
1714 // Handle the common negative case fast, ignoring CVR qualifiers.
1715 QualType CType = T->getCanonicalTypeInternal();
1716
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001717 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001718 // test.
1719 if (!isa<ArrayType>(CType) &&
1720 !isa<ArrayType>(CType.getUnqualifiedType()))
1721 return 0;
1722
1723 // Apply any CVR qualifiers from the array type to the element type. This
1724 // implements C99 6.7.3p8: "If the specification of an array type includes
1725 // any type qualifiers, the element type is so qualified, not the array type."
1726
1727 // If we get here, we either have type qualifiers on the type, or we have
1728 // sugar such as a typedef in the way. If we have type qualifiers on the type
1729 // we must propagate them down into the elemeng type.
1730 unsigned CVRQuals = T.getCVRQualifiers();
1731 unsigned AddrSpace = 0;
1732 Type *Ty = T.getTypePtr();
1733
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001734 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001735 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001736 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1737 AddrSpace = EXTQT->getAddressSpace();
1738 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001739 } else {
1740 T = Ty->getDesugaredType();
1741 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1742 break;
1743 CVRQuals |= T.getCVRQualifiers();
1744 Ty = T.getTypePtr();
1745 }
1746 }
1747
1748 // If we have a simple case, just return now.
1749 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1750 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1751 return ATy;
1752
1753 // Otherwise, we have an array and we have qualifiers on it. Push the
1754 // qualifiers into the array element type and return a new array type.
1755 // Get the canonical version of the element with the extra qualifiers on it.
1756 // This can recursively sink qualifiers through multiple levels of arrays.
1757 QualType NewEltTy = ATy->getElementType();
1758 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001759 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001760 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1761
1762 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1763 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1764 CAT->getSizeModifier(),
1765 CAT->getIndexTypeQualifier()));
1766 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1767 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1768 IAT->getSizeModifier(),
1769 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001770
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001771 if (const DependentSizedArrayType *DSAT
1772 = dyn_cast<DependentSizedArrayType>(ATy))
1773 return cast<ArrayType>(
1774 getDependentSizedArrayType(NewEltTy,
1775 DSAT->getSizeExpr(),
1776 DSAT->getSizeModifier(),
1777 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001778
Chris Lattnera1923f62008-08-04 07:31:14 +00001779 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1780 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1781 VAT->getSizeModifier(),
1782 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001783}
1784
1785
Chris Lattner19eb97e2008-04-02 05:18:44 +00001786/// getArrayDecayedType - Return the properly qualified result of decaying the
1787/// specified array type to a pointer. This operation is non-trivial when
1788/// handling typedefs etc. The canonical type of "T" must be an array type,
1789/// this returns a pointer to a properly qualified element of the array.
1790///
1791/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1792QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001793 // Get the element type with 'getAsArrayType' so that we don't lose any
1794 // typedefs in the element type of the array. This also handles propagation
1795 // of type qualifiers from the array type into the element type if present
1796 // (C99 6.7.3p8).
1797 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1798 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001799
Chris Lattnera1923f62008-08-04 07:31:14 +00001800 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001801
1802 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001803 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001804}
1805
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001806QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001807 QualType ElemTy = VAT->getElementType();
1808
1809 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1810 return getBaseElementType(VAT);
1811
1812 return ElemTy;
1813}
1814
Chris Lattner4b009652007-07-25 00:24:17 +00001815/// getFloatingRank - Return a relative rank for floating point types.
1816/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001817static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001818 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001819 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001820
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001821 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001822 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001823 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001824 case BuiltinType::Float: return FloatRank;
1825 case BuiltinType::Double: return DoubleRank;
1826 case BuiltinType::LongDouble: return LongDoubleRank;
1827 }
1828}
1829
Steve Narofffa0c4532007-08-27 01:41:48 +00001830/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1831/// point or a complex type (based on typeDomain/typeSize).
1832/// 'typeDomain' is a real floating point or complex type.
1833/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001834QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1835 QualType Domain) const {
1836 FloatingRank EltRank = getFloatingRank(Size);
1837 if (Domain->isComplexType()) {
1838 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001839 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001840 case FloatRank: return FloatComplexTy;
1841 case DoubleRank: return DoubleComplexTy;
1842 case LongDoubleRank: return LongDoubleComplexTy;
1843 }
Chris Lattner4b009652007-07-25 00:24:17 +00001844 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001845
1846 assert(Domain->isRealFloatingType() && "Unknown domain!");
1847 switch (EltRank) {
1848 default: assert(0 && "getFloatingRank(): illegal value for rank");
1849 case FloatRank: return FloatTy;
1850 case DoubleRank: return DoubleTy;
1851 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001852 }
Chris Lattner4b009652007-07-25 00:24:17 +00001853}
1854
Chris Lattner51285d82008-04-06 23:55:33 +00001855/// getFloatingTypeOrder - Compare the rank of the two specified floating
1856/// point types, ignoring the domain of the type (i.e. 'double' ==
1857/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1858/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001859int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1860 FloatingRank LHSR = getFloatingRank(LHS);
1861 FloatingRank RHSR = getFloatingRank(RHS);
1862
1863 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001864 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001865 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001866 return 1;
1867 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001868}
1869
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001870/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1871/// routine will assert if passed a built-in type that isn't an integer or enum,
1872/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001873unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001874 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001875 if (EnumType* ET = dyn_cast<EnumType>(T))
1876 T = ET->getDecl()->getIntegerType().getTypePtr();
1877
1878 // There are two things which impact the integer rank: the width, and
1879 // the ordering of builtins. The builtin ordering is encoded in the
1880 // bottom three bits; the width is encoded in the bits above that.
1881 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1882 return FWIT->getWidth() << 3;
1883 }
1884
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001885 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001886 default: assert(0 && "getIntegerRank(): not a built-in integer");
1887 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001888 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001889 case BuiltinType::Char_S:
1890 case BuiltinType::Char_U:
1891 case BuiltinType::SChar:
1892 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001893 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001894 case BuiltinType::Short:
1895 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001896 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001897 case BuiltinType::Int:
1898 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001899 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001900 case BuiltinType::Long:
1901 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001902 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001903 case BuiltinType::LongLong:
1904 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001905 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001906 }
1907}
1908
Chris Lattner51285d82008-04-06 23:55:33 +00001909/// getIntegerTypeOrder - Returns the highest ranked integer type:
1910/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1911/// LHS < RHS, return -1.
1912int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001913 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1914 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001915 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001916
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001917 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1918 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001919
Chris Lattner51285d82008-04-06 23:55:33 +00001920 unsigned LHSRank = getIntegerRank(LHSC);
1921 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001922
Chris Lattner51285d82008-04-06 23:55:33 +00001923 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1924 if (LHSRank == RHSRank) return 0;
1925 return LHSRank > RHSRank ? 1 : -1;
1926 }
Chris Lattner4b009652007-07-25 00:24:17 +00001927
Chris Lattner51285d82008-04-06 23:55:33 +00001928 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1929 if (LHSUnsigned) {
1930 // If the unsigned [LHS] type is larger, return it.
1931 if (LHSRank >= RHSRank)
1932 return 1;
1933
1934 // If the signed type can represent all values of the unsigned type, it
1935 // wins. Because we are dealing with 2's complement and types that are
1936 // powers of two larger than each other, this is always safe.
1937 return -1;
1938 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001939
Chris Lattner51285d82008-04-06 23:55:33 +00001940 // If the unsigned [RHS] type is larger, return it.
1941 if (RHSRank >= LHSRank)
1942 return -1;
1943
1944 // If the signed type can represent all values of the unsigned type, it
1945 // wins. Because we are dealing with 2's complement and types that are
1946 // powers of two larger than each other, this is always safe.
1947 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001948}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001949
1950// getCFConstantStringType - Return the type used for constant CFStrings.
1951QualType ASTContext::getCFConstantStringType() {
1952 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001953 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001954 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001955 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001956 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001957
1958 // const int *isa;
1959 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001960 // int flags;
1961 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001962 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001963 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001964 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001965 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001966
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001967 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001968 for (unsigned i = 0; i < 4; ++i) {
1969 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1970 SourceLocation(), 0,
1971 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001972 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001973 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001974 }
1975
1976 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001977 }
1978
1979 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001980}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001981
Anders Carlssonf58cac72008-08-30 19:34:46 +00001982QualType ASTContext::getObjCFastEnumerationStateType()
1983{
1984 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001985 ObjCFastEnumerationStateTypeDecl =
1986 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1987 &Idents.get("__objcFastEnumerationState"));
1988
Anders Carlssonf58cac72008-08-30 19:34:46 +00001989 QualType FieldTypes[] = {
1990 UnsignedLongTy,
1991 getPointerType(ObjCIdType),
1992 getPointerType(UnsignedLongTy),
1993 getConstantArrayType(UnsignedLongTy,
1994 llvm::APInt(32, 5), ArrayType::Normal, 0)
1995 };
1996
Douglas Gregor8acb7272008-12-11 16:49:14 +00001997 for (size_t i = 0; i < 4; ++i) {
1998 FieldDecl *Field = FieldDecl::Create(*this,
1999 ObjCFastEnumerationStateTypeDecl,
2000 SourceLocation(), 0,
2001 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002002 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002003 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002004 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00002005
Douglas Gregor8acb7272008-12-11 16:49:14 +00002006 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00002007 }
2008
2009 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2010}
2011
Anders Carlssone3f02572007-10-29 06:33:42 +00002012// This returns true if a type has been typedefed to BOOL:
2013// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00002014static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002015 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00002016 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2017 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002018
2019 return false;
2020}
2021
Ted Kremenek42730c52008-01-07 19:49:32 +00002022/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002023/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00002024int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00002025 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002026
2027 // Make all integer and enum types at least as large as an int
2028 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002029 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002030 // Treat arrays as pointers, since that's how they're passed in.
2031 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002032 sz = getTypeSize(VoidPtrTy);
2033 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002034}
2035
Ted Kremenek42730c52008-01-07 19:49:32 +00002036/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002037/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002038void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00002039 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002040 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002041 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00002042 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002043 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002044 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002045 // Compute size of all parameters.
2046 // Start with computing size of a pointer in number of bytes.
2047 // FIXME: There might(should) be a better way of doing this computation!
2048 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002049 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002050 // The first two arguments (self and _cmd) are pointers; account for
2051 // their size.
2052 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002053 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2054 E = Decl->param_end(); PI != E; ++PI) {
2055 QualType PType = (*PI)->getType();
2056 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002057 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002058 ParmOffset += sz;
2059 }
2060 S += llvm::utostr(ParmOffset);
2061 S += "@0:";
2062 S += llvm::utostr(PtrSize);
2063
2064 // Argument types.
2065 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002066 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2067 E = Decl->param_end(); PI != E; ++PI) {
2068 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002069 QualType PType = PVDecl->getOriginalType();
2070 if (const ArrayType *AT =
Steve Naroff78380fb2009-04-14 00:03:58 +00002071 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2072 // Use array's original type only if it has known number of
2073 // elements.
Steve Naroff6777bf32009-04-14 00:40:09 +00002074 if (!isa<ConstantArrayType>(AT))
Steve Naroff78380fb2009-04-14 00:03:58 +00002075 PType = PVDecl->getType();
2076 } else if (PType->isFunctionType())
2077 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002078 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002079 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002080 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002081 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002082 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002083 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002084 }
2085}
2086
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002087/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002088/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002089/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2090/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002091/// Property attributes are stored as a comma-delimited C string. The simple
2092/// attributes readonly and bycopy are encoded as single characters. The
2093/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2094/// encoded as single characters, followed by an identifier. Property types
2095/// are also encoded as a parametrized attribute. The characters used to encode
2096/// these attributes are defined by the following enumeration:
2097/// @code
2098/// enum PropertyAttributes {
2099/// kPropertyReadOnly = 'R', // property is read-only.
2100/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2101/// kPropertyByref = '&', // property is a reference to the value last assigned
2102/// kPropertyDynamic = 'D', // property is dynamic
2103/// kPropertyGetter = 'G', // followed by getter selector name
2104/// kPropertySetter = 'S', // followed by setter selector name
2105/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2106/// kPropertyType = 't' // followed by old-style type encoding.
2107/// kPropertyWeak = 'W' // 'weak' property
2108/// kPropertyStrong = 'P' // property GC'able
2109/// kPropertyNonAtomic = 'N' // property non-atomic
2110/// };
2111/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002112void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2113 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002114 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002115 // Collect information from the property implementation decl(s).
2116 bool Dynamic = false;
2117 ObjCPropertyImplDecl *SynthesizePID = 0;
2118
2119 // FIXME: Duplicated code due to poor abstraction.
2120 if (Container) {
2121 if (const ObjCCategoryImplDecl *CID =
2122 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2123 for (ObjCCategoryImplDecl::propimpl_iterator
2124 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2125 ObjCPropertyImplDecl *PID = *i;
2126 if (PID->getPropertyDecl() == PD) {
2127 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2128 Dynamic = true;
2129 } else {
2130 SynthesizePID = PID;
2131 }
2132 }
2133 }
2134 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002135 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002136 for (ObjCCategoryImplDecl::propimpl_iterator
2137 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2138 ObjCPropertyImplDecl *PID = *i;
2139 if (PID->getPropertyDecl() == PD) {
2140 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2141 Dynamic = true;
2142 } else {
2143 SynthesizePID = PID;
2144 }
2145 }
2146 }
2147 }
2148 }
2149
2150 // FIXME: This is not very efficient.
2151 S = "T";
2152
2153 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002154 // GCC has some special rules regarding encoding of properties which
2155 // closely resembles encoding of ivars.
Daniel Dunbar701c8502009-04-20 06:37:24 +00002156 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002157 true /* outermost type */,
2158 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002159
2160 if (PD->isReadOnly()) {
2161 S += ",R";
2162 } else {
2163 switch (PD->getSetterKind()) {
2164 case ObjCPropertyDecl::Assign: break;
2165 case ObjCPropertyDecl::Copy: S += ",C"; break;
2166 case ObjCPropertyDecl::Retain: S += ",&"; break;
2167 }
2168 }
2169
2170 // It really isn't clear at all what this means, since properties
2171 // are "dynamic by default".
2172 if (Dynamic)
2173 S += ",D";
2174
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002175 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2176 S += ",N";
2177
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002178 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2179 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002180 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002181 }
2182
2183 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2184 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002185 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002186 }
2187
2188 if (SynthesizePID) {
2189 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2190 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002191 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002192 }
2193
2194 // FIXME: OBJCGC: weak & strong
2195}
2196
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002197/// getLegacyIntegralTypeEncoding -
2198/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002199/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002200/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2201///
2202void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2203 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2204 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002205 if (BT->getKind() == BuiltinType::ULong &&
2206 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002207 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002208 else
2209 if (BT->getKind() == BuiltinType::Long &&
2210 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002211 PointeeTy = IntTy;
2212 }
2213 }
2214}
2215
Fariborz Jahanian248db262008-01-22 22:44:46 +00002216void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002217 const FieldDecl *Field) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002218 // We follow the behavior of gcc, expanding structures which are
2219 // directly pointed to, and expanding embedded structures. Note that
2220 // these rules are sufficient to prevent recursive encoding of the
2221 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002222 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2223 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002224}
2225
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002226static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002227 const FieldDecl *FD) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002228 const Expr *E = FD->getBitWidth();
2229 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2230 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2231 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2232 S += 'b';
2233 S += llvm::utostr(N);
2234}
2235
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002236void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2237 bool ExpandPointedToStructures,
2238 bool ExpandStructures,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002239 const FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002240 bool OutermostType,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002241 bool EncodingProperty) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002242 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002243 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002244 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002245 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002246 else {
2247 char encoding;
2248 switch (BT->getKind()) {
2249 default: assert(0 && "Unhandled builtin type kind");
2250 case BuiltinType::Void: encoding = 'v'; break;
2251 case BuiltinType::Bool: encoding = 'B'; break;
2252 case BuiltinType::Char_U:
2253 case BuiltinType::UChar: encoding = 'C'; break;
2254 case BuiltinType::UShort: encoding = 'S'; break;
2255 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002256 case BuiltinType::ULong:
2257 encoding =
2258 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2259 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002260 case BuiltinType::ULongLong: encoding = 'Q'; break;
2261 case BuiltinType::Char_S:
2262 case BuiltinType::SChar: encoding = 'c'; break;
2263 case BuiltinType::Short: encoding = 's'; break;
2264 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002265 case BuiltinType::Long:
2266 encoding =
2267 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2268 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002269 case BuiltinType::LongLong: encoding = 'q'; break;
2270 case BuiltinType::Float: encoding = 'f'; break;
2271 case BuiltinType::Double: encoding = 'd'; break;
2272 case BuiltinType::LongDouble: encoding = 'd'; break;
2273 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002274
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002275 S += encoding;
2276 }
Anders Carlsson70e16dd2009-04-09 21:55:45 +00002277 } else if (const ComplexType *CT = T->getAsComplexType()) {
2278 S += 'j';
2279 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2280 false);
2281 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002282 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2283 ExpandPointedToStructures,
2284 ExpandStructures, FD);
2285 if (FD || EncodingProperty) {
2286 // Note that we do extended encoding of protocol qualifer list
2287 // Only when doing ivar or property encoding.
2288 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2289 S += '"';
2290 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2291 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2292 S += '<';
2293 S += Proto->getNameAsString();
2294 S += '>';
2295 }
2296 S += '"';
2297 }
2298 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002299 }
2300 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002301 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002302 bool isReadOnly = false;
2303 // For historical/compatibility reasons, the read-only qualifier of the
2304 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2305 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2306 // Also, do not emit the 'r' for anything but the outermost type!
2307 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2308 if (OutermostType && T.isConstQualified()) {
2309 isReadOnly = true;
2310 S += 'r';
2311 }
2312 }
2313 else if (OutermostType) {
2314 QualType P = PointeeTy;
2315 while (P->getAsPointerType())
2316 P = P->getAsPointerType()->getPointeeType();
2317 if (P.isConstQualified()) {
2318 isReadOnly = true;
2319 S += 'r';
2320 }
2321 }
2322 if (isReadOnly) {
2323 // Another legacy compatibility encoding. Some ObjC qualifier and type
2324 // combinations need to be rearranged.
2325 // Rewrite "in const" from "nr" to "rn"
2326 const char * s = S.c_str();
2327 int len = S.length();
2328 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2329 std::string replace = "rn";
2330 S.replace(S.end()-2, S.end(), replace);
2331 }
2332 }
Steve Naroff17c03822009-02-12 17:52:19 +00002333 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002334 S += '@';
2335 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002336 }
2337 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002338 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002339 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002340 // Another historical/compatibility reason.
2341 // We encode the underlying type which comes out as
2342 // {...};
2343 S += '^';
2344 getObjCEncodingForTypeImpl(PointeeTy, S,
2345 false, ExpandPointedToStructures,
2346 NULL);
2347 return;
2348 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002349 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002350 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002351 const ObjCInterfaceType *OIT =
2352 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002353 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002354 S += '"';
2355 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002356 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2357 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2358 S += '<';
2359 S += Proto->getNameAsString();
2360 S += '>';
2361 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002362 S += '"';
2363 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002364 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002365 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002366 S += '#';
2367 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002368 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002369 S += ':';
2370 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002371 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002372
2373 if (PointeeTy->isCharType()) {
2374 // char pointer types should be encoded as '*' unless it is a
2375 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002376 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002377 S += '*';
2378 return;
2379 }
2380 }
2381
2382 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002383 getLegacyIntegralTypeEncoding(PointeeTy);
2384
2385 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002386 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002387 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002388 } else if (const ArrayType *AT =
2389 // Ignore type qualifiers etc.
2390 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002391 if (isa<IncompleteArrayType>(AT)) {
2392 // Incomplete arrays are encoded as a pointer to the array element.
2393 S += '^';
2394
2395 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2396 false, ExpandStructures, FD);
2397 } else {
2398 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002399
Anders Carlsson858c64d2009-02-22 01:38:57 +00002400 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2401 S += llvm::utostr(CAT->getSize().getZExtValue());
2402 else {
2403 //Variable length arrays are encoded as a regular array with 0 elements.
2404 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2405 S += '0';
2406 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002407
Anders Carlsson858c64d2009-02-22 01:38:57 +00002408 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2409 false, ExpandStructures, FD);
2410 S += ']';
2411 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002412 } else if (T->getAsFunctionType()) {
2413 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002414 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002415 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002416 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002417 // Anonymous structures print as '?'
2418 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2419 S += II->getName();
2420 } else {
2421 S += '?';
2422 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002423 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002424 S += '=';
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002425 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2426 FieldEnd = RDecl->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002427 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002428 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002429 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002430 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002431 S += '"';
2432 }
2433
2434 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002435 if (Field->isBitField()) {
2436 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2437 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002438 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002439 QualType qt = Field->getType();
2440 getLegacyIntegralTypeEncoding(qt);
2441 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002442 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002443 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002444 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002445 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002446 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002447 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002448 if (FD && FD->isBitField())
2449 EncodeBitField(this, S, FD);
2450 else
2451 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002452 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002453 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002454 } else if (T->isObjCInterfaceType()) {
2455 // @encode(class_name)
2456 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2457 S += '{';
2458 const IdentifierInfo *II = OI->getIdentifier();
2459 S += II->getName();
2460 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002461 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002462 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002463 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002464 if (RecFields[i]->isBitField())
2465 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2466 RecFields[i]);
2467 else
2468 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2469 FD);
2470 }
2471 S += '}';
2472 }
2473 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002474 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002475}
2476
Ted Kremenek42730c52008-01-07 19:49:32 +00002477void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002478 std::string& S) const {
2479 if (QT & Decl::OBJC_TQ_In)
2480 S += 'n';
2481 if (QT & Decl::OBJC_TQ_Inout)
2482 S += 'N';
2483 if (QT & Decl::OBJC_TQ_Out)
2484 S += 'o';
2485 if (QT & Decl::OBJC_TQ_Bycopy)
2486 S += 'O';
2487 if (QT & Decl::OBJC_TQ_Byref)
2488 S += 'R';
2489 if (QT & Decl::OBJC_TQ_Oneway)
2490 S += 'V';
2491}
2492
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002493void ASTContext::setBuiltinVaListType(QualType T)
2494{
2495 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2496
2497 BuiltinVaListType = T;
2498}
2499
Ted Kremenek42730c52008-01-07 19:49:32 +00002500void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002501{
Ted Kremenek42730c52008-01-07 19:49:32 +00002502 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002503
2504 // typedef struct objc_object *id;
2505 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002506 // User error - caller will issue diagnostics.
2507 if (!ptr)
2508 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002509 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002510 // User error - caller will issue diagnostics.
2511 if (!rec)
2512 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002513 IdStructType = rec;
2514}
2515
Ted Kremenek42730c52008-01-07 19:49:32 +00002516void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002517{
Ted Kremenek42730c52008-01-07 19:49:32 +00002518 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002519
2520 // typedef struct objc_selector *SEL;
2521 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002522 if (!ptr)
2523 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002524 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002525 if (!rec)
2526 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002527 SelStructType = rec;
2528}
2529
Ted Kremenek42730c52008-01-07 19:49:32 +00002530void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002531{
Ted Kremenek42730c52008-01-07 19:49:32 +00002532 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002533}
2534
Ted Kremenek42730c52008-01-07 19:49:32 +00002535void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002536{
Ted Kremenek42730c52008-01-07 19:49:32 +00002537 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002538
2539 // typedef struct objc_class *Class;
2540 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2541 assert(ptr && "'Class' incorrectly typed");
2542 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2543 assert(rec && "'Class' incorrectly typed");
2544 ClassStructType = rec;
2545}
2546
Ted Kremenek42730c52008-01-07 19:49:32 +00002547void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2548 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002549 "'NSConstantString' type already set!");
2550
Ted Kremenek42730c52008-01-07 19:49:32 +00002551 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002552}
2553
Douglas Gregordd13e842009-03-30 22:58:21 +00002554/// \brief Retrieve the template name that represents a qualified
2555/// template name such as \c std::vector.
2556TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2557 bool TemplateKeyword,
2558 TemplateDecl *Template) {
2559 llvm::FoldingSetNodeID ID;
2560 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2561
2562 void *InsertPos = 0;
2563 QualifiedTemplateName *QTN =
2564 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2565 if (!QTN) {
2566 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2567 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2568 }
2569
2570 return TemplateName(QTN);
2571}
2572
2573/// \brief Retrieve the template name that represents a dependent
2574/// template name such as \c MetaFun::template apply.
2575TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2576 const IdentifierInfo *Name) {
2577 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2578
2579 llvm::FoldingSetNodeID ID;
2580 DependentTemplateName::Profile(ID, NNS, Name);
2581
2582 void *InsertPos = 0;
2583 DependentTemplateName *QTN =
2584 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2585
2586 if (QTN)
2587 return TemplateName(QTN);
2588
2589 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2590 if (CanonNNS == NNS) {
2591 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2592 } else {
2593 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2594 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2595 }
2596
2597 DependentTemplateNames.InsertNode(QTN, InsertPos);
2598 return TemplateName(QTN);
2599}
2600
Douglas Gregorc6507e42008-11-03 14:12:49 +00002601/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002602/// TargetInfo, produce the corresponding type. The unsigned @p Type
2603/// is actually a value of type @c TargetInfo::IntType.
2604QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002605 switch (Type) {
2606 case TargetInfo::NoInt: return QualType();
2607 case TargetInfo::SignedShort: return ShortTy;
2608 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2609 case TargetInfo::SignedInt: return IntTy;
2610 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2611 case TargetInfo::SignedLong: return LongTy;
2612 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2613 case TargetInfo::SignedLongLong: return LongLongTy;
2614 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2615 }
2616
2617 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002618 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002619}
Ted Kremenek118930e2008-07-24 23:58:27 +00002620
2621//===----------------------------------------------------------------------===//
2622// Type Predicates.
2623//===----------------------------------------------------------------------===//
2624
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002625/// isObjCNSObjectType - Return true if this is an NSObject object using
2626/// NSObject attribute on a c-style pointer type.
2627/// FIXME - Make it work directly on types.
2628///
2629bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2630 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2631 if (TypedefDecl *TD = TDT->getDecl())
2632 if (TD->getAttr<ObjCNSObjectAttr>())
2633 return true;
2634 }
2635 return false;
2636}
2637
Ted Kremenek118930e2008-07-24 23:58:27 +00002638/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2639/// to an object type. This includes "id" and "Class" (two 'special' pointers
2640/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2641/// ID type).
2642bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002643 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002644 return true;
2645
Steve Naroffd9e00802008-10-21 18:24:04 +00002646 // Blocks are objects.
2647 if (Ty->isBlockPointerType())
2648 return true;
2649
2650 // All other object types are pointers.
Chris Lattnera008d172009-04-12 23:51:02 +00002651 const PointerType *PT = Ty->getAsPointerType();
2652 if (PT == 0)
Ted Kremenek118930e2008-07-24 23:58:27 +00002653 return false;
2654
Chris Lattnera008d172009-04-12 23:51:02 +00002655 // If this a pointer to an interface (e.g. NSString*), it is ok.
2656 if (PT->getPointeeType()->isObjCInterfaceType() ||
2657 // If is has NSObject attribute, OK as well.
2658 isObjCNSObjectType(Ty))
2659 return true;
2660
Ted Kremenek118930e2008-07-24 23:58:27 +00002661 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2662 // pointer types. This looks for the typedef specifically, not for the
Chris Lattnera008d172009-04-12 23:51:02 +00002663 // underlying type. Iteratively strip off typedefs so that we can handle
2664 // typedefs of typedefs.
2665 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2666 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2667 Ty.getUnqualifiedType() == getObjCClassType())
2668 return true;
2669
2670 Ty = TDT->getDecl()->getUnderlyingType();
2671 }
Ted Kremenek118930e2008-07-24 23:58:27 +00002672
Chris Lattnera008d172009-04-12 23:51:02 +00002673 return false;
Ted Kremenek118930e2008-07-24 23:58:27 +00002674}
2675
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002676/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2677/// garbage collection attribute.
2678///
2679QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002680 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002681 if (getLangOptions().ObjC1 &&
2682 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002683 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002684 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002685 // (or pointers to them) be treated as though they were declared
2686 // as __strong.
2687 if (GCAttrs == QualType::GCNone) {
2688 if (isObjCObjectPointerType(Ty))
2689 GCAttrs = QualType::Strong;
2690 else if (Ty->isPointerType())
2691 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2692 }
Fariborz Jahaniand7b01972009-04-11 00:00:54 +00002693 // Non-pointers have none gc'able attribute regardless of the attribute
2694 // set on them.
2695 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2696 return QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002697 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002698 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002699}
2700
Chris Lattner6ff358b2008-04-07 06:51:04 +00002701//===----------------------------------------------------------------------===//
2702// Type Compatibility Testing
2703//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002704
Steve Naroff3454b6c2008-09-04 15:10:53 +00002705/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002706/// block types. Types must be strictly compatible here. For example,
2707/// C unfortunately doesn't produce an error for the following:
2708///
2709/// int (*emptyArgFunc)();
2710/// int (*intArgList)(int) = emptyArgFunc;
2711///
2712/// For blocks, we will produce an error for the following (similar to C++):
2713///
2714/// int (^emptyArgBlock)();
2715/// int (^intArgBlock)(int) = emptyArgBlock;
2716///
2717/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2718///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002719bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002720 const FunctionType *lbase = lhs->getAsFunctionType();
2721 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002722 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2723 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stump896ceed2009-04-01 01:17:39 +00002724 if (lproto && rproto == 0)
2725 return false;
2726 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff3454b6c2008-09-04 15:10:53 +00002727}
2728
Chris Lattner6ff358b2008-04-07 06:51:04 +00002729/// areCompatVectorTypes - Return true if the two specified vector types are
2730/// compatible.
2731static bool areCompatVectorTypes(const VectorType *LHS,
2732 const VectorType *RHS) {
2733 assert(LHS->isCanonical() && RHS->isCanonical());
2734 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002735 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002736}
2737
Eli Friedman0d9549b2008-08-22 00:56:42 +00002738/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002739/// compatible for assignment from RHS to LHS. This handles validation of any
2740/// protocol qualifiers on the LHS or RHS.
2741///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002742bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2743 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002744 // Verify that the base decls are compatible: the RHS must be a subclass of
2745 // the LHS.
2746 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2747 return false;
2748
2749 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2750 // protocol qualified at all, then we are good.
2751 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2752 return true;
2753
2754 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2755 // isn't a superset.
2756 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2757 return true; // FIXME: should return false!
2758
2759 // Finally, we must have two protocol-qualified interfaces.
2760 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2761 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002762
Steve Naroff98e71b82009-03-01 16:12:44 +00002763 // All LHS protocols must have a presence on the RHS.
2764 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002765
Steve Naroff98e71b82009-03-01 16:12:44 +00002766 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2767 LHSPE = LHSP->qual_end();
2768 LHSPI != LHSPE; LHSPI++) {
2769 bool RHSImplementsProtocol = false;
2770
2771 // If the RHS doesn't implement the protocol on the left, the types
2772 // are incompatible.
2773 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2774 RHSPE = RHSP->qual_end();
2775 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2776 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2777 RHSImplementsProtocol = true;
2778 }
2779 // FIXME: For better diagnostics, consider passing back the protocol name.
2780 if (!RHSImplementsProtocol)
2781 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002782 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002783 // The RHS implements all protocols listed on the LHS.
2784 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002785}
2786
Steve Naroff17c03822009-02-12 17:52:19 +00002787bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2788 // get the "pointed to" types
2789 const PointerType *LHSPT = LHS->getAsPointerType();
2790 const PointerType *RHSPT = RHS->getAsPointerType();
2791
2792 if (!LHSPT || !RHSPT)
2793 return false;
2794
2795 QualType lhptee = LHSPT->getPointeeType();
2796 QualType rhptee = RHSPT->getPointeeType();
2797 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2798 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2799 // ID acts sort of like void* for ObjC interfaces
2800 if (LHSIface && isObjCIdStructType(rhptee))
2801 return true;
2802 if (RHSIface && isObjCIdStructType(lhptee))
2803 return true;
2804 if (!LHSIface || !RHSIface)
2805 return false;
2806 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2807 canAssignObjCInterfaces(RHSIface, LHSIface);
2808}
2809
Steve Naroff85f0dc52007-10-15 20:41:53 +00002810/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2811/// both shall have the identically qualified version of a compatible type.
2812/// C99 6.2.7p1: Two types have compatible types if their types are the
2813/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002814bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2815 return !mergeTypes(LHS, RHS).isNull();
2816}
2817
2818QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2819 const FunctionType *lbase = lhs->getAsFunctionType();
2820 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002821 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2822 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002823 bool allLTypes = true;
2824 bool allRTypes = true;
2825
2826 // Check return type
2827 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2828 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002829 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2830 allLTypes = false;
2831 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2832 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002833
2834 if (lproto && rproto) { // two C99 style function prototypes
2835 unsigned lproto_nargs = lproto->getNumArgs();
2836 unsigned rproto_nargs = rproto->getNumArgs();
2837
2838 // Compatible functions must have the same number of arguments
2839 if (lproto_nargs != rproto_nargs)
2840 return QualType();
2841
2842 // Variadic and non-variadic functions aren't compatible
2843 if (lproto->isVariadic() != rproto->isVariadic())
2844 return QualType();
2845
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002846 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2847 return QualType();
2848
Eli Friedman0d9549b2008-08-22 00:56:42 +00002849 // Check argument compatibility
2850 llvm::SmallVector<QualType, 10> types;
2851 for (unsigned i = 0; i < lproto_nargs; i++) {
2852 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2853 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2854 QualType argtype = mergeTypes(largtype, rargtype);
2855 if (argtype.isNull()) return QualType();
2856 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002857 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2858 allLTypes = false;
2859 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2860 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002861 }
2862 if (allLTypes) return lhs;
2863 if (allRTypes) return rhs;
2864 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002865 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002866 }
2867
2868 if (lproto) allRTypes = false;
2869 if (rproto) allLTypes = false;
2870
Douglas Gregor4fa58902009-02-26 23:50:07 +00002871 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002872 if (proto) {
2873 if (proto->isVariadic()) return QualType();
2874 // Check that the types are compatible with the types that
2875 // would result from default argument promotions (C99 6.7.5.3p15).
2876 // The only types actually affected are promotable integer
2877 // types and floats, which would be passed as a different
2878 // type depending on whether the prototype is visible.
2879 unsigned proto_nargs = proto->getNumArgs();
2880 for (unsigned i = 0; i < proto_nargs; ++i) {
2881 QualType argTy = proto->getArgType(i);
2882 if (argTy->isPromotableIntegerType() ||
2883 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2884 return QualType();
2885 }
2886
2887 if (allLTypes) return lhs;
2888 if (allRTypes) return rhs;
2889 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002890 proto->getNumArgs(), lproto->isVariadic(),
2891 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002892 }
2893
2894 if (allLTypes) return lhs;
2895 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002896 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002897}
2898
2899QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002900 // C++ [expr]: If an expression initially has the type "reference to T", the
2901 // type is adjusted to "T" prior to any further analysis, the expression
2902 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002903 // expression is an lvalue unless the reference is an rvalue reference and
2904 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002905 // FIXME: C++ shouldn't be going through here! The rules are different
2906 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00002907 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2908 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002909 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002910 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002911 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002912 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002913
Eli Friedman0d9549b2008-08-22 00:56:42 +00002914 QualType LHSCan = getCanonicalType(LHS),
2915 RHSCan = getCanonicalType(RHS);
2916
2917 // If two types are identical, they are compatible.
2918 if (LHSCan == RHSCan)
2919 return LHS;
2920
2921 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002922 // Note that we handle extended qualifiers later, in the
2923 // case for ExtQualType.
2924 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002925 return QualType();
2926
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00002927 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
2928 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002929
Chris Lattnerc38d4522008-01-14 05:45:46 +00002930 // We want to consider the two function types to be the same for these
2931 // comparisons, just force one to the other.
2932 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2933 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002934
2935 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002936 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2937 LHSClass = Type::ConstantArray;
2938 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2939 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002940
Nate Begemanaf6ed502008-04-18 23:10:10 +00002941 // Canonicalize ExtVector -> Vector.
2942 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2943 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002944
Chris Lattner7cdcb252008-04-07 06:38:24 +00002945 // Consider qualified interfaces and interfaces the same.
2946 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2947 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002948
Chris Lattnerb5709e22008-04-07 05:43:21 +00002949 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002950 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002951 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2952 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00002953
Steve Naroff0773c582009-04-14 15:11:46 +00002954 // 'id' and 'Class' act sort of like void* for ObjC interfaces
2955 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00002956 return LHS;
Steve Naroff0773c582009-04-14 15:11:46 +00002957 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00002958 return RHS;
2959
Steve Naroff28ceff72008-12-10 22:14:21 +00002960 // ID is compatible with all qualified id types.
2961 if (LHS->isObjCQualifiedIdType()) {
2962 if (const PointerType *PT = RHS->getAsPointerType()) {
2963 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00002964 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002965 return LHS;
2966 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2967 // Unfortunately, this API is part of Sema (which we don't have access
2968 // to. Need to refactor. The following check is insufficient, since we
2969 // need to make sure the class implements the protocol.
2970 if (pType->isObjCInterfaceType())
2971 return LHS;
2972 }
2973 }
2974 if (RHS->isObjCQualifiedIdType()) {
2975 if (const PointerType *PT = LHS->getAsPointerType()) {
2976 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00002977 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002978 return RHS;
2979 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2980 // Unfortunately, this API is part of Sema (which we don't have access
2981 // to. Need to refactor. The following check is insufficient, since we
2982 // need to make sure the class implements the protocol.
2983 if (pType->isObjCInterfaceType())
2984 return RHS;
2985 }
2986 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002987 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2988 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002989 if (const EnumType* ETy = LHS->getAsEnumType()) {
2990 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2991 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002992 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002993 if (const EnumType* ETy = RHS->getAsEnumType()) {
2994 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2995 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002996 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002997
Eli Friedman0d9549b2008-08-22 00:56:42 +00002998 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002999 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003000
Steve Naroffc88babe2008-01-09 22:43:08 +00003001 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003002 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00003003#define TYPE(Class, Base)
3004#define ABSTRACT_TYPE(Class, Base)
3005#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3006#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3007#include "clang/AST/TypeNodes.def"
3008 assert(false && "Non-canonical and dependent types shouldn't get here");
3009 return QualType();
3010
Sebastian Redlce6fff02009-03-16 23:22:08 +00003011 case Type::LValueReference:
3012 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003013 case Type::MemberPointer:
3014 assert(false && "C++ should never be in mergeTypes");
3015 return QualType();
3016
3017 case Type::IncompleteArray:
3018 case Type::VariableArray:
3019 case Type::FunctionProto:
3020 case Type::ExtVector:
3021 case Type::ObjCQualifiedInterface:
3022 assert(false && "Types are eliminated above");
3023 return QualType();
3024
Chris Lattnerc38d4522008-01-14 05:45:46 +00003025 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003026 {
3027 // Merge two pointer types, while trying to preserve typedef info
3028 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3029 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3030 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3031 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003032 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3033 return LHS;
3034 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3035 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003036 return getPointerType(ResultType);
3037 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003038 case Type::BlockPointer:
3039 {
3040 // Merge two block pointer types, while trying to preserve typedef info
3041 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3042 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3043 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3044 if (ResultType.isNull()) return QualType();
3045 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3046 return LHS;
3047 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3048 return RHS;
3049 return getBlockPointerType(ResultType);
3050 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003051 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003052 {
3053 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3054 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3055 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3056 return QualType();
3057
3058 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3059 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3060 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3061 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003062 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3063 return LHS;
3064 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3065 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003066 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3067 ArrayType::ArraySizeModifier(), 0);
3068 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3069 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003070 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3071 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003072 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3073 return LHS;
3074 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3075 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003076 if (LVAT) {
3077 // FIXME: This isn't correct! But tricky to implement because
3078 // the array's size has to be the size of LHS, but the type
3079 // has to be different.
3080 return LHS;
3081 }
3082 if (RVAT) {
3083 // FIXME: This isn't correct! But tricky to implement because
3084 // the array's size has to be the size of RHS, but the type
3085 // has to be different.
3086 return RHS;
3087 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003088 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3089 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003090 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003091 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003092 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003093 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003094 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003095 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003096 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003097 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3098 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003099 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003100 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003101 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003102 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003103 case Type::Complex:
3104 // Distinct complex types are incompatible.
3105 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003106 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003107 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003108 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3109 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003110 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003111 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003112 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003113 // FIXME: This should be type compatibility, e.g. whether
3114 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003115 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3116 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3117 if (LHSIface && RHSIface &&
3118 canAssignObjCInterfaces(LHSIface, RHSIface))
3119 return LHS;
3120
Eli Friedman0d9549b2008-08-22 00:56:42 +00003121 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003122 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003123 case Type::ObjCQualifiedId:
3124 // Distinct qualified id's are not compatible.
3125 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003126 case Type::FixedWidthInt:
3127 // Distinct fixed-width integers are not compatible.
3128 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003129 case Type::ExtQual:
3130 // FIXME: ExtQual types can be compatible even if they're not
3131 // identical!
3132 return QualType();
3133 // First attempt at an implementation, but I'm not really sure it's
3134 // right...
3135#if 0
3136 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3137 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3138 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3139 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3140 return QualType();
3141 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3142 LHSBase = QualType(LQual->getBaseType(), 0);
3143 RHSBase = QualType(RQual->getBaseType(), 0);
3144 ResultType = mergeTypes(LHSBase, RHSBase);
3145 if (ResultType.isNull()) return QualType();
3146 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3147 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3148 return LHS;
3149 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3150 return RHS;
3151 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3152 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3153 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3154 return ResultType;
3155#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003156
3157 case Type::TemplateSpecialization:
3158 assert(false && "Dependent types have no size");
3159 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003160 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003161
3162 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003163}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003164
Chris Lattner1d78a862008-04-07 07:01:58 +00003165//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003166// Integer Predicates
3167//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003168
Eli Friedman0832dbc2008-06-28 06:23:08 +00003169unsigned ASTContext::getIntWidth(QualType T) {
3170 if (T == BoolTy)
3171 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003172 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3173 return FWIT->getWidth();
3174 }
3175 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003176 return (unsigned)getTypeSize(T);
3177}
3178
3179QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3180 assert(T->isSignedIntegerType() && "Unexpected type");
3181 if (const EnumType* ETy = T->getAsEnumType())
3182 T = ETy->getDecl()->getIntegerType();
3183 const BuiltinType* BTy = T->getAsBuiltinType();
3184 assert (BTy && "Unexpected signed integer type");
3185 switch (BTy->getKind()) {
3186 case BuiltinType::Char_S:
3187 case BuiltinType::SChar:
3188 return UnsignedCharTy;
3189 case BuiltinType::Short:
3190 return UnsignedShortTy;
3191 case BuiltinType::Int:
3192 return UnsignedIntTy;
3193 case BuiltinType::Long:
3194 return UnsignedLongTy;
3195 case BuiltinType::LongLong:
3196 return UnsignedLongLongTy;
3197 default:
3198 assert(0 && "Unexpected signed integer type");
3199 return QualType();
3200 }
3201}
3202
3203
3204//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00003205// Serialization Support
3206//===----------------------------------------------------------------------===//
3207
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003208enum {
3209 BasicMetadataBlock = 1,
3210 ASTContextBlock = 2,
3211 DeclsBlock = 3
3212};
3213
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003214void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3215 // Create bitstream.
3216 llvm::BitstreamWriter Stream(Buffer);
3217
3218 // Emit the preamble.
3219 Stream.Emit((unsigned)'B', 8);
3220 Stream.Emit((unsigned)'C', 8);
3221 Stream.Emit(0xC, 4);
3222 Stream.Emit(0xF, 4);
3223 Stream.Emit(0xE, 4);
3224 Stream.Emit(0x0, 4);
3225
3226 // Create serializer.
3227 llvm::Serializer S(Stream);
3228
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003229 // ===---------------------------------------------------===/
3230 // Serialize the "Translation Unit" metadata.
3231 // ===---------------------------------------------------===/
3232
3233 // Emit ASTContext.
3234 S.EnterBlock(ASTContextBlock);
3235 S.EmitOwnedPtr(this);
3236 S.ExitBlock(); // exit "ASTContextBlock"
3237
3238 S.EnterBlock(BasicMetadataBlock);
3239
3240 // Block for SourceManager and Target. Allows easy skipping
3241 // around to the block for the Selectors during deserialization.
3242 S.EnterBlock();
3243
3244 // Emit the SourceManager.
3245 S.Emit(getSourceManager());
3246
3247 // Emit the Target.
3248 S.EmitPtr(&Target);
3249 S.EmitCStr(Target.getTargetTriple());
3250
3251 S.ExitBlock(); // exit "SourceManager and Target Block"
3252
3253 // Emit the Selectors.
3254 S.Emit(Selectors);
3255
3256 // Emit the Identifier Table.
3257 S.Emit(Idents);
3258
3259 S.ExitBlock(); // exit "BasicMetadataBlock"
3260}
3261
3262
Ted Kremenek738e6c02007-10-31 17:10:13 +00003263/// Emit - Serialize an ASTContext object to Bitcode.
3264void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00003265 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00003266 S.EmitRef(SourceMgr);
3267 S.EmitRef(Target);
3268 S.EmitRef(Idents);
3269 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003270
Ted Kremenek68228a92007-10-31 22:44:07 +00003271 // Emit the size of the type vector so that we can reserve that size
3272 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003273 S.EmitInt(Types.size());
3274
Ted Kremenek034a78c2007-11-13 22:02:55 +00003275 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3276 I!=E;++I)
3277 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003278
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003279 S.EmitOwnedPtr(TUDecl);
3280
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003281 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003282}
3283
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003284
3285ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3286 FileManager &FMgr) {
3287 // Check if the file is of the proper length.
3288 if (Buffer.getBufferSize() & 0x3) {
3289 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3290 return 0;
3291 }
3292
3293 // Create the bitstream reader.
3294 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3295 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3296
3297 if (Stream.Read(8) != 'B' ||
3298 Stream.Read(8) != 'C' ||
3299 Stream.Read(4) != 0xC ||
3300 Stream.Read(4) != 0xF ||
3301 Stream.Read(4) != 0xE ||
3302 Stream.Read(4) != 0x0) {
3303 // FIXME: Provide diagnostic.
3304 return NULL;
3305 }
3306
3307 // Create the deserializer.
3308 llvm::Deserializer Dezr(Stream);
3309
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003310 // ===---------------------------------------------------===/
3311 // Deserialize the "Translation Unit" metadata.
3312 // ===---------------------------------------------------===/
3313
3314 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3315 // (which will appear earlier) and record its location.
3316
3317 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3318 assert (FoundBlock);
3319
3320 llvm::Deserializer::Location ASTContextBlockLoc =
3321 Dezr.getCurrentBlockLocation();
3322
3323 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3324 assert (FoundBlock);
3325
3326 // Read the SourceManager.
3327 SourceManager::CreateAndRegister(Dezr, FMgr);
3328
3329 { // Read the TargetInfo.
3330 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3331 char* triple = Dezr.ReadCStr(NULL,0,true);
3332 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3333 delete [] triple;
3334 }
3335
3336 // For Selectors, we must read the identifier table first because the
3337 // SelectorTable depends on the identifiers being already deserialized.
3338 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3339 Dezr.SkipBlock();
3340
3341 // Read the identifier table.
3342 IdentifierTable::CreateAndRegister(Dezr);
3343
3344 // Now jump back and read the selectors.
3345 Dezr.JumpTo(SelectorBlkLoc);
3346 SelectorTable::CreateAndRegister(Dezr);
3347
3348 // Now jump back to ASTContextBlock and read the ASTContext.
3349 Dezr.JumpTo(ASTContextBlockLoc);
3350 return Dezr.ReadOwnedPtr<ASTContext>();
3351}
3352
Ted Kremenekacba3612007-11-13 00:25:37 +00003353ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00003354
3355 // Read the language options.
3356 LangOptions LOpts;
3357 LOpts.Read(D);
3358
Ted Kremenek68228a92007-10-31 22:44:07 +00003359 SourceManager &SM = D.ReadRef<SourceManager>();
3360 TargetInfo &t = D.ReadRef<TargetInfo>();
3361 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3362 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00003363
Ted Kremenek68228a92007-10-31 22:44:07 +00003364 unsigned size_reserve = D.ReadInt();
3365
Douglas Gregor24afd4a2008-11-17 14:58:09 +00003366 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3367 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00003368
Ted Kremenek034a78c2007-11-13 22:02:55 +00003369 for (unsigned i = 0; i < size_reserve; ++i)
3370 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00003371
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003372 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3373
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003374 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00003375
3376 return A;
3377}
Douglas Gregorc34897d2009-04-09 22:27:44 +00003378
3379ExternalASTSource::~ExternalASTSource() { }
3380
3381void ExternalASTSource::PrintStats() { }