blob: 7da1bf080c4fa0e6b3407689d1521e404b941b3d [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();
Chris Lattner911b8672009-03-13 22:38:49 +000044 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.NoBuiltin);
Daniel Dunbarde300732008-08-11 04:54:23 +000045 TUDecl = TranslationUnitDecl::Create(*this);
46}
47
Chris Lattner4b009652007-07-25 00:24:17 +000048ASTContext::~ASTContext() {
49 // Deallocate all the types.
50 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000051 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000052 Types.pop_back();
53 }
Eli Friedman65489b72008-05-27 03:08:09 +000054
Nuno Lopes355a8682008-12-17 22:30:25 +000055 {
56 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
57 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
58 while (I != E) {
59 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
60 delete R;
61 }
62 }
63
64 {
65 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
66 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
67 while (I != E) {
68 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
69 delete R;
70 }
71 }
72
73 {
Chris Lattner608c1e32009-03-31 09:24:30 +000074 llvm::DenseMap<const ObjCInterfaceDecl*, RecordDecl*>::iterator
Nuno Lopes355a8682008-12-17 22:30:25 +000075 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
76 while (I != E) {
Chris Lattner608c1e32009-03-31 09:24:30 +000077 RecordDecl *R = (I++)->second;
Nuno Lopes355a8682008-12-17 22:30:25 +000078 R->Destroy(*this);
79 }
80 }
81
Douglas Gregor1e589cc2009-03-26 23:50:42 +000082 // Destroy nested-name-specifiers.
Douglas Gregor3c4eae52009-03-27 23:54:10 +000083 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
84 NNS = NestedNameSpecifiers.begin(),
85 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregorbccd97c2009-03-27 23:25:45 +000086 NNS != NNSEnd;
Douglas Gregor3c4eae52009-03-27 23:54:10 +000087 /* Increment in loop */)
88 (*NNS++).Destroy(*this);
Douglas Gregor1e589cc2009-03-26 23:50:42 +000089
90 if (GlobalNestedNameSpecifier)
91 GlobalNestedNameSpecifier->Destroy(*this);
92
Eli Friedman65489b72008-05-27 03:08:09 +000093 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000094}
95
Douglas Gregorc34897d2009-04-09 22:27:44 +000096void
97ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
98 ExternalSource.reset(Source.take());
99}
100
Chris Lattner4b009652007-07-25 00:24:17 +0000101void ASTContext::PrintStats() const {
102 fprintf(stderr, "*** AST Context Stats:\n");
103 fprintf(stderr, " %d types total.\n", (int)Types.size());
104 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +0000105 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000106 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
107 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
108
Chris Lattner4b009652007-07-25 00:24:17 +0000109 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000110 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
111 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000112 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000113 unsigned NumExtQual = 0;
114
Chris Lattner4b009652007-07-25 00:24:17 +0000115 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
116 Type *T = Types[i];
117 if (isa<BuiltinType>(T))
118 ++NumBuiltin;
119 else if (isa<PointerType>(T))
120 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +0000121 else if (isa<BlockPointerType>(T))
122 ++NumBlockPointer;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000123 else if (isa<LValueReferenceType>(T))
124 ++NumLValueReference;
125 else if (isa<RValueReferenceType>(T))
126 ++NumRValueReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000127 else if (isa<MemberPointerType>(T))
128 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000129 else if (isa<ComplexType>(T))
130 ++NumComplex;
131 else if (isa<ArrayType>(T))
132 ++NumArray;
133 else if (isa<VectorType>(T))
134 ++NumVector;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000135 else if (isa<FunctionNoProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000136 ++NumFunctionNP;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000137 else if (isa<FunctionProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000138 ++NumFunctionP;
139 else if (isa<TypedefType>(T))
140 ++NumTypeName;
141 else if (TagType *TT = dyn_cast<TagType>(T)) {
142 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000143 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000144 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000145 case TagDecl::TK_struct: ++NumTagStruct; break;
146 case TagDecl::TK_union: ++NumTagUnion; break;
147 case TagDecl::TK_class: ++NumTagClass; break;
148 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000149 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000150 } else if (isa<ObjCInterfaceType>(T))
151 ++NumObjCInterfaces;
152 else if (isa<ObjCQualifiedInterfaceType>(T))
153 ++NumObjCQualifiedInterfaces;
154 else if (isa<ObjCQualifiedIdType>(T))
155 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000156 else if (isa<TypeOfType>(T))
157 ++NumTypeOfTypes;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000158 else if (isa<TypeOfExprType>(T))
159 ++NumTypeOfExprTypes;
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000160 else if (isa<ExtQualType>(T))
161 ++NumExtQual;
Steve Naroff948fd372007-09-17 14:16:13 +0000162 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000163 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000164 assert(0 && "Unknown type!");
165 }
166 }
167
168 fprintf(stderr, " %d builtin types\n", NumBuiltin);
169 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000170 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000171 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
172 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000173 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000174 fprintf(stderr, " %d complex types\n", NumComplex);
175 fprintf(stderr, " %d array types\n", NumArray);
176 fprintf(stderr, " %d vector types\n", NumVector);
177 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
178 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
179 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
180 fprintf(stderr, " %d tagged types\n", NumTagged);
181 fprintf(stderr, " %d struct types\n", NumTagStruct);
182 fprintf(stderr, " %d union types\n", NumTagUnion);
183 fprintf(stderr, " %d class types\n", NumTagClass);
184 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000185 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000186 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000187 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000188 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000189 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000190 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000191 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000192 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000193
Chris Lattner4b009652007-07-25 00:24:17 +0000194 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
195 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
196 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlce6fff02009-03-16 23:22:08 +0000197 NumLValueReference*sizeof(LValueReferenceType)+
198 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000199 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor4fa58902009-02-26 23:50:07 +0000200 NumFunctionP*sizeof(FunctionProtoType)+
201 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroffe0430632008-05-21 15:59:22 +0000202 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000203 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
204 NumExtQual*sizeof(ExtQualType)));
Douglas Gregorc34897d2009-04-09 22:27:44 +0000205
206 if (ExternalSource.get()) {
207 fprintf(stderr, "\n");
208 ExternalSource->PrintStats();
209 }
Chris Lattner4b009652007-07-25 00:24:17 +0000210}
211
212
213void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000214 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000215}
216
Chris Lattner4b009652007-07-25 00:24:17 +0000217void ASTContext::InitBuiltinTypes() {
218 assert(VoidTy.isNull() && "Context reinitialized?");
219
220 // C99 6.2.5p19.
221 InitBuiltinType(VoidTy, BuiltinType::Void);
222
223 // C99 6.2.5p2.
224 InitBuiltinType(BoolTy, BuiltinType::Bool);
225 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000226 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000227 InitBuiltinType(CharTy, BuiltinType::Char_S);
228 else
229 InitBuiltinType(CharTy, BuiltinType::Char_U);
230 // C99 6.2.5p4.
231 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
232 InitBuiltinType(ShortTy, BuiltinType::Short);
233 InitBuiltinType(IntTy, BuiltinType::Int);
234 InitBuiltinType(LongTy, BuiltinType::Long);
235 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
236
237 // C99 6.2.5p6.
238 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
239 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
240 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
241 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
242 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
243
244 // C99 6.2.5p10.
245 InitBuiltinType(FloatTy, BuiltinType::Float);
246 InitBuiltinType(DoubleTy, BuiltinType::Double);
247 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000248
Chris Lattnere1dafe72009-02-26 23:43:47 +0000249 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
250 InitBuiltinType(WCharTy, BuiltinType::WChar);
251 else // C99
252 WCharTy = getFromTargetType(Target.getWCharType());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000253
Douglas Gregord2baafd2008-10-21 16:13:35 +0000254 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000255 InitBuiltinType(OverloadTy, BuiltinType::Overload);
256
257 // Placeholder type for type-dependent expressions whose type is
258 // completely unknown. No code should ever check a type against
259 // DependentTy and users should never see it; however, it is here to
260 // help diagnose failures to properly check for type-dependent
261 // expressions.
262 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000263
Chris Lattner4b009652007-07-25 00:24:17 +0000264 // C99 6.2.5p11.
265 FloatComplexTy = getComplexType(FloatTy);
266 DoubleComplexTy = getComplexType(DoubleTy);
267 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000268
Steve Naroff9d12c902007-10-15 14:41:52 +0000269 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000270 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000271 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000272 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000273 ClassStructType = 0;
274
Ted Kremenek42730c52008-01-07 19:49:32 +0000275 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000276
277 // void * type
278 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000279}
280
281//===----------------------------------------------------------------------===//
282// Type Sizing and Analysis
283//===----------------------------------------------------------------------===//
284
Chris Lattner2a674dc2008-06-30 18:32:54 +0000285/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
286/// scalar floating point type.
287const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
288 const BuiltinType *BT = T->getAsBuiltinType();
289 assert(BT && "Not a floating point type!");
290 switch (BT->getKind()) {
291 default: assert(0 && "Not a floating point type!");
292 case BuiltinType::Float: return Target.getFloatFormat();
293 case BuiltinType::Double: return Target.getDoubleFormat();
294 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
295 }
296}
297
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000298/// getDeclAlign - Return a conservative estimate of the alignment of the
299/// specified decl. Note that bitfields do not have a valid alignment, so
300/// this method will assert on them.
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +0000301unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman0ee57322009-02-22 02:56:25 +0000302 unsigned Align = Target.getCharWidth();
303
304 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
305 Align = std::max(Align, AA->getAlignment());
306
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000307 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
308 QualType T = VD->getType();
309 // Incomplete or function types default to 1.
Eli Friedman0ee57322009-02-22 02:56:25 +0000310 if (!T->isIncompleteType() && !T->isFunctionType()) {
311 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
312 T = cast<ArrayType>(T)->getElementType();
313
314 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
315 }
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000316 }
Eli Friedman0ee57322009-02-22 02:56:25 +0000317
318 return Align / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000319}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000320
Chris Lattner4b009652007-07-25 00:24:17 +0000321/// getTypeSize - Return the size of the specified type, in bits. This method
322/// does not work on incomplete types.
323std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000324ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000325 T = getCanonicalType(T);
Mike Stump44d1f402009-02-27 18:32:39 +0000326 uint64_t Width=0;
327 unsigned Align=8;
Chris Lattner4b009652007-07-25 00:24:17 +0000328 switch (T->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000329#define TYPE(Class, Base)
330#define ABSTRACT_TYPE(Class, Base)
331#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
332#define DEPENDENT_TYPE(Class, Base) case Type::Class:
333#include "clang/AST/TypeNodes.def"
334 assert(false && "Should not see non-canonical or dependent types");
335 break;
336
Chris Lattner4b009652007-07-25 00:24:17 +0000337 case Type::FunctionNoProto:
338 case Type::FunctionProto:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000339 case Type::IncompleteArray:
Chris Lattner4b009652007-07-25 00:24:17 +0000340 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000341 case Type::VariableArray:
342 assert(0 && "VLAs not implemented yet!");
343 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000344 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000345
Chris Lattner8cd0e932008-03-05 18:54:05 +0000346 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000347 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000348 Align = EltInfo.second;
349 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000350 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000351 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000352 case Type::Vector: {
353 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000354 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000355 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000356 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000357 // If the alignment is not a power of 2, round up to the next power of 2.
358 // This happens for non-power-of-2 length vectors.
359 // FIXME: this should probably be a target property.
360 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000361 break;
362 }
363
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000364 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000365 switch (cast<BuiltinType>(T)->getKind()) {
366 default: assert(0 && "Unknown builtin type!");
367 case BuiltinType::Void:
368 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000369 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000370 Width = Target.getBoolWidth();
371 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000372 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000373 case BuiltinType::Char_S:
374 case BuiltinType::Char_U:
375 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000376 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000377 Width = Target.getCharWidth();
378 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000379 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000380 case BuiltinType::WChar:
381 Width = Target.getWCharWidth();
382 Align = Target.getWCharAlign();
383 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000384 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000385 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000386 Width = Target.getShortWidth();
387 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000388 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000389 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000390 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000391 Width = Target.getIntWidth();
392 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000393 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000394 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000395 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000396 Width = Target.getLongWidth();
397 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000398 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000399 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000400 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000401 Width = Target.getLongLongWidth();
402 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000403 break;
404 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000405 Width = Target.getFloatWidth();
406 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000407 break;
408 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000409 Width = Target.getDoubleWidth();
410 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000411 break;
412 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000413 Width = Target.getLongDoubleWidth();
414 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000415 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000416 }
417 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000418 case Type::FixedWidthInt:
419 // FIXME: This isn't precisely correct; the width/alignment should depend
420 // on the available types for the target
421 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattnere9174982009-02-15 21:20:13 +0000422 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000423 Align = Width;
424 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000425 case Type::ExtQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000426 // FIXME: Pointers into different addr spaces could have different sizes and
427 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000428 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000429 case Type::ObjCQualifiedId:
Eli Friedman2f6d70d2009-02-22 04:02:33 +0000430 case Type::ObjCQualifiedClass:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000431 case Type::ObjCQualifiedInterface:
Chris Lattner1d78a862008-04-07 07:01:58 +0000432 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000433 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000434 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000435 case Type::BlockPointer: {
436 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
437 Width = Target.getPointerWidth(AS);
438 Align = Target.getPointerAlign(AS);
439 break;
440 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000441 case Type::Pointer: {
442 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000443 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000444 Align = Target.getPointerAlign(AS);
445 break;
446 }
Sebastian Redlce6fff02009-03-16 23:22:08 +0000447 case Type::LValueReference:
448 case Type::RValueReference:
Chris Lattner4b009652007-07-25 00:24:17 +0000449 // "When applied to a reference or a reference type, the result is the size
450 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000451 // FIXME: This is wrong for struct layout: a reference in a struct has
452 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000453 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000454 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000455 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-01-24 21:16:55 +0000456 // the GCC ABI, where pointers to data are one pointer large, pointers to
457 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl18cffee2009-01-24 23:29:36 +0000458 // other compilers too, we need to delegate this completely to TargetInfo
459 // or some ABI abstraction layer.
Sebastian Redl75555032009-01-24 21:16:55 +0000460 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
461 unsigned AS = Pointee.getAddressSpace();
462 Width = Target.getPointerWidth(AS);
463 if (Pointee->isFunctionType())
464 Width *= 2;
465 Align = Target.getPointerAlign(AS);
466 // GCC aligns at single pointer width.
467 }
Chris Lattner4b009652007-07-25 00:24:17 +0000468 case Type::Complex: {
469 // Complex types have the same alignment as their elements, but twice the
470 // size.
471 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000472 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000473 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000474 Align = EltInfo.second;
475 break;
476 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000477 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000478 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000479 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
480 Width = Layout.getSize();
481 Align = Layout.getAlignment();
482 break;
483 }
Douglas Gregor4fa58902009-02-26 23:50:07 +0000484 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000485 case Type::Enum: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000486 const TagType *TT = cast<TagType>(T);
487
488 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000489 Width = 1;
490 Align = 1;
491 break;
492 }
493
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000494 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000495 return getTypeInfo(ET->getDecl()->getIntegerType());
496
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000497 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000498 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
499 Width = Layout.getSize();
500 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000501 break;
502 }
Douglas Gregordd13e842009-03-30 22:58:21 +0000503
504 case Type::TemplateSpecialization:
505 assert(false && "Dependent types have no size");
506 break;
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000507 }
Chris Lattner4b009652007-07-25 00:24:17 +0000508
509 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000510 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000511}
512
Chris Lattner83165b52009-01-27 18:08:34 +0000513/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
514/// type for the current target in bits. This can be different than the ABI
515/// alignment in cases where it is beneficial for performance to overalign
516/// a data type.
517unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
518 unsigned ABIAlign = getTypeAlign(T);
519
520 // Doubles should be naturally aligned if possible.
Daniel Dunbarc61a8002009-02-18 19:59:32 +0000521 if (T->isSpecificBuiltinType(BuiltinType::Double))
522 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000523
524 return ABIAlign;
525}
526
527
Devang Patelbfe323c2008-06-04 21:22:16 +0000528/// LayoutField - Field layout.
529void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000530 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000531 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000532 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000533 uint64_t FieldOffset = IsUnion ? 0 : Size;
534 uint64_t FieldSize;
535 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000536
537 // FIXME: Should this override struct packing? Probably we want to
538 // take the minimum?
539 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
540 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000541
542 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
543 // TODO: Need to check this algorithm on other targets!
544 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000545 FieldSize =
546 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000547
548 std::pair<uint64_t, unsigned> FieldInfo =
549 Context.getTypeInfo(FD->getType());
550 uint64_t TypeSize = FieldInfo.first;
551
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000552 // Determine the alignment of this bitfield. The packing
553 // attributes define a maximum and the alignment attribute defines
554 // a minimum.
555 // FIXME: What is the right behavior when the specified alignment
556 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000557 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000558 if (FieldPacking)
559 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000560 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
561 FieldAlign = std::max(FieldAlign, AA->getAlignment());
562
563 // Check if we need to add padding to give the field the correct
564 // alignment.
565 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
566 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
567
568 // Padding members don't affect overall alignment
569 if (!FD->getIdentifier())
570 FieldAlign = 1;
571 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000572 if (FD->getType()->isIncompleteArrayType()) {
573 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000574 // query getTypeInfo about these, so we figure it out here.
575 // Flexible array members don't have any size, but they
576 // have to be aligned appropriately for their element type.
577 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000578 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000579 FieldAlign = Context.getTypeAlign(ATy->getElementType());
580 } else {
581 std::pair<uint64_t, unsigned> FieldInfo =
582 Context.getTypeInfo(FD->getType());
583 FieldSize = FieldInfo.first;
584 FieldAlign = FieldInfo.second;
585 }
586
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000587 // Determine the alignment of this bitfield. The packing
588 // attributes define a maximum and the alignment attribute defines
589 // a minimum. Additionally, the packing alignment must be at least
590 // a byte for non-bitfields.
591 //
592 // FIXME: What is the right behavior when the specified alignment
593 // is smaller than the specified packing?
594 if (FieldPacking)
595 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000596 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
597 FieldAlign = std::max(FieldAlign, AA->getAlignment());
598
599 // Round up the current record size to the field's alignment boundary.
600 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
601 }
602
603 // Place this field at the current location.
604 FieldOffsets[FieldNo] = FieldOffset;
605
606 // Reserve space for this field.
607 if (IsUnion) {
608 Size = std::max(Size, FieldSize);
609 } else {
610 Size = FieldOffset + FieldSize;
611 }
612
613 // Remember max struct/class alignment.
614 Alignment = std::max(Alignment, FieldAlign);
615}
616
Fariborz Jahaniana2c97df2009-03-05 20:08:48 +0000617void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000618 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000619 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
620 if (SuperClass)
621 CollectObjCIvars(SuperClass, Fields);
622 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
623 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000624 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000625 if (!IVDecl->isInvalidDecl())
626 Fields.push_back(cast<FieldDecl>(IVDecl));
627 }
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000628 // look into properties.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000629 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
630 E = OI->prop_end(*this); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000631 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000632 Fields.push_back(cast<FieldDecl>(IV));
633 }
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000634}
635
636/// addRecordToClass - produces record info. for the class for its
637/// ivars and all those inherited.
638///
Chris Lattner9329cf52009-03-31 08:48:01 +0000639const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) {
Chris Lattner608c1e32009-03-31 09:24:30 +0000640 RecordDecl *&RD = ASTRecordForInterface[D];
641 if (RD) {
642 // If we have a record decl already and it is either a definition or if 'D'
643 // is still a forward declaration, return it.
644 if (RD->isDefinition() || D->isForwardDecl())
645 return RD;
646 }
647
648 // If D is a forward declaration, then just make a forward struct decl.
649 if (D->isForwardDecl())
650 return RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
651 D->getLocation(),
652 D->getIdentifier());
Chris Lattner9329cf52009-03-31 08:48:01 +0000653
654 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000655 CollectObjCIvars(D, RecFields);
Chris Lattner608c1e32009-03-31 09:24:30 +0000656
657 if (RD == 0)
658 RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(),
659 D->getIdentifier());
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000660 /// FIXME! Can do collection of ivars and adding to the record while
661 /// doing it.
Chris Lattner41b780c2009-03-31 08:58:42 +0000662 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000663 RD->addDecl(*this,
664 FieldDecl::Create(*this, RD,
Chris Lattner608c1e32009-03-31 09:24:30 +0000665 RecFields[i]->getLocation(),
666 RecFields[i]->getIdentifier(),
667 RecFields[i]->getType(),
668 RecFields[i]->getBitWidth(), false));
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000669 }
Chris Lattner9329cf52009-03-31 08:48:01 +0000670
Chris Lattner608c1e32009-03-31 09:24:30 +0000671 RD->completeDefinition(*this);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000672 return RD;
673}
Devang Patel4b6bf702008-06-04 21:54:36 +0000674
Fariborz Jahanianea944842008-12-18 17:29:46 +0000675/// setFieldDecl - maps a field for the given Ivar reference node.
676//
677void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
678 const ObjCIvarDecl *Ivar,
679 const ObjCIvarRefExpr *MRef) {
Chris Lattner04f4db72009-03-31 08:31:13 +0000680 ASTFieldForIvarRef[MRef] = OI->lookupFieldDeclForIvar(*this, Ivar);
Fariborz Jahanianea944842008-12-18 17:29:46 +0000681}
682
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000683/// getASTObjcInterfaceLayout - Get or compute information about the layout of
684/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000685/// position information.
686const ASTRecordLayout &
687ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
688 // Look up this layout, if already laid out, return what we have.
689 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
690 if (Entry) return *Entry;
691
692 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
693 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000694 ASTRecordLayout *NewEntry = NULL;
Fariborz Jahanianba50c332009-04-08 21:54:52 +0000695 // FIXME. Add actual count of synthesized ivars, instead of count
696 // of properties which is the upper bound, but is safe.
Daniel Dunbar998510f2009-04-08 20:18:15 +0000697 unsigned FieldCount =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000698 D->ivar_size() + std::distance(D->prop_begin(*this), D->prop_end(*this));
Devang Patel8682d882008-06-06 02:14:01 +0000699 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
700 FieldCount++;
701 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
702 unsigned Alignment = SL.getAlignment();
703 uint64_t Size = SL.getSize();
704 NewEntry = new ASTRecordLayout(Size, Alignment);
705 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000706 // Super class is at the beginning of the layout.
707 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000708 } else {
709 NewEntry = new ASTRecordLayout();
710 NewEntry->InitializeLayout(FieldCount);
711 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000712 Entry = NewEntry;
713
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000714 unsigned StructPacking = 0;
715 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
716 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000717
718 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
719 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
720 AA->getAlignment()));
721
722 // Layout each ivar sequentially.
723 unsigned i = 0;
724 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
725 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
726 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000727 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000728 }
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000729 // Also synthesized ivars
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000730 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
731 E = D->prop_end(*this); I != E; ++I) {
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000732 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
733 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
734 }
Fariborz Jahanian84c45692009-04-01 19:37:34 +0000735
Devang Patel4b6bf702008-06-04 21:54:36 +0000736 // Finally, round the size of the total struct up to the alignment of the
737 // struct itself.
738 NewEntry->FinalizeLayout();
739 return *NewEntry;
740}
741
Devang Patel7a78e432007-11-01 19:11:01 +0000742/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000743/// specified record (struct/union/class), which indicates its size and field
744/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000745const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000746 D = D->getDefinition(*this);
747 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000748
Chris Lattner4b009652007-07-25 00:24:17 +0000749 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000750 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000751 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000752
Devang Patel7a78e432007-11-01 19:11:01 +0000753 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
754 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
755 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000756 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000757
Douglas Gregor39677622008-12-11 20:41:00 +0000758 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000759 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
760 D->field_end(*this)));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000761 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000762
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000763 unsigned StructPacking = 0;
764 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
765 StructPacking = PA->getAlignment();
766
Eli Friedman5949a022008-05-30 09:31:38 +0000767 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000768 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
769 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000770
Eli Friedman5949a022008-05-30 09:31:38 +0000771 // Layout each field, for now, just sequentially, respecting alignment. In
772 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000773 unsigned FieldIdx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000774 for (RecordDecl::field_iterator Field = D->field_begin(*this),
775 FieldEnd = D->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000776 Field != FieldEnd; (void)++Field, ++FieldIdx)
777 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000778
779 // Finally, round the size of the total struct up to the alignment of the
780 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000781 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000782 return *NewEntry;
783}
784
Chris Lattner4b009652007-07-25 00:24:17 +0000785//===----------------------------------------------------------------------===//
786// Type creation/memoization methods
787//===----------------------------------------------------------------------===//
788
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000789QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000790 QualType CanT = getCanonicalType(T);
791 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000792 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000793
794 // If we are composing extended qualifiers together, merge together into one
795 // ExtQualType node.
796 unsigned CVRQuals = T.getCVRQualifiers();
797 QualType::GCAttrTypes GCAttr = QualType::GCNone;
798 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000799
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000800 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
801 // If this type already has an address space specified, it cannot get
802 // another one.
803 assert(EQT->getAddressSpace() == 0 &&
804 "Type cannot be in multiple addr spaces!");
805 GCAttr = EQT->getObjCGCAttr();
806 TypeNode = EQT->getBaseType();
807 }
Chris Lattner35fef522008-02-20 20:55:12 +0000808
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000809 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000810 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000811 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000812 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000813 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000814 return QualType(EXTQy, CVRQuals);
815
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000816 // If the base type isn't canonical, this won't be a canonical type either,
817 // so fill in the canonical type field.
818 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000819 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000820 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000821
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000822 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000823 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000824 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000825 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000826 ExtQualType *New =
827 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000828 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000829 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000830 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000831}
832
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000833QualType ASTContext::getObjCGCQualType(QualType T,
834 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000835 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000836 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000837 return T;
838
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000839 // If we are composing extended qualifiers together, merge together into one
840 // ExtQualType node.
841 unsigned CVRQuals = T.getCVRQualifiers();
842 Type *TypeNode = T.getTypePtr();
843 unsigned AddressSpace = 0;
844
845 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
846 // If this type already has an address space specified, it cannot get
847 // another one.
848 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
849 "Type cannot be in multiple addr spaces!");
850 AddressSpace = EQT->getAddressSpace();
851 TypeNode = EQT->getBaseType();
852 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000853
854 // Check if we've already instantiated an gc qual'd type of this type.
855 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000856 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000857 void *InsertPos = 0;
858 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000859 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000860
861 // If the base type isn't canonical, this won't be a canonical type either,
862 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +0000863 // FIXME: Isn't this also not canonical if the base type is a array
864 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000865 QualType Canonical;
866 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000867 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000868
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000869 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000870 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
871 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
872 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000873 ExtQualType *New =
874 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000875 ExtQualTypes.InsertNode(New, InsertPos);
876 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000877 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000878}
Chris Lattner4b009652007-07-25 00:24:17 +0000879
880/// getComplexType - Return the uniqued reference to the type for a complex
881/// number with the specified element type.
882QualType ASTContext::getComplexType(QualType T) {
883 // Unique pointers, to guarantee there is only one pointer of a particular
884 // structure.
885 llvm::FoldingSetNodeID ID;
886 ComplexType::Profile(ID, T);
887
888 void *InsertPos = 0;
889 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
890 return QualType(CT, 0);
891
892 // If the pointee type isn't canonical, this won't be a canonical type either,
893 // so fill in the canonical type field.
894 QualType Canonical;
895 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000896 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000897
898 // Get the new insert position for the node we care about.
899 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000900 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000901 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000902 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000903 Types.push_back(New);
904 ComplexTypes.InsertNode(New, InsertPos);
905 return QualType(New, 0);
906}
907
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000908QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
909 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
910 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
911 FixedWidthIntType *&Entry = Map[Width];
912 if (!Entry)
913 Entry = new FixedWidthIntType(Width, Signed);
914 return QualType(Entry, 0);
915}
Chris Lattner4b009652007-07-25 00:24:17 +0000916
917/// getPointerType - Return the uniqued reference to the type for a pointer to
918/// the specified type.
919QualType ASTContext::getPointerType(QualType T) {
920 // Unique pointers, to guarantee there is only one pointer of a particular
921 // structure.
922 llvm::FoldingSetNodeID ID;
923 PointerType::Profile(ID, T);
924
925 void *InsertPos = 0;
926 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
927 return QualType(PT, 0);
928
929 // If the pointee type isn't canonical, this won't be a canonical type either,
930 // so fill in the canonical type field.
931 QualType Canonical;
932 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000933 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000934
935 // Get the new insert position for the node we care about.
936 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000937 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000938 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000939 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000940 Types.push_back(New);
941 PointerTypes.InsertNode(New, InsertPos);
942 return QualType(New, 0);
943}
944
Steve Naroff7aa54752008-08-27 16:04:49 +0000945/// getBlockPointerType - Return the uniqued reference to the type for
946/// a pointer to the specified block.
947QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000948 assert(T->isFunctionType() && "block of function types only");
949 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000950 // structure.
951 llvm::FoldingSetNodeID ID;
952 BlockPointerType::Profile(ID, T);
953
954 void *InsertPos = 0;
955 if (BlockPointerType *PT =
956 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
957 return QualType(PT, 0);
958
Steve Narofffd5b19d2008-08-28 19:20:44 +0000959 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000960 // type either so fill in the canonical type field.
961 QualType Canonical;
962 if (!T->isCanonical()) {
963 Canonical = getBlockPointerType(getCanonicalType(T));
964
965 // Get the new insert position for the node we care about.
966 BlockPointerType *NewIP =
967 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000968 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000969 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000970 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000971 Types.push_back(New);
972 BlockPointerTypes.InsertNode(New, InsertPos);
973 return QualType(New, 0);
974}
975
Sebastian Redlce6fff02009-03-16 23:22:08 +0000976/// getLValueReferenceType - Return the uniqued reference to the type for an
977/// lvalue reference to the specified type.
978QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +0000979 // Unique pointers, to guarantee there is only one pointer of a particular
980 // structure.
981 llvm::FoldingSetNodeID ID;
982 ReferenceType::Profile(ID, T);
983
984 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000985 if (LValueReferenceType *RT =
986 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000987 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000988
Chris Lattner4b009652007-07-25 00:24:17 +0000989 // If the referencee type isn't canonical, this won't be a canonical type
990 // either, so fill in the canonical type field.
991 QualType Canonical;
992 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000993 Canonical = getLValueReferenceType(getCanonicalType(T));
994
Chris Lattner4b009652007-07-25 00:24:17 +0000995 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000996 LValueReferenceType *NewIP =
997 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000998 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000999 }
1000
Sebastian Redlce6fff02009-03-16 23:22:08 +00001001 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001002 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001003 LValueReferenceTypes.InsertNode(New, InsertPos);
1004 return QualType(New, 0);
1005}
1006
1007/// getRValueReferenceType - Return the uniqued reference to the type for an
1008/// rvalue reference to the specified type.
1009QualType ASTContext::getRValueReferenceType(QualType T) {
1010 // Unique pointers, to guarantee there is only one pointer of a particular
1011 // structure.
1012 llvm::FoldingSetNodeID ID;
1013 ReferenceType::Profile(ID, T);
1014
1015 void *InsertPos = 0;
1016 if (RValueReferenceType *RT =
1017 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1018 return QualType(RT, 0);
1019
1020 // If the referencee type isn't canonical, this won't be a canonical type
1021 // either, so fill in the canonical type field.
1022 QualType Canonical;
1023 if (!T->isCanonical()) {
1024 Canonical = getRValueReferenceType(getCanonicalType(T));
1025
1026 // Get the new insert position for the node we care about.
1027 RValueReferenceType *NewIP =
1028 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1029 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1030 }
1031
1032 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1033 Types.push_back(New);
1034 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001035 return QualType(New, 0);
1036}
1037
Sebastian Redl75555032009-01-24 21:16:55 +00001038/// getMemberPointerType - Return the uniqued reference to the type for a
1039/// member pointer to the specified type, in the specified class.
1040QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1041{
1042 // Unique pointers, to guarantee there is only one pointer of a particular
1043 // structure.
1044 llvm::FoldingSetNodeID ID;
1045 MemberPointerType::Profile(ID, T, Cls);
1046
1047 void *InsertPos = 0;
1048 if (MemberPointerType *PT =
1049 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1050 return QualType(PT, 0);
1051
1052 // If the pointee or class type isn't canonical, this won't be a canonical
1053 // type either, so fill in the canonical type field.
1054 QualType Canonical;
1055 if (!T->isCanonical()) {
1056 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1057
1058 // Get the new insert position for the node we care about.
1059 MemberPointerType *NewIP =
1060 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1061 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1062 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001063 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001064 Types.push_back(New);
1065 MemberPointerTypes.InsertNode(New, InsertPos);
1066 return QualType(New, 0);
1067}
1068
Steve Naroff83c13012007-08-30 01:06:46 +00001069/// getConstantArrayType - Return the unique reference to the type for an
1070/// array of the specified element type.
1071QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +00001072 const llvm::APInt &ArySize,
1073 ArrayType::ArraySizeModifier ASM,
1074 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001075 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001076 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001077
1078 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001079 if (ConstantArrayType *ATP =
1080 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001081 return QualType(ATP, 0);
1082
1083 // If the element type isn't canonical, this won't be a canonical type either,
1084 // so fill in the canonical type field.
1085 QualType Canonical;
1086 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001087 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001088 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001089 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001090 ConstantArrayType *NewIP =
1091 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001092 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001093 }
1094
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001095 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001096 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001097 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001098 Types.push_back(New);
1099 return QualType(New, 0);
1100}
1101
Steve Naroffe2579e32007-08-30 18:14:25 +00001102/// getVariableArrayType - Returns a non-unique reference to the type for a
1103/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +00001104QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1105 ArrayType::ArraySizeModifier ASM,
1106 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001107 // Since we don't unique expressions, it isn't possible to unique VLA's
1108 // that have an expression provided for their size.
1109
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001110 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001111 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001112
1113 VariableArrayTypes.push_back(New);
1114 Types.push_back(New);
1115 return QualType(New, 0);
1116}
1117
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001118/// getDependentSizedArrayType - Returns a non-unique reference to
1119/// the type for a dependently-sized array of the specified element
1120/// type. FIXME: We will need these to be uniqued, or at least
1121/// comparable, at some point.
1122QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1123 ArrayType::ArraySizeModifier ASM,
1124 unsigned EltTypeQuals) {
1125 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1126 "Size must be type- or value-dependent!");
1127
1128 // Since we don't unique expressions, it isn't possible to unique
1129 // dependently-sized array types.
1130
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001131 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001132 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1133 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001134
1135 DependentSizedArrayTypes.push_back(New);
1136 Types.push_back(New);
1137 return QualType(New, 0);
1138}
1139
Eli Friedman8ff07782008-02-15 18:16:39 +00001140QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1141 ArrayType::ArraySizeModifier ASM,
1142 unsigned EltTypeQuals) {
1143 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001144 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001145
1146 void *InsertPos = 0;
1147 if (IncompleteArrayType *ATP =
1148 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1149 return QualType(ATP, 0);
1150
1151 // If the element type isn't canonical, this won't be a canonical type
1152 // either, so fill in the canonical type field.
1153 QualType Canonical;
1154
1155 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001156 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001157 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001158
1159 // Get the new insert position for the node we care about.
1160 IncompleteArrayType *NewIP =
1161 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001162 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001163 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001164
Steve Naroff93fd2112009-01-27 22:08:43 +00001165 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001166 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001167
1168 IncompleteArrayTypes.InsertNode(New, InsertPos);
1169 Types.push_back(New);
1170 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001171}
1172
Chris Lattner4b009652007-07-25 00:24:17 +00001173/// getVectorType - Return the unique reference to a vector type of
1174/// the specified element type and size. VectorType must be a built-in type.
1175QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1176 BuiltinType *baseType;
1177
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001178 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001179 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1180
1181 // Check if we've already instantiated a vector of this type.
1182 llvm::FoldingSetNodeID ID;
1183 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1184 void *InsertPos = 0;
1185 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1186 return QualType(VTP, 0);
1187
1188 // If the element type isn't canonical, this won't be a canonical type either,
1189 // so fill in the canonical type field.
1190 QualType Canonical;
1191 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001192 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001193
1194 // Get the new insert position for the node we care about.
1195 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001196 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001197 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001198 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001199 VectorTypes.InsertNode(New, InsertPos);
1200 Types.push_back(New);
1201 return QualType(New, 0);
1202}
1203
Nate Begemanaf6ed502008-04-18 23:10:10 +00001204/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001205/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001206QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001207 BuiltinType *baseType;
1208
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001209 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001210 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001211
1212 // Check if we've already instantiated a vector of this type.
1213 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001214 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001215 void *InsertPos = 0;
1216 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1217 return QualType(VTP, 0);
1218
1219 // If the element type isn't canonical, this won't be a canonical type either,
1220 // so fill in the canonical type field.
1221 QualType Canonical;
1222 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001223 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001224
1225 // Get the new insert position for the node we care about.
1226 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001227 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001228 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001229 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001230 VectorTypes.InsertNode(New, InsertPos);
1231 Types.push_back(New);
1232 return QualType(New, 0);
1233}
1234
Douglas Gregor4fa58902009-02-26 23:50:07 +00001235/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001236///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001237QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001238 // Unique functions, to guarantee there is only one function of a particular
1239 // structure.
1240 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001241 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001242
1243 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001244 if (FunctionNoProtoType *FT =
1245 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001246 return QualType(FT, 0);
1247
1248 QualType Canonical;
1249 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001250 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001251
1252 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001253 FunctionNoProtoType *NewIP =
1254 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001255 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001256 }
1257
Douglas Gregor4fa58902009-02-26 23:50:07 +00001258 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001259 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001260 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001261 return QualType(New, 0);
1262}
1263
1264/// getFunctionType - Return a normal function type with a typed argument
1265/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001266QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001267 unsigned NumArgs, bool isVariadic,
1268 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001269 // Unique functions, to guarantee there is only one function of a particular
1270 // structure.
1271 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001272 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001273 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001274
1275 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001276 if (FunctionProtoType *FTP =
1277 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001278 return QualType(FTP, 0);
1279
1280 // Determine whether the type being created is already canonical or not.
1281 bool isCanonical = ResultTy->isCanonical();
1282 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1283 if (!ArgArray[i]->isCanonical())
1284 isCanonical = false;
1285
1286 // If this type isn't canonical, get the canonical version of it.
1287 QualType Canonical;
1288 if (!isCanonical) {
1289 llvm::SmallVector<QualType, 16> CanonicalArgs;
1290 CanonicalArgs.reserve(NumArgs);
1291 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001292 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001293
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001294 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001295 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001296 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001297
1298 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001299 FunctionProtoType *NewIP =
1300 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001301 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001302 }
1303
Douglas Gregor4fa58902009-02-26 23:50:07 +00001304 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001305 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001306 FunctionProtoType *FTP =
1307 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001308 NumArgs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001309 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001310 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001311 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001312 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001313 return QualType(FTP, 0);
1314}
1315
Douglas Gregor1d661552008-04-13 21:07:44 +00001316/// getTypeDeclType - Return the unique reference to the type for the
1317/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001318QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001319 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001320 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1321
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001322 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001323 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001324 else if (isa<TemplateTypeParmDecl>(Decl)) {
1325 assert(false && "Template type parameter types are always available.");
1326 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001327 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001328
Douglas Gregor2e047592009-02-28 01:32:25 +00001329 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001330 if (PrevDecl)
1331 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001332 else
1333 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001334 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001335 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1336 if (PrevDecl)
1337 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001338 else
1339 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001340 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001341 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001342 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001343
Ted Kremenek46a837c2008-09-05 17:16:31 +00001344 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001345 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001346}
1347
Chris Lattner4b009652007-07-25 00:24:17 +00001348/// getTypedefType - Return the unique reference to the type for the
1349/// specified typename decl.
1350QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1351 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1352
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001353 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001354 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001355 Types.push_back(Decl->TypeForDecl);
1356 return QualType(Decl->TypeForDecl, 0);
1357}
1358
Ted Kremenek42730c52008-01-07 19:49:32 +00001359/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001360/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001361QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001362 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1363
Steve Naroff93fd2112009-01-27 22:08:43 +00001364 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001365 Types.push_back(Decl->TypeForDecl);
1366 return QualType(Decl->TypeForDecl, 0);
1367}
1368
Douglas Gregora4918772009-02-05 23:33:38 +00001369/// \brief Retrieve the template type parameter type for a template
1370/// parameter with the given depth, index, and (optionally) name.
1371QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1372 IdentifierInfo *Name) {
1373 llvm::FoldingSetNodeID ID;
1374 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1375 void *InsertPos = 0;
1376 TemplateTypeParmType *TypeParm
1377 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1378
1379 if (TypeParm)
1380 return QualType(TypeParm, 0);
1381
1382 if (Name)
1383 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1384 getTemplateTypeParmType(Depth, Index));
1385 else
1386 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1387
1388 Types.push_back(TypeParm);
1389 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1390
1391 return QualType(TypeParm, 0);
1392}
1393
Douglas Gregor8e458f42009-02-09 18:46:07 +00001394QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001395ASTContext::getTemplateSpecializationType(TemplateName Template,
1396 const TemplateArgument *Args,
1397 unsigned NumArgs,
1398 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001399 if (!Canon.isNull())
1400 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001401
Douglas Gregor8e458f42009-02-09 18:46:07 +00001402 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001403 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001404
Douglas Gregor8e458f42009-02-09 18:46:07 +00001405 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001406 TemplateSpecializationType *Spec
1407 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001408
1409 if (Spec)
1410 return QualType(Spec, 0);
1411
Douglas Gregordd13e842009-03-30 22:58:21 +00001412 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001413 sizeof(TemplateArgument) * NumArgs),
1414 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001415 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001416 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001417 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001418
1419 return QualType(Spec, 0);
1420}
1421
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001422QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001423ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001424 QualType NamedType) {
1425 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001426 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001427
1428 void *InsertPos = 0;
1429 QualifiedNameType *T
1430 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1431 if (T)
1432 return QualType(T, 0);
1433
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001434 T = new (*this) QualifiedNameType(NNS, NamedType,
1435 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001436 Types.push_back(T);
1437 QualifiedNameTypes.InsertNode(T, InsertPos);
1438 return QualType(T, 0);
1439}
1440
Douglas Gregord3022602009-03-27 23:10:48 +00001441QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1442 const IdentifierInfo *Name,
1443 QualType Canon) {
1444 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1445
1446 if (Canon.isNull()) {
1447 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1448 if (CanonNNS != NNS)
1449 Canon = getTypenameType(CanonNNS, Name);
1450 }
1451
1452 llvm::FoldingSetNodeID ID;
1453 TypenameType::Profile(ID, NNS, Name);
1454
1455 void *InsertPos = 0;
1456 TypenameType *T
1457 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1458 if (T)
1459 return QualType(T, 0);
1460
1461 T = new (*this) TypenameType(NNS, Name, Canon);
1462 Types.push_back(T);
1463 TypenameTypes.InsertNode(T, InsertPos);
1464 return QualType(T, 0);
1465}
1466
Douglas Gregor77da5802009-04-01 00:28:59 +00001467QualType
1468ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1469 const TemplateSpecializationType *TemplateId,
1470 QualType Canon) {
1471 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1472
1473 if (Canon.isNull()) {
1474 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1475 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1476 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1477 const TemplateSpecializationType *CanonTemplateId
1478 = CanonType->getAsTemplateSpecializationType();
1479 assert(CanonTemplateId &&
1480 "Canonical type must also be a template specialization type");
1481 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1482 }
1483 }
1484
1485 llvm::FoldingSetNodeID ID;
1486 TypenameType::Profile(ID, NNS, TemplateId);
1487
1488 void *InsertPos = 0;
1489 TypenameType *T
1490 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1491 if (T)
1492 return QualType(T, 0);
1493
1494 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1495 Types.push_back(T);
1496 TypenameTypes.InsertNode(T, InsertPos);
1497 return QualType(T, 0);
1498}
1499
Chris Lattnere1352302008-04-07 04:56:42 +00001500/// CmpProtocolNames - Comparison predicate for sorting protocols
1501/// alphabetically.
1502static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1503 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001504 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001505}
1506
1507static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1508 unsigned &NumProtocols) {
1509 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1510
1511 // Sort protocols, keyed by name.
1512 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1513
1514 // Remove duplicates.
1515 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1516 NumProtocols = ProtocolsEnd-Protocols;
1517}
1518
1519
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001520/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1521/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001522QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1523 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001524 // Sort the protocol list alphabetically to canonicalize it.
1525 SortAndUniqueProtocols(Protocols, NumProtocols);
1526
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001527 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001528 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001529
1530 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001531 if (ObjCQualifiedInterfaceType *QT =
1532 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001533 return QualType(QT, 0);
1534
1535 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001536 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001537 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001538
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001539 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001540 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001541 return QualType(QType, 0);
1542}
1543
Chris Lattnere1352302008-04-07 04:56:42 +00001544/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1545/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001546QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001547 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001548 // Sort the protocol list alphabetically to canonicalize it.
1549 SortAndUniqueProtocols(Protocols, NumProtocols);
1550
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001551 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001552 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001553
1554 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001555 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001556 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001557 return QualType(QT, 0);
1558
1559 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001560 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001561 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001562 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001563 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001564 return QualType(QType, 0);
1565}
1566
Douglas Gregor4fa58902009-02-26 23:50:07 +00001567/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1568/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001569/// multiple declarations that refer to "typeof(x)" all contain different
1570/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1571/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001572QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001573 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001574 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001575 Types.push_back(toe);
1576 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001577}
1578
Steve Naroff0604dd92007-08-01 18:02:17 +00001579/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1580/// TypeOfType AST's. The only motivation to unique these nodes would be
1581/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1582/// an issue. This doesn't effect the type checker, since it operates
1583/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001584QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001585 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001586 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001587 Types.push_back(tot);
1588 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001589}
1590
Chris Lattner4b009652007-07-25 00:24:17 +00001591/// getTagDeclType - Return the unique reference to the type for the
1592/// specified TagDecl (struct/union/class/enum) decl.
1593QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001594 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001595 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001596}
1597
1598/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1599/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1600/// needs to agree with the definition in <stddef.h>.
1601QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001602 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001603}
1604
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001605/// getSignedWCharType - Return the type of "signed wchar_t".
1606/// Used when in C++, as a GCC extension.
1607QualType ASTContext::getSignedWCharType() const {
1608 // FIXME: derive from "Target" ?
1609 return WCharTy;
1610}
1611
1612/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1613/// Used when in C++, as a GCC extension.
1614QualType ASTContext::getUnsignedWCharType() const {
1615 // FIXME: derive from "Target" ?
1616 return UnsignedIntTy;
1617}
1618
Chris Lattner4b009652007-07-25 00:24:17 +00001619/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1620/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1621QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001622 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001623}
1624
Chris Lattner19eb97e2008-04-02 05:18:44 +00001625//===----------------------------------------------------------------------===//
1626// Type Operators
1627//===----------------------------------------------------------------------===//
1628
Chris Lattner3dae6f42008-04-06 22:41:35 +00001629/// getCanonicalType - Return the canonical (structural) type corresponding to
1630/// the specified potentially non-canonical type. The non-canonical version
1631/// of a type may have many "decorated" versions of types. Decorators can
1632/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1633/// to be free of any of these, allowing two canonical types to be compared
1634/// for exact equality with a simple pointer comparison.
1635QualType ASTContext::getCanonicalType(QualType T) {
1636 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001637
1638 // If the result has type qualifiers, make sure to canonicalize them as well.
1639 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1640 if (TypeQuals == 0) return CanType;
1641
1642 // If the type qualifiers are on an array type, get the canonical type of the
1643 // array with the qualifiers applied to the element type.
1644 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1645 if (!AT)
1646 return CanType.getQualifiedType(TypeQuals);
1647
1648 // Get the canonical version of the element with the extra qualifiers on it.
1649 // This can recursively sink qualifiers through multiple levels of arrays.
1650 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1651 NewEltTy = getCanonicalType(NewEltTy);
1652
1653 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1654 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1655 CAT->getIndexTypeQualifier());
1656 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1657 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1658 IAT->getIndexTypeQualifier());
1659
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001660 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1661 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1662 DSAT->getSizeModifier(),
1663 DSAT->getIndexTypeQualifier());
1664
Chris Lattnera1923f62008-08-04 07:31:14 +00001665 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1666 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1667 VAT->getSizeModifier(),
1668 VAT->getIndexTypeQualifier());
1669}
1670
Douglas Gregord3022602009-03-27 23:10:48 +00001671NestedNameSpecifier *
1672ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1673 if (!NNS)
1674 return 0;
1675
1676 switch (NNS->getKind()) {
1677 case NestedNameSpecifier::Identifier:
1678 // Canonicalize the prefix but keep the identifier the same.
1679 return NestedNameSpecifier::Create(*this,
1680 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1681 NNS->getAsIdentifier());
1682
1683 case NestedNameSpecifier::Namespace:
1684 // A namespace is canonical; build a nested-name-specifier with
1685 // this namespace and no prefix.
1686 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1687
1688 case NestedNameSpecifier::TypeSpec:
1689 case NestedNameSpecifier::TypeSpecWithTemplate: {
1690 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1691 NestedNameSpecifier *Prefix = 0;
1692
1693 // FIXME: This isn't the right check!
1694 if (T->isDependentType())
1695 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1696
1697 return NestedNameSpecifier::Create(*this, Prefix,
1698 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1699 T.getTypePtr());
1700 }
1701
1702 case NestedNameSpecifier::Global:
1703 // The global specifier is canonical and unique.
1704 return NNS;
1705 }
1706
1707 // Required to silence a GCC warning
1708 return 0;
1709}
1710
Chris Lattnera1923f62008-08-04 07:31:14 +00001711
1712const ArrayType *ASTContext::getAsArrayType(QualType T) {
1713 // Handle the non-qualified case efficiently.
1714 if (T.getCVRQualifiers() == 0) {
1715 // Handle the common positive case fast.
1716 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1717 return AT;
1718 }
1719
1720 // Handle the common negative case fast, ignoring CVR qualifiers.
1721 QualType CType = T->getCanonicalTypeInternal();
1722
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001723 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001724 // test.
1725 if (!isa<ArrayType>(CType) &&
1726 !isa<ArrayType>(CType.getUnqualifiedType()))
1727 return 0;
1728
1729 // Apply any CVR qualifiers from the array type to the element type. This
1730 // implements C99 6.7.3p8: "If the specification of an array type includes
1731 // any type qualifiers, the element type is so qualified, not the array type."
1732
1733 // If we get here, we either have type qualifiers on the type, or we have
1734 // sugar such as a typedef in the way. If we have type qualifiers on the type
1735 // we must propagate them down into the elemeng type.
1736 unsigned CVRQuals = T.getCVRQualifiers();
1737 unsigned AddrSpace = 0;
1738 Type *Ty = T.getTypePtr();
1739
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001740 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001741 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001742 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1743 AddrSpace = EXTQT->getAddressSpace();
1744 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001745 } else {
1746 T = Ty->getDesugaredType();
1747 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1748 break;
1749 CVRQuals |= T.getCVRQualifiers();
1750 Ty = T.getTypePtr();
1751 }
1752 }
1753
1754 // If we have a simple case, just return now.
1755 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1756 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1757 return ATy;
1758
1759 // Otherwise, we have an array and we have qualifiers on it. Push the
1760 // qualifiers into the array element type and return a new array type.
1761 // Get the canonical version of the element with the extra qualifiers on it.
1762 // This can recursively sink qualifiers through multiple levels of arrays.
1763 QualType NewEltTy = ATy->getElementType();
1764 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001765 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001766 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1767
1768 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1769 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1770 CAT->getSizeModifier(),
1771 CAT->getIndexTypeQualifier()));
1772 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1773 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1774 IAT->getSizeModifier(),
1775 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001776
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001777 if (const DependentSizedArrayType *DSAT
1778 = dyn_cast<DependentSizedArrayType>(ATy))
1779 return cast<ArrayType>(
1780 getDependentSizedArrayType(NewEltTy,
1781 DSAT->getSizeExpr(),
1782 DSAT->getSizeModifier(),
1783 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001784
Chris Lattnera1923f62008-08-04 07:31:14 +00001785 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1786 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1787 VAT->getSizeModifier(),
1788 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001789}
1790
1791
Chris Lattner19eb97e2008-04-02 05:18:44 +00001792/// getArrayDecayedType - Return the properly qualified result of decaying the
1793/// specified array type to a pointer. This operation is non-trivial when
1794/// handling typedefs etc. The canonical type of "T" must be an array type,
1795/// this returns a pointer to a properly qualified element of the array.
1796///
1797/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1798QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001799 // Get the element type with 'getAsArrayType' so that we don't lose any
1800 // typedefs in the element type of the array. This also handles propagation
1801 // of type qualifiers from the array type into the element type if present
1802 // (C99 6.7.3p8).
1803 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1804 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001805
Chris Lattnera1923f62008-08-04 07:31:14 +00001806 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001807
1808 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001809 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001810}
1811
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001812QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001813 QualType ElemTy = VAT->getElementType();
1814
1815 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1816 return getBaseElementType(VAT);
1817
1818 return ElemTy;
1819}
1820
Chris Lattner4b009652007-07-25 00:24:17 +00001821/// getFloatingRank - Return a relative rank for floating point types.
1822/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001823static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001824 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001825 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001826
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001827 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001828 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001829 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001830 case BuiltinType::Float: return FloatRank;
1831 case BuiltinType::Double: return DoubleRank;
1832 case BuiltinType::LongDouble: return LongDoubleRank;
1833 }
1834}
1835
Steve Narofffa0c4532007-08-27 01:41:48 +00001836/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1837/// point or a complex type (based on typeDomain/typeSize).
1838/// 'typeDomain' is a real floating point or complex type.
1839/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001840QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1841 QualType Domain) const {
1842 FloatingRank EltRank = getFloatingRank(Size);
1843 if (Domain->isComplexType()) {
1844 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001845 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001846 case FloatRank: return FloatComplexTy;
1847 case DoubleRank: return DoubleComplexTy;
1848 case LongDoubleRank: return LongDoubleComplexTy;
1849 }
Chris Lattner4b009652007-07-25 00:24:17 +00001850 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001851
1852 assert(Domain->isRealFloatingType() && "Unknown domain!");
1853 switch (EltRank) {
1854 default: assert(0 && "getFloatingRank(): illegal value for rank");
1855 case FloatRank: return FloatTy;
1856 case DoubleRank: return DoubleTy;
1857 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001858 }
Chris Lattner4b009652007-07-25 00:24:17 +00001859}
1860
Chris Lattner51285d82008-04-06 23:55:33 +00001861/// getFloatingTypeOrder - Compare the rank of the two specified floating
1862/// point types, ignoring the domain of the type (i.e. 'double' ==
1863/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1864/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001865int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1866 FloatingRank LHSR = getFloatingRank(LHS);
1867 FloatingRank RHSR = getFloatingRank(RHS);
1868
1869 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001870 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001871 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001872 return 1;
1873 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001874}
1875
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001876/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1877/// routine will assert if passed a built-in type that isn't an integer or enum,
1878/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001879unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001880 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001881 if (EnumType* ET = dyn_cast<EnumType>(T))
1882 T = ET->getDecl()->getIntegerType().getTypePtr();
1883
1884 // There are two things which impact the integer rank: the width, and
1885 // the ordering of builtins. The builtin ordering is encoded in the
1886 // bottom three bits; the width is encoded in the bits above that.
1887 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1888 return FWIT->getWidth() << 3;
1889 }
1890
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001891 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001892 default: assert(0 && "getIntegerRank(): not a built-in integer");
1893 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001894 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001895 case BuiltinType::Char_S:
1896 case BuiltinType::Char_U:
1897 case BuiltinType::SChar:
1898 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001899 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001900 case BuiltinType::Short:
1901 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001902 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001903 case BuiltinType::Int:
1904 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001905 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001906 case BuiltinType::Long:
1907 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001908 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001909 case BuiltinType::LongLong:
1910 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001911 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001912 }
1913}
1914
Chris Lattner51285d82008-04-06 23:55:33 +00001915/// getIntegerTypeOrder - Returns the highest ranked integer type:
1916/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1917/// LHS < RHS, return -1.
1918int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001919 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1920 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001921 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001922
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001923 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1924 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001925
Chris Lattner51285d82008-04-06 23:55:33 +00001926 unsigned LHSRank = getIntegerRank(LHSC);
1927 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001928
Chris Lattner51285d82008-04-06 23:55:33 +00001929 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1930 if (LHSRank == RHSRank) return 0;
1931 return LHSRank > RHSRank ? 1 : -1;
1932 }
Chris Lattner4b009652007-07-25 00:24:17 +00001933
Chris Lattner51285d82008-04-06 23:55:33 +00001934 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1935 if (LHSUnsigned) {
1936 // If the unsigned [LHS] type is larger, return it.
1937 if (LHSRank >= RHSRank)
1938 return 1;
1939
1940 // If the signed type can represent all values of the unsigned type, it
1941 // wins. Because we are dealing with 2's complement and types that are
1942 // powers of two larger than each other, this is always safe.
1943 return -1;
1944 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001945
Chris Lattner51285d82008-04-06 23:55:33 +00001946 // If the unsigned [RHS] type is larger, return it.
1947 if (RHSRank >= LHSRank)
1948 return -1;
1949
1950 // If the signed type can represent all values of the unsigned type, it
1951 // wins. Because we are dealing with 2's complement and types that are
1952 // powers of two larger than each other, this is always safe.
1953 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001954}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001955
1956// getCFConstantStringType - Return the type used for constant CFStrings.
1957QualType ASTContext::getCFConstantStringType() {
1958 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001959 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001960 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001961 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001962 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001963
1964 // const int *isa;
1965 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001966 // int flags;
1967 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001968 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001969 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001970 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001971 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001972
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001973 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001974 for (unsigned i = 0; i < 4; ++i) {
1975 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1976 SourceLocation(), 0,
1977 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001978 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001979 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001980 }
1981
1982 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001983 }
1984
1985 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001986}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001987
Anders Carlssonf58cac72008-08-30 19:34:46 +00001988QualType ASTContext::getObjCFastEnumerationStateType()
1989{
1990 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001991 ObjCFastEnumerationStateTypeDecl =
1992 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1993 &Idents.get("__objcFastEnumerationState"));
1994
Anders Carlssonf58cac72008-08-30 19:34:46 +00001995 QualType FieldTypes[] = {
1996 UnsignedLongTy,
1997 getPointerType(ObjCIdType),
1998 getPointerType(UnsignedLongTy),
1999 getConstantArrayType(UnsignedLongTy,
2000 llvm::APInt(32, 5), ArrayType::Normal, 0)
2001 };
2002
Douglas Gregor8acb7272008-12-11 16:49:14 +00002003 for (size_t i = 0; i < 4; ++i) {
2004 FieldDecl *Field = FieldDecl::Create(*this,
2005 ObjCFastEnumerationStateTypeDecl,
2006 SourceLocation(), 0,
2007 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002008 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002009 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002010 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00002011
Douglas Gregor8acb7272008-12-11 16:49:14 +00002012 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00002013 }
2014
2015 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2016}
2017
Anders Carlssone3f02572007-10-29 06:33:42 +00002018// This returns true if a type has been typedefed to BOOL:
2019// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00002020static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002021 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00002022 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2023 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002024
2025 return false;
2026}
2027
Ted Kremenek42730c52008-01-07 19:49:32 +00002028/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002029/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00002030int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00002031 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002032
2033 // Make all integer and enum types at least as large as an int
2034 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002035 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002036 // Treat arrays as pointers, since that's how they're passed in.
2037 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002038 sz = getTypeSize(VoidPtrTy);
2039 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002040}
2041
Ted Kremenek42730c52008-01-07 19:49:32 +00002042/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002043/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002044void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00002045 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002046 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002047 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00002048 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002049 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002050 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002051 // Compute size of all parameters.
2052 // Start with computing size of a pointer in number of bytes.
2053 // FIXME: There might(should) be a better way of doing this computation!
2054 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002055 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002056 // The first two arguments (self and _cmd) are pointers; account for
2057 // their size.
2058 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002059 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2060 E = Decl->param_end(); PI != E; ++PI) {
2061 QualType PType = (*PI)->getType();
2062 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002063 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002064 ParmOffset += sz;
2065 }
2066 S += llvm::utostr(ParmOffset);
2067 S += "@0:";
2068 S += llvm::utostr(PtrSize);
2069
2070 // Argument types.
2071 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002072 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2073 E = Decl->param_end(); PI != E; ++PI) {
2074 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002075 QualType PType = PVDecl->getOriginalType();
2076 if (const ArrayType *AT =
2077 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
2078 // Use array's original type only if it has known number of
2079 // elements.
2080 if (!dyn_cast<ConstantArrayType>(AT))
2081 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002082 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002083 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002084 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002085 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002086 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002087 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002088 }
2089}
2090
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002091/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002092/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002093/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2094/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002095/// Property attributes are stored as a comma-delimited C string. The simple
2096/// attributes readonly and bycopy are encoded as single characters. The
2097/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2098/// encoded as single characters, followed by an identifier. Property types
2099/// are also encoded as a parametrized attribute. The characters used to encode
2100/// these attributes are defined by the following enumeration:
2101/// @code
2102/// enum PropertyAttributes {
2103/// kPropertyReadOnly = 'R', // property is read-only.
2104/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2105/// kPropertyByref = '&', // property is a reference to the value last assigned
2106/// kPropertyDynamic = 'D', // property is dynamic
2107/// kPropertyGetter = 'G', // followed by getter selector name
2108/// kPropertySetter = 'S', // followed by setter selector name
2109/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2110/// kPropertyType = 't' // followed by old-style type encoding.
2111/// kPropertyWeak = 'W' // 'weak' property
2112/// kPropertyStrong = 'P' // property GC'able
2113/// kPropertyNonAtomic = 'N' // property non-atomic
2114/// };
2115/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002116void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2117 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002118 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002119 // Collect information from the property implementation decl(s).
2120 bool Dynamic = false;
2121 ObjCPropertyImplDecl *SynthesizePID = 0;
2122
2123 // FIXME: Duplicated code due to poor abstraction.
2124 if (Container) {
2125 if (const ObjCCategoryImplDecl *CID =
2126 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2127 for (ObjCCategoryImplDecl::propimpl_iterator
2128 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2129 ObjCPropertyImplDecl *PID = *i;
2130 if (PID->getPropertyDecl() == PD) {
2131 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2132 Dynamic = true;
2133 } else {
2134 SynthesizePID = PID;
2135 }
2136 }
2137 }
2138 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002139 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002140 for (ObjCCategoryImplDecl::propimpl_iterator
2141 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2142 ObjCPropertyImplDecl *PID = *i;
2143 if (PID->getPropertyDecl() == PD) {
2144 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2145 Dynamic = true;
2146 } else {
2147 SynthesizePID = PID;
2148 }
2149 }
2150 }
2151 }
2152 }
2153
2154 // FIXME: This is not very efficient.
2155 S = "T";
2156
2157 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002158 // GCC has some special rules regarding encoding of properties which
2159 // closely resembles encoding of ivars.
2160 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2161 true /* outermost type */,
2162 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002163
2164 if (PD->isReadOnly()) {
2165 S += ",R";
2166 } else {
2167 switch (PD->getSetterKind()) {
2168 case ObjCPropertyDecl::Assign: break;
2169 case ObjCPropertyDecl::Copy: S += ",C"; break;
2170 case ObjCPropertyDecl::Retain: S += ",&"; break;
2171 }
2172 }
2173
2174 // It really isn't clear at all what this means, since properties
2175 // are "dynamic by default".
2176 if (Dynamic)
2177 S += ",D";
2178
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002179 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2180 S += ",N";
2181
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002182 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2183 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002184 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002185 }
2186
2187 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2188 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002189 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002190 }
2191
2192 if (SynthesizePID) {
2193 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2194 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002195 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002196 }
2197
2198 // FIXME: OBJCGC: weak & strong
2199}
2200
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002201/// getLegacyIntegralTypeEncoding -
2202/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002203/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002204/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2205///
2206void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2207 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2208 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002209 if (BT->getKind() == BuiltinType::ULong &&
2210 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002211 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002212 else
2213 if (BT->getKind() == BuiltinType::Long &&
2214 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002215 PointeeTy = IntTy;
2216 }
2217 }
2218}
2219
Fariborz Jahanian248db262008-01-22 22:44:46 +00002220void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002221 FieldDecl *Field) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002222 // We follow the behavior of gcc, expanding structures which are
2223 // directly pointed to, and expanding embedded structures. Note that
2224 // these rules are sufficient to prevent recursive encoding of the
2225 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002226 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2227 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002228}
2229
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002230static void EncodeBitField(const ASTContext *Context, std::string& S,
2231 FieldDecl *FD) {
2232 const Expr *E = FD->getBitWidth();
2233 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2234 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2235 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2236 S += 'b';
2237 S += llvm::utostr(N);
2238}
2239
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002240void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2241 bool ExpandPointedToStructures,
2242 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002243 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002244 bool OutermostType,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002245 bool EncodingProperty) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002246 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002247 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002248 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002249 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002250 else {
2251 char encoding;
2252 switch (BT->getKind()) {
2253 default: assert(0 && "Unhandled builtin type kind");
2254 case BuiltinType::Void: encoding = 'v'; break;
2255 case BuiltinType::Bool: encoding = 'B'; break;
2256 case BuiltinType::Char_U:
2257 case BuiltinType::UChar: encoding = 'C'; break;
2258 case BuiltinType::UShort: encoding = 'S'; break;
2259 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002260 case BuiltinType::ULong:
2261 encoding =
2262 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2263 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002264 case BuiltinType::ULongLong: encoding = 'Q'; break;
2265 case BuiltinType::Char_S:
2266 case BuiltinType::SChar: encoding = 'c'; break;
2267 case BuiltinType::Short: encoding = 's'; break;
2268 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002269 case BuiltinType::Long:
2270 encoding =
2271 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2272 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002273 case BuiltinType::LongLong: encoding = 'q'; break;
2274 case BuiltinType::Float: encoding = 'f'; break;
2275 case BuiltinType::Double: encoding = 'd'; break;
2276 case BuiltinType::LongDouble: encoding = 'd'; break;
2277 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002278
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002279 S += encoding;
2280 }
Anders Carlsson70e16dd2009-04-09 21:55:45 +00002281 } else if (const ComplexType *CT = T->getAsComplexType()) {
2282 S += 'j';
2283 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2284 false);
2285 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002286 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2287 ExpandPointedToStructures,
2288 ExpandStructures, FD);
2289 if (FD || EncodingProperty) {
2290 // Note that we do extended encoding of protocol qualifer list
2291 // Only when doing ivar or property encoding.
2292 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2293 S += '"';
2294 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2295 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2296 S += '<';
2297 S += Proto->getNameAsString();
2298 S += '>';
2299 }
2300 S += '"';
2301 }
2302 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002303 }
2304 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002305 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002306 bool isReadOnly = false;
2307 // For historical/compatibility reasons, the read-only qualifier of the
2308 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2309 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2310 // Also, do not emit the 'r' for anything but the outermost type!
2311 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2312 if (OutermostType && T.isConstQualified()) {
2313 isReadOnly = true;
2314 S += 'r';
2315 }
2316 }
2317 else if (OutermostType) {
2318 QualType P = PointeeTy;
2319 while (P->getAsPointerType())
2320 P = P->getAsPointerType()->getPointeeType();
2321 if (P.isConstQualified()) {
2322 isReadOnly = true;
2323 S += 'r';
2324 }
2325 }
2326 if (isReadOnly) {
2327 // Another legacy compatibility encoding. Some ObjC qualifier and type
2328 // combinations need to be rearranged.
2329 // Rewrite "in const" from "nr" to "rn"
2330 const char * s = S.c_str();
2331 int len = S.length();
2332 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2333 std::string replace = "rn";
2334 S.replace(S.end()-2, S.end(), replace);
2335 }
2336 }
Steve Naroff17c03822009-02-12 17:52:19 +00002337 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002338 S += '@';
2339 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002340 }
2341 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002342 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002343 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002344 // Another historical/compatibility reason.
2345 // We encode the underlying type which comes out as
2346 // {...};
2347 S += '^';
2348 getObjCEncodingForTypeImpl(PointeeTy, S,
2349 false, ExpandPointedToStructures,
2350 NULL);
2351 return;
2352 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002353 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002354 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002355 const ObjCInterfaceType *OIT =
2356 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002357 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002358 S += '"';
2359 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002360 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2361 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2362 S += '<';
2363 S += Proto->getNameAsString();
2364 S += '>';
2365 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002366 S += '"';
2367 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002368 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002369 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002370 S += '#';
2371 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002372 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002373 S += ':';
2374 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002375 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002376
2377 if (PointeeTy->isCharType()) {
2378 // char pointer types should be encoded as '*' unless it is a
2379 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002380 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002381 S += '*';
2382 return;
2383 }
2384 }
2385
2386 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002387 getLegacyIntegralTypeEncoding(PointeeTy);
2388
2389 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002390 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002391 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002392 } else if (const ArrayType *AT =
2393 // Ignore type qualifiers etc.
2394 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002395 if (isa<IncompleteArrayType>(AT)) {
2396 // Incomplete arrays are encoded as a pointer to the array element.
2397 S += '^';
2398
2399 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2400 false, ExpandStructures, FD);
2401 } else {
2402 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002403
Anders Carlsson858c64d2009-02-22 01:38:57 +00002404 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2405 S += llvm::utostr(CAT->getSize().getZExtValue());
2406 else {
2407 //Variable length arrays are encoded as a regular array with 0 elements.
2408 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2409 S += '0';
2410 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002411
Anders Carlsson858c64d2009-02-22 01:38:57 +00002412 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2413 false, ExpandStructures, FD);
2414 S += ']';
2415 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002416 } else if (T->getAsFunctionType()) {
2417 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002418 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002419 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002420 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002421 // Anonymous structures print as '?'
2422 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2423 S += II->getName();
2424 } else {
2425 S += '?';
2426 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002427 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002428 S += '=';
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002429 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2430 FieldEnd = RDecl->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002431 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002432 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002433 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002434 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002435 S += '"';
2436 }
2437
2438 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002439 if (Field->isBitField()) {
2440 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2441 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002442 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002443 QualType qt = Field->getType();
2444 getLegacyIntegralTypeEncoding(qt);
2445 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002446 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002447 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002448 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002449 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002450 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002451 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002452 if (FD && FD->isBitField())
2453 EncodeBitField(this, S, FD);
2454 else
2455 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002456 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002457 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002458 } else if (T->isObjCInterfaceType()) {
2459 // @encode(class_name)
2460 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2461 S += '{';
2462 const IdentifierInfo *II = OI->getIdentifier();
2463 S += II->getName();
2464 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002465 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002466 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002467 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002468 if (RecFields[i]->isBitField())
2469 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2470 RecFields[i]);
2471 else
2472 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2473 FD);
2474 }
2475 S += '}';
2476 }
2477 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002478 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002479}
2480
Ted Kremenek42730c52008-01-07 19:49:32 +00002481void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002482 std::string& S) const {
2483 if (QT & Decl::OBJC_TQ_In)
2484 S += 'n';
2485 if (QT & Decl::OBJC_TQ_Inout)
2486 S += 'N';
2487 if (QT & Decl::OBJC_TQ_Out)
2488 S += 'o';
2489 if (QT & Decl::OBJC_TQ_Bycopy)
2490 S += 'O';
2491 if (QT & Decl::OBJC_TQ_Byref)
2492 S += 'R';
2493 if (QT & Decl::OBJC_TQ_Oneway)
2494 S += 'V';
2495}
2496
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002497void ASTContext::setBuiltinVaListType(QualType T)
2498{
2499 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2500
2501 BuiltinVaListType = T;
2502}
2503
Ted Kremenek42730c52008-01-07 19:49:32 +00002504void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002505{
Ted Kremenek42730c52008-01-07 19:49:32 +00002506 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002507
2508 // typedef struct objc_object *id;
2509 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002510 // User error - caller will issue diagnostics.
2511 if (!ptr)
2512 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002513 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002514 // User error - caller will issue diagnostics.
2515 if (!rec)
2516 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002517 IdStructType = rec;
2518}
2519
Ted Kremenek42730c52008-01-07 19:49:32 +00002520void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002521{
Ted Kremenek42730c52008-01-07 19:49:32 +00002522 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002523
2524 // typedef struct objc_selector *SEL;
2525 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002526 if (!ptr)
2527 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002528 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002529 if (!rec)
2530 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002531 SelStructType = rec;
2532}
2533
Ted Kremenek42730c52008-01-07 19:49:32 +00002534void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002535{
Ted Kremenek42730c52008-01-07 19:49:32 +00002536 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002537}
2538
Ted Kremenek42730c52008-01-07 19:49:32 +00002539void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002540{
Ted Kremenek42730c52008-01-07 19:49:32 +00002541 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002542
2543 // typedef struct objc_class *Class;
2544 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2545 assert(ptr && "'Class' incorrectly typed");
2546 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2547 assert(rec && "'Class' incorrectly typed");
2548 ClassStructType = rec;
2549}
2550
Ted Kremenek42730c52008-01-07 19:49:32 +00002551void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2552 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002553 "'NSConstantString' type already set!");
2554
Ted Kremenek42730c52008-01-07 19:49:32 +00002555 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002556}
2557
Douglas Gregordd13e842009-03-30 22:58:21 +00002558/// \brief Retrieve the template name that represents a qualified
2559/// template name such as \c std::vector.
2560TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2561 bool TemplateKeyword,
2562 TemplateDecl *Template) {
2563 llvm::FoldingSetNodeID ID;
2564 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2565
2566 void *InsertPos = 0;
2567 QualifiedTemplateName *QTN =
2568 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2569 if (!QTN) {
2570 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2571 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2572 }
2573
2574 return TemplateName(QTN);
2575}
2576
2577/// \brief Retrieve the template name that represents a dependent
2578/// template name such as \c MetaFun::template apply.
2579TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2580 const IdentifierInfo *Name) {
2581 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2582
2583 llvm::FoldingSetNodeID ID;
2584 DependentTemplateName::Profile(ID, NNS, Name);
2585
2586 void *InsertPos = 0;
2587 DependentTemplateName *QTN =
2588 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2589
2590 if (QTN)
2591 return TemplateName(QTN);
2592
2593 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2594 if (CanonNNS == NNS) {
2595 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2596 } else {
2597 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2598 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2599 }
2600
2601 DependentTemplateNames.InsertNode(QTN, InsertPos);
2602 return TemplateName(QTN);
2603}
2604
Douglas Gregorc6507e42008-11-03 14:12:49 +00002605/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002606/// TargetInfo, produce the corresponding type. The unsigned @p Type
2607/// is actually a value of type @c TargetInfo::IntType.
2608QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002609 switch (Type) {
2610 case TargetInfo::NoInt: return QualType();
2611 case TargetInfo::SignedShort: return ShortTy;
2612 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2613 case TargetInfo::SignedInt: return IntTy;
2614 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2615 case TargetInfo::SignedLong: return LongTy;
2616 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2617 case TargetInfo::SignedLongLong: return LongLongTy;
2618 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2619 }
2620
2621 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002622 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002623}
Ted Kremenek118930e2008-07-24 23:58:27 +00002624
2625//===----------------------------------------------------------------------===//
2626// Type Predicates.
2627//===----------------------------------------------------------------------===//
2628
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002629/// isObjCNSObjectType - Return true if this is an NSObject object using
2630/// NSObject attribute on a c-style pointer type.
2631/// FIXME - Make it work directly on types.
2632///
2633bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2634 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2635 if (TypedefDecl *TD = TDT->getDecl())
2636 if (TD->getAttr<ObjCNSObjectAttr>())
2637 return true;
2638 }
2639 return false;
2640}
2641
Ted Kremenek118930e2008-07-24 23:58:27 +00002642/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2643/// to an object type. This includes "id" and "Class" (two 'special' pointers
2644/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2645/// ID type).
2646bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002647 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002648 return true;
2649
Steve Naroffd9e00802008-10-21 18:24:04 +00002650 // Blocks are objects.
2651 if (Ty->isBlockPointerType())
2652 return true;
2653
2654 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002655 if (!Ty->isPointerType())
2656 return false;
2657
2658 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2659 // pointer types. This looks for the typedef specifically, not for the
2660 // underlying type.
Eli Friedman9c2b33f2009-03-22 23:00:19 +00002661 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2662 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002663 return true;
2664
2665 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002666 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2667 return true;
2668
2669 // If is has NSObject attribute, OK as well.
2670 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002671}
2672
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002673/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2674/// garbage collection attribute.
2675///
2676QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002677 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002678 if (getLangOptions().ObjC1 &&
2679 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002680 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002681 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002682 // (or pointers to them) be treated as though they were declared
2683 // as __strong.
2684 if (GCAttrs == QualType::GCNone) {
2685 if (isObjCObjectPointerType(Ty))
2686 GCAttrs = QualType::Strong;
2687 else if (Ty->isPointerType())
2688 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2689 }
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002690 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002691 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002692}
2693
Chris Lattner6ff358b2008-04-07 06:51:04 +00002694//===----------------------------------------------------------------------===//
2695// Type Compatibility Testing
2696//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002697
Steve Naroff3454b6c2008-09-04 15:10:53 +00002698/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002699/// block types. Types must be strictly compatible here. For example,
2700/// C unfortunately doesn't produce an error for the following:
2701///
2702/// int (*emptyArgFunc)();
2703/// int (*intArgList)(int) = emptyArgFunc;
2704///
2705/// For blocks, we will produce an error for the following (similar to C++):
2706///
2707/// int (^emptyArgBlock)();
2708/// int (^intArgBlock)(int) = emptyArgBlock;
2709///
2710/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2711///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002712bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002713 const FunctionType *lbase = lhs->getAsFunctionType();
2714 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002715 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2716 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stump896ceed2009-04-01 01:17:39 +00002717 if (lproto && rproto == 0)
2718 return false;
2719 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff3454b6c2008-09-04 15:10:53 +00002720}
2721
Chris Lattner6ff358b2008-04-07 06:51:04 +00002722/// areCompatVectorTypes - Return true if the two specified vector types are
2723/// compatible.
2724static bool areCompatVectorTypes(const VectorType *LHS,
2725 const VectorType *RHS) {
2726 assert(LHS->isCanonical() && RHS->isCanonical());
2727 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002728 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002729}
2730
Eli Friedman0d9549b2008-08-22 00:56:42 +00002731/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002732/// compatible for assignment from RHS to LHS. This handles validation of any
2733/// protocol qualifiers on the LHS or RHS.
2734///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002735bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2736 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002737 // Verify that the base decls are compatible: the RHS must be a subclass of
2738 // the LHS.
2739 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2740 return false;
2741
2742 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2743 // protocol qualified at all, then we are good.
2744 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2745 return true;
2746
2747 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2748 // isn't a superset.
2749 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2750 return true; // FIXME: should return false!
2751
2752 // Finally, we must have two protocol-qualified interfaces.
2753 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2754 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002755
Steve Naroff98e71b82009-03-01 16:12:44 +00002756 // All LHS protocols must have a presence on the RHS.
2757 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002758
Steve Naroff98e71b82009-03-01 16:12:44 +00002759 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2760 LHSPE = LHSP->qual_end();
2761 LHSPI != LHSPE; LHSPI++) {
2762 bool RHSImplementsProtocol = false;
2763
2764 // If the RHS doesn't implement the protocol on the left, the types
2765 // are incompatible.
2766 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2767 RHSPE = RHSP->qual_end();
2768 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2769 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2770 RHSImplementsProtocol = true;
2771 }
2772 // FIXME: For better diagnostics, consider passing back the protocol name.
2773 if (!RHSImplementsProtocol)
2774 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002775 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002776 // The RHS implements all protocols listed on the LHS.
2777 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002778}
2779
Steve Naroff17c03822009-02-12 17:52:19 +00002780bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2781 // get the "pointed to" types
2782 const PointerType *LHSPT = LHS->getAsPointerType();
2783 const PointerType *RHSPT = RHS->getAsPointerType();
2784
2785 if (!LHSPT || !RHSPT)
2786 return false;
2787
2788 QualType lhptee = LHSPT->getPointeeType();
2789 QualType rhptee = RHSPT->getPointeeType();
2790 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2791 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2792 // ID acts sort of like void* for ObjC interfaces
2793 if (LHSIface && isObjCIdStructType(rhptee))
2794 return true;
2795 if (RHSIface && isObjCIdStructType(lhptee))
2796 return true;
2797 if (!LHSIface || !RHSIface)
2798 return false;
2799 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2800 canAssignObjCInterfaces(RHSIface, LHSIface);
2801}
2802
Steve Naroff85f0dc52007-10-15 20:41:53 +00002803/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2804/// both shall have the identically qualified version of a compatible type.
2805/// C99 6.2.7p1: Two types have compatible types if their types are the
2806/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002807bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2808 return !mergeTypes(LHS, RHS).isNull();
2809}
2810
2811QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2812 const FunctionType *lbase = lhs->getAsFunctionType();
2813 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002814 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2815 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002816 bool allLTypes = true;
2817 bool allRTypes = true;
2818
2819 // Check return type
2820 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2821 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002822 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2823 allLTypes = false;
2824 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2825 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002826
2827 if (lproto && rproto) { // two C99 style function prototypes
2828 unsigned lproto_nargs = lproto->getNumArgs();
2829 unsigned rproto_nargs = rproto->getNumArgs();
2830
2831 // Compatible functions must have the same number of arguments
2832 if (lproto_nargs != rproto_nargs)
2833 return QualType();
2834
2835 // Variadic and non-variadic functions aren't compatible
2836 if (lproto->isVariadic() != rproto->isVariadic())
2837 return QualType();
2838
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002839 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2840 return QualType();
2841
Eli Friedman0d9549b2008-08-22 00:56:42 +00002842 // Check argument compatibility
2843 llvm::SmallVector<QualType, 10> types;
2844 for (unsigned i = 0; i < lproto_nargs; i++) {
2845 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2846 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2847 QualType argtype = mergeTypes(largtype, rargtype);
2848 if (argtype.isNull()) return QualType();
2849 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002850 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2851 allLTypes = false;
2852 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2853 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002854 }
2855 if (allLTypes) return lhs;
2856 if (allRTypes) return rhs;
2857 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002858 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002859 }
2860
2861 if (lproto) allRTypes = false;
2862 if (rproto) allLTypes = false;
2863
Douglas Gregor4fa58902009-02-26 23:50:07 +00002864 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002865 if (proto) {
2866 if (proto->isVariadic()) return QualType();
2867 // Check that the types are compatible with the types that
2868 // would result from default argument promotions (C99 6.7.5.3p15).
2869 // The only types actually affected are promotable integer
2870 // types and floats, which would be passed as a different
2871 // type depending on whether the prototype is visible.
2872 unsigned proto_nargs = proto->getNumArgs();
2873 for (unsigned i = 0; i < proto_nargs; ++i) {
2874 QualType argTy = proto->getArgType(i);
2875 if (argTy->isPromotableIntegerType() ||
2876 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2877 return QualType();
2878 }
2879
2880 if (allLTypes) return lhs;
2881 if (allRTypes) return rhs;
2882 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002883 proto->getNumArgs(), lproto->isVariadic(),
2884 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002885 }
2886
2887 if (allLTypes) return lhs;
2888 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002889 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002890}
2891
2892QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002893 // C++ [expr]: If an expression initially has the type "reference to T", the
2894 // type is adjusted to "T" prior to any further analysis, the expression
2895 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002896 // expression is an lvalue unless the reference is an rvalue reference and
2897 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002898 // FIXME: C++ shouldn't be going through here! The rules are different
2899 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00002900 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2901 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002902 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002903 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002904 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002905 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002906
Eli Friedman0d9549b2008-08-22 00:56:42 +00002907 QualType LHSCan = getCanonicalType(LHS),
2908 RHSCan = getCanonicalType(RHS);
2909
2910 // If two types are identical, they are compatible.
2911 if (LHSCan == RHSCan)
2912 return LHS;
2913
2914 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002915 // Note that we handle extended qualifiers later, in the
2916 // case for ExtQualType.
2917 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002918 return QualType();
2919
2920 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2921 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2922
Chris Lattnerc38d4522008-01-14 05:45:46 +00002923 // We want to consider the two function types to be the same for these
2924 // comparisons, just force one to the other.
2925 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2926 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002927
2928 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002929 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2930 LHSClass = Type::ConstantArray;
2931 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2932 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002933
Nate Begemanaf6ed502008-04-18 23:10:10 +00002934 // Canonicalize ExtVector -> Vector.
2935 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2936 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002937
Chris Lattner7cdcb252008-04-07 06:38:24 +00002938 // Consider qualified interfaces and interfaces the same.
2939 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2940 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002941
Chris Lattnerb5709e22008-04-07 05:43:21 +00002942 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002943 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002944 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2945 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2946
2947 // ID acts sort of like void* for ObjC interfaces
2948 if (LHSIface && isObjCIdStructType(RHS))
2949 return LHS;
2950 if (RHSIface && isObjCIdStructType(LHS))
2951 return RHS;
2952
Steve Naroff28ceff72008-12-10 22:14:21 +00002953 // ID is compatible with all qualified id types.
2954 if (LHS->isObjCQualifiedIdType()) {
2955 if (const PointerType *PT = RHS->getAsPointerType()) {
2956 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002957 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002958 return LHS;
2959 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2960 // Unfortunately, this API is part of Sema (which we don't have access
2961 // to. Need to refactor. The following check is insufficient, since we
2962 // need to make sure the class implements the protocol.
2963 if (pType->isObjCInterfaceType())
2964 return LHS;
2965 }
2966 }
2967 if (RHS->isObjCQualifiedIdType()) {
2968 if (const PointerType *PT = LHS->getAsPointerType()) {
2969 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002970 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002971 return RHS;
2972 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2973 // Unfortunately, this API is part of Sema (which we don't have access
2974 // to. Need to refactor. The following check is insufficient, since we
2975 // need to make sure the class implements the protocol.
2976 if (pType->isObjCInterfaceType())
2977 return RHS;
2978 }
2979 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002980 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2981 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002982 if (const EnumType* ETy = LHS->getAsEnumType()) {
2983 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2984 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002985 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002986 if (const EnumType* ETy = RHS->getAsEnumType()) {
2987 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2988 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002989 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002990
Eli Friedman0d9549b2008-08-22 00:56:42 +00002991 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002992 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002993
Steve Naroffc88babe2008-01-09 22:43:08 +00002994 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002995 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00002996#define TYPE(Class, Base)
2997#define ABSTRACT_TYPE(Class, Base)
2998#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2999#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3000#include "clang/AST/TypeNodes.def"
3001 assert(false && "Non-canonical and dependent types shouldn't get here");
3002 return QualType();
3003
Sebastian Redlce6fff02009-03-16 23:22:08 +00003004 case Type::LValueReference:
3005 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003006 case Type::MemberPointer:
3007 assert(false && "C++ should never be in mergeTypes");
3008 return QualType();
3009
3010 case Type::IncompleteArray:
3011 case Type::VariableArray:
3012 case Type::FunctionProto:
3013 case Type::ExtVector:
3014 case Type::ObjCQualifiedInterface:
3015 assert(false && "Types are eliminated above");
3016 return QualType();
3017
Chris Lattnerc38d4522008-01-14 05:45:46 +00003018 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003019 {
3020 // Merge two pointer types, while trying to preserve typedef info
3021 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3022 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3023 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3024 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003025 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3026 return LHS;
3027 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3028 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003029 return getPointerType(ResultType);
3030 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003031 case Type::BlockPointer:
3032 {
3033 // Merge two block pointer types, while trying to preserve typedef info
3034 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3035 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3036 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3037 if (ResultType.isNull()) return QualType();
3038 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3039 return LHS;
3040 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3041 return RHS;
3042 return getBlockPointerType(ResultType);
3043 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003044 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003045 {
3046 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3047 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3048 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3049 return QualType();
3050
3051 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3052 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3053 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3054 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003055 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3056 return LHS;
3057 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3058 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003059 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3060 ArrayType::ArraySizeModifier(), 0);
3061 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3062 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003063 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3064 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003065 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3066 return LHS;
3067 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3068 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003069 if (LVAT) {
3070 // FIXME: This isn't correct! But tricky to implement because
3071 // the array's size has to be the size of LHS, but the type
3072 // has to be different.
3073 return LHS;
3074 }
3075 if (RVAT) {
3076 // FIXME: This isn't correct! But tricky to implement because
3077 // the array's size has to be the size of RHS, but the type
3078 // has to be different.
3079 return RHS;
3080 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003081 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3082 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003083 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003084 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003085 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003086 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003087 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003088 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003089 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003090 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3091 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003092 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003093 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003094 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003095 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003096 case Type::Complex:
3097 // Distinct complex types are incompatible.
3098 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003099 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003100 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003101 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3102 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003103 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003104 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003105 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003106 // FIXME: This should be type compatibility, e.g. whether
3107 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003108 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3109 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3110 if (LHSIface && RHSIface &&
3111 canAssignObjCInterfaces(LHSIface, RHSIface))
3112 return LHS;
3113
Eli Friedman0d9549b2008-08-22 00:56:42 +00003114 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003115 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003116 case Type::ObjCQualifiedId:
3117 // Distinct qualified id's are not compatible.
3118 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003119 case Type::FixedWidthInt:
3120 // Distinct fixed-width integers are not compatible.
3121 return QualType();
3122 case Type::ObjCQualifiedClass:
3123 // Distinct qualified classes are not compatible.
3124 return QualType();
3125 case Type::ExtQual:
3126 // FIXME: ExtQual types can be compatible even if they're not
3127 // identical!
3128 return QualType();
3129 // First attempt at an implementation, but I'm not really sure it's
3130 // right...
3131#if 0
3132 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3133 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3134 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3135 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3136 return QualType();
3137 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3138 LHSBase = QualType(LQual->getBaseType(), 0);
3139 RHSBase = QualType(RQual->getBaseType(), 0);
3140 ResultType = mergeTypes(LHSBase, RHSBase);
3141 if (ResultType.isNull()) return QualType();
3142 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3143 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3144 return LHS;
3145 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3146 return RHS;
3147 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3148 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3149 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3150 return ResultType;
3151#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003152
3153 case Type::TemplateSpecialization:
3154 assert(false && "Dependent types have no size");
3155 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003156 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003157
3158 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003159}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003160
Chris Lattner1d78a862008-04-07 07:01:58 +00003161//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003162// Integer Predicates
3163//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003164
Eli Friedman0832dbc2008-06-28 06:23:08 +00003165unsigned ASTContext::getIntWidth(QualType T) {
3166 if (T == BoolTy)
3167 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003168 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3169 return FWIT->getWidth();
3170 }
3171 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003172 return (unsigned)getTypeSize(T);
3173}
3174
3175QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3176 assert(T->isSignedIntegerType() && "Unexpected type");
3177 if (const EnumType* ETy = T->getAsEnumType())
3178 T = ETy->getDecl()->getIntegerType();
3179 const BuiltinType* BTy = T->getAsBuiltinType();
3180 assert (BTy && "Unexpected signed integer type");
3181 switch (BTy->getKind()) {
3182 case BuiltinType::Char_S:
3183 case BuiltinType::SChar:
3184 return UnsignedCharTy;
3185 case BuiltinType::Short:
3186 return UnsignedShortTy;
3187 case BuiltinType::Int:
3188 return UnsignedIntTy;
3189 case BuiltinType::Long:
3190 return UnsignedLongTy;
3191 case BuiltinType::LongLong:
3192 return UnsignedLongLongTy;
3193 default:
3194 assert(0 && "Unexpected signed integer type");
3195 return QualType();
3196 }
3197}
3198
3199
3200//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00003201// Serialization Support
3202//===----------------------------------------------------------------------===//
3203
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003204enum {
3205 BasicMetadataBlock = 1,
3206 ASTContextBlock = 2,
3207 DeclsBlock = 3
3208};
3209
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003210void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3211 // Create bitstream.
3212 llvm::BitstreamWriter Stream(Buffer);
3213
3214 // Emit the preamble.
3215 Stream.Emit((unsigned)'B', 8);
3216 Stream.Emit((unsigned)'C', 8);
3217 Stream.Emit(0xC, 4);
3218 Stream.Emit(0xF, 4);
3219 Stream.Emit(0xE, 4);
3220 Stream.Emit(0x0, 4);
3221
3222 // Create serializer.
3223 llvm::Serializer S(Stream);
3224
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003225 // ===---------------------------------------------------===/
3226 // Serialize the "Translation Unit" metadata.
3227 // ===---------------------------------------------------===/
3228
3229 // Emit ASTContext.
3230 S.EnterBlock(ASTContextBlock);
3231 S.EmitOwnedPtr(this);
3232 S.ExitBlock(); // exit "ASTContextBlock"
3233
3234 S.EnterBlock(BasicMetadataBlock);
3235
3236 // Block for SourceManager and Target. Allows easy skipping
3237 // around to the block for the Selectors during deserialization.
3238 S.EnterBlock();
3239
3240 // Emit the SourceManager.
3241 S.Emit(getSourceManager());
3242
3243 // Emit the Target.
3244 S.EmitPtr(&Target);
3245 S.EmitCStr(Target.getTargetTriple());
3246
3247 S.ExitBlock(); // exit "SourceManager and Target Block"
3248
3249 // Emit the Selectors.
3250 S.Emit(Selectors);
3251
3252 // Emit the Identifier Table.
3253 S.Emit(Idents);
3254
3255 S.ExitBlock(); // exit "BasicMetadataBlock"
3256}
3257
3258
Ted Kremenek738e6c02007-10-31 17:10:13 +00003259/// Emit - Serialize an ASTContext object to Bitcode.
3260void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00003261 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00003262 S.EmitRef(SourceMgr);
3263 S.EmitRef(Target);
3264 S.EmitRef(Idents);
3265 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003266
Ted Kremenek68228a92007-10-31 22:44:07 +00003267 // Emit the size of the type vector so that we can reserve that size
3268 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003269 S.EmitInt(Types.size());
3270
Ted Kremenek034a78c2007-11-13 22:02:55 +00003271 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3272 I!=E;++I)
3273 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003274
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003275 S.EmitOwnedPtr(TUDecl);
3276
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003277 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003278}
3279
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003280
3281ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3282 FileManager &FMgr) {
3283 // Check if the file is of the proper length.
3284 if (Buffer.getBufferSize() & 0x3) {
3285 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3286 return 0;
3287 }
3288
3289 // Create the bitstream reader.
3290 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3291 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3292
3293 if (Stream.Read(8) != 'B' ||
3294 Stream.Read(8) != 'C' ||
3295 Stream.Read(4) != 0xC ||
3296 Stream.Read(4) != 0xF ||
3297 Stream.Read(4) != 0xE ||
3298 Stream.Read(4) != 0x0) {
3299 // FIXME: Provide diagnostic.
3300 return NULL;
3301 }
3302
3303 // Create the deserializer.
3304 llvm::Deserializer Dezr(Stream);
3305
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003306 // ===---------------------------------------------------===/
3307 // Deserialize the "Translation Unit" metadata.
3308 // ===---------------------------------------------------===/
3309
3310 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3311 // (which will appear earlier) and record its location.
3312
3313 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3314 assert (FoundBlock);
3315
3316 llvm::Deserializer::Location ASTContextBlockLoc =
3317 Dezr.getCurrentBlockLocation();
3318
3319 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3320 assert (FoundBlock);
3321
3322 // Read the SourceManager.
3323 SourceManager::CreateAndRegister(Dezr, FMgr);
3324
3325 { // Read the TargetInfo.
3326 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3327 char* triple = Dezr.ReadCStr(NULL,0,true);
3328 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3329 delete [] triple;
3330 }
3331
3332 // For Selectors, we must read the identifier table first because the
3333 // SelectorTable depends on the identifiers being already deserialized.
3334 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3335 Dezr.SkipBlock();
3336
3337 // Read the identifier table.
3338 IdentifierTable::CreateAndRegister(Dezr);
3339
3340 // Now jump back and read the selectors.
3341 Dezr.JumpTo(SelectorBlkLoc);
3342 SelectorTable::CreateAndRegister(Dezr);
3343
3344 // Now jump back to ASTContextBlock and read the ASTContext.
3345 Dezr.JumpTo(ASTContextBlockLoc);
3346 return Dezr.ReadOwnedPtr<ASTContext>();
3347}
3348
Ted Kremenekacba3612007-11-13 00:25:37 +00003349ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00003350
3351 // Read the language options.
3352 LangOptions LOpts;
3353 LOpts.Read(D);
3354
Ted Kremenek68228a92007-10-31 22:44:07 +00003355 SourceManager &SM = D.ReadRef<SourceManager>();
3356 TargetInfo &t = D.ReadRef<TargetInfo>();
3357 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3358 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00003359
Ted Kremenek68228a92007-10-31 22:44:07 +00003360 unsigned size_reserve = D.ReadInt();
3361
Douglas Gregor24afd4a2008-11-17 14:58:09 +00003362 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3363 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00003364
Ted Kremenek034a78c2007-11-13 22:02:55 +00003365 for (unsigned i = 0; i < size_reserve; ++i)
3366 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00003367
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003368 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3369
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003370 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00003371
3372 return A;
3373}
Douglas Gregorc34897d2009-04-09 22:27:44 +00003374
3375ExternalASTSource::~ExternalASTSource() { }
3376
3377void ExternalASTSource::PrintStats() { }