blob: f156ac458cf9eba5c046bc1462cb400b3074439e [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 Dunbare0edb2e2009-04-22 04:39:47 +0000624void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
625 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
626 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
627 if (SuperClass)
628 CollectObjCIvars(SuperClass, Fields);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000629 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
630 E = OI->ivar_end(); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000631 ObjCIvarDecl *IVDecl = *I;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000632 if (!IVDecl->isInvalidDecl())
633 Fields.push_back(cast<FieldDecl>(IVDecl));
634 }
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000635 // look into properties.
Daniel Dunbare0edb2e2009-04-22 04:39:47 +0000636 for (ObjCInterfaceDecl::prop_iterator I = OI->prop_begin(*this),
637 E = OI->prop_end(*this); I != E; ++I) {
Chris Lattner9329cf52009-03-31 08:48:01 +0000638 if (ObjCIvarDecl *IV = (*I)->getPropertyIvarDecl())
Fariborz Jahanianc625fa92009-03-31 00:06:29 +0000639 Fields.push_back(cast<FieldDecl>(IV));
640 }
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000641}
642
643/// addRecordToClass - produces record info. for the class for its
644/// ivars and all those inherited.
645///
Chris Lattner9329cf52009-03-31 08:48:01 +0000646const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D) {
Daniel Dunbardbc8af72009-04-21 21:41:56 +0000647 // FIXME: The only client relying on this working in the presence of
Daniel Dunbare0edb2e2009-04-22 04:39:47 +0000648 // forward declarations is IRgen, which should not need it. Fix
649 // and simplify this code.
Chris Lattner608c1e32009-03-31 09:24:30 +0000650 RecordDecl *&RD = ASTRecordForInterface[D];
651 if (RD) {
652 // If we have a record decl already and it is either a definition or if 'D'
653 // is still a forward declaration, return it.
654 if (RD->isDefinition() || D->isForwardDecl())
655 return RD;
656 }
657
658 // If D is a forward declaration, then just make a forward struct decl.
659 if (D->isForwardDecl())
660 return RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
661 D->getLocation(),
662 D->getIdentifier());
Chris Lattner9329cf52009-03-31 08:48:01 +0000663
664 llvm::SmallVector<FieldDecl*, 32> RecFields;
Daniel Dunbare0edb2e2009-04-22 04:39:47 +0000665 CollectObjCIvars(D, RecFields);
Chris Lattner608c1e32009-03-31 09:24:30 +0000666
667 if (RD == 0)
668 RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(),
669 D->getIdentifier());
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000670 /// FIXME! Can do collection of ivars and adding to the record while
671 /// doing it.
Chris Lattner41b780c2009-03-31 08:58:42 +0000672 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000673 RD->addDecl(*this,
674 FieldDecl::Create(*this, RD,
Chris Lattner608c1e32009-03-31 09:24:30 +0000675 RecFields[i]->getLocation(),
676 RecFields[i]->getIdentifier(),
677 RecFields[i]->getType(),
678 RecFields[i]->getBitWidth(), false));
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000679 }
Chris Lattner9329cf52009-03-31 08:48:01 +0000680
Chris Lattner608c1e32009-03-31 09:24:30 +0000681 RD->completeDefinition(*this);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000682 return RD;
683}
Devang Patel4b6bf702008-06-04 21:54:36 +0000684
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000685/// getASTObjcInterfaceLayout - Get or compute information about the layout of
686/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000687/// position information.
688const ASTRecordLayout &
689ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
690 // Look up this layout, if already laid out, return what we have.
691 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
692 if (Entry) return *Entry;
693
694 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
695 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000696 ASTRecordLayout *NewEntry = NULL;
Fariborz Jahanianba50c332009-04-08 21:54:52 +0000697 // FIXME. Add actual count of synthesized ivars, instead of count
698 // of properties which is the upper bound, but is safe.
Daniel Dunbar998510f2009-04-08 20:18:15 +0000699 unsigned FieldCount =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000700 D->ivar_size() + std::distance(D->prop_begin(*this), D->prop_end(*this));
Devang Patel8682d882008-06-06 02:14:01 +0000701 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
702 FieldCount++;
703 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
704 unsigned Alignment = SL.getAlignment();
705 uint64_t Size = SL.getSize();
706 NewEntry = new ASTRecordLayout(Size, Alignment);
707 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000708 // Super class is at the beginning of the layout.
709 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000710 } else {
711 NewEntry = new ASTRecordLayout();
712 NewEntry->InitializeLayout(FieldCount);
713 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000714 Entry = NewEntry;
715
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000716 unsigned StructPacking = 0;
717 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
718 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000719
720 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
721 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
722 AA->getAlignment()));
723
724 // Layout each ivar sequentially.
725 unsigned i = 0;
726 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
727 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
728 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000729 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000730 }
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000731 // Also synthesized ivars
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000732 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
733 E = D->prop_end(*this); I != E; ++I) {
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000734 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
735 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
736 }
Fariborz Jahanian84c45692009-04-01 19:37:34 +0000737
Devang Patel4b6bf702008-06-04 21:54:36 +0000738 // Finally, round the size of the total struct up to the alignment of the
739 // struct itself.
740 NewEntry->FinalizeLayout();
741 return *NewEntry;
742}
743
Devang Patel7a78e432007-11-01 19:11:01 +0000744/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000745/// specified record (struct/union/class), which indicates its size and field
746/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000747const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000748 D = D->getDefinition(*this);
749 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000750
Chris Lattner4b009652007-07-25 00:24:17 +0000751 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000752 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000753 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000754
Devang Patel7a78e432007-11-01 19:11:01 +0000755 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
756 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
757 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000758 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000759
Douglas Gregor39677622008-12-11 20:41:00 +0000760 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000761 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
762 D->field_end(*this)));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000763 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000764
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000765 unsigned StructPacking = 0;
766 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
767 StructPacking = PA->getAlignment();
768
Eli Friedman5949a022008-05-30 09:31:38 +0000769 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000770 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
771 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000772
Eli Friedman5949a022008-05-30 09:31:38 +0000773 // Layout each field, for now, just sequentially, respecting alignment. In
774 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000775 unsigned FieldIdx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000776 for (RecordDecl::field_iterator Field = D->field_begin(*this),
777 FieldEnd = D->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000778 Field != FieldEnd; (void)++Field, ++FieldIdx)
779 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000780
781 // Finally, round the size of the total struct up to the alignment of the
782 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000783 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000784 return *NewEntry;
785}
786
Chris Lattner4b009652007-07-25 00:24:17 +0000787//===----------------------------------------------------------------------===//
788// Type creation/memoization methods
789//===----------------------------------------------------------------------===//
790
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000791QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000792 QualType CanT = getCanonicalType(T);
793 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000794 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000795
796 // If we are composing extended qualifiers together, merge together into one
797 // ExtQualType node.
798 unsigned CVRQuals = T.getCVRQualifiers();
799 QualType::GCAttrTypes GCAttr = QualType::GCNone;
800 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000801
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000802 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
803 // If this type already has an address space specified, it cannot get
804 // another one.
805 assert(EQT->getAddressSpace() == 0 &&
806 "Type cannot be in multiple addr spaces!");
807 GCAttr = EQT->getObjCGCAttr();
808 TypeNode = EQT->getBaseType();
809 }
Chris Lattner35fef522008-02-20 20:55:12 +0000810
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000811 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000812 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000813 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000814 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000815 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000816 return QualType(EXTQy, CVRQuals);
817
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000818 // If the base type isn't canonical, this won't be a canonical type either,
819 // so fill in the canonical type field.
820 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000821 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000822 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000823
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000824 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000825 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000826 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000827 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000828 ExtQualType *New =
829 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000830 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000831 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000832 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000833}
834
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000835QualType ASTContext::getObjCGCQualType(QualType T,
836 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000837 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000838 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000839 return T;
840
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000841 // If we are composing extended qualifiers together, merge together into one
842 // ExtQualType node.
843 unsigned CVRQuals = T.getCVRQualifiers();
844 Type *TypeNode = T.getTypePtr();
845 unsigned AddressSpace = 0;
846
847 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
848 // If this type already has an address space specified, it cannot get
849 // another one.
850 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
851 "Type cannot be in multiple addr spaces!");
852 AddressSpace = EQT->getAddressSpace();
853 TypeNode = EQT->getBaseType();
854 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000855
856 // Check if we've already instantiated an gc qual'd type of this type.
857 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000858 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000859 void *InsertPos = 0;
860 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000861 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000862
863 // If the base type isn't canonical, this won't be a canonical type either,
864 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +0000865 // FIXME: Isn't this also not canonical if the base type is a array
866 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000867 QualType Canonical;
868 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000869 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000870
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000871 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000872 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
873 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
874 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000875 ExtQualType *New =
876 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000877 ExtQualTypes.InsertNode(New, InsertPos);
878 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000879 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000880}
Chris Lattner4b009652007-07-25 00:24:17 +0000881
882/// getComplexType - Return the uniqued reference to the type for a complex
883/// number with the specified element type.
884QualType ASTContext::getComplexType(QualType T) {
885 // Unique pointers, to guarantee there is only one pointer of a particular
886 // structure.
887 llvm::FoldingSetNodeID ID;
888 ComplexType::Profile(ID, T);
889
890 void *InsertPos = 0;
891 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
892 return QualType(CT, 0);
893
894 // If the pointee type isn't canonical, this won't be a canonical type either,
895 // so fill in the canonical type field.
896 QualType Canonical;
897 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000898 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000899
900 // Get the new insert position for the node we care about.
901 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000902 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000903 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000904 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000905 Types.push_back(New);
906 ComplexTypes.InsertNode(New, InsertPos);
907 return QualType(New, 0);
908}
909
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000910QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
911 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
912 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
913 FixedWidthIntType *&Entry = Map[Width];
914 if (!Entry)
915 Entry = new FixedWidthIntType(Width, Signed);
916 return QualType(Entry, 0);
917}
Chris Lattner4b009652007-07-25 00:24:17 +0000918
919/// getPointerType - Return the uniqued reference to the type for a pointer to
920/// the specified type.
921QualType ASTContext::getPointerType(QualType T) {
922 // Unique pointers, to guarantee there is only one pointer of a particular
923 // structure.
924 llvm::FoldingSetNodeID ID;
925 PointerType::Profile(ID, T);
926
927 void *InsertPos = 0;
928 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
929 return QualType(PT, 0);
930
931 // If the pointee type isn't canonical, this won't be a canonical type either,
932 // so fill in the canonical type field.
933 QualType Canonical;
934 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000935 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000936
937 // Get the new insert position for the node we care about.
938 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000939 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000940 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000941 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000942 Types.push_back(New);
943 PointerTypes.InsertNode(New, InsertPos);
944 return QualType(New, 0);
945}
946
Steve Naroff7aa54752008-08-27 16:04:49 +0000947/// getBlockPointerType - Return the uniqued reference to the type for
948/// a pointer to the specified block.
949QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000950 assert(T->isFunctionType() && "block of function types only");
951 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000952 // structure.
953 llvm::FoldingSetNodeID ID;
954 BlockPointerType::Profile(ID, T);
955
956 void *InsertPos = 0;
957 if (BlockPointerType *PT =
958 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
959 return QualType(PT, 0);
960
Steve Narofffd5b19d2008-08-28 19:20:44 +0000961 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000962 // type either so fill in the canonical type field.
963 QualType Canonical;
964 if (!T->isCanonical()) {
965 Canonical = getBlockPointerType(getCanonicalType(T));
966
967 // Get the new insert position for the node we care about.
968 BlockPointerType *NewIP =
969 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000970 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000971 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000972 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000973 Types.push_back(New);
974 BlockPointerTypes.InsertNode(New, InsertPos);
975 return QualType(New, 0);
976}
977
Sebastian Redlce6fff02009-03-16 23:22:08 +0000978/// getLValueReferenceType - Return the uniqued reference to the type for an
979/// lvalue reference to the specified type.
980QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +0000981 // Unique pointers, to guarantee there is only one pointer of a particular
982 // structure.
983 llvm::FoldingSetNodeID ID;
984 ReferenceType::Profile(ID, T);
985
986 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000987 if (LValueReferenceType *RT =
988 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000989 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000990
Chris Lattner4b009652007-07-25 00:24:17 +0000991 // If the referencee type isn't canonical, this won't be a canonical type
992 // either, so fill in the canonical type field.
993 QualType Canonical;
994 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000995 Canonical = getLValueReferenceType(getCanonicalType(T));
996
Chris Lattner4b009652007-07-25 00:24:17 +0000997 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000998 LValueReferenceType *NewIP =
999 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001000 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001001 }
1002
Sebastian Redlce6fff02009-03-16 23:22:08 +00001003 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001004 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001005 LValueReferenceTypes.InsertNode(New, InsertPos);
1006 return QualType(New, 0);
1007}
1008
1009/// getRValueReferenceType - Return the uniqued reference to the type for an
1010/// rvalue reference to the specified type.
1011QualType ASTContext::getRValueReferenceType(QualType T) {
1012 // Unique pointers, to guarantee there is only one pointer of a particular
1013 // structure.
1014 llvm::FoldingSetNodeID ID;
1015 ReferenceType::Profile(ID, T);
1016
1017 void *InsertPos = 0;
1018 if (RValueReferenceType *RT =
1019 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1020 return QualType(RT, 0);
1021
1022 // If the referencee type isn't canonical, this won't be a canonical type
1023 // either, so fill in the canonical type field.
1024 QualType Canonical;
1025 if (!T->isCanonical()) {
1026 Canonical = getRValueReferenceType(getCanonicalType(T));
1027
1028 // Get the new insert position for the node we care about.
1029 RValueReferenceType *NewIP =
1030 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1031 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1032 }
1033
1034 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1035 Types.push_back(New);
1036 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001037 return QualType(New, 0);
1038}
1039
Sebastian Redl75555032009-01-24 21:16:55 +00001040/// getMemberPointerType - Return the uniqued reference to the type for a
1041/// member pointer to the specified type, in the specified class.
1042QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1043{
1044 // Unique pointers, to guarantee there is only one pointer of a particular
1045 // structure.
1046 llvm::FoldingSetNodeID ID;
1047 MemberPointerType::Profile(ID, T, Cls);
1048
1049 void *InsertPos = 0;
1050 if (MemberPointerType *PT =
1051 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1052 return QualType(PT, 0);
1053
1054 // If the pointee or class type isn't canonical, this won't be a canonical
1055 // type either, so fill in the canonical type field.
1056 QualType Canonical;
1057 if (!T->isCanonical()) {
1058 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1059
1060 // Get the new insert position for the node we care about.
1061 MemberPointerType *NewIP =
1062 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1063 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1064 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001065 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001066 Types.push_back(New);
1067 MemberPointerTypes.InsertNode(New, InsertPos);
1068 return QualType(New, 0);
1069}
1070
Steve Naroff83c13012007-08-30 01:06:46 +00001071/// getConstantArrayType - Return the unique reference to the type for an
1072/// array of the specified element type.
1073QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +00001074 const llvm::APInt &ArySize,
1075 ArrayType::ArraySizeModifier ASM,
1076 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001077 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001078 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001079
1080 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001081 if (ConstantArrayType *ATP =
1082 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001083 return QualType(ATP, 0);
1084
1085 // If the element type isn't canonical, this won't be a canonical type either,
1086 // so fill in the canonical type field.
1087 QualType Canonical;
1088 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001089 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001090 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001091 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001092 ConstantArrayType *NewIP =
1093 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001094 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001095 }
1096
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001097 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001098 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001099 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001100 Types.push_back(New);
1101 return QualType(New, 0);
1102}
1103
Steve Naroffe2579e32007-08-30 18:14:25 +00001104/// getVariableArrayType - Returns a non-unique reference to the type for a
1105/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +00001106QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1107 ArrayType::ArraySizeModifier ASM,
1108 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001109 // Since we don't unique expressions, it isn't possible to unique VLA's
1110 // that have an expression provided for their size.
1111
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001112 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001113 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001114
1115 VariableArrayTypes.push_back(New);
1116 Types.push_back(New);
1117 return QualType(New, 0);
1118}
1119
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001120/// getDependentSizedArrayType - Returns a non-unique reference to
1121/// the type for a dependently-sized array of the specified element
1122/// type. FIXME: We will need these to be uniqued, or at least
1123/// comparable, at some point.
1124QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1125 ArrayType::ArraySizeModifier ASM,
1126 unsigned EltTypeQuals) {
1127 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1128 "Size must be type- or value-dependent!");
1129
1130 // Since we don't unique expressions, it isn't possible to unique
1131 // dependently-sized array types.
1132
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001133 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001134 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1135 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001136
1137 DependentSizedArrayTypes.push_back(New);
1138 Types.push_back(New);
1139 return QualType(New, 0);
1140}
1141
Eli Friedman8ff07782008-02-15 18:16:39 +00001142QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1143 ArrayType::ArraySizeModifier ASM,
1144 unsigned EltTypeQuals) {
1145 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001146 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001147
1148 void *InsertPos = 0;
1149 if (IncompleteArrayType *ATP =
1150 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1151 return QualType(ATP, 0);
1152
1153 // If the element type isn't canonical, this won't be a canonical type
1154 // either, so fill in the canonical type field.
1155 QualType Canonical;
1156
1157 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001158 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001159 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001160
1161 // Get the new insert position for the node we care about.
1162 IncompleteArrayType *NewIP =
1163 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001164 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001165 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001166
Steve Naroff93fd2112009-01-27 22:08:43 +00001167 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001168 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001169
1170 IncompleteArrayTypes.InsertNode(New, InsertPos);
1171 Types.push_back(New);
1172 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001173}
1174
Chris Lattner4b009652007-07-25 00:24:17 +00001175/// getVectorType - Return the unique reference to a vector type of
1176/// the specified element type and size. VectorType must be a built-in type.
1177QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1178 BuiltinType *baseType;
1179
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001180 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001181 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1182
1183 // Check if we've already instantiated a vector of this type.
1184 llvm::FoldingSetNodeID ID;
1185 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1186 void *InsertPos = 0;
1187 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1188 return QualType(VTP, 0);
1189
1190 // If the element type isn't canonical, this won't be a canonical type either,
1191 // so fill in the canonical type field.
1192 QualType Canonical;
1193 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001194 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001195
1196 // Get the new insert position for the node we care about.
1197 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001198 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001199 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001200 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001201 VectorTypes.InsertNode(New, InsertPos);
1202 Types.push_back(New);
1203 return QualType(New, 0);
1204}
1205
Nate Begemanaf6ed502008-04-18 23:10:10 +00001206/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001207/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001208QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001209 BuiltinType *baseType;
1210
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001211 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001212 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001213
1214 // Check if we've already instantiated a vector of this type.
1215 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001216 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001217 void *InsertPos = 0;
1218 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1219 return QualType(VTP, 0);
1220
1221 // If the element type isn't canonical, this won't be a canonical type either,
1222 // so fill in the canonical type field.
1223 QualType Canonical;
1224 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001225 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001226
1227 // Get the new insert position for the node we care about.
1228 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001229 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001230 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001231 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001232 VectorTypes.InsertNode(New, InsertPos);
1233 Types.push_back(New);
1234 return QualType(New, 0);
1235}
1236
Douglas Gregor4fa58902009-02-26 23:50:07 +00001237/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001238///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001239QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001240 // Unique functions, to guarantee there is only one function of a particular
1241 // structure.
1242 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001243 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001244
1245 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001246 if (FunctionNoProtoType *FT =
1247 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001248 return QualType(FT, 0);
1249
1250 QualType Canonical;
1251 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001252 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001253
1254 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001255 FunctionNoProtoType *NewIP =
1256 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001257 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001258 }
1259
Douglas Gregor4fa58902009-02-26 23:50:07 +00001260 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001261 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001262 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001263 return QualType(New, 0);
1264}
1265
1266/// getFunctionType - Return a normal function type with a typed argument
1267/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001268QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001269 unsigned NumArgs, bool isVariadic,
1270 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001271 // Unique functions, to guarantee there is only one function of a particular
1272 // structure.
1273 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001274 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001275 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001276
1277 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001278 if (FunctionProtoType *FTP =
1279 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001280 return QualType(FTP, 0);
1281
1282 // Determine whether the type being created is already canonical or not.
1283 bool isCanonical = ResultTy->isCanonical();
1284 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1285 if (!ArgArray[i]->isCanonical())
1286 isCanonical = false;
1287
1288 // If this type isn't canonical, get the canonical version of it.
1289 QualType Canonical;
1290 if (!isCanonical) {
1291 llvm::SmallVector<QualType, 16> CanonicalArgs;
1292 CanonicalArgs.reserve(NumArgs);
1293 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001294 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001295
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001296 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001297 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001298 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001299
1300 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001301 FunctionProtoType *NewIP =
1302 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001303 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001304 }
1305
Douglas Gregor4fa58902009-02-26 23:50:07 +00001306 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001307 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001308 FunctionProtoType *FTP =
1309 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001310 NumArgs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001311 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001312 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001313 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001314 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001315 return QualType(FTP, 0);
1316}
1317
Douglas Gregor1d661552008-04-13 21:07:44 +00001318/// getTypeDeclType - Return the unique reference to the type for the
1319/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001320QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001321 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001322 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1323
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001324 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001325 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001326 else if (isa<TemplateTypeParmDecl>(Decl)) {
1327 assert(false && "Template type parameter types are always available.");
1328 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001329 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001330
Douglas Gregor2e047592009-02-28 01:32:25 +00001331 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001332 if (PrevDecl)
1333 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001334 else
1335 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001336 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001337 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1338 if (PrevDecl)
1339 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001340 else
1341 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001342 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001343 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001344 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001345
Ted Kremenek46a837c2008-09-05 17:16:31 +00001346 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001347 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001348}
1349
Chris Lattner4b009652007-07-25 00:24:17 +00001350/// getTypedefType - Return the unique reference to the type for the
1351/// specified typename decl.
1352QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1353 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1354
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001355 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001356 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001357 Types.push_back(Decl->TypeForDecl);
1358 return QualType(Decl->TypeForDecl, 0);
1359}
1360
Ted Kremenek42730c52008-01-07 19:49:32 +00001361/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001362/// specified ObjC interface decl.
Daniel Dunbarbe1ff272009-04-22 04:34:53 +00001363QualType ASTContext::getObjCInterfaceType(const ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001364 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1365
Daniel Dunbarbe1ff272009-04-22 04:34:53 +00001366 ObjCInterfaceDecl *OID = const_cast<ObjCInterfaceDecl*>(Decl);
1367 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, OID);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001368 Types.push_back(Decl->TypeForDecl);
1369 return QualType(Decl->TypeForDecl, 0);
1370}
1371
Douglas Gregora4918772009-02-05 23:33:38 +00001372/// \brief Retrieve the template type parameter type for a template
1373/// parameter with the given depth, index, and (optionally) name.
1374QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1375 IdentifierInfo *Name) {
1376 llvm::FoldingSetNodeID ID;
1377 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1378 void *InsertPos = 0;
1379 TemplateTypeParmType *TypeParm
1380 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1381
1382 if (TypeParm)
1383 return QualType(TypeParm, 0);
1384
1385 if (Name)
1386 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1387 getTemplateTypeParmType(Depth, Index));
1388 else
1389 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1390
1391 Types.push_back(TypeParm);
1392 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1393
1394 return QualType(TypeParm, 0);
1395}
1396
Douglas Gregor8e458f42009-02-09 18:46:07 +00001397QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001398ASTContext::getTemplateSpecializationType(TemplateName Template,
1399 const TemplateArgument *Args,
1400 unsigned NumArgs,
1401 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001402 if (!Canon.isNull())
1403 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001404
Douglas Gregor8e458f42009-02-09 18:46:07 +00001405 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001406 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001407
Douglas Gregor8e458f42009-02-09 18:46:07 +00001408 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001409 TemplateSpecializationType *Spec
1410 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001411
1412 if (Spec)
1413 return QualType(Spec, 0);
1414
Douglas Gregordd13e842009-03-30 22:58:21 +00001415 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001416 sizeof(TemplateArgument) * NumArgs),
1417 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001418 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001419 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001420 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001421
1422 return QualType(Spec, 0);
1423}
1424
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001425QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001426ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001427 QualType NamedType) {
1428 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001429 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001430
1431 void *InsertPos = 0;
1432 QualifiedNameType *T
1433 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1434 if (T)
1435 return QualType(T, 0);
1436
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001437 T = new (*this) QualifiedNameType(NNS, NamedType,
1438 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001439 Types.push_back(T);
1440 QualifiedNameTypes.InsertNode(T, InsertPos);
1441 return QualType(T, 0);
1442}
1443
Douglas Gregord3022602009-03-27 23:10:48 +00001444QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1445 const IdentifierInfo *Name,
1446 QualType Canon) {
1447 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1448
1449 if (Canon.isNull()) {
1450 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1451 if (CanonNNS != NNS)
1452 Canon = getTypenameType(CanonNNS, Name);
1453 }
1454
1455 llvm::FoldingSetNodeID ID;
1456 TypenameType::Profile(ID, NNS, Name);
1457
1458 void *InsertPos = 0;
1459 TypenameType *T
1460 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1461 if (T)
1462 return QualType(T, 0);
1463
1464 T = new (*this) TypenameType(NNS, Name, Canon);
1465 Types.push_back(T);
1466 TypenameTypes.InsertNode(T, InsertPos);
1467 return QualType(T, 0);
1468}
1469
Douglas Gregor77da5802009-04-01 00:28:59 +00001470QualType
1471ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1472 const TemplateSpecializationType *TemplateId,
1473 QualType Canon) {
1474 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1475
1476 if (Canon.isNull()) {
1477 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1478 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1479 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1480 const TemplateSpecializationType *CanonTemplateId
1481 = CanonType->getAsTemplateSpecializationType();
1482 assert(CanonTemplateId &&
1483 "Canonical type must also be a template specialization type");
1484 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1485 }
1486 }
1487
1488 llvm::FoldingSetNodeID ID;
1489 TypenameType::Profile(ID, NNS, TemplateId);
1490
1491 void *InsertPos = 0;
1492 TypenameType *T
1493 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1494 if (T)
1495 return QualType(T, 0);
1496
1497 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1498 Types.push_back(T);
1499 TypenameTypes.InsertNode(T, InsertPos);
1500 return QualType(T, 0);
1501}
1502
Chris Lattnere1352302008-04-07 04:56:42 +00001503/// CmpProtocolNames - Comparison predicate for sorting protocols
1504/// alphabetically.
1505static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1506 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001507 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001508}
1509
1510static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1511 unsigned &NumProtocols) {
1512 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1513
1514 // Sort protocols, keyed by name.
1515 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1516
1517 // Remove duplicates.
1518 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1519 NumProtocols = ProtocolsEnd-Protocols;
1520}
1521
1522
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001523/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1524/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001525QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1526 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001527 // Sort the protocol list alphabetically to canonicalize it.
1528 SortAndUniqueProtocols(Protocols, NumProtocols);
1529
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001530 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001531 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001532
1533 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001534 if (ObjCQualifiedInterfaceType *QT =
1535 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001536 return QualType(QT, 0);
1537
1538 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001539 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001540 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001541
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001542 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001543 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001544 return QualType(QType, 0);
1545}
1546
Chris Lattnere1352302008-04-07 04:56:42 +00001547/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1548/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001549QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001550 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001551 // Sort the protocol list alphabetically to canonicalize it.
1552 SortAndUniqueProtocols(Protocols, NumProtocols);
1553
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001554 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001555 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001556
1557 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001558 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001559 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001560 return QualType(QT, 0);
1561
1562 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001563 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001564 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001565 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001566 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001567 return QualType(QType, 0);
1568}
1569
Douglas Gregor4fa58902009-02-26 23:50:07 +00001570/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1571/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001572/// multiple declarations that refer to "typeof(x)" all contain different
1573/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1574/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001575QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001576 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001577 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001578 Types.push_back(toe);
1579 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001580}
1581
Steve Naroff0604dd92007-08-01 18:02:17 +00001582/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1583/// TypeOfType AST's. The only motivation to unique these nodes would be
1584/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1585/// an issue. This doesn't effect the type checker, since it operates
1586/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001587QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001588 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001589 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001590 Types.push_back(tot);
1591 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001592}
1593
Chris Lattner4b009652007-07-25 00:24:17 +00001594/// getTagDeclType - Return the unique reference to the type for the
1595/// specified TagDecl (struct/union/class/enum) decl.
1596QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001597 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001598 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001599}
1600
1601/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1602/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1603/// needs to agree with the definition in <stddef.h>.
1604QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001605 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001606}
1607
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001608/// getSignedWCharType - Return the type of "signed wchar_t".
1609/// Used when in C++, as a GCC extension.
1610QualType ASTContext::getSignedWCharType() const {
1611 // FIXME: derive from "Target" ?
1612 return WCharTy;
1613}
1614
1615/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1616/// Used when in C++, as a GCC extension.
1617QualType ASTContext::getUnsignedWCharType() const {
1618 // FIXME: derive from "Target" ?
1619 return UnsignedIntTy;
1620}
1621
Chris Lattner4b009652007-07-25 00:24:17 +00001622/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1623/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1624QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001625 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001626}
1627
Chris Lattner19eb97e2008-04-02 05:18:44 +00001628//===----------------------------------------------------------------------===//
1629// Type Operators
1630//===----------------------------------------------------------------------===//
1631
Chris Lattner3dae6f42008-04-06 22:41:35 +00001632/// getCanonicalType - Return the canonical (structural) type corresponding to
1633/// the specified potentially non-canonical type. The non-canonical version
1634/// of a type may have many "decorated" versions of types. Decorators can
1635/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1636/// to be free of any of these, allowing two canonical types to be compared
1637/// for exact equality with a simple pointer comparison.
1638QualType ASTContext::getCanonicalType(QualType T) {
1639 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001640
1641 // If the result has type qualifiers, make sure to canonicalize them as well.
1642 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1643 if (TypeQuals == 0) return CanType;
1644
1645 // If the type qualifiers are on an array type, get the canonical type of the
1646 // array with the qualifiers applied to the element type.
1647 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1648 if (!AT)
1649 return CanType.getQualifiedType(TypeQuals);
1650
1651 // Get the canonical version of the element with the extra qualifiers on it.
1652 // This can recursively sink qualifiers through multiple levels of arrays.
1653 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1654 NewEltTy = getCanonicalType(NewEltTy);
1655
1656 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1657 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1658 CAT->getIndexTypeQualifier());
1659 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1660 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1661 IAT->getIndexTypeQualifier());
1662
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001663 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1664 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1665 DSAT->getSizeModifier(),
1666 DSAT->getIndexTypeQualifier());
1667
Chris Lattnera1923f62008-08-04 07:31:14 +00001668 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1669 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1670 VAT->getSizeModifier(),
1671 VAT->getIndexTypeQualifier());
1672}
1673
Douglas Gregord3022602009-03-27 23:10:48 +00001674NestedNameSpecifier *
1675ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1676 if (!NNS)
1677 return 0;
1678
1679 switch (NNS->getKind()) {
1680 case NestedNameSpecifier::Identifier:
1681 // Canonicalize the prefix but keep the identifier the same.
1682 return NestedNameSpecifier::Create(*this,
1683 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1684 NNS->getAsIdentifier());
1685
1686 case NestedNameSpecifier::Namespace:
1687 // A namespace is canonical; build a nested-name-specifier with
1688 // this namespace and no prefix.
1689 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1690
1691 case NestedNameSpecifier::TypeSpec:
1692 case NestedNameSpecifier::TypeSpecWithTemplate: {
1693 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1694 NestedNameSpecifier *Prefix = 0;
1695
1696 // FIXME: This isn't the right check!
1697 if (T->isDependentType())
1698 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1699
1700 return NestedNameSpecifier::Create(*this, Prefix,
1701 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1702 T.getTypePtr());
1703 }
1704
1705 case NestedNameSpecifier::Global:
1706 // The global specifier is canonical and unique.
1707 return NNS;
1708 }
1709
1710 // Required to silence a GCC warning
1711 return 0;
1712}
1713
Chris Lattnera1923f62008-08-04 07:31:14 +00001714
1715const ArrayType *ASTContext::getAsArrayType(QualType T) {
1716 // Handle the non-qualified case efficiently.
1717 if (T.getCVRQualifiers() == 0) {
1718 // Handle the common positive case fast.
1719 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1720 return AT;
1721 }
1722
1723 // Handle the common negative case fast, ignoring CVR qualifiers.
1724 QualType CType = T->getCanonicalTypeInternal();
1725
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001726 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001727 // test.
1728 if (!isa<ArrayType>(CType) &&
1729 !isa<ArrayType>(CType.getUnqualifiedType()))
1730 return 0;
1731
1732 // Apply any CVR qualifiers from the array type to the element type. This
1733 // implements C99 6.7.3p8: "If the specification of an array type includes
1734 // any type qualifiers, the element type is so qualified, not the array type."
1735
1736 // If we get here, we either have type qualifiers on the type, or we have
1737 // sugar such as a typedef in the way. If we have type qualifiers on the type
1738 // we must propagate them down into the elemeng type.
1739 unsigned CVRQuals = T.getCVRQualifiers();
1740 unsigned AddrSpace = 0;
1741 Type *Ty = T.getTypePtr();
1742
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001743 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001744 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001745 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1746 AddrSpace = EXTQT->getAddressSpace();
1747 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001748 } else {
1749 T = Ty->getDesugaredType();
1750 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1751 break;
1752 CVRQuals |= T.getCVRQualifiers();
1753 Ty = T.getTypePtr();
1754 }
1755 }
1756
1757 // If we have a simple case, just return now.
1758 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1759 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1760 return ATy;
1761
1762 // Otherwise, we have an array and we have qualifiers on it. Push the
1763 // qualifiers into the array element type and return a new array type.
1764 // Get the canonical version of the element with the extra qualifiers on it.
1765 // This can recursively sink qualifiers through multiple levels of arrays.
1766 QualType NewEltTy = ATy->getElementType();
1767 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001768 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001769 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1770
1771 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1772 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1773 CAT->getSizeModifier(),
1774 CAT->getIndexTypeQualifier()));
1775 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1776 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1777 IAT->getSizeModifier(),
1778 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001779
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001780 if (const DependentSizedArrayType *DSAT
1781 = dyn_cast<DependentSizedArrayType>(ATy))
1782 return cast<ArrayType>(
1783 getDependentSizedArrayType(NewEltTy,
1784 DSAT->getSizeExpr(),
1785 DSAT->getSizeModifier(),
1786 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001787
Chris Lattnera1923f62008-08-04 07:31:14 +00001788 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1789 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1790 VAT->getSizeModifier(),
1791 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001792}
1793
1794
Chris Lattner19eb97e2008-04-02 05:18:44 +00001795/// getArrayDecayedType - Return the properly qualified result of decaying the
1796/// specified array type to a pointer. This operation is non-trivial when
1797/// handling typedefs etc. The canonical type of "T" must be an array type,
1798/// this returns a pointer to a properly qualified element of the array.
1799///
1800/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1801QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001802 // Get the element type with 'getAsArrayType' so that we don't lose any
1803 // typedefs in the element type of the array. This also handles propagation
1804 // of type qualifiers from the array type into the element type if present
1805 // (C99 6.7.3p8).
1806 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1807 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001808
Chris Lattnera1923f62008-08-04 07:31:14 +00001809 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001810
1811 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001812 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001813}
1814
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001815QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001816 QualType ElemTy = VAT->getElementType();
1817
1818 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1819 return getBaseElementType(VAT);
1820
1821 return ElemTy;
1822}
1823
Chris Lattner4b009652007-07-25 00:24:17 +00001824/// getFloatingRank - Return a relative rank for floating point types.
1825/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001826static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001827 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001828 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001829
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001830 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001831 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001832 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001833 case BuiltinType::Float: return FloatRank;
1834 case BuiltinType::Double: return DoubleRank;
1835 case BuiltinType::LongDouble: return LongDoubleRank;
1836 }
1837}
1838
Steve Narofffa0c4532007-08-27 01:41:48 +00001839/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1840/// point or a complex type (based on typeDomain/typeSize).
1841/// 'typeDomain' is a real floating point or complex type.
1842/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001843QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1844 QualType Domain) const {
1845 FloatingRank EltRank = getFloatingRank(Size);
1846 if (Domain->isComplexType()) {
1847 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001848 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001849 case FloatRank: return FloatComplexTy;
1850 case DoubleRank: return DoubleComplexTy;
1851 case LongDoubleRank: return LongDoubleComplexTy;
1852 }
Chris Lattner4b009652007-07-25 00:24:17 +00001853 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001854
1855 assert(Domain->isRealFloatingType() && "Unknown domain!");
1856 switch (EltRank) {
1857 default: assert(0 && "getFloatingRank(): illegal value for rank");
1858 case FloatRank: return FloatTy;
1859 case DoubleRank: return DoubleTy;
1860 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001861 }
Chris Lattner4b009652007-07-25 00:24:17 +00001862}
1863
Chris Lattner51285d82008-04-06 23:55:33 +00001864/// getFloatingTypeOrder - Compare the rank of the two specified floating
1865/// point types, ignoring the domain of the type (i.e. 'double' ==
1866/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1867/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001868int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1869 FloatingRank LHSR = getFloatingRank(LHS);
1870 FloatingRank RHSR = getFloatingRank(RHS);
1871
1872 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001873 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001874 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001875 return 1;
1876 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001877}
1878
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001879/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1880/// routine will assert if passed a built-in type that isn't an integer or enum,
1881/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001882unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001883 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001884 if (EnumType* ET = dyn_cast<EnumType>(T))
1885 T = ET->getDecl()->getIntegerType().getTypePtr();
1886
1887 // There are two things which impact the integer rank: the width, and
1888 // the ordering of builtins. The builtin ordering is encoded in the
1889 // bottom three bits; the width is encoded in the bits above that.
1890 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1891 return FWIT->getWidth() << 3;
1892 }
1893
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001894 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001895 default: assert(0 && "getIntegerRank(): not a built-in integer");
1896 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001897 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001898 case BuiltinType::Char_S:
1899 case BuiltinType::Char_U:
1900 case BuiltinType::SChar:
1901 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001902 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001903 case BuiltinType::Short:
1904 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001905 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001906 case BuiltinType::Int:
1907 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001908 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001909 case BuiltinType::Long:
1910 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001911 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001912 case BuiltinType::LongLong:
1913 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001914 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001915 }
1916}
1917
Chris Lattner51285d82008-04-06 23:55:33 +00001918/// getIntegerTypeOrder - Returns the highest ranked integer type:
1919/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1920/// LHS < RHS, return -1.
1921int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001922 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1923 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001924 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001925
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001926 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1927 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001928
Chris Lattner51285d82008-04-06 23:55:33 +00001929 unsigned LHSRank = getIntegerRank(LHSC);
1930 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001931
Chris Lattner51285d82008-04-06 23:55:33 +00001932 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1933 if (LHSRank == RHSRank) return 0;
1934 return LHSRank > RHSRank ? 1 : -1;
1935 }
Chris Lattner4b009652007-07-25 00:24:17 +00001936
Chris Lattner51285d82008-04-06 23:55:33 +00001937 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1938 if (LHSUnsigned) {
1939 // If the unsigned [LHS] type is larger, return it.
1940 if (LHSRank >= RHSRank)
1941 return 1;
1942
1943 // If the signed type can represent all values of the unsigned type, it
1944 // wins. Because we are dealing with 2's complement and types that are
1945 // powers of two larger than each other, this is always safe.
1946 return -1;
1947 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001948
Chris Lattner51285d82008-04-06 23:55:33 +00001949 // If the unsigned [RHS] type is larger, return it.
1950 if (RHSRank >= LHSRank)
1951 return -1;
1952
1953 // If the signed type can represent all values of the unsigned type, it
1954 // wins. Because we are dealing with 2's complement and types that are
1955 // powers of two larger than each other, this is always safe.
1956 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001957}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001958
1959// getCFConstantStringType - Return the type used for constant CFStrings.
1960QualType ASTContext::getCFConstantStringType() {
1961 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001962 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001963 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001964 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001965 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001966
1967 // const int *isa;
1968 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001969 // int flags;
1970 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001971 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001972 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001973 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001974 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001975
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001976 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001977 for (unsigned i = 0; i < 4; ++i) {
1978 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1979 SourceLocation(), 0,
1980 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001981 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001982 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001983 }
1984
1985 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001986 }
1987
1988 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001989}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001990
Anders Carlssonf58cac72008-08-30 19:34:46 +00001991QualType ASTContext::getObjCFastEnumerationStateType()
1992{
1993 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001994 ObjCFastEnumerationStateTypeDecl =
1995 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1996 &Idents.get("__objcFastEnumerationState"));
1997
Anders Carlssonf58cac72008-08-30 19:34:46 +00001998 QualType FieldTypes[] = {
1999 UnsignedLongTy,
2000 getPointerType(ObjCIdType),
2001 getPointerType(UnsignedLongTy),
2002 getConstantArrayType(UnsignedLongTy,
2003 llvm::APInt(32, 5), ArrayType::Normal, 0)
2004 };
2005
Douglas Gregor8acb7272008-12-11 16:49:14 +00002006 for (size_t i = 0; i < 4; ++i) {
2007 FieldDecl *Field = FieldDecl::Create(*this,
2008 ObjCFastEnumerationStateTypeDecl,
2009 SourceLocation(), 0,
2010 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002011 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002012 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002013 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00002014
Douglas Gregor8acb7272008-12-11 16:49:14 +00002015 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00002016 }
2017
2018 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2019}
2020
Anders Carlssone3f02572007-10-29 06:33:42 +00002021// This returns true if a type has been typedefed to BOOL:
2022// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00002023static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002024 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00002025 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2026 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002027
2028 return false;
2029}
2030
Ted Kremenek42730c52008-01-07 19:49:32 +00002031/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002032/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00002033int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00002034 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002035
2036 // Make all integer and enum types at least as large as an int
2037 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002038 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002039 // Treat arrays as pointers, since that's how they're passed in.
2040 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002041 sz = getTypeSize(VoidPtrTy);
2042 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002043}
2044
Ted Kremenek42730c52008-01-07 19:49:32 +00002045/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002046/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002047void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00002048 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002049 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002050 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00002051 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002052 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002053 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002054 // Compute size of all parameters.
2055 // Start with computing size of a pointer in number of bytes.
2056 // FIXME: There might(should) be a better way of doing this computation!
2057 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002058 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002059 // The first two arguments (self and _cmd) are pointers; account for
2060 // their size.
2061 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002062 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2063 E = Decl->param_end(); PI != E; ++PI) {
2064 QualType PType = (*PI)->getType();
2065 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002066 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002067 ParmOffset += sz;
2068 }
2069 S += llvm::utostr(ParmOffset);
2070 S += "@0:";
2071 S += llvm::utostr(PtrSize);
2072
2073 // Argument types.
2074 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002075 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2076 E = Decl->param_end(); PI != E; ++PI) {
2077 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002078 QualType PType = PVDecl->getOriginalType();
2079 if (const ArrayType *AT =
Steve Naroff78380fb2009-04-14 00:03:58 +00002080 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2081 // Use array's original type only if it has known number of
2082 // elements.
Steve Naroff6777bf32009-04-14 00:40:09 +00002083 if (!isa<ConstantArrayType>(AT))
Steve Naroff78380fb2009-04-14 00:03:58 +00002084 PType = PVDecl->getType();
2085 } else if (PType->isFunctionType())
2086 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002087 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002088 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002089 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002090 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002091 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002092 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002093 }
2094}
2095
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002096/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002097/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002098/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2099/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002100/// Property attributes are stored as a comma-delimited C string. The simple
2101/// attributes readonly and bycopy are encoded as single characters. The
2102/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2103/// encoded as single characters, followed by an identifier. Property types
2104/// are also encoded as a parametrized attribute. The characters used to encode
2105/// these attributes are defined by the following enumeration:
2106/// @code
2107/// enum PropertyAttributes {
2108/// kPropertyReadOnly = 'R', // property is read-only.
2109/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2110/// kPropertyByref = '&', // property is a reference to the value last assigned
2111/// kPropertyDynamic = 'D', // property is dynamic
2112/// kPropertyGetter = 'G', // followed by getter selector name
2113/// kPropertySetter = 'S', // followed by setter selector name
2114/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2115/// kPropertyType = 't' // followed by old-style type encoding.
2116/// kPropertyWeak = 'W' // 'weak' property
2117/// kPropertyStrong = 'P' // property GC'able
2118/// kPropertyNonAtomic = 'N' // property non-atomic
2119/// };
2120/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002121void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2122 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002123 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002124 // Collect information from the property implementation decl(s).
2125 bool Dynamic = false;
2126 ObjCPropertyImplDecl *SynthesizePID = 0;
2127
2128 // FIXME: Duplicated code due to poor abstraction.
2129 if (Container) {
2130 if (const ObjCCategoryImplDecl *CID =
2131 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2132 for (ObjCCategoryImplDecl::propimpl_iterator
2133 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2134 ObjCPropertyImplDecl *PID = *i;
2135 if (PID->getPropertyDecl() == PD) {
2136 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2137 Dynamic = true;
2138 } else {
2139 SynthesizePID = PID;
2140 }
2141 }
2142 }
2143 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002144 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002145 for (ObjCCategoryImplDecl::propimpl_iterator
2146 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2147 ObjCPropertyImplDecl *PID = *i;
2148 if (PID->getPropertyDecl() == PD) {
2149 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2150 Dynamic = true;
2151 } else {
2152 SynthesizePID = PID;
2153 }
2154 }
2155 }
2156 }
2157 }
2158
2159 // FIXME: This is not very efficient.
2160 S = "T";
2161
2162 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002163 // GCC has some special rules regarding encoding of properties which
2164 // closely resembles encoding of ivars.
Daniel Dunbar701c8502009-04-20 06:37:24 +00002165 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002166 true /* outermost type */,
2167 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002168
2169 if (PD->isReadOnly()) {
2170 S += ",R";
2171 } else {
2172 switch (PD->getSetterKind()) {
2173 case ObjCPropertyDecl::Assign: break;
2174 case ObjCPropertyDecl::Copy: S += ",C"; break;
2175 case ObjCPropertyDecl::Retain: S += ",&"; break;
2176 }
2177 }
2178
2179 // It really isn't clear at all what this means, since properties
2180 // are "dynamic by default".
2181 if (Dynamic)
2182 S += ",D";
2183
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002184 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2185 S += ",N";
2186
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002187 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2188 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002189 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002190 }
2191
2192 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2193 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002194 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002195 }
2196
2197 if (SynthesizePID) {
2198 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2199 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002200 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002201 }
2202
2203 // FIXME: OBJCGC: weak & strong
2204}
2205
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002206/// getLegacyIntegralTypeEncoding -
2207/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002208/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002209/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2210///
2211void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2212 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2213 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002214 if (BT->getKind() == BuiltinType::ULong &&
2215 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002216 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002217 else
2218 if (BT->getKind() == BuiltinType::Long &&
2219 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002220 PointeeTy = IntTy;
2221 }
2222 }
2223}
2224
Fariborz Jahanian248db262008-01-22 22:44:46 +00002225void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002226 const FieldDecl *Field) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002227 // We follow the behavior of gcc, expanding structures which are
2228 // directly pointed to, and expanding embedded structures. Note that
2229 // these rules are sufficient to prevent recursive encoding of the
2230 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002231 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2232 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002233}
2234
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002235static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002236 const FieldDecl *FD) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002237 const Expr *E = FD->getBitWidth();
2238 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2239 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2240 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2241 S += 'b';
2242 S += llvm::utostr(N);
2243}
2244
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002245void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2246 bool ExpandPointedToStructures,
2247 bool ExpandStructures,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002248 const FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002249 bool OutermostType,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002250 bool EncodingProperty) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002251 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002252 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002253 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002254 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002255 else {
2256 char encoding;
2257 switch (BT->getKind()) {
2258 default: assert(0 && "Unhandled builtin type kind");
2259 case BuiltinType::Void: encoding = 'v'; break;
2260 case BuiltinType::Bool: encoding = 'B'; break;
2261 case BuiltinType::Char_U:
2262 case BuiltinType::UChar: encoding = 'C'; break;
2263 case BuiltinType::UShort: encoding = 'S'; break;
2264 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002265 case BuiltinType::ULong:
2266 encoding =
2267 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2268 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002269 case BuiltinType::ULongLong: encoding = 'Q'; break;
2270 case BuiltinType::Char_S:
2271 case BuiltinType::SChar: encoding = 'c'; break;
2272 case BuiltinType::Short: encoding = 's'; break;
2273 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002274 case BuiltinType::Long:
2275 encoding =
2276 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2277 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002278 case BuiltinType::LongLong: encoding = 'q'; break;
2279 case BuiltinType::Float: encoding = 'f'; break;
2280 case BuiltinType::Double: encoding = 'd'; break;
2281 case BuiltinType::LongDouble: encoding = 'd'; break;
2282 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002283
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002284 S += encoding;
2285 }
Anders Carlsson70e16dd2009-04-09 21:55:45 +00002286 } else if (const ComplexType *CT = T->getAsComplexType()) {
2287 S += 'j';
2288 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2289 false);
2290 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002291 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2292 ExpandPointedToStructures,
2293 ExpandStructures, FD);
2294 if (FD || EncodingProperty) {
2295 // Note that we do extended encoding of protocol qualifer list
2296 // Only when doing ivar or property encoding.
2297 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2298 S += '"';
2299 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2300 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2301 S += '<';
2302 S += Proto->getNameAsString();
2303 S += '>';
2304 }
2305 S += '"';
2306 }
2307 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002308 }
2309 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002310 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002311 bool isReadOnly = false;
2312 // For historical/compatibility reasons, the read-only qualifier of the
2313 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2314 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2315 // Also, do not emit the 'r' for anything but the outermost type!
2316 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2317 if (OutermostType && T.isConstQualified()) {
2318 isReadOnly = true;
2319 S += 'r';
2320 }
2321 }
2322 else if (OutermostType) {
2323 QualType P = PointeeTy;
2324 while (P->getAsPointerType())
2325 P = P->getAsPointerType()->getPointeeType();
2326 if (P.isConstQualified()) {
2327 isReadOnly = true;
2328 S += 'r';
2329 }
2330 }
2331 if (isReadOnly) {
2332 // Another legacy compatibility encoding. Some ObjC qualifier and type
2333 // combinations need to be rearranged.
2334 // Rewrite "in const" from "nr" to "rn"
2335 const char * s = S.c_str();
2336 int len = S.length();
2337 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2338 std::string replace = "rn";
2339 S.replace(S.end()-2, S.end(), replace);
2340 }
2341 }
Steve Naroff17c03822009-02-12 17:52:19 +00002342 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002343 S += '@';
2344 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002345 }
2346 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002347 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002348 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002349 // Another historical/compatibility reason.
2350 // We encode the underlying type which comes out as
2351 // {...};
2352 S += '^';
2353 getObjCEncodingForTypeImpl(PointeeTy, S,
2354 false, ExpandPointedToStructures,
2355 NULL);
2356 return;
2357 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002358 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002359 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002360 const ObjCInterfaceType *OIT =
2361 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002362 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002363 S += '"';
2364 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002365 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2366 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2367 S += '<';
2368 S += Proto->getNameAsString();
2369 S += '>';
2370 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002371 S += '"';
2372 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002373 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002374 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002375 S += '#';
2376 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002377 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002378 S += ':';
2379 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002380 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002381
2382 if (PointeeTy->isCharType()) {
2383 // char pointer types should be encoded as '*' unless it is a
2384 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002385 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002386 S += '*';
2387 return;
2388 }
2389 }
2390
2391 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002392 getLegacyIntegralTypeEncoding(PointeeTy);
2393
2394 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002395 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002396 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002397 } else if (const ArrayType *AT =
2398 // Ignore type qualifiers etc.
2399 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002400 if (isa<IncompleteArrayType>(AT)) {
2401 // Incomplete arrays are encoded as a pointer to the array element.
2402 S += '^';
2403
2404 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2405 false, ExpandStructures, FD);
2406 } else {
2407 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002408
Anders Carlsson858c64d2009-02-22 01:38:57 +00002409 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2410 S += llvm::utostr(CAT->getSize().getZExtValue());
2411 else {
2412 //Variable length arrays are encoded as a regular array with 0 elements.
2413 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2414 S += '0';
2415 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002416
Anders Carlsson858c64d2009-02-22 01:38:57 +00002417 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2418 false, ExpandStructures, FD);
2419 S += ']';
2420 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002421 } else if (T->getAsFunctionType()) {
2422 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002423 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002424 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002425 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002426 // Anonymous structures print as '?'
2427 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2428 S += II->getName();
2429 } else {
2430 S += '?';
2431 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002432 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002433 S += '=';
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002434 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2435 FieldEnd = RDecl->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002436 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002437 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002438 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002439 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002440 S += '"';
2441 }
2442
2443 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002444 if (Field->isBitField()) {
2445 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2446 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002447 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002448 QualType qt = Field->getType();
2449 getLegacyIntegralTypeEncoding(qt);
2450 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002451 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002452 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002453 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002454 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002455 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002456 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002457 if (FD && FD->isBitField())
2458 EncodeBitField(this, S, FD);
2459 else
2460 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002461 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002462 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002463 } else if (T->isObjCInterfaceType()) {
2464 // @encode(class_name)
2465 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2466 S += '{';
2467 const IdentifierInfo *II = OI->getIdentifier();
2468 S += II->getName();
2469 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002470 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002471 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002472 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002473 if (RecFields[i]->isBitField())
2474 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2475 RecFields[i]);
2476 else
2477 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2478 FD);
2479 }
2480 S += '}';
2481 }
2482 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002483 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002484}
2485
Ted Kremenek42730c52008-01-07 19:49:32 +00002486void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002487 std::string& S) const {
2488 if (QT & Decl::OBJC_TQ_In)
2489 S += 'n';
2490 if (QT & Decl::OBJC_TQ_Inout)
2491 S += 'N';
2492 if (QT & Decl::OBJC_TQ_Out)
2493 S += 'o';
2494 if (QT & Decl::OBJC_TQ_Bycopy)
2495 S += 'O';
2496 if (QT & Decl::OBJC_TQ_Byref)
2497 S += 'R';
2498 if (QT & Decl::OBJC_TQ_Oneway)
2499 S += 'V';
2500}
2501
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002502void ASTContext::setBuiltinVaListType(QualType T)
2503{
2504 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2505
2506 BuiltinVaListType = T;
2507}
2508
Ted Kremenek42730c52008-01-07 19:49:32 +00002509void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002510{
Ted Kremenek42730c52008-01-07 19:49:32 +00002511 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002512
2513 // typedef struct objc_object *id;
2514 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002515 // User error - caller will issue diagnostics.
2516 if (!ptr)
2517 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002518 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002519 // User error - caller will issue diagnostics.
2520 if (!rec)
2521 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002522 IdStructType = rec;
2523}
2524
Ted Kremenek42730c52008-01-07 19:49:32 +00002525void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002526{
Ted Kremenek42730c52008-01-07 19:49:32 +00002527 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002528
2529 // typedef struct objc_selector *SEL;
2530 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002531 if (!ptr)
2532 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002533 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002534 if (!rec)
2535 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002536 SelStructType = rec;
2537}
2538
Ted Kremenek42730c52008-01-07 19:49:32 +00002539void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002540{
Ted Kremenek42730c52008-01-07 19:49:32 +00002541 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002542}
2543
Ted Kremenek42730c52008-01-07 19:49:32 +00002544void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002545{
Ted Kremenek42730c52008-01-07 19:49:32 +00002546 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002547
2548 // typedef struct objc_class *Class;
2549 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2550 assert(ptr && "'Class' incorrectly typed");
2551 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2552 assert(rec && "'Class' incorrectly typed");
2553 ClassStructType = rec;
2554}
2555
Ted Kremenek42730c52008-01-07 19:49:32 +00002556void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2557 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002558 "'NSConstantString' type already set!");
2559
Ted Kremenek42730c52008-01-07 19:49:32 +00002560 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002561}
2562
Douglas Gregordd13e842009-03-30 22:58:21 +00002563/// \brief Retrieve the template name that represents a qualified
2564/// template name such as \c std::vector.
2565TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2566 bool TemplateKeyword,
2567 TemplateDecl *Template) {
2568 llvm::FoldingSetNodeID ID;
2569 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2570
2571 void *InsertPos = 0;
2572 QualifiedTemplateName *QTN =
2573 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2574 if (!QTN) {
2575 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2576 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2577 }
2578
2579 return TemplateName(QTN);
2580}
2581
2582/// \brief Retrieve the template name that represents a dependent
2583/// template name such as \c MetaFun::template apply.
2584TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2585 const IdentifierInfo *Name) {
2586 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2587
2588 llvm::FoldingSetNodeID ID;
2589 DependentTemplateName::Profile(ID, NNS, Name);
2590
2591 void *InsertPos = 0;
2592 DependentTemplateName *QTN =
2593 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2594
2595 if (QTN)
2596 return TemplateName(QTN);
2597
2598 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2599 if (CanonNNS == NNS) {
2600 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2601 } else {
2602 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2603 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2604 }
2605
2606 DependentTemplateNames.InsertNode(QTN, InsertPos);
2607 return TemplateName(QTN);
2608}
2609
Douglas Gregorc6507e42008-11-03 14:12:49 +00002610/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002611/// TargetInfo, produce the corresponding type. The unsigned @p Type
2612/// is actually a value of type @c TargetInfo::IntType.
2613QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002614 switch (Type) {
2615 case TargetInfo::NoInt: return QualType();
2616 case TargetInfo::SignedShort: return ShortTy;
2617 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2618 case TargetInfo::SignedInt: return IntTy;
2619 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2620 case TargetInfo::SignedLong: return LongTy;
2621 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2622 case TargetInfo::SignedLongLong: return LongLongTy;
2623 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2624 }
2625
2626 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002627 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002628}
Ted Kremenek118930e2008-07-24 23:58:27 +00002629
2630//===----------------------------------------------------------------------===//
2631// Type Predicates.
2632//===----------------------------------------------------------------------===//
2633
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002634/// isObjCNSObjectType - Return true if this is an NSObject object using
2635/// NSObject attribute on a c-style pointer type.
2636/// FIXME - Make it work directly on types.
2637///
2638bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2639 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2640 if (TypedefDecl *TD = TDT->getDecl())
2641 if (TD->getAttr<ObjCNSObjectAttr>())
2642 return true;
2643 }
2644 return false;
2645}
2646
Ted Kremenek118930e2008-07-24 23:58:27 +00002647/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2648/// to an object type. This includes "id" and "Class" (two 'special' pointers
2649/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2650/// ID type).
2651bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002652 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002653 return true;
2654
Steve Naroffd9e00802008-10-21 18:24:04 +00002655 // Blocks are objects.
2656 if (Ty->isBlockPointerType())
2657 return true;
2658
2659 // All other object types are pointers.
Chris Lattnera008d172009-04-12 23:51:02 +00002660 const PointerType *PT = Ty->getAsPointerType();
2661 if (PT == 0)
Ted Kremenek118930e2008-07-24 23:58:27 +00002662 return false;
2663
Chris Lattnera008d172009-04-12 23:51:02 +00002664 // If this a pointer to an interface (e.g. NSString*), it is ok.
2665 if (PT->getPointeeType()->isObjCInterfaceType() ||
2666 // If is has NSObject attribute, OK as well.
2667 isObjCNSObjectType(Ty))
2668 return true;
2669
Ted Kremenek118930e2008-07-24 23:58:27 +00002670 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2671 // pointer types. This looks for the typedef specifically, not for the
Chris Lattnera008d172009-04-12 23:51:02 +00002672 // underlying type. Iteratively strip off typedefs so that we can handle
2673 // typedefs of typedefs.
2674 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2675 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2676 Ty.getUnqualifiedType() == getObjCClassType())
2677 return true;
2678
2679 Ty = TDT->getDecl()->getUnderlyingType();
2680 }
Ted Kremenek118930e2008-07-24 23:58:27 +00002681
Chris Lattnera008d172009-04-12 23:51:02 +00002682 return false;
Ted Kremenek118930e2008-07-24 23:58:27 +00002683}
2684
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002685/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2686/// garbage collection attribute.
2687///
2688QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002689 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002690 if (getLangOptions().ObjC1 &&
2691 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002692 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002693 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002694 // (or pointers to them) be treated as though they were declared
2695 // as __strong.
2696 if (GCAttrs == QualType::GCNone) {
2697 if (isObjCObjectPointerType(Ty))
2698 GCAttrs = QualType::Strong;
2699 else if (Ty->isPointerType())
2700 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2701 }
Fariborz Jahaniand7b01972009-04-11 00:00:54 +00002702 // Non-pointers have none gc'able attribute regardless of the attribute
2703 // set on them.
2704 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2705 return QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002706 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002707 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002708}
2709
Chris Lattner6ff358b2008-04-07 06:51:04 +00002710//===----------------------------------------------------------------------===//
2711// Type Compatibility Testing
2712//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002713
Steve Naroff3454b6c2008-09-04 15:10:53 +00002714/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002715/// block types. Types must be strictly compatible here. For example,
2716/// C unfortunately doesn't produce an error for the following:
2717///
2718/// int (*emptyArgFunc)();
2719/// int (*intArgList)(int) = emptyArgFunc;
2720///
2721/// For blocks, we will produce an error for the following (similar to C++):
2722///
2723/// int (^emptyArgBlock)();
2724/// int (^intArgBlock)(int) = emptyArgBlock;
2725///
2726/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2727///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002728bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002729 const FunctionType *lbase = lhs->getAsFunctionType();
2730 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002731 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2732 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stump896ceed2009-04-01 01:17:39 +00002733 if (lproto && rproto == 0)
2734 return false;
2735 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff3454b6c2008-09-04 15:10:53 +00002736}
2737
Chris Lattner6ff358b2008-04-07 06:51:04 +00002738/// areCompatVectorTypes - Return true if the two specified vector types are
2739/// compatible.
2740static bool areCompatVectorTypes(const VectorType *LHS,
2741 const VectorType *RHS) {
2742 assert(LHS->isCanonical() && RHS->isCanonical());
2743 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002744 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002745}
2746
Eli Friedman0d9549b2008-08-22 00:56:42 +00002747/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002748/// compatible for assignment from RHS to LHS. This handles validation of any
2749/// protocol qualifiers on the LHS or RHS.
2750///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002751bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2752 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002753 // Verify that the base decls are compatible: the RHS must be a subclass of
2754 // the LHS.
2755 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2756 return false;
2757
2758 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2759 // protocol qualified at all, then we are good.
2760 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2761 return true;
2762
2763 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2764 // isn't a superset.
2765 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2766 return true; // FIXME: should return false!
2767
2768 // Finally, we must have two protocol-qualified interfaces.
2769 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2770 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002771
Steve Naroff98e71b82009-03-01 16:12:44 +00002772 // All LHS protocols must have a presence on the RHS.
2773 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002774
Steve Naroff98e71b82009-03-01 16:12:44 +00002775 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2776 LHSPE = LHSP->qual_end();
2777 LHSPI != LHSPE; LHSPI++) {
2778 bool RHSImplementsProtocol = false;
2779
2780 // If the RHS doesn't implement the protocol on the left, the types
2781 // are incompatible.
2782 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2783 RHSPE = RHSP->qual_end();
2784 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2785 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2786 RHSImplementsProtocol = true;
2787 }
2788 // FIXME: For better diagnostics, consider passing back the protocol name.
2789 if (!RHSImplementsProtocol)
2790 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002791 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002792 // The RHS implements all protocols listed on the LHS.
2793 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002794}
2795
Steve Naroff17c03822009-02-12 17:52:19 +00002796bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2797 // get the "pointed to" types
2798 const PointerType *LHSPT = LHS->getAsPointerType();
2799 const PointerType *RHSPT = RHS->getAsPointerType();
2800
2801 if (!LHSPT || !RHSPT)
2802 return false;
2803
2804 QualType lhptee = LHSPT->getPointeeType();
2805 QualType rhptee = RHSPT->getPointeeType();
2806 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2807 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2808 // ID acts sort of like void* for ObjC interfaces
2809 if (LHSIface && isObjCIdStructType(rhptee))
2810 return true;
2811 if (RHSIface && isObjCIdStructType(lhptee))
2812 return true;
2813 if (!LHSIface || !RHSIface)
2814 return false;
2815 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2816 canAssignObjCInterfaces(RHSIface, LHSIface);
2817}
2818
Steve Naroff85f0dc52007-10-15 20:41:53 +00002819/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2820/// both shall have the identically qualified version of a compatible type.
2821/// C99 6.2.7p1: Two types have compatible types if their types are the
2822/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002823bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2824 return !mergeTypes(LHS, RHS).isNull();
2825}
2826
2827QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2828 const FunctionType *lbase = lhs->getAsFunctionType();
2829 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002830 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2831 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002832 bool allLTypes = true;
2833 bool allRTypes = true;
2834
2835 // Check return type
2836 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2837 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002838 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2839 allLTypes = false;
2840 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2841 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002842
2843 if (lproto && rproto) { // two C99 style function prototypes
2844 unsigned lproto_nargs = lproto->getNumArgs();
2845 unsigned rproto_nargs = rproto->getNumArgs();
2846
2847 // Compatible functions must have the same number of arguments
2848 if (lproto_nargs != rproto_nargs)
2849 return QualType();
2850
2851 // Variadic and non-variadic functions aren't compatible
2852 if (lproto->isVariadic() != rproto->isVariadic())
2853 return QualType();
2854
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002855 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2856 return QualType();
2857
Eli Friedman0d9549b2008-08-22 00:56:42 +00002858 // Check argument compatibility
2859 llvm::SmallVector<QualType, 10> types;
2860 for (unsigned i = 0; i < lproto_nargs; i++) {
2861 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2862 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2863 QualType argtype = mergeTypes(largtype, rargtype);
2864 if (argtype.isNull()) return QualType();
2865 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002866 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2867 allLTypes = false;
2868 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2869 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002870 }
2871 if (allLTypes) return lhs;
2872 if (allRTypes) return rhs;
2873 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002874 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002875 }
2876
2877 if (lproto) allRTypes = false;
2878 if (rproto) allLTypes = false;
2879
Douglas Gregor4fa58902009-02-26 23:50:07 +00002880 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002881 if (proto) {
2882 if (proto->isVariadic()) return QualType();
2883 // Check that the types are compatible with the types that
2884 // would result from default argument promotions (C99 6.7.5.3p15).
2885 // The only types actually affected are promotable integer
2886 // types and floats, which would be passed as a different
2887 // type depending on whether the prototype is visible.
2888 unsigned proto_nargs = proto->getNumArgs();
2889 for (unsigned i = 0; i < proto_nargs; ++i) {
2890 QualType argTy = proto->getArgType(i);
2891 if (argTy->isPromotableIntegerType() ||
2892 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2893 return QualType();
2894 }
2895
2896 if (allLTypes) return lhs;
2897 if (allRTypes) return rhs;
2898 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002899 proto->getNumArgs(), lproto->isVariadic(),
2900 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002901 }
2902
2903 if (allLTypes) return lhs;
2904 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002905 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002906}
2907
2908QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002909 // C++ [expr]: If an expression initially has the type "reference to T", the
2910 // type is adjusted to "T" prior to any further analysis, the expression
2911 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002912 // expression is an lvalue unless the reference is an rvalue reference and
2913 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002914 // FIXME: C++ shouldn't be going through here! The rules are different
2915 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00002916 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2917 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002918 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002919 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002920 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002921 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002922
Eli Friedman0d9549b2008-08-22 00:56:42 +00002923 QualType LHSCan = getCanonicalType(LHS),
2924 RHSCan = getCanonicalType(RHS);
2925
2926 // If two types are identical, they are compatible.
2927 if (LHSCan == RHSCan)
2928 return LHS;
2929
2930 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002931 // Note that we handle extended qualifiers later, in the
2932 // case for ExtQualType.
2933 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002934 return QualType();
2935
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00002936 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
2937 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002938
Chris Lattnerc38d4522008-01-14 05:45:46 +00002939 // We want to consider the two function types to be the same for these
2940 // comparisons, just force one to the other.
2941 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2942 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002943
2944 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002945 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2946 LHSClass = Type::ConstantArray;
2947 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2948 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002949
Nate Begemanaf6ed502008-04-18 23:10:10 +00002950 // Canonicalize ExtVector -> Vector.
2951 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2952 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002953
Chris Lattner7cdcb252008-04-07 06:38:24 +00002954 // Consider qualified interfaces and interfaces the same.
2955 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2956 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002957
Chris Lattnerb5709e22008-04-07 05:43:21 +00002958 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002959 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002960 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2961 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00002962
Steve Naroff0773c582009-04-14 15:11:46 +00002963 // 'id' and 'Class' act sort of like void* for ObjC interfaces
2964 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00002965 return LHS;
Steve Naroff0773c582009-04-14 15:11:46 +00002966 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00002967 return RHS;
2968
Steve Naroff28ceff72008-12-10 22:14:21 +00002969 // ID is compatible with all qualified id types.
2970 if (LHS->isObjCQualifiedIdType()) {
2971 if (const PointerType *PT = RHS->getAsPointerType()) {
2972 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00002973 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002974 return LHS;
2975 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2976 // Unfortunately, this API is part of Sema (which we don't have access
2977 // to. Need to refactor. The following check is insufficient, since we
2978 // need to make sure the class implements the protocol.
2979 if (pType->isObjCInterfaceType())
2980 return LHS;
2981 }
2982 }
2983 if (RHS->isObjCQualifiedIdType()) {
2984 if (const PointerType *PT = LHS->getAsPointerType()) {
2985 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00002986 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002987 return RHS;
2988 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2989 // Unfortunately, this API is part of Sema (which we don't have access
2990 // to. Need to refactor. The following check is insufficient, since we
2991 // need to make sure the class implements the protocol.
2992 if (pType->isObjCInterfaceType())
2993 return RHS;
2994 }
2995 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002996 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2997 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002998 if (const EnumType* ETy = LHS->getAsEnumType()) {
2999 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
3000 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003001 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003002 if (const EnumType* ETy = RHS->getAsEnumType()) {
3003 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3004 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003005 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003006
Eli Friedman0d9549b2008-08-22 00:56:42 +00003007 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003008 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003009
Steve Naroffc88babe2008-01-09 22:43:08 +00003010 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003011 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00003012#define TYPE(Class, Base)
3013#define ABSTRACT_TYPE(Class, Base)
3014#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3015#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3016#include "clang/AST/TypeNodes.def"
3017 assert(false && "Non-canonical and dependent types shouldn't get here");
3018 return QualType();
3019
Sebastian Redlce6fff02009-03-16 23:22:08 +00003020 case Type::LValueReference:
3021 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003022 case Type::MemberPointer:
3023 assert(false && "C++ should never be in mergeTypes");
3024 return QualType();
3025
3026 case Type::IncompleteArray:
3027 case Type::VariableArray:
3028 case Type::FunctionProto:
3029 case Type::ExtVector:
3030 case Type::ObjCQualifiedInterface:
3031 assert(false && "Types are eliminated above");
3032 return QualType();
3033
Chris Lattnerc38d4522008-01-14 05:45:46 +00003034 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003035 {
3036 // Merge two pointer types, while trying to preserve typedef info
3037 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3038 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3039 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3040 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003041 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3042 return LHS;
3043 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3044 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003045 return getPointerType(ResultType);
3046 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003047 case Type::BlockPointer:
3048 {
3049 // Merge two block pointer types, while trying to preserve typedef info
3050 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3051 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3052 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3053 if (ResultType.isNull()) return QualType();
3054 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3055 return LHS;
3056 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3057 return RHS;
3058 return getBlockPointerType(ResultType);
3059 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003060 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003061 {
3062 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3063 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3064 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3065 return QualType();
3066
3067 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3068 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3069 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3070 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003071 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3072 return LHS;
3073 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3074 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003075 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3076 ArrayType::ArraySizeModifier(), 0);
3077 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3078 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003079 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3080 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003081 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3082 return LHS;
3083 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3084 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003085 if (LVAT) {
3086 // FIXME: This isn't correct! But tricky to implement because
3087 // the array's size has to be the size of LHS, but the type
3088 // has to be different.
3089 return LHS;
3090 }
3091 if (RVAT) {
3092 // FIXME: This isn't correct! But tricky to implement because
3093 // the array's size has to be the size of RHS, but the type
3094 // has to be different.
3095 return RHS;
3096 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003097 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3098 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003099 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003100 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003101 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003102 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003103 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003104 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003105 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003106 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3107 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003108 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003109 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003110 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003111 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003112 case Type::Complex:
3113 // Distinct complex types are incompatible.
3114 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003115 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003116 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003117 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3118 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003119 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003120 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003121 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003122 // FIXME: This should be type compatibility, e.g. whether
3123 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003124 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3125 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3126 if (LHSIface && RHSIface &&
3127 canAssignObjCInterfaces(LHSIface, RHSIface))
3128 return LHS;
3129
Eli Friedman0d9549b2008-08-22 00:56:42 +00003130 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003131 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003132 case Type::ObjCQualifiedId:
3133 // Distinct qualified id's are not compatible.
3134 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003135 case Type::FixedWidthInt:
3136 // Distinct fixed-width integers are not compatible.
3137 return QualType();
3138 case Type::ObjCQualifiedClass:
3139 // Distinct qualified classes are not compatible.
3140 return QualType();
3141 case Type::ExtQual:
3142 // FIXME: ExtQual types can be compatible even if they're not
3143 // identical!
3144 return QualType();
3145 // First attempt at an implementation, but I'm not really sure it's
3146 // right...
3147#if 0
3148 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3149 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3150 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3151 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3152 return QualType();
3153 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3154 LHSBase = QualType(LQual->getBaseType(), 0);
3155 RHSBase = QualType(RQual->getBaseType(), 0);
3156 ResultType = mergeTypes(LHSBase, RHSBase);
3157 if (ResultType.isNull()) return QualType();
3158 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3159 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3160 return LHS;
3161 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3162 return RHS;
3163 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3164 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3165 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3166 return ResultType;
3167#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003168
3169 case Type::TemplateSpecialization:
3170 assert(false && "Dependent types have no size");
3171 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003172 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003173
3174 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003175}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003176
Chris Lattner1d78a862008-04-07 07:01:58 +00003177//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003178// Integer Predicates
3179//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003180
Eli Friedman0832dbc2008-06-28 06:23:08 +00003181unsigned ASTContext::getIntWidth(QualType T) {
3182 if (T == BoolTy)
3183 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003184 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3185 return FWIT->getWidth();
3186 }
3187 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003188 return (unsigned)getTypeSize(T);
3189}
3190
3191QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3192 assert(T->isSignedIntegerType() && "Unexpected type");
3193 if (const EnumType* ETy = T->getAsEnumType())
3194 T = ETy->getDecl()->getIntegerType();
3195 const BuiltinType* BTy = T->getAsBuiltinType();
3196 assert (BTy && "Unexpected signed integer type");
3197 switch (BTy->getKind()) {
3198 case BuiltinType::Char_S:
3199 case BuiltinType::SChar:
3200 return UnsignedCharTy;
3201 case BuiltinType::Short:
3202 return UnsignedShortTy;
3203 case BuiltinType::Int:
3204 return UnsignedIntTy;
3205 case BuiltinType::Long:
3206 return UnsignedLongTy;
3207 case BuiltinType::LongLong:
3208 return UnsignedLongLongTy;
3209 default:
3210 assert(0 && "Unexpected signed integer type");
3211 return QualType();
3212 }
3213}
3214
3215
3216//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00003217// Serialization Support
3218//===----------------------------------------------------------------------===//
3219
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003220enum {
3221 BasicMetadataBlock = 1,
3222 ASTContextBlock = 2,
3223 DeclsBlock = 3
3224};
3225
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003226void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3227 // Create bitstream.
3228 llvm::BitstreamWriter Stream(Buffer);
3229
3230 // Emit the preamble.
3231 Stream.Emit((unsigned)'B', 8);
3232 Stream.Emit((unsigned)'C', 8);
3233 Stream.Emit(0xC, 4);
3234 Stream.Emit(0xF, 4);
3235 Stream.Emit(0xE, 4);
3236 Stream.Emit(0x0, 4);
3237
3238 // Create serializer.
3239 llvm::Serializer S(Stream);
3240
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003241 // ===---------------------------------------------------===/
3242 // Serialize the "Translation Unit" metadata.
3243 // ===---------------------------------------------------===/
3244
3245 // Emit ASTContext.
3246 S.EnterBlock(ASTContextBlock);
3247 S.EmitOwnedPtr(this);
3248 S.ExitBlock(); // exit "ASTContextBlock"
3249
3250 S.EnterBlock(BasicMetadataBlock);
3251
3252 // Block for SourceManager and Target. Allows easy skipping
3253 // around to the block for the Selectors during deserialization.
3254 S.EnterBlock();
3255
3256 // Emit the SourceManager.
3257 S.Emit(getSourceManager());
3258
3259 // Emit the Target.
3260 S.EmitPtr(&Target);
3261 S.EmitCStr(Target.getTargetTriple());
3262
3263 S.ExitBlock(); // exit "SourceManager and Target Block"
3264
3265 // Emit the Selectors.
3266 S.Emit(Selectors);
3267
3268 // Emit the Identifier Table.
3269 S.Emit(Idents);
3270
3271 S.ExitBlock(); // exit "BasicMetadataBlock"
3272}
3273
3274
Ted Kremenek738e6c02007-10-31 17:10:13 +00003275/// Emit - Serialize an ASTContext object to Bitcode.
3276void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00003277 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00003278 S.EmitRef(SourceMgr);
3279 S.EmitRef(Target);
3280 S.EmitRef(Idents);
3281 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003282
Ted Kremenek68228a92007-10-31 22:44:07 +00003283 // Emit the size of the type vector so that we can reserve that size
3284 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003285 S.EmitInt(Types.size());
3286
Ted Kremenek034a78c2007-11-13 22:02:55 +00003287 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3288 I!=E;++I)
3289 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003290
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003291 S.EmitOwnedPtr(TUDecl);
3292
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003293 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003294}
3295
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003296
3297ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3298 FileManager &FMgr) {
3299 // Check if the file is of the proper length.
3300 if (Buffer.getBufferSize() & 0x3) {
3301 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3302 return 0;
3303 }
3304
3305 // Create the bitstream reader.
3306 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3307 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3308
3309 if (Stream.Read(8) != 'B' ||
3310 Stream.Read(8) != 'C' ||
3311 Stream.Read(4) != 0xC ||
3312 Stream.Read(4) != 0xF ||
3313 Stream.Read(4) != 0xE ||
3314 Stream.Read(4) != 0x0) {
3315 // FIXME: Provide diagnostic.
3316 return NULL;
3317 }
3318
3319 // Create the deserializer.
3320 llvm::Deserializer Dezr(Stream);
3321
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003322 // ===---------------------------------------------------===/
3323 // Deserialize the "Translation Unit" metadata.
3324 // ===---------------------------------------------------===/
3325
3326 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3327 // (which will appear earlier) and record its location.
3328
3329 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3330 assert (FoundBlock);
3331
3332 llvm::Deserializer::Location ASTContextBlockLoc =
3333 Dezr.getCurrentBlockLocation();
3334
3335 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3336 assert (FoundBlock);
3337
3338 // Read the SourceManager.
3339 SourceManager::CreateAndRegister(Dezr, FMgr);
3340
3341 { // Read the TargetInfo.
3342 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3343 char* triple = Dezr.ReadCStr(NULL,0,true);
3344 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3345 delete [] triple;
3346 }
3347
3348 // For Selectors, we must read the identifier table first because the
3349 // SelectorTable depends on the identifiers being already deserialized.
3350 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3351 Dezr.SkipBlock();
3352
3353 // Read the identifier table.
3354 IdentifierTable::CreateAndRegister(Dezr);
3355
3356 // Now jump back and read the selectors.
3357 Dezr.JumpTo(SelectorBlkLoc);
3358 SelectorTable::CreateAndRegister(Dezr);
3359
3360 // Now jump back to ASTContextBlock and read the ASTContext.
3361 Dezr.JumpTo(ASTContextBlockLoc);
3362 return Dezr.ReadOwnedPtr<ASTContext>();
3363}
3364
Ted Kremenekacba3612007-11-13 00:25:37 +00003365ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00003366
3367 // Read the language options.
3368 LangOptions LOpts;
3369 LOpts.Read(D);
3370
Ted Kremenek68228a92007-10-31 22:44:07 +00003371 SourceManager &SM = D.ReadRef<SourceManager>();
3372 TargetInfo &t = D.ReadRef<TargetInfo>();
3373 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3374 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00003375
Ted Kremenek68228a92007-10-31 22:44:07 +00003376 unsigned size_reserve = D.ReadInt();
3377
Douglas Gregor24afd4a2008-11-17 14:58:09 +00003378 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3379 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00003380
Ted Kremenek034a78c2007-11-13 22:02:55 +00003381 for (unsigned i = 0; i < size_reserve; ++i)
3382 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00003383
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003384 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3385
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003386 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00003387
3388 return A;
3389}
Douglas Gregorc34897d2009-04-09 22:27:44 +00003390
3391ExternalASTSource::~ExternalASTSource() { }
3392
3393void ExternalASTSource::PrintStats() { }