blob: 307c0e4454afa65408d49b5dbf98605795ff0ba7 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000020#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000021#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000022#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000023#include "llvm/Bitcode/Serialize.h"
24#include "llvm/Bitcode/Deserialize.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000025#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000026#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
29enum FloatingRank {
30 FloatRank, DoubleRank, LongDoubleRank
31};
32
Chris Lattner61710852008-10-05 17:34:18 +000033ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
34 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000035 IdentifierTable &idents, SelectorTable &sels,
Steve Naroffc0ac4922009-01-27 23:20:32 +000036 bool FreeMem, unsigned size_reserve) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000037 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
38 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Chris Lattnered0e4972009-03-28 01:44:40 +000039 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels) {
Daniel Dunbare91593e2008-08-11 04:54:23 +000040 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Chris Lattner7644f072009-03-13 22:38:49 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.NoBuiltin);
Daniel Dunbare91593e2008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
44}
45
Reid Spencer5f016e22007-07-11 17:01:13 +000046ASTContext::~ASTContext() {
47 // Deallocate all the types.
48 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000049 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000050 Types.pop_back();
51 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000052
Nuno Lopesb74668e2008-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 Lattner23499252009-03-31 09:24:30 +000072 llvm::DenseMap<const ObjCInterfaceDecl*, RecordDecl*>::iterator
Nuno Lopesb74668e2008-12-17 22:30:25 +000073 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
74 while (I != E) {
Chris Lattner23499252009-03-31 09:24:30 +000075 RecordDecl *R = (I++)->second;
Nuno Lopesb74668e2008-12-17 22:30:25 +000076 R->Destroy(*this);
77 }
78 }
79
Douglas Gregorab452ba2009-03-26 23:50:42 +000080 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000081 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
82 NNS = NestedNameSpecifiers.begin(),
83 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000084 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000085 /* Increment in loop */)
86 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000087
88 if (GlobalNestedNameSpecifier)
89 GlobalNestedNameSpecifier->Destroy(*this);
90
Eli Friedmanb26153c2008-05-27 03:08:09 +000091 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +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 Dunbar248e1c02008-09-26 03:23:00 +000098 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +000099 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
100 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
101
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000103 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
104 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000105 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000106 unsigned NumExtQual = 0;
107
Reid Spencer5f016e22007-07-11 17:01:13 +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 Dunbar248e1c02008-09-26 03:23:00 +0000114 else if (isa<BlockPointerType>(T))
115 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000116 else if (isa<LValueReferenceType>(T))
117 ++NumLValueReference;
118 else if (isa<RValueReferenceType>(T))
119 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000120 else if (isa<MemberPointerType>(T))
121 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000122 else if (isa<ComplexType>(T))
123 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 else if (isa<ArrayType>(T))
125 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000126 else if (isa<VectorType>(T))
127 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000128 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000130 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 ++NumFunctionP;
132 else if (isa<TypedefType>(T))
133 ++NumTypeName;
134 else if (TagType *TT = dyn_cast<TagType>(T)) {
135 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000136 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000142 }
Ted Kremeneka526c5c2008-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 Naroff6cc18962008-05-21 15:59:22 +0000149 else if (isa<TypeOfType>(T))
150 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000151 else if (isa<TypeOfExprType>(T))
152 ++NumTypeOfExprTypes;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000153 else if (isa<ExtQualType>(T))
154 ++NumExtQual;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000155 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000156 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +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 Dunbar248e1c02008-09-26 03:23:00 +0000163 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000164 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
165 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000166 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000167 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000169 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000170 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 Kremeneka526c5c2008-01-07 19:49:32 +0000178 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000179 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000180 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000181 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000182 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000183 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000184 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000185 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
188 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000189 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000190 NumLValueReference*sizeof(LValueReferenceType)+
191 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000192 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000193 NumFunctionP*sizeof(FunctionProtoType)+
194 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000195 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000196 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
197 NumExtQual*sizeof(ExtQualType)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000198}
199
200
201void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000202 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000203}
204
Reid Spencer5f016e22007-07-11 17:01:13 +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 Lattner98be4942008-03-05 18:54:05 +0000214 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +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);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000236
Chris Lattner3a250322009-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());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000241
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000242 // Placeholder type for functions.
Douglas Gregor898574e2008-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 Gregor8e9bebd2008-10-21 16:13:35 +0000251
Reid Spencer5f016e22007-07-11 17:01:13 +0000252 // C99 6.2.5p11.
253 FloatComplexTy = getComplexType(FloatTy);
254 DoubleComplexTy = getComplexType(DoubleTy);
255 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000256
Steve Naroff7e219e42007-10-15 14:41:52 +0000257 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000258 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000259 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000260 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000261 ClassStructType = 0;
262
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000263 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000264
265 // void * type
266 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000267}
268
Chris Lattner464175b2007-07-18 17:52:12 +0000269//===----------------------------------------------------------------------===//
270// Type Sizing and Analysis
271//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000272
Chris Lattnerb7cfe882008-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 Lattneraf707ab2009-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 Dunbarb7d08442009-02-17 22:16:19 +0000289unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-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 Lattneraf707ab2009-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 Friedmandcdafb62009-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 Lattneraf707ab2009-01-24 21:53:27 +0000304 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000305
306 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000307}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000308
Chris Lattnera7674d82007-07-13 22:13:22 +0000309/// getTypeSize - Return the size of the specified type, in bits. This method
310/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000311std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000312ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000313 T = getCanonicalType(T);
Mike Stump5e301002009-02-27 18:32:39 +0000314 uint64_t Width=0;
315 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000316 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-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 Lattner692233e2007-07-13 22:27:08 +0000325 case Type::FunctionNoProto:
326 case Type::FunctionProto:
Douglas Gregor72564e72009-02-26 23:50:07 +0000327 case Type::IncompleteArray:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000328 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000329 case Type::VariableArray:
330 assert(0 && "VLAs not implemented yet!");
331 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000332 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000333
Chris Lattner98be4942008-03-05 18:54:05 +0000334 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000335 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000336 Align = EltInfo.second;
337 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000338 }
Nate Begeman213541a2008-04-18 23:10:10 +0000339 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000340 case Type::Vector: {
341 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000342 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000343 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000344 Align = Width;
Nate Begeman6fe7c8a2009-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 Lattner030d8842007-07-19 22:06:24 +0000349 break;
350 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000351
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000352 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000353 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000354 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000355 case BuiltinType::Void:
356 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000357 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000358 Width = Target.getBoolWidth();
359 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000360 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000361 case BuiltinType::Char_S:
362 case BuiltinType::Char_U:
363 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000364 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000365 Width = Target.getCharWidth();
366 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000367 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000368 case BuiltinType::WChar:
369 Width = Target.getWCharWidth();
370 Align = Target.getWCharAlign();
371 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000372 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000373 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000374 Width = Target.getShortWidth();
375 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000376 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000377 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000378 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000379 Width = Target.getIntWidth();
380 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000381 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000382 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000383 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000384 Width = Target.getLongWidth();
385 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000386 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000387 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000388 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000389 Width = Target.getLongLongWidth();
390 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000391 break;
392 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000393 Width = Target.getFloatWidth();
394 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000395 break;
396 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000397 Width = Target.getDoubleWidth();
398 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000399 break;
400 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000401 Width = Target.getLongDoubleWidth();
402 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000403 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000404 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000405 break;
Eli Friedmanf98aba32009-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 Lattner736166b2009-02-15 21:20:13 +0000410 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000411 Align = Width;
412 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000413 case Type::ExtQual:
Chris Lattner98be4942008-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 Jahanianf11284a2009-02-17 18:27:45 +0000416 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000417 case Type::ObjCQualifiedId:
Eli Friedman4bdf0872009-02-22 04:02:33 +0000418 case Type::ObjCQualifiedClass:
Douglas Gregor72564e72009-02-26 23:50:07 +0000419 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000420 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000421 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000422 break;
Steve Naroff485eeff2008-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 Lattnerf72a4432008-03-08 08:34:58 +0000429 case Type::Pointer: {
430 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000431 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000432 Align = Target.getPointerAlign(AS);
433 break;
434 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000435 case Type::LValueReference:
436 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000437 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000438 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000439 // FIXME: This is wrong for struct layout: a reference in a struct has
440 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000441 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000442 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000443 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-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 Redl8edef7c2009-01-24 23:29:36 +0000446 // other compilers too, we need to delegate this completely to TargetInfo
447 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-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 Lattner5d2a6302007-07-18 18:26:58 +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 Lattner98be4942008-03-05 18:54:05 +0000460 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000461 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000462 Align = EltInfo.second;
463 break;
464 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000465 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000466 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000467 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
468 Width = Layout.getSize();
469 Align = Layout.getAlignment();
470 break;
471 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000472 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000473 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000474 const TagType *TT = cast<TagType>(T);
475
476 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000477 Width = 1;
478 Align = 1;
479 break;
480 }
481
Daniel Dunbar1d751182008-11-08 05:48:37 +0000482 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000483 return getTypeInfo(ET->getDecl()->getIntegerType());
484
Daniel Dunbar1d751182008-11-08 05:48:37 +0000485 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000486 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
487 Width = Layout.getSize();
488 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000489 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000490 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000491
492 case Type::TemplateSpecialization:
493 assert(false && "Dependent types have no size");
494 break;
Chris Lattner71763312008-04-06 22:05:18 +0000495 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000496
Chris Lattner464175b2007-07-18 17:52:12 +0000497 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000498 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000499}
500
Chris Lattner34ebde42009-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 Dunbare00d5c02009-02-18 19:59:32 +0000509 if (T->isSpecificBuiltinType(BuiltinType::Double))
510 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000511
512 return ABIAlign;
513}
514
515
Devang Patel8b277042008-06-04 21:22:16 +0000516/// LayoutField - Field layout.
517void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000518 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000519 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000520 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000521 uint64_t FieldOffset = IsUnion ? 0 : Size;
522 uint64_t FieldSize;
523 unsigned FieldAlign;
Daniel Dunbar3b0db902008-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 Patel8b277042008-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 Dunbar32442bb2008-08-13 23:47:13 +0000533 FieldSize =
534 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-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 Dunbar3b0db902008-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 Patel8b277042008-06-04 21:22:16 +0000545 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000546 if (FieldPacking)
547 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-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 Lattner8389eab2008-08-09 21:35:13 +0000560 if (FD->getType()->isIncompleteArrayType()) {
561 // This is a flexible array member; we can't directly
Devang Patel8b277042008-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 Lattnerc63a1f22008-08-04 07:31:14 +0000566 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-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 Dunbar3b0db902008-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 Patel8b277042008-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 Jahanian88e469c2009-03-05 20:08:48 +0000605void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
Chris Lattnerf1690852009-03-31 08:48:01 +0000606 llvm::SmallVectorImpl<FieldDecl*> &Fields) const {
Fariborz Jahaniana769c002008-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 Lattnerf1690852009-03-31 08:48:01 +0000612 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000613 if (!IVDecl->isInvalidDecl())
614 Fields.push_back(cast<FieldDecl>(IVDecl));
615 }
Fariborz Jahanianaf3e7222009-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 Lattnerf1690852009-03-31 08:48:01 +0000619 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +0000620 Fields.push_back(cast<FieldDecl>(IV));
621 }
Fariborz Jahaniana769c002008-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 Lattnerf1690852009-03-31 08:48:01 +0000627const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) {
Chris Lattner23499252009-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 Lattnerf1690852009-03-31 08:48:01 +0000641
642 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000643 CollectObjCIvars(D, RecFields);
Chris Lattner23499252009-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 Jahaniana769c002008-12-17 21:40:49 +0000648 /// FIXME! Can do collection of ivars and adding to the record while
649 /// doing it.
Chris Lattner16ff7052009-03-31 08:58:42 +0000650 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Chris Lattner23499252009-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 Jahaniana769c002008-12-17 21:40:49 +0000656 }
Chris Lattnerf1690852009-03-31 08:48:01 +0000657
Chris Lattner23499252009-03-31 09:24:30 +0000658 RD->completeDefinition(*this);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000659 return RD;
660}
Devang Patel44a3dde2008-06-04 21:54:36 +0000661
Fariborz Jahanianefc4c4b2008-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 Lattnerda046392009-03-31 08:31:13 +0000667 ASTFieldForIvarRef[MRef] = OI->lookupFieldDeclForIvar(*this, Ivar);
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000668}
669
Chris Lattner61710852008-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 Patel44a3dde2008-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 Patel6a5a34c2008-06-06 02:14:01 +0000681 ASTRecordLayout *NewEntry = NULL;
682 unsigned FieldCount = D->ivar_size();
683 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
684 FieldCount++;
685 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
686 unsigned Alignment = SL.getAlignment();
687 uint64_t Size = SL.getSize();
688 NewEntry = new ASTRecordLayout(Size, Alignment);
689 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000690 // Super class is at the beginning of the layout.
691 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000692 } else {
693 NewEntry = new ASTRecordLayout();
694 NewEntry->InitializeLayout(FieldCount);
695 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000696 Entry = NewEntry;
697
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000698 unsigned StructPacking = 0;
699 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
700 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000701
702 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
703 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
704 AA->getAlignment()));
705
706 // Layout each ivar sequentially.
707 unsigned i = 0;
708 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
709 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
710 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000711 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000712 }
Fariborz Jahanian18191882009-03-31 18:11:23 +0000713 // Also synthesized ivars
714 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(),
715 E = D->prop_end(); I != E; ++I) {
716 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
717 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
718 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000719
Devang Patel44a3dde2008-06-04 21:54:36 +0000720 // Finally, round the size of the total struct up to the alignment of the
721 // struct itself.
722 NewEntry->FinalizeLayout();
723 return *NewEntry;
724}
725
Devang Patel88a981b2007-11-01 19:11:01 +0000726/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000727/// specified record (struct/union/class), which indicates its size and field
728/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000729const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000730 D = D->getDefinition(*this);
731 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000732
Chris Lattner464175b2007-07-18 17:52:12 +0000733 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000734 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000735 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000736
Devang Patel88a981b2007-11-01 19:11:01 +0000737 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
738 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
739 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000740 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000741
Douglas Gregore267ff32008-12-11 20:41:00 +0000742 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000743 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000744 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000745
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000746 unsigned StructPacking = 0;
747 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
748 StructPacking = PA->getAlignment();
749
Eli Friedman4bd998b2008-05-30 09:31:38 +0000750 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000751 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
752 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000753
Eli Friedman4bd998b2008-05-30 09:31:38 +0000754 // Layout each field, for now, just sequentially, respecting alignment. In
755 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000756 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000757 for (RecordDecl::field_iterator Field = D->field_begin(),
758 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000759 Field != FieldEnd; (void)++Field, ++FieldIdx)
760 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000761
762 // Finally, round the size of the total struct up to the alignment of the
763 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000764 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000765 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000766}
767
Chris Lattnera7674d82007-07-13 22:13:22 +0000768//===----------------------------------------------------------------------===//
769// Type creation/memoization methods
770//===----------------------------------------------------------------------===//
771
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000772QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000773 QualType CanT = getCanonicalType(T);
774 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000775 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000776
777 // If we are composing extended qualifiers together, merge together into one
778 // ExtQualType node.
779 unsigned CVRQuals = T.getCVRQualifiers();
780 QualType::GCAttrTypes GCAttr = QualType::GCNone;
781 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000782
Chris Lattnerb7d25532009-02-18 22:53:11 +0000783 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
784 // If this type already has an address space specified, it cannot get
785 // another one.
786 assert(EQT->getAddressSpace() == 0 &&
787 "Type cannot be in multiple addr spaces!");
788 GCAttr = EQT->getObjCGCAttr();
789 TypeNode = EQT->getBaseType();
790 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000791
Chris Lattnerb7d25532009-02-18 22:53:11 +0000792 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000793 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000794 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000795 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000796 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000797 return QualType(EXTQy, CVRQuals);
798
Christopher Lambebb97e92008-02-04 02:31:56 +0000799 // If the base type isn't canonical, this won't be a canonical type either,
800 // so fill in the canonical type field.
801 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000802 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000803 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000804
Chris Lattnerb7d25532009-02-18 22:53:11 +0000805 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000806 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000807 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000808 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000809 ExtQualType *New =
810 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000811 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000812 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000813 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000814}
815
Chris Lattnerb7d25532009-02-18 22:53:11 +0000816QualType ASTContext::getObjCGCQualType(QualType T,
817 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000818 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000819 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000820 return T;
821
Chris Lattnerb7d25532009-02-18 22:53:11 +0000822 // If we are composing extended qualifiers together, merge together into one
823 // ExtQualType node.
824 unsigned CVRQuals = T.getCVRQualifiers();
825 Type *TypeNode = T.getTypePtr();
826 unsigned AddressSpace = 0;
827
828 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
829 // If this type already has an address space specified, it cannot get
830 // another one.
831 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
832 "Type cannot be in multiple addr spaces!");
833 AddressSpace = EQT->getAddressSpace();
834 TypeNode = EQT->getBaseType();
835 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000836
837 // Check if we've already instantiated an gc qual'd type of this type.
838 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000839 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000840 void *InsertPos = 0;
841 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000842 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000843
844 // If the base type isn't canonical, this won't be a canonical type either,
845 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000846 // FIXME: Isn't this also not canonical if the base type is a array
847 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000848 QualType Canonical;
849 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000850 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000851
Chris Lattnerb7d25532009-02-18 22:53:11 +0000852 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000853 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
854 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
855 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000856 ExtQualType *New =
857 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000858 ExtQualTypes.InsertNode(New, InsertPos);
859 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000860 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000861}
Chris Lattnera7674d82007-07-13 22:13:22 +0000862
Reid Spencer5f016e22007-07-11 17:01:13 +0000863/// getComplexType - Return the uniqued reference to the type for a complex
864/// number with the specified element type.
865QualType ASTContext::getComplexType(QualType T) {
866 // Unique pointers, to guarantee there is only one pointer of a particular
867 // structure.
868 llvm::FoldingSetNodeID ID;
869 ComplexType::Profile(ID, T);
870
871 void *InsertPos = 0;
872 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
873 return QualType(CT, 0);
874
875 // If the pointee type isn't canonical, this won't be a canonical type either,
876 // so fill in the canonical type field.
877 QualType Canonical;
878 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000879 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000880
881 // Get the new insert position for the node we care about.
882 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000883 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 }
Steve Narofff83820b2009-01-27 22:08:43 +0000885 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 Types.push_back(New);
887 ComplexTypes.InsertNode(New, InsertPos);
888 return QualType(New, 0);
889}
890
Eli Friedmanf98aba32009-02-13 02:31:07 +0000891QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
892 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
893 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
894 FixedWidthIntType *&Entry = Map[Width];
895 if (!Entry)
896 Entry = new FixedWidthIntType(Width, Signed);
897 return QualType(Entry, 0);
898}
Reid Spencer5f016e22007-07-11 17:01:13 +0000899
900/// getPointerType - Return the uniqued reference to the type for a pointer to
901/// the specified type.
902QualType ASTContext::getPointerType(QualType T) {
903 // Unique pointers, to guarantee there is only one pointer of a particular
904 // structure.
905 llvm::FoldingSetNodeID ID;
906 PointerType::Profile(ID, T);
907
908 void *InsertPos = 0;
909 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
910 return QualType(PT, 0);
911
912 // If the pointee type isn't canonical, this won't be a canonical type either,
913 // so fill in the canonical type field.
914 QualType Canonical;
915 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000916 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000917
918 // Get the new insert position for the node we care about.
919 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000920 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000921 }
Steve Narofff83820b2009-01-27 22:08:43 +0000922 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 Types.push_back(New);
924 PointerTypes.InsertNode(New, InsertPos);
925 return QualType(New, 0);
926}
927
Steve Naroff5618bd42008-08-27 16:04:49 +0000928/// getBlockPointerType - Return the uniqued reference to the type for
929/// a pointer to the specified block.
930QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000931 assert(T->isFunctionType() && "block of function types only");
932 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000933 // structure.
934 llvm::FoldingSetNodeID ID;
935 BlockPointerType::Profile(ID, T);
936
937 void *InsertPos = 0;
938 if (BlockPointerType *PT =
939 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
940 return QualType(PT, 0);
941
Steve Naroff296e8d52008-08-28 19:20:44 +0000942 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000943 // type either so fill in the canonical type field.
944 QualType Canonical;
945 if (!T->isCanonical()) {
946 Canonical = getBlockPointerType(getCanonicalType(T));
947
948 // Get the new insert position for the node we care about.
949 BlockPointerType *NewIP =
950 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000951 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000952 }
Steve Narofff83820b2009-01-27 22:08:43 +0000953 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000954 Types.push_back(New);
955 BlockPointerTypes.InsertNode(New, InsertPos);
956 return QualType(New, 0);
957}
958
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000959/// getLValueReferenceType - Return the uniqued reference to the type for an
960/// lvalue reference to the specified type.
961QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 // Unique pointers, to guarantee there is only one pointer of a particular
963 // structure.
964 llvm::FoldingSetNodeID ID;
965 ReferenceType::Profile(ID, T);
966
967 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000968 if (LValueReferenceType *RT =
969 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000970 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000971
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 // If the referencee type isn't canonical, this won't be a canonical type
973 // either, so fill in the canonical type field.
974 QualType Canonical;
975 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000976 Canonical = getLValueReferenceType(getCanonicalType(T));
977
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000979 LValueReferenceType *NewIP =
980 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000981 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 }
983
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000984 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000986 LValueReferenceTypes.InsertNode(New, InsertPos);
987 return QualType(New, 0);
988}
989
990/// getRValueReferenceType - Return the uniqued reference to the type for an
991/// rvalue reference to the specified type.
992QualType ASTContext::getRValueReferenceType(QualType T) {
993 // Unique pointers, to guarantee there is only one pointer of a particular
994 // structure.
995 llvm::FoldingSetNodeID ID;
996 ReferenceType::Profile(ID, T);
997
998 void *InsertPos = 0;
999 if (RValueReferenceType *RT =
1000 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1001 return QualType(RT, 0);
1002
1003 // If the referencee type isn't canonical, this won't be a canonical type
1004 // either, so fill in the canonical type field.
1005 QualType Canonical;
1006 if (!T->isCanonical()) {
1007 Canonical = getRValueReferenceType(getCanonicalType(T));
1008
1009 // Get the new insert position for the node we care about.
1010 RValueReferenceType *NewIP =
1011 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1012 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1013 }
1014
1015 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1016 Types.push_back(New);
1017 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001018 return QualType(New, 0);
1019}
1020
Sebastian Redlf30208a2009-01-24 21:16:55 +00001021/// getMemberPointerType - Return the uniqued reference to the type for a
1022/// member pointer to the specified type, in the specified class.
1023QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1024{
1025 // Unique pointers, to guarantee there is only one pointer of a particular
1026 // structure.
1027 llvm::FoldingSetNodeID ID;
1028 MemberPointerType::Profile(ID, T, Cls);
1029
1030 void *InsertPos = 0;
1031 if (MemberPointerType *PT =
1032 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1033 return QualType(PT, 0);
1034
1035 // If the pointee or class type isn't canonical, this won't be a canonical
1036 // type either, so fill in the canonical type field.
1037 QualType Canonical;
1038 if (!T->isCanonical()) {
1039 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1040
1041 // Get the new insert position for the node we care about.
1042 MemberPointerType *NewIP =
1043 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1044 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1045 }
Steve Narofff83820b2009-01-27 22:08:43 +00001046 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001047 Types.push_back(New);
1048 MemberPointerTypes.InsertNode(New, InsertPos);
1049 return QualType(New, 0);
1050}
1051
Steve Narofffb22d962007-08-30 01:06:46 +00001052/// getConstantArrayType - Return the unique reference to the type for an
1053/// array of the specified element type.
1054QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001055 const llvm::APInt &ArySize,
1056 ArrayType::ArraySizeModifier ASM,
1057 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001059 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001060
1061 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001062 if (ConstantArrayType *ATP =
1063 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001064 return QualType(ATP, 0);
1065
1066 // If the element type isn't canonical, this won't be a canonical type either,
1067 // so fill in the canonical type field.
1068 QualType Canonical;
1069 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001070 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001071 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001073 ConstantArrayType *NewIP =
1074 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001075 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 }
1077
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001078 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001079 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001080 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001081 Types.push_back(New);
1082 return QualType(New, 0);
1083}
1084
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001085/// getVariableArrayType - Returns a non-unique reference to the type for a
1086/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001087QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1088 ArrayType::ArraySizeModifier ASM,
1089 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001090 // Since we don't unique expressions, it isn't possible to unique VLA's
1091 // that have an expression provided for their size.
1092
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001093 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001094 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001095
1096 VariableArrayTypes.push_back(New);
1097 Types.push_back(New);
1098 return QualType(New, 0);
1099}
1100
Douglas Gregor898574e2008-12-05 23:32:09 +00001101/// getDependentSizedArrayType - Returns a non-unique reference to
1102/// the type for a dependently-sized array of the specified element
1103/// type. FIXME: We will need these to be uniqued, or at least
1104/// comparable, at some point.
1105QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1106 ArrayType::ArraySizeModifier ASM,
1107 unsigned EltTypeQuals) {
1108 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1109 "Size must be type- or value-dependent!");
1110
1111 // Since we don't unique expressions, it isn't possible to unique
1112 // dependently-sized array types.
1113
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001114 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001115 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1116 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001117
1118 DependentSizedArrayTypes.push_back(New);
1119 Types.push_back(New);
1120 return QualType(New, 0);
1121}
1122
Eli Friedmanc5773c42008-02-15 18:16:39 +00001123QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1124 ArrayType::ArraySizeModifier ASM,
1125 unsigned EltTypeQuals) {
1126 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001127 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001128
1129 void *InsertPos = 0;
1130 if (IncompleteArrayType *ATP =
1131 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1132 return QualType(ATP, 0);
1133
1134 // If the element type isn't canonical, this won't be a canonical type
1135 // either, so fill in the canonical type field.
1136 QualType Canonical;
1137
1138 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001139 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001140 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001141
1142 // Get the new insert position for the node we care about.
1143 IncompleteArrayType *NewIP =
1144 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001145 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001146 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001147
Steve Narofff83820b2009-01-27 22:08:43 +00001148 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001149 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001150
1151 IncompleteArrayTypes.InsertNode(New, InsertPos);
1152 Types.push_back(New);
1153 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001154}
1155
Steve Naroff73322922007-07-18 18:00:27 +00001156/// getVectorType - Return the unique reference to a vector type of
1157/// the specified element type and size. VectorType must be a built-in type.
1158QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 BuiltinType *baseType;
1160
Chris Lattnerf52ab252008-04-06 22:59:24 +00001161 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001162 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001163
1164 // Check if we've already instantiated a vector of this type.
1165 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001166 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001167 void *InsertPos = 0;
1168 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1169 return QualType(VTP, 0);
1170
1171 // If the element type isn't canonical, this won't be a canonical type either,
1172 // so fill in the canonical type field.
1173 QualType Canonical;
1174 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001175 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001176
1177 // Get the new insert position for the node we care about.
1178 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001179 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 }
Steve Narofff83820b2009-01-27 22:08:43 +00001181 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 VectorTypes.InsertNode(New, InsertPos);
1183 Types.push_back(New);
1184 return QualType(New, 0);
1185}
1186
Nate Begeman213541a2008-04-18 23:10:10 +00001187/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001188/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001189QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001190 BuiltinType *baseType;
1191
Chris Lattnerf52ab252008-04-06 22:59:24 +00001192 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001193 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001194
1195 // Check if we've already instantiated a vector of this type.
1196 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001197 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001198 void *InsertPos = 0;
1199 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1200 return QualType(VTP, 0);
1201
1202 // If the element type isn't canonical, this won't be a canonical type either,
1203 // so fill in the canonical type field.
1204 QualType Canonical;
1205 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001206 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001207
1208 // Get the new insert position for the node we care about.
1209 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001210 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001211 }
Steve Narofff83820b2009-01-27 22:08:43 +00001212 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001213 VectorTypes.InsertNode(New, InsertPos);
1214 Types.push_back(New);
1215 return QualType(New, 0);
1216}
1217
Douglas Gregor72564e72009-02-26 23:50:07 +00001218/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001219///
Douglas Gregor72564e72009-02-26 23:50:07 +00001220QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001221 // Unique functions, to guarantee there is only one function of a particular
1222 // structure.
1223 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001224 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001225
1226 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001227 if (FunctionNoProtoType *FT =
1228 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001229 return QualType(FT, 0);
1230
1231 QualType Canonical;
1232 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001233 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001234
1235 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001236 FunctionNoProtoType *NewIP =
1237 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001238 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001239 }
1240
Douglas Gregor72564e72009-02-26 23:50:07 +00001241 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001243 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 return QualType(New, 0);
1245}
1246
1247/// getFunctionType - Return a normal function type with a typed argument
1248/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001249QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001250 unsigned NumArgs, bool isVariadic,
1251 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001252 // Unique functions, to guarantee there is only one function of a particular
1253 // structure.
1254 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001255 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001256 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001257
1258 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001259 if (FunctionProtoType *FTP =
1260 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 return QualType(FTP, 0);
1262
1263 // Determine whether the type being created is already canonical or not.
1264 bool isCanonical = ResultTy->isCanonical();
1265 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1266 if (!ArgArray[i]->isCanonical())
1267 isCanonical = false;
1268
1269 // If this type isn't canonical, get the canonical version of it.
1270 QualType Canonical;
1271 if (!isCanonical) {
1272 llvm::SmallVector<QualType, 16> CanonicalArgs;
1273 CanonicalArgs.reserve(NumArgs);
1274 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001275 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001276
Chris Lattnerf52ab252008-04-06 22:59:24 +00001277 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001278 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001279 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001280
1281 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001282 FunctionProtoType *NewIP =
1283 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001284 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001285 }
1286
Douglas Gregor72564e72009-02-26 23:50:07 +00001287 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001288 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001289 FunctionProtoType *FTP =
1290 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001291 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001292 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001293 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001294 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001295 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 return QualType(FTP, 0);
1297}
1298
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001299/// getTypeDeclType - Return the unique reference to the type for the
1300/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001301QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001302 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001303 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1304
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001305 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001306 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001307 else if (isa<TemplateTypeParmDecl>(Decl)) {
1308 assert(false && "Template type parameter types are always available.");
1309 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001310 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001311
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001312 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001313 if (PrevDecl)
1314 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001315 else
1316 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001317 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001318 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1319 if (PrevDecl)
1320 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001321 else
1322 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001323 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001324 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001325 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001326
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001327 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001328 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001329}
1330
Reid Spencer5f016e22007-07-11 17:01:13 +00001331/// getTypedefType - Return the unique reference to the type for the
1332/// specified typename decl.
1333QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1334 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1335
Chris Lattnerf52ab252008-04-06 22:59:24 +00001336 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001337 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001338 Types.push_back(Decl->TypeForDecl);
1339 return QualType(Decl->TypeForDecl, 0);
1340}
1341
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001342/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001343/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001344QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001345 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1346
Steve Narofff83820b2009-01-27 22:08:43 +00001347 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001348 Types.push_back(Decl->TypeForDecl);
1349 return QualType(Decl->TypeForDecl, 0);
1350}
1351
Douglas Gregorfab9d672009-02-05 23:33:38 +00001352/// \brief Retrieve the template type parameter type for a template
1353/// parameter with the given depth, index, and (optionally) name.
1354QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1355 IdentifierInfo *Name) {
1356 llvm::FoldingSetNodeID ID;
1357 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1358 void *InsertPos = 0;
1359 TemplateTypeParmType *TypeParm
1360 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1361
1362 if (TypeParm)
1363 return QualType(TypeParm, 0);
1364
1365 if (Name)
1366 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1367 getTemplateTypeParmType(Depth, Index));
1368 else
1369 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1370
1371 Types.push_back(TypeParm);
1372 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1373
1374 return QualType(TypeParm, 0);
1375}
1376
Douglas Gregor55f6b142009-02-09 18:46:07 +00001377QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001378ASTContext::getTemplateSpecializationType(TemplateName Template,
1379 const TemplateArgument *Args,
1380 unsigned NumArgs,
1381 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001382 if (!Canon.isNull())
1383 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001384
Douglas Gregor55f6b142009-02-09 18:46:07 +00001385 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001386 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001387
Douglas Gregor55f6b142009-02-09 18:46:07 +00001388 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001389 TemplateSpecializationType *Spec
1390 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001391
1392 if (Spec)
1393 return QualType(Spec, 0);
1394
Douglas Gregor7532dc62009-03-30 22:58:21 +00001395 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001396 sizeof(TemplateArgument) * NumArgs),
1397 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001398 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001399 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001400 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001401
1402 return QualType(Spec, 0);
1403}
1404
Douglas Gregore4e5b052009-03-19 00:18:19 +00001405QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001406ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001407 QualType NamedType) {
1408 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001409 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001410
1411 void *InsertPos = 0;
1412 QualifiedNameType *T
1413 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1414 if (T)
1415 return QualType(T, 0);
1416
Douglas Gregorab452ba2009-03-26 23:50:42 +00001417 T = new (*this) QualifiedNameType(NNS, NamedType,
1418 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001419 Types.push_back(T);
1420 QualifiedNameTypes.InsertNode(T, InsertPos);
1421 return QualType(T, 0);
1422}
1423
Douglas Gregord57959a2009-03-27 23:10:48 +00001424QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1425 const IdentifierInfo *Name,
1426 QualType Canon) {
1427 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1428
1429 if (Canon.isNull()) {
1430 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1431 if (CanonNNS != NNS)
1432 Canon = getTypenameType(CanonNNS, Name);
1433 }
1434
1435 llvm::FoldingSetNodeID ID;
1436 TypenameType::Profile(ID, NNS, Name);
1437
1438 void *InsertPos = 0;
1439 TypenameType *T
1440 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1441 if (T)
1442 return QualType(T, 0);
1443
1444 T = new (*this) TypenameType(NNS, Name, Canon);
1445 Types.push_back(T);
1446 TypenameTypes.InsertNode(T, InsertPos);
1447 return QualType(T, 0);
1448}
1449
Douglas Gregor17343172009-04-01 00:28:59 +00001450QualType
1451ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1452 const TemplateSpecializationType *TemplateId,
1453 QualType Canon) {
1454 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1455
1456 if (Canon.isNull()) {
1457 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1458 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1459 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1460 const TemplateSpecializationType *CanonTemplateId
1461 = CanonType->getAsTemplateSpecializationType();
1462 assert(CanonTemplateId &&
1463 "Canonical type must also be a template specialization type");
1464 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1465 }
1466 }
1467
1468 llvm::FoldingSetNodeID ID;
1469 TypenameType::Profile(ID, NNS, TemplateId);
1470
1471 void *InsertPos = 0;
1472 TypenameType *T
1473 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1474 if (T)
1475 return QualType(T, 0);
1476
1477 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1478 Types.push_back(T);
1479 TypenameTypes.InsertNode(T, InsertPos);
1480 return QualType(T, 0);
1481}
1482
Chris Lattner88cb27a2008-04-07 04:56:42 +00001483/// CmpProtocolNames - Comparison predicate for sorting protocols
1484/// alphabetically.
1485static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1486 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001487 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001488}
1489
1490static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1491 unsigned &NumProtocols) {
1492 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1493
1494 // Sort protocols, keyed by name.
1495 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1496
1497 // Remove duplicates.
1498 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1499 NumProtocols = ProtocolsEnd-Protocols;
1500}
1501
1502
Chris Lattner065f0d72008-04-07 04:44:08 +00001503/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1504/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001505QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1506 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001507 // Sort the protocol list alphabetically to canonicalize it.
1508 SortAndUniqueProtocols(Protocols, NumProtocols);
1509
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001510 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001511 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001512
1513 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001514 if (ObjCQualifiedInterfaceType *QT =
1515 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001516 return QualType(QT, 0);
1517
1518 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001519 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001520 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001521
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001522 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001523 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001524 return QualType(QType, 0);
1525}
1526
Chris Lattner88cb27a2008-04-07 04:56:42 +00001527/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1528/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001529QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001530 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001531 // Sort the protocol list alphabetically to canonicalize it.
1532 SortAndUniqueProtocols(Protocols, NumProtocols);
1533
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001534 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001535 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001536
1537 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001538 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001539 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001540 return QualType(QT, 0);
1541
1542 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001543 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001544 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001545 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001546 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001547 return QualType(QType, 0);
1548}
1549
Douglas Gregor72564e72009-02-26 23:50:07 +00001550/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1551/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001552/// multiple declarations that refer to "typeof(x)" all contain different
1553/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1554/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001555QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001556 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001557 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001558 Types.push_back(toe);
1559 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001560}
1561
Steve Naroff9752f252007-08-01 18:02:17 +00001562/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1563/// TypeOfType AST's. The only motivation to unique these nodes would be
1564/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1565/// an issue. This doesn't effect the type checker, since it operates
1566/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001567QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001568 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001569 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001570 Types.push_back(tot);
1571 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001572}
1573
Reid Spencer5f016e22007-07-11 17:01:13 +00001574/// getTagDeclType - Return the unique reference to the type for the
1575/// specified TagDecl (struct/union/class/enum) decl.
1576QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001577 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001578 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001579}
1580
1581/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1582/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1583/// needs to agree with the definition in <stddef.h>.
1584QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001585 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001586}
1587
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001588/// getSignedWCharType - Return the type of "signed wchar_t".
1589/// Used when in C++, as a GCC extension.
1590QualType ASTContext::getSignedWCharType() const {
1591 // FIXME: derive from "Target" ?
1592 return WCharTy;
1593}
1594
1595/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1596/// Used when in C++, as a GCC extension.
1597QualType ASTContext::getUnsignedWCharType() const {
1598 // FIXME: derive from "Target" ?
1599 return UnsignedIntTy;
1600}
1601
Chris Lattner8b9023b2007-07-13 03:05:23 +00001602/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1603/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1604QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001605 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001606}
1607
Chris Lattnere6327742008-04-02 05:18:44 +00001608//===----------------------------------------------------------------------===//
1609// Type Operators
1610//===----------------------------------------------------------------------===//
1611
Chris Lattner77c96472008-04-06 22:41:35 +00001612/// getCanonicalType - Return the canonical (structural) type corresponding to
1613/// the specified potentially non-canonical type. The non-canonical version
1614/// of a type may have many "decorated" versions of types. Decorators can
1615/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1616/// to be free of any of these, allowing two canonical types to be compared
1617/// for exact equality with a simple pointer comparison.
1618QualType ASTContext::getCanonicalType(QualType T) {
1619 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001620
1621 // If the result has type qualifiers, make sure to canonicalize them as well.
1622 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1623 if (TypeQuals == 0) return CanType;
1624
1625 // If the type qualifiers are on an array type, get the canonical type of the
1626 // array with the qualifiers applied to the element type.
1627 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1628 if (!AT)
1629 return CanType.getQualifiedType(TypeQuals);
1630
1631 // Get the canonical version of the element with the extra qualifiers on it.
1632 // This can recursively sink qualifiers through multiple levels of arrays.
1633 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1634 NewEltTy = getCanonicalType(NewEltTy);
1635
1636 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1637 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1638 CAT->getIndexTypeQualifier());
1639 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1640 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1641 IAT->getIndexTypeQualifier());
1642
Douglas Gregor898574e2008-12-05 23:32:09 +00001643 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1644 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1645 DSAT->getSizeModifier(),
1646 DSAT->getIndexTypeQualifier());
1647
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001648 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1649 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1650 VAT->getSizeModifier(),
1651 VAT->getIndexTypeQualifier());
1652}
1653
Douglas Gregord57959a2009-03-27 23:10:48 +00001654NestedNameSpecifier *
1655ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1656 if (!NNS)
1657 return 0;
1658
1659 switch (NNS->getKind()) {
1660 case NestedNameSpecifier::Identifier:
1661 // Canonicalize the prefix but keep the identifier the same.
1662 return NestedNameSpecifier::Create(*this,
1663 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1664 NNS->getAsIdentifier());
1665
1666 case NestedNameSpecifier::Namespace:
1667 // A namespace is canonical; build a nested-name-specifier with
1668 // this namespace and no prefix.
1669 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1670
1671 case NestedNameSpecifier::TypeSpec:
1672 case NestedNameSpecifier::TypeSpecWithTemplate: {
1673 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1674 NestedNameSpecifier *Prefix = 0;
1675
1676 // FIXME: This isn't the right check!
1677 if (T->isDependentType())
1678 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1679
1680 return NestedNameSpecifier::Create(*this, Prefix,
1681 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1682 T.getTypePtr());
1683 }
1684
1685 case NestedNameSpecifier::Global:
1686 // The global specifier is canonical and unique.
1687 return NNS;
1688 }
1689
1690 // Required to silence a GCC warning
1691 return 0;
1692}
1693
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001694
1695const ArrayType *ASTContext::getAsArrayType(QualType T) {
1696 // Handle the non-qualified case efficiently.
1697 if (T.getCVRQualifiers() == 0) {
1698 // Handle the common positive case fast.
1699 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1700 return AT;
1701 }
1702
1703 // Handle the common negative case fast, ignoring CVR qualifiers.
1704 QualType CType = T->getCanonicalTypeInternal();
1705
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001706 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001707 // test.
1708 if (!isa<ArrayType>(CType) &&
1709 !isa<ArrayType>(CType.getUnqualifiedType()))
1710 return 0;
1711
1712 // Apply any CVR qualifiers from the array type to the element type. This
1713 // implements C99 6.7.3p8: "If the specification of an array type includes
1714 // any type qualifiers, the element type is so qualified, not the array type."
1715
1716 // If we get here, we either have type qualifiers on the type, or we have
1717 // sugar such as a typedef in the way. If we have type qualifiers on the type
1718 // we must propagate them down into the elemeng type.
1719 unsigned CVRQuals = T.getCVRQualifiers();
1720 unsigned AddrSpace = 0;
1721 Type *Ty = T.getTypePtr();
1722
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001723 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001724 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001725 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1726 AddrSpace = EXTQT->getAddressSpace();
1727 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001728 } else {
1729 T = Ty->getDesugaredType();
1730 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1731 break;
1732 CVRQuals |= T.getCVRQualifiers();
1733 Ty = T.getTypePtr();
1734 }
1735 }
1736
1737 // If we have a simple case, just return now.
1738 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1739 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1740 return ATy;
1741
1742 // Otherwise, we have an array and we have qualifiers on it. Push the
1743 // qualifiers into the array element type and return a new array type.
1744 // Get the canonical version of the element with the extra qualifiers on it.
1745 // This can recursively sink qualifiers through multiple levels of arrays.
1746 QualType NewEltTy = ATy->getElementType();
1747 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001748 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001749 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1750
1751 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1752 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1753 CAT->getSizeModifier(),
1754 CAT->getIndexTypeQualifier()));
1755 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1756 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1757 IAT->getSizeModifier(),
1758 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001759
Douglas Gregor898574e2008-12-05 23:32:09 +00001760 if (const DependentSizedArrayType *DSAT
1761 = dyn_cast<DependentSizedArrayType>(ATy))
1762 return cast<ArrayType>(
1763 getDependentSizedArrayType(NewEltTy,
1764 DSAT->getSizeExpr(),
1765 DSAT->getSizeModifier(),
1766 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001767
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001768 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1769 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1770 VAT->getSizeModifier(),
1771 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001772}
1773
1774
Chris Lattnere6327742008-04-02 05:18:44 +00001775/// getArrayDecayedType - Return the properly qualified result of decaying the
1776/// specified array type to a pointer. This operation is non-trivial when
1777/// handling typedefs etc. The canonical type of "T" must be an array type,
1778/// this returns a pointer to a properly qualified element of the array.
1779///
1780/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1781QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001782 // Get the element type with 'getAsArrayType' so that we don't lose any
1783 // typedefs in the element type of the array. This also handles propagation
1784 // of type qualifiers from the array type into the element type if present
1785 // (C99 6.7.3p8).
1786 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1787 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001788
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001789 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001790
1791 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001792 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001793}
1794
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001795QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001796 QualType ElemTy = VAT->getElementType();
1797
1798 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1799 return getBaseElementType(VAT);
1800
1801 return ElemTy;
1802}
1803
Reid Spencer5f016e22007-07-11 17:01:13 +00001804/// getFloatingRank - Return a relative rank for floating point types.
1805/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001806static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001807 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001808 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001809
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001810 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001811 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001812 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001813 case BuiltinType::Float: return FloatRank;
1814 case BuiltinType::Double: return DoubleRank;
1815 case BuiltinType::LongDouble: return LongDoubleRank;
1816 }
1817}
1818
Steve Naroff716c7302007-08-27 01:41:48 +00001819/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1820/// point or a complex type (based on typeDomain/typeSize).
1821/// 'typeDomain' is a real floating point or complex type.
1822/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001823QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1824 QualType Domain) const {
1825 FloatingRank EltRank = getFloatingRank(Size);
1826 if (Domain->isComplexType()) {
1827 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001828 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001829 case FloatRank: return FloatComplexTy;
1830 case DoubleRank: return DoubleComplexTy;
1831 case LongDoubleRank: return LongDoubleComplexTy;
1832 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001833 }
Chris Lattner1361b112008-04-06 23:58:54 +00001834
1835 assert(Domain->isRealFloatingType() && "Unknown domain!");
1836 switch (EltRank) {
1837 default: assert(0 && "getFloatingRank(): illegal value for rank");
1838 case FloatRank: return FloatTy;
1839 case DoubleRank: return DoubleTy;
1840 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001841 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001842}
1843
Chris Lattner7cfeb082008-04-06 23:55:33 +00001844/// getFloatingTypeOrder - Compare the rank of the two specified floating
1845/// point types, ignoring the domain of the type (i.e. 'double' ==
1846/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1847/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001848int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1849 FloatingRank LHSR = getFloatingRank(LHS);
1850 FloatingRank RHSR = getFloatingRank(RHS);
1851
1852 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001853 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001854 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001855 return 1;
1856 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001857}
1858
Chris Lattnerf52ab252008-04-06 22:59:24 +00001859/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1860/// routine will assert if passed a built-in type that isn't an integer or enum,
1861/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001862unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001863 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001864 if (EnumType* ET = dyn_cast<EnumType>(T))
1865 T = ET->getDecl()->getIntegerType().getTypePtr();
1866
1867 // There are two things which impact the integer rank: the width, and
1868 // the ordering of builtins. The builtin ordering is encoded in the
1869 // bottom three bits; the width is encoded in the bits above that.
1870 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1871 return FWIT->getWidth() << 3;
1872 }
1873
Chris Lattnerf52ab252008-04-06 22:59:24 +00001874 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001875 default: assert(0 && "getIntegerRank(): not a built-in integer");
1876 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001877 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001878 case BuiltinType::Char_S:
1879 case BuiltinType::Char_U:
1880 case BuiltinType::SChar:
1881 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001882 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001883 case BuiltinType::Short:
1884 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001885 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001886 case BuiltinType::Int:
1887 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001888 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001889 case BuiltinType::Long:
1890 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001891 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001892 case BuiltinType::LongLong:
1893 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001894 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001895 }
1896}
1897
Chris Lattner7cfeb082008-04-06 23:55:33 +00001898/// getIntegerTypeOrder - Returns the highest ranked integer type:
1899/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1900/// LHS < RHS, return -1.
1901int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001902 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1903 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001904 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001905
Chris Lattnerf52ab252008-04-06 22:59:24 +00001906 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1907 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001908
Chris Lattner7cfeb082008-04-06 23:55:33 +00001909 unsigned LHSRank = getIntegerRank(LHSC);
1910 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001911
Chris Lattner7cfeb082008-04-06 23:55:33 +00001912 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1913 if (LHSRank == RHSRank) return 0;
1914 return LHSRank > RHSRank ? 1 : -1;
1915 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001916
Chris Lattner7cfeb082008-04-06 23:55:33 +00001917 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1918 if (LHSUnsigned) {
1919 // If the unsigned [LHS] type is larger, return it.
1920 if (LHSRank >= RHSRank)
1921 return 1;
1922
1923 // If the signed type can represent all values of the unsigned type, it
1924 // wins. Because we are dealing with 2's complement and types that are
1925 // powers of two larger than each other, this is always safe.
1926 return -1;
1927 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001928
Chris Lattner7cfeb082008-04-06 23:55:33 +00001929 // If the unsigned [RHS] type is larger, return it.
1930 if (RHSRank >= LHSRank)
1931 return -1;
1932
1933 // If the signed type can represent all values of the unsigned type, it
1934 // wins. Because we are dealing with 2's complement and types that are
1935 // powers of two larger than each other, this is always safe.
1936 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001937}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001938
1939// getCFConstantStringType - Return the type used for constant CFStrings.
1940QualType ASTContext::getCFConstantStringType() {
1941 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001942 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001943 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001944 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001945 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001946
1947 // const int *isa;
1948 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001949 // int flags;
1950 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001951 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001952 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001953 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001954 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001955
Anders Carlsson71993dd2007-08-17 05:31:46 +00001956 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001957 for (unsigned i = 0; i < 4; ++i) {
1958 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1959 SourceLocation(), 0,
1960 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001961 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001962 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001963 }
1964
1965 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001966 }
1967
1968 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001969}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001970
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001971QualType ASTContext::getObjCFastEnumerationStateType()
1972{
1973 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001974 ObjCFastEnumerationStateTypeDecl =
1975 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1976 &Idents.get("__objcFastEnumerationState"));
1977
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001978 QualType FieldTypes[] = {
1979 UnsignedLongTy,
1980 getPointerType(ObjCIdType),
1981 getPointerType(UnsignedLongTy),
1982 getConstantArrayType(UnsignedLongTy,
1983 llvm::APInt(32, 5), ArrayType::Normal, 0)
1984 };
1985
Douglas Gregor44b43212008-12-11 16:49:14 +00001986 for (size_t i = 0; i < 4; ++i) {
1987 FieldDecl *Field = FieldDecl::Create(*this,
1988 ObjCFastEnumerationStateTypeDecl,
1989 SourceLocation(), 0,
1990 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001991 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001992 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001993 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001994
Douglas Gregor44b43212008-12-11 16:49:14 +00001995 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001996 }
1997
1998 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1999}
2000
Anders Carlssone8c49532007-10-29 06:33:42 +00002001// This returns true if a type has been typedefed to BOOL:
2002// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002003static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002004 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002005 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2006 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002007
2008 return false;
2009}
2010
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002011/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002012/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002013int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002014 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002015
2016 // Make all integer and enum types at least as large as an int
2017 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002018 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002019 // Treat arrays as pointers, since that's how they're passed in.
2020 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002021 sz = getTypeSize(VoidPtrTy);
2022 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002023}
2024
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002025/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002026/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002027void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002028 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002029 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002030 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002031 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002032 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002033 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002034 // Compute size of all parameters.
2035 // Start with computing size of a pointer in number of bytes.
2036 // FIXME: There might(should) be a better way of doing this computation!
2037 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002038 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002039 // The first two arguments (self and _cmd) are pointers; account for
2040 // their size.
2041 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002042 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2043 E = Decl->param_end(); PI != E; ++PI) {
2044 QualType PType = (*PI)->getType();
2045 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002046 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002047 ParmOffset += sz;
2048 }
2049 S += llvm::utostr(ParmOffset);
2050 S += "@0:";
2051 S += llvm::utostr(PtrSize);
2052
2053 // Argument types.
2054 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002055 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2056 E = Decl->param_end(); PI != E; ++PI) {
2057 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002058 QualType PType = PVDecl->getOriginalType();
2059 if (const ArrayType *AT =
2060 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
2061 // Use array's original type only if it has known number of
2062 // elements.
2063 if (!dyn_cast<ConstantArrayType>(AT))
2064 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002065 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002066 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002067 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002068 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002069 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002070 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002071 }
2072}
2073
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002074/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002075/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002076/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2077/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002078/// Property attributes are stored as a comma-delimited C string. The simple
2079/// attributes readonly and bycopy are encoded as single characters. The
2080/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2081/// encoded as single characters, followed by an identifier. Property types
2082/// are also encoded as a parametrized attribute. The characters used to encode
2083/// these attributes are defined by the following enumeration:
2084/// @code
2085/// enum PropertyAttributes {
2086/// kPropertyReadOnly = 'R', // property is read-only.
2087/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2088/// kPropertyByref = '&', // property is a reference to the value last assigned
2089/// kPropertyDynamic = 'D', // property is dynamic
2090/// kPropertyGetter = 'G', // followed by getter selector name
2091/// kPropertySetter = 'S', // followed by setter selector name
2092/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2093/// kPropertyType = 't' // followed by old-style type encoding.
2094/// kPropertyWeak = 'W' // 'weak' property
2095/// kPropertyStrong = 'P' // property GC'able
2096/// kPropertyNonAtomic = 'N' // property non-atomic
2097/// };
2098/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002099void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2100 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002101 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002102 // Collect information from the property implementation decl(s).
2103 bool Dynamic = false;
2104 ObjCPropertyImplDecl *SynthesizePID = 0;
2105
2106 // FIXME: Duplicated code due to poor abstraction.
2107 if (Container) {
2108 if (const ObjCCategoryImplDecl *CID =
2109 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2110 for (ObjCCategoryImplDecl::propimpl_iterator
2111 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2112 ObjCPropertyImplDecl *PID = *i;
2113 if (PID->getPropertyDecl() == PD) {
2114 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2115 Dynamic = true;
2116 } else {
2117 SynthesizePID = PID;
2118 }
2119 }
2120 }
2121 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002122 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002123 for (ObjCCategoryImplDecl::propimpl_iterator
2124 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2125 ObjCPropertyImplDecl *PID = *i;
2126 if (PID->getPropertyDecl() == PD) {
2127 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2128 Dynamic = true;
2129 } else {
2130 SynthesizePID = PID;
2131 }
2132 }
2133 }
2134 }
2135 }
2136
2137 // FIXME: This is not very efficient.
2138 S = "T";
2139
2140 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002141 // GCC has some special rules regarding encoding of properties which
2142 // closely resembles encoding of ivars.
2143 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2144 true /* outermost type */,
2145 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002146
2147 if (PD->isReadOnly()) {
2148 S += ",R";
2149 } else {
2150 switch (PD->getSetterKind()) {
2151 case ObjCPropertyDecl::Assign: break;
2152 case ObjCPropertyDecl::Copy: S += ",C"; break;
2153 case ObjCPropertyDecl::Retain: S += ",&"; break;
2154 }
2155 }
2156
2157 // It really isn't clear at all what this means, since properties
2158 // are "dynamic by default".
2159 if (Dynamic)
2160 S += ",D";
2161
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002162 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2163 S += ",N";
2164
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002165 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2166 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002167 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002168 }
2169
2170 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2171 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002172 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002173 }
2174
2175 if (SynthesizePID) {
2176 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2177 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002178 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002179 }
2180
2181 // FIXME: OBJCGC: weak & strong
2182}
2183
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002184/// getLegacyIntegralTypeEncoding -
2185/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002186/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002187/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2188///
2189void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2190 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2191 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002192 if (BT->getKind() == BuiltinType::ULong &&
2193 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002194 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002195 else
2196 if (BT->getKind() == BuiltinType::Long &&
2197 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002198 PointeeTy = IntTy;
2199 }
2200 }
2201}
2202
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002203void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002204 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002205 // We follow the behavior of gcc, expanding structures which are
2206 // directly pointed to, and expanding embedded structures. Note that
2207 // these rules are sufficient to prevent recursive encoding of the
2208 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002209 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2210 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002211}
2212
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002213static void EncodeBitField(const ASTContext *Context, std::string& S,
2214 FieldDecl *FD) {
2215 const Expr *E = FD->getBitWidth();
2216 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2217 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2218 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2219 S += 'b';
2220 S += llvm::utostr(N);
2221}
2222
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002223void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2224 bool ExpandPointedToStructures,
2225 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002226 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002227 bool OutermostType,
2228 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00002229 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002230 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002231 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002232 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002233 else {
2234 char encoding;
2235 switch (BT->getKind()) {
2236 default: assert(0 && "Unhandled builtin type kind");
2237 case BuiltinType::Void: encoding = 'v'; break;
2238 case BuiltinType::Bool: encoding = 'B'; break;
2239 case BuiltinType::Char_U:
2240 case BuiltinType::UChar: encoding = 'C'; break;
2241 case BuiltinType::UShort: encoding = 'S'; break;
2242 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002243 case BuiltinType::ULong:
2244 encoding =
2245 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2246 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002247 case BuiltinType::ULongLong: encoding = 'Q'; break;
2248 case BuiltinType::Char_S:
2249 case BuiltinType::SChar: encoding = 'c'; break;
2250 case BuiltinType::Short: encoding = 's'; break;
2251 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002252 case BuiltinType::Long:
2253 encoding =
2254 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2255 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002256 case BuiltinType::LongLong: encoding = 'q'; break;
2257 case BuiltinType::Float: encoding = 'f'; break;
2258 case BuiltinType::Double: encoding = 'd'; break;
2259 case BuiltinType::LongDouble: encoding = 'd'; break;
2260 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002261
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002262 S += encoding;
2263 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002264 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002265 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002266 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2267 ExpandPointedToStructures,
2268 ExpandStructures, FD);
2269 if (FD || EncodingProperty) {
2270 // Note that we do extended encoding of protocol qualifer list
2271 // Only when doing ivar or property encoding.
2272 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2273 S += '"';
2274 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2275 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2276 S += '<';
2277 S += Proto->getNameAsString();
2278 S += '>';
2279 }
2280 S += '"';
2281 }
2282 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002283 }
2284 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002285 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002286 bool isReadOnly = false;
2287 // For historical/compatibility reasons, the read-only qualifier of the
2288 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2289 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2290 // Also, do not emit the 'r' for anything but the outermost type!
2291 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2292 if (OutermostType && T.isConstQualified()) {
2293 isReadOnly = true;
2294 S += 'r';
2295 }
2296 }
2297 else if (OutermostType) {
2298 QualType P = PointeeTy;
2299 while (P->getAsPointerType())
2300 P = P->getAsPointerType()->getPointeeType();
2301 if (P.isConstQualified()) {
2302 isReadOnly = true;
2303 S += 'r';
2304 }
2305 }
2306 if (isReadOnly) {
2307 // Another legacy compatibility encoding. Some ObjC qualifier and type
2308 // combinations need to be rearranged.
2309 // Rewrite "in const" from "nr" to "rn"
2310 const char * s = S.c_str();
2311 int len = S.length();
2312 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2313 std::string replace = "rn";
2314 S.replace(S.end()-2, S.end(), replace);
2315 }
2316 }
Steve Naroff389bf462009-02-12 17:52:19 +00002317 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002318 S += '@';
2319 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002320 }
2321 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002322 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002323 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002324 // Another historical/compatibility reason.
2325 // We encode the underlying type which comes out as
2326 // {...};
2327 S += '^';
2328 getObjCEncodingForTypeImpl(PointeeTy, S,
2329 false, ExpandPointedToStructures,
2330 NULL);
2331 return;
2332 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002333 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002334 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002335 const ObjCInterfaceType *OIT =
2336 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002337 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002338 S += '"';
2339 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002340 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2341 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2342 S += '<';
2343 S += Proto->getNameAsString();
2344 S += '>';
2345 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002346 S += '"';
2347 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002348 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002349 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002350 S += '#';
2351 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002352 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002353 S += ':';
2354 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002355 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002356
2357 if (PointeeTy->isCharType()) {
2358 // char pointer types should be encoded as '*' unless it is a
2359 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002360 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002361 S += '*';
2362 return;
2363 }
2364 }
2365
2366 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002367 getLegacyIntegralTypeEncoding(PointeeTy);
2368
2369 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002370 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002371 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002372 } else if (const ArrayType *AT =
2373 // Ignore type qualifiers etc.
2374 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002375 if (isa<IncompleteArrayType>(AT)) {
2376 // Incomplete arrays are encoded as a pointer to the array element.
2377 S += '^';
2378
2379 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2380 false, ExpandStructures, FD);
2381 } else {
2382 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002383
Anders Carlsson559a8332009-02-22 01:38:57 +00002384 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2385 S += llvm::utostr(CAT->getSize().getZExtValue());
2386 else {
2387 //Variable length arrays are encoded as a regular array with 0 elements.
2388 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2389 S += '0';
2390 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002391
Anders Carlsson559a8332009-02-22 01:38:57 +00002392 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2393 false, ExpandStructures, FD);
2394 S += ']';
2395 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002396 } else if (T->getAsFunctionType()) {
2397 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002398 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002399 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002400 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002401 // Anonymous structures print as '?'
2402 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2403 S += II->getName();
2404 } else {
2405 S += '?';
2406 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002407 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002408 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002409 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2410 FieldEnd = RDecl->field_end();
2411 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002412 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002413 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002414 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002415 S += '"';
2416 }
2417
2418 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002419 if (Field->isBitField()) {
2420 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2421 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002422 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002423 QualType qt = Field->getType();
2424 getLegacyIntegralTypeEncoding(qt);
2425 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002426 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002427 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002428 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002429 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002430 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002431 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002432 if (FD && FD->isBitField())
2433 EncodeBitField(this, S, FD);
2434 else
2435 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002436 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002437 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002438 } else if (T->isObjCInterfaceType()) {
2439 // @encode(class_name)
2440 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2441 S += '{';
2442 const IdentifierInfo *II = OI->getIdentifier();
2443 S += II->getName();
2444 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002445 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002446 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002447 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002448 if (RecFields[i]->isBitField())
2449 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2450 RecFields[i]);
2451 else
2452 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2453 FD);
2454 }
2455 S += '}';
2456 }
2457 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002458 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002459}
2460
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002461void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002462 std::string& S) const {
2463 if (QT & Decl::OBJC_TQ_In)
2464 S += 'n';
2465 if (QT & Decl::OBJC_TQ_Inout)
2466 S += 'N';
2467 if (QT & Decl::OBJC_TQ_Out)
2468 S += 'o';
2469 if (QT & Decl::OBJC_TQ_Bycopy)
2470 S += 'O';
2471 if (QT & Decl::OBJC_TQ_Byref)
2472 S += 'R';
2473 if (QT & Decl::OBJC_TQ_Oneway)
2474 S += 'V';
2475}
2476
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002477void ASTContext::setBuiltinVaListType(QualType T)
2478{
2479 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2480
2481 BuiltinVaListType = T;
2482}
2483
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002484void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002485{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002486 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002487
2488 // typedef struct objc_object *id;
2489 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002490 // User error - caller will issue diagnostics.
2491 if (!ptr)
2492 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002493 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002494 // User error - caller will issue diagnostics.
2495 if (!rec)
2496 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002497 IdStructType = rec;
2498}
2499
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002500void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002501{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002502 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002503
2504 // typedef struct objc_selector *SEL;
2505 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002506 if (!ptr)
2507 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002508 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002509 if (!rec)
2510 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002511 SelStructType = rec;
2512}
2513
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002514void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002515{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002516 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002517}
2518
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002519void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002520{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002521 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002522
2523 // typedef struct objc_class *Class;
2524 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2525 assert(ptr && "'Class' incorrectly typed");
2526 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2527 assert(rec && "'Class' incorrectly typed");
2528 ClassStructType = rec;
2529}
2530
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002531void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2532 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002533 "'NSConstantString' type already set!");
2534
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002535 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002536}
2537
Douglas Gregor7532dc62009-03-30 22:58:21 +00002538/// \brief Retrieve the template name that represents a qualified
2539/// template name such as \c std::vector.
2540TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2541 bool TemplateKeyword,
2542 TemplateDecl *Template) {
2543 llvm::FoldingSetNodeID ID;
2544 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2545
2546 void *InsertPos = 0;
2547 QualifiedTemplateName *QTN =
2548 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2549 if (!QTN) {
2550 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2551 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2552 }
2553
2554 return TemplateName(QTN);
2555}
2556
2557/// \brief Retrieve the template name that represents a dependent
2558/// template name such as \c MetaFun::template apply.
2559TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2560 const IdentifierInfo *Name) {
2561 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2562
2563 llvm::FoldingSetNodeID ID;
2564 DependentTemplateName::Profile(ID, NNS, Name);
2565
2566 void *InsertPos = 0;
2567 DependentTemplateName *QTN =
2568 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2569
2570 if (QTN)
2571 return TemplateName(QTN);
2572
2573 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2574 if (CanonNNS == NNS) {
2575 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2576 } else {
2577 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2578 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2579 }
2580
2581 DependentTemplateNames.InsertNode(QTN, InsertPos);
2582 return TemplateName(QTN);
2583}
2584
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002585/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002586/// TargetInfo, produce the corresponding type. The unsigned @p Type
2587/// is actually a value of type @c TargetInfo::IntType.
2588QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002589 switch (Type) {
2590 case TargetInfo::NoInt: return QualType();
2591 case TargetInfo::SignedShort: return ShortTy;
2592 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2593 case TargetInfo::SignedInt: return IntTy;
2594 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2595 case TargetInfo::SignedLong: return LongTy;
2596 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2597 case TargetInfo::SignedLongLong: return LongLongTy;
2598 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2599 }
2600
2601 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002602 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002603}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002604
2605//===----------------------------------------------------------------------===//
2606// Type Predicates.
2607//===----------------------------------------------------------------------===//
2608
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002609/// isObjCNSObjectType - Return true if this is an NSObject object using
2610/// NSObject attribute on a c-style pointer type.
2611/// FIXME - Make it work directly on types.
2612///
2613bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2614 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2615 if (TypedefDecl *TD = TDT->getDecl())
2616 if (TD->getAttr<ObjCNSObjectAttr>())
2617 return true;
2618 }
2619 return false;
2620}
2621
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002622/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2623/// to an object type. This includes "id" and "Class" (two 'special' pointers
2624/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2625/// ID type).
2626bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002627 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002628 return true;
2629
Steve Naroff6ae98502008-10-21 18:24:04 +00002630 // Blocks are objects.
2631 if (Ty->isBlockPointerType())
2632 return true;
2633
2634 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002635 if (!Ty->isPointerType())
2636 return false;
2637
2638 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2639 // pointer types. This looks for the typedef specifically, not for the
2640 // underlying type.
Eli Friedman5fdeae12009-03-22 23:00:19 +00002641 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2642 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002643 return true;
2644
2645 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002646 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2647 return true;
2648
2649 // If is has NSObject attribute, OK as well.
2650 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002651}
2652
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002653/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2654/// garbage collection attribute.
2655///
2656QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002657 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002658 if (getLangOptions().ObjC1 &&
2659 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002660 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002661 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002662 // (or pointers to them) be treated as though they were declared
2663 // as __strong.
2664 if (GCAttrs == QualType::GCNone) {
2665 if (isObjCObjectPointerType(Ty))
2666 GCAttrs = QualType::Strong;
2667 else if (Ty->isPointerType())
2668 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2669 }
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002670 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002671 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002672}
2673
Chris Lattner6ac46a42008-04-07 06:51:04 +00002674//===----------------------------------------------------------------------===//
2675// Type Compatibility Testing
2676//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002677
Steve Naroff1c7d0672008-09-04 15:10:53 +00002678/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002679/// block types. Types must be strictly compatible here. For example,
2680/// C unfortunately doesn't produce an error for the following:
2681///
2682/// int (*emptyArgFunc)();
2683/// int (*intArgList)(int) = emptyArgFunc;
2684///
2685/// For blocks, we will produce an error for the following (similar to C++):
2686///
2687/// int (^emptyArgBlock)();
2688/// int (^intArgBlock)(int) = emptyArgBlock;
2689///
2690/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2691///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002692bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002693 const FunctionType *lbase = lhs->getAsFunctionType();
2694 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002695 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2696 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002697 if (lproto && rproto == 0)
2698 return false;
2699 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002700}
2701
Chris Lattner6ac46a42008-04-07 06:51:04 +00002702/// areCompatVectorTypes - Return true if the two specified vector types are
2703/// compatible.
2704static bool areCompatVectorTypes(const VectorType *LHS,
2705 const VectorType *RHS) {
2706 assert(LHS->isCanonical() && RHS->isCanonical());
2707 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002708 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002709}
2710
Eli Friedman3d815e72008-08-22 00:56:42 +00002711/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002712/// compatible for assignment from RHS to LHS. This handles validation of any
2713/// protocol qualifiers on the LHS or RHS.
2714///
Eli Friedman3d815e72008-08-22 00:56:42 +00002715bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2716 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002717 // Verify that the base decls are compatible: the RHS must be a subclass of
2718 // the LHS.
2719 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2720 return false;
2721
2722 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2723 // protocol qualified at all, then we are good.
2724 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2725 return true;
2726
2727 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2728 // isn't a superset.
2729 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2730 return true; // FIXME: should return false!
2731
2732 // Finally, we must have two protocol-qualified interfaces.
2733 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2734 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002735
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002736 // All LHS protocols must have a presence on the RHS.
2737 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002738
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002739 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2740 LHSPE = LHSP->qual_end();
2741 LHSPI != LHSPE; LHSPI++) {
2742 bool RHSImplementsProtocol = false;
2743
2744 // If the RHS doesn't implement the protocol on the left, the types
2745 // are incompatible.
2746 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2747 RHSPE = RHSP->qual_end();
2748 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2749 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2750 RHSImplementsProtocol = true;
2751 }
2752 // FIXME: For better diagnostics, consider passing back the protocol name.
2753 if (!RHSImplementsProtocol)
2754 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002755 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002756 // The RHS implements all protocols listed on the LHS.
2757 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002758}
2759
Steve Naroff389bf462009-02-12 17:52:19 +00002760bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2761 // get the "pointed to" types
2762 const PointerType *LHSPT = LHS->getAsPointerType();
2763 const PointerType *RHSPT = RHS->getAsPointerType();
2764
2765 if (!LHSPT || !RHSPT)
2766 return false;
2767
2768 QualType lhptee = LHSPT->getPointeeType();
2769 QualType rhptee = RHSPT->getPointeeType();
2770 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2771 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2772 // ID acts sort of like void* for ObjC interfaces
2773 if (LHSIface && isObjCIdStructType(rhptee))
2774 return true;
2775 if (RHSIface && isObjCIdStructType(lhptee))
2776 return true;
2777 if (!LHSIface || !RHSIface)
2778 return false;
2779 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2780 canAssignObjCInterfaces(RHSIface, LHSIface);
2781}
2782
Steve Naroffec0550f2007-10-15 20:41:53 +00002783/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2784/// both shall have the identically qualified version of a compatible type.
2785/// C99 6.2.7p1: Two types have compatible types if their types are the
2786/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002787bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2788 return !mergeTypes(LHS, RHS).isNull();
2789}
2790
2791QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2792 const FunctionType *lbase = lhs->getAsFunctionType();
2793 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002794 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2795 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002796 bool allLTypes = true;
2797 bool allRTypes = true;
2798
2799 // Check return type
2800 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2801 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002802 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2803 allLTypes = false;
2804 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2805 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002806
2807 if (lproto && rproto) { // two C99 style function prototypes
2808 unsigned lproto_nargs = lproto->getNumArgs();
2809 unsigned rproto_nargs = rproto->getNumArgs();
2810
2811 // Compatible functions must have the same number of arguments
2812 if (lproto_nargs != rproto_nargs)
2813 return QualType();
2814
2815 // Variadic and non-variadic functions aren't compatible
2816 if (lproto->isVariadic() != rproto->isVariadic())
2817 return QualType();
2818
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002819 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2820 return QualType();
2821
Eli Friedman3d815e72008-08-22 00:56:42 +00002822 // Check argument compatibility
2823 llvm::SmallVector<QualType, 10> types;
2824 for (unsigned i = 0; i < lproto_nargs; i++) {
2825 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2826 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2827 QualType argtype = mergeTypes(largtype, rargtype);
2828 if (argtype.isNull()) return QualType();
2829 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002830 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2831 allLTypes = false;
2832 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2833 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002834 }
2835 if (allLTypes) return lhs;
2836 if (allRTypes) return rhs;
2837 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002838 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002839 }
2840
2841 if (lproto) allRTypes = false;
2842 if (rproto) allLTypes = false;
2843
Douglas Gregor72564e72009-02-26 23:50:07 +00002844 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002845 if (proto) {
2846 if (proto->isVariadic()) return QualType();
2847 // Check that the types are compatible with the types that
2848 // would result from default argument promotions (C99 6.7.5.3p15).
2849 // The only types actually affected are promotable integer
2850 // types and floats, which would be passed as a different
2851 // type depending on whether the prototype is visible.
2852 unsigned proto_nargs = proto->getNumArgs();
2853 for (unsigned i = 0; i < proto_nargs; ++i) {
2854 QualType argTy = proto->getArgType(i);
2855 if (argTy->isPromotableIntegerType() ||
2856 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2857 return QualType();
2858 }
2859
2860 if (allLTypes) return lhs;
2861 if (allRTypes) return rhs;
2862 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002863 proto->getNumArgs(), lproto->isVariadic(),
2864 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002865 }
2866
2867 if (allLTypes) return lhs;
2868 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002869 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002870}
2871
2872QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002873 // C++ [expr]: If an expression initially has the type "reference to T", the
2874 // type is adjusted to "T" prior to any further analysis, the expression
2875 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002876 // expression is an lvalue unless the reference is an rvalue reference and
2877 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002878 // FIXME: C++ shouldn't be going through here! The rules are different
2879 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002880 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2881 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002882 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002883 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002884 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002885 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002886
Eli Friedman3d815e72008-08-22 00:56:42 +00002887 QualType LHSCan = getCanonicalType(LHS),
2888 RHSCan = getCanonicalType(RHS);
2889
2890 // If two types are identical, they are compatible.
2891 if (LHSCan == RHSCan)
2892 return LHS;
2893
2894 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002895 // Note that we handle extended qualifiers later, in the
2896 // case for ExtQualType.
2897 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002898 return QualType();
2899
2900 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2901 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2902
Chris Lattner1adb8832008-01-14 05:45:46 +00002903 // We want to consider the two function types to be the same for these
2904 // comparisons, just force one to the other.
2905 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2906 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002907
2908 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002909 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2910 LHSClass = Type::ConstantArray;
2911 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2912 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002913
Nate Begeman213541a2008-04-18 23:10:10 +00002914 // Canonicalize ExtVector -> Vector.
2915 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2916 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002917
Chris Lattnerb0489812008-04-07 06:38:24 +00002918 // Consider qualified interfaces and interfaces the same.
2919 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2920 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002921
Chris Lattnera36a61f2008-04-07 05:43:21 +00002922 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002923 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002924 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2925 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2926
2927 // ID acts sort of like void* for ObjC interfaces
2928 if (LHSIface && isObjCIdStructType(RHS))
2929 return LHS;
2930 if (RHSIface && isObjCIdStructType(LHS))
2931 return RHS;
2932
Steve Naroffbc76dd02008-12-10 22:14:21 +00002933 // ID is compatible with all qualified id types.
2934 if (LHS->isObjCQualifiedIdType()) {
2935 if (const PointerType *PT = RHS->getAsPointerType()) {
2936 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002937 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002938 return LHS;
2939 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2940 // Unfortunately, this API is part of Sema (which we don't have access
2941 // to. Need to refactor. The following check is insufficient, since we
2942 // need to make sure the class implements the protocol.
2943 if (pType->isObjCInterfaceType())
2944 return LHS;
2945 }
2946 }
2947 if (RHS->isObjCQualifiedIdType()) {
2948 if (const PointerType *PT = LHS->getAsPointerType()) {
2949 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002950 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002951 return RHS;
2952 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2953 // Unfortunately, this API is part of Sema (which we don't have access
2954 // to. Need to refactor. The following check is insufficient, since we
2955 // need to make sure the class implements the protocol.
2956 if (pType->isObjCInterfaceType())
2957 return RHS;
2958 }
2959 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002960 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2961 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002962 if (const EnumType* ETy = LHS->getAsEnumType()) {
2963 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2964 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002965 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002966 if (const EnumType* ETy = RHS->getAsEnumType()) {
2967 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2968 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002969 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002970
Eli Friedman3d815e72008-08-22 00:56:42 +00002971 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002972 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002973
Steve Naroff4a746782008-01-09 22:43:08 +00002974 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002975 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00002976#define TYPE(Class, Base)
2977#define ABSTRACT_TYPE(Class, Base)
2978#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2979#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2980#include "clang/AST/TypeNodes.def"
2981 assert(false && "Non-canonical and dependent types shouldn't get here");
2982 return QualType();
2983
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002984 case Type::LValueReference:
2985 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00002986 case Type::MemberPointer:
2987 assert(false && "C++ should never be in mergeTypes");
2988 return QualType();
2989
2990 case Type::IncompleteArray:
2991 case Type::VariableArray:
2992 case Type::FunctionProto:
2993 case Type::ExtVector:
2994 case Type::ObjCQualifiedInterface:
2995 assert(false && "Types are eliminated above");
2996 return QualType();
2997
Chris Lattner1adb8832008-01-14 05:45:46 +00002998 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002999 {
3000 // Merge two pointer types, while trying to preserve typedef info
3001 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3002 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3003 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3004 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003005 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3006 return LHS;
3007 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3008 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003009 return getPointerType(ResultType);
3010 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003011 case Type::BlockPointer:
3012 {
3013 // Merge two block pointer types, while trying to preserve typedef info
3014 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3015 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3016 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3017 if (ResultType.isNull()) return QualType();
3018 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3019 return LHS;
3020 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3021 return RHS;
3022 return getBlockPointerType(ResultType);
3023 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003024 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003025 {
3026 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3027 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3028 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3029 return QualType();
3030
3031 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3032 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3033 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3034 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003035 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3036 return LHS;
3037 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3038 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003039 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3040 ArrayType::ArraySizeModifier(), 0);
3041 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3042 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003043 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3044 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003045 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3046 return LHS;
3047 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3048 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003049 if (LVAT) {
3050 // FIXME: This isn't correct! But tricky to implement because
3051 // the array's size has to be the size of LHS, but the type
3052 // has to be different.
3053 return LHS;
3054 }
3055 if (RVAT) {
3056 // FIXME: This isn't correct! But tricky to implement because
3057 // the array's size has to be the size of RHS, but the type
3058 // has to be different.
3059 return RHS;
3060 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003061 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3062 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003063 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003064 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003065 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003066 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003067 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003068 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003069 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003070 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3071 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003072 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003073 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003074 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003075 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003076 case Type::Complex:
3077 // Distinct complex types are incompatible.
3078 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003079 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003080 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003081 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3082 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003083 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003084 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003085 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003086 // FIXME: This should be type compatibility, e.g. whether
3087 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003088 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3089 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3090 if (LHSIface && RHSIface &&
3091 canAssignObjCInterfaces(LHSIface, RHSIface))
3092 return LHS;
3093
Eli Friedman3d815e72008-08-22 00:56:42 +00003094 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003095 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003096 case Type::ObjCQualifiedId:
3097 // Distinct qualified id's are not compatible.
3098 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003099 case Type::FixedWidthInt:
3100 // Distinct fixed-width integers are not compatible.
3101 return QualType();
3102 case Type::ObjCQualifiedClass:
3103 // Distinct qualified classes are not compatible.
3104 return QualType();
3105 case Type::ExtQual:
3106 // FIXME: ExtQual types can be compatible even if they're not
3107 // identical!
3108 return QualType();
3109 // First attempt at an implementation, but I'm not really sure it's
3110 // right...
3111#if 0
3112 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3113 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3114 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3115 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3116 return QualType();
3117 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3118 LHSBase = QualType(LQual->getBaseType(), 0);
3119 RHSBase = QualType(RQual->getBaseType(), 0);
3120 ResultType = mergeTypes(LHSBase, RHSBase);
3121 if (ResultType.isNull()) return QualType();
3122 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3123 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3124 return LHS;
3125 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3126 return RHS;
3127 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3128 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3129 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3130 return ResultType;
3131#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003132
3133 case Type::TemplateSpecialization:
3134 assert(false && "Dependent types have no size");
3135 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003136 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003137
3138 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003139}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003140
Chris Lattner5426bf62008-04-07 07:01:58 +00003141//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003142// Integer Predicates
3143//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003144
Eli Friedmanad74a752008-06-28 06:23:08 +00003145unsigned ASTContext::getIntWidth(QualType T) {
3146 if (T == BoolTy)
3147 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003148 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3149 return FWIT->getWidth();
3150 }
3151 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003152 return (unsigned)getTypeSize(T);
3153}
3154
3155QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3156 assert(T->isSignedIntegerType() && "Unexpected type");
3157 if (const EnumType* ETy = T->getAsEnumType())
3158 T = ETy->getDecl()->getIntegerType();
3159 const BuiltinType* BTy = T->getAsBuiltinType();
3160 assert (BTy && "Unexpected signed integer type");
3161 switch (BTy->getKind()) {
3162 case BuiltinType::Char_S:
3163 case BuiltinType::SChar:
3164 return UnsignedCharTy;
3165 case BuiltinType::Short:
3166 return UnsignedShortTy;
3167 case BuiltinType::Int:
3168 return UnsignedIntTy;
3169 case BuiltinType::Long:
3170 return UnsignedLongTy;
3171 case BuiltinType::LongLong:
3172 return UnsignedLongLongTy;
3173 default:
3174 assert(0 && "Unexpected signed integer type");
3175 return QualType();
3176 }
3177}
3178
3179
3180//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00003181// Serialization Support
3182//===----------------------------------------------------------------------===//
3183
Chris Lattnera9376d42009-03-28 03:45:20 +00003184enum {
3185 BasicMetadataBlock = 1,
3186 ASTContextBlock = 2,
3187 DeclsBlock = 3
3188};
3189
Chris Lattner557c5b12009-03-28 04:27:18 +00003190void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3191 // Create bitstream.
3192 llvm::BitstreamWriter Stream(Buffer);
3193
3194 // Emit the preamble.
3195 Stream.Emit((unsigned)'B', 8);
3196 Stream.Emit((unsigned)'C', 8);
3197 Stream.Emit(0xC, 4);
3198 Stream.Emit(0xF, 4);
3199 Stream.Emit(0xE, 4);
3200 Stream.Emit(0x0, 4);
3201
3202 // Create serializer.
3203 llvm::Serializer S(Stream);
3204
Chris Lattnera9376d42009-03-28 03:45:20 +00003205 // ===---------------------------------------------------===/
3206 // Serialize the "Translation Unit" metadata.
3207 // ===---------------------------------------------------===/
3208
3209 // Emit ASTContext.
3210 S.EnterBlock(ASTContextBlock);
3211 S.EmitOwnedPtr(this);
3212 S.ExitBlock(); // exit "ASTContextBlock"
3213
3214 S.EnterBlock(BasicMetadataBlock);
3215
3216 // Block for SourceManager and Target. Allows easy skipping
3217 // around to the block for the Selectors during deserialization.
3218 S.EnterBlock();
3219
3220 // Emit the SourceManager.
3221 S.Emit(getSourceManager());
3222
3223 // Emit the Target.
3224 S.EmitPtr(&Target);
3225 S.EmitCStr(Target.getTargetTriple());
3226
3227 S.ExitBlock(); // exit "SourceManager and Target Block"
3228
3229 // Emit the Selectors.
3230 S.Emit(Selectors);
3231
3232 // Emit the Identifier Table.
3233 S.Emit(Idents);
3234
3235 S.ExitBlock(); // exit "BasicMetadataBlock"
3236}
3237
3238
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003239/// Emit - Serialize an ASTContext object to Bitcode.
3240void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003241 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00003242 S.EmitRef(SourceMgr);
3243 S.EmitRef(Target);
3244 S.EmitRef(Idents);
3245 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003246
Ted Kremenekfee04522007-10-31 22:44:07 +00003247 // Emit the size of the type vector so that we can reserve that size
3248 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00003249 S.EmitInt(Types.size());
3250
Ted Kremenek03ed4402007-11-13 22:02:55 +00003251 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3252 I!=E;++I)
3253 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00003254
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003255 S.EmitOwnedPtr(TUDecl);
3256
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003257 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003258}
3259
Chris Lattner557c5b12009-03-28 04:27:18 +00003260
3261ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3262 FileManager &FMgr) {
3263 // Check if the file is of the proper length.
3264 if (Buffer.getBufferSize() & 0x3) {
3265 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3266 return 0;
3267 }
3268
3269 // Create the bitstream reader.
3270 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3271 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3272
3273 if (Stream.Read(8) != 'B' ||
3274 Stream.Read(8) != 'C' ||
3275 Stream.Read(4) != 0xC ||
3276 Stream.Read(4) != 0xF ||
3277 Stream.Read(4) != 0xE ||
3278 Stream.Read(4) != 0x0) {
3279 // FIXME: Provide diagnostic.
3280 return NULL;
3281 }
3282
3283 // Create the deserializer.
3284 llvm::Deserializer Dezr(Stream);
3285
Chris Lattnera9376d42009-03-28 03:45:20 +00003286 // ===---------------------------------------------------===/
3287 // Deserialize the "Translation Unit" metadata.
3288 // ===---------------------------------------------------===/
3289
3290 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3291 // (which will appear earlier) and record its location.
3292
3293 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3294 assert (FoundBlock);
3295
3296 llvm::Deserializer::Location ASTContextBlockLoc =
3297 Dezr.getCurrentBlockLocation();
3298
3299 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3300 assert (FoundBlock);
3301
3302 // Read the SourceManager.
3303 SourceManager::CreateAndRegister(Dezr, FMgr);
3304
3305 { // Read the TargetInfo.
3306 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3307 char* triple = Dezr.ReadCStr(NULL,0,true);
3308 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3309 delete [] triple;
3310 }
3311
3312 // For Selectors, we must read the identifier table first because the
3313 // SelectorTable depends on the identifiers being already deserialized.
3314 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3315 Dezr.SkipBlock();
3316
3317 // Read the identifier table.
3318 IdentifierTable::CreateAndRegister(Dezr);
3319
3320 // Now jump back and read the selectors.
3321 Dezr.JumpTo(SelectorBlkLoc);
3322 SelectorTable::CreateAndRegister(Dezr);
3323
3324 // Now jump back to ASTContextBlock and read the ASTContext.
3325 Dezr.JumpTo(ASTContextBlockLoc);
3326 return Dezr.ReadOwnedPtr<ASTContext>();
3327}
3328
Ted Kremenek0f84c002007-11-13 00:25:37 +00003329ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003330
3331 // Read the language options.
3332 LangOptions LOpts;
3333 LOpts.Read(D);
3334
Ted Kremenekfee04522007-10-31 22:44:07 +00003335 SourceManager &SM = D.ReadRef<SourceManager>();
3336 TargetInfo &t = D.ReadRef<TargetInfo>();
3337 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3338 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00003339
Ted Kremenekfee04522007-10-31 22:44:07 +00003340 unsigned size_reserve = D.ReadInt();
3341
Douglas Gregor2e1cd422008-11-17 14:58:09 +00003342 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3343 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00003344
Ted Kremenek03ed4402007-11-13 22:02:55 +00003345 for (unsigned i = 0; i < size_reserve; ++i)
3346 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00003347
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003348 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3349
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003350 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00003351
3352 return A;
3353}