blob: 2b4bc89aacef7eca8638b12bcbaa27c25f4f3da2 [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:
Eli Friedman4bdf0872009-02-22 04:02:33 +0000390 case Type::ObjCQualifiedClass:
Chris Lattner5426bf62008-04-07 07:01:58 +0000391 Width = Target.getPointerWidth(0);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000392 Align = Target.getPointerAlign(0);
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000393 break;
Steve Naroff485eeff2008-09-24 15:05:44 +0000394 case Type::BlockPointer: {
395 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
396 Width = Target.getPointerWidth(AS);
397 Align = Target.getPointerAlign(AS);
398 break;
399 }
Chris Lattnerf72a4432008-03-08 08:34:58 +0000400 case Type::Pointer: {
401 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner5426bf62008-04-07 07:01:58 +0000402 Width = Target.getPointerWidth(AS);
Chris Lattnerf72a4432008-03-08 08:34:58 +0000403 Align = Target.getPointerAlign(AS);
404 break;
405 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000406 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000407 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000408 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6f62c2a2007-12-19 19:23:28 +0000409 // FIXME: This is wrong for struct layout: a reference in a struct has
410 // pointer size.
Chris Lattnerbdcd6372008-04-02 17:35:06 +0000411 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Sebastian Redlf30208a2009-01-24 21:16:55 +0000412 case Type::MemberPointer: {
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000413 // FIXME: This is not only platform- but also ABI-dependent. We follow
Sebastian Redlf30208a2009-01-24 21:16:55 +0000414 // the GCC ABI, where pointers to data are one pointer large, pointers to
415 // functions two pointers. But if we want to support ABI compatibility with
Sebastian Redl8edef7c2009-01-24 23:29:36 +0000416 // other compilers too, we need to delegate this completely to TargetInfo
417 // or some ABI abstraction layer.
Sebastian Redlf30208a2009-01-24 21:16:55 +0000418 QualType Pointee = cast<MemberPointerType>(T)->getPointeeType();
419 unsigned AS = Pointee.getAddressSpace();
420 Width = Target.getPointerWidth(AS);
421 if (Pointee->isFunctionType())
422 Width *= 2;
423 Align = Target.getPointerAlign(AS);
424 // GCC aligns at single pointer width.
425 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000426 case Type::Complex: {
427 // Complex types have the same alignment as their elements, but twice the
428 // size.
429 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner98be4942008-03-05 18:54:05 +0000430 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000431 Width = EltInfo.first*2;
Chris Lattner5d2a6302007-07-18 18:26:58 +0000432 Align = EltInfo.second;
433 break;
434 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000435 case Type::ObjCInterface: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000436 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel44a3dde2008-06-04 21:54:36 +0000437 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
438 Width = Layout.getSize();
439 Align = Layout.getAlignment();
440 break;
441 }
Chris Lattner71763312008-04-06 22:05:18 +0000442 case Type::Tagged: {
Daniel Dunbar1d751182008-11-08 05:48:37 +0000443 const TagType *TT = cast<TagType>(T);
444
445 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattner8389eab2008-08-09 21:35:13 +0000446 Width = 1;
447 Align = 1;
448 break;
449 }
450
Daniel Dunbar1d751182008-11-08 05:48:37 +0000451 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner71763312008-04-06 22:05:18 +0000452 return getTypeInfo(ET->getDecl()->getIntegerType());
453
Daniel Dunbar1d751182008-11-08 05:48:37 +0000454 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner71763312008-04-06 22:05:18 +0000455 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
456 Width = Layout.getSize();
457 Align = Layout.getAlignment();
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000458 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000459 }
Chris Lattner71763312008-04-06 22:05:18 +0000460 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000461
Chris Lattner464175b2007-07-18 17:52:12 +0000462 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner9e9b6dc2008-03-08 08:52:55 +0000463 return std::make_pair(Width, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000464}
465
Chris Lattner34ebde42009-01-27 18:08:34 +0000466/// getPreferredTypeAlign - Return the "preferred" alignment of the specified
467/// type for the current target in bits. This can be different than the ABI
468/// alignment in cases where it is beneficial for performance to overalign
469/// a data type.
470unsigned ASTContext::getPreferredTypeAlign(const Type *T) {
471 unsigned ABIAlign = getTypeAlign(T);
472
473 // Doubles should be naturally aligned if possible.
Daniel Dunbare00d5c02009-02-18 19:59:32 +0000474 if (T->isSpecificBuiltinType(BuiltinType::Double))
475 return std::max(ABIAlign, 64U);
Chris Lattner34ebde42009-01-27 18:08:34 +0000476
477 return ABIAlign;
478}
479
480
Devang Patel8b277042008-06-04 21:22:16 +0000481/// LayoutField - Field layout.
482void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000483 bool IsUnion, unsigned StructPacking,
Devang Patel8b277042008-06-04 21:22:16 +0000484 ASTContext &Context) {
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000485 unsigned FieldPacking = StructPacking;
Devang Patel8b277042008-06-04 21:22:16 +0000486 uint64_t FieldOffset = IsUnion ? 0 : Size;
487 uint64_t FieldSize;
488 unsigned FieldAlign;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000489
490 // FIXME: Should this override struct packing? Probably we want to
491 // take the minimum?
492 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
493 FieldPacking = PA->getAlignment();
Devang Patel8b277042008-06-04 21:22:16 +0000494
495 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
496 // TODO: Need to check this algorithm on other targets!
497 // (tested on Linux-X86)
Daniel Dunbar32442bb2008-08-13 23:47:13 +0000498 FieldSize =
499 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patel8b277042008-06-04 21:22:16 +0000500
501 std::pair<uint64_t, unsigned> FieldInfo =
502 Context.getTypeInfo(FD->getType());
503 uint64_t TypeSize = FieldInfo.first;
504
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000505 // Determine the alignment of this bitfield. The packing
506 // attributes define a maximum and the alignment attribute defines
507 // a minimum.
508 // FIXME: What is the right behavior when the specified alignment
509 // is smaller than the specified packing?
Devang Patel8b277042008-06-04 21:22:16 +0000510 FieldAlign = FieldInfo.second;
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000511 if (FieldPacking)
512 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patel8b277042008-06-04 21:22:16 +0000513 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
514 FieldAlign = std::max(FieldAlign, AA->getAlignment());
515
516 // Check if we need to add padding to give the field the correct
517 // alignment.
518 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
519 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
520
521 // Padding members don't affect overall alignment
522 if (!FD->getIdentifier())
523 FieldAlign = 1;
524 } else {
Chris Lattner8389eab2008-08-09 21:35:13 +0000525 if (FD->getType()->isIncompleteArrayType()) {
526 // This is a flexible array member; we can't directly
Devang Patel8b277042008-06-04 21:22:16 +0000527 // query getTypeInfo about these, so we figure it out here.
528 // Flexible array members don't have any size, but they
529 // have to be aligned appropriately for their element type.
530 FieldSize = 0;
Chris Lattnerc63a1f22008-08-04 07:31:14 +0000531 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patel8b277042008-06-04 21:22:16 +0000532 FieldAlign = Context.getTypeAlign(ATy->getElementType());
533 } else {
534 std::pair<uint64_t, unsigned> FieldInfo =
535 Context.getTypeInfo(FD->getType());
536 FieldSize = FieldInfo.first;
537 FieldAlign = FieldInfo.second;
538 }
539
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000540 // Determine the alignment of this bitfield. The packing
541 // attributes define a maximum and the alignment attribute defines
542 // a minimum. Additionally, the packing alignment must be at least
543 // a byte for non-bitfields.
544 //
545 // FIXME: What is the right behavior when the specified alignment
546 // is smaller than the specified packing?
547 if (FieldPacking)
548 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patel8b277042008-06-04 21:22:16 +0000549 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
550 FieldAlign = std::max(FieldAlign, AA->getAlignment());
551
552 // Round up the current record size to the field's alignment boundary.
553 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
554 }
555
556 // Place this field at the current location.
557 FieldOffsets[FieldNo] = FieldOffset;
558
559 // Reserve space for this field.
560 if (IsUnion) {
561 Size = std::max(Size, FieldSize);
562 } else {
563 Size = FieldOffset + FieldSize;
564 }
565
566 // Remember max struct/class alignment.
567 Alignment = std::max(Alignment, FieldAlign);
568}
569
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000570static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
571 std::vector<FieldDecl*> &Fields) {
572 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
573 if (SuperClass)
574 CollectObjCIvars(SuperClass, Fields);
575 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
576 E = OI->ivar_end(); I != E; ++I) {
577 ObjCIvarDecl *IVDecl = (*I);
578 if (!IVDecl->isInvalidDecl())
579 Fields.push_back(cast<FieldDecl>(IVDecl));
580 }
581}
582
583/// addRecordToClass - produces record info. for the class for its
584/// ivars and all those inherited.
585///
586const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
587{
588 const RecordDecl *&RD = ASTRecordForInterface[D];
589 if (RD)
590 return RD;
591 std::vector<FieldDecl*> RecFields;
592 CollectObjCIvars(D, RecFields);
593 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
594 D->getLocation(),
595 D->getIdentifier());
596 /// FIXME! Can do collection of ivars and adding to the record while
597 /// doing it.
598 for (unsigned int i = 0; i != RecFields.size(); i++) {
599 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
600 RecFields[i]->getLocation(),
601 RecFields[i]->getIdentifier(),
602 RecFields[i]->getType(),
Douglas Gregor4afa39d2009-01-20 01:17:11 +0000603 RecFields[i]->getBitWidth(), false);
Douglas Gregor482b77d2009-01-12 23:27:07 +0000604 NewRD->addDecl(Field);
Fariborz Jahaniana769c002008-12-17 21:40:49 +0000605 }
606 NewRD->completeDefinition(*this);
607 RD = NewRD;
608 return RD;
609}
Devang Patel44a3dde2008-06-04 21:54:36 +0000610
Fariborz Jahanianefc4c4b2008-12-18 17:29:46 +0000611/// setFieldDecl - maps a field for the given Ivar reference node.
612//
613void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
614 const ObjCIvarDecl *Ivar,
615 const ObjCIvarRefExpr *MRef) {
616 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
617 lookupFieldDeclForIvar(*this, Ivar);
618 ASTFieldForIvarRef[MRef] = FD;
619}
620
Chris Lattner61710852008-10-05 17:34:18 +0000621/// getASTObjcInterfaceLayout - Get or compute information about the layout of
622/// the specified Objective C, which indicates its size and ivar
Devang Patel44a3dde2008-06-04 21:54:36 +0000623/// position information.
624const ASTRecordLayout &
625ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
626 // Look up this layout, if already laid out, return what we have.
627 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
628 if (Entry) return *Entry;
629
630 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
631 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel6a5a34c2008-06-06 02:14:01 +0000632 ASTRecordLayout *NewEntry = NULL;
633 unsigned FieldCount = D->ivar_size();
634 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
635 FieldCount++;
636 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
637 unsigned Alignment = SL.getAlignment();
638 uint64_t Size = SL.getSize();
639 NewEntry = new ASTRecordLayout(Size, Alignment);
640 NewEntry->InitializeLayout(FieldCount);
Chris Lattner61710852008-10-05 17:34:18 +0000641 // Super class is at the beginning of the layout.
642 NewEntry->SetFieldOffset(0, 0);
Devang Patel6a5a34c2008-06-06 02:14:01 +0000643 } else {
644 NewEntry = new ASTRecordLayout();
645 NewEntry->InitializeLayout(FieldCount);
646 }
Devang Patel44a3dde2008-06-04 21:54:36 +0000647 Entry = NewEntry;
648
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000649 unsigned StructPacking = 0;
650 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
651 StructPacking = PA->getAlignment();
Devang Patel44a3dde2008-06-04 21:54:36 +0000652
653 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
654 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
655 AA->getAlignment()));
656
657 // Layout each ivar sequentially.
658 unsigned i = 0;
659 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
660 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
661 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000662 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel44a3dde2008-06-04 21:54:36 +0000663 }
664
665 // Finally, round the size of the total struct up to the alignment of the
666 // struct itself.
667 NewEntry->FinalizeLayout();
668 return *NewEntry;
669}
670
Devang Patel88a981b2007-11-01 19:11:01 +0000671/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000672/// specified record (struct/union/class), which indicates its size and field
673/// position information.
Chris Lattner98be4942008-03-05 18:54:05 +0000674const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek4b7c9832008-09-05 17:16:31 +0000675 D = D->getDefinition(*this);
676 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman4bd998b2008-05-30 09:31:38 +0000677
Chris Lattner464175b2007-07-18 17:52:12 +0000678 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000679 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000680 if (Entry) return *Entry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000681
Devang Patel88a981b2007-11-01 19:11:01 +0000682 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
683 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
684 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000685 Entry = NewEntry;
Eli Friedman4bd998b2008-05-30 09:31:38 +0000686
Douglas Gregore267ff32008-12-11 20:41:00 +0000687 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor44b43212008-12-11 16:49:14 +0000688 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +0000689 bool IsUnion = D->isUnion();
Chris Lattner464175b2007-07-18 17:52:12 +0000690
Daniel Dunbar3b0db902008-10-16 02:34:03 +0000691 unsigned StructPacking = 0;
692 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
693 StructPacking = PA->getAlignment();
694
Eli Friedman4bd998b2008-05-30 09:31:38 +0000695 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patel8b277042008-06-04 21:22:16 +0000696 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
697 AA->getAlignment()));
Anders Carlsson8af226a2008-02-18 07:13:09 +0000698
Eli Friedman4bd998b2008-05-30 09:31:38 +0000699 // Layout each field, for now, just sequentially, respecting alignment. In
700 // the future, this will need to be tweakable by targets.
Douglas Gregor44b43212008-12-11 16:49:14 +0000701 unsigned FieldIdx = 0;
Douglas Gregorf8d49f62009-01-09 17:18:27 +0000702 for (RecordDecl::field_iterator Field = D->field_begin(),
703 FieldEnd = D->field_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000704 Field != FieldEnd; (void)++Field, ++FieldIdx)
705 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman4bd998b2008-05-30 09:31:38 +0000706
707 // Finally, round the size of the total struct up to the alignment of the
708 // struct itself.
Devang Patel8b277042008-06-04 21:22:16 +0000709 NewEntry->FinalizeLayout();
Chris Lattner5d2a6302007-07-18 18:26:58 +0000710 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000711}
712
Chris Lattnera7674d82007-07-13 22:13:22 +0000713//===----------------------------------------------------------------------===//
714// Type creation/memoization methods
715//===----------------------------------------------------------------------===//
716
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000717QualType ASTContext::getAddrSpaceQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000718 QualType CanT = getCanonicalType(T);
719 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattnerf46699c2008-02-20 20:55:12 +0000720 return T;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000721
722 // If we are composing extended qualifiers together, merge together into one
723 // ExtQualType node.
724 unsigned CVRQuals = T.getCVRQualifiers();
725 QualType::GCAttrTypes GCAttr = QualType::GCNone;
726 Type *TypeNode = T.getTypePtr();
Chris Lattnerf46699c2008-02-20 20:55:12 +0000727
Chris Lattnerb7d25532009-02-18 22:53:11 +0000728 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
729 // If this type already has an address space specified, it cannot get
730 // another one.
731 assert(EQT->getAddressSpace() == 0 &&
732 "Type cannot be in multiple addr spaces!");
733 GCAttr = EQT->getObjCGCAttr();
734 TypeNode = EQT->getBaseType();
735 }
Chris Lattnerf46699c2008-02-20 20:55:12 +0000736
Chris Lattnerb7d25532009-02-18 22:53:11 +0000737 // Check if we've already instantiated this type.
Christopher Lambebb97e92008-02-04 02:31:56 +0000738 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000739 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Christopher Lambebb97e92008-02-04 02:31:56 +0000740 void *InsertPos = 0;
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000741 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000742 return QualType(EXTQy, CVRQuals);
743
Christopher Lambebb97e92008-02-04 02:31:56 +0000744 // If the base type isn't canonical, this won't be a canonical type either,
745 // so fill in the canonical type field.
746 QualType Canonical;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000747 if (!TypeNode->isCanonical()) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000748 Canonical = getAddrSpaceQualType(CanT, AddressSpace);
Christopher Lambebb97e92008-02-04 02:31:56 +0000749
Chris Lattnerb7d25532009-02-18 22:53:11 +0000750 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000751 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000752 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lambebb97e92008-02-04 02:31:56 +0000753 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000754 ExtQualType *New =
755 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahanianf11284a2009-02-17 18:27:45 +0000756 ExtQualTypes.InsertNode(New, InsertPos);
Christopher Lambebb97e92008-02-04 02:31:56 +0000757 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000758 return QualType(New, CVRQuals);
Christopher Lambebb97e92008-02-04 02:31:56 +0000759}
760
Chris Lattnerb7d25532009-02-18 22:53:11 +0000761QualType ASTContext::getObjCGCQualType(QualType T,
762 QualType::GCAttrTypes GCAttr) {
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000763 QualType CanT = getCanonicalType(T);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000764 if (CanT.getObjCGCAttr() == GCAttr)
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000765 return T;
766
Chris Lattnerb7d25532009-02-18 22:53:11 +0000767 // If we are composing extended qualifiers together, merge together into one
768 // ExtQualType node.
769 unsigned CVRQuals = T.getCVRQualifiers();
770 Type *TypeNode = T.getTypePtr();
771 unsigned AddressSpace = 0;
772
773 if (ExtQualType *EQT = dyn_cast<ExtQualType>(TypeNode)) {
774 // If this type already has an address space specified, it cannot get
775 // another one.
776 assert(EQT->getObjCGCAttr() == QualType::GCNone &&
777 "Type cannot be in multiple addr spaces!");
778 AddressSpace = EQT->getAddressSpace();
779 TypeNode = EQT->getBaseType();
780 }
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000781
782 // Check if we've already instantiated an gc qual'd type of this type.
783 llvm::FoldingSetNodeID ID;
Chris Lattnerb7d25532009-02-18 22:53:11 +0000784 ExtQualType::Profile(ID, TypeNode, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000785 void *InsertPos = 0;
786 if (ExtQualType *EXTQy = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattnerb7d25532009-02-18 22:53:11 +0000787 return QualType(EXTQy, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000788
789 // If the base type isn't canonical, this won't be a canonical type either,
790 // so fill in the canonical type field.
791 QualType Canonical;
792 if (!T->isCanonical()) {
Chris Lattnerb7d25532009-02-18 22:53:11 +0000793 Canonical = getObjCGCQualType(CanT, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000794
Chris Lattnerb7d25532009-02-18 22:53:11 +0000795 // Update InsertPos, the previous call could have invalidated it.
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000796 ExtQualType *NewIP = ExtQualTypes.FindNodeOrInsertPos(ID, InsertPos);
797 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
798 }
Chris Lattnerb7d25532009-02-18 22:53:11 +0000799 ExtQualType *New =
800 new (*this, 8) ExtQualType(TypeNode, Canonical, AddressSpace, GCAttr);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000801 ExtQualTypes.InsertNode(New, InsertPos);
802 Types.push_back(New);
Chris Lattnerb7d25532009-02-18 22:53:11 +0000803 return QualType(New, CVRQuals);
Fariborz Jahaniand33d9c02009-02-18 05:09:49 +0000804}
Chris Lattnera7674d82007-07-13 22:13:22 +0000805
Reid Spencer5f016e22007-07-11 17:01:13 +0000806/// getComplexType - Return the uniqued reference to the type for a complex
807/// number with the specified element type.
808QualType ASTContext::getComplexType(QualType T) {
809 // Unique pointers, to guarantee there is only one pointer of a particular
810 // structure.
811 llvm::FoldingSetNodeID ID;
812 ComplexType::Profile(ID, T);
813
814 void *InsertPos = 0;
815 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
816 return QualType(CT, 0);
817
818 // If the pointee type isn't canonical, this won't be a canonical type either,
819 // so fill in the canonical type field.
820 QualType Canonical;
821 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000822 Canonical = getComplexType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000823
824 // Get the new insert position for the node we care about.
825 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000826 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000827 }
Steve Narofff83820b2009-01-27 22:08:43 +0000828 ComplexType *New = new (*this,8) ComplexType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 Types.push_back(New);
830 ComplexTypes.InsertNode(New, InsertPos);
831 return QualType(New, 0);
832}
833
Eli Friedmanf98aba32009-02-13 02:31:07 +0000834QualType ASTContext::getFixedWidthIntType(unsigned Width, bool Signed) {
835 llvm::DenseMap<unsigned, FixedWidthIntType*> &Map = Signed ?
836 SignedFixedWidthIntTypes : UnsignedFixedWidthIntTypes;
837 FixedWidthIntType *&Entry = Map[Width];
838 if (!Entry)
839 Entry = new FixedWidthIntType(Width, Signed);
840 return QualType(Entry, 0);
841}
Reid Spencer5f016e22007-07-11 17:01:13 +0000842
843/// getPointerType - Return the uniqued reference to the type for a pointer to
844/// the specified type.
845QualType ASTContext::getPointerType(QualType T) {
846 // Unique pointers, to guarantee there is only one pointer of a particular
847 // structure.
848 llvm::FoldingSetNodeID ID;
849 PointerType::Profile(ID, T);
850
851 void *InsertPos = 0;
852 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
853 return QualType(PT, 0);
854
855 // If the pointee type isn't canonical, this won't be a canonical type either,
856 // so fill in the canonical type field.
857 QualType Canonical;
858 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000859 Canonical = getPointerType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000860
861 // Get the new insert position for the node we care about.
862 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000863 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000864 }
Steve Narofff83820b2009-01-27 22:08:43 +0000865 PointerType *New = new (*this,8) PointerType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 Types.push_back(New);
867 PointerTypes.InsertNode(New, InsertPos);
868 return QualType(New, 0);
869}
870
Steve Naroff5618bd42008-08-27 16:04:49 +0000871/// getBlockPointerType - Return the uniqued reference to the type for
872/// a pointer to the specified block.
873QualType ASTContext::getBlockPointerType(QualType T) {
Steve Naroff296e8d52008-08-28 19:20:44 +0000874 assert(T->isFunctionType() && "block of function types only");
875 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff5618bd42008-08-27 16:04:49 +0000876 // structure.
877 llvm::FoldingSetNodeID ID;
878 BlockPointerType::Profile(ID, T);
879
880 void *InsertPos = 0;
881 if (BlockPointerType *PT =
882 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
883 return QualType(PT, 0);
884
Steve Naroff296e8d52008-08-28 19:20:44 +0000885 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff5618bd42008-08-27 16:04:49 +0000886 // type either so fill in the canonical type field.
887 QualType Canonical;
888 if (!T->isCanonical()) {
889 Canonical = getBlockPointerType(getCanonicalType(T));
890
891 // Get the new insert position for the node we care about.
892 BlockPointerType *NewIP =
893 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000894 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff5618bd42008-08-27 16:04:49 +0000895 }
Steve Narofff83820b2009-01-27 22:08:43 +0000896 BlockPointerType *New = new (*this,8) BlockPointerType(T, Canonical);
Steve Naroff5618bd42008-08-27 16:04:49 +0000897 Types.push_back(New);
898 BlockPointerTypes.InsertNode(New, InsertPos);
899 return QualType(New, 0);
900}
901
Reid Spencer5f016e22007-07-11 17:01:13 +0000902/// getReferenceType - Return the uniqued reference to the type for a reference
903/// to the specified type.
904QualType ASTContext::getReferenceType(QualType T) {
905 // Unique pointers, to guarantee there is only one pointer of a particular
906 // structure.
907 llvm::FoldingSetNodeID ID;
908 ReferenceType::Profile(ID, T);
909
910 void *InsertPos = 0;
911 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
912 return QualType(RT, 0);
913
914 // If the referencee type isn't canonical, this won't be a canonical type
915 // either, so fill in the canonical type field.
916 QualType Canonical;
917 if (!T->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000918 Canonical = getReferenceType(getCanonicalType(T));
Reid Spencer5f016e22007-07-11 17:01:13 +0000919
920 // Get the new insert position for the node we care about.
921 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000922 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 }
924
Steve Narofff83820b2009-01-27 22:08:43 +0000925 ReferenceType *New = new (*this,8) ReferenceType(T, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +0000926 Types.push_back(New);
927 ReferenceTypes.InsertNode(New, InsertPos);
928 return QualType(New, 0);
929}
930
Sebastian Redlf30208a2009-01-24 21:16:55 +0000931/// getMemberPointerType - Return the uniqued reference to the type for a
932/// member pointer to the specified type, in the specified class.
933QualType ASTContext::getMemberPointerType(QualType T, const Type *Cls)
934{
935 // Unique pointers, to guarantee there is only one pointer of a particular
936 // structure.
937 llvm::FoldingSetNodeID ID;
938 MemberPointerType::Profile(ID, T, Cls);
939
940 void *InsertPos = 0;
941 if (MemberPointerType *PT =
942 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
943 return QualType(PT, 0);
944
945 // If the pointee or class type isn't canonical, this won't be a canonical
946 // type either, so fill in the canonical type field.
947 QualType Canonical;
948 if (!T->isCanonical()) {
949 Canonical = getMemberPointerType(getCanonicalType(T),getCanonicalType(Cls));
950
951 // Get the new insert position for the node we care about.
952 MemberPointerType *NewIP =
953 MemberPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
954 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
955 }
Steve Narofff83820b2009-01-27 22:08:43 +0000956 MemberPointerType *New = new (*this,8) MemberPointerType(T, Cls, Canonical);
Sebastian Redlf30208a2009-01-24 21:16:55 +0000957 Types.push_back(New);
958 MemberPointerTypes.InsertNode(New, InsertPos);
959 return QualType(New, 0);
960}
961
Steve Narofffb22d962007-08-30 01:06:46 +0000962/// getConstantArrayType - Return the unique reference to the type for an
963/// array of the specified element type.
964QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000965 const llvm::APInt &ArySize,
966 ArrayType::ArraySizeModifier ASM,
967 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000968 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +0000969 ConstantArrayType::Profile(ID, EltTy, ArySize, ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000970
971 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000972 if (ConstantArrayType *ATP =
973 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 return QualType(ATP, 0);
975
976 // If the element type isn't canonical, this won't be a canonical type either,
977 // so fill in the canonical type field.
978 QualType Canonical;
979 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +0000980 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroffc9406122007-08-30 18:10:14 +0000981 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000983 ConstantArrayType *NewIP =
984 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +0000985 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 }
987
Ted Kremenek566c2ba2009-01-19 21:31:22 +0000988 ConstantArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +0000989 new(*this,8)ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000990 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 Types.push_back(New);
992 return QualType(New, 0);
993}
994
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000995/// getVariableArrayType - Returns a non-unique reference to the type for a
996/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000997QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
998 ArrayType::ArraySizeModifier ASM,
999 unsigned EltTypeQuals) {
Eli Friedmanc5773c42008-02-15 18:16:39 +00001000 // Since we don't unique expressions, it isn't possible to unique VLA's
1001 // that have an expression provided for their size.
1002
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001003 VariableArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001004 new(*this,8)VariableArrayType(EltTy,QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001005
1006 VariableArrayTypes.push_back(New);
1007 Types.push_back(New);
1008 return QualType(New, 0);
1009}
1010
Douglas Gregor898574e2008-12-05 23:32:09 +00001011/// getDependentSizedArrayType - Returns a non-unique reference to
1012/// the type for a dependently-sized array of the specified element
1013/// type. FIXME: We will need these to be uniqued, or at least
1014/// comparable, at some point.
1015QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
1016 ArrayType::ArraySizeModifier ASM,
1017 unsigned EltTypeQuals) {
1018 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
1019 "Size must be type- or value-dependent!");
1020
1021 // Since we don't unique expressions, it isn't possible to unique
1022 // dependently-sized array types.
1023
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001024 DependentSizedArrayType *New =
Steve Narofff83820b2009-01-27 22:08:43 +00001025 new (*this,8) DependentSizedArrayType(EltTy, QualType(), NumElts,
1026 ASM, EltTypeQuals);
Douglas Gregor898574e2008-12-05 23:32:09 +00001027
1028 DependentSizedArrayTypes.push_back(New);
1029 Types.push_back(New);
1030 return QualType(New, 0);
1031}
1032
Eli Friedmanc5773c42008-02-15 18:16:39 +00001033QualType ASTContext::getIncompleteArrayType(QualType EltTy,
1034 ArrayType::ArraySizeModifier ASM,
1035 unsigned EltTypeQuals) {
1036 llvm::FoldingSetNodeID ID;
Chris Lattner0be2ef22009-02-19 17:31:02 +00001037 IncompleteArrayType::Profile(ID, EltTy, ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001038
1039 void *InsertPos = 0;
1040 if (IncompleteArrayType *ATP =
1041 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
1042 return QualType(ATP, 0);
1043
1044 // If the element type isn't canonical, this won't be a canonical type
1045 // either, so fill in the canonical type field.
1046 QualType Canonical;
1047
1048 if (!EltTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001049 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001050 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001051
1052 // Get the new insert position for the node we care about.
1053 IncompleteArrayType *NewIP =
1054 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001055 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek2bd24ba2007-10-29 23:37:31 +00001056 }
Eli Friedmanc5773c42008-02-15 18:16:39 +00001057
Steve Narofff83820b2009-01-27 22:08:43 +00001058 IncompleteArrayType *New = new (*this,8) IncompleteArrayType(EltTy, Canonical,
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001059 ASM, EltTypeQuals);
Eli Friedmanc5773c42008-02-15 18:16:39 +00001060
1061 IncompleteArrayTypes.InsertNode(New, InsertPos);
1062 Types.push_back(New);
1063 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +00001064}
1065
Steve Naroff73322922007-07-18 18:00:27 +00001066/// getVectorType - Return the unique reference to a vector type of
1067/// the specified element type and size. VectorType must be a built-in type.
1068QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 BuiltinType *baseType;
1070
Chris Lattnerf52ab252008-04-06 22:59:24 +00001071 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +00001072 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001073
1074 // Check if we've already instantiated a vector of this type.
1075 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +00001076 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +00001077 void *InsertPos = 0;
1078 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1079 return QualType(VTP, 0);
1080
1081 // If the element type isn't canonical, this won't be a canonical type either,
1082 // so fill in the canonical type field.
1083 QualType Canonical;
1084 if (!vecType->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001085 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +00001086
1087 // Get the new insert position for the node we care about.
1088 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001089 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 }
Steve Narofff83820b2009-01-27 22:08:43 +00001091 VectorType *New = new (*this,8) VectorType(vecType, NumElts, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001092 VectorTypes.InsertNode(New, InsertPos);
1093 Types.push_back(New);
1094 return QualType(New, 0);
1095}
1096
Nate Begeman213541a2008-04-18 23:10:10 +00001097/// getExtVectorType - Return the unique reference to an extended vector type of
Steve Naroff73322922007-07-18 18:00:27 +00001098/// the specified element type and size. VectorType must be a built-in type.
Nate Begeman213541a2008-04-18 23:10:10 +00001099QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff73322922007-07-18 18:00:27 +00001100 BuiltinType *baseType;
1101
Chris Lattnerf52ab252008-04-06 22:59:24 +00001102 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begeman213541a2008-04-18 23:10:10 +00001103 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Steve Naroff73322922007-07-18 18:00:27 +00001104
1105 // Check if we've already instantiated a vector of this type.
1106 llvm::FoldingSetNodeID ID;
Nate Begeman213541a2008-04-18 23:10:10 +00001107 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Steve Naroff73322922007-07-18 18:00:27 +00001108 void *InsertPos = 0;
1109 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
1110 return QualType(VTP, 0);
1111
1112 // If the element type isn't canonical, this won't be a canonical type either,
1113 // so fill in the canonical type field.
1114 QualType Canonical;
1115 if (!vecType->isCanonical()) {
Nate Begeman213541a2008-04-18 23:10:10 +00001116 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Steve Naroff73322922007-07-18 18:00:27 +00001117
1118 // Get the new insert position for the node we care about.
1119 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001120 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff73322922007-07-18 18:00:27 +00001121 }
Steve Narofff83820b2009-01-27 22:08:43 +00001122 ExtVectorType *New = new (*this,8) ExtVectorType(vecType, NumElts, Canonical);
Steve Naroff73322922007-07-18 18:00:27 +00001123 VectorTypes.InsertNode(New, InsertPos);
1124 Types.push_back(New);
1125 return QualType(New, 0);
1126}
1127
Reid Spencer5f016e22007-07-11 17:01:13 +00001128/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
1129///
1130QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
1131 // Unique functions, to guarantee there is only one function of a particular
1132 // structure.
1133 llvm::FoldingSetNodeID ID;
1134 FunctionTypeNoProto::Profile(ID, ResultTy);
1135
1136 void *InsertPos = 0;
1137 if (FunctionTypeNoProto *FT =
1138 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
1139 return QualType(FT, 0);
1140
1141 QualType Canonical;
1142 if (!ResultTy->isCanonical()) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001143 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Reid Spencer5f016e22007-07-11 17:01:13 +00001144
1145 // Get the new insert position for the node we care about.
1146 FunctionTypeNoProto *NewIP =
1147 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001148 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001149 }
1150
Steve Narofff83820b2009-01-27 22:08:43 +00001151 FunctionTypeNoProto *New =new(*this,8)FunctionTypeNoProto(ResultTy,Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001152 Types.push_back(New);
Eli Friedman56cd7e32008-02-25 22:11:40 +00001153 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 return QualType(New, 0);
1155}
1156
1157/// getFunctionType - Return a normal function type with a typed argument
1158/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner61710852008-10-05 17:34:18 +00001159QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001160 unsigned NumArgs, bool isVariadic,
1161 unsigned TypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001162 // Unique functions, to guarantee there is only one function of a particular
1163 // structure.
1164 llvm::FoldingSetNodeID ID;
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001165 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1166 TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001167
1168 void *InsertPos = 0;
1169 if (FunctionTypeProto *FTP =
1170 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1171 return QualType(FTP, 0);
1172
1173 // Determine whether the type being created is already canonical or not.
1174 bool isCanonical = ResultTy->isCanonical();
1175 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1176 if (!ArgArray[i]->isCanonical())
1177 isCanonical = false;
1178
1179 // If this type isn't canonical, get the canonical version of it.
1180 QualType Canonical;
1181 if (!isCanonical) {
1182 llvm::SmallVector<QualType, 16> CanonicalArgs;
1183 CanonicalArgs.reserve(NumArgs);
1184 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerf52ab252008-04-06 22:59:24 +00001185 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Reid Spencer5f016e22007-07-11 17:01:13 +00001186
Chris Lattnerf52ab252008-04-06 22:59:24 +00001187 Canonical = getFunctionType(getCanonicalType(ResultTy),
Reid Spencer5f016e22007-07-11 17:01:13 +00001188 &CanonicalArgs[0], NumArgs,
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00001189 isVariadic, TypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +00001190
1191 // Get the new insert position for the node we care about.
1192 FunctionTypeProto *NewIP =
1193 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattnerf6e764f2008-10-12 00:26:57 +00001194 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 }
1196
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001197 // FunctionTypeProto objects are allocated with extra bytes after them
1198 // for a variable size array (for parameter types) at the end of them.
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 FunctionTypeProto *FTP =
Steve Naroffc0ac4922009-01-27 23:20:32 +00001200 (FunctionTypeProto*)Allocate(sizeof(FunctionTypeProto) +
1201 NumArgs*sizeof(QualType), 8);
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00001203 TypeQuals, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 Types.push_back(FTP);
1205 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1206 return QualType(FTP, 0);
1207}
1208
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001209/// getTypeDeclType - Return the unique reference to the type for the
1210/// specified type declaration.
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001211QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001212 assert(Decl && "Passed null for Decl param");
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001213 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1214
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001215 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001216 return getTypedefType(Typedef);
Douglas Gregorfab9d672009-02-05 23:33:38 +00001217 else if (isa<TemplateTypeParmDecl>(Decl)) {
1218 assert(false && "Template type parameter types are always available.");
1219 } else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001220 return getObjCInterfaceType(ObjCInterface);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001221
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001222 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001223 if (PrevDecl)
1224 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001225 else
1226 Decl->TypeForDecl = new (*this,8) CXXRecordType(CXXRecord);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001227 }
Argyrios Kyrtzidis1e6759e2008-10-16 16:50:47 +00001228 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001229 if (PrevDecl)
1230 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001231 else
1232 Decl->TypeForDecl = new (*this,8) RecordType(Record);
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001233 }
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001234 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1235 if (PrevDecl)
1236 Decl->TypeForDecl = PrevDecl->TypeForDecl;
Steve Narofff83820b2009-01-27 22:08:43 +00001237 else
1238 Decl->TypeForDecl = new (*this,8) EnumType(Enum);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001239 }
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001240 else
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001241 assert(false && "TypeDecl without a type?");
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001242
Ted Kremenek4b7c9832008-09-05 17:16:31 +00001243 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argyrios Kyrtzidis49aa7ff2008-08-07 20:55:28 +00001244 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001245}
1246
Reid Spencer5f016e22007-07-11 17:01:13 +00001247/// getTypedefType - Return the unique reference to the type for the
1248/// specified typename decl.
1249QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1250 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1251
Chris Lattnerf52ab252008-04-06 22:59:24 +00001252 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Steve Narofff83820b2009-01-27 22:08:43 +00001253 Decl->TypeForDecl = new(*this,8) TypedefType(Type::TypeName, Decl, Canonical);
Reid Spencer5f016e22007-07-11 17:01:13 +00001254 Types.push_back(Decl->TypeForDecl);
1255 return QualType(Decl->TypeForDecl, 0);
1256}
1257
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001258/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff3536b442007-09-06 21:24:23 +00001259/// specified ObjC interface decl.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001260QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff3536b442007-09-06 21:24:23 +00001261 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1262
Steve Narofff83820b2009-01-27 22:08:43 +00001263 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +00001264 Types.push_back(Decl->TypeForDecl);
1265 return QualType(Decl->TypeForDecl, 0);
1266}
1267
Fariborz Jahanianf3710ba2009-02-14 20:13:28 +00001268/// buildObjCInterfaceType - Returns a new type for the interface
1269/// declaration, regardless. It also removes any previously built
1270/// record declaration so caller can rebuild it.
1271QualType ASTContext::buildObjCInterfaceType(ObjCInterfaceDecl *Decl) {
1272 const RecordDecl *&RD = ASTRecordForInterface[Decl];
1273 if (RD)
1274 RD = 0;
1275 Decl->TypeForDecl = new(*this,8) ObjCInterfaceType(Type::ObjCInterface, Decl);
1276 Types.push_back(Decl->TypeForDecl);
1277 return QualType(Decl->TypeForDecl, 0);
1278}
1279
Douglas Gregorfab9d672009-02-05 23:33:38 +00001280/// \brief Retrieve the template type parameter type for a template
1281/// parameter with the given depth, index, and (optionally) name.
1282QualType ASTContext::getTemplateTypeParmType(unsigned Depth, unsigned Index,
1283 IdentifierInfo *Name) {
1284 llvm::FoldingSetNodeID ID;
1285 TemplateTypeParmType::Profile(ID, Depth, Index, Name);
1286 void *InsertPos = 0;
1287 TemplateTypeParmType *TypeParm
1288 = TemplateTypeParmTypes.FindNodeOrInsertPos(ID, InsertPos);
1289
1290 if (TypeParm)
1291 return QualType(TypeParm, 0);
1292
1293 if (Name)
1294 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index, Name,
1295 getTemplateTypeParmType(Depth, Index));
1296 else
1297 TypeParm = new (*this, 8) TemplateTypeParmType(Depth, Index);
1298
1299 Types.push_back(TypeParm);
1300 TemplateTypeParmTypes.InsertNode(TypeParm, InsertPos);
1301
1302 return QualType(TypeParm, 0);
1303}
1304
Douglas Gregor55f6b142009-02-09 18:46:07 +00001305QualType
1306ASTContext::getClassTemplateSpecializationType(TemplateDecl *Template,
1307 unsigned NumArgs,
1308 uintptr_t *Args, bool *ArgIsType,
1309 QualType Canon) {
Douglas Gregorfc705b82009-02-26 22:19:44 +00001310 Canon = getCanonicalType(Canon);
1311
Douglas Gregor55f6b142009-02-09 18:46:07 +00001312 llvm::FoldingSetNodeID ID;
1313 ClassTemplateSpecializationType::Profile(ID, Template, NumArgs, Args,
1314 ArgIsType);
1315 void *InsertPos = 0;
1316 ClassTemplateSpecializationType *Spec
1317 = ClassTemplateSpecializationTypes.FindNodeOrInsertPos(ID, InsertPos);
1318
1319 if (Spec)
1320 return QualType(Spec, 0);
1321
1322 void *Mem = Allocate(sizeof(ClassTemplateSpecializationType) +
1323 (sizeof(uintptr_t) *
1324 (ClassTemplateSpecializationType::
1325 getNumPackedWords(NumArgs) +
1326 NumArgs)), 8);
1327 Spec = new (Mem) ClassTemplateSpecializationType(Template, NumArgs, Args,
1328 ArgIsType, Canon);
1329 Types.push_back(Spec);
1330 ClassTemplateSpecializationTypes.InsertNode(Spec, InsertPos);
1331
1332 return QualType(Spec, 0);
1333}
1334
Chris Lattner88cb27a2008-04-07 04:56:42 +00001335/// CmpProtocolNames - Comparison predicate for sorting protocols
1336/// alphabetically.
1337static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1338 const ObjCProtocolDecl *RHS) {
Douglas Gregor2e1cd422008-11-17 14:58:09 +00001339 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattner88cb27a2008-04-07 04:56:42 +00001340}
1341
1342static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1343 unsigned &NumProtocols) {
1344 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1345
1346 // Sort protocols, keyed by name.
1347 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1348
1349 // Remove duplicates.
1350 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1351 NumProtocols = ProtocolsEnd-Protocols;
1352}
1353
1354
Chris Lattner065f0d72008-04-07 04:44:08 +00001355/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1356/// the given interface decl and the conforming protocol list.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001357QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1358 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001359 // Sort the protocol list alphabetically to canonicalize it.
1360 SortAndUniqueProtocols(Protocols, NumProtocols);
1361
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001362 llvm::FoldingSetNodeID ID;
Chris Lattnerb0489812008-04-07 06:38:24 +00001363 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001364
1365 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001366 if (ObjCQualifiedInterfaceType *QT =
1367 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001368 return QualType(QT, 0);
1369
1370 // No Match;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001371 ObjCQualifiedInterfaceType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001372 new (*this,8) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001373
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001374 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001375 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +00001376 return QualType(QType, 0);
1377}
1378
Chris Lattner88cb27a2008-04-07 04:56:42 +00001379/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1380/// and the conforming protocol list.
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001381QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001382 unsigned NumProtocols) {
Chris Lattner88cb27a2008-04-07 04:56:42 +00001383 // Sort the protocol list alphabetically to canonicalize it.
1384 SortAndUniqueProtocols(Protocols, NumProtocols);
1385
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001386 llvm::FoldingSetNodeID ID;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001387 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001388
1389 void *InsertPos = 0;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001390 if (ObjCQualifiedIdType *QT =
Chris Lattner62f5f7f2008-07-26 00:46:50 +00001391 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001392 return QualType(QT, 0);
1393
1394 // No Match;
Ted Kremenek566c2ba2009-01-19 21:31:22 +00001395 ObjCQualifiedIdType *QType =
Steve Narofff83820b2009-01-27 22:08:43 +00001396 new (*this,8) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001397 Types.push_back(QType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001398 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahanianc5692492007-12-17 21:03:50 +00001399 return QualType(QType, 0);
1400}
1401
Steve Naroff9752f252007-08-01 18:02:17 +00001402/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1403/// TypeOfExpr AST's (since expression's are never shared). For example,
1404/// multiple declarations that refer to "typeof(x)" all contain different
1405/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1406/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +00001407QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001408 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Narofff83820b2009-01-27 22:08:43 +00001409 TypeOfExpr *toe = new (*this,8) TypeOfExpr(tofExpr, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001410 Types.push_back(toe);
1411 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001412}
1413
Steve Naroff9752f252007-08-01 18:02:17 +00001414/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1415/// TypeOfType AST's. The only motivation to unique these nodes would be
1416/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1417/// an issue. This doesn't effect the type checker, since it operates
1418/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +00001419QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001420 QualType Canonical = getCanonicalType(tofType);
Steve Narofff83820b2009-01-27 22:08:43 +00001421 TypeOfType *tot = new (*this,8) TypeOfType(tofType, Canonical);
Steve Naroff9752f252007-08-01 18:02:17 +00001422 Types.push_back(tot);
1423 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +00001424}
1425
Reid Spencer5f016e22007-07-11 17:01:13 +00001426/// getTagDeclType - Return the unique reference to the type for the
1427/// specified TagDecl (struct/union/class/enum) decl.
1428QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +00001429 assert (Decl);
Douglas Gregor2ce52f32008-04-13 21:07:44 +00001430 return getTypeDeclType(Decl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001431}
1432
1433/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1434/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1435/// needs to agree with the definition in <stddef.h>.
1436QualType ASTContext::getSizeType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001437 return getFromTargetType(Target.getSizeType());
Reid Spencer5f016e22007-07-11 17:01:13 +00001438}
1439
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001440/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfd888a52008-02-12 08:29:21 +00001441/// width of characters in wide strings, The value is target dependent and
1442/// needs to agree with the definition in <stddef.h>.
Argyrios Kyrtzidis55f4b022008-08-09 17:20:01 +00001443QualType ASTContext::getWCharType() const {
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001444 if (LangOpts.CPlusPlus)
1445 return WCharTy;
1446
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001447 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1448 // wide-character type?
1449 return getFromTargetType(Target.getWCharType());
Eli Friedmanfd888a52008-02-12 08:29:21 +00001450}
1451
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00001452/// getSignedWCharType - Return the type of "signed wchar_t".
1453/// Used when in C++, as a GCC extension.
1454QualType ASTContext::getSignedWCharType() const {
1455 // FIXME: derive from "Target" ?
1456 return WCharTy;
1457}
1458
1459/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1460/// Used when in C++, as a GCC extension.
1461QualType ASTContext::getUnsignedWCharType() const {
1462 // FIXME: derive from "Target" ?
1463 return UnsignedIntTy;
1464}
1465
Chris Lattner8b9023b2007-07-13 03:05:23 +00001466/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1467/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1468QualType ASTContext::getPointerDiffType() const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00001469 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner8b9023b2007-07-13 03:05:23 +00001470}
1471
Chris Lattnere6327742008-04-02 05:18:44 +00001472//===----------------------------------------------------------------------===//
1473// Type Operators
1474//===----------------------------------------------------------------------===//
1475
Chris Lattner77c96472008-04-06 22:41:35 +00001476/// getCanonicalType - Return the canonical (structural) type corresponding to
1477/// the specified potentially non-canonical type. The non-canonical version
1478/// of a type may have many "decorated" versions of types. Decorators can
1479/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1480/// to be free of any of these, allowing two canonical types to be compared
1481/// for exact equality with a simple pointer comparison.
1482QualType ASTContext::getCanonicalType(QualType T) {
1483 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001484
1485 // If the result has type qualifiers, make sure to canonicalize them as well.
1486 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1487 if (TypeQuals == 0) return CanType;
1488
1489 // If the type qualifiers are on an array type, get the canonical type of the
1490 // array with the qualifiers applied to the element type.
1491 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1492 if (!AT)
1493 return CanType.getQualifiedType(TypeQuals);
1494
1495 // Get the canonical version of the element with the extra qualifiers on it.
1496 // This can recursively sink qualifiers through multiple levels of arrays.
1497 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1498 NewEltTy = getCanonicalType(NewEltTy);
1499
1500 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1501 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1502 CAT->getIndexTypeQualifier());
1503 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1504 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1505 IAT->getIndexTypeQualifier());
1506
Douglas Gregor898574e2008-12-05 23:32:09 +00001507 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1508 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1509 DSAT->getSizeModifier(),
1510 DSAT->getIndexTypeQualifier());
1511
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001512 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1513 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1514 VAT->getSizeModifier(),
1515 VAT->getIndexTypeQualifier());
1516}
1517
1518
1519const ArrayType *ASTContext::getAsArrayType(QualType T) {
1520 // Handle the non-qualified case efficiently.
1521 if (T.getCVRQualifiers() == 0) {
1522 // Handle the common positive case fast.
1523 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1524 return AT;
1525 }
1526
1527 // Handle the common negative case fast, ignoring CVR qualifiers.
1528 QualType CType = T->getCanonicalTypeInternal();
1529
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001530 // Make sure to look through type qualifiers (like ExtQuals) for the negative
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001531 // test.
1532 if (!isa<ArrayType>(CType) &&
1533 !isa<ArrayType>(CType.getUnqualifiedType()))
1534 return 0;
1535
1536 // Apply any CVR qualifiers from the array type to the element type. This
1537 // implements C99 6.7.3p8: "If the specification of an array type includes
1538 // any type qualifiers, the element type is so qualified, not the array type."
1539
1540 // If we get here, we either have type qualifiers on the type, or we have
1541 // sugar such as a typedef in the way. If we have type qualifiers on the type
1542 // we must propagate them down into the elemeng type.
1543 unsigned CVRQuals = T.getCVRQualifiers();
1544 unsigned AddrSpace = 0;
1545 Type *Ty = T.getTypePtr();
1546
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001547 // Rip through ExtQualType's and typedefs to get to a concrete type.
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001548 while (1) {
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001549 if (const ExtQualType *EXTQT = dyn_cast<ExtQualType>(Ty)) {
1550 AddrSpace = EXTQT->getAddressSpace();
1551 Ty = EXTQT->getBaseType();
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001552 } else {
1553 T = Ty->getDesugaredType();
1554 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1555 break;
1556 CVRQuals |= T.getCVRQualifiers();
1557 Ty = T.getTypePtr();
1558 }
1559 }
1560
1561 // If we have a simple case, just return now.
1562 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1563 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1564 return ATy;
1565
1566 // Otherwise, we have an array and we have qualifiers on it. Push the
1567 // qualifiers into the array element type and return a new array type.
1568 // Get the canonical version of the element with the extra qualifiers on it.
1569 // This can recursively sink qualifiers through multiple levels of arrays.
1570 QualType NewEltTy = ATy->getElementType();
1571 if (AddrSpace)
Fariborz Jahanianf11284a2009-02-17 18:27:45 +00001572 NewEltTy = getAddrSpaceQualType(NewEltTy, AddrSpace);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001573 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1574
1575 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1576 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1577 CAT->getSizeModifier(),
1578 CAT->getIndexTypeQualifier()));
1579 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1580 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1581 IAT->getSizeModifier(),
1582 IAT->getIndexTypeQualifier()));
Douglas Gregor898574e2008-12-05 23:32:09 +00001583
Douglas Gregor898574e2008-12-05 23:32:09 +00001584 if (const DependentSizedArrayType *DSAT
1585 = dyn_cast<DependentSizedArrayType>(ATy))
1586 return cast<ArrayType>(
1587 getDependentSizedArrayType(NewEltTy,
1588 DSAT->getSizeExpr(),
1589 DSAT->getSizeModifier(),
1590 DSAT->getIndexTypeQualifier()));
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001591
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001592 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1593 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1594 VAT->getSizeModifier(),
1595 VAT->getIndexTypeQualifier()));
Chris Lattner77c96472008-04-06 22:41:35 +00001596}
1597
1598
Chris Lattnere6327742008-04-02 05:18:44 +00001599/// getArrayDecayedType - Return the properly qualified result of decaying the
1600/// specified array type to a pointer. This operation is non-trivial when
1601/// handling typedefs etc. The canonical type of "T" must be an array type,
1602/// this returns a pointer to a properly qualified element of the array.
1603///
1604/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1605QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001606 // Get the element type with 'getAsArrayType' so that we don't lose any
1607 // typedefs in the element type of the array. This also handles propagation
1608 // of type qualifiers from the array type into the element type if present
1609 // (C99 6.7.3p8).
1610 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1611 assert(PrettyArrayType && "Not an array type!");
Chris Lattnere6327742008-04-02 05:18:44 +00001612
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001613 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattnere6327742008-04-02 05:18:44 +00001614
1615 // int x[restrict 4] -> int *restrict
Chris Lattnerc63a1f22008-08-04 07:31:14 +00001616 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattnere6327742008-04-02 05:18:44 +00001617}
1618
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001619QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson6183a992008-12-21 03:44:36 +00001620 QualType ElemTy = VAT->getElementType();
1621
1622 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1623 return getBaseElementType(VAT);
1624
1625 return ElemTy;
1626}
1627
Reid Spencer5f016e22007-07-11 17:01:13 +00001628/// getFloatingRank - Return a relative rank for floating point types.
1629/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnera75cea32008-04-06 23:38:49 +00001630static FloatingRank getFloatingRank(QualType T) {
Christopher Lambebb97e92008-02-04 02:31:56 +00001631 if (const ComplexType *CT = T->getAsComplexType())
Reid Spencer5f016e22007-07-11 17:01:13 +00001632 return getFloatingRank(CT->getElementType());
Chris Lattnera75cea32008-04-06 23:38:49 +00001633
Daniel Dunbard786f6a2009-01-05 22:14:37 +00001634 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lambebb97e92008-02-04 02:31:56 +00001635 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnera75cea32008-04-06 23:38:49 +00001636 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +00001637 case BuiltinType::Float: return FloatRank;
1638 case BuiltinType::Double: return DoubleRank;
1639 case BuiltinType::LongDouble: return LongDoubleRank;
1640 }
1641}
1642
Steve Naroff716c7302007-08-27 01:41:48 +00001643/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1644/// point or a complex type (based on typeDomain/typeSize).
1645/// 'typeDomain' is a real floating point or complex type.
1646/// 'typeSize' is a real floating point or complex type.
Chris Lattner1361b112008-04-06 23:58:54 +00001647QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1648 QualType Domain) const {
1649 FloatingRank EltRank = getFloatingRank(Size);
1650 if (Domain->isComplexType()) {
1651 switch (EltRank) {
Steve Naroff716c7302007-08-27 01:41:48 +00001652 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +00001653 case FloatRank: return FloatComplexTy;
1654 case DoubleRank: return DoubleComplexTy;
1655 case LongDoubleRank: return LongDoubleComplexTy;
1656 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 }
Chris Lattner1361b112008-04-06 23:58:54 +00001658
1659 assert(Domain->isRealFloatingType() && "Unknown domain!");
1660 switch (EltRank) {
1661 default: assert(0 && "getFloatingRank(): illegal value for rank");
1662 case FloatRank: return FloatTy;
1663 case DoubleRank: return DoubleTy;
1664 case LongDoubleRank: return LongDoubleTy;
Steve Narofff1448a02007-08-27 01:27:54 +00001665 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001666}
1667
Chris Lattner7cfeb082008-04-06 23:55:33 +00001668/// getFloatingTypeOrder - Compare the rank of the two specified floating
1669/// point types, ignoring the domain of the type (i.e. 'double' ==
1670/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1671/// LHS < RHS, return -1.
Chris Lattnera75cea32008-04-06 23:38:49 +00001672int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1673 FloatingRank LHSR = getFloatingRank(LHS);
1674 FloatingRank RHSR = getFloatingRank(RHS);
1675
1676 if (LHSR == RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001677 return 0;
Chris Lattnera75cea32008-04-06 23:38:49 +00001678 if (LHSR > RHSR)
Steve Narofffb0d4962007-08-27 15:30:22 +00001679 return 1;
1680 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001681}
1682
Chris Lattnerf52ab252008-04-06 22:59:24 +00001683/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1684/// routine will assert if passed a built-in type that isn't an integer or enum,
1685/// or if it is not canonicalized.
Eli Friedmanf98aba32009-02-13 02:31:07 +00001686unsigned ASTContext::getIntegerRank(Type *T) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001687 assert(T->isCanonical() && "T should be canonicalized");
Eli Friedmanf98aba32009-02-13 02:31:07 +00001688 if (EnumType* ET = dyn_cast<EnumType>(T))
1689 T = ET->getDecl()->getIntegerType().getTypePtr();
1690
1691 // There are two things which impact the integer rank: the width, and
1692 // the ordering of builtins. The builtin ordering is encoded in the
1693 // bottom three bits; the width is encoded in the bits above that.
1694 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
1695 return FWIT->getWidth() << 3;
1696 }
1697
Chris Lattnerf52ab252008-04-06 22:59:24 +00001698 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner7cfeb082008-04-06 23:55:33 +00001699 default: assert(0 && "getIntegerRank(): not a built-in integer");
1700 case BuiltinType::Bool:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001701 return 1 + (getIntWidth(BoolTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001702 case BuiltinType::Char_S:
1703 case BuiltinType::Char_U:
1704 case BuiltinType::SChar:
1705 case BuiltinType::UChar:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001706 return 2 + (getIntWidth(CharTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001707 case BuiltinType::Short:
1708 case BuiltinType::UShort:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001709 return 3 + (getIntWidth(ShortTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001710 case BuiltinType::Int:
1711 case BuiltinType::UInt:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001712 return 4 + (getIntWidth(IntTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001713 case BuiltinType::Long:
1714 case BuiltinType::ULong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001715 return 5 + (getIntWidth(LongTy) << 3);
Chris Lattner7cfeb082008-04-06 23:55:33 +00001716 case BuiltinType::LongLong:
1717 case BuiltinType::ULongLong:
Eli Friedmanf98aba32009-02-13 02:31:07 +00001718 return 6 + (getIntWidth(LongLongTy) << 3);
Chris Lattnerf52ab252008-04-06 22:59:24 +00001719 }
1720}
1721
Chris Lattner7cfeb082008-04-06 23:55:33 +00001722/// getIntegerTypeOrder - Returns the highest ranked integer type:
1723/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1724/// LHS < RHS, return -1.
1725int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerf52ab252008-04-06 22:59:24 +00001726 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1727 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner7cfeb082008-04-06 23:55:33 +00001728 if (LHSC == RHSC) return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00001729
Chris Lattnerf52ab252008-04-06 22:59:24 +00001730 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1731 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +00001732
Chris Lattner7cfeb082008-04-06 23:55:33 +00001733 unsigned LHSRank = getIntegerRank(LHSC);
1734 unsigned RHSRank = getIntegerRank(RHSC);
Reid Spencer5f016e22007-07-11 17:01:13 +00001735
Chris Lattner7cfeb082008-04-06 23:55:33 +00001736 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1737 if (LHSRank == RHSRank) return 0;
1738 return LHSRank > RHSRank ? 1 : -1;
1739 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001740
Chris Lattner7cfeb082008-04-06 23:55:33 +00001741 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1742 if (LHSUnsigned) {
1743 // If the unsigned [LHS] type is larger, return it.
1744 if (LHSRank >= RHSRank)
1745 return 1;
1746
1747 // If the signed type can represent all values of the unsigned type, it
1748 // wins. Because we are dealing with 2's complement and types that are
1749 // powers of two larger than each other, this is always safe.
1750 return -1;
1751 }
Chris Lattnerf52ab252008-04-06 22:59:24 +00001752
Chris Lattner7cfeb082008-04-06 23:55:33 +00001753 // If the unsigned [RHS] type is larger, return it.
1754 if (RHSRank >= LHSRank)
1755 return -1;
1756
1757 // If the signed type can represent all values of the unsigned type, it
1758 // wins. Because we are dealing with 2's complement and types that are
1759 // powers of two larger than each other, this is always safe.
1760 return 1;
Reid Spencer5f016e22007-07-11 17:01:13 +00001761}
Anders Carlsson71993dd2007-08-17 05:31:46 +00001762
1763// getCFConstantStringType - Return the type used for constant CFStrings.
1764QualType ASTContext::getCFConstantStringType() {
1765 if (!CFConstantStringTypeDecl) {
Chris Lattner6c2b6eb2008-03-15 06:12:44 +00001766 CFConstantStringTypeDecl =
Argyrios Kyrtzidis39ba4ae2008-06-09 23:19:58 +00001767 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenekdf042e62008-09-05 01:34:33 +00001768 &Idents.get("NSConstantString"));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001769 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +00001770
1771 // const int *isa;
1772 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +00001773 // int flags;
1774 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +00001775 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001776 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +00001777 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +00001778 FieldTypes[3] = LongTy;
Douglas Gregor44b43212008-12-11 16:49:14 +00001779
Anders Carlsson71993dd2007-08-17 05:31:46 +00001780 // Create fields
Douglas Gregor44b43212008-12-11 16:49:14 +00001781 for (unsigned i = 0; i < 4; ++i) {
1782 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1783 SourceLocation(), 0,
1784 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001785 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001786 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001787 }
1788
1789 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlsson71993dd2007-08-17 05:31:46 +00001790 }
1791
1792 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +00001793}
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001794
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001795QualType ASTContext::getObjCFastEnumerationStateType()
1796{
1797 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor44b43212008-12-11 16:49:14 +00001798 ObjCFastEnumerationStateTypeDecl =
1799 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1800 &Idents.get("__objcFastEnumerationState"));
1801
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001802 QualType FieldTypes[] = {
1803 UnsignedLongTy,
1804 getPointerType(ObjCIdType),
1805 getPointerType(UnsignedLongTy),
1806 getConstantArrayType(UnsignedLongTy,
1807 llvm::APInt(32, 5), ArrayType::Normal, 0)
1808 };
1809
Douglas Gregor44b43212008-12-11 16:49:14 +00001810 for (size_t i = 0; i < 4; ++i) {
1811 FieldDecl *Field = FieldDecl::Create(*this,
1812 ObjCFastEnumerationStateTypeDecl,
1813 SourceLocation(), 0,
1814 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregor4afa39d2009-01-20 01:17:11 +00001815 /*Mutable=*/false);
Douglas Gregor482b77d2009-01-12 23:27:07 +00001816 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor44b43212008-12-11 16:49:14 +00001817 }
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001818
Douglas Gregor44b43212008-12-11 16:49:14 +00001819 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonbd4c1ad2008-08-30 19:34:46 +00001820 }
1821
1822 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1823}
1824
Anders Carlssone8c49532007-10-29 06:33:42 +00001825// This returns true if a type has been typedefed to BOOL:
1826// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +00001827static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +00001828 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnerbb49c3e2008-11-24 03:52:59 +00001829 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1830 return II->isStr("BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001831
1832 return false;
1833}
1834
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001835/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001836/// purpose.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001837int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner98be4942008-03-05 18:54:05 +00001838 uint64_t sz = getTypeSize(type);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001839
1840 // Make all integer and enum types at least as large as an int
1841 if (sz > 0 && type->isIntegralType())
Chris Lattner98be4942008-03-05 18:54:05 +00001842 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001843 // Treat arrays as pointers, since that's how they're passed in.
1844 else if (type->isArrayType())
Chris Lattner98be4942008-03-05 18:54:05 +00001845 sz = getTypeSize(VoidPtrTy);
1846 return sz / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001847}
1848
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001849/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001850/// declaration.
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001851void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001852 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001853 // FIXME: This is not very efficient.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001854 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001855 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001856 // Encode result type.
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001857 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001858 // Compute size of all parameters.
1859 // Start with computing size of a pointer in number of bytes.
1860 // FIXME: There might(should) be a better way of doing this computation!
1861 SourceLocation Loc;
Chris Lattner98be4942008-03-05 18:54:05 +00001862 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001863 // The first two arguments (self and _cmd) are pointers; account for
1864 // their size.
1865 int ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001866 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1867 E = Decl->param_end(); PI != E; ++PI) {
1868 QualType PType = (*PI)->getType();
1869 int sz = getObjCEncodingTypeSize(PType);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001870 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001871 ParmOffset += sz;
1872 }
1873 S += llvm::utostr(ParmOffset);
1874 S += "@0:";
1875 S += llvm::utostr(PtrSize);
1876
1877 // Argument types.
1878 ParmOffset = 2 * PtrSize;
Chris Lattner89951a82009-02-20 18:43:26 +00001879 for (ObjCMethodDecl::param_iterator PI = Decl->param_begin(),
1880 E = Decl->param_end(); PI != E; ++PI) {
1881 ParmVarDecl *PVDecl = *PI;
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001882 QualType PType = PVDecl->getOriginalType();
1883 if (const ArrayType *AT =
1884 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1885 // Use array's original type only if it has known number of
1886 // elements.
1887 if (!dyn_cast<ConstantArrayType>(AT))
1888 PType = PVDecl->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001889 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001890 // 'in', 'inout', etc.
Fariborz Jahanian4306d3c2008-12-20 23:29:59 +00001891 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbar0d504c12008-10-17 20:21:44 +00001892 getObjCEncodingForType(PType, S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001893 S += llvm::utostr(ParmOffset);
Ted Kremeneka526c5c2008-01-07 19:49:32 +00001894 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +00001895 }
1896}
1897
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001898/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001899/// property declaration. If non-NULL, Container must be either an
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001900/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1901/// NULL when getting encodings for protocol properties.
Fariborz Jahanian83bccb82009-01-20 20:04:12 +00001902/// Property attributes are stored as a comma-delimited C string. The simple
1903/// attributes readonly and bycopy are encoded as single characters. The
1904/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1905/// encoded as single characters, followed by an identifier. Property types
1906/// are also encoded as a parametrized attribute. The characters used to encode
1907/// these attributes are defined by the following enumeration:
1908/// @code
1909/// enum PropertyAttributes {
1910/// kPropertyReadOnly = 'R', // property is read-only.
1911/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1912/// kPropertyByref = '&', // property is a reference to the value last assigned
1913/// kPropertyDynamic = 'D', // property is dynamic
1914/// kPropertyGetter = 'G', // followed by getter selector name
1915/// kPropertySetter = 'S', // followed by setter selector name
1916/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1917/// kPropertyType = 't' // followed by old-style type encoding.
1918/// kPropertyWeak = 'W' // 'weak' property
1919/// kPropertyStrong = 'P' // property GC'able
1920/// kPropertyNonAtomic = 'N' // property non-atomic
1921/// };
1922/// @endcode
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001923void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1924 const Decl *Container,
Chris Lattnere6db3b02008-11-19 07:24:05 +00001925 std::string& S) {
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001926 // Collect information from the property implementation decl(s).
1927 bool Dynamic = false;
1928 ObjCPropertyImplDecl *SynthesizePID = 0;
1929
1930 // FIXME: Duplicated code due to poor abstraction.
1931 if (Container) {
1932 if (const ObjCCategoryImplDecl *CID =
1933 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1934 for (ObjCCategoryImplDecl::propimpl_iterator
1935 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1936 ObjCPropertyImplDecl *PID = *i;
1937 if (PID->getPropertyDecl() == PD) {
1938 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1939 Dynamic = true;
1940 } else {
1941 SynthesizePID = PID;
1942 }
1943 }
1944 }
1945 } else {
Chris Lattner61710852008-10-05 17:34:18 +00001946 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001947 for (ObjCCategoryImplDecl::propimpl_iterator
1948 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1949 ObjCPropertyImplDecl *PID = *i;
1950 if (PID->getPropertyDecl() == PD) {
1951 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1952 Dynamic = true;
1953 } else {
1954 SynthesizePID = PID;
1955 }
1956 }
1957 }
1958 }
1959 }
1960
1961 // FIXME: This is not very efficient.
1962 S = "T";
1963
1964 // Encode result type.
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001965 // GCC has some special rules regarding encoding of properties which
1966 // closely resembles encoding of ivars.
1967 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1968 true /* outermost type */,
1969 true /* encoding for property */);
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001970
1971 if (PD->isReadOnly()) {
1972 S += ",R";
1973 } else {
1974 switch (PD->getSetterKind()) {
1975 case ObjCPropertyDecl::Assign: break;
1976 case ObjCPropertyDecl::Copy: S += ",C"; break;
1977 case ObjCPropertyDecl::Retain: S += ",&"; break;
1978 }
1979 }
1980
1981 // It really isn't clear at all what this means, since properties
1982 // are "dynamic by default".
1983 if (Dynamic)
1984 S += ",D";
1985
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00001986 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
1987 S += ",N";
1988
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001989 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1990 S += ",G";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001991 S += PD->getGetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001992 }
1993
1994 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1995 S += ",S";
Chris Lattner077bf5e2008-11-24 03:33:13 +00001996 S += PD->getSetterName().getAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00001997 }
1998
1999 if (SynthesizePID) {
2000 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
2001 S += ",V";
Chris Lattner39f34e92008-11-24 04:00:27 +00002002 S += OID->getNameAsString();
Daniel Dunbarc56f34a2008-08-28 04:38:10 +00002003 }
2004
2005 // FIXME: OBJCGC: weak & strong
2006}
2007
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002008/// getLegacyIntegralTypeEncoding -
2009/// Another legacy compatibility encoding: 32-bit longs are encoded as
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002010/// 'l' or 'L' , but not always. For typedefs, we need to use
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002011/// 'i' or 'I' instead if encoding a struct field, or a pointer!
2012///
2013void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
2014 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
2015 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002016 if (BT->getKind() == BuiltinType::ULong &&
2017 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002018 PointeeTy = UnsignedIntTy;
Fariborz Jahanianc657eba2009-02-11 23:59:18 +00002019 else
2020 if (BT->getKind() == BuiltinType::Long &&
2021 ((const_cast<ASTContext *>(this))->getIntWidth(PointeeTy) == 32))
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002022 PointeeTy = IntTy;
2023 }
2024 }
2025}
2026
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002027void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002028 FieldDecl *Field) const {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002029 // We follow the behavior of gcc, expanding structures which are
2030 // directly pointed to, and expanding embedded structures. Note that
2031 // these rules are sufficient to prevent recursive encoding of the
2032 // same type.
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002033 getObjCEncodingForTypeImpl(T, S, true, true, Field,
2034 true /* outermost type */);
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002035}
2036
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002037static void EncodeBitField(const ASTContext *Context, std::string& S,
2038 FieldDecl *FD) {
2039 const Expr *E = FD->getBitWidth();
2040 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
2041 ASTContext *Ctx = const_cast<ASTContext*>(Context);
2042 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
2043 S += 'b';
2044 S += llvm::utostr(N);
2045}
2046
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002047void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
2048 bool ExpandPointedToStructures,
2049 bool ExpandStructures,
Fariborz Jahanian5b8c7d92008-12-22 23:22:27 +00002050 FieldDecl *FD,
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002051 bool OutermostType,
2052 bool EncodingProperty) const {
Anders Carlssone8c49532007-10-29 06:33:42 +00002053 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002054 if (FD && FD->isBitField()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002055 EncodeBitField(this, S, FD);
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002056 }
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002057 else {
2058 char encoding;
2059 switch (BT->getKind()) {
2060 default: assert(0 && "Unhandled builtin type kind");
2061 case BuiltinType::Void: encoding = 'v'; break;
2062 case BuiltinType::Bool: encoding = 'B'; break;
2063 case BuiltinType::Char_U:
2064 case BuiltinType::UChar: encoding = 'C'; break;
2065 case BuiltinType::UShort: encoding = 'S'; break;
2066 case BuiltinType::UInt: encoding = 'I'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002067 case BuiltinType::ULong:
2068 encoding =
2069 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'L' : 'Q';
2070 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002071 case BuiltinType::ULongLong: encoding = 'Q'; break;
2072 case BuiltinType::Char_S:
2073 case BuiltinType::SChar: encoding = 'c'; break;
2074 case BuiltinType::Short: encoding = 's'; break;
2075 case BuiltinType::Int: encoding = 'i'; break;
Fariborz Jahanian72696e12009-02-11 22:31:45 +00002076 case BuiltinType::Long:
2077 encoding =
2078 (const_cast<ASTContext *>(this))->getIntWidth(T) == 32 ? 'l' : 'q';
2079 break;
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002080 case BuiltinType::LongLong: encoding = 'q'; break;
2081 case BuiltinType::Float: encoding = 'f'; break;
2082 case BuiltinType::Double: encoding = 'd'; break;
2083 case BuiltinType::LongDouble: encoding = 'd'; break;
2084 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002085
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002086 S += encoding;
2087 }
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002088 }
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002089 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002090 getObjCEncodingForTypeImpl(getObjCIdType(), S,
2091 ExpandPointedToStructures,
2092 ExpandStructures, FD);
2093 if (FD || EncodingProperty) {
2094 // Note that we do extended encoding of protocol qualifer list
2095 // Only when doing ivar or property encoding.
2096 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
2097 S += '"';
2098 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
2099 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
2100 S += '<';
2101 S += Proto->getNameAsString();
2102 S += '>';
2103 }
2104 S += '"';
2105 }
2106 return;
Fariborz Jahanianc5692492007-12-17 21:03:50 +00002107 }
2108 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002109 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002110 bool isReadOnly = false;
2111 // For historical/compatibility reasons, the read-only qualifier of the
2112 // pointee gets emitted _before_ the '^'. The read-only qualifier of
2113 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
2114 // Also, do not emit the 'r' for anything but the outermost type!
2115 if (dyn_cast<TypedefType>(T.getTypePtr())) {
2116 if (OutermostType && T.isConstQualified()) {
2117 isReadOnly = true;
2118 S += 'r';
2119 }
2120 }
2121 else if (OutermostType) {
2122 QualType P = PointeeTy;
2123 while (P->getAsPointerType())
2124 P = P->getAsPointerType()->getPointeeType();
2125 if (P.isConstQualified()) {
2126 isReadOnly = true;
2127 S += 'r';
2128 }
2129 }
2130 if (isReadOnly) {
2131 // Another legacy compatibility encoding. Some ObjC qualifier and type
2132 // combinations need to be rearranged.
2133 // Rewrite "in const" from "nr" to "rn"
2134 const char * s = S.c_str();
2135 int len = S.length();
2136 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
2137 std::string replace = "rn";
2138 S.replace(S.end()-2, S.end(), replace);
2139 }
2140 }
Steve Naroff389bf462009-02-12 17:52:19 +00002141 if (isObjCIdStructType(PointeeTy)) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002142 S += '@';
2143 return;
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002144 }
2145 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanianbb99bde2009-02-16 21:41:04 +00002146 if (!EncodingProperty &&
Fariborz Jahanian225dfd72009-02-16 22:09:26 +00002147 isa<TypedefType>(PointeeTy.getTypePtr())) {
Fariborz Jahanian3e1b16c2008-12-23 21:30:15 +00002148 // Another historical/compatibility reason.
2149 // We encode the underlying type which comes out as
2150 // {...};
2151 S += '^';
2152 getObjCEncodingForTypeImpl(PointeeTy, S,
2153 false, ExpandPointedToStructures,
2154 NULL);
2155 return;
2156 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002157 S += '@';
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002158 if (FD || EncodingProperty) {
Fariborz Jahanian86f938b2009-02-21 18:23:24 +00002159 const ObjCInterfaceType *OIT =
2160 PointeeTy.getUnqualifiedType()->getAsObjCInterfaceType();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002161 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002162 S += '"';
2163 S += OI->getNameAsCString();
Fariborz Jahanian090b3f72009-01-20 19:14:18 +00002164 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
2165 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
2166 S += '<';
2167 S += Proto->getNameAsString();
2168 S += '>';
2169 }
Fariborz Jahanianadcaf542008-12-20 19:17:01 +00002170 S += '"';
2171 }
Fariborz Jahanianc166d732008-12-19 00:14:49 +00002172 return;
Steve Naroff389bf462009-02-12 17:52:19 +00002173 } else if (isObjCClassStructType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002174 S += '#';
2175 return;
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002176 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson8baaca52007-10-31 02:53:19 +00002177 S += ':';
2178 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00002179 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002180
2181 if (PointeeTy->isCharType()) {
2182 // char pointer types should be encoded as '*' unless it is a
2183 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00002184 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002185 S += '*';
2186 return;
2187 }
2188 }
2189
2190 S += '^';
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002191 getLegacyIntegralTypeEncoding(PointeeTy);
2192
2193 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002194 false, ExpandPointedToStructures,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002195 NULL);
Chris Lattnerc63a1f22008-08-04 07:31:14 +00002196 } else if (const ArrayType *AT =
2197 // Ignore type qualifiers etc.
2198 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson559a8332009-02-22 01:38:57 +00002199 if (isa<IncompleteArrayType>(AT)) {
2200 // Incomplete arrays are encoded as a pointer to the array element.
2201 S += '^';
2202
2203 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2204 false, ExpandStructures, FD);
2205 } else {
2206 S += '[';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002207
Anders Carlsson559a8332009-02-22 01:38:57 +00002208 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
2209 S += llvm::utostr(CAT->getSize().getZExtValue());
2210 else {
2211 //Variable length arrays are encoded as a regular array with 0 elements.
2212 assert(isa<VariableArrayType>(AT) && "Unknown array type!");
2213 S += '0';
2214 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002215
Anders Carlsson559a8332009-02-22 01:38:57 +00002216 getObjCEncodingForTypeImpl(AT->getElementType(), S,
2217 false, ExpandStructures, FD);
2218 S += ']';
2219 }
Anders Carlssonc0a87b72007-10-30 00:06:20 +00002220 } else if (T->getAsFunctionType()) {
2221 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002222 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbar82a6cfb2008-10-17 07:30:50 +00002223 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002224 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar502a4a12008-10-17 06:22:57 +00002225 // Anonymous structures print as '?'
2226 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2227 S += II->getName();
2228 } else {
2229 S += '?';
2230 }
Daniel Dunbar0d504c12008-10-17 20:21:44 +00002231 if (ExpandStructures) {
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002232 S += '=';
Douglas Gregor44b43212008-12-11 16:49:14 +00002233 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2234 FieldEnd = RDecl->field_end();
2235 Field != FieldEnd; ++Field) {
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002236 if (FD) {
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002237 S += '"';
Douglas Gregor44b43212008-12-11 16:49:14 +00002238 S += Field->getNameAsString();
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002239 S += '"';
2240 }
2241
2242 // Special case bit-fields.
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002243 if (Field->isBitField()) {
2244 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2245 (*Field));
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002246 } else {
Fariborz Jahaniana1c033e2008-12-23 19:56:47 +00002247 QualType qt = Field->getType();
2248 getLegacyIntegralTypeEncoding(qt);
2249 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002250 FD);
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002251 }
Fariborz Jahanian7d6b46d2008-01-22 22:44:46 +00002252 }
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00002253 }
Daniel Dunbard96b35b2008-10-17 16:17:37 +00002254 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff5e711242007-12-12 22:30:11 +00002255 } else if (T->isEnumeralType()) {
Fariborz Jahanian8b4bf902009-01-13 01:18:13 +00002256 if (FD && FD->isBitField())
2257 EncodeBitField(this, S, FD);
2258 else
2259 S += 'i';
Steve Naroff485eeff2008-09-24 15:05:44 +00002260 } else if (T->isBlockPointerType()) {
Steve Naroff21a98b12009-02-02 18:24:29 +00002261 S += "@?"; // Unlike a pointer-to-function, which is "^?".
Fariborz Jahanian43822ea2008-12-19 23:34:38 +00002262 } else if (T->isObjCInterfaceType()) {
2263 // @encode(class_name)
2264 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2265 S += '{';
2266 const IdentifierInfo *II = OI->getIdentifier();
2267 S += II->getName();
2268 S += '=';
2269 std::vector<FieldDecl*> RecFields;
2270 CollectObjCIvars(OI, RecFields);
2271 for (unsigned int i = 0; i != RecFields.size(); i++) {
2272 if (RecFields[i]->isBitField())
2273 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2274 RecFields[i]);
2275 else
2276 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2277 FD);
2278 }
2279 S += '}';
2280 }
2281 else
Steve Narofff69cc5d2008-01-30 19:17:43 +00002282 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00002283}
2284
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002285void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00002286 std::string& S) const {
2287 if (QT & Decl::OBJC_TQ_In)
2288 S += 'n';
2289 if (QT & Decl::OBJC_TQ_Inout)
2290 S += 'N';
2291 if (QT & Decl::OBJC_TQ_Out)
2292 S += 'o';
2293 if (QT & Decl::OBJC_TQ_Bycopy)
2294 S += 'O';
2295 if (QT & Decl::OBJC_TQ_Byref)
2296 S += 'R';
2297 if (QT & Decl::OBJC_TQ_Oneway)
2298 S += 'V';
2299}
2300
Anders Carlssonb2cf3572007-10-11 01:00:40 +00002301void ASTContext::setBuiltinVaListType(QualType T)
2302{
2303 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2304
2305 BuiltinVaListType = T;
2306}
2307
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002308void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff7e219e42007-10-15 14:41:52 +00002309{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002310 ObjCIdType = getTypedefType(TD);
Steve Naroff7e219e42007-10-15 14:41:52 +00002311
2312 // typedef struct objc_object *id;
2313 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002314 // User error - caller will issue diagnostics.
2315 if (!ptr)
2316 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002317 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002318 // User error - caller will issue diagnostics.
2319 if (!rec)
2320 return;
Steve Naroff7e219e42007-10-15 14:41:52 +00002321 IdStructType = rec;
2322}
2323
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002324void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002325{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002326 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002327
2328 // typedef struct objc_selector *SEL;
2329 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002330 if (!ptr)
2331 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002332 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahanianc55a2402009-01-16 19:58:32 +00002333 if (!rec)
2334 return;
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00002335 SelStructType = rec;
2336}
2337
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002338void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002339{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002340 ObjCProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00002341}
2342
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002343void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson8baaca52007-10-31 02:53:19 +00002344{
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002345 ObjCClassType = getTypedefType(TD);
Anders Carlsson8baaca52007-10-31 02:53:19 +00002346
2347 // typedef struct objc_class *Class;
2348 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2349 assert(ptr && "'Class' incorrectly typed");
2350 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2351 assert(rec && "'Class' incorrectly typed");
2352 ClassStructType = rec;
2353}
2354
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002355void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2356 assert(ObjCConstantStringType.isNull() &&
Steve Naroff21988912007-10-15 23:35:17 +00002357 "'NSConstantString' type already set!");
2358
Ted Kremeneka526c5c2008-01-07 19:49:32 +00002359 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Naroff21988912007-10-15 23:35:17 +00002360}
2361
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002362/// getFromTargetType - Given one of the integer types provided by
Douglas Gregord9341122008-11-03 15:57:00 +00002363/// TargetInfo, produce the corresponding type. The unsigned @p Type
2364/// is actually a value of type @c TargetInfo::IntType.
2365QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002366 switch (Type) {
2367 case TargetInfo::NoInt: return QualType();
2368 case TargetInfo::SignedShort: return ShortTy;
2369 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2370 case TargetInfo::SignedInt: return IntTy;
2371 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2372 case TargetInfo::SignedLong: return LongTy;
2373 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2374 case TargetInfo::SignedLongLong: return LongLongTy;
2375 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2376 }
2377
2378 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbarb3ac5432008-11-11 01:16:00 +00002379 return QualType();
Douglas Gregorb4e66d52008-11-03 14:12:49 +00002380}
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002381
2382//===----------------------------------------------------------------------===//
2383// Type Predicates.
2384//===----------------------------------------------------------------------===//
2385
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002386/// isObjCNSObjectType - Return true if this is an NSObject object using
2387/// NSObject attribute on a c-style pointer type.
2388/// FIXME - Make it work directly on types.
2389///
2390bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2391 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2392 if (TypedefDecl *TD = TDT->getDecl())
2393 if (TD->getAttr<ObjCNSObjectAttr>())
2394 return true;
2395 }
2396 return false;
2397}
2398
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002399/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2400/// to an object type. This includes "id" and "Class" (two 'special' pointers
2401/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2402/// ID type).
2403bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
Steve Naroffd4617772009-02-23 18:36:16 +00002404 if (Ty->isObjCQualifiedIdType())
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002405 return true;
2406
Steve Naroff6ae98502008-10-21 18:24:04 +00002407 // Blocks are objects.
2408 if (Ty->isBlockPointerType())
2409 return true;
2410
2411 // All other object types are pointers.
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002412 if (!Ty->isPointerType())
2413 return false;
2414
2415 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2416 // pointer types. This looks for the typedef specifically, not for the
2417 // underlying type.
2418 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2419 return true;
2420
2421 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanianfa23c1d2009-01-13 23:34:40 +00002422 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2423 return true;
2424
2425 // If is has NSObject attribute, OK as well.
2426 return isObjCNSObjectType(Ty);
Ted Kremenekb6ccaac2008-07-24 23:58:27 +00002427}
2428
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002429/// getObjCGCAttr - Returns one of GCNone, Weak or Strong objc's
2430/// garbage collection attribute.
2431///
2432QualType::GCAttrTypes ASTContext::getObjCGCAttrKind(const QualType &Ty) const {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002433 QualType::GCAttrTypes GCAttrs = QualType::GCNone;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002434 if (getLangOptions().ObjC1 &&
2435 getLangOptions().getGCMode() != LangOptions::NonGC) {
Chris Lattnerb7d25532009-02-18 22:53:11 +00002436 GCAttrs = Ty.getObjCGCAttr();
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002437 // Default behavious under objective-c's gc is for objective-c pointers
Fariborz Jahaniana223cca2009-02-19 23:36:06 +00002438 // (or pointers to them) be treated as though they were declared
2439 // as __strong.
2440 if (GCAttrs == QualType::GCNone) {
2441 if (isObjCObjectPointerType(Ty))
2442 GCAttrs = QualType::Strong;
2443 else if (Ty->isPointerType())
2444 return getObjCGCAttrKind(Ty->getAsPointerType()->getPointeeType());
2445 }
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002446 }
Chris Lattnerb7d25532009-02-18 22:53:11 +00002447 return GCAttrs;
Fariborz Jahanian4fd83ea2009-02-18 21:49:28 +00002448}
2449
Chris Lattner6ac46a42008-04-07 06:51:04 +00002450//===----------------------------------------------------------------------===//
2451// Type Compatibility Testing
2452//===----------------------------------------------------------------------===//
Chris Lattner770951b2007-11-01 05:03:41 +00002453
Steve Naroff1c7d0672008-09-04 15:10:53 +00002454/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffdd972f22008-09-05 22:11:13 +00002455/// block types. Types must be strictly compatible here. For example,
2456/// C unfortunately doesn't produce an error for the following:
2457///
2458/// int (*emptyArgFunc)();
2459/// int (*intArgList)(int) = emptyArgFunc;
2460///
2461/// For blocks, we will produce an error for the following (similar to C++):
2462///
2463/// int (^emptyArgBlock)();
2464/// int (^intArgBlock)(int) = emptyArgBlock;
2465///
2466/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2467///
Steve Naroff1c7d0672008-09-04 15:10:53 +00002468bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroffc0febd52008-12-10 17:49:55 +00002469 const FunctionType *lbase = lhs->getAsFunctionType();
2470 const FunctionType *rbase = rhs->getAsFunctionType();
2471 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2472 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2473 if (lproto && rproto)
2474 return !mergeTypes(lhs, rhs).isNull();
2475 return false;
Steve Naroff1c7d0672008-09-04 15:10:53 +00002476}
2477
Chris Lattner6ac46a42008-04-07 06:51:04 +00002478/// areCompatVectorTypes - Return true if the two specified vector types are
2479/// compatible.
2480static bool areCompatVectorTypes(const VectorType *LHS,
2481 const VectorType *RHS) {
2482 assert(LHS->isCanonical() && RHS->isCanonical());
2483 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner61710852008-10-05 17:34:18 +00002484 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ac46a42008-04-07 06:51:04 +00002485}
2486
Eli Friedman3d815e72008-08-22 00:56:42 +00002487/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ac46a42008-04-07 06:51:04 +00002488/// compatible for assignment from RHS to LHS. This handles validation of any
2489/// protocol qualifiers on the LHS or RHS.
2490///
Eli Friedman3d815e72008-08-22 00:56:42 +00002491bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2492 const ObjCInterfaceType *RHS) {
Chris Lattner6ac46a42008-04-07 06:51:04 +00002493 // Verify that the base decls are compatible: the RHS must be a subclass of
2494 // the LHS.
2495 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2496 return false;
2497
2498 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2499 // protocol qualified at all, then we are good.
2500 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2501 return true;
2502
2503 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2504 // isn't a superset.
2505 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2506 return true; // FIXME: should return false!
2507
2508 // Finally, we must have two protocol-qualified interfaces.
2509 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2510 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2511 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2512 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2513 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2514 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2515
2516 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2517 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2518 // LHS in a single parallel scan until we run out of elements in LHS.
2519 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2520 ObjCProtocolDecl *LHSProto = *LHSPI;
2521
2522 while (RHSPI != RHSPE) {
2523 ObjCProtocolDecl *RHSProto = *RHSPI++;
2524 // If the RHS has a protocol that the LHS doesn't, ignore it.
2525 if (RHSProto != LHSProto)
2526 continue;
2527
2528 // Otherwise, the RHS does have this element.
2529 ++LHSPI;
2530 if (LHSPI == LHSPE)
2531 return true; // All protocols in LHS exist in RHS.
2532
2533 LHSProto = *LHSPI;
2534 }
2535
2536 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2537 return false;
2538}
2539
Steve Naroff389bf462009-02-12 17:52:19 +00002540bool ASTContext::areComparableObjCPointerTypes(QualType LHS, QualType RHS) {
2541 // get the "pointed to" types
2542 const PointerType *LHSPT = LHS->getAsPointerType();
2543 const PointerType *RHSPT = RHS->getAsPointerType();
2544
2545 if (!LHSPT || !RHSPT)
2546 return false;
2547
2548 QualType lhptee = LHSPT->getPointeeType();
2549 QualType rhptee = RHSPT->getPointeeType();
2550 const ObjCInterfaceType* LHSIface = lhptee->getAsObjCInterfaceType();
2551 const ObjCInterfaceType* RHSIface = rhptee->getAsObjCInterfaceType();
2552 // ID acts sort of like void* for ObjC interfaces
2553 if (LHSIface && isObjCIdStructType(rhptee))
2554 return true;
2555 if (RHSIface && isObjCIdStructType(lhptee))
2556 return true;
2557 if (!LHSIface || !RHSIface)
2558 return false;
2559 return canAssignObjCInterfaces(LHSIface, RHSIface) ||
2560 canAssignObjCInterfaces(RHSIface, LHSIface);
2561}
2562
Steve Naroffec0550f2007-10-15 20:41:53 +00002563/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2564/// both shall have the identically qualified version of a compatible type.
2565/// C99 6.2.7p1: Two types have compatible types if their types are the
2566/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman3d815e72008-08-22 00:56:42 +00002567bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2568 return !mergeTypes(LHS, RHS).isNull();
2569}
2570
2571QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2572 const FunctionType *lbase = lhs->getAsFunctionType();
2573 const FunctionType *rbase = rhs->getAsFunctionType();
2574 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2575 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2576 bool allLTypes = true;
2577 bool allRTypes = true;
2578
2579 // Check return type
2580 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2581 if (retType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002582 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2583 allLTypes = false;
2584 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2585 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002586
2587 if (lproto && rproto) { // two C99 style function prototypes
2588 unsigned lproto_nargs = lproto->getNumArgs();
2589 unsigned rproto_nargs = rproto->getNumArgs();
2590
2591 // Compatible functions must have the same number of arguments
2592 if (lproto_nargs != rproto_nargs)
2593 return QualType();
2594
2595 // Variadic and non-variadic functions aren't compatible
2596 if (lproto->isVariadic() != rproto->isVariadic())
2597 return QualType();
2598
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002599 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2600 return QualType();
2601
Eli Friedman3d815e72008-08-22 00:56:42 +00002602 // Check argument compatibility
2603 llvm::SmallVector<QualType, 10> types;
2604 for (unsigned i = 0; i < lproto_nargs; i++) {
2605 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2606 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2607 QualType argtype = mergeTypes(largtype, rargtype);
2608 if (argtype.isNull()) return QualType();
2609 types.push_back(argtype);
Chris Lattner61710852008-10-05 17:34:18 +00002610 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2611 allLTypes = false;
2612 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2613 allRTypes = false;
Eli Friedman3d815e72008-08-22 00:56:42 +00002614 }
2615 if (allLTypes) return lhs;
2616 if (allRTypes) return rhs;
2617 return getFunctionType(retType, types.begin(), types.size(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002618 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002619 }
2620
2621 if (lproto) allRTypes = false;
2622 if (rproto) allLTypes = false;
2623
2624 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2625 if (proto) {
2626 if (proto->isVariadic()) return QualType();
2627 // Check that the types are compatible with the types that
2628 // would result from default argument promotions (C99 6.7.5.3p15).
2629 // The only types actually affected are promotable integer
2630 // types and floats, which would be passed as a different
2631 // type depending on whether the prototype is visible.
2632 unsigned proto_nargs = proto->getNumArgs();
2633 for (unsigned i = 0; i < proto_nargs; ++i) {
2634 QualType argTy = proto->getArgType(i);
2635 if (argTy->isPromotableIntegerType() ||
2636 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2637 return QualType();
2638 }
2639
2640 if (allLTypes) return lhs;
2641 if (allRTypes) return rhs;
2642 return getFunctionType(retType, proto->arg_type_begin(),
Argyrios Kyrtzidis7fb5e482008-10-26 16:43:14 +00002643 proto->getNumArgs(), lproto->isVariadic(),
2644 lproto->getTypeQuals());
Eli Friedman3d815e72008-08-22 00:56:42 +00002645 }
2646
2647 if (allLTypes) return lhs;
2648 if (allRTypes) return rhs;
2649 return getFunctionTypeNoProto(retType);
2650}
2651
2652QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling43d69752007-12-03 07:33:35 +00002653 // C++ [expr]: If an expression initially has the type "reference to T", the
2654 // type is adjusted to "T" prior to any further analysis, the expression
2655 // designates the object or function denoted by the reference, and the
2656 // expression is an lvalue.
Eli Friedman3d815e72008-08-22 00:56:42 +00002657 // FIXME: C++ shouldn't be going through here! The rules are different
2658 // enough that they should be handled separately.
2659 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002660 LHS = RT->getPointeeType();
Eli Friedman3d815e72008-08-22 00:56:42 +00002661 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattnerc4e40592008-04-07 04:07:56 +00002662 RHS = RT->getPointeeType();
Chris Lattnerf3692dc2008-04-07 05:37:56 +00002663
Eli Friedman3d815e72008-08-22 00:56:42 +00002664 QualType LHSCan = getCanonicalType(LHS),
2665 RHSCan = getCanonicalType(RHS);
2666
2667 // If two types are identical, they are compatible.
2668 if (LHSCan == RHSCan)
2669 return LHS;
2670
2671 // If the qualifiers are different, the types aren't compatible
2672 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2673 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2674 return QualType();
2675
2676 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2677 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2678
Chris Lattner1adb8832008-01-14 05:45:46 +00002679 // We want to consider the two function types to be the same for these
2680 // comparisons, just force one to the other.
2681 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2682 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman4c721d32008-02-12 08:23:06 +00002683
2684 // Same as above for arrays
Chris Lattnera36a61f2008-04-07 05:43:21 +00002685 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2686 LHSClass = Type::ConstantArray;
2687 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2688 RHSClass = Type::ConstantArray;
Steve Naroffec0550f2007-10-15 20:41:53 +00002689
Nate Begeman213541a2008-04-18 23:10:10 +00002690 // Canonicalize ExtVector -> Vector.
2691 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2692 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnera36a61f2008-04-07 05:43:21 +00002693
Chris Lattnerb0489812008-04-07 06:38:24 +00002694 // Consider qualified interfaces and interfaces the same.
2695 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2696 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman3d815e72008-08-22 00:56:42 +00002697
Chris Lattnera36a61f2008-04-07 05:43:21 +00002698 // If the canonical type classes don't match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002699 if (LHSClass != RHSClass) {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002700 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2701 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2702
2703 // ID acts sort of like void* for ObjC interfaces
2704 if (LHSIface && isObjCIdStructType(RHS))
2705 return LHS;
2706 if (RHSIface && isObjCIdStructType(LHS))
2707 return RHS;
2708
Steve Naroffbc76dd02008-12-10 22:14:21 +00002709 // ID is compatible with all qualified id types.
2710 if (LHS->isObjCQualifiedIdType()) {
2711 if (const PointerType *PT = RHS->getAsPointerType()) {
2712 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002713 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002714 return LHS;
2715 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2716 // Unfortunately, this API is part of Sema (which we don't have access
2717 // to. Need to refactor. The following check is insufficient, since we
2718 // need to make sure the class implements the protocol.
2719 if (pType->isObjCInterfaceType())
2720 return LHS;
2721 }
2722 }
2723 if (RHS->isObjCQualifiedIdType()) {
2724 if (const PointerType *PT = LHS->getAsPointerType()) {
2725 QualType pType = PT->getPointeeType();
Steve Naroff389bf462009-02-12 17:52:19 +00002726 if (isObjCIdStructType(pType))
Steve Naroffbc76dd02008-12-10 22:14:21 +00002727 return RHS;
2728 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2729 // Unfortunately, this API is part of Sema (which we don't have access
2730 // to. Need to refactor. The following check is insufficient, since we
2731 // need to make sure the class implements the protocol.
2732 if (pType->isObjCInterfaceType())
2733 return RHS;
2734 }
2735 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002736 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2737 // a signed integer type, or an unsigned integer type.
Eli Friedman3d815e72008-08-22 00:56:42 +00002738 if (const EnumType* ETy = LHS->getAsEnumType()) {
2739 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2740 return RHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002741 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002742 if (const EnumType* ETy = RHS->getAsEnumType()) {
2743 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2744 return LHS;
Eli Friedmanbab96962008-02-12 08:46:17 +00002745 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002746
Eli Friedman3d815e72008-08-22 00:56:42 +00002747 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002748 }
Eli Friedman3d815e72008-08-22 00:56:42 +00002749
Steve Naroff4a746782008-01-09 22:43:08 +00002750 // The canonical type classes match.
Chris Lattner1adb8832008-01-14 05:45:46 +00002751 switch (LHSClass) {
Chris Lattner1adb8832008-01-14 05:45:46 +00002752 case Type::Pointer:
Eli Friedman3d815e72008-08-22 00:56:42 +00002753 {
2754 // Merge two pointer types, while trying to preserve typedef info
2755 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2756 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2757 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2758 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002759 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2760 return LHS;
2761 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2762 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002763 return getPointerType(ResultType);
2764 }
Steve Naroffc0febd52008-12-10 17:49:55 +00002765 case Type::BlockPointer:
2766 {
2767 // Merge two block pointer types, while trying to preserve typedef info
2768 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2769 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2770 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2771 if (ResultType.isNull()) return QualType();
2772 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2773 return LHS;
2774 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2775 return RHS;
2776 return getBlockPointerType(ResultType);
2777 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002778 case Type::ConstantArray:
Eli Friedman3d815e72008-08-22 00:56:42 +00002779 {
2780 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2781 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2782 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2783 return QualType();
2784
2785 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2786 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2787 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2788 if (ResultType.isNull()) return QualType();
Chris Lattner61710852008-10-05 17:34:18 +00002789 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2790 return LHS;
2791 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2792 return RHS;
Eli Friedman3bc0f452008-08-22 01:48:21 +00002793 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2794 ArrayType::ArraySizeModifier(), 0);
2795 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2796 ArrayType::ArraySizeModifier(), 0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002797 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2798 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner61710852008-10-05 17:34:18 +00002799 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2800 return LHS;
2801 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2802 return RHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002803 if (LVAT) {
2804 // FIXME: This isn't correct! But tricky to implement because
2805 // the array's size has to be the size of LHS, but the type
2806 // has to be different.
2807 return LHS;
2808 }
2809 if (RVAT) {
2810 // FIXME: This isn't correct! But tricky to implement because
2811 // the array's size has to be the size of RHS, but the type
2812 // has to be different.
2813 return RHS;
2814 }
Eli Friedman3bc0f452008-08-22 01:48:21 +00002815 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2816 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner61710852008-10-05 17:34:18 +00002817 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman3d815e72008-08-22 00:56:42 +00002818 }
Chris Lattner1adb8832008-01-14 05:45:46 +00002819 case Type::FunctionNoProto:
Eli Friedman3d815e72008-08-22 00:56:42 +00002820 return mergeFunctionTypes(LHS, RHS);
2821 case Type::Tagged:
Eli Friedman3d815e72008-08-22 00:56:42 +00002822 // FIXME: Why are these compatible?
Steve Naroff389bf462009-02-12 17:52:19 +00002823 if (isObjCIdStructType(LHS) && isObjCClassStructType(RHS)) return LHS;
2824 if (isObjCClassStructType(LHS) && isObjCIdStructType(RHS)) return LHS;
Eli Friedman3d815e72008-08-22 00:56:42 +00002825 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002826 case Type::Builtin:
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002827 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman3d815e72008-08-22 00:56:42 +00002828 return QualType();
Daniel Dunbar64cfdb72009-01-28 21:22:12 +00002829 case Type::Complex:
2830 // Distinct complex types are incompatible.
2831 return QualType();
Chris Lattner3cc4c0c2008-04-07 05:55:38 +00002832 case Type::Vector:
Eli Friedman3d815e72008-08-22 00:56:42 +00002833 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2834 return LHS;
Chris Lattner61710852008-10-05 17:34:18 +00002835 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002836 case Type::ObjCInterface: {
Steve Naroff5fd659d2009-02-21 16:18:07 +00002837 // Check if the interfaces are assignment compatible.
2838 const ObjCInterfaceType* LHSIface = LHS->getAsObjCInterfaceType();
2839 const ObjCInterfaceType* RHSIface = RHS->getAsObjCInterfaceType();
2840 if (LHSIface && RHSIface &&
2841 canAssignObjCInterfaces(LHSIface, RHSIface))
2842 return LHS;
2843
Eli Friedman3d815e72008-08-22 00:56:42 +00002844 return QualType();
Cedric Venet61490e92009-02-21 17:14:49 +00002845 }
Steve Naroffbc76dd02008-12-10 22:14:21 +00002846 case Type::ObjCQualifiedId:
2847 // Distinct qualified id's are not compatible.
2848 return QualType();
Chris Lattner1adb8832008-01-14 05:45:46 +00002849 default:
2850 assert(0 && "unexpected type");
Eli Friedman3d815e72008-08-22 00:56:42 +00002851 return QualType();
Steve Naroffec0550f2007-10-15 20:41:53 +00002852 }
Steve Naroffec0550f2007-10-15 20:41:53 +00002853}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002854
Chris Lattner5426bf62008-04-07 07:01:58 +00002855//===----------------------------------------------------------------------===//
Eli Friedmanad74a752008-06-28 06:23:08 +00002856// Integer Predicates
2857//===----------------------------------------------------------------------===//
Chris Lattner88054de2009-01-16 07:15:35 +00002858
Eli Friedmanad74a752008-06-28 06:23:08 +00002859unsigned ASTContext::getIntWidth(QualType T) {
2860 if (T == BoolTy)
2861 return 1;
Eli Friedmanf98aba32009-02-13 02:31:07 +00002862 if (FixedWidthIntType* FWIT = dyn_cast<FixedWidthIntType>(T)) {
2863 return FWIT->getWidth();
2864 }
2865 // For builtin types, just use the standard type sizing method
Eli Friedmanad74a752008-06-28 06:23:08 +00002866 return (unsigned)getTypeSize(T);
2867}
2868
2869QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2870 assert(T->isSignedIntegerType() && "Unexpected type");
2871 if (const EnumType* ETy = T->getAsEnumType())
2872 T = ETy->getDecl()->getIntegerType();
2873 const BuiltinType* BTy = T->getAsBuiltinType();
2874 assert (BTy && "Unexpected signed integer type");
2875 switch (BTy->getKind()) {
2876 case BuiltinType::Char_S:
2877 case BuiltinType::SChar:
2878 return UnsignedCharTy;
2879 case BuiltinType::Short:
2880 return UnsignedShortTy;
2881 case BuiltinType::Int:
2882 return UnsignedIntTy;
2883 case BuiltinType::Long:
2884 return UnsignedLongTy;
2885 case BuiltinType::LongLong:
2886 return UnsignedLongLongTy;
2887 default:
2888 assert(0 && "Unexpected signed integer type");
2889 return QualType();
2890 }
2891}
2892
2893
2894//===----------------------------------------------------------------------===//
Chris Lattner5426bf62008-04-07 07:01:58 +00002895// Serialization Support
2896//===----------------------------------------------------------------------===//
2897
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002898/// Emit - Serialize an ASTContext object to Bitcode.
2899void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002900 S.Emit(LangOpts);
Ted Kremenek54513502007-10-31 20:00:03 +00002901 S.EmitRef(SourceMgr);
2902 S.EmitRef(Target);
2903 S.EmitRef(Idents);
2904 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002905
Ted Kremenekfee04522007-10-31 22:44:07 +00002906 // Emit the size of the type vector so that we can reserve that size
2907 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00002908 S.EmitInt(Types.size());
2909
Ted Kremenek03ed4402007-11-13 22:02:55 +00002910 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2911 I!=E;++I)
2912 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00002913
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002914 S.EmitOwnedPtr(TUDecl);
2915
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002916 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00002917}
2918
Ted Kremenek0f84c002007-11-13 00:25:37 +00002919ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremeneke7d07d12008-06-04 15:55:15 +00002920
2921 // Read the language options.
2922 LangOptions LOpts;
2923 LOpts.Read(D);
2924
Ted Kremenekfee04522007-10-31 22:44:07 +00002925 SourceManager &SM = D.ReadRef<SourceManager>();
2926 TargetInfo &t = D.ReadRef<TargetInfo>();
2927 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2928 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattner0ed844b2008-04-04 06:12:32 +00002929
Ted Kremenekfee04522007-10-31 22:44:07 +00002930 unsigned size_reserve = D.ReadInt();
2931
Douglas Gregor2e1cd422008-11-17 14:58:09 +00002932 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2933 size_reserve);
Ted Kremenekfee04522007-10-31 22:44:07 +00002934
Ted Kremenek03ed4402007-11-13 22:02:55 +00002935 for (unsigned i = 0; i < size_reserve; ++i)
2936 Type::Create(*A,i,D);
Chris Lattner0ed844b2008-04-04 06:12:32 +00002937
Argyrios Kyrtzidisef177822008-04-17 14:40:12 +00002938 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2939
Ted Kremeneka9a4a242007-11-01 18:11:32 +00002940 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00002941
2942 return A;
2943}