blob: e976ccf967ae15d7af51a4c9040b06163a6b99ab [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,
Douglas Gregor6ab35242009-04-09 21:40:53 +0000606 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
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.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000617 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
618 E = OI->prop_end(*this); 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) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000651 RD->addDecl(*this,
652 FieldDecl::Create(*this, RD,
Chris Lattner23499252009-03-31 09:24:30 +0000653 RecFields[i]->getLocation(),
654 RecFields[i]->getIdentifier(),
655 RecFields[i]->getType(),
656 RecFields[i]->getBitWidth(), false));
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000657 }
Chris Lattnerf1690852009-03-31 08:48:01 +0000658
Chris Lattner23499252009-03-31 09:24:30 +0000659 RD->completeDefinition(*this);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000660 return RD;
661}
Devang Patel44a3dde2008-06-04 21:54:36 +0000662
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000663/// setFieldDecl - maps a field for the given Ivar reference node.
664//
665void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
666 const ObjCIvarDecl *Ivar,
667 const ObjCIvarRefExpr *MRef) {
Chris Lattnerda046392009-03-31 08:31:13 +0000668 ASTFieldForIvarRef[MRef] = OI->lookupFieldDeclForIvar(*this, Ivar);
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000669}
670
Chris Lattner61710852008-10-05 17:34:18 +0000671/// getASTObjcInterfaceLayout - Get or compute information about the layout of
672/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000673/// position information.
674const ASTRecordLayout &
675ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
676 // Look up this layout, if already laid out, return what we have.
677 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
678 if (Entry) return *Entry;
679
680 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
681 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000682 ASTRecordLayout *NewEntry = NULL;
Fariborz Jahanian0c7ce5b2009-04-08 21:54:52 +0000683 // FIXME. Add actual count of synthesized ivars, instead of count
684 // of properties which is the upper bound, but is safe.
Daniel Dunbar4af44122009-04-08 20:18:15 +0000685 unsigned FieldCount =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000686 D->ivar_size() + std::distance(D->prop_begin(*this), D->prop_end(*this));
Devang Patel6a5a34c2008-06-06 02:14:01 +0000687 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
688 FieldCount++;
689 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
690 unsigned Alignment = SL.getAlignment();
691 uint64_t Size = SL.getSize();
692 NewEntry = new ASTRecordLayout(Size, Alignment);
693 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000694 // Super class is at the beginning of the layout.
695 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000696 } else {
697 NewEntry = new ASTRecordLayout();
698 NewEntry->InitializeLayout(FieldCount);
699 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000700 Entry = NewEntry;
701
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000702 unsigned StructPacking = 0;
703 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
704 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000705
706 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
707 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
708 AA->getAlignment()));
709
710 // Layout each ivar sequentially.
711 unsigned i = 0;
712 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
713 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
714 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000715 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000716 }
Fariborz Jahanian18191882009-03-31 18:11:23 +0000717 // Also synthesized ivars
Douglas Gregor6ab35242009-04-09 21:40:53 +0000718 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
719 E = D->prop_end(*this); I != E; ++I) {
Fariborz Jahanian18191882009-03-31 18:11:23 +0000720 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
721 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
722 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000723
Devang Patel44a3dde2008-06-04 21:54:36 +0000724 // Finally, round the size of the total struct up to the alignment of the
725 // struct itself.
726 NewEntry->FinalizeLayout();
727 return *NewEntry;
728}
729
Devang Patel88a981b2007-11-01 19:11:01 +0000730/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000731/// specified record (struct/union/class), which indicates its size and field
732/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000733const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000734 D = D->getDefinition(*this);
735 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000736
Chris Lattner464175b2007-07-18 17:52:12 +0000737 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000738 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000739 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000740
Devang Patel88a981b2007-11-01 19:11:01 +0000741 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
742 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
743 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000744 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000745
Douglas Gregore267ff32008-12-11 20:41:00 +0000746 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000747 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
748 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000749 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000750
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000751 unsigned StructPacking = 0;
752 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
753 StructPacking = PA->getAlignment();
754
Eli Friedman4bd998b2008-05-30 09:31:38 +0000755 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000756 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
757 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000758
Eli Friedman4bd998b2008-05-30 09:31:38 +0000759 // Layout each field, for now, just sequentially, respecting alignment. In
760 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000761 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000762 for (RecordDecl::field_iterator Field = D->field_begin(*this),
763 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000764 Field != FieldEnd; (void)++Field, ++FieldIdx)
765 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000766
767 // Finally, round the size of the total struct up to the alignment of the
768 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000769 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000770 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000771}
772
Chris Lattnera7674d82007-07-13 22:13:22 +0000773//===----------------------------------------------------------------------===//
774// Type creation/memoization methods
775//===----------------------------------------------------------------------===//
776
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000777QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000778 QualType CanT = getCanonicalType(T);
779 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000780 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000781
782 // If we are composing extended qualifiers together, merge together into one
783 // ExtQualType node.
784 unsigned CVRQuals = T.getCVRQualifiers();
785 QualType::GCAttrTypes GCAttr = QualType::GCNone;
786 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000787
Chris Lattnerb7d25532009-02-18 22:53:11 +0000788 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
789 // If this type already has an address space specified, it cannot get
790 // another one.
791 assert(EQT->getAddressSpace() == 0 &&
792 "Type cannot be in multiple addr spaces!");
793 GCAttr = EQT->getObjCGCAttr();
794 TypeNode = EQT->getBaseType();
795 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000796
Chris Lattnerb7d25532009-02-18 22:53:11 +0000797 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000798 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000799 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000800 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000801 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000802 return QualType(EXTQy, CVRQuals);
803
Christopher Lambebb97e92008-02-04 02:31:56 +0000804 // If the base type isn't canonical, this won't be a canonical type either,
805 // so fill in the canonical type field.
806 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000807 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000808 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000809
Chris Lattnerb7d25532009-02-18 22:53:11 +0000810 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000811 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000812 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000813 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000814 ExtQualType *New =
815 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000816 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000817 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000818 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000819}
820
Chris Lattnerb7d25532009-02-18 22:53:11 +0000821QualType ASTContext::getObjCGCQualType(QualType T,
822 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000823 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000824 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000825 return T;
826
Chris Lattnerb7d25532009-02-18 22:53:11 +0000827 // If we are composing extended qualifiers together, merge together into one
828 // ExtQualType node.
829 unsigned CVRQuals = T.getCVRQualifiers();
830 Type *TypeNode = T.getTypePtr();
831 unsigned AddressSpace = 0;
832
833 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
834 // If this type already has an address space specified, it cannot get
835 // another one.
836 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
837 "Type cannot be in multiple addr spaces!");
838 AddressSpace = EQT->getAddressSpace();
839 TypeNode = EQT->getBaseType();
840 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000841
842 // Check if we've already instantiated an gc qual'd type of this type.
843 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000844 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000845 void *InsertPos = 0;
846 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000847 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000848
849 // If the base type isn't canonical, this won't be a canonical type either,
850 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000851 // FIXME: Isn't this also not canonical if the base type is a array
852 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000853 QualType Canonical;
854 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000855 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000856
Chris Lattnerb7d25532009-02-18 22:53:11 +0000857 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000858 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
859 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
860 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000861 ExtQualType *New =
862 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000863 ExtQualTypes.InsertNode(New, InsertPos);
864 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000865 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000866}
Chris Lattnera7674d82007-07-13 22:13:22 +0000867
Reid Spencer5f016e22007-07-11 17:01:13 +0000868/// getComplexType - Return the uniqued reference to the type for a complex
869/// number with the specified element type.
870QualType ASTContext::getComplexType(QualType T) {
871 // Unique pointers, to guarantee there is only one pointer of a particular
872 // structure.
873 llvm::FoldingSetNodeID ID;
874 ComplexType::Profile(ID, T);
875
876 void *InsertPos = 0;
877 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
878 return QualType(CT, 0);
879
880 // If the pointee type isn't canonical, this won't be a canonical type either,
881 // so fill in the canonical type field.
882 QualType Canonical;
883 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000884 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000885
886 // Get the new insert position for the node we care about.
887 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000888 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 }
Steve Narofff83820b2009-01-27 22:08:43 +0000890 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000891 Types.push_back(New);
892 ComplexTypes.InsertNode(New, InsertPos);
893 return QualType(New, 0);
894}
895
Eli Friedmanf98aba32009-02-13 02:31:07 +0000896QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
897 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
898 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
899 FixedWidthIntType *&Entry = Map[Width];
900 if (!Entry)
901 Entry = new FixedWidthIntType(Width, Signed);
902 return QualType(Entry, 0);
903}
Reid Spencer5f016e22007-07-11 17:01:13 +0000904
905/// getPointerType - Return the uniqued reference to the type for a pointer to
906/// the specified type.
907QualType ASTContext::getPointerType(QualType T) {
908 // Unique pointers, to guarantee there is only one pointer of a particular
909 // structure.
910 llvm::FoldingSetNodeID ID;
911 PointerType::Profile(ID, T);
912
913 void *InsertPos = 0;
914 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
915 return QualType(PT, 0);
916
917 // If the pointee type isn't canonical, this won't be a canonical type either,
918 // so fill in the canonical type field.
919 QualType Canonical;
920 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000921 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000922
923 // Get the new insert position for the node we care about.
924 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000925 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000926 }
Steve Narofff83820b2009-01-27 22:08:43 +0000927 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000928 Types.push_back(New);
929 PointerTypes.InsertNode(New, InsertPos);
930 return QualType(New, 0);
931}
932
Steve Naroff5618bd42008-08-27 16:04:49 +0000933/// getBlockPointerType - Return the uniqued reference to the type for
934/// a pointer to the specified block.
935QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000936 assert(T->isFunctionType() && "block of function types only");
937 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000938 // structure.
939 llvm::FoldingSetNodeID ID;
940 BlockPointerType::Profile(ID, T);
941
942 void *InsertPos = 0;
943 if (BlockPointerType *PT =
944 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
945 return QualType(PT, 0);
946
Steve Naroff296e8d52008-08-28 19:20:44 +0000947 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000948 // type either so fill in the canonical type field.
949 QualType Canonical;
950 if (!T->isCanonical()) {
951 Canonical = getBlockPointerType(getCanonicalType(T));
952
953 // Get the new insert position for the node we care about.
954 BlockPointerType *NewIP =
955 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000956 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000957 }
Steve Narofff83820b2009-01-27 22:08:43 +0000958 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000959 Types.push_back(New);
960 BlockPointerTypes.InsertNode(New, InsertPos);
961 return QualType(New, 0);
962}
963
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000964/// getLValueReferenceType - Return the uniqued reference to the type for an
965/// lvalue reference to the specified type.
966QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000967 // Unique pointers, to guarantee there is only one pointer of a particular
968 // structure.
969 llvm::FoldingSetNodeID ID;
970 ReferenceType::Profile(ID, T);
971
972 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000973 if (LValueReferenceType *RT =
974 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000975 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000976
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 // If the referencee type isn't canonical, this won't be a canonical type
978 // either, so fill in the canonical type field.
979 QualType Canonical;
980 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000981 Canonical = getLValueReferenceType(getCanonicalType(T));
982
Reid Spencer5f016e22007-07-11 17:01:13 +0000983 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000984 LValueReferenceType *NewIP =
985 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000986 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 }
988
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000989 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000991 LValueReferenceTypes.InsertNode(New, InsertPos);
992 return QualType(New, 0);
993}
994
995/// getRValueReferenceType - Return the uniqued reference to the type for an
996/// rvalue reference to the specified type.
997QualType ASTContext::getRValueReferenceType(QualType T) {
998 // Unique pointers, to guarantee there is only one pointer of a particular
999 // structure.
1000 llvm::FoldingSetNodeID ID;
1001 ReferenceType::Profile(ID, T);
1002
1003 void *InsertPos = 0;
1004 if (RValueReferenceType *RT =
1005 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1006 return QualType(RT, 0);
1007
1008 // If the referencee type isn't canonical, this won't be a canonical type
1009 // either, so fill in the canonical type field.
1010 QualType Canonical;
1011 if (!T->isCanonical()) {
1012 Canonical = getRValueReferenceType(getCanonicalType(T));
1013
1014 // Get the new insert position for the node we care about.
1015 RValueReferenceType *NewIP =
1016 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1017 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1018 }
1019
1020 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1021 Types.push_back(New);
1022 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001023 return QualType(New, 0);
1024}
1025
Sebastian Redlf30208a2009-01-24 21:16:55 +00001026/// getMemberPointerType - Return the uniqued reference to the type for a
1027/// member pointer to the specified type, in the specified class.
1028QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1029{
1030 // Unique pointers, to guarantee there is only one pointer of a particular
1031 // structure.
1032 llvm::FoldingSetNodeID ID;
1033 MemberPointerType::Profile(ID, T, Cls);
1034
1035 void *InsertPos = 0;
1036 if (MemberPointerType *PT =
1037 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1038 return QualType(PT, 0);
1039
1040 // If the pointee or class type isn't canonical, this won't be a canonical
1041 // type either, so fill in the canonical type field.
1042 QualType Canonical;
1043 if (!T->isCanonical()) {
1044 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1045
1046 // Get the new insert position for the node we care about.
1047 MemberPointerType *NewIP =
1048 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1049 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1050 }
Steve Narofff83820b2009-01-27 22:08:43 +00001051 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001052 Types.push_back(New);
1053 MemberPointerTypes.InsertNode(New, InsertPos);
1054 return QualType(New, 0);
1055}
1056
Steve Narofffb22d962007-08-30 01:06:46 +00001057/// getConstantArrayType - Return the unique reference to the type for an
1058/// array of the specified element type.
1059QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001060 const llvm::APInt &ArySize,
1061 ArrayType::ArraySizeModifier ASM,
1062 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001063 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001064 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001065
1066 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001067 if (ConstantArrayType *ATP =
1068 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 return QualType(ATP, 0);
1070
1071 // If the element type isn't canonical, this won't be a canonical type either,
1072 // so fill in the canonical type field.
1073 QualType Canonical;
1074 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001075 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001076 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001078 ConstantArrayType *NewIP =
1079 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001080 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001081 }
1082
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001083 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001084 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001085 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 Types.push_back(New);
1087 return QualType(New, 0);
1088}
1089
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001090/// getVariableArrayType - Returns a non-unique reference to the type for a
1091/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001092QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1093 ArrayType::ArraySizeModifier ASM,
1094 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001095 // Since we don't unique expressions, it isn't possible to unique VLA's
1096 // that have an expression provided for their size.
1097
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001098 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001099 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001100
1101 VariableArrayTypes.push_back(New);
1102 Types.push_back(New);
1103 return QualType(New, 0);
1104}
1105
Douglas Gregor898574e2008-12-05 23:32:09 +00001106/// getDependentSizedArrayType - Returns a non-unique reference to
1107/// the type for a dependently-sized array of the specified element
1108/// type. FIXME: We will need these to be uniqued, or at least
1109/// comparable, at some point.
1110QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1111 ArrayType::ArraySizeModifier ASM,
1112 unsigned EltTypeQuals) {
1113 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1114 "Size must be type- or value-dependent!");
1115
1116 // Since we don't unique expressions, it isn't possible to unique
1117 // dependently-sized array types.
1118
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001119 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001120 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1121 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001122
1123 DependentSizedArrayTypes.push_back(New);
1124 Types.push_back(New);
1125 return QualType(New, 0);
1126}
1127
Eli Friedmanc5773c42008-02-15 18:16:39 +00001128QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1129 ArrayType::ArraySizeModifier ASM,
1130 unsigned EltTypeQuals) {
1131 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001132 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001133
1134 void *InsertPos = 0;
1135 if (IncompleteArrayType *ATP =
1136 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1137 return QualType(ATP, 0);
1138
1139 // If the element type isn't canonical, this won't be a canonical type
1140 // either, so fill in the canonical type field.
1141 QualType Canonical;
1142
1143 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001144 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001145 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001146
1147 // Get the new insert position for the node we care about.
1148 IncompleteArrayType *NewIP =
1149 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001150 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001151 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001152
Steve Narofff83820b2009-01-27 22:08:43 +00001153 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001154 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001155
1156 IncompleteArrayTypes.InsertNode(New, InsertPos);
1157 Types.push_back(New);
1158 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001159}
1160
Steve Naroff73322922007-07-18 18:00:27 +00001161/// getVectorType - Return the unique reference to a vector type of
1162/// the specified element type and size. VectorType must be a built-in type.
1163QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 BuiltinType *baseType;
1165
Chris Lattnerf52ab252008-04-06 22:59:24 +00001166 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001167 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001168
1169 // Check if we've already instantiated a vector of this type.
1170 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001171 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 void *InsertPos = 0;
1173 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1174 return QualType(VTP, 0);
1175
1176 // If the element type isn't canonical, this won't be a canonical type either,
1177 // so fill in the canonical type field.
1178 QualType Canonical;
1179 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001180 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001181
1182 // Get the new insert position for the node we care about.
1183 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001184 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001185 }
Steve Narofff83820b2009-01-27 22:08:43 +00001186 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 VectorTypes.InsertNode(New, InsertPos);
1188 Types.push_back(New);
1189 return QualType(New, 0);
1190}
1191
Nate Begeman213541a2008-04-18 23:10:10 +00001192/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001193/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001194QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001195 BuiltinType *baseType;
1196
Chris Lattnerf52ab252008-04-06 22:59:24 +00001197 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001198 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001199
1200 // Check if we've already instantiated a vector of this type.
1201 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001202 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001203 void *InsertPos = 0;
1204 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1205 return QualType(VTP, 0);
1206
1207 // If the element type isn't canonical, this won't be a canonical type either,
1208 // so fill in the canonical type field.
1209 QualType Canonical;
1210 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001211 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001212
1213 // Get the new insert position for the node we care about.
1214 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001215 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001216 }
Steve Narofff83820b2009-01-27 22:08:43 +00001217 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001218 VectorTypes.InsertNode(New, InsertPos);
1219 Types.push_back(New);
1220 return QualType(New, 0);
1221}
1222
Douglas Gregor72564e72009-02-26 23:50:07 +00001223/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001224///
Douglas Gregor72564e72009-02-26 23:50:07 +00001225QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001226 // Unique functions, to guarantee there is only one function of a particular
1227 // structure.
1228 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001229 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001230
1231 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001232 if (FunctionNoProtoType *FT =
1233 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 return QualType(FT, 0);
1235
1236 QualType Canonical;
1237 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001238 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001239
1240 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001241 FunctionNoProtoType *NewIP =
1242 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001243 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 }
1245
Douglas Gregor72564e72009-02-26 23:50:07 +00001246 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001248 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001249 return QualType(New, 0);
1250}
1251
1252/// getFunctionType - Return a normal function type with a typed argument
1253/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001254QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001255 unsigned NumArgs, bool isVariadic,
1256 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001257 // Unique functions, to guarantee there is only one function of a particular
1258 // structure.
1259 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001260 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001261 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001262
1263 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001264 if (FunctionProtoType *FTP =
1265 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 return QualType(FTP, 0);
1267
1268 // Determine whether the type being created is already canonical or not.
1269 bool isCanonical = ResultTy->isCanonical();
1270 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1271 if (!ArgArray[i]->isCanonical())
1272 isCanonical = false;
1273
1274 // If this type isn't canonical, get the canonical version of it.
1275 QualType Canonical;
1276 if (!isCanonical) {
1277 llvm::SmallVector<QualType, 16> CanonicalArgs;
1278 CanonicalArgs.reserve(NumArgs);
1279 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001280 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001281
Chris Lattnerf52ab252008-04-06 22:59:24 +00001282 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001284 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001285
1286 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001287 FunctionProtoType *NewIP =
1288 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001289 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 }
1291
Douglas Gregor72564e72009-02-26 23:50:07 +00001292 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001293 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001294 FunctionProtoType *FTP =
1295 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001296 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001297 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001298 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001299 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001300 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001301 return QualType(FTP, 0);
1302}
1303
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001304/// getTypeDeclType - Return the unique reference to the type for the
1305/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001306QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001307 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001308 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1309
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001310 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001311 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001312 else if (isa<TemplateTypeParmDecl>(Decl)) {
1313 assert(false && "Template type parameter types are always available.");
1314 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001315 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001316
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001317 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001318 if (PrevDecl)
1319 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001320 else
1321 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001322 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001323 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1324 if (PrevDecl)
1325 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001326 else
1327 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001328 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001329 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001330 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001331
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001332 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001333 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001334}
1335
Reid Spencer5f016e22007-07-11 17:01:13 +00001336/// getTypedefType - Return the unique reference to the type for the
1337/// specified typename decl.
1338QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1339 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1340
Chris Lattnerf52ab252008-04-06 22:59:24 +00001341 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001342 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001343 Types.push_back(Decl->TypeForDecl);
1344 return QualType(Decl->TypeForDecl, 0);
1345}
1346
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001347/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001348/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001349QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001350 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1351
Steve Narofff83820b2009-01-27 22:08:43 +00001352 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001353 Types.push_back(Decl->TypeForDecl);
1354 return QualType(Decl->TypeForDecl, 0);
1355}
1356
Douglas Gregorfab9d672009-02-05 23:33:38 +00001357/// \brief Retrieve the template type parameter type for a template
1358/// parameter with the given depth, index, and (optionally) name.
1359QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1360 IdentifierInfo *Name) {
1361 llvm::FoldingSetNodeID ID;
1362 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1363 void *InsertPos = 0;
1364 TemplateTypeParmType *TypeParm
1365 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1366
1367 if (TypeParm)
1368 return QualType(TypeParm, 0);
1369
1370 if (Name)
1371 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1372 getTemplateTypeParmType(Depth, Index));
1373 else
1374 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1375
1376 Types.push_back(TypeParm);
1377 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1378
1379 return QualType(TypeParm, 0);
1380}
1381
Douglas Gregor55f6b142009-02-09 18:46:07 +00001382QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001383ASTContext::getTemplateSpecializationType(TemplateName Template,
1384 const TemplateArgument *Args,
1385 unsigned NumArgs,
1386 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001387 if (!Canon.isNull())
1388 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001389
Douglas Gregor55f6b142009-02-09 18:46:07 +00001390 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001391 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001392
Douglas Gregor55f6b142009-02-09 18:46:07 +00001393 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001394 TemplateSpecializationType *Spec
1395 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001396
1397 if (Spec)
1398 return QualType(Spec, 0);
1399
Douglas Gregor7532dc62009-03-30 22:58:21 +00001400 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001401 sizeof(TemplateArgument) * NumArgs),
1402 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001403 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001404 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001405 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001406
1407 return QualType(Spec, 0);
1408}
1409
Douglas Gregore4e5b052009-03-19 00:18:19 +00001410QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001411ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001412 QualType NamedType) {
1413 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001414 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001415
1416 void *InsertPos = 0;
1417 QualifiedNameType *T
1418 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1419 if (T)
1420 return QualType(T, 0);
1421
Douglas Gregorab452ba2009-03-26 23:50:42 +00001422 T = new (*this) QualifiedNameType(NNS, NamedType,
1423 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001424 Types.push_back(T);
1425 QualifiedNameTypes.InsertNode(T, InsertPos);
1426 return QualType(T, 0);
1427}
1428
Douglas Gregord57959a2009-03-27 23:10:48 +00001429QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1430 const IdentifierInfo *Name,
1431 QualType Canon) {
1432 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1433
1434 if (Canon.isNull()) {
1435 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1436 if (CanonNNS != NNS)
1437 Canon = getTypenameType(CanonNNS, Name);
1438 }
1439
1440 llvm::FoldingSetNodeID ID;
1441 TypenameType::Profile(ID, NNS, Name);
1442
1443 void *InsertPos = 0;
1444 TypenameType *T
1445 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1446 if (T)
1447 return QualType(T, 0);
1448
1449 T = new (*this) TypenameType(NNS, Name, Canon);
1450 Types.push_back(T);
1451 TypenameTypes.InsertNode(T, InsertPos);
1452 return QualType(T, 0);
1453}
1454
Douglas Gregor17343172009-04-01 00:28:59 +00001455QualType
1456ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1457 const TemplateSpecializationType *TemplateId,
1458 QualType Canon) {
1459 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1460
1461 if (Canon.isNull()) {
1462 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1463 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1464 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1465 const TemplateSpecializationType *CanonTemplateId
1466 = CanonType->getAsTemplateSpecializationType();
1467 assert(CanonTemplateId &&
1468 "Canonical type must also be a template specialization type");
1469 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1470 }
1471 }
1472
1473 llvm::FoldingSetNodeID ID;
1474 TypenameType::Profile(ID, NNS, TemplateId);
1475
1476 void *InsertPos = 0;
1477 TypenameType *T
1478 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1479 if (T)
1480 return QualType(T, 0);
1481
1482 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1483 Types.push_back(T);
1484 TypenameTypes.InsertNode(T, InsertPos);
1485 return QualType(T, 0);
1486}
1487
Chris Lattner88cb27a2008-04-07 04:56:42 +00001488/// CmpProtocolNames - Comparison predicate for sorting protocols
1489/// alphabetically.
1490static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1491 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001492 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001493}
1494
1495static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1496 unsigned &NumProtocols) {
1497 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1498
1499 // Sort protocols, keyed by name.
1500 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1501
1502 // Remove duplicates.
1503 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1504 NumProtocols = ProtocolsEnd-Protocols;
1505}
1506
1507
Chris Lattner065f0d72008-04-07 04:44:08 +00001508/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1509/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001510QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1511 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001512 // Sort the protocol list alphabetically to canonicalize it.
1513 SortAndUniqueProtocols(Protocols, NumProtocols);
1514
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001515 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001516 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001517
1518 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001519 if (ObjCQualifiedInterfaceType *QT =
1520 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001521 return QualType(QT, 0);
1522
1523 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001524 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001525 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001526
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001527 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001528 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001529 return QualType(QType, 0);
1530}
1531
Chris Lattner88cb27a2008-04-07 04:56:42 +00001532/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1533/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001534QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001535 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001536 // Sort the protocol list alphabetically to canonicalize it.
1537 SortAndUniqueProtocols(Protocols, NumProtocols);
1538
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001539 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001540 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001541
1542 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001543 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001544 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001545 return QualType(QT, 0);
1546
1547 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001548 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001549 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001550 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001551 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001552 return QualType(QType, 0);
1553}
1554
Douglas Gregor72564e72009-02-26 23:50:07 +00001555/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1556/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001557/// multiple declarations that refer to "typeof(x)" all contain different
1558/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1559/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001560QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001561 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001562 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001563 Types.push_back(toe);
1564 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001565}
1566
Steve Naroff9752f252007-08-01 18:02:17 +00001567/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1568/// TypeOfType AST's. The only motivation to unique these nodes would be
1569/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1570/// an issue. This doesn't effect the type checker, since it operates
1571/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001572QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001573 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001574 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001575 Types.push_back(tot);
1576 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001577}
1578
Reid Spencer5f016e22007-07-11 17:01:13 +00001579/// getTagDeclType - Return the unique reference to the type for the
1580/// specified TagDecl (struct/union/class/enum) decl.
1581QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001582 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001583 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001584}
1585
1586/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1587/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1588/// needs to agree with the definition in <stddef.h>.
1589QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001590 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001591}
1592
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001593/// getSignedWCharType - Return the type of "signed wchar_t".
1594/// Used when in C++, as a GCC extension.
1595QualType ASTContext::getSignedWCharType() const {
1596 // FIXME: derive from "Target" ?
1597 return WCharTy;
1598}
1599
1600/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1601/// Used when in C++, as a GCC extension.
1602QualType ASTContext::getUnsignedWCharType() const {
1603 // FIXME: derive from "Target" ?
1604 return UnsignedIntTy;
1605}
1606
Chris Lattner8b9023b2007-07-13 03:05:23 +00001607/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1608/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1609QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001610 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001611}
1612
Chris Lattnere6327742008-04-02 05:18:44 +00001613//===----------------------------------------------------------------------===//
1614// Type Operators
1615//===----------------------------------------------------------------------===//
1616
Chris Lattner77c96472008-04-06 22:41:35 +00001617/// getCanonicalType - Return the canonical (structural) type corresponding to
1618/// the specified potentially non-canonical type. The non-canonical version
1619/// of a type may have many "decorated" versions of types. Decorators can
1620/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1621/// to be free of any of these, allowing two canonical types to be compared
1622/// for exact equality with a simple pointer comparison.
1623QualType ASTContext::getCanonicalType(QualType T) {
1624 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001625
1626 // If the result has type qualifiers, make sure to canonicalize them as well.
1627 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1628 if (TypeQuals == 0) return CanType;
1629
1630 // If the type qualifiers are on an array type, get the canonical type of the
1631 // array with the qualifiers applied to the element type.
1632 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1633 if (!AT)
1634 return CanType.getQualifiedType(TypeQuals);
1635
1636 // Get the canonical version of the element with the extra qualifiers on it.
1637 // This can recursively sink qualifiers through multiple levels of arrays.
1638 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1639 NewEltTy = getCanonicalType(NewEltTy);
1640
1641 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1642 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1643 CAT->getIndexTypeQualifier());
1644 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1645 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1646 IAT->getIndexTypeQualifier());
1647
Douglas Gregor898574e2008-12-05 23:32:09 +00001648 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1649 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1650 DSAT->getSizeModifier(),
1651 DSAT->getIndexTypeQualifier());
1652
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001653 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1654 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1655 VAT->getSizeModifier(),
1656 VAT->getIndexTypeQualifier());
1657}
1658
Douglas Gregord57959a2009-03-27 23:10:48 +00001659NestedNameSpecifier *
1660ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1661 if (!NNS)
1662 return 0;
1663
1664 switch (NNS->getKind()) {
1665 case NestedNameSpecifier::Identifier:
1666 // Canonicalize the prefix but keep the identifier the same.
1667 return NestedNameSpecifier::Create(*this,
1668 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1669 NNS->getAsIdentifier());
1670
1671 case NestedNameSpecifier::Namespace:
1672 // A namespace is canonical; build a nested-name-specifier with
1673 // this namespace and no prefix.
1674 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1675
1676 case NestedNameSpecifier::TypeSpec:
1677 case NestedNameSpecifier::TypeSpecWithTemplate: {
1678 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1679 NestedNameSpecifier *Prefix = 0;
1680
1681 // FIXME: This isn't the right check!
1682 if (T->isDependentType())
1683 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1684
1685 return NestedNameSpecifier::Create(*this, Prefix,
1686 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1687 T.getTypePtr());
1688 }
1689
1690 case NestedNameSpecifier::Global:
1691 // The global specifier is canonical and unique.
1692 return NNS;
1693 }
1694
1695 // Required to silence a GCC warning
1696 return 0;
1697}
1698
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001699
1700const ArrayType *ASTContext::getAsArrayType(QualType T) {
1701 // Handle the non-qualified case efficiently.
1702 if (T.getCVRQualifiers() == 0) {
1703 // Handle the common positive case fast.
1704 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1705 return AT;
1706 }
1707
1708 // Handle the common negative case fast, ignoring CVR qualifiers.
1709 QualType CType = T->getCanonicalTypeInternal();
1710
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001711 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001712 // test.
1713 if (!isa<ArrayType>(CType) &&
1714 !isa<ArrayType>(CType.getUnqualifiedType()))
1715 return 0;
1716
1717 // Apply any CVR qualifiers from the array type to the element type. This
1718 // implements C99 6.7.3p8: "If the specification of an array type includes
1719 // any type qualifiers, the element type is so qualified, not the array type."
1720
1721 // If we get here, we either have type qualifiers on the type, or we have
1722 // sugar such as a typedef in the way. If we have type qualifiers on the type
1723 // we must propagate them down into the elemeng type.
1724 unsigned CVRQuals = T.getCVRQualifiers();
1725 unsigned AddrSpace = 0;
1726 Type *Ty = T.getTypePtr();
1727
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001728 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001729 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001730 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1731 AddrSpace = EXTQT->getAddressSpace();
1732 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001733 } else {
1734 T = Ty->getDesugaredType();
1735 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1736 break;
1737 CVRQuals |= T.getCVRQualifiers();
1738 Ty = T.getTypePtr();
1739 }
1740 }
1741
1742 // If we have a simple case, just return now.
1743 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1744 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1745 return ATy;
1746
1747 // Otherwise, we have an array and we have qualifiers on it. Push the
1748 // qualifiers into the array element type and return a new array type.
1749 // Get the canonical version of the element with the extra qualifiers on it.
1750 // This can recursively sink qualifiers through multiple levels of arrays.
1751 QualType NewEltTy = ATy->getElementType();
1752 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001753 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001754 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1755
1756 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1757 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1758 CAT->getSizeModifier(),
1759 CAT->getIndexTypeQualifier()));
1760 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1761 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1762 IAT->getSizeModifier(),
1763 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001764
Douglas Gregor898574e2008-12-05 23:32:09 +00001765 if (const DependentSizedArrayType *DSAT
1766 = dyn_cast<DependentSizedArrayType>(ATy))
1767 return cast<ArrayType>(
1768 getDependentSizedArrayType(NewEltTy,
1769 DSAT->getSizeExpr(),
1770 DSAT->getSizeModifier(),
1771 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001772
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001773 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1774 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1775 VAT->getSizeModifier(),
1776 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001777}
1778
1779
Chris Lattnere6327742008-04-02 05:18:44 +00001780/// getArrayDecayedType - Return the properly qualified result of decaying the
1781/// specified array type to a pointer. This operation is non-trivial when
1782/// handling typedefs etc. The canonical type of "T" must be an array type,
1783/// this returns a pointer to a properly qualified element of the array.
1784///
1785/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1786QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001787 // Get the element type with 'getAsArrayType' so that we don't lose any
1788 // typedefs in the element type of the array. This also handles propagation
1789 // of type qualifiers from the array type into the element type if present
1790 // (C99 6.7.3p8).
1791 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1792 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001793
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001794 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001795
1796 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001797 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001798}
1799
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001800QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001801 QualType ElemTy = VAT->getElementType();
1802
1803 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1804 return getBaseElementType(VAT);
1805
1806 return ElemTy;
1807}
1808
Reid Spencer5f016e22007-07-11 17:01:13 +00001809/// getFloatingRank - Return a relative rank for floating point types.
1810/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001811static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001812 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001813 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001814
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001815 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001816 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001817 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001818 case BuiltinType::Float: return FloatRank;
1819 case BuiltinType::Double: return DoubleRank;
1820 case BuiltinType::LongDouble: return LongDoubleRank;
1821 }
1822}
1823
Steve Naroff716c7302007-08-27 01:41:48 +00001824/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1825/// point or a complex type (based on typeDomain/typeSize).
1826/// 'typeDomain' is a real floating point or complex type.
1827/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001828QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1829 QualType Domain) const {
1830 FloatingRank EltRank = getFloatingRank(Size);
1831 if (Domain->isComplexType()) {
1832 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001833 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001834 case FloatRank: return FloatComplexTy;
1835 case DoubleRank: return DoubleComplexTy;
1836 case LongDoubleRank: return LongDoubleComplexTy;
1837 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001838 }
Chris Lattner1361b112008-04-06 23:58:54 +00001839
1840 assert(Domain->isRealFloatingType() && "Unknown domain!");
1841 switch (EltRank) {
1842 default: assert(0 && "getFloatingRank(): illegal value for rank");
1843 case FloatRank: return FloatTy;
1844 case DoubleRank: return DoubleTy;
1845 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001846 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001847}
1848
Chris Lattner7cfeb082008-04-06 23:55:33 +00001849/// getFloatingTypeOrder - Compare the rank of the two specified floating
1850/// point types, ignoring the domain of the type (i.e. 'double' ==
1851/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1852/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001853int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1854 FloatingRank LHSR = getFloatingRank(LHS);
1855 FloatingRank RHSR = getFloatingRank(RHS);
1856
1857 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001858 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001859 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001860 return 1;
1861 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001862}
1863
Chris Lattnerf52ab252008-04-06 22:59:24 +00001864/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1865/// routine will assert if passed a built-in type that isn't an integer or enum,
1866/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001867unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001868 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001869 if (EnumType* ET = dyn_cast<EnumType>(T))
1870 T = ET->getDecl()->getIntegerType().getTypePtr();
1871
1872 // There are two things which impact the integer rank: the width, and
1873 // the ordering of builtins. The builtin ordering is encoded in the
1874 // bottom three bits; the width is encoded in the bits above that.
1875 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1876 return FWIT->getWidth() << 3;
1877 }
1878
Chris Lattnerf52ab252008-04-06 22:59:24 +00001879 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001880 default: assert(0 && "getIntegerRank(): not a built-in integer");
1881 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001882 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001883 case BuiltinType::Char_S:
1884 case BuiltinType::Char_U:
1885 case BuiltinType::SChar:
1886 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001887 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001888 case BuiltinType::Short:
1889 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001890 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001891 case BuiltinType::Int:
1892 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001893 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001894 case BuiltinType::Long:
1895 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001896 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001897 case BuiltinType::LongLong:
1898 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001899 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001900 }
1901}
1902
Chris Lattner7cfeb082008-04-06 23:55:33 +00001903/// getIntegerTypeOrder - Returns the highest ranked integer type:
1904/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1905/// LHS < RHS, return -1.
1906int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001907 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1908 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001909 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001910
Chris Lattnerf52ab252008-04-06 22:59:24 +00001911 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1912 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001913
Chris Lattner7cfeb082008-04-06 23:55:33 +00001914 unsigned LHSRank = getIntegerRank(LHSC);
1915 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001916
Chris Lattner7cfeb082008-04-06 23:55:33 +00001917 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1918 if (LHSRank == RHSRank) return 0;
1919 return LHSRank > RHSRank ? 1 : -1;
1920 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001921
Chris Lattner7cfeb082008-04-06 23:55:33 +00001922 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1923 if (LHSUnsigned) {
1924 // If the unsigned [LHS] type is larger, return it.
1925 if (LHSRank >= RHSRank)
1926 return 1;
1927
1928 // If the signed type can represent all values of the unsigned type, it
1929 // wins. Because we are dealing with 2's complement and types that are
1930 // powers of two larger than each other, this is always safe.
1931 return -1;
1932 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001933
Chris Lattner7cfeb082008-04-06 23:55:33 +00001934 // If the unsigned [RHS] type is larger, return it.
1935 if (RHSRank >= LHSRank)
1936 return -1;
1937
1938 // If the signed type can represent all values of the unsigned type, it
1939 // wins. Because we are dealing with 2's complement and types that are
1940 // powers of two larger than each other, this is always safe.
1941 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001942}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001943
1944// getCFConstantStringType - Return the type used for constant CFStrings.
1945QualType ASTContext::getCFConstantStringType() {
1946 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001947 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001948 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001949 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001950 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001951
1952 // const int *isa;
1953 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001954 // int flags;
1955 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001956 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001957 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001958 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001959 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001960
Anders Carlsson71993dd2007-08-17 05:31:46 +00001961 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001962 for (unsigned i = 0; i < 4; ++i) {
1963 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1964 SourceLocation(), 0,
1965 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001966 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001967 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001968 }
1969
1970 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001971 }
1972
1973 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001974}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001975
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001976QualType ASTContext::getObjCFastEnumerationStateType()
1977{
1978 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001979 ObjCFastEnumerationStateTypeDecl =
1980 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1981 &Idents.get("__objcFastEnumerationState"));
1982
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001983 QualType FieldTypes[] = {
1984 UnsignedLongTy,
1985 getPointerType(ObjCIdType),
1986 getPointerType(UnsignedLongTy),
1987 getConstantArrayType(UnsignedLongTy,
1988 llvm::APInt(32, 5), ArrayType::Normal, 0)
1989 };
1990
Douglas Gregor44b43212008-12-11 16:49:14 +00001991 for (size_t i = 0; i < 4; ++i) {
1992 FieldDecl *Field = FieldDecl::Create(*this,
1993 ObjCFastEnumerationStateTypeDecl,
1994 SourceLocation(), 0,
1995 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001996 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001997 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001998 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001999
Douglas Gregor44b43212008-12-11 16:49:14 +00002000 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002001 }
2002
2003 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2004}
2005
Anders Carlssone8c49532007-10-29 06:33:42 +00002006// This returns true if a type has been typedefed to BOOL:
2007// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002008static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002009 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002010 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2011 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002012
2013 return false;
2014}
2015
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002016/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002017/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002018int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002019 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002020
2021 // Make all integer and enum types at least as large as an int
2022 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002023 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002024 // Treat arrays as pointers, since that's how they're passed in.
2025 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002026 sz = getTypeSize(VoidPtrTy);
2027 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002028}
2029
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002030/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002031/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002032void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002033 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002034 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002035 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002036 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002037 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002038 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002039 // Compute size of all parameters.
2040 // Start with computing size of a pointer in number of bytes.
2041 // FIXME: There might(should) be a better way of doing this computation!
2042 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002043 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002044 // The first two arguments (self and _cmd) are pointers; account for
2045 // their size.
2046 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002047 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2048 E = Decl->param_end(); PI != E; ++PI) {
2049 QualType PType = (*PI)->getType();
2050 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002051 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002052 ParmOffset += sz;
2053 }
2054 S += llvm::utostr(ParmOffset);
2055 S += "@0:";
2056 S += llvm::utostr(PtrSize);
2057
2058 // Argument types.
2059 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002060 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2061 E = Decl->param_end(); PI != E; ++PI) {
2062 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002063 QualType PType = PVDecl->getOriginalType();
2064 if (const ArrayType *AT =
2065 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
2066 // Use array's original type only if it has known number of
2067 // elements.
2068 if (!dyn_cast<ConstantArrayType>(AT))
2069 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002070 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002071 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002072 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002073 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002074 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002075 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002076 }
2077}
2078
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002079/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002080/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002081/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2082/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002083/// Property attributes are stored as a comma-delimited C string. The simple
2084/// attributes readonly and bycopy are encoded as single characters. The
2085/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2086/// encoded as single characters, followed by an identifier. Property types
2087/// are also encoded as a parametrized attribute. The characters used to encode
2088/// these attributes are defined by the following enumeration:
2089/// @code
2090/// enum PropertyAttributes {
2091/// kPropertyReadOnly = 'R', // property is read-only.
2092/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2093/// kPropertyByref = '&', // property is a reference to the value last assigned
2094/// kPropertyDynamic = 'D', // property is dynamic
2095/// kPropertyGetter = 'G', // followed by getter selector name
2096/// kPropertySetter = 'S', // followed by setter selector name
2097/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2098/// kPropertyType = 't' // followed by old-style type encoding.
2099/// kPropertyWeak = 'W' // 'weak' property
2100/// kPropertyStrong = 'P' // property GC'able
2101/// kPropertyNonAtomic = 'N' // property non-atomic
2102/// };
2103/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002104void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2105 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002106 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002107 // Collect information from the property implementation decl(s).
2108 bool Dynamic = false;
2109 ObjCPropertyImplDecl *SynthesizePID = 0;
2110
2111 // FIXME: Duplicated code due to poor abstraction.
2112 if (Container) {
2113 if (const ObjCCategoryImplDecl *CID =
2114 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2115 for (ObjCCategoryImplDecl::propimpl_iterator
2116 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2117 ObjCPropertyImplDecl *PID = *i;
2118 if (PID->getPropertyDecl() == PD) {
2119 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2120 Dynamic = true;
2121 } else {
2122 SynthesizePID = PID;
2123 }
2124 }
2125 }
2126 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002127 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002128 for (ObjCCategoryImplDecl::propimpl_iterator
2129 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2130 ObjCPropertyImplDecl *PID = *i;
2131 if (PID->getPropertyDecl() == PD) {
2132 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2133 Dynamic = true;
2134 } else {
2135 SynthesizePID = PID;
2136 }
2137 }
2138 }
2139 }
2140 }
2141
2142 // FIXME: This is not very efficient.
2143 S = "T";
2144
2145 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002146 // GCC has some special rules regarding encoding of properties which
2147 // closely resembles encoding of ivars.
2148 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2149 true /* outermost type */,
2150 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002151
2152 if (PD->isReadOnly()) {
2153 S += ",R";
2154 } else {
2155 switch (PD->getSetterKind()) {
2156 case ObjCPropertyDecl::Assign: break;
2157 case ObjCPropertyDecl::Copy: S += ",C"; break;
2158 case ObjCPropertyDecl::Retain: S += ",&"; break;
2159 }
2160 }
2161
2162 // It really isn't clear at all what this means, since properties
2163 // are "dynamic by default".
2164 if (Dynamic)
2165 S += ",D";
2166
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002167 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2168 S += ",N";
2169
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002170 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2171 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002172 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002173 }
2174
2175 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2176 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002177 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002178 }
2179
2180 if (SynthesizePID) {
2181 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2182 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002183 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002184 }
2185
2186 // FIXME: OBJCGC: weak & strong
2187}
2188
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002189/// getLegacyIntegralTypeEncoding -
2190/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002191/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002192/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2193///
2194void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2195 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2196 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002197 if (BT->getKind() == BuiltinType::ULong &&
2198 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002199 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002200 else
2201 if (BT->getKind() == BuiltinType::Long &&
2202 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002203 PointeeTy = IntTy;
2204 }
2205 }
2206}
2207
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002208void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002209 FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002210 // We follow the behavior of gcc, expanding structures which are
2211 // directly pointed to, and expanding embedded structures. Note that
2212 // these rules are sufficient to prevent recursive encoding of the
2213 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002214 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2215 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002216}
2217
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002218static void EncodeBitField(const ASTContext *Context, std::string& S,
2219 FieldDecl *FD) {
2220 const Expr *E = FD->getBitWidth();
2221 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2222 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2223 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2224 S += 'b';
2225 S += llvm::utostr(N);
2226}
2227
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002228void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2229 bool ExpandPointedToStructures,
2230 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002231 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002232 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002233 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002234 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002235 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002236 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002237 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002238 else {
2239 char encoding;
2240 switch (BT->getKind()) {
2241 default: assert(0 && "Unhandled builtin type kind");
2242 case BuiltinType::Void: encoding = 'v'; break;
2243 case BuiltinType::Bool: encoding = 'B'; break;
2244 case BuiltinType::Char_U:
2245 case BuiltinType::UChar: encoding = 'C'; break;
2246 case BuiltinType::UShort: encoding = 'S'; break;
2247 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002248 case BuiltinType::ULong:
2249 encoding =
2250 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2251 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002252 case BuiltinType::ULongLong: encoding = 'Q'; break;
2253 case BuiltinType::Char_S:
2254 case BuiltinType::SChar: encoding = 'c'; break;
2255 case BuiltinType::Short: encoding = 's'; break;
2256 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002257 case BuiltinType::Long:
2258 encoding =
2259 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2260 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002261 case BuiltinType::LongLong: encoding = 'q'; break;
2262 case BuiltinType::Float: encoding = 'f'; break;
2263 case BuiltinType::Double: encoding = 'd'; break;
2264 case BuiltinType::LongDouble: encoding = 'd'; break;
2265 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002266
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002267 S += encoding;
2268 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002269 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002270 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002271 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2272 ExpandPointedToStructures,
2273 ExpandStructures, FD);
2274 if (FD || EncodingProperty) {
2275 // Note that we do extended encoding of protocol qualifer list
2276 // Only when doing ivar or property encoding.
2277 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2278 S += '"';
2279 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2280 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2281 S += '<';
2282 S += Proto->getNameAsString();
2283 S += '>';
2284 }
2285 S += '"';
2286 }
2287 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002288 }
2289 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002290 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002291 bool isReadOnly = false;
2292 // For historical/compatibility reasons, the read-only qualifier of the
2293 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2294 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2295 // Also, do not emit the 'r' for anything but the outermost type!
2296 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2297 if (OutermostType && T.isConstQualified()) {
2298 isReadOnly = true;
2299 S += 'r';
2300 }
2301 }
2302 else if (OutermostType) {
2303 QualType P = PointeeTy;
2304 while (P->getAsPointerType())
2305 P = P->getAsPointerType()->getPointeeType();
2306 if (P.isConstQualified()) {
2307 isReadOnly = true;
2308 S += 'r';
2309 }
2310 }
2311 if (isReadOnly) {
2312 // Another legacy compatibility encoding. Some ObjC qualifier and type
2313 // combinations need to be rearranged.
2314 // Rewrite "in const" from "nr" to "rn"
2315 const char * s = S.c_str();
2316 int len = S.length();
2317 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2318 std::string replace = "rn";
2319 S.replace(S.end()-2, S.end(), replace);
2320 }
2321 }
Steve Naroff389bf462009-02-12 17:52:19 +00002322 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002323 S += '@';
2324 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002325 }
2326 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002327 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002328 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002329 // Another historical/compatibility reason.
2330 // We encode the underlying type which comes out as
2331 // {...};
2332 S += '^';
2333 getObjCEncodingForTypeImpl(PointeeTy, S,
2334 false, ExpandPointedToStructures,
2335 NULL);
2336 return;
2337 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002338 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002339 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002340 const ObjCInterfaceType *OIT =
2341 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002342 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002343 S += '"';
2344 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002345 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2346 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2347 S += '<';
2348 S += Proto->getNameAsString();
2349 S += '>';
2350 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002351 S += '"';
2352 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002353 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002354 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002355 S += '#';
2356 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002357 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002358 S += ':';
2359 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002360 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002361
2362 if (PointeeTy->isCharType()) {
2363 // char pointer types should be encoded as '*' unless it is a
2364 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002365 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002366 S += '*';
2367 return;
2368 }
2369 }
2370
2371 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002372 getLegacyIntegralTypeEncoding(PointeeTy);
2373
2374 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002375 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002376 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002377 } else if (const ArrayType *AT =
2378 // Ignore type qualifiers etc.
2379 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002380 if (isa<IncompleteArrayType>(AT)) {
2381 // Incomplete arrays are encoded as a pointer to the array element.
2382 S += '^';
2383
2384 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2385 false, ExpandStructures, FD);
2386 } else {
2387 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002388
Anders Carlsson559a8332009-02-22 01:38:57 +00002389 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2390 S += llvm::utostr(CAT->getSize().getZExtValue());
2391 else {
2392 //Variable length arrays are encoded as a regular array with 0 elements.
2393 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2394 S += '0';
2395 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002396
Anders Carlsson559a8332009-02-22 01:38:57 +00002397 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2398 false, ExpandStructures, FD);
2399 S += ']';
2400 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002401 } else if (T->getAsFunctionType()) {
2402 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002403 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002404 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002405 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002406 // Anonymous structures print as '?'
2407 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2408 S += II->getName();
2409 } else {
2410 S += '?';
2411 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002412 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002413 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002414 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2415 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002416 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002417 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002418 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002419 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002420 S += '"';
2421 }
2422
2423 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002424 if (Field->isBitField()) {
2425 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2426 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002427 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002428 QualType qt = Field->getType();
2429 getLegacyIntegralTypeEncoding(qt);
2430 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002431 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002432 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002433 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002434 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002435 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002436 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002437 if (FD && FD->isBitField())
2438 EncodeBitField(this, S, FD);
2439 else
2440 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002441 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002442 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002443 } else if (T->isObjCInterfaceType()) {
2444 // @encode(class_name)
2445 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2446 S += '{';
2447 const IdentifierInfo *II = OI->getIdentifier();
2448 S += II->getName();
2449 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002450 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002451 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002452 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002453 if (RecFields[i]->isBitField())
2454 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2455 RecFields[i]);
2456 else
2457 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2458 FD);
2459 }
2460 S += '}';
2461 }
2462 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002463 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002464}
2465
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002466void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002467 std::string& S) const {
2468 if (QT & Decl::OBJC_TQ_In)
2469 S += 'n';
2470 if (QT & Decl::OBJC_TQ_Inout)
2471 S += 'N';
2472 if (QT & Decl::OBJC_TQ_Out)
2473 S += 'o';
2474 if (QT & Decl::OBJC_TQ_Bycopy)
2475 S += 'O';
2476 if (QT & Decl::OBJC_TQ_Byref)
2477 S += 'R';
2478 if (QT & Decl::OBJC_TQ_Oneway)
2479 S += 'V';
2480}
2481
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002482void ASTContext::setBuiltinVaListType(QualType T)
2483{
2484 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2485
2486 BuiltinVaListType = T;
2487}
2488
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002489void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002490{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002491 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002492
2493 // typedef struct objc_object *id;
2494 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002495 // User error - caller will issue diagnostics.
2496 if (!ptr)
2497 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002498 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002499 // User error - caller will issue diagnostics.
2500 if (!rec)
2501 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002502 IdStructType = rec;
2503}
2504
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002505void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002506{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002507 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002508
2509 // typedef struct objc_selector *SEL;
2510 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002511 if (!ptr)
2512 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002513 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002514 if (!rec)
2515 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002516 SelStructType = rec;
2517}
2518
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002519void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002520{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002521 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002522}
2523
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002524void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002525{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002526 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002527
2528 // typedef struct objc_class *Class;
2529 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2530 assert(ptr && "'Class' incorrectly typed");
2531 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2532 assert(rec && "'Class' incorrectly typed");
2533 ClassStructType = rec;
2534}
2535
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002536void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2537 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002538 "'NSConstantString' type already set!");
2539
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002540 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002541}
2542
Douglas Gregor7532dc62009-03-30 22:58:21 +00002543/// \brief Retrieve the template name that represents a qualified
2544/// template name such as \c std::vector.
2545TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2546 bool TemplateKeyword,
2547 TemplateDecl *Template) {
2548 llvm::FoldingSetNodeID ID;
2549 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2550
2551 void *InsertPos = 0;
2552 QualifiedTemplateName *QTN =
2553 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2554 if (!QTN) {
2555 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2556 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2557 }
2558
2559 return TemplateName(QTN);
2560}
2561
2562/// \brief Retrieve the template name that represents a dependent
2563/// template name such as \c MetaFun::template apply.
2564TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2565 const IdentifierInfo *Name) {
2566 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2567
2568 llvm::FoldingSetNodeID ID;
2569 DependentTemplateName::Profile(ID, NNS, Name);
2570
2571 void *InsertPos = 0;
2572 DependentTemplateName *QTN =
2573 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2574
2575 if (QTN)
2576 return TemplateName(QTN);
2577
2578 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2579 if (CanonNNS == NNS) {
2580 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2581 } else {
2582 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2583 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2584 }
2585
2586 DependentTemplateNames.InsertNode(QTN, InsertPos);
2587 return TemplateName(QTN);
2588}
2589
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002590/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002591/// TargetInfo, produce the corresponding type. The unsigned @p Type
2592/// is actually a value of type @c TargetInfo::IntType.
2593QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002594 switch (Type) {
2595 case TargetInfo::NoInt: return QualType();
2596 case TargetInfo::SignedShort: return ShortTy;
2597 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2598 case TargetInfo::SignedInt: return IntTy;
2599 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2600 case TargetInfo::SignedLong: return LongTy;
2601 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2602 case TargetInfo::SignedLongLong: return LongLongTy;
2603 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2604 }
2605
2606 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002607 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002608}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002609
2610//===----------------------------------------------------------------------===//
2611// Type Predicates.
2612//===----------------------------------------------------------------------===//
2613
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002614/// isObjCNSObjectType - Return true if this is an NSObject object using
2615/// NSObject attribute on a c-style pointer type.
2616/// FIXME - Make it work directly on types.
2617///
2618bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2619 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2620 if (TypedefDecl *TD = TDT->getDecl())
2621 if (TD->getAttr<ObjCNSObjectAttr>())
2622 return true;
2623 }
2624 return false;
2625}
2626
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002627/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2628/// to an object type. This includes "id" and "Class" (two 'special' pointers
2629/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2630/// ID type).
2631bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002632 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002633 return true;
2634
Steve Naroff6ae98502008-10-21 18:24:04 +00002635 // Blocks are objects.
2636 if (Ty->isBlockPointerType())
2637 return true;
2638
2639 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002640 if (!Ty->isPointerType())
2641 return false;
2642
2643 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2644 // pointer types. This looks for the typedef specifically, not for the
2645 // underlying type.
Eli Friedman5fdeae12009-03-22 23:00:19 +00002646 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2647 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002648 return true;
2649
2650 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002651 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2652 return true;
2653
2654 // If is has NSObject attribute, OK as well.
2655 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002656}
2657
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002658/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2659/// garbage collection attribute.
2660///
2661QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002662 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002663 if (getLangOptions().ObjC1 &&
2664 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002665 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002666 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002667 // (or pointers to them) be treated as though they were declared
2668 // as __strong.
2669 if (GCAttrs == QualType::GCNone) {
2670 if (isObjCObjectPointerType(Ty))
2671 GCAttrs = QualType::Strong;
2672 else if (Ty->isPointerType())
2673 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2674 }
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002675 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002676 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002677}
2678
Chris Lattner6ac46a42008-04-07 06:51:04 +00002679//===----------------------------------------------------------------------===//
2680// Type Compatibility Testing
2681//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002682
Steve Naroff1c7d0672008-09-04 15:10:53 +00002683/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002684/// block types. Types must be strictly compatible here. For example,
2685/// C unfortunately doesn't produce an error for the following:
2686///
2687/// int (*emptyArgFunc)();
2688/// int (*intArgList)(int) = emptyArgFunc;
2689///
2690/// For blocks, we will produce an error for the following (similar to C++):
2691///
2692/// int (^emptyArgBlock)();
2693/// int (^intArgBlock)(int) = emptyArgBlock;
2694///
2695/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2696///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002697bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002698 const FunctionType *lbase = lhs->getAsFunctionType();
2699 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002700 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2701 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002702 if (lproto && rproto == 0)
2703 return false;
2704 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002705}
2706
Chris Lattner6ac46a42008-04-07 06:51:04 +00002707/// areCompatVectorTypes - Return true if the two specified vector types are
2708/// compatible.
2709static bool areCompatVectorTypes(const VectorType *LHS,
2710 const VectorType *RHS) {
2711 assert(LHS->isCanonical() && RHS->isCanonical());
2712 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002713 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002714}
2715
Eli Friedman3d815e72008-08-22 00:56:42 +00002716/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002717/// compatible for assignment from RHS to LHS. This handles validation of any
2718/// protocol qualifiers on the LHS or RHS.
2719///
Eli Friedman3d815e72008-08-22 00:56:42 +00002720bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2721 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002722 // Verify that the base decls are compatible: the RHS must be a subclass of
2723 // the LHS.
2724 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2725 return false;
2726
2727 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2728 // protocol qualified at all, then we are good.
2729 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2730 return true;
2731
2732 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2733 // isn't a superset.
2734 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2735 return true; // FIXME: should return false!
2736
2737 // Finally, we must have two protocol-qualified interfaces.
2738 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2739 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002740
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002741 // All LHS protocols must have a presence on the RHS.
2742 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002743
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002744 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2745 LHSPE = LHSP->qual_end();
2746 LHSPI != LHSPE; LHSPI++) {
2747 bool RHSImplementsProtocol = false;
2748
2749 // If the RHS doesn't implement the protocol on the left, the types
2750 // are incompatible.
2751 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2752 RHSPE = RHSP->qual_end();
2753 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2754 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2755 RHSImplementsProtocol = true;
2756 }
2757 // FIXME: For better diagnostics, consider passing back the protocol name.
2758 if (!RHSImplementsProtocol)
2759 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002760 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002761 // The RHS implements all protocols listed on the LHS.
2762 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002763}
2764
Steve Naroff389bf462009-02-12 17:52:19 +00002765bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2766 // get the "pointed to" types
2767 const PointerType *LHSPT = LHS->getAsPointerType();
2768 const PointerType *RHSPT = RHS->getAsPointerType();
2769
2770 if (!LHSPT || !RHSPT)
2771 return false;
2772
2773 QualType lhptee = LHSPT->getPointeeType();
2774 QualType rhptee = RHSPT->getPointeeType();
2775 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2776 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2777 // ID acts sort of like void* for ObjC interfaces
2778 if (LHSIface && isObjCIdStructType(rhptee))
2779 return true;
2780 if (RHSIface && isObjCIdStructType(lhptee))
2781 return true;
2782 if (!LHSIface || !RHSIface)
2783 return false;
2784 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2785 canAssignObjCInterfaces(RHSIface, LHSIface);
2786}
2787
Steve Naroffec0550f2007-10-15 20:41:53 +00002788/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2789/// both shall have the identically qualified version of a compatible type.
2790/// C99 6.2.7p1: Two types have compatible types if their types are the
2791/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002792bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2793 return !mergeTypes(LHS, RHS).isNull();
2794}
2795
2796QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2797 const FunctionType *lbase = lhs->getAsFunctionType();
2798 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002799 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2800 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002801 bool allLTypes = true;
2802 bool allRTypes = true;
2803
2804 // Check return type
2805 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2806 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002807 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2808 allLTypes = false;
2809 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2810 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002811
2812 if (lproto && rproto) { // two C99 style function prototypes
2813 unsigned lproto_nargs = lproto->getNumArgs();
2814 unsigned rproto_nargs = rproto->getNumArgs();
2815
2816 // Compatible functions must have the same number of arguments
2817 if (lproto_nargs != rproto_nargs)
2818 return QualType();
2819
2820 // Variadic and non-variadic functions aren't compatible
2821 if (lproto->isVariadic() != rproto->isVariadic())
2822 return QualType();
2823
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002824 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2825 return QualType();
2826
Eli Friedman3d815e72008-08-22 00:56:42 +00002827 // Check argument compatibility
2828 llvm::SmallVector<QualType, 10> types;
2829 for (unsigned i = 0; i < lproto_nargs; i++) {
2830 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2831 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2832 QualType argtype = mergeTypes(largtype, rargtype);
2833 if (argtype.isNull()) return QualType();
2834 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002835 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2836 allLTypes = false;
2837 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2838 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002839 }
2840 if (allLTypes) return lhs;
2841 if (allRTypes) return rhs;
2842 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002843 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002844 }
2845
2846 if (lproto) allRTypes = false;
2847 if (rproto) allLTypes = false;
2848
Douglas Gregor72564e72009-02-26 23:50:07 +00002849 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002850 if (proto) {
2851 if (proto->isVariadic()) return QualType();
2852 // Check that the types are compatible with the types that
2853 // would result from default argument promotions (C99 6.7.5.3p15).
2854 // The only types actually affected are promotable integer
2855 // types and floats, which would be passed as a different
2856 // type depending on whether the prototype is visible.
2857 unsigned proto_nargs = proto->getNumArgs();
2858 for (unsigned i = 0; i < proto_nargs; ++i) {
2859 QualType argTy = proto->getArgType(i);
2860 if (argTy->isPromotableIntegerType() ||
2861 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2862 return QualType();
2863 }
2864
2865 if (allLTypes) return lhs;
2866 if (allRTypes) return rhs;
2867 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002868 proto->getNumArgs(), lproto->isVariadic(),
2869 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002870 }
2871
2872 if (allLTypes) return lhs;
2873 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002874 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002875}
2876
2877QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002878 // C++ [expr]: If an expression initially has the type "reference to T", the
2879 // type is adjusted to "T" prior to any further analysis, the expression
2880 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002881 // expression is an lvalue unless the reference is an rvalue reference and
2882 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002883 // FIXME: C++ shouldn't be going through here! The rules are different
2884 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002885 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2886 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002887 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002888 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002889 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002890 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002891
Eli Friedman3d815e72008-08-22 00:56:42 +00002892 QualType LHSCan = getCanonicalType(LHS),
2893 RHSCan = getCanonicalType(RHS);
2894
2895 // If two types are identical, they are compatible.
2896 if (LHSCan == RHSCan)
2897 return LHS;
2898
2899 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002900 // Note that we handle extended qualifiers later, in the
2901 // case for ExtQualType.
2902 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002903 return QualType();
2904
2905 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2906 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2907
Chris Lattner1adb8832008-01-14 05:45:46 +00002908 // We want to consider the two function types to be the same for these
2909 // comparisons, just force one to the other.
2910 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2911 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002912
2913 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002914 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2915 LHSClass = Type::ConstantArray;
2916 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2917 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002918
Nate Begeman213541a2008-04-18 23:10:10 +00002919 // Canonicalize ExtVector -> Vector.
2920 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2921 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002922
Chris Lattnerb0489812008-04-07 06:38:24 +00002923 // Consider qualified interfaces and interfaces the same.
2924 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2925 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002926
Chris Lattnera36a61f2008-04-07 05:43:21 +00002927 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002928 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002929 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2930 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2931
2932 // ID acts sort of like void* for ObjC interfaces
2933 if (LHSIface && isObjCIdStructType(RHS))
2934 return LHS;
2935 if (RHSIface && isObjCIdStructType(LHS))
2936 return RHS;
2937
Steve Naroffbc76dd02008-12-10 22:14:21 +00002938 // ID is compatible with all qualified id types.
2939 if (LHS->isObjCQualifiedIdType()) {
2940 if (const PointerType *PT = RHS->getAsPointerType()) {
2941 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002942 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002943 return LHS;
2944 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2945 // Unfortunately, this API is part of Sema (which we don't have access
2946 // to. Need to refactor. The following check is insufficient, since we
2947 // need to make sure the class implements the protocol.
2948 if (pType->isObjCInterfaceType())
2949 return LHS;
2950 }
2951 }
2952 if (RHS->isObjCQualifiedIdType()) {
2953 if (const PointerType *PT = LHS->getAsPointerType()) {
2954 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002955 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002956 return RHS;
2957 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2958 // Unfortunately, this API is part of Sema (which we don't have access
2959 // to. Need to refactor. The following check is insufficient, since we
2960 // need to make sure the class implements the protocol.
2961 if (pType->isObjCInterfaceType())
2962 return RHS;
2963 }
2964 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002965 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2966 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002967 if (const EnumType* ETy = LHS->getAsEnumType()) {
2968 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2969 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002970 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002971 if (const EnumType* ETy = RHS->getAsEnumType()) {
2972 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2973 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002974 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002975
Eli Friedman3d815e72008-08-22 00:56:42 +00002976 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002977 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002978
Steve Naroff4a746782008-01-09 22:43:08 +00002979 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002980 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00002981#define TYPE(Class, Base)
2982#define ABSTRACT_TYPE(Class, Base)
2983#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2984#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2985#include "clang/AST/TypeNodes.def"
2986 assert(false && "Non-canonical and dependent types shouldn't get here");
2987 return QualType();
2988
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002989 case Type::LValueReference:
2990 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00002991 case Type::MemberPointer:
2992 assert(false && "C++ should never be in mergeTypes");
2993 return QualType();
2994
2995 case Type::IncompleteArray:
2996 case Type::VariableArray:
2997 case Type::FunctionProto:
2998 case Type::ExtVector:
2999 case Type::ObjCQualifiedInterface:
3000 assert(false && "Types are eliminated above");
3001 return QualType();
3002
Chris Lattner1adb8832008-01-14 05:45:46 +00003003 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003004 {
3005 // Merge two pointer types, while trying to preserve typedef info
3006 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3007 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3008 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3009 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003010 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3011 return LHS;
3012 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3013 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003014 return getPointerType(ResultType);
3015 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003016 case Type::BlockPointer:
3017 {
3018 // Merge two block pointer types, while trying to preserve typedef info
3019 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3020 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3021 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3022 if (ResultType.isNull()) return QualType();
3023 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3024 return LHS;
3025 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3026 return RHS;
3027 return getBlockPointerType(ResultType);
3028 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003029 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003030 {
3031 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3032 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3033 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3034 return QualType();
3035
3036 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3037 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3038 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3039 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003040 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3041 return LHS;
3042 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3043 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003044 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3045 ArrayType::ArraySizeModifier(), 0);
3046 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3047 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003048 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3049 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003050 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3051 return LHS;
3052 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3053 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003054 if (LVAT) {
3055 // FIXME: This isn't correct! But tricky to implement because
3056 // the array's size has to be the size of LHS, but the type
3057 // has to be different.
3058 return LHS;
3059 }
3060 if (RVAT) {
3061 // FIXME: This isn't correct! But tricky to implement because
3062 // the array's size has to be the size of RHS, but the type
3063 // has to be different.
3064 return RHS;
3065 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003066 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3067 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003068 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003069 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003070 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003071 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003072 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003073 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003074 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003075 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3076 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003077 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003078 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003079 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003080 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003081 case Type::Complex:
3082 // Distinct complex types are incompatible.
3083 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003084 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003085 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003086 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3087 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003088 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003089 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003090 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003091 // FIXME: This should be type compatibility, e.g. whether
3092 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003093 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3094 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3095 if (LHSIface && RHSIface &&
3096 canAssignObjCInterfaces(LHSIface, RHSIface))
3097 return LHS;
3098
Eli Friedman3d815e72008-08-22 00:56:42 +00003099 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003100 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003101 case Type::ObjCQualifiedId:
3102 // Distinct qualified id's are not compatible.
3103 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003104 case Type::FixedWidthInt:
3105 // Distinct fixed-width integers are not compatible.
3106 return QualType();
3107 case Type::ObjCQualifiedClass:
3108 // Distinct qualified classes are not compatible.
3109 return QualType();
3110 case Type::ExtQual:
3111 // FIXME: ExtQual types can be compatible even if they're not
3112 // identical!
3113 return QualType();
3114 // First attempt at an implementation, but I'm not really sure it's
3115 // right...
3116#if 0
3117 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3118 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3119 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3120 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3121 return QualType();
3122 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3123 LHSBase = QualType(LQual->getBaseType(), 0);
3124 RHSBase = QualType(RQual->getBaseType(), 0);
3125 ResultType = mergeTypes(LHSBase, RHSBase);
3126 if (ResultType.isNull()) return QualType();
3127 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3128 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3129 return LHS;
3130 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3131 return RHS;
3132 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3133 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3134 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3135 return ResultType;
3136#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003137
3138 case Type::TemplateSpecialization:
3139 assert(false && "Dependent types have no size");
3140 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003141 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003142
3143 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003144}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003145
Chris Lattner5426bf62008-04-07 07:01:58 +00003146//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003147// Integer Predicates
3148//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003149
Eli Friedmanad74a752008-06-28 06:23:08 +00003150unsigned ASTContext::getIntWidth(QualType T) {
3151 if (T == BoolTy)
3152 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003153 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3154 return FWIT->getWidth();
3155 }
3156 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003157 return (unsigned)getTypeSize(T);
3158}
3159
3160QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3161 assert(T->isSignedIntegerType() && "Unexpected type");
3162 if (const EnumType* ETy = T->getAsEnumType())
3163 T = ETy->getDecl()->getIntegerType();
3164 const BuiltinType* BTy = T->getAsBuiltinType();
3165 assert (BTy && "Unexpected signed integer type");
3166 switch (BTy->getKind()) {
3167 case BuiltinType::Char_S:
3168 case BuiltinType::SChar:
3169 return UnsignedCharTy;
3170 case BuiltinType::Short:
3171 return UnsignedShortTy;
3172 case BuiltinType::Int:
3173 return UnsignedIntTy;
3174 case BuiltinType::Long:
3175 return UnsignedLongTy;
3176 case BuiltinType::LongLong:
3177 return UnsignedLongLongTy;
3178 default:
3179 assert(0 && "Unexpected signed integer type");
3180 return QualType();
3181 }
3182}
3183
3184
3185//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00003186// Serialization Support
3187//===----------------------------------------------------------------------===//
3188
Chris Lattnera9376d42009-03-28 03:45:20 +00003189enum {
3190 BasicMetadataBlock = 1,
3191 ASTContextBlock = 2,
3192 DeclsBlock = 3
3193};
3194
Chris Lattner557c5b12009-03-28 04:27:18 +00003195void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3196 // Create bitstream.
3197 llvm::BitstreamWriter Stream(Buffer);
3198
3199 // Emit the preamble.
3200 Stream.Emit((unsigned)'B', 8);
3201 Stream.Emit((unsigned)'C', 8);
3202 Stream.Emit(0xC, 4);
3203 Stream.Emit(0xF, 4);
3204 Stream.Emit(0xE, 4);
3205 Stream.Emit(0x0, 4);
3206
3207 // Create serializer.
3208 llvm::Serializer S(Stream);
3209
Chris Lattnera9376d42009-03-28 03:45:20 +00003210 // ===---------------------------------------------------===/
3211 // Serialize the "Translation Unit" metadata.
3212 // ===---------------------------------------------------===/
3213
3214 // Emit ASTContext.
3215 S.EnterBlock(ASTContextBlock);
3216 S.EmitOwnedPtr(this);
3217 S.ExitBlock(); // exit "ASTContextBlock"
3218
3219 S.EnterBlock(BasicMetadataBlock);
3220
3221 // Block for SourceManager and Target. Allows easy skipping
3222 // around to the block for the Selectors during deserialization.
3223 S.EnterBlock();
3224
3225 // Emit the SourceManager.
3226 S.Emit(getSourceManager());
3227
3228 // Emit the Target.
3229 S.EmitPtr(&Target);
3230 S.EmitCStr(Target.getTargetTriple());
3231
3232 S.ExitBlock(); // exit "SourceManager and Target Block"
3233
3234 // Emit the Selectors.
3235 S.Emit(Selectors);
3236
3237 // Emit the Identifier Table.
3238 S.Emit(Idents);
3239
3240 S.ExitBlock(); // exit "BasicMetadataBlock"
3241}
3242
3243
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003244/// Emit - Serialize an ASTContext object to Bitcode.
3245void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003246 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00003247 S.EmitRef(SourceMgr);
3248 S.EmitRef(Target);
3249 S.EmitRef(Idents);
3250 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003251
Ted Kremenekfee04522007-10-31 22:44:07 +00003252 // Emit the size of the type vector so that we can reserve that size
3253 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00003254 S.EmitInt(Types.size());
3255
Ted Kremenek03ed4402007-11-13 22:02:55 +00003256 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3257 I!=E;++I)
3258 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00003259
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003260 S.EmitOwnedPtr(TUDecl);
3261
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003262 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003263}
3264
Chris Lattner557c5b12009-03-28 04:27:18 +00003265
3266ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3267 FileManager &FMgr) {
3268 // Check if the file is of the proper length.
3269 if (Buffer.getBufferSize() & 0x3) {
3270 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3271 return 0;
3272 }
3273
3274 // Create the bitstream reader.
3275 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3276 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3277
3278 if (Stream.Read(8) != 'B' ||
3279 Stream.Read(8) != 'C' ||
3280 Stream.Read(4) != 0xC ||
3281 Stream.Read(4) != 0xF ||
3282 Stream.Read(4) != 0xE ||
3283 Stream.Read(4) != 0x0) {
3284 // FIXME: Provide diagnostic.
3285 return NULL;
3286 }
3287
3288 // Create the deserializer.
3289 llvm::Deserializer Dezr(Stream);
3290
Chris Lattnera9376d42009-03-28 03:45:20 +00003291 // ===---------------------------------------------------===/
3292 // Deserialize the "Translation Unit" metadata.
3293 // ===---------------------------------------------------===/
3294
3295 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3296 // (which will appear earlier) and record its location.
3297
3298 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3299 assert (FoundBlock);
3300
3301 llvm::Deserializer::Location ASTContextBlockLoc =
3302 Dezr.getCurrentBlockLocation();
3303
3304 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3305 assert (FoundBlock);
3306
3307 // Read the SourceManager.
3308 SourceManager::CreateAndRegister(Dezr, FMgr);
3309
3310 { // Read the TargetInfo.
3311 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3312 char* triple = Dezr.ReadCStr(NULL,0,true);
3313 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3314 delete [] triple;
3315 }
3316
3317 // For Selectors, we must read the identifier table first because the
3318 // SelectorTable depends on the identifiers being already deserialized.
3319 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3320 Dezr.SkipBlock();
3321
3322 // Read the identifier table.
3323 IdentifierTable::CreateAndRegister(Dezr);
3324
3325 // Now jump back and read the selectors.
3326 Dezr.JumpTo(SelectorBlkLoc);
3327 SelectorTable::CreateAndRegister(Dezr);
3328
3329 // Now jump back to ASTContextBlock and read the ASTContext.
3330 Dezr.JumpTo(ASTContextBlockLoc);
3331 return Dezr.ReadOwnedPtr<ASTContext>();
3332}
3333
Ted Kremenek0f84c002007-11-13 00:25:37 +00003334ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003335
3336 // Read the language options.
3337 LangOptions LOpts;
3338 LOpts.Read(D);
3339
Ted Kremenekfee04522007-10-31 22:44:07 +00003340 SourceManager &SM = D.ReadRef<SourceManager>();
3341 TargetInfo &t = D.ReadRef<TargetInfo>();
3342 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3343 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00003344
Ted Kremenekfee04522007-10-31 22:44:07 +00003345 unsigned size_reserve = D.ReadInt();
3346
Douglas Gregor2e1cd422008-11-17 14:58:09 +00003347 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3348 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00003349
Ted Kremenek03ed4402007-11-13 22:02:55 +00003350 for (unsigned i = 0; i < size_reserve; ++i)
3351 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00003352
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003353 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3354
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003355 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00003356
3357 return A;
3358}