blob: a3a2778860ed51b43d365006ca9cd7baf9ee99bd [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregor279272e2009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
Douglas Gregorc34897d2009-04-09 22:27:44 +000019#include "clang/AST/ExternalASTSource.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000020#include "clang/AST/RecordLayout.h"
Chris Lattnerb09b31d2009-03-28 03:45:20 +000021#include "clang/Basic/SourceManager.h"
Chris Lattner4b009652007-07-25 00:24:17 +000022#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000023#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000024#include "llvm/Bitcode/Serialize.h"
25#include "llvm/Bitcode/Deserialize.h"
Nate Begeman7903d052009-01-18 06:42:49 +000026#include "llvm/Support/MathExtras.h"
Chris Lattnerf4fbc442009-03-28 04:27:18 +000027#include "llvm/Support/MemoryBuffer.h"
Chris Lattner4b009652007-07-25 00:24:17 +000028using namespace clang;
29
30enum FloatingRank {
31 FloatRank, DoubleRank, LongDoubleRank
32};
33
Chris Lattner2fda0ed2008-10-05 17:34:18 +000034ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
35 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000036 IdentifierTable &idents, SelectorTable &sels,
Steve Naroff207b9ec2009-01-27 23:20:32 +000037 bool FreeMem, unsigned size_reserve) :
Douglas Gregor1e589cc2009-03-26 23:50:42 +000038 GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
39 ObjCFastEnumerationStateTypeDecl(0), SourceMgr(SM), LangOpts(LOpts),
Douglas Gregorc34897d2009-04-09 22:27:44 +000040 FreeMemory(FreeMem), Target(t), Idents(idents), Selectors(sels),
41 ExternalSource(0) {
Daniel Dunbarde300732008-08-11 04:54:23 +000042 if (size_reserve > 0) Types.reserve(size_reserve);
43 InitBuiltinTypes();
Chris Lattner911b8672009-03-13 22:38:49 +000044 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.NoBuiltin);
Daniel Dunbarde300732008-08-11 04:54:23 +000045 TUDecl = TranslationUnitDecl::Create(*this);
46}
47
Chris Lattner4b009652007-07-25 00:24:17 +000048ASTContext::~ASTContext() {
49 // Deallocate all the types.
50 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000051 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000052 Types.pop_back();
53 }
Eli Friedman65489b72008-05-27 03:08:09 +000054
Nuno Lopes355a8682008-12-17 22:30:25 +000055 {
56 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
57 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
58 while (I != E) {
59 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
60 delete R;
61 }
62 }
63
64 {
65 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
66 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
67 while (I != E) {
68 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
69 delete R;
70 }
71 }
72
73 {
Chris Lattner608c1e32009-03-31 09:24:30 +000074 llvm::DenseMap<const ObjCInterfaceDecl*, RecordDecl*>::iterator
Nuno Lopes355a8682008-12-17 22:30:25 +000075 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
76 while (I != E) {
Chris Lattner608c1e32009-03-31 09:24:30 +000077 RecordDecl *R = (I++)->second;
Nuno Lopes355a8682008-12-17 22:30:25 +000078 R->Destroy(*this);
79 }
80 }
81
Douglas Gregor1e589cc2009-03-26 23:50:42 +000082 // Destroy nested-name-specifiers.
Douglas Gregor3c4eae52009-03-27 23:54:10 +000083 for (llvm::FoldingSet<NestedNameSpecifier>::iterator
84 NNS = NestedNameSpecifiers.begin(),
85 NNSEnd = NestedNameSpecifiers.end();
Douglas Gregorbccd97c2009-03-27 23:25:45 +000086 NNS != NNSEnd;
Douglas Gregor3c4eae52009-03-27 23:54:10 +000087 /* Increment in loop */)
88 (*NNS++).Destroy(*this);
Douglas Gregor1e589cc2009-03-26 23:50:42 +000089
90 if (GlobalNestedNameSpecifier)
91 GlobalNestedNameSpecifier->Destroy(*this);
92
Eli Friedman65489b72008-05-27 03:08:09 +000093 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000094}
95
Douglas Gregorc34897d2009-04-09 22:27:44 +000096void
97ASTContext::setExternalSource(llvm::OwningPtr<ExternalASTSource> &Source) {
98 ExternalSource.reset(Source.take());
99}
100
Chris Lattner4b009652007-07-25 00:24:17 +0000101void ASTContext::PrintStats() const {
102 fprintf(stderr, "*** AST Context Stats:\n");
103 fprintf(stderr, " %d types total.\n", (int)Types.size());
104 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +0000105 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000106 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
107 unsigned NumLValueReference = 0, NumRValueReference = 0, NumMemberPointer = 0;
108
Chris Lattner4b009652007-07-25 00:24:17 +0000109 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000110 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
111 unsigned NumObjCQualifiedIds = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000112 unsigned NumTypeOfTypes = 0, NumTypeOfExprTypes = 0;
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000113 unsigned NumExtQual = 0;
114
Chris Lattner4b009652007-07-25 00:24:17 +0000115 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
116 Type *T = Types[i];
117 if (isa<BuiltinType>(T))
118 ++NumBuiltin;
119 else if (isa<PointerType>(T))
120 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +0000121 else if (isa<BlockPointerType>(T))
122 ++NumBlockPointer;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000123 else if (isa<LValueReferenceType>(T))
124 ++NumLValueReference;
125 else if (isa<RValueReferenceType>(T))
126 ++NumRValueReference;
Sebastian Redl75555032009-01-24 21:16:55 +0000127 else if (isa<MemberPointerType>(T))
128 ++NumMemberPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000129 else if (isa<ComplexType>(T))
130 ++NumComplex;
131 else if (isa<ArrayType>(T))
132 ++NumArray;
133 else if (isa<VectorType>(T))
134 ++NumVector;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000135 else if (isa<FunctionNoProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000136 ++NumFunctionNP;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000137 else if (isa<FunctionProtoType>(T))
Chris Lattner4b009652007-07-25 00:24:17 +0000138 ++NumFunctionP;
139 else if (isa<TypedefType>(T))
140 ++NumTypeName;
141 else if (TagType *TT = dyn_cast<TagType>(T)) {
142 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000143 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000144 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000145 case TagDecl::TK_struct: ++NumTagStruct; break;
146 case TagDecl::TK_union: ++NumTagUnion; break;
147 case TagDecl::TK_class: ++NumTagClass; break;
148 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000149 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000150 } else if (isa<ObjCInterfaceType>(T))
151 ++NumObjCInterfaces;
152 else if (isa<ObjCQualifiedInterfaceType>(T))
153 ++NumObjCQualifiedInterfaces;
154 else if (isa<ObjCQualifiedIdType>(T))
155 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000156 else if (isa<TypeOfType>(T))
157 ++NumTypeOfTypes;
Douglas Gregor4fa58902009-02-26 23:50:07 +0000158 else if (isa<TypeOfExprType>(T))
159 ++NumTypeOfExprTypes;
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000160 else if (isa<ExtQualType>(T))
161 ++NumExtQual;
Steve Naroff948fd372007-09-17 14:16:13 +0000162 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000163 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000164 assert(0 && "Unknown type!");
165 }
166 }
167
168 fprintf(stderr, " %d builtin types\n", NumBuiltin);
169 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000170 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000171 fprintf(stderr, " %d lvalue reference types\n", NumLValueReference);
172 fprintf(stderr, " %d rvalue reference types\n", NumRValueReference);
Sebastian Redl75555032009-01-24 21:16:55 +0000173 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000174 fprintf(stderr, " %d complex types\n", NumComplex);
175 fprintf(stderr, " %d array types\n", NumArray);
176 fprintf(stderr, " %d vector types\n", NumVector);
177 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
178 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
179 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
180 fprintf(stderr, " %d tagged types\n", NumTagged);
181 fprintf(stderr, " %d struct types\n", NumTagStruct);
182 fprintf(stderr, " %d union types\n", NumTagUnion);
183 fprintf(stderr, " %d class types\n", NumTagClass);
184 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000185 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000186 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000187 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000188 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000189 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000190 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
Douglas Gregor4fa58902009-02-26 23:50:07 +0000191 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprTypes);
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000192 fprintf(stderr, " %d attribute-qualified types\n", NumExtQual);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000193
Chris Lattner4b009652007-07-25 00:24:17 +0000194 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
195 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
196 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlce6fff02009-03-16 23:22:08 +0000197 NumLValueReference*sizeof(LValueReferenceType)+
198 NumRValueReference*sizeof(RValueReferenceType)+
Sebastian Redl75555032009-01-24 21:16:55 +0000199 NumMemberPointer*sizeof(MemberPointerType)+
Douglas Gregor4fa58902009-02-26 23:50:07 +0000200 NumFunctionP*sizeof(FunctionProtoType)+
201 NumFunctionNP*sizeof(FunctionNoProtoType)+
Steve Naroffe0430632008-05-21 15:59:22 +0000202 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
Douglas Gregord2b6edc2009-04-07 17:20:56 +0000203 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprTypes*sizeof(TypeOfExprType)+
204 NumExtQual*sizeof(ExtQualType)));
Douglas Gregorc34897d2009-04-09 22:27:44 +0000205
206 if (ExternalSource.get()) {
207 fprintf(stderr, "\n");
208 ExternalSource->PrintStats();
209 }
Chris Lattner4b009652007-07-25 00:24:17 +0000210}
211
212
213void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroff93fd2112009-01-27 22:08:43 +0000214 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000215}
216
Chris Lattner4b009652007-07-25 00:24:17 +0000217void ASTContext::InitBuiltinTypes() {
218 assert(VoidTy.isNull() && "Context reinitialized?");
219
220 // C99 6.2.5p19.
221 InitBuiltinType(VoidTy, BuiltinType::Void);
222
223 // C99 6.2.5p2.
224 InitBuiltinType(BoolTy, BuiltinType::Bool);
225 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000226 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000227 InitBuiltinType(CharTy, BuiltinType::Char_S);
228 else
229 InitBuiltinType(CharTy, BuiltinType::Char_U);
230 // C99 6.2.5p4.
231 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
232 InitBuiltinType(ShortTy, BuiltinType::Short);
233 InitBuiltinType(IntTy, BuiltinType::Int);
234 InitBuiltinType(LongTy, BuiltinType::Long);
235 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
236
237 // C99 6.2.5p6.
238 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
239 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
240 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
241 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
242 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
243
244 // C99 6.2.5p10.
245 InitBuiltinType(FloatTy, BuiltinType::Float);
246 InitBuiltinType(DoubleTy, BuiltinType::Double);
247 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000248
Chris Lattnere1dafe72009-02-26 23:43:47 +0000249 if (LangOpts.CPlusPlus) // C++ 3.9.1p5
250 InitBuiltinType(WCharTy, BuiltinType::WChar);
251 else // C99
252 WCharTy = getFromTargetType(Target.getWCharType());
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000253
Douglas Gregord2baafd2008-10-21 16:13:35 +0000254 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000255 InitBuiltinType(OverloadTy, BuiltinType::Overload);
256
257 // Placeholder type for type-dependent expressions whose type is
258 // completely unknown. No code should ever check a type against
259 // DependentTy and users should never see it; however, it is here to
260 // help diagnose failures to properly check for type-dependent
261 // expressions.
262 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000263
Chris Lattner4b009652007-07-25 00:24:17 +0000264 // C99 6.2.5p11.
265 FloatComplexTy = getComplexType(FloatTy);
266 DoubleComplexTy = getComplexType(DoubleTy);
267 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000268
Steve Naroff9d12c902007-10-15 14:41:52 +0000269 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000270 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000271 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000272 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000273 ClassStructType = 0;
274
Ted Kremenek42730c52008-01-07 19:49:32 +0000275 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000276
277 // void * type
278 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000279}
280
281//===----------------------------------------------------------------------===//
282// Type Sizing and Analysis
283//===----------------------------------------------------------------------===//
284
Chris Lattner2a674dc2008-06-30 18:32:54 +0000285/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
286/// scalar floating point type.
287const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
288 const BuiltinType *BT = T->getAsBuiltinType();
289 assert(BT && "Not a floating point type!");
290 switch (BT->getKind()) {
291 default: assert(0 && "Not a floating point type!");
292 case BuiltinType::Float: return Target.getFloatFormat();
293 case BuiltinType::Double: return Target.getDoubleFormat();
294 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
295 }
296}
297
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000298/// getDeclAlign - Return a conservative estimate of the alignment of the
299/// specified decl. Note that bitfields do not have a valid alignment, so
300/// this method will assert on them.
Daniel Dunbar96d1f1b2009-02-17 22:16:19 +0000301unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedman0ee57322009-02-22 02:56:25 +0000302 unsigned Align = Target.getCharWidth();
303
304 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
305 Align = std::max(Align, AA->getAlignment());
306
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000307 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
308 QualType T = VD->getType();
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000309 if (const ReferenceType* RT = T->getAsReferenceType()) {
310 unsigned AS = RT->getPointeeType().getAddressSpace();
Anders Carlssoneeaeda32009-04-10 04:52:36 +0000311 Align = Target.getPointerAlign(AS);
Anders Carlssonaa0783b2009-04-10 04:47:03 +0000312 } else if (!T->isIncompleteType() && !T->isFunctionType()) {
313 // Incomplete or function types default to 1.
Eli Friedman0ee57322009-02-22 02:56:25 +0000314 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
315 T = cast<ArrayType>(T)->getElementType();
316
317 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
318 }
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000319 }
Eli Friedman0ee57322009-02-22 02:56:25 +0000320
321 return Align / Target.getCharWidth();
Chris Lattnerbd3153e2009-01-24 21:53:27 +0000322}
Chris Lattner2a674dc2008-06-30 18:32:54 +0000323
Chris Lattner4b009652007-07-25 00:24:17 +0000324/// getTypeSize - Return the size of the specified type, in bits. This method
325/// does not work on incomplete types.
326std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000327ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000328 T = getCanonicalType(T);
Mike Stump44d1f402009-02-27 18:32:39 +0000329 uint64_t Width=0;
330 unsigned Align=8;
Chris Lattner4b009652007-07-25 00:24:17 +0000331 switch (T->getTypeClass()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +0000332#define TYPE(Class, Base)
333#define ABSTRACT_TYPE(Class, Base)
334#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
335#define DEPENDENT_TYPE(Class, Base) case Type::Class:
336#include "clang/AST/TypeNodes.def"
337 assert(false && "Should not see non-canonical or dependent types");
338 break;
339
Chris Lattner4b009652007-07-25 00:24:17 +0000340 case Type::FunctionNoProto:
341 case Type::FunctionProto:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000342 case Type::IncompleteArray:
Chris Lattner4b009652007-07-25 00:24:17 +0000343 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000344 case Type::VariableArray:
345 assert(0 && "VLAs not implemented yet!");
346 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000347 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000348
Chris Lattner8cd0e932008-03-05 18:54:05 +0000349 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000350 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000351 Align = EltInfo.second;
352 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000353 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000354 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000355 case Type::Vector: {
356 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000357 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000358 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000359 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000360 // If the alignment is not a power of 2, round up to the next power of 2.
361 // This happens for non-power-of-2 length vectors.
362 // FIXME: this should probably be a target property.
363 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000364 break;
365 }
366
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000367 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000368 switch (cast<BuiltinType>(T)->getKind()) {
369 default: assert(0 && "Unknown builtin type!");
370 case BuiltinType::Void:
371 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000372 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000373 Width = Target.getBoolWidth();
374 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000375 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000376 case BuiltinType::Char_S:
377 case BuiltinType::Char_U:
378 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000379 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000380 Width = Target.getCharWidth();
381 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000382 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000383 case BuiltinType::WChar:
384 Width = Target.getWCharWidth();
385 Align = Target.getWCharAlign();
386 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000387 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000388 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000389 Width = Target.getShortWidth();
390 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000391 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000392 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000393 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000394 Width = Target.getIntWidth();
395 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000396 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000397 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000398 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000399 Width = Target.getLongWidth();
400 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000401 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000402 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000403 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000404 Width = Target.getLongLongWidth();
405 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000406 break;
407 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000408 Width = Target.getFloatWidth();
409 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000410 break;
411 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000412 Width = Target.getDoubleWidth();
413 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000414 break;
415 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000416 Width = Target.getLongDoubleWidth();
417 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000418 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000419 }
420 break;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000421 case Type::FixedWidthInt:
422 // FIXME: This isn't precisely correct; the width/alignment should depend
423 // on the available types for the target
424 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattnere9174982009-02-15 21:20:13 +0000425 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000426 Align = Width;
427 break;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000428 case Type::ExtQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000429 // FIXME: Pointers into different addr spaces could have different sizes and
430 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000431 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000432 case Type::ObjCQualifiedId:
Eli Friedman2f6d70d2009-02-22 04:02:33 +0000433 case Type::ObjCQualifiedClass:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000434 case Type::ObjCQualifiedInterface:
Chris Lattner1d78a862008-04-07 07:01:58 +0000435 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000436 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000437 break;
Steve Naroff62f09f52008-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 Lattner461a6c52008-03-08 08:34:58 +0000444 case Type::Pointer: {
445 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000446 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000447 Align = Target.getPointerAlign(AS);
448 break;
449 }
Sebastian Redlce6fff02009-03-16 23:22:08 +0000450 case Type::LValueReference:
451 case Type::RValueReference:
Chris Lattner4b009652007-07-25 00:24:17 +0000452 // "When applied to a reference or a reference type, the result is the size
453 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000454 // FIXME: This is wrong for struct layout: a reference in a struct has
455 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000456 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redl75555032009-01-24 21:16:55 +0000457 case Type::MemberPointer: {
Sebastian Redl18cffee2009-01-24 23:29:36 +0000458 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redl75555032009-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 Redl18cffee2009-01-24 23:29:36 +0000461 // other compilers too, we need to delegate this completely to TargetInfo
462 // or some ABI abstraction layer.
Sebastian Redl75555032009-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 Lattner4b009652007-07-25 00:24:17 +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 Lattner8cd0e932008-03-05 18:54:05 +0000475 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000476 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000477 Align = EltInfo.second;
478 break;
479 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000480 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000481 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000482 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
483 Width = Layout.getSize();
484 Align = Layout.getAlignment();
485 break;
486 }
Douglas Gregor4fa58902009-02-26 23:50:07 +0000487 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +0000488 case Type::Enum: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000489 const TagType *TT = cast<TagType>(T);
490
491 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000492 Width = 1;
493 Align = 1;
494 break;
495 }
496
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000497 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000498 return getTypeInfo(ET->getDecl()->getIntegerType());
499
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000500 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000501 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
502 Width = Layout.getSize();
503 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000504 break;
505 }
Douglas Gregordd13e842009-03-30 22:58:21 +0000506
507 case Type::TemplateSpecialization:
508 assert(false && "Dependent types have no size");
509 break;
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000510 }
Chris Lattner4b009652007-07-25 00:24:17 +0000511
512 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000513 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000514}
515
Chris Lattner83165b52009-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 Dunbarc61a8002009-02-18 19:59:32 +0000524 if (T->isSpecificBuiltinType(BuiltinType::Double))
525 return std::max(ABIAlign, 64U);
Chris Lattner83165b52009-01-27 18:08:34 +0000526
527 return ABIAlign;
528}
529
530
Devang Patelbfe323c2008-06-04 21:22:16 +0000531/// LayoutField - Field layout.
532void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000533 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000534 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000535 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000536 uint64_t FieldOffset = IsUnion ? 0 : Size;
537 uint64_t FieldSize;
538 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-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 Patelbfe323c2008-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 Dunbar7cbcbf42008-08-13 23:47:13 +0000548 FieldSize =
549 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-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 Dunbar2cb762f2008-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 Patelbfe323c2008-06-04 21:22:16 +0000560 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000561 if (FieldPacking)
562 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-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 Lattnerfd799692008-08-09 21:35:13 +0000575 if (FD->getType()->isIncompleteArrayType()) {
576 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-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 Lattnera1923f62008-08-04 07:31:14 +0000581 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000582 FieldAlign = Context.getTypeAlign(ATy->getElementType());
Anders Carlsson0843ea52009-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 Patelbfe323c2008-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 Dunbar2cb762f2008-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 Patelbfe323c2008-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 Dunbar5573a582009-04-22 03:45:12 +0000624static void CollectLocalObjCIvars(ASTContext *Ctx,
625 const ObjCInterfaceDecl *OI,
626 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000627 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
628 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000629 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000630 if (!IVDecl->isInvalidDecl())
631 Fields.push_back(cast<FieldDecl>(IVDecl));
632 }
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000633 // look into properties.
Daniel Dunbar5573a582009-04-22 03:45:12 +0000634 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*Ctx),
635 E = OI->prop_end(*Ctx); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000636 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000637 Fields.push_back(cast<FieldDecl>(IV));
638 }
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000639}
640
Daniel Dunbar5573a582009-04-22 03:45:12 +0000641
642void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
643 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
644 if (const ObjCInterfaceDecl *SuperClass = OI->getSuperClass())
645 CollectObjCIvars(SuperClass, Fields);
646 CollectLocalObjCIvars(this, OI, Fields);
647}
648
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000649/// addRecordToClass - produces record info. for the class for its
650/// ivars and all those inherited.
651///
Chris Lattner9329cf52009-03-31 08:48:01 +0000652const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) {
Daniel Dunbardbc8af72009-04-21 21:41:56 +0000653 // FIXME: The only client relying on this working in the presence of
Daniel Dunbar5573a582009-04-22 03:45:12 +0000654 // forward declarations is CodeGenTypes in IRgen, which should not
655 // need it. Fix and simplify this code.
Chris Lattner608c1e32009-03-31 09:24:30 +0000656 RecordDecl *&RD = ASTRecordForInterface[D];
657 if (RD) {
658 // If we have a record decl already and it is either a definition or if 'D'
659 // is still a forward declaration, return it.
660 if (RD->isDefinition() || D->isForwardDecl())
661 return RD;
662 }
663
664 // If D is a forward declaration, then just make a forward struct decl.
665 if (D->isForwardDecl())
666 return RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
667 D->getLocation(),
668 D->getIdentifier());
Chris Lattner9329cf52009-03-31 08:48:01 +0000669
670 llvm::SmallVector<FieldDecl*, 32> RecFields;
Daniel Dunbar5573a582009-04-22 03:45:12 +0000671 CollectLocalObjCIvars(this, D, RecFields);
Chris Lattner608c1e32009-03-31 09:24:30 +0000672
673 if (RD == 0)
674 RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(),
675 D->getIdentifier());
Daniel Dunbar5573a582009-04-22 03:45:12 +0000676
677
678 const RecordDecl *SRD;
679 if (const ObjCInterfaceDecl *SuperClass = D->getSuperClass()) {
680 SRD = addRecordToClass(SuperClass);
681 } else {
682 SRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, SourceLocation(), 0);
683 const_cast<RecordDecl*>(SRD)->completeDefinition(*this);
684 }
685
686 RD->addDecl(*this,
687 FieldDecl::Create(*this, RD,
688 SourceLocation(),
689 0,
690 getTagDeclType(const_cast<RecordDecl*>(SRD)),
691 0, false));
692
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000693 /// FIXME! Can do collection of ivars and adding to the record while
694 /// doing it.
Chris Lattner41b780c2009-03-31 08:58:42 +0000695 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000696 RD->addDecl(*this,
697 FieldDecl::Create(*this, RD,
Chris Lattner608c1e32009-03-31 09:24:30 +0000698 RecFields[i]->getLocation(),
699 RecFields[i]->getIdentifier(),
700 RecFields[i]->getType(),
701 RecFields[i]->getBitWidth(), false));
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000702 }
Chris Lattner9329cf52009-03-31 08:48:01 +0000703
Chris Lattner608c1e32009-03-31 09:24:30 +0000704 RD->completeDefinition(*this);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000705 return RD;
706}
Devang Patel4b6bf702008-06-04 21:54:36 +0000707
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000708/// getASTObjcInterfaceLayout - Get or compute information about the layout of
709/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000710/// position information.
711const ASTRecordLayout &
712ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
713 // Look up this layout, if already laid out, return what we have.
714 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
715 if (Entry) return *Entry;
716
717 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
718 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000719 ASTRecordLayout *NewEntry = NULL;
Fariborz Jahanianba50c332009-04-08 21:54:52 +0000720 // FIXME. Add actual count of synthesized ivars, instead of count
721 // of properties which is the upper bound, but is safe.
Daniel Dunbar998510f2009-04-08 20:18:15 +0000722 unsigned FieldCount =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000723 D->ivar_size() + std::distance(D->prop_begin(*this), D->prop_end(*this));
Devang Patel8682d882008-06-06 02:14:01 +0000724 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
725 FieldCount++;
726 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
727 unsigned Alignment = SL.getAlignment();
728 uint64_t Size = SL.getSize();
729 NewEntry = new ASTRecordLayout(Size, Alignment);
730 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000731 // Super class is at the beginning of the layout.
732 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000733 } else {
734 NewEntry = new ASTRecordLayout();
735 NewEntry->InitializeLayout(FieldCount);
736 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000737 Entry = NewEntry;
738
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000739 unsigned StructPacking = 0;
740 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
741 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000742
743 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
744 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
745 AA->getAlignment()));
746
747 // Layout each ivar sequentially.
748 unsigned i = 0;
749 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
750 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
751 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000752 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000753 }
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000754 // Also synthesized ivars
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000755 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
756 E = D->prop_end(*this); I != E; ++I) {
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000757 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
758 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
759 }
Fariborz Jahanian84c45692009-04-01 19:37:34 +0000760
Devang Patel4b6bf702008-06-04 21:54:36 +0000761 // Finally, round the size of the total struct up to the alignment of the
762 // struct itself.
763 NewEntry->FinalizeLayout();
764 return *NewEntry;
765}
766
Devang Patel7a78e432007-11-01 19:11:01 +0000767/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000768/// specified record (struct/union/class), which indicates its size and field
769/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000770const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000771 D = D->getDefinition(*this);
772 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000773
Chris Lattner4b009652007-07-25 00:24:17 +0000774 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000775 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000776 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000777
Devang Patel7a78e432007-11-01 19:11:01 +0000778 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
779 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
780 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000781 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000782
Douglas Gregor39677622008-12-11 20:41:00 +0000783 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000784 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
785 D->field_end(*this)));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000786 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000787
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000788 unsigned StructPacking = 0;
789 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
790 StructPacking = PA->getAlignment();
791
Eli Friedman5949a022008-05-30 09:31:38 +0000792 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000793 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
794 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000795
Eli Friedman5949a022008-05-30 09:31:38 +0000796 // Layout each field, for now, just sequentially, respecting alignment. In
797 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000798 unsigned FieldIdx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000799 for (RecordDecl::field_iterator Field = D->field_begin(*this),
800 FieldEnd = D->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000801 Field != FieldEnd; (void)++Field, ++FieldIdx)
802 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000803
804 // Finally, round the size of the total struct up to the alignment of the
805 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000806 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000807 return *NewEntry;
808}
809
Chris Lattner4b009652007-07-25 00:24:17 +0000810//===----------------------------------------------------------------------===//
811// Type creation/memoization methods
812//===----------------------------------------------------------------------===//
813
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000814QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000815 QualType CanT = getCanonicalType(T);
816 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000817 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000818
819 // If we are composing extended qualifiers together, merge together into one
820 // ExtQualType node.
821 unsigned CVRQuals = T.getCVRQualifiers();
822 QualType::GCAttrTypes GCAttr = QualType::GCNone;
823 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000824
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000825 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
826 // If this type already has an address space specified, it cannot get
827 // another one.
828 assert(EQT->getAddressSpace() == 0 &&
829 "Type cannot be in multiple addr spaces!");
830 GCAttr = EQT->getObjCGCAttr();
831 TypeNode = EQT->getBaseType();
832 }
Chris Lattner35fef522008-02-20 20:55:12 +0000833
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000834 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000835 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000836 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000837 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000838 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000839 return QualType(EXTQy, CVRQuals);
840
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000841 // If the base type isn't canonical, this won't be a canonical type either,
842 // so fill in the canonical type field.
843 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000844 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000845 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000846
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000847 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000848 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000849 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000850 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000851 ExtQualType *New =
852 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000853 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000854 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000855 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000856}
857
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000858QualType ASTContext::getObjCGCQualType(QualType T,
859 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000860 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000861 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000862 return T;
863
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000864 // If we are composing extended qualifiers together, merge together into one
865 // ExtQualType node.
866 unsigned CVRQuals = T.getCVRQualifiers();
867 Type *TypeNode = T.getTypePtr();
868 unsigned AddressSpace = 0;
869
870 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
871 // If this type already has an address space specified, it cannot get
872 // another one.
873 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
874 "Type cannot be in multiple addr spaces!");
875 AddressSpace = EQT->getAddressSpace();
876 TypeNode = EQT->getBaseType();
877 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000878
879 // Check if we've already instantiated an gc qual'd type of this type.
880 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000881 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000882 void *InsertPos = 0;
883 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000884 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000885
886 // If the base type isn't canonical, this won't be a canonical type either,
887 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +0000888 // FIXME: Isn't this also not canonical if the base type is a array
889 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000890 QualType Canonical;
891 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000892 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000893
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000894 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000895 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
896 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
897 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000898 ExtQualType *New =
899 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000900 ExtQualTypes.InsertNode(New, InsertPos);
901 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000902 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000903}
Chris Lattner4b009652007-07-25 00:24:17 +0000904
905/// getComplexType - Return the uniqued reference to the type for a complex
906/// number with the specified element type.
907QualType ASTContext::getComplexType(QualType T) {
908 // Unique pointers, to guarantee there is only one pointer of a particular
909 // structure.
910 llvm::FoldingSetNodeID ID;
911 ComplexType::Profile(ID, T);
912
913 void *InsertPos = 0;
914 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
915 return QualType(CT, 0);
916
917 // If the pointee type isn't canonical, this won't be a canonical type either,
918 // so fill in the canonical type field.
919 QualType Canonical;
920 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000921 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000922
923 // Get the new insert position for the node we care about.
924 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000925 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000926 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000927 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000928 Types.push_back(New);
929 ComplexTypes.InsertNode(New, InsertPos);
930 return QualType(New, 0);
931}
932
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000933QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
934 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
935 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
936 FixedWidthIntType *&Entry = Map[Width];
937 if (!Entry)
938 Entry = new FixedWidthIntType(Width, Signed);
939 return QualType(Entry, 0);
940}
Chris Lattner4b009652007-07-25 00:24:17 +0000941
942/// getPointerType - Return the uniqued reference to the type for a pointer to
943/// the specified type.
944QualType ASTContext::getPointerType(QualType T) {
945 // Unique pointers, to guarantee there is only one pointer of a particular
946 // structure.
947 llvm::FoldingSetNodeID ID;
948 PointerType::Profile(ID, T);
949
950 void *InsertPos = 0;
951 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
952 return QualType(PT, 0);
953
954 // If the pointee type isn't canonical, this won't be a canonical type either,
955 // so fill in the canonical type field.
956 QualType Canonical;
957 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000958 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000959
960 // Get the new insert position for the node we care about.
961 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000962 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000963 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000964 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000965 Types.push_back(New);
966 PointerTypes.InsertNode(New, InsertPos);
967 return QualType(New, 0);
968}
969
Steve Naroff7aa54752008-08-27 16:04:49 +0000970/// getBlockPointerType - Return the uniqued reference to the type for
971/// a pointer to the specified block.
972QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000973 assert(T->isFunctionType() && "block of function types only");
974 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000975 // structure.
976 llvm::FoldingSetNodeID ID;
977 BlockPointerType::Profile(ID, T);
978
979 void *InsertPos = 0;
980 if (BlockPointerType *PT =
981 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
982 return QualType(PT, 0);
983
Steve Narofffd5b19d2008-08-28 19:20:44 +0000984 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000985 // type either so fill in the canonical type field.
986 QualType Canonical;
987 if (!T->isCanonical()) {
988 Canonical = getBlockPointerType(getCanonicalType(T));
989
990 // Get the new insert position for the node we care about.
991 BlockPointerType *NewIP =
992 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000993 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000994 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000995 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000996 Types.push_back(New);
997 BlockPointerTypes.InsertNode(New, InsertPos);
998 return QualType(New, 0);
999}
1000
Sebastian Redlce6fff02009-03-16 23:22:08 +00001001/// getLValueReferenceType - Return the uniqued reference to the type for an
1002/// lvalue reference to the specified type.
1003QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +00001004 // Unique pointers, to guarantee there is only one pointer of a particular
1005 // structure.
1006 llvm::FoldingSetNodeID ID;
1007 ReferenceType::Profile(ID, T);
1008
1009 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +00001010 if (LValueReferenceType *RT =
1011 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001012 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001013
Chris Lattner4b009652007-07-25 00:24:17 +00001014 // If the referencee type isn't canonical, this won't be a canonical type
1015 // either, so fill in the canonical type field.
1016 QualType Canonical;
1017 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +00001018 Canonical = getLValueReferenceType(getCanonicalType(T));
1019
Chris Lattner4b009652007-07-25 00:24:17 +00001020 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +00001021 LValueReferenceType *NewIP =
1022 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001023 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001024 }
1025
Sebastian Redlce6fff02009-03-16 23:22:08 +00001026 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001027 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001028 LValueReferenceTypes.InsertNode(New, InsertPos);
1029 return QualType(New, 0);
1030}
1031
1032/// getRValueReferenceType - Return the uniqued reference to the type for an
1033/// rvalue reference to the specified type.
1034QualType ASTContext::getRValueReferenceType(QualType T) {
1035 // Unique pointers, to guarantee there is only one pointer of a particular
1036 // structure.
1037 llvm::FoldingSetNodeID ID;
1038 ReferenceType::Profile(ID, T);
1039
1040 void *InsertPos = 0;
1041 if (RValueReferenceType *RT =
1042 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1043 return QualType(RT, 0);
1044
1045 // If the referencee type isn't canonical, this won't be a canonical type
1046 // either, so fill in the canonical type field.
1047 QualType Canonical;
1048 if (!T->isCanonical()) {
1049 Canonical = getRValueReferenceType(getCanonicalType(T));
1050
1051 // Get the new insert position for the node we care about.
1052 RValueReferenceType *NewIP =
1053 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1054 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1055 }
1056
1057 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1058 Types.push_back(New);
1059 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001060 return QualType(New, 0);
1061}
1062
Sebastian Redl75555032009-01-24 21:16:55 +00001063/// getMemberPointerType - Return the uniqued reference to the type for a
1064/// member pointer to the specified type, in the specified class.
1065QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1066{
1067 // Unique pointers, to guarantee there is only one pointer of a particular
1068 // structure.
1069 llvm::FoldingSetNodeID ID;
1070 MemberPointerType::Profile(ID, T, Cls);
1071
1072 void *InsertPos = 0;
1073 if (MemberPointerType *PT =
1074 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1075 return QualType(PT, 0);
1076
1077 // If the pointee or class type isn't canonical, this won't be a canonical
1078 // type either, so fill in the canonical type field.
1079 QualType Canonical;
1080 if (!T->isCanonical()) {
1081 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1082
1083 // Get the new insert position for the node we care about.
1084 MemberPointerType *NewIP =
1085 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1086 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1087 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001088 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001089 Types.push_back(New);
1090 MemberPointerTypes.InsertNode(New, InsertPos);
1091 return QualType(New, 0);
1092}
1093
Steve Naroff83c13012007-08-30 01:06:46 +00001094/// getConstantArrayType - Return the unique reference to the type for an
1095/// array of the specified element type.
1096QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +00001097 const llvm::APInt &ArySize,
1098 ArrayType::ArraySizeModifier ASM,
1099 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001100 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001101 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001102
1103 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001104 if (ConstantArrayType *ATP =
1105 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001106 return QualType(ATP, 0);
1107
1108 // If the element type isn't canonical, this won't be a canonical type either,
1109 // so fill in the canonical type field.
1110 QualType Canonical;
1111 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001112 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001113 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001114 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001115 ConstantArrayType *NewIP =
1116 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001117 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001118 }
1119
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001120 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001121 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001122 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001123 Types.push_back(New);
1124 return QualType(New, 0);
1125}
1126
Steve Naroffe2579e32007-08-30 18:14:25 +00001127/// getVariableArrayType - Returns a non-unique reference to the type for a
1128/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +00001129QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1130 ArrayType::ArraySizeModifier ASM,
1131 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001132 // Since we don't unique expressions, it isn't possible to unique VLA's
1133 // that have an expression provided for their size.
1134
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001135 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001136 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001137
1138 VariableArrayTypes.push_back(New);
1139 Types.push_back(New);
1140 return QualType(New, 0);
1141}
1142
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001143/// getDependentSizedArrayType - Returns a non-unique reference to
1144/// the type for a dependently-sized array of the specified element
1145/// type. FIXME: We will need these to be uniqued, or at least
1146/// comparable, at some point.
1147QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1148 ArrayType::ArraySizeModifier ASM,
1149 unsigned EltTypeQuals) {
1150 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1151 "Size must be type- or value-dependent!");
1152
1153 // Since we don't unique expressions, it isn't possible to unique
1154 // dependently-sized array types.
1155
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001156 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001157 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1158 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001159
1160 DependentSizedArrayTypes.push_back(New);
1161 Types.push_back(New);
1162 return QualType(New, 0);
1163}
1164
Eli Friedman8ff07782008-02-15 18:16:39 +00001165QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1166 ArrayType::ArraySizeModifier ASM,
1167 unsigned EltTypeQuals) {
1168 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001169 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001170
1171 void *InsertPos = 0;
1172 if (IncompleteArrayType *ATP =
1173 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1174 return QualType(ATP, 0);
1175
1176 // If the element type isn't canonical, this won't be a canonical type
1177 // either, so fill in the canonical type field.
1178 QualType Canonical;
1179
1180 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001181 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001182 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001183
1184 // Get the new insert position for the node we care about.
1185 IncompleteArrayType *NewIP =
1186 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001187 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001188 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001189
Steve Naroff93fd2112009-01-27 22:08:43 +00001190 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001191 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001192
1193 IncompleteArrayTypes.InsertNode(New, InsertPos);
1194 Types.push_back(New);
1195 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001196}
1197
Chris Lattner4b009652007-07-25 00:24:17 +00001198/// getVectorType - Return the unique reference to a vector type of
1199/// the specified element type and size. VectorType must be a built-in type.
1200QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1201 BuiltinType *baseType;
1202
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001203 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001204 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1205
1206 // Check if we've already instantiated a vector of this type.
1207 llvm::FoldingSetNodeID ID;
1208 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1209 void *InsertPos = 0;
1210 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1211 return QualType(VTP, 0);
1212
1213 // If the element type isn't canonical, this won't be a canonical type either,
1214 // so fill in the canonical type field.
1215 QualType Canonical;
1216 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001217 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001218
1219 // Get the new insert position for the node we care about.
1220 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001221 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001222 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001223 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001224 VectorTypes.InsertNode(New, InsertPos);
1225 Types.push_back(New);
1226 return QualType(New, 0);
1227}
1228
Nate Begemanaf6ed502008-04-18 23:10:10 +00001229/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001230/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001231QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001232 BuiltinType *baseType;
1233
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001234 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001235 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001236
1237 // Check if we've already instantiated a vector of this type.
1238 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001239 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001240 void *InsertPos = 0;
1241 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1242 return QualType(VTP, 0);
1243
1244 // If the element type isn't canonical, this won't be a canonical type either,
1245 // so fill in the canonical type field.
1246 QualType Canonical;
1247 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001248 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001249
1250 // Get the new insert position for the node we care about.
1251 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001252 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001253 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001254 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001255 VectorTypes.InsertNode(New, InsertPos);
1256 Types.push_back(New);
1257 return QualType(New, 0);
1258}
1259
Douglas Gregor4fa58902009-02-26 23:50:07 +00001260/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001261///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001262QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001263 // Unique functions, to guarantee there is only one function of a particular
1264 // structure.
1265 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001266 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001267
1268 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001269 if (FunctionNoProtoType *FT =
1270 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001271 return QualType(FT, 0);
1272
1273 QualType Canonical;
1274 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001275 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001276
1277 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001278 FunctionNoProtoType *NewIP =
1279 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001280 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001281 }
1282
Douglas Gregor4fa58902009-02-26 23:50:07 +00001283 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001284 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001285 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001286 return QualType(New, 0);
1287}
1288
1289/// getFunctionType - Return a normal function type with a typed argument
1290/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001291QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001292 unsigned NumArgs, bool isVariadic,
1293 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001294 // Unique functions, to guarantee there is only one function of a particular
1295 // structure.
1296 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001297 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001298 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001299
1300 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001301 if (FunctionProtoType *FTP =
1302 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001303 return QualType(FTP, 0);
1304
1305 // Determine whether the type being created is already canonical or not.
1306 bool isCanonical = ResultTy->isCanonical();
1307 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1308 if (!ArgArray[i]->isCanonical())
1309 isCanonical = false;
1310
1311 // If this type isn't canonical, get the canonical version of it.
1312 QualType Canonical;
1313 if (!isCanonical) {
1314 llvm::SmallVector<QualType, 16> CanonicalArgs;
1315 CanonicalArgs.reserve(NumArgs);
1316 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001317 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001318
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001319 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001320 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001321 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001322
1323 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001324 FunctionProtoType *NewIP =
1325 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001326 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001327 }
1328
Douglas Gregor4fa58902009-02-26 23:50:07 +00001329 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001330 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001331 FunctionProtoType *FTP =
1332 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001333 NumArgs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001334 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001335 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001336 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001337 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001338 return QualType(FTP, 0);
1339}
1340
Douglas Gregor1d661552008-04-13 21:07:44 +00001341/// getTypeDeclType - Return the unique reference to the type for the
1342/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001343QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001344 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001345 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1346
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001347 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001348 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001349 else if (isa<TemplateTypeParmDecl>(Decl)) {
1350 assert(false && "Template type parameter types are always available.");
1351 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001352 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001353
Douglas Gregor2e047592009-02-28 01:32:25 +00001354 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001355 if (PrevDecl)
1356 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001357 else
1358 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001359 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001360 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1361 if (PrevDecl)
1362 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001363 else
1364 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001365 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001366 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001367 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001368
Ted Kremenek46a837c2008-09-05 17:16:31 +00001369 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001370 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001371}
1372
Chris Lattner4b009652007-07-25 00:24:17 +00001373/// getTypedefType - Return the unique reference to the type for the
1374/// specified typename decl.
1375QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1376 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1377
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001378 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001379 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001380 Types.push_back(Decl->TypeForDecl);
1381 return QualType(Decl->TypeForDecl, 0);
1382}
1383
Ted Kremenek42730c52008-01-07 19:49:32 +00001384/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001385/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001386QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001387 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1388
Steve Naroff93fd2112009-01-27 22:08:43 +00001389 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001390 Types.push_back(Decl->TypeForDecl);
1391 return QualType(Decl->TypeForDecl, 0);
1392}
1393
Douglas Gregora4918772009-02-05 23:33:38 +00001394/// \brief Retrieve the template type parameter type for a template
1395/// parameter with the given depth, index, and (optionally) name.
1396QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1397 IdentifierInfo *Name) {
1398 llvm::FoldingSetNodeID ID;
1399 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1400 void *InsertPos = 0;
1401 TemplateTypeParmType *TypeParm
1402 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1403
1404 if (TypeParm)
1405 return QualType(TypeParm, 0);
1406
1407 if (Name)
1408 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1409 getTemplateTypeParmType(Depth, Index));
1410 else
1411 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1412
1413 Types.push_back(TypeParm);
1414 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1415
1416 return QualType(TypeParm, 0);
1417}
1418
Douglas Gregor8e458f42009-02-09 18:46:07 +00001419QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001420ASTContext::getTemplateSpecializationType(TemplateName Template,
1421 const TemplateArgument *Args,
1422 unsigned NumArgs,
1423 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001424 if (!Canon.isNull())
1425 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001426
Douglas Gregor8e458f42009-02-09 18:46:07 +00001427 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001428 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001429
Douglas Gregor8e458f42009-02-09 18:46:07 +00001430 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001431 TemplateSpecializationType *Spec
1432 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001433
1434 if (Spec)
1435 return QualType(Spec, 0);
1436
Douglas Gregordd13e842009-03-30 22:58:21 +00001437 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001438 sizeof(TemplateArgument) * NumArgs),
1439 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001440 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001441 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001442 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001443
1444 return QualType(Spec, 0);
1445}
1446
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001447QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001448ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001449 QualType NamedType) {
1450 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001451 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001452
1453 void *InsertPos = 0;
1454 QualifiedNameType *T
1455 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1456 if (T)
1457 return QualType(T, 0);
1458
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001459 T = new (*this) QualifiedNameType(NNS, NamedType,
1460 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001461 Types.push_back(T);
1462 QualifiedNameTypes.InsertNode(T, InsertPos);
1463 return QualType(T, 0);
1464}
1465
Douglas Gregord3022602009-03-27 23:10:48 +00001466QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1467 const IdentifierInfo *Name,
1468 QualType Canon) {
1469 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1470
1471 if (Canon.isNull()) {
1472 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1473 if (CanonNNS != NNS)
1474 Canon = getTypenameType(CanonNNS, Name);
1475 }
1476
1477 llvm::FoldingSetNodeID ID;
1478 TypenameType::Profile(ID, NNS, Name);
1479
1480 void *InsertPos = 0;
1481 TypenameType *T
1482 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1483 if (T)
1484 return QualType(T, 0);
1485
1486 T = new (*this) TypenameType(NNS, Name, Canon);
1487 Types.push_back(T);
1488 TypenameTypes.InsertNode(T, InsertPos);
1489 return QualType(T, 0);
1490}
1491
Douglas Gregor77da5802009-04-01 00:28:59 +00001492QualType
1493ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1494 const TemplateSpecializationType *TemplateId,
1495 QualType Canon) {
1496 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1497
1498 if (Canon.isNull()) {
1499 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1500 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1501 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1502 const TemplateSpecializationType *CanonTemplateId
1503 = CanonType->getAsTemplateSpecializationType();
1504 assert(CanonTemplateId &&
1505 "Canonical type must also be a template specialization type");
1506 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1507 }
1508 }
1509
1510 llvm::FoldingSetNodeID ID;
1511 TypenameType::Profile(ID, NNS, TemplateId);
1512
1513 void *InsertPos = 0;
1514 TypenameType *T
1515 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1516 if (T)
1517 return QualType(T, 0);
1518
1519 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1520 Types.push_back(T);
1521 TypenameTypes.InsertNode(T, InsertPos);
1522 return QualType(T, 0);
1523}
1524
Chris Lattnere1352302008-04-07 04:56:42 +00001525/// CmpProtocolNames - Comparison predicate for sorting protocols
1526/// alphabetically.
1527static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1528 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001529 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001530}
1531
1532static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1533 unsigned &NumProtocols) {
1534 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1535
1536 // Sort protocols, keyed by name.
1537 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1538
1539 // Remove duplicates.
1540 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1541 NumProtocols = ProtocolsEnd-Protocols;
1542}
1543
1544
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001545/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1546/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001547QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1548 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001549 // Sort the protocol list alphabetically to canonicalize it.
1550 SortAndUniqueProtocols(Protocols, NumProtocols);
1551
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001552 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001553 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001554
1555 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001556 if (ObjCQualifiedInterfaceType *QT =
1557 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001558 return QualType(QT, 0);
1559
1560 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001561 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001562 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001563
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001564 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001565 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001566 return QualType(QType, 0);
1567}
1568
Chris Lattnere1352302008-04-07 04:56:42 +00001569/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1570/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001571QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001572 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001573 // Sort the protocol list alphabetically to canonicalize it.
1574 SortAndUniqueProtocols(Protocols, NumProtocols);
1575
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001576 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001577 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001578
1579 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001580 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001581 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001582 return QualType(QT, 0);
1583
1584 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001585 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001586 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001587 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001588 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001589 return QualType(QType, 0);
1590}
1591
Douglas Gregor4fa58902009-02-26 23:50:07 +00001592/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1593/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001594/// multiple declarations that refer to "typeof(x)" all contain different
1595/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1596/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001597QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001598 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001599 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001600 Types.push_back(toe);
1601 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001602}
1603
Steve Naroff0604dd92007-08-01 18:02:17 +00001604/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1605/// TypeOfType AST's. The only motivation to unique these nodes would be
1606/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1607/// an issue. This doesn't effect the type checker, since it operates
1608/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001609QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001610 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001611 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001612 Types.push_back(tot);
1613 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001614}
1615
Chris Lattner4b009652007-07-25 00:24:17 +00001616/// getTagDeclType - Return the unique reference to the type for the
1617/// specified TagDecl (struct/union/class/enum) decl.
1618QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001619 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001620 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001621}
1622
1623/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1624/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1625/// needs to agree with the definition in <stddef.h>.
1626QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001627 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001628}
1629
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001630/// getSignedWCharType - Return the type of "signed wchar_t".
1631/// Used when in C++, as a GCC extension.
1632QualType ASTContext::getSignedWCharType() const {
1633 // FIXME: derive from "Target" ?
1634 return WCharTy;
1635}
1636
1637/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1638/// Used when in C++, as a GCC extension.
1639QualType ASTContext::getUnsignedWCharType() const {
1640 // FIXME: derive from "Target" ?
1641 return UnsignedIntTy;
1642}
1643
Chris Lattner4b009652007-07-25 00:24:17 +00001644/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1645/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1646QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001647 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001648}
1649
Chris Lattner19eb97e2008-04-02 05:18:44 +00001650//===----------------------------------------------------------------------===//
1651// Type Operators
1652//===----------------------------------------------------------------------===//
1653
Chris Lattner3dae6f42008-04-06 22:41:35 +00001654/// getCanonicalType - Return the canonical (structural) type corresponding to
1655/// the specified potentially non-canonical type. The non-canonical version
1656/// of a type may have many "decorated" versions of types. Decorators can
1657/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1658/// to be free of any of these, allowing two canonical types to be compared
1659/// for exact equality with a simple pointer comparison.
1660QualType ASTContext::getCanonicalType(QualType T) {
1661 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001662
1663 // If the result has type qualifiers, make sure to canonicalize them as well.
1664 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1665 if (TypeQuals == 0) return CanType;
1666
1667 // If the type qualifiers are on an array type, get the canonical type of the
1668 // array with the qualifiers applied to the element type.
1669 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1670 if (!AT)
1671 return CanType.getQualifiedType(TypeQuals);
1672
1673 // Get the canonical version of the element with the extra qualifiers on it.
1674 // This can recursively sink qualifiers through multiple levels of arrays.
1675 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1676 NewEltTy = getCanonicalType(NewEltTy);
1677
1678 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1679 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1680 CAT->getIndexTypeQualifier());
1681 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1682 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1683 IAT->getIndexTypeQualifier());
1684
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001685 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1686 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1687 DSAT->getSizeModifier(),
1688 DSAT->getIndexTypeQualifier());
1689
Chris Lattnera1923f62008-08-04 07:31:14 +00001690 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1691 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1692 VAT->getSizeModifier(),
1693 VAT->getIndexTypeQualifier());
1694}
1695
Douglas Gregord3022602009-03-27 23:10:48 +00001696NestedNameSpecifier *
1697ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1698 if (!NNS)
1699 return 0;
1700
1701 switch (NNS->getKind()) {
1702 case NestedNameSpecifier::Identifier:
1703 // Canonicalize the prefix but keep the identifier the same.
1704 return NestedNameSpecifier::Create(*this,
1705 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1706 NNS->getAsIdentifier());
1707
1708 case NestedNameSpecifier::Namespace:
1709 // A namespace is canonical; build a nested-name-specifier with
1710 // this namespace and no prefix.
1711 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1712
1713 case NestedNameSpecifier::TypeSpec:
1714 case NestedNameSpecifier::TypeSpecWithTemplate: {
1715 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1716 NestedNameSpecifier *Prefix = 0;
1717
1718 // FIXME: This isn't the right check!
1719 if (T->isDependentType())
1720 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1721
1722 return NestedNameSpecifier::Create(*this, Prefix,
1723 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1724 T.getTypePtr());
1725 }
1726
1727 case NestedNameSpecifier::Global:
1728 // The global specifier is canonical and unique.
1729 return NNS;
1730 }
1731
1732 // Required to silence a GCC warning
1733 return 0;
1734}
1735
Chris Lattnera1923f62008-08-04 07:31:14 +00001736
1737const ArrayType *ASTContext::getAsArrayType(QualType T) {
1738 // Handle the non-qualified case efficiently.
1739 if (T.getCVRQualifiers() == 0) {
1740 // Handle the common positive case fast.
1741 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1742 return AT;
1743 }
1744
1745 // Handle the common negative case fast, ignoring CVR qualifiers.
1746 QualType CType = T->getCanonicalTypeInternal();
1747
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001748 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001749 // test.
1750 if (!isa<ArrayType>(CType) &&
1751 !isa<ArrayType>(CType.getUnqualifiedType()))
1752 return 0;
1753
1754 // Apply any CVR qualifiers from the array type to the element type. This
1755 // implements C99 6.7.3p8: "If the specification of an array type includes
1756 // any type qualifiers, the element type is so qualified, not the array type."
1757
1758 // If we get here, we either have type qualifiers on the type, or we have
1759 // sugar such as a typedef in the way. If we have type qualifiers on the type
1760 // we must propagate them down into the elemeng type.
1761 unsigned CVRQuals = T.getCVRQualifiers();
1762 unsigned AddrSpace = 0;
1763 Type *Ty = T.getTypePtr();
1764
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001765 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001766 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001767 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1768 AddrSpace = EXTQT->getAddressSpace();
1769 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001770 } else {
1771 T = Ty->getDesugaredType();
1772 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1773 break;
1774 CVRQuals |= T.getCVRQualifiers();
1775 Ty = T.getTypePtr();
1776 }
1777 }
1778
1779 // If we have a simple case, just return now.
1780 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1781 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1782 return ATy;
1783
1784 // Otherwise, we have an array and we have qualifiers on it. Push the
1785 // qualifiers into the array element type and return a new array type.
1786 // Get the canonical version of the element with the extra qualifiers on it.
1787 // This can recursively sink qualifiers through multiple levels of arrays.
1788 QualType NewEltTy = ATy->getElementType();
1789 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001790 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001791 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1792
1793 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1794 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1795 CAT->getSizeModifier(),
1796 CAT->getIndexTypeQualifier()));
1797 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1798 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1799 IAT->getSizeModifier(),
1800 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001801
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001802 if (const DependentSizedArrayType *DSAT
1803 = dyn_cast<DependentSizedArrayType>(ATy))
1804 return cast<ArrayType>(
1805 getDependentSizedArrayType(NewEltTy,
1806 DSAT->getSizeExpr(),
1807 DSAT->getSizeModifier(),
1808 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001809
Chris Lattnera1923f62008-08-04 07:31:14 +00001810 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1811 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1812 VAT->getSizeModifier(),
1813 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001814}
1815
1816
Chris Lattner19eb97e2008-04-02 05:18:44 +00001817/// getArrayDecayedType - Return the properly qualified result of decaying the
1818/// specified array type to a pointer. This operation is non-trivial when
1819/// handling typedefs etc. The canonical type of "T" must be an array type,
1820/// this returns a pointer to a properly qualified element of the array.
1821///
1822/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1823QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001824 // Get the element type with 'getAsArrayType' so that we don't lose any
1825 // typedefs in the element type of the array. This also handles propagation
1826 // of type qualifiers from the array type into the element type if present
1827 // (C99 6.7.3p8).
1828 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1829 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001830
Chris Lattnera1923f62008-08-04 07:31:14 +00001831 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001832
1833 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001834 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001835}
1836
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001837QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001838 QualType ElemTy = VAT->getElementType();
1839
1840 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1841 return getBaseElementType(VAT);
1842
1843 return ElemTy;
1844}
1845
Chris Lattner4b009652007-07-25 00:24:17 +00001846/// getFloatingRank - Return a relative rank for floating point types.
1847/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001848static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001849 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001850 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001851
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001852 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001853 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001854 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001855 case BuiltinType::Float: return FloatRank;
1856 case BuiltinType::Double: return DoubleRank;
1857 case BuiltinType::LongDouble: return LongDoubleRank;
1858 }
1859}
1860
Steve Narofffa0c4532007-08-27 01:41:48 +00001861/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1862/// point or a complex type (based on typeDomain/typeSize).
1863/// 'typeDomain' is a real floating point or complex type.
1864/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001865QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1866 QualType Domain) const {
1867 FloatingRank EltRank = getFloatingRank(Size);
1868 if (Domain->isComplexType()) {
1869 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001870 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001871 case FloatRank: return FloatComplexTy;
1872 case DoubleRank: return DoubleComplexTy;
1873 case LongDoubleRank: return LongDoubleComplexTy;
1874 }
Chris Lattner4b009652007-07-25 00:24:17 +00001875 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001876
1877 assert(Domain->isRealFloatingType() && "Unknown domain!");
1878 switch (EltRank) {
1879 default: assert(0 && "getFloatingRank(): illegal value for rank");
1880 case FloatRank: return FloatTy;
1881 case DoubleRank: return DoubleTy;
1882 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001883 }
Chris Lattner4b009652007-07-25 00:24:17 +00001884}
1885
Chris Lattner51285d82008-04-06 23:55:33 +00001886/// getFloatingTypeOrder - Compare the rank of the two specified floating
1887/// point types, ignoring the domain of the type (i.e. 'double' ==
1888/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1889/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001890int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1891 FloatingRank LHSR = getFloatingRank(LHS);
1892 FloatingRank RHSR = getFloatingRank(RHS);
1893
1894 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001895 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001896 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001897 return 1;
1898 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001899}
1900
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001901/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1902/// routine will assert if passed a built-in type that isn't an integer or enum,
1903/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001904unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001905 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001906 if (EnumType* ET = dyn_cast<EnumType>(T))
1907 T = ET->getDecl()->getIntegerType().getTypePtr();
1908
1909 // There are two things which impact the integer rank: the width, and
1910 // the ordering of builtins. The builtin ordering is encoded in the
1911 // bottom three bits; the width is encoded in the bits above that.
1912 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1913 return FWIT->getWidth() << 3;
1914 }
1915
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001916 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001917 default: assert(0 && "getIntegerRank(): not a built-in integer");
1918 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001919 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001920 case BuiltinType::Char_S:
1921 case BuiltinType::Char_U:
1922 case BuiltinType::SChar:
1923 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001924 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001925 case BuiltinType::Short:
1926 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001927 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001928 case BuiltinType::Int:
1929 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001930 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001931 case BuiltinType::Long:
1932 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001933 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001934 case BuiltinType::LongLong:
1935 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001936 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001937 }
1938}
1939
Chris Lattner51285d82008-04-06 23:55:33 +00001940/// getIntegerTypeOrder - Returns the highest ranked integer type:
1941/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1942/// LHS < RHS, return -1.
1943int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001944 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1945 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001946 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001947
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001948 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1949 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001950
Chris Lattner51285d82008-04-06 23:55:33 +00001951 unsigned LHSRank = getIntegerRank(LHSC);
1952 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001953
Chris Lattner51285d82008-04-06 23:55:33 +00001954 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1955 if (LHSRank == RHSRank) return 0;
1956 return LHSRank > RHSRank ? 1 : -1;
1957 }
Chris Lattner4b009652007-07-25 00:24:17 +00001958
Chris Lattner51285d82008-04-06 23:55:33 +00001959 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1960 if (LHSUnsigned) {
1961 // If the unsigned [LHS] type is larger, return it.
1962 if (LHSRank >= RHSRank)
1963 return 1;
1964
1965 // If the signed type can represent all values of the unsigned type, it
1966 // wins. Because we are dealing with 2's complement and types that are
1967 // powers of two larger than each other, this is always safe.
1968 return -1;
1969 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001970
Chris Lattner51285d82008-04-06 23:55:33 +00001971 // If the unsigned [RHS] type is larger, return it.
1972 if (RHSRank >= LHSRank)
1973 return -1;
1974
1975 // If the signed type can represent all values of the unsigned type, it
1976 // wins. Because we are dealing with 2's complement and types that are
1977 // powers of two larger than each other, this is always safe.
1978 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001979}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001980
1981// getCFConstantStringType - Return the type used for constant CFStrings.
1982QualType ASTContext::getCFConstantStringType() {
1983 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001984 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001985 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001986 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001987 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001988
1989 // const int *isa;
1990 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001991 // int flags;
1992 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001993 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001994 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001995 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001996 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001997
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001998 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001999 for (unsigned i = 0; i < 4; ++i) {
2000 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2001 SourceLocation(), 0,
2002 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002003 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002004 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002005 }
2006
2007 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002008 }
2009
2010 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00002011}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002012
Anders Carlssonf58cac72008-08-30 19:34:46 +00002013QualType ASTContext::getObjCFastEnumerationStateType()
2014{
2015 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00002016 ObjCFastEnumerationStateTypeDecl =
2017 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2018 &Idents.get("__objcFastEnumerationState"));
2019
Anders Carlssonf58cac72008-08-30 19:34:46 +00002020 QualType FieldTypes[] = {
2021 UnsignedLongTy,
2022 getPointerType(ObjCIdType),
2023 getPointerType(UnsignedLongTy),
2024 getConstantArrayType(UnsignedLongTy,
2025 llvm::APInt(32, 5), ArrayType::Normal, 0)
2026 };
2027
Douglas Gregor8acb7272008-12-11 16:49:14 +00002028 for (size_t i = 0; i < 4; ++i) {
2029 FieldDecl *Field = FieldDecl::Create(*this,
2030 ObjCFastEnumerationStateTypeDecl,
2031 SourceLocation(), 0,
2032 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002033 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002034 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002035 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00002036
Douglas Gregor8acb7272008-12-11 16:49:14 +00002037 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00002038 }
2039
2040 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2041}
2042
Anders Carlssone3f02572007-10-29 06:33:42 +00002043// This returns true if a type has been typedefed to BOOL:
2044// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00002045static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002046 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00002047 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2048 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002049
2050 return false;
2051}
2052
Ted Kremenek42730c52008-01-07 19:49:32 +00002053/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002054/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00002055int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00002056 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002057
2058 // Make all integer and enum types at least as large as an int
2059 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002060 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002061 // Treat arrays as pointers, since that's how they're passed in.
2062 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002063 sz = getTypeSize(VoidPtrTy);
2064 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002065}
2066
Ted Kremenek42730c52008-01-07 19:49:32 +00002067/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002068/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002069void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00002070 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002071 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002072 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00002073 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002074 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002075 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002076 // Compute size of all parameters.
2077 // Start with computing size of a pointer in number of bytes.
2078 // FIXME: There might(should) be a better way of doing this computation!
2079 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002080 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002081 // The first two arguments (self and _cmd) are pointers; account for
2082 // their size.
2083 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002084 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2085 E = Decl->param_end(); PI != E; ++PI) {
2086 QualType PType = (*PI)->getType();
2087 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002088 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002089 ParmOffset += sz;
2090 }
2091 S += llvm::utostr(ParmOffset);
2092 S += "@0:";
2093 S += llvm::utostr(PtrSize);
2094
2095 // Argument types.
2096 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002097 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2098 E = Decl->param_end(); PI != E; ++PI) {
2099 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002100 QualType PType = PVDecl->getOriginalType();
2101 if (const ArrayType *AT =
Steve Naroff78380fb2009-04-14 00:03:58 +00002102 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2103 // Use array's original type only if it has known number of
2104 // elements.
Steve Naroff6777bf32009-04-14 00:40:09 +00002105 if (!isa<ConstantArrayType>(AT))
Steve Naroff78380fb2009-04-14 00:03:58 +00002106 PType = PVDecl->getType();
2107 } else if (PType->isFunctionType())
2108 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002109 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002110 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002111 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002112 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002113 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002114 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002115 }
2116}
2117
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002118/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002119/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002120/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2121/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002122/// Property attributes are stored as a comma-delimited C string. The simple
2123/// attributes readonly and bycopy are encoded as single characters. The
2124/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2125/// encoded as single characters, followed by an identifier. Property types
2126/// are also encoded as a parametrized attribute. The characters used to encode
2127/// these attributes are defined by the following enumeration:
2128/// @code
2129/// enum PropertyAttributes {
2130/// kPropertyReadOnly = 'R', // property is read-only.
2131/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2132/// kPropertyByref = '&', // property is a reference to the value last assigned
2133/// kPropertyDynamic = 'D', // property is dynamic
2134/// kPropertyGetter = 'G', // followed by getter selector name
2135/// kPropertySetter = 'S', // followed by setter selector name
2136/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2137/// kPropertyType = 't' // followed by old-style type encoding.
2138/// kPropertyWeak = 'W' // 'weak' property
2139/// kPropertyStrong = 'P' // property GC'able
2140/// kPropertyNonAtomic = 'N' // property non-atomic
2141/// };
2142/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002143void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2144 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002145 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002146 // Collect information from the property implementation decl(s).
2147 bool Dynamic = false;
2148 ObjCPropertyImplDecl *SynthesizePID = 0;
2149
2150 // FIXME: Duplicated code due to poor abstraction.
2151 if (Container) {
2152 if (const ObjCCategoryImplDecl *CID =
2153 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2154 for (ObjCCategoryImplDecl::propimpl_iterator
2155 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2156 ObjCPropertyImplDecl *PID = *i;
2157 if (PID->getPropertyDecl() == PD) {
2158 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2159 Dynamic = true;
2160 } else {
2161 SynthesizePID = PID;
2162 }
2163 }
2164 }
2165 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002166 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002167 for (ObjCCategoryImplDecl::propimpl_iterator
2168 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2169 ObjCPropertyImplDecl *PID = *i;
2170 if (PID->getPropertyDecl() == PD) {
2171 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2172 Dynamic = true;
2173 } else {
2174 SynthesizePID = PID;
2175 }
2176 }
2177 }
2178 }
2179 }
2180
2181 // FIXME: This is not very efficient.
2182 S = "T";
2183
2184 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002185 // GCC has some special rules regarding encoding of properties which
2186 // closely resembles encoding of ivars.
Daniel Dunbar701c8502009-04-20 06:37:24 +00002187 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002188 true /* outermost type */,
2189 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002190
2191 if (PD->isReadOnly()) {
2192 S += ",R";
2193 } else {
2194 switch (PD->getSetterKind()) {
2195 case ObjCPropertyDecl::Assign: break;
2196 case ObjCPropertyDecl::Copy: S += ",C"; break;
2197 case ObjCPropertyDecl::Retain: S += ",&"; break;
2198 }
2199 }
2200
2201 // It really isn't clear at all what this means, since properties
2202 // are "dynamic by default".
2203 if (Dynamic)
2204 S += ",D";
2205
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002206 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2207 S += ",N";
2208
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002209 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2210 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002211 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002212 }
2213
2214 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2215 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002216 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002217 }
2218
2219 if (SynthesizePID) {
2220 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2221 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002222 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002223 }
2224
2225 // FIXME: OBJCGC: weak & strong
2226}
2227
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002228/// getLegacyIntegralTypeEncoding -
2229/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002230/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002231/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2232///
2233void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2234 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2235 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002236 if (BT->getKind() == BuiltinType::ULong &&
2237 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002238 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002239 else
2240 if (BT->getKind() == BuiltinType::Long &&
2241 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002242 PointeeTy = IntTy;
2243 }
2244 }
2245}
2246
Fariborz Jahanian248db262008-01-22 22:44:46 +00002247void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002248 const FieldDecl *Field) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002249 // We follow the behavior of gcc, expanding structures which are
2250 // directly pointed to, and expanding embedded structures. Note that
2251 // these rules are sufficient to prevent recursive encoding of the
2252 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002253 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2254 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002255}
2256
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002257static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002258 const FieldDecl *FD) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002259 const Expr *E = FD->getBitWidth();
2260 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2261 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2262 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2263 S += 'b';
2264 S += llvm::utostr(N);
2265}
2266
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002267void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2268 bool ExpandPointedToStructures,
2269 bool ExpandStructures,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002270 const FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002271 bool OutermostType,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002272 bool EncodingProperty) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002273 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002274 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002275 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002276 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002277 else {
2278 char encoding;
2279 switch (BT->getKind()) {
2280 default: assert(0 && "Unhandled builtin type kind");
2281 case BuiltinType::Void: encoding = 'v'; break;
2282 case BuiltinType::Bool: encoding = 'B'; break;
2283 case BuiltinType::Char_U:
2284 case BuiltinType::UChar: encoding = 'C'; break;
2285 case BuiltinType::UShort: encoding = 'S'; break;
2286 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002287 case BuiltinType::ULong:
2288 encoding =
2289 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2290 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002291 case BuiltinType::ULongLong: encoding = 'Q'; break;
2292 case BuiltinType::Char_S:
2293 case BuiltinType::SChar: encoding = 'c'; break;
2294 case BuiltinType::Short: encoding = 's'; break;
2295 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002296 case BuiltinType::Long:
2297 encoding =
2298 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2299 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002300 case BuiltinType::LongLong: encoding = 'q'; break;
2301 case BuiltinType::Float: encoding = 'f'; break;
2302 case BuiltinType::Double: encoding = 'd'; break;
2303 case BuiltinType::LongDouble: encoding = 'd'; break;
2304 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002305
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002306 S += encoding;
2307 }
Anders Carlsson70e16dd2009-04-09 21:55:45 +00002308 } else if (const ComplexType *CT = T->getAsComplexType()) {
2309 S += 'j';
2310 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2311 false);
2312 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002313 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2314 ExpandPointedToStructures,
2315 ExpandStructures, FD);
2316 if (FD || EncodingProperty) {
2317 // Note that we do extended encoding of protocol qualifer list
2318 // Only when doing ivar or property encoding.
2319 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2320 S += '"';
2321 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2322 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2323 S += '<';
2324 S += Proto->getNameAsString();
2325 S += '>';
2326 }
2327 S += '"';
2328 }
2329 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002330 }
2331 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002332 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002333 bool isReadOnly = false;
2334 // For historical/compatibility reasons, the read-only qualifier of the
2335 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2336 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2337 // Also, do not emit the 'r' for anything but the outermost type!
2338 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2339 if (OutermostType && T.isConstQualified()) {
2340 isReadOnly = true;
2341 S += 'r';
2342 }
2343 }
2344 else if (OutermostType) {
2345 QualType P = PointeeTy;
2346 while (P->getAsPointerType())
2347 P = P->getAsPointerType()->getPointeeType();
2348 if (P.isConstQualified()) {
2349 isReadOnly = true;
2350 S += 'r';
2351 }
2352 }
2353 if (isReadOnly) {
2354 // Another legacy compatibility encoding. Some ObjC qualifier and type
2355 // combinations need to be rearranged.
2356 // Rewrite "in const" from "nr" to "rn"
2357 const char * s = S.c_str();
2358 int len = S.length();
2359 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2360 std::string replace = "rn";
2361 S.replace(S.end()-2, S.end(), replace);
2362 }
2363 }
Steve Naroff17c03822009-02-12 17:52:19 +00002364 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002365 S += '@';
2366 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002367 }
2368 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002369 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002370 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002371 // Another historical/compatibility reason.
2372 // We encode the underlying type which comes out as
2373 // {...};
2374 S += '^';
2375 getObjCEncodingForTypeImpl(PointeeTy, S,
2376 false, ExpandPointedToStructures,
2377 NULL);
2378 return;
2379 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002380 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002381 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002382 const ObjCInterfaceType *OIT =
2383 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002384 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002385 S += '"';
2386 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002387 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2388 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2389 S += '<';
2390 S += Proto->getNameAsString();
2391 S += '>';
2392 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002393 S += '"';
2394 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002395 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002396 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002397 S += '#';
2398 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002399 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002400 S += ':';
2401 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002402 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002403
2404 if (PointeeTy->isCharType()) {
2405 // char pointer types should be encoded as '*' unless it is a
2406 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002407 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002408 S += '*';
2409 return;
2410 }
2411 }
2412
2413 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002414 getLegacyIntegralTypeEncoding(PointeeTy);
2415
2416 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002417 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002418 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002419 } else if (const ArrayType *AT =
2420 // Ignore type qualifiers etc.
2421 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002422 if (isa<IncompleteArrayType>(AT)) {
2423 // Incomplete arrays are encoded as a pointer to the array element.
2424 S += '^';
2425
2426 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2427 false, ExpandStructures, FD);
2428 } else {
2429 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002430
Anders Carlsson858c64d2009-02-22 01:38:57 +00002431 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2432 S += llvm::utostr(CAT->getSize().getZExtValue());
2433 else {
2434 //Variable length arrays are encoded as a regular array with 0 elements.
2435 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2436 S += '0';
2437 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002438
Anders Carlsson858c64d2009-02-22 01:38:57 +00002439 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2440 false, ExpandStructures, FD);
2441 S += ']';
2442 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002443 } else if (T->getAsFunctionType()) {
2444 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002445 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002446 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002447 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002448 // Anonymous structures print as '?'
2449 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2450 S += II->getName();
2451 } else {
2452 S += '?';
2453 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002454 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002455 S += '=';
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002456 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2457 FieldEnd = RDecl->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002458 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002459 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002460 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002461 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002462 S += '"';
2463 }
2464
2465 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002466 if (Field->isBitField()) {
2467 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2468 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002469 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002470 QualType qt = Field->getType();
2471 getLegacyIntegralTypeEncoding(qt);
2472 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002473 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002474 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002475 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002476 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002477 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002478 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002479 if (FD && FD->isBitField())
2480 EncodeBitField(this, S, FD);
2481 else
2482 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002483 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002484 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002485 } else if (T->isObjCInterfaceType()) {
2486 // @encode(class_name)
2487 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2488 S += '{';
2489 const IdentifierInfo *II = OI->getIdentifier();
2490 S += II->getName();
2491 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002492 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002493 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002494 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002495 if (RecFields[i]->isBitField())
2496 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2497 RecFields[i]);
2498 else
2499 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2500 FD);
2501 }
2502 S += '}';
2503 }
2504 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002505 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002506}
2507
Ted Kremenek42730c52008-01-07 19:49:32 +00002508void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002509 std::string& S) const {
2510 if (QT & Decl::OBJC_TQ_In)
2511 S += 'n';
2512 if (QT & Decl::OBJC_TQ_Inout)
2513 S += 'N';
2514 if (QT & Decl::OBJC_TQ_Out)
2515 S += 'o';
2516 if (QT & Decl::OBJC_TQ_Bycopy)
2517 S += 'O';
2518 if (QT & Decl::OBJC_TQ_Byref)
2519 S += 'R';
2520 if (QT & Decl::OBJC_TQ_Oneway)
2521 S += 'V';
2522}
2523
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002524void ASTContext::setBuiltinVaListType(QualType T)
2525{
2526 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2527
2528 BuiltinVaListType = T;
2529}
2530
Ted Kremenek42730c52008-01-07 19:49:32 +00002531void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002532{
Ted Kremenek42730c52008-01-07 19:49:32 +00002533 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002534
2535 // typedef struct objc_object *id;
2536 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002537 // User error - caller will issue diagnostics.
2538 if (!ptr)
2539 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002540 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002541 // User error - caller will issue diagnostics.
2542 if (!rec)
2543 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002544 IdStructType = rec;
2545}
2546
Ted Kremenek42730c52008-01-07 19:49:32 +00002547void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002548{
Ted Kremenek42730c52008-01-07 19:49:32 +00002549 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002550
2551 // typedef struct objc_selector *SEL;
2552 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002553 if (!ptr)
2554 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002555 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002556 if (!rec)
2557 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002558 SelStructType = rec;
2559}
2560
Ted Kremenek42730c52008-01-07 19:49:32 +00002561void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002562{
Ted Kremenek42730c52008-01-07 19:49:32 +00002563 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002564}
2565
Ted Kremenek42730c52008-01-07 19:49:32 +00002566void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002567{
Ted Kremenek42730c52008-01-07 19:49:32 +00002568 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002569
2570 // typedef struct objc_class *Class;
2571 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2572 assert(ptr && "'Class' incorrectly typed");
2573 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2574 assert(rec && "'Class' incorrectly typed");
2575 ClassStructType = rec;
2576}
2577
Ted Kremenek42730c52008-01-07 19:49:32 +00002578void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2579 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002580 "'NSConstantString' type already set!");
2581
Ted Kremenek42730c52008-01-07 19:49:32 +00002582 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002583}
2584
Douglas Gregordd13e842009-03-30 22:58:21 +00002585/// \brief Retrieve the template name that represents a qualified
2586/// template name such as \c std::vector.
2587TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2588 bool TemplateKeyword,
2589 TemplateDecl *Template) {
2590 llvm::FoldingSetNodeID ID;
2591 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2592
2593 void *InsertPos = 0;
2594 QualifiedTemplateName *QTN =
2595 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2596 if (!QTN) {
2597 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2598 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2599 }
2600
2601 return TemplateName(QTN);
2602}
2603
2604/// \brief Retrieve the template name that represents a dependent
2605/// template name such as \c MetaFun::template apply.
2606TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2607 const IdentifierInfo *Name) {
2608 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2609
2610 llvm::FoldingSetNodeID ID;
2611 DependentTemplateName::Profile(ID, NNS, Name);
2612
2613 void *InsertPos = 0;
2614 DependentTemplateName *QTN =
2615 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2616
2617 if (QTN)
2618 return TemplateName(QTN);
2619
2620 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2621 if (CanonNNS == NNS) {
2622 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2623 } else {
2624 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2625 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2626 }
2627
2628 DependentTemplateNames.InsertNode(QTN, InsertPos);
2629 return TemplateName(QTN);
2630}
2631
Douglas Gregorc6507e42008-11-03 14:12:49 +00002632/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002633/// TargetInfo, produce the corresponding type. The unsigned @p Type
2634/// is actually a value of type @c TargetInfo::IntType.
2635QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002636 switch (Type) {
2637 case TargetInfo::NoInt: return QualType();
2638 case TargetInfo::SignedShort: return ShortTy;
2639 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2640 case TargetInfo::SignedInt: return IntTy;
2641 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2642 case TargetInfo::SignedLong: return LongTy;
2643 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2644 case TargetInfo::SignedLongLong: return LongLongTy;
2645 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2646 }
2647
2648 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002649 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002650}
Ted Kremenek118930e2008-07-24 23:58:27 +00002651
2652//===----------------------------------------------------------------------===//
2653// Type Predicates.
2654//===----------------------------------------------------------------------===//
2655
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002656/// isObjCNSObjectType - Return true if this is an NSObject object using
2657/// NSObject attribute on a c-style pointer type.
2658/// FIXME - Make it work directly on types.
2659///
2660bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2661 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2662 if (TypedefDecl *TD = TDT->getDecl())
2663 if (TD->getAttr<ObjCNSObjectAttr>())
2664 return true;
2665 }
2666 return false;
2667}
2668
Ted Kremenek118930e2008-07-24 23:58:27 +00002669/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2670/// to an object type. This includes "id" and "Class" (two 'special' pointers
2671/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2672/// ID type).
2673bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002674 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002675 return true;
2676
Steve Naroffd9e00802008-10-21 18:24:04 +00002677 // Blocks are objects.
2678 if (Ty->isBlockPointerType())
2679 return true;
2680
2681 // All other object types are pointers.
Chris Lattnera008d172009-04-12 23:51:02 +00002682 const PointerType *PT = Ty->getAsPointerType();
2683 if (PT == 0)
Ted Kremenek118930e2008-07-24 23:58:27 +00002684 return false;
2685
Chris Lattnera008d172009-04-12 23:51:02 +00002686 // If this a pointer to an interface (e.g. NSString*), it is ok.
2687 if (PT->getPointeeType()->isObjCInterfaceType() ||
2688 // If is has NSObject attribute, OK as well.
2689 isObjCNSObjectType(Ty))
2690 return true;
2691
Ted Kremenek118930e2008-07-24 23:58:27 +00002692 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2693 // pointer types. This looks for the typedef specifically, not for the
Chris Lattnera008d172009-04-12 23:51:02 +00002694 // underlying type. Iteratively strip off typedefs so that we can handle
2695 // typedefs of typedefs.
2696 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2697 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2698 Ty.getUnqualifiedType() == getObjCClassType())
2699 return true;
2700
2701 Ty = TDT->getDecl()->getUnderlyingType();
2702 }
Ted Kremenek118930e2008-07-24 23:58:27 +00002703
Chris Lattnera008d172009-04-12 23:51:02 +00002704 return false;
Ted Kremenek118930e2008-07-24 23:58:27 +00002705}
2706
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002707/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2708/// garbage collection attribute.
2709///
2710QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002711 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002712 if (getLangOptions().ObjC1 &&
2713 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002714 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002715 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002716 // (or pointers to them) be treated as though they were declared
2717 // as __strong.
2718 if (GCAttrs == QualType::GCNone) {
2719 if (isObjCObjectPointerType(Ty))
2720 GCAttrs = QualType::Strong;
2721 else if (Ty->isPointerType())
2722 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2723 }
Fariborz Jahaniand7b01972009-04-11 00:00:54 +00002724 // Non-pointers have none gc'able attribute regardless of the attribute
2725 // set on them.
2726 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2727 return QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002728 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002729 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002730}
2731
Chris Lattner6ff358b2008-04-07 06:51:04 +00002732//===----------------------------------------------------------------------===//
2733// Type Compatibility Testing
2734//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002735
Steve Naroff3454b6c2008-09-04 15:10:53 +00002736/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002737/// block types. Types must be strictly compatible here. For example,
2738/// C unfortunately doesn't produce an error for the following:
2739///
2740/// int (*emptyArgFunc)();
2741/// int (*intArgList)(int) = emptyArgFunc;
2742///
2743/// For blocks, we will produce an error for the following (similar to C++):
2744///
2745/// int (^emptyArgBlock)();
2746/// int (^intArgBlock)(int) = emptyArgBlock;
2747///
2748/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2749///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002750bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002751 const FunctionType *lbase = lhs->getAsFunctionType();
2752 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002753 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2754 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stump896ceed2009-04-01 01:17:39 +00002755 if (lproto && rproto == 0)
2756 return false;
2757 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff3454b6c2008-09-04 15:10:53 +00002758}
2759
Chris Lattner6ff358b2008-04-07 06:51:04 +00002760/// areCompatVectorTypes - Return true if the two specified vector types are
2761/// compatible.
2762static bool areCompatVectorTypes(const VectorType *LHS,
2763 const VectorType *RHS) {
2764 assert(LHS->isCanonical() && RHS->isCanonical());
2765 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002766 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002767}
2768
Eli Friedman0d9549b2008-08-22 00:56:42 +00002769/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002770/// compatible for assignment from RHS to LHS. This handles validation of any
2771/// protocol qualifiers on the LHS or RHS.
2772///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002773bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2774 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002775 // Verify that the base decls are compatible: the RHS must be a subclass of
2776 // the LHS.
2777 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2778 return false;
2779
2780 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2781 // protocol qualified at all, then we are good.
2782 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2783 return true;
2784
2785 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2786 // isn't a superset.
2787 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2788 return true; // FIXME: should return false!
2789
2790 // Finally, we must have two protocol-qualified interfaces.
2791 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2792 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002793
Steve Naroff98e71b82009-03-01 16:12:44 +00002794 // All LHS protocols must have a presence on the RHS.
2795 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002796
Steve Naroff98e71b82009-03-01 16:12:44 +00002797 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2798 LHSPE = LHSP->qual_end();
2799 LHSPI != LHSPE; LHSPI++) {
2800 bool RHSImplementsProtocol = false;
2801
2802 // If the RHS doesn't implement the protocol on the left, the types
2803 // are incompatible.
2804 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2805 RHSPE = RHSP->qual_end();
2806 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2807 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2808 RHSImplementsProtocol = true;
2809 }
2810 // FIXME: For better diagnostics, consider passing back the protocol name.
2811 if (!RHSImplementsProtocol)
2812 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002813 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002814 // The RHS implements all protocols listed on the LHS.
2815 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002816}
2817
Steve Naroff17c03822009-02-12 17:52:19 +00002818bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2819 // get the "pointed to" types
2820 const PointerType *LHSPT = LHS->getAsPointerType();
2821 const PointerType *RHSPT = RHS->getAsPointerType();
2822
2823 if (!LHSPT || !RHSPT)
2824 return false;
2825
2826 QualType lhptee = LHSPT->getPointeeType();
2827 QualType rhptee = RHSPT->getPointeeType();
2828 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2829 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2830 // ID acts sort of like void* for ObjC interfaces
2831 if (LHSIface && isObjCIdStructType(rhptee))
2832 return true;
2833 if (RHSIface && isObjCIdStructType(lhptee))
2834 return true;
2835 if (!LHSIface || !RHSIface)
2836 return false;
2837 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2838 canAssignObjCInterfaces(RHSIface, LHSIface);
2839}
2840
Steve Naroff85f0dc52007-10-15 20:41:53 +00002841/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2842/// both shall have the identically qualified version of a compatible type.
2843/// C99 6.2.7p1: Two types have compatible types if their types are the
2844/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002845bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2846 return !mergeTypes(LHS, RHS).isNull();
2847}
2848
2849QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2850 const FunctionType *lbase = lhs->getAsFunctionType();
2851 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002852 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2853 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002854 bool allLTypes = true;
2855 bool allRTypes = true;
2856
2857 // Check return type
2858 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2859 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002860 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2861 allLTypes = false;
2862 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2863 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002864
2865 if (lproto && rproto) { // two C99 style function prototypes
2866 unsigned lproto_nargs = lproto->getNumArgs();
2867 unsigned rproto_nargs = rproto->getNumArgs();
2868
2869 // Compatible functions must have the same number of arguments
2870 if (lproto_nargs != rproto_nargs)
2871 return QualType();
2872
2873 // Variadic and non-variadic functions aren't compatible
2874 if (lproto->isVariadic() != rproto->isVariadic())
2875 return QualType();
2876
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002877 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2878 return QualType();
2879
Eli Friedman0d9549b2008-08-22 00:56:42 +00002880 // Check argument compatibility
2881 llvm::SmallVector<QualType, 10> types;
2882 for (unsigned i = 0; i < lproto_nargs; i++) {
2883 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2884 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2885 QualType argtype = mergeTypes(largtype, rargtype);
2886 if (argtype.isNull()) return QualType();
2887 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002888 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2889 allLTypes = false;
2890 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2891 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002892 }
2893 if (allLTypes) return lhs;
2894 if (allRTypes) return rhs;
2895 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002896 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002897 }
2898
2899 if (lproto) allRTypes = false;
2900 if (rproto) allLTypes = false;
2901
Douglas Gregor4fa58902009-02-26 23:50:07 +00002902 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002903 if (proto) {
2904 if (proto->isVariadic()) return QualType();
2905 // Check that the types are compatible with the types that
2906 // would result from default argument promotions (C99 6.7.5.3p15).
2907 // The only types actually affected are promotable integer
2908 // types and floats, which would be passed as a different
2909 // type depending on whether the prototype is visible.
2910 unsigned proto_nargs = proto->getNumArgs();
2911 for (unsigned i = 0; i < proto_nargs; ++i) {
2912 QualType argTy = proto->getArgType(i);
2913 if (argTy->isPromotableIntegerType() ||
2914 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2915 return QualType();
2916 }
2917
2918 if (allLTypes) return lhs;
2919 if (allRTypes) return rhs;
2920 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002921 proto->getNumArgs(), lproto->isVariadic(),
2922 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002923 }
2924
2925 if (allLTypes) return lhs;
2926 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002927 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002928}
2929
2930QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002931 // C++ [expr]: If an expression initially has the type "reference to T", the
2932 // type is adjusted to "T" prior to any further analysis, the expression
2933 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002934 // expression is an lvalue unless the reference is an rvalue reference and
2935 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002936 // FIXME: C++ shouldn't be going through here! The rules are different
2937 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00002938 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2939 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002940 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002941 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002942 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002943 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002944
Eli Friedman0d9549b2008-08-22 00:56:42 +00002945 QualType LHSCan = getCanonicalType(LHS),
2946 RHSCan = getCanonicalType(RHS);
2947
2948 // If two types are identical, they are compatible.
2949 if (LHSCan == RHSCan)
2950 return LHS;
2951
2952 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002953 // Note that we handle extended qualifiers later, in the
2954 // case for ExtQualType.
2955 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002956 return QualType();
2957
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00002958 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
2959 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002960
Chris Lattnerc38d4522008-01-14 05:45:46 +00002961 // We want to consider the two function types to be the same for these
2962 // comparisons, just force one to the other.
2963 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2964 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002965
2966 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002967 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2968 LHSClass = Type::ConstantArray;
2969 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2970 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002971
Nate Begemanaf6ed502008-04-18 23:10:10 +00002972 // Canonicalize ExtVector -> Vector.
2973 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2974 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002975
Chris Lattner7cdcb252008-04-07 06:38:24 +00002976 // Consider qualified interfaces and interfaces the same.
2977 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2978 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002979
Chris Lattnerb5709e22008-04-07 05:43:21 +00002980 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002981 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002982 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2983 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00002984
Steve Naroff0773c582009-04-14 15:11:46 +00002985 // 'id' and 'Class' act sort of like void* for ObjC interfaces
2986 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00002987 return LHS;
Steve Naroff0773c582009-04-14 15:11:46 +00002988 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00002989 return RHS;
2990
Steve Naroff28ceff72008-12-10 22:14:21 +00002991 // ID is compatible with all qualified id types.
2992 if (LHS->isObjCQualifiedIdType()) {
2993 if (const PointerType *PT = RHS->getAsPointerType()) {
2994 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00002995 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002996 return LHS;
2997 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2998 // Unfortunately, this API is part of Sema (which we don't have access
2999 // to. Need to refactor. The following check is insufficient, since we
3000 // need to make sure the class implements the protocol.
3001 if (pType->isObjCInterfaceType())
3002 return LHS;
3003 }
3004 }
3005 if (RHS->isObjCQualifiedIdType()) {
3006 if (const PointerType *PT = LHS->getAsPointerType()) {
3007 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00003008 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00003009 return RHS;
3010 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3011 // Unfortunately, this API is part of Sema (which we don't have access
3012 // to. Need to refactor. The following check is insufficient, since we
3013 // need to make sure the class implements the protocol.
3014 if (pType->isObjCInterfaceType())
3015 return RHS;
3016 }
3017 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003018 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3019 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003020 if (const EnumType* ETy = LHS->getAsEnumType()) {
3021 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3022 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003023 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003024 if (const EnumType* ETy = RHS->getAsEnumType()) {
3025 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3026 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003027 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003028
Eli Friedman0d9549b2008-08-22 00:56:42 +00003029 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003030 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003031
Steve Naroffc88babe2008-01-09 22:43:08 +00003032 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003033 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00003034#define TYPE(Class, Base)
3035#define ABSTRACT_TYPE(Class, Base)
3036#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3037#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3038#include "clang/AST/TypeNodes.def"
3039 assert(false && "Non-canonical and dependent types shouldn't get here");
3040 return QualType();
3041
Sebastian Redlce6fff02009-03-16 23:22:08 +00003042 case Type::LValueReference:
3043 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003044 case Type::MemberPointer:
3045 assert(false && "C++ should never be in mergeTypes");
3046 return QualType();
3047
3048 case Type::IncompleteArray:
3049 case Type::VariableArray:
3050 case Type::FunctionProto:
3051 case Type::ExtVector:
3052 case Type::ObjCQualifiedInterface:
3053 assert(false && "Types are eliminated above");
3054 return QualType();
3055
Chris Lattnerc38d4522008-01-14 05:45:46 +00003056 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003057 {
3058 // Merge two pointer types, while trying to preserve typedef info
3059 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3060 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3061 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3062 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003063 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3064 return LHS;
3065 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3066 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003067 return getPointerType(ResultType);
3068 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003069 case Type::BlockPointer:
3070 {
3071 // Merge two block pointer types, while trying to preserve typedef info
3072 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3073 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3074 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3075 if (ResultType.isNull()) return QualType();
3076 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3077 return LHS;
3078 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3079 return RHS;
3080 return getBlockPointerType(ResultType);
3081 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003082 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003083 {
3084 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3085 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3086 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3087 return QualType();
3088
3089 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3090 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3091 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3092 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003093 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3094 return LHS;
3095 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3096 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003097 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3098 ArrayType::ArraySizeModifier(), 0);
3099 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3100 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003101 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3102 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003103 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3104 return LHS;
3105 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3106 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003107 if (LVAT) {
3108 // FIXME: This isn't correct! But tricky to implement because
3109 // the array's size has to be the size of LHS, but the type
3110 // has to be different.
3111 return LHS;
3112 }
3113 if (RVAT) {
3114 // FIXME: This isn't correct! But tricky to implement because
3115 // the array's size has to be the size of RHS, but the type
3116 // has to be different.
3117 return RHS;
3118 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003119 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3120 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003121 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003122 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003123 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003124 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003125 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003126 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003127 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003128 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3129 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003130 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003131 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003132 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003133 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003134 case Type::Complex:
3135 // Distinct complex types are incompatible.
3136 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003137 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003138 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003139 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3140 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003141 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003142 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003143 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003144 // FIXME: This should be type compatibility, e.g. whether
3145 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003146 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3147 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3148 if (LHSIface && RHSIface &&
3149 canAssignObjCInterfaces(LHSIface, RHSIface))
3150 return LHS;
3151
Eli Friedman0d9549b2008-08-22 00:56:42 +00003152 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003153 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003154 case Type::ObjCQualifiedId:
3155 // Distinct qualified id's are not compatible.
3156 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003157 case Type::FixedWidthInt:
3158 // Distinct fixed-width integers are not compatible.
3159 return QualType();
3160 case Type::ObjCQualifiedClass:
3161 // Distinct qualified classes are not compatible.
3162 return QualType();
3163 case Type::ExtQual:
3164 // FIXME: ExtQual types can be compatible even if they're not
3165 // identical!
3166 return QualType();
3167 // First attempt at an implementation, but I'm not really sure it's
3168 // right...
3169#if 0
3170 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3171 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3172 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3173 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3174 return QualType();
3175 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3176 LHSBase = QualType(LQual->getBaseType(), 0);
3177 RHSBase = QualType(RQual->getBaseType(), 0);
3178 ResultType = mergeTypes(LHSBase, RHSBase);
3179 if (ResultType.isNull()) return QualType();
3180 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3181 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3182 return LHS;
3183 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3184 return RHS;
3185 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3186 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3187 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3188 return ResultType;
3189#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003190
3191 case Type::TemplateSpecialization:
3192 assert(false && "Dependent types have no size");
3193 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003194 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003195
3196 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003197}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003198
Chris Lattner1d78a862008-04-07 07:01:58 +00003199//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003200// Integer Predicates
3201//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003202
Eli Friedman0832dbc2008-06-28 06:23:08 +00003203unsigned ASTContext::getIntWidth(QualType T) {
3204 if (T == BoolTy)
3205 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003206 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3207 return FWIT->getWidth();
3208 }
3209 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003210 return (unsigned)getTypeSize(T);
3211}
3212
3213QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3214 assert(T->isSignedIntegerType() && "Unexpected type");
3215 if (const EnumType* ETy = T->getAsEnumType())
3216 T = ETy->getDecl()->getIntegerType();
3217 const BuiltinType* BTy = T->getAsBuiltinType();
3218 assert (BTy && "Unexpected signed integer type");
3219 switch (BTy->getKind()) {
3220 case BuiltinType::Char_S:
3221 case BuiltinType::SChar:
3222 return UnsignedCharTy;
3223 case BuiltinType::Short:
3224 return UnsignedShortTy;
3225 case BuiltinType::Int:
3226 return UnsignedIntTy;
3227 case BuiltinType::Long:
3228 return UnsignedLongTy;
3229 case BuiltinType::LongLong:
3230 return UnsignedLongLongTy;
3231 default:
3232 assert(0 && "Unexpected signed integer type");
3233 return QualType();
3234 }
3235}
3236
3237
3238//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00003239// Serialization Support
3240//===----------------------------------------------------------------------===//
3241
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003242enum {
3243 BasicMetadataBlock = 1,
3244 ASTContextBlock = 2,
3245 DeclsBlock = 3
3246};
3247
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003248void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3249 // Create bitstream.
3250 llvm::BitstreamWriter Stream(Buffer);
3251
3252 // Emit the preamble.
3253 Stream.Emit((unsigned)'B', 8);
3254 Stream.Emit((unsigned)'C', 8);
3255 Stream.Emit(0xC, 4);
3256 Stream.Emit(0xF, 4);
3257 Stream.Emit(0xE, 4);
3258 Stream.Emit(0x0, 4);
3259
3260 // Create serializer.
3261 llvm::Serializer S(Stream);
3262
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003263 // ===---------------------------------------------------===/
3264 // Serialize the "Translation Unit" metadata.
3265 // ===---------------------------------------------------===/
3266
3267 // Emit ASTContext.
3268 S.EnterBlock(ASTContextBlock);
3269 S.EmitOwnedPtr(this);
3270 S.ExitBlock(); // exit "ASTContextBlock"
3271
3272 S.EnterBlock(BasicMetadataBlock);
3273
3274 // Block for SourceManager and Target. Allows easy skipping
3275 // around to the block for the Selectors during deserialization.
3276 S.EnterBlock();
3277
3278 // Emit the SourceManager.
3279 S.Emit(getSourceManager());
3280
3281 // Emit the Target.
3282 S.EmitPtr(&Target);
3283 S.EmitCStr(Target.getTargetTriple());
3284
3285 S.ExitBlock(); // exit "SourceManager and Target Block"
3286
3287 // Emit the Selectors.
3288 S.Emit(Selectors);
3289
3290 // Emit the Identifier Table.
3291 S.Emit(Idents);
3292
3293 S.ExitBlock(); // exit "BasicMetadataBlock"
3294}
3295
3296
Ted Kremenek738e6c02007-10-31 17:10:13 +00003297/// Emit - Serialize an ASTContext object to Bitcode.
3298void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00003299 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00003300 S.EmitRef(SourceMgr);
3301 S.EmitRef(Target);
3302 S.EmitRef(Idents);
3303 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003304
Ted Kremenek68228a92007-10-31 22:44:07 +00003305 // Emit the size of the type vector so that we can reserve that size
3306 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003307 S.EmitInt(Types.size());
3308
Ted Kremenek034a78c2007-11-13 22:02:55 +00003309 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3310 I!=E;++I)
3311 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003312
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003313 S.EmitOwnedPtr(TUDecl);
3314
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003315 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003316}
3317
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003318
3319ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3320 FileManager &FMgr) {
3321 // Check if the file is of the proper length.
3322 if (Buffer.getBufferSize() & 0x3) {
3323 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3324 return 0;
3325 }
3326
3327 // Create the bitstream reader.
3328 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3329 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3330
3331 if (Stream.Read(8) != 'B' ||
3332 Stream.Read(8) != 'C' ||
3333 Stream.Read(4) != 0xC ||
3334 Stream.Read(4) != 0xF ||
3335 Stream.Read(4) != 0xE ||
3336 Stream.Read(4) != 0x0) {
3337 // FIXME: Provide diagnostic.
3338 return NULL;
3339 }
3340
3341 // Create the deserializer.
3342 llvm::Deserializer Dezr(Stream);
3343
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003344 // ===---------------------------------------------------===/
3345 // Deserialize the "Translation Unit" metadata.
3346 // ===---------------------------------------------------===/
3347
3348 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3349 // (which will appear earlier) and record its location.
3350
3351 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3352 assert (FoundBlock);
3353
3354 llvm::Deserializer::Location ASTContextBlockLoc =
3355 Dezr.getCurrentBlockLocation();
3356
3357 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3358 assert (FoundBlock);
3359
3360 // Read the SourceManager.
3361 SourceManager::CreateAndRegister(Dezr, FMgr);
3362
3363 { // Read the TargetInfo.
3364 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3365 char* triple = Dezr.ReadCStr(NULL,0,true);
3366 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3367 delete [] triple;
3368 }
3369
3370 // For Selectors, we must read the identifier table first because the
3371 // SelectorTable depends on the identifiers being already deserialized.
3372 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3373 Dezr.SkipBlock();
3374
3375 // Read the identifier table.
3376 IdentifierTable::CreateAndRegister(Dezr);
3377
3378 // Now jump back and read the selectors.
3379 Dezr.JumpTo(SelectorBlkLoc);
3380 SelectorTable::CreateAndRegister(Dezr);
3381
3382 // Now jump back to ASTContextBlock and read the ASTContext.
3383 Dezr.JumpTo(ASTContextBlockLoc);
3384 return Dezr.ReadOwnedPtr<ASTContext>();
3385}
3386
Ted Kremenekacba3612007-11-13 00:25:37 +00003387ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00003388
3389 // Read the language options.
3390 LangOptions LOpts;
3391 LOpts.Read(D);
3392
Ted Kremenek68228a92007-10-31 22:44:07 +00003393 SourceManager &SM = D.ReadRef<SourceManager>();
3394 TargetInfo &t = D.ReadRef<TargetInfo>();
3395 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3396 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00003397
Ted Kremenek68228a92007-10-31 22:44:07 +00003398 unsigned size_reserve = D.ReadInt();
3399
Douglas Gregor24afd4a2008-11-17 14:58:09 +00003400 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3401 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00003402
Ted Kremenek034a78c2007-11-13 22:02:55 +00003403 for (unsigned i = 0; i < size_reserve; ++i)
3404 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00003405
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003406 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3407
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003408 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00003409
3410 return A;
3411}
Douglas Gregorc34897d2009-04-09 22:27:44 +00003412
3413ExternalASTSource::~ExternalASTSource() { }
3414
3415void ExternalASTSource::PrintStats() { }