blob: d20387dd95eedb0bc37694640e1d9a733193fc1c [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000015#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Daniel Dunbarde300732008-08-11 04:54:23 +000017#include "clang/AST/Expr.h"
18#include "clang/AST/RecordLayout.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include "clang/Basic/TargetInfo.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000020#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000021#include "llvm/Bitcode/Serialize.h"
22#include "llvm/Bitcode/Deserialize.h"
Nate Begeman7903d052009-01-18 06:42:49 +000023#include "llvm/Support/MathExtras.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000024
Chris Lattner4b009652007-07-25 00:24:17 +000025using namespace clang;
26
27enum FloatingRank {
28 FloatRank, DoubleRank, LongDoubleRank
29};
30
Chris Lattner2fda0ed2008-10-05 17:34:18 +000031ASTContext::ASTContext(const LangOptions& LOpts, SourceManager &SM,
32 TargetInfo &t,
Daniel Dunbarde300732008-08-11 04:54:23 +000033 IdentifierTable &idents, SelectorTable &sels,
34 unsigned size_reserve) :
Anders Carlssonf58cac72008-08-30 19:34:46 +000035 CFConstantStringTypeDecl(0), ObjCFastEnumerationStateTypeDecl(0),
36 SourceMgr(SM), LangOpts(LOpts), Target(t),
Douglas Gregor24afd4a2008-11-17 14:58:09 +000037 Idents(idents), Selectors(sels)
Daniel Dunbarde300732008-08-11 04:54:23 +000038{
39 if (size_reserve > 0) Types.reserve(size_reserve);
40 InitBuiltinTypes();
41 BuiltinInfo.InitializeBuiltins(idents, Target);
42 TUDecl = TranslationUnitDecl::Create(*this);
43}
44
Chris Lattner4b009652007-07-25 00:24:17 +000045ASTContext::~ASTContext() {
46 // Deallocate all the types.
47 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000048 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000049 Types.pop_back();
50 }
Eli Friedman65489b72008-05-27 03:08:09 +000051
Nuno Lopes355a8682008-12-17 22:30:25 +000052 {
53 llvm::DenseMap<const RecordDecl*, const ASTRecordLayout*>::iterator
54 I = ASTRecordLayouts.begin(), E = ASTRecordLayouts.end();
55 while (I != E) {
56 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
57 delete R;
58 }
59 }
60
61 {
62 llvm::DenseMap<const ObjCInterfaceDecl*, const ASTRecordLayout*>::iterator
63 I = ASTObjCInterfaces.begin(), E = ASTObjCInterfaces.end();
64 while (I != E) {
65 ASTRecordLayout *R = const_cast<ASTRecordLayout*>((I++)->second);
66 delete R;
67 }
68 }
69
70 {
71 llvm::DenseMap<const ObjCInterfaceDecl*, const RecordDecl*>::iterator
72 I = ASTRecordForInterface.begin(), E = ASTRecordForInterface.end();
73 while (I != E) {
74 RecordDecl *R = const_cast<RecordDecl*>((I++)->second);
75 R->Destroy(*this);
76 }
77 }
78
Eli Friedman65489b72008-05-27 03:08:09 +000079 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000080}
81
82void ASTContext::PrintStats() const {
83 fprintf(stderr, "*** AST Context Stats:\n");
84 fprintf(stderr, " %d types total.\n", (int)Types.size());
85 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Daniel Dunbar47677342008-09-26 03:23:00 +000086 unsigned NumVector = 0, NumComplex = 0, NumBlockPointer = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000087 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
88
89 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000090 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
91 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000092 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000093
94 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
95 Type *T = Types[i];
96 if (isa<BuiltinType>(T))
97 ++NumBuiltin;
98 else if (isa<PointerType>(T))
99 ++NumPointer;
Daniel Dunbar47677342008-09-26 03:23:00 +0000100 else if (isa<BlockPointerType>(T))
101 ++NumBlockPointer;
Chris Lattner4b009652007-07-25 00:24:17 +0000102 else if (isa<ReferenceType>(T))
103 ++NumReference;
104 else if (isa<ComplexType>(T))
105 ++NumComplex;
106 else if (isa<ArrayType>(T))
107 ++NumArray;
108 else if (isa<VectorType>(T))
109 ++NumVector;
110 else if (isa<FunctionTypeNoProto>(T))
111 ++NumFunctionNP;
112 else if (isa<FunctionTypeProto>(T))
113 ++NumFunctionP;
114 else if (isa<TypedefType>(T))
115 ++NumTypeName;
116 else if (TagType *TT = dyn_cast<TagType>(T)) {
117 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000118 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000119 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000120 case TagDecl::TK_struct: ++NumTagStruct; break;
121 case TagDecl::TK_union: ++NumTagUnion; break;
122 case TagDecl::TK_class: ++NumTagClass; break;
123 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +0000124 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000125 } else if (isa<ObjCInterfaceType>(T))
126 ++NumObjCInterfaces;
127 else if (isa<ObjCQualifiedInterfaceType>(T))
128 ++NumObjCQualifiedInterfaces;
129 else if (isa<ObjCQualifiedIdType>(T))
130 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +0000131 else if (isa<TypeOfType>(T))
132 ++NumTypeOfTypes;
133 else if (isa<TypeOfExpr>(T))
134 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +0000135 else {
Chris Lattner8a35b462007-12-12 06:43:05 +0000136 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +0000137 assert(0 && "Unknown type!");
138 }
139 }
140
141 fprintf(stderr, " %d builtin types\n", NumBuiltin);
142 fprintf(stderr, " %d pointer types\n", NumPointer);
Daniel Dunbar47677342008-09-26 03:23:00 +0000143 fprintf(stderr, " %d block pointer types\n", NumBlockPointer);
Chris Lattner4b009652007-07-25 00:24:17 +0000144 fprintf(stderr, " %d reference types\n", NumReference);
145 fprintf(stderr, " %d complex types\n", NumComplex);
146 fprintf(stderr, " %d array types\n", NumArray);
147 fprintf(stderr, " %d vector types\n", NumVector);
148 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
149 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
150 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
151 fprintf(stderr, " %d tagged types\n", NumTagged);
152 fprintf(stderr, " %d struct types\n", NumTagStruct);
153 fprintf(stderr, " %d union types\n", NumTagUnion);
154 fprintf(stderr, " %d class types\n", NumTagClass);
155 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000156 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000157 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000158 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000159 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000160 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000161 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
162 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
163
Chris Lattner4b009652007-07-25 00:24:17 +0000164 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
165 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
166 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
167 NumFunctionP*sizeof(FunctionTypeProto)+
168 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000169 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
170 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000171}
172
173
174void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
Steve Naroffbd9375a2009-01-19 22:45:10 +0000175 void *Mem = Allocator.Allocate(sizeof(BuiltinType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000176 Types.push_back((R = QualType(new (Mem) BuiltinType(K),0)).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000177}
178
Chris Lattner4b009652007-07-25 00:24:17 +0000179void ASTContext::InitBuiltinTypes() {
180 assert(VoidTy.isNull() && "Context reinitialized?");
181
182 // C99 6.2.5p19.
183 InitBuiltinType(VoidTy, BuiltinType::Void);
184
185 // C99 6.2.5p2.
186 InitBuiltinType(BoolTy, BuiltinType::Bool);
187 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000188 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000189 InitBuiltinType(CharTy, BuiltinType::Char_S);
190 else
191 InitBuiltinType(CharTy, BuiltinType::Char_U);
192 // C99 6.2.5p4.
193 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
194 InitBuiltinType(ShortTy, BuiltinType::Short);
195 InitBuiltinType(IntTy, BuiltinType::Int);
196 InitBuiltinType(LongTy, BuiltinType::Long);
197 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
198
199 // C99 6.2.5p6.
200 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
201 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
202 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
203 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
204 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
205
206 // C99 6.2.5p10.
207 InitBuiltinType(FloatTy, BuiltinType::Float);
208 InitBuiltinType(DoubleTy, BuiltinType::Double);
209 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000210
211 // C++ 3.9.1p5
212 InitBuiltinType(WCharTy, BuiltinType::WChar);
213
Douglas Gregord2baafd2008-10-21 16:13:35 +0000214 // Placeholder type for functions.
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000215 InitBuiltinType(OverloadTy, BuiltinType::Overload);
216
217 // Placeholder type for type-dependent expressions whose type is
218 // completely unknown. No code should ever check a type against
219 // DependentTy and users should never see it; however, it is here to
220 // help diagnose failures to properly check for type-dependent
221 // expressions.
222 InitBuiltinType(DependentTy, BuiltinType::Dependent);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000223
Chris Lattner4b009652007-07-25 00:24:17 +0000224 // C99 6.2.5p11.
225 FloatComplexTy = getComplexType(FloatTy);
226 DoubleComplexTy = getComplexType(DoubleTy);
227 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Douglas Gregord2baafd2008-10-21 16:13:35 +0000228
Steve Naroff9d12c902007-10-15 14:41:52 +0000229 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000230 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000231 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000232 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000233 ClassStructType = 0;
234
Ted Kremenek42730c52008-01-07 19:49:32 +0000235 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000236
237 // void * type
238 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000239}
240
241//===----------------------------------------------------------------------===//
242// Type Sizing and Analysis
243//===----------------------------------------------------------------------===//
244
Chris Lattner2a674dc2008-06-30 18:32:54 +0000245/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
246/// scalar floating point type.
247const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
248 const BuiltinType *BT = T->getAsBuiltinType();
249 assert(BT && "Not a floating point type!");
250 switch (BT->getKind()) {
251 default: assert(0 && "Not a floating point type!");
252 case BuiltinType::Float: return Target.getFloatFormat();
253 case BuiltinType::Double: return Target.getDoubleFormat();
254 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
255 }
256}
257
258
Chris Lattner4b009652007-07-25 00:24:17 +0000259/// getTypeSize - Return the size of the specified type, in bits. This method
260/// does not work on incomplete types.
261std::pair<uint64_t, unsigned>
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000262ASTContext::getTypeInfo(const Type *T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000263 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000264 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000265 unsigned Align;
266 switch (T->getTypeClass()) {
267 case Type::TypeName: assert(0 && "Not a canonical type!");
268 case Type::FunctionNoProto:
269 case Type::FunctionProto:
270 default:
271 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000272 case Type::VariableArray:
273 assert(0 && "VLAs not implemented yet!");
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000274 case Type::DependentSizedArray:
275 assert(0 && "Dependently-sized arrays don't have a known size");
Steve Naroff83c13012007-08-30 01:06:46 +0000276 case Type::ConstantArray: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000277 const ConstantArrayType *CAT = cast<ConstantArrayType>(T);
Steve Naroff83c13012007-08-30 01:06:46 +0000278
Chris Lattner8cd0e932008-03-05 18:54:05 +0000279 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000280 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000281 Align = EltInfo.second;
282 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000283 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000284 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000285 case Type::Vector: {
286 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000287 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000288 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000289 Align = Width;
Nate Begeman7903d052009-01-18 06:42:49 +0000290 // If the alignment is not a power of 2, round up to the next power of 2.
291 // This happens for non-power-of-2 length vectors.
292 // FIXME: this should probably be a target property.
293 Align = 1 << llvm::Log2_32_Ceil(Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000294 break;
295 }
296
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000297 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000298 switch (cast<BuiltinType>(T)->getKind()) {
299 default: assert(0 && "Unknown builtin type!");
300 case BuiltinType::Void:
301 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000302 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000303 Width = Target.getBoolWidth();
304 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000305 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000306 case BuiltinType::Char_S:
307 case BuiltinType::Char_U:
308 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000309 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000310 Width = Target.getCharWidth();
311 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000312 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000313 case BuiltinType::WChar:
314 Width = Target.getWCharWidth();
315 Align = Target.getWCharAlign();
316 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000317 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000318 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000319 Width = Target.getShortWidth();
320 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000321 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000322 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000323 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000324 Width = Target.getIntWidth();
325 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000326 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000327 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000328 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000329 Width = Target.getLongWidth();
330 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000331 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000332 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000333 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000334 Width = Target.getLongLongWidth();
335 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000336 break;
337 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000338 Width = Target.getFloatWidth();
339 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000340 break;
341 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000342 Width = Target.getDoubleWidth();
343 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000344 break;
345 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000346 Width = Target.getLongDoubleWidth();
347 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000348 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000349 }
350 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000351 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000352 // FIXME: Pointers into different addr spaces could have different sizes and
353 // alignment requirements: getPointerInfo should take an AddrSpace.
354 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000355 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000356 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000357 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000358 break;
Steve Naroff62f09f52008-09-24 15:05:44 +0000359 case Type::BlockPointer: {
360 unsigned AS = cast<BlockPointerType>(T)->getPointeeType().getAddressSpace();
361 Width = Target.getPointerWidth(AS);
362 Align = Target.getPointerAlign(AS);
363 break;
364 }
Chris Lattner461a6c52008-03-08 08:34:58 +0000365 case Type::Pointer: {
366 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000367 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000368 Align = Target.getPointerAlign(AS);
369 break;
370 }
Chris Lattner4b009652007-07-25 00:24:17 +0000371 case Type::Reference:
372 // "When applied to a reference or a reference type, the result is the size
373 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000374 // FIXME: This is wrong for struct layout: a reference in a struct has
375 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000376 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000377
378 case Type::Complex: {
379 // Complex types have the same alignment as their elements, but twice the
380 // size.
381 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000382 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000383 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000384 Align = EltInfo.second;
385 break;
386 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000387 case Type::ObjCInterface: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000388 const ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
Devang Patel4b6bf702008-06-04 21:54:36 +0000389 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
390 Width = Layout.getSize();
391 Align = Layout.getAlignment();
392 break;
393 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000394 case Type::Tagged: {
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000395 const TagType *TT = cast<TagType>(T);
396
397 if (TT->getDecl()->isInvalidDecl()) {
Chris Lattnerfd799692008-08-09 21:35:13 +0000398 Width = 1;
399 Align = 1;
400 break;
401 }
402
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000403 if (const EnumType *ET = dyn_cast<EnumType>(TT))
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000404 return getTypeInfo(ET->getDecl()->getIntegerType());
405
Daniel Dunbar7d6a5d22008-11-08 05:48:37 +0000406 const RecordType *RT = cast<RecordType>(TT);
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000407 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
408 Width = Layout.getSize();
409 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000410 break;
411 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000412 }
Chris Lattner4b009652007-07-25 00:24:17 +0000413
414 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000415 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000416}
417
Devang Patelbfe323c2008-06-04 21:22:16 +0000418/// LayoutField - Field layout.
419void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000420 bool IsUnion, unsigned StructPacking,
Devang Patelbfe323c2008-06-04 21:22:16 +0000421 ASTContext &Context) {
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000422 unsigned FieldPacking = StructPacking;
Devang Patelbfe323c2008-06-04 21:22:16 +0000423 uint64_t FieldOffset = IsUnion ? 0 : Size;
424 uint64_t FieldSize;
425 unsigned FieldAlign;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000426
427 // FIXME: Should this override struct packing? Probably we want to
428 // take the minimum?
429 if (const PackedAttr *PA = FD->getAttr<PackedAttr>())
430 FieldPacking = PA->getAlignment();
Devang Patelbfe323c2008-06-04 21:22:16 +0000431
432 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
433 // TODO: Need to check this algorithm on other targets!
434 // (tested on Linux-X86)
Daniel Dunbar7cbcbf42008-08-13 23:47:13 +0000435 FieldSize =
436 BitWidthExpr->getIntegerConstantExprValue(Context).getZExtValue();
Devang Patelbfe323c2008-06-04 21:22:16 +0000437
438 std::pair<uint64_t, unsigned> FieldInfo =
439 Context.getTypeInfo(FD->getType());
440 uint64_t TypeSize = FieldInfo.first;
441
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000442 // Determine the alignment of this bitfield. The packing
443 // attributes define a maximum and the alignment attribute defines
444 // a minimum.
445 // FIXME: What is the right behavior when the specified alignment
446 // is smaller than the specified packing?
Devang Patelbfe323c2008-06-04 21:22:16 +0000447 FieldAlign = FieldInfo.second;
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000448 if (FieldPacking)
449 FieldAlign = std::min(FieldAlign, FieldPacking);
Devang Patelbfe323c2008-06-04 21:22:16 +0000450 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
451 FieldAlign = std::max(FieldAlign, AA->getAlignment());
452
453 // Check if we need to add padding to give the field the correct
454 // alignment.
455 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
456 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
457
458 // Padding members don't affect overall alignment
459 if (!FD->getIdentifier())
460 FieldAlign = 1;
461 } else {
Chris Lattnerfd799692008-08-09 21:35:13 +0000462 if (FD->getType()->isIncompleteArrayType()) {
463 // This is a flexible array member; we can't directly
Devang Patelbfe323c2008-06-04 21:22:16 +0000464 // query getTypeInfo about these, so we figure it out here.
465 // Flexible array members don't have any size, but they
466 // have to be aligned appropriately for their element type.
467 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000468 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000469 FieldAlign = Context.getTypeAlign(ATy->getElementType());
470 } else {
471 std::pair<uint64_t, unsigned> FieldInfo =
472 Context.getTypeInfo(FD->getType());
473 FieldSize = FieldInfo.first;
474 FieldAlign = FieldInfo.second;
475 }
476
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000477 // Determine the alignment of this bitfield. The packing
478 // attributes define a maximum and the alignment attribute defines
479 // a minimum. Additionally, the packing alignment must be at least
480 // a byte for non-bitfields.
481 //
482 // FIXME: What is the right behavior when the specified alignment
483 // is smaller than the specified packing?
484 if (FieldPacking)
485 FieldAlign = std::min(FieldAlign, std::max(8U, FieldPacking));
Devang Patelbfe323c2008-06-04 21:22:16 +0000486 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
487 FieldAlign = std::max(FieldAlign, AA->getAlignment());
488
489 // Round up the current record size to the field's alignment boundary.
490 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
491 }
492
493 // Place this field at the current location.
494 FieldOffsets[FieldNo] = FieldOffset;
495
496 // Reserve space for this field.
497 if (IsUnion) {
498 Size = std::max(Size, FieldSize);
499 } else {
500 Size = FieldOffset + FieldSize;
501 }
502
503 // Remember max struct/class alignment.
504 Alignment = std::max(Alignment, FieldAlign);
505}
506
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000507static void CollectObjCIvars(const ObjCInterfaceDecl *OI,
508 std::vector<FieldDecl*> &Fields) {
509 const ObjCInterfaceDecl *SuperClass = OI->getSuperClass();
510 if (SuperClass)
511 CollectObjCIvars(SuperClass, Fields);
512 for (ObjCInterfaceDecl::ivar_iterator I = OI->ivar_begin(),
513 E = OI->ivar_end(); I != E; ++I) {
514 ObjCIvarDecl *IVDecl = (*I);
515 if (!IVDecl->isInvalidDecl())
516 Fields.push_back(cast<FieldDecl>(IVDecl));
517 }
518}
519
520/// addRecordToClass - produces record info. for the class for its
521/// ivars and all those inherited.
522///
523const RecordDecl *ASTContext::addRecordToClass(const ObjCInterfaceDecl *D)
524{
525 const RecordDecl *&RD = ASTRecordForInterface[D];
526 if (RD)
527 return RD;
528 std::vector<FieldDecl*> RecFields;
529 CollectObjCIvars(D, RecFields);
530 RecordDecl *NewRD = RecordDecl::Create(*this, TagDecl::TK_struct, 0,
531 D->getLocation(),
532 D->getIdentifier());
533 /// FIXME! Can do collection of ivars and adding to the record while
534 /// doing it.
535 for (unsigned int i = 0; i != RecFields.size(); i++) {
536 FieldDecl *Field = FieldDecl::Create(*this, NewRD,
537 RecFields[i]->getLocation(),
538 RecFields[i]->getIdentifier(),
539 RecFields[i]->getType(),
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +0000540 RecFields[i]->getBitWidth(), false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +0000541 NewRD->addDecl(Field);
Fariborz Jahanian0556b152008-12-17 21:40:49 +0000542 }
543 NewRD->completeDefinition(*this);
544 RD = NewRD;
545 return RD;
546}
Devang Patel4b6bf702008-06-04 21:54:36 +0000547
Fariborz Jahanianea944842008-12-18 17:29:46 +0000548/// setFieldDecl - maps a field for the given Ivar reference node.
549//
550void ASTContext::setFieldDecl(const ObjCInterfaceDecl *OI,
551 const ObjCIvarDecl *Ivar,
552 const ObjCIvarRefExpr *MRef) {
553 FieldDecl *FD = (const_cast<ObjCInterfaceDecl *>(OI))->
554 lookupFieldDeclForIvar(*this, Ivar);
555 ASTFieldForIvarRef[MRef] = FD;
556}
557
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000558/// getASTObjcInterfaceLayout - Get or compute information about the layout of
559/// the specified Objective C, which indicates its size and ivar
Devang Patel4b6bf702008-06-04 21:54:36 +0000560/// position information.
561const ASTRecordLayout &
562ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
563 // Look up this layout, if already laid out, return what we have.
564 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
565 if (Entry) return *Entry;
566
567 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
568 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000569 ASTRecordLayout *NewEntry = NULL;
570 unsigned FieldCount = D->ivar_size();
571 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
572 FieldCount++;
573 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
574 unsigned Alignment = SL.getAlignment();
575 uint64_t Size = SL.getSize();
576 NewEntry = new ASTRecordLayout(Size, Alignment);
577 NewEntry->InitializeLayout(FieldCount);
Chris Lattner2fda0ed2008-10-05 17:34:18 +0000578 // Super class is at the beginning of the layout.
579 NewEntry->SetFieldOffset(0, 0);
Devang Patel8682d882008-06-06 02:14:01 +0000580 } else {
581 NewEntry = new ASTRecordLayout();
582 NewEntry->InitializeLayout(FieldCount);
583 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000584 Entry = NewEntry;
585
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000586 unsigned StructPacking = 0;
587 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
588 StructPacking = PA->getAlignment();
Devang Patel4b6bf702008-06-04 21:54:36 +0000589
590 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
591 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
592 AA->getAlignment()));
593
594 // Layout each ivar sequentially.
595 unsigned i = 0;
596 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
597 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
598 const ObjCIvarDecl* Ivar = (*IVI);
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000599 NewEntry->LayoutField(Ivar, i++, false, StructPacking, *this);
Devang Patel4b6bf702008-06-04 21:54:36 +0000600 }
601
602 // Finally, round the size of the total struct up to the alignment of the
603 // struct itself.
604 NewEntry->FinalizeLayout();
605 return *NewEntry;
606}
607
Devang Patel7a78e432007-11-01 19:11:01 +0000608/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000609/// specified record (struct/union/class), which indicates its size and field
610/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000611const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Ted Kremenek46a837c2008-09-05 17:16:31 +0000612 D = D->getDefinition(*this);
613 assert(D && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000614
Chris Lattner4b009652007-07-25 00:24:17 +0000615 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000616 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000617 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000618
Devang Patel7a78e432007-11-01 19:11:01 +0000619 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
620 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
621 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000622 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000623
Douglas Gregor39677622008-12-11 20:41:00 +0000624 // FIXME: Avoid linear walk through the fields, if possible.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000625 NewEntry->InitializeLayout(std::distance(D->field_begin(), D->field_end()));
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000626 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000627
Daniel Dunbar2cb762f2008-10-16 02:34:03 +0000628 unsigned StructPacking = 0;
629 if (const PackedAttr *PA = D->getAttr<PackedAttr>())
630 StructPacking = PA->getAlignment();
631
Eli Friedman5949a022008-05-30 09:31:38 +0000632 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000633 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
634 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000635
Eli Friedman5949a022008-05-30 09:31:38 +0000636 // Layout each field, for now, just sequentially, respecting alignment. In
637 // the future, this will need to be tweakable by targets.
Douglas Gregor8acb7272008-12-11 16:49:14 +0000638 unsigned FieldIdx = 0;
Douglas Gregor5d764842009-01-09 17:18:27 +0000639 for (RecordDecl::field_iterator Field = D->field_begin(),
640 FieldEnd = D->field_end();
Douglas Gregor8acb7272008-12-11 16:49:14 +0000641 Field != FieldEnd; (void)++Field, ++FieldIdx)
642 NewEntry->LayoutField(*Field, FieldIdx, IsUnion, StructPacking, *this);
Eli Friedman5949a022008-05-30 09:31:38 +0000643
644 // Finally, round the size of the total struct up to the alignment of the
645 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000646 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000647 return *NewEntry;
648}
649
Chris Lattner4b009652007-07-25 00:24:17 +0000650//===----------------------------------------------------------------------===//
651// Type creation/memoization methods
652//===----------------------------------------------------------------------===//
653
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000654QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000655 QualType CanT = getCanonicalType(T);
656 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000657 return T;
658
659 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
660 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000661 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000662 "Type is already address space qualified");
663
664 // Check if we've already instantiated an address space qual'd type of this
665 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000666 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000667 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000668 void *InsertPos = 0;
669 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
670 return QualType(ASQy, 0);
671
672 // If the base type isn't canonical, this won't be a canonical type either,
673 // so fill in the canonical type field.
674 QualType Canonical;
675 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000676 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000677
678 // Get the new insert position for the node we care about.
679 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000680 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000681 }
Steve Naroffbd9375a2009-01-19 22:45:10 +0000682 void *Mem = Allocator.Allocate(sizeof(ASQualType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000683 ASQualType *New = new (Mem) ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000684 ASQualTypes.InsertNode(New, InsertPos);
685 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000686 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000687}
688
Chris Lattner4b009652007-07-25 00:24:17 +0000689
690/// getComplexType - Return the uniqued reference to the type for a complex
691/// number with the specified element type.
692QualType ASTContext::getComplexType(QualType T) {
693 // Unique pointers, to guarantee there is only one pointer of a particular
694 // structure.
695 llvm::FoldingSetNodeID ID;
696 ComplexType::Profile(ID, T);
697
698 void *InsertPos = 0;
699 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
700 return QualType(CT, 0);
701
702 // If the pointee type isn't canonical, this won't be a canonical type either,
703 // so fill in the canonical type field.
704 QualType Canonical;
705 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000706 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000707
708 // Get the new insert position for the node we care about.
709 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000710 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000711 }
Steve Naroffbd9375a2009-01-19 22:45:10 +0000712 void *Mem = Allocator.Allocate(sizeof(ComplexType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000713 ComplexType *New = new (Mem) ComplexType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000714 Types.push_back(New);
715 ComplexTypes.InsertNode(New, InsertPos);
716 return QualType(New, 0);
717}
718
719
720/// getPointerType - Return the uniqued reference to the type for a pointer to
721/// the specified type.
722QualType ASTContext::getPointerType(QualType T) {
723 // Unique pointers, to guarantee there is only one pointer of a particular
724 // structure.
725 llvm::FoldingSetNodeID ID;
726 PointerType::Profile(ID, T);
727
728 void *InsertPos = 0;
729 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
730 return QualType(PT, 0);
731
732 // If the pointee type isn't canonical, this won't be a canonical type either,
733 // so fill in the canonical type field.
734 QualType Canonical;
735 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000736 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000737
738 // Get the new insert position for the node we care about.
739 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000740 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000741 }
Steve Naroffbd9375a2009-01-19 22:45:10 +0000742 void *Mem = Allocator.Allocate(sizeof(PointerType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000743 PointerType *New = new (Mem) PointerType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000744 Types.push_back(New);
745 PointerTypes.InsertNode(New, InsertPos);
746 return QualType(New, 0);
747}
748
Steve Naroff7aa54752008-08-27 16:04:49 +0000749/// getBlockPointerType - Return the uniqued reference to the type for
750/// a pointer to the specified block.
751QualType ASTContext::getBlockPointerType(QualType T) {
Steve Narofffd5b19d2008-08-28 19:20:44 +0000752 assert(T->isFunctionType() && "block of function types only");
753 // Unique pointers, to guarantee there is only one block of a particular
Steve Naroff7aa54752008-08-27 16:04:49 +0000754 // structure.
755 llvm::FoldingSetNodeID ID;
756 BlockPointerType::Profile(ID, T);
757
758 void *InsertPos = 0;
759 if (BlockPointerType *PT =
760 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos))
761 return QualType(PT, 0);
762
Steve Narofffd5b19d2008-08-28 19:20:44 +0000763 // If the block pointee type isn't canonical, this won't be a canonical
Steve Naroff7aa54752008-08-27 16:04:49 +0000764 // type either so fill in the canonical type field.
765 QualType Canonical;
766 if (!T->isCanonical()) {
767 Canonical = getBlockPointerType(getCanonicalType(T));
768
769 // Get the new insert position for the node we care about.
770 BlockPointerType *NewIP =
771 BlockPointerTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000772 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Steve Naroff7aa54752008-08-27 16:04:49 +0000773 }
Steve Naroffbd9375a2009-01-19 22:45:10 +0000774 void *Mem = Allocator.Allocate(sizeof(BlockPointerType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000775 BlockPointerType *New = new (Mem) BlockPointerType(T, Canonical);
Steve Naroff7aa54752008-08-27 16:04:49 +0000776 Types.push_back(New);
777 BlockPointerTypes.InsertNode(New, InsertPos);
778 return QualType(New, 0);
779}
780
Chris Lattner4b009652007-07-25 00:24:17 +0000781/// getReferenceType - Return the uniqued reference to the type for a reference
782/// to the specified type.
783QualType ASTContext::getReferenceType(QualType T) {
784 // Unique pointers, to guarantee there is only one pointer of a particular
785 // structure.
786 llvm::FoldingSetNodeID ID;
787 ReferenceType::Profile(ID, T);
788
789 void *InsertPos = 0;
790 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
791 return QualType(RT, 0);
792
793 // If the referencee type isn't canonical, this won't be a canonical type
794 // either, so fill in the canonical type field.
795 QualType Canonical;
796 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000797 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000798
799 // Get the new insert position for the node we care about.
800 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000801 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000802 }
803
Steve Naroffbd9375a2009-01-19 22:45:10 +0000804 void *Mem = Allocator.Allocate(sizeof(ReferenceType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000805 ReferenceType *New = new (Mem) ReferenceType(T, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000806 Types.push_back(New);
807 ReferenceTypes.InsertNode(New, InsertPos);
808 return QualType(New, 0);
809}
810
Steve Naroff83c13012007-08-30 01:06:46 +0000811/// getConstantArrayType - Return the unique reference to the type for an
812/// array of the specified element type.
813QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000814 const llvm::APInt &ArySize,
815 ArrayType::ArraySizeModifier ASM,
816 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000817 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000818 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000819
820 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000821 if (ConstantArrayType *ATP =
822 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000823 return QualType(ATP, 0);
824
825 // If the element type isn't canonical, this won't be a canonical type either,
826 // so fill in the canonical type field.
827 QualType Canonical;
828 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000829 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000830 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000831 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000832 ConstantArrayType *NewIP =
833 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000834 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000835 }
836
Steve Naroffbd9375a2009-01-19 22:45:10 +0000837 void *Mem = Allocator.Allocate(sizeof(ConstantArrayType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000838 ConstantArrayType *New =
839 new (Mem) ConstantArrayType(EltTy, Canonical, ArySize, ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000840 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000841 Types.push_back(New);
842 return QualType(New, 0);
843}
844
Steve Naroffe2579e32007-08-30 18:14:25 +0000845/// getVariableArrayType - Returns a non-unique reference to the type for a
846/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000847QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
848 ArrayType::ArraySizeModifier ASM,
849 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000850 // Since we don't unique expressions, it isn't possible to unique VLA's
851 // that have an expression provided for their size.
852
Steve Naroffbd9375a2009-01-19 22:45:10 +0000853 void *Mem = Allocator.Allocate(sizeof(VariableArrayType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000854 VariableArrayType *New =
855 new (Mem) VariableArrayType(EltTy, QualType(), NumElts, ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000856
857 VariableArrayTypes.push_back(New);
858 Types.push_back(New);
859 return QualType(New, 0);
860}
861
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000862/// getDependentSizedArrayType - Returns a non-unique reference to
863/// the type for a dependently-sized array of the specified element
864/// type. FIXME: We will need these to be uniqued, or at least
865/// comparable, at some point.
866QualType ASTContext::getDependentSizedArrayType(QualType EltTy, Expr *NumElts,
867 ArrayType::ArraySizeModifier ASM,
868 unsigned EltTypeQuals) {
869 assert((NumElts->isTypeDependent() || NumElts->isValueDependent()) &&
870 "Size must be type- or value-dependent!");
871
872 // Since we don't unique expressions, it isn't possible to unique
873 // dependently-sized array types.
874
Steve Naroffbd9375a2009-01-19 22:45:10 +0000875 void *Mem = Allocator.Allocate(sizeof(DependentSizedArrayType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000876 DependentSizedArrayType *New =
877 new (Mem) DependentSizedArrayType(EltTy, QualType(), NumElts,
878 ASM, EltTypeQuals);
Douglas Gregor1b21c7f2008-12-05 23:32:09 +0000879
880 DependentSizedArrayTypes.push_back(New);
881 Types.push_back(New);
882 return QualType(New, 0);
883}
884
Eli Friedman8ff07782008-02-15 18:16:39 +0000885QualType ASTContext::getIncompleteArrayType(QualType EltTy,
886 ArrayType::ArraySizeModifier ASM,
887 unsigned EltTypeQuals) {
888 llvm::FoldingSetNodeID ID;
889 IncompleteArrayType::Profile(ID, EltTy);
890
891 void *InsertPos = 0;
892 if (IncompleteArrayType *ATP =
893 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
894 return QualType(ATP, 0);
895
896 // If the element type isn't canonical, this won't be a canonical type
897 // either, so fill in the canonical type field.
898 QualType Canonical;
899
900 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000901 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000902 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000903
904 // Get the new insert position for the node we care about.
905 IncompleteArrayType *NewIP =
906 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000907 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000908 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000909
Steve Naroffbd9375a2009-01-19 22:45:10 +0000910 void *Mem = Allocator.Allocate(sizeof(IncompleteArrayType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000911 IncompleteArrayType *New = new (Mem) IncompleteArrayType(EltTy, Canonical,
912 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000913
914 IncompleteArrayTypes.InsertNode(New, InsertPos);
915 Types.push_back(New);
916 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000917}
918
Chris Lattner4b009652007-07-25 00:24:17 +0000919/// getVectorType - Return the unique reference to a vector type of
920/// the specified element type and size. VectorType must be a built-in type.
921QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
922 BuiltinType *baseType;
923
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000924 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000925 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
926
927 // Check if we've already instantiated a vector of this type.
928 llvm::FoldingSetNodeID ID;
929 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
930 void *InsertPos = 0;
931 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
932 return QualType(VTP, 0);
933
934 // If the element type isn't canonical, this won't be a canonical type either,
935 // so fill in the canonical type field.
936 QualType Canonical;
937 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000938 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000939
940 // Get the new insert position for the node we care about.
941 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000942 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000943 }
Steve Naroffbd9375a2009-01-19 22:45:10 +0000944 void *Mem = Allocator.Allocate(sizeof(VectorType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000945 VectorType *New = new (Mem) VectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000946 VectorTypes.InsertNode(New, InsertPos);
947 Types.push_back(New);
948 return QualType(New, 0);
949}
950
Nate Begemanaf6ed502008-04-18 23:10:10 +0000951/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000952/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000953QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000954 BuiltinType *baseType;
955
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000956 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000957 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000958
959 // Check if we've already instantiated a vector of this type.
960 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000961 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000962 void *InsertPos = 0;
963 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
964 return QualType(VTP, 0);
965
966 // If the element type isn't canonical, this won't be a canonical type either,
967 // so fill in the canonical type field.
968 QualType Canonical;
969 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000970 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000971
972 // Get the new insert position for the node we care about.
973 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +0000974 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +0000975 }
Steve Naroffbd9375a2009-01-19 22:45:10 +0000976 void *Mem = Allocator.Allocate(sizeof(ExtVectorType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +0000977 ExtVectorType *New = new (Mem) ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000978 VectorTypes.InsertNode(New, InsertPos);
979 Types.push_back(New);
980 return QualType(New, 0);
981}
982
983/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
984///
985QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
986 // Unique functions, to guarantee there is only one function of a particular
987 // structure.
988 llvm::FoldingSetNodeID ID;
989 FunctionTypeNoProto::Profile(ID, ResultTy);
990
991 void *InsertPos = 0;
992 if (FunctionTypeNoProto *FT =
993 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
994 return QualType(FT, 0);
995
996 QualType Canonical;
997 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000998 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000999
1000 // Get the new insert position for the node we care about.
1001 FunctionTypeNoProto *NewIP =
1002 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001003 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001004 }
1005
Douglas Gregor5c561212009-01-20 21:02:13 +00001006 void *Mem = Allocator.Allocate(sizeof(FunctionTypeNoProto), 8);
1007 FunctionTypeNoProto *New = new (Mem) FunctionTypeNoProto(ResultTy, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001008 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +00001009 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +00001010 return QualType(New, 0);
1011}
1012
1013/// getFunctionType - Return a normal function type with a typed argument
1014/// list. isVariadic indicates whether the argument list includes '...'.
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001015QualType ASTContext::getFunctionType(QualType ResultTy,const QualType *ArgArray,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001016 unsigned NumArgs, bool isVariadic,
1017 unsigned TypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +00001018 // Unique functions, to guarantee there is only one function of a particular
1019 // structure.
1020 llvm::FoldingSetNodeID ID;
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001021 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic,
1022 TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001023
1024 void *InsertPos = 0;
1025 if (FunctionTypeProto *FTP =
1026 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
1027 return QualType(FTP, 0);
1028
1029 // Determine whether the type being created is already canonical or not.
1030 bool isCanonical = ResultTy->isCanonical();
1031 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
1032 if (!ArgArray[i]->isCanonical())
1033 isCanonical = false;
1034
1035 // If this type isn't canonical, get the canonical version of it.
1036 QualType Canonical;
1037 if (!isCanonical) {
1038 llvm::SmallVector<QualType, 16> CanonicalArgs;
1039 CanonicalArgs.reserve(NumArgs);
1040 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001041 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +00001042
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001043 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +00001044 &CanonicalArgs[0], NumArgs,
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00001045 isVariadic, TypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +00001046
1047 // Get the new insert position for the node we care about.
1048 FunctionTypeProto *NewIP =
1049 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner578a37e2008-10-12 00:26:57 +00001050 assert(NewIP == 0 && "Shouldn't be in the map!"); NewIP = NewIP;
Chris Lattner4b009652007-07-25 00:24:17 +00001051 }
1052
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001053 // FunctionTypeProto objects are allocated with extra bytes after them
1054 // for a variable size array (for parameter types) at the end of them.
1055 // FIXME: Can we do better than forcing a 16-byte alignment?
Chris Lattner4b009652007-07-25 00:24:17 +00001056 FunctionTypeProto *FTP =
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001057 (FunctionTypeProto*)Allocator.Allocate(sizeof(FunctionTypeProto) +
1058 NumArgs*sizeof(QualType), 16);
Chris Lattner4b009652007-07-25 00:24:17 +00001059 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
Argiris Kirtzidis4b269b42008-10-24 21:46:40 +00001060 TypeQuals, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001061 Types.push_back(FTP);
1062 FunctionTypeProtos.InsertNode(FTP, InsertPos);
1063 return QualType(FTP, 0);
1064}
1065
Douglas Gregor1d661552008-04-13 21:07:44 +00001066/// getTypeDeclType - Return the unique reference to the type for the
1067/// specified type declaration.
Ted Kremenek46a837c2008-09-05 17:16:31 +00001068QualType ASTContext::getTypeDeclType(TypeDecl *Decl, TypeDecl* PrevDecl) {
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001069 assert(Decl && "Passed null for Decl param");
Douglas Gregor1d661552008-04-13 21:07:44 +00001070 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1071
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001072 if (TypedefDecl *Typedef = dyn_cast<TypedefDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001073 return getTypedefType(Typedef);
Douglas Gregordd861062008-12-05 18:15:24 +00001074 else if (TemplateTypeParmDecl *TP = dyn_cast<TemplateTypeParmDecl>(Decl))
1075 return getTemplateTypeParmType(TP);
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001076 else if (ObjCInterfaceDecl *ObjCInterface = dyn_cast<ObjCInterfaceDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +00001077 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001078
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001079 if (CXXRecordDecl *CXXRecord = dyn_cast<CXXRecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001080 if (PrevDecl)
1081 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1082 else {
Steve Naroffbd9375a2009-01-19 22:45:10 +00001083 void *Mem = Allocator.Allocate(sizeof(CXXRecordType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001084 Decl->TypeForDecl = new (Mem) CXXRecordType(CXXRecord);
1085 }
Ted Kremenek46a837c2008-09-05 17:16:31 +00001086 }
Argiris Kirtzidiseeec5482008-10-16 16:50:47 +00001087 else if (RecordDecl *Record = dyn_cast<RecordDecl>(Decl)) {
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001088 if (PrevDecl)
1089 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1090 else {
Steve Naroffbd9375a2009-01-19 22:45:10 +00001091 void *Mem = Allocator.Allocate(sizeof(RecordType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001092 Decl->TypeForDecl = new (Mem) RecordType(Record);
1093 }
Ted Kremenek46a837c2008-09-05 17:16:31 +00001094 }
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001095 else if (EnumDecl *Enum = dyn_cast<EnumDecl>(Decl)) {
1096 if (PrevDecl)
1097 Decl->TypeForDecl = PrevDecl->TypeForDecl;
1098 else {
Steve Naroffbd9375a2009-01-19 22:45:10 +00001099 void *Mem = Allocator.Allocate(sizeof(EnumType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001100 Decl->TypeForDecl = new (Mem) EnumType(Enum);
1101 }
1102 }
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001103 else
Douglas Gregor1d661552008-04-13 21:07:44 +00001104 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001105
Ted Kremenek46a837c2008-09-05 17:16:31 +00001106 if (!PrevDecl) Types.push_back(Decl->TypeForDecl);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +00001107 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +00001108}
1109
Chris Lattner4b009652007-07-25 00:24:17 +00001110/// getTypedefType - Return the unique reference to the type for the
1111/// specified typename decl.
1112QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
1113 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1114
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001115 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Steve Naroffbd9375a2009-01-19 22:45:10 +00001116 void *Mem = Allocator.Allocate(sizeof(TypedefType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001117 Decl->TypeForDecl = new (Mem) TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +00001118 Types.push_back(Decl->TypeForDecl);
1119 return QualType(Decl->TypeForDecl, 0);
1120}
1121
Douglas Gregordd861062008-12-05 18:15:24 +00001122/// getTemplateTypeParmType - Return the unique reference to the type
1123/// for the specified template type parameter declaration.
1124QualType ASTContext::getTemplateTypeParmType(TemplateTypeParmDecl *Decl) {
1125 if (!Decl->TypeForDecl) {
Steve Naroffbd9375a2009-01-19 22:45:10 +00001126 void *Mem = Allocator.Allocate(sizeof(TemplateTypeParmType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001127 Decl->TypeForDecl = new (Mem) TemplateTypeParmType(Decl);
Douglas Gregordd861062008-12-05 18:15:24 +00001128 Types.push_back(Decl->TypeForDecl);
1129 }
1130 return QualType(Decl->TypeForDecl, 0);
1131}
1132
Ted Kremenek42730c52008-01-07 19:49:32 +00001133/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +00001134/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +00001135QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +00001136 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
1137
Steve Naroffbd9375a2009-01-19 22:45:10 +00001138 void *Mem = Allocator.Allocate(sizeof(ObjCInterfaceType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001139 Decl->TypeForDecl = new (Mem) ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +00001140 Types.push_back(Decl->TypeForDecl);
1141 return QualType(Decl->TypeForDecl, 0);
1142}
1143
Chris Lattnere1352302008-04-07 04:56:42 +00001144/// CmpProtocolNames - Comparison predicate for sorting protocols
1145/// alphabetically.
1146static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
1147 const ObjCProtocolDecl *RHS) {
Douglas Gregor24afd4a2008-11-17 14:58:09 +00001148 return LHS->getDeclName() < RHS->getDeclName();
Chris Lattnere1352302008-04-07 04:56:42 +00001149}
1150
1151static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
1152 unsigned &NumProtocols) {
1153 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
1154
1155 // Sort protocols, keyed by name.
1156 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
1157
1158 // Remove duplicates.
1159 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
1160 NumProtocols = ProtocolsEnd-Protocols;
1161}
1162
1163
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +00001164/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
1165/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +00001166QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
1167 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001168 // Sort the protocol list alphabetically to canonicalize it.
1169 SortAndUniqueProtocols(Protocols, NumProtocols);
1170
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001171 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +00001172 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001173
1174 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001175 if (ObjCQualifiedInterfaceType *QT =
1176 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001177 return QualType(QT, 0);
1178
1179 // No Match;
Steve Naroffbd9375a2009-01-19 22:45:10 +00001180 void *Mem = Allocator.Allocate(sizeof(ObjCQualifiedInterfaceType), 8);
Ted Kremenek42730c52008-01-07 19:49:32 +00001181 ObjCQualifiedInterfaceType *QType =
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001182 new (Mem) ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
1183
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001184 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001185 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +00001186 return QualType(QType, 0);
1187}
1188
Chris Lattnere1352302008-04-07 04:56:42 +00001189/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
1190/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +00001191QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001192 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +00001193 // Sort the protocol list alphabetically to canonicalize it.
1194 SortAndUniqueProtocols(Protocols, NumProtocols);
1195
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001196 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +00001197 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001198
1199 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001200 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +00001201 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001202 return QualType(QT, 0);
1203
1204 // No Match;
Steve Naroffbd9375a2009-01-19 22:45:10 +00001205 void *Mem = Allocator.Allocate(sizeof(ObjCQualifiedIdType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001206 ObjCQualifiedIdType *QType =
1207 new (Mem) ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001208 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +00001209 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001210 return QualType(QType, 0);
1211}
1212
Steve Naroff0604dd92007-08-01 18:02:17 +00001213/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
1214/// TypeOfExpr AST's (since expression's are never shared). For example,
1215/// multiple declarations that refer to "typeof(x)" all contain different
1216/// DeclRefExpr's. This doesn't effect the type checker, since it operates
1217/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +00001218QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001219 QualType Canonical = getCanonicalType(tofExpr->getType());
Douglas Gregor5c561212009-01-20 21:02:13 +00001220 void *Mem = Allocator.Allocate(sizeof(TypeOfExpr), 8);
1221 TypeOfExpr *toe = new (Mem) TypeOfExpr(tofExpr, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001222 Types.push_back(toe);
1223 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001224}
1225
Steve Naroff0604dd92007-08-01 18:02:17 +00001226/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
1227/// TypeOfType AST's. The only motivation to unique these nodes would be
1228/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
1229/// an issue. This doesn't effect the type checker, since it operates
1230/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +00001231QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001232 QualType Canonical = getCanonicalType(tofType);
Steve Naroffbd9375a2009-01-19 22:45:10 +00001233 void *Mem = Allocator.Allocate(sizeof(TypeOfType), 8);
Ted Kremenekc70e7d02009-01-19 21:31:22 +00001234 TypeOfType *tot = new (Mem) TypeOfType(tofType, Canonical);
Steve Naroff0604dd92007-08-01 18:02:17 +00001235 Types.push_back(tot);
1236 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +00001237}
1238
Chris Lattner4b009652007-07-25 00:24:17 +00001239/// getTagDeclType - Return the unique reference to the type for the
1240/// specified TagDecl (struct/union/class/enum) decl.
1241QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +00001242 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +00001243 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +00001244}
1245
1246/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
1247/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
1248/// needs to agree with the definition in <stddef.h>.
1249QualType ASTContext::getSizeType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001250 return getFromTargetType(Target.getSizeType());
Chris Lattner4b009652007-07-25 00:24:17 +00001251}
1252
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001253/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001254/// width of characters in wide strings, The value is target dependent and
1255/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001256QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001257 if (LangOpts.CPlusPlus)
1258 return WCharTy;
1259
Douglas Gregorc6507e42008-11-03 14:12:49 +00001260 // FIXME: In C, shouldn't WCharTy just be a typedef of the target's
1261 // wide-character type?
1262 return getFromTargetType(Target.getWCharType());
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001263}
1264
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001265/// getSignedWCharType - Return the type of "signed wchar_t".
1266/// Used when in C++, as a GCC extension.
1267QualType ASTContext::getSignedWCharType() const {
1268 // FIXME: derive from "Target" ?
1269 return WCharTy;
1270}
1271
1272/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1273/// Used when in C++, as a GCC extension.
1274QualType ASTContext::getUnsignedWCharType() const {
1275 // FIXME: derive from "Target" ?
1276 return UnsignedIntTy;
1277}
1278
Chris Lattner4b009652007-07-25 00:24:17 +00001279/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1280/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1281QualType ASTContext::getPointerDiffType() const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00001282 return getFromTargetType(Target.getPtrDiffType(0));
Chris Lattner4b009652007-07-25 00:24:17 +00001283}
1284
Chris Lattner19eb97e2008-04-02 05:18:44 +00001285//===----------------------------------------------------------------------===//
1286// Type Operators
1287//===----------------------------------------------------------------------===//
1288
Chris Lattner3dae6f42008-04-06 22:41:35 +00001289/// getCanonicalType - Return the canonical (structural) type corresponding to
1290/// the specified potentially non-canonical type. The non-canonical version
1291/// of a type may have many "decorated" versions of types. Decorators can
1292/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1293/// to be free of any of these, allowing two canonical types to be compared
1294/// for exact equality with a simple pointer comparison.
1295QualType ASTContext::getCanonicalType(QualType T) {
1296 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001297
1298 // If the result has type qualifiers, make sure to canonicalize them as well.
1299 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1300 if (TypeQuals == 0) return CanType;
1301
1302 // If the type qualifiers are on an array type, get the canonical type of the
1303 // array with the qualifiers applied to the element type.
1304 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1305 if (!AT)
1306 return CanType.getQualifiedType(TypeQuals);
1307
1308 // Get the canonical version of the element with the extra qualifiers on it.
1309 // This can recursively sink qualifiers through multiple levels of arrays.
1310 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1311 NewEltTy = getCanonicalType(NewEltTy);
1312
1313 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1314 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1315 CAT->getIndexTypeQualifier());
1316 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1317 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1318 IAT->getIndexTypeQualifier());
1319
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001320 if (DependentSizedArrayType *DSAT = dyn_cast<DependentSizedArrayType>(AT))
1321 return getDependentSizedArrayType(NewEltTy, DSAT->getSizeExpr(),
1322 DSAT->getSizeModifier(),
1323 DSAT->getIndexTypeQualifier());
1324
Chris Lattnera1923f62008-08-04 07:31:14 +00001325 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1326 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1327 VAT->getSizeModifier(),
1328 VAT->getIndexTypeQualifier());
1329}
1330
1331
1332const ArrayType *ASTContext::getAsArrayType(QualType T) {
1333 // Handle the non-qualified case efficiently.
1334 if (T.getCVRQualifiers() == 0) {
1335 // Handle the common positive case fast.
1336 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1337 return AT;
1338 }
1339
1340 // Handle the common negative case fast, ignoring CVR qualifiers.
1341 QualType CType = T->getCanonicalTypeInternal();
1342
1343 // Make sure to look through type qualifiers (like ASQuals) for the negative
1344 // test.
1345 if (!isa<ArrayType>(CType) &&
1346 !isa<ArrayType>(CType.getUnqualifiedType()))
1347 return 0;
1348
1349 // Apply any CVR qualifiers from the array type to the element type. This
1350 // implements C99 6.7.3p8: "If the specification of an array type includes
1351 // any type qualifiers, the element type is so qualified, not the array type."
1352
1353 // If we get here, we either have type qualifiers on the type, or we have
1354 // sugar such as a typedef in the way. If we have type qualifiers on the type
1355 // we must propagate them down into the elemeng type.
1356 unsigned CVRQuals = T.getCVRQualifiers();
1357 unsigned AddrSpace = 0;
1358 Type *Ty = T.getTypePtr();
1359
1360 // Rip through ASQualType's and typedefs to get to a concrete type.
1361 while (1) {
1362 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1363 AddrSpace = ASQT->getAddressSpace();
1364 Ty = ASQT->getBaseType();
1365 } else {
1366 T = Ty->getDesugaredType();
1367 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1368 break;
1369 CVRQuals |= T.getCVRQualifiers();
1370 Ty = T.getTypePtr();
1371 }
1372 }
1373
1374 // If we have a simple case, just return now.
1375 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1376 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1377 return ATy;
1378
1379 // Otherwise, we have an array and we have qualifiers on it. Push the
1380 // qualifiers into the array element type and return a new array type.
1381 // Get the canonical version of the element with the extra qualifiers on it.
1382 // This can recursively sink qualifiers through multiple levels of arrays.
1383 QualType NewEltTy = ATy->getElementType();
1384 if (AddrSpace)
1385 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1386 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1387
1388 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1389 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1390 CAT->getSizeModifier(),
1391 CAT->getIndexTypeQualifier()));
1392 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1393 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1394 IAT->getSizeModifier(),
1395 IAT->getIndexTypeQualifier()));
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001396
Douglas Gregor1b21c7f2008-12-05 23:32:09 +00001397 if (const DependentSizedArrayType *DSAT
1398 = dyn_cast<DependentSizedArrayType>(ATy))
1399 return cast<ArrayType>(
1400 getDependentSizedArrayType(NewEltTy,
1401 DSAT->getSizeExpr(),
1402 DSAT->getSizeModifier(),
1403 DSAT->getIndexTypeQualifier()));
Chris Lattnera1923f62008-08-04 07:31:14 +00001404
Chris Lattnera1923f62008-08-04 07:31:14 +00001405 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1406 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1407 VAT->getSizeModifier(),
1408 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001409}
1410
1411
Chris Lattner19eb97e2008-04-02 05:18:44 +00001412/// getArrayDecayedType - Return the properly qualified result of decaying the
1413/// specified array type to a pointer. This operation is non-trivial when
1414/// handling typedefs etc. The canonical type of "T" must be an array type,
1415/// this returns a pointer to a properly qualified element of the array.
1416///
1417/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1418QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001419 // Get the element type with 'getAsArrayType' so that we don't lose any
1420 // typedefs in the element type of the array. This also handles propagation
1421 // of type qualifiers from the array type into the element type if present
1422 // (C99 6.7.3p8).
1423 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1424 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001425
Chris Lattnera1923f62008-08-04 07:31:14 +00001426 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001427
1428 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001429 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001430}
1431
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001432QualType ASTContext::getBaseElementType(const VariableArrayType *VAT) {
Anders Carlsson76d19c82008-12-21 03:44:36 +00001433 QualType ElemTy = VAT->getElementType();
1434
1435 if (const VariableArrayType *VAT = getAsVariableArrayType(ElemTy))
1436 return getBaseElementType(VAT);
1437
1438 return ElemTy;
1439}
1440
Chris Lattner4b009652007-07-25 00:24:17 +00001441/// getFloatingRank - Return a relative rank for floating point types.
1442/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001443static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001444 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001445 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001446
Daniel Dunbar4a0b75c2009-01-05 22:14:37 +00001447 assert(T->getAsBuiltinType() && "getFloatingRank(): not a floating type");
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001448 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001449 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001450 case BuiltinType::Float: return FloatRank;
1451 case BuiltinType::Double: return DoubleRank;
1452 case BuiltinType::LongDouble: return LongDoubleRank;
1453 }
1454}
1455
Steve Narofffa0c4532007-08-27 01:41:48 +00001456/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1457/// point or a complex type (based on typeDomain/typeSize).
1458/// 'typeDomain' is a real floating point or complex type.
1459/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001460QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1461 QualType Domain) const {
1462 FloatingRank EltRank = getFloatingRank(Size);
1463 if (Domain->isComplexType()) {
1464 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001465 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001466 case FloatRank: return FloatComplexTy;
1467 case DoubleRank: return DoubleComplexTy;
1468 case LongDoubleRank: return LongDoubleComplexTy;
1469 }
Chris Lattner4b009652007-07-25 00:24:17 +00001470 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001471
1472 assert(Domain->isRealFloatingType() && "Unknown domain!");
1473 switch (EltRank) {
1474 default: assert(0 && "getFloatingRank(): illegal value for rank");
1475 case FloatRank: return FloatTy;
1476 case DoubleRank: return DoubleTy;
1477 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001478 }
Chris Lattner4b009652007-07-25 00:24:17 +00001479}
1480
Chris Lattner51285d82008-04-06 23:55:33 +00001481/// getFloatingTypeOrder - Compare the rank of the two specified floating
1482/// point types, ignoring the domain of the type (i.e. 'double' ==
1483/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1484/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001485int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1486 FloatingRank LHSR = getFloatingRank(LHS);
1487 FloatingRank RHSR = getFloatingRank(RHS);
1488
1489 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001490 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001491 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001492 return 1;
1493 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001494}
1495
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001496/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1497/// routine will assert if passed a built-in type that isn't an integer or enum,
1498/// or if it is not canonicalized.
1499static unsigned getIntegerRank(Type *T) {
1500 assert(T->isCanonical() && "T should be canonicalized");
1501 if (isa<EnumType>(T))
1502 return 4;
1503
1504 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001505 default: assert(0 && "getIntegerRank(): not a built-in integer");
1506 case BuiltinType::Bool:
1507 return 1;
1508 case BuiltinType::Char_S:
1509 case BuiltinType::Char_U:
1510 case BuiltinType::SChar:
1511 case BuiltinType::UChar:
1512 return 2;
1513 case BuiltinType::Short:
1514 case BuiltinType::UShort:
1515 return 3;
1516 case BuiltinType::Int:
1517 case BuiltinType::UInt:
1518 return 4;
1519 case BuiltinType::Long:
1520 case BuiltinType::ULong:
1521 return 5;
1522 case BuiltinType::LongLong:
1523 case BuiltinType::ULongLong:
1524 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001525 }
1526}
1527
Chris Lattner51285d82008-04-06 23:55:33 +00001528/// getIntegerTypeOrder - Returns the highest ranked integer type:
1529/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1530/// LHS < RHS, return -1.
1531int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001532 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1533 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001534 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001535
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001536 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1537 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001538
Chris Lattner51285d82008-04-06 23:55:33 +00001539 unsigned LHSRank = getIntegerRank(LHSC);
1540 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001541
Chris Lattner51285d82008-04-06 23:55:33 +00001542 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1543 if (LHSRank == RHSRank) return 0;
1544 return LHSRank > RHSRank ? 1 : -1;
1545 }
Chris Lattner4b009652007-07-25 00:24:17 +00001546
Chris Lattner51285d82008-04-06 23:55:33 +00001547 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1548 if (LHSUnsigned) {
1549 // If the unsigned [LHS] type is larger, return it.
1550 if (LHSRank >= RHSRank)
1551 return 1;
1552
1553 // If the signed type can represent all values of the unsigned type, it
1554 // wins. Because we are dealing with 2's complement and types that are
1555 // powers of two larger than each other, this is always safe.
1556 return -1;
1557 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001558
Chris Lattner51285d82008-04-06 23:55:33 +00001559 // If the unsigned [RHS] type is larger, return it.
1560 if (RHSRank >= LHSRank)
1561 return -1;
1562
1563 // If the signed type can represent all values of the unsigned type, it
1564 // wins. Because we are dealing with 2's complement and types that are
1565 // powers of two larger than each other, this is always safe.
1566 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001567}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001568
1569// getCFConstantStringType - Return the type used for constant CFStrings.
1570QualType ASTContext::getCFConstantStringType() {
1571 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001572 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001573 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Ted Kremenek2c984042008-09-05 01:34:33 +00001574 &Idents.get("NSConstantString"));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001575 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001576
1577 // const int *isa;
1578 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001579 // int flags;
1580 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001581 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001582 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001583 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001584 FieldTypes[3] = LongTy;
Douglas Gregor8acb7272008-12-11 16:49:14 +00001585
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001586 // Create fields
Douglas Gregor8acb7272008-12-11 16:49:14 +00001587 for (unsigned i = 0; i < 4; ++i) {
1588 FieldDecl *Field = FieldDecl::Create(*this, CFConstantStringTypeDecl,
1589 SourceLocation(), 0,
1590 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001591 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001592 CFConstantStringTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001593 }
1594
1595 CFConstantStringTypeDecl->completeDefinition(*this);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001596 }
1597
1598 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001599}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001600
Anders Carlssonf58cac72008-08-30 19:34:46 +00001601QualType ASTContext::getObjCFastEnumerationStateType()
1602{
1603 if (!ObjCFastEnumerationStateTypeDecl) {
Douglas Gregor8acb7272008-12-11 16:49:14 +00001604 ObjCFastEnumerationStateTypeDecl =
1605 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
1606 &Idents.get("__objcFastEnumerationState"));
1607
Anders Carlssonf58cac72008-08-30 19:34:46 +00001608 QualType FieldTypes[] = {
1609 UnsignedLongTy,
1610 getPointerType(ObjCIdType),
1611 getPointerType(UnsignedLongTy),
1612 getConstantArrayType(UnsignedLongTy,
1613 llvm::APInt(32, 5), ArrayType::Normal, 0)
1614 };
1615
Douglas Gregor8acb7272008-12-11 16:49:14 +00001616 for (size_t i = 0; i < 4; ++i) {
1617 FieldDecl *Field = FieldDecl::Create(*this,
1618 ObjCFastEnumerationStateTypeDecl,
1619 SourceLocation(), 0,
1620 FieldTypes[i], /*BitWidth=*/0,
Douglas Gregoraf8ad2b2009-01-20 01:17:11 +00001621 /*Mutable=*/false);
Douglas Gregor03b2ad22009-01-12 23:27:07 +00001622 ObjCFastEnumerationStateTypeDecl->addDecl(Field);
Douglas Gregor8acb7272008-12-11 16:49:14 +00001623 }
Anders Carlssonf58cac72008-08-30 19:34:46 +00001624
Douglas Gregor8acb7272008-12-11 16:49:14 +00001625 ObjCFastEnumerationStateTypeDecl->completeDefinition(*this);
Anders Carlssonf58cac72008-08-30 19:34:46 +00001626 }
1627
1628 return getTagDeclType(ObjCFastEnumerationStateTypeDecl);
1629}
1630
Anders Carlssone3f02572007-10-29 06:33:42 +00001631// This returns true if a type has been typedefed to BOOL:
1632// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001633static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001634 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner85fb3842008-11-24 03:52:59 +00001635 if (IdentifierInfo *II = TT->getDecl()->getIdentifier())
1636 return II->isStr("BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001637
1638 return false;
1639}
1640
Ted Kremenek42730c52008-01-07 19:49:32 +00001641/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001642/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001643int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001644 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001645
1646 // Make all integer and enum types at least as large as an int
1647 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001648 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001649 // Treat arrays as pointers, since that's how they're passed in.
1650 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001651 sz = getTypeSize(VoidPtrTy);
1652 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001653}
1654
Ted Kremenek42730c52008-01-07 19:49:32 +00001655/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001656/// declaration.
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001657void ASTContext::getObjCEncodingForMethodDecl(const ObjCMethodDecl *Decl,
Chris Lattnerae430292008-11-19 07:24:05 +00001658 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001659 // FIXME: This is not very efficient.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001660 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001661 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001662 // Encode result type.
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001663 getObjCEncodingForType(Decl->getResultType(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001664 // Compute size of all parameters.
1665 // Start with computing size of a pointer in number of bytes.
1666 // FIXME: There might(should) be a better way of doing this computation!
1667 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001668 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001669 // The first two arguments (self and _cmd) are pointers; account for
1670 // their size.
1671 int ParmOffset = 2 * PtrSize;
1672 int NumOfParams = Decl->getNumParams();
1673 for (int i = 0; i < NumOfParams; i++) {
1674 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001675 int sz = getObjCEncodingTypeSize (PType);
1676 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001677 ParmOffset += sz;
1678 }
1679 S += llvm::utostr(ParmOffset);
1680 S += "@0:";
1681 S += llvm::utostr(PtrSize);
1682
1683 // Argument types.
1684 ParmOffset = 2 * PtrSize;
1685 for (int i = 0; i < NumOfParams; i++) {
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001686 ParmVarDecl *PVDecl = Decl->getParamDecl(i);
1687 QualType PType = PVDecl->getOriginalType();
1688 if (const ArrayType *AT =
1689 dyn_cast<ArrayType>(PType->getCanonicalTypeInternal()))
1690 // Use array's original type only if it has known number of
1691 // elements.
1692 if (!dyn_cast<ConstantArrayType>(AT))
1693 PType = PVDecl->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001694 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001695 // 'in', 'inout', etc.
Fariborz Jahaniane26cb432008-12-20 23:29:59 +00001696 getObjCEncodingForTypeQualifier(PVDecl->getObjCDeclQualifier(), S);
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00001697 getObjCEncodingForType(PType, S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001698 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001699 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001700 }
1701}
1702
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001703/// getObjCEncodingForPropertyDecl - Return the encoded type for this
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001704/// property declaration. If non-NULL, Container must be either an
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001705/// ObjCCategoryImplDecl or ObjCImplementationDecl; it should only be
1706/// NULL when getting encodings for protocol properties.
Fariborz Jahanian501ef5c2009-01-20 20:04:12 +00001707/// Property attributes are stored as a comma-delimited C string. The simple
1708/// attributes readonly and bycopy are encoded as single characters. The
1709/// parametrized attributes, getter=name, setter=name, and ivar=name, are
1710/// encoded as single characters, followed by an identifier. Property types
1711/// are also encoded as a parametrized attribute. The characters used to encode
1712/// these attributes are defined by the following enumeration:
1713/// @code
1714/// enum PropertyAttributes {
1715/// kPropertyReadOnly = 'R', // property is read-only.
1716/// kPropertyBycopy = 'C', // property is a copy of the value last assigned
1717/// kPropertyByref = '&', // property is a reference to the value last assigned
1718/// kPropertyDynamic = 'D', // property is dynamic
1719/// kPropertyGetter = 'G', // followed by getter selector name
1720/// kPropertySetter = 'S', // followed by setter selector name
1721/// kPropertyInstanceVariable = 'V' // followed by instance variable name
1722/// kPropertyType = 't' // followed by old-style type encoding.
1723/// kPropertyWeak = 'W' // 'weak' property
1724/// kPropertyStrong = 'P' // property GC'able
1725/// kPropertyNonAtomic = 'N' // property non-atomic
1726/// };
1727/// @endcode
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001728void ASTContext::getObjCEncodingForPropertyDecl(const ObjCPropertyDecl *PD,
1729 const Decl *Container,
Chris Lattnerae430292008-11-19 07:24:05 +00001730 std::string& S) {
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001731 // Collect information from the property implementation decl(s).
1732 bool Dynamic = false;
1733 ObjCPropertyImplDecl *SynthesizePID = 0;
1734
1735 // FIXME: Duplicated code due to poor abstraction.
1736 if (Container) {
1737 if (const ObjCCategoryImplDecl *CID =
1738 dyn_cast<ObjCCategoryImplDecl>(Container)) {
1739 for (ObjCCategoryImplDecl::propimpl_iterator
1740 i = CID->propimpl_begin(), e = CID->propimpl_end(); i != e; ++i) {
1741 ObjCPropertyImplDecl *PID = *i;
1742 if (PID->getPropertyDecl() == PD) {
1743 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1744 Dynamic = true;
1745 } else {
1746 SynthesizePID = PID;
1747 }
1748 }
1749 }
1750 } else {
Chris Lattner2fda0ed2008-10-05 17:34:18 +00001751 const ObjCImplementationDecl *OID=cast<ObjCImplementationDecl>(Container);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001752 for (ObjCCategoryImplDecl::propimpl_iterator
1753 i = OID->propimpl_begin(), e = OID->propimpl_end(); i != e; ++i) {
1754 ObjCPropertyImplDecl *PID = *i;
1755 if (PID->getPropertyDecl() == PD) {
1756 if (PID->getPropertyImplementation()==ObjCPropertyImplDecl::Dynamic) {
1757 Dynamic = true;
1758 } else {
1759 SynthesizePID = PID;
1760 }
1761 }
1762 }
1763 }
1764 }
1765
1766 // FIXME: This is not very efficient.
1767 S = "T";
1768
1769 // Encode result type.
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001770 // GCC has some special rules regarding encoding of properties which
1771 // closely resembles encoding of ivars.
1772 getObjCEncodingForTypeImpl(PD->getType(), S, true, true, NULL,
1773 true /* outermost type */,
1774 true /* encoding for property */);
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001775
1776 if (PD->isReadOnly()) {
1777 S += ",R";
1778 } else {
1779 switch (PD->getSetterKind()) {
1780 case ObjCPropertyDecl::Assign: break;
1781 case ObjCPropertyDecl::Copy: S += ",C"; break;
1782 case ObjCPropertyDecl::Retain: S += ",&"; break;
1783 }
1784 }
1785
1786 // It really isn't clear at all what this means, since properties
1787 // are "dynamic by default".
1788 if (Dynamic)
1789 S += ",D";
1790
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001791 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_nonatomic)
1792 S += ",N";
1793
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001794 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_getter) {
1795 S += ",G";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001796 S += PD->getGetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001797 }
1798
1799 if (PD->getPropertyAttributes() & ObjCPropertyDecl::OBJC_PR_setter) {
1800 S += ",S";
Chris Lattner3a8f2942008-11-24 03:33:13 +00001801 S += PD->getSetterName().getAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001802 }
1803
1804 if (SynthesizePID) {
1805 const ObjCIvarDecl *OID = SynthesizePID->getPropertyIvarDecl();
1806 S += ",V";
Chris Lattner6c5ec622008-11-24 04:00:27 +00001807 S += OID->getNameAsString();
Daniel Dunbar698d6f32008-08-28 04:38:10 +00001808 }
1809
1810 // FIXME: OBJCGC: weak & strong
1811}
1812
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001813/// getLegacyIntegralTypeEncoding -
1814/// Another legacy compatibility encoding: 32-bit longs are encoded as
1815/// 'l' or 'L', but not always. For typedefs, we need to use
1816/// 'i' or 'I' instead if encoding a struct field, or a pointer!
1817///
1818void ASTContext::getLegacyIntegralTypeEncoding (QualType &PointeeTy) const {
1819 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
1820 if (const BuiltinType *BT = PointeeTy->getAsBuiltinType()) {
1821 if (BT->getKind() == BuiltinType::ULong)
1822 PointeeTy = UnsignedIntTy;
1823 else if (BT->getKind() == BuiltinType::Long)
1824 PointeeTy = IntTy;
1825 }
1826 }
1827}
1828
Fariborz Jahanian248db262008-01-22 22:44:46 +00001829void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001830 FieldDecl *Field) const {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001831 // We follow the behavior of gcc, expanding structures which are
1832 // directly pointed to, and expanding embedded structures. Note that
1833 // these rules are sufficient to prevent recursive encoding of the
1834 // same type.
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00001835 getObjCEncodingForTypeImpl(T, S, true, true, Field,
1836 true /* outermost type */);
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001837}
1838
Fariborz Jahaniand1361952009-01-13 01:18:13 +00001839static void EncodeBitField(const ASTContext *Context, std::string& S,
1840 FieldDecl *FD) {
1841 const Expr *E = FD->getBitWidth();
1842 assert(E && "bitfield width not there - getObjCEncodingForTypeImpl");
1843 ASTContext *Ctx = const_cast<ASTContext*>(Context);
1844 unsigned N = E->getIntegerConstantExprValue(*Ctx).getZExtValue();
1845 S += 'b';
1846 S += llvm::utostr(N);
1847}
1848
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00001849void ASTContext::getObjCEncodingForTypeImpl(QualType T, std::string& S,
1850 bool ExpandPointedToStructures,
1851 bool ExpandStructures,
Fariborz Jahanian89ed86b2008-12-22 23:22:27 +00001852 FieldDecl *FD,
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001853 bool OutermostType,
1854 bool EncodingProperty) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00001855 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001856 if (FD && FD->isBitField()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00001857 EncodeBitField(this, S, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001858 }
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001859 else {
1860 char encoding;
1861 switch (BT->getKind()) {
1862 default: assert(0 && "Unhandled builtin type kind");
1863 case BuiltinType::Void: encoding = 'v'; break;
1864 case BuiltinType::Bool: encoding = 'B'; break;
1865 case BuiltinType::Char_U:
1866 case BuiltinType::UChar: encoding = 'C'; break;
1867 case BuiltinType::UShort: encoding = 'S'; break;
1868 case BuiltinType::UInt: encoding = 'I'; break;
1869 case BuiltinType::ULong: encoding = 'L'; break;
1870 case BuiltinType::ULongLong: encoding = 'Q'; break;
1871 case BuiltinType::Char_S:
1872 case BuiltinType::SChar: encoding = 'c'; break;
1873 case BuiltinType::Short: encoding = 's'; break;
1874 case BuiltinType::Int: encoding = 'i'; break;
1875 case BuiltinType::Long: encoding = 'l'; break;
1876 case BuiltinType::LongLong: encoding = 'q'; break;
1877 case BuiltinType::Float: encoding = 'f'; break;
1878 case BuiltinType::Double: encoding = 'd'; break;
1879 case BuiltinType::LongDouble: encoding = 'd'; break;
1880 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001881
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001882 S += encoding;
1883 }
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001884 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001885 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001886 getObjCEncodingForTypeImpl(getObjCIdType(), S,
1887 ExpandPointedToStructures,
1888 ExpandStructures, FD);
1889 if (FD || EncodingProperty) {
1890 // Note that we do extended encoding of protocol qualifer list
1891 // Only when doing ivar or property encoding.
1892 const ObjCQualifiedIdType *QIDT = T->getAsObjCQualifiedIdType();
1893 S += '"';
1894 for (unsigned i =0; i < QIDT->getNumProtocols(); i++) {
1895 ObjCProtocolDecl *Proto = QIDT->getProtocols(i);
1896 S += '<';
1897 S += Proto->getNameAsString();
1898 S += '>';
1899 }
1900 S += '"';
1901 }
1902 return;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001903 }
1904 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001905 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001906 bool isReadOnly = false;
1907 // For historical/compatibility reasons, the read-only qualifier of the
1908 // pointee gets emitted _before_ the '^'. The read-only qualifier of
1909 // the pointer itself gets ignored, _unless_ we are looking at a typedef!
1910 // Also, do not emit the 'r' for anything but the outermost type!
1911 if (dyn_cast<TypedefType>(T.getTypePtr())) {
1912 if (OutermostType && T.isConstQualified()) {
1913 isReadOnly = true;
1914 S += 'r';
1915 }
1916 }
1917 else if (OutermostType) {
1918 QualType P = PointeeTy;
1919 while (P->getAsPointerType())
1920 P = P->getAsPointerType()->getPointeeType();
1921 if (P.isConstQualified()) {
1922 isReadOnly = true;
1923 S += 'r';
1924 }
1925 }
1926 if (isReadOnly) {
1927 // Another legacy compatibility encoding. Some ObjC qualifier and type
1928 // combinations need to be rearranged.
1929 // Rewrite "in const" from "nr" to "rn"
1930 const char * s = S.c_str();
1931 int len = S.length();
1932 if (len >= 2 && s[len-2] == 'n' && s[len-1] == 'r') {
1933 std::string replace = "rn";
1934 S.replace(S.end()-2, S.end(), replace);
1935 }
1936 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00001937 if (isObjCIdType(PointeeTy)) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001938 S += '@';
1939 return;
Fariborz Jahanianc8679472008-12-19 00:14:49 +00001940 }
1941 else if (PointeeTy->isObjCInterfaceType()) {
Fariborz Jahaniand3498aa2008-12-23 21:30:15 +00001942 if (dyn_cast<TypedefType>(PointeeTy.getTypePtr())) {
1943 // Another historical/compatibility reason.
1944 // We encode the underlying type which comes out as
1945 // {...};
1946 S += '^';
1947 getObjCEncodingForTypeImpl(PointeeTy, S,
1948 false, ExpandPointedToStructures,
1949 NULL);
1950 return;
1951 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00001952 S += '@';
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001953 if (FD || EncodingProperty) {
1954 const ObjCInterfaceType *OIT = PointeeTy->getAsObjCInterfaceType();
1955 ObjCInterfaceDecl *OI = OIT->getDecl();
Fariborz Jahanian320ac422008-12-20 19:17:01 +00001956 S += '"';
1957 S += OI->getNameAsCString();
Fariborz Jahanian892d5db2009-01-20 19:14:18 +00001958 for (unsigned i =0; i < OIT->getNumProtocols(); i++) {
1959 ObjCProtocolDecl *Proto = OIT->getProtocol(i);
1960 S += '<';
1961 S += Proto->getNameAsString();
1962 S += '>';
1963 }
Fariborz Jahanian320ac422008-12-20 19:17:01 +00001964 S += '"';
1965 }
Fariborz Jahanianc8679472008-12-19 00:14:49 +00001966 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001967 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001968 S += '#';
1969 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001970 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001971 S += ':';
1972 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001973 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001974
1975 if (PointeeTy->isCharType()) {
1976 // char pointer types should be encoded as '*' unless it is a
1977 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001978 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001979 S += '*';
1980 return;
1981 }
1982 }
1983
1984 S += '^';
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00001985 getLegacyIntegralTypeEncoding(PointeeTy);
1986
1987 getObjCEncodingForTypeImpl(PointeeTy, S,
Daniel Dunbaraa913102008-10-17 16:17:37 +00001988 false, ExpandPointedToStructures,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00001989 NULL);
Chris Lattnera1923f62008-08-04 07:31:14 +00001990 } else if (const ArrayType *AT =
1991 // Ignore type qualifiers etc.
1992 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001993 S += '[';
1994
1995 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1996 S += llvm::utostr(CAT->getSize().getZExtValue());
1997 else
1998 assert(0 && "Unhandled array type!");
1999
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002000 getObjCEncodingForTypeImpl(AT->getElementType(), S,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002001 false, ExpandStructures, FD);
Anders Carlsson36f07d82007-10-29 05:01:08 +00002002 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00002003 } else if (T->getAsFunctionType()) {
2004 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002005 } else if (const RecordType *RTy = T->getAsRecordType()) {
Daniel Dunbarf8cfe562008-10-17 07:30:50 +00002006 RecordDecl *RDecl = RTy->getDecl();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002007 S += RDecl->isUnion() ? '(' : '{';
Daniel Dunbar146b2d02008-10-17 06:22:57 +00002008 // Anonymous structures print as '?'
2009 if (const IdentifierInfo *II = RDecl->getIdentifier()) {
2010 S += II->getName();
2011 } else {
2012 S += '?';
2013 }
Daniel Dunbarc9197cd2008-10-17 20:21:44 +00002014 if (ExpandStructures) {
Fariborz Jahanian248db262008-01-22 22:44:46 +00002015 S += '=';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002016 for (RecordDecl::field_iterator Field = RDecl->field_begin(),
2017 FieldEnd = RDecl->field_end();
2018 Field != FieldEnd; ++Field) {
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002019 if (FD) {
Daniel Dunbaraa913102008-10-17 16:17:37 +00002020 S += '"';
Douglas Gregor8acb7272008-12-11 16:49:14 +00002021 S += Field->getNameAsString();
Daniel Dunbaraa913102008-10-17 16:17:37 +00002022 S += '"';
2023 }
2024
2025 // Special case bit-fields.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002026 if (Field->isBitField()) {
2027 getObjCEncodingForTypeImpl(Field->getType(), S, false, true,
2028 (*Field));
Daniel Dunbaraa913102008-10-17 16:17:37 +00002029 } else {
Fariborz Jahaniane07d9ec2008-12-23 19:56:47 +00002030 QualType qt = Field->getType();
2031 getLegacyIntegralTypeEncoding(qt);
2032 getObjCEncodingForTypeImpl(qt, S, false, true,
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002033 FD);
Daniel Dunbaraa913102008-10-17 16:17:37 +00002034 }
Fariborz Jahanian248db262008-01-22 22:44:46 +00002035 }
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00002036 }
Daniel Dunbaraa913102008-10-17 16:17:37 +00002037 S += RDecl->isUnion() ? ')' : '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00002038 } else if (T->isEnumeralType()) {
Fariborz Jahaniand1361952009-01-13 01:18:13 +00002039 if (FD && FD->isBitField())
2040 EncodeBitField(this, S, FD);
2041 else
2042 S += 'i';
Steve Naroff62f09f52008-09-24 15:05:44 +00002043 } else if (T->isBlockPointerType()) {
2044 S += '^'; // This type string is the same as general pointers.
Fariborz Jahanian0cd547f2008-12-19 23:34:38 +00002045 } else if (T->isObjCInterfaceType()) {
2046 // @encode(class_name)
2047 ObjCInterfaceDecl *OI = T->getAsObjCInterfaceType()->getDecl();
2048 S += '{';
2049 const IdentifierInfo *II = OI->getIdentifier();
2050 S += II->getName();
2051 S += '=';
2052 std::vector<FieldDecl*> RecFields;
2053 CollectObjCIvars(OI, RecFields);
2054 for (unsigned int i = 0; i != RecFields.size(); i++) {
2055 if (RecFields[i]->isBitField())
2056 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2057 RecFields[i]);
2058 else
2059 getObjCEncodingForTypeImpl(RecFields[i]->getType(), S, false, true,
2060 FD);
2061 }
2062 S += '}';
2063 }
2064 else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00002065 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00002066}
2067
Ted Kremenek42730c52008-01-07 19:49:32 +00002068void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00002069 std::string& S) const {
2070 if (QT & Decl::OBJC_TQ_In)
2071 S += 'n';
2072 if (QT & Decl::OBJC_TQ_Inout)
2073 S += 'N';
2074 if (QT & Decl::OBJC_TQ_Out)
2075 S += 'o';
2076 if (QT & Decl::OBJC_TQ_Bycopy)
2077 S += 'O';
2078 if (QT & Decl::OBJC_TQ_Byref)
2079 S += 'R';
2080 if (QT & Decl::OBJC_TQ_Oneway)
2081 S += 'V';
2082}
2083
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00002084void ASTContext::setBuiltinVaListType(QualType T)
2085{
2086 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
2087
2088 BuiltinVaListType = T;
2089}
2090
Ted Kremenek42730c52008-01-07 19:49:32 +00002091void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00002092{
Ted Kremenek42730c52008-01-07 19:49:32 +00002093 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00002094
2095 // typedef struct objc_object *id;
2096 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002097 // User error - caller will issue diagnostics.
2098 if (!ptr)
2099 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002100 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002101 // User error - caller will issue diagnostics.
2102 if (!rec)
2103 return;
Steve Naroff9d12c902007-10-15 14:41:52 +00002104 IdStructType = rec;
2105}
2106
Ted Kremenek42730c52008-01-07 19:49:32 +00002107void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002108{
Ted Kremenek42730c52008-01-07 19:49:32 +00002109 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002110
2111 // typedef struct objc_selector *SEL;
2112 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002113 if (!ptr)
2114 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002115 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
Fariborz Jahaniande939672009-01-16 19:58:32 +00002116 if (!rec)
2117 return;
Fariborz Jahanianf807c202007-10-16 20:40:23 +00002118 SelStructType = rec;
2119}
2120
Ted Kremenek42730c52008-01-07 19:49:32 +00002121void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002122{
Ted Kremenek42730c52008-01-07 19:49:32 +00002123 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00002124}
2125
Ted Kremenek42730c52008-01-07 19:49:32 +00002126void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002127{
Ted Kremenek42730c52008-01-07 19:49:32 +00002128 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00002129
2130 // typedef struct objc_class *Class;
2131 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
2132 assert(ptr && "'Class' incorrectly typed");
2133 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
2134 assert(rec && "'Class' incorrectly typed");
2135 ClassStructType = rec;
2136}
2137
Ted Kremenek42730c52008-01-07 19:49:32 +00002138void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
2139 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00002140 "'NSConstantString' type already set!");
2141
Ted Kremenek42730c52008-01-07 19:49:32 +00002142 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00002143}
2144
Douglas Gregorc6507e42008-11-03 14:12:49 +00002145/// getFromTargetType - Given one of the integer types provided by
Douglas Gregorbb66b412008-11-03 15:57:00 +00002146/// TargetInfo, produce the corresponding type. The unsigned @p Type
2147/// is actually a value of type @c TargetInfo::IntType.
2148QualType ASTContext::getFromTargetType(unsigned Type) const {
Douglas Gregorc6507e42008-11-03 14:12:49 +00002149 switch (Type) {
2150 case TargetInfo::NoInt: return QualType();
2151 case TargetInfo::SignedShort: return ShortTy;
2152 case TargetInfo::UnsignedShort: return UnsignedShortTy;
2153 case TargetInfo::SignedInt: return IntTy;
2154 case TargetInfo::UnsignedInt: return UnsignedIntTy;
2155 case TargetInfo::SignedLong: return LongTy;
2156 case TargetInfo::UnsignedLong: return UnsignedLongTy;
2157 case TargetInfo::SignedLongLong: return LongLongTy;
2158 case TargetInfo::UnsignedLongLong: return UnsignedLongLongTy;
2159 }
2160
2161 assert(false && "Unhandled TargetInfo::IntType value");
Daniel Dunbar7b0dcc22008-11-11 01:16:00 +00002162 return QualType();
Douglas Gregorc6507e42008-11-03 14:12:49 +00002163}
Ted Kremenek118930e2008-07-24 23:58:27 +00002164
2165//===----------------------------------------------------------------------===//
2166// Type Predicates.
2167//===----------------------------------------------------------------------===//
2168
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002169/// isObjCNSObjectType - Return true if this is an NSObject object using
2170/// NSObject attribute on a c-style pointer type.
2171/// FIXME - Make it work directly on types.
2172///
2173bool ASTContext::isObjCNSObjectType(QualType Ty) const {
2174 if (TypedefType *TDT = dyn_cast<TypedefType>(Ty)) {
2175 if (TypedefDecl *TD = TDT->getDecl())
2176 if (TD->getAttr<ObjCNSObjectAttr>())
2177 return true;
2178 }
2179 return false;
2180}
2181
Ted Kremenek118930e2008-07-24 23:58:27 +00002182/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
2183/// to an object type. This includes "id" and "Class" (two 'special' pointers
2184/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
2185/// ID type).
2186bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
2187 if (Ty->isObjCQualifiedIdType())
2188 return true;
2189
Steve Naroffd9e00802008-10-21 18:24:04 +00002190 // Blocks are objects.
2191 if (Ty->isBlockPointerType())
2192 return true;
2193
2194 // All other object types are pointers.
Ted Kremenek118930e2008-07-24 23:58:27 +00002195 if (!Ty->isPointerType())
2196 return false;
2197
2198 // Check to see if this is 'id' or 'Class', both of which are typedefs for
2199 // pointer types. This looks for the typedef specifically, not for the
2200 // underlying type.
2201 if (Ty == getObjCIdType() || Ty == getObjCClassType())
2202 return true;
2203
2204 // If this a pointer to an interface (e.g. NSString*), it is ok.
Fariborz Jahanian82f54962009-01-13 23:34:40 +00002205 if (Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType())
2206 return true;
2207
2208 // If is has NSObject attribute, OK as well.
2209 return isObjCNSObjectType(Ty);
Ted Kremenek118930e2008-07-24 23:58:27 +00002210}
2211
Chris Lattner6ff358b2008-04-07 06:51:04 +00002212//===----------------------------------------------------------------------===//
2213// Type Compatibility Testing
2214//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00002215
Steve Naroff3454b6c2008-09-04 15:10:53 +00002216/// typesAreBlockCompatible - This routine is called when comparing two
Steve Naroffd6163f32008-09-05 22:11:13 +00002217/// block types. Types must be strictly compatible here. For example,
2218/// C unfortunately doesn't produce an error for the following:
2219///
2220/// int (*emptyArgFunc)();
2221/// int (*intArgList)(int) = emptyArgFunc;
2222///
2223/// For blocks, we will produce an error for the following (similar to C++):
2224///
2225/// int (^emptyArgBlock)();
2226/// int (^intArgBlock)(int) = emptyArgBlock;
2227///
2228/// FIXME: When the dust settles on this integration, fold this into mergeTypes.
2229///
Steve Naroff3454b6c2008-09-04 15:10:53 +00002230bool ASTContext::typesAreBlockCompatible(QualType lhs, QualType rhs) {
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002231 const FunctionType *lbase = lhs->getAsFunctionType();
2232 const FunctionType *rbase = rhs->getAsFunctionType();
2233 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2234 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2235 if (lproto && rproto)
2236 return !mergeTypes(lhs, rhs).isNull();
2237 return false;
Steve Naroff3454b6c2008-09-04 15:10:53 +00002238}
2239
Chris Lattner6ff358b2008-04-07 06:51:04 +00002240/// areCompatVectorTypes - Return true if the two specified vector types are
2241/// compatible.
2242static bool areCompatVectorTypes(const VectorType *LHS,
2243 const VectorType *RHS) {
2244 assert(LHS->isCanonical() && RHS->isCanonical());
2245 return LHS->getElementType() == RHS->getElementType() &&
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002246 LHS->getNumElements() == RHS->getNumElements();
Chris Lattner6ff358b2008-04-07 06:51:04 +00002247}
2248
Eli Friedman0d9549b2008-08-22 00:56:42 +00002249/// canAssignObjCInterfaces - Return true if the two interface types are
Chris Lattner6ff358b2008-04-07 06:51:04 +00002250/// compatible for assignment from RHS to LHS. This handles validation of any
2251/// protocol qualifiers on the LHS or RHS.
2252///
Eli Friedman0d9549b2008-08-22 00:56:42 +00002253bool ASTContext::canAssignObjCInterfaces(const ObjCInterfaceType *LHS,
2254 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00002255 // Verify that the base decls are compatible: the RHS must be a subclass of
2256 // the LHS.
2257 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
2258 return false;
2259
2260 // RHS must have a superset of the protocols in the LHS. If the LHS is not
2261 // protocol qualified at all, then we are good.
2262 if (!isa<ObjCQualifiedInterfaceType>(LHS))
2263 return true;
2264
2265 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
2266 // isn't a superset.
2267 if (!isa<ObjCQualifiedInterfaceType>(RHS))
2268 return true; // FIXME: should return false!
2269
2270 // Finally, we must have two protocol-qualified interfaces.
2271 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
2272 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
2273 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
2274 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
2275 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
2276 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
2277
2278 // All protocols in LHS must have a presence in RHS. Since the protocol lists
2279 // are both sorted alphabetically and have no duplicates, we can scan RHS and
2280 // LHS in a single parallel scan until we run out of elements in LHS.
2281 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
2282 ObjCProtocolDecl *LHSProto = *LHSPI;
2283
2284 while (RHSPI != RHSPE) {
2285 ObjCProtocolDecl *RHSProto = *RHSPI++;
2286 // If the RHS has a protocol that the LHS doesn't, ignore it.
2287 if (RHSProto != LHSProto)
2288 continue;
2289
2290 // Otherwise, the RHS does have this element.
2291 ++LHSPI;
2292 if (LHSPI == LHSPE)
2293 return true; // All protocols in LHS exist in RHS.
2294
2295 LHSProto = *LHSPI;
2296 }
2297
2298 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
2299 return false;
2300}
2301
Steve Naroff85f0dc52007-10-15 20:41:53 +00002302/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
2303/// both shall have the identically qualified version of a compatible type.
2304/// C99 6.2.7p1: Two types have compatible types if their types are the
2305/// same. See 6.7.[2,3,5] for additional rules.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002306bool ASTContext::typesAreCompatible(QualType LHS, QualType RHS) {
2307 return !mergeTypes(LHS, RHS).isNull();
2308}
2309
2310QualType ASTContext::mergeFunctionTypes(QualType lhs, QualType rhs) {
2311 const FunctionType *lbase = lhs->getAsFunctionType();
2312 const FunctionType *rbase = rhs->getAsFunctionType();
2313 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
2314 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
2315 bool allLTypes = true;
2316 bool allRTypes = true;
2317
2318 // Check return type
2319 QualType retType = mergeTypes(lbase->getResultType(), rbase->getResultType());
2320 if (retType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002321 if (getCanonicalType(retType) != getCanonicalType(lbase->getResultType()))
2322 allLTypes = false;
2323 if (getCanonicalType(retType) != getCanonicalType(rbase->getResultType()))
2324 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002325
2326 if (lproto && rproto) { // two C99 style function prototypes
2327 unsigned lproto_nargs = lproto->getNumArgs();
2328 unsigned rproto_nargs = rproto->getNumArgs();
2329
2330 // Compatible functions must have the same number of arguments
2331 if (lproto_nargs != rproto_nargs)
2332 return QualType();
2333
2334 // Variadic and non-variadic functions aren't compatible
2335 if (lproto->isVariadic() != rproto->isVariadic())
2336 return QualType();
2337
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002338 if (lproto->getTypeQuals() != rproto->getTypeQuals())
2339 return QualType();
2340
Eli Friedman0d9549b2008-08-22 00:56:42 +00002341 // Check argument compatibility
2342 llvm::SmallVector<QualType, 10> types;
2343 for (unsigned i = 0; i < lproto_nargs; i++) {
2344 QualType largtype = lproto->getArgType(i).getUnqualifiedType();
2345 QualType rargtype = rproto->getArgType(i).getUnqualifiedType();
2346 QualType argtype = mergeTypes(largtype, rargtype);
2347 if (argtype.isNull()) return QualType();
2348 types.push_back(argtype);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002349 if (getCanonicalType(argtype) != getCanonicalType(largtype))
2350 allLTypes = false;
2351 if (getCanonicalType(argtype) != getCanonicalType(rargtype))
2352 allRTypes = false;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002353 }
2354 if (allLTypes) return lhs;
2355 if (allRTypes) return rhs;
2356 return getFunctionType(retType, types.begin(), types.size(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002357 lproto->isVariadic(), lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002358 }
2359
2360 if (lproto) allRTypes = false;
2361 if (rproto) allLTypes = false;
2362
2363 const FunctionTypeProto *proto = lproto ? lproto : rproto;
2364 if (proto) {
2365 if (proto->isVariadic()) return QualType();
2366 // Check that the types are compatible with the types that
2367 // would result from default argument promotions (C99 6.7.5.3p15).
2368 // The only types actually affected are promotable integer
2369 // types and floats, which would be passed as a different
2370 // type depending on whether the prototype is visible.
2371 unsigned proto_nargs = proto->getNumArgs();
2372 for (unsigned i = 0; i < proto_nargs; ++i) {
2373 QualType argTy = proto->getArgType(i);
2374 if (argTy->isPromotableIntegerType() ||
2375 getCanonicalType(argTy).getUnqualifiedType() == FloatTy)
2376 return QualType();
2377 }
2378
2379 if (allLTypes) return lhs;
2380 if (allRTypes) return rhs;
2381 return getFunctionType(retType, proto->arg_type_begin(),
Argiris Kirtzidis65b99642008-10-26 16:43:14 +00002382 proto->getNumArgs(), lproto->isVariadic(),
2383 lproto->getTypeQuals());
Eli Friedman0d9549b2008-08-22 00:56:42 +00002384 }
2385
2386 if (allLTypes) return lhs;
2387 if (allRTypes) return rhs;
2388 return getFunctionTypeNoProto(retType);
2389}
2390
2391QualType ASTContext::mergeTypes(QualType LHS, QualType RHS) {
Bill Wendling6a9d8542007-12-03 07:33:35 +00002392 // C++ [expr]: If an expression initially has the type "reference to T", the
2393 // type is adjusted to "T" prior to any further analysis, the expression
2394 // designates the object or function denoted by the reference, and the
2395 // expression is an lvalue.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002396 // FIXME: C++ shouldn't be going through here! The rules are different
2397 // enough that they should be handled separately.
2398 if (const ReferenceType *RT = LHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002399 LHS = RT->getPointeeType();
Eli Friedman0d9549b2008-08-22 00:56:42 +00002400 if (const ReferenceType *RT = RHS->getAsReferenceType())
Chris Lattner855fed42008-04-07 04:07:56 +00002401 RHS = RT->getPointeeType();
Chris Lattnerd47d6042008-04-07 05:37:56 +00002402
Eli Friedman0d9549b2008-08-22 00:56:42 +00002403 QualType LHSCan = getCanonicalType(LHS),
2404 RHSCan = getCanonicalType(RHS);
2405
2406 // If two types are identical, they are compatible.
2407 if (LHSCan == RHSCan)
2408 return LHS;
2409
2410 // If the qualifiers are different, the types aren't compatible
2411 if (LHSCan.getCVRQualifiers() != RHSCan.getCVRQualifiers() ||
2412 LHSCan.getAddressSpace() != RHSCan.getAddressSpace())
2413 return QualType();
2414
2415 Type::TypeClass LHSClass = LHSCan->getTypeClass();
2416 Type::TypeClass RHSClass = RHSCan->getTypeClass();
2417
Chris Lattnerc38d4522008-01-14 05:45:46 +00002418 // We want to consider the two function types to be the same for these
2419 // comparisons, just force one to the other.
2420 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
2421 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00002422
2423 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00002424 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
2425 LHSClass = Type::ConstantArray;
2426 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
2427 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00002428
Nate Begemanaf6ed502008-04-18 23:10:10 +00002429 // Canonicalize ExtVector -> Vector.
2430 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
2431 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00002432
Chris Lattner7cdcb252008-04-07 06:38:24 +00002433 // Consider qualified interfaces and interfaces the same.
2434 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
2435 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002436
Chris Lattnerb5709e22008-04-07 05:43:21 +00002437 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002438 if (LHSClass != RHSClass) {
Steve Naroff28ceff72008-12-10 22:14:21 +00002439 // ID is compatible with all qualified id types.
2440 if (LHS->isObjCQualifiedIdType()) {
2441 if (const PointerType *PT = RHS->getAsPointerType()) {
2442 QualType pType = PT->getPointeeType();
2443 if (isObjCIdType(pType))
2444 return LHS;
2445 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2446 // Unfortunately, this API is part of Sema (which we don't have access
2447 // to. Need to refactor. The following check is insufficient, since we
2448 // need to make sure the class implements the protocol.
2449 if (pType->isObjCInterfaceType())
2450 return LHS;
2451 }
2452 }
2453 if (RHS->isObjCQualifiedIdType()) {
2454 if (const PointerType *PT = LHS->getAsPointerType()) {
2455 QualType pType = PT->getPointeeType();
2456 if (isObjCIdType(pType))
2457 return RHS;
2458 // FIXME: need to use ObjCQualifiedIdTypesAreCompatible(LHS, RHS, true).
2459 // Unfortunately, this API is part of Sema (which we don't have access
2460 // to. Need to refactor. The following check is insufficient, since we
2461 // need to make sure the class implements the protocol.
2462 if (pType->isObjCInterfaceType())
2463 return RHS;
2464 }
2465 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002466 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
2467 // a signed integer type, or an unsigned integer type.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002468 if (const EnumType* ETy = LHS->getAsEnumType()) {
2469 if (ETy->getDecl()->getIntegerType() == RHSCan.getUnqualifiedType())
2470 return RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002471 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002472 if (const EnumType* ETy = RHS->getAsEnumType()) {
2473 if (ETy->getDecl()->getIntegerType() == LHSCan.getUnqualifiedType())
2474 return LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00002475 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002476
Eli Friedman0d9549b2008-08-22 00:56:42 +00002477 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002478 }
Eli Friedman0d9549b2008-08-22 00:56:42 +00002479
Steve Naroffc88babe2008-01-09 22:43:08 +00002480 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00002481 switch (LHSClass) {
Chris Lattnerc38d4522008-01-14 05:45:46 +00002482 case Type::Pointer:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002483 {
2484 // Merge two pointer types, while trying to preserve typedef info
2485 QualType LHSPointee = LHS->getAsPointerType()->getPointeeType();
2486 QualType RHSPointee = RHS->getAsPointerType()->getPointeeType();
2487 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2488 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002489 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2490 return LHS;
2491 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2492 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002493 return getPointerType(ResultType);
2494 }
Steve Naroff09e1b9e2008-12-10 17:49:55 +00002495 case Type::BlockPointer:
2496 {
2497 // Merge two block pointer types, while trying to preserve typedef info
2498 QualType LHSPointee = LHS->getAsBlockPointerType()->getPointeeType();
2499 QualType RHSPointee = RHS->getAsBlockPointerType()->getPointeeType();
2500 QualType ResultType = mergeTypes(LHSPointee, RHSPointee);
2501 if (ResultType.isNull()) return QualType();
2502 if (getCanonicalType(LHSPointee) == getCanonicalType(ResultType))
2503 return LHS;
2504 if (getCanonicalType(RHSPointee) == getCanonicalType(ResultType))
2505 return RHS;
2506 return getBlockPointerType(ResultType);
2507 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002508 case Type::ConstantArray:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002509 {
2510 const ConstantArrayType* LCAT = getAsConstantArrayType(LHS);
2511 const ConstantArrayType* RCAT = getAsConstantArrayType(RHS);
2512 if (LCAT && RCAT && RCAT->getSize() != LCAT->getSize())
2513 return QualType();
2514
2515 QualType LHSElem = getAsArrayType(LHS)->getElementType();
2516 QualType RHSElem = getAsArrayType(RHS)->getElementType();
2517 QualType ResultType = mergeTypes(LHSElem, RHSElem);
2518 if (ResultType.isNull()) return QualType();
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002519 if (LCAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2520 return LHS;
2521 if (RCAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2522 return RHS;
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002523 if (LCAT) return getConstantArrayType(ResultType, LCAT->getSize(),
2524 ArrayType::ArraySizeModifier(), 0);
2525 if (RCAT) return getConstantArrayType(ResultType, RCAT->getSize(),
2526 ArrayType::ArraySizeModifier(), 0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002527 const VariableArrayType* LVAT = getAsVariableArrayType(LHS);
2528 const VariableArrayType* RVAT = getAsVariableArrayType(RHS);
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002529 if (LVAT && getCanonicalType(LHSElem) == getCanonicalType(ResultType))
2530 return LHS;
2531 if (RVAT && getCanonicalType(RHSElem) == getCanonicalType(ResultType))
2532 return RHS;
Eli Friedman0d9549b2008-08-22 00:56:42 +00002533 if (LVAT) {
2534 // FIXME: This isn't correct! But tricky to implement because
2535 // the array's size has to be the size of LHS, but the type
2536 // has to be different.
2537 return LHS;
2538 }
2539 if (RVAT) {
2540 // FIXME: This isn't correct! But tricky to implement because
2541 // the array's size has to be the size of RHS, but the type
2542 // has to be different.
2543 return RHS;
2544 }
Eli Friedmanc91a3f32008-08-22 01:48:21 +00002545 if (getCanonicalType(LHSElem) == getCanonicalType(ResultType)) return LHS;
2546 if (getCanonicalType(RHSElem) == getCanonicalType(ResultType)) return RHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002547 return getIncompleteArrayType(ResultType, ArrayType::ArraySizeModifier(),0);
Eli Friedman0d9549b2008-08-22 00:56:42 +00002548 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00002549 case Type::FunctionNoProto:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002550 return mergeFunctionTypes(LHS, RHS);
2551 case Type::Tagged:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002552 // FIXME: Why are these compatible?
2553 if (isObjCIdType(LHS) && isObjCClassType(RHS)) return LHS;
2554 if (isObjCClassType(LHS) && isObjCIdType(RHS)) return LHS;
2555 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002556 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002557 // Only exactly equal builtin types are compatible, which is tested above.
Eli Friedman0d9549b2008-08-22 00:56:42 +00002558 return QualType();
Chris Lattnerd1240fa2008-04-07 05:55:38 +00002559 case Type::Vector:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002560 if (areCompatVectorTypes(LHS->getAsVectorType(), RHS->getAsVectorType()))
2561 return LHS;
Chris Lattner2fda0ed2008-10-05 17:34:18 +00002562 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002563 case Type::ObjCInterface:
Eli Friedman0d9549b2008-08-22 00:56:42 +00002564 // Distinct ObjC interfaces are not compatible; see canAssignObjCInterfaces
2565 // for checking assignment/comparison safety
2566 return QualType();
Steve Naroff28ceff72008-12-10 22:14:21 +00002567 case Type::ObjCQualifiedId:
2568 // Distinct qualified id's are not compatible.
2569 return QualType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00002570 default:
2571 assert(0 && "unexpected type");
Eli Friedman0d9549b2008-08-22 00:56:42 +00002572 return QualType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00002573 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00002574}
Ted Kremenek738e6c02007-10-31 17:10:13 +00002575
Chris Lattner1d78a862008-04-07 07:01:58 +00002576//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00002577// Integer Predicates
2578//===----------------------------------------------------------------------===//
Chris Lattner74f67012009-01-16 07:15:35 +00002579
Eli Friedman0832dbc2008-06-28 06:23:08 +00002580unsigned ASTContext::getIntWidth(QualType T) {
2581 if (T == BoolTy)
2582 return 1;
2583 // At the moment, only bool has padding bits
2584 return (unsigned)getTypeSize(T);
2585}
2586
2587QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
2588 assert(T->isSignedIntegerType() && "Unexpected type");
2589 if (const EnumType* ETy = T->getAsEnumType())
2590 T = ETy->getDecl()->getIntegerType();
2591 const BuiltinType* BTy = T->getAsBuiltinType();
2592 assert (BTy && "Unexpected signed integer type");
2593 switch (BTy->getKind()) {
2594 case BuiltinType::Char_S:
2595 case BuiltinType::SChar:
2596 return UnsignedCharTy;
2597 case BuiltinType::Short:
2598 return UnsignedShortTy;
2599 case BuiltinType::Int:
2600 return UnsignedIntTy;
2601 case BuiltinType::Long:
2602 return UnsignedLongTy;
2603 case BuiltinType::LongLong:
2604 return UnsignedLongLongTy;
2605 default:
2606 assert(0 && "Unexpected signed integer type");
2607 return QualType();
2608 }
2609}
2610
2611
2612//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00002613// Serialization Support
2614//===----------------------------------------------------------------------===//
2615
Ted Kremenek738e6c02007-10-31 17:10:13 +00002616/// Emit - Serialize an ASTContext object to Bitcode.
2617void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00002618 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00002619 S.EmitRef(SourceMgr);
2620 S.EmitRef(Target);
2621 S.EmitRef(Idents);
2622 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002623
Ted Kremenek68228a92007-10-31 22:44:07 +00002624 // Emit the size of the type vector so that we can reserve that size
2625 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002626 S.EmitInt(Types.size());
2627
Ted Kremenek034a78c2007-11-13 22:02:55 +00002628 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
2629 I!=E;++I)
2630 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00002631
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002632 S.EmitOwnedPtr(TUDecl);
2633
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002634 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00002635}
2636
Ted Kremenekacba3612007-11-13 00:25:37 +00002637ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00002638
2639 // Read the language options.
2640 LangOptions LOpts;
2641 LOpts.Read(D);
2642
Ted Kremenek68228a92007-10-31 22:44:07 +00002643 SourceManager &SM = D.ReadRef<SourceManager>();
2644 TargetInfo &t = D.ReadRef<TargetInfo>();
2645 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
2646 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00002647
Ted Kremenek68228a92007-10-31 22:44:07 +00002648 unsigned size_reserve = D.ReadInt();
2649
Douglas Gregor24afd4a2008-11-17 14:58:09 +00002650 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels,
2651 size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00002652
Ted Kremenek034a78c2007-11-13 22:02:55 +00002653 for (unsigned i = 0; i < size_reserve; ++i)
2654 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00002655
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00002656 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
2657
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00002658 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00002659
2660 return A;
2661}