blob: c4597e2235e4e4d056de195d99e9bf30000acad7 [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
Fariborz Jahaniana2c97df2009-03-05 20:08:48 +0000624void ASTContext::CollectObjCIvars(const ObjCInterfaceDecl *OI,
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000625 llvm::SmallVectorImpl<FieldDecl*> &Fields) {
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000626 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
627 if (SuperClass)
628 CollectObjCIvars(SuperClass, Fields);
629 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.
Douglas Gregorc55b0b02009-04-09 21:40:53 +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
648 // 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;
Fariborz Jahanian0556b152008-12-17 21:40:49 +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.
Ted Kremenek42730c52008-01-07 19:49:32 +00001363QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001364 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1365
Steve Naroff93fd2112009-01-27 22:08:43 +00001366 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001367 Types.push_back(Decl->TypeForDecl);
1368 return QualType(Decl->TypeForDecl, 0);
1369}
1370
Douglas Gregora4918772009-02-05 23:33:38 +00001371/// \brief Retrieve the template type parameter type for a template
1372/// parameter with the given depth, index, and (optionally) name.
1373QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1374 IdentifierInfo *Name) {
1375 llvm::FoldingSetNodeID ID;
1376 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1377 void *InsertPos = 0;
1378 TemplateTypeParmType *TypeParm
1379 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1380
1381 if (TypeParm)
1382 return QualType(TypeParm, 0);
1383
1384 if (Name)
1385 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1386 getTemplateTypeParmType(Depth, Index));
1387 else
1388 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1389
1390 Types.push_back(TypeParm);
1391 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1392
1393 return QualType(TypeParm, 0);
1394}
1395
Douglas Gregor8e458f42009-02-09 18:46:07 +00001396QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001397ASTContext::getTemplateSpecializationType(TemplateName Template,
1398 const TemplateArgument *Args,
1399 unsigned NumArgs,
1400 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001401 if (!Canon.isNull())
1402 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001403
Douglas Gregor8e458f42009-02-09 18:46:07 +00001404 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001405 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001406
Douglas Gregor8e458f42009-02-09 18:46:07 +00001407 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001408 TemplateSpecializationType *Spec
1409 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001410
1411 if (Spec)
1412 return QualType(Spec, 0);
1413
Douglas Gregordd13e842009-03-30 22:58:21 +00001414 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001415 sizeof(TemplateArgument) * NumArgs),
1416 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001417 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001418 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001419 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001420
1421 return QualType(Spec, 0);
1422}
1423
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001424QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001425ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001426 QualType NamedType) {
1427 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001428 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001429
1430 void *InsertPos = 0;
1431 QualifiedNameType *T
1432 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1433 if (T)
1434 return QualType(T, 0);
1435
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001436 T = new (*this) QualifiedNameType(NNS, NamedType,
1437 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001438 Types.push_back(T);
1439 QualifiedNameTypes.InsertNode(T, InsertPos);
1440 return QualType(T, 0);
1441}
1442
Douglas Gregord3022602009-03-27 23:10:48 +00001443QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1444 const IdentifierInfo *Name,
1445 QualType Canon) {
1446 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1447
1448 if (Canon.isNull()) {
1449 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1450 if (CanonNNS != NNS)
1451 Canon = getTypenameType(CanonNNS, Name);
1452 }
1453
1454 llvm::FoldingSetNodeID ID;
1455 TypenameType::Profile(ID, NNS, Name);
1456
1457 void *InsertPos = 0;
1458 TypenameType *T
1459 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1460 if (T)
1461 return QualType(T, 0);
1462
1463 T = new (*this) TypenameType(NNS, Name, Canon);
1464 Types.push_back(T);
1465 TypenameTypes.InsertNode(T, InsertPos);
1466 return QualType(T, 0);
1467}
1468
Douglas Gregor77da5802009-04-01 00:28:59 +00001469QualType
1470ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1471 const TemplateSpecializationType *TemplateId,
1472 QualType Canon) {
1473 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1474
1475 if (Canon.isNull()) {
1476 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1477 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1478 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1479 const TemplateSpecializationType *CanonTemplateId
1480 = CanonType->getAsTemplateSpecializationType();
1481 assert(CanonTemplateId &&
1482 "Canonical type must also be a template specialization type");
1483 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1484 }
1485 }
1486
1487 llvm::FoldingSetNodeID ID;
1488 TypenameType::Profile(ID, NNS, TemplateId);
1489
1490 void *InsertPos = 0;
1491 TypenameType *T
1492 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1493 if (T)
1494 return QualType(T, 0);
1495
1496 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1497 Types.push_back(T);
1498 TypenameTypes.InsertNode(T, InsertPos);
1499 return QualType(T, 0);
1500}
1501
Chris Lattnere1352302008-04-07 04:56:42 +00001502/// CmpProtocolNames - Comparison predicate for sorting protocols
1503/// alphabetically.
1504static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1505 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001506 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001507}
1508
1509static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1510 unsigned &NumProtocols) {
1511 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1512
1513 // Sort protocols, keyed by name.
1514 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1515
1516 // Remove duplicates.
1517 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1518 NumProtocols = ProtocolsEnd-Protocols;
1519}
1520
1521
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001522/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1523/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001524QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1525 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001526 // Sort the protocol list alphabetically to canonicalize it.
1527 SortAndUniqueProtocols(Protocols, NumProtocols);
1528
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001529 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001530 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001531
1532 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001533 if (ObjCQualifiedInterfaceType *QT =
1534 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001535 return QualType(QT, 0);
1536
1537 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001538 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001539 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001540
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001541 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001542 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001543 return QualType(QType, 0);
1544}
1545
Chris Lattnere1352302008-04-07 04:56:42 +00001546/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1547/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001548QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001549 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001550 // Sort the protocol list alphabetically to canonicalize it.
1551 SortAndUniqueProtocols(Protocols, NumProtocols);
1552
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001553 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001554 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001555
1556 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001557 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001558 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001559 return QualType(QT, 0);
1560
1561 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001562 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001563 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001564 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001565 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001566 return QualType(QType, 0);
1567}
1568
Douglas Gregor4fa58902009-02-26 23:50:07 +00001569/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1570/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001571/// multiple declarations that refer to "typeof(x)" all contain different
1572/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1573/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001574QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001575 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001576 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001577 Types.push_back(toe);
1578 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001579}
1580
Steve Naroff0604dd92007-08-01 18:02:17 +00001581/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1582/// TypeOfType AST's. The only motivation to unique these nodes would be
1583/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1584/// an issue. This doesn't effect the type checker, since it operates
1585/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001586QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001587 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001588 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001589 Types.push_back(tot);
1590 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001591}
1592
Chris Lattner4b009652007-07-25 00:24:17 +00001593/// getTagDeclType - Return the unique reference to the type for the
1594/// specified TagDecl (struct/union/class/enum) decl.
1595QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001596 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001597 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001598}
1599
1600/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1601/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1602/// needs to agree with the definition in <stddef.h>.
1603QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001604 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001605}
1606
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001607/// getSignedWCharType - Return the type of "signed wchar_t".
1608/// Used when in C++, as a GCC extension.
1609QualType ASTContext::getSignedWCharType() const {
1610 // FIXME: derive from "Target" ?
1611 return WCharTy;
1612}
1613
1614/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1615/// Used when in C++, as a GCC extension.
1616QualType ASTContext::getUnsignedWCharType() const {
1617 // FIXME: derive from "Target" ?
1618 return UnsignedIntTy;
1619}
1620
Chris Lattner4b009652007-07-25 00:24:17 +00001621/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1622/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1623QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001624 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001625}
1626
Chris Lattner19eb97e2008-04-02 05:18:44 +00001627//===----------------------------------------------------------------------===//
1628// Type Operators
1629//===----------------------------------------------------------------------===//
1630
Chris Lattner3dae6f42008-04-06 22:41:35 +00001631/// getCanonicalType - Return the canonical (structural) type corresponding to
1632/// the specified potentially non-canonical type. The non-canonical version
1633/// of a type may have many "decorated" versions of types. Decorators can
1634/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1635/// to be free of any of these, allowing two canonical types to be compared
1636/// for exact equality with a simple pointer comparison.
1637QualType ASTContext::getCanonicalType(QualType T) {
1638 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001639
1640 // If the result has type qualifiers, make sure to canonicalize them as well.
1641 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1642 if (TypeQuals == 0) return CanType;
1643
1644 // If the type qualifiers are on an array type, get the canonical type of the
1645 // array with the qualifiers applied to the element type.
1646 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1647 if (!AT)
1648 return CanType.getQualifiedType(TypeQuals);
1649
1650 // Get the canonical version of the element with the extra qualifiers on it.
1651 // This can recursively sink qualifiers through multiple levels of arrays.
1652 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1653 NewEltTy = getCanonicalType(NewEltTy);
1654
1655 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1656 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1657 CAT->getIndexTypeQualifier());
1658 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1659 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1660 IAT->getIndexTypeQualifier());
1661
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001662 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1663 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1664 DSAT->getSizeModifier(),
1665 DSAT->getIndexTypeQualifier());
1666
Chris Lattnera1923f62008-08-04 07:31:14 +00001667 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1668 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1669 VAT->getSizeModifier(),
1670 VAT->getIndexTypeQualifier());
1671}
1672
Douglas Gregord3022602009-03-27 23:10:48 +00001673NestedNameSpecifier *
1674ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1675 if (!NNS)
1676 return 0;
1677
1678 switch (NNS->getKind()) {
1679 case NestedNameSpecifier::Identifier:
1680 // Canonicalize the prefix but keep the identifier the same.
1681 return NestedNameSpecifier::Create(*this,
1682 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1683 NNS->getAsIdentifier());
1684
1685 case NestedNameSpecifier::Namespace:
1686 // A namespace is canonical; build a nested-name-specifier with
1687 // this namespace and no prefix.
1688 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1689
1690 case NestedNameSpecifier::TypeSpec:
1691 case NestedNameSpecifier::TypeSpecWithTemplate: {
1692 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1693 NestedNameSpecifier *Prefix = 0;
1694
1695 // FIXME: This isn't the right check!
1696 if (T->isDependentType())
1697 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1698
1699 return NestedNameSpecifier::Create(*this, Prefix,
1700 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1701 T.getTypePtr());
1702 }
1703
1704 case NestedNameSpecifier::Global:
1705 // The global specifier is canonical and unique.
1706 return NNS;
1707 }
1708
1709 // Required to silence a GCC warning
1710 return 0;
1711}
1712
Chris Lattnera1923f62008-08-04 07:31:14 +00001713
1714const ArrayType *ASTContext::getAsArrayType(QualType T) {
1715 // Handle the non-qualified case efficiently.
1716 if (T.getCVRQualifiers() == 0) {
1717 // Handle the common positive case fast.
1718 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1719 return AT;
1720 }
1721
1722 // Handle the common negative case fast, ignoring CVR qualifiers.
1723 QualType CType = T->getCanonicalTypeInternal();
1724
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001725 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001726 // test.
1727 if (!isa<ArrayType>(CType) &&
1728 !isa<ArrayType>(CType.getUnqualifiedType()))
1729 return 0;
1730
1731 // Apply any CVR qualifiers from the array type to the element type. This
1732 // implements C99 6.7.3p8: "If the specification of an array type includes
1733 // any type qualifiers, the element type is so qualified, not the array type."
1734
1735 // If we get here, we either have type qualifiers on the type, or we have
1736 // sugar such as a typedef in the way. If we have type qualifiers on the type
1737 // we must propagate them down into the elemeng type.
1738 unsigned CVRQuals = T.getCVRQualifiers();
1739 unsigned AddrSpace = 0;
1740 Type *Ty = T.getTypePtr();
1741
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001742 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001743 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001744 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1745 AddrSpace = EXTQT->getAddressSpace();
1746 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001747 } else {
1748 T = Ty->getDesugaredType();
1749 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1750 break;
1751 CVRQuals |= T.getCVRQualifiers();
1752 Ty = T.getTypePtr();
1753 }
1754 }
1755
1756 // If we have a simple case, just return now.
1757 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1758 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1759 return ATy;
1760
1761 // Otherwise, we have an array and we have qualifiers on it. Push the
1762 // qualifiers into the array element type and return a new array type.
1763 // Get the canonical version of the element with the extra qualifiers on it.
1764 // This can recursively sink qualifiers through multiple levels of arrays.
1765 QualType NewEltTy = ATy->getElementType();
1766 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001767 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001768 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1769
1770 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1771 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1772 CAT->getSizeModifier(),
1773 CAT->getIndexTypeQualifier()));
1774 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1775 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1776 IAT->getSizeModifier(),
1777 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001778
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001779 if (const DependentSizedArrayType *DSAT
1780 = dyn_cast<DependentSizedArrayType>(ATy))
1781 return cast<ArrayType>(
1782 getDependentSizedArrayType(NewEltTy,
1783 DSAT->getSizeExpr(),
1784 DSAT->getSizeModifier(),
1785 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001786
Chris Lattnera1923f62008-08-04 07:31:14 +00001787 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1788 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1789 VAT->getSizeModifier(),
1790 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001791}
1792
1793
Chris Lattner19eb97e2008-04-02 05:18:44 +00001794/// getArrayDecayedType - Return the properly qualified result of decaying the
1795/// specified array type to a pointer. This operation is non-trivial when
1796/// handling typedefs etc. The canonical type of "T" must be an array type,
1797/// this returns a pointer to a properly qualified element of the array.
1798///
1799/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1800QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001801 // Get the element type with 'getAsArrayType' so that we don't lose any
1802 // typedefs in the element type of the array. This also handles propagation
1803 // of type qualifiers from the array type into the element type if present
1804 // (C99 6.7.3p8).
1805 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1806 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001807
Chris Lattnera1923f62008-08-04 07:31:14 +00001808 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001809
1810 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001811 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001812}
1813
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001814QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001815 QualType ElemTy = VAT->getElementType();
1816
1817 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1818 return getBaseElementType(VAT);
1819
1820 return ElemTy;
1821}
1822
Chris Lattner4b009652007-07-25 00:24:17 +00001823/// getFloatingRank - Return a relative rank for floating point types.
1824/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001825static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001826 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001827 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001828
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001829 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001830 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001831 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001832 case BuiltinType::Float: return FloatRank;
1833 case BuiltinType::Double: return DoubleRank;
1834 case BuiltinType::LongDouble: return LongDoubleRank;
1835 }
1836}
1837
Steve Narofffa0c4532007-08-27 01:41:48 +00001838/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1839/// point or a complex type (based on typeDomain/typeSize).
1840/// 'typeDomain' is a real floating point or complex type.
1841/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001842QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1843 QualType Domain) const {
1844 FloatingRank EltRank = getFloatingRank(Size);
1845 if (Domain->isComplexType()) {
1846 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001847 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001848 case FloatRank: return FloatComplexTy;
1849 case DoubleRank: return DoubleComplexTy;
1850 case LongDoubleRank: return LongDoubleComplexTy;
1851 }
Chris Lattner4b009652007-07-25 00:24:17 +00001852 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001853
1854 assert(Domain->isRealFloatingType() && "Unknown domain!");
1855 switch (EltRank) {
1856 default: assert(0 && "getFloatingRank(): illegal value for rank");
1857 case FloatRank: return FloatTy;
1858 case DoubleRank: return DoubleTy;
1859 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001860 }
Chris Lattner4b009652007-07-25 00:24:17 +00001861}
1862
Chris Lattner51285d82008-04-06 23:55:33 +00001863/// getFloatingTypeOrder - Compare the rank of the two specified floating
1864/// point types, ignoring the domain of the type (i.e. 'double' ==
1865/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1866/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001867int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1868 FloatingRank LHSR = getFloatingRank(LHS);
1869 FloatingRank RHSR = getFloatingRank(RHS);
1870
1871 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001872 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001873 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001874 return 1;
1875 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001876}
1877
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001878/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1879/// routine will assert if passed a built-in type that isn't an integer or enum,
1880/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001881unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001882 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001883 if (EnumType* ET = dyn_cast<EnumType>(T))
1884 T = ET->getDecl()->getIntegerType().getTypePtr();
1885
1886 // There are two things which impact the integer rank: the width, and
1887 // the ordering of builtins. The builtin ordering is encoded in the
1888 // bottom three bits; the width is encoded in the bits above that.
1889 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1890 return FWIT->getWidth() << 3;
1891 }
1892
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001893 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001894 default: assert(0 && "getIntegerRank(): not a built-in integer");
1895 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001896 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001897 case BuiltinType::Char_S:
1898 case BuiltinType::Char_U:
1899 case BuiltinType::SChar:
1900 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001901 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001902 case BuiltinType::Short:
1903 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001904 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001905 case BuiltinType::Int:
1906 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001907 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001908 case BuiltinType::Long:
1909 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001910 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001911 case BuiltinType::LongLong:
1912 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001913 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001914 }
1915}
1916
Chris Lattner51285d82008-04-06 23:55:33 +00001917/// getIntegerTypeOrder - Returns the highest ranked integer type:
1918/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1919/// LHS < RHS, return -1.
1920int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001921 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1922 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001923 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001924
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001925 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1926 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001927
Chris Lattner51285d82008-04-06 23:55:33 +00001928 unsigned LHSRank = getIntegerRank(LHSC);
1929 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001930
Chris Lattner51285d82008-04-06 23:55:33 +00001931 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1932 if (LHSRank == RHSRank) return 0;
1933 return LHSRank > RHSRank ? 1 : -1;
1934 }
Chris Lattner4b009652007-07-25 00:24:17 +00001935
Chris Lattner51285d82008-04-06 23:55:33 +00001936 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1937 if (LHSUnsigned) {
1938 // If the unsigned [LHS] type is larger, return it.
1939 if (LHSRank >= RHSRank)
1940 return 1;
1941
1942 // If the signed type can represent all values of the unsigned type, it
1943 // wins. Because we are dealing with 2's complement and types that are
1944 // powers of two larger than each other, this is always safe.
1945 return -1;
1946 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001947
Chris Lattner51285d82008-04-06 23:55:33 +00001948 // If the unsigned [RHS] type is larger, return it.
1949 if (RHSRank >= LHSRank)
1950 return -1;
1951
1952 // If the signed type can represent all values of the unsigned type, it
1953 // wins. Because we are dealing with 2's complement and types that are
1954 // powers of two larger than each other, this is always safe.
1955 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001956}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001957
1958// getCFConstantStringType - Return the type used for constant CFStrings.
1959QualType ASTContext::getCFConstantStringType() {
1960 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001961 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001962 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001963 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001964 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001965
1966 // const int *isa;
1967 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001968 // int flags;
1969 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001970 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001971 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001972 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001973 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001974
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001975 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001976 for (unsigned i = 0; i < 4; ++i) {
1977 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1978 SourceLocation(), 0,
1979 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001980 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001981 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001982 }
1983
1984 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001985 }
1986
1987 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001988}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001989
Anders Carlssonf58cac72008-08-30 19:34:46 +00001990QualType ASTContext::getObjCFastEnumerationStateType()
1991{
1992 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001993 ObjCFastEnumerationStateTypeDecl =
1994 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1995 &Idents.get("__objcFastEnumerationState"));
1996
Anders Carlssonf58cac72008-08-30 19:34:46 +00001997 QualType FieldTypes[] = {
1998 UnsignedLongTy,
1999 getPointerType(ObjCIdType),
2000 getPointerType(UnsignedLongTy),
2001 getConstantArrayType(UnsignedLongTy,
2002 llvm::APInt(32, 5), ArrayType::Normal, 0)
2003 };
2004
Douglas Gregor8acb7272008-12-11 16:49:14 +00002005 for (size_t i = 0; i < 4; ++i) {
2006 FieldDecl *Field = FieldDecl::Create(*this,
2007 ObjCFastEnumerationStateTypeDecl,
2008 SourceLocation(), 0,
2009 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002010 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002011 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002012 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00002013
Douglas Gregor8acb7272008-12-11 16:49:14 +00002014 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00002015 }
2016
2017 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2018}
2019
Anders Carlssone3f02572007-10-29 06:33:42 +00002020// This returns true if a type has been typedefed to BOOL:
2021// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00002022static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002023 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00002024 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2025 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002026
2027 return false;
2028}
2029
Ted Kremenek42730c52008-01-07 19:49:32 +00002030/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002031/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00002032int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00002033 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002034
2035 // Make all integer and enum types at least as large as an int
2036 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002037 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002038 // Treat arrays as pointers, since that's how they're passed in.
2039 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002040 sz = getTypeSize(VoidPtrTy);
2041 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002042}
2043
Ted Kremenek42730c52008-01-07 19:49:32 +00002044/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002045/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002046void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00002047 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002048 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002049 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00002050 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002051 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002052 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002053 // Compute size of all parameters.
2054 // Start with computing size of a pointer in number of bytes.
2055 // FIXME: There might(should) be a better way of doing this computation!
2056 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002057 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002058 // The first two arguments (self and _cmd) are pointers; account for
2059 // their size.
2060 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002061 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2062 E = Decl->param_end(); PI != E; ++PI) {
2063 QualType PType = (*PI)->getType();
2064 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002065 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002066 ParmOffset += sz;
2067 }
2068 S += llvm::utostr(ParmOffset);
2069 S += "@0:";
2070 S += llvm::utostr(PtrSize);
2071
2072 // Argument types.
2073 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002074 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2075 E = Decl->param_end(); PI != E; ++PI) {
2076 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002077 QualType PType = PVDecl->getOriginalType();
2078 if (const ArrayType *AT =
Steve Naroff78380fb2009-04-14 00:03:58 +00002079 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2080 // Use array's original type only if it has known number of
2081 // elements.
Steve Naroff6777bf32009-04-14 00:40:09 +00002082 if (!isa<ConstantArrayType>(AT))
Steve Naroff78380fb2009-04-14 00:03:58 +00002083 PType = PVDecl->getType();
2084 } else if (PType->isFunctionType())
2085 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002086 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002087 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002088 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002089 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002090 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002091 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002092 }
2093}
2094
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002095/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002096/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002097/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2098/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002099/// Property attributes are stored as a comma-delimited C string. The simple
2100/// attributes readonly and bycopy are encoded as single characters. The
2101/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2102/// encoded as single characters, followed by an identifier. Property types
2103/// are also encoded as a parametrized attribute. The characters used to encode
2104/// these attributes are defined by the following enumeration:
2105/// @code
2106/// enum PropertyAttributes {
2107/// kPropertyReadOnly = 'R', // property is read-only.
2108/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2109/// kPropertyByref = '&', // property is a reference to the value last assigned
2110/// kPropertyDynamic = 'D', // property is dynamic
2111/// kPropertyGetter = 'G', // followed by getter selector name
2112/// kPropertySetter = 'S', // followed by setter selector name
2113/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2114/// kPropertyType = 't' // followed by old-style type encoding.
2115/// kPropertyWeak = 'W' // 'weak' property
2116/// kPropertyStrong = 'P' // property GC'able
2117/// kPropertyNonAtomic = 'N' // property non-atomic
2118/// };
2119/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002120void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2121 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002122 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002123 // Collect information from the property implementation decl(s).
2124 bool Dynamic = false;
2125 ObjCPropertyImplDecl *SynthesizePID = 0;
2126
2127 // FIXME: Duplicated code due to poor abstraction.
2128 if (Container) {
2129 if (const ObjCCategoryImplDecl *CID =
2130 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2131 for (ObjCCategoryImplDecl::propimpl_iterator
2132 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2133 ObjCPropertyImplDecl *PID = *i;
2134 if (PID->getPropertyDecl() == PD) {
2135 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2136 Dynamic = true;
2137 } else {
2138 SynthesizePID = PID;
2139 }
2140 }
2141 }
2142 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002143 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002144 for (ObjCCategoryImplDecl::propimpl_iterator
2145 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2146 ObjCPropertyImplDecl *PID = *i;
2147 if (PID->getPropertyDecl() == PD) {
2148 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2149 Dynamic = true;
2150 } else {
2151 SynthesizePID = PID;
2152 }
2153 }
2154 }
2155 }
2156 }
2157
2158 // FIXME: This is not very efficient.
2159 S = "T";
2160
2161 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002162 // GCC has some special rules regarding encoding of properties which
2163 // closely resembles encoding of ivars.
Daniel Dunbar701c8502009-04-20 06:37:24 +00002164 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002165 true /* outermost type */,
2166 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002167
2168 if (PD->isReadOnly()) {
2169 S += ",R";
2170 } else {
2171 switch (PD->getSetterKind()) {
2172 case ObjCPropertyDecl::Assign: break;
2173 case ObjCPropertyDecl::Copy: S += ",C"; break;
2174 case ObjCPropertyDecl::Retain: S += ",&"; break;
2175 }
2176 }
2177
2178 // It really isn't clear at all what this means, since properties
2179 // are "dynamic by default".
2180 if (Dynamic)
2181 S += ",D";
2182
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002183 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2184 S += ",N";
2185
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002186 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2187 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002188 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002189 }
2190
2191 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2192 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002193 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002194 }
2195
2196 if (SynthesizePID) {
2197 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2198 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002199 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002200 }
2201
2202 // FIXME: OBJCGC: weak & strong
2203}
2204
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002205/// getLegacyIntegralTypeEncoding -
2206/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002207/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002208/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2209///
2210void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2211 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2212 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002213 if (BT->getKind() == BuiltinType::ULong &&
2214 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002215 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002216 else
2217 if (BT->getKind() == BuiltinType::Long &&
2218 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002219 PointeeTy = IntTy;
2220 }
2221 }
2222}
2223
Fariborz Jahanian248db262008-01-22 22:44:46 +00002224void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002225 const FieldDecl *Field) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002226 // We follow the behavior of gcc, expanding structures which are
2227 // directly pointed to, and expanding embedded structures. Note that
2228 // these rules are sufficient to prevent recursive encoding of the
2229 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002230 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2231 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002232}
2233
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002234static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002235 const FieldDecl *FD) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002236 const Expr *E = FD->getBitWidth();
2237 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2238 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2239 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2240 S += 'b';
2241 S += llvm::utostr(N);
2242}
2243
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002244void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2245 bool ExpandPointedToStructures,
2246 bool ExpandStructures,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002247 const FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002248 bool OutermostType,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002249 bool EncodingProperty) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002250 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002251 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002252 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002253 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002254 else {
2255 char encoding;
2256 switch (BT->getKind()) {
2257 default: assert(0 && "Unhandled builtin type kind");
2258 case BuiltinType::Void: encoding = 'v'; break;
2259 case BuiltinType::Bool: encoding = 'B'; break;
2260 case BuiltinType::Char_U:
2261 case BuiltinType::UChar: encoding = 'C'; break;
2262 case BuiltinType::UShort: encoding = 'S'; break;
2263 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002264 case BuiltinType::ULong:
2265 encoding =
2266 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2267 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002268 case BuiltinType::ULongLong: encoding = 'Q'; break;
2269 case BuiltinType::Char_S:
2270 case BuiltinType::SChar: encoding = 'c'; break;
2271 case BuiltinType::Short: encoding = 's'; break;
2272 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002273 case BuiltinType::Long:
2274 encoding =
2275 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2276 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002277 case BuiltinType::LongLong: encoding = 'q'; break;
2278 case BuiltinType::Float: encoding = 'f'; break;
2279 case BuiltinType::Double: encoding = 'd'; break;
2280 case BuiltinType::LongDouble: encoding = 'd'; break;
2281 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002282
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002283 S += encoding;
2284 }
Anders Carlsson70e16dd2009-04-09 21:55:45 +00002285 } else if (const ComplexType *CT = T->getAsComplexType()) {
2286 S += 'j';
2287 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2288 false);
2289 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002290 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2291 ExpandPointedToStructures,
2292 ExpandStructures, FD);
2293 if (FD || EncodingProperty) {
2294 // Note that we do extended encoding of protocol qualifer list
2295 // Only when doing ivar or property encoding.
2296 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2297 S += '"';
2298 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2299 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2300 S += '<';
2301 S += Proto->getNameAsString();
2302 S += '>';
2303 }
2304 S += '"';
2305 }
2306 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002307 }
2308 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002309 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002310 bool isReadOnly = false;
2311 // For historical/compatibility reasons, the read-only qualifier of the
2312 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2313 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2314 // Also, do not emit the 'r' for anything but the outermost type!
2315 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2316 if (OutermostType && T.isConstQualified()) {
2317 isReadOnly = true;
2318 S += 'r';
2319 }
2320 }
2321 else if (OutermostType) {
2322 QualType P = PointeeTy;
2323 while (P->getAsPointerType())
2324 P = P->getAsPointerType()->getPointeeType();
2325 if (P.isConstQualified()) {
2326 isReadOnly = true;
2327 S += 'r';
2328 }
2329 }
2330 if (isReadOnly) {
2331 // Another legacy compatibility encoding. Some ObjC qualifier and type
2332 // combinations need to be rearranged.
2333 // Rewrite "in const" from "nr" to "rn"
2334 const char * s = S.c_str();
2335 int len = S.length();
2336 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2337 std::string replace = "rn";
2338 S.replace(S.end()-2, S.end(), replace);
2339 }
2340 }
Steve Naroff17c03822009-02-12 17:52:19 +00002341 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002342 S += '@';
2343 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002344 }
2345 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002346 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002347 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002348 // Another historical/compatibility reason.
2349 // We encode the underlying type which comes out as
2350 // {...};
2351 S += '^';
2352 getObjCEncodingForTypeImpl(PointeeTy, S,
2353 false, ExpandPointedToStructures,
2354 NULL);
2355 return;
2356 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002357 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002358 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002359 const ObjCInterfaceType *OIT =
2360 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002361 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002362 S += '"';
2363 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002364 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2365 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2366 S += '<';
2367 S += Proto->getNameAsString();
2368 S += '>';
2369 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002370 S += '"';
2371 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002372 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002373 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002374 S += '#';
2375 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002376 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002377 S += ':';
2378 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002379 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002380
2381 if (PointeeTy->isCharType()) {
2382 // char pointer types should be encoded as '*' unless it is a
2383 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002384 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002385 S += '*';
2386 return;
2387 }
2388 }
2389
2390 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002391 getLegacyIntegralTypeEncoding(PointeeTy);
2392
2393 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002394 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002395 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002396 } else if (const ArrayType *AT =
2397 // Ignore type qualifiers etc.
2398 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002399 if (isa<IncompleteArrayType>(AT)) {
2400 // Incomplete arrays are encoded as a pointer to the array element.
2401 S += '^';
2402
2403 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2404 false, ExpandStructures, FD);
2405 } else {
2406 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002407
Anders Carlsson858c64d2009-02-22 01:38:57 +00002408 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2409 S += llvm::utostr(CAT->getSize().getZExtValue());
2410 else {
2411 //Variable length arrays are encoded as a regular array with 0 elements.
2412 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2413 S += '0';
2414 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002415
Anders Carlsson858c64d2009-02-22 01:38:57 +00002416 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2417 false, ExpandStructures, FD);
2418 S += ']';
2419 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002420 } else if (T->getAsFunctionType()) {
2421 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002422 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002423 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002424 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002425 // Anonymous structures print as '?'
2426 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2427 S += II->getName();
2428 } else {
2429 S += '?';
2430 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002431 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002432 S += '=';
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002433 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2434 FieldEnd = RDecl->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002435 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002436 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002437 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002438 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002439 S += '"';
2440 }
2441
2442 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002443 if (Field->isBitField()) {
2444 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2445 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002446 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002447 QualType qt = Field->getType();
2448 getLegacyIntegralTypeEncoding(qt);
2449 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002450 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002451 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002452 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002453 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002454 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002455 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002456 if (FD && FD->isBitField())
2457 EncodeBitField(this, S, FD);
2458 else
2459 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002460 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002461 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002462 } else if (T->isObjCInterfaceType()) {
2463 // @encode(class_name)
2464 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2465 S += '{';
2466 const IdentifierInfo *II = OI->getIdentifier();
2467 S += II->getName();
2468 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002469 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002470 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002471 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002472 if (RecFields[i]->isBitField())
2473 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2474 RecFields[i]);
2475 else
2476 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2477 FD);
2478 }
2479 S += '}';
2480 }
2481 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002482 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002483}
2484
Ted Kremenek42730c52008-01-07 19:49:32 +00002485void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002486 std::string& S) const {
2487 if (QT & Decl::OBJC_TQ_In)
2488 S += 'n';
2489 if (QT & Decl::OBJC_TQ_Inout)
2490 S += 'N';
2491 if (QT & Decl::OBJC_TQ_Out)
2492 S += 'o';
2493 if (QT & Decl::OBJC_TQ_Bycopy)
2494 S += 'O';
2495 if (QT & Decl::OBJC_TQ_Byref)
2496 S += 'R';
2497 if (QT & Decl::OBJC_TQ_Oneway)
2498 S += 'V';
2499}
2500
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002501void ASTContext::setBuiltinVaListType(QualType T)
2502{
2503 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2504
2505 BuiltinVaListType = T;
2506}
2507
Ted Kremenek42730c52008-01-07 19:49:32 +00002508void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002509{
Ted Kremenek42730c52008-01-07 19:49:32 +00002510 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002511
2512 // typedef struct objc_object *id;
2513 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002514 // User error - caller will issue diagnostics.
2515 if (!ptr)
2516 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002517 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002518 // User error - caller will issue diagnostics.
2519 if (!rec)
2520 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002521 IdStructType = rec;
2522}
2523
Ted Kremenek42730c52008-01-07 19:49:32 +00002524void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002525{
Ted Kremenek42730c52008-01-07 19:49:32 +00002526 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002527
2528 // typedef struct objc_selector *SEL;
2529 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002530 if (!ptr)
2531 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002532 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002533 if (!rec)
2534 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002535 SelStructType = rec;
2536}
2537
Ted Kremenek42730c52008-01-07 19:49:32 +00002538void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002539{
Ted Kremenek42730c52008-01-07 19:49:32 +00002540 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002541}
2542
Ted Kremenek42730c52008-01-07 19:49:32 +00002543void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002544{
Ted Kremenek42730c52008-01-07 19:49:32 +00002545 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002546
2547 // typedef struct objc_class *Class;
2548 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2549 assert(ptr && "'Class' incorrectly typed");
2550 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2551 assert(rec && "'Class' incorrectly typed");
2552 ClassStructType = rec;
2553}
2554
Ted Kremenek42730c52008-01-07 19:49:32 +00002555void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2556 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002557 "'NSConstantString' type already set!");
2558
Ted Kremenek42730c52008-01-07 19:49:32 +00002559 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002560}
2561
Douglas Gregordd13e842009-03-30 22:58:21 +00002562/// \brief Retrieve the template name that represents a qualified
2563/// template name such as \c std::vector.
2564TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2565 bool TemplateKeyword,
2566 TemplateDecl *Template) {
2567 llvm::FoldingSetNodeID ID;
2568 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2569
2570 void *InsertPos = 0;
2571 QualifiedTemplateName *QTN =
2572 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2573 if (!QTN) {
2574 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2575 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2576 }
2577
2578 return TemplateName(QTN);
2579}
2580
2581/// \brief Retrieve the template name that represents a dependent
2582/// template name such as \c MetaFun::template apply.
2583TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2584 const IdentifierInfo *Name) {
2585 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2586
2587 llvm::FoldingSetNodeID ID;
2588 DependentTemplateName::Profile(ID, NNS, Name);
2589
2590 void *InsertPos = 0;
2591 DependentTemplateName *QTN =
2592 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2593
2594 if (QTN)
2595 return TemplateName(QTN);
2596
2597 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2598 if (CanonNNS == NNS) {
2599 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2600 } else {
2601 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2602 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2603 }
2604
2605 DependentTemplateNames.InsertNode(QTN, InsertPos);
2606 return TemplateName(QTN);
2607}
2608
Douglas Gregorc6507e42008-11-03 14:12:49 +00002609/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002610/// TargetInfo, produce the corresponding type. The unsigned @p Type
2611/// is actually a value of type @c TargetInfo::IntType.
2612QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002613 switch (Type) {
2614 case TargetInfo::NoInt: return QualType();
2615 case TargetInfo::SignedShort: return ShortTy;
2616 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2617 case TargetInfo::SignedInt: return IntTy;
2618 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2619 case TargetInfo::SignedLong: return LongTy;
2620 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2621 case TargetInfo::SignedLongLong: return LongLongTy;
2622 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2623 }
2624
2625 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002626 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002627}
Ted Kremenek118930e2008-07-24 23:58:27 +00002628
2629//===----------------------------------------------------------------------===//
2630// Type Predicates.
2631//===----------------------------------------------------------------------===//
2632
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002633/// isObjCNSObjectType - Return true if this is an NSObject object using
2634/// NSObject attribute on a c-style pointer type.
2635/// FIXME - Make it work directly on types.
2636///
2637bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2638 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2639 if (TypedefDecl *TD = TDT->getDecl())
2640 if (TD->getAttr<ObjCNSObjectAttr>())
2641 return true;
2642 }
2643 return false;
2644}
2645
Ted Kremenek118930e2008-07-24 23:58:27 +00002646/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2647/// to an object type. This includes "id" and "Class" (two 'special' pointers
2648/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2649/// ID type).
2650bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002651 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002652 return true;
2653
Steve Naroffd9e00802008-10-21 18:24:04 +00002654 // Blocks are objects.
2655 if (Ty->isBlockPointerType())
2656 return true;
2657
2658 // All other object types are pointers.
Chris Lattnera008d172009-04-12 23:51:02 +00002659 const PointerType *PT = Ty->getAsPointerType();
2660 if (PT == 0)
Ted Kremenek118930e2008-07-24 23:58:27 +00002661 return false;
2662
Chris Lattnera008d172009-04-12 23:51:02 +00002663 // If this a pointer to an interface (e.g. NSString*), it is ok.
2664 if (PT->getPointeeType()->isObjCInterfaceType() ||
2665 // If is has NSObject attribute, OK as well.
2666 isObjCNSObjectType(Ty))
2667 return true;
2668
Ted Kremenek118930e2008-07-24 23:58:27 +00002669 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2670 // pointer types. This looks for the typedef specifically, not for the
Chris Lattnera008d172009-04-12 23:51:02 +00002671 // underlying type. Iteratively strip off typedefs so that we can handle
2672 // typedefs of typedefs.
2673 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2674 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2675 Ty.getUnqualifiedType() == getObjCClassType())
2676 return true;
2677
2678 Ty = TDT->getDecl()->getUnderlyingType();
2679 }
Ted Kremenek118930e2008-07-24 23:58:27 +00002680
Chris Lattnera008d172009-04-12 23:51:02 +00002681 return false;
Ted Kremenek118930e2008-07-24 23:58:27 +00002682}
2683
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002684/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2685/// garbage collection attribute.
2686///
2687QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002688 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002689 if (getLangOptions().ObjC1 &&
2690 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002691 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002692 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002693 // (or pointers to them) be treated as though they were declared
2694 // as __strong.
2695 if (GCAttrs == QualType::GCNone) {
2696 if (isObjCObjectPointerType(Ty))
2697 GCAttrs = QualType::Strong;
2698 else if (Ty->isPointerType())
2699 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2700 }
Fariborz Jahaniand7b01972009-04-11 00:00:54 +00002701 // Non-pointers have none gc'able attribute regardless of the attribute
2702 // set on them.
2703 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2704 return QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002705 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002706 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002707}
2708
Chris Lattner6ff358b2008-04-07 06:51:04 +00002709//===----------------------------------------------------------------------===//
2710// Type Compatibility Testing
2711//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002712
Steve Naroff3454b6c2008-09-04 15:10:53 +00002713/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002714/// block types. Types must be strictly compatible here. For example,
2715/// C unfortunately doesn't produce an error for the following:
2716///
2717/// int (*emptyArgFunc)();
2718/// int (*intArgList)(int) = emptyArgFunc;
2719///
2720/// For blocks, we will produce an error for the following (similar to C++):
2721///
2722/// int (^emptyArgBlock)();
2723/// int (^intArgBlock)(int) = emptyArgBlock;
2724///
2725/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2726///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002727bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002728 const FunctionType *lbase = lhs->getAsFunctionType();
2729 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002730 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2731 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stump896ceed2009-04-01 01:17:39 +00002732 if (lproto && rproto == 0)
2733 return false;
2734 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff3454b6c2008-09-04 15:10:53 +00002735}
2736
Chris Lattner6ff358b2008-04-07 06:51:04 +00002737/// areCompatVectorTypes - Return true if the two specified vector types are
2738/// compatible.
2739static bool areCompatVectorTypes(const VectorType *LHS,
2740 const VectorType *RHS) {
2741 assert(LHS->isCanonical() && RHS->isCanonical());
2742 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002743 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002744}
2745
Eli Friedman0d9549b2008-08-22 00:56:42 +00002746/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002747/// compatible for assignment from RHS to LHS. This handles validation of any
2748/// protocol qualifiers on the LHS or RHS.
2749///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002750bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2751 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002752 // Verify that the base decls are compatible: the RHS must be a subclass of
2753 // the LHS.
2754 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2755 return false;
2756
2757 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2758 // protocol qualified at all, then we are good.
2759 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2760 return true;
2761
2762 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2763 // isn't a superset.
2764 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2765 return true; // FIXME: should return false!
2766
2767 // Finally, we must have two protocol-qualified interfaces.
2768 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2769 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002770
Steve Naroff98e71b82009-03-01 16:12:44 +00002771 // All LHS protocols must have a presence on the RHS.
2772 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002773
Steve Naroff98e71b82009-03-01 16:12:44 +00002774 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2775 LHSPE = LHSP->qual_end();
2776 LHSPI != LHSPE; LHSPI++) {
2777 bool RHSImplementsProtocol = false;
2778
2779 // If the RHS doesn't implement the protocol on the left, the types
2780 // are incompatible.
2781 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2782 RHSPE = RHSP->qual_end();
2783 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2784 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2785 RHSImplementsProtocol = true;
2786 }
2787 // FIXME: For better diagnostics, consider passing back the protocol name.
2788 if (!RHSImplementsProtocol)
2789 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002790 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002791 // The RHS implements all protocols listed on the LHS.
2792 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002793}
2794
Steve Naroff17c03822009-02-12 17:52:19 +00002795bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2796 // get the "pointed to" types
2797 const PointerType *LHSPT = LHS->getAsPointerType();
2798 const PointerType *RHSPT = RHS->getAsPointerType();
2799
2800 if (!LHSPT || !RHSPT)
2801 return false;
2802
2803 QualType lhptee = LHSPT->getPointeeType();
2804 QualType rhptee = RHSPT->getPointeeType();
2805 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2806 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2807 // ID acts sort of like void* for ObjC interfaces
2808 if (LHSIface && isObjCIdStructType(rhptee))
2809 return true;
2810 if (RHSIface && isObjCIdStructType(lhptee))
2811 return true;
2812 if (!LHSIface || !RHSIface)
2813 return false;
2814 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2815 canAssignObjCInterfaces(RHSIface, LHSIface);
2816}
2817
Steve Naroff85f0dc52007-10-15 20:41:53 +00002818/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2819/// both shall have the identically qualified version of a compatible type.
2820/// C99 6.2.7p1: Two types have compatible types if their types are the
2821/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002822bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2823 return !mergeTypes(LHS, RHS).isNull();
2824}
2825
2826QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2827 const FunctionType *lbase = lhs->getAsFunctionType();
2828 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002829 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2830 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002831 bool allLTypes = true;
2832 bool allRTypes = true;
2833
2834 // Check return type
2835 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2836 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002837 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2838 allLTypes = false;
2839 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2840 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002841
2842 if (lproto && rproto) { // two C99 style function prototypes
2843 unsigned lproto_nargs = lproto->getNumArgs();
2844 unsigned rproto_nargs = rproto->getNumArgs();
2845
2846 // Compatible functions must have the same number of arguments
2847 if (lproto_nargs != rproto_nargs)
2848 return QualType();
2849
2850 // Variadic and non-variadic functions aren't compatible
2851 if (lproto->isVariadic() != rproto->isVariadic())
2852 return QualType();
2853
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002854 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2855 return QualType();
2856
Eli Friedman0d9549b2008-08-22 00:56:42 +00002857 // Check argument compatibility
2858 llvm::SmallVector<QualType, 10> types;
2859 for (unsigned i = 0; i < lproto_nargs; i++) {
2860 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2861 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2862 QualType argtype = mergeTypes(largtype, rargtype);
2863 if (argtype.isNull()) return QualType();
2864 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002865 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2866 allLTypes = false;
2867 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2868 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002869 }
2870 if (allLTypes) return lhs;
2871 if (allRTypes) return rhs;
2872 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002873 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002874 }
2875
2876 if (lproto) allRTypes = false;
2877 if (rproto) allLTypes = false;
2878
Douglas Gregor4fa58902009-02-26 23:50:07 +00002879 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002880 if (proto) {
2881 if (proto->isVariadic()) return QualType();
2882 // Check that the types are compatible with the types that
2883 // would result from default argument promotions (C99 6.7.5.3p15).
2884 // The only types actually affected are promotable integer
2885 // types and floats, which would be passed as a different
2886 // type depending on whether the prototype is visible.
2887 unsigned proto_nargs = proto->getNumArgs();
2888 for (unsigned i = 0; i < proto_nargs; ++i) {
2889 QualType argTy = proto->getArgType(i);
2890 if (argTy->isPromotableIntegerType() ||
2891 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2892 return QualType();
2893 }
2894
2895 if (allLTypes) return lhs;
2896 if (allRTypes) return rhs;
2897 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002898 proto->getNumArgs(), lproto->isVariadic(),
2899 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002900 }
2901
2902 if (allLTypes) return lhs;
2903 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002904 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002905}
2906
2907QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002908 // C++ [expr]: If an expression initially has the type "reference to T", the
2909 // type is adjusted to "T" prior to any further analysis, the expression
2910 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002911 // expression is an lvalue unless the reference is an rvalue reference and
2912 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002913 // FIXME: C++ shouldn't be going through here! The rules are different
2914 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00002915 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2916 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002917 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002918 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002919 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002920 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002921
Eli Friedman0d9549b2008-08-22 00:56:42 +00002922 QualType LHSCan = getCanonicalType(LHS),
2923 RHSCan = getCanonicalType(RHS);
2924
2925 // If two types are identical, they are compatible.
2926 if (LHSCan == RHSCan)
2927 return LHS;
2928
2929 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002930 // Note that we handle extended qualifiers later, in the
2931 // case for ExtQualType.
2932 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002933 return QualType();
2934
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00002935 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
2936 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002937
Chris Lattnerc38d4522008-01-14 05:45:46 +00002938 // We want to consider the two function types to be the same for these
2939 // comparisons, just force one to the other.
2940 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2941 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002942
2943 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002944 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2945 LHSClass = Type::ConstantArray;
2946 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2947 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002948
Nate Begemanaf6ed502008-04-18 23:10:10 +00002949 // Canonicalize ExtVector -> Vector.
2950 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2951 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002952
Chris Lattner7cdcb252008-04-07 06:38:24 +00002953 // Consider qualified interfaces and interfaces the same.
2954 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2955 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002956
Chris Lattnerb5709e22008-04-07 05:43:21 +00002957 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002958 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002959 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2960 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00002961
Steve Naroff0773c582009-04-14 15:11:46 +00002962 // 'id' and 'Class' act sort of like void* for ObjC interfaces
2963 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00002964 return LHS;
Steve Naroff0773c582009-04-14 15:11:46 +00002965 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00002966 return RHS;
2967
Steve Naroff28ceff72008-12-10 22:14:21 +00002968 // ID is compatible with all qualified id types.
2969 if (LHS->isObjCQualifiedIdType()) {
2970 if (const PointerType *PT = RHS->getAsPointerType()) {
2971 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00002972 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002973 return LHS;
2974 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2975 // Unfortunately, this API is part of Sema (which we don't have access
2976 // to. Need to refactor. The following check is insufficient, since we
2977 // need to make sure the class implements the protocol.
2978 if (pType->isObjCInterfaceType())
2979 return LHS;
2980 }
2981 }
2982 if (RHS->isObjCQualifiedIdType()) {
2983 if (const PointerType *PT = LHS->getAsPointerType()) {
2984 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00002985 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002986 return RHS;
2987 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2988 // Unfortunately, this API is part of Sema (which we don't have access
2989 // to. Need to refactor. The following check is insufficient, since we
2990 // need to make sure the class implements the protocol.
2991 if (pType->isObjCInterfaceType())
2992 return RHS;
2993 }
2994 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002995 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2996 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002997 if (const EnumType* ETy = LHS->getAsEnumType()) {
2998 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2999 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003000 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003001 if (const EnumType* ETy = RHS->getAsEnumType()) {
3002 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3003 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003004 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003005
Eli Friedman0d9549b2008-08-22 00:56:42 +00003006 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003007 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003008
Steve Naroffc88babe2008-01-09 22:43:08 +00003009 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003010 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00003011#define TYPE(Class, Base)
3012#define ABSTRACT_TYPE(Class, Base)
3013#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3014#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3015#include "clang/AST/TypeNodes.def"
3016 assert(false && "Non-canonical and dependent types shouldn't get here");
3017 return QualType();
3018
Sebastian Redlce6fff02009-03-16 23:22:08 +00003019 case Type::LValueReference:
3020 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003021 case Type::MemberPointer:
3022 assert(false && "C++ should never be in mergeTypes");
3023 return QualType();
3024
3025 case Type::IncompleteArray:
3026 case Type::VariableArray:
3027 case Type::FunctionProto:
3028 case Type::ExtVector:
3029 case Type::ObjCQualifiedInterface:
3030 assert(false && "Types are eliminated above");
3031 return QualType();
3032
Chris Lattnerc38d4522008-01-14 05:45:46 +00003033 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003034 {
3035 // Merge two pointer types, while trying to preserve typedef info
3036 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3037 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3038 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3039 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003040 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3041 return LHS;
3042 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3043 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003044 return getPointerType(ResultType);
3045 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003046 case Type::BlockPointer:
3047 {
3048 // Merge two block pointer types, while trying to preserve typedef info
3049 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3050 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3051 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3052 if (ResultType.isNull()) return QualType();
3053 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3054 return LHS;
3055 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3056 return RHS;
3057 return getBlockPointerType(ResultType);
3058 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003059 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003060 {
3061 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3062 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3063 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3064 return QualType();
3065
3066 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3067 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3068 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3069 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003070 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3071 return LHS;
3072 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3073 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003074 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3075 ArrayType::ArraySizeModifier(), 0);
3076 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3077 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003078 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3079 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003080 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3081 return LHS;
3082 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3083 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003084 if (LVAT) {
3085 // FIXME: This isn't correct! But tricky to implement because
3086 // the array's size has to be the size of LHS, but the type
3087 // has to be different.
3088 return LHS;
3089 }
3090 if (RVAT) {
3091 // FIXME: This isn't correct! But tricky to implement because
3092 // the array's size has to be the size of RHS, but the type
3093 // has to be different.
3094 return RHS;
3095 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003096 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3097 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003098 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003099 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003100 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003101 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003102 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003103 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003104 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003105 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3106 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003107 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003108 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003109 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003110 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003111 case Type::Complex:
3112 // Distinct complex types are incompatible.
3113 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003114 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003115 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003116 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3117 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003118 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003119 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003120 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003121 // FIXME: This should be type compatibility, e.g. whether
3122 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003123 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3124 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3125 if (LHSIface && RHSIface &&
3126 canAssignObjCInterfaces(LHSIface, RHSIface))
3127 return LHS;
3128
Eli Friedman0d9549b2008-08-22 00:56:42 +00003129 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003130 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003131 case Type::ObjCQualifiedId:
3132 // Distinct qualified id's are not compatible.
3133 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003134 case Type::FixedWidthInt:
3135 // Distinct fixed-width integers are not compatible.
3136 return QualType();
3137 case Type::ObjCQualifiedClass:
3138 // Distinct qualified classes are not compatible.
3139 return QualType();
3140 case Type::ExtQual:
3141 // FIXME: ExtQual types can be compatible even if they're not
3142 // identical!
3143 return QualType();
3144 // First attempt at an implementation, but I'm not really sure it's
3145 // right...
3146#if 0
3147 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3148 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3149 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3150 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3151 return QualType();
3152 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3153 LHSBase = QualType(LQual->getBaseType(), 0);
3154 RHSBase = QualType(RQual->getBaseType(), 0);
3155 ResultType = mergeTypes(LHSBase, RHSBase);
3156 if (ResultType.isNull()) return QualType();
3157 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3158 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3159 return LHS;
3160 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3161 return RHS;
3162 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3163 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3164 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3165 return ResultType;
3166#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003167
3168 case Type::TemplateSpecialization:
3169 assert(false && "Dependent types have no size");
3170 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003171 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003172
3173 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003174}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003175
Chris Lattner1d78a862008-04-07 07:01:58 +00003176//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003177// Integer Predicates
3178//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003179
Eli Friedman0832dbc2008-06-28 06:23:08 +00003180unsigned ASTContext::getIntWidth(QualType T) {
3181 if (T == BoolTy)
3182 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003183 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3184 return FWIT->getWidth();
3185 }
3186 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003187 return (unsigned)getTypeSize(T);
3188}
3189
3190QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3191 assert(T->isSignedIntegerType() && "Unexpected type");
3192 if (const EnumType* ETy = T->getAsEnumType())
3193 T = ETy->getDecl()->getIntegerType();
3194 const BuiltinType* BTy = T->getAsBuiltinType();
3195 assert (BTy && "Unexpected signed integer type");
3196 switch (BTy->getKind()) {
3197 case BuiltinType::Char_S:
3198 case BuiltinType::SChar:
3199 return UnsignedCharTy;
3200 case BuiltinType::Short:
3201 return UnsignedShortTy;
3202 case BuiltinType::Int:
3203 return UnsignedIntTy;
3204 case BuiltinType::Long:
3205 return UnsignedLongTy;
3206 case BuiltinType::LongLong:
3207 return UnsignedLongLongTy;
3208 default:
3209 assert(0 && "Unexpected signed integer type");
3210 return QualType();
3211 }
3212}
3213
3214
3215//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00003216// Serialization Support
3217//===----------------------------------------------------------------------===//
3218
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003219enum {
3220 BasicMetadataBlock = 1,
3221 ASTContextBlock = 2,
3222 DeclsBlock = 3
3223};
3224
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003225void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3226 // Create bitstream.
3227 llvm::BitstreamWriter Stream(Buffer);
3228
3229 // Emit the preamble.
3230 Stream.Emit((unsigned)'B', 8);
3231 Stream.Emit((unsigned)'C', 8);
3232 Stream.Emit(0xC, 4);
3233 Stream.Emit(0xF, 4);
3234 Stream.Emit(0xE, 4);
3235 Stream.Emit(0x0, 4);
3236
3237 // Create serializer.
3238 llvm::Serializer S(Stream);
3239
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003240 // ===---------------------------------------------------===/
3241 // Serialize the "Translation Unit" metadata.
3242 // ===---------------------------------------------------===/
3243
3244 // Emit ASTContext.
3245 S.EnterBlock(ASTContextBlock);
3246 S.EmitOwnedPtr(this);
3247 S.ExitBlock(); // exit "ASTContextBlock"
3248
3249 S.EnterBlock(BasicMetadataBlock);
3250
3251 // Block for SourceManager and Target. Allows easy skipping
3252 // around to the block for the Selectors during deserialization.
3253 S.EnterBlock();
3254
3255 // Emit the SourceManager.
3256 S.Emit(getSourceManager());
3257
3258 // Emit the Target.
3259 S.EmitPtr(&Target);
3260 S.EmitCStr(Target.getTargetTriple());
3261
3262 S.ExitBlock(); // exit "SourceManager and Target Block"
3263
3264 // Emit the Selectors.
3265 S.Emit(Selectors);
3266
3267 // Emit the Identifier Table.
3268 S.Emit(Idents);
3269
3270 S.ExitBlock(); // exit "BasicMetadataBlock"
3271}
3272
3273
Ted Kremenek738e6c02007-10-31 17:10:13 +00003274/// Emit - Serialize an ASTContext object to Bitcode.
3275void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00003276 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00003277 S.EmitRef(SourceMgr);
3278 S.EmitRef(Target);
3279 S.EmitRef(Idents);
3280 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003281
Ted Kremenek68228a92007-10-31 22:44:07 +00003282 // Emit the size of the type vector so that we can reserve that size
3283 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003284 S.EmitInt(Types.size());
3285
Ted Kremenek034a78c2007-11-13 22:02:55 +00003286 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3287 I!=E;++I)
3288 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003289
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003290 S.EmitOwnedPtr(TUDecl);
3291
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003292 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003293}
3294
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003295
3296ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3297 FileManager &FMgr) {
3298 // Check if the file is of the proper length.
3299 if (Buffer.getBufferSize() & 0x3) {
3300 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3301 return 0;
3302 }
3303
3304 // Create the bitstream reader.
3305 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3306 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3307
3308 if (Stream.Read(8) != 'B' ||
3309 Stream.Read(8) != 'C' ||
3310 Stream.Read(4) != 0xC ||
3311 Stream.Read(4) != 0xF ||
3312 Stream.Read(4) != 0xE ||
3313 Stream.Read(4) != 0x0) {
3314 // FIXME: Provide diagnostic.
3315 return NULL;
3316 }
3317
3318 // Create the deserializer.
3319 llvm::Deserializer Dezr(Stream);
3320
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003321 // ===---------------------------------------------------===/
3322 // Deserialize the "Translation Unit" metadata.
3323 // ===---------------------------------------------------===/
3324
3325 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3326 // (which will appear earlier) and record its location.
3327
3328 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3329 assert (FoundBlock);
3330
3331 llvm::Deserializer::Location ASTContextBlockLoc =
3332 Dezr.getCurrentBlockLocation();
3333
3334 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3335 assert (FoundBlock);
3336
3337 // Read the SourceManager.
3338 SourceManager::CreateAndRegister(Dezr, FMgr);
3339
3340 { // Read the TargetInfo.
3341 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3342 char* triple = Dezr.ReadCStr(NULL,0,true);
3343 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3344 delete [] triple;
3345 }
3346
3347 // For Selectors, we must read the identifier table first because the
3348 // SelectorTable depends on the identifiers being already deserialized.
3349 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3350 Dezr.SkipBlock();
3351
3352 // Read the identifier table.
3353 IdentifierTable::CreateAndRegister(Dezr);
3354
3355 // Now jump back and read the selectors.
3356 Dezr.JumpTo(SelectorBlkLoc);
3357 SelectorTable::CreateAndRegister(Dezr);
3358
3359 // Now jump back to ASTContextBlock and read the ASTContext.
3360 Dezr.JumpTo(ASTContextBlockLoc);
3361 return Dezr.ReadOwnedPtr<ASTContext>();
3362}
3363
Ted Kremenekacba3612007-11-13 00:25:37 +00003364ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00003365
3366 // Read the language options.
3367 LangOptions LOpts;
3368 LOpts.Read(D);
3369
Ted Kremenek68228a92007-10-31 22:44:07 +00003370 SourceManager &SM = D.ReadRef<SourceManager>();
3371 TargetInfo &t = D.ReadRef<TargetInfo>();
3372 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3373 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00003374
Ted Kremenek68228a92007-10-31 22:44:07 +00003375 unsigned size_reserve = D.ReadInt();
3376
Douglas Gregor24afd4a2008-11-17 14:58:09 +00003377 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3378 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00003379
Ted Kremenek034a78c2007-11-13 22:02:55 +00003380 for (unsigned i = 0; i < size_reserve; ++i)
3381 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00003382
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003383 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3384
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003385 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00003386
3387 return A;
3388}
Douglas Gregorc34897d2009-04-09 22:27:44 +00003389
3390ExternalASTSource::~ExternalASTSource() { }
3391
3392void ExternalASTSource::PrintStats() { }