blob: ef4ff1526e5892da61fdbb99b6d91f94ae1bcecd [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();
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000309 if (const ReferenceType* RT = T->getAsReferenceType()) {
310 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssoneeaeda32009-04-10 04:52:36 +0000311 Align = Target.getPointerAlign(AS);
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000312 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
313 // Incomplete or function types default to 1.
Eli Friedman0ee57322009-02-22 02:56:25 +0000314 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
315 T = cast<ArrayType>(T)->getElementType();
316
317 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
318 }
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000319 }
Eli Friedman0ee57322009-02-22 02:56:25 +0000320
321 return Align / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000322}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000323
Chris Lattner4b009652007-07-25 00:24:17 +0000324/// getTypeSize - Return the size of the specified type, in bits. This method
325/// does not work on incomplete types.
326std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000327ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000328 T = getCanonicalType(T);
Mike Stump44d1f402009-02-27 18:32:39 +0000329 uint64_t Width=0;
330 unsigned Align=8;
Chris Lattner4b009652007-07-25 00:24:17 +0000331 switch (T->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000332#define TYPE(Class, Base)
333#define ABSTRACT_TYPE(Class, Base)
334#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
335#define DEPENDENT_TYPE(Class, Base) case Type::Class:
336#include "clang/AST/TypeNodes.def"
337 assert(false && "Should not see non-canonical or dependent types");
338 break;
339
Chris Lattner4b009652007-07-25 00:24:17 +0000340 case Type::FunctionNoProto:
341 case Type::FunctionProto:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000342 case Type::IncompleteArray:
Chris Lattner4b009652007-07-25 00:24:17 +0000343 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000344 case Type::VariableArray:
345 assert(0 && "VLAs not implemented yet!");
346 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000347 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000348
Chris Lattner8cd0e932008-03-05 18:54:05 +0000349 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000350 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000351 Align = EltInfo.second;
352 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000353 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000354 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000355 case Type::Vector: {
356 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000357 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000358 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000359 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000360 // If the alignment is not a power of 2, round up to the next power of 2.
361 // This happens for non-power-of-2 length vectors.
362 // FIXME: this should probably be a target property.
363 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000364 break;
365 }
366
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000367 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000368 switch (cast<BuiltinType>(T)->getKind()) {
369 default: assert(0 && "Unknown builtin type!");
370 case BuiltinType::Void:
371 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000372 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000373 Width = Target.getBoolWidth();
374 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000375 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000376 case BuiltinType::Char_S:
377 case BuiltinType::Char_U:
378 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000379 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000380 Width = Target.getCharWidth();
381 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000382 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000383 case BuiltinType::WChar:
384 Width = Target.getWCharWidth();
385 Align = Target.getWCharAlign();
386 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000387 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000388 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000389 Width = Target.getShortWidth();
390 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000391 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000392 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000393 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000394 Width = Target.getIntWidth();
395 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000396 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000397 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000398 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000399 Width = Target.getLongWidth();
400 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000401 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000402 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000403 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000404 Width = Target.getLongLongWidth();
405 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000406 break;
407 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000408 Width = Target.getFloatWidth();
409 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000410 break;
411 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000412 Width = Target.getDoubleWidth();
413 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000414 break;
415 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000416 Width = Target.getLongDoubleWidth();
417 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000418 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000419 }
420 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000421 case Type::FixedWidthInt:
422 // FIXME: This isn't precisely correct; the width/alignment should depend
423 // on the available types for the target
424 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattnere9174982009-02-15 21:20:13 +0000425 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000426 Align = Width;
427 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000428 case Type::ExtQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000429 // FIXME: Pointers into different addr spaces could have different sizes and
430 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000431 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000432 case Type::ObjCQualifiedId:
Eli Friedman2f6d70d2009-02-22 04:02:33 +0000433 case Type::ObjCQualifiedClass:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000434 case Type::ObjCQualifiedInterface:
Chris Lattner1d78a862008-04-07 07:01:58 +0000435 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000436 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000437 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000438 case Type::BlockPointer: {
439 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
440 Width = Target.getPointerWidth(AS);
441 Align = Target.getPointerAlign(AS);
442 break;
443 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000444 case Type::Pointer: {
445 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000446 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000447 Align = Target.getPointerAlign(AS);
448 break;
449 }
Sebastian Redlce6fff02009-03-16 23:22:08 +0000450 case Type::LValueReference:
451 case Type::RValueReference:
Chris Lattner4b009652007-07-25 00:24:17 +0000452 // "When applied to a reference or a reference type, the result is the size
453 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000454 // FIXME: This is wrong for struct layout: a reference in a struct has
455 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000456 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000457 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000458 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-01-24 21:16:55 +0000459 // the GCC ABI, where pointers to data are one pointer large, pointers to
460 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl18cffee2009-01-24 23:29:36 +0000461 // other compilers too, we need to delegate this completely to TargetInfo
462 // or some ABI abstraction layer.
Sebastian Redl75555032009-01-24 21:16:55 +0000463 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
464 unsigned AS = Pointee.getAddressSpace();
465 Width = Target.getPointerWidth(AS);
466 if (Pointee->isFunctionType())
467 Width *= 2;
468 Align = Target.getPointerAlign(AS);
469 // GCC aligns at single pointer width.
470 }
Chris Lattner4b009652007-07-25 00:24:17 +0000471 case Type::Complex: {
472 // Complex types have the same alignment as their elements, but twice the
473 // size.
474 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000475 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000476 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000477 Align = EltInfo.second;
478 break;
479 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000480 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000481 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000482 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
483 Width = Layout.getSize();
484 Align = Layout.getAlignment();
485 break;
486 }
Douglas Gregor4fa58902009-02-26 23:50:07 +0000487 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000488 case Type::Enum: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000489 const TagType *TT = cast<TagType>(T);
490
491 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000492 Width = 1;
493 Align = 1;
494 break;
495 }
496
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000497 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000498 return getTypeInfo(ET->getDecl()->getIntegerType());
499
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000500 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000501 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
502 Width = Layout.getSize();
503 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000504 break;
505 }
Douglas Gregordd13e842009-03-30 22:58:21 +0000506
507 case Type::TemplateSpecialization:
508 assert(false && "Dependent types have no size");
509 break;
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000510 }
Chris Lattner4b009652007-07-25 00:24:17 +0000511
512 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000513 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000514}
515
Chris Lattner83165b52009-01-27 18:08:34 +0000516/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
517/// type for the current target in bits. This can be different than the ABI
518/// alignment in cases where it is beneficial for performance to overalign
519/// a data type.
520unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
521 unsigned ABIAlign = getTypeAlign(T);
522
523 // Doubles should be naturally aligned if possible.
Daniel Dunbarc61a8002009-02-18 19:59:32 +0000524 if (T->isSpecificBuiltinType(BuiltinType::Double))
525 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000526
527 return ABIAlign;
528}
529
530
Devang Patelbfe323c2008-06-04 21:22:16 +0000531/// LayoutField - Field layout.
532void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000533 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000534 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000535 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000536 uint64_t FieldOffset = IsUnion ? 0 : Size;
537 uint64_t FieldSize;
538 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000539
540 // FIXME: Should this override struct packing? Probably we want to
541 // take the minimum?
542 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
543 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000544
545 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
546 // TODO: Need to check this algorithm on other targets!
547 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000548 FieldSize =
549 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000550
551 std::pair<uint64_t, unsigned> FieldInfo =
552 Context.getTypeInfo(FD->getType());
553 uint64_t TypeSize = FieldInfo.first;
554
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000555 // Determine the alignment of this bitfield. The packing
556 // attributes define a maximum and the alignment attribute defines
557 // a minimum.
558 // FIXME: What is the right behavior when the specified alignment
559 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000560 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000561 if (FieldPacking)
562 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000563 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
564 FieldAlign = std::max(FieldAlign, AA->getAlignment());
565
566 // Check if we need to add padding to give the field the correct
567 // alignment.
568 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
569 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
570
571 // Padding members don't affect overall alignment
572 if (!FD->getIdentifier())
573 FieldAlign = 1;
574 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000575 if (FD->getType()->isIncompleteArrayType()) {
576 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000577 // query getTypeInfo about these, so we figure it out here.
578 // Flexible array members don't have any size, but they
579 // have to be aligned appropriately for their element type.
580 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000581 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000582 FieldAlign = Context.getTypeAlign(ATy->getElementType());
583 } else {
584 std::pair<uint64_t, unsigned> FieldInfo =
585 Context.getTypeInfo(FD->getType());
586 FieldSize = FieldInfo.first;
587 FieldAlign = FieldInfo.second;
588 }
589
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000590 // Determine the alignment of this bitfield. The packing
591 // attributes define a maximum and the alignment attribute defines
592 // a minimum. Additionally, the packing alignment must be at least
593 // a byte for non-bitfields.
594 //
595 // FIXME: What is the right behavior when the specified alignment
596 // is smaller than the specified packing?
597 if (FieldPacking)
598 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000599 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
600 FieldAlign = std::max(FieldAlign, AA->getAlignment());
601
602 // Round up the current record size to the field's alignment boundary.
603 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
604 }
605
606 // Place this field at the current location.
607 FieldOffsets[FieldNo] = FieldOffset;
608
609 // Reserve space for this field.
610 if (IsUnion) {
611 Size = std::max(Size, FieldSize);
612 } else {
613 Size = FieldOffset + FieldSize;
614 }
615
616 // Remember max struct/class alignment.
617 Alignment = std::max(Alignment, FieldAlign);
618}
619
Fariborz Jahaniana2c97df2009-03-05 20:08:48 +0000620void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000621 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000622 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
623 if (SuperClass)
624 CollectObjCIvars(SuperClass, Fields);
625 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
626 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000627 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000628 if (!IVDecl->isInvalidDecl())
629 Fields.push_back(cast<FieldDecl>(IVDecl));
630 }
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000631 // look into properties.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000632 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
633 E = OI->prop_end(*this); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000634 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000635 Fields.push_back(cast<FieldDecl>(IV));
636 }
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000637}
638
639/// addRecordToClass - produces record info. for the class for its
640/// ivars and all those inherited.
641///
Chris Lattner9329cf52009-03-31 08:48:01 +0000642const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) {
Chris Lattner608c1e32009-03-31 09:24:30 +0000643 RecordDecl *&RD = ASTRecordForInterface[D];
644 if (RD) {
645 // If we have a record decl already and it is either a definition or if 'D'
646 // is still a forward declaration, return it.
647 if (RD->isDefinition() || D->isForwardDecl())
648 return RD;
649 }
650
651 // If D is a forward declaration, then just make a forward struct decl.
652 if (D->isForwardDecl())
653 return RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
654 D->getLocation(),
655 D->getIdentifier());
Chris Lattner9329cf52009-03-31 08:48:01 +0000656
657 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000658 CollectObjCIvars(D, RecFields);
Chris Lattner608c1e32009-03-31 09:24:30 +0000659
660 if (RD == 0)
661 RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(),
662 D->getIdentifier());
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000663 /// FIXME! Can do collection of ivars and adding to the record while
664 /// doing it.
Chris Lattner41b780c2009-03-31 08:58:42 +0000665 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000666 RD->addDecl(*this,
667 FieldDecl::Create(*this, RD,
Chris Lattner608c1e32009-03-31 09:24:30 +0000668 RecFields[i]->getLocation(),
669 RecFields[i]->getIdentifier(),
670 RecFields[i]->getType(),
671 RecFields[i]->getBitWidth(), false));
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000672 }
Chris Lattner9329cf52009-03-31 08:48:01 +0000673
Chris Lattner608c1e32009-03-31 09:24:30 +0000674 RD->completeDefinition(*this);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000675 return RD;
676}
Devang Patel4b6bf702008-06-04 21:54:36 +0000677
Fariborz Jahanianea944842008-12-18 17:29:46 +0000678/// setFieldDecl - maps a field for the given Ivar reference node.
679//
680void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
681 const ObjCIvarDecl *Ivar,
682 const ObjCIvarRefExpr *MRef) {
Chris Lattner04f4db72009-03-31 08:31:13 +0000683 ASTFieldForIvarRef[MRef] = OI->lookupFieldDeclForIvar(*this, Ivar);
Fariborz Jahanianea944842008-12-18 17:29:46 +0000684}
685
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000686/// getASTObjcInterfaceLayout - Get or compute information about the layout of
687/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000688/// position information.
689const ASTRecordLayout &
690ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
691 // Look up this layout, if already laid out, return what we have.
692 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
693 if (Entry) return *Entry;
694
695 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
696 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000697 ASTRecordLayout *NewEntry = NULL;
Fariborz Jahanianba50c332009-04-08 21:54:52 +0000698 // FIXME. Add actual count of synthesized ivars, instead of count
699 // of properties which is the upper bound, but is safe.
Daniel Dunbar998510f2009-04-08 20:18:15 +0000700 unsigned FieldCount =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000701 D->ivar_size() + std::distance(D->prop_begin(*this), D->prop_end(*this));
Devang Patel8682d882008-06-06 02:14:01 +0000702 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
703 FieldCount++;
704 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
705 unsigned Alignment = SL.getAlignment();
706 uint64_t Size = SL.getSize();
707 NewEntry = new ASTRecordLayout(Size, Alignment);
708 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000709 // Super class is at the beginning of the layout.
710 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000711 } else {
712 NewEntry = new ASTRecordLayout();
713 NewEntry->InitializeLayout(FieldCount);
714 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000715 Entry = NewEntry;
716
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000717 unsigned StructPacking = 0;
718 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
719 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000720
721 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
722 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
723 AA->getAlignment()));
724
725 // Layout each ivar sequentially.
726 unsigned i = 0;
727 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
728 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
729 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000730 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000731 }
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000732 // Also synthesized ivars
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000733 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
734 E = D->prop_end(*this); I != E; ++I) {
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000735 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
736 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
737 }
Fariborz Jahanian84c45692009-04-01 19:37:34 +0000738
Devang Patel4b6bf702008-06-04 21:54:36 +0000739 // Finally, round the size of the total struct up to the alignment of the
740 // struct itself.
741 NewEntry->FinalizeLayout();
742 return *NewEntry;
743}
744
Devang Patel7a78e432007-11-01 19:11:01 +0000745/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000746/// specified record (struct/union/class), which indicates its size and field
747/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000748const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000749 D = D->getDefinition(*this);
750 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000751
Chris Lattner4b009652007-07-25 00:24:17 +0000752 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000753 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000754 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000755
Devang Patel7a78e432007-11-01 19:11:01 +0000756 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
757 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
758 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000759 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000760
Douglas Gregor39677622008-12-11 20:41:00 +0000761 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000762 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
763 D->field_end(*this)));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000764 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000765
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000766 unsigned StructPacking = 0;
767 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
768 StructPacking = PA->getAlignment();
769
Eli Friedman5949a022008-05-30 09:31:38 +0000770 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000771 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
772 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000773
Eli Friedman5949a022008-05-30 09:31:38 +0000774 // Layout each field, for now, just sequentially, respecting alignment. In
775 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000776 unsigned FieldIdx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000777 for (RecordDecl::field_iterator Field = D->field_begin(*this),
778 FieldEnd = D->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000779 Field != FieldEnd; (void)++Field, ++FieldIdx)
780 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000781
782 // Finally, round the size of the total struct up to the alignment of the
783 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000784 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000785 return *NewEntry;
786}
787
Chris Lattner4b009652007-07-25 00:24:17 +0000788//===----------------------------------------------------------------------===//
789// Type creation/memoization methods
790//===----------------------------------------------------------------------===//
791
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000792QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000793 QualType CanT = getCanonicalType(T);
794 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000795 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000796
797 // If we are composing extended qualifiers together, merge together into one
798 // ExtQualType node.
799 unsigned CVRQuals = T.getCVRQualifiers();
800 QualType::GCAttrTypes GCAttr = QualType::GCNone;
801 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000802
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000803 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
804 // If this type already has an address space specified, it cannot get
805 // another one.
806 assert(EQT->getAddressSpace() == 0 &&
807 "Type cannot be in multiple addr spaces!");
808 GCAttr = EQT->getObjCGCAttr();
809 TypeNode = EQT->getBaseType();
810 }
Chris Lattner35fef522008-02-20 20:55:12 +0000811
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000812 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000813 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000814 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000815 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000816 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000817 return QualType(EXTQy, CVRQuals);
818
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000819 // If the base type isn't canonical, this won't be a canonical type either,
820 // so fill in the canonical type field.
821 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000822 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000823 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000824
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000825 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000826 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000827 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000828 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000829 ExtQualType *New =
830 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000831 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000832 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000833 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000834}
835
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000836QualType ASTContext::getObjCGCQualType(QualType T,
837 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000838 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000839 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000840 return T;
841
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000842 // If we are composing extended qualifiers together, merge together into one
843 // ExtQualType node.
844 unsigned CVRQuals = T.getCVRQualifiers();
845 Type *TypeNode = T.getTypePtr();
846 unsigned AddressSpace = 0;
847
848 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
849 // If this type already has an address space specified, it cannot get
850 // another one.
851 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
852 "Type cannot be in multiple addr spaces!");
853 AddressSpace = EQT->getAddressSpace();
854 TypeNode = EQT->getBaseType();
855 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000856
857 // Check if we've already instantiated an gc qual'd type of this type.
858 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000859 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000860 void *InsertPos = 0;
861 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000862 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000863
864 // If the base type isn't canonical, this won't be a canonical type either,
865 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +0000866 // FIXME: Isn't this also not canonical if the base type is a array
867 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000868 QualType Canonical;
869 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000870 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000871
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000872 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000873 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
874 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
875 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000876 ExtQualType *New =
877 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000878 ExtQualTypes.InsertNode(New, InsertPos);
879 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000880 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000881}
Chris Lattner4b009652007-07-25 00:24:17 +0000882
883/// getComplexType - Return the uniqued reference to the type for a complex
884/// number with the specified element type.
885QualType ASTContext::getComplexType(QualType T) {
886 // Unique pointers, to guarantee there is only one pointer of a particular
887 // structure.
888 llvm::FoldingSetNodeID ID;
889 ComplexType::Profile(ID, T);
890
891 void *InsertPos = 0;
892 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
893 return QualType(CT, 0);
894
895 // If the pointee type isn't canonical, this won't be a canonical type either,
896 // so fill in the canonical type field.
897 QualType Canonical;
898 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000899 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000900
901 // Get the new insert position for the node we care about.
902 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000903 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000904 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000905 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000906 Types.push_back(New);
907 ComplexTypes.InsertNode(New, InsertPos);
908 return QualType(New, 0);
909}
910
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000911QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
912 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
913 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
914 FixedWidthIntType *&Entry = Map[Width];
915 if (!Entry)
916 Entry = new FixedWidthIntType(Width, Signed);
917 return QualType(Entry, 0);
918}
Chris Lattner4b009652007-07-25 00:24:17 +0000919
920/// getPointerType - Return the uniqued reference to the type for a pointer to
921/// the specified type.
922QualType ASTContext::getPointerType(QualType T) {
923 // Unique pointers, to guarantee there is only one pointer of a particular
924 // structure.
925 llvm::FoldingSetNodeID ID;
926 PointerType::Profile(ID, T);
927
928 void *InsertPos = 0;
929 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
930 return QualType(PT, 0);
931
932 // If the pointee type isn't canonical, this won't be a canonical type either,
933 // so fill in the canonical type field.
934 QualType Canonical;
935 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000936 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000937
938 // Get the new insert position for the node we care about.
939 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000940 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000941 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000942 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000943 Types.push_back(New);
944 PointerTypes.InsertNode(New, InsertPos);
945 return QualType(New, 0);
946}
947
Steve Naroff7aa54752008-08-27 16:04:49 +0000948/// getBlockPointerType - Return the uniqued reference to the type for
949/// a pointer to the specified block.
950QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000951 assert(T->isFunctionType() && "block of function types only");
952 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000953 // structure.
954 llvm::FoldingSetNodeID ID;
955 BlockPointerType::Profile(ID, T);
956
957 void *InsertPos = 0;
958 if (BlockPointerType *PT =
959 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
960 return QualType(PT, 0);
961
Steve Narofffd5b19d2008-08-28 19:20:44 +0000962 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000963 // type either so fill in the canonical type field.
964 QualType Canonical;
965 if (!T->isCanonical()) {
966 Canonical = getBlockPointerType(getCanonicalType(T));
967
968 // Get the new insert position for the node we care about.
969 BlockPointerType *NewIP =
970 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000971 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000972 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000973 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000974 Types.push_back(New);
975 BlockPointerTypes.InsertNode(New, InsertPos);
976 return QualType(New, 0);
977}
978
Sebastian Redlce6fff02009-03-16 23:22:08 +0000979/// getLValueReferenceType - Return the uniqued reference to the type for an
980/// lvalue reference to the specified type.
981QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +0000982 // Unique pointers, to guarantee there is only one pointer of a particular
983 // structure.
984 llvm::FoldingSetNodeID ID;
985 ReferenceType::Profile(ID, T);
986
987 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000988 if (LValueReferenceType *RT =
989 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000990 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000991
Chris Lattner4b009652007-07-25 00:24:17 +0000992 // If the referencee type isn't canonical, this won't be a canonical type
993 // either, so fill in the canonical type field.
994 QualType Canonical;
995 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000996 Canonical = getLValueReferenceType(getCanonicalType(T));
997
Chris Lattner4b009652007-07-25 00:24:17 +0000998 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000999 LValueReferenceType *NewIP =
1000 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001001 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001002 }
1003
Sebastian Redlce6fff02009-03-16 23:22:08 +00001004 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001005 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001006 LValueReferenceTypes.InsertNode(New, InsertPos);
1007 return QualType(New, 0);
1008}
1009
1010/// getRValueReferenceType - Return the uniqued reference to the type for an
1011/// rvalue reference to the specified type.
1012QualType ASTContext::getRValueReferenceType(QualType T) {
1013 // Unique pointers, to guarantee there is only one pointer of a particular
1014 // structure.
1015 llvm::FoldingSetNodeID ID;
1016 ReferenceType::Profile(ID, T);
1017
1018 void *InsertPos = 0;
1019 if (RValueReferenceType *RT =
1020 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1021 return QualType(RT, 0);
1022
1023 // If the referencee type isn't canonical, this won't be a canonical type
1024 // either, so fill in the canonical type field.
1025 QualType Canonical;
1026 if (!T->isCanonical()) {
1027 Canonical = getRValueReferenceType(getCanonicalType(T));
1028
1029 // Get the new insert position for the node we care about.
1030 RValueReferenceType *NewIP =
1031 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1032 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1033 }
1034
1035 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1036 Types.push_back(New);
1037 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001038 return QualType(New, 0);
1039}
1040
Sebastian Redl75555032009-01-24 21:16:55 +00001041/// getMemberPointerType - Return the uniqued reference to the type for a
1042/// member pointer to the specified type, in the specified class.
1043QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1044{
1045 // Unique pointers, to guarantee there is only one pointer of a particular
1046 // structure.
1047 llvm::FoldingSetNodeID ID;
1048 MemberPointerType::Profile(ID, T, Cls);
1049
1050 void *InsertPos = 0;
1051 if (MemberPointerType *PT =
1052 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1053 return QualType(PT, 0);
1054
1055 // If the pointee or class type isn't canonical, this won't be a canonical
1056 // type either, so fill in the canonical type field.
1057 QualType Canonical;
1058 if (!T->isCanonical()) {
1059 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1060
1061 // Get the new insert position for the node we care about.
1062 MemberPointerType *NewIP =
1063 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1064 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1065 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001066 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001067 Types.push_back(New);
1068 MemberPointerTypes.InsertNode(New, InsertPos);
1069 return QualType(New, 0);
1070}
1071
Steve Naroff83c13012007-08-30 01:06:46 +00001072/// getConstantArrayType - Return the unique reference to the type for an
1073/// array of the specified element type.
1074QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +00001075 const llvm::APInt &ArySize,
1076 ArrayType::ArraySizeModifier ASM,
1077 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001078 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001079 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001080
1081 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001082 if (ConstantArrayType *ATP =
1083 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001084 return QualType(ATP, 0);
1085
1086 // If the element type isn't canonical, this won't be a canonical type either,
1087 // so fill in the canonical type field.
1088 QualType Canonical;
1089 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001090 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001091 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001092 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001093 ConstantArrayType *NewIP =
1094 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001095 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001096 }
1097
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001098 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001099 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001100 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001101 Types.push_back(New);
1102 return QualType(New, 0);
1103}
1104
Steve Naroffe2579e32007-08-30 18:14:25 +00001105/// getVariableArrayType - Returns a non-unique reference to the type for a
1106/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +00001107QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1108 ArrayType::ArraySizeModifier ASM,
1109 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001110 // Since we don't unique expressions, it isn't possible to unique VLA's
1111 // that have an expression provided for their size.
1112
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001113 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001114 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001115
1116 VariableArrayTypes.push_back(New);
1117 Types.push_back(New);
1118 return QualType(New, 0);
1119}
1120
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001121/// getDependentSizedArrayType - Returns a non-unique reference to
1122/// the type for a dependently-sized array of the specified element
1123/// type. FIXME: We will need these to be uniqued, or at least
1124/// comparable, at some point.
1125QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1126 ArrayType::ArraySizeModifier ASM,
1127 unsigned EltTypeQuals) {
1128 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1129 "Size must be type- or value-dependent!");
1130
1131 // Since we don't unique expressions, it isn't possible to unique
1132 // dependently-sized array types.
1133
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001134 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001135 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1136 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001137
1138 DependentSizedArrayTypes.push_back(New);
1139 Types.push_back(New);
1140 return QualType(New, 0);
1141}
1142
Eli Friedman8ff07782008-02-15 18:16:39 +00001143QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1144 ArrayType::ArraySizeModifier ASM,
1145 unsigned EltTypeQuals) {
1146 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001147 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001148
1149 void *InsertPos = 0;
1150 if (IncompleteArrayType *ATP =
1151 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1152 return QualType(ATP, 0);
1153
1154 // If the element type isn't canonical, this won't be a canonical type
1155 // either, so fill in the canonical type field.
1156 QualType Canonical;
1157
1158 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001159 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001160 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001161
1162 // Get the new insert position for the node we care about.
1163 IncompleteArrayType *NewIP =
1164 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001165 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001166 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001167
Steve Naroff93fd2112009-01-27 22:08:43 +00001168 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001169 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001170
1171 IncompleteArrayTypes.InsertNode(New, InsertPos);
1172 Types.push_back(New);
1173 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001174}
1175
Chris Lattner4b009652007-07-25 00:24:17 +00001176/// getVectorType - Return the unique reference to a vector type of
1177/// the specified element type and size. VectorType must be a built-in type.
1178QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1179 BuiltinType *baseType;
1180
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001181 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001182 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1183
1184 // Check if we've already instantiated a vector of this type.
1185 llvm::FoldingSetNodeID ID;
1186 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1187 void *InsertPos = 0;
1188 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1189 return QualType(VTP, 0);
1190
1191 // If the element type isn't canonical, this won't be a canonical type either,
1192 // so fill in the canonical type field.
1193 QualType Canonical;
1194 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001195 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001196
1197 // Get the new insert position for the node we care about.
1198 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001199 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001200 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001201 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001202 VectorTypes.InsertNode(New, InsertPos);
1203 Types.push_back(New);
1204 return QualType(New, 0);
1205}
1206
Nate Begemanaf6ed502008-04-18 23:10:10 +00001207/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001208/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001209QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001210 BuiltinType *baseType;
1211
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001212 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001213 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001214
1215 // Check if we've already instantiated a vector of this type.
1216 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001217 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001218 void *InsertPos = 0;
1219 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1220 return QualType(VTP, 0);
1221
1222 // If the element type isn't canonical, this won't be a canonical type either,
1223 // so fill in the canonical type field.
1224 QualType Canonical;
1225 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001226 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001227
1228 // Get the new insert position for the node we care about.
1229 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001230 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001231 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001232 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001233 VectorTypes.InsertNode(New, InsertPos);
1234 Types.push_back(New);
1235 return QualType(New, 0);
1236}
1237
Douglas Gregor4fa58902009-02-26 23:50:07 +00001238/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001239///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001240QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001241 // Unique functions, to guarantee there is only one function of a particular
1242 // structure.
1243 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001244 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001245
1246 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001247 if (FunctionNoProtoType *FT =
1248 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001249 return QualType(FT, 0);
1250
1251 QualType Canonical;
1252 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001253 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001254
1255 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001256 FunctionNoProtoType *NewIP =
1257 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001258 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001259 }
1260
Douglas Gregor4fa58902009-02-26 23:50:07 +00001261 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001262 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001263 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001264 return QualType(New, 0);
1265}
1266
1267/// getFunctionType - Return a normal function type with a typed argument
1268/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001269QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001270 unsigned NumArgs, bool isVariadic,
1271 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001272 // Unique functions, to guarantee there is only one function of a particular
1273 // structure.
1274 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001275 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001276 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001277
1278 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001279 if (FunctionProtoType *FTP =
1280 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001281 return QualType(FTP, 0);
1282
1283 // Determine whether the type being created is already canonical or not.
1284 bool isCanonical = ResultTy->isCanonical();
1285 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1286 if (!ArgArray[i]->isCanonical())
1287 isCanonical = false;
1288
1289 // If this type isn't canonical, get the canonical version of it.
1290 QualType Canonical;
1291 if (!isCanonical) {
1292 llvm::SmallVector<QualType, 16> CanonicalArgs;
1293 CanonicalArgs.reserve(NumArgs);
1294 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001295 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001296
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001297 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001298 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001299 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001300
1301 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001302 FunctionProtoType *NewIP =
1303 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001304 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001305 }
1306
Douglas Gregor4fa58902009-02-26 23:50:07 +00001307 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001308 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001309 FunctionProtoType *FTP =
1310 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001311 NumArgs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001312 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001313 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001314 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001315 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001316 return QualType(FTP, 0);
1317}
1318
Douglas Gregor1d661552008-04-13 21:07:44 +00001319/// getTypeDeclType - Return the unique reference to the type for the
1320/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001321QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001322 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001323 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1324
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001325 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001326 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001327 else if (isa<TemplateTypeParmDecl>(Decl)) {
1328 assert(false && "Template type parameter types are always available.");
1329 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001330 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001331
Douglas Gregor2e047592009-02-28 01:32:25 +00001332 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001333 if (PrevDecl)
1334 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001335 else
1336 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001337 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001338 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1339 if (PrevDecl)
1340 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001341 else
1342 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001343 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001344 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001345 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001346
Ted Kremenek46a837c2008-09-05 17:16:31 +00001347 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001348 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001349}
1350
Chris Lattner4b009652007-07-25 00:24:17 +00001351/// getTypedefType - Return the unique reference to the type for the
1352/// specified typename decl.
1353QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1354 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1355
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001356 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001357 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001358 Types.push_back(Decl->TypeForDecl);
1359 return QualType(Decl->TypeForDecl, 0);
1360}
1361
Ted Kremenek42730c52008-01-07 19:49:32 +00001362/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001363/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001364QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001365 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1366
Steve Naroff93fd2112009-01-27 22:08:43 +00001367 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001368 Types.push_back(Decl->TypeForDecl);
1369 return QualType(Decl->TypeForDecl, 0);
1370}
1371
Douglas Gregora4918772009-02-05 23:33:38 +00001372/// \brief Retrieve the template type parameter type for a template
1373/// parameter with the given depth, index, and (optionally) name.
1374QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1375 IdentifierInfo *Name) {
1376 llvm::FoldingSetNodeID ID;
1377 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1378 void *InsertPos = 0;
1379 TemplateTypeParmType *TypeParm
1380 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1381
1382 if (TypeParm)
1383 return QualType(TypeParm, 0);
1384
1385 if (Name)
1386 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1387 getTemplateTypeParmType(Depth, Index));
1388 else
1389 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1390
1391 Types.push_back(TypeParm);
1392 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1393
1394 return QualType(TypeParm, 0);
1395}
1396
Douglas Gregor8e458f42009-02-09 18:46:07 +00001397QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001398ASTContext::getTemplateSpecializationType(TemplateName Template,
1399 const TemplateArgument *Args,
1400 unsigned NumArgs,
1401 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001402 if (!Canon.isNull())
1403 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001404
Douglas Gregor8e458f42009-02-09 18:46:07 +00001405 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001406 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001407
Douglas Gregor8e458f42009-02-09 18:46:07 +00001408 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001409 TemplateSpecializationType *Spec
1410 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001411
1412 if (Spec)
1413 return QualType(Spec, 0);
1414
Douglas Gregordd13e842009-03-30 22:58:21 +00001415 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001416 sizeof(TemplateArgument) * NumArgs),
1417 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001418 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001419 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001420 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001421
1422 return QualType(Spec, 0);
1423}
1424
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001425QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001426ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001427 QualType NamedType) {
1428 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001429 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001430
1431 void *InsertPos = 0;
1432 QualifiedNameType *T
1433 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1434 if (T)
1435 return QualType(T, 0);
1436
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001437 T = new (*this) QualifiedNameType(NNS, NamedType,
1438 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001439 Types.push_back(T);
1440 QualifiedNameTypes.InsertNode(T, InsertPos);
1441 return QualType(T, 0);
1442}
1443
Douglas Gregord3022602009-03-27 23:10:48 +00001444QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1445 const IdentifierInfo *Name,
1446 QualType Canon) {
1447 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1448
1449 if (Canon.isNull()) {
1450 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1451 if (CanonNNS != NNS)
1452 Canon = getTypenameType(CanonNNS, Name);
1453 }
1454
1455 llvm::FoldingSetNodeID ID;
1456 TypenameType::Profile(ID, NNS, Name);
1457
1458 void *InsertPos = 0;
1459 TypenameType *T
1460 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1461 if (T)
1462 return QualType(T, 0);
1463
1464 T = new (*this) TypenameType(NNS, Name, Canon);
1465 Types.push_back(T);
1466 TypenameTypes.InsertNode(T, InsertPos);
1467 return QualType(T, 0);
1468}
1469
Douglas Gregor77da5802009-04-01 00:28:59 +00001470QualType
1471ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1472 const TemplateSpecializationType *TemplateId,
1473 QualType Canon) {
1474 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1475
1476 if (Canon.isNull()) {
1477 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1478 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1479 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1480 const TemplateSpecializationType *CanonTemplateId
1481 = CanonType->getAsTemplateSpecializationType();
1482 assert(CanonTemplateId &&
1483 "Canonical type must also be a template specialization type");
1484 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1485 }
1486 }
1487
1488 llvm::FoldingSetNodeID ID;
1489 TypenameType::Profile(ID, NNS, TemplateId);
1490
1491 void *InsertPos = 0;
1492 TypenameType *T
1493 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1494 if (T)
1495 return QualType(T, 0);
1496
1497 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1498 Types.push_back(T);
1499 TypenameTypes.InsertNode(T, InsertPos);
1500 return QualType(T, 0);
1501}
1502
Chris Lattnere1352302008-04-07 04:56:42 +00001503/// CmpProtocolNames - Comparison predicate for sorting protocols
1504/// alphabetically.
1505static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1506 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001507 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001508}
1509
1510static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1511 unsigned &NumProtocols) {
1512 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1513
1514 // Sort protocols, keyed by name.
1515 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1516
1517 // Remove duplicates.
1518 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1519 NumProtocols = ProtocolsEnd-Protocols;
1520}
1521
1522
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001523/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1524/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001525QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1526 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001527 // Sort the protocol list alphabetically to canonicalize it.
1528 SortAndUniqueProtocols(Protocols, NumProtocols);
1529
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001530 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001531 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001532
1533 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001534 if (ObjCQualifiedInterfaceType *QT =
1535 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001536 return QualType(QT, 0);
1537
1538 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001539 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001540 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001541
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001542 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001543 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001544 return QualType(QType, 0);
1545}
1546
Chris Lattnere1352302008-04-07 04:56:42 +00001547/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1548/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001549QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001550 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001551 // Sort the protocol list alphabetically to canonicalize it.
1552 SortAndUniqueProtocols(Protocols, NumProtocols);
1553
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001554 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001555 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001556
1557 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001558 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001559 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001560 return QualType(QT, 0);
1561
1562 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001563 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001564 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001565 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001566 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001567 return QualType(QType, 0);
1568}
1569
Douglas Gregor4fa58902009-02-26 23:50:07 +00001570/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1571/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001572/// multiple declarations that refer to "typeof(x)" all contain different
1573/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1574/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001575QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001576 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001577 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001578 Types.push_back(toe);
1579 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001580}
1581
Steve Naroff0604dd92007-08-01 18:02:17 +00001582/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1583/// TypeOfType AST's. The only motivation to unique these nodes would be
1584/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1585/// an issue. This doesn't effect the type checker, since it operates
1586/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001587QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001588 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001589 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001590 Types.push_back(tot);
1591 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001592}
1593
Chris Lattner4b009652007-07-25 00:24:17 +00001594/// getTagDeclType - Return the unique reference to the type for the
1595/// specified TagDecl (struct/union/class/enum) decl.
1596QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001597 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001598 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001599}
1600
1601/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1602/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1603/// needs to agree with the definition in <stddef.h>.
1604QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001605 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001606}
1607
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001608/// getSignedWCharType - Return the type of "signed wchar_t".
1609/// Used when in C++, as a GCC extension.
1610QualType ASTContext::getSignedWCharType() const {
1611 // FIXME: derive from "Target" ?
1612 return WCharTy;
1613}
1614
1615/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1616/// Used when in C++, as a GCC extension.
1617QualType ASTContext::getUnsignedWCharType() const {
1618 // FIXME: derive from "Target" ?
1619 return UnsignedIntTy;
1620}
1621
Chris Lattner4b009652007-07-25 00:24:17 +00001622/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1623/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1624QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001625 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001626}
1627
Chris Lattner19eb97e2008-04-02 05:18:44 +00001628//===----------------------------------------------------------------------===//
1629// Type Operators
1630//===----------------------------------------------------------------------===//
1631
Chris Lattner3dae6f42008-04-06 22:41:35 +00001632/// getCanonicalType - Return the canonical (structural) type corresponding to
1633/// the specified potentially non-canonical type. The non-canonical version
1634/// of a type may have many "decorated" versions of types. Decorators can
1635/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1636/// to be free of any of these, allowing two canonical types to be compared
1637/// for exact equality with a simple pointer comparison.
1638QualType ASTContext::getCanonicalType(QualType T) {
1639 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001640
1641 // If the result has type qualifiers, make sure to canonicalize them as well.
1642 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1643 if (TypeQuals == 0) return CanType;
1644
1645 // If the type qualifiers are on an array type, get the canonical type of the
1646 // array with the qualifiers applied to the element type.
1647 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1648 if (!AT)
1649 return CanType.getQualifiedType(TypeQuals);
1650
1651 // Get the canonical version of the element with the extra qualifiers on it.
1652 // This can recursively sink qualifiers through multiple levels of arrays.
1653 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1654 NewEltTy = getCanonicalType(NewEltTy);
1655
1656 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1657 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1658 CAT->getIndexTypeQualifier());
1659 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1660 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1661 IAT->getIndexTypeQualifier());
1662
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001663 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1664 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1665 DSAT->getSizeModifier(),
1666 DSAT->getIndexTypeQualifier());
1667
Chris Lattnera1923f62008-08-04 07:31:14 +00001668 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1669 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1670 VAT->getSizeModifier(),
1671 VAT->getIndexTypeQualifier());
1672}
1673
Douglas Gregord3022602009-03-27 23:10:48 +00001674NestedNameSpecifier *
1675ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1676 if (!NNS)
1677 return 0;
1678
1679 switch (NNS->getKind()) {
1680 case NestedNameSpecifier::Identifier:
1681 // Canonicalize the prefix but keep the identifier the same.
1682 return NestedNameSpecifier::Create(*this,
1683 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1684 NNS->getAsIdentifier());
1685
1686 case NestedNameSpecifier::Namespace:
1687 // A namespace is canonical; build a nested-name-specifier with
1688 // this namespace and no prefix.
1689 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1690
1691 case NestedNameSpecifier::TypeSpec:
1692 case NestedNameSpecifier::TypeSpecWithTemplate: {
1693 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1694 NestedNameSpecifier *Prefix = 0;
1695
1696 // FIXME: This isn't the right check!
1697 if (T->isDependentType())
1698 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1699
1700 return NestedNameSpecifier::Create(*this, Prefix,
1701 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1702 T.getTypePtr());
1703 }
1704
1705 case NestedNameSpecifier::Global:
1706 // The global specifier is canonical and unique.
1707 return NNS;
1708 }
1709
1710 // Required to silence a GCC warning
1711 return 0;
1712}
1713
Chris Lattnera1923f62008-08-04 07:31:14 +00001714
1715const ArrayType *ASTContext::getAsArrayType(QualType T) {
1716 // Handle the non-qualified case efficiently.
1717 if (T.getCVRQualifiers() == 0) {
1718 // Handle the common positive case fast.
1719 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1720 return AT;
1721 }
1722
1723 // Handle the common negative case fast, ignoring CVR qualifiers.
1724 QualType CType = T->getCanonicalTypeInternal();
1725
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001726 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001727 // test.
1728 if (!isa<ArrayType>(CType) &&
1729 !isa<ArrayType>(CType.getUnqualifiedType()))
1730 return 0;
1731
1732 // Apply any CVR qualifiers from the array type to the element type. This
1733 // implements C99 6.7.3p8: "If the specification of an array type includes
1734 // any type qualifiers, the element type is so qualified, not the array type."
1735
1736 // If we get here, we either have type qualifiers on the type, or we have
1737 // sugar such as a typedef in the way. If we have type qualifiers on the type
1738 // we must propagate them down into the elemeng type.
1739 unsigned CVRQuals = T.getCVRQualifiers();
1740 unsigned AddrSpace = 0;
1741 Type *Ty = T.getTypePtr();
1742
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001743 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001744 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001745 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1746 AddrSpace = EXTQT->getAddressSpace();
1747 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001748 } else {
1749 T = Ty->getDesugaredType();
1750 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1751 break;
1752 CVRQuals |= T.getCVRQualifiers();
1753 Ty = T.getTypePtr();
1754 }
1755 }
1756
1757 // If we have a simple case, just return now.
1758 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1759 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1760 return ATy;
1761
1762 // Otherwise, we have an array and we have qualifiers on it. Push the
1763 // qualifiers into the array element type and return a new array type.
1764 // Get the canonical version of the element with the extra qualifiers on it.
1765 // This can recursively sink qualifiers through multiple levels of arrays.
1766 QualType NewEltTy = ATy->getElementType();
1767 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001768 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001769 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1770
1771 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1772 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1773 CAT->getSizeModifier(),
1774 CAT->getIndexTypeQualifier()));
1775 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1776 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1777 IAT->getSizeModifier(),
1778 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001779
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001780 if (const DependentSizedArrayType *DSAT
1781 = dyn_cast<DependentSizedArrayType>(ATy))
1782 return cast<ArrayType>(
1783 getDependentSizedArrayType(NewEltTy,
1784 DSAT->getSizeExpr(),
1785 DSAT->getSizeModifier(),
1786 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001787
Chris Lattnera1923f62008-08-04 07:31:14 +00001788 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1789 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1790 VAT->getSizeModifier(),
1791 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001792}
1793
1794
Chris Lattner19eb97e2008-04-02 05:18:44 +00001795/// getArrayDecayedType - Return the properly qualified result of decaying the
1796/// specified array type to a pointer. This operation is non-trivial when
1797/// handling typedefs etc. The canonical type of "T" must be an array type,
1798/// this returns a pointer to a properly qualified element of the array.
1799///
1800/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1801QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001802 // Get the element type with 'getAsArrayType' so that we don't lose any
1803 // typedefs in the element type of the array. This also handles propagation
1804 // of type qualifiers from the array type into the element type if present
1805 // (C99 6.7.3p8).
1806 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1807 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001808
Chris Lattnera1923f62008-08-04 07:31:14 +00001809 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001810
1811 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001812 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001813}
1814
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001815QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001816 QualType ElemTy = VAT->getElementType();
1817
1818 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1819 return getBaseElementType(VAT);
1820
1821 return ElemTy;
1822}
1823
Chris Lattner4b009652007-07-25 00:24:17 +00001824/// getFloatingRank - Return a relative rank for floating point types.
1825/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001826static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001827 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001828 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001829
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001830 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001831 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001832 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001833 case BuiltinType::Float: return FloatRank;
1834 case BuiltinType::Double: return DoubleRank;
1835 case BuiltinType::LongDouble: return LongDoubleRank;
1836 }
1837}
1838
Steve Narofffa0c4532007-08-27 01:41:48 +00001839/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1840/// point or a complex type (based on typeDomain/typeSize).
1841/// 'typeDomain' is a real floating point or complex type.
1842/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001843QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1844 QualType Domain) const {
1845 FloatingRank EltRank = getFloatingRank(Size);
1846 if (Domain->isComplexType()) {
1847 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001848 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001849 case FloatRank: return FloatComplexTy;
1850 case DoubleRank: return DoubleComplexTy;
1851 case LongDoubleRank: return LongDoubleComplexTy;
1852 }
Chris Lattner4b009652007-07-25 00:24:17 +00001853 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001854
1855 assert(Domain->isRealFloatingType() && "Unknown domain!");
1856 switch (EltRank) {
1857 default: assert(0 && "getFloatingRank(): illegal value for rank");
1858 case FloatRank: return FloatTy;
1859 case DoubleRank: return DoubleTy;
1860 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001861 }
Chris Lattner4b009652007-07-25 00:24:17 +00001862}
1863
Chris Lattner51285d82008-04-06 23:55:33 +00001864/// getFloatingTypeOrder - Compare the rank of the two specified floating
1865/// point types, ignoring the domain of the type (i.e. 'double' ==
1866/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1867/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001868int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1869 FloatingRank LHSR = getFloatingRank(LHS);
1870 FloatingRank RHSR = getFloatingRank(RHS);
1871
1872 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001873 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001874 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001875 return 1;
1876 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001877}
1878
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001879/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1880/// routine will assert if passed a built-in type that isn't an integer or enum,
1881/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001882unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001883 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001884 if (EnumType* ET = dyn_cast<EnumType>(T))
1885 T = ET->getDecl()->getIntegerType().getTypePtr();
1886
1887 // There are two things which impact the integer rank: the width, and
1888 // the ordering of builtins. The builtin ordering is encoded in the
1889 // bottom three bits; the width is encoded in the bits above that.
1890 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1891 return FWIT->getWidth() << 3;
1892 }
1893
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001894 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001895 default: assert(0 && "getIntegerRank(): not a built-in integer");
1896 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001897 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001898 case BuiltinType::Char_S:
1899 case BuiltinType::Char_U:
1900 case BuiltinType::SChar:
1901 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001902 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001903 case BuiltinType::Short:
1904 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001905 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001906 case BuiltinType::Int:
1907 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001908 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001909 case BuiltinType::Long:
1910 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001911 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001912 case BuiltinType::LongLong:
1913 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001914 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001915 }
1916}
1917
Chris Lattner51285d82008-04-06 23:55:33 +00001918/// getIntegerTypeOrder - Returns the highest ranked integer type:
1919/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1920/// LHS < RHS, return -1.
1921int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001922 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1923 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001924 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001925
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001926 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1927 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001928
Chris Lattner51285d82008-04-06 23:55:33 +00001929 unsigned LHSRank = getIntegerRank(LHSC);
1930 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001931
Chris Lattner51285d82008-04-06 23:55:33 +00001932 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1933 if (LHSRank == RHSRank) return 0;
1934 return LHSRank > RHSRank ? 1 : -1;
1935 }
Chris Lattner4b009652007-07-25 00:24:17 +00001936
Chris Lattner51285d82008-04-06 23:55:33 +00001937 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1938 if (LHSUnsigned) {
1939 // If the unsigned [LHS] type is larger, return it.
1940 if (LHSRank >= RHSRank)
1941 return 1;
1942
1943 // If the signed type can represent all values of the unsigned type, it
1944 // wins. Because we are dealing with 2's complement and types that are
1945 // powers of two larger than each other, this is always safe.
1946 return -1;
1947 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001948
Chris Lattner51285d82008-04-06 23:55:33 +00001949 // If the unsigned [RHS] type is larger, return it.
1950 if (RHSRank >= LHSRank)
1951 return -1;
1952
1953 // If the signed type can represent all values of the unsigned type, it
1954 // wins. Because we are dealing with 2's complement and types that are
1955 // powers of two larger than each other, this is always safe.
1956 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001957}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001958
1959// getCFConstantStringType - Return the type used for constant CFStrings.
1960QualType ASTContext::getCFConstantStringType() {
1961 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001962 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001963 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001964 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001965 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001966
1967 // const int *isa;
1968 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001969 // int flags;
1970 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001971 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001972 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001973 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001974 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001975
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001976 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001977 for (unsigned i = 0; i < 4; ++i) {
1978 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1979 SourceLocation(), 0,
1980 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001981 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001982 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001983 }
1984
1985 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001986 }
1987
1988 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001989}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001990
Anders Carlssonf58cac72008-08-30 19:34:46 +00001991QualType ASTContext::getObjCFastEnumerationStateType()
1992{
1993 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001994 ObjCFastEnumerationStateTypeDecl =
1995 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1996 &Idents.get("__objcFastEnumerationState"));
1997
Anders Carlssonf58cac72008-08-30 19:34:46 +00001998 QualType FieldTypes[] = {
1999 UnsignedLongTy,
2000 getPointerType(ObjCIdType),
2001 getPointerType(UnsignedLongTy),
2002 getConstantArrayType(UnsignedLongTy,
2003 llvm::APInt(32, 5), ArrayType::Normal, 0)
2004 };
2005
Douglas Gregor8acb7272008-12-11 16:49:14 +00002006 for (size_t i = 0; i < 4; ++i) {
2007 FieldDecl *Field = FieldDecl::Create(*this,
2008 ObjCFastEnumerationStateTypeDecl,
2009 SourceLocation(), 0,
2010 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002011 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002012 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002013 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00002014
Douglas Gregor8acb7272008-12-11 16:49:14 +00002015 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00002016 }
2017
2018 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2019}
2020
Anders Carlssone3f02572007-10-29 06:33:42 +00002021// This returns true if a type has been typedefed to BOOL:
2022// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00002023static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002024 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00002025 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2026 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002027
2028 return false;
2029}
2030
Ted Kremenek42730c52008-01-07 19:49:32 +00002031/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002032/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00002033int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00002034 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002035
2036 // Make all integer and enum types at least as large as an int
2037 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002038 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002039 // Treat arrays as pointers, since that's how they're passed in.
2040 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002041 sz = getTypeSize(VoidPtrTy);
2042 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002043}
2044
Ted Kremenek42730c52008-01-07 19:49:32 +00002045/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002046/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002047void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00002048 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002049 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002050 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00002051 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002052 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002053 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002054 // Compute size of all parameters.
2055 // Start with computing size of a pointer in number of bytes.
2056 // FIXME: There might(should) be a better way of doing this computation!
2057 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002058 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002059 // The first two arguments (self and _cmd) are pointers; account for
2060 // their size.
2061 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002062 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2063 E = Decl->param_end(); PI != E; ++PI) {
2064 QualType PType = (*PI)->getType();
2065 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002066 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002067 ParmOffset += sz;
2068 }
2069 S += llvm::utostr(ParmOffset);
2070 S += "@0:";
2071 S += llvm::utostr(PtrSize);
2072
2073 // Argument types.
2074 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002075 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2076 E = Decl->param_end(); PI != E; ++PI) {
2077 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002078 QualType PType = PVDecl->getOriginalType();
2079 if (const ArrayType *AT =
2080 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
2081 // Use array's original type only if it has known number of
2082 // elements.
2083 if (!dyn_cast<ConstantArrayType>(AT))
2084 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002085 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002086 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002087 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002088 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002089 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002090 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002091 }
2092}
2093
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002094/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002095/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002096/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2097/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002098/// Property attributes are stored as a comma-delimited C string. The simple
2099/// attributes readonly and bycopy are encoded as single characters. The
2100/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2101/// encoded as single characters, followed by an identifier. Property types
2102/// are also encoded as a parametrized attribute. The characters used to encode
2103/// these attributes are defined by the following enumeration:
2104/// @code
2105/// enum PropertyAttributes {
2106/// kPropertyReadOnly = 'R', // property is read-only.
2107/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2108/// kPropertyByref = '&', // property is a reference to the value last assigned
2109/// kPropertyDynamic = 'D', // property is dynamic
2110/// kPropertyGetter = 'G', // followed by getter selector name
2111/// kPropertySetter = 'S', // followed by setter selector name
2112/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2113/// kPropertyType = 't' // followed by old-style type encoding.
2114/// kPropertyWeak = 'W' // 'weak' property
2115/// kPropertyStrong = 'P' // property GC'able
2116/// kPropertyNonAtomic = 'N' // property non-atomic
2117/// };
2118/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002119void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2120 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002121 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002122 // Collect information from the property implementation decl(s).
2123 bool Dynamic = false;
2124 ObjCPropertyImplDecl *SynthesizePID = 0;
2125
2126 // FIXME: Duplicated code due to poor abstraction.
2127 if (Container) {
2128 if (const ObjCCategoryImplDecl *CID =
2129 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2130 for (ObjCCategoryImplDecl::propimpl_iterator
2131 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2132 ObjCPropertyImplDecl *PID = *i;
2133 if (PID->getPropertyDecl() == PD) {
2134 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2135 Dynamic = true;
2136 } else {
2137 SynthesizePID = PID;
2138 }
2139 }
2140 }
2141 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002142 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002143 for (ObjCCategoryImplDecl::propimpl_iterator
2144 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2145 ObjCPropertyImplDecl *PID = *i;
2146 if (PID->getPropertyDecl() == PD) {
2147 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2148 Dynamic = true;
2149 } else {
2150 SynthesizePID = PID;
2151 }
2152 }
2153 }
2154 }
2155 }
2156
2157 // FIXME: This is not very efficient.
2158 S = "T";
2159
2160 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002161 // GCC has some special rules regarding encoding of properties which
2162 // closely resembles encoding of ivars.
2163 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2164 true /* outermost type */,
2165 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002166
2167 if (PD->isReadOnly()) {
2168 S += ",R";
2169 } else {
2170 switch (PD->getSetterKind()) {
2171 case ObjCPropertyDecl::Assign: break;
2172 case ObjCPropertyDecl::Copy: S += ",C"; break;
2173 case ObjCPropertyDecl::Retain: S += ",&"; break;
2174 }
2175 }
2176
2177 // It really isn't clear at all what this means, since properties
2178 // are "dynamic by default".
2179 if (Dynamic)
2180 S += ",D";
2181
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002182 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2183 S += ",N";
2184
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002185 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2186 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002187 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002188 }
2189
2190 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2191 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002192 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002193 }
2194
2195 if (SynthesizePID) {
2196 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2197 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002198 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002199 }
2200
2201 // FIXME: OBJCGC: weak & strong
2202}
2203
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002204/// getLegacyIntegralTypeEncoding -
2205/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002206/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002207/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2208///
2209void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2210 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2211 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002212 if (BT->getKind() == BuiltinType::ULong &&
2213 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002214 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002215 else
2216 if (BT->getKind() == BuiltinType::Long &&
2217 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002218 PointeeTy = IntTy;
2219 }
2220 }
2221}
2222
Fariborz Jahanian248db262008-01-22 22:44:46 +00002223void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002224 FieldDecl *Field) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002225 // We follow the behavior of gcc, expanding structures which are
2226 // directly pointed to, and expanding embedded structures. Note that
2227 // these rules are sufficient to prevent recursive encoding of the
2228 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002229 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2230 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002231}
2232
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002233static void EncodeBitField(const ASTContext *Context, std::string& S,
2234 FieldDecl *FD) {
2235 const Expr *E = FD->getBitWidth();
2236 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2237 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2238 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2239 S += 'b';
2240 S += llvm::utostr(N);
2241}
2242
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002243void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2244 bool ExpandPointedToStructures,
2245 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002246 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002247 bool OutermostType,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002248 bool EncodingProperty) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002249 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002250 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002251 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002252 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002253 else {
2254 char encoding;
2255 switch (BT->getKind()) {
2256 default: assert(0 && "Unhandled builtin type kind");
2257 case BuiltinType::Void: encoding = 'v'; break;
2258 case BuiltinType::Bool: encoding = 'B'; break;
2259 case BuiltinType::Char_U:
2260 case BuiltinType::UChar: encoding = 'C'; break;
2261 case BuiltinType::UShort: encoding = 'S'; break;
2262 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002263 case BuiltinType::ULong:
2264 encoding =
2265 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2266 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002267 case BuiltinType::ULongLong: encoding = 'Q'; break;
2268 case BuiltinType::Char_S:
2269 case BuiltinType::SChar: encoding = 'c'; break;
2270 case BuiltinType::Short: encoding = 's'; break;
2271 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002272 case BuiltinType::Long:
2273 encoding =
2274 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2275 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002276 case BuiltinType::LongLong: encoding = 'q'; break;
2277 case BuiltinType::Float: encoding = 'f'; break;
2278 case BuiltinType::Double: encoding = 'd'; break;
2279 case BuiltinType::LongDouble: encoding = 'd'; break;
2280 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002281
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002282 S += encoding;
2283 }
Anders Carlsson70e16dd2009-04-09 21:55:45 +00002284 } else if (const ComplexType *CT = T->getAsComplexType()) {
2285 S += 'j';
2286 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2287 false);
2288 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002289 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2290 ExpandPointedToStructures,
2291 ExpandStructures, FD);
2292 if (FD || EncodingProperty) {
2293 // Note that we do extended encoding of protocol qualifer list
2294 // Only when doing ivar or property encoding.
2295 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2296 S += '"';
2297 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2298 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2299 S += '<';
2300 S += Proto->getNameAsString();
2301 S += '>';
2302 }
2303 S += '"';
2304 }
2305 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002306 }
2307 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002308 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002309 bool isReadOnly = false;
2310 // For historical/compatibility reasons, the read-only qualifier of the
2311 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2312 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2313 // Also, do not emit the 'r' for anything but the outermost type!
2314 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2315 if (OutermostType && T.isConstQualified()) {
2316 isReadOnly = true;
2317 S += 'r';
2318 }
2319 }
2320 else if (OutermostType) {
2321 QualType P = PointeeTy;
2322 while (P->getAsPointerType())
2323 P = P->getAsPointerType()->getPointeeType();
2324 if (P.isConstQualified()) {
2325 isReadOnly = true;
2326 S += 'r';
2327 }
2328 }
2329 if (isReadOnly) {
2330 // Another legacy compatibility encoding. Some ObjC qualifier and type
2331 // combinations need to be rearranged.
2332 // Rewrite "in const" from "nr" to "rn"
2333 const char * s = S.c_str();
2334 int len = S.length();
2335 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2336 std::string replace = "rn";
2337 S.replace(S.end()-2, S.end(), replace);
2338 }
2339 }
Steve Naroff17c03822009-02-12 17:52:19 +00002340 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002341 S += '@';
2342 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002343 }
2344 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002345 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002346 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002347 // Another historical/compatibility reason.
2348 // We encode the underlying type which comes out as
2349 // {...};
2350 S += '^';
2351 getObjCEncodingForTypeImpl(PointeeTy, S,
2352 false, ExpandPointedToStructures,
2353 NULL);
2354 return;
2355 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002356 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002357 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002358 const ObjCInterfaceType *OIT =
2359 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002360 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002361 S += '"';
2362 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002363 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2364 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2365 S += '<';
2366 S += Proto->getNameAsString();
2367 S += '>';
2368 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002369 S += '"';
2370 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002371 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002372 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002373 S += '#';
2374 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002375 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002376 S += ':';
2377 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002378 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002379
2380 if (PointeeTy->isCharType()) {
2381 // char pointer types should be encoded as '*' unless it is a
2382 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002383 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002384 S += '*';
2385 return;
2386 }
2387 }
2388
2389 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002390 getLegacyIntegralTypeEncoding(PointeeTy);
2391
2392 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002393 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002394 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002395 } else if (const ArrayType *AT =
2396 // Ignore type qualifiers etc.
2397 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002398 if (isa<IncompleteArrayType>(AT)) {
2399 // Incomplete arrays are encoded as a pointer to the array element.
2400 S += '^';
2401
2402 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2403 false, ExpandStructures, FD);
2404 } else {
2405 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002406
Anders Carlsson858c64d2009-02-22 01:38:57 +00002407 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2408 S += llvm::utostr(CAT->getSize().getZExtValue());
2409 else {
2410 //Variable length arrays are encoded as a regular array with 0 elements.
2411 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2412 S += '0';
2413 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002414
Anders Carlsson858c64d2009-02-22 01:38:57 +00002415 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2416 false, ExpandStructures, FD);
2417 S += ']';
2418 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002419 } else if (T->getAsFunctionType()) {
2420 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002421 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002422 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002423 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002424 // Anonymous structures print as '?'
2425 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2426 S += II->getName();
2427 } else {
2428 S += '?';
2429 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002430 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002431 S += '=';
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002432 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2433 FieldEnd = RDecl->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002434 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002435 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002436 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002437 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002438 S += '"';
2439 }
2440
2441 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002442 if (Field->isBitField()) {
2443 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2444 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002445 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002446 QualType qt = Field->getType();
2447 getLegacyIntegralTypeEncoding(qt);
2448 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002449 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002450 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002451 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002452 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002453 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002454 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002455 if (FD && FD->isBitField())
2456 EncodeBitField(this, S, FD);
2457 else
2458 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002459 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002460 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002461 } else if (T->isObjCInterfaceType()) {
2462 // @encode(class_name)
2463 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2464 S += '{';
2465 const IdentifierInfo *II = OI->getIdentifier();
2466 S += II->getName();
2467 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002468 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002469 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002470 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002471 if (RecFields[i]->isBitField())
2472 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2473 RecFields[i]);
2474 else
2475 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2476 FD);
2477 }
2478 S += '}';
2479 }
2480 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002481 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002482}
2483
Ted Kremenek42730c52008-01-07 19:49:32 +00002484void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002485 std::string& S) const {
2486 if (QT & Decl::OBJC_TQ_In)
2487 S += 'n';
2488 if (QT & Decl::OBJC_TQ_Inout)
2489 S += 'N';
2490 if (QT & Decl::OBJC_TQ_Out)
2491 S += 'o';
2492 if (QT & Decl::OBJC_TQ_Bycopy)
2493 S += 'O';
2494 if (QT & Decl::OBJC_TQ_Byref)
2495 S += 'R';
2496 if (QT & Decl::OBJC_TQ_Oneway)
2497 S += 'V';
2498}
2499
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002500void ASTContext::setBuiltinVaListType(QualType T)
2501{
2502 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2503
2504 BuiltinVaListType = T;
2505}
2506
Ted Kremenek42730c52008-01-07 19:49:32 +00002507void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002508{
Ted Kremenek42730c52008-01-07 19:49:32 +00002509 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002510
2511 // typedef struct objc_object *id;
2512 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002513 // User error - caller will issue diagnostics.
2514 if (!ptr)
2515 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002516 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002517 // User error - caller will issue diagnostics.
2518 if (!rec)
2519 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002520 IdStructType = rec;
2521}
2522
Ted Kremenek42730c52008-01-07 19:49:32 +00002523void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002524{
Ted Kremenek42730c52008-01-07 19:49:32 +00002525 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002526
2527 // typedef struct objc_selector *SEL;
2528 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002529 if (!ptr)
2530 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002531 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002532 if (!rec)
2533 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002534 SelStructType = rec;
2535}
2536
Ted Kremenek42730c52008-01-07 19:49:32 +00002537void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002538{
Ted Kremenek42730c52008-01-07 19:49:32 +00002539 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002540}
2541
Ted Kremenek42730c52008-01-07 19:49:32 +00002542void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002543{
Ted Kremenek42730c52008-01-07 19:49:32 +00002544 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002545
2546 // typedef struct objc_class *Class;
2547 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2548 assert(ptr && "'Class' incorrectly typed");
2549 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2550 assert(rec && "'Class' incorrectly typed");
2551 ClassStructType = rec;
2552}
2553
Ted Kremenek42730c52008-01-07 19:49:32 +00002554void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2555 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002556 "'NSConstantString' type already set!");
2557
Ted Kremenek42730c52008-01-07 19:49:32 +00002558 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002559}
2560
Douglas Gregordd13e842009-03-30 22:58:21 +00002561/// \brief Retrieve the template name that represents a qualified
2562/// template name such as \c std::vector.
2563TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2564 bool TemplateKeyword,
2565 TemplateDecl *Template) {
2566 llvm::FoldingSetNodeID ID;
2567 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2568
2569 void *InsertPos = 0;
2570 QualifiedTemplateName *QTN =
2571 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2572 if (!QTN) {
2573 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2574 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2575 }
2576
2577 return TemplateName(QTN);
2578}
2579
2580/// \brief Retrieve the template name that represents a dependent
2581/// template name such as \c MetaFun::template apply.
2582TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2583 const IdentifierInfo *Name) {
2584 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2585
2586 llvm::FoldingSetNodeID ID;
2587 DependentTemplateName::Profile(ID, NNS, Name);
2588
2589 void *InsertPos = 0;
2590 DependentTemplateName *QTN =
2591 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2592
2593 if (QTN)
2594 return TemplateName(QTN);
2595
2596 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2597 if (CanonNNS == NNS) {
2598 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2599 } else {
2600 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2601 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2602 }
2603
2604 DependentTemplateNames.InsertNode(QTN, InsertPos);
2605 return TemplateName(QTN);
2606}
2607
Douglas Gregorc6507e42008-11-03 14:12:49 +00002608/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002609/// TargetInfo, produce the corresponding type. The unsigned @p Type
2610/// is actually a value of type @c TargetInfo::IntType.
2611QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002612 switch (Type) {
2613 case TargetInfo::NoInt: return QualType();
2614 case TargetInfo::SignedShort: return ShortTy;
2615 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2616 case TargetInfo::SignedInt: return IntTy;
2617 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2618 case TargetInfo::SignedLong: return LongTy;
2619 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2620 case TargetInfo::SignedLongLong: return LongLongTy;
2621 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2622 }
2623
2624 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002625 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002626}
Ted Kremenek118930e2008-07-24 23:58:27 +00002627
2628//===----------------------------------------------------------------------===//
2629// Type Predicates.
2630//===----------------------------------------------------------------------===//
2631
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002632/// isObjCNSObjectType - Return true if this is an NSObject object using
2633/// NSObject attribute on a c-style pointer type.
2634/// FIXME - Make it work directly on types.
2635///
2636bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2637 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2638 if (TypedefDecl *TD = TDT->getDecl())
2639 if (TD->getAttr<ObjCNSObjectAttr>())
2640 return true;
2641 }
2642 return false;
2643}
2644
Ted Kremenek118930e2008-07-24 23:58:27 +00002645/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2646/// to an object type. This includes "id" and "Class" (two 'special' pointers
2647/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2648/// ID type).
2649bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002650 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002651 return true;
2652
Steve Naroffd9e00802008-10-21 18:24:04 +00002653 // Blocks are objects.
2654 if (Ty->isBlockPointerType())
2655 return true;
2656
2657 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002658 if (!Ty->isPointerType())
2659 return false;
2660
2661 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2662 // pointer types. This looks for the typedef specifically, not for the
2663 // underlying type.
Eli Friedman9c2b33f2009-03-22 23:00:19 +00002664 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2665 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002666 return true;
2667
2668 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002669 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2670 return true;
2671
2672 // If is has NSObject attribute, OK as well.
2673 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002674}
2675
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002676/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2677/// garbage collection attribute.
2678///
2679QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002680 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002681 if (getLangOptions().ObjC1 &&
2682 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002683 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002684 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002685 // (or pointers to them) be treated as though they were declared
2686 // as __strong.
2687 if (GCAttrs == QualType::GCNone) {
2688 if (isObjCObjectPointerType(Ty))
2689 GCAttrs = QualType::Strong;
2690 else if (Ty->isPointerType())
2691 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2692 }
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002693 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002694 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002695}
2696
Chris Lattner6ff358b2008-04-07 06:51:04 +00002697//===----------------------------------------------------------------------===//
2698// Type Compatibility Testing
2699//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002700
Steve Naroff3454b6c2008-09-04 15:10:53 +00002701/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002702/// block types. Types must be strictly compatible here. For example,
2703/// C unfortunately doesn't produce an error for the following:
2704///
2705/// int (*emptyArgFunc)();
2706/// int (*intArgList)(int) = emptyArgFunc;
2707///
2708/// For blocks, we will produce an error for the following (similar to C++):
2709///
2710/// int (^emptyArgBlock)();
2711/// int (^intArgBlock)(int) = emptyArgBlock;
2712///
2713/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2714///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002715bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002716 const FunctionType *lbase = lhs->getAsFunctionType();
2717 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002718 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2719 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stump896ceed2009-04-01 01:17:39 +00002720 if (lproto && rproto == 0)
2721 return false;
2722 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff3454b6c2008-09-04 15:10:53 +00002723}
2724
Chris Lattner6ff358b2008-04-07 06:51:04 +00002725/// areCompatVectorTypes - Return true if the two specified vector types are
2726/// compatible.
2727static bool areCompatVectorTypes(const VectorType *LHS,
2728 const VectorType *RHS) {
2729 assert(LHS->isCanonical() && RHS->isCanonical());
2730 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002731 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002732}
2733
Eli Friedman0d9549b2008-08-22 00:56:42 +00002734/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002735/// compatible for assignment from RHS to LHS. This handles validation of any
2736/// protocol qualifiers on the LHS or RHS.
2737///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002738bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2739 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002740 // Verify that the base decls are compatible: the RHS must be a subclass of
2741 // the LHS.
2742 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2743 return false;
2744
2745 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2746 // protocol qualified at all, then we are good.
2747 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2748 return true;
2749
2750 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2751 // isn't a superset.
2752 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2753 return true; // FIXME: should return false!
2754
2755 // Finally, we must have two protocol-qualified interfaces.
2756 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2757 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002758
Steve Naroff98e71b82009-03-01 16:12:44 +00002759 // All LHS protocols must have a presence on the RHS.
2760 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002761
Steve Naroff98e71b82009-03-01 16:12:44 +00002762 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2763 LHSPE = LHSP->qual_end();
2764 LHSPI != LHSPE; LHSPI++) {
2765 bool RHSImplementsProtocol = false;
2766
2767 // If the RHS doesn't implement the protocol on the left, the types
2768 // are incompatible.
2769 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2770 RHSPE = RHSP->qual_end();
2771 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2772 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2773 RHSImplementsProtocol = true;
2774 }
2775 // FIXME: For better diagnostics, consider passing back the protocol name.
2776 if (!RHSImplementsProtocol)
2777 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002778 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002779 // The RHS implements all protocols listed on the LHS.
2780 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002781}
2782
Steve Naroff17c03822009-02-12 17:52:19 +00002783bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2784 // get the "pointed to" types
2785 const PointerType *LHSPT = LHS->getAsPointerType();
2786 const PointerType *RHSPT = RHS->getAsPointerType();
2787
2788 if (!LHSPT || !RHSPT)
2789 return false;
2790
2791 QualType lhptee = LHSPT->getPointeeType();
2792 QualType rhptee = RHSPT->getPointeeType();
2793 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2794 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2795 // ID acts sort of like void* for ObjC interfaces
2796 if (LHSIface && isObjCIdStructType(rhptee))
2797 return true;
2798 if (RHSIface && isObjCIdStructType(lhptee))
2799 return true;
2800 if (!LHSIface || !RHSIface)
2801 return false;
2802 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2803 canAssignObjCInterfaces(RHSIface, LHSIface);
2804}
2805
Steve Naroff85f0dc52007-10-15 20:41:53 +00002806/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2807/// both shall have the identically qualified version of a compatible type.
2808/// C99 6.2.7p1: Two types have compatible types if their types are the
2809/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002810bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2811 return !mergeTypes(LHS, RHS).isNull();
2812}
2813
2814QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2815 const FunctionType *lbase = lhs->getAsFunctionType();
2816 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002817 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2818 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002819 bool allLTypes = true;
2820 bool allRTypes = true;
2821
2822 // Check return type
2823 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2824 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002825 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2826 allLTypes = false;
2827 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2828 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002829
2830 if (lproto && rproto) { // two C99 style function prototypes
2831 unsigned lproto_nargs = lproto->getNumArgs();
2832 unsigned rproto_nargs = rproto->getNumArgs();
2833
2834 // Compatible functions must have the same number of arguments
2835 if (lproto_nargs != rproto_nargs)
2836 return QualType();
2837
2838 // Variadic and non-variadic functions aren't compatible
2839 if (lproto->isVariadic() != rproto->isVariadic())
2840 return QualType();
2841
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002842 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2843 return QualType();
2844
Eli Friedman0d9549b2008-08-22 00:56:42 +00002845 // Check argument compatibility
2846 llvm::SmallVector<QualType, 10> types;
2847 for (unsigned i = 0; i < lproto_nargs; i++) {
2848 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2849 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2850 QualType argtype = mergeTypes(largtype, rargtype);
2851 if (argtype.isNull()) return QualType();
2852 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002853 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2854 allLTypes = false;
2855 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2856 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002857 }
2858 if (allLTypes) return lhs;
2859 if (allRTypes) return rhs;
2860 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002861 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002862 }
2863
2864 if (lproto) allRTypes = false;
2865 if (rproto) allLTypes = false;
2866
Douglas Gregor4fa58902009-02-26 23:50:07 +00002867 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002868 if (proto) {
2869 if (proto->isVariadic()) return QualType();
2870 // Check that the types are compatible with the types that
2871 // would result from default argument promotions (C99 6.7.5.3p15).
2872 // The only types actually affected are promotable integer
2873 // types and floats, which would be passed as a different
2874 // type depending on whether the prototype is visible.
2875 unsigned proto_nargs = proto->getNumArgs();
2876 for (unsigned i = 0; i < proto_nargs; ++i) {
2877 QualType argTy = proto->getArgType(i);
2878 if (argTy->isPromotableIntegerType() ||
2879 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2880 return QualType();
2881 }
2882
2883 if (allLTypes) return lhs;
2884 if (allRTypes) return rhs;
2885 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002886 proto->getNumArgs(), lproto->isVariadic(),
2887 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002888 }
2889
2890 if (allLTypes) return lhs;
2891 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002892 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002893}
2894
2895QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002896 // C++ [expr]: If an expression initially has the type "reference to T", the
2897 // type is adjusted to "T" prior to any further analysis, the expression
2898 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002899 // expression is an lvalue unless the reference is an rvalue reference and
2900 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002901 // FIXME: C++ shouldn't be going through here! The rules are different
2902 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00002903 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2904 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002905 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002906 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002907 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002908 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002909
Eli Friedman0d9549b2008-08-22 00:56:42 +00002910 QualType LHSCan = getCanonicalType(LHS),
2911 RHSCan = getCanonicalType(RHS);
2912
2913 // If two types are identical, they are compatible.
2914 if (LHSCan == RHSCan)
2915 return LHS;
2916
2917 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002918 // Note that we handle extended qualifiers later, in the
2919 // case for ExtQualType.
2920 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002921 return QualType();
2922
2923 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2924 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2925
Chris Lattnerc38d4522008-01-14 05:45:46 +00002926 // We want to consider the two function types to be the same for these
2927 // comparisons, just force one to the other.
2928 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2929 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002930
2931 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002932 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2933 LHSClass = Type::ConstantArray;
2934 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2935 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002936
Nate Begemanaf6ed502008-04-18 23:10:10 +00002937 // Canonicalize ExtVector -> Vector.
2938 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2939 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002940
Chris Lattner7cdcb252008-04-07 06:38:24 +00002941 // Consider qualified interfaces and interfaces the same.
2942 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2943 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002944
Chris Lattnerb5709e22008-04-07 05:43:21 +00002945 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002946 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002947 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2948 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2949
2950 // ID acts sort of like void* for ObjC interfaces
2951 if (LHSIface && isObjCIdStructType(RHS))
2952 return LHS;
2953 if (RHSIface && isObjCIdStructType(LHS))
2954 return RHS;
2955
Steve Naroff28ceff72008-12-10 22:14:21 +00002956 // ID is compatible with all qualified id types.
2957 if (LHS->isObjCQualifiedIdType()) {
2958 if (const PointerType *PT = RHS->getAsPointerType()) {
2959 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002960 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002961 return LHS;
2962 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2963 // Unfortunately, this API is part of Sema (which we don't have access
2964 // to. Need to refactor. The following check is insufficient, since we
2965 // need to make sure the class implements the protocol.
2966 if (pType->isObjCInterfaceType())
2967 return LHS;
2968 }
2969 }
2970 if (RHS->isObjCQualifiedIdType()) {
2971 if (const PointerType *PT = LHS->getAsPointerType()) {
2972 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002973 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002974 return RHS;
2975 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2976 // Unfortunately, this API is part of Sema (which we don't have access
2977 // to. Need to refactor. The following check is insufficient, since we
2978 // need to make sure the class implements the protocol.
2979 if (pType->isObjCInterfaceType())
2980 return RHS;
2981 }
2982 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002983 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2984 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002985 if (const EnumType* ETy = LHS->getAsEnumType()) {
2986 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2987 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002988 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002989 if (const EnumType* ETy = RHS->getAsEnumType()) {
2990 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2991 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002992 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002993
Eli Friedman0d9549b2008-08-22 00:56:42 +00002994 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002995 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002996
Steve Naroffc88babe2008-01-09 22:43:08 +00002997 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002998 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00002999#define TYPE(Class, Base)
3000#define ABSTRACT_TYPE(Class, Base)
3001#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3002#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3003#include "clang/AST/TypeNodes.def"
3004 assert(false && "Non-canonical and dependent types shouldn't get here");
3005 return QualType();
3006
Sebastian Redlce6fff02009-03-16 23:22:08 +00003007 case Type::LValueReference:
3008 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003009 case Type::MemberPointer:
3010 assert(false && "C++ should never be in mergeTypes");
3011 return QualType();
3012
3013 case Type::IncompleteArray:
3014 case Type::VariableArray:
3015 case Type::FunctionProto:
3016 case Type::ExtVector:
3017 case Type::ObjCQualifiedInterface:
3018 assert(false && "Types are eliminated above");
3019 return QualType();
3020
Chris Lattnerc38d4522008-01-14 05:45:46 +00003021 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003022 {
3023 // Merge two pointer types, while trying to preserve typedef info
3024 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3025 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3026 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3027 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003028 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3029 return LHS;
3030 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3031 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003032 return getPointerType(ResultType);
3033 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003034 case Type::BlockPointer:
3035 {
3036 // Merge two block pointer types, while trying to preserve typedef info
3037 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3038 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3039 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3040 if (ResultType.isNull()) return QualType();
3041 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3042 return LHS;
3043 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3044 return RHS;
3045 return getBlockPointerType(ResultType);
3046 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003047 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003048 {
3049 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3050 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3051 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3052 return QualType();
3053
3054 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3055 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3056 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3057 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003058 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3059 return LHS;
3060 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3061 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003062 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3063 ArrayType::ArraySizeModifier(), 0);
3064 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3065 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003066 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3067 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003068 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3069 return LHS;
3070 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3071 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003072 if (LVAT) {
3073 // FIXME: This isn't correct! But tricky to implement because
3074 // the array's size has to be the size of LHS, but the type
3075 // has to be different.
3076 return LHS;
3077 }
3078 if (RVAT) {
3079 // FIXME: This isn't correct! But tricky to implement because
3080 // the array's size has to be the size of RHS, but the type
3081 // has to be different.
3082 return RHS;
3083 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003084 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3085 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003086 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003087 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003088 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003089 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003090 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003091 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003092 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003093 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3094 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003095 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003096 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003097 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003098 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003099 case Type::Complex:
3100 // Distinct complex types are incompatible.
3101 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003102 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003103 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003104 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3105 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003106 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003107 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003108 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003109 // FIXME: This should be type compatibility, e.g. whether
3110 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003111 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3112 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3113 if (LHSIface && RHSIface &&
3114 canAssignObjCInterfaces(LHSIface, RHSIface))
3115 return LHS;
3116
Eli Friedman0d9549b2008-08-22 00:56:42 +00003117 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003118 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003119 case Type::ObjCQualifiedId:
3120 // Distinct qualified id's are not compatible.
3121 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003122 case Type::FixedWidthInt:
3123 // Distinct fixed-width integers are not compatible.
3124 return QualType();
3125 case Type::ObjCQualifiedClass:
3126 // Distinct qualified classes are not compatible.
3127 return QualType();
3128 case Type::ExtQual:
3129 // FIXME: ExtQual types can be compatible even if they're not
3130 // identical!
3131 return QualType();
3132 // First attempt at an implementation, but I'm not really sure it's
3133 // right...
3134#if 0
3135 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3136 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3137 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3138 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3139 return QualType();
3140 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3141 LHSBase = QualType(LQual->getBaseType(), 0);
3142 RHSBase = QualType(RQual->getBaseType(), 0);
3143 ResultType = mergeTypes(LHSBase, RHSBase);
3144 if (ResultType.isNull()) return QualType();
3145 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3146 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3147 return LHS;
3148 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3149 return RHS;
3150 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3151 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3152 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3153 return ResultType;
3154#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003155
3156 case Type::TemplateSpecialization:
3157 assert(false && "Dependent types have no size");
3158 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003159 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003160
3161 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003162}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003163
Chris Lattner1d78a862008-04-07 07:01:58 +00003164//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003165// Integer Predicates
3166//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003167
Eli Friedman0832dbc2008-06-28 06:23:08 +00003168unsigned ASTContext::getIntWidth(QualType T) {
3169 if (T == BoolTy)
3170 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003171 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3172 return FWIT->getWidth();
3173 }
3174 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003175 return (unsigned)getTypeSize(T);
3176}
3177
3178QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3179 assert(T->isSignedIntegerType() && "Unexpected type");
3180 if (const EnumType* ETy = T->getAsEnumType())
3181 T = ETy->getDecl()->getIntegerType();
3182 const BuiltinType* BTy = T->getAsBuiltinType();
3183 assert (BTy && "Unexpected signed integer type");
3184 switch (BTy->getKind()) {
3185 case BuiltinType::Char_S:
3186 case BuiltinType::SChar:
3187 return UnsignedCharTy;
3188 case BuiltinType::Short:
3189 return UnsignedShortTy;
3190 case BuiltinType::Int:
3191 return UnsignedIntTy;
3192 case BuiltinType::Long:
3193 return UnsignedLongTy;
3194 case BuiltinType::LongLong:
3195 return UnsignedLongLongTy;
3196 default:
3197 assert(0 && "Unexpected signed integer type");
3198 return QualType();
3199 }
3200}
3201
3202
3203//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00003204// Serialization Support
3205//===----------------------------------------------------------------------===//
3206
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003207enum {
3208 BasicMetadataBlock = 1,
3209 ASTContextBlock = 2,
3210 DeclsBlock = 3
3211};
3212
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003213void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3214 // Create bitstream.
3215 llvm::BitstreamWriter Stream(Buffer);
3216
3217 // Emit the preamble.
3218 Stream.Emit((unsigned)'B', 8);
3219 Stream.Emit((unsigned)'C', 8);
3220 Stream.Emit(0xC, 4);
3221 Stream.Emit(0xF, 4);
3222 Stream.Emit(0xE, 4);
3223 Stream.Emit(0x0, 4);
3224
3225 // Create serializer.
3226 llvm::Serializer S(Stream);
3227
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003228 // ===---------------------------------------------------===/
3229 // Serialize the "Translation Unit" metadata.
3230 // ===---------------------------------------------------===/
3231
3232 // Emit ASTContext.
3233 S.EnterBlock(ASTContextBlock);
3234 S.EmitOwnedPtr(this);
3235 S.ExitBlock(); // exit "ASTContextBlock"
3236
3237 S.EnterBlock(BasicMetadataBlock);
3238
3239 // Block for SourceManager and Target. Allows easy skipping
3240 // around to the block for the Selectors during deserialization.
3241 S.EnterBlock();
3242
3243 // Emit the SourceManager.
3244 S.Emit(getSourceManager());
3245
3246 // Emit the Target.
3247 S.EmitPtr(&Target);
3248 S.EmitCStr(Target.getTargetTriple());
3249
3250 S.ExitBlock(); // exit "SourceManager and Target Block"
3251
3252 // Emit the Selectors.
3253 S.Emit(Selectors);
3254
3255 // Emit the Identifier Table.
3256 S.Emit(Idents);
3257
3258 S.ExitBlock(); // exit "BasicMetadataBlock"
3259}
3260
3261
Ted Kremenek738e6c02007-10-31 17:10:13 +00003262/// Emit - Serialize an ASTContext object to Bitcode.
3263void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00003264 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00003265 S.EmitRef(SourceMgr);
3266 S.EmitRef(Target);
3267 S.EmitRef(Idents);
3268 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003269
Ted Kremenek68228a92007-10-31 22:44:07 +00003270 // Emit the size of the type vector so that we can reserve that size
3271 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003272 S.EmitInt(Types.size());
3273
Ted Kremenek034a78c2007-11-13 22:02:55 +00003274 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3275 I!=E;++I)
3276 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003277
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003278 S.EmitOwnedPtr(TUDecl);
3279
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003280 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003281}
3282
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003283
3284ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3285 FileManager &FMgr) {
3286 // Check if the file is of the proper length.
3287 if (Buffer.getBufferSize() & 0x3) {
3288 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3289 return 0;
3290 }
3291
3292 // Create the bitstream reader.
3293 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3294 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3295
3296 if (Stream.Read(8) != 'B' ||
3297 Stream.Read(8) != 'C' ||
3298 Stream.Read(4) != 0xC ||
3299 Stream.Read(4) != 0xF ||
3300 Stream.Read(4) != 0xE ||
3301 Stream.Read(4) != 0x0) {
3302 // FIXME: Provide diagnostic.
3303 return NULL;
3304 }
3305
3306 // Create the deserializer.
3307 llvm::Deserializer Dezr(Stream);
3308
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003309 // ===---------------------------------------------------===/
3310 // Deserialize the "Translation Unit" metadata.
3311 // ===---------------------------------------------------===/
3312
3313 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3314 // (which will appear earlier) and record its location.
3315
3316 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3317 assert (FoundBlock);
3318
3319 llvm::Deserializer::Location ASTContextBlockLoc =
3320 Dezr.getCurrentBlockLocation();
3321
3322 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3323 assert (FoundBlock);
3324
3325 // Read the SourceManager.
3326 SourceManager::CreateAndRegister(Dezr, FMgr);
3327
3328 { // Read the TargetInfo.
3329 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3330 char* triple = Dezr.ReadCStr(NULL,0,true);
3331 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3332 delete [] triple;
3333 }
3334
3335 // For Selectors, we must read the identifier table first because the
3336 // SelectorTable depends on the identifiers being already deserialized.
3337 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3338 Dezr.SkipBlock();
3339
3340 // Read the identifier table.
3341 IdentifierTable::CreateAndRegister(Dezr);
3342
3343 // Now jump back and read the selectors.
3344 Dezr.JumpTo(SelectorBlkLoc);
3345 SelectorTable::CreateAndRegister(Dezr);
3346
3347 // Now jump back to ASTContextBlock and read the ASTContext.
3348 Dezr.JumpTo(ASTContextBlockLoc);
3349 return Dezr.ReadOwnedPtr<ASTContext>();
3350}
3351
Ted Kremenekacba3612007-11-13 00:25:37 +00003352ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00003353
3354 // Read the language options.
3355 LangOptions LOpts;
3356 LOpts.Read(D);
3357
Ted Kremenek68228a92007-10-31 22:44:07 +00003358 SourceManager &SM = D.ReadRef<SourceManager>();
3359 TargetInfo &t = D.ReadRef<TargetInfo>();
3360 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3361 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00003362
Ted Kremenek68228a92007-10-31 22:44:07 +00003363 unsigned size_reserve = D.ReadInt();
3364
Douglas Gregor24afd4a2008-11-17 14:58:09 +00003365 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3366 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00003367
Ted Kremenek034a78c2007-11-13 22:02:55 +00003368 for (unsigned i = 0; i < size_reserve; ++i)
3369 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00003370
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003371 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3372
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003373 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00003374
3375 return A;
3376}
Douglas Gregorc34897d2009-04-09 22:27:44 +00003377
3378ExternalASTSource::~ExternalASTSource() { }
3379
3380void ExternalASTSource::PrintStats() { }