blob: af7cf124ef290e99abdfd24200b3a12e25dd99bf [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.
Daniel Dunbarbe1ff272009-04-22 04:34:53 +00001386QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001387 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1388
Daniel Dunbarbe1ff272009-04-22 04:34:53 +00001389 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1390 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001391 Types.push_back(Decl->TypeForDecl);
1392 return QualType(Decl->TypeForDecl, 0);
1393}
1394
Douglas Gregora4918772009-02-05 23:33:38 +00001395/// \brief Retrieve the template type parameter type for a template
1396/// parameter with the given depth, index, and (optionally) name.
1397QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1398 IdentifierInfo *Name) {
1399 llvm::FoldingSetNodeID ID;
1400 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1401 void *InsertPos = 0;
1402 TemplateTypeParmType *TypeParm
1403 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1404
1405 if (TypeParm)
1406 return QualType(TypeParm, 0);
1407
1408 if (Name)
1409 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1410 getTemplateTypeParmType(Depth, Index));
1411 else
1412 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1413
1414 Types.push_back(TypeParm);
1415 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1416
1417 return QualType(TypeParm, 0);
1418}
1419
Douglas Gregor8e458f42009-02-09 18:46:07 +00001420QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001421ASTContext::getTemplateSpecializationType(TemplateName Template,
1422 const TemplateArgument *Args,
1423 unsigned NumArgs,
1424 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001425 if (!Canon.isNull())
1426 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001427
Douglas Gregor8e458f42009-02-09 18:46:07 +00001428 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001429 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001430
Douglas Gregor8e458f42009-02-09 18:46:07 +00001431 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001432 TemplateSpecializationType *Spec
1433 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001434
1435 if (Spec)
1436 return QualType(Spec, 0);
1437
Douglas Gregordd13e842009-03-30 22:58:21 +00001438 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001439 sizeof(TemplateArgument) * NumArgs),
1440 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001441 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001442 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001443 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001444
1445 return QualType(Spec, 0);
1446}
1447
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001448QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001449ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001450 QualType NamedType) {
1451 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001452 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001453
1454 void *InsertPos = 0;
1455 QualifiedNameType *T
1456 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1457 if (T)
1458 return QualType(T, 0);
1459
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001460 T = new (*this) QualifiedNameType(NNS, NamedType,
1461 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001462 Types.push_back(T);
1463 QualifiedNameTypes.InsertNode(T, InsertPos);
1464 return QualType(T, 0);
1465}
1466
Douglas Gregord3022602009-03-27 23:10:48 +00001467QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1468 const IdentifierInfo *Name,
1469 QualType Canon) {
1470 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1471
1472 if (Canon.isNull()) {
1473 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1474 if (CanonNNS != NNS)
1475 Canon = getTypenameType(CanonNNS, Name);
1476 }
1477
1478 llvm::FoldingSetNodeID ID;
1479 TypenameType::Profile(ID, NNS, Name);
1480
1481 void *InsertPos = 0;
1482 TypenameType *T
1483 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1484 if (T)
1485 return QualType(T, 0);
1486
1487 T = new (*this) TypenameType(NNS, Name, Canon);
1488 Types.push_back(T);
1489 TypenameTypes.InsertNode(T, InsertPos);
1490 return QualType(T, 0);
1491}
1492
Douglas Gregor77da5802009-04-01 00:28:59 +00001493QualType
1494ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1495 const TemplateSpecializationType *TemplateId,
1496 QualType Canon) {
1497 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1498
1499 if (Canon.isNull()) {
1500 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1501 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1502 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1503 const TemplateSpecializationType *CanonTemplateId
1504 = CanonType->getAsTemplateSpecializationType();
1505 assert(CanonTemplateId &&
1506 "Canonical type must also be a template specialization type");
1507 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1508 }
1509 }
1510
1511 llvm::FoldingSetNodeID ID;
1512 TypenameType::Profile(ID, NNS, TemplateId);
1513
1514 void *InsertPos = 0;
1515 TypenameType *T
1516 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1517 if (T)
1518 return QualType(T, 0);
1519
1520 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1521 Types.push_back(T);
1522 TypenameTypes.InsertNode(T, InsertPos);
1523 return QualType(T, 0);
1524}
1525
Chris Lattnere1352302008-04-07 04:56:42 +00001526/// CmpProtocolNames - Comparison predicate for sorting protocols
1527/// alphabetically.
1528static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1529 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001530 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001531}
1532
1533static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1534 unsigned &NumProtocols) {
1535 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1536
1537 // Sort protocols, keyed by name.
1538 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1539
1540 // Remove duplicates.
1541 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1542 NumProtocols = ProtocolsEnd-Protocols;
1543}
1544
1545
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001546/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1547/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001548QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1549 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001550 // Sort the protocol list alphabetically to canonicalize it.
1551 SortAndUniqueProtocols(Protocols, NumProtocols);
1552
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001553 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001554 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001555
1556 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001557 if (ObjCQualifiedInterfaceType *QT =
1558 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001559 return QualType(QT, 0);
1560
1561 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001562 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001563 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001564
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001565 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001566 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001567 return QualType(QType, 0);
1568}
1569
Chris Lattnere1352302008-04-07 04:56:42 +00001570/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1571/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001572QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001573 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001574 // Sort the protocol list alphabetically to canonicalize it.
1575 SortAndUniqueProtocols(Protocols, NumProtocols);
1576
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001577 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001578 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001579
1580 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001581 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001582 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001583 return QualType(QT, 0);
1584
1585 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001586 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001587 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001588 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001589 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001590 return QualType(QType, 0);
1591}
1592
Douglas Gregor4fa58902009-02-26 23:50:07 +00001593/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1594/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001595/// multiple declarations that refer to "typeof(x)" all contain different
1596/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1597/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001598QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001599 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001600 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001601 Types.push_back(toe);
1602 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001603}
1604
Steve Naroff0604dd92007-08-01 18:02:17 +00001605/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1606/// TypeOfType AST's. The only motivation to unique these nodes would be
1607/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1608/// an issue. This doesn't effect the type checker, since it operates
1609/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001610QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001611 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001612 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001613 Types.push_back(tot);
1614 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001615}
1616
Chris Lattner4b009652007-07-25 00:24:17 +00001617/// getTagDeclType - Return the unique reference to the type for the
1618/// specified TagDecl (struct/union/class/enum) decl.
1619QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001620 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001621 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001622}
1623
1624/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1625/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1626/// needs to agree with the definition in <stddef.h>.
1627QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001628 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001629}
1630
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001631/// getSignedWCharType - Return the type of "signed wchar_t".
1632/// Used when in C++, as a GCC extension.
1633QualType ASTContext::getSignedWCharType() const {
1634 // FIXME: derive from "Target" ?
1635 return WCharTy;
1636}
1637
1638/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1639/// Used when in C++, as a GCC extension.
1640QualType ASTContext::getUnsignedWCharType() const {
1641 // FIXME: derive from "Target" ?
1642 return UnsignedIntTy;
1643}
1644
Chris Lattner4b009652007-07-25 00:24:17 +00001645/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1646/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1647QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001648 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001649}
1650
Chris Lattner19eb97e2008-04-02 05:18:44 +00001651//===----------------------------------------------------------------------===//
1652// Type Operators
1653//===----------------------------------------------------------------------===//
1654
Chris Lattner3dae6f42008-04-06 22:41:35 +00001655/// getCanonicalType - Return the canonical (structural) type corresponding to
1656/// the specified potentially non-canonical type. The non-canonical version
1657/// of a type may have many "decorated" versions of types. Decorators can
1658/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1659/// to be free of any of these, allowing two canonical types to be compared
1660/// for exact equality with a simple pointer comparison.
1661QualType ASTContext::getCanonicalType(QualType T) {
1662 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001663
1664 // If the result has type qualifiers, make sure to canonicalize them as well.
1665 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1666 if (TypeQuals == 0) return CanType;
1667
1668 // If the type qualifiers are on an array type, get the canonical type of the
1669 // array with the qualifiers applied to the element type.
1670 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1671 if (!AT)
1672 return CanType.getQualifiedType(TypeQuals);
1673
1674 // Get the canonical version of the element with the extra qualifiers on it.
1675 // This can recursively sink qualifiers through multiple levels of arrays.
1676 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1677 NewEltTy = getCanonicalType(NewEltTy);
1678
1679 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1680 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1681 CAT->getIndexTypeQualifier());
1682 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1683 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1684 IAT->getIndexTypeQualifier());
1685
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001686 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1687 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1688 DSAT->getSizeModifier(),
1689 DSAT->getIndexTypeQualifier());
1690
Chris Lattnera1923f62008-08-04 07:31:14 +00001691 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1692 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1693 VAT->getSizeModifier(),
1694 VAT->getIndexTypeQualifier());
1695}
1696
Douglas Gregord3022602009-03-27 23:10:48 +00001697NestedNameSpecifier *
1698ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1699 if (!NNS)
1700 return 0;
1701
1702 switch (NNS->getKind()) {
1703 case NestedNameSpecifier::Identifier:
1704 // Canonicalize the prefix but keep the identifier the same.
1705 return NestedNameSpecifier::Create(*this,
1706 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1707 NNS->getAsIdentifier());
1708
1709 case NestedNameSpecifier::Namespace:
1710 // A namespace is canonical; build a nested-name-specifier with
1711 // this namespace and no prefix.
1712 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1713
1714 case NestedNameSpecifier::TypeSpec:
1715 case NestedNameSpecifier::TypeSpecWithTemplate: {
1716 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1717 NestedNameSpecifier *Prefix = 0;
1718
1719 // FIXME: This isn't the right check!
1720 if (T->isDependentType())
1721 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1722
1723 return NestedNameSpecifier::Create(*this, Prefix,
1724 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1725 T.getTypePtr());
1726 }
1727
1728 case NestedNameSpecifier::Global:
1729 // The global specifier is canonical and unique.
1730 return NNS;
1731 }
1732
1733 // Required to silence a GCC warning
1734 return 0;
1735}
1736
Chris Lattnera1923f62008-08-04 07:31:14 +00001737
1738const ArrayType *ASTContext::getAsArrayType(QualType T) {
1739 // Handle the non-qualified case efficiently.
1740 if (T.getCVRQualifiers() == 0) {
1741 // Handle the common positive case fast.
1742 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1743 return AT;
1744 }
1745
1746 // Handle the common negative case fast, ignoring CVR qualifiers.
1747 QualType CType = T->getCanonicalTypeInternal();
1748
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001749 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001750 // test.
1751 if (!isa<ArrayType>(CType) &&
1752 !isa<ArrayType>(CType.getUnqualifiedType()))
1753 return 0;
1754
1755 // Apply any CVR qualifiers from the array type to the element type. This
1756 // implements C99 6.7.3p8: "If the specification of an array type includes
1757 // any type qualifiers, the element type is so qualified, not the array type."
1758
1759 // If we get here, we either have type qualifiers on the type, or we have
1760 // sugar such as a typedef in the way. If we have type qualifiers on the type
1761 // we must propagate them down into the elemeng type.
1762 unsigned CVRQuals = T.getCVRQualifiers();
1763 unsigned AddrSpace = 0;
1764 Type *Ty = T.getTypePtr();
1765
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001766 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001767 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001768 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1769 AddrSpace = EXTQT->getAddressSpace();
1770 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001771 } else {
1772 T = Ty->getDesugaredType();
1773 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1774 break;
1775 CVRQuals |= T.getCVRQualifiers();
1776 Ty = T.getTypePtr();
1777 }
1778 }
1779
1780 // If we have a simple case, just return now.
1781 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1782 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1783 return ATy;
1784
1785 // Otherwise, we have an array and we have qualifiers on it. Push the
1786 // qualifiers into the array element type and return a new array type.
1787 // Get the canonical version of the element with the extra qualifiers on it.
1788 // This can recursively sink qualifiers through multiple levels of arrays.
1789 QualType NewEltTy = ATy->getElementType();
1790 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001791 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001792 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1793
1794 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1795 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1796 CAT->getSizeModifier(),
1797 CAT->getIndexTypeQualifier()));
1798 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1799 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1800 IAT->getSizeModifier(),
1801 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001802
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001803 if (const DependentSizedArrayType *DSAT
1804 = dyn_cast<DependentSizedArrayType>(ATy))
1805 return cast<ArrayType>(
1806 getDependentSizedArrayType(NewEltTy,
1807 DSAT->getSizeExpr(),
1808 DSAT->getSizeModifier(),
1809 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001810
Chris Lattnera1923f62008-08-04 07:31:14 +00001811 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1812 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1813 VAT->getSizeModifier(),
1814 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001815}
1816
1817
Chris Lattner19eb97e2008-04-02 05:18:44 +00001818/// getArrayDecayedType - Return the properly qualified result of decaying the
1819/// specified array type to a pointer. This operation is non-trivial when
1820/// handling typedefs etc. The canonical type of "T" must be an array type,
1821/// this returns a pointer to a properly qualified element of the array.
1822///
1823/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1824QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001825 // Get the element type with 'getAsArrayType' so that we don't lose any
1826 // typedefs in the element type of the array. This also handles propagation
1827 // of type qualifiers from the array type into the element type if present
1828 // (C99 6.7.3p8).
1829 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1830 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001831
Chris Lattnera1923f62008-08-04 07:31:14 +00001832 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001833
1834 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001835 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001836}
1837
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001838QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001839 QualType ElemTy = VAT->getElementType();
1840
1841 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1842 return getBaseElementType(VAT);
1843
1844 return ElemTy;
1845}
1846
Chris Lattner4b009652007-07-25 00:24:17 +00001847/// getFloatingRank - Return a relative rank for floating point types.
1848/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001849static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001850 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001851 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001852
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001853 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001854 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001855 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001856 case BuiltinType::Float: return FloatRank;
1857 case BuiltinType::Double: return DoubleRank;
1858 case BuiltinType::LongDouble: return LongDoubleRank;
1859 }
1860}
1861
Steve Narofffa0c4532007-08-27 01:41:48 +00001862/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1863/// point or a complex type (based on typeDomain/typeSize).
1864/// 'typeDomain' is a real floating point or complex type.
1865/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001866QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1867 QualType Domain) const {
1868 FloatingRank EltRank = getFloatingRank(Size);
1869 if (Domain->isComplexType()) {
1870 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001871 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001872 case FloatRank: return FloatComplexTy;
1873 case DoubleRank: return DoubleComplexTy;
1874 case LongDoubleRank: return LongDoubleComplexTy;
1875 }
Chris Lattner4b009652007-07-25 00:24:17 +00001876 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001877
1878 assert(Domain->isRealFloatingType() && "Unknown domain!");
1879 switch (EltRank) {
1880 default: assert(0 && "getFloatingRank(): illegal value for rank");
1881 case FloatRank: return FloatTy;
1882 case DoubleRank: return DoubleTy;
1883 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001884 }
Chris Lattner4b009652007-07-25 00:24:17 +00001885}
1886
Chris Lattner51285d82008-04-06 23:55:33 +00001887/// getFloatingTypeOrder - Compare the rank of the two specified floating
1888/// point types, ignoring the domain of the type (i.e. 'double' ==
1889/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1890/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001891int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1892 FloatingRank LHSR = getFloatingRank(LHS);
1893 FloatingRank RHSR = getFloatingRank(RHS);
1894
1895 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001896 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001897 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001898 return 1;
1899 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001900}
1901
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001902/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1903/// routine will assert if passed a built-in type that isn't an integer or enum,
1904/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001905unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001906 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001907 if (EnumType* ET = dyn_cast<EnumType>(T))
1908 T = ET->getDecl()->getIntegerType().getTypePtr();
1909
1910 // There are two things which impact the integer rank: the width, and
1911 // the ordering of builtins. The builtin ordering is encoded in the
1912 // bottom three bits; the width is encoded in the bits above that.
1913 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1914 return FWIT->getWidth() << 3;
1915 }
1916
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001917 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001918 default: assert(0 && "getIntegerRank(): not a built-in integer");
1919 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001920 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001921 case BuiltinType::Char_S:
1922 case BuiltinType::Char_U:
1923 case BuiltinType::SChar:
1924 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001925 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001926 case BuiltinType::Short:
1927 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001928 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001929 case BuiltinType::Int:
1930 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001931 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001932 case BuiltinType::Long:
1933 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001934 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001935 case BuiltinType::LongLong:
1936 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001937 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001938 }
1939}
1940
Chris Lattner51285d82008-04-06 23:55:33 +00001941/// getIntegerTypeOrder - Returns the highest ranked integer type:
1942/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1943/// LHS < RHS, return -1.
1944int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001945 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1946 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001947 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001948
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001949 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1950 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001951
Chris Lattner51285d82008-04-06 23:55:33 +00001952 unsigned LHSRank = getIntegerRank(LHSC);
1953 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001954
Chris Lattner51285d82008-04-06 23:55:33 +00001955 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1956 if (LHSRank == RHSRank) return 0;
1957 return LHSRank > RHSRank ? 1 : -1;
1958 }
Chris Lattner4b009652007-07-25 00:24:17 +00001959
Chris Lattner51285d82008-04-06 23:55:33 +00001960 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1961 if (LHSUnsigned) {
1962 // If the unsigned [LHS] type is larger, return it.
1963 if (LHSRank >= RHSRank)
1964 return 1;
1965
1966 // If the signed type can represent all values of the unsigned type, it
1967 // wins. Because we are dealing with 2's complement and types that are
1968 // powers of two larger than each other, this is always safe.
1969 return -1;
1970 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001971
Chris Lattner51285d82008-04-06 23:55:33 +00001972 // If the unsigned [RHS] type is larger, return it.
1973 if (RHSRank >= LHSRank)
1974 return -1;
1975
1976 // If the signed type can represent all values of the unsigned type, it
1977 // wins. Because we are dealing with 2's complement and types that are
1978 // powers of two larger than each other, this is always safe.
1979 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001980}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001981
1982// getCFConstantStringType - Return the type used for constant CFStrings.
1983QualType ASTContext::getCFConstantStringType() {
1984 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001985 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001986 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001987 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001988 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001989
1990 // const int *isa;
1991 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001992 // int flags;
1993 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001994 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001995 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001996 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001997 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001998
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001999 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00002000 for (unsigned i = 0; i < 4; ++i) {
2001 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
2002 SourceLocation(), 0,
2003 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002004 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002005 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002006 }
2007
2008 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00002009 }
2010
2011 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00002012}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002013
Anders Carlssonf58cac72008-08-30 19:34:46 +00002014QualType ASTContext::getObjCFastEnumerationStateType()
2015{
2016 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00002017 ObjCFastEnumerationStateTypeDecl =
2018 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
2019 &Idents.get("__objcFastEnumerationState"));
2020
Anders Carlssonf58cac72008-08-30 19:34:46 +00002021 QualType FieldTypes[] = {
2022 UnsignedLongTy,
2023 getPointerType(ObjCIdType),
2024 getPointerType(UnsignedLongTy),
2025 getConstantArrayType(UnsignedLongTy,
2026 llvm::APInt(32, 5), ArrayType::Normal, 0)
2027 };
2028
Douglas Gregor8acb7272008-12-11 16:49:14 +00002029 for (size_t i = 0; i < 4; ++i) {
2030 FieldDecl *Field = FieldDecl::Create(*this,
2031 ObjCFastEnumerationStateTypeDecl,
2032 SourceLocation(), 0,
2033 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002034 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002035 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002036 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00002037
Douglas Gregor8acb7272008-12-11 16:49:14 +00002038 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00002039 }
2040
2041 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2042}
2043
Anders Carlssone3f02572007-10-29 06:33:42 +00002044// This returns true if a type has been typedefed to BOOL:
2045// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00002046static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002047 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00002048 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2049 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002050
2051 return false;
2052}
2053
Ted Kremenek42730c52008-01-07 19:49:32 +00002054/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002055/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00002056int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00002057 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002058
2059 // Make all integer and enum types at least as large as an int
2060 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002061 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002062 // Treat arrays as pointers, since that's how they're passed in.
2063 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002064 sz = getTypeSize(VoidPtrTy);
2065 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002066}
2067
Ted Kremenek42730c52008-01-07 19:49:32 +00002068/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002069/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002070void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00002071 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002072 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002073 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00002074 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002075 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002076 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002077 // Compute size of all parameters.
2078 // Start with computing size of a pointer in number of bytes.
2079 // FIXME: There might(should) be a better way of doing this computation!
2080 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002081 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002082 // The first two arguments (self and _cmd) are pointers; account for
2083 // their size.
2084 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002085 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2086 E = Decl->param_end(); PI != E; ++PI) {
2087 QualType PType = (*PI)->getType();
2088 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002089 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002090 ParmOffset += sz;
2091 }
2092 S += llvm::utostr(ParmOffset);
2093 S += "@0:";
2094 S += llvm::utostr(PtrSize);
2095
2096 // Argument types.
2097 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002098 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2099 E = Decl->param_end(); PI != E; ++PI) {
2100 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002101 QualType PType = PVDecl->getOriginalType();
2102 if (const ArrayType *AT =
Steve Naroff78380fb2009-04-14 00:03:58 +00002103 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2104 // Use array's original type only if it has known number of
2105 // elements.
Steve Naroff6777bf32009-04-14 00:40:09 +00002106 if (!isa<ConstantArrayType>(AT))
Steve Naroff78380fb2009-04-14 00:03:58 +00002107 PType = PVDecl->getType();
2108 } else if (PType->isFunctionType())
2109 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002110 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002111 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002112 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002113 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002114 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002115 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002116 }
2117}
2118
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002119/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002120/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002121/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2122/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002123/// Property attributes are stored as a comma-delimited C string. The simple
2124/// attributes readonly and bycopy are encoded as single characters. The
2125/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2126/// encoded as single characters, followed by an identifier. Property types
2127/// are also encoded as a parametrized attribute. The characters used to encode
2128/// these attributes are defined by the following enumeration:
2129/// @code
2130/// enum PropertyAttributes {
2131/// kPropertyReadOnly = 'R', // property is read-only.
2132/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2133/// kPropertyByref = '&', // property is a reference to the value last assigned
2134/// kPropertyDynamic = 'D', // property is dynamic
2135/// kPropertyGetter = 'G', // followed by getter selector name
2136/// kPropertySetter = 'S', // followed by setter selector name
2137/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2138/// kPropertyType = 't' // followed by old-style type encoding.
2139/// kPropertyWeak = 'W' // 'weak' property
2140/// kPropertyStrong = 'P' // property GC'able
2141/// kPropertyNonAtomic = 'N' // property non-atomic
2142/// };
2143/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002144void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2145 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002146 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002147 // Collect information from the property implementation decl(s).
2148 bool Dynamic = false;
2149 ObjCPropertyImplDecl *SynthesizePID = 0;
2150
2151 // FIXME: Duplicated code due to poor abstraction.
2152 if (Container) {
2153 if (const ObjCCategoryImplDecl *CID =
2154 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2155 for (ObjCCategoryImplDecl::propimpl_iterator
2156 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2157 ObjCPropertyImplDecl *PID = *i;
2158 if (PID->getPropertyDecl() == PD) {
2159 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2160 Dynamic = true;
2161 } else {
2162 SynthesizePID = PID;
2163 }
2164 }
2165 }
2166 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002167 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002168 for (ObjCCategoryImplDecl::propimpl_iterator
2169 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2170 ObjCPropertyImplDecl *PID = *i;
2171 if (PID->getPropertyDecl() == PD) {
2172 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2173 Dynamic = true;
2174 } else {
2175 SynthesizePID = PID;
2176 }
2177 }
2178 }
2179 }
2180 }
2181
2182 // FIXME: This is not very efficient.
2183 S = "T";
2184
2185 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002186 // GCC has some special rules regarding encoding of properties which
2187 // closely resembles encoding of ivars.
Daniel Dunbar701c8502009-04-20 06:37:24 +00002188 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002189 true /* outermost type */,
2190 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002191
2192 if (PD->isReadOnly()) {
2193 S += ",R";
2194 } else {
2195 switch (PD->getSetterKind()) {
2196 case ObjCPropertyDecl::Assign: break;
2197 case ObjCPropertyDecl::Copy: S += ",C"; break;
2198 case ObjCPropertyDecl::Retain: S += ",&"; break;
2199 }
2200 }
2201
2202 // It really isn't clear at all what this means, since properties
2203 // are "dynamic by default".
2204 if (Dynamic)
2205 S += ",D";
2206
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002207 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2208 S += ",N";
2209
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002210 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2211 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002212 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002213 }
2214
2215 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2216 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002217 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002218 }
2219
2220 if (SynthesizePID) {
2221 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2222 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002223 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002224 }
2225
2226 // FIXME: OBJCGC: weak & strong
2227}
2228
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002229/// getLegacyIntegralTypeEncoding -
2230/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002231/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002232/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2233///
2234void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2235 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2236 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002237 if (BT->getKind() == BuiltinType::ULong &&
2238 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002239 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002240 else
2241 if (BT->getKind() == BuiltinType::Long &&
2242 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002243 PointeeTy = IntTy;
2244 }
2245 }
2246}
2247
Fariborz Jahanian248db262008-01-22 22:44:46 +00002248void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002249 const FieldDecl *Field) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002250 // We follow the behavior of gcc, expanding structures which are
2251 // directly pointed to, and expanding embedded structures. Note that
2252 // these rules are sufficient to prevent recursive encoding of the
2253 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002254 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2255 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002256}
2257
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002258static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002259 const FieldDecl *FD) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002260 const Expr *E = FD->getBitWidth();
2261 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2262 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2263 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2264 S += 'b';
2265 S += llvm::utostr(N);
2266}
2267
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002268void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2269 bool ExpandPointedToStructures,
2270 bool ExpandStructures,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002271 const FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002272 bool OutermostType,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002273 bool EncodingProperty) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002274 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002275 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002276 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002277 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002278 else {
2279 char encoding;
2280 switch (BT->getKind()) {
2281 default: assert(0 && "Unhandled builtin type kind");
2282 case BuiltinType::Void: encoding = 'v'; break;
2283 case BuiltinType::Bool: encoding = 'B'; break;
2284 case BuiltinType::Char_U:
2285 case BuiltinType::UChar: encoding = 'C'; break;
2286 case BuiltinType::UShort: encoding = 'S'; break;
2287 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002288 case BuiltinType::ULong:
2289 encoding =
2290 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2291 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002292 case BuiltinType::ULongLong: encoding = 'Q'; break;
2293 case BuiltinType::Char_S:
2294 case BuiltinType::SChar: encoding = 'c'; break;
2295 case BuiltinType::Short: encoding = 's'; break;
2296 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002297 case BuiltinType::Long:
2298 encoding =
2299 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2300 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002301 case BuiltinType::LongLong: encoding = 'q'; break;
2302 case BuiltinType::Float: encoding = 'f'; break;
2303 case BuiltinType::Double: encoding = 'd'; break;
2304 case BuiltinType::LongDouble: encoding = 'd'; break;
2305 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002306
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002307 S += encoding;
2308 }
Anders Carlsson70e16dd2009-04-09 21:55:45 +00002309 } else if (const ComplexType *CT = T->getAsComplexType()) {
2310 S += 'j';
2311 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2312 false);
2313 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002314 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2315 ExpandPointedToStructures,
2316 ExpandStructures, FD);
2317 if (FD || EncodingProperty) {
2318 // Note that we do extended encoding of protocol qualifer list
2319 // Only when doing ivar or property encoding.
2320 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2321 S += '"';
2322 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2323 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2324 S += '<';
2325 S += Proto->getNameAsString();
2326 S += '>';
2327 }
2328 S += '"';
2329 }
2330 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002331 }
2332 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002333 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002334 bool isReadOnly = false;
2335 // For historical/compatibility reasons, the read-only qualifier of the
2336 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2337 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2338 // Also, do not emit the 'r' for anything but the outermost type!
2339 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2340 if (OutermostType && T.isConstQualified()) {
2341 isReadOnly = true;
2342 S += 'r';
2343 }
2344 }
2345 else if (OutermostType) {
2346 QualType P = PointeeTy;
2347 while (P->getAsPointerType())
2348 P = P->getAsPointerType()->getPointeeType();
2349 if (P.isConstQualified()) {
2350 isReadOnly = true;
2351 S += 'r';
2352 }
2353 }
2354 if (isReadOnly) {
2355 // Another legacy compatibility encoding. Some ObjC qualifier and type
2356 // combinations need to be rearranged.
2357 // Rewrite "in const" from "nr" to "rn"
2358 const char * s = S.c_str();
2359 int len = S.length();
2360 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2361 std::string replace = "rn";
2362 S.replace(S.end()-2, S.end(), replace);
2363 }
2364 }
Steve Naroff17c03822009-02-12 17:52:19 +00002365 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002366 S += '@';
2367 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002368 }
2369 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002370 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002371 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002372 // Another historical/compatibility reason.
2373 // We encode the underlying type which comes out as
2374 // {...};
2375 S += '^';
2376 getObjCEncodingForTypeImpl(PointeeTy, S,
2377 false, ExpandPointedToStructures,
2378 NULL);
2379 return;
2380 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002381 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002382 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002383 const ObjCInterfaceType *OIT =
2384 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002385 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002386 S += '"';
2387 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002388 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2389 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2390 S += '<';
2391 S += Proto->getNameAsString();
2392 S += '>';
2393 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002394 S += '"';
2395 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002396 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002397 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002398 S += '#';
2399 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002400 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002401 S += ':';
2402 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002403 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002404
2405 if (PointeeTy->isCharType()) {
2406 // char pointer types should be encoded as '*' unless it is a
2407 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002408 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002409 S += '*';
2410 return;
2411 }
2412 }
2413
2414 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002415 getLegacyIntegralTypeEncoding(PointeeTy);
2416
2417 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002418 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002419 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002420 } else if (const ArrayType *AT =
2421 // Ignore type qualifiers etc.
2422 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002423 if (isa<IncompleteArrayType>(AT)) {
2424 // Incomplete arrays are encoded as a pointer to the array element.
2425 S += '^';
2426
2427 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2428 false, ExpandStructures, FD);
2429 } else {
2430 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002431
Anders Carlsson858c64d2009-02-22 01:38:57 +00002432 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2433 S += llvm::utostr(CAT->getSize().getZExtValue());
2434 else {
2435 //Variable length arrays are encoded as a regular array with 0 elements.
2436 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2437 S += '0';
2438 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002439
Anders Carlsson858c64d2009-02-22 01:38:57 +00002440 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2441 false, ExpandStructures, FD);
2442 S += ']';
2443 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002444 } else if (T->getAsFunctionType()) {
2445 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002446 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002447 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002448 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002449 // Anonymous structures print as '?'
2450 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2451 S += II->getName();
2452 } else {
2453 S += '?';
2454 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002455 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002456 S += '=';
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002457 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2458 FieldEnd = RDecl->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002459 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002460 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002461 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002462 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002463 S += '"';
2464 }
2465
2466 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002467 if (Field->isBitField()) {
2468 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2469 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002470 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002471 QualType qt = Field->getType();
2472 getLegacyIntegralTypeEncoding(qt);
2473 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002474 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002475 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002476 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002477 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002478 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002479 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002480 if (FD && FD->isBitField())
2481 EncodeBitField(this, S, FD);
2482 else
2483 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002484 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002485 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002486 } else if (T->isObjCInterfaceType()) {
2487 // @encode(class_name)
2488 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2489 S += '{';
2490 const IdentifierInfo *II = OI->getIdentifier();
2491 S += II->getName();
2492 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002493 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002494 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002495 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002496 if (RecFields[i]->isBitField())
2497 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2498 RecFields[i]);
2499 else
2500 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2501 FD);
2502 }
2503 S += '}';
2504 }
2505 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002506 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002507}
2508
Ted Kremenek42730c52008-01-07 19:49:32 +00002509void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002510 std::string& S) const {
2511 if (QT & Decl::OBJC_TQ_In)
2512 S += 'n';
2513 if (QT & Decl::OBJC_TQ_Inout)
2514 S += 'N';
2515 if (QT & Decl::OBJC_TQ_Out)
2516 S += 'o';
2517 if (QT & Decl::OBJC_TQ_Bycopy)
2518 S += 'O';
2519 if (QT & Decl::OBJC_TQ_Byref)
2520 S += 'R';
2521 if (QT & Decl::OBJC_TQ_Oneway)
2522 S += 'V';
2523}
2524
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002525void ASTContext::setBuiltinVaListType(QualType T)
2526{
2527 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2528
2529 BuiltinVaListType = T;
2530}
2531
Ted Kremenek42730c52008-01-07 19:49:32 +00002532void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002533{
Ted Kremenek42730c52008-01-07 19:49:32 +00002534 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002535
2536 // typedef struct objc_object *id;
2537 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002538 // User error - caller will issue diagnostics.
2539 if (!ptr)
2540 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002541 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002542 // User error - caller will issue diagnostics.
2543 if (!rec)
2544 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002545 IdStructType = rec;
2546}
2547
Ted Kremenek42730c52008-01-07 19:49:32 +00002548void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002549{
Ted Kremenek42730c52008-01-07 19:49:32 +00002550 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002551
2552 // typedef struct objc_selector *SEL;
2553 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002554 if (!ptr)
2555 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002556 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002557 if (!rec)
2558 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002559 SelStructType = rec;
2560}
2561
Ted Kremenek42730c52008-01-07 19:49:32 +00002562void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002563{
Ted Kremenek42730c52008-01-07 19:49:32 +00002564 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002565}
2566
Ted Kremenek42730c52008-01-07 19:49:32 +00002567void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002568{
Ted Kremenek42730c52008-01-07 19:49:32 +00002569 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002570
2571 // typedef struct objc_class *Class;
2572 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2573 assert(ptr && "'Class' incorrectly typed");
2574 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2575 assert(rec && "'Class' incorrectly typed");
2576 ClassStructType = rec;
2577}
2578
Ted Kremenek42730c52008-01-07 19:49:32 +00002579void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2580 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002581 "'NSConstantString' type already set!");
2582
Ted Kremenek42730c52008-01-07 19:49:32 +00002583 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002584}
2585
Douglas Gregordd13e842009-03-30 22:58:21 +00002586/// \brief Retrieve the template name that represents a qualified
2587/// template name such as \c std::vector.
2588TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2589 bool TemplateKeyword,
2590 TemplateDecl *Template) {
2591 llvm::FoldingSetNodeID ID;
2592 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2593
2594 void *InsertPos = 0;
2595 QualifiedTemplateName *QTN =
2596 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2597 if (!QTN) {
2598 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2599 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2600 }
2601
2602 return TemplateName(QTN);
2603}
2604
2605/// \brief Retrieve the template name that represents a dependent
2606/// template name such as \c MetaFun::template apply.
2607TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2608 const IdentifierInfo *Name) {
2609 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2610
2611 llvm::FoldingSetNodeID ID;
2612 DependentTemplateName::Profile(ID, NNS, Name);
2613
2614 void *InsertPos = 0;
2615 DependentTemplateName *QTN =
2616 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2617
2618 if (QTN)
2619 return TemplateName(QTN);
2620
2621 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2622 if (CanonNNS == NNS) {
2623 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2624 } else {
2625 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2626 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2627 }
2628
2629 DependentTemplateNames.InsertNode(QTN, InsertPos);
2630 return TemplateName(QTN);
2631}
2632
Douglas Gregorc6507e42008-11-03 14:12:49 +00002633/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002634/// TargetInfo, produce the corresponding type. The unsigned @p Type
2635/// is actually a value of type @c TargetInfo::IntType.
2636QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002637 switch (Type) {
2638 case TargetInfo::NoInt: return QualType();
2639 case TargetInfo::SignedShort: return ShortTy;
2640 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2641 case TargetInfo::SignedInt: return IntTy;
2642 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2643 case TargetInfo::SignedLong: return LongTy;
2644 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2645 case TargetInfo::SignedLongLong: return LongLongTy;
2646 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2647 }
2648
2649 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002650 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002651}
Ted Kremenek118930e2008-07-24 23:58:27 +00002652
2653//===----------------------------------------------------------------------===//
2654// Type Predicates.
2655//===----------------------------------------------------------------------===//
2656
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002657/// isObjCNSObjectType - Return true if this is an NSObject object using
2658/// NSObject attribute on a c-style pointer type.
2659/// FIXME - Make it work directly on types.
2660///
2661bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2662 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2663 if (TypedefDecl *TD = TDT->getDecl())
2664 if (TD->getAttr<ObjCNSObjectAttr>())
2665 return true;
2666 }
2667 return false;
2668}
2669
Ted Kremenek118930e2008-07-24 23:58:27 +00002670/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2671/// to an object type. This includes "id" and "Class" (two 'special' pointers
2672/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2673/// ID type).
2674bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002675 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002676 return true;
2677
Steve Naroffd9e00802008-10-21 18:24:04 +00002678 // Blocks are objects.
2679 if (Ty->isBlockPointerType())
2680 return true;
2681
2682 // All other object types are pointers.
Chris Lattnera008d172009-04-12 23:51:02 +00002683 const PointerType *PT = Ty->getAsPointerType();
2684 if (PT == 0)
Ted Kremenek118930e2008-07-24 23:58:27 +00002685 return false;
2686
Chris Lattnera008d172009-04-12 23:51:02 +00002687 // If this a pointer to an interface (e.g. NSString*), it is ok.
2688 if (PT->getPointeeType()->isObjCInterfaceType() ||
2689 // If is has NSObject attribute, OK as well.
2690 isObjCNSObjectType(Ty))
2691 return true;
2692
Ted Kremenek118930e2008-07-24 23:58:27 +00002693 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2694 // pointer types. This looks for the typedef specifically, not for the
Chris Lattnera008d172009-04-12 23:51:02 +00002695 // underlying type. Iteratively strip off typedefs so that we can handle
2696 // typedefs of typedefs.
2697 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2698 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2699 Ty.getUnqualifiedType() == getObjCClassType())
2700 return true;
2701
2702 Ty = TDT->getDecl()->getUnderlyingType();
2703 }
Ted Kremenek118930e2008-07-24 23:58:27 +00002704
Chris Lattnera008d172009-04-12 23:51:02 +00002705 return false;
Ted Kremenek118930e2008-07-24 23:58:27 +00002706}
2707
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002708/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2709/// garbage collection attribute.
2710///
2711QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002712 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002713 if (getLangOptions().ObjC1 &&
2714 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002715 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002716 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002717 // (or pointers to them) be treated as though they were declared
2718 // as __strong.
2719 if (GCAttrs == QualType::GCNone) {
2720 if (isObjCObjectPointerType(Ty))
2721 GCAttrs = QualType::Strong;
2722 else if (Ty->isPointerType())
2723 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2724 }
Fariborz Jahaniand7b01972009-04-11 00:00:54 +00002725 // Non-pointers have none gc'able attribute regardless of the attribute
2726 // set on them.
2727 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2728 return QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002729 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002730 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002731}
2732
Chris Lattner6ff358b2008-04-07 06:51:04 +00002733//===----------------------------------------------------------------------===//
2734// Type Compatibility Testing
2735//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002736
Steve Naroff3454b6c2008-09-04 15:10:53 +00002737/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002738/// block types. Types must be strictly compatible here. For example,
2739/// C unfortunately doesn't produce an error for the following:
2740///
2741/// int (*emptyArgFunc)();
2742/// int (*intArgList)(int) = emptyArgFunc;
2743///
2744/// For blocks, we will produce an error for the following (similar to C++):
2745///
2746/// int (^emptyArgBlock)();
2747/// int (^intArgBlock)(int) = emptyArgBlock;
2748///
2749/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2750///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002751bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002752 const FunctionType *lbase = lhs->getAsFunctionType();
2753 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002754 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2755 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stump896ceed2009-04-01 01:17:39 +00002756 if (lproto && rproto == 0)
2757 return false;
2758 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff3454b6c2008-09-04 15:10:53 +00002759}
2760
Chris Lattner6ff358b2008-04-07 06:51:04 +00002761/// areCompatVectorTypes - Return true if the two specified vector types are
2762/// compatible.
2763static bool areCompatVectorTypes(const VectorType *LHS,
2764 const VectorType *RHS) {
2765 assert(LHS->isCanonical() && RHS->isCanonical());
2766 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002767 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002768}
2769
Eli Friedman0d9549b2008-08-22 00:56:42 +00002770/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002771/// compatible for assignment from RHS to LHS. This handles validation of any
2772/// protocol qualifiers on the LHS or RHS.
2773///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002774bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2775 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002776 // Verify that the base decls are compatible: the RHS must be a subclass of
2777 // the LHS.
2778 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2779 return false;
2780
2781 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2782 // protocol qualified at all, then we are good.
2783 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2784 return true;
2785
2786 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2787 // isn't a superset.
2788 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2789 return true; // FIXME: should return false!
2790
2791 // Finally, we must have two protocol-qualified interfaces.
2792 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2793 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002794
Steve Naroff98e71b82009-03-01 16:12:44 +00002795 // All LHS protocols must have a presence on the RHS.
2796 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002797
Steve Naroff98e71b82009-03-01 16:12:44 +00002798 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2799 LHSPE = LHSP->qual_end();
2800 LHSPI != LHSPE; LHSPI++) {
2801 bool RHSImplementsProtocol = false;
2802
2803 // If the RHS doesn't implement the protocol on the left, the types
2804 // are incompatible.
2805 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2806 RHSPE = RHSP->qual_end();
2807 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2808 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2809 RHSImplementsProtocol = true;
2810 }
2811 // FIXME: For better diagnostics, consider passing back the protocol name.
2812 if (!RHSImplementsProtocol)
2813 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002814 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002815 // The RHS implements all protocols listed on the LHS.
2816 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002817}
2818
Steve Naroff17c03822009-02-12 17:52:19 +00002819bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2820 // get the "pointed to" types
2821 const PointerType *LHSPT = LHS->getAsPointerType();
2822 const PointerType *RHSPT = RHS->getAsPointerType();
2823
2824 if (!LHSPT || !RHSPT)
2825 return false;
2826
2827 QualType lhptee = LHSPT->getPointeeType();
2828 QualType rhptee = RHSPT->getPointeeType();
2829 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2830 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2831 // ID acts sort of like void* for ObjC interfaces
2832 if (LHSIface && isObjCIdStructType(rhptee))
2833 return true;
2834 if (RHSIface && isObjCIdStructType(lhptee))
2835 return true;
2836 if (!LHSIface || !RHSIface)
2837 return false;
2838 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2839 canAssignObjCInterfaces(RHSIface, LHSIface);
2840}
2841
Steve Naroff85f0dc52007-10-15 20:41:53 +00002842/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2843/// both shall have the identically qualified version of a compatible type.
2844/// C99 6.2.7p1: Two types have compatible types if their types are the
2845/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002846bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2847 return !mergeTypes(LHS, RHS).isNull();
2848}
2849
2850QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2851 const FunctionType *lbase = lhs->getAsFunctionType();
2852 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002853 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2854 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002855 bool allLTypes = true;
2856 bool allRTypes = true;
2857
2858 // Check return type
2859 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2860 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002861 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2862 allLTypes = false;
2863 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2864 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002865
2866 if (lproto && rproto) { // two C99 style function prototypes
2867 unsigned lproto_nargs = lproto->getNumArgs();
2868 unsigned rproto_nargs = rproto->getNumArgs();
2869
2870 // Compatible functions must have the same number of arguments
2871 if (lproto_nargs != rproto_nargs)
2872 return QualType();
2873
2874 // Variadic and non-variadic functions aren't compatible
2875 if (lproto->isVariadic() != rproto->isVariadic())
2876 return QualType();
2877
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002878 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2879 return QualType();
2880
Eli Friedman0d9549b2008-08-22 00:56:42 +00002881 // Check argument compatibility
2882 llvm::SmallVector<QualType, 10> types;
2883 for (unsigned i = 0; i < lproto_nargs; i++) {
2884 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2885 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2886 QualType argtype = mergeTypes(largtype, rargtype);
2887 if (argtype.isNull()) return QualType();
2888 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002889 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2890 allLTypes = false;
2891 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2892 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002893 }
2894 if (allLTypes) return lhs;
2895 if (allRTypes) return rhs;
2896 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002897 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002898 }
2899
2900 if (lproto) allRTypes = false;
2901 if (rproto) allLTypes = false;
2902
Douglas Gregor4fa58902009-02-26 23:50:07 +00002903 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002904 if (proto) {
2905 if (proto->isVariadic()) return QualType();
2906 // Check that the types are compatible with the types that
2907 // would result from default argument promotions (C99 6.7.5.3p15).
2908 // The only types actually affected are promotable integer
2909 // types and floats, which would be passed as a different
2910 // type depending on whether the prototype is visible.
2911 unsigned proto_nargs = proto->getNumArgs();
2912 for (unsigned i = 0; i < proto_nargs; ++i) {
2913 QualType argTy = proto->getArgType(i);
2914 if (argTy->isPromotableIntegerType() ||
2915 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2916 return QualType();
2917 }
2918
2919 if (allLTypes) return lhs;
2920 if (allRTypes) return rhs;
2921 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002922 proto->getNumArgs(), lproto->isVariadic(),
2923 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002924 }
2925
2926 if (allLTypes) return lhs;
2927 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002928 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002929}
2930
2931QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002932 // C++ [expr]: If an expression initially has the type "reference to T", the
2933 // type is adjusted to "T" prior to any further analysis, the expression
2934 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002935 // expression is an lvalue unless the reference is an rvalue reference and
2936 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002937 // FIXME: C++ shouldn't be going through here! The rules are different
2938 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00002939 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2940 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002941 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002942 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002943 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002944 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002945
Eli Friedman0d9549b2008-08-22 00:56:42 +00002946 QualType LHSCan = getCanonicalType(LHS),
2947 RHSCan = getCanonicalType(RHS);
2948
2949 // If two types are identical, they are compatible.
2950 if (LHSCan == RHSCan)
2951 return LHS;
2952
2953 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002954 // Note that we handle extended qualifiers later, in the
2955 // case for ExtQualType.
2956 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002957 return QualType();
2958
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00002959 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
2960 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002961
Chris Lattnerc38d4522008-01-14 05:45:46 +00002962 // We want to consider the two function types to be the same for these
2963 // comparisons, just force one to the other.
2964 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2965 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002966
2967 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002968 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2969 LHSClass = Type::ConstantArray;
2970 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2971 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002972
Nate Begemanaf6ed502008-04-18 23:10:10 +00002973 // Canonicalize ExtVector -> Vector.
2974 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2975 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002976
Chris Lattner7cdcb252008-04-07 06:38:24 +00002977 // Consider qualified interfaces and interfaces the same.
2978 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2979 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002980
Chris Lattnerb5709e22008-04-07 05:43:21 +00002981 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002982 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002983 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2984 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00002985
Steve Naroff0773c582009-04-14 15:11:46 +00002986 // 'id' and 'Class' act sort of like void* for ObjC interfaces
2987 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00002988 return LHS;
Steve Naroff0773c582009-04-14 15:11:46 +00002989 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00002990 return RHS;
2991
Steve Naroff28ceff72008-12-10 22:14:21 +00002992 // ID is compatible with all qualified id types.
2993 if (LHS->isObjCQualifiedIdType()) {
2994 if (const PointerType *PT = RHS->getAsPointerType()) {
2995 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00002996 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002997 return LHS;
2998 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2999 // Unfortunately, this API is part of Sema (which we don't have access
3000 // to. Need to refactor. The following check is insufficient, since we
3001 // need to make sure the class implements the protocol.
3002 if (pType->isObjCInterfaceType())
3003 return LHS;
3004 }
3005 }
3006 if (RHS->isObjCQualifiedIdType()) {
3007 if (const PointerType *PT = LHS->getAsPointerType()) {
3008 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00003009 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00003010 return RHS;
3011 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
3012 // Unfortunately, this API is part of Sema (which we don't have access
3013 // to. Need to refactor. The following check is insufficient, since we
3014 // need to make sure the class implements the protocol.
3015 if (pType->isObjCInterfaceType())
3016 return RHS;
3017 }
3018 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003019 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
3020 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003021 if (const EnumType* ETy = LHS->getAsEnumType()) {
3022 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3023 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003024 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003025 if (const EnumType* ETy = RHS->getAsEnumType()) {
3026 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3027 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003028 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003029
Eli Friedman0d9549b2008-08-22 00:56:42 +00003030 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003031 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003032
Steve Naroffc88babe2008-01-09 22:43:08 +00003033 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003034 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00003035#define TYPE(Class, Base)
3036#define ABSTRACT_TYPE(Class, Base)
3037#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3038#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3039#include "clang/AST/TypeNodes.def"
3040 assert(false && "Non-canonical and dependent types shouldn't get here");
3041 return QualType();
3042
Sebastian Redlce6fff02009-03-16 23:22:08 +00003043 case Type::LValueReference:
3044 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003045 case Type::MemberPointer:
3046 assert(false && "C++ should never be in mergeTypes");
3047 return QualType();
3048
3049 case Type::IncompleteArray:
3050 case Type::VariableArray:
3051 case Type::FunctionProto:
3052 case Type::ExtVector:
3053 case Type::ObjCQualifiedInterface:
3054 assert(false && "Types are eliminated above");
3055 return QualType();
3056
Chris Lattnerc38d4522008-01-14 05:45:46 +00003057 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003058 {
3059 // Merge two pointer types, while trying to preserve typedef info
3060 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3061 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3062 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3063 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003064 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3065 return LHS;
3066 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3067 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003068 return getPointerType(ResultType);
3069 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003070 case Type::BlockPointer:
3071 {
3072 // Merge two block pointer types, while trying to preserve typedef info
3073 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3074 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3075 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3076 if (ResultType.isNull()) return QualType();
3077 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3078 return LHS;
3079 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3080 return RHS;
3081 return getBlockPointerType(ResultType);
3082 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003083 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003084 {
3085 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3086 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3087 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3088 return QualType();
3089
3090 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3091 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3092 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3093 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003094 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3095 return LHS;
3096 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3097 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003098 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3099 ArrayType::ArraySizeModifier(), 0);
3100 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3101 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003102 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3103 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003104 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3105 return LHS;
3106 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3107 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003108 if (LVAT) {
3109 // FIXME: This isn't correct! But tricky to implement because
3110 // the array's size has to be the size of LHS, but the type
3111 // has to be different.
3112 return LHS;
3113 }
3114 if (RVAT) {
3115 // FIXME: This isn't correct! But tricky to implement because
3116 // the array's size has to be the size of RHS, but the type
3117 // has to be different.
3118 return RHS;
3119 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003120 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3121 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003122 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003123 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003124 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003125 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003126 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003127 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003128 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003129 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3130 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003131 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003132 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003133 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003134 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003135 case Type::Complex:
3136 // Distinct complex types are incompatible.
3137 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003138 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003139 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003140 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3141 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003142 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003143 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003144 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003145 // FIXME: This should be type compatibility, e.g. whether
3146 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003147 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3148 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3149 if (LHSIface && RHSIface &&
3150 canAssignObjCInterfaces(LHSIface, RHSIface))
3151 return LHS;
3152
Eli Friedman0d9549b2008-08-22 00:56:42 +00003153 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003154 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003155 case Type::ObjCQualifiedId:
3156 // Distinct qualified id's are not compatible.
3157 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003158 case Type::FixedWidthInt:
3159 // Distinct fixed-width integers are not compatible.
3160 return QualType();
3161 case Type::ObjCQualifiedClass:
3162 // Distinct qualified classes are not compatible.
3163 return QualType();
3164 case Type::ExtQual:
3165 // FIXME: ExtQual types can be compatible even if they're not
3166 // identical!
3167 return QualType();
3168 // First attempt at an implementation, but I'm not really sure it's
3169 // right...
3170#if 0
3171 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3172 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3173 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3174 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3175 return QualType();
3176 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3177 LHSBase = QualType(LQual->getBaseType(), 0);
3178 RHSBase = QualType(RQual->getBaseType(), 0);
3179 ResultType = mergeTypes(LHSBase, RHSBase);
3180 if (ResultType.isNull()) return QualType();
3181 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3182 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3183 return LHS;
3184 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3185 return RHS;
3186 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3187 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3188 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3189 return ResultType;
3190#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003191
3192 case Type::TemplateSpecialization:
3193 assert(false && "Dependent types have no size");
3194 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003195 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003196
3197 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003198}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003199
Chris Lattner1d78a862008-04-07 07:01:58 +00003200//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003201// Integer Predicates
3202//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003203
Eli Friedman0832dbc2008-06-28 06:23:08 +00003204unsigned ASTContext::getIntWidth(QualType T) {
3205 if (T == BoolTy)
3206 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003207 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3208 return FWIT->getWidth();
3209 }
3210 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003211 return (unsigned)getTypeSize(T);
3212}
3213
3214QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3215 assert(T->isSignedIntegerType() && "Unexpected type");
3216 if (const EnumType* ETy = T->getAsEnumType())
3217 T = ETy->getDecl()->getIntegerType();
3218 const BuiltinType* BTy = T->getAsBuiltinType();
3219 assert (BTy && "Unexpected signed integer type");
3220 switch (BTy->getKind()) {
3221 case BuiltinType::Char_S:
3222 case BuiltinType::SChar:
3223 return UnsignedCharTy;
3224 case BuiltinType::Short:
3225 return UnsignedShortTy;
3226 case BuiltinType::Int:
3227 return UnsignedIntTy;
3228 case BuiltinType::Long:
3229 return UnsignedLongTy;
3230 case BuiltinType::LongLong:
3231 return UnsignedLongLongTy;
3232 default:
3233 assert(0 && "Unexpected signed integer type");
3234 return QualType();
3235 }
3236}
3237
3238
3239//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00003240// Serialization Support
3241//===----------------------------------------------------------------------===//
3242
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003243enum {
3244 BasicMetadataBlock = 1,
3245 ASTContextBlock = 2,
3246 DeclsBlock = 3
3247};
3248
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003249void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3250 // Create bitstream.
3251 llvm::BitstreamWriter Stream(Buffer);
3252
3253 // Emit the preamble.
3254 Stream.Emit((unsigned)'B', 8);
3255 Stream.Emit((unsigned)'C', 8);
3256 Stream.Emit(0xC, 4);
3257 Stream.Emit(0xF, 4);
3258 Stream.Emit(0xE, 4);
3259 Stream.Emit(0x0, 4);
3260
3261 // Create serializer.
3262 llvm::Serializer S(Stream);
3263
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003264 // ===---------------------------------------------------===/
3265 // Serialize the "Translation Unit" metadata.
3266 // ===---------------------------------------------------===/
3267
3268 // Emit ASTContext.
3269 S.EnterBlock(ASTContextBlock);
3270 S.EmitOwnedPtr(this);
3271 S.ExitBlock(); // exit "ASTContextBlock"
3272
3273 S.EnterBlock(BasicMetadataBlock);
3274
3275 // Block for SourceManager and Target. Allows easy skipping
3276 // around to the block for the Selectors during deserialization.
3277 S.EnterBlock();
3278
3279 // Emit the SourceManager.
3280 S.Emit(getSourceManager());
3281
3282 // Emit the Target.
3283 S.EmitPtr(&Target);
3284 S.EmitCStr(Target.getTargetTriple());
3285
3286 S.ExitBlock(); // exit "SourceManager and Target Block"
3287
3288 // Emit the Selectors.
3289 S.Emit(Selectors);
3290
3291 // Emit the Identifier Table.
3292 S.Emit(Idents);
3293
3294 S.ExitBlock(); // exit "BasicMetadataBlock"
3295}
3296
3297
Ted Kremenek738e6c02007-10-31 17:10:13 +00003298/// Emit - Serialize an ASTContext object to Bitcode.
3299void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00003300 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00003301 S.EmitRef(SourceMgr);
3302 S.EmitRef(Target);
3303 S.EmitRef(Idents);
3304 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003305
Ted Kremenek68228a92007-10-31 22:44:07 +00003306 // Emit the size of the type vector so that we can reserve that size
3307 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003308 S.EmitInt(Types.size());
3309
Ted Kremenek034a78c2007-11-13 22:02:55 +00003310 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3311 I!=E;++I)
3312 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003313
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003314 S.EmitOwnedPtr(TUDecl);
3315
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003316 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003317}
3318
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003319
3320ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3321 FileManager &FMgr) {
3322 // Check if the file is of the proper length.
3323 if (Buffer.getBufferSize() & 0x3) {
3324 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3325 return 0;
3326 }
3327
3328 // Create the bitstream reader.
3329 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3330 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3331
3332 if (Stream.Read(8) != 'B' ||
3333 Stream.Read(8) != 'C' ||
3334 Stream.Read(4) != 0xC ||
3335 Stream.Read(4) != 0xF ||
3336 Stream.Read(4) != 0xE ||
3337 Stream.Read(4) != 0x0) {
3338 // FIXME: Provide diagnostic.
3339 return NULL;
3340 }
3341
3342 // Create the deserializer.
3343 llvm::Deserializer Dezr(Stream);
3344
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003345 // ===---------------------------------------------------===/
3346 // Deserialize the "Translation Unit" metadata.
3347 // ===---------------------------------------------------===/
3348
3349 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3350 // (which will appear earlier) and record its location.
3351
3352 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3353 assert (FoundBlock);
3354
3355 llvm::Deserializer::Location ASTContextBlockLoc =
3356 Dezr.getCurrentBlockLocation();
3357
3358 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3359 assert (FoundBlock);
3360
3361 // Read the SourceManager.
3362 SourceManager::CreateAndRegister(Dezr, FMgr);
3363
3364 { // Read the TargetInfo.
3365 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3366 char* triple = Dezr.ReadCStr(NULL,0,true);
3367 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3368 delete [] triple;
3369 }
3370
3371 // For Selectors, we must read the identifier table first because the
3372 // SelectorTable depends on the identifiers being already deserialized.
3373 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3374 Dezr.SkipBlock();
3375
3376 // Read the identifier table.
3377 IdentifierTable::CreateAndRegister(Dezr);
3378
3379 // Now jump back and read the selectors.
3380 Dezr.JumpTo(SelectorBlkLoc);
3381 SelectorTable::CreateAndRegister(Dezr);
3382
3383 // Now jump back to ASTContextBlock and read the ASTContext.
3384 Dezr.JumpTo(ASTContextBlockLoc);
3385 return Dezr.ReadOwnedPtr<ASTContext>();
3386}
3387
Ted Kremenekacba3612007-11-13 00:25:37 +00003388ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00003389
3390 // Read the language options.
3391 LangOptions LOpts;
3392 LOpts.Read(D);
3393
Ted Kremenek68228a92007-10-31 22:44:07 +00003394 SourceManager &SM = D.ReadRef<SourceManager>();
3395 TargetInfo &t = D.ReadRef<TargetInfo>();
3396 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3397 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00003398
Ted Kremenek68228a92007-10-31 22:44:07 +00003399 unsigned size_reserve = D.ReadInt();
3400
Douglas Gregor24afd4a2008-11-17 14:58:09 +00003401 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3402 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00003403
Ted Kremenek034a78c2007-11-13 22:02:55 +00003404 for (unsigned i = 0; i < size_reserve; ++i)
3405 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00003406
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003407 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3408
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003409 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00003410
3411 return A;
3412}
Douglas Gregorc34897d2009-04-09 22:27:44 +00003413
3414ExternalASTSource::~ExternalASTSource() { }
3415
3416void ExternalASTSource::PrintStats() { }