blob: d056ed4cb70b15959349e14b6c4ea9006f72a749 [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;
Daniel Dunbar998510f2009-04-08 20:18:15 +0000682 unsigned FieldCount =
683 D->ivar_size() + std::distance(D->prop_begin(), D->prop_end());
Devang Patel8682d882008-06-06 02:14:01 +0000684 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
685 FieldCount++;
686 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
687 unsigned Alignment = SL.getAlignment();
688 uint64_t Size = SL.getSize();
689 NewEntry = new ASTRecordLayout(Size, Alignment);
690 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000691 // Super class is at the beginning of the layout.
692 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000693 } else {
694 NewEntry = new ASTRecordLayout();
695 NewEntry->InitializeLayout(FieldCount);
696 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000697 Entry = NewEntry;
698
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000699 unsigned StructPacking = 0;
700 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
701 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000702
703 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
704 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
705 AA->getAlignment()));
706
707 // Layout each ivar sequentially.
708 unsigned i = 0;
709 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
710 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
711 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000712 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000713 }
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000714 // Also synthesized ivars
715 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(),
716 E = D->prop_end(); I != E; ++I) {
717 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
718 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
719 }
Fariborz Jahanian84c45692009-04-01 19:37:34 +0000720
Devang Patel4b6bf702008-06-04 21:54:36 +0000721 // Finally, round the size of the total struct up to the alignment of the
722 // struct itself.
723 NewEntry->FinalizeLayout();
724 return *NewEntry;
725}
726
Devang Patel7a78e432007-11-01 19:11:01 +0000727/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000728/// specified record (struct/union/class), which indicates its size and field
729/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000730const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000731 D = D->getDefinition(*this);
732 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000733
Chris Lattner4b009652007-07-25 00:24:17 +0000734 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000735 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000736 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000737
Devang Patel7a78e432007-11-01 19:11:01 +0000738 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
739 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
740 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000741 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000742
Douglas Gregor39677622008-12-11 20:41:00 +0000743 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000744 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000745 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000746
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000747 unsigned StructPacking = 0;
748 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
749 StructPacking = PA->getAlignment();
750
Eli Friedman5949a022008-05-30 09:31:38 +0000751 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000752 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
753 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000754
Eli Friedman5949a022008-05-30 09:31:38 +0000755 // Layout each field, for now, just sequentially, respecting alignment. In
756 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000757 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000758 for (RecordDecl::field_iterator Field = D->field_begin(),
759 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000760 Field != FieldEnd; (void)++Field, ++FieldIdx)
761 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000762
763 // Finally, round the size of the total struct up to the alignment of the
764 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000765 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000766 return *NewEntry;
767}
768
Chris Lattner4b009652007-07-25 00:24:17 +0000769//===----------------------------------------------------------------------===//
770// Type creation/memoization methods
771//===----------------------------------------------------------------------===//
772
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000773QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000774 QualType CanT = getCanonicalType(T);
775 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000776 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000777
778 // If we are composing extended qualifiers together, merge together into one
779 // ExtQualType node.
780 unsigned CVRQuals = T.getCVRQualifiers();
781 QualType::GCAttrTypes GCAttr = QualType::GCNone;
782 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000783
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000784 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
785 // If this type already has an address space specified, it cannot get
786 // another one.
787 assert(EQT->getAddressSpace() == 0 &&
788 "Type cannot be in multiple addr spaces!");
789 GCAttr = EQT->getObjCGCAttr();
790 TypeNode = EQT->getBaseType();
791 }
Chris Lattner35fef522008-02-20 20:55:12 +0000792
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000793 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000794 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000795 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000796 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000797 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000798 return QualType(EXTQy, CVRQuals);
799
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000800 // If the base type isn't canonical, this won't be a canonical type either,
801 // so fill in the canonical type field.
802 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000803 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000804 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000805
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000806 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000807 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000808 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000809 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000810 ExtQualType *New =
811 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000812 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000813 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000814 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000815}
816
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000817QualType ASTContext::getObjCGCQualType(QualType T,
818 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000819 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000820 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000821 return T;
822
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000823 // If we are composing extended qualifiers together, merge together into one
824 // ExtQualType node.
825 unsigned CVRQuals = T.getCVRQualifiers();
826 Type *TypeNode = T.getTypePtr();
827 unsigned AddressSpace = 0;
828
829 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
830 // If this type already has an address space specified, it cannot get
831 // another one.
832 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
833 "Type cannot be in multiple addr spaces!");
834 AddressSpace = EQT->getAddressSpace();
835 TypeNode = EQT->getBaseType();
836 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000837
838 // Check if we've already instantiated an gc qual'd type of this type.
839 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000840 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000841 void *InsertPos = 0;
842 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000843 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000844
845 // If the base type isn't canonical, this won't be a canonical type either,
846 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +0000847 // FIXME: Isn't this also not canonical if the base type is a array
848 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000849 QualType Canonical;
850 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000851 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000852
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000853 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000854 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
855 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
856 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000857 ExtQualType *New =
858 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000859 ExtQualTypes.InsertNode(New, InsertPos);
860 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000861 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000862}
Chris Lattner4b009652007-07-25 00:24:17 +0000863
864/// getComplexType - Return the uniqued reference to the type for a complex
865/// number with the specified element type.
866QualType ASTContext::getComplexType(QualType T) {
867 // Unique pointers, to guarantee there is only one pointer of a particular
868 // structure.
869 llvm::FoldingSetNodeID ID;
870 ComplexType::Profile(ID, T);
871
872 void *InsertPos = 0;
873 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
874 return QualType(CT, 0);
875
876 // If the pointee type isn't canonical, this won't be a canonical type either,
877 // so fill in the canonical type field.
878 QualType Canonical;
879 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000880 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000881
882 // Get the new insert position for the node we care about.
883 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000884 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000885 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000886 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000887 Types.push_back(New);
888 ComplexTypes.InsertNode(New, InsertPos);
889 return QualType(New, 0);
890}
891
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000892QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
893 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
894 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
895 FixedWidthIntType *&Entry = Map[Width];
896 if (!Entry)
897 Entry = new FixedWidthIntType(Width, Signed);
898 return QualType(Entry, 0);
899}
Chris Lattner4b009652007-07-25 00:24:17 +0000900
901/// getPointerType - Return the uniqued reference to the type for a pointer to
902/// the specified type.
903QualType ASTContext::getPointerType(QualType T) {
904 // Unique pointers, to guarantee there is only one pointer of a particular
905 // structure.
906 llvm::FoldingSetNodeID ID;
907 PointerType::Profile(ID, T);
908
909 void *InsertPos = 0;
910 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
911 return QualType(PT, 0);
912
913 // If the pointee type isn't canonical, this won't be a canonical type either,
914 // so fill in the canonical type field.
915 QualType Canonical;
916 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000917 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000918
919 // Get the new insert position for the node we care about.
920 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000921 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000922 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000923 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000924 Types.push_back(New);
925 PointerTypes.InsertNode(New, InsertPos);
926 return QualType(New, 0);
927}
928
Steve Naroff7aa54752008-08-27 16:04:49 +0000929/// getBlockPointerType - Return the uniqued reference to the type for
930/// a pointer to the specified block.
931QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000932 assert(T->isFunctionType() && "block of function types only");
933 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000934 // structure.
935 llvm::FoldingSetNodeID ID;
936 BlockPointerType::Profile(ID, T);
937
938 void *InsertPos = 0;
939 if (BlockPointerType *PT =
940 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
941 return QualType(PT, 0);
942
Steve Narofffd5b19d2008-08-28 19:20:44 +0000943 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000944 // type either so fill in the canonical type field.
945 QualType Canonical;
946 if (!T->isCanonical()) {
947 Canonical = getBlockPointerType(getCanonicalType(T));
948
949 // Get the new insert position for the node we care about.
950 BlockPointerType *NewIP =
951 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000952 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000953 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000954 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000955 Types.push_back(New);
956 BlockPointerTypes.InsertNode(New, InsertPos);
957 return QualType(New, 0);
958}
959
Sebastian Redlce6fff02009-03-16 23:22:08 +0000960/// getLValueReferenceType - Return the uniqued reference to the type for an
961/// lvalue reference to the specified type.
962QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +0000963 // Unique pointers, to guarantee there is only one pointer of a particular
964 // structure.
965 llvm::FoldingSetNodeID ID;
966 ReferenceType::Profile(ID, T);
967
968 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000969 if (LValueReferenceType *RT =
970 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000971 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000972
Chris Lattner4b009652007-07-25 00:24:17 +0000973 // If the referencee type isn't canonical, this won't be a canonical type
974 // either, so fill in the canonical type field.
975 QualType Canonical;
976 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000977 Canonical = getLValueReferenceType(getCanonicalType(T));
978
Chris Lattner4b009652007-07-25 00:24:17 +0000979 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000980 LValueReferenceType *NewIP =
981 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000982 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000983 }
984
Sebastian Redlce6fff02009-03-16 23:22:08 +0000985 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000986 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000987 LValueReferenceTypes.InsertNode(New, InsertPos);
988 return QualType(New, 0);
989}
990
991/// getRValueReferenceType - Return the uniqued reference to the type for an
992/// rvalue reference to the specified type.
993QualType ASTContext::getRValueReferenceType(QualType T) {
994 // Unique pointers, to guarantee there is only one pointer of a particular
995 // structure.
996 llvm::FoldingSetNodeID ID;
997 ReferenceType::Profile(ID, T);
998
999 void *InsertPos = 0;
1000 if (RValueReferenceType *RT =
1001 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1002 return QualType(RT, 0);
1003
1004 // If the referencee type isn't canonical, this won't be a canonical type
1005 // either, so fill in the canonical type field.
1006 QualType Canonical;
1007 if (!T->isCanonical()) {
1008 Canonical = getRValueReferenceType(getCanonicalType(T));
1009
1010 // Get the new insert position for the node we care about.
1011 RValueReferenceType *NewIP =
1012 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1013 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1014 }
1015
1016 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1017 Types.push_back(New);
1018 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001019 return QualType(New, 0);
1020}
1021
Sebastian Redl75555032009-01-24 21:16:55 +00001022/// getMemberPointerType - Return the uniqued reference to the type for a
1023/// member pointer to the specified type, in the specified class.
1024QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1025{
1026 // Unique pointers, to guarantee there is only one pointer of a particular
1027 // structure.
1028 llvm::FoldingSetNodeID ID;
1029 MemberPointerType::Profile(ID, T, Cls);
1030
1031 void *InsertPos = 0;
1032 if (MemberPointerType *PT =
1033 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1034 return QualType(PT, 0);
1035
1036 // If the pointee or class type isn't canonical, this won't be a canonical
1037 // type either, so fill in the canonical type field.
1038 QualType Canonical;
1039 if (!T->isCanonical()) {
1040 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1041
1042 // Get the new insert position for the node we care about.
1043 MemberPointerType *NewIP =
1044 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1045 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1046 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001047 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001048 Types.push_back(New);
1049 MemberPointerTypes.InsertNode(New, InsertPos);
1050 return QualType(New, 0);
1051}
1052
Steve Naroff83c13012007-08-30 01:06:46 +00001053/// getConstantArrayType - Return the unique reference to the type for an
1054/// array of the specified element type.
1055QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +00001056 const llvm::APInt &ArySize,
1057 ArrayType::ArraySizeModifier ASM,
1058 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001059 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001060 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001061
1062 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001063 if (ConstantArrayType *ATP =
1064 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001065 return QualType(ATP, 0);
1066
1067 // If the element type isn't canonical, this won't be a canonical type either,
1068 // so fill in the canonical type field.
1069 QualType Canonical;
1070 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001071 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001072 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001073 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001074 ConstantArrayType *NewIP =
1075 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001076 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001077 }
1078
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001079 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001080 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001081 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001082 Types.push_back(New);
1083 return QualType(New, 0);
1084}
1085
Steve Naroffe2579e32007-08-30 18:14:25 +00001086/// getVariableArrayType - Returns a non-unique reference to the type for a
1087/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +00001088QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1089 ArrayType::ArraySizeModifier ASM,
1090 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001091 // Since we don't unique expressions, it isn't possible to unique VLA's
1092 // that have an expression provided for their size.
1093
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001094 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001095 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001096
1097 VariableArrayTypes.push_back(New);
1098 Types.push_back(New);
1099 return QualType(New, 0);
1100}
1101
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001102/// getDependentSizedArrayType - Returns a non-unique reference to
1103/// the type for a dependently-sized array of the specified element
1104/// type. FIXME: We will need these to be uniqued, or at least
1105/// comparable, at some point.
1106QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1107 ArrayType::ArraySizeModifier ASM,
1108 unsigned EltTypeQuals) {
1109 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1110 "Size must be type- or value-dependent!");
1111
1112 // Since we don't unique expressions, it isn't possible to unique
1113 // dependently-sized array types.
1114
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001115 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001116 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1117 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001118
1119 DependentSizedArrayTypes.push_back(New);
1120 Types.push_back(New);
1121 return QualType(New, 0);
1122}
1123
Eli Friedman8ff07782008-02-15 18:16:39 +00001124QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1125 ArrayType::ArraySizeModifier ASM,
1126 unsigned EltTypeQuals) {
1127 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001128 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001129
1130 void *InsertPos = 0;
1131 if (IncompleteArrayType *ATP =
1132 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1133 return QualType(ATP, 0);
1134
1135 // If the element type isn't canonical, this won't be a canonical type
1136 // either, so fill in the canonical type field.
1137 QualType Canonical;
1138
1139 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001140 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001141 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001142
1143 // Get the new insert position for the node we care about.
1144 IncompleteArrayType *NewIP =
1145 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001146 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001147 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001148
Steve Naroff93fd2112009-01-27 22:08:43 +00001149 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001150 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001151
1152 IncompleteArrayTypes.InsertNode(New, InsertPos);
1153 Types.push_back(New);
1154 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001155}
1156
Chris Lattner4b009652007-07-25 00:24:17 +00001157/// getVectorType - Return the unique reference to a vector type of
1158/// the specified element type and size. VectorType must be a built-in type.
1159QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1160 BuiltinType *baseType;
1161
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001162 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001163 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1164
1165 // Check if we've already instantiated a vector of this type.
1166 llvm::FoldingSetNodeID ID;
1167 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1168 void *InsertPos = 0;
1169 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1170 return QualType(VTP, 0);
1171
1172 // If the element type isn't canonical, this won't be a canonical type either,
1173 // so fill in the canonical type field.
1174 QualType Canonical;
1175 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001176 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001177
1178 // Get the new insert position for the node we care about.
1179 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001180 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001181 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001182 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001183 VectorTypes.InsertNode(New, InsertPos);
1184 Types.push_back(New);
1185 return QualType(New, 0);
1186}
1187
Nate Begemanaf6ed502008-04-18 23:10:10 +00001188/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001189/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001190QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001191 BuiltinType *baseType;
1192
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001193 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001194 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001195
1196 // Check if we've already instantiated a vector of this type.
1197 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001198 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001199 void *InsertPos = 0;
1200 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1201 return QualType(VTP, 0);
1202
1203 // If the element type isn't canonical, this won't be a canonical type either,
1204 // so fill in the canonical type field.
1205 QualType Canonical;
1206 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001207 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001208
1209 // Get the new insert position for the node we care about.
1210 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001211 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001212 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001213 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001214 VectorTypes.InsertNode(New, InsertPos);
1215 Types.push_back(New);
1216 return QualType(New, 0);
1217}
1218
Douglas Gregor4fa58902009-02-26 23:50:07 +00001219/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001220///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001221QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001222 // Unique functions, to guarantee there is only one function of a particular
1223 // structure.
1224 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001225 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001226
1227 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001228 if (FunctionNoProtoType *FT =
1229 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001230 return QualType(FT, 0);
1231
1232 QualType Canonical;
1233 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001234 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001235
1236 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001237 FunctionNoProtoType *NewIP =
1238 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001239 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001240 }
1241
Douglas Gregor4fa58902009-02-26 23:50:07 +00001242 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001243 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001244 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001245 return QualType(New, 0);
1246}
1247
1248/// getFunctionType - Return a normal function type with a typed argument
1249/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001250QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001251 unsigned NumArgs, bool isVariadic,
1252 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001253 // Unique functions, to guarantee there is only one function of a particular
1254 // structure.
1255 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001256 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001257 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001258
1259 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001260 if (FunctionProtoType *FTP =
1261 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001262 return QualType(FTP, 0);
1263
1264 // Determine whether the type being created is already canonical or not.
1265 bool isCanonical = ResultTy->isCanonical();
1266 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1267 if (!ArgArray[i]->isCanonical())
1268 isCanonical = false;
1269
1270 // If this type isn't canonical, get the canonical version of it.
1271 QualType Canonical;
1272 if (!isCanonical) {
1273 llvm::SmallVector<QualType, 16> CanonicalArgs;
1274 CanonicalArgs.reserve(NumArgs);
1275 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001276 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001277
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001278 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001279 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001280 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001281
1282 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001283 FunctionProtoType *NewIP =
1284 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001285 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001286 }
1287
Douglas Gregor4fa58902009-02-26 23:50:07 +00001288 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001289 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001290 FunctionProtoType *FTP =
1291 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001292 NumArgs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001293 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001294 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001295 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001296 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001297 return QualType(FTP, 0);
1298}
1299
Douglas Gregor1d661552008-04-13 21:07:44 +00001300/// getTypeDeclType - Return the unique reference to the type for the
1301/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001302QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001303 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001304 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1305
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001306 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001307 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001308 else if (isa<TemplateTypeParmDecl>(Decl)) {
1309 assert(false && "Template type parameter types are always available.");
1310 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001311 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001312
Douglas Gregor2e047592009-02-28 01:32:25 +00001313 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001314 if (PrevDecl)
1315 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001316 else
1317 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001318 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001319 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1320 if (PrevDecl)
1321 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001322 else
1323 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001324 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001325 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001326 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001327
Ted Kremenek46a837c2008-09-05 17:16:31 +00001328 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001329 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001330}
1331
Chris Lattner4b009652007-07-25 00:24:17 +00001332/// getTypedefType - Return the unique reference to the type for the
1333/// specified typename decl.
1334QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1335 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1336
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001337 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001338 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001339 Types.push_back(Decl->TypeForDecl);
1340 return QualType(Decl->TypeForDecl, 0);
1341}
1342
Ted Kremenek42730c52008-01-07 19:49:32 +00001343/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001344/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001345QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001346 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1347
Steve Naroff93fd2112009-01-27 22:08:43 +00001348 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001349 Types.push_back(Decl->TypeForDecl);
1350 return QualType(Decl->TypeForDecl, 0);
1351}
1352
Douglas Gregora4918772009-02-05 23:33:38 +00001353/// \brief Retrieve the template type parameter type for a template
1354/// parameter with the given depth, index, and (optionally) name.
1355QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1356 IdentifierInfo *Name) {
1357 llvm::FoldingSetNodeID ID;
1358 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1359 void *InsertPos = 0;
1360 TemplateTypeParmType *TypeParm
1361 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1362
1363 if (TypeParm)
1364 return QualType(TypeParm, 0);
1365
1366 if (Name)
1367 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1368 getTemplateTypeParmType(Depth, Index));
1369 else
1370 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1371
1372 Types.push_back(TypeParm);
1373 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1374
1375 return QualType(TypeParm, 0);
1376}
1377
Douglas Gregor8e458f42009-02-09 18:46:07 +00001378QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001379ASTContext::getTemplateSpecializationType(TemplateName Template,
1380 const TemplateArgument *Args,
1381 unsigned NumArgs,
1382 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001383 if (!Canon.isNull())
1384 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001385
Douglas Gregor8e458f42009-02-09 18:46:07 +00001386 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001387 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001388
Douglas Gregor8e458f42009-02-09 18:46:07 +00001389 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001390 TemplateSpecializationType *Spec
1391 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001392
1393 if (Spec)
1394 return QualType(Spec, 0);
1395
Douglas Gregordd13e842009-03-30 22:58:21 +00001396 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001397 sizeof(TemplateArgument) * NumArgs),
1398 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001399 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001400 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001401 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001402
1403 return QualType(Spec, 0);
1404}
1405
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001406QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001407ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001408 QualType NamedType) {
1409 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001410 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001411
1412 void *InsertPos = 0;
1413 QualifiedNameType *T
1414 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1415 if (T)
1416 return QualType(T, 0);
1417
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001418 T = new (*this) QualifiedNameType(NNS, NamedType,
1419 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001420 Types.push_back(T);
1421 QualifiedNameTypes.InsertNode(T, InsertPos);
1422 return QualType(T, 0);
1423}
1424
Douglas Gregord3022602009-03-27 23:10:48 +00001425QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1426 const IdentifierInfo *Name,
1427 QualType Canon) {
1428 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1429
1430 if (Canon.isNull()) {
1431 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1432 if (CanonNNS != NNS)
1433 Canon = getTypenameType(CanonNNS, Name);
1434 }
1435
1436 llvm::FoldingSetNodeID ID;
1437 TypenameType::Profile(ID, NNS, Name);
1438
1439 void *InsertPos = 0;
1440 TypenameType *T
1441 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1442 if (T)
1443 return QualType(T, 0);
1444
1445 T = new (*this) TypenameType(NNS, Name, Canon);
1446 Types.push_back(T);
1447 TypenameTypes.InsertNode(T, InsertPos);
1448 return QualType(T, 0);
1449}
1450
Douglas Gregor77da5802009-04-01 00:28:59 +00001451QualType
1452ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1453 const TemplateSpecializationType *TemplateId,
1454 QualType Canon) {
1455 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1456
1457 if (Canon.isNull()) {
1458 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1459 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1460 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1461 const TemplateSpecializationType *CanonTemplateId
1462 = CanonType->getAsTemplateSpecializationType();
1463 assert(CanonTemplateId &&
1464 "Canonical type must also be a template specialization type");
1465 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1466 }
1467 }
1468
1469 llvm::FoldingSetNodeID ID;
1470 TypenameType::Profile(ID, NNS, TemplateId);
1471
1472 void *InsertPos = 0;
1473 TypenameType *T
1474 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1475 if (T)
1476 return QualType(T, 0);
1477
1478 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1479 Types.push_back(T);
1480 TypenameTypes.InsertNode(T, InsertPos);
1481 return QualType(T, 0);
1482}
1483
Chris Lattnere1352302008-04-07 04:56:42 +00001484/// CmpProtocolNames - Comparison predicate for sorting protocols
1485/// alphabetically.
1486static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1487 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001488 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001489}
1490
1491static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1492 unsigned &NumProtocols) {
1493 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1494
1495 // Sort protocols, keyed by name.
1496 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1497
1498 // Remove duplicates.
1499 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1500 NumProtocols = ProtocolsEnd-Protocols;
1501}
1502
1503
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001504/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1505/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001506QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1507 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001508 // Sort the protocol list alphabetically to canonicalize it.
1509 SortAndUniqueProtocols(Protocols, NumProtocols);
1510
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001511 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001512 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001513
1514 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001515 if (ObjCQualifiedInterfaceType *QT =
1516 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001517 return QualType(QT, 0);
1518
1519 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001520 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001521 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001522
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001523 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001524 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001525 return QualType(QType, 0);
1526}
1527
Chris Lattnere1352302008-04-07 04:56:42 +00001528/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1529/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001530QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001531 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001532 // Sort the protocol list alphabetically to canonicalize it.
1533 SortAndUniqueProtocols(Protocols, NumProtocols);
1534
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001535 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001536 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001537
1538 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001539 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001540 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001541 return QualType(QT, 0);
1542
1543 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001544 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001545 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001546 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001547 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001548 return QualType(QType, 0);
1549}
1550
Douglas Gregor4fa58902009-02-26 23:50:07 +00001551/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1552/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001553/// multiple declarations that refer to "typeof(x)" all contain different
1554/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1555/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001556QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001557 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001558 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001559 Types.push_back(toe);
1560 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001561}
1562
Steve Naroff0604dd92007-08-01 18:02:17 +00001563/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1564/// TypeOfType AST's. The only motivation to unique these nodes would be
1565/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1566/// an issue. This doesn't effect the type checker, since it operates
1567/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001568QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001569 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001570 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001571 Types.push_back(tot);
1572 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001573}
1574
Chris Lattner4b009652007-07-25 00:24:17 +00001575/// getTagDeclType - Return the unique reference to the type for the
1576/// specified TagDecl (struct/union/class/enum) decl.
1577QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001578 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001579 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001580}
1581
1582/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1583/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1584/// needs to agree with the definition in <stddef.h>.
1585QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001586 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001587}
1588
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001589/// getSignedWCharType - Return the type of "signed wchar_t".
1590/// Used when in C++, as a GCC extension.
1591QualType ASTContext::getSignedWCharType() const {
1592 // FIXME: derive from "Target" ?
1593 return WCharTy;
1594}
1595
1596/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1597/// Used when in C++, as a GCC extension.
1598QualType ASTContext::getUnsignedWCharType() const {
1599 // FIXME: derive from "Target" ?
1600 return UnsignedIntTy;
1601}
1602
Chris Lattner4b009652007-07-25 00:24:17 +00001603/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1604/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1605QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001606 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001607}
1608
Chris Lattner19eb97e2008-04-02 05:18:44 +00001609//===----------------------------------------------------------------------===//
1610// Type Operators
1611//===----------------------------------------------------------------------===//
1612
Chris Lattner3dae6f42008-04-06 22:41:35 +00001613/// getCanonicalType - Return the canonical (structural) type corresponding to
1614/// the specified potentially non-canonical type. The non-canonical version
1615/// of a type may have many "decorated" versions of types. Decorators can
1616/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1617/// to be free of any of these, allowing two canonical types to be compared
1618/// for exact equality with a simple pointer comparison.
1619QualType ASTContext::getCanonicalType(QualType T) {
1620 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001621
1622 // If the result has type qualifiers, make sure to canonicalize them as well.
1623 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1624 if (TypeQuals == 0) return CanType;
1625
1626 // If the type qualifiers are on an array type, get the canonical type of the
1627 // array with the qualifiers applied to the element type.
1628 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1629 if (!AT)
1630 return CanType.getQualifiedType(TypeQuals);
1631
1632 // Get the canonical version of the element with the extra qualifiers on it.
1633 // This can recursively sink qualifiers through multiple levels of arrays.
1634 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1635 NewEltTy = getCanonicalType(NewEltTy);
1636
1637 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1638 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1639 CAT->getIndexTypeQualifier());
1640 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1641 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1642 IAT->getIndexTypeQualifier());
1643
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001644 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1645 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1646 DSAT->getSizeModifier(),
1647 DSAT->getIndexTypeQualifier());
1648
Chris Lattnera1923f62008-08-04 07:31:14 +00001649 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1650 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1651 VAT->getSizeModifier(),
1652 VAT->getIndexTypeQualifier());
1653}
1654
Douglas Gregord3022602009-03-27 23:10:48 +00001655NestedNameSpecifier *
1656ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1657 if (!NNS)
1658 return 0;
1659
1660 switch (NNS->getKind()) {
1661 case NestedNameSpecifier::Identifier:
1662 // Canonicalize the prefix but keep the identifier the same.
1663 return NestedNameSpecifier::Create(*this,
1664 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1665 NNS->getAsIdentifier());
1666
1667 case NestedNameSpecifier::Namespace:
1668 // A namespace is canonical; build a nested-name-specifier with
1669 // this namespace and no prefix.
1670 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1671
1672 case NestedNameSpecifier::TypeSpec:
1673 case NestedNameSpecifier::TypeSpecWithTemplate: {
1674 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1675 NestedNameSpecifier *Prefix = 0;
1676
1677 // FIXME: This isn't the right check!
1678 if (T->isDependentType())
1679 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1680
1681 return NestedNameSpecifier::Create(*this, Prefix,
1682 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1683 T.getTypePtr());
1684 }
1685
1686 case NestedNameSpecifier::Global:
1687 // The global specifier is canonical and unique.
1688 return NNS;
1689 }
1690
1691 // Required to silence a GCC warning
1692 return 0;
1693}
1694
Chris Lattnera1923f62008-08-04 07:31:14 +00001695
1696const ArrayType *ASTContext::getAsArrayType(QualType T) {
1697 // Handle the non-qualified case efficiently.
1698 if (T.getCVRQualifiers() == 0) {
1699 // Handle the common positive case fast.
1700 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1701 return AT;
1702 }
1703
1704 // Handle the common negative case fast, ignoring CVR qualifiers.
1705 QualType CType = T->getCanonicalTypeInternal();
1706
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001707 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001708 // test.
1709 if (!isa<ArrayType>(CType) &&
1710 !isa<ArrayType>(CType.getUnqualifiedType()))
1711 return 0;
1712
1713 // Apply any CVR qualifiers from the array type to the element type. This
1714 // implements C99 6.7.3p8: "If the specification of an array type includes
1715 // any type qualifiers, the element type is so qualified, not the array type."
1716
1717 // If we get here, we either have type qualifiers on the type, or we have
1718 // sugar such as a typedef in the way. If we have type qualifiers on the type
1719 // we must propagate them down into the elemeng type.
1720 unsigned CVRQuals = T.getCVRQualifiers();
1721 unsigned AddrSpace = 0;
1722 Type *Ty = T.getTypePtr();
1723
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001724 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001725 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001726 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1727 AddrSpace = EXTQT->getAddressSpace();
1728 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001729 } else {
1730 T = Ty->getDesugaredType();
1731 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1732 break;
1733 CVRQuals |= T.getCVRQualifiers();
1734 Ty = T.getTypePtr();
1735 }
1736 }
1737
1738 // If we have a simple case, just return now.
1739 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1740 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1741 return ATy;
1742
1743 // Otherwise, we have an array and we have qualifiers on it. Push the
1744 // qualifiers into the array element type and return a new array type.
1745 // Get the canonical version of the element with the extra qualifiers on it.
1746 // This can recursively sink qualifiers through multiple levels of arrays.
1747 QualType NewEltTy = ATy->getElementType();
1748 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001749 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001750 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1751
1752 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1753 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1754 CAT->getSizeModifier(),
1755 CAT->getIndexTypeQualifier()));
1756 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1757 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1758 IAT->getSizeModifier(),
1759 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001760
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001761 if (const DependentSizedArrayType *DSAT
1762 = dyn_cast<DependentSizedArrayType>(ATy))
1763 return cast<ArrayType>(
1764 getDependentSizedArrayType(NewEltTy,
1765 DSAT->getSizeExpr(),
1766 DSAT->getSizeModifier(),
1767 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001768
Chris Lattnera1923f62008-08-04 07:31:14 +00001769 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1770 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1771 VAT->getSizeModifier(),
1772 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001773}
1774
1775
Chris Lattner19eb97e2008-04-02 05:18:44 +00001776/// getArrayDecayedType - Return the properly qualified result of decaying the
1777/// specified array type to a pointer. This operation is non-trivial when
1778/// handling typedefs etc. The canonical type of "T" must be an array type,
1779/// this returns a pointer to a properly qualified element of the array.
1780///
1781/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1782QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001783 // Get the element type with 'getAsArrayType' so that we don't lose any
1784 // typedefs in the element type of the array. This also handles propagation
1785 // of type qualifiers from the array type into the element type if present
1786 // (C99 6.7.3p8).
1787 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1788 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001789
Chris Lattnera1923f62008-08-04 07:31:14 +00001790 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001791
1792 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001793 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001794}
1795
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001796QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001797 QualType ElemTy = VAT->getElementType();
1798
1799 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1800 return getBaseElementType(VAT);
1801
1802 return ElemTy;
1803}
1804
Chris Lattner4b009652007-07-25 00:24:17 +00001805/// getFloatingRank - Return a relative rank for floating point types.
1806/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001807static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001808 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001809 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001810
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001811 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001812 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001813 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001814 case BuiltinType::Float: return FloatRank;
1815 case BuiltinType::Double: return DoubleRank;
1816 case BuiltinType::LongDouble: return LongDoubleRank;
1817 }
1818}
1819
Steve Narofffa0c4532007-08-27 01:41:48 +00001820/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1821/// point or a complex type (based on typeDomain/typeSize).
1822/// 'typeDomain' is a real floating point or complex type.
1823/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001824QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1825 QualType Domain) const {
1826 FloatingRank EltRank = getFloatingRank(Size);
1827 if (Domain->isComplexType()) {
1828 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001829 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001830 case FloatRank: return FloatComplexTy;
1831 case DoubleRank: return DoubleComplexTy;
1832 case LongDoubleRank: return LongDoubleComplexTy;
1833 }
Chris Lattner4b009652007-07-25 00:24:17 +00001834 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001835
1836 assert(Domain->isRealFloatingType() && "Unknown domain!");
1837 switch (EltRank) {
1838 default: assert(0 && "getFloatingRank(): illegal value for rank");
1839 case FloatRank: return FloatTy;
1840 case DoubleRank: return DoubleTy;
1841 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001842 }
Chris Lattner4b009652007-07-25 00:24:17 +00001843}
1844
Chris Lattner51285d82008-04-06 23:55:33 +00001845/// getFloatingTypeOrder - Compare the rank of the two specified floating
1846/// point types, ignoring the domain of the type (i.e. 'double' ==
1847/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1848/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001849int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1850 FloatingRank LHSR = getFloatingRank(LHS);
1851 FloatingRank RHSR = getFloatingRank(RHS);
1852
1853 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001854 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001855 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001856 return 1;
1857 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001858}
1859
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001860/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1861/// routine will assert if passed a built-in type that isn't an integer or enum,
1862/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001863unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001864 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001865 if (EnumType* ET = dyn_cast<EnumType>(T))
1866 T = ET->getDecl()->getIntegerType().getTypePtr();
1867
1868 // There are two things which impact the integer rank: the width, and
1869 // the ordering of builtins. The builtin ordering is encoded in the
1870 // bottom three bits; the width is encoded in the bits above that.
1871 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1872 return FWIT->getWidth() << 3;
1873 }
1874
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001875 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001876 default: assert(0 && "getIntegerRank(): not a built-in integer");
1877 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001878 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001879 case BuiltinType::Char_S:
1880 case BuiltinType::Char_U:
1881 case BuiltinType::SChar:
1882 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001883 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001884 case BuiltinType::Short:
1885 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001886 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001887 case BuiltinType::Int:
1888 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001889 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001890 case BuiltinType::Long:
1891 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001892 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001893 case BuiltinType::LongLong:
1894 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001895 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001896 }
1897}
1898
Chris Lattner51285d82008-04-06 23:55:33 +00001899/// getIntegerTypeOrder - Returns the highest ranked integer type:
1900/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1901/// LHS < RHS, return -1.
1902int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001903 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1904 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001905 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001906
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001907 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1908 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001909
Chris Lattner51285d82008-04-06 23:55:33 +00001910 unsigned LHSRank = getIntegerRank(LHSC);
1911 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001912
Chris Lattner51285d82008-04-06 23:55:33 +00001913 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1914 if (LHSRank == RHSRank) return 0;
1915 return LHSRank > RHSRank ? 1 : -1;
1916 }
Chris Lattner4b009652007-07-25 00:24:17 +00001917
Chris Lattner51285d82008-04-06 23:55:33 +00001918 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1919 if (LHSUnsigned) {
1920 // If the unsigned [LHS] type is larger, return it.
1921 if (LHSRank >= RHSRank)
1922 return 1;
1923
1924 // If the signed type can represent all values of the unsigned type, it
1925 // wins. Because we are dealing with 2's complement and types that are
1926 // powers of two larger than each other, this is always safe.
1927 return -1;
1928 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001929
Chris Lattner51285d82008-04-06 23:55:33 +00001930 // If the unsigned [RHS] type is larger, return it.
1931 if (RHSRank >= LHSRank)
1932 return -1;
1933
1934 // If the signed type can represent all values of the unsigned type, it
1935 // wins. Because we are dealing with 2's complement and types that are
1936 // powers of two larger than each other, this is always safe.
1937 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001938}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001939
1940// getCFConstantStringType - Return the type used for constant CFStrings.
1941QualType ASTContext::getCFConstantStringType() {
1942 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001943 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001944 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001945 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001946 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001947
1948 // const int *isa;
1949 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001950 // int flags;
1951 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001952 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001953 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001954 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001955 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001956
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001957 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001958 for (unsigned i = 0; i < 4; ++i) {
1959 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1960 SourceLocation(), 0,
1961 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001962 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001963 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001964 }
1965
1966 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001967 }
1968
1969 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001970}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001971
Anders Carlssonf58cac72008-08-30 19:34:46 +00001972QualType ASTContext::getObjCFastEnumerationStateType()
1973{
1974 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001975 ObjCFastEnumerationStateTypeDecl =
1976 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1977 &Idents.get("__objcFastEnumerationState"));
1978
Anders Carlssonf58cac72008-08-30 19:34:46 +00001979 QualType FieldTypes[] = {
1980 UnsignedLongTy,
1981 getPointerType(ObjCIdType),
1982 getPointerType(UnsignedLongTy),
1983 getConstantArrayType(UnsignedLongTy,
1984 llvm::APInt(32, 5), ArrayType::Normal, 0)
1985 };
1986
Douglas Gregor8acb7272008-12-11 16:49:14 +00001987 for (size_t i = 0; i < 4; ++i) {
1988 FieldDecl *Field = FieldDecl::Create(*this,
1989 ObjCFastEnumerationStateTypeDecl,
1990 SourceLocation(), 0,
1991 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001992 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001993 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001994 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001995
Douglas Gregor8acb7272008-12-11 16:49:14 +00001996 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001997 }
1998
1999 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2000}
2001
Anders Carlssone3f02572007-10-29 06:33:42 +00002002// This returns true if a type has been typedefed to BOOL:
2003// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00002004static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002005 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00002006 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2007 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002008
2009 return false;
2010}
2011
Ted Kremenek42730c52008-01-07 19:49:32 +00002012/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002013/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00002014int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00002015 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002016
2017 // Make all integer and enum types at least as large as an int
2018 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002019 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002020 // Treat arrays as pointers, since that's how they're passed in.
2021 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002022 sz = getTypeSize(VoidPtrTy);
2023 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002024}
2025
Ted Kremenek42730c52008-01-07 19:49:32 +00002026/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002027/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002028void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00002029 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002030 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002031 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00002032 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002033 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002034 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002035 // Compute size of all parameters.
2036 // Start with computing size of a pointer in number of bytes.
2037 // FIXME: There might(should) be a better way of doing this computation!
2038 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002039 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002040 // The first two arguments (self and _cmd) are pointers; account for
2041 // their size.
2042 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002043 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2044 E = Decl->param_end(); PI != E; ++PI) {
2045 QualType PType = (*PI)->getType();
2046 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002047 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002048 ParmOffset += sz;
2049 }
2050 S += llvm::utostr(ParmOffset);
2051 S += "@0:";
2052 S += llvm::utostr(PtrSize);
2053
2054 // Argument types.
2055 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002056 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2057 E = Decl->param_end(); PI != E; ++PI) {
2058 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002059 QualType PType = PVDecl->getOriginalType();
2060 if (const ArrayType *AT =
2061 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
2062 // Use array's original type only if it has known number of
2063 // elements.
2064 if (!dyn_cast<ConstantArrayType>(AT))
2065 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002066 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002067 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002068 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002069 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002070 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002071 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002072 }
2073}
2074
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002075/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002076/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002077/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2078/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002079/// Property attributes are stored as a comma-delimited C string. The simple
2080/// attributes readonly and bycopy are encoded as single characters. The
2081/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2082/// encoded as single characters, followed by an identifier. Property types
2083/// are also encoded as a parametrized attribute. The characters used to encode
2084/// these attributes are defined by the following enumeration:
2085/// @code
2086/// enum PropertyAttributes {
2087/// kPropertyReadOnly = 'R', // property is read-only.
2088/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2089/// kPropertyByref = '&', // property is a reference to the value last assigned
2090/// kPropertyDynamic = 'D', // property is dynamic
2091/// kPropertyGetter = 'G', // followed by getter selector name
2092/// kPropertySetter = 'S', // followed by setter selector name
2093/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2094/// kPropertyType = 't' // followed by old-style type encoding.
2095/// kPropertyWeak = 'W' // 'weak' property
2096/// kPropertyStrong = 'P' // property GC'able
2097/// kPropertyNonAtomic = 'N' // property non-atomic
2098/// };
2099/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002100void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2101 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002102 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002103 // Collect information from the property implementation decl(s).
2104 bool Dynamic = false;
2105 ObjCPropertyImplDecl *SynthesizePID = 0;
2106
2107 // FIXME: Duplicated code due to poor abstraction.
2108 if (Container) {
2109 if (const ObjCCategoryImplDecl *CID =
2110 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2111 for (ObjCCategoryImplDecl::propimpl_iterator
2112 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2113 ObjCPropertyImplDecl *PID = *i;
2114 if (PID->getPropertyDecl() == PD) {
2115 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2116 Dynamic = true;
2117 } else {
2118 SynthesizePID = PID;
2119 }
2120 }
2121 }
2122 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002123 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002124 for (ObjCCategoryImplDecl::propimpl_iterator
2125 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2126 ObjCPropertyImplDecl *PID = *i;
2127 if (PID->getPropertyDecl() == PD) {
2128 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2129 Dynamic = true;
2130 } else {
2131 SynthesizePID = PID;
2132 }
2133 }
2134 }
2135 }
2136 }
2137
2138 // FIXME: This is not very efficient.
2139 S = "T";
2140
2141 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002142 // GCC has some special rules regarding encoding of properties which
2143 // closely resembles encoding of ivars.
2144 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2145 true /* outermost type */,
2146 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002147
2148 if (PD->isReadOnly()) {
2149 S += ",R";
2150 } else {
2151 switch (PD->getSetterKind()) {
2152 case ObjCPropertyDecl::Assign: break;
2153 case ObjCPropertyDecl::Copy: S += ",C"; break;
2154 case ObjCPropertyDecl::Retain: S += ",&"; break;
2155 }
2156 }
2157
2158 // It really isn't clear at all what this means, since properties
2159 // are "dynamic by default".
2160 if (Dynamic)
2161 S += ",D";
2162
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002163 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2164 S += ",N";
2165
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002166 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2167 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002168 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002169 }
2170
2171 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2172 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002173 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002174 }
2175
2176 if (SynthesizePID) {
2177 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2178 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002179 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002180 }
2181
2182 // FIXME: OBJCGC: weak & strong
2183}
2184
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002185/// getLegacyIntegralTypeEncoding -
2186/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002187/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002188/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2189///
2190void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2191 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2192 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002193 if (BT->getKind() == BuiltinType::ULong &&
2194 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002195 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002196 else
2197 if (BT->getKind() == BuiltinType::Long &&
2198 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002199 PointeeTy = IntTy;
2200 }
2201 }
2202}
2203
Fariborz Jahanian248db262008-01-22 22:44:46 +00002204void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002205 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002206 // We follow the behavior of gcc, expanding structures which are
2207 // directly pointed to, and expanding embedded structures. Note that
2208 // these rules are sufficient to prevent recursive encoding of the
2209 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002210 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2211 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002212}
2213
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002214static void EncodeBitField(const ASTContext *Context, std::string& S,
2215 FieldDecl *FD) {
2216 const Expr *E = FD->getBitWidth();
2217 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2218 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2219 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2220 S += 'b';
2221 S += llvm::utostr(N);
2222}
2223
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002224void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2225 bool ExpandPointedToStructures,
2226 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002227 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002228 bool OutermostType,
2229 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00002230 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002231 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002232 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002233 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002234 else {
2235 char encoding;
2236 switch (BT->getKind()) {
2237 default: assert(0 && "Unhandled builtin type kind");
2238 case BuiltinType::Void: encoding = 'v'; break;
2239 case BuiltinType::Bool: encoding = 'B'; break;
2240 case BuiltinType::Char_U:
2241 case BuiltinType::UChar: encoding = 'C'; break;
2242 case BuiltinType::UShort: encoding = 'S'; break;
2243 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002244 case BuiltinType::ULong:
2245 encoding =
2246 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2247 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002248 case BuiltinType::ULongLong: encoding = 'Q'; break;
2249 case BuiltinType::Char_S:
2250 case BuiltinType::SChar: encoding = 'c'; break;
2251 case BuiltinType::Short: encoding = 's'; break;
2252 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002253 case BuiltinType::Long:
2254 encoding =
2255 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2256 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002257 case BuiltinType::LongLong: encoding = 'q'; break;
2258 case BuiltinType::Float: encoding = 'f'; break;
2259 case BuiltinType::Double: encoding = 'd'; break;
2260 case BuiltinType::LongDouble: encoding = 'd'; break;
2261 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002262
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002263 S += encoding;
2264 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002265 }
Ted Kremenek42730c52008-01-07 19:49:32 +00002266 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002267 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2268 ExpandPointedToStructures,
2269 ExpandStructures, FD);
2270 if (FD || EncodingProperty) {
2271 // Note that we do extended encoding of protocol qualifer list
2272 // Only when doing ivar or property encoding.
2273 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2274 S += '"';
2275 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2276 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2277 S += '<';
2278 S += Proto->getNameAsString();
2279 S += '>';
2280 }
2281 S += '"';
2282 }
2283 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002284 }
2285 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002286 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002287 bool isReadOnly = false;
2288 // For historical/compatibility reasons, the read-only qualifier of the
2289 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2290 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2291 // Also, do not emit the 'r' for anything but the outermost type!
2292 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2293 if (OutermostType && T.isConstQualified()) {
2294 isReadOnly = true;
2295 S += 'r';
2296 }
2297 }
2298 else if (OutermostType) {
2299 QualType P = PointeeTy;
2300 while (P->getAsPointerType())
2301 P = P->getAsPointerType()->getPointeeType();
2302 if (P.isConstQualified()) {
2303 isReadOnly = true;
2304 S += 'r';
2305 }
2306 }
2307 if (isReadOnly) {
2308 // Another legacy compatibility encoding. Some ObjC qualifier and type
2309 // combinations need to be rearranged.
2310 // Rewrite "in const" from "nr" to "rn"
2311 const char * s = S.c_str();
2312 int len = S.length();
2313 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2314 std::string replace = "rn";
2315 S.replace(S.end()-2, S.end(), replace);
2316 }
2317 }
Steve Naroff17c03822009-02-12 17:52:19 +00002318 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002319 S += '@';
2320 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002321 }
2322 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002323 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002324 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002325 // Another historical/compatibility reason.
2326 // We encode the underlying type which comes out as
2327 // {...};
2328 S += '^';
2329 getObjCEncodingForTypeImpl(PointeeTy, S,
2330 false, ExpandPointedToStructures,
2331 NULL);
2332 return;
2333 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002334 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002335 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002336 const ObjCInterfaceType *OIT =
2337 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002338 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002339 S += '"';
2340 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002341 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2342 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2343 S += '<';
2344 S += Proto->getNameAsString();
2345 S += '>';
2346 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002347 S += '"';
2348 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002349 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002350 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002351 S += '#';
2352 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002353 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002354 S += ':';
2355 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002356 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002357
2358 if (PointeeTy->isCharType()) {
2359 // char pointer types should be encoded as '*' unless it is a
2360 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002361 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002362 S += '*';
2363 return;
2364 }
2365 }
2366
2367 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002368 getLegacyIntegralTypeEncoding(PointeeTy);
2369
2370 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002371 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002372 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002373 } else if (const ArrayType *AT =
2374 // Ignore type qualifiers etc.
2375 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002376 if (isa<IncompleteArrayType>(AT)) {
2377 // Incomplete arrays are encoded as a pointer to the array element.
2378 S += '^';
2379
2380 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2381 false, ExpandStructures, FD);
2382 } else {
2383 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002384
Anders Carlsson858c64d2009-02-22 01:38:57 +00002385 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2386 S += llvm::utostr(CAT->getSize().getZExtValue());
2387 else {
2388 //Variable length arrays are encoded as a regular array with 0 elements.
2389 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2390 S += '0';
2391 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002392
Anders Carlsson858c64d2009-02-22 01:38:57 +00002393 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2394 false, ExpandStructures, FD);
2395 S += ']';
2396 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002397 } else if (T->getAsFunctionType()) {
2398 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002399 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002400 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002401 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002402 // Anonymous structures print as '?'
2403 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2404 S += II->getName();
2405 } else {
2406 S += '?';
2407 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002408 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002409 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002410 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2411 FieldEnd = RDecl->field_end();
2412 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002413 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002414 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002415 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002416 S += '"';
2417 }
2418
2419 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002420 if (Field->isBitField()) {
2421 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2422 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002423 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002424 QualType qt = Field->getType();
2425 getLegacyIntegralTypeEncoding(qt);
2426 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002427 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002428 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002429 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002430 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002431 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002432 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002433 if (FD && FD->isBitField())
2434 EncodeBitField(this, S, FD);
2435 else
2436 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002437 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002438 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002439 } else if (T->isObjCInterfaceType()) {
2440 // @encode(class_name)
2441 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2442 S += '{';
2443 const IdentifierInfo *II = OI->getIdentifier();
2444 S += II->getName();
2445 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002446 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002447 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002448 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002449 if (RecFields[i]->isBitField())
2450 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2451 RecFields[i]);
2452 else
2453 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2454 FD);
2455 }
2456 S += '}';
2457 }
2458 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002459 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002460}
2461
Ted Kremenek42730c52008-01-07 19:49:32 +00002462void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002463 std::string& S) const {
2464 if (QT & Decl::OBJC_TQ_In)
2465 S += 'n';
2466 if (QT & Decl::OBJC_TQ_Inout)
2467 S += 'N';
2468 if (QT & Decl::OBJC_TQ_Out)
2469 S += 'o';
2470 if (QT & Decl::OBJC_TQ_Bycopy)
2471 S += 'O';
2472 if (QT & Decl::OBJC_TQ_Byref)
2473 S += 'R';
2474 if (QT & Decl::OBJC_TQ_Oneway)
2475 S += 'V';
2476}
2477
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002478void ASTContext::setBuiltinVaListType(QualType T)
2479{
2480 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2481
2482 BuiltinVaListType = T;
2483}
2484
Ted Kremenek42730c52008-01-07 19:49:32 +00002485void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002486{
Ted Kremenek42730c52008-01-07 19:49:32 +00002487 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002488
2489 // typedef struct objc_object *id;
2490 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002491 // User error - caller will issue diagnostics.
2492 if (!ptr)
2493 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002494 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002495 // User error - caller will issue diagnostics.
2496 if (!rec)
2497 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002498 IdStructType = rec;
2499}
2500
Ted Kremenek42730c52008-01-07 19:49:32 +00002501void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002502{
Ted Kremenek42730c52008-01-07 19:49:32 +00002503 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002504
2505 // typedef struct objc_selector *SEL;
2506 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002507 if (!ptr)
2508 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002509 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002510 if (!rec)
2511 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002512 SelStructType = rec;
2513}
2514
Ted Kremenek42730c52008-01-07 19:49:32 +00002515void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002516{
Ted Kremenek42730c52008-01-07 19:49:32 +00002517 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002518}
2519
Ted Kremenek42730c52008-01-07 19:49:32 +00002520void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002521{
Ted Kremenek42730c52008-01-07 19:49:32 +00002522 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002523
2524 // typedef struct objc_class *Class;
2525 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2526 assert(ptr && "'Class' incorrectly typed");
2527 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2528 assert(rec && "'Class' incorrectly typed");
2529 ClassStructType = rec;
2530}
2531
Ted Kremenek42730c52008-01-07 19:49:32 +00002532void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2533 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002534 "'NSConstantString' type already set!");
2535
Ted Kremenek42730c52008-01-07 19:49:32 +00002536 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002537}
2538
Douglas Gregordd13e842009-03-30 22:58:21 +00002539/// \brief Retrieve the template name that represents a qualified
2540/// template name such as \c std::vector.
2541TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2542 bool TemplateKeyword,
2543 TemplateDecl *Template) {
2544 llvm::FoldingSetNodeID ID;
2545 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2546
2547 void *InsertPos = 0;
2548 QualifiedTemplateName *QTN =
2549 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2550 if (!QTN) {
2551 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2552 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2553 }
2554
2555 return TemplateName(QTN);
2556}
2557
2558/// \brief Retrieve the template name that represents a dependent
2559/// template name such as \c MetaFun::template apply.
2560TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2561 const IdentifierInfo *Name) {
2562 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2563
2564 llvm::FoldingSetNodeID ID;
2565 DependentTemplateName::Profile(ID, NNS, Name);
2566
2567 void *InsertPos = 0;
2568 DependentTemplateName *QTN =
2569 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2570
2571 if (QTN)
2572 return TemplateName(QTN);
2573
2574 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2575 if (CanonNNS == NNS) {
2576 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2577 } else {
2578 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2579 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2580 }
2581
2582 DependentTemplateNames.InsertNode(QTN, InsertPos);
2583 return TemplateName(QTN);
2584}
2585
Douglas Gregorc6507e42008-11-03 14:12:49 +00002586/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002587/// TargetInfo, produce the corresponding type. The unsigned @p Type
2588/// is actually a value of type @c TargetInfo::IntType.
2589QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002590 switch (Type) {
2591 case TargetInfo::NoInt: return QualType();
2592 case TargetInfo::SignedShort: return ShortTy;
2593 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2594 case TargetInfo::SignedInt: return IntTy;
2595 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2596 case TargetInfo::SignedLong: return LongTy;
2597 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2598 case TargetInfo::SignedLongLong: return LongLongTy;
2599 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2600 }
2601
2602 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002603 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002604}
Ted Kremenek118930e2008-07-24 23:58:27 +00002605
2606//===----------------------------------------------------------------------===//
2607// Type Predicates.
2608//===----------------------------------------------------------------------===//
2609
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002610/// isObjCNSObjectType - Return true if this is an NSObject object using
2611/// NSObject attribute on a c-style pointer type.
2612/// FIXME - Make it work directly on types.
2613///
2614bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2615 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2616 if (TypedefDecl *TD = TDT->getDecl())
2617 if (TD->getAttr<ObjCNSObjectAttr>())
2618 return true;
2619 }
2620 return false;
2621}
2622
Ted Kremenek118930e2008-07-24 23:58:27 +00002623/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2624/// to an object type. This includes "id" and "Class" (two 'special' pointers
2625/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2626/// ID type).
2627bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002628 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002629 return true;
2630
Steve Naroffd9e00802008-10-21 18:24:04 +00002631 // Blocks are objects.
2632 if (Ty->isBlockPointerType())
2633 return true;
2634
2635 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002636 if (!Ty->isPointerType())
2637 return false;
2638
2639 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2640 // pointer types. This looks for the typedef specifically, not for the
2641 // underlying type.
Eli Friedman9c2b33f2009-03-22 23:00:19 +00002642 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2643 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002644 return true;
2645
2646 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002647 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2648 return true;
2649
2650 // If is has NSObject attribute, OK as well.
2651 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002652}
2653
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002654/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2655/// garbage collection attribute.
2656///
2657QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002658 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002659 if (getLangOptions().ObjC1 &&
2660 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002661 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002662 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002663 // (or pointers to them) be treated as though they were declared
2664 // as __strong.
2665 if (GCAttrs == QualType::GCNone) {
2666 if (isObjCObjectPointerType(Ty))
2667 GCAttrs = QualType::Strong;
2668 else if (Ty->isPointerType())
2669 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2670 }
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002671 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002672 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002673}
2674
Chris Lattner6ff358b2008-04-07 06:51:04 +00002675//===----------------------------------------------------------------------===//
2676// Type Compatibility Testing
2677//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002678
Steve Naroff3454b6c2008-09-04 15:10:53 +00002679/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002680/// block types. Types must be strictly compatible here. For example,
2681/// C unfortunately doesn't produce an error for the following:
2682///
2683/// int (*emptyArgFunc)();
2684/// int (*intArgList)(int) = emptyArgFunc;
2685///
2686/// For blocks, we will produce an error for the following (similar to C++):
2687///
2688/// int (^emptyArgBlock)();
2689/// int (^intArgBlock)(int) = emptyArgBlock;
2690///
2691/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2692///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002693bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002694 const FunctionType *lbase = lhs->getAsFunctionType();
2695 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002696 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2697 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stump896ceed2009-04-01 01:17:39 +00002698 if (lproto && rproto == 0)
2699 return false;
2700 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff3454b6c2008-09-04 15:10:53 +00002701}
2702
Chris Lattner6ff358b2008-04-07 06:51:04 +00002703/// areCompatVectorTypes - Return true if the two specified vector types are
2704/// compatible.
2705static bool areCompatVectorTypes(const VectorType *LHS,
2706 const VectorType *RHS) {
2707 assert(LHS->isCanonical() && RHS->isCanonical());
2708 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002709 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002710}
2711
Eli Friedman0d9549b2008-08-22 00:56:42 +00002712/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002713/// compatible for assignment from RHS to LHS. This handles validation of any
2714/// protocol qualifiers on the LHS or RHS.
2715///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002716bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2717 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002718 // Verify that the base decls are compatible: the RHS must be a subclass of
2719 // the LHS.
2720 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2721 return false;
2722
2723 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2724 // protocol qualified at all, then we are good.
2725 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2726 return true;
2727
2728 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2729 // isn't a superset.
2730 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2731 return true; // FIXME: should return false!
2732
2733 // Finally, we must have two protocol-qualified interfaces.
2734 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2735 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002736
Steve Naroff98e71b82009-03-01 16:12:44 +00002737 // All LHS protocols must have a presence on the RHS.
2738 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002739
Steve Naroff98e71b82009-03-01 16:12:44 +00002740 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2741 LHSPE = LHSP->qual_end();
2742 LHSPI != LHSPE; LHSPI++) {
2743 bool RHSImplementsProtocol = false;
2744
2745 // If the RHS doesn't implement the protocol on the left, the types
2746 // are incompatible.
2747 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2748 RHSPE = RHSP->qual_end();
2749 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2750 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2751 RHSImplementsProtocol = true;
2752 }
2753 // FIXME: For better diagnostics, consider passing back the protocol name.
2754 if (!RHSImplementsProtocol)
2755 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002756 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002757 // The RHS implements all protocols listed on the LHS.
2758 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002759}
2760
Steve Naroff17c03822009-02-12 17:52:19 +00002761bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2762 // get the "pointed to" types
2763 const PointerType *LHSPT = LHS->getAsPointerType();
2764 const PointerType *RHSPT = RHS->getAsPointerType();
2765
2766 if (!LHSPT || !RHSPT)
2767 return false;
2768
2769 QualType lhptee = LHSPT->getPointeeType();
2770 QualType rhptee = RHSPT->getPointeeType();
2771 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2772 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2773 // ID acts sort of like void* for ObjC interfaces
2774 if (LHSIface && isObjCIdStructType(rhptee))
2775 return true;
2776 if (RHSIface && isObjCIdStructType(lhptee))
2777 return true;
2778 if (!LHSIface || !RHSIface)
2779 return false;
2780 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2781 canAssignObjCInterfaces(RHSIface, LHSIface);
2782}
2783
Steve Naroff85f0dc52007-10-15 20:41:53 +00002784/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2785/// both shall have the identically qualified version of a compatible type.
2786/// C99 6.2.7p1: Two types have compatible types if their types are the
2787/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002788bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2789 return !mergeTypes(LHS, RHS).isNull();
2790}
2791
2792QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2793 const FunctionType *lbase = lhs->getAsFunctionType();
2794 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002795 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2796 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002797 bool allLTypes = true;
2798 bool allRTypes = true;
2799
2800 // Check return type
2801 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2802 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002803 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2804 allLTypes = false;
2805 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2806 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002807
2808 if (lproto && rproto) { // two C99 style function prototypes
2809 unsigned lproto_nargs = lproto->getNumArgs();
2810 unsigned rproto_nargs = rproto->getNumArgs();
2811
2812 // Compatible functions must have the same number of arguments
2813 if (lproto_nargs != rproto_nargs)
2814 return QualType();
2815
2816 // Variadic and non-variadic functions aren't compatible
2817 if (lproto->isVariadic() != rproto->isVariadic())
2818 return QualType();
2819
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002820 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2821 return QualType();
2822
Eli Friedman0d9549b2008-08-22 00:56:42 +00002823 // Check argument compatibility
2824 llvm::SmallVector<QualType, 10> types;
2825 for (unsigned i = 0; i < lproto_nargs; i++) {
2826 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2827 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2828 QualType argtype = mergeTypes(largtype, rargtype);
2829 if (argtype.isNull()) return QualType();
2830 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002831 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2832 allLTypes = false;
2833 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2834 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002835 }
2836 if (allLTypes) return lhs;
2837 if (allRTypes) return rhs;
2838 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002839 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002840 }
2841
2842 if (lproto) allRTypes = false;
2843 if (rproto) allLTypes = false;
2844
Douglas Gregor4fa58902009-02-26 23:50:07 +00002845 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002846 if (proto) {
2847 if (proto->isVariadic()) return QualType();
2848 // Check that the types are compatible with the types that
2849 // would result from default argument promotions (C99 6.7.5.3p15).
2850 // The only types actually affected are promotable integer
2851 // types and floats, which would be passed as a different
2852 // type depending on whether the prototype is visible.
2853 unsigned proto_nargs = proto->getNumArgs();
2854 for (unsigned i = 0; i < proto_nargs; ++i) {
2855 QualType argTy = proto->getArgType(i);
2856 if (argTy->isPromotableIntegerType() ||
2857 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2858 return QualType();
2859 }
2860
2861 if (allLTypes) return lhs;
2862 if (allRTypes) return rhs;
2863 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002864 proto->getNumArgs(), lproto->isVariadic(),
2865 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002866 }
2867
2868 if (allLTypes) return lhs;
2869 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002870 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002871}
2872
2873QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002874 // C++ [expr]: If an expression initially has the type "reference to T", the
2875 // type is adjusted to "T" prior to any further analysis, the expression
2876 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002877 // expression is an lvalue unless the reference is an rvalue reference and
2878 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002879 // FIXME: C++ shouldn't be going through here! The rules are different
2880 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00002881 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2882 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002883 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002884 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002885 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002886 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002887
Eli Friedman0d9549b2008-08-22 00:56:42 +00002888 QualType LHSCan = getCanonicalType(LHS),
2889 RHSCan = getCanonicalType(RHS);
2890
2891 // If two types are identical, they are compatible.
2892 if (LHSCan == RHSCan)
2893 return LHS;
2894
2895 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002896 // Note that we handle extended qualifiers later, in the
2897 // case for ExtQualType.
2898 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002899 return QualType();
2900
2901 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2902 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2903
Chris Lattnerc38d4522008-01-14 05:45:46 +00002904 // We want to consider the two function types to be the same for these
2905 // comparisons, just force one to the other.
2906 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2907 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002908
2909 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002910 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2911 LHSClass = Type::ConstantArray;
2912 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2913 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002914
Nate Begemanaf6ed502008-04-18 23:10:10 +00002915 // Canonicalize ExtVector -> Vector.
2916 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2917 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002918
Chris Lattner7cdcb252008-04-07 06:38:24 +00002919 // Consider qualified interfaces and interfaces the same.
2920 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2921 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002922
Chris Lattnerb5709e22008-04-07 05:43:21 +00002923 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002924 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002925 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2926 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2927
2928 // ID acts sort of like void* for ObjC interfaces
2929 if (LHSIface && isObjCIdStructType(RHS))
2930 return LHS;
2931 if (RHSIface && isObjCIdStructType(LHS))
2932 return RHS;
2933
Steve Naroff28ceff72008-12-10 22:14:21 +00002934 // ID is compatible with all qualified id types.
2935 if (LHS->isObjCQualifiedIdType()) {
2936 if (const PointerType *PT = RHS->getAsPointerType()) {
2937 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002938 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002939 return LHS;
2940 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2941 // Unfortunately, this API is part of Sema (which we don't have access
2942 // to. Need to refactor. The following check is insufficient, since we
2943 // need to make sure the class implements the protocol.
2944 if (pType->isObjCInterfaceType())
2945 return LHS;
2946 }
2947 }
2948 if (RHS->isObjCQualifiedIdType()) {
2949 if (const PointerType *PT = LHS->getAsPointerType()) {
2950 QualType pType = PT->getPointeeType();
Steve Naroff17c03822009-02-12 17:52:19 +00002951 if (isObjCIdStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002952 return RHS;
2953 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2954 // Unfortunately, this API is part of Sema (which we don't have access
2955 // to. Need to refactor. The following check is insufficient, since we
2956 // need to make sure the class implements the protocol.
2957 if (pType->isObjCInterfaceType())
2958 return RHS;
2959 }
2960 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002961 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2962 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002963 if (const EnumType* ETy = LHS->getAsEnumType()) {
2964 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2965 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002966 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002967 if (const EnumType* ETy = RHS->getAsEnumType()) {
2968 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2969 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002970 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002971
Eli Friedman0d9549b2008-08-22 00:56:42 +00002972 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002973 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002974
Steve Naroffc88babe2008-01-09 22:43:08 +00002975 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002976 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00002977#define TYPE(Class, Base)
2978#define ABSTRACT_TYPE(Class, Base)
2979#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2980#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2981#include "clang/AST/TypeNodes.def"
2982 assert(false && "Non-canonical and dependent types shouldn't get here");
2983 return QualType();
2984
Sebastian Redlce6fff02009-03-16 23:22:08 +00002985 case Type::LValueReference:
2986 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00002987 case Type::MemberPointer:
2988 assert(false && "C++ should never be in mergeTypes");
2989 return QualType();
2990
2991 case Type::IncompleteArray:
2992 case Type::VariableArray:
2993 case Type::FunctionProto:
2994 case Type::ExtVector:
2995 case Type::ObjCQualifiedInterface:
2996 assert(false && "Types are eliminated above");
2997 return QualType();
2998
Chris Lattnerc38d4522008-01-14 05:45:46 +00002999 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003000 {
3001 // Merge two pointer types, while trying to preserve typedef info
3002 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3003 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3004 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3005 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003006 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3007 return LHS;
3008 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3009 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003010 return getPointerType(ResultType);
3011 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003012 case Type::BlockPointer:
3013 {
3014 // Merge two block pointer types, while trying to preserve typedef info
3015 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3016 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3017 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3018 if (ResultType.isNull()) return QualType();
3019 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3020 return LHS;
3021 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3022 return RHS;
3023 return getBlockPointerType(ResultType);
3024 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003025 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003026 {
3027 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3028 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3029 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3030 return QualType();
3031
3032 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3033 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3034 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3035 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003036 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3037 return LHS;
3038 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3039 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003040 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3041 ArrayType::ArraySizeModifier(), 0);
3042 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3043 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003044 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3045 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003046 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3047 return LHS;
3048 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3049 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003050 if (LVAT) {
3051 // FIXME: This isn't correct! But tricky to implement because
3052 // the array's size has to be the size of LHS, but the type
3053 // has to be different.
3054 return LHS;
3055 }
3056 if (RVAT) {
3057 // FIXME: This isn't correct! But tricky to implement because
3058 // the array's size has to be the size of RHS, but the type
3059 // has to be different.
3060 return RHS;
3061 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003062 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3063 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003064 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003065 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003066 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003067 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003068 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003069 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003070 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003071 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3072 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003073 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003074 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003075 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003076 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003077 case Type::Complex:
3078 // Distinct complex types are incompatible.
3079 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003080 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003081 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003082 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3083 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003084 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003085 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003086 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003087 // FIXME: This should be type compatibility, e.g. whether
3088 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003089 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3090 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3091 if (LHSIface && RHSIface &&
3092 canAssignObjCInterfaces(LHSIface, RHSIface))
3093 return LHS;
3094
Eli Friedman0d9549b2008-08-22 00:56:42 +00003095 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003096 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003097 case Type::ObjCQualifiedId:
3098 // Distinct qualified id's are not compatible.
3099 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003100 case Type::FixedWidthInt:
3101 // Distinct fixed-width integers are not compatible.
3102 return QualType();
3103 case Type::ObjCQualifiedClass:
3104 // Distinct qualified classes are not compatible.
3105 return QualType();
3106 case Type::ExtQual:
3107 // FIXME: ExtQual types can be compatible even if they're not
3108 // identical!
3109 return QualType();
3110 // First attempt at an implementation, but I'm not really sure it's
3111 // right...
3112#if 0
3113 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3114 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3115 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3116 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3117 return QualType();
3118 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3119 LHSBase = QualType(LQual->getBaseType(), 0);
3120 RHSBase = QualType(RQual->getBaseType(), 0);
3121 ResultType = mergeTypes(LHSBase, RHSBase);
3122 if (ResultType.isNull()) return QualType();
3123 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3124 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3125 return LHS;
3126 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3127 return RHS;
3128 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3129 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3130 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3131 return ResultType;
3132#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003133
3134 case Type::TemplateSpecialization:
3135 assert(false && "Dependent types have no size");
3136 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003137 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003138
3139 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003140}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003141
Chris Lattner1d78a862008-04-07 07:01:58 +00003142//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003143// Integer Predicates
3144//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003145
Eli Friedman0832dbc2008-06-28 06:23:08 +00003146unsigned ASTContext::getIntWidth(QualType T) {
3147 if (T == BoolTy)
3148 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003149 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3150 return FWIT->getWidth();
3151 }
3152 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003153 return (unsigned)getTypeSize(T);
3154}
3155
3156QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3157 assert(T->isSignedIntegerType() && "Unexpected type");
3158 if (const EnumType* ETy = T->getAsEnumType())
3159 T = ETy->getDecl()->getIntegerType();
3160 const BuiltinType* BTy = T->getAsBuiltinType();
3161 assert (BTy && "Unexpected signed integer type");
3162 switch (BTy->getKind()) {
3163 case BuiltinType::Char_S:
3164 case BuiltinType::SChar:
3165 return UnsignedCharTy;
3166 case BuiltinType::Short:
3167 return UnsignedShortTy;
3168 case BuiltinType::Int:
3169 return UnsignedIntTy;
3170 case BuiltinType::Long:
3171 return UnsignedLongTy;
3172 case BuiltinType::LongLong:
3173 return UnsignedLongLongTy;
3174 default:
3175 assert(0 && "Unexpected signed integer type");
3176 return QualType();
3177 }
3178}
3179
3180
3181//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00003182// Serialization Support
3183//===----------------------------------------------------------------------===//
3184
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003185enum {
3186 BasicMetadataBlock = 1,
3187 ASTContextBlock = 2,
3188 DeclsBlock = 3
3189};
3190
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003191void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3192 // Create bitstream.
3193 llvm::BitstreamWriter Stream(Buffer);
3194
3195 // Emit the preamble.
3196 Stream.Emit((unsigned)'B', 8);
3197 Stream.Emit((unsigned)'C', 8);
3198 Stream.Emit(0xC, 4);
3199 Stream.Emit(0xF, 4);
3200 Stream.Emit(0xE, 4);
3201 Stream.Emit(0x0, 4);
3202
3203 // Create serializer.
3204 llvm::Serializer S(Stream);
3205
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003206 // ===---------------------------------------------------===/
3207 // Serialize the "Translation Unit" metadata.
3208 // ===---------------------------------------------------===/
3209
3210 // Emit ASTContext.
3211 S.EnterBlock(ASTContextBlock);
3212 S.EmitOwnedPtr(this);
3213 S.ExitBlock(); // exit "ASTContextBlock"
3214
3215 S.EnterBlock(BasicMetadataBlock);
3216
3217 // Block for SourceManager and Target. Allows easy skipping
3218 // around to the block for the Selectors during deserialization.
3219 S.EnterBlock();
3220
3221 // Emit the SourceManager.
3222 S.Emit(getSourceManager());
3223
3224 // Emit the Target.
3225 S.EmitPtr(&Target);
3226 S.EmitCStr(Target.getTargetTriple());
3227
3228 S.ExitBlock(); // exit "SourceManager and Target Block"
3229
3230 // Emit the Selectors.
3231 S.Emit(Selectors);
3232
3233 // Emit the Identifier Table.
3234 S.Emit(Idents);
3235
3236 S.ExitBlock(); // exit "BasicMetadataBlock"
3237}
3238
3239
Ted Kremenek738e6c02007-10-31 17:10:13 +00003240/// Emit - Serialize an ASTContext object to Bitcode.
3241void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00003242 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00003243 S.EmitRef(SourceMgr);
3244 S.EmitRef(Target);
3245 S.EmitRef(Idents);
3246 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003247
Ted Kremenek68228a92007-10-31 22:44:07 +00003248 // Emit the size of the type vector so that we can reserve that size
3249 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003250 S.EmitInt(Types.size());
3251
Ted Kremenek034a78c2007-11-13 22:02:55 +00003252 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3253 I!=E;++I)
3254 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003255
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003256 S.EmitOwnedPtr(TUDecl);
3257
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003258 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003259}
3260
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003261
3262ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3263 FileManager &FMgr) {
3264 // Check if the file is of the proper length.
3265 if (Buffer.getBufferSize() & 0x3) {
3266 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3267 return 0;
3268 }
3269
3270 // Create the bitstream reader.
3271 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3272 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3273
3274 if (Stream.Read(8) != 'B' ||
3275 Stream.Read(8) != 'C' ||
3276 Stream.Read(4) != 0xC ||
3277 Stream.Read(4) != 0xF ||
3278 Stream.Read(4) != 0xE ||
3279 Stream.Read(4) != 0x0) {
3280 // FIXME: Provide diagnostic.
3281 return NULL;
3282 }
3283
3284 // Create the deserializer.
3285 llvm::Deserializer Dezr(Stream);
3286
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003287 // ===---------------------------------------------------===/
3288 // Deserialize the "Translation Unit" metadata.
3289 // ===---------------------------------------------------===/
3290
3291 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3292 // (which will appear earlier) and record its location.
3293
3294 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3295 assert (FoundBlock);
3296
3297 llvm::Deserializer::Location ASTContextBlockLoc =
3298 Dezr.getCurrentBlockLocation();
3299
3300 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3301 assert (FoundBlock);
3302
3303 // Read the SourceManager.
3304 SourceManager::CreateAndRegister(Dezr, FMgr);
3305
3306 { // Read the TargetInfo.
3307 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3308 char* triple = Dezr.ReadCStr(NULL,0,true);
3309 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3310 delete [] triple;
3311 }
3312
3313 // For Selectors, we must read the identifier table first because the
3314 // SelectorTable depends on the identifiers being already deserialized.
3315 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3316 Dezr.SkipBlock();
3317
3318 // Read the identifier table.
3319 IdentifierTable::CreateAndRegister(Dezr);
3320
3321 // Now jump back and read the selectors.
3322 Dezr.JumpTo(SelectorBlkLoc);
3323 SelectorTable::CreateAndRegister(Dezr);
3324
3325 // Now jump back to ASTContextBlock and read the ASTContext.
3326 Dezr.JumpTo(ASTContextBlockLoc);
3327 return Dezr.ReadOwnedPtr<ASTContext>();
3328}
3329
Ted Kremenekacba3612007-11-13 00:25:37 +00003330ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00003331
3332 // Read the language options.
3333 LangOptions LOpts;
3334 LOpts.Read(D);
3335
Ted Kremenek68228a92007-10-31 22:44:07 +00003336 SourceManager &SM = D.ReadRef<SourceManager>();
3337 TargetInfo &t = D.ReadRef<TargetInfo>();
3338 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3339 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00003340
Ted Kremenek68228a92007-10-31 22:44:07 +00003341 unsigned size_reserve = D.ReadInt();
3342
Douglas Gregor24afd4a2008-11-17 14:58:09 +00003343 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3344 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00003345
Ted Kremenek034a78c2007-11-13 22:02:55 +00003346 for (unsigned i = 0; i < size_reserve; ++i)
3347 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00003348
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003349 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3350
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003351 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00003352
3353 return A;
3354}