blob: fce2289574796f8a4006261b9e6b7da07b549fc2 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Douglas Gregoraaba5e32009-02-04 19:02:06 +000017#include "clang/AST/DeclTemplate.h"
Daniel Dunbare91593e2008-08-11 04:54:23 +000018#include "clang/AST/Expr.h"
19#include "clang/AST/RecordLayout.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000020#include "clang/Basic/TargetInfo.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000021#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000022#include "llvm/Bitcode/Serialize.h"
23#include "llvm/Bitcode/Deserialize.h"
Nate Begeman6fe7c8a2009-01-18 06:42:49 +000024#include "llvm/Support/MathExtras.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000025
Reid Spencer5f016e22007-07-11 17:01:13 +000026using namespace clang;
27
28enum FloatingRank {
29 FloatRank, DoubleRank, LongDoubleRank
30};
31
Chris Lattner61710852008-10-05 17:34:18 +000032ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
33 TargetInfo &t,
Daniel Dunbare91593e2008-08-11 04:54:23 +000034 IdentifierTable &idents, SelectorTable &sels,
Steve Naroffc0ac4922009-01-27 23:20:32 +000035 bool FreeMem, unsigned size_reserve) :
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +000036 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
Steve Naroffc0ac4922009-01-27 23:20:32 +000037 SourceMgr(SM), LangOpts(LOpts), FreeMemory(FreeMem), Target(t),
Douglas Gregor2e1cd422008-11-17 14:58:09 +000038 Idents(idents), Selectors(sels)
Daniel Dunbare91593e2008-08-11 04:54:23 +000039{
40 if (size_reserve > 0) Types.reserve(size_reserve);
41 InitBuiltinTypes();
Douglas Gregor3573c0c2009-02-14 20:49:29 +000042 BuiltinInfo.InitializeBuiltins(idents, Target, LangOpts.Freestanding);
Daniel Dunbare91593e2008-08-11 04:54:23 +000043 TUDecl = TranslationUnitDecl::Create(*this);
44}
45
Reid Spencer5f016e22007-07-11 17:01:13 +000046ASTContext::~ASTContext() {
47 // Deallocate all the types.
48 while (!Types.empty()) {
Ted Kremenek4b05b1d2008-05-21 16:38:54 +000049 Types.back()->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000050 Types.pop_back();
51 }
Eli Friedmanb26153c2008-05-27 03:08:09 +000052
Nuno Lopesb74668e2008-12-17 22:30:25 +000053 {
54 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
55 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
56 while (I != E) {
57 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
58 delete R;
59 }
60 }
61
62 {
63 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
64 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
65 while (I != E) {
66 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
67 delete R;
68 }
69 }
70
71 {
72 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
73 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
74 while (I != E) {
75 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
76 R->Destroy(*this);
77 }
78 }
79
Eli Friedmanb26153c2008-05-27 03:08:09 +000080 TUDecl->Destroy(*this);
Reid Spencer5f016e22007-07-11 17:01:13 +000081}
82
83void ASTContext::PrintStats() const {
84 fprintf(stderr, "*** AST Context Stats:\n");
85 fprintf(stderr, " %d types total.\n", (int)Types.size());
86 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar248e1c02008-09-26 03:23:00 +000087 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000088 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Sebastian Redlf30208a2009-01-24 21:16:55 +000089 unsigned NumMemberPointer = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000090
91 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +000092 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
93 unsigned NumObjCQualifiedIds = 0;
Steve Naroff6cc18962008-05-21 15:59:22 +000094 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000095
96 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
97 Type *T = Types[i];
98 if (isa<BuiltinType>(T))
99 ++NumBuiltin;
100 else if (isa<PointerType>(T))
101 ++NumPointer;
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000102 else if (isa<BlockPointerType>(T))
103 ++NumBlockPointer;
Reid Spencer5f016e22007-07-11 17:01:13 +0000104 else if (isa<ReferenceType>(T))
105 ++NumReference;
Sebastian Redlf30208a2009-01-24 21:16:55 +0000106 else if (isa<MemberPointerType>(T))
107 ++NumMemberPointer;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000108 else if (isa<ComplexType>(T))
109 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 else if (isa<ArrayType>(T))
111 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +0000112 else if (isa<VectorType>(T))
113 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +0000114 else if (isa<FunctionTypeNoProto>(T))
115 ++NumFunctionNP;
116 else if (isa<FunctionTypeProto>(T))
117 ++NumFunctionP;
118 else if (isa<TypedefType>(T))
119 ++NumTypeName;
120 else if (TagType *TT = dyn_cast<TagType>(T)) {
121 ++NumTagged;
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000122 switch (TT->getDecl()->getTagKind()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000123 default: assert(0 && "Unknown tagged type!");
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000124 case TagDecl::TK_struct: ++NumTagStruct; break;
125 case TagDecl::TK_union: ++NumTagUnion; break;
126 case TagDecl::TK_class: ++NumTagClass; break;
127 case TagDecl::TK_enum: ++NumTagEnum; break;
Reid Spencer5f016e22007-07-11 17:01:13 +0000128 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000129 } else if (isa<ObjCInterfaceType>(T))
130 ++NumObjCInterfaces;
131 else if (isa<ObjCQualifiedInterfaceType>(T))
132 ++NumObjCQualifiedInterfaces;
133 else if (isa<ObjCQualifiedIdType>(T))
134 ++NumObjCQualifiedIds;
Steve Naroff6cc18962008-05-21 15:59:22 +0000135 else if (isa<TypeOfType>(T))
136 ++NumTypeOfTypes;
137 else if (isa<TypeOfExpr>(T))
138 ++NumTypeOfExprs;
Steve Naroff3f128ad2007-09-17 14:16:13 +0000139 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +0000140 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 assert(0 && "Unknown type!");
142 }
143 }
144
145 fprintf(stderr, " %d builtin types\n", NumBuiltin);
146 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar248e1c02008-09-26 03:23:00 +0000147 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Reid Spencer5f016e22007-07-11 17:01:13 +0000148 fprintf(stderr, " %d reference types\n", NumReference);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000149 fprintf(stderr, " %d member pointer types\n", NumMemberPointer);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000150 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +0000151 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +0000152 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
154 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
155 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
156 fprintf(stderr, " %d tagged types\n", NumTagged);
157 fprintf(stderr, " %d struct types\n", NumTagStruct);
158 fprintf(stderr, " %d union types\n", NumTagUnion);
159 fprintf(stderr, " %d class types\n", NumTagClass);
160 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000161 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000162 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000163 NumObjCQualifiedInterfaces);
Fariborz Jahanianc5692492007-12-17 21:03:50 +0000164 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000165 NumObjCQualifiedIds);
Steve Naroff6cc18962008-05-21 15:59:22 +0000166 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
167 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
168
Reid Spencer5f016e22007-07-11 17:01:13 +0000169 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
170 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000171 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Sebastian Redlf30208a2009-01-24 21:16:55 +0000172 NumMemberPointer*sizeof(MemberPointerType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 NumFunctionP*sizeof(FunctionTypeProto)+
174 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroff6cc18962008-05-21 15:59:22 +0000175 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
176 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Reid Spencer5f016e22007-07-11 17:01:13 +0000177}
178
179
180void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Narofff83820b2009-01-27 22:08:43 +0000181 Types.push_back((R = QualType(new (*this,8) BuiltinType(K),0)).getTypePtr());
Reid Spencer5f016e22007-07-11 17:01:13 +0000182}
183
Reid Spencer5f016e22007-07-11 17:01:13 +0000184void ASTContext::InitBuiltinTypes() {
185 assert(VoidTy.isNull() && "Context reinitialized?");
186
187 // C99 6.2.5p19.
188 InitBuiltinType(VoidTy, BuiltinType::Void);
189
190 // C99 6.2.5p2.
191 InitBuiltinType(BoolTy, BuiltinType::Bool);
192 // C99 6.2.5p3.
Chris Lattner98be4942008-03-05 18:54:05 +0000193 if (Target.isCharSigned())
Reid Spencer5f016e22007-07-11 17:01:13 +0000194 InitBuiltinType(CharTy, BuiltinType::Char_S);
195 else
196 InitBuiltinType(CharTy, BuiltinType::Char_U);
197 // C99 6.2.5p4.
198 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
199 InitBuiltinType(ShortTy, BuiltinType::Short);
200 InitBuiltinType(IntTy, BuiltinType::Int);
201 InitBuiltinType(LongTy, BuiltinType::Long);
202 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
203
204 // C99 6.2.5p6.
205 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
206 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
207 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
208 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
209 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
210
211 // C99 6.2.5p10.
212 InitBuiltinType(FloatTy, BuiltinType::Float);
213 InitBuiltinType(DoubleTy, BuiltinType::Double);
214 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000215
216 // C++ 3.9.1p5
217 InitBuiltinType(WCharTy, BuiltinType::WChar);
218
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000219 // Placeholder type for functions.
Douglas Gregor898574e2008-12-05 23:32:09 +0000220 InitBuiltinType(OverloadTy, BuiltinType::Overload);
221
222 // Placeholder type for type-dependent expressions whose type is
223 // completely unknown. No code should ever check a type against
224 // DependentTy and users should never see it; however, it is here to
225 // help diagnose failures to properly check for type-dependent
226 // expressions.
227 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000228
Reid Spencer5f016e22007-07-11 17:01:13 +0000229 // C99 6.2.5p11.
230 FloatComplexTy = getComplexType(FloatTy);
231 DoubleComplexTy = getComplexType(DoubleTy);
232 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregor8e9bebd2008-10-21 16:13:35 +0000233
Steve Naroff7e219e42007-10-15 14:41:52 +0000234 BuiltinVaListType = QualType();
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000235 ObjCIdType = QualType();
Steve Naroff7e219e42007-10-15 14:41:52 +0000236 IdStructType = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000237 ObjCClassType = QualType();
Anders Carlsson8baaca52007-10-31 02:53:19 +0000238 ClassStructType = 0;
239
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000240 ObjCConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000241
242 // void * type
243 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000244}
245
Chris Lattner464175b2007-07-18 17:52:12 +0000246//===----------------------------------------------------------------------===//
247// Type Sizing and Analysis
248//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000249
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000250/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
251/// scalar floating point type.
252const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
253 const BuiltinType *BT = T->getAsBuiltinType();
254 assert(BT && "Not a floating point type!");
255 switch (BT->getKind()) {
256 default: assert(0 && "Not a floating point type!");
257 case BuiltinType::Float: return Target.getFloatFormat();
258 case BuiltinType::Double: return Target.getDoubleFormat();
259 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
260 }
261}
262
Chris Lattneraf707ab2009-01-24 21:53:27 +0000263/// getDeclAlign - Return a conservative estimate of the alignment of the
264/// specified decl. Note that bitfields do not have a valid alignment, so
265/// this method will assert on them.
Daniel Dunbarb7d08442009-02-17 22:16:19 +0000266unsigned ASTContext::getDeclAlignInBytes(const Decl *D) {
Eli Friedmandcdafb62009-02-22 02:56:25 +0000267 unsigned Align = Target.getCharWidth();
268
269 if (const AlignedAttr* AA = D->getAttr<AlignedAttr>())
270 Align = std::max(Align, AA->getAlignment());
271
Chris Lattneraf707ab2009-01-24 21:53:27 +0000272 if (const ValueDecl *VD = dyn_cast<ValueDecl>(D)) {
273 QualType T = VD->getType();
274 // Incomplete or function types default to 1.
Eli Friedmandcdafb62009-02-22 02:56:25 +0000275 if (!T->isIncompleteType() && !T->isFunctionType()) {
276 while (isa<VariableArrayType>(T) || isa<IncompleteArrayType>(T))
277 T = cast<ArrayType>(T)->getElementType();
278
279 Align = std::max(Align, getPreferredTypeAlign(T.getTypePtr()));
280 }
Chris Lattneraf707ab2009-01-24 21:53:27 +0000281 }
Eli Friedmandcdafb62009-02-22 02:56:25 +0000282
283 return Align / Target.getCharWidth();
Chris Lattneraf707ab2009-01-24 21:53:27 +0000284}
Chris Lattnerb7cfe882008-06-30 18:32:54 +0000285
Chris Lattnera7674d82007-07-13 22:13:22 +0000286/// getTypeSize - Return the size of the specified type, in bits. This method
287/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000288std::pair<uint64_t, unsigned>
Daniel Dunbar1d751182008-11-08 05:48:37 +0000289ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000290 T = getCanonicalType(T);
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000291 uint64_t Width;
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000292 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000293 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000294 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000295 case Type::FunctionNoProto:
296 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000297 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000298 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000299 case Type::VariableArray:
300 assert(0 && "VLAs not implemented yet!");
Douglas Gregor898574e2008-12-05 23:32:09 +0000301 case Type::DependentSizedArray:
302 assert(0 && "Dependently-sized arrays don't have a known size");
Steve Narofffb22d962007-08-30 01:06:46 +0000303 case Type::ConstantArray: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000304 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Narofffb22d962007-08-30 01:06:46 +0000305
Chris Lattner98be4942008-03-05 18:54:05 +0000306 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000307 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000308 Align = EltInfo.second;
309 break;
Christopher Lamb5c09a022007-12-29 05:10:55 +0000310 }
Nate Begeman213541a2008-04-18 23:10:10 +0000311 case Type::ExtVector:
Chris Lattner030d8842007-07-19 22:06:24 +0000312 case Type::Vector: {
313 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000314 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000315 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman4bd998b2008-05-30 09:31:38 +0000316 Align = Width;
Nate Begeman6fe7c8a2009-01-18 06:42:49 +0000317 // If the alignment is not a power of 2, round up to the next power of 2.
318 // This happens for non-power-of-2 length vectors.
319 // FIXME: this should probably be a target property.
320 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner030d8842007-07-19 22:06:24 +0000321 break;
322 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000323
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000324 case Type::Builtin:
Chris Lattnera7674d82007-07-13 22:13:22 +0000325 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000326 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000327 case BuiltinType::Void:
328 assert(0 && "Incomplete types have no size!");
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000329 case BuiltinType::Bool:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000330 Width = Target.getBoolWidth();
331 Align = Target.getBoolAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000332 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000333 case BuiltinType::Char_S:
334 case BuiltinType::Char_U:
335 case BuiltinType::UChar:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000336 case BuiltinType::SChar:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000337 Width = Target.getCharWidth();
338 Align = Target.getCharAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000339 break;
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +0000340 case BuiltinType::WChar:
341 Width = Target.getWCharWidth();
342 Align = Target.getWCharAlign();
343 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000344 case BuiltinType::UShort:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000345 case BuiltinType::Short:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000346 Width = Target.getShortWidth();
347 Align = Target.getShortAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000348 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000349 case BuiltinType::UInt:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000350 case BuiltinType::Int:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000351 Width = Target.getIntWidth();
352 Align = Target.getIntAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000353 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000354 case BuiltinType::ULong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000355 case BuiltinType::Long:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000356 Width = Target.getLongWidth();
357 Align = Target.getLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000358 break;
Chris Lattner692233e2007-07-13 22:27:08 +0000359 case BuiltinType::ULongLong:
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000360 case BuiltinType::LongLong:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000361 Width = Target.getLongLongWidth();
362 Align = Target.getLongLongAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000363 break;
364 case BuiltinType::Float:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000365 Width = Target.getFloatWidth();
366 Align = Target.getFloatAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000367 break;
368 case BuiltinType::Double:
Chris Lattner5426bf62008-04-07 07:01:58 +0000369 Width = Target.getDoubleWidth();
370 Align = Target.getDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000371 break;
372 case BuiltinType::LongDouble:
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000373 Width = Target.getLongDoubleWidth();
374 Align = Target.getLongDoubleAlign();
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000375 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000376 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000377 break;
Eli Friedmanf98aba32009-02-13 02:31:07 +0000378 case Type::FixedWidthInt:
379 // FIXME: This isn't precisely correct; the width/alignment should depend
380 // on the available types for the target
381 Width = cast<FixedWidthIntType>(T)->getWidth();
Chris Lattner736166b2009-02-15 21:20:13 +0000382 Width = std::max(llvm::NextPowerOf2(Width - 1), (uint64_t)8);
Eli Friedmanf98aba32009-02-13 02:31:07 +0000383 Align = Width;
384 break;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000385 case Type::ExtQual:
Chris Lattner98be4942008-03-05 18:54:05 +0000386 // FIXME: Pointers into different addr spaces could have different sizes and
387 // alignment requirements: getPointerInfo should take an AddrSpace.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000388 return getTypeInfo(QualType(cast<ExtQualType>(T)->getBaseType(), 0));
Ted Kremeneka526c5c2008-01-07 19:49:32 +0000389 case Type::ObjCQualifiedId:
Chris Lattner5426bf62008-04-07 07:01:58 +0000390 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000391 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000392 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000393 case Type::BlockPointer: {
394 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
395 Width = Target.getPointerWidth(AS);
396 Align = Target.getPointerAlign(AS);
397 break;
398 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000399 case Type::Pointer: {
400 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000401 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000402 Align = Target.getPointerAlign(AS);
403 break;
404 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000405 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000406 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000407 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000408 // FIXME: This is wrong for struct layout: a reference in a struct has
409 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000410 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000411 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000412 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000413 // the GCC ABI, where pointers to data are one pointer large, pointers to
414 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000415 // other compilers too, we need to delegate this completely to TargetInfo
416 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000417 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
418 unsigned AS = Pointee.getAddressSpace();
419 Width = Target.getPointerWidth(AS);
420 if (Pointee->isFunctionType())
421 Width *= 2;
422 Align = Target.getPointerAlign(AS);
423 // GCC aligns at single pointer width.
424 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000425 case Type::Complex: {
426 // Complex types have the same alignment as their elements, but twice the
427 // size.
428 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000429 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000430 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000431 Align = EltInfo.second;
432 break;
433 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000434 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000435 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000436 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
437 Width = Layout.getSize();
438 Align = Layout.getAlignment();
439 break;
440 }
Chris Lattner71763312008-04-06 22:05:18 +0000441 case Type::Tagged: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000442 const TagType *TT = cast<TagType>(T);
443
444 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000445 Width = 1;
446 Align = 1;
447 break;
448 }
449
Daniel Dunbar1d751182008-11-08 05:48:37 +0000450 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000451 return getTypeInfo(ET->getDecl()->getIntegerType());
452
Daniel Dunbar1d751182008-11-08 05:48:37 +0000453 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000454 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
455 Width = Layout.getSize();
456 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000457 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000458 }
Chris Lattner71763312008-04-06 22:05:18 +0000459 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000460
Chris Lattner464175b2007-07-18 17:52:12 +0000461 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000462 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000463}
464
Chris Lattner34ebde42009-01-27 18:08:34 +0000465/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
466/// type for the current target in bits. This can be different than the ABI
467/// alignment in cases where it is beneficial for performance to overalign
468/// a data type.
469unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
470 unsigned ABIAlign = getTypeAlign(T);
471
472 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000473 if (T->isSpecificBuiltinType(BuiltinType::Double))
474 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000475
476 return ABIAlign;
477}
478
479
Devang Patel8b277042008-06-04 21:22:16 +0000480/// LayoutField - Field layout.
481void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000482 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000483 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000484 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000485 uint64_t FieldOffset = IsUnion ? 0 : Size;
486 uint64_t FieldSize;
487 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000488
489 // FIXME: Should this override struct packing? Probably we want to
490 // take the minimum?
491 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
492 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000493
494 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
495 // TODO: Need to check this algorithm on other targets!
496 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000497 FieldSize =
498 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000499
500 std::pair<uint64_t, unsigned> FieldInfo =
501 Context.getTypeInfo(FD->getType());
502 uint64_t TypeSize = FieldInfo.first;
503
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000504 // Determine the alignment of this bitfield. The packing
505 // attributes define a maximum and the alignment attribute defines
506 // a minimum.
507 // FIXME: What is the right behavior when the specified alignment
508 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000509 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000510 if (FieldPacking)
511 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000512 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
513 FieldAlign = std::max(FieldAlign, AA->getAlignment());
514
515 // Check if we need to add padding to give the field the correct
516 // alignment.
517 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
518 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
519
520 // Padding members don't affect overall alignment
521 if (!FD->getIdentifier())
522 FieldAlign = 1;
523 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000524 if (FD->getType()->isIncompleteArrayType()) {
525 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000526 // query getTypeInfo about these, so we figure it out here.
527 // Flexible array members don't have any size, but they
528 // have to be aligned appropriately for their element type.
529 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000530 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000531 FieldAlign = Context.getTypeAlign(ATy->getElementType());
532 } else {
533 std::pair<uint64_t, unsigned> FieldInfo =
534 Context.getTypeInfo(FD->getType());
535 FieldSize = FieldInfo.first;
536 FieldAlign = FieldInfo.second;
537 }
538
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000539 // Determine the alignment of this bitfield. The packing
540 // attributes define a maximum and the alignment attribute defines
541 // a minimum. Additionally, the packing alignment must be at least
542 // a byte for non-bitfields.
543 //
544 // FIXME: What is the right behavior when the specified alignment
545 // is smaller than the specified packing?
546 if (FieldPacking)
547 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000548 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
549 FieldAlign = std::max(FieldAlign, AA->getAlignment());
550
551 // Round up the current record size to the field's alignment boundary.
552 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
553 }
554
555 // Place this field at the current location.
556 FieldOffsets[FieldNo] = FieldOffset;
557
558 // Reserve space for this field.
559 if (IsUnion) {
560 Size = std::max(Size, FieldSize);
561 } else {
562 Size = FieldOffset + FieldSize;
563 }
564
565 // Remember max struct/class alignment.
566 Alignment = std::max(Alignment, FieldAlign);
567}
568
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000569static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
570 std::vector<FieldDecl*> &Fields) {
571 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
572 if (SuperClass)
573 CollectObjCIvars(SuperClass, Fields);
574 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
575 E = OI->ivar_end(); I != E; ++I) {
576 ObjCIvarDecl *IVDecl = (*I);
577 if (!IVDecl->isInvalidDecl())
578 Fields.push_back(cast<FieldDecl>(IVDecl));
579 }
580}
581
582/// addRecordToClass - produces record info. for the class for its
583/// ivars and all those inherited.
584///
585const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
586{
587 const RecordDecl *&RD = ASTRecordForInterface[D];
588 if (RD)
589 return RD;
590 std::vector<FieldDecl*> RecFields;
591 CollectObjCIvars(D, RecFields);
592 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
593 D->getLocation(),
594 D->getIdentifier());
595 /// FIXME! Can do collection of ivars and adding to the record while
596 /// doing it.
597 for (unsigned int i = 0; i != RecFields.size(); i++) {
598 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
599 RecFields[i]->getLocation(),
600 RecFields[i]->getIdentifier(),
601 RecFields[i]->getType(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000602 RecFields[i]->getBitWidth(), false);
Douglas Gregor482b77d2009-01-12 23:27:07 +0000603 NewRD->addDecl(Field);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000604 }
605 NewRD->completeDefinition(*this);
606 RD = NewRD;
607 return RD;
608}
Devang Patel44a3dde2008-06-04 21:54:36 +0000609
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000610/// setFieldDecl - maps a field for the given Ivar reference node.
611//
612void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
613 const ObjCIvarDecl *Ivar,
614 const ObjCIvarRefExpr *MRef) {
615 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
616 lookupFieldDeclForIvar(*this, Ivar);
617 ASTFieldForIvarRef[MRef] = FD;
618}
619
Chris Lattner61710852008-10-05 17:34:18 +0000620/// getASTObjcInterfaceLayout - Get or compute information about the layout of
621/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000622/// position information.
623const ASTRecordLayout &
624ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
625 // Look up this layout, if already laid out, return what we have.
626 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
627 if (Entry) return *Entry;
628
629 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
630 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000631 ASTRecordLayout *NewEntry = NULL;
632 unsigned FieldCount = D->ivar_size();
633 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
634 FieldCount++;
635 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
636 unsigned Alignment = SL.getAlignment();
637 uint64_t Size = SL.getSize();
638 NewEntry = new ASTRecordLayout(Size, Alignment);
639 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000640 // Super class is at the beginning of the layout.
641 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000642 } else {
643 NewEntry = new ASTRecordLayout();
644 NewEntry->InitializeLayout(FieldCount);
645 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000646 Entry = NewEntry;
647
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000648 unsigned StructPacking = 0;
649 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
650 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000651
652 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
653 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
654 AA->getAlignment()));
655
656 // Layout each ivar sequentially.
657 unsigned i = 0;
658 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
659 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
660 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000661 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000662 }
663
664 // Finally, round the size of the total struct up to the alignment of the
665 // struct itself.
666 NewEntry->FinalizeLayout();
667 return *NewEntry;
668}
669
Devang Patel88a981b2007-11-01 19:11:01 +0000670/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000671/// specified record (struct/union/class), which indicates its size and field
672/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000673const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000674 D = D->getDefinition(*this);
675 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000676
Chris Lattner464175b2007-07-18 17:52:12 +0000677 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000678 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000679 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000680
Devang Patel88a981b2007-11-01 19:11:01 +0000681 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
682 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
683 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000684 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000685
Douglas Gregore267ff32008-12-11 20:41:00 +0000686 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000687 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000688 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000689
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000690 unsigned StructPacking = 0;
691 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
692 StructPacking = PA->getAlignment();
693
Eli Friedman4bd998b2008-05-30 09:31:38 +0000694 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000695 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
696 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000697
Eli Friedman4bd998b2008-05-30 09:31:38 +0000698 // Layout each field, for now, just sequentially, respecting alignment. In
699 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000700 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000701 for (RecordDecl::field_iterator Field = D->field_begin(),
702 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000703 Field != FieldEnd; (void)++Field, ++FieldIdx)
704 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000705
706 // Finally, round the size of the total struct up to the alignment of the
707 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000708 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000709 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000710}
711
Chris Lattnera7674d82007-07-13 22:13:22 +0000712//===----------------------------------------------------------------------===//
713// Type creation/memoization methods
714//===----------------------------------------------------------------------===//
715
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000716QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000717 QualType CanT = getCanonicalType(T);
718 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000719 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000720
721 // If we are composing extended qualifiers together, merge together into one
722 // ExtQualType node.
723 unsigned CVRQuals = T.getCVRQualifiers();
724 QualType::GCAttrTypes GCAttr = QualType::GCNone;
725 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000726
Chris Lattnerb7d25532009-02-18 22:53:11 +0000727 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
728 // If this type already has an address space specified, it cannot get
729 // another one.
730 assert(EQT->getAddressSpace() == 0 &&
731 "Type cannot be in multiple addr spaces!");
732 GCAttr = EQT->getObjCGCAttr();
733 TypeNode = EQT->getBaseType();
734 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000735
Chris Lattnerb7d25532009-02-18 22:53:11 +0000736 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000737 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000738 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000739 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000740 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000741 return QualType(EXTQy, CVRQuals);
742
Christopher Lambebb97e92008-02-04 02:31:56 +0000743 // If the base type isn't canonical, this won't be a canonical type either,
744 // so fill in the canonical type field.
745 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000746 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000747 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000748
Chris Lattnerb7d25532009-02-18 22:53:11 +0000749 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000750 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000751 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000752 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000753 ExtQualType *New =
754 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000755 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000756 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000757 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000758}
759
Chris Lattnerb7d25532009-02-18 22:53:11 +0000760QualType ASTContext::getObjCGCQualType(QualType T,
761 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000762 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000763 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000764 return T;
765
Chris Lattnerb7d25532009-02-18 22:53:11 +0000766 // If we are composing extended qualifiers together, merge together into one
767 // ExtQualType node.
768 unsigned CVRQuals = T.getCVRQualifiers();
769 Type *TypeNode = T.getTypePtr();
770 unsigned AddressSpace = 0;
771
772 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
773 // If this type already has an address space specified, it cannot get
774 // another one.
775 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
776 "Type cannot be in multiple addr spaces!");
777 AddressSpace = EQT->getAddressSpace();
778 TypeNode = EQT->getBaseType();
779 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000780
781 // Check if we've already instantiated an gc qual'd type of this type.
782 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000783 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000784 void *InsertPos = 0;
785 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000786 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000787
788 // If the base type isn't canonical, this won't be a canonical type either,
789 // so fill in the canonical type field.
790 QualType Canonical;
791 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000792 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000793
Chris Lattnerb7d25532009-02-18 22:53:11 +0000794 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000795 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
796 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
797 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000798 ExtQualType *New =
799 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000800 ExtQualTypes.InsertNode(New, InsertPos);
801 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000802 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000803}
Chris Lattnera7674d82007-07-13 22:13:22 +0000804
Reid Spencer5f016e22007-07-11 17:01:13 +0000805/// getComplexType - Return the uniqued reference to the type for a complex
806/// number with the specified element type.
807QualType ASTContext::getComplexType(QualType T) {
808 // Unique pointers, to guarantee there is only one pointer of a particular
809 // structure.
810 llvm::FoldingSetNodeID ID;
811 ComplexType::Profile(ID, T);
812
813 void *InsertPos = 0;
814 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
815 return QualType(CT, 0);
816
817 // If the pointee type isn't canonical, this won't be a canonical type either,
818 // so fill in the canonical type field.
819 QualType Canonical;
820 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000821 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000822
823 // Get the new insert position for the node we care about.
824 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000825 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000826 }
Steve Narofff83820b2009-01-27 22:08:43 +0000827 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000828 Types.push_back(New);
829 ComplexTypes.InsertNode(New, InsertPos);
830 return QualType(New, 0);
831}
832
Eli Friedmanf98aba32009-02-13 02:31:07 +0000833QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
834 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
835 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
836 FixedWidthIntType *&Entry = Map[Width];
837 if (!Entry)
838 Entry = new FixedWidthIntType(Width, Signed);
839 return QualType(Entry, 0);
840}
Reid Spencer5f016e22007-07-11 17:01:13 +0000841
842/// getPointerType - Return the uniqued reference to the type for a pointer to
843/// the specified type.
844QualType ASTContext::getPointerType(QualType T) {
845 // Unique pointers, to guarantee there is only one pointer of a particular
846 // structure.
847 llvm::FoldingSetNodeID ID;
848 PointerType::Profile(ID, T);
849
850 void *InsertPos = 0;
851 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
852 return QualType(PT, 0);
853
854 // If the pointee type isn't canonical, this won't be a canonical type either,
855 // so fill in the canonical type field.
856 QualType Canonical;
857 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000858 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000859
860 // Get the new insert position for the node we care about.
861 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000862 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000863 }
Steve Narofff83820b2009-01-27 22:08:43 +0000864 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000865 Types.push_back(New);
866 PointerTypes.InsertNode(New, InsertPos);
867 return QualType(New, 0);
868}
869
Steve Naroff5618bd42008-08-27 16:04:49 +0000870/// getBlockPointerType - Return the uniqued reference to the type for
871/// a pointer to the specified block.
872QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000873 assert(T->isFunctionType() && "block of function types only");
874 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000875 // structure.
876 llvm::FoldingSetNodeID ID;
877 BlockPointerType::Profile(ID, T);
878
879 void *InsertPos = 0;
880 if (BlockPointerType *PT =
881 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
882 return QualType(PT, 0);
883
Steve Naroff296e8d52008-08-28 19:20:44 +0000884 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000885 // type either so fill in the canonical type field.
886 QualType Canonical;
887 if (!T->isCanonical()) {
888 Canonical = getBlockPointerType(getCanonicalType(T));
889
890 // Get the new insert position for the node we care about.
891 BlockPointerType *NewIP =
892 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000893 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000894 }
Steve Narofff83820b2009-01-27 22:08:43 +0000895 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000896 Types.push_back(New);
897 BlockPointerTypes.InsertNode(New, InsertPos);
898 return QualType(New, 0);
899}
900
Reid Spencer5f016e22007-07-11 17:01:13 +0000901/// getReferenceType - Return the uniqued reference to the type for a reference
902/// to the specified type.
903QualType ASTContext::getReferenceType(QualType T) {
904 // Unique pointers, to guarantee there is only one pointer of a particular
905 // structure.
906 llvm::FoldingSetNodeID ID;
907 ReferenceType::Profile(ID, T);
908
909 void *InsertPos = 0;
910 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
911 return QualType(RT, 0);
912
913 // If the referencee type isn't canonical, this won't be a canonical type
914 // either, so fill in the canonical type field.
915 QualType Canonical;
916 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000917 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000918
919 // Get the new insert position for the node we care about.
920 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000921 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000922 }
923
Steve Narofff83820b2009-01-27 22:08:43 +0000924 ReferenceType *New = new (*this,8) ReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000925 Types.push_back(New);
926 ReferenceTypes.InsertNode(New, InsertPos);
927 return QualType(New, 0);
928}
929
Sebastian Redlf30208a2009-01-24 21:16:55 +0000930/// getMemberPointerType - Return the uniqued reference to the type for a
931/// member pointer to the specified type, in the specified class.
932QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
933{
934 // Unique pointers, to guarantee there is only one pointer of a particular
935 // structure.
936 llvm::FoldingSetNodeID ID;
937 MemberPointerType::Profile(ID, T, Cls);
938
939 void *InsertPos = 0;
940 if (MemberPointerType *PT =
941 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
942 return QualType(PT, 0);
943
944 // If the pointee or class type isn't canonical, this won't be a canonical
945 // type either, so fill in the canonical type field.
946 QualType Canonical;
947 if (!T->isCanonical()) {
948 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
949
950 // Get the new insert position for the node we care about.
951 MemberPointerType *NewIP =
952 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
953 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
954 }
Steve Narofff83820b2009-01-27 22:08:43 +0000955 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000956 Types.push_back(New);
957 MemberPointerTypes.InsertNode(New, InsertPos);
958 return QualType(New, 0);
959}
960
Steve Narofffb22d962007-08-30 01:06:46 +0000961/// getConstantArrayType - Return the unique reference to the type for an
962/// array of the specified element type.
963QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000964 const llvm::APInt &ArySize,
965 ArrayType::ArraySizeModifier ASM,
966 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000967 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +0000968 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000969
970 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000971 if (ConstantArrayType *ATP =
972 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000973 return QualType(ATP, 0);
974
975 // If the element type isn't canonical, this won't be a canonical type either,
976 // so fill in the canonical type field.
977 QualType Canonical;
978 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000979 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000980 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000981 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000982 ConstantArrayType *NewIP =
983 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000984 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000985 }
986
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000987 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +0000988 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000989 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 Types.push_back(New);
991 return QualType(New, 0);
992}
993
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000994/// getVariableArrayType - Returns a non-unique reference to the type for a
995/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000996QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
997 ArrayType::ArraySizeModifier ASM,
998 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +0000999 // Since we don't unique expressions, it isn't possible to unique VLA's
1000 // that have an expression provided for their size.
1001
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001002 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001003 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001004
1005 VariableArrayTypes.push_back(New);
1006 Types.push_back(New);
1007 return QualType(New, 0);
1008}
1009
Douglas Gregor898574e2008-12-05 23:32:09 +00001010/// getDependentSizedArrayType - Returns a non-unique reference to
1011/// the type for a dependently-sized array of the specified element
1012/// type. FIXME: We will need these to be uniqued, or at least
1013/// comparable, at some point.
1014QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1015 ArrayType::ArraySizeModifier ASM,
1016 unsigned EltTypeQuals) {
1017 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1018 "Size must be type- or value-dependent!");
1019
1020 // Since we don't unique expressions, it isn't possible to unique
1021 // dependently-sized array types.
1022
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001023 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001024 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1025 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001026
1027 DependentSizedArrayTypes.push_back(New);
1028 Types.push_back(New);
1029 return QualType(New, 0);
1030}
1031
Eli Friedmanc5773c42008-02-15 18:16:39 +00001032QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1033 ArrayType::ArraySizeModifier ASM,
1034 unsigned EltTypeQuals) {
1035 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001036 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001037
1038 void *InsertPos = 0;
1039 if (IncompleteArrayType *ATP =
1040 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1041 return QualType(ATP, 0);
1042
1043 // If the element type isn't canonical, this won't be a canonical type
1044 // either, so fill in the canonical type field.
1045 QualType Canonical;
1046
1047 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001048 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001049 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001050
1051 // Get the new insert position for the node we care about.
1052 IncompleteArrayType *NewIP =
1053 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001054 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001055 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001056
Steve Narofff83820b2009-01-27 22:08:43 +00001057 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001058 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001059
1060 IncompleteArrayTypes.InsertNode(New, InsertPos);
1061 Types.push_back(New);
1062 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001063}
1064
Steve Naroff73322922007-07-18 18:00:27 +00001065/// getVectorType - Return the unique reference to a vector type of
1066/// the specified element type and size. VectorType must be a built-in type.
1067QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001068 BuiltinType *baseType;
1069
Chris Lattnerf52ab252008-04-06 22:59:24 +00001070 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001071 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001072
1073 // Check if we've already instantiated a vector of this type.
1074 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001075 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 void *InsertPos = 0;
1077 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1078 return QualType(VTP, 0);
1079
1080 // If the element type isn't canonical, this won't be a canonical type either,
1081 // so fill in the canonical type field.
1082 QualType Canonical;
1083 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001084 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001085
1086 // Get the new insert position for the node we care about.
1087 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001088 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001089 }
Steve Narofff83820b2009-01-27 22:08:43 +00001090 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001091 VectorTypes.InsertNode(New, InsertPos);
1092 Types.push_back(New);
1093 return QualType(New, 0);
1094}
1095
Nate Begeman213541a2008-04-18 23:10:10 +00001096/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001097/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001098QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001099 BuiltinType *baseType;
1100
Chris Lattnerf52ab252008-04-06 22:59:24 +00001101 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001102 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001103
1104 // Check if we've already instantiated a vector of this type.
1105 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001106 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001107 void *InsertPos = 0;
1108 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1109 return QualType(VTP, 0);
1110
1111 // If the element type isn't canonical, this won't be a canonical type either,
1112 // so fill in the canonical type field.
1113 QualType Canonical;
1114 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001115 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001116
1117 // Get the new insert position for the node we care about.
1118 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001119 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001120 }
Steve Narofff83820b2009-01-27 22:08:43 +00001121 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001122 VectorTypes.InsertNode(New, InsertPos);
1123 Types.push_back(New);
1124 return QualType(New, 0);
1125}
1126
Reid Spencer5f016e22007-07-11 17:01:13 +00001127/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
1128///
1129QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
1130 // Unique functions, to guarantee there is only one function of a particular
1131 // structure.
1132 llvm::FoldingSetNodeID ID;
1133 FunctionTypeNoProto::Profile(ID, ResultTy);
1134
1135 void *InsertPos = 0;
1136 if (FunctionTypeNoProto *FT =
1137 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
1138 return QualType(FT, 0);
1139
1140 QualType Canonical;
1141 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001142 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001143
1144 // Get the new insert position for the node we care about.
1145 FunctionTypeNoProto *NewIP =
1146 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001147 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 }
1149
Steve Narofff83820b2009-01-27 22:08:43 +00001150 FunctionTypeNoProto *New =new(*this,8)FunctionTypeNoProto(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001151 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +00001152 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 return QualType(New, 0);
1154}
1155
1156/// getFunctionType - Return a normal function type with a typed argument
1157/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001158QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001159 unsigned NumArgs, bool isVariadic,
1160 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001161 // Unique functions, to guarantee there is only one function of a particular
1162 // structure.
1163 llvm::FoldingSetNodeID ID;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001164 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1165 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001166
1167 void *InsertPos = 0;
1168 if (FunctionTypeProto *FTP =
1169 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1170 return QualType(FTP, 0);
1171
1172 // Determine whether the type being created is already canonical or not.
1173 bool isCanonical = ResultTy->isCanonical();
1174 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1175 if (!ArgArray[i]->isCanonical())
1176 isCanonical = false;
1177
1178 // If this type isn't canonical, get the canonical version of it.
1179 QualType Canonical;
1180 if (!isCanonical) {
1181 llvm::SmallVector<QualType, 16> CanonicalArgs;
1182 CanonicalArgs.reserve(NumArgs);
1183 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001184 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001185
Chris Lattnerf52ab252008-04-06 22:59:24 +00001186 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001187 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001188 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001189
1190 // Get the new insert position for the node we care about.
1191 FunctionTypeProto *NewIP =
1192 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001193 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 }
1195
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001196 // FunctionTypeProto objects are allocated with extra bytes after them
1197 // for a variable size array (for parameter types) at the end of them.
Reid Spencer5f016e22007-07-11 17:01:13 +00001198 FunctionTypeProto *FTP =
Steve Naroffc0ac4922009-01-27 23:20:32 +00001199 (FunctionTypeProto*)Allocate(sizeof(FunctionTypeProto) +
1200 NumArgs*sizeof(QualType), 8);
Reid Spencer5f016e22007-07-11 17:01:13 +00001201 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001202 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001203 Types.push_back(FTP);
1204 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1205 return QualType(FTP, 0);
1206}
1207
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001208/// getTypeDeclType - Return the unique reference to the type for the
1209/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001210QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001211 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001212 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1213
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001214 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001215 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001216 else if (isa<TemplateTypeParmDecl>(Decl)) {
1217 assert(false && "Template type parameter types are always available.");
1218 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001219 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001220
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001221 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001222 if (PrevDecl)
1223 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001224 else
1225 Decl->TypeForDecl = new (*this,8) CXXRecordType(CXXRecord);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001226 }
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001227 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001228 if (PrevDecl)
1229 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001230 else
1231 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001232 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001233 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1234 if (PrevDecl)
1235 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001236 else
1237 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001238 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001239 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001240 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001241
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001242 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001243 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001244}
1245
Reid Spencer5f016e22007-07-11 17:01:13 +00001246/// getTypedefType - Return the unique reference to the type for the
1247/// specified typename decl.
1248QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1249 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1250
Chris Lattnerf52ab252008-04-06 22:59:24 +00001251 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Steve Narofff83820b2009-01-27 22:08:43 +00001252 Decl->TypeForDecl = new(*this,8) TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 Types.push_back(Decl->TypeForDecl);
1254 return QualType(Decl->TypeForDecl, 0);
1255}
1256
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001257/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001258/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001259QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001260 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1261
Steve Narofff83820b2009-01-27 22:08:43 +00001262 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001263 Types.push_back(Decl->TypeForDecl);
1264 return QualType(Decl->TypeForDecl, 0);
1265}
1266
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001267/// buildObjCInterfaceType - Returns a new type for the interface
1268/// declaration, regardless. It also removes any previously built
1269/// record declaration so caller can rebuild it.
1270QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1271 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1272 if (RD)
1273 RD = 0;
1274 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1275 Types.push_back(Decl->TypeForDecl);
1276 return QualType(Decl->TypeForDecl, 0);
1277}
1278
Douglas Gregorfab9d672009-02-05 23:33:38 +00001279/// \brief Retrieve the template type parameter type for a template
1280/// parameter with the given depth, index, and (optionally) name.
1281QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1282 IdentifierInfo *Name) {
1283 llvm::FoldingSetNodeID ID;
1284 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1285 void *InsertPos = 0;
1286 TemplateTypeParmType *TypeParm
1287 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1288
1289 if (TypeParm)
1290 return QualType(TypeParm, 0);
1291
1292 if (Name)
1293 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1294 getTemplateTypeParmType(Depth, Index));
1295 else
1296 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1297
1298 Types.push_back(TypeParm);
1299 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1300
1301 return QualType(TypeParm, 0);
1302}
1303
Douglas Gregor55f6b142009-02-09 18:46:07 +00001304QualType
1305ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
1306 unsigned NumArgs,
1307 uintptr_t *Args, bool *ArgIsType,
1308 QualType Canon) {
1309 llvm::FoldingSetNodeID ID;
1310 ClassTemplateSpecializationType::Profile(ID, Template, NumArgs, Args,
1311 ArgIsType);
1312 void *InsertPos = 0;
1313 ClassTemplateSpecializationType *Spec
1314 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1315
1316 if (Spec)
1317 return QualType(Spec, 0);
1318
1319 void *Mem = Allocate(sizeof(ClassTemplateSpecializationType) +
1320 (sizeof(uintptr_t) *
1321 (ClassTemplateSpecializationType::
1322 getNumPackedWords(NumArgs) +
1323 NumArgs)), 8);
1324 Spec = new (Mem) ClassTemplateSpecializationType(Template, NumArgs, Args,
1325 ArgIsType, Canon);
1326 Types.push_back(Spec);
1327 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1328
1329 return QualType(Spec, 0);
1330}
1331
Chris Lattner88cb27a2008-04-07 04:56:42 +00001332/// CmpProtocolNames - Comparison predicate for sorting protocols
1333/// alphabetically.
1334static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1335 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001336 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001337}
1338
1339static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1340 unsigned &NumProtocols) {
1341 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1342
1343 // Sort protocols, keyed by name.
1344 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1345
1346 // Remove duplicates.
1347 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1348 NumProtocols = ProtocolsEnd-Protocols;
1349}
1350
1351
Chris Lattner065f0d72008-04-07 04:44:08 +00001352/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1353/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001354QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1355 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001356 // Sort the protocol list alphabetically to canonicalize it.
1357 SortAndUniqueProtocols(Protocols, NumProtocols);
1358
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001359 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001360 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001361
1362 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001363 if (ObjCQualifiedInterfaceType *QT =
1364 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001365 return QualType(QT, 0);
1366
1367 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001368 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001369 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001370
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001371 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001372 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001373 return QualType(QType, 0);
1374}
1375
Chris Lattner88cb27a2008-04-07 04:56:42 +00001376/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1377/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001378QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001379 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001380 // Sort the protocol list alphabetically to canonicalize it.
1381 SortAndUniqueProtocols(Protocols, NumProtocols);
1382
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001383 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001384 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001385
1386 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001387 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001388 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001389 return QualType(QT, 0);
1390
1391 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001392 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001393 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001394 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001395 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001396 return QualType(QType, 0);
1397}
1398
Steve Naroff15509f42009-02-21 20:17:11 +00001399/// getObjCQualifiedClassType - Return an ObjCQualifiedIdType for the 'Class'
1400/// decl and the conforming protocol list.
1401QualType ASTContext::getObjCQualifiedClassType(ObjCProtocolDecl **Protocols,
1402 unsigned NumProtocols) {
1403 // Sort the protocol list alphabetically to canonicalize it.
1404 SortAndUniqueProtocols(Protocols, NumProtocols);
1405
1406 llvm::FoldingSetNodeID ID;
1407 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
1408
1409 void *InsertPos = 0;
1410 if (ObjCQualifiedClassType *QT =
1411 ObjCQualifiedClassTypes.FindNodeOrInsertPos(ID, InsertPos))
1412 return QualType(QT, 0);
1413
1414 // No Match;
1415 ObjCQualifiedClassType *QType =
1416 new (*this,8) ObjCQualifiedClassType(Protocols, NumProtocols);
1417 Types.push_back(QType);
1418 ObjCQualifiedClassTypes.InsertNode(QType, InsertPos);
1419 return QualType(QType, 0);
1420}
1421
Steve Naroff9752f252007-08-01 18:02:17 +00001422/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1423/// TypeOfExpr AST's (since expression's are never shared). For example,
1424/// multiple declarations that refer to "typeof(x)" all contain different
1425/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1426/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +00001427QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001428 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Narofff83820b2009-01-27 22:08:43 +00001429 TypeOfExpr *toe = new (*this,8) TypeOfExpr(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001430 Types.push_back(toe);
1431 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001432}
1433
Steve Naroff9752f252007-08-01 18:02:17 +00001434/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1435/// TypeOfType AST's. The only motivation to unique these nodes would be
1436/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1437/// an issue. This doesn't effect the type checker, since it operates
1438/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001439QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001440 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001441 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001442 Types.push_back(tot);
1443 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001444}
1445
Reid Spencer5f016e22007-07-11 17:01:13 +00001446/// getTagDeclType - Return the unique reference to the type for the
1447/// specified TagDecl (struct/union/class/enum) decl.
1448QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001449 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001450 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001451}
1452
1453/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1454/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1455/// needs to agree with the definition in <stddef.h>.
1456QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001457 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001458}
1459
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001460/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfd888a52008-02-12 08:29:21 +00001461/// width of characters in wide strings, The value is target dependent and
1462/// needs to agree with the definition in <stddef.h>.
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001463QualType ASTContext::getWCharType() const {
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001464 if (LangOpts.CPlusPlus)
1465 return WCharTy;
1466
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001467 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1468 // wide-character type?
1469 return getFromTargetType(Target.getWCharType());
Eli Friedmanfd888a52008-02-12 08:29:21 +00001470}
1471
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001472/// getSignedWCharType - Return the type of "signed wchar_t".
1473/// Used when in C++, as a GCC extension.
1474QualType ASTContext::getSignedWCharType() const {
1475 // FIXME: derive from "Target" ?
1476 return WCharTy;
1477}
1478
1479/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1480/// Used when in C++, as a GCC extension.
1481QualType ASTContext::getUnsignedWCharType() const {
1482 // FIXME: derive from "Target" ?
1483 return UnsignedIntTy;
1484}
1485
Chris Lattner8b9023b2007-07-13 03:05:23 +00001486/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1487/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1488QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001489 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001490}
1491
Chris Lattnere6327742008-04-02 05:18:44 +00001492//===----------------------------------------------------------------------===//
1493// Type Operators
1494//===----------------------------------------------------------------------===//
1495
Chris Lattner77c96472008-04-06 22:41:35 +00001496/// getCanonicalType - Return the canonical (structural) type corresponding to
1497/// the specified potentially non-canonical type. The non-canonical version
1498/// of a type may have many "decorated" versions of types. Decorators can
1499/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1500/// to be free of any of these, allowing two canonical types to be compared
1501/// for exact equality with a simple pointer comparison.
1502QualType ASTContext::getCanonicalType(QualType T) {
1503 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001504
1505 // If the result has type qualifiers, make sure to canonicalize them as well.
1506 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1507 if (TypeQuals == 0) return CanType;
1508
1509 // If the type qualifiers are on an array type, get the canonical type of the
1510 // array with the qualifiers applied to the element type.
1511 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1512 if (!AT)
1513 return CanType.getQualifiedType(TypeQuals);
1514
1515 // Get the canonical version of the element with the extra qualifiers on it.
1516 // This can recursively sink qualifiers through multiple levels of arrays.
1517 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1518 NewEltTy = getCanonicalType(NewEltTy);
1519
1520 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1521 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1522 CAT->getIndexTypeQualifier());
1523 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1524 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1525 IAT->getIndexTypeQualifier());
1526
Douglas Gregor898574e2008-12-05 23:32:09 +00001527 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1528 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1529 DSAT->getSizeModifier(),
1530 DSAT->getIndexTypeQualifier());
1531
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001532 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1533 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1534 VAT->getSizeModifier(),
1535 VAT->getIndexTypeQualifier());
1536}
1537
1538
1539const ArrayType *ASTContext::getAsArrayType(QualType T) {
1540 // Handle the non-qualified case efficiently.
1541 if (T.getCVRQualifiers() == 0) {
1542 // Handle the common positive case fast.
1543 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1544 return AT;
1545 }
1546
1547 // Handle the common negative case fast, ignoring CVR qualifiers.
1548 QualType CType = T->getCanonicalTypeInternal();
1549
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001550 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001551 // test.
1552 if (!isa<ArrayType>(CType) &&
1553 !isa<ArrayType>(CType.getUnqualifiedType()))
1554 return 0;
1555
1556 // Apply any CVR qualifiers from the array type to the element type. This
1557 // implements C99 6.7.3p8: "If the specification of an array type includes
1558 // any type qualifiers, the element type is so qualified, not the array type."
1559
1560 // If we get here, we either have type qualifiers on the type, or we have
1561 // sugar such as a typedef in the way. If we have type qualifiers on the type
1562 // we must propagate them down into the elemeng type.
1563 unsigned CVRQuals = T.getCVRQualifiers();
1564 unsigned AddrSpace = 0;
1565 Type *Ty = T.getTypePtr();
1566
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001567 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001568 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001569 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1570 AddrSpace = EXTQT->getAddressSpace();
1571 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001572 } else {
1573 T = Ty->getDesugaredType();
1574 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1575 break;
1576 CVRQuals |= T.getCVRQualifiers();
1577 Ty = T.getTypePtr();
1578 }
1579 }
1580
1581 // If we have a simple case, just return now.
1582 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1583 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1584 return ATy;
1585
1586 // Otherwise, we have an array and we have qualifiers on it. Push the
1587 // qualifiers into the array element type and return a new array type.
1588 // Get the canonical version of the element with the extra qualifiers on it.
1589 // This can recursively sink qualifiers through multiple levels of arrays.
1590 QualType NewEltTy = ATy->getElementType();
1591 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001592 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001593 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1594
1595 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1596 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1597 CAT->getSizeModifier(),
1598 CAT->getIndexTypeQualifier()));
1599 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1600 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1601 IAT->getSizeModifier(),
1602 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001603
Douglas Gregor898574e2008-12-05 23:32:09 +00001604 if (const DependentSizedArrayType *DSAT
1605 = dyn_cast<DependentSizedArrayType>(ATy))
1606 return cast<ArrayType>(
1607 getDependentSizedArrayType(NewEltTy,
1608 DSAT->getSizeExpr(),
1609 DSAT->getSizeModifier(),
1610 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001611
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001612 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1613 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1614 VAT->getSizeModifier(),
1615 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001616}
1617
1618
Chris Lattnere6327742008-04-02 05:18:44 +00001619/// getArrayDecayedType - Return the properly qualified result of decaying the
1620/// specified array type to a pointer. This operation is non-trivial when
1621/// handling typedefs etc. The canonical type of "T" must be an array type,
1622/// this returns a pointer to a properly qualified element of the array.
1623///
1624/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1625QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001626 // Get the element type with 'getAsArrayType' so that we don't lose any
1627 // typedefs in the element type of the array. This also handles propagation
1628 // of type qualifiers from the array type into the element type if present
1629 // (C99 6.7.3p8).
1630 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1631 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001632
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001633 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001634
1635 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001636 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001637}
1638
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001639QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001640 QualType ElemTy = VAT->getElementType();
1641
1642 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1643 return getBaseElementType(VAT);
1644
1645 return ElemTy;
1646}
1647
Reid Spencer5f016e22007-07-11 17:01:13 +00001648/// getFloatingRank - Return a relative rank for floating point types.
1649/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001650static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001651 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001652 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001653
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001654 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001655 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001656 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 case BuiltinType::Float: return FloatRank;
1658 case BuiltinType::Double: return DoubleRank;
1659 case BuiltinType::LongDouble: return LongDoubleRank;
1660 }
1661}
1662
Steve Naroff716c7302007-08-27 01:41:48 +00001663/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1664/// point or a complex type (based on typeDomain/typeSize).
1665/// 'typeDomain' is a real floating point or complex type.
1666/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001667QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1668 QualType Domain) const {
1669 FloatingRank EltRank = getFloatingRank(Size);
1670 if (Domain->isComplexType()) {
1671 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001672 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001673 case FloatRank: return FloatComplexTy;
1674 case DoubleRank: return DoubleComplexTy;
1675 case LongDoubleRank: return LongDoubleComplexTy;
1676 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001677 }
Chris Lattner1361b112008-04-06 23:58:54 +00001678
1679 assert(Domain->isRealFloatingType() && "Unknown domain!");
1680 switch (EltRank) {
1681 default: assert(0 && "getFloatingRank(): illegal value for rank");
1682 case FloatRank: return FloatTy;
1683 case DoubleRank: return DoubleTy;
1684 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001685 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001686}
1687
Chris Lattner7cfeb082008-04-06 23:55:33 +00001688/// getFloatingTypeOrder - Compare the rank of the two specified floating
1689/// point types, ignoring the domain of the type (i.e. 'double' ==
1690/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1691/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001692int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1693 FloatingRank LHSR = getFloatingRank(LHS);
1694 FloatingRank RHSR = getFloatingRank(RHS);
1695
1696 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001697 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001698 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001699 return 1;
1700 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001701}
1702
Chris Lattnerf52ab252008-04-06 22:59:24 +00001703/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1704/// routine will assert if passed a built-in type that isn't an integer or enum,
1705/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001706unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001707 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001708 if (EnumType* ET = dyn_cast<EnumType>(T))
1709 T = ET->getDecl()->getIntegerType().getTypePtr();
1710
1711 // There are two things which impact the integer rank: the width, and
1712 // the ordering of builtins. The builtin ordering is encoded in the
1713 // bottom three bits; the width is encoded in the bits above that.
1714 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1715 return FWIT->getWidth() << 3;
1716 }
1717
Chris Lattnerf52ab252008-04-06 22:59:24 +00001718 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001719 default: assert(0 && "getIntegerRank(): not a built-in integer");
1720 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001721 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001722 case BuiltinType::Char_S:
1723 case BuiltinType::Char_U:
1724 case BuiltinType::SChar:
1725 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001726 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001727 case BuiltinType::Short:
1728 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001729 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001730 case BuiltinType::Int:
1731 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001732 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001733 case BuiltinType::Long:
1734 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001735 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001736 case BuiltinType::LongLong:
1737 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001738 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001739 }
1740}
1741
Chris Lattner7cfeb082008-04-06 23:55:33 +00001742/// getIntegerTypeOrder - Returns the highest ranked integer type:
1743/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1744/// LHS < RHS, return -1.
1745int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001746 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1747 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001748 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001749
Chris Lattnerf52ab252008-04-06 22:59:24 +00001750 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1751 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001752
Chris Lattner7cfeb082008-04-06 23:55:33 +00001753 unsigned LHSRank = getIntegerRank(LHSC);
1754 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001755
Chris Lattner7cfeb082008-04-06 23:55:33 +00001756 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1757 if (LHSRank == RHSRank) return 0;
1758 return LHSRank > RHSRank ? 1 : -1;
1759 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001760
Chris Lattner7cfeb082008-04-06 23:55:33 +00001761 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1762 if (LHSUnsigned) {
1763 // If the unsigned [LHS] type is larger, return it.
1764 if (LHSRank >= RHSRank)
1765 return 1;
1766
1767 // If the signed type can represent all values of the unsigned type, it
1768 // wins. Because we are dealing with 2's complement and types that are
1769 // powers of two larger than each other, this is always safe.
1770 return -1;
1771 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001772
Chris Lattner7cfeb082008-04-06 23:55:33 +00001773 // If the unsigned [RHS] type is larger, return it.
1774 if (RHSRank >= LHSRank)
1775 return -1;
1776
1777 // If the signed type can represent all values of the unsigned type, it
1778 // wins. Because we are dealing with 2's complement and types that are
1779 // powers of two larger than each other, this is always safe.
1780 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001781}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001782
1783// getCFConstantStringType - Return the type used for constant CFStrings.
1784QualType ASTContext::getCFConstantStringType() {
1785 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001786 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001787 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001788 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001789 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001790
1791 // const int *isa;
1792 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001793 // int flags;
1794 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001795 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001796 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001797 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001798 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001799
Anders Carlsson71993dd2007-08-17 05:31:46 +00001800 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001801 for (unsigned i = 0; i < 4; ++i) {
1802 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1803 SourceLocation(), 0,
1804 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001805 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001806 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001807 }
1808
1809 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001810 }
1811
1812 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001813}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001814
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001815QualType ASTContext::getObjCFastEnumerationStateType()
1816{
1817 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001818 ObjCFastEnumerationStateTypeDecl =
1819 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1820 &Idents.get("__objcFastEnumerationState"));
1821
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001822 QualType FieldTypes[] = {
1823 UnsignedLongTy,
1824 getPointerType(ObjCIdType),
1825 getPointerType(UnsignedLongTy),
1826 getConstantArrayType(UnsignedLongTy,
1827 llvm::APInt(32, 5), ArrayType::Normal, 0)
1828 };
1829
Douglas Gregor44b43212008-12-11 16:49:14 +00001830 for (size_t i = 0; i < 4; ++i) {
1831 FieldDecl *Field = FieldDecl::Create(*this,
1832 ObjCFastEnumerationStateTypeDecl,
1833 SourceLocation(), 0,
1834 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001835 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001836 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001837 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001838
Douglas Gregor44b43212008-12-11 16:49:14 +00001839 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001840 }
1841
1842 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1843}
1844
Anders Carlssone8c49532007-10-29 06:33:42 +00001845// This returns true if a type has been typedefed to BOOL:
1846// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001847static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001848 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001849 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1850 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001851
1852 return false;
1853}
1854
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001855/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001856/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001857int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001858 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001859
1860 // Make all integer and enum types at least as large as an int
1861 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001862 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001863 // Treat arrays as pointers, since that's how they're passed in.
1864 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001865 sz = getTypeSize(VoidPtrTy);
1866 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001867}
1868
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001869/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001870/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001871void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001872 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001873 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001874 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001875 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001876 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001877 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001878 // Compute size of all parameters.
1879 // Start with computing size of a pointer in number of bytes.
1880 // FIXME: There might(should) be a better way of doing this computation!
1881 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001882 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001883 // The first two arguments (self and _cmd) are pointers; account for
1884 // their size.
1885 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001886 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1887 E = Decl->param_end(); PI != E; ++PI) {
1888 QualType PType = (*PI)->getType();
1889 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001890 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001891 ParmOffset += sz;
1892 }
1893 S += llvm::utostr(ParmOffset);
1894 S += "@0:";
1895 S += llvm::utostr(PtrSize);
1896
1897 // Argument types.
1898 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001899 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1900 E = Decl->param_end(); PI != E; ++PI) {
1901 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001902 QualType PType = PVDecl->getOriginalType();
1903 if (const ArrayType *AT =
1904 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1905 // Use array's original type only if it has known number of
1906 // elements.
1907 if (!dyn_cast<ConstantArrayType>(AT))
1908 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001909 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001910 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001911 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001912 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001913 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001914 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001915 }
1916}
1917
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001918/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001919/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001920/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1921/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001922/// Property attributes are stored as a comma-delimited C string. The simple
1923/// attributes readonly and bycopy are encoded as single characters. The
1924/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1925/// encoded as single characters, followed by an identifier. Property types
1926/// are also encoded as a parametrized attribute. The characters used to encode
1927/// these attributes are defined by the following enumeration:
1928/// @code
1929/// enum PropertyAttributes {
1930/// kPropertyReadOnly = 'R', // property is read-only.
1931/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1932/// kPropertyByref = '&', // property is a reference to the value last assigned
1933/// kPropertyDynamic = 'D', // property is dynamic
1934/// kPropertyGetter = 'G', // followed by getter selector name
1935/// kPropertySetter = 'S', // followed by setter selector name
1936/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1937/// kPropertyType = 't' // followed by old-style type encoding.
1938/// kPropertyWeak = 'W' // 'weak' property
1939/// kPropertyStrong = 'P' // property GC'able
1940/// kPropertyNonAtomic = 'N' // property non-atomic
1941/// };
1942/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001943void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1944 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001945 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001946 // Collect information from the property implementation decl(s).
1947 bool Dynamic = false;
1948 ObjCPropertyImplDecl *SynthesizePID = 0;
1949
1950 // FIXME: Duplicated code due to poor abstraction.
1951 if (Container) {
1952 if (const ObjCCategoryImplDecl *CID =
1953 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1954 for (ObjCCategoryImplDecl::propimpl_iterator
1955 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1956 ObjCPropertyImplDecl *PID = *i;
1957 if (PID->getPropertyDecl() == PD) {
1958 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1959 Dynamic = true;
1960 } else {
1961 SynthesizePID = PID;
1962 }
1963 }
1964 }
1965 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001966 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001967 for (ObjCCategoryImplDecl::propimpl_iterator
1968 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1969 ObjCPropertyImplDecl *PID = *i;
1970 if (PID->getPropertyDecl() == PD) {
1971 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1972 Dynamic = true;
1973 } else {
1974 SynthesizePID = PID;
1975 }
1976 }
1977 }
1978 }
1979 }
1980
1981 // FIXME: This is not very efficient.
1982 S = "T";
1983
1984 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001985 // GCC has some special rules regarding encoding of properties which
1986 // closely resembles encoding of ivars.
1987 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1988 true /* outermost type */,
1989 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001990
1991 if (PD->isReadOnly()) {
1992 S += ",R";
1993 } else {
1994 switch (PD->getSetterKind()) {
1995 case ObjCPropertyDecl::Assign: break;
1996 case ObjCPropertyDecl::Copy: S += ",C"; break;
1997 case ObjCPropertyDecl::Retain: S += ",&"; break;
1998 }
1999 }
2000
2001 // It really isn't clear at all what this means, since properties
2002 // are "dynamic by default".
2003 if (Dynamic)
2004 S += ",D";
2005
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002006 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
2007 S += ",N";
2008
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002009 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
2010 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002011 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002012 }
2013
2014 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
2015 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00002016 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002017 }
2018
2019 if (SynthesizePID) {
2020 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2021 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002022 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002023 }
2024
2025 // FIXME: OBJCGC: weak & strong
2026}
2027
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002028/// getLegacyIntegralTypeEncoding -
2029/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002030/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002031/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2032///
2033void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2034 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2035 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002036 if (BT->getKind() == BuiltinType::ULong &&
2037 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002038 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002039 else
2040 if (BT->getKind() == BuiltinType::Long &&
2041 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002042 PointeeTy = IntTy;
2043 }
2044 }
2045}
2046
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002047void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002048 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002049 // We follow the behavior of gcc, expanding structures which are
2050 // directly pointed to, and expanding embedded structures. Note that
2051 // these rules are sufficient to prevent recursive encoding of the
2052 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002053 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2054 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002055}
2056
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002057static void EncodeBitField(const ASTContext *Context, std::string& S,
2058 FieldDecl *FD) {
2059 const Expr *E = FD->getBitWidth();
2060 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2061 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2062 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2063 S += 'b';
2064 S += llvm::utostr(N);
2065}
2066
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002067void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2068 bool ExpandPointedToStructures,
2069 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002070 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002071 bool OutermostType,
2072 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00002073 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002074 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002075 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002076 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002077 else {
2078 char encoding;
2079 switch (BT->getKind()) {
2080 default: assert(0 && "Unhandled builtin type kind");
2081 case BuiltinType::Void: encoding = 'v'; break;
2082 case BuiltinType::Bool: encoding = 'B'; break;
2083 case BuiltinType::Char_U:
2084 case BuiltinType::UChar: encoding = 'C'; break;
2085 case BuiltinType::UShort: encoding = 'S'; break;
2086 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002087 case BuiltinType::ULong:
2088 encoding =
2089 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2090 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002091 case BuiltinType::ULongLong: encoding = 'Q'; break;
2092 case BuiltinType::Char_S:
2093 case BuiltinType::SChar: encoding = 'c'; break;
2094 case BuiltinType::Short: encoding = 's'; break;
2095 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002096 case BuiltinType::Long:
2097 encoding =
2098 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2099 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002100 case BuiltinType::LongLong: encoding = 'q'; break;
2101 case BuiltinType::Float: encoding = 'f'; break;
2102 case BuiltinType::Double: encoding = 'd'; break;
2103 case BuiltinType::LongDouble: encoding = 'd'; break;
2104 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002105
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002106 S += encoding;
2107 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002108 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002109 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002110 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2111 ExpandPointedToStructures,
2112 ExpandStructures, FD);
2113 if (FD || EncodingProperty) {
2114 // Note that we do extended encoding of protocol qualifer list
2115 // Only when doing ivar or property encoding.
2116 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2117 S += '"';
2118 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2119 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2120 S += '<';
2121 S += Proto->getNameAsString();
2122 S += '>';
2123 }
2124 S += '"';
2125 }
2126 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002127 }
2128 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002129 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002130 bool isReadOnly = false;
2131 // For historical/compatibility reasons, the read-only qualifier of the
2132 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2133 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2134 // Also, do not emit the 'r' for anything but the outermost type!
2135 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2136 if (OutermostType && T.isConstQualified()) {
2137 isReadOnly = true;
2138 S += 'r';
2139 }
2140 }
2141 else if (OutermostType) {
2142 QualType P = PointeeTy;
2143 while (P->getAsPointerType())
2144 P = P->getAsPointerType()->getPointeeType();
2145 if (P.isConstQualified()) {
2146 isReadOnly = true;
2147 S += 'r';
2148 }
2149 }
2150 if (isReadOnly) {
2151 // Another legacy compatibility encoding. Some ObjC qualifier and type
2152 // combinations need to be rearranged.
2153 // Rewrite "in const" from "nr" to "rn"
2154 const char * s = S.c_str();
2155 int len = S.length();
2156 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2157 std::string replace = "rn";
2158 S.replace(S.end()-2, S.end(), replace);
2159 }
2160 }
Steve Naroff389bf462009-02-12 17:52:19 +00002161 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002162 S += '@';
2163 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002164 }
2165 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002166 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002167 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002168 // Another historical/compatibility reason.
2169 // We encode the underlying type which comes out as
2170 // {...};
2171 S += '^';
2172 getObjCEncodingForTypeImpl(PointeeTy, S,
2173 false, ExpandPointedToStructures,
2174 NULL);
2175 return;
2176 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002177 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002178 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002179 const ObjCInterfaceType *OIT =
2180 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002181 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002182 S += '"';
2183 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002184 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2185 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2186 S += '<';
2187 S += Proto->getNameAsString();
2188 S += '>';
2189 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002190 S += '"';
2191 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002192 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002193 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002194 S += '#';
2195 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002196 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002197 S += ':';
2198 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002199 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002200
2201 if (PointeeTy->isCharType()) {
2202 // char pointer types should be encoded as '*' unless it is a
2203 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002204 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002205 S += '*';
2206 return;
2207 }
2208 }
2209
2210 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002211 getLegacyIntegralTypeEncoding(PointeeTy);
2212
2213 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002214 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002215 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002216 } else if (const ArrayType *AT =
2217 // Ignore type qualifiers etc.
2218 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002219 if (isa<IncompleteArrayType>(AT)) {
2220 // Incomplete arrays are encoded as a pointer to the array element.
2221 S += '^';
2222
2223 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2224 false, ExpandStructures, FD);
2225 } else {
2226 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002227
Anders Carlsson559a8332009-02-22 01:38:57 +00002228 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2229 S += llvm::utostr(CAT->getSize().getZExtValue());
2230 else {
2231 //Variable length arrays are encoded as a regular array with 0 elements.
2232 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2233 S += '0';
2234 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002235
Anders Carlsson559a8332009-02-22 01:38:57 +00002236 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2237 false, ExpandStructures, FD);
2238 S += ']';
2239 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002240 } else if (T->getAsFunctionType()) {
2241 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002242 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002243 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002244 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002245 // Anonymous structures print as '?'
2246 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2247 S += II->getName();
2248 } else {
2249 S += '?';
2250 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002251 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002252 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002253 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2254 FieldEnd = RDecl->field_end();
2255 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002256 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002257 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002258 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002259 S += '"';
2260 }
2261
2262 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002263 if (Field->isBitField()) {
2264 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2265 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002266 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002267 QualType qt = Field->getType();
2268 getLegacyIntegralTypeEncoding(qt);
2269 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002270 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002271 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002272 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002273 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002274 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002275 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002276 if (FD && FD->isBitField())
2277 EncodeBitField(this, S, FD);
2278 else
2279 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002280 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002281 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002282 } else if (T->isObjCInterfaceType()) {
2283 // @encode(class_name)
2284 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2285 S += '{';
2286 const IdentifierInfo *II = OI->getIdentifier();
2287 S += II->getName();
2288 S += '=';
2289 std::vector<FieldDecl*> RecFields;
2290 CollectObjCIvars(OI, RecFields);
2291 for (unsigned int i = 0; i != RecFields.size(); i++) {
2292 if (RecFields[i]->isBitField())
2293 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2294 RecFields[i]);
2295 else
2296 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2297 FD);
2298 }
2299 S += '}';
2300 }
2301 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002302 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002303}
2304
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002305void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002306 std::string& S) const {
2307 if (QT & Decl::OBJC_TQ_In)
2308 S += 'n';
2309 if (QT & Decl::OBJC_TQ_Inout)
2310 S += 'N';
2311 if (QT & Decl::OBJC_TQ_Out)
2312 S += 'o';
2313 if (QT & Decl::OBJC_TQ_Bycopy)
2314 S += 'O';
2315 if (QT & Decl::OBJC_TQ_Byref)
2316 S += 'R';
2317 if (QT & Decl::OBJC_TQ_Oneway)
2318 S += 'V';
2319}
2320
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002321void ASTContext::setBuiltinVaListType(QualType T)
2322{
2323 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2324
2325 BuiltinVaListType = T;
2326}
2327
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002328void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002329{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002330 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002331
2332 // typedef struct objc_object *id;
2333 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002334 // User error - caller will issue diagnostics.
2335 if (!ptr)
2336 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002337 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002338 // User error - caller will issue diagnostics.
2339 if (!rec)
2340 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002341 IdStructType = rec;
2342}
2343
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002344void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002345{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002346 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002347
2348 // typedef struct objc_selector *SEL;
2349 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002350 if (!ptr)
2351 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002352 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002353 if (!rec)
2354 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002355 SelStructType = rec;
2356}
2357
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002358void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002359{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002360 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002361}
2362
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002363void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002364{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002365 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002366
2367 // typedef struct objc_class *Class;
2368 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2369 assert(ptr && "'Class' incorrectly typed");
2370 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2371 assert(rec && "'Class' incorrectly typed");
2372 ClassStructType = rec;
2373}
2374
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002375void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2376 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002377 "'NSConstantString' type already set!");
2378
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002379 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002380}
2381
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002382/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002383/// TargetInfo, produce the corresponding type. The unsigned @p Type
2384/// is actually a value of type @c TargetInfo::IntType.
2385QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002386 switch (Type) {
2387 case TargetInfo::NoInt: return QualType();
2388 case TargetInfo::SignedShort: return ShortTy;
2389 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2390 case TargetInfo::SignedInt: return IntTy;
2391 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2392 case TargetInfo::SignedLong: return LongTy;
2393 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2394 case TargetInfo::SignedLongLong: return LongLongTy;
2395 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2396 }
2397
2398 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002399 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002400}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002401
2402//===----------------------------------------------------------------------===//
2403// Type Predicates.
2404//===----------------------------------------------------------------------===//
2405
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002406/// isObjCNSObjectType - Return true if this is an NSObject object using
2407/// NSObject attribute on a c-style pointer type.
2408/// FIXME - Make it work directly on types.
2409///
2410bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2411 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2412 if (TypedefDecl *TD = TDT->getDecl())
2413 if (TD->getAttr<ObjCNSObjectAttr>())
2414 return true;
2415 }
2416 return false;
2417}
2418
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002419/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2420/// to an object type. This includes "id" and "Class" (two 'special' pointers
2421/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2422/// ID type).
2423bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Narofff7f52e72009-02-21 21:17:01 +00002424 if (Ty->isObjCQualifiedIdType() || Ty->isObjCQualifiedClassType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002425 return true;
2426
Steve Naroff6ae98502008-10-21 18:24:04 +00002427 // Blocks are objects.
2428 if (Ty->isBlockPointerType())
2429 return true;
2430
2431 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002432 if (!Ty->isPointerType())
2433 return false;
2434
2435 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2436 // pointer types. This looks for the typedef specifically, not for the
2437 // underlying type.
2438 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2439 return true;
2440
2441 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002442 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2443 return true;
2444
2445 // If is has NSObject attribute, OK as well.
2446 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002447}
2448
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002449/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2450/// garbage collection attribute.
2451///
2452QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002453 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002454 if (getLangOptions().ObjC1 &&
2455 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002456 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002457 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002458 // (or pointers to them) be treated as though they were declared
2459 // as __strong.
2460 if (GCAttrs == QualType::GCNone) {
2461 if (isObjCObjectPointerType(Ty))
2462 GCAttrs = QualType::Strong;
2463 else if (Ty->isPointerType())
2464 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2465 }
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002466 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002467 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002468}
2469
Chris Lattner6ac46a42008-04-07 06:51:04 +00002470//===----------------------------------------------------------------------===//
2471// Type Compatibility Testing
2472//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002473
Steve Naroff1c7d0672008-09-04 15:10:53 +00002474/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002475/// block types. Types must be strictly compatible here. For example,
2476/// C unfortunately doesn't produce an error for the following:
2477///
2478/// int (*emptyArgFunc)();
2479/// int (*intArgList)(int) = emptyArgFunc;
2480///
2481/// For blocks, we will produce an error for the following (similar to C++):
2482///
2483/// int (^emptyArgBlock)();
2484/// int (^intArgBlock)(int) = emptyArgBlock;
2485///
2486/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2487///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002488bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002489 const FunctionType *lbase = lhs->getAsFunctionType();
2490 const FunctionType *rbase = rhs->getAsFunctionType();
2491 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2492 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2493 if (lproto && rproto)
2494 return !mergeTypes(lhs, rhs).isNull();
2495 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002496}
2497
Chris Lattner6ac46a42008-04-07 06:51:04 +00002498/// areCompatVectorTypes - Return true if the two specified vector types are
2499/// compatible.
2500static bool areCompatVectorTypes(const VectorType *LHS,
2501 const VectorType *RHS) {
2502 assert(LHS->isCanonical() && RHS->isCanonical());
2503 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002504 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002505}
2506
Eli Friedman3d815e72008-08-22 00:56:42 +00002507/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002508/// compatible for assignment from RHS to LHS. This handles validation of any
2509/// protocol qualifiers on the LHS or RHS.
2510///
Eli Friedman3d815e72008-08-22 00:56:42 +00002511bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2512 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002513 // Verify that the base decls are compatible: the RHS must be a subclass of
2514 // the LHS.
2515 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2516 return false;
2517
2518 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2519 // protocol qualified at all, then we are good.
2520 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2521 return true;
2522
2523 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2524 // isn't a superset.
2525 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2526 return true; // FIXME: should return false!
2527
2528 // Finally, we must have two protocol-qualified interfaces.
2529 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2530 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2531 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2532 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2533 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2534 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2535
2536 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2537 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2538 // LHS in a single parallel scan until we run out of elements in LHS.
2539 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2540 ObjCProtocolDecl *LHSProto = *LHSPI;
2541
2542 while (RHSPI != RHSPE) {
2543 ObjCProtocolDecl *RHSProto = *RHSPI++;
2544 // If the RHS has a protocol that the LHS doesn't, ignore it.
2545 if (RHSProto != LHSProto)
2546 continue;
2547
2548 // Otherwise, the RHS does have this element.
2549 ++LHSPI;
2550 if (LHSPI == LHSPE)
2551 return true; // All protocols in LHS exist in RHS.
2552
2553 LHSProto = *LHSPI;
2554 }
2555
2556 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2557 return false;
2558}
2559
Steve Naroff389bf462009-02-12 17:52:19 +00002560bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2561 // get the "pointed to" types
2562 const PointerType *LHSPT = LHS->getAsPointerType();
2563 const PointerType *RHSPT = RHS->getAsPointerType();
2564
2565 if (!LHSPT || !RHSPT)
2566 return false;
2567
2568 QualType lhptee = LHSPT->getPointeeType();
2569 QualType rhptee = RHSPT->getPointeeType();
2570 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2571 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2572 // ID acts sort of like void* for ObjC interfaces
2573 if (LHSIface && isObjCIdStructType(rhptee))
2574 return true;
2575 if (RHSIface && isObjCIdStructType(lhptee))
2576 return true;
2577 if (!LHSIface || !RHSIface)
2578 return false;
2579 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2580 canAssignObjCInterfaces(RHSIface, LHSIface);
2581}
2582
Steve Naroffec0550f2007-10-15 20:41:53 +00002583/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2584/// both shall have the identically qualified version of a compatible type.
2585/// C99 6.2.7p1: Two types have compatible types if their types are the
2586/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002587bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2588 return !mergeTypes(LHS, RHS).isNull();
2589}
2590
2591QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2592 const FunctionType *lbase = lhs->getAsFunctionType();
2593 const FunctionType *rbase = rhs->getAsFunctionType();
2594 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2595 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2596 bool allLTypes = true;
2597 bool allRTypes = true;
2598
2599 // Check return type
2600 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2601 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002602 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2603 allLTypes = false;
2604 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2605 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002606
2607 if (lproto && rproto) { // two C99 style function prototypes
2608 unsigned lproto_nargs = lproto->getNumArgs();
2609 unsigned rproto_nargs = rproto->getNumArgs();
2610
2611 // Compatible functions must have the same number of arguments
2612 if (lproto_nargs != rproto_nargs)
2613 return QualType();
2614
2615 // Variadic and non-variadic functions aren't compatible
2616 if (lproto->isVariadic() != rproto->isVariadic())
2617 return QualType();
2618
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002619 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2620 return QualType();
2621
Eli Friedman3d815e72008-08-22 00:56:42 +00002622 // Check argument compatibility
2623 llvm::SmallVector<QualType, 10> types;
2624 for (unsigned i = 0; i < lproto_nargs; i++) {
2625 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2626 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2627 QualType argtype = mergeTypes(largtype, rargtype);
2628 if (argtype.isNull()) return QualType();
2629 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002630 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2631 allLTypes = false;
2632 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2633 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002634 }
2635 if (allLTypes) return lhs;
2636 if (allRTypes) return rhs;
2637 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002638 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002639 }
2640
2641 if (lproto) allRTypes = false;
2642 if (rproto) allLTypes = false;
2643
2644 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2645 if (proto) {
2646 if (proto->isVariadic()) return QualType();
2647 // Check that the types are compatible with the types that
2648 // would result from default argument promotions (C99 6.7.5.3p15).
2649 // The only types actually affected are promotable integer
2650 // types and floats, which would be passed as a different
2651 // type depending on whether the prototype is visible.
2652 unsigned proto_nargs = proto->getNumArgs();
2653 for (unsigned i = 0; i < proto_nargs; ++i) {
2654 QualType argTy = proto->getArgType(i);
2655 if (argTy->isPromotableIntegerType() ||
2656 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2657 return QualType();
2658 }
2659
2660 if (allLTypes) return lhs;
2661 if (allRTypes) return rhs;
2662 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002663 proto->getNumArgs(), lproto->isVariadic(),
2664 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002665 }
2666
2667 if (allLTypes) return lhs;
2668 if (allRTypes) return rhs;
2669 return getFunctionTypeNoProto(retType);
2670}
2671
2672QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002673 // C++ [expr]: If an expression initially has the type "reference to T", the
2674 // type is adjusted to "T" prior to any further analysis, the expression
2675 // designates the object or function denoted by the reference, and the
2676 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00002677 // FIXME: C++ shouldn't be going through here! The rules are different
2678 // enough that they should be handled separately.
2679 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002680 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002681 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002682 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002683
Eli Friedman3d815e72008-08-22 00:56:42 +00002684 QualType LHSCan = getCanonicalType(LHS),
2685 RHSCan = getCanonicalType(RHS);
2686
2687 // If two types are identical, they are compatible.
2688 if (LHSCan == RHSCan)
2689 return LHS;
2690
2691 // If the qualifiers are different, the types aren't compatible
2692 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2693 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2694 return QualType();
2695
2696 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2697 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2698
Chris Lattner1adb8832008-01-14 05:45:46 +00002699 // We want to consider the two function types to be the same for these
2700 // comparisons, just force one to the other.
2701 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2702 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002703
2704 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002705 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2706 LHSClass = Type::ConstantArray;
2707 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2708 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002709
Nate Begeman213541a2008-04-18 23:10:10 +00002710 // Canonicalize ExtVector -> Vector.
2711 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2712 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002713
Chris Lattnerb0489812008-04-07 06:38:24 +00002714 // Consider qualified interfaces and interfaces the same.
2715 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2716 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002717
Chris Lattnera36a61f2008-04-07 05:43:21 +00002718 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002719 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002720 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2721 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2722
2723 // ID acts sort of like void* for ObjC interfaces
2724 if (LHSIface && isObjCIdStructType(RHS))
2725 return LHS;
2726 if (RHSIface && isObjCIdStructType(LHS))
2727 return RHS;
2728
Steve Naroffbc76dd02008-12-10 22:14:21 +00002729 // ID is compatible with all qualified id types.
2730 if (LHS->isObjCQualifiedIdType()) {
2731 if (const PointerType *PT = RHS->getAsPointerType()) {
2732 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002733 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002734 return LHS;
2735 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2736 // Unfortunately, this API is part of Sema (which we don't have access
2737 // to. Need to refactor. The following check is insufficient, since we
2738 // need to make sure the class implements the protocol.
2739 if (pType->isObjCInterfaceType())
2740 return LHS;
2741 }
2742 }
2743 if (RHS->isObjCQualifiedIdType()) {
2744 if (const PointerType *PT = LHS->getAsPointerType()) {
2745 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002746 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002747 return RHS;
2748 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2749 // Unfortunately, this API is part of Sema (which we don't have access
2750 // to. Need to refactor. The following check is insufficient, since we
2751 // need to make sure the class implements the protocol.
2752 if (pType->isObjCInterfaceType())
2753 return RHS;
2754 }
2755 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002756 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2757 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002758 if (const EnumType* ETy = LHS->getAsEnumType()) {
2759 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2760 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002761 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002762 if (const EnumType* ETy = RHS->getAsEnumType()) {
2763 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2764 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002765 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002766
Eli Friedman3d815e72008-08-22 00:56:42 +00002767 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002768 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002769
Steve Naroff4a746782008-01-09 22:43:08 +00002770 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002771 switch (LHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00002772 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002773 {
2774 // Merge two pointer types, while trying to preserve typedef info
2775 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2776 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2777 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2778 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002779 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2780 return LHS;
2781 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2782 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002783 return getPointerType(ResultType);
2784 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002785 case Type::BlockPointer:
2786 {
2787 // Merge two block pointer types, while trying to preserve typedef info
2788 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2789 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2790 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2791 if (ResultType.isNull()) return QualType();
2792 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2793 return LHS;
2794 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2795 return RHS;
2796 return getBlockPointerType(ResultType);
2797 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002798 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002799 {
2800 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2801 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2802 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2803 return QualType();
2804
2805 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2806 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2807 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2808 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002809 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2810 return LHS;
2811 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2812 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002813 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2814 ArrayType::ArraySizeModifier(), 0);
2815 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2816 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002817 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2818 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002819 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2820 return LHS;
2821 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2822 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002823 if (LVAT) {
2824 // FIXME: This isn't correct! But tricky to implement because
2825 // the array's size has to be the size of LHS, but the type
2826 // has to be different.
2827 return LHS;
2828 }
2829 if (RVAT) {
2830 // FIXME: This isn't correct! But tricky to implement because
2831 // the array's size has to be the size of RHS, but the type
2832 // has to be different.
2833 return RHS;
2834 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002835 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2836 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002837 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002838 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002839 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002840 return mergeFunctionTypes(LHS, RHS);
2841 case Type::Tagged:
Eli Friedman3d815e72008-08-22 00:56:42 +00002842 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00002843 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2844 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002845 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002846 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002847 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002848 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00002849 case Type::Complex:
2850 // Distinct complex types are incompatible.
2851 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002852 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002853 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2854 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002855 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002856 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002857 // Check if the interfaces are assignment compatible.
2858 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2859 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2860 if (LHSIface && RHSIface &&
2861 canAssignObjCInterfaces(LHSIface, RHSIface))
2862 return LHS;
2863
Eli Friedman3d815e72008-08-22 00:56:42 +00002864 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002865 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00002866 case Type::ObjCQualifiedId:
2867 // Distinct qualified id's are not compatible.
2868 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002869 default:
2870 assert(0 && "unexpected type");
Eli Friedman3d815e72008-08-22 00:56:42 +00002871 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002872 }
Steve Naroffec0550f2007-10-15 20:41:53 +00002873}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002874
Chris Lattner5426bf62008-04-07 07:01:58 +00002875//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002876// Integer Predicates
2877//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00002878
Eli Friedmanad74a752008-06-28 06:23:08 +00002879unsigned ASTContext::getIntWidth(QualType T) {
2880 if (T == BoolTy)
2881 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002882 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2883 return FWIT->getWidth();
2884 }
2885 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00002886 return (unsigned)getTypeSize(T);
2887}
2888
2889QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2890 assert(T->isSignedIntegerType() && "Unexpected type");
2891 if (const EnumType* ETy = T->getAsEnumType())
2892 T = ETy->getDecl()->getIntegerType();
2893 const BuiltinType* BTy = T->getAsBuiltinType();
2894 assert (BTy && "Unexpected signed integer type");
2895 switch (BTy->getKind()) {
2896 case BuiltinType::Char_S:
2897 case BuiltinType::SChar:
2898 return UnsignedCharTy;
2899 case BuiltinType::Short:
2900 return UnsignedShortTy;
2901 case BuiltinType::Int:
2902 return UnsignedIntTy;
2903 case BuiltinType::Long:
2904 return UnsignedLongTy;
2905 case BuiltinType::LongLong:
2906 return UnsignedLongLongTy;
2907 default:
2908 assert(0 && "Unexpected signed integer type");
2909 return QualType();
2910 }
2911}
2912
2913
2914//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002915// Serialization Support
2916//===----------------------------------------------------------------------===//
2917
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002918/// Emit - Serialize an ASTContext object to Bitcode.
2919void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002920 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002921 S.EmitRef(SourceMgr);
2922 S.EmitRef(Target);
2923 S.EmitRef(Idents);
2924 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002925
Ted Kremenekfee04522007-10-31 22:44:07 +00002926 // Emit the size of the type vector so that we can reserve that size
2927 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002928 S.EmitInt(Types.size());
2929
Ted Kremenek03ed4402007-11-13 22:02:55 +00002930 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2931 I!=E;++I)
2932 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002933
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002934 S.EmitOwnedPtr(TUDecl);
2935
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002936 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002937}
2938
Ted Kremenek0f84c002007-11-13 00:25:37 +00002939ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002940
2941 // Read the language options.
2942 LangOptions LOpts;
2943 LOpts.Read(D);
2944
Ted Kremenekfee04522007-10-31 22:44:07 +00002945 SourceManager &SM = D.ReadRef<SourceManager>();
2946 TargetInfo &t = D.ReadRef<TargetInfo>();
2947 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2948 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002949
Ted Kremenekfee04522007-10-31 22:44:07 +00002950 unsigned size_reserve = D.ReadInt();
2951
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002952 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2953 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002954
Ted Kremenek03ed4402007-11-13 22:02:55 +00002955 for (unsigned i = 0; i < size_reserve; ++i)
2956 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002957
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002958 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2959
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002960 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002961
2962 return A;
2963}