blob: 7d3c119d57a50770eea589e3dc0a882276081a8f [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"
Douglas Gregor2cf26342009-04-09 22:27:44 +000019#include "clang/AST/ExternalASTSource.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattnera9376d42009-03-28 03:45:20 +000021#include "clang/Basic/SourceManager.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000023#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000024#include "llvm/Bitcode/Serialize.h"
25#include "llvm/Bitcode/Deserialize.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000026#include "llvm/Support/MathExtras.h"
Chris Lattner557c5b12009-03-28 04:27:18 +000027#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000028using namespace clang;
29
30enum FloatingRank {
31 FloatRank, DoubleRank, LongDoubleRank
32};
33
Chris Lattner61710852008-10-05 17:34:18 +000034ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
35 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000036 IdentifierTable &idents, SelectorTable &sels,
Steve Naroffc0ac4922009-01-27 23:20:32 +000037 bool FreeMem, unsigned size_reserve) :
Douglas Gregorab452ba2009-03-26 23:50:42 +000038 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
39 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregor2cf26342009-04-09 22:27:44 +000040 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
41 ExternalSource(0) {
Daniel Dunbare91593e2008-08-11 04:54:23 +000042 if (size_reserve > 0) Types.reserve(size_reserve);
43 InitBuiltinTypes();
Douglas Gregor71dfdb92009-04-22 04:56:28 +000044 BuiltinInfo.InitializeTargetBuiltins(Target);
45 BuiltinInfo.InitializeBuiltins(idents, LangOpts.NoBuiltin);
Daniel Dunbare91593e2008-08-11 04:54:23 +000046 TUDecl = TranslationUnitDecl::Create(*this);
47}
48
Reid Spencer5f016e22007-07-11 17:01:13 +000049ASTContext::~ASTContext() {
50 // Deallocate all the types.
51 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000052 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000053 Types.pop_back();
54 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000055
Nuno Lopesb74668e2008-12-17 22:30:25 +000056 {
57 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
58 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
59 while (I != E) {
60 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
61 delete R;
62 }
63 }
64
65 {
66 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
67 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
68 while (I != E) {
69 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
70 delete R;
71 }
72 }
73
74 {
Chris Lattner23499252009-03-31 09:24:30 +000075 llvm::DenseMap<const ObjCInterfaceDecl*, RecordDecl*>::iterator
Nuno Lopesb74668e2008-12-17 22:30:25 +000076 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
77 while (I != E) {
Chris Lattner23499252009-03-31 09:24:30 +000078 RecordDecl *R = (I++)->second;
Nuno Lopesb74668e2008-12-17 22:30:25 +000079 R->Destroy(*this);
80 }
81 }
82
Douglas Gregorab452ba2009-03-26 23:50:42 +000083 // Destroy nested-name-specifiers.
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000084 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
85 NNS = NestedNameSpecifiers.begin(),
86 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregore7dcd782009-03-27 23:25:45 +000087 NNS != NNSEnd;
Douglas Gregor1ae0afa2009-03-27 23:54:10 +000088 /* Increment in loop */)
89 (*NNS++).Destroy(*this);
Douglas Gregorab452ba2009-03-26 23:50:42 +000090
91 if (GlobalNestedNameSpecifier)
92 GlobalNestedNameSpecifier->Destroy(*this);
93
Eli Friedmanb26153c2008-05-27 03:08:09 +000094 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000095}
96
Douglas Gregor2cf26342009-04-09 22:27:44 +000097void
98ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
99 ExternalSource.reset(Source.take());
100}
101
Reid Spencer5f016e22007-07-11 17:01:13 +0000102void ASTContext::PrintStats() const {
103 fprintf(stderr, "*** AST Context Stats:\n");
104 fprintf(stderr, " %d types total.\n", (int)Types.size());
105 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000106 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000107 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
108 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
109
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000111 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
112 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +0000113 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000114 unsigned NumExtQual = 0;
115
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
117 Type *T = Types[i];
118 if (isa<BuiltinType>(T))
119 ++NumBuiltin;
120 else if (isa<PointerType>(T))
121 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000122 else if (isa<BlockPointerType>(T))
123 ++NumBlockPointer;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000124 else if (isa<LValueReferenceType>(T))
125 ++NumLValueReference;
126 else if (isa<RValueReferenceType>(T))
127 ++NumRValueReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000128 else if (isa<MemberPointerType>(T))
129 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000130 else if (isa<ComplexType>(T))
131 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 else if (isa<ArrayType>(T))
133 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000134 else if (isa<VectorType>(T))
135 ++NumVector;
Douglas Gregor72564e72009-02-26 23:50:07 +0000136 else if (isa<FunctionNoProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000137 ++NumFunctionNP;
Douglas Gregor72564e72009-02-26 23:50:07 +0000138 else if (isa<FunctionProtoType>(T))
Reid Spencer5f016e22007-07-11 17:01:13 +0000139 ++NumFunctionP;
140 else if (isa<TypedefType>(T))
141 ++NumTypeName;
142 else if (TagType *TT = dyn_cast<TagType>(T)) {
143 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000144 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000146 case TagDecl::TK_struct: ++NumTagStruct; break;
147 case TagDecl::TK_union: ++NumTagUnion; break;
148 case TagDecl::TK_class: ++NumTagClass; break;
149 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000150 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000151 } else if (isa<ObjCInterfaceType>(T))
152 ++NumObjCInterfaces;
153 else if (isa<ObjCQualifiedInterfaceType>(T))
154 ++NumObjCQualifiedInterfaces;
155 else if (isa<ObjCQualifiedIdType>(T))
156 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000157 else if (isa<TypeOfType>(T))
158 ++NumTypeOfTypes;
Douglas Gregor72564e72009-02-26 23:50:07 +0000159 else if (isa<TypeOfExprType>(T))
160 ++NumTypeOfExprTypes;
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000161 else if (isa<ExtQualType>(T))
162 ++NumExtQual;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000163 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000164 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 assert(0 && "Unknown type!");
166 }
167 }
168
169 fprintf(stderr, " %d builtin types\n", NumBuiltin);
170 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000171 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000172 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
173 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000174 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000175 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000177 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
179 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
180 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
181 fprintf(stderr, " %d tagged types\n", NumTagged);
182 fprintf(stderr, " %d struct types\n", NumTagStruct);
183 fprintf(stderr, " %d union types\n", NumTagUnion);
184 fprintf(stderr, " %d class types\n", NumTagClass);
185 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000186 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000187 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000188 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000189 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000190 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000191 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor72564e72009-02-26 23:50:07 +0000192 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000193 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000194
Reid Spencer5f016e22007-07-11 17:01:13 +0000195 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
196 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000197 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000198 NumLValueReference*sizeof(LValueReferenceType)+
199 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000200 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor72564e72009-02-26 23:50:07 +0000201 NumFunctionP*sizeof(FunctionProtoType)+
202 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000203 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregorc2ee10d2009-04-07 17:20:56 +0000204 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
205 NumExtQual*sizeof(ExtQualType)));
Douglas Gregor2cf26342009-04-09 22:27:44 +0000206
207 if (ExternalSource.get()) {
208 fprintf(stderr, "\n");
209 ExternalSource->PrintStats();
210 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000211}
212
213
214void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000215 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000216}
217
Reid Spencer5f016e22007-07-11 17:01:13 +0000218void ASTContext::InitBuiltinTypes() {
219 assert(VoidTy.isNull() && "Context reinitialized?");
220
221 // C99 6.2.5p19.
222 InitBuiltinType(VoidTy, BuiltinType::Void);
223
224 // C99 6.2.5p2.
225 InitBuiltinType(BoolTy, BuiltinType::Bool);
226 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000227 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000228 InitBuiltinType(CharTy, BuiltinType::Char_S);
229 else
230 InitBuiltinType(CharTy, BuiltinType::Char_U);
231 // C99 6.2.5p4.
232 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
233 InitBuiltinType(ShortTy, BuiltinType::Short);
234 InitBuiltinType(IntTy, BuiltinType::Int);
235 InitBuiltinType(LongTy, BuiltinType::Long);
236 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
237
238 // C99 6.2.5p6.
239 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
240 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
241 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
242 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
243 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
244
245 // C99 6.2.5p10.
246 InitBuiltinType(FloatTy, BuiltinType::Float);
247 InitBuiltinType(DoubleTy, BuiltinType::Double);
248 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000249
Chris Lattner3a250322009-02-26 23:43:47 +0000250 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
251 InitBuiltinType(WCharTy, BuiltinType::WChar);
252 else // C99
253 WCharTy = getFromTargetType(Target.getWCharType());
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000254
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000255 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000256 InitBuiltinType(OverloadTy, BuiltinType::Overload);
257
258 // Placeholder type for type-dependent expressions whose type is
259 // completely unknown. No code should ever check a type against
260 // DependentTy and users should never see it; however, it is here to
261 // help diagnose failures to properly check for type-dependent
262 // expressions.
263 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000264
Reid Spencer5f016e22007-07-11 17:01:13 +0000265 // C99 6.2.5p11.
266 FloatComplexTy = getComplexType(FloatTy);
267 DoubleComplexTy = getComplexType(DoubleTy);
268 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000269
Steve Naroff7e219e42007-10-15 14:41:52 +0000270 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000271 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000272 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000273 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000274 ClassStructType = 0;
275
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000276 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000277
278 // void * type
279 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000280}
281
Chris Lattner464175b2007-07-18 17:52:12 +0000282//===----------------------------------------------------------------------===//
283// Type Sizing and Analysis
284//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000285
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000286/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
287/// scalar floating point type.
288const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
289 const BuiltinType *BT = T->getAsBuiltinType();
290 assert(BT && "Not a floating point type!");
291 switch (BT->getKind()) {
292 default: assert(0 && "Not a floating point type!");
293 case BuiltinType::Float: return Target.getFloatFormat();
294 case BuiltinType::Double: return Target.getDoubleFormat();
295 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
296 }
297}
298
Chris Lattneraf707ab2009-01-24 21:53:27 +0000299/// getDeclAlign - Return a conservative estimate of the alignment of the
300/// specified decl. Note that bitfields do not have a valid alignment, so
301/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000302unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000303 unsigned Align = Target.getCharWidth();
304
305 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
306 Align = std::max(Align, AA->getAlignment());
307
Chris Lattneraf707ab2009-01-24 21:53:27 +0000308 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
309 QualType T = VD->getType();
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000310 if (const ReferenceType* RT = T->getAsReferenceType()) {
311 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssonf0930232009-04-10 04:52:36 +0000312 Align = Target.getPointerAlign(AS);
Anders Carlsson4cc2cfd2009-04-10 04:47:03 +0000313 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
314 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000315 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
316 T = cast<ArrayType>(T)->getElementType();
317
318 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
319 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000320 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000321
322 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000323}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000324
Chris Lattnera7674d82007-07-13 22:13:22 +0000325/// getTypeSize - Return the size of the specified type, in bits. This method
326/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000327std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000328ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000329 T = getCanonicalType(T);
Mike Stump5e301002009-02-27 18:32:39 +0000330 uint64_t Width=0;
331 unsigned Align=8;
Chris Lattnera7674d82007-07-13 22:13:22 +0000332 switch (T->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +0000333#define TYPE(Class, Base)
334#define ABSTRACT_TYPE(Class, Base)
335#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
336#define DEPENDENT_TYPE(Class, Base) case Type::Class:
337#include "clang/AST/TypeNodes.def"
338 assert(false && "Should not see non-canonical or dependent types");
339 break;
340
Chris Lattner692233e2007-07-13 22:27:08 +0000341 case Type::FunctionNoProto:
342 case Type::FunctionProto:
Douglas Gregor72564e72009-02-26 23:50:07 +0000343 case Type::IncompleteArray:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000344 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000345 case Type::VariableArray:
346 assert(0 && "VLAs not implemented yet!");
347 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000348 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000349
Chris Lattner98be4942008-03-05 18:54:05 +0000350 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000351 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000352 Align = EltInfo.second;
353 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000354 }
Nate Begeman213541a2008-04-18 23:10:10 +0000355 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000356 case Type::Vector: {
357 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000358 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000359 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000360 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000361 // If the alignment is not a power of 2, round up to the next power of 2.
362 // This happens for non-power-of-2 length vectors.
363 // FIXME: this should probably be a target property.
364 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000365 break;
366 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000367
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000368 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000369 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000370 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000371 case BuiltinType::Void:
372 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000373 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000374 Width = Target.getBoolWidth();
375 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000376 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000377 case BuiltinType::Char_S:
378 case BuiltinType::Char_U:
379 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000380 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000381 Width = Target.getCharWidth();
382 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000383 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000384 case BuiltinType::WChar:
385 Width = Target.getWCharWidth();
386 Align = Target.getWCharAlign();
387 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000388 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000389 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000390 Width = Target.getShortWidth();
391 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000392 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000393 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000394 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000395 Width = Target.getIntWidth();
396 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000397 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000398 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000399 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000400 Width = Target.getLongWidth();
401 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000402 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000403 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000404 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000405 Width = Target.getLongLongWidth();
406 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000407 break;
408 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000409 Width = Target.getFloatWidth();
410 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000411 break;
412 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000413 Width = Target.getDoubleWidth();
414 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000415 break;
416 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000417 Width = Target.getLongDoubleWidth();
418 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000419 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000420 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000421 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000422 case Type::FixedWidthInt:
423 // FIXME: This isn't precisely correct; the width/alignment should depend
424 // on the available types for the target
425 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000426 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000427 Align = Width;
428 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000429 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000430 // FIXME: Pointers into different addr spaces could have different sizes and
431 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000432 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000433 case Type::ObjCQualifiedId:
Douglas Gregor72564e72009-02-26 23:50:07 +0000434 case Type::ObjCQualifiedInterface:
Chris Lattner5426bf62008-04-07 07:01:58 +0000435 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000436 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000437 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000438 case Type::BlockPointer: {
439 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
440 Width = Target.getPointerWidth(AS);
441 Align = Target.getPointerAlign(AS);
442 break;
443 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000444 case Type::Pointer: {
445 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000446 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000447 Align = Target.getPointerAlign(AS);
448 break;
449 }
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000450 case Type::LValueReference:
451 case Type::RValueReference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000452 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000453 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000454 // FIXME: This is wrong for struct layout: a reference in a struct has
455 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000456 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000457 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000458 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000459 // the GCC ABI, where pointers to data are one pointer large, pointers to
460 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000461 // other compilers too, we need to delegate this completely to TargetInfo
462 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000463 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
464 unsigned AS = Pointee.getAddressSpace();
465 Width = Target.getPointerWidth(AS);
466 if (Pointee->isFunctionType())
467 Width *= 2;
468 Align = Target.getPointerAlign(AS);
469 // GCC aligns at single pointer width.
470 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000471 case Type::Complex: {
472 // Complex types have the same alignment as their elements, but twice the
473 // size.
474 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000475 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000476 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000477 Align = EltInfo.second;
478 break;
479 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000480 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000481 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000482 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
483 Width = Layout.getSize();
484 Align = Layout.getAlignment();
485 break;
486 }
Douglas Gregor72564e72009-02-26 23:50:07 +0000487 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +0000488 case Type::Enum: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000489 const TagType *TT = cast<TagType>(T);
490
491 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000492 Width = 1;
493 Align = 1;
494 break;
495 }
496
Daniel Dunbar1d751182008-11-08 05:48:37 +0000497 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000498 return getTypeInfo(ET->getDecl()->getIntegerType());
499
Daniel Dunbar1d751182008-11-08 05:48:37 +0000500 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000501 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
502 Width = Layout.getSize();
503 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000504 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000505 }
Douglas Gregor7532dc62009-03-30 22:58:21 +0000506
507 case Type::TemplateSpecialization:
508 assert(false && "Dependent types have no size");
509 break;
Chris Lattner71763312008-04-06 22:05:18 +0000510 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000511
Chris Lattner464175b2007-07-18 17:52:12 +0000512 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000513 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000514}
515
Chris Lattner34ebde42009-01-27 18:08:34 +0000516/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
517/// type for the current target in bits. This can be different than the ABI
518/// alignment in cases where it is beneficial for performance to overalign
519/// a data type.
520unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
521 unsigned ABIAlign = getTypeAlign(T);
522
523 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000524 if (T->isSpecificBuiltinType(BuiltinType::Double))
525 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000526
527 return ABIAlign;
528}
529
530
Devang Patel8b277042008-06-04 21:22:16 +0000531/// LayoutField - Field layout.
532void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000533 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000534 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000535 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000536 uint64_t FieldOffset = IsUnion ? 0 : Size;
537 uint64_t FieldSize;
538 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000539
540 // FIXME: Should this override struct packing? Probably we want to
541 // take the minimum?
542 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
543 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000544
545 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
546 // TODO: Need to check this algorithm on other targets!
547 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000548 FieldSize =
549 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000550
551 std::pair<uint64_t, unsigned> FieldInfo =
552 Context.getTypeInfo(FD->getType());
553 uint64_t TypeSize = FieldInfo.first;
554
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000555 // Determine the alignment of this bitfield. The packing
556 // attributes define a maximum and the alignment attribute defines
557 // a minimum.
558 // FIXME: What is the right behavior when the specified alignment
559 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000560 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000561 if (FieldPacking)
562 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000563 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
564 FieldAlign = std::max(FieldAlign, AA->getAlignment());
565
566 // Check if we need to add padding to give the field the correct
567 // alignment.
568 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
569 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
570
571 // Padding members don't affect overall alignment
572 if (!FD->getIdentifier())
573 FieldAlign = 1;
574 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000575 if (FD->getType()->isIncompleteArrayType()) {
576 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000577 // query getTypeInfo about these, so we figure it out here.
578 // Flexible array members don't have any size, but they
579 // have to be aligned appropriately for their element type.
580 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000581 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000582 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson2f1169f2009-04-10 05:31:15 +0000583 } else if (const ReferenceType *RT = FD->getType()->getAsReferenceType()) {
584 unsigned AS = RT->getPointeeType().getAddressSpace();
585 FieldSize = Context.Target.getPointerWidth(AS);
586 FieldAlign = Context.Target.getPointerAlign(AS);
Devang Patel8b277042008-06-04 21:22:16 +0000587 } else {
588 std::pair<uint64_t, unsigned> FieldInfo =
589 Context.getTypeInfo(FD->getType());
590 FieldSize = FieldInfo.first;
591 FieldAlign = FieldInfo.second;
592 }
593
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000594 // Determine the alignment of this bitfield. The packing
595 // attributes define a maximum and the alignment attribute defines
596 // a minimum. Additionally, the packing alignment must be at least
597 // a byte for non-bitfields.
598 //
599 // FIXME: What is the right behavior when the specified alignment
600 // is smaller than the specified packing?
601 if (FieldPacking)
602 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000603 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
604 FieldAlign = std::max(FieldAlign, AA->getAlignment());
605
606 // Round up the current record size to the field's alignment boundary.
607 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
608 }
609
610 // Place this field at the current location.
611 FieldOffsets[FieldNo] = FieldOffset;
612
613 // Reserve space for this field.
614 if (IsUnion) {
615 Size = std::max(Size, FieldSize);
616 } else {
617 Size = FieldOffset + FieldSize;
618 }
619
620 // Remember max struct/class alignment.
621 Alignment = std::max(Alignment, FieldAlign);
622}
623
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000624static void CollectLocalObjCIvars(ASTContext *Ctx,
625 const ObjCInterfaceDecl *OI,
626 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000627 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
628 E = OI->ivar_end(); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000629 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000630 if (!IVDecl->isInvalidDecl())
631 Fields.push_back(cast<FieldDecl>(IVDecl));
632 }
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +0000633 // look into properties.
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000634 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*Ctx),
635 E = OI->prop_end(*Ctx); I != E; ++I) {
Chris Lattnerf1690852009-03-31 08:48:01 +0000636 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
Fariborz Jahanianaf3e7222009-03-31 00:06:29 +0000637 Fields.push_back(cast<FieldDecl>(IV));
638 }
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000639}
640
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000641void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
642 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
643 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
644 CollectObjCIvars(SuperClass, Fields);
645 CollectLocalObjCIvars(this, OI, Fields);
646}
647
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000648/// addRecordToClass - produces record info. for the class for its
649/// ivars and all those inherited.
650///
Chris Lattnerf1690852009-03-31 08:48:01 +0000651const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) {
Daniel Dunbar75da6742009-04-22 10:56:29 +0000652 assert(!D->isForwardDecl() && "Invalid decl!");
653
Chris Lattner23499252009-03-31 09:24:30 +0000654 RecordDecl *&RD = ASTRecordForInterface[D];
Daniel Dunbar75da6742009-04-22 10:56:29 +0000655 if (RD)
656 return RD;
Chris Lattnerf1690852009-03-31 08:48:01 +0000657
658 llvm::SmallVector<FieldDecl*, 32> RecFields;
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000659 CollectLocalObjCIvars(this, D, RecFields);
Chris Lattner23499252009-03-31 09:24:30 +0000660
Daniel Dunbar75da6742009-04-22 10:56:29 +0000661 RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(),
662 D->getIdentifier());
Daniel Dunbara80a0f62009-04-22 17:43:55 +0000663 const RecordDecl *SRD;
664 if (const ObjCInterfaceDecl *SuperClass = D->getSuperClass()) {
665 SRD = addRecordToClass(SuperClass);
666 } else {
667 SRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, SourceLocation(), 0);
668 const_cast<RecordDecl*>(SRD)->completeDefinition(*this);
669 }
670
671 RD->addDecl(*this,
672 FieldDecl::Create(*this, RD,
673 SourceLocation(),
674 0,
675 getTagDeclType(const_cast<RecordDecl*>(SRD)),
676 0, false));
677
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000678 /// FIXME! Can do collection of ivars and adding to the record while
679 /// doing it.
Chris Lattner16ff7052009-03-31 08:58:42 +0000680 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Douglas Gregor6ab35242009-04-09 21:40:53 +0000681 RD->addDecl(*this,
682 FieldDecl::Create(*this, RD,
Chris Lattner23499252009-03-31 09:24:30 +0000683 RecFields[i]->getLocation(),
684 RecFields[i]->getIdentifier(),
685 RecFields[i]->getType(),
686 RecFields[i]->getBitWidth(), false));
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000687 }
Chris Lattnerf1690852009-03-31 08:48:01 +0000688
Chris Lattner23499252009-03-31 09:24:30 +0000689 RD->completeDefinition(*this);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000690 return RD;
691}
Devang Patel44a3dde2008-06-04 21:54:36 +0000692
Chris Lattner61710852008-10-05 17:34:18 +0000693/// getASTObjcInterfaceLayout - Get or compute information about the layout of
694/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000695/// position information.
696const ASTRecordLayout &
697ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
698 // Look up this layout, if already laid out, return what we have.
699 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
700 if (Entry) return *Entry;
701
702 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
703 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000704 ASTRecordLayout *NewEntry = NULL;
Fariborz Jahanian0c7ce5b2009-04-08 21:54:52 +0000705 // FIXME. Add actual count of synthesized ivars, instead of count
706 // of properties which is the upper bound, but is safe.
Daniel Dunbar4af44122009-04-08 20:18:15 +0000707 unsigned FieldCount =
Douglas Gregor6ab35242009-04-09 21:40:53 +0000708 D->ivar_size() + std::distance(D->prop_begin(*this), D->prop_end(*this));
Devang Patel6a5a34c2008-06-06 02:14:01 +0000709 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
710 FieldCount++;
711 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
712 unsigned Alignment = SL.getAlignment();
713 uint64_t Size = SL.getSize();
714 NewEntry = new ASTRecordLayout(Size, Alignment);
715 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000716 // Super class is at the beginning of the layout.
717 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000718 } else {
719 NewEntry = new ASTRecordLayout();
720 NewEntry->InitializeLayout(FieldCount);
721 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000722 Entry = NewEntry;
723
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000724 unsigned StructPacking = 0;
725 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
726 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000727
728 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
729 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
730 AA->getAlignment()));
731
732 // Layout each ivar sequentially.
733 unsigned i = 0;
734 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
735 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
736 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000737 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000738 }
Fariborz Jahanian18191882009-03-31 18:11:23 +0000739 // Also synthesized ivars
Douglas Gregor6ab35242009-04-09 21:40:53 +0000740 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
741 E = D->prop_end(*this); I != E; ++I) {
Fariborz Jahanian18191882009-03-31 18:11:23 +0000742 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
743 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
744 }
Fariborz Jahanian99eee362009-04-01 19:37:34 +0000745
Devang Patel44a3dde2008-06-04 21:54:36 +0000746 // Finally, round the size of the total struct up to the alignment of the
747 // struct itself.
748 NewEntry->FinalizeLayout();
749 return *NewEntry;
750}
751
Devang Patel88a981b2007-11-01 19:11:01 +0000752/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000753/// specified record (struct/union/class), which indicates its size and field
754/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000755const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000756 D = D->getDefinition(*this);
757 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000758
Chris Lattner464175b2007-07-18 17:52:12 +0000759 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000760 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000761 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000762
Devang Patel88a981b2007-11-01 19:11:01 +0000763 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
764 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
765 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000766 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000767
Douglas Gregore267ff32008-12-11 20:41:00 +0000768 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor6ab35242009-04-09 21:40:53 +0000769 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
770 D->field_end(*this)));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000771 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000772
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000773 unsigned StructPacking = 0;
774 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
775 StructPacking = PA->getAlignment();
776
Eli Friedman4bd998b2008-05-30 09:31:38 +0000777 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000778 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
779 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000780
Eli Friedman4bd998b2008-05-30 09:31:38 +0000781 // Layout each field, for now, just sequentially, respecting alignment. In
782 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000783 unsigned FieldIdx = 0;
Douglas Gregor6ab35242009-04-09 21:40:53 +0000784 for (RecordDecl::field_iterator Field = D->field_begin(*this),
785 FieldEnd = D->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +0000786 Field != FieldEnd; (void)++Field, ++FieldIdx)
787 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000788
789 // Finally, round the size of the total struct up to the alignment of the
790 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000791 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000792 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000793}
794
Chris Lattnera7674d82007-07-13 22:13:22 +0000795//===----------------------------------------------------------------------===//
796// Type creation/memoization methods
797//===----------------------------------------------------------------------===//
798
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000799QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000800 QualType CanT = getCanonicalType(T);
801 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000802 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000803
804 // If we are composing extended qualifiers together, merge together into one
805 // ExtQualType node.
806 unsigned CVRQuals = T.getCVRQualifiers();
807 QualType::GCAttrTypes GCAttr = QualType::GCNone;
808 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000809
Chris Lattnerb7d25532009-02-18 22:53:11 +0000810 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
811 // If this type already has an address space specified, it cannot get
812 // another one.
813 assert(EQT->getAddressSpace() == 0 &&
814 "Type cannot be in multiple addr spaces!");
815 GCAttr = EQT->getObjCGCAttr();
816 TypeNode = EQT->getBaseType();
817 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000818
Chris Lattnerb7d25532009-02-18 22:53:11 +0000819 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000820 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000821 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000822 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000823 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000824 return QualType(EXTQy, CVRQuals);
825
Christopher Lambebb97e92008-02-04 02:31:56 +0000826 // If the base type isn't canonical, this won't be a canonical type either,
827 // so fill in the canonical type field.
828 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000829 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000830 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000831
Chris Lattnerb7d25532009-02-18 22:53:11 +0000832 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000833 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000834 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000835 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000836 ExtQualType *New =
837 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000838 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000839 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000840 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000841}
842
Chris Lattnerb7d25532009-02-18 22:53:11 +0000843QualType ASTContext::getObjCGCQualType(QualType T,
844 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000845 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000846 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000847 return T;
848
Chris Lattnerb7d25532009-02-18 22:53:11 +0000849 // If we are composing extended qualifiers together, merge together into one
850 // ExtQualType node.
851 unsigned CVRQuals = T.getCVRQualifiers();
852 Type *TypeNode = T.getTypePtr();
853 unsigned AddressSpace = 0;
854
855 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
856 // If this type already has an address space specified, it cannot get
857 // another one.
858 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
859 "Type cannot be in multiple addr spaces!");
860 AddressSpace = EQT->getAddressSpace();
861 TypeNode = EQT->getBaseType();
862 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000863
864 // Check if we've already instantiated an gc qual'd type of this type.
865 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000866 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000867 void *InsertPos = 0;
868 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000869 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000870
871 // If the base type isn't canonical, this won't be a canonical type either,
872 // so fill in the canonical type field.
Eli Friedman5a61f0e2009-02-27 23:04:43 +0000873 // FIXME: Isn't this also not canonical if the base type is a array
874 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000875 QualType Canonical;
876 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000877 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000878
Chris Lattnerb7d25532009-02-18 22:53:11 +0000879 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000880 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
881 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
882 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000883 ExtQualType *New =
884 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000885 ExtQualTypes.InsertNode(New, InsertPos);
886 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000887 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000888}
Chris Lattnera7674d82007-07-13 22:13:22 +0000889
Reid Spencer5f016e22007-07-11 17:01:13 +0000890/// getComplexType - Return the uniqued reference to the type for a complex
891/// number with the specified element type.
892QualType ASTContext::getComplexType(QualType T) {
893 // Unique pointers, to guarantee there is only one pointer of a particular
894 // structure.
895 llvm::FoldingSetNodeID ID;
896 ComplexType::Profile(ID, T);
897
898 void *InsertPos = 0;
899 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
900 return QualType(CT, 0);
901
902 // If the pointee type isn't canonical, this won't be a canonical type either,
903 // so fill in the canonical type field.
904 QualType Canonical;
905 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000906 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000907
908 // Get the new insert position for the node we care about.
909 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000910 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000911 }
Steve Narofff83820b2009-01-27 22:08:43 +0000912 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 Types.push_back(New);
914 ComplexTypes.InsertNode(New, InsertPos);
915 return QualType(New, 0);
916}
917
Eli Friedmanf98aba32009-02-13 02:31:07 +0000918QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
919 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
920 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
921 FixedWidthIntType *&Entry = Map[Width];
922 if (!Entry)
923 Entry = new FixedWidthIntType(Width, Signed);
924 return QualType(Entry, 0);
925}
Reid Spencer5f016e22007-07-11 17:01:13 +0000926
927/// getPointerType - Return the uniqued reference to the type for a pointer to
928/// the specified type.
929QualType ASTContext::getPointerType(QualType T) {
930 // Unique pointers, to guarantee there is only one pointer of a particular
931 // structure.
932 llvm::FoldingSetNodeID ID;
933 PointerType::Profile(ID, T);
934
935 void *InsertPos = 0;
936 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
937 return QualType(PT, 0);
938
939 // If the pointee type isn't canonical, this won't be a canonical type either,
940 // so fill in the canonical type field.
941 QualType Canonical;
942 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000943 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000944
945 // Get the new insert position for the node we care about.
946 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000947 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000948 }
Steve Narofff83820b2009-01-27 22:08:43 +0000949 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000950 Types.push_back(New);
951 PointerTypes.InsertNode(New, InsertPos);
952 return QualType(New, 0);
953}
954
Steve Naroff5618bd42008-08-27 16:04:49 +0000955/// getBlockPointerType - Return the uniqued reference to the type for
956/// a pointer to the specified block.
957QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000958 assert(T->isFunctionType() && "block of function types only");
959 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000960 // structure.
961 llvm::FoldingSetNodeID ID;
962 BlockPointerType::Profile(ID, T);
963
964 void *InsertPos = 0;
965 if (BlockPointerType *PT =
966 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
967 return QualType(PT, 0);
968
Steve Naroff296e8d52008-08-28 19:20:44 +0000969 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000970 // type either so fill in the canonical type field.
971 QualType Canonical;
972 if (!T->isCanonical()) {
973 Canonical = getBlockPointerType(getCanonicalType(T));
974
975 // Get the new insert position for the node we care about.
976 BlockPointerType *NewIP =
977 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000978 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000979 }
Steve Narofff83820b2009-01-27 22:08:43 +0000980 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000981 Types.push_back(New);
982 BlockPointerTypes.InsertNode(New, InsertPos);
983 return QualType(New, 0);
984}
985
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000986/// getLValueReferenceType - Return the uniqued reference to the type for an
987/// lvalue reference to the specified type.
988QualType ASTContext::getLValueReferenceType(QualType T) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000989 // Unique pointers, to guarantee there is only one pointer of a particular
990 // structure.
991 llvm::FoldingSetNodeID ID;
992 ReferenceType::Profile(ID, T);
993
994 void *InsertPos = 0;
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000995 if (LValueReferenceType *RT =
996 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 return QualType(RT, 0);
Sebastian Redl7c80bd62009-03-16 23:22:08 +0000998
Reid Spencer5f016e22007-07-11 17:01:13 +0000999 // If the referencee type isn't canonical, this won't be a canonical type
1000 // either, so fill in the canonical type field.
1001 QualType Canonical;
1002 if (!T->isCanonical()) {
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001003 Canonical = getLValueReferenceType(getCanonicalType(T));
1004
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 // Get the new insert position for the node we care about.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001006 LValueReferenceType *NewIP =
1007 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001008 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001009 }
1010
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001011 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 Types.push_back(New);
Sebastian Redl7c80bd62009-03-16 23:22:08 +00001013 LValueReferenceTypes.InsertNode(New, InsertPos);
1014 return QualType(New, 0);
1015}
1016
1017/// getRValueReferenceType - Return the uniqued reference to the type for an
1018/// rvalue reference to the specified type.
1019QualType ASTContext::getRValueReferenceType(QualType T) {
1020 // Unique pointers, to guarantee there is only one pointer of a particular
1021 // structure.
1022 llvm::FoldingSetNodeID ID;
1023 ReferenceType::Profile(ID, T);
1024
1025 void *InsertPos = 0;
1026 if (RValueReferenceType *RT =
1027 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1028 return QualType(RT, 0);
1029
1030 // If the referencee type isn't canonical, this won't be a canonical type
1031 // either, so fill in the canonical type field.
1032 QualType Canonical;
1033 if (!T->isCanonical()) {
1034 Canonical = getRValueReferenceType(getCanonicalType(T));
1035
1036 // Get the new insert position for the node we care about.
1037 RValueReferenceType *NewIP =
1038 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1039 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1040 }
1041
1042 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1043 Types.push_back(New);
1044 RValueReferenceTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001045 return QualType(New, 0);
1046}
1047
Sebastian Redlf30208a2009-01-24 21:16:55 +00001048/// getMemberPointerType - Return the uniqued reference to the type for a
1049/// member pointer to the specified type, in the specified class.
1050QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1051{
1052 // Unique pointers, to guarantee there is only one pointer of a particular
1053 // structure.
1054 llvm::FoldingSetNodeID ID;
1055 MemberPointerType::Profile(ID, T, Cls);
1056
1057 void *InsertPos = 0;
1058 if (MemberPointerType *PT =
1059 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1060 return QualType(PT, 0);
1061
1062 // If the pointee or class type isn't canonical, this won't be a canonical
1063 // type either, so fill in the canonical type field.
1064 QualType Canonical;
1065 if (!T->isCanonical()) {
1066 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1067
1068 // Get the new insert position for the node we care about.
1069 MemberPointerType *NewIP =
1070 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1071 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1072 }
Steve Narofff83820b2009-01-27 22:08:43 +00001073 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +00001074 Types.push_back(New);
1075 MemberPointerTypes.InsertNode(New, InsertPos);
1076 return QualType(New, 0);
1077}
1078
Steve Narofffb22d962007-08-30 01:06:46 +00001079/// getConstantArrayType - Return the unique reference to the type for an
1080/// array of the specified element type.
1081QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +00001082 const llvm::APInt &ArySize,
1083 ArrayType::ArraySizeModifier ASM,
1084 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001086 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001087
1088 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001089 if (ConstantArrayType *ATP =
1090 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001091 return QualType(ATP, 0);
1092
1093 // If the element type isn't canonical, this won't be a canonical type either,
1094 // so fill in the canonical type field.
1095 QualType Canonical;
1096 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001097 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +00001098 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001099 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001100 ConstantArrayType *NewIP =
1101 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001102 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001103 }
1104
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001105 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001106 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001107 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001108 Types.push_back(New);
1109 return QualType(New, 0);
1110}
1111
Steve Naroffbdbf7b02007-08-30 18:14:25 +00001112/// getVariableArrayType - Returns a non-unique reference to the type for a
1113/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +00001114QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1115 ArrayType::ArraySizeModifier ASM,
1116 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001117 // Since we don't unique expressions, it isn't possible to unique VLA's
1118 // that have an expression provided for their size.
1119
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001120 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001121 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001122
1123 VariableArrayTypes.push_back(New);
1124 Types.push_back(New);
1125 return QualType(New, 0);
1126}
1127
Douglas Gregor898574e2008-12-05 23:32:09 +00001128/// getDependentSizedArrayType - Returns a non-unique reference to
1129/// the type for a dependently-sized array of the specified element
1130/// type. FIXME: We will need these to be uniqued, or at least
1131/// comparable, at some point.
1132QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1133 ArrayType::ArraySizeModifier ASM,
1134 unsigned EltTypeQuals) {
1135 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1136 "Size must be type- or value-dependent!");
1137
1138 // Since we don't unique expressions, it isn't possible to unique
1139 // dependently-sized array types.
1140
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001141 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001142 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1143 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001144
1145 DependentSizedArrayTypes.push_back(New);
1146 Types.push_back(New);
1147 return QualType(New, 0);
1148}
1149
Eli Friedmanc5773c42008-02-15 18:16:39 +00001150QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1151 ArrayType::ArraySizeModifier ASM,
1152 unsigned EltTypeQuals) {
1153 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001154 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001155
1156 void *InsertPos = 0;
1157 if (IncompleteArrayType *ATP =
1158 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1159 return QualType(ATP, 0);
1160
1161 // If the element type isn't canonical, this won't be a canonical type
1162 // either, so fill in the canonical type field.
1163 QualType Canonical;
1164
1165 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001166 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001167 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001168
1169 // Get the new insert position for the node we care about.
1170 IncompleteArrayType *NewIP =
1171 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001172 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001173 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001174
Steve Narofff83820b2009-01-27 22:08:43 +00001175 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001176 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001177
1178 IncompleteArrayTypes.InsertNode(New, InsertPos);
1179 Types.push_back(New);
1180 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001181}
1182
Steve Naroff73322922007-07-18 18:00:27 +00001183/// getVectorType - Return the unique reference to a vector type of
1184/// the specified element type and size. VectorType must be a built-in type.
1185QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 BuiltinType *baseType;
1187
Chris Lattnerf52ab252008-04-06 22:59:24 +00001188 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001189 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001190
1191 // Check if we've already instantiated a vector of this type.
1192 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001193 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 void *InsertPos = 0;
1195 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1196 return QualType(VTP, 0);
1197
1198 // If the element type isn't canonical, this won't be a canonical type either,
1199 // so fill in the canonical type field.
1200 QualType Canonical;
1201 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001202 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001203
1204 // Get the new insert position for the node we care about.
1205 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001206 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001207 }
Steve Narofff83820b2009-01-27 22:08:43 +00001208 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001209 VectorTypes.InsertNode(New, InsertPos);
1210 Types.push_back(New);
1211 return QualType(New, 0);
1212}
1213
Nate Begeman213541a2008-04-18 23:10:10 +00001214/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001215/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001216QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001217 BuiltinType *baseType;
1218
Chris Lattnerf52ab252008-04-06 22:59:24 +00001219 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001220 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001221
1222 // Check if we've already instantiated a vector of this type.
1223 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001224 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001225 void *InsertPos = 0;
1226 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1227 return QualType(VTP, 0);
1228
1229 // If the element type isn't canonical, this won't be a canonical type either,
1230 // so fill in the canonical type field.
1231 QualType Canonical;
1232 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001233 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001234
1235 // Get the new insert position for the node we care about.
1236 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001237 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001238 }
Steve Narofff83820b2009-01-27 22:08:43 +00001239 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001240 VectorTypes.InsertNode(New, InsertPos);
1241 Types.push_back(New);
1242 return QualType(New, 0);
1243}
1244
Douglas Gregor72564e72009-02-26 23:50:07 +00001245/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Reid Spencer5f016e22007-07-11 17:01:13 +00001246///
Douglas Gregor72564e72009-02-26 23:50:07 +00001247QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001248 // Unique functions, to guarantee there is only one function of a particular
1249 // structure.
1250 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001251 FunctionNoProtoType::Profile(ID, ResultTy);
Reid Spencer5f016e22007-07-11 17:01:13 +00001252
1253 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001254 if (FunctionNoProtoType *FT =
1255 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 return QualType(FT, 0);
1257
1258 QualType Canonical;
1259 if (!ResultTy->isCanonical()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001260 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001261
1262 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001263 FunctionNoProtoType *NewIP =
1264 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001265 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001266 }
1267
Douglas Gregor72564e72009-02-26 23:50:07 +00001268 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 Types.push_back(New);
Douglas Gregor72564e72009-02-26 23:50:07 +00001270 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 return QualType(New, 0);
1272}
1273
1274/// getFunctionType - Return a normal function type with a typed argument
1275/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001276QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001277 unsigned NumArgs, bool isVariadic,
1278 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 // Unique functions, to guarantee there is only one function of a particular
1280 // structure.
1281 llvm::FoldingSetNodeID ID;
Douglas Gregor72564e72009-02-26 23:50:07 +00001282 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001283 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001284
1285 void *InsertPos = 0;
Douglas Gregor72564e72009-02-26 23:50:07 +00001286 if (FunctionProtoType *FTP =
1287 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 return QualType(FTP, 0);
1289
1290 // Determine whether the type being created is already canonical or not.
1291 bool isCanonical = ResultTy->isCanonical();
1292 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1293 if (!ArgArray[i]->isCanonical())
1294 isCanonical = false;
1295
1296 // If this type isn't canonical, get the canonical version of it.
1297 QualType Canonical;
1298 if (!isCanonical) {
1299 llvm::SmallVector<QualType, 16> CanonicalArgs;
1300 CanonicalArgs.reserve(NumArgs);
1301 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001302 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001303
Chris Lattnerf52ab252008-04-06 22:59:24 +00001304 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001305 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001306 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001307
1308 // Get the new insert position for the node we care about.
Douglas Gregor72564e72009-02-26 23:50:07 +00001309 FunctionProtoType *NewIP =
1310 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001311 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 }
1313
Douglas Gregor72564e72009-02-26 23:50:07 +00001314 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001315 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor72564e72009-02-26 23:50:07 +00001316 FunctionProtoType *FTP =
1317 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroffc0ac4922009-01-27 23:20:32 +00001318 NumArgs*sizeof(QualType), 8);
Douglas Gregor72564e72009-02-26 23:50:07 +00001319 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001320 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001321 Types.push_back(FTP);
Douglas Gregor72564e72009-02-26 23:50:07 +00001322 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001323 return QualType(FTP, 0);
1324}
1325
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001326/// getTypeDeclType - Return the unique reference to the type for the
1327/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001328QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001329 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001330 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1331
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001332 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001333 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001334 else if (isa<TemplateTypeParmDecl>(Decl)) {
1335 assert(false && "Template type parameter types are always available.");
1336 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001337 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001338
Douglas Gregorc1efaec2009-02-28 01:32:25 +00001339 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001340 if (PrevDecl)
1341 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001342 else
1343 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001344 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001345 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1346 if (PrevDecl)
1347 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001348 else
1349 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001350 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001351 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001352 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001353
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001354 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001355 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001356}
1357
Reid Spencer5f016e22007-07-11 17:01:13 +00001358/// getTypedefType - Return the unique reference to the type for the
1359/// specified typename decl.
1360QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1361 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1362
Chris Lattnerf52ab252008-04-06 22:59:24 +00001363 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001364 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001365 Types.push_back(Decl->TypeForDecl);
1366 return QualType(Decl->TypeForDecl, 0);
1367}
1368
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001369/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001370/// specified ObjC interface decl.
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001371QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001372 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1373
Daniel Dunbar3b3a4582009-04-22 04:34:53 +00001374 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1375 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff3536b442007-09-06 21:24:23 +00001376 Types.push_back(Decl->TypeForDecl);
1377 return QualType(Decl->TypeForDecl, 0);
1378}
1379
Douglas Gregorfab9d672009-02-05 23:33:38 +00001380/// \brief Retrieve the template type parameter type for a template
1381/// parameter with the given depth, index, and (optionally) name.
1382QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1383 IdentifierInfo *Name) {
1384 llvm::FoldingSetNodeID ID;
1385 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1386 void *InsertPos = 0;
1387 TemplateTypeParmType *TypeParm
1388 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1389
1390 if (TypeParm)
1391 return QualType(TypeParm, 0);
1392
1393 if (Name)
1394 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1395 getTemplateTypeParmType(Depth, Index));
1396 else
1397 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1398
1399 Types.push_back(TypeParm);
1400 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1401
1402 return QualType(TypeParm, 0);
1403}
1404
Douglas Gregor55f6b142009-02-09 18:46:07 +00001405QualType
Douglas Gregor7532dc62009-03-30 22:58:21 +00001406ASTContext::getTemplateSpecializationType(TemplateName Template,
1407 const TemplateArgument *Args,
1408 unsigned NumArgs,
1409 QualType Canon) {
Douglas Gregor40808ce2009-03-09 23:48:35 +00001410 if (!Canon.isNull())
1411 Canon = getCanonicalType(Canon);
Douglas Gregorfc705b82009-02-26 22:19:44 +00001412
Douglas Gregor55f6b142009-02-09 18:46:07 +00001413 llvm::FoldingSetNodeID ID;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001414 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregor40808ce2009-03-09 23:48:35 +00001415
Douglas Gregor55f6b142009-02-09 18:46:07 +00001416 void *InsertPos = 0;
Douglas Gregor7532dc62009-03-30 22:58:21 +00001417 TemplateSpecializationType *Spec
1418 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001419
1420 if (Spec)
1421 return QualType(Spec, 0);
1422
Douglas Gregor7532dc62009-03-30 22:58:21 +00001423 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregor40808ce2009-03-09 23:48:35 +00001424 sizeof(TemplateArgument) * NumArgs),
1425 8);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001426 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001427 Types.push_back(Spec);
Douglas Gregor7532dc62009-03-30 22:58:21 +00001428 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor55f6b142009-02-09 18:46:07 +00001429
1430 return QualType(Spec, 0);
1431}
1432
Douglas Gregore4e5b052009-03-19 00:18:19 +00001433QualType
Douglas Gregorab452ba2009-03-26 23:50:42 +00001434ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregore4e5b052009-03-19 00:18:19 +00001435 QualType NamedType) {
1436 llvm::FoldingSetNodeID ID;
Douglas Gregorab452ba2009-03-26 23:50:42 +00001437 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregore4e5b052009-03-19 00:18:19 +00001438
1439 void *InsertPos = 0;
1440 QualifiedNameType *T
1441 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1442 if (T)
1443 return QualType(T, 0);
1444
Douglas Gregorab452ba2009-03-26 23:50:42 +00001445 T = new (*this) QualifiedNameType(NNS, NamedType,
1446 getCanonicalType(NamedType));
Douglas Gregore4e5b052009-03-19 00:18:19 +00001447 Types.push_back(T);
1448 QualifiedNameTypes.InsertNode(T, InsertPos);
1449 return QualType(T, 0);
1450}
1451
Douglas Gregord57959a2009-03-27 23:10:48 +00001452QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1453 const IdentifierInfo *Name,
1454 QualType Canon) {
1455 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1456
1457 if (Canon.isNull()) {
1458 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1459 if (CanonNNS != NNS)
1460 Canon = getTypenameType(CanonNNS, Name);
1461 }
1462
1463 llvm::FoldingSetNodeID ID;
1464 TypenameType::Profile(ID, NNS, Name);
1465
1466 void *InsertPos = 0;
1467 TypenameType *T
1468 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1469 if (T)
1470 return QualType(T, 0);
1471
1472 T = new (*this) TypenameType(NNS, Name, Canon);
1473 Types.push_back(T);
1474 TypenameTypes.InsertNode(T, InsertPos);
1475 return QualType(T, 0);
1476}
1477
Douglas Gregor17343172009-04-01 00:28:59 +00001478QualType
1479ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1480 const TemplateSpecializationType *TemplateId,
1481 QualType Canon) {
1482 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1483
1484 if (Canon.isNull()) {
1485 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1486 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1487 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1488 const TemplateSpecializationType *CanonTemplateId
1489 = CanonType->getAsTemplateSpecializationType();
1490 assert(CanonTemplateId &&
1491 "Canonical type must also be a template specialization type");
1492 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1493 }
1494 }
1495
1496 llvm::FoldingSetNodeID ID;
1497 TypenameType::Profile(ID, NNS, TemplateId);
1498
1499 void *InsertPos = 0;
1500 TypenameType *T
1501 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1502 if (T)
1503 return QualType(T, 0);
1504
1505 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1506 Types.push_back(T);
1507 TypenameTypes.InsertNode(T, InsertPos);
1508 return QualType(T, 0);
1509}
1510
Chris Lattner88cb27a2008-04-07 04:56:42 +00001511/// CmpProtocolNames - Comparison predicate for sorting protocols
1512/// alphabetically.
1513static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1514 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001515 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001516}
1517
1518static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1519 unsigned &NumProtocols) {
1520 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1521
1522 // Sort protocols, keyed by name.
1523 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1524
1525 // Remove duplicates.
1526 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1527 NumProtocols = ProtocolsEnd-Protocols;
1528}
1529
1530
Chris Lattner065f0d72008-04-07 04:44:08 +00001531/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1532/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001533QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1534 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001535 // Sort the protocol list alphabetically to canonicalize it.
1536 SortAndUniqueProtocols(Protocols, NumProtocols);
1537
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001538 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001539 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001540
1541 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001542 if (ObjCQualifiedInterfaceType *QT =
1543 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001544 return QualType(QT, 0);
1545
1546 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001547 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001548 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001549
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001550 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001551 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001552 return QualType(QType, 0);
1553}
1554
Chris Lattner88cb27a2008-04-07 04:56:42 +00001555/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1556/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001557QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001558 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001559 // Sort the protocol list alphabetically to canonicalize it.
1560 SortAndUniqueProtocols(Protocols, NumProtocols);
1561
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001562 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001563 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001564
1565 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001566 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001567 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001568 return QualType(QT, 0);
1569
1570 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001571 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001572 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001573 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001574 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001575 return QualType(QType, 0);
1576}
1577
Douglas Gregor72564e72009-02-26 23:50:07 +00001578/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1579/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff9752f252007-08-01 18:02:17 +00001580/// multiple declarations that refer to "typeof(x)" all contain different
1581/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1582/// on canonical type's (which are always unique).
Douglas Gregor72564e72009-02-26 23:50:07 +00001583QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001584 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor72564e72009-02-26 23:50:07 +00001585 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001586 Types.push_back(toe);
1587 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001588}
1589
Steve Naroff9752f252007-08-01 18:02:17 +00001590/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1591/// TypeOfType AST's. The only motivation to unique these nodes would be
1592/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1593/// an issue. This doesn't effect the type checker, since it operates
1594/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001595QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001596 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001597 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001598 Types.push_back(tot);
1599 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001600}
1601
Reid Spencer5f016e22007-07-11 17:01:13 +00001602/// getTagDeclType - Return the unique reference to the type for the
1603/// specified TagDecl (struct/union/class/enum) decl.
1604QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001605 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001606 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001607}
1608
1609/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1610/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1611/// needs to agree with the definition in <stddef.h>.
1612QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001613 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001614}
1615
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001616/// getSignedWCharType - Return the type of "signed wchar_t".
1617/// Used when in C++, as a GCC extension.
1618QualType ASTContext::getSignedWCharType() const {
1619 // FIXME: derive from "Target" ?
1620 return WCharTy;
1621}
1622
1623/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1624/// Used when in C++, as a GCC extension.
1625QualType ASTContext::getUnsignedWCharType() const {
1626 // FIXME: derive from "Target" ?
1627 return UnsignedIntTy;
1628}
1629
Chris Lattner8b9023b2007-07-13 03:05:23 +00001630/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1631/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1632QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001633 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001634}
1635
Chris Lattnere6327742008-04-02 05:18:44 +00001636//===----------------------------------------------------------------------===//
1637// Type Operators
1638//===----------------------------------------------------------------------===//
1639
Chris Lattner77c96472008-04-06 22:41:35 +00001640/// getCanonicalType - Return the canonical (structural) type corresponding to
1641/// the specified potentially non-canonical type. The non-canonical version
1642/// of a type may have many "decorated" versions of types. Decorators can
1643/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1644/// to be free of any of these, allowing two canonical types to be compared
1645/// for exact equality with a simple pointer comparison.
1646QualType ASTContext::getCanonicalType(QualType T) {
1647 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001648
1649 // If the result has type qualifiers, make sure to canonicalize them as well.
1650 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1651 if (TypeQuals == 0) return CanType;
1652
1653 // If the type qualifiers are on an array type, get the canonical type of the
1654 // array with the qualifiers applied to the element type.
1655 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1656 if (!AT)
1657 return CanType.getQualifiedType(TypeQuals);
1658
1659 // Get the canonical version of the element with the extra qualifiers on it.
1660 // This can recursively sink qualifiers through multiple levels of arrays.
1661 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1662 NewEltTy = getCanonicalType(NewEltTy);
1663
1664 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1665 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1666 CAT->getIndexTypeQualifier());
1667 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1668 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1669 IAT->getIndexTypeQualifier());
1670
Douglas Gregor898574e2008-12-05 23:32:09 +00001671 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1672 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1673 DSAT->getSizeModifier(),
1674 DSAT->getIndexTypeQualifier());
1675
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001676 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1677 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1678 VAT->getSizeModifier(),
1679 VAT->getIndexTypeQualifier());
1680}
1681
Douglas Gregord57959a2009-03-27 23:10:48 +00001682NestedNameSpecifier *
1683ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1684 if (!NNS)
1685 return 0;
1686
1687 switch (NNS->getKind()) {
1688 case NestedNameSpecifier::Identifier:
1689 // Canonicalize the prefix but keep the identifier the same.
1690 return NestedNameSpecifier::Create(*this,
1691 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1692 NNS->getAsIdentifier());
1693
1694 case NestedNameSpecifier::Namespace:
1695 // A namespace is canonical; build a nested-name-specifier with
1696 // this namespace and no prefix.
1697 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1698
1699 case NestedNameSpecifier::TypeSpec:
1700 case NestedNameSpecifier::TypeSpecWithTemplate: {
1701 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1702 NestedNameSpecifier *Prefix = 0;
1703
1704 // FIXME: This isn't the right check!
1705 if (T->isDependentType())
1706 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1707
1708 return NestedNameSpecifier::Create(*this, Prefix,
1709 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1710 T.getTypePtr());
1711 }
1712
1713 case NestedNameSpecifier::Global:
1714 // The global specifier is canonical and unique.
1715 return NNS;
1716 }
1717
1718 // Required to silence a GCC warning
1719 return 0;
1720}
1721
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001722
1723const ArrayType *ASTContext::getAsArrayType(QualType T) {
1724 // Handle the non-qualified case efficiently.
1725 if (T.getCVRQualifiers() == 0) {
1726 // Handle the common positive case fast.
1727 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1728 return AT;
1729 }
1730
1731 // Handle the common negative case fast, ignoring CVR qualifiers.
1732 QualType CType = T->getCanonicalTypeInternal();
1733
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001734 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001735 // test.
1736 if (!isa<ArrayType>(CType) &&
1737 !isa<ArrayType>(CType.getUnqualifiedType()))
1738 return 0;
1739
1740 // Apply any CVR qualifiers from the array type to the element type. This
1741 // implements C99 6.7.3p8: "If the specification of an array type includes
1742 // any type qualifiers, the element type is so qualified, not the array type."
1743
1744 // If we get here, we either have type qualifiers on the type, or we have
1745 // sugar such as a typedef in the way. If we have type qualifiers on the type
1746 // we must propagate them down into the elemeng type.
1747 unsigned CVRQuals = T.getCVRQualifiers();
1748 unsigned AddrSpace = 0;
1749 Type *Ty = T.getTypePtr();
1750
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001751 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001752 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001753 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1754 AddrSpace = EXTQT->getAddressSpace();
1755 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001756 } else {
1757 T = Ty->getDesugaredType();
1758 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1759 break;
1760 CVRQuals |= T.getCVRQualifiers();
1761 Ty = T.getTypePtr();
1762 }
1763 }
1764
1765 // If we have a simple case, just return now.
1766 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1767 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1768 return ATy;
1769
1770 // Otherwise, we have an array and we have qualifiers on it. Push the
1771 // qualifiers into the array element type and return a new array type.
1772 // Get the canonical version of the element with the extra qualifiers on it.
1773 // This can recursively sink qualifiers through multiple levels of arrays.
1774 QualType NewEltTy = ATy->getElementType();
1775 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001776 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001777 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1778
1779 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1780 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1781 CAT->getSizeModifier(),
1782 CAT->getIndexTypeQualifier()));
1783 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1784 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1785 IAT->getSizeModifier(),
1786 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001787
Douglas Gregor898574e2008-12-05 23:32:09 +00001788 if (const DependentSizedArrayType *DSAT
1789 = dyn_cast<DependentSizedArrayType>(ATy))
1790 return cast<ArrayType>(
1791 getDependentSizedArrayType(NewEltTy,
1792 DSAT->getSizeExpr(),
1793 DSAT->getSizeModifier(),
1794 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001795
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001796 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1797 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1798 VAT->getSizeModifier(),
1799 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001800}
1801
1802
Chris Lattnere6327742008-04-02 05:18:44 +00001803/// getArrayDecayedType - Return the properly qualified result of decaying the
1804/// specified array type to a pointer. This operation is non-trivial when
1805/// handling typedefs etc. The canonical type of "T" must be an array type,
1806/// this returns a pointer to a properly qualified element of the array.
1807///
1808/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1809QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001810 // Get the element type with 'getAsArrayType' so that we don't lose any
1811 // typedefs in the element type of the array. This also handles propagation
1812 // of type qualifiers from the array type into the element type if present
1813 // (C99 6.7.3p8).
1814 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1815 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001816
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001817 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001818
1819 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001820 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001821}
1822
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001823QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001824 QualType ElemTy = VAT->getElementType();
1825
1826 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1827 return getBaseElementType(VAT);
1828
1829 return ElemTy;
1830}
1831
Reid Spencer5f016e22007-07-11 17:01:13 +00001832/// getFloatingRank - Return a relative rank for floating point types.
1833/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001834static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001835 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001836 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001837
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001838 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001839 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001840 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001841 case BuiltinType::Float: return FloatRank;
1842 case BuiltinType::Double: return DoubleRank;
1843 case BuiltinType::LongDouble: return LongDoubleRank;
1844 }
1845}
1846
Steve Naroff716c7302007-08-27 01:41:48 +00001847/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1848/// point or a complex type (based on typeDomain/typeSize).
1849/// 'typeDomain' is a real floating point or complex type.
1850/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001851QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1852 QualType Domain) const {
1853 FloatingRank EltRank = getFloatingRank(Size);
1854 if (Domain->isComplexType()) {
1855 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001856 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001857 case FloatRank: return FloatComplexTy;
1858 case DoubleRank: return DoubleComplexTy;
1859 case LongDoubleRank: return LongDoubleComplexTy;
1860 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001861 }
Chris Lattner1361b112008-04-06 23:58:54 +00001862
1863 assert(Domain->isRealFloatingType() && "Unknown domain!");
1864 switch (EltRank) {
1865 default: assert(0 && "getFloatingRank(): illegal value for rank");
1866 case FloatRank: return FloatTy;
1867 case DoubleRank: return DoubleTy;
1868 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001869 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001870}
1871
Chris Lattner7cfeb082008-04-06 23:55:33 +00001872/// getFloatingTypeOrder - Compare the rank of the two specified floating
1873/// point types, ignoring the domain of the type (i.e. 'double' ==
1874/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1875/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001876int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1877 FloatingRank LHSR = getFloatingRank(LHS);
1878 FloatingRank RHSR = getFloatingRank(RHS);
1879
1880 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001881 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001882 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001883 return 1;
1884 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001885}
1886
Chris Lattnerf52ab252008-04-06 22:59:24 +00001887/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1888/// routine will assert if passed a built-in type that isn't an integer or enum,
1889/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001890unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001891 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001892 if (EnumType* ET = dyn_cast<EnumType>(T))
1893 T = ET->getDecl()->getIntegerType().getTypePtr();
1894
1895 // There are two things which impact the integer rank: the width, and
1896 // the ordering of builtins. The builtin ordering is encoded in the
1897 // bottom three bits; the width is encoded in the bits above that.
1898 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1899 return FWIT->getWidth() << 3;
1900 }
1901
Chris Lattnerf52ab252008-04-06 22:59:24 +00001902 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001903 default: assert(0 && "getIntegerRank(): not a built-in integer");
1904 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001905 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001906 case BuiltinType::Char_S:
1907 case BuiltinType::Char_U:
1908 case BuiltinType::SChar:
1909 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001910 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001911 case BuiltinType::Short:
1912 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001913 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001914 case BuiltinType::Int:
1915 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001916 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001917 case BuiltinType::Long:
1918 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001919 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001920 case BuiltinType::LongLong:
1921 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001922 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001923 }
1924}
1925
Chris Lattner7cfeb082008-04-06 23:55:33 +00001926/// getIntegerTypeOrder - Returns the highest ranked integer type:
1927/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1928/// LHS < RHS, return -1.
1929int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001930 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1931 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001932 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001933
Chris Lattnerf52ab252008-04-06 22:59:24 +00001934 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1935 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001936
Chris Lattner7cfeb082008-04-06 23:55:33 +00001937 unsigned LHSRank = getIntegerRank(LHSC);
1938 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001939
Chris Lattner7cfeb082008-04-06 23:55:33 +00001940 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1941 if (LHSRank == RHSRank) return 0;
1942 return LHSRank > RHSRank ? 1 : -1;
1943 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001944
Chris Lattner7cfeb082008-04-06 23:55:33 +00001945 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1946 if (LHSUnsigned) {
1947 // If the unsigned [LHS] type is larger, return it.
1948 if (LHSRank >= RHSRank)
1949 return 1;
1950
1951 // If the signed type can represent all values of the unsigned type, it
1952 // wins. Because we are dealing with 2's complement and types that are
1953 // powers of two larger than each other, this is always safe.
1954 return -1;
1955 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001956
Chris Lattner7cfeb082008-04-06 23:55:33 +00001957 // If the unsigned [RHS] type is larger, return it.
1958 if (RHSRank >= LHSRank)
1959 return -1;
1960
1961 // If the signed type can represent all values of the unsigned type, it
1962 // wins. Because we are dealing with 2's complement and types that are
1963 // powers of two larger than each other, this is always safe.
1964 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001965}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001966
1967// getCFConstantStringType - Return the type used for constant CFStrings.
1968QualType ASTContext::getCFConstantStringType() {
1969 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001970 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001971 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001972 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001973 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001974
1975 // const int *isa;
1976 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001977 // int flags;
1978 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001979 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001980 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001981 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001982 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001983
Anders Carlsson71993dd2007-08-17 05:31:46 +00001984 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001985 for (unsigned i = 0; i < 4; ++i) {
1986 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1987 SourceLocation(), 0,
1988 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001989 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00001990 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001991 }
1992
1993 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001994 }
1995
1996 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001997}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001998
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001999QualType ASTContext::getObjCFastEnumerationStateType()
2000{
2001 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00002002 ObjCFastEnumerationStateTypeDecl =
2003 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2004 &Idents.get("__objcFastEnumerationState"));
2005
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002006 QualType FieldTypes[] = {
2007 UnsignedLongTy,
2008 getPointerType(ObjCIdType),
2009 getPointerType(UnsignedLongTy),
2010 getConstantArrayType(UnsignedLongTy,
2011 llvm::APInt(32, 5), ArrayType::Normal, 0)
2012 };
2013
Douglas Gregor44b43212008-12-11 16:49:14 +00002014 for (size_t i = 0; i < 4; ++i) {
2015 FieldDecl *Field = FieldDecl::Create(*this,
2016 ObjCFastEnumerationStateTypeDecl,
2017 SourceLocation(), 0,
2018 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00002019 /*Mutable=*/false);
Douglas Gregor6ab35242009-04-09 21:40:53 +00002020 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00002021 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002022
Douglas Gregor44b43212008-12-11 16:49:14 +00002023 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00002024 }
2025
2026 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2027}
2028
Anders Carlssone8c49532007-10-29 06:33:42 +00002029// This returns true if a type has been typedefed to BOOL:
2030// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00002031static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002032 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00002033 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2034 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002035
2036 return false;
2037}
2038
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002039/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002040/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002041int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00002042 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002043
2044 // Make all integer and enum types at least as large as an int
2045 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00002046 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002047 // Treat arrays as pointers, since that's how they're passed in.
2048 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00002049 sz = getTypeSize(VoidPtrTy);
2050 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002051}
2052
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002053/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002054/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002055void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002056 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002057 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002058 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002059 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002060 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002061 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002062 // Compute size of all parameters.
2063 // Start with computing size of a pointer in number of bytes.
2064 // FIXME: There might(should) be a better way of doing this computation!
2065 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00002066 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002067 // The first two arguments (self and _cmd) are pointers; account for
2068 // their size.
2069 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002070 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2071 E = Decl->param_end(); PI != E; ++PI) {
2072 QualType PType = (*PI)->getType();
2073 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002074 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002075 ParmOffset += sz;
2076 }
2077 S += llvm::utostr(ParmOffset);
2078 S += "@0:";
2079 S += llvm::utostr(PtrSize);
2080
2081 // Argument types.
2082 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00002083 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2084 E = Decl->param_end(); PI != E; ++PI) {
2085 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002086 QualType PType = PVDecl->getOriginalType();
2087 if (const ArrayType *AT =
Steve Naroffab76d452009-04-14 00:03:58 +00002088 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2089 // Use array's original type only if it has known number of
2090 // elements.
Steve Naroffbb3fde32009-04-14 00:40:09 +00002091 if (!isa<ConstantArrayType>(AT))
Steve Naroffab76d452009-04-14 00:03:58 +00002092 PType = PVDecl->getType();
2093 } else if (PType->isFunctionType())
2094 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002095 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002096 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00002097 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002098 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002099 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002100 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00002101 }
2102}
2103
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002104/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002105/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002106/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2107/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00002108/// Property attributes are stored as a comma-delimited C string. The simple
2109/// attributes readonly and bycopy are encoded as single characters. The
2110/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2111/// encoded as single characters, followed by an identifier. Property types
2112/// are also encoded as a parametrized attribute. The characters used to encode
2113/// these attributes are defined by the following enumeration:
2114/// @code
2115/// enum PropertyAttributes {
2116/// kPropertyReadOnly = 'R', // property is read-only.
2117/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2118/// kPropertyByref = '&', // property is a reference to the value last assigned
2119/// kPropertyDynamic = 'D', // property is dynamic
2120/// kPropertyGetter = 'G', // followed by getter selector name
2121/// kPropertySetter = 'S', // followed by setter selector name
2122/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2123/// kPropertyType = 't' // followed by old-style type encoding.
2124/// kPropertyWeak = 'W' // 'weak' property
2125/// kPropertyStrong = 'P' // property GC'able
2126/// kPropertyNonAtomic = 'N' // property non-atomic
2127/// };
2128/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002129void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2130 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00002131 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002132 // Collect information from the property implementation decl(s).
2133 bool Dynamic = false;
2134 ObjCPropertyImplDecl *SynthesizePID = 0;
2135
2136 // FIXME: Duplicated code due to poor abstraction.
2137 if (Container) {
2138 if (const ObjCCategoryImplDecl *CID =
2139 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2140 for (ObjCCategoryImplDecl::propimpl_iterator
2141 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2142 ObjCPropertyImplDecl *PID = *i;
2143 if (PID->getPropertyDecl() == PD) {
2144 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2145 Dynamic = true;
2146 } else {
2147 SynthesizePID = PID;
2148 }
2149 }
2150 }
2151 } else {
Chris Lattner61710852008-10-05 17:34:18 +00002152 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002153 for (ObjCCategoryImplDecl::propimpl_iterator
2154 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2155 ObjCPropertyImplDecl *PID = *i;
2156 if (PID->getPropertyDecl() == PD) {
2157 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2158 Dynamic = true;
2159 } else {
2160 SynthesizePID = PID;
2161 }
2162 }
2163 }
2164 }
2165 }
2166
2167 // FIXME: This is not very efficient.
2168 S = "T";
2169
2170 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002171 // GCC has some special rules regarding encoding of properties which
2172 // closely resembles encoding of ivars.
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002173 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002174 true /* outermost type */,
2175 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002176
2177 if (PD->isReadOnly()) {
2178 S += ",R";
2179 } else {
2180 switch (PD->getSetterKind()) {
2181 case ObjCPropertyDecl::Assign: break;
2182 case ObjCPropertyDecl::Copy: S += ",C"; break;
2183 case ObjCPropertyDecl::Retain: S += ",&"; break;
2184 }
2185 }
2186
2187 // It really isn't clear at all what this means, since properties
2188 // are "dynamic by default".
2189 if (Dynamic)
2190 S += ",D";
2191
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002192 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2193 S += ",N";
2194
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002195 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2196 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002197 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002198 }
2199
2200 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2201 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002202 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002203 }
2204
2205 if (SynthesizePID) {
2206 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2207 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002208 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002209 }
2210
2211 // FIXME: OBJCGC: weak & strong
2212}
2213
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002214/// getLegacyIntegralTypeEncoding -
2215/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002216/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002217/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2218///
2219void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2220 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2221 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002222 if (BT->getKind() == BuiltinType::ULong &&
2223 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002224 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002225 else
2226 if (BT->getKind() == BuiltinType::Long &&
2227 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002228 PointeeTy = IntTy;
2229 }
2230 }
2231}
2232
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002233void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002234 const FieldDecl *Field) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002235 // We follow the behavior of gcc, expanding structures which are
2236 // directly pointed to, and expanding embedded structures. Note that
2237 // these rules are sufficient to prevent recursive encoding of the
2238 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002239 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2240 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002241}
2242
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002243static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002244 const FieldDecl *FD) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002245 const Expr *E = FD->getBitWidth();
2246 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2247 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2248 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2249 S += 'b';
2250 S += llvm::utostr(N);
2251}
2252
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002253void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2254 bool ExpandPointedToStructures,
2255 bool ExpandStructures,
Daniel Dunbar153bfe52009-04-20 06:37:24 +00002256 const FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002257 bool OutermostType,
Douglas Gregor6ab35242009-04-09 21:40:53 +00002258 bool EncodingProperty) {
Anders Carlssone8c49532007-10-29 06:33:42 +00002259 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002260 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002261 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002262 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002263 else {
2264 char encoding;
2265 switch (BT->getKind()) {
2266 default: assert(0 && "Unhandled builtin type kind");
2267 case BuiltinType::Void: encoding = 'v'; break;
2268 case BuiltinType::Bool: encoding = 'B'; break;
2269 case BuiltinType::Char_U:
2270 case BuiltinType::UChar: encoding = 'C'; break;
2271 case BuiltinType::UShort: encoding = 'S'; break;
2272 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002273 case BuiltinType::ULong:
2274 encoding =
2275 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2276 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002277 case BuiltinType::ULongLong: encoding = 'Q'; break;
2278 case BuiltinType::Char_S:
2279 case BuiltinType::SChar: encoding = 'c'; break;
2280 case BuiltinType::Short: encoding = 's'; break;
2281 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002282 case BuiltinType::Long:
2283 encoding =
2284 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2285 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002286 case BuiltinType::LongLong: encoding = 'q'; break;
2287 case BuiltinType::Float: encoding = 'f'; break;
2288 case BuiltinType::Double: encoding = 'd'; break;
2289 case BuiltinType::LongDouble: encoding = 'd'; break;
2290 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002291
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002292 S += encoding;
2293 }
Anders Carlssonc612f7b2009-04-09 21:55:45 +00002294 } else if (const ComplexType *CT = T->getAsComplexType()) {
2295 S += 'j';
2296 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2297 false);
2298 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002299 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2300 ExpandPointedToStructures,
2301 ExpandStructures, FD);
2302 if (FD || EncodingProperty) {
2303 // Note that we do extended encoding of protocol qualifer list
2304 // Only when doing ivar or property encoding.
2305 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2306 S += '"';
2307 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2308 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2309 S += '<';
2310 S += Proto->getNameAsString();
2311 S += '>';
2312 }
2313 S += '"';
2314 }
2315 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002316 }
2317 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002318 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002319 bool isReadOnly = false;
2320 // For historical/compatibility reasons, the read-only qualifier of the
2321 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2322 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2323 // Also, do not emit the 'r' for anything but the outermost type!
2324 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2325 if (OutermostType && T.isConstQualified()) {
2326 isReadOnly = true;
2327 S += 'r';
2328 }
2329 }
2330 else if (OutermostType) {
2331 QualType P = PointeeTy;
2332 while (P->getAsPointerType())
2333 P = P->getAsPointerType()->getPointeeType();
2334 if (P.isConstQualified()) {
2335 isReadOnly = true;
2336 S += 'r';
2337 }
2338 }
2339 if (isReadOnly) {
2340 // Another legacy compatibility encoding. Some ObjC qualifier and type
2341 // combinations need to be rearranged.
2342 // Rewrite "in const" from "nr" to "rn"
2343 const char * s = S.c_str();
2344 int len = S.length();
2345 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2346 std::string replace = "rn";
2347 S.replace(S.end()-2, S.end(), replace);
2348 }
2349 }
Steve Naroff389bf462009-02-12 17:52:19 +00002350 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002351 S += '@';
2352 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002353 }
2354 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002355 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002356 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002357 // Another historical/compatibility reason.
2358 // We encode the underlying type which comes out as
2359 // {...};
2360 S += '^';
2361 getObjCEncodingForTypeImpl(PointeeTy, S,
2362 false, ExpandPointedToStructures,
2363 NULL);
2364 return;
2365 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002366 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002367 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002368 const ObjCInterfaceType *OIT =
2369 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002370 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002371 S += '"';
2372 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002373 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2374 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2375 S += '<';
2376 S += Proto->getNameAsString();
2377 S += '>';
2378 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002379 S += '"';
2380 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002381 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002382 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002383 S += '#';
2384 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002385 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002386 S += ':';
2387 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002388 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002389
2390 if (PointeeTy->isCharType()) {
2391 // char pointer types should be encoded as '*' unless it is a
2392 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002393 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002394 S += '*';
2395 return;
2396 }
2397 }
2398
2399 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002400 getLegacyIntegralTypeEncoding(PointeeTy);
2401
2402 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002403 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002404 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002405 } else if (const ArrayType *AT =
2406 // Ignore type qualifiers etc.
2407 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002408 if (isa<IncompleteArrayType>(AT)) {
2409 // Incomplete arrays are encoded as a pointer to the array element.
2410 S += '^';
2411
2412 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2413 false, ExpandStructures, FD);
2414 } else {
2415 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002416
Anders Carlsson559a8332009-02-22 01:38:57 +00002417 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2418 S += llvm::utostr(CAT->getSize().getZExtValue());
2419 else {
2420 //Variable length arrays are encoded as a regular array with 0 elements.
2421 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2422 S += '0';
2423 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002424
Anders Carlsson559a8332009-02-22 01:38:57 +00002425 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2426 false, ExpandStructures, FD);
2427 S += ']';
2428 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002429 } else if (T->getAsFunctionType()) {
2430 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002431 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002432 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002433 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002434 // Anonymous structures print as '?'
2435 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2436 S += II->getName();
2437 } else {
2438 S += '?';
2439 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002440 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002441 S += '=';
Douglas Gregor6ab35242009-04-09 21:40:53 +00002442 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2443 FieldEnd = RDecl->field_end(*this);
Douglas Gregor44b43212008-12-11 16:49:14 +00002444 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002445 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002446 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002447 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002448 S += '"';
2449 }
2450
2451 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002452 if (Field->isBitField()) {
2453 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2454 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002455 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002456 QualType qt = Field->getType();
2457 getLegacyIntegralTypeEncoding(qt);
2458 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002459 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002460 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002461 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002462 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002463 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002464 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002465 if (FD && FD->isBitField())
2466 EncodeBitField(this, S, FD);
2467 else
2468 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002469 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002470 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002471 } else if (T->isObjCInterfaceType()) {
2472 // @encode(class_name)
2473 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2474 S += '{';
2475 const IdentifierInfo *II = OI->getIdentifier();
2476 S += II->getName();
2477 S += '=';
Chris Lattnerf1690852009-03-31 08:48:01 +00002478 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002479 CollectObjCIvars(OI, RecFields);
Chris Lattnerf1690852009-03-31 08:48:01 +00002480 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002481 if (RecFields[i]->isBitField())
2482 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2483 RecFields[i]);
2484 else
2485 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2486 FD);
2487 }
2488 S += '}';
2489 }
2490 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002491 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002492}
2493
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002494void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002495 std::string& S) const {
2496 if (QT & Decl::OBJC_TQ_In)
2497 S += 'n';
2498 if (QT & Decl::OBJC_TQ_Inout)
2499 S += 'N';
2500 if (QT & Decl::OBJC_TQ_Out)
2501 S += 'o';
2502 if (QT & Decl::OBJC_TQ_Bycopy)
2503 S += 'O';
2504 if (QT & Decl::OBJC_TQ_Byref)
2505 S += 'R';
2506 if (QT & Decl::OBJC_TQ_Oneway)
2507 S += 'V';
2508}
2509
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002510void ASTContext::setBuiltinVaListType(QualType T)
2511{
2512 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2513
2514 BuiltinVaListType = T;
2515}
2516
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002517void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002518{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002519 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002520
2521 // typedef struct objc_object *id;
2522 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002523 // User error - caller will issue diagnostics.
2524 if (!ptr)
2525 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002526 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002527 // User error - caller will issue diagnostics.
2528 if (!rec)
2529 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002530 IdStructType = rec;
2531}
2532
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002533void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002534{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002535 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002536
2537 // typedef struct objc_selector *SEL;
2538 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002539 if (!ptr)
2540 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002541 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002542 if (!rec)
2543 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002544 SelStructType = rec;
2545}
2546
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002547void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002548{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002549 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002550}
2551
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002552void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002553{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002554 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002555
2556 // typedef struct objc_class *Class;
2557 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2558 assert(ptr && "'Class' incorrectly typed");
2559 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2560 assert(rec && "'Class' incorrectly typed");
2561 ClassStructType = rec;
2562}
2563
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002564void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2565 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002566 "'NSConstantString' type already set!");
2567
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002568 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002569}
2570
Douglas Gregor7532dc62009-03-30 22:58:21 +00002571/// \brief Retrieve the template name that represents a qualified
2572/// template name such as \c std::vector.
2573TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2574 bool TemplateKeyword,
2575 TemplateDecl *Template) {
2576 llvm::FoldingSetNodeID ID;
2577 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2578
2579 void *InsertPos = 0;
2580 QualifiedTemplateName *QTN =
2581 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2582 if (!QTN) {
2583 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2584 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2585 }
2586
2587 return TemplateName(QTN);
2588}
2589
2590/// \brief Retrieve the template name that represents a dependent
2591/// template name such as \c MetaFun::template apply.
2592TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2593 const IdentifierInfo *Name) {
2594 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2595
2596 llvm::FoldingSetNodeID ID;
2597 DependentTemplateName::Profile(ID, NNS, Name);
2598
2599 void *InsertPos = 0;
2600 DependentTemplateName *QTN =
2601 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2602
2603 if (QTN)
2604 return TemplateName(QTN);
2605
2606 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2607 if (CanonNNS == NNS) {
2608 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2609 } else {
2610 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2611 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2612 }
2613
2614 DependentTemplateNames.InsertNode(QTN, InsertPos);
2615 return TemplateName(QTN);
2616}
2617
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002618/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002619/// TargetInfo, produce the corresponding type. The unsigned @p Type
2620/// is actually a value of type @c TargetInfo::IntType.
2621QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002622 switch (Type) {
2623 case TargetInfo::NoInt: return QualType();
2624 case TargetInfo::SignedShort: return ShortTy;
2625 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2626 case TargetInfo::SignedInt: return IntTy;
2627 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2628 case TargetInfo::SignedLong: return LongTy;
2629 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2630 case TargetInfo::SignedLongLong: return LongLongTy;
2631 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2632 }
2633
2634 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002635 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002636}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002637
2638//===----------------------------------------------------------------------===//
2639// Type Predicates.
2640//===----------------------------------------------------------------------===//
2641
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002642/// isObjCNSObjectType - Return true if this is an NSObject object using
2643/// NSObject attribute on a c-style pointer type.
2644/// FIXME - Make it work directly on types.
2645///
2646bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2647 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2648 if (TypedefDecl *TD = TDT->getDecl())
2649 if (TD->getAttr<ObjCNSObjectAttr>())
2650 return true;
2651 }
2652 return false;
2653}
2654
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002655/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2656/// to an object type. This includes "id" and "Class" (two 'special' pointers
2657/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2658/// ID type).
2659bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002660 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002661 return true;
2662
Steve Naroff6ae98502008-10-21 18:24:04 +00002663 // Blocks are objects.
2664 if (Ty->isBlockPointerType())
2665 return true;
2666
2667 // All other object types are pointers.
Chris Lattner16ede0e2009-04-12 23:51:02 +00002668 const PointerType *PT = Ty->getAsPointerType();
2669 if (PT == 0)
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002670 return false;
2671
Chris Lattner16ede0e2009-04-12 23:51:02 +00002672 // If this a pointer to an interface (e.g. NSString*), it is ok.
2673 if (PT->getPointeeType()->isObjCInterfaceType() ||
2674 // If is has NSObject attribute, OK as well.
2675 isObjCNSObjectType(Ty))
2676 return true;
2677
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002678 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2679 // pointer types. This looks for the typedef specifically, not for the
Chris Lattner16ede0e2009-04-12 23:51:02 +00002680 // underlying type. Iteratively strip off typedefs so that we can handle
2681 // typedefs of typedefs.
2682 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2683 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2684 Ty.getUnqualifiedType() == getObjCClassType())
2685 return true;
2686
2687 Ty = TDT->getDecl()->getUnderlyingType();
2688 }
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002689
Chris Lattner16ede0e2009-04-12 23:51:02 +00002690 return false;
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002691}
2692
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002693/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2694/// garbage collection attribute.
2695///
2696QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002697 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002698 if (getLangOptions().ObjC1 &&
2699 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002700 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002701 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002702 // (or pointers to them) be treated as though they were declared
2703 // as __strong.
2704 if (GCAttrs == QualType::GCNone) {
2705 if (isObjCObjectPointerType(Ty))
2706 GCAttrs = QualType::Strong;
2707 else if (Ty->isPointerType())
2708 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2709 }
Fariborz Jahanianc2112182009-04-11 00:00:54 +00002710 // Non-pointers have none gc'able attribute regardless of the attribute
2711 // set on them.
2712 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2713 return QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002714 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002715 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002716}
2717
Chris Lattner6ac46a42008-04-07 06:51:04 +00002718//===----------------------------------------------------------------------===//
2719// Type Compatibility Testing
2720//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002721
Steve Naroff1c7d0672008-09-04 15:10:53 +00002722/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002723/// block types. Types must be strictly compatible here. For example,
2724/// C unfortunately doesn't produce an error for the following:
2725///
2726/// int (*emptyArgFunc)();
2727/// int (*intArgList)(int) = emptyArgFunc;
2728///
2729/// For blocks, we will produce an error for the following (similar to C++):
2730///
2731/// int (^emptyArgBlock)();
2732/// int (^intArgBlock)(int) = emptyArgBlock;
2733///
2734/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2735///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002736bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002737 const FunctionType *lbase = lhs->getAsFunctionType();
2738 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002739 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2740 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stumpaab0f7a2009-04-01 01:17:39 +00002741 if (lproto && rproto == 0)
2742 return false;
2743 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff1c7d0672008-09-04 15:10:53 +00002744}
2745
Chris Lattner6ac46a42008-04-07 06:51:04 +00002746/// areCompatVectorTypes - Return true if the two specified vector types are
2747/// compatible.
2748static bool areCompatVectorTypes(const VectorType *LHS,
2749 const VectorType *RHS) {
2750 assert(LHS->isCanonical() && RHS->isCanonical());
2751 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002752 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002753}
2754
Eli Friedman3d815e72008-08-22 00:56:42 +00002755/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002756/// compatible for assignment from RHS to LHS. This handles validation of any
2757/// protocol qualifiers on the LHS or RHS.
2758///
Eli Friedman3d815e72008-08-22 00:56:42 +00002759bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2760 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002761 // Verify that the base decls are compatible: the RHS must be a subclass of
2762 // the LHS.
2763 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2764 return false;
2765
2766 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2767 // protocol qualified at all, then we are good.
2768 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2769 return true;
2770
2771 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2772 // isn't a superset.
2773 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2774 return true; // FIXME: should return false!
2775
2776 // Finally, we must have two protocol-qualified interfaces.
2777 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2778 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ac46a42008-04-07 06:51:04 +00002779
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002780 // All LHS protocols must have a presence on the RHS.
2781 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ac46a42008-04-07 06:51:04 +00002782
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002783 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2784 LHSPE = LHSP->qual_end();
2785 LHSPI != LHSPE; LHSPI++) {
2786 bool RHSImplementsProtocol = false;
2787
2788 // If the RHS doesn't implement the protocol on the left, the types
2789 // are incompatible.
2790 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2791 RHSPE = RHSP->qual_end();
2792 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2793 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2794 RHSImplementsProtocol = true;
2795 }
2796 // FIXME: For better diagnostics, consider passing back the protocol name.
2797 if (!RHSImplementsProtocol)
2798 return false;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002799 }
Steve Naroff91b0b0c2009-03-01 16:12:44 +00002800 // The RHS implements all protocols listed on the LHS.
2801 return true;
Chris Lattner6ac46a42008-04-07 06:51:04 +00002802}
2803
Steve Naroff389bf462009-02-12 17:52:19 +00002804bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2805 // get the "pointed to" types
2806 const PointerType *LHSPT = LHS->getAsPointerType();
2807 const PointerType *RHSPT = RHS->getAsPointerType();
2808
2809 if (!LHSPT || !RHSPT)
2810 return false;
2811
2812 QualType lhptee = LHSPT->getPointeeType();
2813 QualType rhptee = RHSPT->getPointeeType();
2814 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2815 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2816 // ID acts sort of like void* for ObjC interfaces
2817 if (LHSIface && isObjCIdStructType(rhptee))
2818 return true;
2819 if (RHSIface && isObjCIdStructType(lhptee))
2820 return true;
2821 if (!LHSIface || !RHSIface)
2822 return false;
2823 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2824 canAssignObjCInterfaces(RHSIface, LHSIface);
2825}
2826
Steve Naroffec0550f2007-10-15 20:41:53 +00002827/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2828/// both shall have the identically qualified version of a compatible type.
2829/// C99 6.2.7p1: Two types have compatible types if their types are the
2830/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002831bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2832 return !mergeTypes(LHS, RHS).isNull();
2833}
2834
2835QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2836 const FunctionType *lbase = lhs->getAsFunctionType();
2837 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor72564e72009-02-26 23:50:07 +00002838 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2839 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman3d815e72008-08-22 00:56:42 +00002840 bool allLTypes = true;
2841 bool allRTypes = true;
2842
2843 // Check return type
2844 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2845 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002846 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2847 allLTypes = false;
2848 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2849 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002850
2851 if (lproto && rproto) { // two C99 style function prototypes
2852 unsigned lproto_nargs = lproto->getNumArgs();
2853 unsigned rproto_nargs = rproto->getNumArgs();
2854
2855 // Compatible functions must have the same number of arguments
2856 if (lproto_nargs != rproto_nargs)
2857 return QualType();
2858
2859 // Variadic and non-variadic functions aren't compatible
2860 if (lproto->isVariadic() != rproto->isVariadic())
2861 return QualType();
2862
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002863 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2864 return QualType();
2865
Eli Friedman3d815e72008-08-22 00:56:42 +00002866 // Check argument compatibility
2867 llvm::SmallVector<QualType, 10> types;
2868 for (unsigned i = 0; i < lproto_nargs; i++) {
2869 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2870 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2871 QualType argtype = mergeTypes(largtype, rargtype);
2872 if (argtype.isNull()) return QualType();
2873 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002874 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2875 allLTypes = false;
2876 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2877 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002878 }
2879 if (allLTypes) return lhs;
2880 if (allRTypes) return rhs;
2881 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002882 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002883 }
2884
2885 if (lproto) allRTypes = false;
2886 if (rproto) allLTypes = false;
2887
Douglas Gregor72564e72009-02-26 23:50:07 +00002888 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman3d815e72008-08-22 00:56:42 +00002889 if (proto) {
2890 if (proto->isVariadic()) return QualType();
2891 // Check that the types are compatible with the types that
2892 // would result from default argument promotions (C99 6.7.5.3p15).
2893 // The only types actually affected are promotable integer
2894 // types and floats, which would be passed as a different
2895 // type depending on whether the prototype is visible.
2896 unsigned proto_nargs = proto->getNumArgs();
2897 for (unsigned i = 0; i < proto_nargs; ++i) {
2898 QualType argTy = proto->getArgType(i);
2899 if (argTy->isPromotableIntegerType() ||
2900 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2901 return QualType();
2902 }
2903
2904 if (allLTypes) return lhs;
2905 if (allRTypes) return rhs;
2906 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002907 proto->getNumArgs(), lproto->isVariadic(),
2908 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002909 }
2910
2911 if (allLTypes) return lhs;
2912 if (allRTypes) return rhs;
Douglas Gregor72564e72009-02-26 23:50:07 +00002913 return getFunctionNoProtoType(retType);
Eli Friedman3d815e72008-08-22 00:56:42 +00002914}
2915
2916QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002917 // C++ [expr]: If an expression initially has the type "reference to T", the
2918 // type is adjusted to "T" prior to any further analysis, the expression
2919 // designates the object or function denoted by the reference, and the
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002920 // expression is an lvalue unless the reference is an rvalue reference and
2921 // the expression is a function call (possibly inside parentheses).
Eli Friedman3d815e72008-08-22 00:56:42 +00002922 // FIXME: C++ shouldn't be going through here! The rules are different
2923 // enough that they should be handled separately.
Sebastian Redl7c80bd62009-03-16 23:22:08 +00002924 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2925 // shouldn't be going through here!
Eli Friedman3d815e72008-08-22 00:56:42 +00002926 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002927 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002928 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002929 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002930
Eli Friedman3d815e72008-08-22 00:56:42 +00002931 QualType LHSCan = getCanonicalType(LHS),
2932 RHSCan = getCanonicalType(RHS);
2933
2934 // If two types are identical, they are compatible.
2935 if (LHSCan == RHSCan)
2936 return LHS;
2937
2938 // If the qualifiers are different, the types aren't compatible
Eli Friedman5a61f0e2009-02-27 23:04:43 +00002939 // Note that we handle extended qualifiers later, in the
2940 // case for ExtQualType.
2941 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman3d815e72008-08-22 00:56:42 +00002942 return QualType();
2943
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00002944 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
2945 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman3d815e72008-08-22 00:56:42 +00002946
Chris Lattner1adb8832008-01-14 05:45:46 +00002947 // We want to consider the two function types to be the same for these
2948 // comparisons, just force one to the other.
2949 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2950 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002951
2952 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002953 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2954 LHSClass = Type::ConstantArray;
2955 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2956 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002957
Nate Begeman213541a2008-04-18 23:10:10 +00002958 // Canonicalize ExtVector -> Vector.
2959 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2960 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002961
Chris Lattnerb0489812008-04-07 06:38:24 +00002962 // Consider qualified interfaces and interfaces the same.
2963 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2964 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002965
Chris Lattnera36a61f2008-04-07 05:43:21 +00002966 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002967 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002968 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2969 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanianc8d2e772009-04-15 21:54:48 +00002970
Steve Naroffd824c9c2009-04-14 15:11:46 +00002971 // 'id' and 'Class' act sort of like void* for ObjC interfaces
2972 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00002973 return LHS;
Steve Naroffd824c9c2009-04-14 15:11:46 +00002974 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff5fd659d2009-02-21 16:18:07 +00002975 return RHS;
2976
Steve Naroffbc76dd02008-12-10 22:14:21 +00002977 // ID is compatible with all qualified id types.
2978 if (LHS->isObjCQualifiedIdType()) {
2979 if (const PointerType *PT = RHS->getAsPointerType()) {
2980 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00002981 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002982 return LHS;
2983 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2984 // Unfortunately, this API is part of Sema (which we don't have access
2985 // to. Need to refactor. The following check is insufficient, since we
2986 // need to make sure the class implements the protocol.
2987 if (pType->isObjCInterfaceType())
2988 return LHS;
2989 }
2990 }
2991 if (RHS->isObjCQualifiedIdType()) {
2992 if (const PointerType *PT = LHS->getAsPointerType()) {
2993 QualType pType = PT->getPointeeType();
Steve Naroffd824c9c2009-04-14 15:11:46 +00002994 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002995 return RHS;
2996 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2997 // Unfortunately, this API is part of Sema (which we don't have access
2998 // to. Need to refactor. The following check is insufficient, since we
2999 // need to make sure the class implements the protocol.
3000 if (pType->isObjCInterfaceType())
3001 return RHS;
3002 }
3003 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003004 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3005 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00003006 if (const EnumType* ETy = LHS->getAsEnumType()) {
3007 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3008 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003009 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003010 if (const EnumType* ETy = RHS->getAsEnumType()) {
3011 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3012 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00003013 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003014
Eli Friedman3d815e72008-08-22 00:56:42 +00003015 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003016 }
Eli Friedman3d815e72008-08-22 00:56:42 +00003017
Steve Naroff4a746782008-01-09 22:43:08 +00003018 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00003019 switch (LHSClass) {
Douglas Gregor72564e72009-02-26 23:50:07 +00003020#define TYPE(Class, Base)
3021#define ABSTRACT_TYPE(Class, Base)
3022#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3023#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3024#include "clang/AST/TypeNodes.def"
3025 assert(false && "Non-canonical and dependent types shouldn't get here");
3026 return QualType();
3027
Sebastian Redl7c80bd62009-03-16 23:22:08 +00003028 case Type::LValueReference:
3029 case Type::RValueReference:
Douglas Gregor72564e72009-02-26 23:50:07 +00003030 case Type::MemberPointer:
3031 assert(false && "C++ should never be in mergeTypes");
3032 return QualType();
3033
3034 case Type::IncompleteArray:
3035 case Type::VariableArray:
3036 case Type::FunctionProto:
3037 case Type::ExtVector:
3038 case Type::ObjCQualifiedInterface:
3039 assert(false && "Types are eliminated above");
3040 return QualType();
3041
Chris Lattner1adb8832008-01-14 05:45:46 +00003042 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00003043 {
3044 // Merge two pointer types, while trying to preserve typedef info
3045 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3046 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3047 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3048 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003049 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3050 return LHS;
3051 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3052 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003053 return getPointerType(ResultType);
3054 }
Steve Naroffc0febd52008-12-10 17:49:55 +00003055 case Type::BlockPointer:
3056 {
3057 // Merge two block pointer types, while trying to preserve typedef info
3058 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3059 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3060 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3061 if (ResultType.isNull()) return QualType();
3062 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3063 return LHS;
3064 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3065 return RHS;
3066 return getBlockPointerType(ResultType);
3067 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003068 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00003069 {
3070 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3071 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3072 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3073 return QualType();
3074
3075 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3076 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3077 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3078 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00003079 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3080 return LHS;
3081 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3082 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00003083 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3084 ArrayType::ArraySizeModifier(), 0);
3085 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3086 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003087 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3088 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00003089 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3090 return LHS;
3091 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3092 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003093 if (LVAT) {
3094 // FIXME: This isn't correct! But tricky to implement because
3095 // the array's size has to be the size of LHS, but the type
3096 // has to be different.
3097 return LHS;
3098 }
3099 if (RVAT) {
3100 // FIXME: This isn't correct! But tricky to implement because
3101 // the array's size has to be the size of RHS, but the type
3102 // has to be different.
3103 return RHS;
3104 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00003105 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3106 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00003107 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00003108 }
Chris Lattner1adb8832008-01-14 05:45:46 +00003109 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00003110 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor72564e72009-02-26 23:50:07 +00003111 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00003112 case Type::Enum:
Eli Friedman3d815e72008-08-22 00:56:42 +00003113 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00003114 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3115 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00003116 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00003117 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003118 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00003119 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00003120 case Type::Complex:
3121 // Distinct complex types are incompatible.
3122 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00003123 case Type::Vector:
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003124 // FIXME: The merged type should be an ExtVector!
Eli Friedman3d815e72008-08-22 00:56:42 +00003125 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3126 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00003127 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003128 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00003129 // Check if the interfaces are assignment compatible.
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003130 // FIXME: This should be type compatibility, e.g. whether
3131 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff5fd659d2009-02-21 16:18:07 +00003132 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3133 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3134 if (LHSIface && RHSIface &&
3135 canAssignObjCInterfaces(LHSIface, RHSIface))
3136 return LHS;
3137
Eli Friedman3d815e72008-08-22 00:56:42 +00003138 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00003139 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00003140 case Type::ObjCQualifiedId:
3141 // Distinct qualified id's are not compatible.
3142 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003143 case Type::FixedWidthInt:
3144 // Distinct fixed-width integers are not compatible.
3145 return QualType();
Eli Friedman5a61f0e2009-02-27 23:04:43 +00003146 case Type::ExtQual:
3147 // FIXME: ExtQual types can be compatible even if they're not
3148 // identical!
3149 return QualType();
3150 // First attempt at an implementation, but I'm not really sure it's
3151 // right...
3152#if 0
3153 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3154 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3155 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3156 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3157 return QualType();
3158 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3159 LHSBase = QualType(LQual->getBaseType(), 0);
3160 RHSBase = QualType(RQual->getBaseType(), 0);
3161 ResultType = mergeTypes(LHSBase, RHSBase);
3162 if (ResultType.isNull()) return QualType();
3163 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3164 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3165 return LHS;
3166 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3167 return RHS;
3168 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3169 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3170 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3171 return ResultType;
3172#endif
Douglas Gregor7532dc62009-03-30 22:58:21 +00003173
3174 case Type::TemplateSpecialization:
3175 assert(false && "Dependent types have no size");
3176 break;
Steve Naroffec0550f2007-10-15 20:41:53 +00003177 }
Douglas Gregor72564e72009-02-26 23:50:07 +00003178
3179 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00003180}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003181
Chris Lattner5426bf62008-04-07 07:01:58 +00003182//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00003183// Integer Predicates
3184//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00003185
Eli Friedmanad74a752008-06-28 06:23:08 +00003186unsigned ASTContext::getIntWidth(QualType T) {
3187 if (T == BoolTy)
3188 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00003189 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3190 return FWIT->getWidth();
3191 }
3192 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00003193 return (unsigned)getTypeSize(T);
3194}
3195
3196QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3197 assert(T->isSignedIntegerType() && "Unexpected type");
3198 if (const EnumType* ETy = T->getAsEnumType())
3199 T = ETy->getDecl()->getIntegerType();
3200 const BuiltinType* BTy = T->getAsBuiltinType();
3201 assert (BTy && "Unexpected signed integer type");
3202 switch (BTy->getKind()) {
3203 case BuiltinType::Char_S:
3204 case BuiltinType::SChar:
3205 return UnsignedCharTy;
3206 case BuiltinType::Short:
3207 return UnsignedShortTy;
3208 case BuiltinType::Int:
3209 return UnsignedIntTy;
3210 case BuiltinType::Long:
3211 return UnsignedLongTy;
3212 case BuiltinType::LongLong:
3213 return UnsignedLongLongTy;
3214 default:
3215 assert(0 && "Unexpected signed integer type");
3216 return QualType();
3217 }
3218}
3219
3220
3221//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00003222// Serialization Support
3223//===----------------------------------------------------------------------===//
3224
Chris Lattnera9376d42009-03-28 03:45:20 +00003225enum {
3226 BasicMetadataBlock = 1,
3227 ASTContextBlock = 2,
3228 DeclsBlock = 3
3229};
3230
Chris Lattner557c5b12009-03-28 04:27:18 +00003231void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3232 // Create bitstream.
3233 llvm::BitstreamWriter Stream(Buffer);
3234
3235 // Emit the preamble.
3236 Stream.Emit((unsigned)'B', 8);
3237 Stream.Emit((unsigned)'C', 8);
3238 Stream.Emit(0xC, 4);
3239 Stream.Emit(0xF, 4);
3240 Stream.Emit(0xE, 4);
3241 Stream.Emit(0x0, 4);
3242
3243 // Create serializer.
3244 llvm::Serializer S(Stream);
3245
Chris Lattnera9376d42009-03-28 03:45:20 +00003246 // ===---------------------------------------------------===/
3247 // Serialize the "Translation Unit" metadata.
3248 // ===---------------------------------------------------===/
3249
3250 // Emit ASTContext.
3251 S.EnterBlock(ASTContextBlock);
3252 S.EmitOwnedPtr(this);
3253 S.ExitBlock(); // exit "ASTContextBlock"
3254
3255 S.EnterBlock(BasicMetadataBlock);
3256
3257 // Block for SourceManager and Target. Allows easy skipping
3258 // around to the block for the Selectors during deserialization.
3259 S.EnterBlock();
3260
3261 // Emit the SourceManager.
3262 S.Emit(getSourceManager());
3263
3264 // Emit the Target.
3265 S.EmitPtr(&Target);
3266 S.EmitCStr(Target.getTargetTriple());
3267
3268 S.ExitBlock(); // exit "SourceManager and Target Block"
3269
3270 // Emit the Selectors.
3271 S.Emit(Selectors);
3272
3273 // Emit the Identifier Table.
3274 S.Emit(Idents);
3275
3276 S.ExitBlock(); // exit "BasicMetadataBlock"
3277}
3278
3279
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003280/// Emit - Serialize an ASTContext object to Bitcode.
3281void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003282 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00003283 S.EmitRef(SourceMgr);
3284 S.EmitRef(Target);
3285 S.EmitRef(Idents);
3286 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003287
Ted Kremenekfee04522007-10-31 22:44:07 +00003288 // Emit the size of the type vector so that we can reserve that size
3289 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00003290 S.EmitInt(Types.size());
3291
Ted Kremenek03ed4402007-11-13 22:02:55 +00003292 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3293 I!=E;++I)
3294 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00003295
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003296 S.EmitOwnedPtr(TUDecl);
3297
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003298 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00003299}
3300
Chris Lattner557c5b12009-03-28 04:27:18 +00003301
3302ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3303 FileManager &FMgr) {
3304 // Check if the file is of the proper length.
3305 if (Buffer.getBufferSize() & 0x3) {
3306 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3307 return 0;
3308 }
3309
3310 // Create the bitstream reader.
3311 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3312 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3313
3314 if (Stream.Read(8) != 'B' ||
3315 Stream.Read(8) != 'C' ||
3316 Stream.Read(4) != 0xC ||
3317 Stream.Read(4) != 0xF ||
3318 Stream.Read(4) != 0xE ||
3319 Stream.Read(4) != 0x0) {
3320 // FIXME: Provide diagnostic.
3321 return NULL;
3322 }
3323
3324 // Create the deserializer.
3325 llvm::Deserializer Dezr(Stream);
3326
Chris Lattnera9376d42009-03-28 03:45:20 +00003327 // ===---------------------------------------------------===/
3328 // Deserialize the "Translation Unit" metadata.
3329 // ===---------------------------------------------------===/
3330
3331 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3332 // (which will appear earlier) and record its location.
3333
3334 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3335 assert (FoundBlock);
3336
3337 llvm::Deserializer::Location ASTContextBlockLoc =
3338 Dezr.getCurrentBlockLocation();
3339
3340 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3341 assert (FoundBlock);
3342
3343 // Read the SourceManager.
3344 SourceManager::CreateAndRegister(Dezr, FMgr);
3345
3346 { // Read the TargetInfo.
3347 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3348 char* triple = Dezr.ReadCStr(NULL,0,true);
3349 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3350 delete [] triple;
3351 }
3352
3353 // For Selectors, we must read the identifier table first because the
3354 // SelectorTable depends on the identifiers being already deserialized.
3355 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3356 Dezr.SkipBlock();
3357
3358 // Read the identifier table.
3359 IdentifierTable::CreateAndRegister(Dezr);
3360
3361 // Now jump back and read the selectors.
3362 Dezr.JumpTo(SelectorBlkLoc);
3363 SelectorTable::CreateAndRegister(Dezr);
3364
3365 // Now jump back to ASTContextBlock and read the ASTContext.
3366 Dezr.JumpTo(ASTContextBlockLoc);
3367 return Dezr.ReadOwnedPtr<ASTContext>();
3368}
3369
Ted Kremenek0f84c002007-11-13 00:25:37 +00003370ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00003371
3372 // Read the language options.
3373 LangOptions LOpts;
3374 LOpts.Read(D);
3375
Ted Kremenekfee04522007-10-31 22:44:07 +00003376 SourceManager &SM = D.ReadRef<SourceManager>();
3377 TargetInfo &t = D.ReadRef<TargetInfo>();
3378 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3379 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00003380
Ted Kremenekfee04522007-10-31 22:44:07 +00003381 unsigned size_reserve = D.ReadInt();
3382
Douglas Gregor2e1cd422008-11-17 14:58:09 +00003383 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3384 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00003385
Ted Kremenek03ed4402007-11-13 22:02:55 +00003386 for (unsigned i = 0; i < size_reserve; ++i)
3387 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00003388
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00003389 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3390
Ted Kremeneka9a4a242007-11-01 18:11:32 +00003391 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00003392
3393 return A;
3394}
Douglas Gregor2cf26342009-04-09 22:27:44 +00003395
3396ExternalASTSource::~ExternalASTSource() { }
3397
3398void ExternalASTSource::PrintStats() { }