blob: aaec98830e5d4a3a32d2e244ee6c883f207086b3 [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"
19#include "clang/AST/RecordLayout.h"
Chris Lattnerb09b31d2009-03-28 03:45:20 +000020#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000021#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000022#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000023#include "llvm/Bitcode/Serialize.h"
24#include "llvm/Bitcode/Deserialize.h"
Nate Begeman7903d052009-01-18 06:42:49 +000025#include "llvm/Support/MathExtras.h"
Chris Lattnerf4fbc442009-03-28 04:27:18 +000026#include "llvm/Support/MemoryBuffer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000027using namespace clang;
28
29enum FloatingRank {
30 FloatRank, DoubleRank, LongDoubleRank
31};
32
Chris Lattner2fda0ed2008-10-05 17:34:18 +000033ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
34 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000035 IdentifierTable &idents, SelectorTable &sels,
Steve Naroff207b9ec2009-01-27 23:20:32 +000036 bool FreeMem, unsigned size_reserve) :
Douglas Gregor1e589cc2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Chris Lattner9c18fde2009-03-28 01:44:40 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels) {
Daniel Dunbarde300732008-08-11 04:54:23 +000040 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Chris Lattner911b8672009-03-13 22:38:49 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.NoBuiltin);
Daniel Dunbarde300732008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
44}
45
Chris Lattner4b009652007-07-25 00:24:17 +000046ASTContext::~ASTContext() {
47 // Deallocate all the types.
48 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000049 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000050 Types.pop_back();
51 }
Eli Friedman65489b72008-05-27 03:08:09 +000052
Nuno Lopes355a8682008-12-17 22:30:25 +000053 {
54 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
55 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
56 while (I != E) {
57 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
58 delete R;
59 }
60 }
61
62 {
63 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
64 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
65 while (I != E) {
66 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
67 delete R;
68 }
69 }
70
71 {
Chris Lattner608c1e32009-03-31 09:24:30 +000072 llvm::DenseMap<const ObjCInterfaceDecl*, RecordDecl*>::iterator
Nuno Lopes355a8682008-12-17 22:30:25 +000073 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
74 while (I != E) {
Chris Lattner608c1e32009-03-31 09:24:30 +000075 RecordDecl *R = (I++)->second;
Nuno Lopes355a8682008-12-17 22:30:25 +000076 R->Destroy(*this);
77 }
78 }
79
Douglas Gregor1e589cc2009-03-26 23:50:42 +000080 // Destroy nested-name-specifiers.
Douglas Gregor3c4eae52009-03-27 23:54:10 +000081 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
82 NNS = NestedNameSpecifiers.begin(),
83 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregorbccd97c2009-03-27 23:25:45 +000084 NNS != NNSEnd;
Douglas Gregor3c4eae52009-03-27 23:54:10 +000085 /* Increment in loop */)
86 (*NNS++).Destroy(*this);
Douglas Gregor1e589cc2009-03-26 23:50:42 +000087
88 if (GlobalNestedNameSpecifier)
89 GlobalNestedNameSpecifier->Destroy(*this);
90
Eli Friedman65489b72008-05-27 03:08:09 +000091 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000092}
93
94void ASTContext::PrintStats() const {
95 fprintf(stderr, "*** AST Context Stats:\n");
96 fprintf(stderr, " %d types total.\n", (int)Types.size());
97 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +000098 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +000099 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
100 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
101
Chris Lattner4b009652007-07-25 00:24:17 +0000102 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000103 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
104 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000105 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000106 unsigned NumExtQual = 0;
107
Chris Lattner4b009652007-07-25 00:24:17 +0000108 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
109 Type *T = Types[i];
110 if (isa<BuiltinType>(T))
111 ++NumBuiltin;
112 else if (isa<PointerType>(T))
113 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +0000114 else if (isa<BlockPointerType>(T))
115 ++NumBlockPointer;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000116 else if (isa<LValueReferenceType>(T))
117 ++NumLValueReference;
118 else if (isa<RValueReferenceType>(T))
119 ++NumRValueReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000120 else if (isa<MemberPointerType>(T))
121 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000122 else if (isa<ComplexType>(T))
123 ++NumComplex;
124 else if (isa<ArrayType>(T))
125 ++NumArray;
126 else if (isa<VectorType>(T))
127 ++NumVector;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000128 else if (isa<FunctionNoProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000129 ++NumFunctionNP;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000130 else if (isa<FunctionProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000131 ++NumFunctionP;
132 else if (isa<TypedefType>(T))
133 ++NumTypeName;
134 else if (TagType *TT = dyn_cast<TagType>(T)) {
135 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000136 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000137 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000138 case TagDecl::TK_struct: ++NumTagStruct; break;
139 case TagDecl::TK_union: ++NumTagUnion; break;
140 case TagDecl::TK_class: ++NumTagClass; break;
141 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000142 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000143 } else if (isa<ObjCInterfaceType>(T))
144 ++NumObjCInterfaces;
145 else if (isa<ObjCQualifiedInterfaceType>(T))
146 ++NumObjCQualifiedInterfaces;
147 else if (isa<ObjCQualifiedIdType>(T))
148 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000149 else if (isa<TypeOfType>(T))
150 ++NumTypeOfTypes;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000151 else if (isa<TypeOfExprType>(T))
152 ++NumTypeOfExprTypes;
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000153 else if (isa<ExtQualType>(T))
154 ++NumExtQual;
Steve Naroff948fd372007-09-17 14:16:13 +0000155 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000156 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000157 assert(0 && "Unknown type!");
158 }
159 }
160
161 fprintf(stderr, " %d builtin types\n", NumBuiltin);
162 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000163 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000164 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
165 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000166 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000167 fprintf(stderr, " %d complex types\n", NumComplex);
168 fprintf(stderr, " %d array types\n", NumArray);
169 fprintf(stderr, " %d vector types\n", NumVector);
170 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
171 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
172 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
173 fprintf(stderr, " %d tagged types\n", NumTagged);
174 fprintf(stderr, " %d struct types\n", NumTagStruct);
175 fprintf(stderr, " %d union types\n", NumTagUnion);
176 fprintf(stderr, " %d class types\n", NumTagClass);
177 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000178 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000179 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000180 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000181 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000182 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000183 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000184 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000185 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000186
Chris Lattner4b009652007-07-25 00:24:17 +0000187 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
188 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
189 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlce6fff02009-03-16 23:22:08 +0000190 NumLValueReference*sizeof(LValueReferenceType)+
191 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000192 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor4fa58902009-02-26 23:50:07 +0000193 NumFunctionP*sizeof(FunctionProtoType)+
194 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroffe0430632008-05-21 15:59:22 +0000195 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000196 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
197 NumExtQual*sizeof(ExtQualType)));
Chris Lattner4b009652007-07-25 00:24:17 +0000198}
199
200
201void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000202 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000203}
204
Chris Lattner4b009652007-07-25 00:24:17 +0000205void ASTContext::InitBuiltinTypes() {
206 assert(VoidTy.isNull() && "Context reinitialized?");
207
208 // C99 6.2.5p19.
209 InitBuiltinType(VoidTy, BuiltinType::Void);
210
211 // C99 6.2.5p2.
212 InitBuiltinType(BoolTy, BuiltinType::Bool);
213 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000214 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000215 InitBuiltinType(CharTy, BuiltinType::Char_S);
216 else
217 InitBuiltinType(CharTy, BuiltinType::Char_U);
218 // C99 6.2.5p4.
219 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
220 InitBuiltinType(ShortTy, BuiltinType::Short);
221 InitBuiltinType(IntTy, BuiltinType::Int);
222 InitBuiltinType(LongTy, BuiltinType::Long);
223 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
224
225 // C99 6.2.5p6.
226 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
227 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
228 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
229 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
230 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
231
232 // C99 6.2.5p10.
233 InitBuiltinType(FloatTy, BuiltinType::Float);
234 InitBuiltinType(DoubleTy, BuiltinType::Double);
235 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000236
Chris Lattnere1dafe72009-02-26 23:43:47 +0000237 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
238 InitBuiltinType(WCharTy, BuiltinType::WChar);
239 else // C99
240 WCharTy = getFromTargetType(Target.getWCharType());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000241
Douglas Gregord2baafd2008-10-21 16:13:35 +0000242 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000243 InitBuiltinType(OverloadTy, BuiltinType::Overload);
244
245 // Placeholder type for type-dependent expressions whose type is
246 // completely unknown. No code should ever check a type against
247 // DependentTy and users should never see it; however, it is here to
248 // help diagnose failures to properly check for type-dependent
249 // expressions.
250 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000251
Chris Lattner4b009652007-07-25 00:24:17 +0000252 // C99 6.2.5p11.
253 FloatComplexTy = getComplexType(FloatTy);
254 DoubleComplexTy = getComplexType(DoubleTy);
255 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000256
Steve Naroff9d12c902007-10-15 14:41:52 +0000257 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000258 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000259 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000260 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000261 ClassStructType = 0;
262
Ted Kremenek42730c52008-01-07 19:49:32 +0000263 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000264
265 // void * type
266 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000267}
268
269//===----------------------------------------------------------------------===//
270// Type Sizing and Analysis
271//===----------------------------------------------------------------------===//
272
Chris Lattner2a674dc2008-06-30 18:32:54 +0000273/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
274/// scalar floating point type.
275const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
276 const BuiltinType *BT = T->getAsBuiltinType();
277 assert(BT && "Not a floating point type!");
278 switch (BT->getKind()) {
279 default: assert(0 && "Not a floating point type!");
280 case BuiltinType::Float: return Target.getFloatFormat();
281 case BuiltinType::Double: return Target.getDoubleFormat();
282 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
283 }
284}
285
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000286/// getDeclAlign - Return a conservative estimate of the alignment of the
287/// specified decl. Note that bitfields do not have a valid alignment, so
288/// this method will assert on them.
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +0000289unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman0ee57322009-02-22 02:56:25 +0000290 unsigned Align = Target.getCharWidth();
291
292 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
293 Align = std::max(Align, AA->getAlignment());
294
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000295 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
296 QualType T = VD->getType();
297 // Incomplete or function types default to 1.
Eli Friedman0ee57322009-02-22 02:56:25 +0000298 if (!T->isIncompleteType() && !T->isFunctionType()) {
299 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
300 T = cast<ArrayType>(T)->getElementType();
301
302 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
303 }
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000304 }
Eli Friedman0ee57322009-02-22 02:56:25 +0000305
306 return Align / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000307}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000308
Chris Lattner4b009652007-07-25 00:24:17 +0000309/// getTypeSize - Return the size of the specified type, in bits. This method
310/// does not work on incomplete types.
311std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000312ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000313 T = getCanonicalType(T);
Mike Stump44d1f402009-02-27 18:32:39 +0000314 uint64_t Width=0;
315 unsigned Align=8;
Chris Lattner4b009652007-07-25 00:24:17 +0000316 switch (T->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000317#define TYPE(Class, Base)
318#define ABSTRACT_TYPE(Class, Base)
319#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
320#define DEPENDENT_TYPE(Class, Base) case Type::Class:
321#include "clang/AST/TypeNodes.def"
322 assert(false && "Should not see non-canonical or dependent types");
323 break;
324
Chris Lattner4b009652007-07-25 00:24:17 +0000325 case Type::FunctionNoProto:
326 case Type::FunctionProto:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000327 case Type::IncompleteArray:
Chris Lattner4b009652007-07-25 00:24:17 +0000328 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000329 case Type::VariableArray:
330 assert(0 && "VLAs not implemented yet!");
331 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000332 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000333
Chris Lattner8cd0e932008-03-05 18:54:05 +0000334 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000335 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000336 Align = EltInfo.second;
337 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000338 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000339 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000340 case Type::Vector: {
341 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000342 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000343 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000344 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000345 // If the alignment is not a power of 2, round up to the next power of 2.
346 // This happens for non-power-of-2 length vectors.
347 // FIXME: this should probably be a target property.
348 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000349 break;
350 }
351
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000352 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000353 switch (cast<BuiltinType>(T)->getKind()) {
354 default: assert(0 && "Unknown builtin type!");
355 case BuiltinType::Void:
356 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000357 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000358 Width = Target.getBoolWidth();
359 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000360 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000361 case BuiltinType::Char_S:
362 case BuiltinType::Char_U:
363 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000364 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000365 Width = Target.getCharWidth();
366 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000367 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000368 case BuiltinType::WChar:
369 Width = Target.getWCharWidth();
370 Align = Target.getWCharAlign();
371 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000372 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000373 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000374 Width = Target.getShortWidth();
375 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000376 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000377 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000378 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000379 Width = Target.getIntWidth();
380 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000381 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000382 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000383 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000384 Width = Target.getLongWidth();
385 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000386 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000387 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000388 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000389 Width = Target.getLongLongWidth();
390 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000391 break;
392 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000393 Width = Target.getFloatWidth();
394 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000395 break;
396 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000397 Width = Target.getDoubleWidth();
398 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000399 break;
400 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000401 Width = Target.getLongDoubleWidth();
402 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000403 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000404 }
405 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000406 case Type::FixedWidthInt:
407 // FIXME: This isn't precisely correct; the width/alignment should depend
408 // on the available types for the target
409 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattnere9174982009-02-15 21:20:13 +0000410 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000411 Align = Width;
412 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000413 case Type::ExtQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000414 // FIXME: Pointers into different addr spaces could have different sizes and
415 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000416 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000417 case Type::ObjCQualifiedId:
Eli Friedman2f6d70d2009-02-22 04:02:33 +0000418 case Type::ObjCQualifiedClass:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000419 case Type::ObjCQualifiedInterface:
Chris Lattner1d78a862008-04-07 07:01:58 +0000420 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000421 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000422 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000423 case Type::BlockPointer: {
424 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
425 Width = Target.getPointerWidth(AS);
426 Align = Target.getPointerAlign(AS);
427 break;
428 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000429 case Type::Pointer: {
430 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000431 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000432 Align = Target.getPointerAlign(AS);
433 break;
434 }
Sebastian Redlce6fff02009-03-16 23:22:08 +0000435 case Type::LValueReference:
436 case Type::RValueReference:
Chris Lattner4b009652007-07-25 00:24:17 +0000437 // "When applied to a reference or a reference type, the result is the size
438 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000439 // FIXME: This is wrong for struct layout: a reference in a struct has
440 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000441 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000442 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000443 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-01-24 21:16:55 +0000444 // the GCC ABI, where pointers to data are one pointer large, pointers to
445 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl18cffee2009-01-24 23:29:36 +0000446 // other compilers too, we need to delegate this completely to TargetInfo
447 // or some ABI abstraction layer.
Sebastian Redl75555032009-01-24 21:16:55 +0000448 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
449 unsigned AS = Pointee.getAddressSpace();
450 Width = Target.getPointerWidth(AS);
451 if (Pointee->isFunctionType())
452 Width *= 2;
453 Align = Target.getPointerAlign(AS);
454 // GCC aligns at single pointer width.
455 }
Chris Lattner4b009652007-07-25 00:24:17 +0000456 case Type::Complex: {
457 // Complex types have the same alignment as their elements, but twice the
458 // size.
459 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000460 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000461 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000462 Align = EltInfo.second;
463 break;
464 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000465 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000466 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000467 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
468 Width = Layout.getSize();
469 Align = Layout.getAlignment();
470 break;
471 }
Douglas Gregor4fa58902009-02-26 23:50:07 +0000472 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000473 case Type::Enum: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000474 const TagType *TT = cast<TagType>(T);
475
476 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000477 Width = 1;
478 Align = 1;
479 break;
480 }
481
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000482 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000483 return getTypeInfo(ET->getDecl()->getIntegerType());
484
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000485 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000486 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
487 Width = Layout.getSize();
488 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000489 break;
490 }
Douglas Gregordd13e842009-03-30 22:58:21 +0000491
492 case Type::TemplateSpecialization:
493 assert(false && "Dependent types have no size");
494 break;
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000495 }
Chris Lattner4b009652007-07-25 00:24:17 +0000496
497 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000498 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000499}
500
Chris Lattner83165b52009-01-27 18:08:34 +0000501/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
502/// type for the current target in bits. This can be different than the ABI
503/// alignment in cases where it is beneficial for performance to overalign
504/// a data type.
505unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
506 unsigned ABIAlign = getTypeAlign(T);
507
508 // Doubles should be naturally aligned if possible.
Daniel Dunbarc61a8002009-02-18 19:59:32 +0000509 if (T->isSpecificBuiltinType(BuiltinType::Double))
510 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000511
512 return ABIAlign;
513}
514
515
Devang Patelbfe323c2008-06-04 21:22:16 +0000516/// LayoutField - Field layout.
517void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000518 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000519 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000520 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000521 uint64_t FieldOffset = IsUnion ? 0 : Size;
522 uint64_t FieldSize;
523 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000524
525 // FIXME: Should this override struct packing? Probably we want to
526 // take the minimum?
527 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
528 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000529
530 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
531 // TODO: Need to check this algorithm on other targets!
532 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000533 FieldSize =
534 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000535
536 std::pair<uint64_t, unsigned> FieldInfo =
537 Context.getTypeInfo(FD->getType());
538 uint64_t TypeSize = FieldInfo.first;
539
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000540 // Determine the alignment of this bitfield. The packing
541 // attributes define a maximum and the alignment attribute defines
542 // a minimum.
543 // FIXME: What is the right behavior when the specified alignment
544 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000545 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000546 if (FieldPacking)
547 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000548 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
549 FieldAlign = std::max(FieldAlign, AA->getAlignment());
550
551 // Check if we need to add padding to give the field the correct
552 // alignment.
553 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
554 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
555
556 // Padding members don't affect overall alignment
557 if (!FD->getIdentifier())
558 FieldAlign = 1;
559 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000560 if (FD->getType()->isIncompleteArrayType()) {
561 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000562 // query getTypeInfo about these, so we figure it out here.
563 // Flexible array members don't have any size, but they
564 // have to be aligned appropriately for their element type.
565 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000566 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000567 FieldAlign = Context.getTypeAlign(ATy->getElementType());
568 } else {
569 std::pair<uint64_t, unsigned> FieldInfo =
570 Context.getTypeInfo(FD->getType());
571 FieldSize = FieldInfo.first;
572 FieldAlign = FieldInfo.second;
573 }
574
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000575 // Determine the alignment of this bitfield. The packing
576 // attributes define a maximum and the alignment attribute defines
577 // a minimum. Additionally, the packing alignment must be at least
578 // a byte for non-bitfields.
579 //
580 // FIXME: What is the right behavior when the specified alignment
581 // is smaller than the specified packing?
582 if (FieldPacking)
583 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000584 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
585 FieldAlign = std::max(FieldAlign, AA->getAlignment());
586
587 // Round up the current record size to the field's alignment boundary.
588 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
589 }
590
591 // Place this field at the current location.
592 FieldOffsets[FieldNo] = FieldOffset;
593
594 // Reserve space for this field.
595 if (IsUnion) {
596 Size = std::max(Size, FieldSize);
597 } else {
598 Size = FieldOffset + FieldSize;
599 }
600
601 // Remember max struct/class alignment.
602 Alignment = std::max(Alignment, FieldAlign);
603}
604
Fariborz Jahaniana2c97df2009-03-05 20:08:48 +0000605void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
Chris Lattner9329cf52009-03-31 08:48:01 +0000606 llvm::SmallVectorImpl<FieldDecl*> &Fields) const {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000607 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
608 if (SuperClass)
609 CollectObjCIvars(SuperClass, Fields);
610 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
611 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000612 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000613 if (!IVDecl->isInvalidDecl())
614 Fields.push_back(cast<FieldDecl>(IVDecl));
615 }
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000616 // look into properties.
617 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
618 E = OI->prop_end(); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000619 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000620 Fields.push_back(cast<FieldDecl>(IV));
621 }
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000622}
623
624/// addRecordToClass - produces record info. for the class for its
625/// ivars and all those inherited.
626///
Chris Lattner9329cf52009-03-31 08:48:01 +0000627const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) {
Chris Lattner608c1e32009-03-31 09:24:30 +0000628 RecordDecl *&RD = ASTRecordForInterface[D];
629 if (RD) {
630 // If we have a record decl already and it is either a definition or if 'D'
631 // is still a forward declaration, return it.
632 if (RD->isDefinition() || D->isForwardDecl())
633 return RD;
634 }
635
636 // If D is a forward declaration, then just make a forward struct decl.
637 if (D->isForwardDecl())
638 return RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
639 D->getLocation(),
640 D->getIdentifier());
Chris Lattner9329cf52009-03-31 08:48:01 +0000641
642 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000643 CollectObjCIvars(D, RecFields);
Chris Lattner608c1e32009-03-31 09:24:30 +0000644
645 if (RD == 0)
646 RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(),
647 D->getIdentifier());
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000648 /// FIXME! Can do collection of ivars and adding to the record while
649 /// doing it.
Chris Lattner41b780c2009-03-31 08:58:42 +0000650 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Chris Lattner608c1e32009-03-31 09:24:30 +0000651 RD->addDecl(FieldDecl::Create(*this, RD,
652 RecFields[i]->getLocation(),
653 RecFields[i]->getIdentifier(),
654 RecFields[i]->getType(),
655 RecFields[i]->getBitWidth(), false));
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000656 }
Chris Lattner9329cf52009-03-31 08:48:01 +0000657
Chris Lattner608c1e32009-03-31 09:24:30 +0000658 RD->completeDefinition(*this);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000659 return RD;
660}
Devang Patel4b6bf702008-06-04 21:54:36 +0000661
Fariborz Jahanianea944842008-12-18 17:29:46 +0000662/// setFieldDecl - maps a field for the given Ivar reference node.
663//
664void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
665 const ObjCIvarDecl *Ivar,
666 const ObjCIvarRefExpr *MRef) {
Chris Lattner04f4db72009-03-31 08:31:13 +0000667 ASTFieldForIvarRef[MRef] = OI->lookupFieldDeclForIvar(*this, Ivar);
Fariborz Jahanianea944842008-12-18 17:29:46 +0000668}
669
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000670/// getASTObjcInterfaceLayout - Get or compute information about the layout of
671/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000672/// position information.
673const ASTRecordLayout &
674ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
675 // Look up this layout, if already laid out, return what we have.
676 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
677 if (Entry) return *Entry;
678
679 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
680 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000681 ASTRecordLayout *NewEntry = NULL;
Fariborz Jahanianba50c332009-04-08 21:54:52 +0000682 // FIXME. Add actual count of synthesized ivars, instead of count
683 // of properties which is the upper bound, but is safe.
Daniel Dunbar998510f2009-04-08 20:18:15 +0000684 unsigned FieldCount =
685 D->ivar_size() + std::distance(D->prop_begin(), D->prop_end());
Devang Patel8682d882008-06-06 02:14:01 +0000686 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
687 FieldCount++;
688 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
689 unsigned Alignment = SL.getAlignment();
690 uint64_t Size = SL.getSize();
691 NewEntry = new ASTRecordLayout(Size, Alignment);
692 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000693 // Super class is at the beginning of the layout.
694 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000695 } else {
696 NewEntry = new ASTRecordLayout();
697 NewEntry->InitializeLayout(FieldCount);
698 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000699 Entry = NewEntry;
700
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000701 unsigned StructPacking = 0;
702 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
703 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000704
705 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
706 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
707 AA->getAlignment()));
708
709 // Layout each ivar sequentially.
710 unsigned i = 0;
711 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
712 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
713 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000714 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000715 }
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000716 // Also synthesized ivars
717 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(),
718 E = D->prop_end(); I != E; ++I) {
719 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
720 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
721 }
Fariborz Jahanian84c45692009-04-01 19:37:34 +0000722
Devang Patel4b6bf702008-06-04 21:54:36 +0000723 // Finally, round the size of the total struct up to the alignment of the
724 // struct itself.
725 NewEntry->FinalizeLayout();
726 return *NewEntry;
727}
728
Devang Patel7a78e432007-11-01 19:11:01 +0000729/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000730/// specified record (struct/union/class), which indicates its size and field
731/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000732const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000733 D = D->getDefinition(*this);
734 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000735
Chris Lattner4b009652007-07-25 00:24:17 +0000736 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000737 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000738 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000739
Devang Patel7a78e432007-11-01 19:11:01 +0000740 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
741 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
742 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000743 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000744
Douglas Gregor39677622008-12-11 20:41:00 +0000745 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000746 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000747 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000748
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000749 unsigned StructPacking = 0;
750 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
751 StructPacking = PA->getAlignment();
752
Eli Friedman5949a022008-05-30 09:31:38 +0000753 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000754 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
755 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000756
Eli Friedman5949a022008-05-30 09:31:38 +0000757 // Layout each field, for now, just sequentially, respecting alignment. In
758 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000759 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000760 for (RecordDecl::field_iterator Field = D->field_begin(),
761 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000762 Field != FieldEnd; (void)++Field, ++FieldIdx)
763 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000764
765 // Finally, round the size of the total struct up to the alignment of the
766 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000767 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000768 return *NewEntry;
769}
770
Chris Lattner4b009652007-07-25 00:24:17 +0000771//===----------------------------------------------------------------------===//
772// Type creation/memoization methods
773//===----------------------------------------------------------------------===//
774
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000775QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000776 QualType CanT = getCanonicalType(T);
777 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000778 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000779
780 // If we are composing extended qualifiers together, merge together into one
781 // ExtQualType node.
782 unsigned CVRQuals = T.getCVRQualifiers();
783 QualType::GCAttrTypes GCAttr = QualType::GCNone;
784 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000785
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000786 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
787 // If this type already has an address space specified, it cannot get
788 // another one.
789 assert(EQT->getAddressSpace() == 0 &&
790 "Type cannot be in multiple addr spaces!");
791 GCAttr = EQT->getObjCGCAttr();
792 TypeNode = EQT->getBaseType();
793 }
Chris Lattner35fef522008-02-20 20:55:12 +0000794
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000795 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000796 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000797 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000798 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000799 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000800 return QualType(EXTQy, CVRQuals);
801
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000802 // If the base type isn't canonical, this won't be a canonical type either,
803 // so fill in the canonical type field.
804 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000805 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000806 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000807
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000808 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000809 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000810 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000811 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000812 ExtQualType *New =
813 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000814 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000815 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000816 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000817}
818
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000819QualType ASTContext::getObjCGCQualType(QualType T,
820 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000821 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000822 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000823 return T;
824
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000825 // If we are composing extended qualifiers together, merge together into one
826 // ExtQualType node.
827 unsigned CVRQuals = T.getCVRQualifiers();
828 Type *TypeNode = T.getTypePtr();
829 unsigned AddressSpace = 0;
830
831 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
832 // If this type already has an address space specified, it cannot get
833 // another one.
834 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
835 "Type cannot be in multiple addr spaces!");
836 AddressSpace = EQT->getAddressSpace();
837 TypeNode = EQT->getBaseType();
838 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000839
840 // Check if we've already instantiated an gc qual'd type of this type.
841 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000842 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000843 void *InsertPos = 0;
844 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000845 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000846
847 // If the base type isn't canonical, this won't be a canonical type either,
848 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +0000849 // FIXME: Isn't this also not canonical if the base type is a array
850 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000851 QualType Canonical;
852 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000853 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000854
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000855 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000856 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
857 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
858 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000859 ExtQualType *New =
860 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000861 ExtQualTypes.InsertNode(New, InsertPos);
862 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000863 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000864}
Chris Lattner4b009652007-07-25 00:24:17 +0000865
866/// getComplexType - Return the uniqued reference to the type for a complex
867/// number with the specified element type.
868QualType ASTContext::getComplexType(QualType T) {
869 // Unique pointers, to guarantee there is only one pointer of a particular
870 // structure.
871 llvm::FoldingSetNodeID ID;
872 ComplexType::Profile(ID, T);
873
874 void *InsertPos = 0;
875 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
876 return QualType(CT, 0);
877
878 // If the pointee type isn't canonical, this won't be a canonical type either,
879 // so fill in the canonical type field.
880 QualType Canonical;
881 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000882 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000883
884 // Get the new insert position for the node we care about.
885 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000886 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000887 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000888 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000889 Types.push_back(New);
890 ComplexTypes.InsertNode(New, InsertPos);
891 return QualType(New, 0);
892}
893
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000894QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
895 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
896 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
897 FixedWidthIntType *&Entry = Map[Width];
898 if (!Entry)
899 Entry = new FixedWidthIntType(Width, Signed);
900 return QualType(Entry, 0);
901}
Chris Lattner4b009652007-07-25 00:24:17 +0000902
903/// getPointerType - Return the uniqued reference to the type for a pointer to
904/// the specified type.
905QualType ASTContext::getPointerType(QualType T) {
906 // Unique pointers, to guarantee there is only one pointer of a particular
907 // structure.
908 llvm::FoldingSetNodeID ID;
909 PointerType::Profile(ID, T);
910
911 void *InsertPos = 0;
912 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
913 return QualType(PT, 0);
914
915 // If the pointee type isn't canonical, this won't be a canonical type either,
916 // so fill in the canonical type field.
917 QualType Canonical;
918 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000919 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000920
921 // Get the new insert position for the node we care about.
922 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000923 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000924 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000925 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000926 Types.push_back(New);
927 PointerTypes.InsertNode(New, InsertPos);
928 return QualType(New, 0);
929}
930
Steve Naroff7aa54752008-08-27 16:04:49 +0000931/// getBlockPointerType - Return the uniqued reference to the type for
932/// a pointer to the specified block.
933QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000934 assert(T->isFunctionType() && "block of function types only");
935 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000936 // structure.
937 llvm::FoldingSetNodeID ID;
938 BlockPointerType::Profile(ID, T);
939
940 void *InsertPos = 0;
941 if (BlockPointerType *PT =
942 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
943 return QualType(PT, 0);
944
Steve Narofffd5b19d2008-08-28 19:20:44 +0000945 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000946 // type either so fill in the canonical type field.
947 QualType Canonical;
948 if (!T->isCanonical()) {
949 Canonical = getBlockPointerType(getCanonicalType(T));
950
951 // Get the new insert position for the node we care about.
952 BlockPointerType *NewIP =
953 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000954 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000955 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000956 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000957 Types.push_back(New);
958 BlockPointerTypes.InsertNode(New, InsertPos);
959 return QualType(New, 0);
960}
961
Sebastian Redlce6fff02009-03-16 23:22:08 +0000962/// getLValueReferenceType - Return the uniqued reference to the type for an
963/// lvalue reference to the specified type.
964QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +0000965 // Unique pointers, to guarantee there is only one pointer of a particular
966 // structure.
967 llvm::FoldingSetNodeID ID;
968 ReferenceType::Profile(ID, T);
969
970 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000971 if (LValueReferenceType *RT =
972 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000973 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000974
Chris Lattner4b009652007-07-25 00:24:17 +0000975 // If the referencee type isn't canonical, this won't be a canonical type
976 // either, so fill in the canonical type field.
977 QualType Canonical;
978 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000979 Canonical = getLValueReferenceType(getCanonicalType(T));
980
Chris Lattner4b009652007-07-25 00:24:17 +0000981 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000982 LValueReferenceType *NewIP =
983 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000984 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000985 }
986
Sebastian Redlce6fff02009-03-16 23:22:08 +0000987 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000988 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000989 LValueReferenceTypes.InsertNode(New, InsertPos);
990 return QualType(New, 0);
991}
992
993/// getRValueReferenceType - Return the uniqued reference to the type for an
994/// rvalue reference to the specified type.
995QualType ASTContext::getRValueReferenceType(QualType T) {
996 // Unique pointers, to guarantee there is only one pointer of a particular
997 // structure.
998 llvm::FoldingSetNodeID ID;
999 ReferenceType::Profile(ID, T);
1000
1001 void *InsertPos = 0;
1002 if (RValueReferenceType *RT =
1003 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1004 return QualType(RT, 0);
1005
1006 // If the referencee type isn't canonical, this won't be a canonical type
1007 // either, so fill in the canonical type field.
1008 QualType Canonical;
1009 if (!T->isCanonical()) {
1010 Canonical = getRValueReferenceType(getCanonicalType(T));
1011
1012 // Get the new insert position for the node we care about.
1013 RValueReferenceType *NewIP =
1014 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1015 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1016 }
1017
1018 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1019 Types.push_back(New);
1020 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001021 return QualType(New, 0);
1022}
1023
Sebastian Redl75555032009-01-24 21:16:55 +00001024/// getMemberPointerType - Return the uniqued reference to the type for a
1025/// member pointer to the specified type, in the specified class.
1026QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1027{
1028 // Unique pointers, to guarantee there is only one pointer of a particular
1029 // structure.
1030 llvm::FoldingSetNodeID ID;
1031 MemberPointerType::Profile(ID, T, Cls);
1032
1033 void *InsertPos = 0;
1034 if (MemberPointerType *PT =
1035 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1036 return QualType(PT, 0);
1037
1038 // If the pointee or class type isn't canonical, this won't be a canonical
1039 // type either, so fill in the canonical type field.
1040 QualType Canonical;
1041 if (!T->isCanonical()) {
1042 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1043
1044 // Get the new insert position for the node we care about.
1045 MemberPointerType *NewIP =
1046 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1047 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1048 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001049 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001050 Types.push_back(New);
1051 MemberPointerTypes.InsertNode(New, InsertPos);
1052 return QualType(New, 0);
1053}
1054
Steve Naroff83c13012007-08-30 01:06:46 +00001055/// getConstantArrayType - Return the unique reference to the type for an
1056/// array of the specified element type.
1057QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +00001058 const llvm::APInt &ArySize,
1059 ArrayType::ArraySizeModifier ASM,
1060 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001061 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001062 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001063
1064 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001065 if (ConstantArrayType *ATP =
1066 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001067 return QualType(ATP, 0);
1068
1069 // If the element type isn't canonical, this won't be a canonical type either,
1070 // so fill in the canonical type field.
1071 QualType Canonical;
1072 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001073 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001074 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001075 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001076 ConstantArrayType *NewIP =
1077 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001078 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001079 }
1080
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001081 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001082 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001083 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001084 Types.push_back(New);
1085 return QualType(New, 0);
1086}
1087
Steve Naroffe2579e32007-08-30 18:14:25 +00001088/// getVariableArrayType - Returns a non-unique reference to the type for a
1089/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +00001090QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1091 ArrayType::ArraySizeModifier ASM,
1092 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001093 // Since we don't unique expressions, it isn't possible to unique VLA's
1094 // that have an expression provided for their size.
1095
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001096 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001097 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001098
1099 VariableArrayTypes.push_back(New);
1100 Types.push_back(New);
1101 return QualType(New, 0);
1102}
1103
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001104/// getDependentSizedArrayType - Returns a non-unique reference to
1105/// the type for a dependently-sized array of the specified element
1106/// type. FIXME: We will need these to be uniqued, or at least
1107/// comparable, at some point.
1108QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1109 ArrayType::ArraySizeModifier ASM,
1110 unsigned EltTypeQuals) {
1111 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1112 "Size must be type- or value-dependent!");
1113
1114 // Since we don't unique expressions, it isn't possible to unique
1115 // dependently-sized array types.
1116
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001117 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001118 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1119 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001120
1121 DependentSizedArrayTypes.push_back(New);
1122 Types.push_back(New);
1123 return QualType(New, 0);
1124}
1125
Eli Friedman8ff07782008-02-15 18:16:39 +00001126QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1127 ArrayType::ArraySizeModifier ASM,
1128 unsigned EltTypeQuals) {
1129 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001130 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001131
1132 void *InsertPos = 0;
1133 if (IncompleteArrayType *ATP =
1134 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1135 return QualType(ATP, 0);
1136
1137 // If the element type isn't canonical, this won't be a canonical type
1138 // either, so fill in the canonical type field.
1139 QualType Canonical;
1140
1141 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001142 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001143 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001144
1145 // Get the new insert position for the node we care about.
1146 IncompleteArrayType *NewIP =
1147 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001148 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001149 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001150
Steve Naroff93fd2112009-01-27 22:08:43 +00001151 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001152 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001153
1154 IncompleteArrayTypes.InsertNode(New, InsertPos);
1155 Types.push_back(New);
1156 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001157}
1158
Chris Lattner4b009652007-07-25 00:24:17 +00001159/// getVectorType - Return the unique reference to a vector type of
1160/// the specified element type and size. VectorType must be a built-in type.
1161QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1162 BuiltinType *baseType;
1163
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001164 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001165 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1166
1167 // Check if we've already instantiated a vector of this type.
1168 llvm::FoldingSetNodeID ID;
1169 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1170 void *InsertPos = 0;
1171 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1172 return QualType(VTP, 0);
1173
1174 // If the element type isn't canonical, this won't be a canonical type either,
1175 // so fill in the canonical type field.
1176 QualType Canonical;
1177 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001178 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001179
1180 // Get the new insert position for the node we care about.
1181 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001182 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001183 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001184 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001185 VectorTypes.InsertNode(New, InsertPos);
1186 Types.push_back(New);
1187 return QualType(New, 0);
1188}
1189
Nate Begemanaf6ed502008-04-18 23:10:10 +00001190/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001191/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001192QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001193 BuiltinType *baseType;
1194
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001195 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001196 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001197
1198 // Check if we've already instantiated a vector of this type.
1199 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001200 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001201 void *InsertPos = 0;
1202 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1203 return QualType(VTP, 0);
1204
1205 // If the element type isn't canonical, this won't be a canonical type either,
1206 // so fill in the canonical type field.
1207 QualType Canonical;
1208 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001209 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001210
1211 // Get the new insert position for the node we care about.
1212 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001213 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001214 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001215 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001216 VectorTypes.InsertNode(New, InsertPos);
1217 Types.push_back(New);
1218 return QualType(New, 0);
1219}
1220
Douglas Gregor4fa58902009-02-26 23:50:07 +00001221/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001222///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001223QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001224 // Unique functions, to guarantee there is only one function of a particular
1225 // structure.
1226 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001227 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001228
1229 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001230 if (FunctionNoProtoType *FT =
1231 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001232 return QualType(FT, 0);
1233
1234 QualType Canonical;
1235 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001236 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001237
1238 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001239 FunctionNoProtoType *NewIP =
1240 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001241 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001242 }
1243
Douglas Gregor4fa58902009-02-26 23:50:07 +00001244 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001245 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001246 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001247 return QualType(New, 0);
1248}
1249
1250/// getFunctionType - Return a normal function type with a typed argument
1251/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001252QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001253 unsigned NumArgs, bool isVariadic,
1254 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001255 // Unique functions, to guarantee there is only one function of a particular
1256 // structure.
1257 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001258 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001259 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001260
1261 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001262 if (FunctionProtoType *FTP =
1263 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001264 return QualType(FTP, 0);
1265
1266 // Determine whether the type being created is already canonical or not.
1267 bool isCanonical = ResultTy->isCanonical();
1268 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1269 if (!ArgArray[i]->isCanonical())
1270 isCanonical = false;
1271
1272 // If this type isn't canonical, get the canonical version of it.
1273 QualType Canonical;
1274 if (!isCanonical) {
1275 llvm::SmallVector<QualType, 16> CanonicalArgs;
1276 CanonicalArgs.reserve(NumArgs);
1277 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001278 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001279
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001280 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001281 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001282 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001283
1284 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001285 FunctionProtoType *NewIP =
1286 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001287 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001288 }
1289
Douglas Gregor4fa58902009-02-26 23:50:07 +00001290 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001291 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001292 FunctionProtoType *FTP =
1293 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001294 NumArgs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001295 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001296 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001297 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001298 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001299 return QualType(FTP, 0);
1300}
1301
Douglas Gregor1d661552008-04-13 21:07:44 +00001302/// getTypeDeclType - Return the unique reference to the type for the
1303/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001304QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001305 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001306 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1307
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001308 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001309 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001310 else if (isa<TemplateTypeParmDecl>(Decl)) {
1311 assert(false && "Template type parameter types are always available.");
1312 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001313 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001314
Douglas Gregor2e047592009-02-28 01:32:25 +00001315 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001316 if (PrevDecl)
1317 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001318 else
1319 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001320 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001321 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1322 if (PrevDecl)
1323 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001324 else
1325 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001326 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001327 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001328 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001329
Ted Kremenek46a837c2008-09-05 17:16:31 +00001330 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001331 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001332}
1333
Chris Lattner4b009652007-07-25 00:24:17 +00001334/// getTypedefType - Return the unique reference to the type for the
1335/// specified typename decl.
1336QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1337 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1338
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001339 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001340 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001341 Types.push_back(Decl->TypeForDecl);
1342 return QualType(Decl->TypeForDecl, 0);
1343}
1344
Ted Kremenek42730c52008-01-07 19:49:32 +00001345/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001346/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001347QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001348 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1349
Steve Naroff93fd2112009-01-27 22:08:43 +00001350 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001351 Types.push_back(Decl->TypeForDecl);
1352 return QualType(Decl->TypeForDecl, 0);
1353}
1354
Douglas Gregora4918772009-02-05 23:33:38 +00001355/// \brief Retrieve the template type parameter type for a template
1356/// parameter with the given depth, index, and (optionally) name.
1357QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1358 IdentifierInfo *Name) {
1359 llvm::FoldingSetNodeID ID;
1360 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1361 void *InsertPos = 0;
1362 TemplateTypeParmType *TypeParm
1363 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1364
1365 if (TypeParm)
1366 return QualType(TypeParm, 0);
1367
1368 if (Name)
1369 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1370 getTemplateTypeParmType(Depth, Index));
1371 else
1372 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1373
1374 Types.push_back(TypeParm);
1375 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1376
1377 return QualType(TypeParm, 0);
1378}
1379
Douglas Gregor8e458f42009-02-09 18:46:07 +00001380QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001381ASTContext::getTemplateSpecializationType(TemplateName Template,
1382 const TemplateArgument *Args,
1383 unsigned NumArgs,
1384 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001385 if (!Canon.isNull())
1386 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001387
Douglas Gregor8e458f42009-02-09 18:46:07 +00001388 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001389 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001390
Douglas Gregor8e458f42009-02-09 18:46:07 +00001391 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001392 TemplateSpecializationType *Spec
1393 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001394
1395 if (Spec)
1396 return QualType(Spec, 0);
1397
Douglas Gregordd13e842009-03-30 22:58:21 +00001398 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001399 sizeof(TemplateArgument) * NumArgs),
1400 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001401 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001402 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001403 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001404
1405 return QualType(Spec, 0);
1406}
1407
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001408QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001409ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001410 QualType NamedType) {
1411 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001412 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001413
1414 void *InsertPos = 0;
1415 QualifiedNameType *T
1416 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1417 if (T)
1418 return QualType(T, 0);
1419
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001420 T = new (*this) QualifiedNameType(NNS, NamedType,
1421 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001422 Types.push_back(T);
1423 QualifiedNameTypes.InsertNode(T, InsertPos);
1424 return QualType(T, 0);
1425}
1426
Douglas Gregord3022602009-03-27 23:10:48 +00001427QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1428 const IdentifierInfo *Name,
1429 QualType Canon) {
1430 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1431
1432 if (Canon.isNull()) {
1433 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1434 if (CanonNNS != NNS)
1435 Canon = getTypenameType(CanonNNS, Name);
1436 }
1437
1438 llvm::FoldingSetNodeID ID;
1439 TypenameType::Profile(ID, NNS, Name);
1440
1441 void *InsertPos = 0;
1442 TypenameType *T
1443 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1444 if (T)
1445 return QualType(T, 0);
1446
1447 T = new (*this) TypenameType(NNS, Name, Canon);
1448 Types.push_back(T);
1449 TypenameTypes.InsertNode(T, InsertPos);
1450 return QualType(T, 0);
1451}
1452
Douglas Gregor77da5802009-04-01 00:28:59 +00001453QualType
1454ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1455 const TemplateSpecializationType *TemplateId,
1456 QualType Canon) {
1457 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1458
1459 if (Canon.isNull()) {
1460 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1461 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1462 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1463 const TemplateSpecializationType *CanonTemplateId
1464 = CanonType->getAsTemplateSpecializationType();
1465 assert(CanonTemplateId &&
1466 "Canonical type must also be a template specialization type");
1467 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1468 }
1469 }
1470
1471 llvm::FoldingSetNodeID ID;
1472 TypenameType::Profile(ID, NNS, TemplateId);
1473
1474 void *InsertPos = 0;
1475 TypenameType *T
1476 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1477 if (T)
1478 return QualType(T, 0);
1479
1480 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1481 Types.push_back(T);
1482 TypenameTypes.InsertNode(T, InsertPos);
1483 return QualType(T, 0);
1484}
1485
Chris Lattnere1352302008-04-07 04:56:42 +00001486/// CmpProtocolNames - Comparison predicate for sorting protocols
1487/// alphabetically.
1488static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1489 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001490 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001491}
1492
1493static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1494 unsigned &NumProtocols) {
1495 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1496
1497 // Sort protocols, keyed by name.
1498 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1499
1500 // Remove duplicates.
1501 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1502 NumProtocols = ProtocolsEnd-Protocols;
1503}
1504
1505
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001506/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1507/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001508QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1509 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001510 // Sort the protocol list alphabetically to canonicalize it.
1511 SortAndUniqueProtocols(Protocols, NumProtocols);
1512
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001513 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001514 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001515
1516 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001517 if (ObjCQualifiedInterfaceType *QT =
1518 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001519 return QualType(QT, 0);
1520
1521 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001522 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001523 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001524
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001525 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001526 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001527 return QualType(QType, 0);
1528}
1529
Chris Lattnere1352302008-04-07 04:56:42 +00001530/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1531/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001532QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001533 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001534 // Sort the protocol list alphabetically to canonicalize it.
1535 SortAndUniqueProtocols(Protocols, NumProtocols);
1536
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001537 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001538 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001539
1540 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001541 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001542 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001543 return QualType(QT, 0);
1544
1545 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001546 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001547 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001548 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001549 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001550 return QualType(QType, 0);
1551}
1552
Douglas Gregor4fa58902009-02-26 23:50:07 +00001553/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1554/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001555/// multiple declarations that refer to "typeof(x)" all contain different
1556/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1557/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001558QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001559 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001560 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001561 Types.push_back(toe);
1562 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001563}
1564
Steve Naroff0604dd92007-08-01 18:02:17 +00001565/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1566/// TypeOfType AST's. The only motivation to unique these nodes would be
1567/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1568/// an issue. This doesn't effect the type checker, since it operates
1569/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001570QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001571 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001572 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001573 Types.push_back(tot);
1574 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001575}
1576
Chris Lattner4b009652007-07-25 00:24:17 +00001577/// getTagDeclType - Return the unique reference to the type for the
1578/// specified TagDecl (struct/union/class/enum) decl.
1579QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001580 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001581 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001582}
1583
1584/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1585/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1586/// needs to agree with the definition in <stddef.h>.
1587QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001588 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001589}
1590
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001591/// getSignedWCharType - Return the type of "signed wchar_t".
1592/// Used when in C++, as a GCC extension.
1593QualType ASTContext::getSignedWCharType() const {
1594 // FIXME: derive from "Target" ?
1595 return WCharTy;
1596}
1597
1598/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1599/// Used when in C++, as a GCC extension.
1600QualType ASTContext::getUnsignedWCharType() const {
1601 // FIXME: derive from "Target" ?
1602 return UnsignedIntTy;
1603}
1604
Chris Lattner4b009652007-07-25 00:24:17 +00001605/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1606/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1607QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001608 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001609}
1610
Chris Lattner19eb97e2008-04-02 05:18:44 +00001611//===----------------------------------------------------------------------===//
1612// Type Operators
1613//===----------------------------------------------------------------------===//
1614
Chris Lattner3dae6f42008-04-06 22:41:35 +00001615/// getCanonicalType - Return the canonical (structural) type corresponding to
1616/// the specified potentially non-canonical type. The non-canonical version
1617/// of a type may have many "decorated" versions of types. Decorators can
1618/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1619/// to be free of any of these, allowing two canonical types to be compared
1620/// for exact equality with a simple pointer comparison.
1621QualType ASTContext::getCanonicalType(QualType T) {
1622 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001623
1624 // If the result has type qualifiers, make sure to canonicalize them as well.
1625 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1626 if (TypeQuals == 0) return CanType;
1627
1628 // If the type qualifiers are on an array type, get the canonical type of the
1629 // array with the qualifiers applied to the element type.
1630 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1631 if (!AT)
1632 return CanType.getQualifiedType(TypeQuals);
1633
1634 // Get the canonical version of the element with the extra qualifiers on it.
1635 // This can recursively sink qualifiers through multiple levels of arrays.
1636 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1637 NewEltTy = getCanonicalType(NewEltTy);
1638
1639 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1640 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1641 CAT->getIndexTypeQualifier());
1642 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1643 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1644 IAT->getIndexTypeQualifier());
1645
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001646 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1647 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1648 DSAT->getSizeModifier(),
1649 DSAT->getIndexTypeQualifier());
1650
Chris Lattnera1923f62008-08-04 07:31:14 +00001651 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1652 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1653 VAT->getSizeModifier(),
1654 VAT->getIndexTypeQualifier());
1655}
1656
Douglas Gregord3022602009-03-27 23:10:48 +00001657NestedNameSpecifier *
1658ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1659 if (!NNS)
1660 return 0;
1661
1662 switch (NNS->getKind()) {
1663 case NestedNameSpecifier::Identifier:
1664 // Canonicalize the prefix but keep the identifier the same.
1665 return NestedNameSpecifier::Create(*this,
1666 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1667 NNS->getAsIdentifier());
1668
1669 case NestedNameSpecifier::Namespace:
1670 // A namespace is canonical; build a nested-name-specifier with
1671 // this namespace and no prefix.
1672 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1673
1674 case NestedNameSpecifier::TypeSpec:
1675 case NestedNameSpecifier::TypeSpecWithTemplate: {
1676 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1677 NestedNameSpecifier *Prefix = 0;
1678
1679 // FIXME: This isn't the right check!
1680 if (T->isDependentType())
1681 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1682
1683 return NestedNameSpecifier::Create(*this, Prefix,
1684 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1685 T.getTypePtr());
1686 }
1687
1688 case NestedNameSpecifier::Global:
1689 // The global specifier is canonical and unique.
1690 return NNS;
1691 }
1692
1693 // Required to silence a GCC warning
1694 return 0;
1695}
1696
Chris Lattnera1923f62008-08-04 07:31:14 +00001697
1698const ArrayType *ASTContext::getAsArrayType(QualType T) {
1699 // Handle the non-qualified case efficiently.
1700 if (T.getCVRQualifiers() == 0) {
1701 // Handle the common positive case fast.
1702 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1703 return AT;
1704 }
1705
1706 // Handle the common negative case fast, ignoring CVR qualifiers.
1707 QualType CType = T->getCanonicalTypeInternal();
1708
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001709 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001710 // test.
1711 if (!isa<ArrayType>(CType) &&
1712 !isa<ArrayType>(CType.getUnqualifiedType()))
1713 return 0;
1714
1715 // Apply any CVR qualifiers from the array type to the element type. This
1716 // implements C99 6.7.3p8: "If the specification of an array type includes
1717 // any type qualifiers, the element type is so qualified, not the array type."
1718
1719 // If we get here, we either have type qualifiers on the type, or we have
1720 // sugar such as a typedef in the way. If we have type qualifiers on the type
1721 // we must propagate them down into the elemeng type.
1722 unsigned CVRQuals = T.getCVRQualifiers();
1723 unsigned AddrSpace = 0;
1724 Type *Ty = T.getTypePtr();
1725
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001726 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001727 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001728 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1729 AddrSpace = EXTQT->getAddressSpace();
1730 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001731 } else {
1732 T = Ty->getDesugaredType();
1733 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1734 break;
1735 CVRQuals |= T.getCVRQualifiers();
1736 Ty = T.getTypePtr();
1737 }
1738 }
1739
1740 // If we have a simple case, just return now.
1741 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1742 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1743 return ATy;
1744
1745 // Otherwise, we have an array and we have qualifiers on it. Push the
1746 // qualifiers into the array element type and return a new array type.
1747 // Get the canonical version of the element with the extra qualifiers on it.
1748 // This can recursively sink qualifiers through multiple levels of arrays.
1749 QualType NewEltTy = ATy->getElementType();
1750 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001751 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001752 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1753
1754 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1755 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1756 CAT->getSizeModifier(),
1757 CAT->getIndexTypeQualifier()));
1758 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1759 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1760 IAT->getSizeModifier(),
1761 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001762
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001763 if (const DependentSizedArrayType *DSAT
1764 = dyn_cast<DependentSizedArrayType>(ATy))
1765 return cast<ArrayType>(
1766 getDependentSizedArrayType(NewEltTy,
1767 DSAT->getSizeExpr(),
1768 DSAT->getSizeModifier(),
1769 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001770
Chris Lattnera1923f62008-08-04 07:31:14 +00001771 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1772 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1773 VAT->getSizeModifier(),
1774 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001775}
1776
1777
Chris Lattner19eb97e2008-04-02 05:18:44 +00001778/// getArrayDecayedType - Return the properly qualified result of decaying the
1779/// specified array type to a pointer. This operation is non-trivial when
1780/// handling typedefs etc. The canonical type of "T" must be an array type,
1781/// this returns a pointer to a properly qualified element of the array.
1782///
1783/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1784QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001785 // Get the element type with 'getAsArrayType' so that we don't lose any
1786 // typedefs in the element type of the array. This also handles propagation
1787 // of type qualifiers from the array type into the element type if present
1788 // (C99 6.7.3p8).
1789 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1790 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001791
Chris Lattnera1923f62008-08-04 07:31:14 +00001792 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001793
1794 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001795 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001796}
1797
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001798QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001799 QualType ElemTy = VAT->getElementType();
1800
1801 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1802 return getBaseElementType(VAT);
1803
1804 return ElemTy;
1805}
1806
Chris Lattner4b009652007-07-25 00:24:17 +00001807/// getFloatingRank - Return a relative rank for floating point types.
1808/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001809static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001810 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001811 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001812
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001813 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001814 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001815 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001816 case BuiltinType::Float: return FloatRank;
1817 case BuiltinType::Double: return DoubleRank;
1818 case BuiltinType::LongDouble: return LongDoubleRank;
1819 }
1820}
1821
Steve Narofffa0c4532007-08-27 01:41:48 +00001822/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1823/// point or a complex type (based on typeDomain/typeSize).
1824/// 'typeDomain' is a real floating point or complex type.
1825/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001826QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1827 QualType Domain) const {
1828 FloatingRank EltRank = getFloatingRank(Size);
1829 if (Domain->isComplexType()) {
1830 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001831 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001832 case FloatRank: return FloatComplexTy;
1833 case DoubleRank: return DoubleComplexTy;
1834 case LongDoubleRank: return LongDoubleComplexTy;
1835 }
Chris Lattner4b009652007-07-25 00:24:17 +00001836 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001837
1838 assert(Domain->isRealFloatingType() && "Unknown domain!");
1839 switch (EltRank) {
1840 default: assert(0 && "getFloatingRank(): illegal value for rank");
1841 case FloatRank: return FloatTy;
1842 case DoubleRank: return DoubleTy;
1843 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001844 }
Chris Lattner4b009652007-07-25 00:24:17 +00001845}
1846
Chris Lattner51285d82008-04-06 23:55:33 +00001847/// getFloatingTypeOrder - Compare the rank of the two specified floating
1848/// point types, ignoring the domain of the type (i.e. 'double' ==
1849/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1850/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001851int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1852 FloatingRank LHSR = getFloatingRank(LHS);
1853 FloatingRank RHSR = getFloatingRank(RHS);
1854
1855 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001856 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001857 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001858 return 1;
1859 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001860}
1861
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001862/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1863/// routine will assert if passed a built-in type that isn't an integer or enum,
1864/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001865unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001866 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001867 if (EnumType* ET = dyn_cast<EnumType>(T))
1868 T = ET->getDecl()->getIntegerType().getTypePtr();
1869
1870 // There are two things which impact the integer rank: the width, and
1871 // the ordering of builtins. The builtin ordering is encoded in the
1872 // bottom three bits; the width is encoded in the bits above that.
1873 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1874 return FWIT->getWidth() << 3;
1875 }
1876
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001877 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001878 default: assert(0 && "getIntegerRank(): not a built-in integer");
1879 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001880 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001881 case BuiltinType::Char_S:
1882 case BuiltinType::Char_U:
1883 case BuiltinType::SChar:
1884 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001885 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001886 case BuiltinType::Short:
1887 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001888 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001889 case BuiltinType::Int:
1890 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001891 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001892 case BuiltinType::Long:
1893 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001894 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001895 case BuiltinType::LongLong:
1896 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001897 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001898 }
1899}
1900
Chris Lattner51285d82008-04-06 23:55:33 +00001901/// getIntegerTypeOrder - Returns the highest ranked integer type:
1902/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1903/// LHS < RHS, return -1.
1904int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001905 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1906 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001907 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001908
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001909 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1910 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001911
Chris Lattner51285d82008-04-06 23:55:33 +00001912 unsigned LHSRank = getIntegerRank(LHSC);
1913 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001914
Chris Lattner51285d82008-04-06 23:55:33 +00001915 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1916 if (LHSRank == RHSRank) return 0;
1917 return LHSRank > RHSRank ? 1 : -1;
1918 }
Chris Lattner4b009652007-07-25 00:24:17 +00001919
Chris Lattner51285d82008-04-06 23:55:33 +00001920 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1921 if (LHSUnsigned) {
1922 // If the unsigned [LHS] type is larger, return it.
1923 if (LHSRank >= RHSRank)
1924 return 1;
1925
1926 // If the signed type can represent all values of the unsigned type, it
1927 // wins. Because we are dealing with 2's complement and types that are
1928 // powers of two larger than each other, this is always safe.
1929 return -1;
1930 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001931
Chris Lattner51285d82008-04-06 23:55:33 +00001932 // If the unsigned [RHS] type is larger, return it.
1933 if (RHSRank >= LHSRank)
1934 return -1;
1935
1936 // If the signed type can represent all values of the unsigned type, it
1937 // wins. Because we are dealing with 2's complement and types that are
1938 // powers of two larger than each other, this is always safe.
1939 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001940}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001941
1942// getCFConstantStringType - Return the type used for constant CFStrings.
1943QualType ASTContext::getCFConstantStringType() {
1944 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001945 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001946 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001947 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001948 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001949
1950 // const int *isa;
1951 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001952 // int flags;
1953 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001954 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001955 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001956 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001957 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001958
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001959 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001960 for (unsigned i = 0; i < 4; ++i) {
1961 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1962 SourceLocation(), 0,
1963 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001964 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001965 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001966 }
1967
1968 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001969 }
1970
1971 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001972}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001973
Anders Carlssonf58cac72008-08-30 19:34:46 +00001974QualType ASTContext::getObjCFastEnumerationStateType()
1975{
1976 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001977 ObjCFastEnumerationStateTypeDecl =
1978 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1979 &Idents.get("__objcFastEnumerationState"));
1980
Anders Carlssonf58cac72008-08-30 19:34:46 +00001981 QualType FieldTypes[] = {
1982 UnsignedLongTy,
1983 getPointerType(ObjCIdType),
1984 getPointerType(UnsignedLongTy),
1985 getConstantArrayType(UnsignedLongTy,
1986 llvm::APInt(32, 5), ArrayType::Normal, 0)
1987 };
1988
Douglas Gregor8acb7272008-12-11 16:49:14 +00001989 for (size_t i = 0; i < 4; ++i) {
1990 FieldDecl *Field = FieldDecl::Create(*this,
1991 ObjCFastEnumerationStateTypeDecl,
1992 SourceLocation(), 0,
1993 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001994 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001995 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001996 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001997
Douglas Gregor8acb7272008-12-11 16:49:14 +00001998 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001999 }
2000
2001 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2002}
2003
Anders Carlssone3f02572007-10-29 06:33:42 +00002004// This returns true if a type has been typedefed to BOOL:
2005// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00002006static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002007 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00002008 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2009 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002010
2011 return false;
2012}
2013
Ted Kremenek42730c52008-01-07 19:49:32 +00002014/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002015/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00002016int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00002017 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002018
2019 // Make all integer and enum types at least as large as an int
2020 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002021 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002022 // Treat arrays as pointers, since that's how they're passed in.
2023 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002024 sz = getTypeSize(VoidPtrTy);
2025 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002026}
2027
Ted Kremenek42730c52008-01-07 19:49:32 +00002028/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002029/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002030void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00002031 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002032 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002033 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00002034 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002035 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002036 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002037 // Compute size of all parameters.
2038 // Start with computing size of a pointer in number of bytes.
2039 // FIXME: There might(should) be a better way of doing this computation!
2040 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002041 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002042 // The first two arguments (self and _cmd) are pointers; account for
2043 // their size.
2044 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002045 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2046 E = Decl->param_end(); PI != E; ++PI) {
2047 QualType PType = (*PI)->getType();
2048 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002049 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002050 ParmOffset += sz;
2051 }
2052 S += llvm::utostr(ParmOffset);
2053 S += "@0:";
2054 S += llvm::utostr(PtrSize);
2055
2056 // Argument types.
2057 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002058 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2059 E = Decl->param_end(); PI != E; ++PI) {
2060 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002061 QualType PType = PVDecl->getOriginalType();
2062 if (const ArrayType *AT =
2063 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
2064 // Use array's original type only if it has known number of
2065 // elements.
2066 if (!dyn_cast<ConstantArrayType>(AT))
2067 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002068 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002069 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002070 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002071 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002072 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002073 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002074 }
2075}
2076
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002077/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002078/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002079/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2080/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002081/// Property attributes are stored as a comma-delimited C string. The simple
2082/// attributes readonly and bycopy are encoded as single characters. The
2083/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2084/// encoded as single characters, followed by an identifier. Property types
2085/// are also encoded as a parametrized attribute. The characters used to encode
2086/// these attributes are defined by the following enumeration:
2087/// @code
2088/// enum PropertyAttributes {
2089/// kPropertyReadOnly = 'R', // property is read-only.
2090/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2091/// kPropertyByref = '&', // property is a reference to the value last assigned
2092/// kPropertyDynamic = 'D', // property is dynamic
2093/// kPropertyGetter = 'G', // followed by getter selector name
2094/// kPropertySetter = 'S', // followed by setter selector name
2095/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2096/// kPropertyType = 't' // followed by old-style type encoding.
2097/// kPropertyWeak = 'W' // 'weak' property
2098/// kPropertyStrong = 'P' // property GC'able
2099/// kPropertyNonAtomic = 'N' // property non-atomic
2100/// };
2101/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002102void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2103 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002104 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002105 // Collect information from the property implementation decl(s).
2106 bool Dynamic = false;
2107 ObjCPropertyImplDecl *SynthesizePID = 0;
2108
2109 // FIXME: Duplicated code due to poor abstraction.
2110 if (Container) {
2111 if (const ObjCCategoryImplDecl *CID =
2112 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2113 for (ObjCCategoryImplDecl::propimpl_iterator
2114 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2115 ObjCPropertyImplDecl *PID = *i;
2116 if (PID->getPropertyDecl() == PD) {
2117 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2118 Dynamic = true;
2119 } else {
2120 SynthesizePID = PID;
2121 }
2122 }
2123 }
2124 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002125 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002126 for (ObjCCategoryImplDecl::propimpl_iterator
2127 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2128 ObjCPropertyImplDecl *PID = *i;
2129 if (PID->getPropertyDecl() == PD) {
2130 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2131 Dynamic = true;
2132 } else {
2133 SynthesizePID = PID;
2134 }
2135 }
2136 }
2137 }
2138 }
2139
2140 // FIXME: This is not very efficient.
2141 S = "T";
2142
2143 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002144 // GCC has some special rules regarding encoding of properties which
2145 // closely resembles encoding of ivars.
2146 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2147 true /* outermost type */,
2148 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002149
2150 if (PD->isReadOnly()) {
2151 S += ",R";
2152 } else {
2153 switch (PD->getSetterKind()) {
2154 case ObjCPropertyDecl::Assign: break;
2155 case ObjCPropertyDecl::Copy: S += ",C"; break;
2156 case ObjCPropertyDecl::Retain: S += ",&"; break;
2157 }
2158 }
2159
2160 // It really isn't clear at all what this means, since properties
2161 // are "dynamic by default".
2162 if (Dynamic)
2163 S += ",D";
2164
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002165 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2166 S += ",N";
2167
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002168 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2169 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002170 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002171 }
2172
2173 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2174 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002175 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002176 }
2177
2178 if (SynthesizePID) {
2179 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2180 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002181 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002182 }
2183
2184 // FIXME: OBJCGC: weak & strong
2185}
2186
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002187/// getLegacyIntegralTypeEncoding -
2188/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002189/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002190/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2191///
2192void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2193 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2194 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002195 if (BT->getKind() == BuiltinType::ULong &&
2196 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002197 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002198 else
2199 if (BT->getKind() == BuiltinType::Long &&
2200 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002201 PointeeTy = IntTy;
2202 }
2203 }
2204}
2205
Fariborz Jahanian248db262008-01-22 22:44:46 +00002206void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002207 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002208 // We follow the behavior of gcc, expanding structures which are
2209 // directly pointed to, and expanding embedded structures. Note that
2210 // these rules are sufficient to prevent recursive encoding of the
2211 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002212 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2213 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002214}
2215
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002216static void EncodeBitField(const ASTContext *Context, std::string& S,
2217 FieldDecl *FD) {
2218 const Expr *E = FD->getBitWidth();
2219 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2220 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2221 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2222 S += 'b';
2223 S += llvm::utostr(N);
2224}
2225
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002226void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2227 bool ExpandPointedToStructures,
2228 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002229 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002230 bool OutermostType,
2231 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00002232 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002233 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002234 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002235 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002236 else {
2237 char encoding;
2238 switch (BT->getKind()) {
2239 default: assert(0 && "Unhandled builtin type kind");
2240 case BuiltinType::Void: encoding = 'v'; break;
2241 case BuiltinType::Bool: encoding = 'B'; break;
2242 case BuiltinType::Char_U:
2243 case BuiltinType::UChar: encoding = 'C'; break;
2244 case BuiltinType::UShort: encoding = 'S'; break;
2245 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002246 case BuiltinType::ULong:
2247 encoding =
2248 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2249 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002250 case BuiltinType::ULongLong: encoding = 'Q'; break;
2251 case BuiltinType::Char_S:
2252 case BuiltinType::SChar: encoding = 'c'; break;
2253 case BuiltinType::Short: encoding = 's'; break;
2254 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002255 case BuiltinType::Long:
2256 encoding =
2257 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2258 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002259 case BuiltinType::LongLong: encoding = 'q'; break;
2260 case BuiltinType::Float: encoding = 'f'; break;
2261 case BuiltinType::Double: encoding = 'd'; break;
2262 case BuiltinType::LongDouble: encoding = 'd'; break;
2263 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002264
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002265 S += encoding;
2266 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002267 }
Ted Kremenek42730c52008-01-07 19:49:32 +00002268 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002269 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2270 ExpandPointedToStructures,
2271 ExpandStructures, FD);
2272 if (FD || EncodingProperty) {
2273 // Note that we do extended encoding of protocol qualifer list
2274 // Only when doing ivar or property encoding.
2275 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2276 S += '"';
2277 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2278 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2279 S += '<';
2280 S += Proto->getNameAsString();
2281 S += '>';
2282 }
2283 S += '"';
2284 }
2285 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002286 }
2287 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002288 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002289 bool isReadOnly = false;
2290 // For historical/compatibility reasons, the read-only qualifier of the
2291 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2292 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2293 // Also, do not emit the 'r' for anything but the outermost type!
2294 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2295 if (OutermostType && T.isConstQualified()) {
2296 isReadOnly = true;
2297 S += 'r';
2298 }
2299 }
2300 else if (OutermostType) {
2301 QualType P = PointeeTy;
2302 while (P->getAsPointerType())
2303 P = P->getAsPointerType()->getPointeeType();
2304 if (P.isConstQualified()) {
2305 isReadOnly = true;
2306 S += 'r';
2307 }
2308 }
2309 if (isReadOnly) {
2310 // Another legacy compatibility encoding. Some ObjC qualifier and type
2311 // combinations need to be rearranged.
2312 // Rewrite "in const" from "nr" to "rn"
2313 const char * s = S.c_str();
2314 int len = S.length();
2315 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2316 std::string replace = "rn";
2317 S.replace(S.end()-2, S.end(), replace);
2318 }
2319 }
Steve Naroff17c03822009-02-12 17:52:19 +00002320 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002321 S += '@';
2322 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002323 }
2324 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002325 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002326 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002327 // Another historical/compatibility reason.
2328 // We encode the underlying type which comes out as
2329 // {...};
2330 S += '^';
2331 getObjCEncodingForTypeImpl(PointeeTy, S,
2332 false, ExpandPointedToStructures,
2333 NULL);
2334 return;
2335 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002336 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002337 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002338 const ObjCInterfaceType *OIT =
2339 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002340 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002341 S += '"';
2342 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002343 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2344 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2345 S += '<';
2346 S += Proto->getNameAsString();
2347 S += '>';
2348 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002349 S += '"';
2350 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002351 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002352 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002353 S += '#';
2354 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002355 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002356 S += ':';
2357 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002358 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002359
2360 if (PointeeTy->isCharType()) {
2361 // char pointer types should be encoded as '*' unless it is a
2362 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002363 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002364 S += '*';
2365 return;
2366 }
2367 }
2368
2369 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002370 getLegacyIntegralTypeEncoding(PointeeTy);
2371
2372 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002373 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002374 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002375 } else if (const ArrayType *AT =
2376 // Ignore type qualifiers etc.
2377 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002378 if (isa<IncompleteArrayType>(AT)) {
2379 // Incomplete arrays are encoded as a pointer to the array element.
2380 S += '^';
2381
2382 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2383 false, ExpandStructures, FD);
2384 } else {
2385 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002386
Anders Carlsson858c64d2009-02-22 01:38:57 +00002387 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2388 S += llvm::utostr(CAT->getSize().getZExtValue());
2389 else {
2390 //Variable length arrays are encoded as a regular array with 0 elements.
2391 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2392 S += '0';
2393 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002394
Anders Carlsson858c64d2009-02-22 01:38:57 +00002395 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2396 false, ExpandStructures, FD);
2397 S += ']';
2398 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002399 } else if (T->getAsFunctionType()) {
2400 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002401 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002402 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002403 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002404 // Anonymous structures print as '?'
2405 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2406 S += II->getName();
2407 } else {
2408 S += '?';
2409 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002410 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002411 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002412 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2413 FieldEnd = RDecl->field_end();
2414 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002415 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002416 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002417 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002418 S += '"';
2419 }
2420
2421 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002422 if (Field->isBitField()) {
2423 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2424 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002425 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002426 QualType qt = Field->getType();
2427 getLegacyIntegralTypeEncoding(qt);
2428 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002429 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002430 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002431 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002432 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002433 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002434 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002435 if (FD && FD->isBitField())
2436 EncodeBitField(this, S, FD);
2437 else
2438 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002439 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002440 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002441 } else if (T->isObjCInterfaceType()) {
2442 // @encode(class_name)
2443 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2444 S += '{';
2445 const IdentifierInfo *II = OI->getIdentifier();
2446 S += II->getName();
2447 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002448 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002449 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002450 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002451 if (RecFields[i]->isBitField())
2452 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2453 RecFields[i]);
2454 else
2455 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2456 FD);
2457 }
2458 S += '}';
2459 }
2460 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002461 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002462}
2463
Ted Kremenek42730c52008-01-07 19:49:32 +00002464void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002465 std::string& S) const {
2466 if (QT & Decl::OBJC_TQ_In)
2467 S += 'n';
2468 if (QT & Decl::OBJC_TQ_Inout)
2469 S += 'N';
2470 if (QT & Decl::OBJC_TQ_Out)
2471 S += 'o';
2472 if (QT & Decl::OBJC_TQ_Bycopy)
2473 S += 'O';
2474 if (QT & Decl::OBJC_TQ_Byref)
2475 S += 'R';
2476 if (QT & Decl::OBJC_TQ_Oneway)
2477 S += 'V';
2478}
2479
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002480void ASTContext::setBuiltinVaListType(QualType T)
2481{
2482 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2483
2484 BuiltinVaListType = T;
2485}
2486
Ted Kremenek42730c52008-01-07 19:49:32 +00002487void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002488{
Ted Kremenek42730c52008-01-07 19:49:32 +00002489 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002490
2491 // typedef struct objc_object *id;
2492 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002493 // User error - caller will issue diagnostics.
2494 if (!ptr)
2495 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002496 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002497 // User error - caller will issue diagnostics.
2498 if (!rec)
2499 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002500 IdStructType = rec;
2501}
2502
Ted Kremenek42730c52008-01-07 19:49:32 +00002503void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002504{
Ted Kremenek42730c52008-01-07 19:49:32 +00002505 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002506
2507 // typedef struct objc_selector *SEL;
2508 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002509 if (!ptr)
2510 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002511 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002512 if (!rec)
2513 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002514 SelStructType = rec;
2515}
2516
Ted Kremenek42730c52008-01-07 19:49:32 +00002517void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002518{
Ted Kremenek42730c52008-01-07 19:49:32 +00002519 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002520}
2521
Ted Kremenek42730c52008-01-07 19:49:32 +00002522void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002523{
Ted Kremenek42730c52008-01-07 19:49:32 +00002524 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002525
2526 // typedef struct objc_class *Class;
2527 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2528 assert(ptr && "'Class' incorrectly typed");
2529 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2530 assert(rec && "'Class' incorrectly typed");
2531 ClassStructType = rec;
2532}
2533
Ted Kremenek42730c52008-01-07 19:49:32 +00002534void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2535 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002536 "'NSConstantString' type already set!");
2537
Ted Kremenek42730c52008-01-07 19:49:32 +00002538 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002539}
2540
Douglas Gregordd13e842009-03-30 22:58:21 +00002541/// \brief Retrieve the template name that represents a qualified
2542/// template name such as \c std::vector.
2543TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2544 bool TemplateKeyword,
2545 TemplateDecl *Template) {
2546 llvm::FoldingSetNodeID ID;
2547 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2548
2549 void *InsertPos = 0;
2550 QualifiedTemplateName *QTN =
2551 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2552 if (!QTN) {
2553 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2554 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2555 }
2556
2557 return TemplateName(QTN);
2558}
2559
2560/// \brief Retrieve the template name that represents a dependent
2561/// template name such as \c MetaFun::template apply.
2562TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2563 const IdentifierInfo *Name) {
2564 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2565
2566 llvm::FoldingSetNodeID ID;
2567 DependentTemplateName::Profile(ID, NNS, Name);
2568
2569 void *InsertPos = 0;
2570 DependentTemplateName *QTN =
2571 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2572
2573 if (QTN)
2574 return TemplateName(QTN);
2575
2576 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2577 if (CanonNNS == NNS) {
2578 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2579 } else {
2580 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2581 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2582 }
2583
2584 DependentTemplateNames.InsertNode(QTN, InsertPos);
2585 return TemplateName(QTN);
2586}
2587
Douglas Gregorc6507e42008-11-03 14:12:49 +00002588/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002589/// TargetInfo, produce the corresponding type. The unsigned @p Type
2590/// is actually a value of type @c TargetInfo::IntType.
2591QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002592 switch (Type) {
2593 case TargetInfo::NoInt: return QualType();
2594 case TargetInfo::SignedShort: return ShortTy;
2595 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2596 case TargetInfo::SignedInt: return IntTy;
2597 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2598 case TargetInfo::SignedLong: return LongTy;
2599 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2600 case TargetInfo::SignedLongLong: return LongLongTy;
2601 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2602 }
2603
2604 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002605 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002606}
Ted Kremenek118930e2008-07-24 23:58:27 +00002607
2608//===----------------------------------------------------------------------===//
2609// Type Predicates.
2610//===----------------------------------------------------------------------===//
2611
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002612/// isObjCNSObjectType - Return true if this is an NSObject object using
2613/// NSObject attribute on a c-style pointer type.
2614/// FIXME - Make it work directly on types.
2615///
2616bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2617 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2618 if (TypedefDecl *TD = TDT->getDecl())
2619 if (TD->getAttr<ObjCNSObjectAttr>())
2620 return true;
2621 }
2622 return false;
2623}
2624
Ted Kremenek118930e2008-07-24 23:58:27 +00002625/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2626/// to an object type. This includes "id" and "Class" (two 'special' pointers
2627/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2628/// ID type).
2629bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002630 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002631 return true;
2632
Steve Naroffd9e00802008-10-21 18:24:04 +00002633 // Blocks are objects.
2634 if (Ty->isBlockPointerType())
2635 return true;
2636
2637 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002638 if (!Ty->isPointerType())
2639 return false;
2640
2641 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2642 // pointer types. This looks for the typedef specifically, not for the
2643 // underlying type.
Eli Friedman9c2b33f2009-03-22 23:00:19 +00002644 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2645 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002646 return true;
2647
2648 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002649 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2650 return true;
2651
2652 // If is has NSObject attribute, OK as well.
2653 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002654}
2655
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002656/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2657/// garbage collection attribute.
2658///
2659QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002660 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002661 if (getLangOptions().ObjC1 &&
2662 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002663 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002664 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002665 // (or pointers to them) be treated as though they were declared
2666 // as __strong.
2667 if (GCAttrs == QualType::GCNone) {
2668 if (isObjCObjectPointerType(Ty))
2669 GCAttrs = QualType::Strong;
2670 else if (Ty->isPointerType())
2671 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2672 }
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002673 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002674 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002675}
2676
Chris Lattner6ff358b2008-04-07 06:51:04 +00002677//===----------------------------------------------------------------------===//
2678// Type Compatibility Testing
2679//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002680
Steve Naroff3454b6c2008-09-04 15:10:53 +00002681/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002682/// block types. Types must be strictly compatible here. For example,
2683/// C unfortunately doesn't produce an error for the following:
2684///
2685/// int (*emptyArgFunc)();
2686/// int (*intArgList)(int) = emptyArgFunc;
2687///
2688/// For blocks, we will produce an error for the following (similar to C++):
2689///
2690/// int (^emptyArgBlock)();
2691/// int (^intArgBlock)(int) = emptyArgBlock;
2692///
2693/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2694///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002695bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002696 const FunctionType *lbase = lhs->getAsFunctionType();
2697 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002698 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2699 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stump896ceed2009-04-01 01:17:39 +00002700 if (lproto && rproto == 0)
2701 return false;
2702 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff3454b6c2008-09-04 15:10:53 +00002703}
2704
Chris Lattner6ff358b2008-04-07 06:51:04 +00002705/// areCompatVectorTypes - Return true if the two specified vector types are
2706/// compatible.
2707static bool areCompatVectorTypes(const VectorType *LHS,
2708 const VectorType *RHS) {
2709 assert(LHS->isCanonical() && RHS->isCanonical());
2710 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002711 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002712}
2713
Eli Friedman0d9549b2008-08-22 00:56:42 +00002714/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002715/// compatible for assignment from RHS to LHS. This handles validation of any
2716/// protocol qualifiers on the LHS or RHS.
2717///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002718bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2719 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002720 // Verify that the base decls are compatible: the RHS must be a subclass of
2721 // the LHS.
2722 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2723 return false;
2724
2725 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2726 // protocol qualified at all, then we are good.
2727 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2728 return true;
2729
2730 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2731 // isn't a superset.
2732 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2733 return true; // FIXME: should return false!
2734
2735 // Finally, we must have two protocol-qualified interfaces.
2736 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2737 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002738
Steve Naroff98e71b82009-03-01 16:12:44 +00002739 // All LHS protocols must have a presence on the RHS.
2740 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002741
Steve Naroff98e71b82009-03-01 16:12:44 +00002742 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2743 LHSPE = LHSP->qual_end();
2744 LHSPI != LHSPE; LHSPI++) {
2745 bool RHSImplementsProtocol = false;
2746
2747 // If the RHS doesn't implement the protocol on the left, the types
2748 // are incompatible.
2749 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2750 RHSPE = RHSP->qual_end();
2751 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2752 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2753 RHSImplementsProtocol = true;
2754 }
2755 // FIXME: For better diagnostics, consider passing back the protocol name.
2756 if (!RHSImplementsProtocol)
2757 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002758 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002759 // The RHS implements all protocols listed on the LHS.
2760 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002761}
2762
Steve Naroff17c03822009-02-12 17:52:19 +00002763bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2764 // get the "pointed to" types
2765 const PointerType *LHSPT = LHS->getAsPointerType();
2766 const PointerType *RHSPT = RHS->getAsPointerType();
2767
2768 if (!LHSPT || !RHSPT)
2769 return false;
2770
2771 QualType lhptee = LHSPT->getPointeeType();
2772 QualType rhptee = RHSPT->getPointeeType();
2773 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2774 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2775 // ID acts sort of like void* for ObjC interfaces
2776 if (LHSIface && isObjCIdStructType(rhptee))
2777 return true;
2778 if (RHSIface && isObjCIdStructType(lhptee))
2779 return true;
2780 if (!LHSIface || !RHSIface)
2781 return false;
2782 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2783 canAssignObjCInterfaces(RHSIface, LHSIface);
2784}
2785
Steve Naroff85f0dc52007-10-15 20:41:53 +00002786/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2787/// both shall have the identically qualified version of a compatible type.
2788/// C99 6.2.7p1: Two types have compatible types if their types are the
2789/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002790bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2791 return !mergeTypes(LHS, RHS).isNull();
2792}
2793
2794QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2795 const FunctionType *lbase = lhs->getAsFunctionType();
2796 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002797 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2798 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002799 bool allLTypes = true;
2800 bool allRTypes = true;
2801
2802 // Check return type
2803 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2804 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002805 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2806 allLTypes = false;
2807 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2808 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002809
2810 if (lproto && rproto) { // two C99 style function prototypes
2811 unsigned lproto_nargs = lproto->getNumArgs();
2812 unsigned rproto_nargs = rproto->getNumArgs();
2813
2814 // Compatible functions must have the same number of arguments
2815 if (lproto_nargs != rproto_nargs)
2816 return QualType();
2817
2818 // Variadic and non-variadic functions aren't compatible
2819 if (lproto->isVariadic() != rproto->isVariadic())
2820 return QualType();
2821
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002822 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2823 return QualType();
2824
Eli Friedman0d9549b2008-08-22 00:56:42 +00002825 // Check argument compatibility
2826 llvm::SmallVector<QualType, 10> types;
2827 for (unsigned i = 0; i < lproto_nargs; i++) {
2828 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2829 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2830 QualType argtype = mergeTypes(largtype, rargtype);
2831 if (argtype.isNull()) return QualType();
2832 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002833 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2834 allLTypes = false;
2835 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2836 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002837 }
2838 if (allLTypes) return lhs;
2839 if (allRTypes) return rhs;
2840 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002841 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002842 }
2843
2844 if (lproto) allRTypes = false;
2845 if (rproto) allLTypes = false;
2846
Douglas Gregor4fa58902009-02-26 23:50:07 +00002847 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002848 if (proto) {
2849 if (proto->isVariadic()) return QualType();
2850 // Check that the types are compatible with the types that
2851 // would result from default argument promotions (C99 6.7.5.3p15).
2852 // The only types actually affected are promotable integer
2853 // types and floats, which would be passed as a different
2854 // type depending on whether the prototype is visible.
2855 unsigned proto_nargs = proto->getNumArgs();
2856 for (unsigned i = 0; i < proto_nargs; ++i) {
2857 QualType argTy = proto->getArgType(i);
2858 if (argTy->isPromotableIntegerType() ||
2859 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2860 return QualType();
2861 }
2862
2863 if (allLTypes) return lhs;
2864 if (allRTypes) return rhs;
2865 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002866 proto->getNumArgs(), lproto->isVariadic(),
2867 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002868 }
2869
2870 if (allLTypes) return lhs;
2871 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002872 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002873}
2874
2875QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002876 // C++ [expr]: If an expression initially has the type "reference to T", the
2877 // type is adjusted to "T" prior to any further analysis, the expression
2878 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002879 // expression is an lvalue unless the reference is an rvalue reference and
2880 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002881 // FIXME: C++ shouldn't be going through here! The rules are different
2882 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00002883 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2884 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002885 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002886 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002887 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002888 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002889
Eli Friedman0d9549b2008-08-22 00:56:42 +00002890 QualType LHSCan = getCanonicalType(LHS),
2891 RHSCan = getCanonicalType(RHS);
2892
2893 // If two types are identical, they are compatible.
2894 if (LHSCan == RHSCan)
2895 return LHS;
2896
2897 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002898 // Note that we handle extended qualifiers later, in the
2899 // case for ExtQualType.
2900 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002901 return QualType();
2902
2903 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2904 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2905
Chris Lattnerc38d4522008-01-14 05:45:46 +00002906 // We want to consider the two function types to be the same for these
2907 // comparisons, just force one to the other.
2908 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2909 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002910
2911 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002912 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2913 LHSClass = Type::ConstantArray;
2914 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2915 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002916
Nate Begemanaf6ed502008-04-18 23:10:10 +00002917 // Canonicalize ExtVector -> Vector.
2918 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2919 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002920
Chris Lattner7cdcb252008-04-07 06:38:24 +00002921 // Consider qualified interfaces and interfaces the same.
2922 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2923 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002924
Chris Lattnerb5709e22008-04-07 05:43:21 +00002925 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002926 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002927 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2928 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2929
2930 // ID acts sort of like void* for ObjC interfaces
2931 if (LHSIface && isObjCIdStructType(RHS))
2932 return LHS;
2933 if (RHSIface && isObjCIdStructType(LHS))
2934 return RHS;
2935
Steve Naroff28ceff72008-12-10 22:14:21 +00002936 // ID is compatible with all qualified id types.
2937 if (LHS->isObjCQualifiedIdType()) {
2938 if (const PointerType *PT = RHS->getAsPointerType()) {
2939 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002940 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002941 return LHS;
2942 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2943 // Unfortunately, this API is part of Sema (which we don't have access
2944 // to. Need to refactor. The following check is insufficient, since we
2945 // need to make sure the class implements the protocol.
2946 if (pType->isObjCInterfaceType())
2947 return LHS;
2948 }
2949 }
2950 if (RHS->isObjCQualifiedIdType()) {
2951 if (const PointerType *PT = LHS->getAsPointerType()) {
2952 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002953 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002954 return RHS;
2955 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2956 // Unfortunately, this API is part of Sema (which we don't have access
2957 // to. Need to refactor. The following check is insufficient, since we
2958 // need to make sure the class implements the protocol.
2959 if (pType->isObjCInterfaceType())
2960 return RHS;
2961 }
2962 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002963 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2964 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002965 if (const EnumType* ETy = LHS->getAsEnumType()) {
2966 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2967 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002968 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002969 if (const EnumType* ETy = RHS->getAsEnumType()) {
2970 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2971 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002972 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002973
Eli Friedman0d9549b2008-08-22 00:56:42 +00002974 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002975 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002976
Steve Naroffc88babe2008-01-09 22:43:08 +00002977 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002978 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00002979#define TYPE(Class, Base)
2980#define ABSTRACT_TYPE(Class, Base)
2981#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2982#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2983#include "clang/AST/TypeNodes.def"
2984 assert(false && "Non-canonical and dependent types shouldn't get here");
2985 return QualType();
2986
Sebastian Redlce6fff02009-03-16 23:22:08 +00002987 case Type::LValueReference:
2988 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00002989 case Type::MemberPointer:
2990 assert(false && "C++ should never be in mergeTypes");
2991 return QualType();
2992
2993 case Type::IncompleteArray:
2994 case Type::VariableArray:
2995 case Type::FunctionProto:
2996 case Type::ExtVector:
2997 case Type::ObjCQualifiedInterface:
2998 assert(false && "Types are eliminated above");
2999 return QualType();
3000
Chris Lattnerc38d4522008-01-14 05:45:46 +00003001 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003002 {
3003 // Merge two pointer types, while trying to preserve typedef info
3004 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3005 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3006 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3007 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003008 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3009 return LHS;
3010 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3011 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003012 return getPointerType(ResultType);
3013 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003014 case Type::BlockPointer:
3015 {
3016 // Merge two block pointer types, while trying to preserve typedef info
3017 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3018 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3019 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3020 if (ResultType.isNull()) return QualType();
3021 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3022 return LHS;
3023 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3024 return RHS;
3025 return getBlockPointerType(ResultType);
3026 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003027 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003028 {
3029 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3030 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3031 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3032 return QualType();
3033
3034 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3035 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3036 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3037 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003038 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3039 return LHS;
3040 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3041 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003042 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3043 ArrayType::ArraySizeModifier(), 0);
3044 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3045 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003046 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3047 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003048 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3049 return LHS;
3050 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3051 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003052 if (LVAT) {
3053 // FIXME: This isn't correct! But tricky to implement because
3054 // the array's size has to be the size of LHS, but the type
3055 // has to be different.
3056 return LHS;
3057 }
3058 if (RVAT) {
3059 // FIXME: This isn't correct! But tricky to implement because
3060 // the array's size has to be the size of RHS, but the type
3061 // has to be different.
3062 return RHS;
3063 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003064 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3065 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003066 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003067 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003068 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003069 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003070 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003071 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003072 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003073 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3074 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003075 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003076 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003077 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003078 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003079 case Type::Complex:
3080 // Distinct complex types are incompatible.
3081 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003082 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003083 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003084 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3085 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003086 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003087 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003088 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003089 // FIXME: This should be type compatibility, e.g. whether
3090 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003091 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3092 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3093 if (LHSIface && RHSIface &&
3094 canAssignObjCInterfaces(LHSIface, RHSIface))
3095 return LHS;
3096
Eli Friedman0d9549b2008-08-22 00:56:42 +00003097 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003098 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003099 case Type::ObjCQualifiedId:
3100 // Distinct qualified id's are not compatible.
3101 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003102 case Type::FixedWidthInt:
3103 // Distinct fixed-width integers are not compatible.
3104 return QualType();
3105 case Type::ObjCQualifiedClass:
3106 // Distinct qualified classes are not compatible.
3107 return QualType();
3108 case Type::ExtQual:
3109 // FIXME: ExtQual types can be compatible even if they're not
3110 // identical!
3111 return QualType();
3112 // First attempt at an implementation, but I'm not really sure it's
3113 // right...
3114#if 0
3115 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3116 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3117 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3118 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3119 return QualType();
3120 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3121 LHSBase = QualType(LQual->getBaseType(), 0);
3122 RHSBase = QualType(RQual->getBaseType(), 0);
3123 ResultType = mergeTypes(LHSBase, RHSBase);
3124 if (ResultType.isNull()) return QualType();
3125 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3126 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3127 return LHS;
3128 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3129 return RHS;
3130 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3131 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3132 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3133 return ResultType;
3134#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003135
3136 case Type::TemplateSpecialization:
3137 assert(false && "Dependent types have no size");
3138 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003139 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003140
3141 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003142}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003143
Chris Lattner1d78a862008-04-07 07:01:58 +00003144//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003145// Integer Predicates
3146//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003147
Eli Friedman0832dbc2008-06-28 06:23:08 +00003148unsigned ASTContext::getIntWidth(QualType T) {
3149 if (T == BoolTy)
3150 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003151 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3152 return FWIT->getWidth();
3153 }
3154 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003155 return (unsigned)getTypeSize(T);
3156}
3157
3158QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3159 assert(T->isSignedIntegerType() && "Unexpected type");
3160 if (const EnumType* ETy = T->getAsEnumType())
3161 T = ETy->getDecl()->getIntegerType();
3162 const BuiltinType* BTy = T->getAsBuiltinType();
3163 assert (BTy && "Unexpected signed integer type");
3164 switch (BTy->getKind()) {
3165 case BuiltinType::Char_S:
3166 case BuiltinType::SChar:
3167 return UnsignedCharTy;
3168 case BuiltinType::Short:
3169 return UnsignedShortTy;
3170 case BuiltinType::Int:
3171 return UnsignedIntTy;
3172 case BuiltinType::Long:
3173 return UnsignedLongTy;
3174 case BuiltinType::LongLong:
3175 return UnsignedLongLongTy;
3176 default:
3177 assert(0 && "Unexpected signed integer type");
3178 return QualType();
3179 }
3180}
3181
3182
3183//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00003184// Serialization Support
3185//===----------------------------------------------------------------------===//
3186
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003187enum {
3188 BasicMetadataBlock = 1,
3189 ASTContextBlock = 2,
3190 DeclsBlock = 3
3191};
3192
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003193void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3194 // Create bitstream.
3195 llvm::BitstreamWriter Stream(Buffer);
3196
3197 // Emit the preamble.
3198 Stream.Emit((unsigned)'B', 8);
3199 Stream.Emit((unsigned)'C', 8);
3200 Stream.Emit(0xC, 4);
3201 Stream.Emit(0xF, 4);
3202 Stream.Emit(0xE, 4);
3203 Stream.Emit(0x0, 4);
3204
3205 // Create serializer.
3206 llvm::Serializer S(Stream);
3207
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003208 // ===---------------------------------------------------===/
3209 // Serialize the "Translation Unit" metadata.
3210 // ===---------------------------------------------------===/
3211
3212 // Emit ASTContext.
3213 S.EnterBlock(ASTContextBlock);
3214 S.EmitOwnedPtr(this);
3215 S.ExitBlock(); // exit "ASTContextBlock"
3216
3217 S.EnterBlock(BasicMetadataBlock);
3218
3219 // Block for SourceManager and Target. Allows easy skipping
3220 // around to the block for the Selectors during deserialization.
3221 S.EnterBlock();
3222
3223 // Emit the SourceManager.
3224 S.Emit(getSourceManager());
3225
3226 // Emit the Target.
3227 S.EmitPtr(&Target);
3228 S.EmitCStr(Target.getTargetTriple());
3229
3230 S.ExitBlock(); // exit "SourceManager and Target Block"
3231
3232 // Emit the Selectors.
3233 S.Emit(Selectors);
3234
3235 // Emit the Identifier Table.
3236 S.Emit(Idents);
3237
3238 S.ExitBlock(); // exit "BasicMetadataBlock"
3239}
3240
3241
Ted Kremenek738e6c02007-10-31 17:10:13 +00003242/// Emit - Serialize an ASTContext object to Bitcode.
3243void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00003244 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00003245 S.EmitRef(SourceMgr);
3246 S.EmitRef(Target);
3247 S.EmitRef(Idents);
3248 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003249
Ted Kremenek68228a92007-10-31 22:44:07 +00003250 // Emit the size of the type vector so that we can reserve that size
3251 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003252 S.EmitInt(Types.size());
3253
Ted Kremenek034a78c2007-11-13 22:02:55 +00003254 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3255 I!=E;++I)
3256 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003257
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003258 S.EmitOwnedPtr(TUDecl);
3259
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003260 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003261}
3262
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003263
3264ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3265 FileManager &FMgr) {
3266 // Check if the file is of the proper length.
3267 if (Buffer.getBufferSize() & 0x3) {
3268 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3269 return 0;
3270 }
3271
3272 // Create the bitstream reader.
3273 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3274 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3275
3276 if (Stream.Read(8) != 'B' ||
3277 Stream.Read(8) != 'C' ||
3278 Stream.Read(4) != 0xC ||
3279 Stream.Read(4) != 0xF ||
3280 Stream.Read(4) != 0xE ||
3281 Stream.Read(4) != 0x0) {
3282 // FIXME: Provide diagnostic.
3283 return NULL;
3284 }
3285
3286 // Create the deserializer.
3287 llvm::Deserializer Dezr(Stream);
3288
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003289 // ===---------------------------------------------------===/
3290 // Deserialize the "Translation Unit" metadata.
3291 // ===---------------------------------------------------===/
3292
3293 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3294 // (which will appear earlier) and record its location.
3295
3296 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3297 assert (FoundBlock);
3298
3299 llvm::Deserializer::Location ASTContextBlockLoc =
3300 Dezr.getCurrentBlockLocation();
3301
3302 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3303 assert (FoundBlock);
3304
3305 // Read the SourceManager.
3306 SourceManager::CreateAndRegister(Dezr, FMgr);
3307
3308 { // Read the TargetInfo.
3309 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3310 char* triple = Dezr.ReadCStr(NULL,0,true);
3311 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3312 delete [] triple;
3313 }
3314
3315 // For Selectors, we must read the identifier table first because the
3316 // SelectorTable depends on the identifiers being already deserialized.
3317 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3318 Dezr.SkipBlock();
3319
3320 // Read the identifier table.
3321 IdentifierTable::CreateAndRegister(Dezr);
3322
3323 // Now jump back and read the selectors.
3324 Dezr.JumpTo(SelectorBlkLoc);
3325 SelectorTable::CreateAndRegister(Dezr);
3326
3327 // Now jump back to ASTContextBlock and read the ASTContext.
3328 Dezr.JumpTo(ASTContextBlockLoc);
3329 return Dezr.ReadOwnedPtr<ASTContext>();
3330}
3331
Ted Kremenekacba3612007-11-13 00:25:37 +00003332ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00003333
3334 // Read the language options.
3335 LangOptions LOpts;
3336 LOpts.Read(D);
3337
Ted Kremenek68228a92007-10-31 22:44:07 +00003338 SourceManager &SM = D.ReadRef<SourceManager>();
3339 TargetInfo &t = D.ReadRef<TargetInfo>();
3340 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3341 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00003342
Ted Kremenek68228a92007-10-31 22:44:07 +00003343 unsigned size_reserve = D.ReadInt();
3344
Douglas Gregor24afd4a2008-11-17 14:58:09 +00003345 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3346 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00003347
Ted Kremenek034a78c2007-11-13 22:02:55 +00003348 for (unsigned i = 0; i < size_reserve; ++i)
3349 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00003350
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003351 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3352
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003353 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00003354
3355 return A;
3356}