blob: cede0e556391cfee3d53f4161767e0bf644b47cf [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;
Reid Spencer5f016e22007-07-11 17:01:13 +0000106
107 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
108 Type *T = Types[i];
109 if (isa<BuiltinType>(T))
110 ++NumBuiltin;
111 else if (isa<PointerType>(T))
112 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000113 else if (isa<BlockPointerType>(T))
114 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000115 else if (isa<LValueReferenceType>(T))
116 ++NumLValueReference;
117 else if (isa<RValueReferenceType>(T))
118 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000119 else if (isa<MemberPointerType>(T))
120 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000121 else if (isa<ComplexType>(T))
122 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 else if (isa<ArrayType>(T))
124 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000125 else if (isa<VectorType>(T))
126 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000127 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000129 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 ++NumFunctionP;
131 else if (isa<TypedefType>(T))
132 ++NumTypeName;
133 else if (TagType *TT = dyn_cast<TagType>(T)) {
134 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000135 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000136 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000137 case TagDecl::TK_struct: ++NumTagStruct; break;
138 case TagDecl::TK_union: ++NumTagUnion; break;
139 case TagDecl::TK_class: ++NumTagClass; break;
140 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000142 } else if (isa<ObjCInterfaceType>(T))
143 ++NumObjCInterfaces;
144 else if (isa<ObjCQualifiedInterfaceType>(T))
145 ++NumObjCQualifiedInterfaces;
146 else if (isa<ObjCQualifiedIdType>(T))
147 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000148 else if (isa<TypeOfType>(T))
149 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000150 else if (isa<TypeOfExprType>(T))
151 ++NumTypeOfExprTypes;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000152 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000153 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000154 assert(0 && "Unknown type!");
155 }
156 }
157
158 fprintf(stderr, " %d builtin types\n", NumBuiltin);
159 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000160 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000161 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
162 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000163 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000164 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000166 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000167 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
168 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
169 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
170 fprintf(stderr, " %d tagged types\n", NumTagged);
171 fprintf(stderr, " %d struct types\n", NumTagStruct);
172 fprintf(stderr, " %d union types\n", NumTagUnion);
173 fprintf(stderr, " %d class types\n", NumTagClass);
174 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000175 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000176 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000177 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000178 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000179 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000180 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000181 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000182
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
184 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000185 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000186 NumLValueReference*sizeof(LValueReferenceType)+
187 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000188 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000189 NumFunctionP*sizeof(FunctionProtoType)+
190 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000191 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000192 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000193}
194
195
196void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000197 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000198}
199
Reid Spencer5f016e22007-07-11 17:01:13 +0000200void ASTContext::InitBuiltinTypes() {
201 assert(VoidTy.isNull() && "Context reinitialized?");
202
203 // C99 6.2.5p19.
204 InitBuiltinType(VoidTy, BuiltinType::Void);
205
206 // C99 6.2.5p2.
207 InitBuiltinType(BoolTy, BuiltinType::Bool);
208 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000209 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000210 InitBuiltinType(CharTy, BuiltinType::Char_S);
211 else
212 InitBuiltinType(CharTy, BuiltinType::Char_U);
213 // C99 6.2.5p4.
214 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
215 InitBuiltinType(ShortTy, BuiltinType::Short);
216 InitBuiltinType(IntTy, BuiltinType::Int);
217 InitBuiltinType(LongTy, BuiltinType::Long);
218 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
219
220 // C99 6.2.5p6.
221 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
222 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
223 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
224 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
225 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
226
227 // C99 6.2.5p10.
228 InitBuiltinType(FloatTy, BuiltinType::Float);
229 InitBuiltinType(DoubleTy, BuiltinType::Double);
230 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000231
Chris Lattner3a250322009-02-26 23:43:47 +0000232 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
233 InitBuiltinType(WCharTy, BuiltinType::WChar);
234 else // C99
235 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000236
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000237 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000238 InitBuiltinType(OverloadTy, BuiltinType::Overload);
239
240 // Placeholder type for type-dependent expressions whose type is
241 // completely unknown. No code should ever check a type against
242 // DependentTy and users should never see it; however, it is here to
243 // help diagnose failures to properly check for type-dependent
244 // expressions.
245 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000246
Reid Spencer5f016e22007-07-11 17:01:13 +0000247 // C99 6.2.5p11.
248 FloatComplexTy = getComplexType(FloatTy);
249 DoubleComplexTy = getComplexType(DoubleTy);
250 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000251
Steve Naroff7e219e42007-10-15 14:41:52 +0000252 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000253 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000254 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000255 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000256 ClassStructType = 0;
257
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000258 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000259
260 // void * type
261 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000262}
263
Chris Lattner464175b2007-07-18 17:52:12 +0000264//===----------------------------------------------------------------------===//
265// Type Sizing and Analysis
266//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000267
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000268/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
269/// scalar floating point type.
270const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
271 const BuiltinType *BT = T->getAsBuiltinType();
272 assert(BT && "Not a floating point type!");
273 switch (BT->getKind()) {
274 default: assert(0 && "Not a floating point type!");
275 case BuiltinType::Float: return Target.getFloatFormat();
276 case BuiltinType::Double: return Target.getDoubleFormat();
277 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
278 }
279}
280
Chris Lattneraf707ab2009-01-24 21:53:27 +0000281/// getDeclAlign - Return a conservative estimate of the alignment of the
282/// specified decl. Note that bitfields do not have a valid alignment, so
283/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000284unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000285 unsigned Align = Target.getCharWidth();
286
287 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
288 Align = std::max(Align, AA->getAlignment());
289
Chris Lattneraf707ab2009-01-24 21:53:27 +0000290 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
291 QualType T = VD->getType();
292 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000293 if (!T->isIncompleteType() && !T->isFunctionType()) {
294 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
295 T = cast<ArrayType>(T)->getElementType();
296
297 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
298 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000299 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000300
301 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000302}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000303
Chris Lattnera7674d82007-07-13 22:13:22 +0000304/// getTypeSize - Return the size of the specified type, in bits. This method
305/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000306std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000307ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000308 T = getCanonicalType(T);
Mike Stump5e301002009-02-27 18:32:39 +0000309 uint64_t Width=0;
310 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000311 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000312#define TYPE(Class, Base)
313#define ABSTRACT_TYPE(Class, Base)
314#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
315#define DEPENDENT_TYPE(Class, Base) case Type::Class:
316#include "clang/AST/TypeNodes.def"
317 assert(false && "Should not see non-canonical or dependent types");
318 break;
319
Chris Lattner692233e2007-07-13 22:27:08 +0000320 case Type::FunctionNoProto:
321 case Type::FunctionProto:
Douglas Gregor72564e72009-02-26 23:50:07 +0000322 case Type::IncompleteArray:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000323 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000324 case Type::VariableArray:
325 assert(0 && "VLAs not implemented yet!");
326 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000327 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000328
Chris Lattner98be4942008-03-05 18:54:05 +0000329 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000330 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000331 Align = EltInfo.second;
332 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000333 }
Nate Begeman213541a2008-04-18 23:10:10 +0000334 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000335 case Type::Vector: {
336 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000337 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000338 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000339 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000340 // If the alignment is not a power of 2, round up to the next power of 2.
341 // This happens for non-power-of-2 length vectors.
342 // FIXME: this should probably be a target property.
343 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000344 break;
345 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000346
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000347 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000348 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000349 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000350 case BuiltinType::Void:
351 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000352 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000353 Width = Target.getBoolWidth();
354 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000355 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000356 case BuiltinType::Char_S:
357 case BuiltinType::Char_U:
358 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000359 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000360 Width = Target.getCharWidth();
361 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000362 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000363 case BuiltinType::WChar:
364 Width = Target.getWCharWidth();
365 Align = Target.getWCharAlign();
366 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000367 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000368 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000369 Width = Target.getShortWidth();
370 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000371 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000372 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000373 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000374 Width = Target.getIntWidth();
375 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000376 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000377 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000378 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000379 Width = Target.getLongWidth();
380 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000381 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000382 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000383 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000384 Width = Target.getLongLongWidth();
385 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000386 break;
387 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000388 Width = Target.getFloatWidth();
389 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000390 break;
391 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000392 Width = Target.getDoubleWidth();
393 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000394 break;
395 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000396 Width = Target.getLongDoubleWidth();
397 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000398 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000399 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000400 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000401 case Type::FixedWidthInt:
402 // FIXME: This isn't precisely correct; the width/alignment should depend
403 // on the available types for the target
404 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000405 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000406 Align = Width;
407 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000408 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000409 // FIXME: Pointers into different addr spaces could have different sizes and
410 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000411 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000412 case Type::ObjCQualifiedId:
Eli Friedman4bdf0872009-02-22 04:02:33 +0000413 case Type::ObjCQualifiedClass:
Douglas Gregor72564e72009-02-26 23:50:07 +0000414 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000415 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000416 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000417 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000418 case Type::BlockPointer: {
419 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
420 Width = Target.getPointerWidth(AS);
421 Align = Target.getPointerAlign(AS);
422 break;
423 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000424 case Type::Pointer: {
425 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000426 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000427 Align = Target.getPointerAlign(AS);
428 break;
429 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000430 case Type::LValueReference:
431 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000432 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000433 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000434 // FIXME: This is wrong for struct layout: a reference in a struct has
435 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000436 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000437 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000438 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000439 // the GCC ABI, where pointers to data are one pointer large, pointers to
440 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000441 // other compilers too, we need to delegate this completely to TargetInfo
442 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000443 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
444 unsigned AS = Pointee.getAddressSpace();
445 Width = Target.getPointerWidth(AS);
446 if (Pointee->isFunctionType())
447 Width *= 2;
448 Align = Target.getPointerAlign(AS);
449 // GCC aligns at single pointer width.
450 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000451 case Type::Complex: {
452 // Complex types have the same alignment as their elements, but twice the
453 // size.
454 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000455 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000456 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000457 Align = EltInfo.second;
458 break;
459 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000460 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000461 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000462 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
463 Width = Layout.getSize();
464 Align = Layout.getAlignment();
465 break;
466 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000467 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000468 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000469 const TagType *TT = cast<TagType>(T);
470
471 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000472 Width = 1;
473 Align = 1;
474 break;
475 }
476
Daniel Dunbar1d751182008-11-08 05:48:37 +0000477 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000478 return getTypeInfo(ET->getDecl()->getIntegerType());
479
Daniel Dunbar1d751182008-11-08 05:48:37 +0000480 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000481 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
482 Width = Layout.getSize();
483 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000484 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000485 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000486
487 case Type::TemplateSpecialization:
488 assert(false && "Dependent types have no size");
489 break;
Chris Lattner71763312008-04-06 22:05:18 +0000490 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000491
Chris Lattner464175b2007-07-18 17:52:12 +0000492 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000493 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000494}
495
Chris Lattner34ebde42009-01-27 18:08:34 +0000496/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
497/// type for the current target in bits. This can be different than the ABI
498/// alignment in cases where it is beneficial for performance to overalign
499/// a data type.
500unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
501 unsigned ABIAlign = getTypeAlign(T);
502
503 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000504 if (T->isSpecificBuiltinType(BuiltinType::Double))
505 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000506
507 return ABIAlign;
508}
509
510
Devang Patel8b277042008-06-04 21:22:16 +0000511/// LayoutField - Field layout.
512void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000513 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000514 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000515 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000516 uint64_t FieldOffset = IsUnion ? 0 : Size;
517 uint64_t FieldSize;
518 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000519
520 // FIXME: Should this override struct packing? Probably we want to
521 // take the minimum?
522 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
523 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000524
525 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
526 // TODO: Need to check this algorithm on other targets!
527 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000528 FieldSize =
529 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000530
531 std::pair<uint64_t, unsigned> FieldInfo =
532 Context.getTypeInfo(FD->getType());
533 uint64_t TypeSize = FieldInfo.first;
534
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000535 // Determine the alignment of this bitfield. The packing
536 // attributes define a maximum and the alignment attribute defines
537 // a minimum.
538 // FIXME: What is the right behavior when the specified alignment
539 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000540 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000541 if (FieldPacking)
542 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000543 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
544 FieldAlign = std::max(FieldAlign, AA->getAlignment());
545
546 // Check if we need to add padding to give the field the correct
547 // alignment.
548 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
549 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
550
551 // Padding members don't affect overall alignment
552 if (!FD->getIdentifier())
553 FieldAlign = 1;
554 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000555 if (FD->getType()->isIncompleteArrayType()) {
556 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000557 // query getTypeInfo about these, so we figure it out here.
558 // Flexible array members don't have any size, but they
559 // have to be aligned appropriately for their element type.
560 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000561 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000562 FieldAlign = Context.getTypeAlign(ATy->getElementType());
563 } else {
564 std::pair<uint64_t, unsigned> FieldInfo =
565 Context.getTypeInfo(FD->getType());
566 FieldSize = FieldInfo.first;
567 FieldAlign = FieldInfo.second;
568 }
569
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000570 // Determine the alignment of this bitfield. The packing
571 // attributes define a maximum and the alignment attribute defines
572 // a minimum. Additionally, the packing alignment must be at least
573 // a byte for non-bitfields.
574 //
575 // FIXME: What is the right behavior when the specified alignment
576 // is smaller than the specified packing?
577 if (FieldPacking)
578 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000579 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
580 FieldAlign = std::max(FieldAlign, AA->getAlignment());
581
582 // Round up the current record size to the field's alignment boundary.
583 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
584 }
585
586 // Place this field at the current location.
587 FieldOffsets[FieldNo] = FieldOffset;
588
589 // Reserve space for this field.
590 if (IsUnion) {
591 Size = std::max(Size, FieldSize);
592 } else {
593 Size = FieldOffset + FieldSize;
594 }
595
596 // Remember max struct/class alignment.
597 Alignment = std::max(Alignment, FieldAlign);
598}
599
Fariborz Jahanian88e469c2009-03-05 20:08:48 +0000600void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
Chris Lattnerf1690852009-03-31 08:48:01 +0000601 llvm::SmallVectorImpl<FieldDecl*> &Fields) const {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000602 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
603 if (SuperClass)
604 CollectObjCIvars(SuperClass, Fields);
605 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
606 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000607 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000608 if (!IVDecl->isInvalidDecl())
609 Fields.push_back(cast<FieldDecl>(IVDecl));
610 }
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +0000611 // look into properties.
612 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(),
613 E = OI->prop_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000614 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +0000615 Fields.push_back(cast<FieldDecl>(IV));
616 }
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000617}
618
619/// addRecordToClass - produces record info. for the class for its
620/// ivars and all those inherited.
621///
Chris Lattnerf1690852009-03-31 08:48:01 +0000622const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) {
Chris Lattner23499252009-03-31 09:24:30 +0000623 RecordDecl *&RD = ASTRecordForInterface[D];
624 if (RD) {
625 // If we have a record decl already and it is either a definition or if 'D'
626 // is still a forward declaration, return it.
627 if (RD->isDefinition() || D->isForwardDecl())
628 return RD;
629 }
630
631 // If D is a forward declaration, then just make a forward struct decl.
632 if (D->isForwardDecl())
633 return RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
634 D->getLocation(),
635 D->getIdentifier());
Chris Lattnerf1690852009-03-31 08:48:01 +0000636
637 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000638 CollectObjCIvars(D, RecFields);
Chris Lattner23499252009-03-31 09:24:30 +0000639
640 if (RD == 0)
641 RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(),
642 D->getIdentifier());
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000643 /// FIXME! Can do collection of ivars and adding to the record while
644 /// doing it.
Chris Lattner16ff7052009-03-31 08:58:42 +0000645 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Chris Lattner23499252009-03-31 09:24:30 +0000646 RD->addDecl(FieldDecl::Create(*this, RD,
647 RecFields[i]->getLocation(),
648 RecFields[i]->getIdentifier(),
649 RecFields[i]->getType(),
650 RecFields[i]->getBitWidth(), false));
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000651 }
Chris Lattnerf1690852009-03-31 08:48:01 +0000652
Chris Lattner23499252009-03-31 09:24:30 +0000653 RD->completeDefinition(*this);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000654 return RD;
655}
Devang Patel44a3dde2008-06-04 21:54:36 +0000656
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000657/// setFieldDecl - maps a field for the given Ivar reference node.
658//
659void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
660 const ObjCIvarDecl *Ivar,
661 const ObjCIvarRefExpr *MRef) {
Chris Lattnerda046392009-03-31 08:31:13 +0000662 ASTFieldForIvarRef[MRef] = OI->lookupFieldDeclForIvar(*this, Ivar);
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000663}
664
Chris Lattner61710852008-10-05 17:34:18 +0000665/// getASTObjcInterfaceLayout - Get or compute information about the layout of
666/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000667/// position information.
668const ASTRecordLayout &
669ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
670 // Look up this layout, if already laid out, return what we have.
671 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
672 if (Entry) return *Entry;
673
674 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
675 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000676 ASTRecordLayout *NewEntry = NULL;
677 unsigned FieldCount = D->ivar_size();
678 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
679 FieldCount++;
680 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
681 unsigned Alignment = SL.getAlignment();
682 uint64_t Size = SL.getSize();
683 NewEntry = new ASTRecordLayout(Size, Alignment);
684 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000685 // Super class is at the beginning of the layout.
686 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000687 } else {
688 NewEntry = new ASTRecordLayout();
689 NewEntry->InitializeLayout(FieldCount);
690 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000691 Entry = NewEntry;
692
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000693 unsigned StructPacking = 0;
694 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
695 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000696
697 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
698 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
699 AA->getAlignment()));
700
701 // Layout each ivar sequentially.
702 unsigned i = 0;
703 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
704 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
705 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000706 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000707 }
Fariborz Jahanian18191882009-03-31 18:11:23 +0000708 // Also synthesized ivars
709 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(),
710 E = D->prop_end(); I != E; ++I) {
711 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
712 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
713 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000714
715 // Finally, round the size of the total struct up to the alignment of the
716 // struct itself.
717 NewEntry->FinalizeLayout();
718 return *NewEntry;
719}
720
Devang Patel88a981b2007-11-01 19:11:01 +0000721/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000722/// specified record (struct/union/class), which indicates its size and field
723/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000724const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000725 D = D->getDefinition(*this);
726 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000727
Chris Lattner464175b2007-07-18 17:52:12 +0000728 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000729 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000730 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000731
Devang Patel88a981b2007-11-01 19:11:01 +0000732 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
733 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
734 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000735 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000736
Douglas Gregore267ff32008-12-11 20:41:00 +0000737 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000738 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000739 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000740
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000741 unsigned StructPacking = 0;
742 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
743 StructPacking = PA->getAlignment();
744
Eli Friedman4bd998b2008-05-30 09:31:38 +0000745 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000746 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
747 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000748
Eli Friedman4bd998b2008-05-30 09:31:38 +0000749 // Layout each field, for now, just sequentially, respecting alignment. In
750 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000751 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000752 for (RecordDecl::field_iterator Field = D->field_begin(),
753 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000754 Field != FieldEnd; (void)++Field, ++FieldIdx)
755 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000756
757 // Finally, round the size of the total struct up to the alignment of the
758 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000759 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000760 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000761}
762
Chris Lattnera7674d82007-07-13 22:13:22 +0000763//===----------------------------------------------------------------------===//
764// Type creation/memoization methods
765//===----------------------------------------------------------------------===//
766
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000767QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000768 QualType CanT = getCanonicalType(T);
769 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000770 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000771
772 // If we are composing extended qualifiers together, merge together into one
773 // ExtQualType node.
774 unsigned CVRQuals = T.getCVRQualifiers();
775 QualType::GCAttrTypes GCAttr = QualType::GCNone;
776 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000777
Chris Lattnerb7d25532009-02-18 22:53:11 +0000778 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
779 // If this type already has an address space specified, it cannot get
780 // another one.
781 assert(EQT->getAddressSpace() == 0 &&
782 "Type cannot be in multiple addr spaces!");
783 GCAttr = EQT->getObjCGCAttr();
784 TypeNode = EQT->getBaseType();
785 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000786
Chris Lattnerb7d25532009-02-18 22:53:11 +0000787 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000788 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000789 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000790 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000791 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000792 return QualType(EXTQy, CVRQuals);
793
Christopher Lambebb97e92008-02-04 02:31:56 +0000794 // If the base type isn't canonical, this won't be a canonical type either,
795 // so fill in the canonical type field.
796 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000797 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000798 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000799
Chris Lattnerb7d25532009-02-18 22:53:11 +0000800 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000801 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000802 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000803 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000804 ExtQualType *New =
805 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000806 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000807 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000808 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000809}
810
Chris Lattnerb7d25532009-02-18 22:53:11 +0000811QualType ASTContext::getObjCGCQualType(QualType T,
812 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000813 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000814 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000815 return T;
816
Chris Lattnerb7d25532009-02-18 22:53:11 +0000817 // If we are composing extended qualifiers together, merge together into one
818 // ExtQualType node.
819 unsigned CVRQuals = T.getCVRQualifiers();
820 Type *TypeNode = T.getTypePtr();
821 unsigned AddressSpace = 0;
822
823 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
824 // If this type already has an address space specified, it cannot get
825 // another one.
826 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
827 "Type cannot be in multiple addr spaces!");
828 AddressSpace = EQT->getAddressSpace();
829 TypeNode = EQT->getBaseType();
830 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000831
832 // Check if we've already instantiated an gc qual'd type of this type.
833 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000834 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000835 void *InsertPos = 0;
836 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000837 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000838
839 // If the base type isn't canonical, this won't be a canonical type either,
840 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000841 // FIXME: Isn't this also not canonical if the base type is a array
842 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000843 QualType Canonical;
844 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000845 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000846
Chris Lattnerb7d25532009-02-18 22:53:11 +0000847 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000848 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
849 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
850 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000851 ExtQualType *New =
852 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000853 ExtQualTypes.InsertNode(New, InsertPos);
854 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000855 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000856}
Chris Lattnera7674d82007-07-13 22:13:22 +0000857
Reid Spencer5f016e22007-07-11 17:01:13 +0000858/// getComplexType - Return the uniqued reference to the type for a complex
859/// number with the specified element type.
860QualType ASTContext::getComplexType(QualType T) {
861 // Unique pointers, to guarantee there is only one pointer of a particular
862 // structure.
863 llvm::FoldingSetNodeID ID;
864 ComplexType::Profile(ID, T);
865
866 void *InsertPos = 0;
867 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
868 return QualType(CT, 0);
869
870 // If the pointee type isn't canonical, this won't be a canonical type either,
871 // so fill in the canonical type field.
872 QualType Canonical;
873 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000874 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000875
876 // Get the new insert position for the node we care about.
877 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000878 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 }
Steve Narofff83820b2009-01-27 22:08:43 +0000880 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 Types.push_back(New);
882 ComplexTypes.InsertNode(New, InsertPos);
883 return QualType(New, 0);
884}
885
Eli Friedmanf98aba32009-02-13 02:31:07 +0000886QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
887 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
888 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
889 FixedWidthIntType *&Entry = Map[Width];
890 if (!Entry)
891 Entry = new FixedWidthIntType(Width, Signed);
892 return QualType(Entry, 0);
893}
Reid Spencer5f016e22007-07-11 17:01:13 +0000894
895/// getPointerType - Return the uniqued reference to the type for a pointer to
896/// the specified type.
897QualType ASTContext::getPointerType(QualType T) {
898 // Unique pointers, to guarantee there is only one pointer of a particular
899 // structure.
900 llvm::FoldingSetNodeID ID;
901 PointerType::Profile(ID, T);
902
903 void *InsertPos = 0;
904 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
905 return QualType(PT, 0);
906
907 // If the pointee type isn't canonical, this won't be a canonical type either,
908 // so fill in the canonical type field.
909 QualType Canonical;
910 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000911 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000912
913 // Get the new insert position for the node we care about.
914 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000915 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000916 }
Steve Narofff83820b2009-01-27 22:08:43 +0000917 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000918 Types.push_back(New);
919 PointerTypes.InsertNode(New, InsertPos);
920 return QualType(New, 0);
921}
922
Steve Naroff5618bd42008-08-27 16:04:49 +0000923/// getBlockPointerType - Return the uniqued reference to the type for
924/// a pointer to the specified block.
925QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000926 assert(T->isFunctionType() && "block of function types only");
927 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000928 // structure.
929 llvm::FoldingSetNodeID ID;
930 BlockPointerType::Profile(ID, T);
931
932 void *InsertPos = 0;
933 if (BlockPointerType *PT =
934 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
935 return QualType(PT, 0);
936
Steve Naroff296e8d52008-08-28 19:20:44 +0000937 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000938 // type either so fill in the canonical type field.
939 QualType Canonical;
940 if (!T->isCanonical()) {
941 Canonical = getBlockPointerType(getCanonicalType(T));
942
943 // Get the new insert position for the node we care about.
944 BlockPointerType *NewIP =
945 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000946 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000947 }
Steve Narofff83820b2009-01-27 22:08:43 +0000948 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000949 Types.push_back(New);
950 BlockPointerTypes.InsertNode(New, InsertPos);
951 return QualType(New, 0);
952}
953
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000954/// getLValueReferenceType - Return the uniqued reference to the type for an
955/// lvalue reference to the specified type.
956QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000957 // Unique pointers, to guarantee there is only one pointer of a particular
958 // structure.
959 llvm::FoldingSetNodeID ID;
960 ReferenceType::Profile(ID, T);
961
962 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000963 if (LValueReferenceType *RT =
964 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000965 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000966
Reid Spencer5f016e22007-07-11 17:01:13 +0000967 // If the referencee type isn't canonical, this won't be a canonical type
968 // either, so fill in the canonical type field.
969 QualType Canonical;
970 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000971 Canonical = getLValueReferenceType(getCanonicalType(T));
972
Reid Spencer5f016e22007-07-11 17:01:13 +0000973 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000974 LValueReferenceType *NewIP =
975 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000976 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000977 }
978
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000979 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000980 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000981 LValueReferenceTypes.InsertNode(New, InsertPos);
982 return QualType(New, 0);
983}
984
985/// getRValueReferenceType - Return the uniqued reference to the type for an
986/// rvalue reference to the specified type.
987QualType ASTContext::getRValueReferenceType(QualType T) {
988 // Unique pointers, to guarantee there is only one pointer of a particular
989 // structure.
990 llvm::FoldingSetNodeID ID;
991 ReferenceType::Profile(ID, T);
992
993 void *InsertPos = 0;
994 if (RValueReferenceType *RT =
995 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
996 return QualType(RT, 0);
997
998 // If the referencee type isn't canonical, this won't be a canonical type
999 // either, so fill in the canonical type field.
1000 QualType Canonical;
1001 if (!T->isCanonical()) {
1002 Canonical = getRValueReferenceType(getCanonicalType(T));
1003
1004 // Get the new insert position for the node we care about.
1005 RValueReferenceType *NewIP =
1006 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1007 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1008 }
1009
1010 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1011 Types.push_back(New);
1012 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 return QualType(New, 0);
1014}
1015
Sebastian Redlf30208a2009-01-24 21:16:55 +00001016/// getMemberPointerType - Return the uniqued reference to the type for a
1017/// member pointer to the specified type, in the specified class.
1018QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1019{
1020 // Unique pointers, to guarantee there is only one pointer of a particular
1021 // structure.
1022 llvm::FoldingSetNodeID ID;
1023 MemberPointerType::Profile(ID, T, Cls);
1024
1025 void *InsertPos = 0;
1026 if (MemberPointerType *PT =
1027 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1028 return QualType(PT, 0);
1029
1030 // If the pointee or class type isn't canonical, this won't be a canonical
1031 // type either, so fill in the canonical type field.
1032 QualType Canonical;
1033 if (!T->isCanonical()) {
1034 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1035
1036 // Get the new insert position for the node we care about.
1037 MemberPointerType *NewIP =
1038 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1039 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1040 }
Steve Narofff83820b2009-01-27 22:08:43 +00001041 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001042 Types.push_back(New);
1043 MemberPointerTypes.InsertNode(New, InsertPos);
1044 return QualType(New, 0);
1045}
1046
Steve Narofffb22d962007-08-30 01:06:46 +00001047/// getConstantArrayType - Return the unique reference to the type for an
1048/// array of the specified element type.
1049QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001050 const llvm::APInt &ArySize,
1051 ArrayType::ArraySizeModifier ASM,
1052 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001053 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001054 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001055
1056 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001057 if (ConstantArrayType *ATP =
1058 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001059 return QualType(ATP, 0);
1060
1061 // If the element type isn't canonical, this won't be a canonical type either,
1062 // so fill in the canonical type field.
1063 QualType Canonical;
1064 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001065 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001066 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001067 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001068 ConstantArrayType *NewIP =
1069 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001070 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 }
1072
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001073 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001074 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001075 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 Types.push_back(New);
1077 return QualType(New, 0);
1078}
1079
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001080/// getVariableArrayType - Returns a non-unique reference to the type for a
1081/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001082QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1083 ArrayType::ArraySizeModifier ASM,
1084 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001085 // Since we don't unique expressions, it isn't possible to unique VLA's
1086 // that have an expression provided for their size.
1087
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001088 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001089 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001090
1091 VariableArrayTypes.push_back(New);
1092 Types.push_back(New);
1093 return QualType(New, 0);
1094}
1095
Douglas Gregor898574e2008-12-05 23:32:09 +00001096/// getDependentSizedArrayType - Returns a non-unique reference to
1097/// the type for a dependently-sized array of the specified element
1098/// type. FIXME: We will need these to be uniqued, or at least
1099/// comparable, at some point.
1100QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1101 ArrayType::ArraySizeModifier ASM,
1102 unsigned EltTypeQuals) {
1103 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1104 "Size must be type- or value-dependent!");
1105
1106 // Since we don't unique expressions, it isn't possible to unique
1107 // dependently-sized array types.
1108
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001109 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001110 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1111 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001112
1113 DependentSizedArrayTypes.push_back(New);
1114 Types.push_back(New);
1115 return QualType(New, 0);
1116}
1117
Eli Friedmanc5773c42008-02-15 18:16:39 +00001118QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1119 ArrayType::ArraySizeModifier ASM,
1120 unsigned EltTypeQuals) {
1121 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001122 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001123
1124 void *InsertPos = 0;
1125 if (IncompleteArrayType *ATP =
1126 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1127 return QualType(ATP, 0);
1128
1129 // If the element type isn't canonical, this won't be a canonical type
1130 // either, so fill in the canonical type field.
1131 QualType Canonical;
1132
1133 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001134 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001135 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001136
1137 // Get the new insert position for the node we care about.
1138 IncompleteArrayType *NewIP =
1139 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001140 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001141 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001142
Steve Narofff83820b2009-01-27 22:08:43 +00001143 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001144 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001145
1146 IncompleteArrayTypes.InsertNode(New, InsertPos);
1147 Types.push_back(New);
1148 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001149}
1150
Steve Naroff73322922007-07-18 18:00:27 +00001151/// getVectorType - Return the unique reference to a vector type of
1152/// the specified element type and size. VectorType must be a built-in type.
1153QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 BuiltinType *baseType;
1155
Chris Lattnerf52ab252008-04-06 22:59:24 +00001156 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001157 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001158
1159 // Check if we've already instantiated a vector of this type.
1160 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001161 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 void *InsertPos = 0;
1163 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1164 return QualType(VTP, 0);
1165
1166 // If the element type isn't canonical, this won't be a canonical type either,
1167 // so fill in the canonical type field.
1168 QualType Canonical;
1169 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001170 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001171
1172 // Get the new insert position for the node we care about.
1173 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001174 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 }
Steve Narofff83820b2009-01-27 22:08:43 +00001176 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001177 VectorTypes.InsertNode(New, InsertPos);
1178 Types.push_back(New);
1179 return QualType(New, 0);
1180}
1181
Nate Begeman213541a2008-04-18 23:10:10 +00001182/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001183/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001184QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001185 BuiltinType *baseType;
1186
Chris Lattnerf52ab252008-04-06 22:59:24 +00001187 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001188 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001189
1190 // Check if we've already instantiated a vector of this type.
1191 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001192 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001193 void *InsertPos = 0;
1194 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1195 return QualType(VTP, 0);
1196
1197 // If the element type isn't canonical, this won't be a canonical type either,
1198 // so fill in the canonical type field.
1199 QualType Canonical;
1200 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001201 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001202
1203 // Get the new insert position for the node we care about.
1204 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001205 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001206 }
Steve Narofff83820b2009-01-27 22:08:43 +00001207 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001208 VectorTypes.InsertNode(New, InsertPos);
1209 Types.push_back(New);
1210 return QualType(New, 0);
1211}
1212
Douglas Gregor72564e72009-02-26 23:50:07 +00001213/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001214///
Douglas Gregor72564e72009-02-26 23:50:07 +00001215QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 // Unique functions, to guarantee there is only one function of a particular
1217 // structure.
1218 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001219 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001220
1221 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001222 if (FunctionNoProtoType *FT =
1223 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 return QualType(FT, 0);
1225
1226 QualType Canonical;
1227 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001228 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001229
1230 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001231 FunctionNoProtoType *NewIP =
1232 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001233 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 }
1235
Douglas Gregor72564e72009-02-26 23:50:07 +00001236 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001237 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001238 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001239 return QualType(New, 0);
1240}
1241
1242/// getFunctionType - Return a normal function type with a typed argument
1243/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001244QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001245 unsigned NumArgs, bool isVariadic,
1246 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 // Unique functions, to guarantee there is only one function of a particular
1248 // structure.
1249 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001250 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001251 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001252
1253 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001254 if (FunctionProtoType *FTP =
1255 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 return QualType(FTP, 0);
1257
1258 // Determine whether the type being created is already canonical or not.
1259 bool isCanonical = ResultTy->isCanonical();
1260 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1261 if (!ArgArray[i]->isCanonical())
1262 isCanonical = false;
1263
1264 // If this type isn't canonical, get the canonical version of it.
1265 QualType Canonical;
1266 if (!isCanonical) {
1267 llvm::SmallVector<QualType, 16> CanonicalArgs;
1268 CanonicalArgs.reserve(NumArgs);
1269 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001270 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001271
Chris Lattnerf52ab252008-04-06 22:59:24 +00001272 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001274 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001275
1276 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001277 FunctionProtoType *NewIP =
1278 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001279 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 }
1281
Douglas Gregor72564e72009-02-26 23:50:07 +00001282 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001283 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001284 FunctionProtoType *FTP =
1285 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001286 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001287 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001288 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001289 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001290 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 return QualType(FTP, 0);
1292}
1293
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001294/// getTypeDeclType - Return the unique reference to the type for the
1295/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001296QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001297 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001298 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1299
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001300 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001301 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001302 else if (isa<TemplateTypeParmDecl>(Decl)) {
1303 assert(false && "Template type parameter types are always available.");
1304 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001305 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001306
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001307 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001308 if (PrevDecl)
1309 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001310 else
1311 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001312 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001313 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1314 if (PrevDecl)
1315 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001316 else
1317 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001318 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001319 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001320 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001321
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001322 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001323 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001324}
1325
Reid Spencer5f016e22007-07-11 17:01:13 +00001326/// getTypedefType - Return the unique reference to the type for the
1327/// specified typename decl.
1328QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1329 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1330
Chris Lattnerf52ab252008-04-06 22:59:24 +00001331 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001332 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001333 Types.push_back(Decl->TypeForDecl);
1334 return QualType(Decl->TypeForDecl, 0);
1335}
1336
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001337/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001338/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001339QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001340 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1341
Steve Narofff83820b2009-01-27 22:08:43 +00001342 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001343 Types.push_back(Decl->TypeForDecl);
1344 return QualType(Decl->TypeForDecl, 0);
1345}
1346
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001347/// buildObjCInterfaceType - Returns a new type for the interface
1348/// declaration, regardless. It also removes any previously built
1349/// record declaration so caller can rebuild it.
1350QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Chris Lattner23499252009-03-31 09:24:30 +00001351 RecordDecl *&RD = ASTRecordForInterface[Decl];
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001352 if (RD)
1353 RD = 0;
1354 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1355 Types.push_back(Decl->TypeForDecl);
1356 return QualType(Decl->TypeForDecl, 0);
1357}
1358
Douglas Gregorfab9d672009-02-05 23:33:38 +00001359/// \brief Retrieve the template type parameter type for a template
1360/// parameter with the given depth, index, and (optionally) name.
1361QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1362 IdentifierInfo *Name) {
1363 llvm::FoldingSetNodeID ID;
1364 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1365 void *InsertPos = 0;
1366 TemplateTypeParmType *TypeParm
1367 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1368
1369 if (TypeParm)
1370 return QualType(TypeParm, 0);
1371
1372 if (Name)
1373 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1374 getTemplateTypeParmType(Depth, Index));
1375 else
1376 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1377
1378 Types.push_back(TypeParm);
1379 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1380
1381 return QualType(TypeParm, 0);
1382}
1383
Douglas Gregor55f6b142009-02-09 18:46:07 +00001384QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001385ASTContext::getTemplateSpecializationType(TemplateName Template,
1386 const TemplateArgument *Args,
1387 unsigned NumArgs,
1388 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001389 if (!Canon.isNull())
1390 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001391
Douglas Gregor55f6b142009-02-09 18:46:07 +00001392 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001393 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001394
Douglas Gregor55f6b142009-02-09 18:46:07 +00001395 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001396 TemplateSpecializationType *Spec
1397 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001398
1399 if (Spec)
1400 return QualType(Spec, 0);
1401
Douglas Gregor7532dc62009-03-30 22:58:21 +00001402 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001403 sizeof(TemplateArgument) * NumArgs),
1404 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001405 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001406 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001407 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001408
1409 return QualType(Spec, 0);
1410}
1411
Douglas Gregore4e5b052009-03-19 00:18:19 +00001412QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001413ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001414 QualType NamedType) {
1415 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001416 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001417
1418 void *InsertPos = 0;
1419 QualifiedNameType *T
1420 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1421 if (T)
1422 return QualType(T, 0);
1423
Douglas Gregorab452ba2009-03-26 23:50:42 +00001424 T = new (*this) QualifiedNameType(NNS, NamedType,
1425 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001426 Types.push_back(T);
1427 QualifiedNameTypes.InsertNode(T, InsertPos);
1428 return QualType(T, 0);
1429}
1430
Douglas Gregord57959a2009-03-27 23:10:48 +00001431QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1432 const IdentifierInfo *Name,
1433 QualType Canon) {
1434 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1435
1436 if (Canon.isNull()) {
1437 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1438 if (CanonNNS != NNS)
1439 Canon = getTypenameType(CanonNNS, Name);
1440 }
1441
1442 llvm::FoldingSetNodeID ID;
1443 TypenameType::Profile(ID, NNS, Name);
1444
1445 void *InsertPos = 0;
1446 TypenameType *T
1447 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1448 if (T)
1449 return QualType(T, 0);
1450
1451 T = new (*this) TypenameType(NNS, Name, Canon);
1452 Types.push_back(T);
1453 TypenameTypes.InsertNode(T, InsertPos);
1454 return QualType(T, 0);
1455}
1456
Douglas Gregor17343172009-04-01 00:28:59 +00001457QualType
1458ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1459 const TemplateSpecializationType *TemplateId,
1460 QualType Canon) {
1461 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1462
1463 if (Canon.isNull()) {
1464 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1465 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1466 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1467 const TemplateSpecializationType *CanonTemplateId
1468 = CanonType->getAsTemplateSpecializationType();
1469 assert(CanonTemplateId &&
1470 "Canonical type must also be a template specialization type");
1471 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1472 }
1473 }
1474
1475 llvm::FoldingSetNodeID ID;
1476 TypenameType::Profile(ID, NNS, TemplateId);
1477
1478 void *InsertPos = 0;
1479 TypenameType *T
1480 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1481 if (T)
1482 return QualType(T, 0);
1483
1484 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1485 Types.push_back(T);
1486 TypenameTypes.InsertNode(T, InsertPos);
1487 return QualType(T, 0);
1488}
1489
Chris Lattner88cb27a2008-04-07 04:56:42 +00001490/// CmpProtocolNames - Comparison predicate for sorting protocols
1491/// alphabetically.
1492static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1493 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001494 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001495}
1496
1497static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1498 unsigned &NumProtocols) {
1499 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1500
1501 // Sort protocols, keyed by name.
1502 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1503
1504 // Remove duplicates.
1505 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1506 NumProtocols = ProtocolsEnd-Protocols;
1507}
1508
1509
Chris Lattner065f0d72008-04-07 04:44:08 +00001510/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1511/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001512QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1513 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001514 // Sort the protocol list alphabetically to canonicalize it.
1515 SortAndUniqueProtocols(Protocols, NumProtocols);
1516
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001517 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001518 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001519
1520 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001521 if (ObjCQualifiedInterfaceType *QT =
1522 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001523 return QualType(QT, 0);
1524
1525 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001526 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001527 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001528
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001529 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001530 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001531 return QualType(QType, 0);
1532}
1533
Chris Lattner88cb27a2008-04-07 04:56:42 +00001534/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1535/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001536QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001537 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001538 // Sort the protocol list alphabetically to canonicalize it.
1539 SortAndUniqueProtocols(Protocols, NumProtocols);
1540
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001541 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001542 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001543
1544 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001545 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001546 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001547 return QualType(QT, 0);
1548
1549 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001550 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001551 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001552 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001553 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001554 return QualType(QType, 0);
1555}
1556
Douglas Gregor72564e72009-02-26 23:50:07 +00001557/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1558/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001559/// multiple declarations that refer to "typeof(x)" all contain different
1560/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1561/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001562QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001563 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001564 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001565 Types.push_back(toe);
1566 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001567}
1568
Steve Naroff9752f252007-08-01 18:02:17 +00001569/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1570/// TypeOfType AST's. The only motivation to unique these nodes would be
1571/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1572/// an issue. This doesn't effect the type checker, since it operates
1573/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001574QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001575 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001576 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001577 Types.push_back(tot);
1578 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001579}
1580
Reid Spencer5f016e22007-07-11 17:01:13 +00001581/// getTagDeclType - Return the unique reference to the type for the
1582/// specified TagDecl (struct/union/class/enum) decl.
1583QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001584 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001585 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001586}
1587
1588/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1589/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1590/// needs to agree with the definition in <stddef.h>.
1591QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001592 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001593}
1594
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001595/// getSignedWCharType - Return the type of "signed wchar_t".
1596/// Used when in C++, as a GCC extension.
1597QualType ASTContext::getSignedWCharType() const {
1598 // FIXME: derive from "Target" ?
1599 return WCharTy;
1600}
1601
1602/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1603/// Used when in C++, as a GCC extension.
1604QualType ASTContext::getUnsignedWCharType() const {
1605 // FIXME: derive from "Target" ?
1606 return UnsignedIntTy;
1607}
1608
Chris Lattner8b9023b2007-07-13 03:05:23 +00001609/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1610/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1611QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001612 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001613}
1614
Chris Lattnere6327742008-04-02 05:18:44 +00001615//===----------------------------------------------------------------------===//
1616// Type Operators
1617//===----------------------------------------------------------------------===//
1618
Chris Lattner77c96472008-04-06 22:41:35 +00001619/// getCanonicalType - Return the canonical (structural) type corresponding to
1620/// the specified potentially non-canonical type. The non-canonical version
1621/// of a type may have many "decorated" versions of types. Decorators can
1622/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1623/// to be free of any of these, allowing two canonical types to be compared
1624/// for exact equality with a simple pointer comparison.
1625QualType ASTContext::getCanonicalType(QualType T) {
1626 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001627
1628 // If the result has type qualifiers, make sure to canonicalize them as well.
1629 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1630 if (TypeQuals == 0) return CanType;
1631
1632 // If the type qualifiers are on an array type, get the canonical type of the
1633 // array with the qualifiers applied to the element type.
1634 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1635 if (!AT)
1636 return CanType.getQualifiedType(TypeQuals);
1637
1638 // Get the canonical version of the element with the extra qualifiers on it.
1639 // This can recursively sink qualifiers through multiple levels of arrays.
1640 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1641 NewEltTy = getCanonicalType(NewEltTy);
1642
1643 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1644 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1645 CAT->getIndexTypeQualifier());
1646 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1647 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1648 IAT->getIndexTypeQualifier());
1649
Douglas Gregor898574e2008-12-05 23:32:09 +00001650 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1651 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1652 DSAT->getSizeModifier(),
1653 DSAT->getIndexTypeQualifier());
1654
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001655 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1656 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1657 VAT->getSizeModifier(),
1658 VAT->getIndexTypeQualifier());
1659}
1660
Douglas Gregord57959a2009-03-27 23:10:48 +00001661NestedNameSpecifier *
1662ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1663 if (!NNS)
1664 return 0;
1665
1666 switch (NNS->getKind()) {
1667 case NestedNameSpecifier::Identifier:
1668 // Canonicalize the prefix but keep the identifier the same.
1669 return NestedNameSpecifier::Create(*this,
1670 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1671 NNS->getAsIdentifier());
1672
1673 case NestedNameSpecifier::Namespace:
1674 // A namespace is canonical; build a nested-name-specifier with
1675 // this namespace and no prefix.
1676 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1677
1678 case NestedNameSpecifier::TypeSpec:
1679 case NestedNameSpecifier::TypeSpecWithTemplate: {
1680 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1681 NestedNameSpecifier *Prefix = 0;
1682
1683 // FIXME: This isn't the right check!
1684 if (T->isDependentType())
1685 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1686
1687 return NestedNameSpecifier::Create(*this, Prefix,
1688 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1689 T.getTypePtr());
1690 }
1691
1692 case NestedNameSpecifier::Global:
1693 // The global specifier is canonical and unique.
1694 return NNS;
1695 }
1696
1697 // Required to silence a GCC warning
1698 return 0;
1699}
1700
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001701
1702const ArrayType *ASTContext::getAsArrayType(QualType T) {
1703 // Handle the non-qualified case efficiently.
1704 if (T.getCVRQualifiers() == 0) {
1705 // Handle the common positive case fast.
1706 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1707 return AT;
1708 }
1709
1710 // Handle the common negative case fast, ignoring CVR qualifiers.
1711 QualType CType = T->getCanonicalTypeInternal();
1712
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001713 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001714 // test.
1715 if (!isa<ArrayType>(CType) &&
1716 !isa<ArrayType>(CType.getUnqualifiedType()))
1717 return 0;
1718
1719 // Apply any CVR qualifiers from the array type to the element type. This
1720 // implements C99 6.7.3p8: "If the specification of an array type includes
1721 // any type qualifiers, the element type is so qualified, not the array type."
1722
1723 // If we get here, we either have type qualifiers on the type, or we have
1724 // sugar such as a typedef in the way. If we have type qualifiers on the type
1725 // we must propagate them down into the elemeng type.
1726 unsigned CVRQuals = T.getCVRQualifiers();
1727 unsigned AddrSpace = 0;
1728 Type *Ty = T.getTypePtr();
1729
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001730 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001731 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001732 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1733 AddrSpace = EXTQT->getAddressSpace();
1734 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001735 } else {
1736 T = Ty->getDesugaredType();
1737 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1738 break;
1739 CVRQuals |= T.getCVRQualifiers();
1740 Ty = T.getTypePtr();
1741 }
1742 }
1743
1744 // If we have a simple case, just return now.
1745 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1746 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1747 return ATy;
1748
1749 // Otherwise, we have an array and we have qualifiers on it. Push the
1750 // qualifiers into the array element type and return a new array type.
1751 // Get the canonical version of the element with the extra qualifiers on it.
1752 // This can recursively sink qualifiers through multiple levels of arrays.
1753 QualType NewEltTy = ATy->getElementType();
1754 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001755 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001756 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1757
1758 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1759 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1760 CAT->getSizeModifier(),
1761 CAT->getIndexTypeQualifier()));
1762 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1763 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1764 IAT->getSizeModifier(),
1765 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001766
Douglas Gregor898574e2008-12-05 23:32:09 +00001767 if (const DependentSizedArrayType *DSAT
1768 = dyn_cast<DependentSizedArrayType>(ATy))
1769 return cast<ArrayType>(
1770 getDependentSizedArrayType(NewEltTy,
1771 DSAT->getSizeExpr(),
1772 DSAT->getSizeModifier(),
1773 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001774
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001775 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1776 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1777 VAT->getSizeModifier(),
1778 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001779}
1780
1781
Chris Lattnere6327742008-04-02 05:18:44 +00001782/// getArrayDecayedType - Return the properly qualified result of decaying the
1783/// specified array type to a pointer. This operation is non-trivial when
1784/// handling typedefs etc. The canonical type of "T" must be an array type,
1785/// this returns a pointer to a properly qualified element of the array.
1786///
1787/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1788QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001789 // Get the element type with 'getAsArrayType' so that we don't lose any
1790 // typedefs in the element type of the array. This also handles propagation
1791 // of type qualifiers from the array type into the element type if present
1792 // (C99 6.7.3p8).
1793 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1794 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001795
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001796 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001797
1798 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001799 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001800}
1801
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001802QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001803 QualType ElemTy = VAT->getElementType();
1804
1805 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1806 return getBaseElementType(VAT);
1807
1808 return ElemTy;
1809}
1810
Reid Spencer5f016e22007-07-11 17:01:13 +00001811/// getFloatingRank - Return a relative rank for floating point types.
1812/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001813static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001814 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001815 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001816
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001817 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001818 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001819 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001820 case BuiltinType::Float: return FloatRank;
1821 case BuiltinType::Double: return DoubleRank;
1822 case BuiltinType::LongDouble: return LongDoubleRank;
1823 }
1824}
1825
Steve Naroff716c7302007-08-27 01:41:48 +00001826/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1827/// point or a complex type (based on typeDomain/typeSize).
1828/// 'typeDomain' is a real floating point or complex type.
1829/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001830QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1831 QualType Domain) const {
1832 FloatingRank EltRank = getFloatingRank(Size);
1833 if (Domain->isComplexType()) {
1834 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001835 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001836 case FloatRank: return FloatComplexTy;
1837 case DoubleRank: return DoubleComplexTy;
1838 case LongDoubleRank: return LongDoubleComplexTy;
1839 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001840 }
Chris Lattner1361b112008-04-06 23:58:54 +00001841
1842 assert(Domain->isRealFloatingType() && "Unknown domain!");
1843 switch (EltRank) {
1844 default: assert(0 && "getFloatingRank(): illegal value for rank");
1845 case FloatRank: return FloatTy;
1846 case DoubleRank: return DoubleTy;
1847 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001848 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001849}
1850
Chris Lattner7cfeb082008-04-06 23:55:33 +00001851/// getFloatingTypeOrder - Compare the rank of the two specified floating
1852/// point types, ignoring the domain of the type (i.e. 'double' ==
1853/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1854/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001855int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1856 FloatingRank LHSR = getFloatingRank(LHS);
1857 FloatingRank RHSR = getFloatingRank(RHS);
1858
1859 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001860 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001861 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001862 return 1;
1863 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001864}
1865
Chris Lattnerf52ab252008-04-06 22:59:24 +00001866/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1867/// routine will assert if passed a built-in type that isn't an integer or enum,
1868/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001869unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001870 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001871 if (EnumType* ET = dyn_cast<EnumType>(T))
1872 T = ET->getDecl()->getIntegerType().getTypePtr();
1873
1874 // There are two things which impact the integer rank: the width, and
1875 // the ordering of builtins. The builtin ordering is encoded in the
1876 // bottom three bits; the width is encoded in the bits above that.
1877 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1878 return FWIT->getWidth() << 3;
1879 }
1880
Chris Lattnerf52ab252008-04-06 22:59:24 +00001881 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001882 default: assert(0 && "getIntegerRank(): not a built-in integer");
1883 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001884 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001885 case BuiltinType::Char_S:
1886 case BuiltinType::Char_U:
1887 case BuiltinType::SChar:
1888 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001889 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001890 case BuiltinType::Short:
1891 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001892 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001893 case BuiltinType::Int:
1894 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001895 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001896 case BuiltinType::Long:
1897 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001898 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001899 case BuiltinType::LongLong:
1900 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001901 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001902 }
1903}
1904
Chris Lattner7cfeb082008-04-06 23:55:33 +00001905/// getIntegerTypeOrder - Returns the highest ranked integer type:
1906/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1907/// LHS < RHS, return -1.
1908int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001909 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1910 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001911 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001912
Chris Lattnerf52ab252008-04-06 22:59:24 +00001913 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1914 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001915
Chris Lattner7cfeb082008-04-06 23:55:33 +00001916 unsigned LHSRank = getIntegerRank(LHSC);
1917 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001918
Chris Lattner7cfeb082008-04-06 23:55:33 +00001919 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1920 if (LHSRank == RHSRank) return 0;
1921 return LHSRank > RHSRank ? 1 : -1;
1922 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001923
Chris Lattner7cfeb082008-04-06 23:55:33 +00001924 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1925 if (LHSUnsigned) {
1926 // If the unsigned [LHS] type is larger, return it.
1927 if (LHSRank >= RHSRank)
1928 return 1;
1929
1930 // If the signed type can represent all values of the unsigned type, it
1931 // wins. Because we are dealing with 2's complement and types that are
1932 // powers of two larger than each other, this is always safe.
1933 return -1;
1934 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001935
Chris Lattner7cfeb082008-04-06 23:55:33 +00001936 // If the unsigned [RHS] type is larger, return it.
1937 if (RHSRank >= LHSRank)
1938 return -1;
1939
1940 // If the signed type can represent all values of the unsigned type, it
1941 // wins. Because we are dealing with 2's complement and types that are
1942 // powers of two larger than each other, this is always safe.
1943 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001944}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001945
1946// getCFConstantStringType - Return the type used for constant CFStrings.
1947QualType ASTContext::getCFConstantStringType() {
1948 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001949 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001950 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001951 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001952 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001953
1954 // const int *isa;
1955 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001956 // int flags;
1957 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001958 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001959 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001960 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001961 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001962
Anders Carlsson71993dd2007-08-17 05:31:46 +00001963 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001964 for (unsigned i = 0; i < 4; ++i) {
1965 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1966 SourceLocation(), 0,
1967 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001968 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001969 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001970 }
1971
1972 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001973 }
1974
1975 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001976}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001977
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001978QualType ASTContext::getObjCFastEnumerationStateType()
1979{
1980 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001981 ObjCFastEnumerationStateTypeDecl =
1982 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1983 &Idents.get("__objcFastEnumerationState"));
1984
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001985 QualType FieldTypes[] = {
1986 UnsignedLongTy,
1987 getPointerType(ObjCIdType),
1988 getPointerType(UnsignedLongTy),
1989 getConstantArrayType(UnsignedLongTy,
1990 llvm::APInt(32, 5), ArrayType::Normal, 0)
1991 };
1992
Douglas Gregor44b43212008-12-11 16:49:14 +00001993 for (size_t i = 0; i < 4; ++i) {
1994 FieldDecl *Field = FieldDecl::Create(*this,
1995 ObjCFastEnumerationStateTypeDecl,
1996 SourceLocation(), 0,
1997 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001998 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001999 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002000 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002001
Douglas Gregor44b43212008-12-11 16:49:14 +00002002 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002003 }
2004
2005 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2006}
2007
Anders Carlssone8c49532007-10-29 06:33:42 +00002008// This returns true if a type has been typedefed to BOOL:
2009// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002010static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002011 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002012 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2013 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002014
2015 return false;
2016}
2017
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002018/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002019/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002020int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002021 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002022
2023 // Make all integer and enum types at least as large as an int
2024 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002025 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002026 // Treat arrays as pointers, since that's how they're passed in.
2027 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002028 sz = getTypeSize(VoidPtrTy);
2029 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002030}
2031
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002032/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002033/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002034void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002035 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002036 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002037 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002038 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002039 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002040 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002041 // Compute size of all parameters.
2042 // Start with computing size of a pointer in number of bytes.
2043 // FIXME: There might(should) be a better way of doing this computation!
2044 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002045 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002046 // The first two arguments (self and _cmd) are pointers; account for
2047 // their size.
2048 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002049 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2050 E = Decl->param_end(); PI != E; ++PI) {
2051 QualType PType = (*PI)->getType();
2052 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002053 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002054 ParmOffset += sz;
2055 }
2056 S += llvm::utostr(ParmOffset);
2057 S += "@0:";
2058 S += llvm::utostr(PtrSize);
2059
2060 // Argument types.
2061 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002062 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2063 E = Decl->param_end(); PI != E; ++PI) {
2064 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002065 QualType PType = PVDecl->getOriginalType();
2066 if (const ArrayType *AT =
2067 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
2068 // Use array's original type only if it has known number of
2069 // elements.
2070 if (!dyn_cast<ConstantArrayType>(AT))
2071 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002072 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002073 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002074 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002075 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002076 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002077 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002078 }
2079}
2080
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002081/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002082/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002083/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2084/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002085/// Property attributes are stored as a comma-delimited C string. The simple
2086/// attributes readonly and bycopy are encoded as single characters. The
2087/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2088/// encoded as single characters, followed by an identifier. Property types
2089/// are also encoded as a parametrized attribute. The characters used to encode
2090/// these attributes are defined by the following enumeration:
2091/// @code
2092/// enum PropertyAttributes {
2093/// kPropertyReadOnly = 'R', // property is read-only.
2094/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2095/// kPropertyByref = '&', // property is a reference to the value last assigned
2096/// kPropertyDynamic = 'D', // property is dynamic
2097/// kPropertyGetter = 'G', // followed by getter selector name
2098/// kPropertySetter = 'S', // followed by setter selector name
2099/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2100/// kPropertyType = 't' // followed by old-style type encoding.
2101/// kPropertyWeak = 'W' // 'weak' property
2102/// kPropertyStrong = 'P' // property GC'able
2103/// kPropertyNonAtomic = 'N' // property non-atomic
2104/// };
2105/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002106void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2107 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002108 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002109 // Collect information from the property implementation decl(s).
2110 bool Dynamic = false;
2111 ObjCPropertyImplDecl *SynthesizePID = 0;
2112
2113 // FIXME: Duplicated code due to poor abstraction.
2114 if (Container) {
2115 if (const ObjCCategoryImplDecl *CID =
2116 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2117 for (ObjCCategoryImplDecl::propimpl_iterator
2118 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2119 ObjCPropertyImplDecl *PID = *i;
2120 if (PID->getPropertyDecl() == PD) {
2121 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2122 Dynamic = true;
2123 } else {
2124 SynthesizePID = PID;
2125 }
2126 }
2127 }
2128 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002129 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002130 for (ObjCCategoryImplDecl::propimpl_iterator
2131 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2132 ObjCPropertyImplDecl *PID = *i;
2133 if (PID->getPropertyDecl() == PD) {
2134 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2135 Dynamic = true;
2136 } else {
2137 SynthesizePID = PID;
2138 }
2139 }
2140 }
2141 }
2142 }
2143
2144 // FIXME: This is not very efficient.
2145 S = "T";
2146
2147 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002148 // GCC has some special rules regarding encoding of properties which
2149 // closely resembles encoding of ivars.
2150 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
2151 true /* outermost type */,
2152 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002153
2154 if (PD->isReadOnly()) {
2155 S += ",R";
2156 } else {
2157 switch (PD->getSetterKind()) {
2158 case ObjCPropertyDecl::Assign: break;
2159 case ObjCPropertyDecl::Copy: S += ",C"; break;
2160 case ObjCPropertyDecl::Retain: S += ",&"; break;
2161 }
2162 }
2163
2164 // It really isn't clear at all what this means, since properties
2165 // are "dynamic by default".
2166 if (Dynamic)
2167 S += ",D";
2168
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002169 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2170 S += ",N";
2171
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002172 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2173 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002174 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002175 }
2176
2177 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2178 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002179 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002180 }
2181
2182 if (SynthesizePID) {
2183 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2184 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002185 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002186 }
2187
2188 // FIXME: OBJCGC: weak & strong
2189}
2190
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002191/// getLegacyIntegralTypeEncoding -
2192/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002193/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002194/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2195///
2196void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2197 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2198 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002199 if (BT->getKind() == BuiltinType::ULong &&
2200 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002201 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002202 else
2203 if (BT->getKind() == BuiltinType::Long &&
2204 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002205 PointeeTy = IntTy;
2206 }
2207 }
2208}
2209
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002210void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002211 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002212 // We follow the behavior of gcc, expanding structures which are
2213 // directly pointed to, and expanding embedded structures. Note that
2214 // these rules are sufficient to prevent recursive encoding of the
2215 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002216 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2217 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002218}
2219
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002220static void EncodeBitField(const ASTContext *Context, std::string& S,
2221 FieldDecl *FD) {
2222 const Expr *E = FD->getBitWidth();
2223 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2224 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2225 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2226 S += 'b';
2227 S += llvm::utostr(N);
2228}
2229
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002230void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2231 bool ExpandPointedToStructures,
2232 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002233 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002234 bool OutermostType,
2235 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00002236 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002237 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002238 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002239 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002240 else {
2241 char encoding;
2242 switch (BT->getKind()) {
2243 default: assert(0 && "Unhandled builtin type kind");
2244 case BuiltinType::Void: encoding = 'v'; break;
2245 case BuiltinType::Bool: encoding = 'B'; break;
2246 case BuiltinType::Char_U:
2247 case BuiltinType::UChar: encoding = 'C'; break;
2248 case BuiltinType::UShort: encoding = 'S'; break;
2249 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002250 case BuiltinType::ULong:
2251 encoding =
2252 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2253 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002254 case BuiltinType::ULongLong: encoding = 'Q'; break;
2255 case BuiltinType::Char_S:
2256 case BuiltinType::SChar: encoding = 'c'; break;
2257 case BuiltinType::Short: encoding = 's'; break;
2258 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002259 case BuiltinType::Long:
2260 encoding =
2261 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2262 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002263 case BuiltinType::LongLong: encoding = 'q'; break;
2264 case BuiltinType::Float: encoding = 'f'; break;
2265 case BuiltinType::Double: encoding = 'd'; break;
2266 case BuiltinType::LongDouble: encoding = 'd'; break;
2267 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002268
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002269 S += encoding;
2270 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002271 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002272 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002273 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2274 ExpandPointedToStructures,
2275 ExpandStructures, FD);
2276 if (FD || EncodingProperty) {
2277 // Note that we do extended encoding of protocol qualifer list
2278 // Only when doing ivar or property encoding.
2279 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2280 S += '"';
2281 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2282 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2283 S += '<';
2284 S += Proto->getNameAsString();
2285 S += '>';
2286 }
2287 S += '"';
2288 }
2289 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002290 }
2291 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002292 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002293 bool isReadOnly = false;
2294 // For historical/compatibility reasons, the read-only qualifier of the
2295 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2296 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2297 // Also, do not emit the 'r' for anything but the outermost type!
2298 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2299 if (OutermostType && T.isConstQualified()) {
2300 isReadOnly = true;
2301 S += 'r';
2302 }
2303 }
2304 else if (OutermostType) {
2305 QualType P = PointeeTy;
2306 while (P->getAsPointerType())
2307 P = P->getAsPointerType()->getPointeeType();
2308 if (P.isConstQualified()) {
2309 isReadOnly = true;
2310 S += 'r';
2311 }
2312 }
2313 if (isReadOnly) {
2314 // Another legacy compatibility encoding. Some ObjC qualifier and type
2315 // combinations need to be rearranged.
2316 // Rewrite "in const" from "nr" to "rn"
2317 const char * s = S.c_str();
2318 int len = S.length();
2319 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2320 std::string replace = "rn";
2321 S.replace(S.end()-2, S.end(), replace);
2322 }
2323 }
Steve Naroff389bf462009-02-12 17:52:19 +00002324 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002325 S += '@';
2326 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002327 }
2328 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002329 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002330 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002331 // Another historical/compatibility reason.
2332 // We encode the underlying type which comes out as
2333 // {...};
2334 S += '^';
2335 getObjCEncodingForTypeImpl(PointeeTy, S,
2336 false, ExpandPointedToStructures,
2337 NULL);
2338 return;
2339 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002340 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002341 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002342 const ObjCInterfaceType *OIT =
2343 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002344 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002345 S += '"';
2346 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002347 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2348 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2349 S += '<';
2350 S += Proto->getNameAsString();
2351 S += '>';
2352 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002353 S += '"';
2354 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002355 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002356 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002357 S += '#';
2358 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002359 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002360 S += ':';
2361 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002362 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002363
2364 if (PointeeTy->isCharType()) {
2365 // char pointer types should be encoded as '*' unless it is a
2366 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002367 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002368 S += '*';
2369 return;
2370 }
2371 }
2372
2373 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002374 getLegacyIntegralTypeEncoding(PointeeTy);
2375
2376 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002377 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002378 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002379 } else if (const ArrayType *AT =
2380 // Ignore type qualifiers etc.
2381 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002382 if (isa<IncompleteArrayType>(AT)) {
2383 // Incomplete arrays are encoded as a pointer to the array element.
2384 S += '^';
2385
2386 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2387 false, ExpandStructures, FD);
2388 } else {
2389 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002390
Anders Carlsson559a8332009-02-22 01:38:57 +00002391 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2392 S += llvm::utostr(CAT->getSize().getZExtValue());
2393 else {
2394 //Variable length arrays are encoded as a regular array with 0 elements.
2395 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2396 S += '0';
2397 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002398
Anders Carlsson559a8332009-02-22 01:38:57 +00002399 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2400 false, ExpandStructures, FD);
2401 S += ']';
2402 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002403 } else if (T->getAsFunctionType()) {
2404 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002405 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002406 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002407 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002408 // Anonymous structures print as '?'
2409 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2410 S += II->getName();
2411 } else {
2412 S += '?';
2413 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002414 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002415 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002416 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2417 FieldEnd = RDecl->field_end();
2418 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002419 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002420 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002421 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002422 S += '"';
2423 }
2424
2425 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002426 if (Field->isBitField()) {
2427 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2428 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002429 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002430 QualType qt = Field->getType();
2431 getLegacyIntegralTypeEncoding(qt);
2432 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002433 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002434 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002435 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002436 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002437 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002438 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002439 if (FD && FD->isBitField())
2440 EncodeBitField(this, S, FD);
2441 else
2442 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002443 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002444 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002445 } else if (T->isObjCInterfaceType()) {
2446 // @encode(class_name)
2447 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2448 S += '{';
2449 const IdentifierInfo *II = OI->getIdentifier();
2450 S += II->getName();
2451 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002452 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002453 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002454 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002455 if (RecFields[i]->isBitField())
2456 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2457 RecFields[i]);
2458 else
2459 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2460 FD);
2461 }
2462 S += '}';
2463 }
2464 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002465 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002466}
2467
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002468void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002469 std::string& S) const {
2470 if (QT & Decl::OBJC_TQ_In)
2471 S += 'n';
2472 if (QT & Decl::OBJC_TQ_Inout)
2473 S += 'N';
2474 if (QT & Decl::OBJC_TQ_Out)
2475 S += 'o';
2476 if (QT & Decl::OBJC_TQ_Bycopy)
2477 S += 'O';
2478 if (QT & Decl::OBJC_TQ_Byref)
2479 S += 'R';
2480 if (QT & Decl::OBJC_TQ_Oneway)
2481 S += 'V';
2482}
2483
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002484void ASTContext::setBuiltinVaListType(QualType T)
2485{
2486 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2487
2488 BuiltinVaListType = T;
2489}
2490
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002491void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002492{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002493 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002494
2495 // typedef struct objc_object *id;
2496 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002497 // User error - caller will issue diagnostics.
2498 if (!ptr)
2499 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002500 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002501 // User error - caller will issue diagnostics.
2502 if (!rec)
2503 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002504 IdStructType = rec;
2505}
2506
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002507void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002508{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002509 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002510
2511 // typedef struct objc_selector *SEL;
2512 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002513 if (!ptr)
2514 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002515 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002516 if (!rec)
2517 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002518 SelStructType = rec;
2519}
2520
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002521void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002522{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002523 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002524}
2525
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002526void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002527{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002528 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002529
2530 // typedef struct objc_class *Class;
2531 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2532 assert(ptr && "'Class' incorrectly typed");
2533 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2534 assert(rec && "'Class' incorrectly typed");
2535 ClassStructType = rec;
2536}
2537
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002538void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2539 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002540 "'NSConstantString' type already set!");
2541
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002542 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002543}
2544
Douglas Gregor7532dc62009-03-30 22:58:21 +00002545/// \brief Retrieve the template name that represents a qualified
2546/// template name such as \c std::vector.
2547TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2548 bool TemplateKeyword,
2549 TemplateDecl *Template) {
2550 llvm::FoldingSetNodeID ID;
2551 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2552
2553 void *InsertPos = 0;
2554 QualifiedTemplateName *QTN =
2555 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2556 if (!QTN) {
2557 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2558 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2559 }
2560
2561 return TemplateName(QTN);
2562}
2563
2564/// \brief Retrieve the template name that represents a dependent
2565/// template name such as \c MetaFun::template apply.
2566TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2567 const IdentifierInfo *Name) {
2568 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2569
2570 llvm::FoldingSetNodeID ID;
2571 DependentTemplateName::Profile(ID, NNS, Name);
2572
2573 void *InsertPos = 0;
2574 DependentTemplateName *QTN =
2575 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2576
2577 if (QTN)
2578 return TemplateName(QTN);
2579
2580 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2581 if (CanonNNS == NNS) {
2582 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2583 } else {
2584 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2585 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2586 }
2587
2588 DependentTemplateNames.InsertNode(QTN, InsertPos);
2589 return TemplateName(QTN);
2590}
2591
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002592/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002593/// TargetInfo, produce the corresponding type. The unsigned @p Type
2594/// is actually a value of type @c TargetInfo::IntType.
2595QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002596 switch (Type) {
2597 case TargetInfo::NoInt: return QualType();
2598 case TargetInfo::SignedShort: return ShortTy;
2599 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2600 case TargetInfo::SignedInt: return IntTy;
2601 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2602 case TargetInfo::SignedLong: return LongTy;
2603 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2604 case TargetInfo::SignedLongLong: return LongLongTy;
2605 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2606 }
2607
2608 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002609 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002610}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002611
2612//===----------------------------------------------------------------------===//
2613// Type Predicates.
2614//===----------------------------------------------------------------------===//
2615
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002616/// isObjCNSObjectType - Return true if this is an NSObject object using
2617/// NSObject attribute on a c-style pointer type.
2618/// FIXME - Make it work directly on types.
2619///
2620bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2621 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2622 if (TypedefDecl *TD = TDT->getDecl())
2623 if (TD->getAttr<ObjCNSObjectAttr>())
2624 return true;
2625 }
2626 return false;
2627}
2628
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002629/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2630/// to an object type. This includes "id" and "Class" (two 'special' pointers
2631/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2632/// ID type).
2633bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002634 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002635 return true;
2636
Steve Naroff6ae98502008-10-21 18:24:04 +00002637 // Blocks are objects.
2638 if (Ty->isBlockPointerType())
2639 return true;
2640
2641 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002642 if (!Ty->isPointerType())
2643 return false;
2644
2645 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2646 // pointer types. This looks for the typedef specifically, not for the
2647 // underlying type.
Eli Friedman5fdeae12009-03-22 23:00:19 +00002648 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2649 Ty.getUnqualifiedType() == getObjCClassType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002650 return true;
2651
2652 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002653 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2654 return true;
2655
2656 // If is has NSObject attribute, OK as well.
2657 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002658}
2659
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002660/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2661/// garbage collection attribute.
2662///
2663QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002664 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002665 if (getLangOptions().ObjC1 &&
2666 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002667 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002668 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002669 // (or pointers to them) be treated as though they were declared
2670 // as __strong.
2671 if (GCAttrs == QualType::GCNone) {
2672 if (isObjCObjectPointerType(Ty))
2673 GCAttrs = QualType::Strong;
2674 else if (Ty->isPointerType())
2675 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2676 }
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002677 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002678 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002679}
2680
Chris Lattner6ac46a42008-04-07 06:51:04 +00002681//===----------------------------------------------------------------------===//
2682// Type Compatibility Testing
2683//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002684
Steve Naroff1c7d0672008-09-04 15:10:53 +00002685/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002686/// block types. Types must be strictly compatible here. For example,
2687/// C unfortunately doesn't produce an error for the following:
2688///
2689/// int (*emptyArgFunc)();
2690/// int (*intArgList)(int) = emptyArgFunc;
2691///
2692/// For blocks, we will produce an error for the following (similar to C++):
2693///
2694/// int (^emptyArgBlock)();
2695/// int (^intArgBlock)(int) = emptyArgBlock;
2696///
2697/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2698///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002699bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002700 const FunctionType *lbase = lhs->getAsFunctionType();
2701 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002702 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2703 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Steve Naroffc0febd52008-12-10 17:49:55 +00002704 if (lproto && rproto)
2705 return !mergeTypes(lhs, rhs).isNull();
2706 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002707}
2708
Chris Lattner6ac46a42008-04-07 06:51:04 +00002709/// areCompatVectorTypes - Return true if the two specified vector types are
2710/// compatible.
2711static bool areCompatVectorTypes(const VectorType *LHS,
2712 const VectorType *RHS) {
2713 assert(LHS->isCanonical() && RHS->isCanonical());
2714 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002715 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002716}
2717
Eli Friedman3d815e72008-08-22 00:56:42 +00002718/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002719/// compatible for assignment from RHS to LHS. This handles validation of any
2720/// protocol qualifiers on the LHS or RHS.
2721///
Eli Friedman3d815e72008-08-22 00:56:42 +00002722bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2723 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002724 // Verify that the base decls are compatible: the RHS must be a subclass of
2725 // the LHS.
2726 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2727 return false;
2728
2729 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2730 // protocol qualified at all, then we are good.
2731 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2732 return true;
2733
2734 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2735 // isn't a superset.
2736 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2737 return true; // FIXME: should return false!
2738
2739 // Finally, we must have two protocol-qualified interfaces.
2740 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2741 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002742
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002743 // All LHS protocols must have a presence on the RHS.
2744 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002745
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002746 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2747 LHSPE = LHSP->qual_end();
2748 LHSPI != LHSPE; LHSPI++) {
2749 bool RHSImplementsProtocol = false;
2750
2751 // If the RHS doesn't implement the protocol on the left, the types
2752 // are incompatible.
2753 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2754 RHSPE = RHSP->qual_end();
2755 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2756 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2757 RHSImplementsProtocol = true;
2758 }
2759 // FIXME: For better diagnostics, consider passing back the protocol name.
2760 if (!RHSImplementsProtocol)
2761 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002762 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002763 // The RHS implements all protocols listed on the LHS.
2764 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002765}
2766
Steve Naroff389bf462009-02-12 17:52:19 +00002767bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2768 // get the "pointed to" types
2769 const PointerType *LHSPT = LHS->getAsPointerType();
2770 const PointerType *RHSPT = RHS->getAsPointerType();
2771
2772 if (!LHSPT || !RHSPT)
2773 return false;
2774
2775 QualType lhptee = LHSPT->getPointeeType();
2776 QualType rhptee = RHSPT->getPointeeType();
2777 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2778 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2779 // ID acts sort of like void* for ObjC interfaces
2780 if (LHSIface && isObjCIdStructType(rhptee))
2781 return true;
2782 if (RHSIface && isObjCIdStructType(lhptee))
2783 return true;
2784 if (!LHSIface || !RHSIface)
2785 return false;
2786 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2787 canAssignObjCInterfaces(RHSIface, LHSIface);
2788}
2789
Steve Naroffec0550f2007-10-15 20:41:53 +00002790/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2791/// both shall have the identically qualified version of a compatible type.
2792/// C99 6.2.7p1: Two types have compatible types if their types are the
2793/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002794bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2795 return !mergeTypes(LHS, RHS).isNull();
2796}
2797
2798QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2799 const FunctionType *lbase = lhs->getAsFunctionType();
2800 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002801 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2802 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002803 bool allLTypes = true;
2804 bool allRTypes = true;
2805
2806 // Check return type
2807 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2808 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002809 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2810 allLTypes = false;
2811 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2812 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002813
2814 if (lproto && rproto) { // two C99 style function prototypes
2815 unsigned lproto_nargs = lproto->getNumArgs();
2816 unsigned rproto_nargs = rproto->getNumArgs();
2817
2818 // Compatible functions must have the same number of arguments
2819 if (lproto_nargs != rproto_nargs)
2820 return QualType();
2821
2822 // Variadic and non-variadic functions aren't compatible
2823 if (lproto->isVariadic() != rproto->isVariadic())
2824 return QualType();
2825
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002826 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2827 return QualType();
2828
Eli Friedman3d815e72008-08-22 00:56:42 +00002829 // Check argument compatibility
2830 llvm::SmallVector<QualType, 10> types;
2831 for (unsigned i = 0; i < lproto_nargs; i++) {
2832 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2833 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2834 QualType argtype = mergeTypes(largtype, rargtype);
2835 if (argtype.isNull()) return QualType();
2836 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002837 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2838 allLTypes = false;
2839 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2840 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002841 }
2842 if (allLTypes) return lhs;
2843 if (allRTypes) return rhs;
2844 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002845 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002846 }
2847
2848 if (lproto) allRTypes = false;
2849 if (rproto) allLTypes = false;
2850
Douglas Gregor72564e72009-02-26 23:50:07 +00002851 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002852 if (proto) {
2853 if (proto->isVariadic()) return QualType();
2854 // Check that the types are compatible with the types that
2855 // would result from default argument promotions (C99 6.7.5.3p15).
2856 // The only types actually affected are promotable integer
2857 // types and floats, which would be passed as a different
2858 // type depending on whether the prototype is visible.
2859 unsigned proto_nargs = proto->getNumArgs();
2860 for (unsigned i = 0; i < proto_nargs; ++i) {
2861 QualType argTy = proto->getArgType(i);
2862 if (argTy->isPromotableIntegerType() ||
2863 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2864 return QualType();
2865 }
2866
2867 if (allLTypes) return lhs;
2868 if (allRTypes) return rhs;
2869 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002870 proto->getNumArgs(), lproto->isVariadic(),
2871 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002872 }
2873
2874 if (allLTypes) return lhs;
2875 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002876 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002877}
2878
2879QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002880 // C++ [expr]: If an expression initially has the type "reference to T", the
2881 // type is adjusted to "T" prior to any further analysis, the expression
2882 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002883 // expression is an lvalue unless the reference is an rvalue reference and
2884 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002885 // FIXME: C++ shouldn't be going through here! The rules are different
2886 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002887 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2888 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002889 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002890 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002891 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002892 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002893
Eli Friedman3d815e72008-08-22 00:56:42 +00002894 QualType LHSCan = getCanonicalType(LHS),
2895 RHSCan = getCanonicalType(RHS);
2896
2897 // If two types are identical, they are compatible.
2898 if (LHSCan == RHSCan)
2899 return LHS;
2900
2901 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002902 // Note that we handle extended qualifiers later, in the
2903 // case for ExtQualType.
2904 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002905 return QualType();
2906
2907 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2908 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2909
Chris Lattner1adb8832008-01-14 05:45:46 +00002910 // We want to consider the two function types to be the same for these
2911 // comparisons, just force one to the other.
2912 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2913 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002914
2915 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002916 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2917 LHSClass = Type::ConstantArray;
2918 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2919 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002920
Nate Begeman213541a2008-04-18 23:10:10 +00002921 // Canonicalize ExtVector -> Vector.
2922 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2923 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002924
Chris Lattnerb0489812008-04-07 06:38:24 +00002925 // Consider qualified interfaces and interfaces the same.
2926 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2927 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002928
Chris Lattnera36a61f2008-04-07 05:43:21 +00002929 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002930 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002931 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2932 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2933
2934 // ID acts sort of like void* for ObjC interfaces
2935 if (LHSIface && isObjCIdStructType(RHS))
2936 return LHS;
2937 if (RHSIface && isObjCIdStructType(LHS))
2938 return RHS;
2939
Steve Naroffbc76dd02008-12-10 22:14:21 +00002940 // ID is compatible with all qualified id types.
2941 if (LHS->isObjCQualifiedIdType()) {
2942 if (const PointerType *PT = RHS->getAsPointerType()) {
2943 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002944 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002945 return LHS;
2946 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2947 // Unfortunately, this API is part of Sema (which we don't have access
2948 // to. Need to refactor. The following check is insufficient, since we
2949 // need to make sure the class implements the protocol.
2950 if (pType->isObjCInterfaceType())
2951 return LHS;
2952 }
2953 }
2954 if (RHS->isObjCQualifiedIdType()) {
2955 if (const PointerType *PT = LHS->getAsPointerType()) {
2956 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002957 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002958 return RHS;
2959 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2960 // Unfortunately, this API is part of Sema (which we don't have access
2961 // to. Need to refactor. The following check is insufficient, since we
2962 // need to make sure the class implements the protocol.
2963 if (pType->isObjCInterfaceType())
2964 return RHS;
2965 }
2966 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002967 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2968 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002969 if (const EnumType* ETy = LHS->getAsEnumType()) {
2970 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2971 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002972 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002973 if (const EnumType* ETy = RHS->getAsEnumType()) {
2974 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2975 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002976 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002977
Eli Friedman3d815e72008-08-22 00:56:42 +00002978 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002979 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002980
Steve Naroff4a746782008-01-09 22:43:08 +00002981 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002982 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00002983#define TYPE(Class, Base)
2984#define ABSTRACT_TYPE(Class, Base)
2985#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2986#define DEPENDENT_TYPE(Class, Base) case Type::Class:
2987#include "clang/AST/TypeNodes.def"
2988 assert(false && "Non-canonical and dependent types shouldn't get here");
2989 return QualType();
2990
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002991 case Type::LValueReference:
2992 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00002993 case Type::MemberPointer:
2994 assert(false && "C++ should never be in mergeTypes");
2995 return QualType();
2996
2997 case Type::IncompleteArray:
2998 case Type::VariableArray:
2999 case Type::FunctionProto:
3000 case Type::ExtVector:
3001 case Type::ObjCQualifiedInterface:
3002 assert(false && "Types are eliminated above");
3003 return QualType();
3004
Chris Lattner1adb8832008-01-14 05:45:46 +00003005 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003006 {
3007 // Merge two pointer types, while trying to preserve typedef info
3008 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3009 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3010 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3011 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003012 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3013 return LHS;
3014 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3015 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003016 return getPointerType(ResultType);
3017 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003018 case Type::BlockPointer:
3019 {
3020 // Merge two block pointer types, while trying to preserve typedef info
3021 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3022 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3023 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3024 if (ResultType.isNull()) return QualType();
3025 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3026 return LHS;
3027 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3028 return RHS;
3029 return getBlockPointerType(ResultType);
3030 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003031 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003032 {
3033 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3034 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3035 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3036 return QualType();
3037
3038 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3039 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3040 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3041 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003042 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3043 return LHS;
3044 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3045 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003046 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3047 ArrayType::ArraySizeModifier(), 0);
3048 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3049 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003050 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3051 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003052 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3053 return LHS;
3054 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3055 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003056 if (LVAT) {
3057 // FIXME: This isn't correct! But tricky to implement because
3058 // the array's size has to be the size of LHS, but the type
3059 // has to be different.
3060 return LHS;
3061 }
3062 if (RVAT) {
3063 // FIXME: This isn't correct! But tricky to implement because
3064 // the array's size has to be the size of RHS, but the type
3065 // has to be different.
3066 return RHS;
3067 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003068 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3069 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003070 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003071 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003072 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003073 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003074 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003075 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003076 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003077 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3078 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003079 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003080 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003081 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003082 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003083 case Type::Complex:
3084 // Distinct complex types are incompatible.
3085 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003086 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003087 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003088 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3089 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003090 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003091 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003092 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003093 // FIXME: This should be type compatibility, e.g. whether
3094 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003095 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3096 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3097 if (LHSIface && RHSIface &&
3098 canAssignObjCInterfaces(LHSIface, RHSIface))
3099 return LHS;
3100
Eli Friedman3d815e72008-08-22 00:56:42 +00003101 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003102 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003103 case Type::ObjCQualifiedId:
3104 // Distinct qualified id's are not compatible.
3105 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003106 case Type::FixedWidthInt:
3107 // Distinct fixed-width integers are not compatible.
3108 return QualType();
3109 case Type::ObjCQualifiedClass:
3110 // Distinct qualified classes are not compatible.
3111 return QualType();
3112 case Type::ExtQual:
3113 // FIXME: ExtQual types can be compatible even if they're not
3114 // identical!
3115 return QualType();
3116 // First attempt at an implementation, but I'm not really sure it's
3117 // right...
3118#if 0
3119 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3120 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3121 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3122 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3123 return QualType();
3124 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3125 LHSBase = QualType(LQual->getBaseType(), 0);
3126 RHSBase = QualType(RQual->getBaseType(), 0);
3127 ResultType = mergeTypes(LHSBase, RHSBase);
3128 if (ResultType.isNull()) return QualType();
3129 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3130 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3131 return LHS;
3132 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3133 return RHS;
3134 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3135 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3136 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3137 return ResultType;
3138#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003139
3140 case Type::TemplateSpecialization:
3141 assert(false && "Dependent types have no size");
3142 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003143 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003144
3145 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003146}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003147
Chris Lattner5426bf62008-04-07 07:01:58 +00003148//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003149// Integer Predicates
3150//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003151
Eli Friedmanad74a752008-06-28 06:23:08 +00003152unsigned ASTContext::getIntWidth(QualType T) {
3153 if (T == BoolTy)
3154 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003155 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3156 return FWIT->getWidth();
3157 }
3158 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003159 return (unsigned)getTypeSize(T);
3160}
3161
3162QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3163 assert(T->isSignedIntegerType() && "Unexpected type");
3164 if (const EnumType* ETy = T->getAsEnumType())
3165 T = ETy->getDecl()->getIntegerType();
3166 const BuiltinType* BTy = T->getAsBuiltinType();
3167 assert (BTy && "Unexpected signed integer type");
3168 switch (BTy->getKind()) {
3169 case BuiltinType::Char_S:
3170 case BuiltinType::SChar:
3171 return UnsignedCharTy;
3172 case BuiltinType::Short:
3173 return UnsignedShortTy;
3174 case BuiltinType::Int:
3175 return UnsignedIntTy;
3176 case BuiltinType::Long:
3177 return UnsignedLongTy;
3178 case BuiltinType::LongLong:
3179 return UnsignedLongLongTy;
3180 default:
3181 assert(0 && "Unexpected signed integer type");
3182 return QualType();
3183 }
3184}
3185
3186
3187//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00003188// Serialization Support
3189//===----------------------------------------------------------------------===//
3190
Chris Lattnera9376d42009-03-28 03:45:20 +00003191enum {
3192 BasicMetadataBlock = 1,
3193 ASTContextBlock = 2,
3194 DeclsBlock = 3
3195};
3196
Chris Lattner557c5b12009-03-28 04:27:18 +00003197void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3198 // Create bitstream.
3199 llvm::BitstreamWriter Stream(Buffer);
3200
3201 // Emit the preamble.
3202 Stream.Emit((unsigned)'B', 8);
3203 Stream.Emit((unsigned)'C', 8);
3204 Stream.Emit(0xC, 4);
3205 Stream.Emit(0xF, 4);
3206 Stream.Emit(0xE, 4);
3207 Stream.Emit(0x0, 4);
3208
3209 // Create serializer.
3210 llvm::Serializer S(Stream);
3211
Chris Lattnera9376d42009-03-28 03:45:20 +00003212 // ===---------------------------------------------------===/
3213 // Serialize the "Translation Unit" metadata.
3214 // ===---------------------------------------------------===/
3215
3216 // Emit ASTContext.
3217 S.EnterBlock(ASTContextBlock);
3218 S.EmitOwnedPtr(this);
3219 S.ExitBlock(); // exit "ASTContextBlock"
3220
3221 S.EnterBlock(BasicMetadataBlock);
3222
3223 // Block for SourceManager and Target. Allows easy skipping
3224 // around to the block for the Selectors during deserialization.
3225 S.EnterBlock();
3226
3227 // Emit the SourceManager.
3228 S.Emit(getSourceManager());
3229
3230 // Emit the Target.
3231 S.EmitPtr(&Target);
3232 S.EmitCStr(Target.getTargetTriple());
3233
3234 S.ExitBlock(); // exit "SourceManager and Target Block"
3235
3236 // Emit the Selectors.
3237 S.Emit(Selectors);
3238
3239 // Emit the Identifier Table.
3240 S.Emit(Idents);
3241
3242 S.ExitBlock(); // exit "BasicMetadataBlock"
3243}
3244
3245
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003246/// Emit - Serialize an ASTContext object to Bitcode.
3247void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003248 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00003249 S.EmitRef(SourceMgr);
3250 S.EmitRef(Target);
3251 S.EmitRef(Idents);
3252 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003253
Ted Kremenekfee04522007-10-31 22:44:07 +00003254 // Emit the size of the type vector so that we can reserve that size
3255 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00003256 S.EmitInt(Types.size());
3257
Ted Kremenek03ed4402007-11-13 22:02:55 +00003258 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3259 I!=E;++I)
3260 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00003261
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003262 S.EmitOwnedPtr(TUDecl);
3263
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003264 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003265}
3266
Chris Lattner557c5b12009-03-28 04:27:18 +00003267
3268ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3269 FileManager &FMgr) {
3270 // Check if the file is of the proper length.
3271 if (Buffer.getBufferSize() & 0x3) {
3272 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3273 return 0;
3274 }
3275
3276 // Create the bitstream reader.
3277 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3278 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3279
3280 if (Stream.Read(8) != 'B' ||
3281 Stream.Read(8) != 'C' ||
3282 Stream.Read(4) != 0xC ||
3283 Stream.Read(4) != 0xF ||
3284 Stream.Read(4) != 0xE ||
3285 Stream.Read(4) != 0x0) {
3286 // FIXME: Provide diagnostic.
3287 return NULL;
3288 }
3289
3290 // Create the deserializer.
3291 llvm::Deserializer Dezr(Stream);
3292
Chris Lattnera9376d42009-03-28 03:45:20 +00003293 // ===---------------------------------------------------===/
3294 // Deserialize the "Translation Unit" metadata.
3295 // ===---------------------------------------------------===/
3296
3297 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3298 // (which will appear earlier) and record its location.
3299
3300 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3301 assert (FoundBlock);
3302
3303 llvm::Deserializer::Location ASTContextBlockLoc =
3304 Dezr.getCurrentBlockLocation();
3305
3306 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3307 assert (FoundBlock);
3308
3309 // Read the SourceManager.
3310 SourceManager::CreateAndRegister(Dezr, FMgr);
3311
3312 { // Read the TargetInfo.
3313 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3314 char* triple = Dezr.ReadCStr(NULL,0,true);
3315 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3316 delete [] triple;
3317 }
3318
3319 // For Selectors, we must read the identifier table first because the
3320 // SelectorTable depends on the identifiers being already deserialized.
3321 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3322 Dezr.SkipBlock();
3323
3324 // Read the identifier table.
3325 IdentifierTable::CreateAndRegister(Dezr);
3326
3327 // Now jump back and read the selectors.
3328 Dezr.JumpTo(SelectorBlkLoc);
3329 SelectorTable::CreateAndRegister(Dezr);
3330
3331 // Now jump back to ASTContextBlock and read the ASTContext.
3332 Dezr.JumpTo(ASTContextBlockLoc);
3333 return Dezr.ReadOwnedPtr<ASTContext>();
3334}
3335
Ted Kremenek0f84c002007-11-13 00:25:37 +00003336ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003337
3338 // Read the language options.
3339 LangOptions LOpts;
3340 LOpts.Read(D);
3341
Ted Kremenekfee04522007-10-31 22:44:07 +00003342 SourceManager &SM = D.ReadRef<SourceManager>();
3343 TargetInfo &t = D.ReadRef<TargetInfo>();
3344 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3345 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00003346
Ted Kremenekfee04522007-10-31 22:44:07 +00003347 unsigned size_reserve = D.ReadInt();
3348
Douglas Gregor2e1cd422008-11-17 14:58:09 +00003349 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3350 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00003351
Ted Kremenek03ed4402007-11-13 22:02:55 +00003352 for (unsigned i = 0; i < size_reserve; ++i)
3353 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00003354
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003355 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3356
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003357 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00003358
3359 return A;
3360}