blob: cfff4b54402cabae6aa074c21307bf627b1b41a9 [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) {
Chris Lattner608c1e32009-03-31 09:24:30 +0000647 RecordDecl *&RD = ASTRecordForInterface[D];
648 if (RD) {
649 // If we have a record decl already and it is either a definition or if 'D'
650 // is still a forward declaration, return it.
651 if (RD->isDefinition() || D->isForwardDecl())
652 return RD;
653 }
654
655 // If D is a forward declaration, then just make a forward struct decl.
656 if (D->isForwardDecl())
657 return RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
658 D->getLocation(),
659 D->getIdentifier());
Chris Lattner9329cf52009-03-31 08:48:01 +0000660
661 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000662 CollectObjCIvars(D, RecFields);
Chris Lattner608c1e32009-03-31 09:24:30 +0000663
664 if (RD == 0)
665 RD = RecordDecl::Create(*this, TagDecl::TK_struct, 0, D->getLocation(),
666 D->getIdentifier());
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000667 /// FIXME! Can do collection of ivars and adding to the record while
668 /// doing it.
Chris Lattner41b780c2009-03-31 08:58:42 +0000669 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000670 RD->addDecl(*this,
671 FieldDecl::Create(*this, RD,
Chris Lattner608c1e32009-03-31 09:24:30 +0000672 RecFields[i]->getLocation(),
673 RecFields[i]->getIdentifier(),
674 RecFields[i]->getType(),
675 RecFields[i]->getBitWidth(), false));
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000676 }
Chris Lattner9329cf52009-03-31 08:48:01 +0000677
Chris Lattner608c1e32009-03-31 09:24:30 +0000678 RD->completeDefinition(*this);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000679 return RD;
680}
Devang Patel4b6bf702008-06-04 21:54:36 +0000681
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000682/// getASTObjcInterfaceLayout - Get or compute information about the layout of
683/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000684/// position information.
685const ASTRecordLayout &
686ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
687 // Look up this layout, if already laid out, return what we have.
688 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
689 if (Entry) return *Entry;
690
691 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
692 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000693 ASTRecordLayout *NewEntry = NULL;
Fariborz Jahanianba50c332009-04-08 21:54:52 +0000694 // FIXME. Add actual count of synthesized ivars, instead of count
695 // of properties which is the upper bound, but is safe.
Daniel Dunbar998510f2009-04-08 20:18:15 +0000696 unsigned FieldCount =
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000697 D->ivar_size() + std::distance(D->prop_begin(*this), D->prop_end(*this));
Devang Patel8682d882008-06-06 02:14:01 +0000698 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
699 FieldCount++;
700 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
701 unsigned Alignment = SL.getAlignment();
702 uint64_t Size = SL.getSize();
703 NewEntry = new ASTRecordLayout(Size, Alignment);
704 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000705 // Super class is at the beginning of the layout.
706 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000707 } else {
708 NewEntry = new ASTRecordLayout();
709 NewEntry->InitializeLayout(FieldCount);
710 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000711 Entry = NewEntry;
712
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000713 unsigned StructPacking = 0;
714 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
715 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000716
717 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
718 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
719 AA->getAlignment()));
720
721 // Layout each ivar sequentially.
722 unsigned i = 0;
723 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
724 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
725 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000726 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000727 }
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000728 // Also synthesized ivars
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000729 for (ObjCInterfaceDecl::prop_iterator I = D->prop_begin(*this),
730 E = D->prop_end(*this); I != E; ++I) {
Fariborz Jahanianfbf44642009-03-31 18:11:23 +0000731 if (ObjCIvarDecl *Ivar = (*I)->getPropertyIvarDecl())
732 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
733 }
Fariborz Jahanian84c45692009-04-01 19:37:34 +0000734
Devang Patel4b6bf702008-06-04 21:54:36 +0000735 // Finally, round the size of the total struct up to the alignment of the
736 // struct itself.
737 NewEntry->FinalizeLayout();
738 return *NewEntry;
739}
740
Devang Patel7a78e432007-11-01 19:11:01 +0000741/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000742/// specified record (struct/union/class), which indicates its size and field
743/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000744const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000745 D = D->getDefinition(*this);
746 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000747
Chris Lattner4b009652007-07-25 00:24:17 +0000748 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000749 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000750 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000751
Devang Patel7a78e432007-11-01 19:11:01 +0000752 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
753 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
754 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000755 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000756
Douglas Gregor39677622008-12-11 20:41:00 +0000757 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000758 NewEntry->InitializeLayout(std::distance(D->field_begin(*this),
759 D->field_end(*this)));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000760 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000761
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000762 unsigned StructPacking = 0;
763 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
764 StructPacking = PA->getAlignment();
765
Eli Friedman5949a022008-05-30 09:31:38 +0000766 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000767 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
768 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000769
Eli Friedman5949a022008-05-30 09:31:38 +0000770 // Layout each field, for now, just sequentially, respecting alignment. In
771 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000772 unsigned FieldIdx = 0;
Douglas Gregorc55b0b02009-04-09 21:40:53 +0000773 for (RecordDecl::field_iterator Field = D->field_begin(*this),
774 FieldEnd = D->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +0000775 Field != FieldEnd; (void)++Field, ++FieldIdx)
776 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000777
778 // Finally, round the size of the total struct up to the alignment of the
779 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000780 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000781 return *NewEntry;
782}
783
Chris Lattner4b009652007-07-25 00:24:17 +0000784//===----------------------------------------------------------------------===//
785// Type creation/memoization methods
786//===----------------------------------------------------------------------===//
787
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000788QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000789 QualType CanT = getCanonicalType(T);
790 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000791 return T;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000792
793 // If we are composing extended qualifiers together, merge together into one
794 // ExtQualType node.
795 unsigned CVRQuals = T.getCVRQualifiers();
796 QualType::GCAttrTypes GCAttr = QualType::GCNone;
797 Type *TypeNode = T.getTypePtr();
Chris Lattner35fef522008-02-20 20:55:12 +0000798
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000799 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
800 // If this type already has an address space specified, it cannot get
801 // another one.
802 assert(EQT->getAddressSpace() == 0 &&
803 "Type cannot be in multiple addr spaces!");
804 GCAttr = EQT->getObjCGCAttr();
805 TypeNode = EQT->getBaseType();
806 }
Chris Lattner35fef522008-02-20 20:55:12 +0000807
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000808 // Check if we've already instantiated this type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000809 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000810 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000811 void *InsertPos = 0;
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000812 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000813 return QualType(EXTQy, CVRQuals);
814
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000815 // If the base type isn't canonical, this won't be a canonical type either,
816 // so fill in the canonical type field.
817 QualType Canonical;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000818 if (!TypeNode->isCanonical()) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000819 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000820
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000821 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000822 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000823 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000824 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000825 ExtQualType *New =
826 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianb60352a2009-02-17 18:27:45 +0000827 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000828 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000829 return QualType(New, CVRQuals);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000830}
831
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000832QualType ASTContext::getObjCGCQualType(QualType T,
833 QualType::GCAttrTypes GCAttr) {
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000834 QualType CanT = getCanonicalType(T);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000835 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000836 return T;
837
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000838 // If we are composing extended qualifiers together, merge together into one
839 // ExtQualType node.
840 unsigned CVRQuals = T.getCVRQualifiers();
841 Type *TypeNode = T.getTypePtr();
842 unsigned AddressSpace = 0;
843
844 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
845 // If this type already has an address space specified, it cannot get
846 // another one.
847 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
848 "Type cannot be in multiple addr spaces!");
849 AddressSpace = EQT->getAddressSpace();
850 TypeNode = EQT->getBaseType();
851 }
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000852
853 // Check if we've already instantiated an gc qual'd type of this type.
854 llvm::FoldingSetNodeID ID;
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000855 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000856 void *InsertPos = 0;
857 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000858 return QualType(EXTQy, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000859
860 // If the base type isn't canonical, this won't be a canonical type either,
861 // so fill in the canonical type field.
Eli Friedman94fcc9a2009-02-27 23:04:43 +0000862 // FIXME: Isn't this also not canonical if the base type is a array
863 // or pointer type? I can't find any documentation for objc_gc, though...
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000864 QualType Canonical;
865 if (!T->isCanonical()) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000866 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000867
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000868 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000869 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
870 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
871 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000872 ExtQualType *New =
873 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000874 ExtQualTypes.InsertNode(New, InsertPos);
875 Types.push_back(New);
Chris Lattner18b5a9a2009-02-18 22:53:11 +0000876 return QualType(New, CVRQuals);
Fariborz Jahanianaf238092009-02-18 05:09:49 +0000877}
Chris Lattner4b009652007-07-25 00:24:17 +0000878
879/// getComplexType - Return the uniqued reference to the type for a complex
880/// number with the specified element type.
881QualType ASTContext::getComplexType(QualType T) {
882 // Unique pointers, to guarantee there is only one pointer of a particular
883 // structure.
884 llvm::FoldingSetNodeID ID;
885 ComplexType::Profile(ID, T);
886
887 void *InsertPos = 0;
888 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
889 return QualType(CT, 0);
890
891 // If the pointee type isn't canonical, this won't be a canonical type either,
892 // so fill in the canonical type field.
893 QualType Canonical;
894 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000895 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000896
897 // Get the new insert position for the node we care about.
898 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000899 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000900 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000901 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000902 Types.push_back(New);
903 ComplexTypes.InsertNode(New, InsertPos);
904 return QualType(New, 0);
905}
906
Eli Friedmanff3fcdf2009-02-13 02:31:07 +0000907QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
908 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
909 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
910 FixedWidthIntType *&Entry = Map[Width];
911 if (!Entry)
912 Entry = new FixedWidthIntType(Width, Signed);
913 return QualType(Entry, 0);
914}
Chris Lattner4b009652007-07-25 00:24:17 +0000915
916/// getPointerType - Return the uniqued reference to the type for a pointer to
917/// the specified type.
918QualType ASTContext::getPointerType(QualType T) {
919 // Unique pointers, to guarantee there is only one pointer of a particular
920 // structure.
921 llvm::FoldingSetNodeID ID;
922 PointerType::Profile(ID, T);
923
924 void *InsertPos = 0;
925 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
926 return QualType(PT, 0);
927
928 // If the pointee type isn't canonical, this won't be a canonical type either,
929 // so fill in the canonical type field.
930 QualType Canonical;
931 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000932 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000933
934 // Get the new insert position for the node we care about.
935 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000936 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000937 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000938 PointerType *New = new (*this,8) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000939 Types.push_back(New);
940 PointerTypes.InsertNode(New, InsertPos);
941 return QualType(New, 0);
942}
943
Steve Naroff7aa54752008-08-27 16:04:49 +0000944/// getBlockPointerType - Return the uniqued reference to the type for
945/// a pointer to the specified block.
946QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000947 assert(T->isFunctionType() && "block of function types only");
948 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000949 // structure.
950 llvm::FoldingSetNodeID ID;
951 BlockPointerType::Profile(ID, T);
952
953 void *InsertPos = 0;
954 if (BlockPointerType *PT =
955 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
956 return QualType(PT, 0);
957
Steve Narofffd5b19d2008-08-28 19:20:44 +0000958 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000959 // type either so fill in the canonical type field.
960 QualType Canonical;
961 if (!T->isCanonical()) {
962 Canonical = getBlockPointerType(getCanonicalType(T));
963
964 // Get the new insert position for the node we care about.
965 BlockPointerType *NewIP =
966 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000967 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000968 }
Steve Naroff93fd2112009-01-27 22:08:43 +0000969 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000970 Types.push_back(New);
971 BlockPointerTypes.InsertNode(New, InsertPos);
972 return QualType(New, 0);
973}
974
Sebastian Redlce6fff02009-03-16 23:22:08 +0000975/// getLValueReferenceType - Return the uniqued reference to the type for an
976/// lvalue reference to the specified type.
977QualType ASTContext::getLValueReferenceType(QualType T) {
Chris Lattner4b009652007-07-25 00:24:17 +0000978 // Unique pointers, to guarantee there is only one pointer of a particular
979 // structure.
980 llvm::FoldingSetNodeID ID;
981 ReferenceType::Profile(ID, T);
982
983 void *InsertPos = 0;
Sebastian Redlce6fff02009-03-16 23:22:08 +0000984 if (LValueReferenceType *RT =
985 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000986 return QualType(RT, 0);
Sebastian Redlce6fff02009-03-16 23:22:08 +0000987
Chris Lattner4b009652007-07-25 00:24:17 +0000988 // If the referencee type isn't canonical, this won't be a canonical type
989 // either, so fill in the canonical type field.
990 QualType Canonical;
991 if (!T->isCanonical()) {
Sebastian Redlce6fff02009-03-16 23:22:08 +0000992 Canonical = getLValueReferenceType(getCanonicalType(T));
993
Chris Lattner4b009652007-07-25 00:24:17 +0000994 // Get the new insert position for the node we care about.
Sebastian Redlce6fff02009-03-16 23:22:08 +0000995 LValueReferenceType *NewIP =
996 LValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000997 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000998 }
999
Sebastian Redlce6fff02009-03-16 23:22:08 +00001000 LValueReferenceType *New = new (*this,8) LValueReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001001 Types.push_back(New);
Sebastian Redlce6fff02009-03-16 23:22:08 +00001002 LValueReferenceTypes.InsertNode(New, InsertPos);
1003 return QualType(New, 0);
1004}
1005
1006/// getRValueReferenceType - Return the uniqued reference to the type for an
1007/// rvalue reference to the specified type.
1008QualType ASTContext::getRValueReferenceType(QualType T) {
1009 // Unique pointers, to guarantee there is only one pointer of a particular
1010 // structure.
1011 llvm::FoldingSetNodeID ID;
1012 ReferenceType::Profile(ID, T);
1013
1014 void *InsertPos = 0;
1015 if (RValueReferenceType *RT =
1016 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
1017 return QualType(RT, 0);
1018
1019 // If the referencee type isn't canonical, this won't be a canonical type
1020 // either, so fill in the canonical type field.
1021 QualType Canonical;
1022 if (!T->isCanonical()) {
1023 Canonical = getRValueReferenceType(getCanonicalType(T));
1024
1025 // Get the new insert position for the node we care about.
1026 RValueReferenceType *NewIP =
1027 RValueReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
1028 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1029 }
1030
1031 RValueReferenceType *New = new (*this,8) RValueReferenceType(T, Canonical);
1032 Types.push_back(New);
1033 RValueReferenceTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001034 return QualType(New, 0);
1035}
1036
Sebastian Redl75555032009-01-24 21:16:55 +00001037/// getMemberPointerType - Return the uniqued reference to the type for a
1038/// member pointer to the specified type, in the specified class.
1039QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
1040{
1041 // Unique pointers, to guarantee there is only one pointer of a particular
1042 // structure.
1043 llvm::FoldingSetNodeID ID;
1044 MemberPointerType::Profile(ID, T, Cls);
1045
1046 void *InsertPos = 0;
1047 if (MemberPointerType *PT =
1048 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
1049 return QualType(PT, 0);
1050
1051 // If the pointee or class type isn't canonical, this won't be a canonical
1052 // type either, so fill in the canonical type field.
1053 QualType Canonical;
1054 if (!T->isCanonical()) {
1055 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
1056
1057 // Get the new insert position for the node we care about.
1058 MemberPointerType *NewIP =
1059 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
1060 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
1061 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001062 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redl75555032009-01-24 21:16:55 +00001063 Types.push_back(New);
1064 MemberPointerTypes.InsertNode(New, InsertPos);
1065 return QualType(New, 0);
1066}
1067
Steve Naroff83c13012007-08-30 01:06:46 +00001068/// getConstantArrayType - Return the unique reference to the type for an
1069/// array of the specified element type.
1070QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +00001071 const llvm::APInt &ArySize,
1072 ArrayType::ArraySizeModifier ASM,
1073 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001074 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001075 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001076
1077 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +00001078 if (ConstantArrayType *ATP =
1079 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001080 return QualType(ATP, 0);
1081
1082 // If the element type isn't canonical, this won't be a canonical type either,
1083 // so fill in the canonical type field.
1084 QualType Canonical;
1085 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001086 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +00001087 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001088 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +00001089 ConstantArrayType *NewIP =
1090 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001091 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001092 }
1093
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001094 ConstantArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001095 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001096 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001097 Types.push_back(New);
1098 return QualType(New, 0);
1099}
1100
Steve Naroffe2579e32007-08-30 18:14:25 +00001101/// getVariableArrayType - Returns a non-unique reference to the type for a
1102/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +00001103QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
1104 ArrayType::ArraySizeModifier ASM,
1105 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +00001106 // Since we don't unique expressions, it isn't possible to unique VLA's
1107 // that have an expression provided for their size.
1108
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001109 VariableArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001110 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001111
1112 VariableArrayTypes.push_back(New);
1113 Types.push_back(New);
1114 return QualType(New, 0);
1115}
1116
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001117/// getDependentSizedArrayType - Returns a non-unique reference to
1118/// the type for a dependently-sized array of the specified element
1119/// type. FIXME: We will need these to be uniqued, or at least
1120/// comparable, at some point.
1121QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1122 ArrayType::ArraySizeModifier ASM,
1123 unsigned EltTypeQuals) {
1124 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1125 "Size must be type- or value-dependent!");
1126
1127 // Since we don't unique expressions, it isn't possible to unique
1128 // dependently-sized array types.
1129
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001130 DependentSizedArrayType *New =
Steve Naroff93fd2112009-01-27 22:08:43 +00001131 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1132 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001133
1134 DependentSizedArrayTypes.push_back(New);
1135 Types.push_back(New);
1136 return QualType(New, 0);
1137}
1138
Eli Friedman8ff07782008-02-15 18:16:39 +00001139QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1140 ArrayType::ArraySizeModifier ASM,
1141 unsigned EltTypeQuals) {
1142 llvm::FoldingSetNodeID ID;
Chris Lattner3f7a8f12009-02-19 17:31:02 +00001143 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001144
1145 void *InsertPos = 0;
1146 if (IncompleteArrayType *ATP =
1147 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1148 return QualType(ATP, 0);
1149
1150 // If the element type isn't canonical, this won't be a canonical type
1151 // either, so fill in the canonical type field.
1152 QualType Canonical;
1153
1154 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001155 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001156 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001157
1158 // Get the new insert position for the node we care about.
1159 IncompleteArrayType *NewIP =
1160 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001161 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +00001162 }
Eli Friedman8ff07782008-02-15 18:16:39 +00001163
Steve Naroff93fd2112009-01-27 22:08:43 +00001164 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001165 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +00001166
1167 IncompleteArrayTypes.InsertNode(New, InsertPos);
1168 Types.push_back(New);
1169 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +00001170}
1171
Chris Lattner4b009652007-07-25 00:24:17 +00001172/// getVectorType - Return the unique reference to a vector type of
1173/// the specified element type and size. VectorType must be a built-in type.
1174QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
1175 BuiltinType *baseType;
1176
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001177 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +00001178 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
1179
1180 // Check if we've already instantiated a vector of this type.
1181 llvm::FoldingSetNodeID ID;
1182 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
1183 void *InsertPos = 0;
1184 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1185 return QualType(VTP, 0);
1186
1187 // If the element type isn't canonical, this won't be a canonical type either,
1188 // so fill in the canonical type field.
1189 QualType Canonical;
1190 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001191 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001192
1193 // Get the new insert position for the node we care about.
1194 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001195 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001196 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001197 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001198 VectorTypes.InsertNode(New, InsertPos);
1199 Types.push_back(New);
1200 return QualType(New, 0);
1201}
1202
Nate Begemanaf6ed502008-04-18 23:10:10 +00001203/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +00001204/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +00001205QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +00001206 BuiltinType *baseType;
1207
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001208 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +00001209 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +00001210
1211 // Check if we've already instantiated a vector of this type.
1212 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +00001213 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +00001214 void *InsertPos = 0;
1215 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1216 return QualType(VTP, 0);
1217
1218 // If the element type isn't canonical, this won't be a canonical type either,
1219 // so fill in the canonical type field.
1220 QualType Canonical;
1221 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +00001222 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +00001223
1224 // Get the new insert position for the node we care about.
1225 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001226 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001227 }
Steve Naroff93fd2112009-01-27 22:08:43 +00001228 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001229 VectorTypes.InsertNode(New, InsertPos);
1230 Types.push_back(New);
1231 return QualType(New, 0);
1232}
1233
Douglas Gregor4fa58902009-02-26 23:50:07 +00001234/// getFunctionNoProtoType - Return a K&R style C function type like 'int()'.
Chris Lattner4b009652007-07-25 00:24:17 +00001235///
Douglas Gregor4fa58902009-02-26 23:50:07 +00001236QualType ASTContext::getFunctionNoProtoType(QualType ResultTy) {
Chris Lattner4b009652007-07-25 00:24:17 +00001237 // Unique functions, to guarantee there is only one function of a particular
1238 // structure.
1239 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001240 FunctionNoProtoType::Profile(ID, ResultTy);
Chris Lattner4b009652007-07-25 00:24:17 +00001241
1242 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001243 if (FunctionNoProtoType *FT =
1244 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001245 return QualType(FT, 0);
1246
1247 QualType Canonical;
1248 if (!ResultTy->isCanonical()) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00001249 Canonical = getFunctionNoProtoType(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +00001250
1251 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001252 FunctionNoProtoType *NewIP =
1253 FunctionNoProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001254 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001255 }
1256
Douglas Gregor4fa58902009-02-26 23:50:07 +00001257 FunctionNoProtoType *New =new(*this,8)FunctionNoProtoType(ResultTy,Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001258 Types.push_back(New);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001259 FunctionNoProtoTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001260 return QualType(New, 0);
1261}
1262
1263/// getFunctionType - Return a normal function type with a typed argument
1264/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001265QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001266 unsigned NumArgs, bool isVariadic,
1267 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001268 // Unique functions, to guarantee there is only one function of a particular
1269 // structure.
1270 llvm::FoldingSetNodeID ID;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001271 FunctionProtoType::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001272 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001273
1274 void *InsertPos = 0;
Douglas Gregor4fa58902009-02-26 23:50:07 +00001275 if (FunctionProtoType *FTP =
1276 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +00001277 return QualType(FTP, 0);
1278
1279 // Determine whether the type being created is already canonical or not.
1280 bool isCanonical = ResultTy->isCanonical();
1281 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1282 if (!ArgArray[i]->isCanonical())
1283 isCanonical = false;
1284
1285 // If this type isn't canonical, get the canonical version of it.
1286 QualType Canonical;
1287 if (!isCanonical) {
1288 llvm::SmallVector<QualType, 16> CanonicalArgs;
1289 CanonicalArgs.reserve(NumArgs);
1290 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001291 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001292
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001293 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001294 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001295 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001296
1297 // Get the new insert position for the node we care about.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001298 FunctionProtoType *NewIP =
1299 FunctionProtoTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001300 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001301 }
1302
Douglas Gregor4fa58902009-02-26 23:50:07 +00001303 // FunctionProtoType objects are allocated with extra bytes after them
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001304 // for a variable size array (for parameter types) at the end of them.
Douglas Gregor4fa58902009-02-26 23:50:07 +00001305 FunctionProtoType *FTP =
1306 (FunctionProtoType*)Allocate(sizeof(FunctionProtoType) +
Steve Naroff207b9ec2009-01-27 23:20:32 +00001307 NumArgs*sizeof(QualType), 8);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001308 new (FTP) FunctionProtoType(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001309 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001310 Types.push_back(FTP);
Douglas Gregor4fa58902009-02-26 23:50:07 +00001311 FunctionProtoTypes.InsertNode(FTP, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001312 return QualType(FTP, 0);
1313}
1314
Douglas Gregor1d661552008-04-13 21:07:44 +00001315/// getTypeDeclType - Return the unique reference to the type for the
1316/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001317QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001318 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001319 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1320
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001321 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001322 return getTypedefType(Typedef);
Douglas Gregora4918772009-02-05 23:33:38 +00001323 else if (isa<TemplateTypeParmDecl>(Decl)) {
1324 assert(false && "Template type parameter types are always available.");
1325 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001326 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001327
Douglas Gregor2e047592009-02-28 01:32:25 +00001328 if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001329 if (PrevDecl)
1330 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001331 else
1332 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek46a837c2008-09-05 17:16:31 +00001333 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001334 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1335 if (PrevDecl)
1336 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Naroff93fd2112009-01-27 22:08:43 +00001337 else
1338 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001339 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001340 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001341 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001342
Ted Kremenek46a837c2008-09-05 17:16:31 +00001343 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001344 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001345}
1346
Chris Lattner4b009652007-07-25 00:24:17 +00001347/// getTypedefType - Return the unique reference to the type for the
1348/// specified typename decl.
1349QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1350 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1351
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001352 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001353 Decl->TypeForDecl = new(*this,8) TypedefType(Type::Typedef, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001354 Types.push_back(Decl->TypeForDecl);
1355 return QualType(Decl->TypeForDecl, 0);
1356}
1357
Ted Kremenek42730c52008-01-07 19:49:32 +00001358/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001359/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001360QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001361 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1362
Steve Naroff93fd2112009-01-27 22:08:43 +00001363 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001364 Types.push_back(Decl->TypeForDecl);
1365 return QualType(Decl->TypeForDecl, 0);
1366}
1367
Douglas Gregora4918772009-02-05 23:33:38 +00001368/// \brief Retrieve the template type parameter type for a template
1369/// parameter with the given depth, index, and (optionally) name.
1370QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1371 IdentifierInfo *Name) {
1372 llvm::FoldingSetNodeID ID;
1373 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1374 void *InsertPos = 0;
1375 TemplateTypeParmType *TypeParm
1376 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1377
1378 if (TypeParm)
1379 return QualType(TypeParm, 0);
1380
1381 if (Name)
1382 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1383 getTemplateTypeParmType(Depth, Index));
1384 else
1385 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1386
1387 Types.push_back(TypeParm);
1388 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1389
1390 return QualType(TypeParm, 0);
1391}
1392
Douglas Gregor8e458f42009-02-09 18:46:07 +00001393QualType
Douglas Gregordd13e842009-03-30 22:58:21 +00001394ASTContext::getTemplateSpecializationType(TemplateName Template,
1395 const TemplateArgument *Args,
1396 unsigned NumArgs,
1397 QualType Canon) {
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001398 if (!Canon.isNull())
1399 Canon = getCanonicalType(Canon);
Douglas Gregor9c7825b2009-02-26 22:19:44 +00001400
Douglas Gregor8e458f42009-02-09 18:46:07 +00001401 llvm::FoldingSetNodeID ID;
Douglas Gregordd13e842009-03-30 22:58:21 +00001402 TemplateSpecializationType::Profile(ID, Template, Args, NumArgs);
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001403
Douglas Gregor8e458f42009-02-09 18:46:07 +00001404 void *InsertPos = 0;
Douglas Gregordd13e842009-03-30 22:58:21 +00001405 TemplateSpecializationType *Spec
1406 = TemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001407
1408 if (Spec)
1409 return QualType(Spec, 0);
1410
Douglas Gregordd13e842009-03-30 22:58:21 +00001411 void *Mem = Allocate((sizeof(TemplateSpecializationType) +
Douglas Gregorf9ff4b12009-03-09 23:48:35 +00001412 sizeof(TemplateArgument) * NumArgs),
1413 8);
Douglas Gregordd13e842009-03-30 22:58:21 +00001414 Spec = new (Mem) TemplateSpecializationType(Template, Args, NumArgs, Canon);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001415 Types.push_back(Spec);
Douglas Gregordd13e842009-03-30 22:58:21 +00001416 TemplateSpecializationTypes.InsertNode(Spec, InsertPos);
Douglas Gregor8e458f42009-02-09 18:46:07 +00001417
1418 return QualType(Spec, 0);
1419}
1420
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001421QualType
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001422ASTContext::getQualifiedNameType(NestedNameSpecifier *NNS,
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001423 QualType NamedType) {
1424 llvm::FoldingSetNodeID ID;
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001425 QualifiedNameType::Profile(ID, NNS, NamedType);
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001426
1427 void *InsertPos = 0;
1428 QualifiedNameType *T
1429 = QualifiedNameTypes.FindNodeOrInsertPos(ID, InsertPos);
1430 if (T)
1431 return QualType(T, 0);
1432
Douglas Gregor1e589cc2009-03-26 23:50:42 +00001433 T = new (*this) QualifiedNameType(NNS, NamedType,
1434 getCanonicalType(NamedType));
Douglas Gregor734b4ba2009-03-19 00:18:19 +00001435 Types.push_back(T);
1436 QualifiedNameTypes.InsertNode(T, InsertPos);
1437 return QualType(T, 0);
1438}
1439
Douglas Gregord3022602009-03-27 23:10:48 +00001440QualType ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1441 const IdentifierInfo *Name,
1442 QualType Canon) {
1443 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1444
1445 if (Canon.isNull()) {
1446 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1447 if (CanonNNS != NNS)
1448 Canon = getTypenameType(CanonNNS, Name);
1449 }
1450
1451 llvm::FoldingSetNodeID ID;
1452 TypenameType::Profile(ID, NNS, Name);
1453
1454 void *InsertPos = 0;
1455 TypenameType *T
1456 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1457 if (T)
1458 return QualType(T, 0);
1459
1460 T = new (*this) TypenameType(NNS, Name, Canon);
1461 Types.push_back(T);
1462 TypenameTypes.InsertNode(T, InsertPos);
1463 return QualType(T, 0);
1464}
1465
Douglas Gregor77da5802009-04-01 00:28:59 +00001466QualType
1467ASTContext::getTypenameType(NestedNameSpecifier *NNS,
1468 const TemplateSpecializationType *TemplateId,
1469 QualType Canon) {
1470 assert(NNS->isDependent() && "nested-name-specifier must be dependent");
1471
1472 if (Canon.isNull()) {
1473 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
1474 QualType CanonType = getCanonicalType(QualType(TemplateId, 0));
1475 if (CanonNNS != NNS || CanonType != QualType(TemplateId, 0)) {
1476 const TemplateSpecializationType *CanonTemplateId
1477 = CanonType->getAsTemplateSpecializationType();
1478 assert(CanonTemplateId &&
1479 "Canonical type must also be a template specialization type");
1480 Canon = getTypenameType(CanonNNS, CanonTemplateId);
1481 }
1482 }
1483
1484 llvm::FoldingSetNodeID ID;
1485 TypenameType::Profile(ID, NNS, TemplateId);
1486
1487 void *InsertPos = 0;
1488 TypenameType *T
1489 = TypenameTypes.FindNodeOrInsertPos(ID, InsertPos);
1490 if (T)
1491 return QualType(T, 0);
1492
1493 T = new (*this) TypenameType(NNS, TemplateId, Canon);
1494 Types.push_back(T);
1495 TypenameTypes.InsertNode(T, InsertPos);
1496 return QualType(T, 0);
1497}
1498
Chris Lattnere1352302008-04-07 04:56:42 +00001499/// CmpProtocolNames - Comparison predicate for sorting protocols
1500/// alphabetically.
1501static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1502 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001503 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001504}
1505
1506static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1507 unsigned &NumProtocols) {
1508 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1509
1510 // Sort protocols, keyed by name.
1511 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1512
1513 // Remove duplicates.
1514 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1515 NumProtocols = ProtocolsEnd-Protocols;
1516}
1517
1518
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001519/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1520/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001521QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1522 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001523 // Sort the protocol list alphabetically to canonicalize it.
1524 SortAndUniqueProtocols(Protocols, NumProtocols);
1525
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001526 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001527 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001528
1529 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001530 if (ObjCQualifiedInterfaceType *QT =
1531 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001532 return QualType(QT, 0);
1533
1534 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +00001535 ObjCQualifiedInterfaceType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001536 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001537
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001538 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001539 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001540 return QualType(QType, 0);
1541}
1542
Chris Lattnere1352302008-04-07 04:56:42 +00001543/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1544/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001545QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001546 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001547 // Sort the protocol list alphabetically to canonicalize it.
1548 SortAndUniqueProtocols(Protocols, NumProtocols);
1549
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001550 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001551 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001552
1553 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001554 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001555 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001556 return QualType(QT, 0);
1557
1558 // No Match;
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001559 ObjCQualifiedIdType *QType =
Steve Naroff93fd2112009-01-27 22:08:43 +00001560 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001561 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001562 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001563 return QualType(QType, 0);
1564}
1565
Douglas Gregor4fa58902009-02-26 23:50:07 +00001566/// getTypeOfExprType - Unlike many "get<Type>" functions, we can't unique
1567/// TypeOfExprType AST's (since expression's are never shared). For example,
Steve Naroff0604dd92007-08-01 18:02:17 +00001568/// multiple declarations that refer to "typeof(x)" all contain different
1569/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1570/// on canonical type's (which are always unique).
Douglas Gregor4fa58902009-02-26 23:50:07 +00001571QualType ASTContext::getTypeOfExprType(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001572 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor4fa58902009-02-26 23:50:07 +00001573 TypeOfExprType *toe = new (*this,8) TypeOfExprType(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001574 Types.push_back(toe);
1575 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001576}
1577
Steve Naroff0604dd92007-08-01 18:02:17 +00001578/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1579/// TypeOfType AST's. The only motivation to unique these nodes would be
1580/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1581/// an issue. This doesn't effect the type checker, since it operates
1582/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001583QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001584 QualType Canonical = getCanonicalType(tofType);
Steve Naroff93fd2112009-01-27 22:08:43 +00001585 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001586 Types.push_back(tot);
1587 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001588}
1589
Chris Lattner4b009652007-07-25 00:24:17 +00001590/// getTagDeclType - Return the unique reference to the type for the
1591/// specified TagDecl (struct/union/class/enum) decl.
1592QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001593 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001594 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001595}
1596
1597/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1598/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1599/// needs to agree with the definition in <stddef.h>.
1600QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001601 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001602}
1603
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001604/// getSignedWCharType - Return the type of "signed wchar_t".
1605/// Used when in C++, as a GCC extension.
1606QualType ASTContext::getSignedWCharType() const {
1607 // FIXME: derive from "Target" ?
1608 return WCharTy;
1609}
1610
1611/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1612/// Used when in C++, as a GCC extension.
1613QualType ASTContext::getUnsignedWCharType() const {
1614 // FIXME: derive from "Target" ?
1615 return UnsignedIntTy;
1616}
1617
Chris Lattner4b009652007-07-25 00:24:17 +00001618/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1619/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1620QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001621 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001622}
1623
Chris Lattner19eb97e2008-04-02 05:18:44 +00001624//===----------------------------------------------------------------------===//
1625// Type Operators
1626//===----------------------------------------------------------------------===//
1627
Chris Lattner3dae6f42008-04-06 22:41:35 +00001628/// getCanonicalType - Return the canonical (structural) type corresponding to
1629/// the specified potentially non-canonical type. The non-canonical version
1630/// of a type may have many "decorated" versions of types. Decorators can
1631/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1632/// to be free of any of these, allowing two canonical types to be compared
1633/// for exact equality with a simple pointer comparison.
1634QualType ASTContext::getCanonicalType(QualType T) {
1635 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001636
1637 // If the result has type qualifiers, make sure to canonicalize them as well.
1638 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1639 if (TypeQuals == 0) return CanType;
1640
1641 // If the type qualifiers are on an array type, get the canonical type of the
1642 // array with the qualifiers applied to the element type.
1643 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1644 if (!AT)
1645 return CanType.getQualifiedType(TypeQuals);
1646
1647 // Get the canonical version of the element with the extra qualifiers on it.
1648 // This can recursively sink qualifiers through multiple levels of arrays.
1649 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1650 NewEltTy = getCanonicalType(NewEltTy);
1651
1652 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1653 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1654 CAT->getIndexTypeQualifier());
1655 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1656 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1657 IAT->getIndexTypeQualifier());
1658
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001659 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1660 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1661 DSAT->getSizeModifier(),
1662 DSAT->getIndexTypeQualifier());
1663
Chris Lattnera1923f62008-08-04 07:31:14 +00001664 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1665 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1666 VAT->getSizeModifier(),
1667 VAT->getIndexTypeQualifier());
1668}
1669
Douglas Gregord3022602009-03-27 23:10:48 +00001670NestedNameSpecifier *
1671ASTContext::getCanonicalNestedNameSpecifier(NestedNameSpecifier *NNS) {
1672 if (!NNS)
1673 return 0;
1674
1675 switch (NNS->getKind()) {
1676 case NestedNameSpecifier::Identifier:
1677 // Canonicalize the prefix but keep the identifier the same.
1678 return NestedNameSpecifier::Create(*this,
1679 getCanonicalNestedNameSpecifier(NNS->getPrefix()),
1680 NNS->getAsIdentifier());
1681
1682 case NestedNameSpecifier::Namespace:
1683 // A namespace is canonical; build a nested-name-specifier with
1684 // this namespace and no prefix.
1685 return NestedNameSpecifier::Create(*this, 0, NNS->getAsNamespace());
1686
1687 case NestedNameSpecifier::TypeSpec:
1688 case NestedNameSpecifier::TypeSpecWithTemplate: {
1689 QualType T = getCanonicalType(QualType(NNS->getAsType(), 0));
1690 NestedNameSpecifier *Prefix = 0;
1691
1692 // FIXME: This isn't the right check!
1693 if (T->isDependentType())
1694 Prefix = getCanonicalNestedNameSpecifier(NNS->getPrefix());
1695
1696 return NestedNameSpecifier::Create(*this, Prefix,
1697 NNS->getKind() == NestedNameSpecifier::TypeSpecWithTemplate,
1698 T.getTypePtr());
1699 }
1700
1701 case NestedNameSpecifier::Global:
1702 // The global specifier is canonical and unique.
1703 return NNS;
1704 }
1705
1706 // Required to silence a GCC warning
1707 return 0;
1708}
1709
Chris Lattnera1923f62008-08-04 07:31:14 +00001710
1711const ArrayType *ASTContext::getAsArrayType(QualType T) {
1712 // Handle the non-qualified case efficiently.
1713 if (T.getCVRQualifiers() == 0) {
1714 // Handle the common positive case fast.
1715 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1716 return AT;
1717 }
1718
1719 // Handle the common negative case fast, ignoring CVR qualifiers.
1720 QualType CType = T->getCanonicalTypeInternal();
1721
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001722 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnera1923f62008-08-04 07:31:14 +00001723 // test.
1724 if (!isa<ArrayType>(CType) &&
1725 !isa<ArrayType>(CType.getUnqualifiedType()))
1726 return 0;
1727
1728 // Apply any CVR qualifiers from the array type to the element type. This
1729 // implements C99 6.7.3p8: "If the specification of an array type includes
1730 // any type qualifiers, the element type is so qualified, not the array type."
1731
1732 // If we get here, we either have type qualifiers on the type, or we have
1733 // sugar such as a typedef in the way. If we have type qualifiers on the type
1734 // we must propagate them down into the elemeng type.
1735 unsigned CVRQuals = T.getCVRQualifiers();
1736 unsigned AddrSpace = 0;
1737 Type *Ty = T.getTypePtr();
1738
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001739 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnera1923f62008-08-04 07:31:14 +00001740 while (1) {
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001741 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1742 AddrSpace = EXTQT->getAddressSpace();
1743 Ty = EXTQT->getBaseType();
Chris Lattnera1923f62008-08-04 07:31:14 +00001744 } else {
1745 T = Ty->getDesugaredType();
1746 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1747 break;
1748 CVRQuals |= T.getCVRQualifiers();
1749 Ty = T.getTypePtr();
1750 }
1751 }
1752
1753 // If we have a simple case, just return now.
1754 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1755 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1756 return ATy;
1757
1758 // Otherwise, we have an array and we have qualifiers on it. Push the
1759 // qualifiers into the array element type and return a new array type.
1760 // Get the canonical version of the element with the extra qualifiers on it.
1761 // This can recursively sink qualifiers through multiple levels of arrays.
1762 QualType NewEltTy = ATy->getElementType();
1763 if (AddrSpace)
Fariborz Jahanianb60352a2009-02-17 18:27:45 +00001764 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnera1923f62008-08-04 07:31:14 +00001765 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1766
1767 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1768 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1769 CAT->getSizeModifier(),
1770 CAT->getIndexTypeQualifier()));
1771 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1772 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1773 IAT->getSizeModifier(),
1774 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001775
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001776 if (const DependentSizedArrayType *DSAT
1777 = dyn_cast<DependentSizedArrayType>(ATy))
1778 return cast<ArrayType>(
1779 getDependentSizedArrayType(NewEltTy,
1780 DSAT->getSizeExpr(),
1781 DSAT->getSizeModifier(),
1782 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001783
Chris Lattnera1923f62008-08-04 07:31:14 +00001784 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1785 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1786 VAT->getSizeModifier(),
1787 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001788}
1789
1790
Chris Lattner19eb97e2008-04-02 05:18:44 +00001791/// getArrayDecayedType - Return the properly qualified result of decaying the
1792/// specified array type to a pointer. This operation is non-trivial when
1793/// handling typedefs etc. The canonical type of "T" must be an array type,
1794/// this returns a pointer to a properly qualified element of the array.
1795///
1796/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1797QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001798 // Get the element type with 'getAsArrayType' so that we don't lose any
1799 // typedefs in the element type of the array. This also handles propagation
1800 // of type qualifiers from the array type into the element type if present
1801 // (C99 6.7.3p8).
1802 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1803 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001804
Chris Lattnera1923f62008-08-04 07:31:14 +00001805 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001806
1807 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001808 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001809}
1810
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001811QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001812 QualType ElemTy = VAT->getElementType();
1813
1814 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1815 return getBaseElementType(VAT);
1816
1817 return ElemTy;
1818}
1819
Chris Lattner4b009652007-07-25 00:24:17 +00001820/// getFloatingRank - Return a relative rank for floating point types.
1821/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001822static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001823 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001824 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001825
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001826 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001827 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001828 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001829 case BuiltinType::Float: return FloatRank;
1830 case BuiltinType::Double: return DoubleRank;
1831 case BuiltinType::LongDouble: return LongDoubleRank;
1832 }
1833}
1834
Steve Narofffa0c4532007-08-27 01:41:48 +00001835/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1836/// point or a complex type (based on typeDomain/typeSize).
1837/// 'typeDomain' is a real floating point or complex type.
1838/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001839QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1840 QualType Domain) const {
1841 FloatingRank EltRank = getFloatingRank(Size);
1842 if (Domain->isComplexType()) {
1843 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001844 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001845 case FloatRank: return FloatComplexTy;
1846 case DoubleRank: return DoubleComplexTy;
1847 case LongDoubleRank: return LongDoubleComplexTy;
1848 }
Chris Lattner4b009652007-07-25 00:24:17 +00001849 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001850
1851 assert(Domain->isRealFloatingType() && "Unknown domain!");
1852 switch (EltRank) {
1853 default: assert(0 && "getFloatingRank(): illegal value for rank");
1854 case FloatRank: return FloatTy;
1855 case DoubleRank: return DoubleTy;
1856 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001857 }
Chris Lattner4b009652007-07-25 00:24:17 +00001858}
1859
Chris Lattner51285d82008-04-06 23:55:33 +00001860/// getFloatingTypeOrder - Compare the rank of the two specified floating
1861/// point types, ignoring the domain of the type (i.e. 'double' ==
1862/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1863/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001864int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1865 FloatingRank LHSR = getFloatingRank(LHS);
1866 FloatingRank RHSR = getFloatingRank(RHS);
1867
1868 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001869 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001870 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001871 return 1;
1872 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001873}
1874
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001875/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1876/// routine will assert if passed a built-in type that isn't an integer or enum,
1877/// or if it is not canonicalized.
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001878unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001879 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001880 if (EnumType* ET = dyn_cast<EnumType>(T))
1881 T = ET->getDecl()->getIntegerType().getTypePtr();
1882
1883 // There are two things which impact the integer rank: the width, and
1884 // the ordering of builtins. The builtin ordering is encoded in the
1885 // bottom three bits; the width is encoded in the bits above that.
1886 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1887 return FWIT->getWidth() << 3;
1888 }
1889
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001890 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001891 default: assert(0 && "getIntegerRank(): not a built-in integer");
1892 case BuiltinType::Bool:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001893 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001894 case BuiltinType::Char_S:
1895 case BuiltinType::Char_U:
1896 case BuiltinType::SChar:
1897 case BuiltinType::UChar:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001898 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001899 case BuiltinType::Short:
1900 case BuiltinType::UShort:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001901 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001902 case BuiltinType::Int:
1903 case BuiltinType::UInt:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001904 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001905 case BuiltinType::Long:
1906 case BuiltinType::ULong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001907 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner51285d82008-04-06 23:55:33 +00001908 case BuiltinType::LongLong:
1909 case BuiltinType::ULongLong:
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00001910 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001911 }
1912}
1913
Chris Lattner51285d82008-04-06 23:55:33 +00001914/// getIntegerTypeOrder - Returns the highest ranked integer type:
1915/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1916/// LHS < RHS, return -1.
1917int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001918 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1919 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001920 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001921
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001922 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1923 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001924
Chris Lattner51285d82008-04-06 23:55:33 +00001925 unsigned LHSRank = getIntegerRank(LHSC);
1926 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001927
Chris Lattner51285d82008-04-06 23:55:33 +00001928 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1929 if (LHSRank == RHSRank) return 0;
1930 return LHSRank > RHSRank ? 1 : -1;
1931 }
Chris Lattner4b009652007-07-25 00:24:17 +00001932
Chris Lattner51285d82008-04-06 23:55:33 +00001933 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1934 if (LHSUnsigned) {
1935 // If the unsigned [LHS] type is larger, return it.
1936 if (LHSRank >= RHSRank)
1937 return 1;
1938
1939 // If the signed type can represent all values of the unsigned type, it
1940 // wins. Because we are dealing with 2's complement and types that are
1941 // powers of two larger than each other, this is always safe.
1942 return -1;
1943 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001944
Chris Lattner51285d82008-04-06 23:55:33 +00001945 // If the unsigned [RHS] type is larger, return it.
1946 if (RHSRank >= LHSRank)
1947 return -1;
1948
1949 // If the signed type can represent all values of the unsigned type, it
1950 // wins. Because we are dealing with 2's complement and types that are
1951 // powers of two larger than each other, this is always safe.
1952 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001953}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001954
1955// getCFConstantStringType - Return the type used for constant CFStrings.
1956QualType ASTContext::getCFConstantStringType() {
1957 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001958 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001959 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001960 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001961 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001962
1963 // const int *isa;
1964 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001965 // int flags;
1966 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001967 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001968 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001969 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001970 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001971
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001972 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001973 for (unsigned i = 0; i < 4; ++i) {
1974 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1975 SourceLocation(), 0,
1976 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001977 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00001978 CFConstantStringTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001979 }
1980
1981 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001982 }
1983
1984 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001985}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001986
Anders Carlssonf58cac72008-08-30 19:34:46 +00001987QualType ASTContext::getObjCFastEnumerationStateType()
1988{
1989 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001990 ObjCFastEnumerationStateTypeDecl =
1991 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1992 &Idents.get("__objcFastEnumerationState"));
1993
Anders Carlssonf58cac72008-08-30 19:34:46 +00001994 QualType FieldTypes[] = {
1995 UnsignedLongTy,
1996 getPointerType(ObjCIdType),
1997 getPointerType(UnsignedLongTy),
1998 getConstantArrayType(UnsignedLongTy,
1999 llvm::APInt(32, 5), ArrayType::Normal, 0)
2000 };
2001
Douglas Gregor8acb7272008-12-11 16:49:14 +00002002 for (size_t i = 0; i < 4; ++i) {
2003 FieldDecl *Field = FieldDecl::Create(*this,
2004 ObjCFastEnumerationStateTypeDecl,
2005 SourceLocation(), 0,
2006 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00002007 /*Mutable=*/false);
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002008 ObjCFastEnumerationStateTypeDecl->addDecl(*this, Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002009 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00002010
Douglas Gregor8acb7272008-12-11 16:49:14 +00002011 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00002012 }
2013
2014 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
2015}
2016
Anders Carlssone3f02572007-10-29 06:33:42 +00002017// This returns true if a type has been typedefed to BOOL:
2018// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00002019static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002020 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00002021 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
2022 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002023
2024 return false;
2025}
2026
Ted Kremenek42730c52008-01-07 19:49:32 +00002027/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002028/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00002029int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00002030 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002031
2032 // Make all integer and enum types at least as large as an int
2033 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002034 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002035 // Treat arrays as pointers, since that's how they're passed in.
2036 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00002037 sz = getTypeSize(VoidPtrTy);
2038 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002039}
2040
Ted Kremenek42730c52008-01-07 19:49:32 +00002041/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002042/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002043void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00002044 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002045 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002046 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00002047 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002048 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002049 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002050 // Compute size of all parameters.
2051 // Start with computing size of a pointer in number of bytes.
2052 // FIXME: There might(should) be a better way of doing this computation!
2053 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00002054 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002055 // The first two arguments (self and _cmd) are pointers; account for
2056 // their size.
2057 int ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002058 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2059 E = Decl->param_end(); PI != E; ++PI) {
2060 QualType PType = (*PI)->getType();
2061 int sz = getObjCEncodingTypeSize(PType);
Ted Kremenek42730c52008-01-07 19:49:32 +00002062 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002063 ParmOffset += sz;
2064 }
2065 S += llvm::utostr(ParmOffset);
2066 S += "@0:";
2067 S += llvm::utostr(PtrSize);
2068
2069 // Argument types.
2070 ParmOffset = 2 * PtrSize;
Chris Lattner5c6b2c62009-02-20 18:43:26 +00002071 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
2072 E = Decl->param_end(); PI != E; ++PI) {
2073 ParmVarDecl *PVDecl = *PI;
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002074 QualType PType = PVDecl->getOriginalType();
2075 if (const ArrayType *AT =
Steve Naroff78380fb2009-04-14 00:03:58 +00002076 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal())) {
2077 // Use array's original type only if it has known number of
2078 // elements.
Steve Naroff6777bf32009-04-14 00:40:09 +00002079 if (!isa<ConstantArrayType>(AT))
Steve Naroff78380fb2009-04-14 00:03:58 +00002080 PType = PVDecl->getType();
2081 } else if (PType->isFunctionType())
2082 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002083 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002084 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00002085 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002086 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002087 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00002088 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00002089 }
2090}
2091
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002092/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002093/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002094/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
2095/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00002096/// Property attributes are stored as a comma-delimited C string. The simple
2097/// attributes readonly and bycopy are encoded as single characters. The
2098/// parametrized attributes, getter=name, setter=name, and ivar=name, are
2099/// encoded as single characters, followed by an identifier. Property types
2100/// are also encoded as a parametrized attribute. The characters used to encode
2101/// these attributes are defined by the following enumeration:
2102/// @code
2103/// enum PropertyAttributes {
2104/// kPropertyReadOnly = 'R', // property is read-only.
2105/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
2106/// kPropertyByref = '&', // property is a reference to the value last assigned
2107/// kPropertyDynamic = 'D', // property is dynamic
2108/// kPropertyGetter = 'G', // followed by getter selector name
2109/// kPropertySetter = 'S', // followed by setter selector name
2110/// kPropertyInstanceVariable = 'V' // followed by instance variable name
2111/// kPropertyType = 't' // followed by old-style type encoding.
2112/// kPropertyWeak = 'W' // 'weak' property
2113/// kPropertyStrong = 'P' // property GC'able
2114/// kPropertyNonAtomic = 'N' // property non-atomic
2115/// };
2116/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002117void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
2118 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00002119 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002120 // Collect information from the property implementation decl(s).
2121 bool Dynamic = false;
2122 ObjCPropertyImplDecl *SynthesizePID = 0;
2123
2124 // FIXME: Duplicated code due to poor abstraction.
2125 if (Container) {
2126 if (const ObjCCategoryImplDecl *CID =
2127 dyn_cast<ObjCCategoryImplDecl>(Container)) {
2128 for (ObjCCategoryImplDecl::propimpl_iterator
2129 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
2130 ObjCPropertyImplDecl *PID = *i;
2131 if (PID->getPropertyDecl() == PD) {
2132 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2133 Dynamic = true;
2134 } else {
2135 SynthesizePID = PID;
2136 }
2137 }
2138 }
2139 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002140 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002141 for (ObjCCategoryImplDecl::propimpl_iterator
2142 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
2143 ObjCPropertyImplDecl *PID = *i;
2144 if (PID->getPropertyDecl() == PD) {
2145 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
2146 Dynamic = true;
2147 } else {
2148 SynthesizePID = PID;
2149 }
2150 }
2151 }
2152 }
2153 }
2154
2155 // FIXME: This is not very efficient.
2156 S = "T";
2157
2158 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002159 // GCC has some special rules regarding encoding of properties which
2160 // closely resembles encoding of ivars.
Daniel Dunbar701c8502009-04-20 06:37:24 +00002161 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, 0,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002162 true /* outermost type */,
2163 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002164
2165 if (PD->isReadOnly()) {
2166 S += ",R";
2167 } else {
2168 switch (PD->getSetterKind()) {
2169 case ObjCPropertyDecl::Assign: break;
2170 case ObjCPropertyDecl::Copy: S += ",C"; break;
2171 case ObjCPropertyDecl::Retain: S += ",&"; break;
2172 }
2173 }
2174
2175 // It really isn't clear at all what this means, since properties
2176 // are "dynamic by default".
2177 if (Dynamic)
2178 S += ",D";
2179
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002180 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2181 S += ",N";
2182
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002183 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2184 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002185 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002186 }
2187
2188 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2189 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00002190 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002191 }
2192
2193 if (SynthesizePID) {
2194 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2195 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00002196 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00002197 }
2198
2199 // FIXME: OBJCGC: weak & strong
2200}
2201
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002202/// getLegacyIntegralTypeEncoding -
2203/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanian89155952009-02-11 23:59:18 +00002204/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002205/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2206///
2207void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2208 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2209 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanian89155952009-02-11 23:59:18 +00002210 if (BT->getKind() == BuiltinType::ULong &&
2211 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002212 PointeeTy = UnsignedIntTy;
Fariborz Jahanian89155952009-02-11 23:59:18 +00002213 else
2214 if (BT->getKind() == BuiltinType::Long &&
2215 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002216 PointeeTy = IntTy;
2217 }
2218 }
2219}
2220
Fariborz Jahanian248db262008-01-22 22:44:46 +00002221void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002222 const FieldDecl *Field) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002223 // We follow the behavior of gcc, expanding structures which are
2224 // directly pointed to, and expanding embedded structures. Note that
2225 // these rules are sufficient to prevent recursive encoding of the
2226 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00002227 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2228 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002229}
2230
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002231static void EncodeBitField(const ASTContext *Context, std::string& S,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002232 const FieldDecl *FD) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002233 const Expr *E = FD->getBitWidth();
2234 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2235 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2236 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2237 S += 'b';
2238 S += llvm::utostr(N);
2239}
2240
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002241void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2242 bool ExpandPointedToStructures,
2243 bool ExpandStructures,
Daniel Dunbar701c8502009-04-20 06:37:24 +00002244 const FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002245 bool OutermostType,
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002246 bool EncodingProperty) {
Anders Carlssone3f02572007-10-29 06:33:42 +00002247 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002248 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002249 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002250 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002251 else {
2252 char encoding;
2253 switch (BT->getKind()) {
2254 default: assert(0 && "Unhandled builtin type kind");
2255 case BuiltinType::Void: encoding = 'v'; break;
2256 case BuiltinType::Bool: encoding = 'B'; break;
2257 case BuiltinType::Char_U:
2258 case BuiltinType::UChar: encoding = 'C'; break;
2259 case BuiltinType::UShort: encoding = 'S'; break;
2260 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002261 case BuiltinType::ULong:
2262 encoding =
2263 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2264 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002265 case BuiltinType::ULongLong: encoding = 'Q'; break;
2266 case BuiltinType::Char_S:
2267 case BuiltinType::SChar: encoding = 'c'; break;
2268 case BuiltinType::Short: encoding = 's'; break;
2269 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanianebd95752009-02-11 22:31:45 +00002270 case BuiltinType::Long:
2271 encoding =
2272 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2273 break;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002274 case BuiltinType::LongLong: encoding = 'q'; break;
2275 case BuiltinType::Float: encoding = 'f'; break;
2276 case BuiltinType::Double: encoding = 'd'; break;
2277 case BuiltinType::LongDouble: encoding = 'd'; break;
2278 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002279
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002280 S += encoding;
2281 }
Anders Carlsson70e16dd2009-04-09 21:55:45 +00002282 } else if (const ComplexType *CT = T->getAsComplexType()) {
2283 S += 'j';
2284 getObjCEncodingForTypeImpl(CT->getElementType(), S, false, false, 0, false,
2285 false);
2286 } else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002287 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2288 ExpandPointedToStructures,
2289 ExpandStructures, FD);
2290 if (FD || EncodingProperty) {
2291 // Note that we do extended encoding of protocol qualifer list
2292 // Only when doing ivar or property encoding.
2293 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2294 S += '"';
2295 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2296 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2297 S += '<';
2298 S += Proto->getNameAsString();
2299 S += '>';
2300 }
2301 S += '"';
2302 }
2303 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00002304 }
2305 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002306 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002307 bool isReadOnly = false;
2308 // For historical/compatibility reasons, the read-only qualifier of the
2309 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2310 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2311 // Also, do not emit the 'r' for anything but the outermost type!
2312 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2313 if (OutermostType && T.isConstQualified()) {
2314 isReadOnly = true;
2315 S += 'r';
2316 }
2317 }
2318 else if (OutermostType) {
2319 QualType P = PointeeTy;
2320 while (P->getAsPointerType())
2321 P = P->getAsPointerType()->getPointeeType();
2322 if (P.isConstQualified()) {
2323 isReadOnly = true;
2324 S += 'r';
2325 }
2326 }
2327 if (isReadOnly) {
2328 // Another legacy compatibility encoding. Some ObjC qualifier and type
2329 // combinations need to be rearranged.
2330 // Rewrite "in const" from "nr" to "rn"
2331 const char * s = S.c_str();
2332 int len = S.length();
2333 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2334 std::string replace = "rn";
2335 S.replace(S.end()-2, S.end(), replace);
2336 }
2337 }
Steve Naroff17c03822009-02-12 17:52:19 +00002338 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002339 S += '@';
2340 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002341 }
2342 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian94675042009-02-16 21:41:04 +00002343 if (!EncodingProperty &&
Fariborz Jahanian6bc0f2d2009-02-16 22:09:26 +00002344 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00002345 // Another historical/compatibility reason.
2346 // We encode the underlying type which comes out as
2347 // {...};
2348 S += '^';
2349 getObjCEncodingForTypeImpl(PointeeTy, S,
2350 false, ExpandPointedToStructures,
2351 NULL);
2352 return;
2353 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002354 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002355 if (FD || EncodingProperty) {
Fariborz Jahanianc69da272009-02-21 18:23:24 +00002356 const ObjCInterfaceType *OIT =
2357 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002358 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002359 S += '"';
2360 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00002361 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2362 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2363 S += '<';
2364 S += Proto->getNameAsString();
2365 S += '>';
2366 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00002367 S += '"';
2368 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00002369 return;
Steve Naroff17c03822009-02-12 17:52:19 +00002370 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002371 S += '#';
2372 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00002373 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002374 S += ':';
2375 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00002376 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002377
2378 if (PointeeTy->isCharType()) {
2379 // char pointer types should be encoded as '*' unless it is a
2380 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00002381 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00002382 S += '*';
2383 return;
2384 }
2385 }
2386
2387 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002388 getLegacyIntegralTypeEncoding(PointeeTy);
2389
2390 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00002391 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002392 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00002393 } else if (const ArrayType *AT =
2394 // Ignore type qualifiers etc.
2395 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson858c64d2009-02-22 01:38:57 +00002396 if (isa<IncompleteArrayType>(AT)) {
2397 // Incomplete arrays are encoded as a pointer to the array element.
2398 S += '^';
2399
2400 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2401 false, ExpandStructures, FD);
2402 } else {
2403 S += '[';
Anders Carlsson36f07d82007-10-29 05:01:08 +00002404
Anders Carlsson858c64d2009-02-22 01:38:57 +00002405 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2406 S += llvm::utostr(CAT->getSize().getZExtValue());
2407 else {
2408 //Variable length arrays are encoded as a regular array with 0 elements.
2409 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2410 S += '0';
2411 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00002412
Anders Carlsson858c64d2009-02-22 01:38:57 +00002413 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2414 false, ExpandStructures, FD);
2415 S += ']';
2416 }
Anders Carlsson5695bb72007-10-30 00:06:20 +00002417 } else if (T->getAsFunctionType()) {
2418 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002419 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002420 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002421 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002422 // Anonymous structures print as '?'
2423 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2424 S += II->getName();
2425 } else {
2426 S += '?';
2427 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002428 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002429 S += '=';
Douglas Gregorc55b0b02009-04-09 21:40:53 +00002430 for (RecordDecl::field_iterator Field = RDecl->field_begin(*this),
2431 FieldEnd = RDecl->field_end(*this);
Douglas Gregor8acb7272008-12-11 16:49:14 +00002432 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002433 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002434 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002435 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002436 S += '"';
2437 }
2438
2439 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002440 if (Field->isBitField()) {
2441 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2442 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002443 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002444 QualType qt = Field->getType();
2445 getLegacyIntegralTypeEncoding(qt);
2446 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002447 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002448 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002449 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002450 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002451 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002452 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002453 if (FD && FD->isBitField())
2454 EncodeBitField(this, S, FD);
2455 else
2456 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002457 } else if (T->isBlockPointerType()) {
Steve Naroff725e0662009-02-02 18:24:29 +00002458 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002459 } else if (T->isObjCInterfaceType()) {
2460 // @encode(class_name)
2461 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2462 S += '{';
2463 const IdentifierInfo *II = OI->getIdentifier();
2464 S += II->getName();
2465 S += '=';
Chris Lattner9329cf52009-03-31 08:48:01 +00002466 llvm::SmallVector<FieldDecl*, 32> RecFields;
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002467 CollectObjCIvars(OI, RecFields);
Chris Lattner9329cf52009-03-31 08:48:01 +00002468 for (unsigned i = 0, e = RecFields.size(); i != e; ++i) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002469 if (RecFields[i]->isBitField())
2470 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2471 RecFields[i]);
2472 else
2473 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2474 FD);
2475 }
2476 S += '}';
2477 }
2478 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002479 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002480}
2481
Ted Kremenek42730c52008-01-07 19:49:32 +00002482void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002483 std::string& S) const {
2484 if (QT & Decl::OBJC_TQ_In)
2485 S += 'n';
2486 if (QT & Decl::OBJC_TQ_Inout)
2487 S += 'N';
2488 if (QT & Decl::OBJC_TQ_Out)
2489 S += 'o';
2490 if (QT & Decl::OBJC_TQ_Bycopy)
2491 S += 'O';
2492 if (QT & Decl::OBJC_TQ_Byref)
2493 S += 'R';
2494 if (QT & Decl::OBJC_TQ_Oneway)
2495 S += 'V';
2496}
2497
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002498void ASTContext::setBuiltinVaListType(QualType T)
2499{
2500 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2501
2502 BuiltinVaListType = T;
2503}
2504
Ted Kremenek42730c52008-01-07 19:49:32 +00002505void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002506{
Ted Kremenek42730c52008-01-07 19:49:32 +00002507 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002508
2509 // typedef struct objc_object *id;
2510 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002511 // User error - caller will issue diagnostics.
2512 if (!ptr)
2513 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002514 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002515 // User error - caller will issue diagnostics.
2516 if (!rec)
2517 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002518 IdStructType = rec;
2519}
2520
Ted Kremenek42730c52008-01-07 19:49:32 +00002521void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002522{
Ted Kremenek42730c52008-01-07 19:49:32 +00002523 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002524
2525 // typedef struct objc_selector *SEL;
2526 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002527 if (!ptr)
2528 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002529 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002530 if (!rec)
2531 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002532 SelStructType = rec;
2533}
2534
Ted Kremenek42730c52008-01-07 19:49:32 +00002535void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002536{
Ted Kremenek42730c52008-01-07 19:49:32 +00002537 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002538}
2539
Ted Kremenek42730c52008-01-07 19:49:32 +00002540void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002541{
Ted Kremenek42730c52008-01-07 19:49:32 +00002542 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002543
2544 // typedef struct objc_class *Class;
2545 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2546 assert(ptr && "'Class' incorrectly typed");
2547 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2548 assert(rec && "'Class' incorrectly typed");
2549 ClassStructType = rec;
2550}
2551
Ted Kremenek42730c52008-01-07 19:49:32 +00002552void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2553 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002554 "'NSConstantString' type already set!");
2555
Ted Kremenek42730c52008-01-07 19:49:32 +00002556 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002557}
2558
Douglas Gregordd13e842009-03-30 22:58:21 +00002559/// \brief Retrieve the template name that represents a qualified
2560/// template name such as \c std::vector.
2561TemplateName ASTContext::getQualifiedTemplateName(NestedNameSpecifier *NNS,
2562 bool TemplateKeyword,
2563 TemplateDecl *Template) {
2564 llvm::FoldingSetNodeID ID;
2565 QualifiedTemplateName::Profile(ID, NNS, TemplateKeyword, Template);
2566
2567 void *InsertPos = 0;
2568 QualifiedTemplateName *QTN =
2569 QualifiedTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2570 if (!QTN) {
2571 QTN = new (*this,4) QualifiedTemplateName(NNS, TemplateKeyword, Template);
2572 QualifiedTemplateNames.InsertNode(QTN, InsertPos);
2573 }
2574
2575 return TemplateName(QTN);
2576}
2577
2578/// \brief Retrieve the template name that represents a dependent
2579/// template name such as \c MetaFun::template apply.
2580TemplateName ASTContext::getDependentTemplateName(NestedNameSpecifier *NNS,
2581 const IdentifierInfo *Name) {
2582 assert(NNS->isDependent() && "Nested name specifier must be dependent");
2583
2584 llvm::FoldingSetNodeID ID;
2585 DependentTemplateName::Profile(ID, NNS, Name);
2586
2587 void *InsertPos = 0;
2588 DependentTemplateName *QTN =
2589 DependentTemplateNames.FindNodeOrInsertPos(ID, InsertPos);
2590
2591 if (QTN)
2592 return TemplateName(QTN);
2593
2594 NestedNameSpecifier *CanonNNS = getCanonicalNestedNameSpecifier(NNS);
2595 if (CanonNNS == NNS) {
2596 QTN = new (*this,4) DependentTemplateName(NNS, Name);
2597 } else {
2598 TemplateName Canon = getDependentTemplateName(CanonNNS, Name);
2599 QTN = new (*this,4) DependentTemplateName(NNS, Name, Canon);
2600 }
2601
2602 DependentTemplateNames.InsertNode(QTN, InsertPos);
2603 return TemplateName(QTN);
2604}
2605
Douglas Gregorc6507e42008-11-03 14:12:49 +00002606/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002607/// TargetInfo, produce the corresponding type. The unsigned @p Type
2608/// is actually a value of type @c TargetInfo::IntType.
2609QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002610 switch (Type) {
2611 case TargetInfo::NoInt: return QualType();
2612 case TargetInfo::SignedShort: return ShortTy;
2613 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2614 case TargetInfo::SignedInt: return IntTy;
2615 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2616 case TargetInfo::SignedLong: return LongTy;
2617 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2618 case TargetInfo::SignedLongLong: return LongLongTy;
2619 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2620 }
2621
2622 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002623 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002624}
Ted Kremenek118930e2008-07-24 23:58:27 +00002625
2626//===----------------------------------------------------------------------===//
2627// Type Predicates.
2628//===----------------------------------------------------------------------===//
2629
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002630/// isObjCNSObjectType - Return true if this is an NSObject object using
2631/// NSObject attribute on a c-style pointer type.
2632/// FIXME - Make it work directly on types.
2633///
2634bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2635 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2636 if (TypedefDecl *TD = TDT->getDecl())
2637 if (TD->getAttr<ObjCNSObjectAttr>())
2638 return true;
2639 }
2640 return false;
2641}
2642
Ted Kremenek118930e2008-07-24 23:58:27 +00002643/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2644/// to an object type. This includes "id" and "Class" (two 'special' pointers
2645/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2646/// ID type).
2647bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroff6805fc42009-02-23 18:36:16 +00002648 if (Ty->isObjCQualifiedIdType())
Ted Kremenek118930e2008-07-24 23:58:27 +00002649 return true;
2650
Steve Naroffd9e00802008-10-21 18:24:04 +00002651 // Blocks are objects.
2652 if (Ty->isBlockPointerType())
2653 return true;
2654
2655 // All other object types are pointers.
Chris Lattnera008d172009-04-12 23:51:02 +00002656 const PointerType *PT = Ty->getAsPointerType();
2657 if (PT == 0)
Ted Kremenek118930e2008-07-24 23:58:27 +00002658 return false;
2659
Chris Lattnera008d172009-04-12 23:51:02 +00002660 // If this a pointer to an interface (e.g. NSString*), it is ok.
2661 if (PT->getPointeeType()->isObjCInterfaceType() ||
2662 // If is has NSObject attribute, OK as well.
2663 isObjCNSObjectType(Ty))
2664 return true;
2665
Ted Kremenek118930e2008-07-24 23:58:27 +00002666 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2667 // pointer types. This looks for the typedef specifically, not for the
Chris Lattnera008d172009-04-12 23:51:02 +00002668 // underlying type. Iteratively strip off typedefs so that we can handle
2669 // typedefs of typedefs.
2670 while (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2671 if (Ty.getUnqualifiedType() == getObjCIdType() ||
2672 Ty.getUnqualifiedType() == getObjCClassType())
2673 return true;
2674
2675 Ty = TDT->getDecl()->getUnderlyingType();
2676 }
Ted Kremenek118930e2008-07-24 23:58:27 +00002677
Chris Lattnera008d172009-04-12 23:51:02 +00002678 return false;
Ted Kremenek118930e2008-07-24 23:58:27 +00002679}
2680
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002681/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2682/// garbage collection attribute.
2683///
2684QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002685 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002686 if (getLangOptions().ObjC1 &&
2687 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002688 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002689 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahanianbbd4ca92009-02-19 23:36:06 +00002690 // (or pointers to them) be treated as though they were declared
2691 // as __strong.
2692 if (GCAttrs == QualType::GCNone) {
2693 if (isObjCObjectPointerType(Ty))
2694 GCAttrs = QualType::Strong;
2695 else if (Ty->isPointerType())
2696 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2697 }
Fariborz Jahaniand7b01972009-04-11 00:00:54 +00002698 // Non-pointers have none gc'able attribute regardless of the attribute
2699 // set on them.
2700 else if (!isObjCObjectPointerType(Ty) && !Ty->isPointerType())
2701 return QualType::GCNone;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002702 }
Chris Lattner18b5a9a2009-02-18 22:53:11 +00002703 return GCAttrs;
Fariborz Jahanianb8ca6ff2009-02-18 21:49:28 +00002704}
2705
Chris Lattner6ff358b2008-04-07 06:51:04 +00002706//===----------------------------------------------------------------------===//
2707// Type Compatibility Testing
2708//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002709
Steve Naroff3454b6c2008-09-04 15:10:53 +00002710/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002711/// block types. Types must be strictly compatible here. For example,
2712/// C unfortunately doesn't produce an error for the following:
2713///
2714/// int (*emptyArgFunc)();
2715/// int (*intArgList)(int) = emptyArgFunc;
2716///
2717/// For blocks, we will produce an error for the following (similar to C++):
2718///
2719/// int (^emptyArgBlock)();
2720/// int (^intArgBlock)(int) = emptyArgBlock;
2721///
2722/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2723///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002724bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002725 const FunctionType *lbase = lhs->getAsFunctionType();
2726 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002727 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2728 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Mike Stump896ceed2009-04-01 01:17:39 +00002729 if (lproto && rproto == 0)
2730 return false;
2731 return !mergeTypes(lhs, rhs).isNull();
Steve Naroff3454b6c2008-09-04 15:10:53 +00002732}
2733
Chris Lattner6ff358b2008-04-07 06:51:04 +00002734/// areCompatVectorTypes - Return true if the two specified vector types are
2735/// compatible.
2736static bool areCompatVectorTypes(const VectorType *LHS,
2737 const VectorType *RHS) {
2738 assert(LHS->isCanonical() && RHS->isCanonical());
2739 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002740 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002741}
2742
Eli Friedman0d9549b2008-08-22 00:56:42 +00002743/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002744/// compatible for assignment from RHS to LHS. This handles validation of any
2745/// protocol qualifiers on the LHS or RHS.
2746///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002747bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2748 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002749 // Verify that the base decls are compatible: the RHS must be a subclass of
2750 // the LHS.
2751 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2752 return false;
2753
2754 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2755 // protocol qualified at all, then we are good.
2756 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2757 return true;
2758
2759 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2760 // isn't a superset.
2761 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2762 return true; // FIXME: should return false!
2763
2764 // Finally, we must have two protocol-qualified interfaces.
2765 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2766 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
Chris Lattner6ff358b2008-04-07 06:51:04 +00002767
Steve Naroff98e71b82009-03-01 16:12:44 +00002768 // All LHS protocols must have a presence on the RHS.
2769 assert(LHSP->qual_begin() != LHSP->qual_end() && "Empty LHS protocol list?");
Chris Lattner6ff358b2008-04-07 06:51:04 +00002770
Steve Naroff98e71b82009-03-01 16:12:44 +00002771 for (ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin(),
2772 LHSPE = LHSP->qual_end();
2773 LHSPI != LHSPE; LHSPI++) {
2774 bool RHSImplementsProtocol = false;
2775
2776 // If the RHS doesn't implement the protocol on the left, the types
2777 // are incompatible.
2778 for (ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin(),
2779 RHSPE = RHSP->qual_end();
2780 !RHSImplementsProtocol && (RHSPI != RHSPE); RHSPI++) {
2781 if ((*RHSPI)->lookupProtocolNamed((*LHSPI)->getIdentifier()))
2782 RHSImplementsProtocol = true;
2783 }
2784 // FIXME: For better diagnostics, consider passing back the protocol name.
2785 if (!RHSImplementsProtocol)
2786 return false;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002787 }
Steve Naroff98e71b82009-03-01 16:12:44 +00002788 // The RHS implements all protocols listed on the LHS.
2789 return true;
Chris Lattner6ff358b2008-04-07 06:51:04 +00002790}
2791
Steve Naroff17c03822009-02-12 17:52:19 +00002792bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2793 // get the "pointed to" types
2794 const PointerType *LHSPT = LHS->getAsPointerType();
2795 const PointerType *RHSPT = RHS->getAsPointerType();
2796
2797 if (!LHSPT || !RHSPT)
2798 return false;
2799
2800 QualType lhptee = LHSPT->getPointeeType();
2801 QualType rhptee = RHSPT->getPointeeType();
2802 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2803 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2804 // ID acts sort of like void* for ObjC interfaces
2805 if (LHSIface && isObjCIdStructType(rhptee))
2806 return true;
2807 if (RHSIface && isObjCIdStructType(lhptee))
2808 return true;
2809 if (!LHSIface || !RHSIface)
2810 return false;
2811 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2812 canAssignObjCInterfaces(RHSIface, LHSIface);
2813}
2814
Steve Naroff85f0dc52007-10-15 20:41:53 +00002815/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2816/// both shall have the identically qualified version of a compatible type.
2817/// C99 6.2.7p1: Two types have compatible types if their types are the
2818/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002819bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2820 return !mergeTypes(LHS, RHS).isNull();
2821}
2822
2823QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2824 const FunctionType *lbase = lhs->getAsFunctionType();
2825 const FunctionType *rbase = rhs->getAsFunctionType();
Douglas Gregor4fa58902009-02-26 23:50:07 +00002826 const FunctionProtoType *lproto = dyn_cast<FunctionProtoType>(lbase);
2827 const FunctionProtoType *rproto = dyn_cast<FunctionProtoType>(rbase);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002828 bool allLTypes = true;
2829 bool allRTypes = true;
2830
2831 // Check return type
2832 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2833 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002834 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2835 allLTypes = false;
2836 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2837 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002838
2839 if (lproto && rproto) { // two C99 style function prototypes
2840 unsigned lproto_nargs = lproto->getNumArgs();
2841 unsigned rproto_nargs = rproto->getNumArgs();
2842
2843 // Compatible functions must have the same number of arguments
2844 if (lproto_nargs != rproto_nargs)
2845 return QualType();
2846
2847 // Variadic and non-variadic functions aren't compatible
2848 if (lproto->isVariadic() != rproto->isVariadic())
2849 return QualType();
2850
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002851 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2852 return QualType();
2853
Eli Friedman0d9549b2008-08-22 00:56:42 +00002854 // Check argument compatibility
2855 llvm::SmallVector<QualType, 10> types;
2856 for (unsigned i = 0; i < lproto_nargs; i++) {
2857 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2858 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2859 QualType argtype = mergeTypes(largtype, rargtype);
2860 if (argtype.isNull()) return QualType();
2861 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002862 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2863 allLTypes = false;
2864 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2865 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002866 }
2867 if (allLTypes) return lhs;
2868 if (allRTypes) return rhs;
2869 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002870 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002871 }
2872
2873 if (lproto) allRTypes = false;
2874 if (rproto) allLTypes = false;
2875
Douglas Gregor4fa58902009-02-26 23:50:07 +00002876 const FunctionProtoType *proto = lproto ? lproto : rproto;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002877 if (proto) {
2878 if (proto->isVariadic()) return QualType();
2879 // Check that the types are compatible with the types that
2880 // would result from default argument promotions (C99 6.7.5.3p15).
2881 // The only types actually affected are promotable integer
2882 // types and floats, which would be passed as a different
2883 // type depending on whether the prototype is visible.
2884 unsigned proto_nargs = proto->getNumArgs();
2885 for (unsigned i = 0; i < proto_nargs; ++i) {
2886 QualType argTy = proto->getArgType(i);
2887 if (argTy->isPromotableIntegerType() ||
2888 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2889 return QualType();
2890 }
2891
2892 if (allLTypes) return lhs;
2893 if (allRTypes) return rhs;
2894 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002895 proto->getNumArgs(), lproto->isVariadic(),
2896 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002897 }
2898
2899 if (allLTypes) return lhs;
2900 if (allRTypes) return rhs;
Douglas Gregor4fa58902009-02-26 23:50:07 +00002901 return getFunctionNoProtoType(retType);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002902}
2903
2904QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002905 // C++ [expr]: If an expression initially has the type "reference to T", the
2906 // type is adjusted to "T" prior to any further analysis, the expression
2907 // designates the object or function denoted by the reference, and the
Sebastian Redlce6fff02009-03-16 23:22:08 +00002908 // expression is an lvalue unless the reference is an rvalue reference and
2909 // the expression is a function call (possibly inside parentheses).
Eli Friedman0d9549b2008-08-22 00:56:42 +00002910 // FIXME: C++ shouldn't be going through here! The rules are different
2911 // enough that they should be handled separately.
Sebastian Redlce6fff02009-03-16 23:22:08 +00002912 // FIXME: Merging of lvalue and rvalue references is incorrect. C++ *really*
2913 // shouldn't be going through here!
Eli Friedman0d9549b2008-08-22 00:56:42 +00002914 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002915 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002916 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002917 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002918
Eli Friedman0d9549b2008-08-22 00:56:42 +00002919 QualType LHSCan = getCanonicalType(LHS),
2920 RHSCan = getCanonicalType(RHS);
2921
2922 // If two types are identical, they are compatible.
2923 if (LHSCan == RHSCan)
2924 return LHS;
2925
2926 // If the qualifiers are different, the types aren't compatible
Eli Friedman94fcc9a2009-02-27 23:04:43 +00002927 // Note that we handle extended qualifiers later, in the
2928 // case for ExtQualType.
2929 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers())
Eli Friedman0d9549b2008-08-22 00:56:42 +00002930 return QualType();
2931
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00002932 Type::TypeClass LHSClass = LHSCan.getUnqualifiedType()->getTypeClass();
2933 Type::TypeClass RHSClass = RHSCan.getUnqualifiedType()->getTypeClass();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002934
Chris Lattnerc38d4522008-01-14 05:45:46 +00002935 // We want to consider the two function types to be the same for these
2936 // comparisons, just force one to the other.
2937 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2938 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002939
2940 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002941 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2942 LHSClass = Type::ConstantArray;
2943 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2944 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002945
Nate Begemanaf6ed502008-04-18 23:10:10 +00002946 // Canonicalize ExtVector -> Vector.
2947 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2948 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002949
Chris Lattner7cdcb252008-04-07 06:38:24 +00002950 // Consider qualified interfaces and interfaces the same.
2951 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2952 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002953
Chris Lattnerb5709e22008-04-07 05:43:21 +00002954 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002955 if (LHSClass != RHSClass) {
Steve Naroff0bbc1352009-02-21 16:18:07 +00002956 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2957 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
Fariborz Jahanian0dc684e2009-04-15 21:54:48 +00002958
Steve Naroff0773c582009-04-14 15:11:46 +00002959 // 'id' and 'Class' act sort of like void* for ObjC interfaces
2960 if (LHSIface && (isObjCIdStructType(RHS) || isObjCClassStructType(RHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00002961 return LHS;
Steve Naroff0773c582009-04-14 15:11:46 +00002962 if (RHSIface && (isObjCIdStructType(LHS) || isObjCClassStructType(LHS)))
Steve Naroff0bbc1352009-02-21 16:18:07 +00002963 return RHS;
2964
Steve Naroff28ceff72008-12-10 22:14:21 +00002965 // ID is compatible with all qualified id types.
2966 if (LHS->isObjCQualifiedIdType()) {
2967 if (const PointerType *PT = RHS->getAsPointerType()) {
2968 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00002969 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002970 return LHS;
2971 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2972 // Unfortunately, this API is part of Sema (which we don't have access
2973 // to. Need to refactor. The following check is insufficient, since we
2974 // need to make sure the class implements the protocol.
2975 if (pType->isObjCInterfaceType())
2976 return LHS;
2977 }
2978 }
2979 if (RHS->isObjCQualifiedIdType()) {
2980 if (const PointerType *PT = LHS->getAsPointerType()) {
2981 QualType pType = PT->getPointeeType();
Steve Naroff0773c582009-04-14 15:11:46 +00002982 if (isObjCIdStructType(pType) || isObjCClassStructType(pType))
Steve Naroff28ceff72008-12-10 22:14:21 +00002983 return RHS;
2984 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2985 // Unfortunately, this API is part of Sema (which we don't have access
2986 // to. Need to refactor. The following check is insufficient, since we
2987 // need to make sure the class implements the protocol.
2988 if (pType->isObjCInterfaceType())
2989 return RHS;
2990 }
2991 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002992 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2993 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002994 if (const EnumType* ETy = LHS->getAsEnumType()) {
2995 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2996 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002997 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002998 if (const EnumType* ETy = RHS->getAsEnumType()) {
2999 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
3000 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00003001 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003002
Eli Friedman0d9549b2008-08-22 00:56:42 +00003003 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003004 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00003005
Steve Naroffc88babe2008-01-09 22:43:08 +00003006 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00003007 switch (LHSClass) {
Douglas Gregor4fa58902009-02-26 23:50:07 +00003008#define TYPE(Class, Base)
3009#define ABSTRACT_TYPE(Class, Base)
3010#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3011#define DEPENDENT_TYPE(Class, Base) case Type::Class:
3012#include "clang/AST/TypeNodes.def"
3013 assert(false && "Non-canonical and dependent types shouldn't get here");
3014 return QualType();
3015
Sebastian Redlce6fff02009-03-16 23:22:08 +00003016 case Type::LValueReference:
3017 case Type::RValueReference:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003018 case Type::MemberPointer:
3019 assert(false && "C++ should never be in mergeTypes");
3020 return QualType();
3021
3022 case Type::IncompleteArray:
3023 case Type::VariableArray:
3024 case Type::FunctionProto:
3025 case Type::ExtVector:
3026 case Type::ObjCQualifiedInterface:
3027 assert(false && "Types are eliminated above");
3028 return QualType();
3029
Chris Lattnerc38d4522008-01-14 05:45:46 +00003030 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003031 {
3032 // Merge two pointer types, while trying to preserve typedef info
3033 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
3034 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
3035 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3036 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003037 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3038 return LHS;
3039 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3040 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003041 return getPointerType(ResultType);
3042 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00003043 case Type::BlockPointer:
3044 {
3045 // Merge two block pointer types, while trying to preserve typedef info
3046 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
3047 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
3048 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
3049 if (ResultType.isNull()) return QualType();
3050 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
3051 return LHS;
3052 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
3053 return RHS;
3054 return getBlockPointerType(ResultType);
3055 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003056 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003057 {
3058 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
3059 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
3060 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
3061 return QualType();
3062
3063 QualType LHSElem = getAsArrayType(LHS)->getElementType();
3064 QualType RHSElem = getAsArrayType(RHS)->getElementType();
3065 QualType ResultType = mergeTypes(LHSElem, RHSElem);
3066 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003067 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3068 return LHS;
3069 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3070 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003071 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
3072 ArrayType::ArraySizeModifier(), 0);
3073 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
3074 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003075 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
3076 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003077 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
3078 return LHS;
3079 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
3080 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003081 if (LVAT) {
3082 // FIXME: This isn't correct! But tricky to implement because
3083 // the array's size has to be the size of LHS, but the type
3084 // has to be different.
3085 return LHS;
3086 }
3087 if (RVAT) {
3088 // FIXME: This isn't correct! But tricky to implement because
3089 // the array's size has to be the size of RHS, but the type
3090 // has to be different.
3091 return RHS;
3092 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00003093 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
3094 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003095 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00003096 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00003097 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003098 return mergeFunctionTypes(LHS, RHS);
Douglas Gregor4fa58902009-02-26 23:50:07 +00003099 case Type::Record:
Douglas Gregor4fa58902009-02-26 23:50:07 +00003100 case Type::Enum:
Eli Friedman0d9549b2008-08-22 00:56:42 +00003101 // FIXME: Why are these compatible?
Steve Naroff17c03822009-02-12 17:52:19 +00003102 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
3103 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00003104 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00003105 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003106 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00003107 return QualType();
Daniel Dunbar457f33d2009-01-28 21:22:12 +00003108 case Type::Complex:
3109 // Distinct complex types are incompatible.
3110 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00003111 case Type::Vector:
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003112 // FIXME: The merged type should be an ExtVector!
Eli Friedman0d9549b2008-08-22 00:56:42 +00003113 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
3114 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00003115 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003116 case Type::ObjCInterface: {
Steve Naroff0bbc1352009-02-21 16:18:07 +00003117 // Check if the interfaces are assignment compatible.
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003118 // FIXME: This should be type compatibility, e.g. whether
3119 // "LHS x; RHS x;" at global scope is legal.
Steve Naroff0bbc1352009-02-21 16:18:07 +00003120 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
3121 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
3122 if (LHSIface && RHSIface &&
3123 canAssignObjCInterfaces(LHSIface, RHSIface))
3124 return LHS;
3125
Eli Friedman0d9549b2008-08-22 00:56:42 +00003126 return QualType();
Cédric Venet23536132009-02-21 17:14:49 +00003127 }
Steve Naroff28ceff72008-12-10 22:14:21 +00003128 case Type::ObjCQualifiedId:
3129 // Distinct qualified id's are not compatible.
3130 return QualType();
Eli Friedman94fcc9a2009-02-27 23:04:43 +00003131 case Type::FixedWidthInt:
3132 // Distinct fixed-width integers are not compatible.
3133 return QualType();
3134 case Type::ObjCQualifiedClass:
3135 // Distinct qualified classes are not compatible.
3136 return QualType();
3137 case Type::ExtQual:
3138 // FIXME: ExtQual types can be compatible even if they're not
3139 // identical!
3140 return QualType();
3141 // First attempt at an implementation, but I'm not really sure it's
3142 // right...
3143#if 0
3144 ExtQualType* LQual = cast<ExtQualType>(LHSCan);
3145 ExtQualType* RQual = cast<ExtQualType>(RHSCan);
3146 if (LQual->getAddressSpace() != RQual->getAddressSpace() ||
3147 LQual->getObjCGCAttr() != RQual->getObjCGCAttr())
3148 return QualType();
3149 QualType LHSBase, RHSBase, ResultType, ResCanUnqual;
3150 LHSBase = QualType(LQual->getBaseType(), 0);
3151 RHSBase = QualType(RQual->getBaseType(), 0);
3152 ResultType = mergeTypes(LHSBase, RHSBase);
3153 if (ResultType.isNull()) return QualType();
3154 ResCanUnqual = getCanonicalType(ResultType).getUnqualifiedType();
3155 if (LHSCan.getUnqualifiedType() == ResCanUnqual)
3156 return LHS;
3157 if (RHSCan.getUnqualifiedType() == ResCanUnqual)
3158 return RHS;
3159 ResultType = getAddrSpaceQualType(ResultType, LQual->getAddressSpace());
3160 ResultType = getObjCGCQualType(ResultType, LQual->getObjCGCAttr());
3161 ResultType.setCVRQualifiers(LHSCan.getCVRQualifiers());
3162 return ResultType;
3163#endif
Douglas Gregordd13e842009-03-30 22:58:21 +00003164
3165 case Type::TemplateSpecialization:
3166 assert(false && "Dependent types have no size");
3167 break;
Steve Naroff85f0dc52007-10-15 20:41:53 +00003168 }
Douglas Gregor4fa58902009-02-26 23:50:07 +00003169
3170 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00003171}
Ted Kremenek738e6c02007-10-31 17:10:13 +00003172
Chris Lattner1d78a862008-04-07 07:01:58 +00003173//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00003174// Integer Predicates
3175//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00003176
Eli Friedman0832dbc2008-06-28 06:23:08 +00003177unsigned ASTContext::getIntWidth(QualType T) {
3178 if (T == BoolTy)
3179 return 1;
Eli Friedmanff3fcdf2009-02-13 02:31:07 +00003180 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
3181 return FWIT->getWidth();
3182 }
3183 // For builtin types, just use the standard type sizing method
Eli Friedman0832dbc2008-06-28 06:23:08 +00003184 return (unsigned)getTypeSize(T);
3185}
3186
3187QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
3188 assert(T->isSignedIntegerType() && "Unexpected type");
3189 if (const EnumType* ETy = T->getAsEnumType())
3190 T = ETy->getDecl()->getIntegerType();
3191 const BuiltinType* BTy = T->getAsBuiltinType();
3192 assert (BTy && "Unexpected signed integer type");
3193 switch (BTy->getKind()) {
3194 case BuiltinType::Char_S:
3195 case BuiltinType::SChar:
3196 return UnsignedCharTy;
3197 case BuiltinType::Short:
3198 return UnsignedShortTy;
3199 case BuiltinType::Int:
3200 return UnsignedIntTy;
3201 case BuiltinType::Long:
3202 return UnsignedLongTy;
3203 case BuiltinType::LongLong:
3204 return UnsignedLongLongTy;
3205 default:
3206 assert(0 && "Unexpected signed integer type");
3207 return QualType();
3208 }
3209}
3210
3211
3212//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00003213// Serialization Support
3214//===----------------------------------------------------------------------===//
3215
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003216enum {
3217 BasicMetadataBlock = 1,
3218 ASTContextBlock = 2,
3219 DeclsBlock = 3
3220};
3221
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003222void ASTContext::EmitASTBitcodeBuffer(std::vector<unsigned char> &Buffer) const{
3223 // Create bitstream.
3224 llvm::BitstreamWriter Stream(Buffer);
3225
3226 // Emit the preamble.
3227 Stream.Emit((unsigned)'B', 8);
3228 Stream.Emit((unsigned)'C', 8);
3229 Stream.Emit(0xC, 4);
3230 Stream.Emit(0xF, 4);
3231 Stream.Emit(0xE, 4);
3232 Stream.Emit(0x0, 4);
3233
3234 // Create serializer.
3235 llvm::Serializer S(Stream);
3236
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003237 // ===---------------------------------------------------===/
3238 // Serialize the "Translation Unit" metadata.
3239 // ===---------------------------------------------------===/
3240
3241 // Emit ASTContext.
3242 S.EnterBlock(ASTContextBlock);
3243 S.EmitOwnedPtr(this);
3244 S.ExitBlock(); // exit "ASTContextBlock"
3245
3246 S.EnterBlock(BasicMetadataBlock);
3247
3248 // Block for SourceManager and Target. Allows easy skipping
3249 // around to the block for the Selectors during deserialization.
3250 S.EnterBlock();
3251
3252 // Emit the SourceManager.
3253 S.Emit(getSourceManager());
3254
3255 // Emit the Target.
3256 S.EmitPtr(&Target);
3257 S.EmitCStr(Target.getTargetTriple());
3258
3259 S.ExitBlock(); // exit "SourceManager and Target Block"
3260
3261 // Emit the Selectors.
3262 S.Emit(Selectors);
3263
3264 // Emit the Identifier Table.
3265 S.Emit(Idents);
3266
3267 S.ExitBlock(); // exit "BasicMetadataBlock"
3268}
3269
3270
Ted Kremenek738e6c02007-10-31 17:10:13 +00003271/// Emit - Serialize an ASTContext object to Bitcode.
3272void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00003273 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00003274 S.EmitRef(SourceMgr);
3275 S.EmitRef(Target);
3276 S.EmitRef(Idents);
3277 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003278
Ted Kremenek68228a92007-10-31 22:44:07 +00003279 // Emit the size of the type vector so that we can reserve that size
3280 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003281 S.EmitInt(Types.size());
3282
Ted Kremenek034a78c2007-11-13 22:02:55 +00003283 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
3284 I!=E;++I)
3285 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00003286
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003287 S.EmitOwnedPtr(TUDecl);
3288
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003289 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00003290}
3291
Chris Lattnerf4fbc442009-03-28 04:27:18 +00003292
3293ASTContext *ASTContext::ReadASTBitcodeBuffer(llvm::MemoryBuffer &Buffer,
3294 FileManager &FMgr) {
3295 // Check if the file is of the proper length.
3296 if (Buffer.getBufferSize() & 0x3) {
3297 // FIXME: Provide diagnostic: "Length should be a multiple of 4 bytes."
3298 return 0;
3299 }
3300
3301 // Create the bitstream reader.
3302 unsigned char *BufPtr = (unsigned char *)Buffer.getBufferStart();
3303 llvm::BitstreamReader Stream(BufPtr, BufPtr+Buffer.getBufferSize());
3304
3305 if (Stream.Read(8) != 'B' ||
3306 Stream.Read(8) != 'C' ||
3307 Stream.Read(4) != 0xC ||
3308 Stream.Read(4) != 0xF ||
3309 Stream.Read(4) != 0xE ||
3310 Stream.Read(4) != 0x0) {
3311 // FIXME: Provide diagnostic.
3312 return NULL;
3313 }
3314
3315 // Create the deserializer.
3316 llvm::Deserializer Dezr(Stream);
3317
Chris Lattnerb09b31d2009-03-28 03:45:20 +00003318 // ===---------------------------------------------------===/
3319 // Deserialize the "Translation Unit" metadata.
3320 // ===---------------------------------------------------===/
3321
3322 // Skip to the BasicMetaDataBlock. First jump to ASTContextBlock
3323 // (which will appear earlier) and record its location.
3324
3325 bool FoundBlock = Dezr.SkipToBlock(ASTContextBlock);
3326 assert (FoundBlock);
3327
3328 llvm::Deserializer::Location ASTContextBlockLoc =
3329 Dezr.getCurrentBlockLocation();
3330
3331 FoundBlock = Dezr.SkipToBlock(BasicMetadataBlock);
3332 assert (FoundBlock);
3333
3334 // Read the SourceManager.
3335 SourceManager::CreateAndRegister(Dezr, FMgr);
3336
3337 { // Read the TargetInfo.
3338 llvm::SerializedPtrID PtrID = Dezr.ReadPtrID();
3339 char* triple = Dezr.ReadCStr(NULL,0,true);
3340 Dezr.RegisterPtr(PtrID, TargetInfo::CreateTargetInfo(std::string(triple)));
3341 delete [] triple;
3342 }
3343
3344 // For Selectors, we must read the identifier table first because the
3345 // SelectorTable depends on the identifiers being already deserialized.
3346 llvm::Deserializer::Location SelectorBlkLoc = Dezr.getCurrentBlockLocation();
3347 Dezr.SkipBlock();
3348
3349 // Read the identifier table.
3350 IdentifierTable::CreateAndRegister(Dezr);
3351
3352 // Now jump back and read the selectors.
3353 Dezr.JumpTo(SelectorBlkLoc);
3354 SelectorTable::CreateAndRegister(Dezr);
3355
3356 // Now jump back to ASTContextBlock and read the ASTContext.
3357 Dezr.JumpTo(ASTContextBlockLoc);
3358 return Dezr.ReadOwnedPtr<ASTContext>();
3359}
3360
Ted Kremenekacba3612007-11-13 00:25:37 +00003361ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00003362
3363 // Read the language options.
3364 LangOptions LOpts;
3365 LOpts.Read(D);
3366
Ted Kremenek68228a92007-10-31 22:44:07 +00003367 SourceManager &SM = D.ReadRef<SourceManager>();
3368 TargetInfo &t = D.ReadRef<TargetInfo>();
3369 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
3370 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00003371
Ted Kremenek68228a92007-10-31 22:44:07 +00003372 unsigned size_reserve = D.ReadInt();
3373
Douglas Gregor24afd4a2008-11-17 14:58:09 +00003374 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
3375 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00003376
Ted Kremenek034a78c2007-11-13 22:02:55 +00003377 for (unsigned i = 0; i < size_reserve; ++i)
3378 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00003379
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00003380 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
3381
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00003382 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00003383
3384 return A;
3385}
Douglas Gregorc34897d2009-04-09 22:27:44 +00003386
3387ExternalASTSource::~ExternalASTSource() { }
3388
3389void ExternalASTSource::PrintStats() { }