blob: a20236f9eb5d430e6b88221bbd1c43983cb31f34 [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"
15#include "clang/AST/Decl.h"
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +000016#include "clang/AST/DeclCXX.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000018#include "clang/Basic/TargetInfo.h"
19#include "llvm/ADT/SmallVector.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"
Anders Carlsson36f07d82007-10-29 05:01:08 +000023
Chris Lattner4b009652007-07-25 00:24:17 +000024using namespace clang;
25
26enum FloatingRank {
27 FloatRank, DoubleRank, LongDoubleRank
28};
29
30ASTContext::~ASTContext() {
31 // Deallocate all the types.
32 while (!Types.empty()) {
Ted Kremenekdb4d5972008-05-21 16:38:54 +000033 Types.back()->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000034 Types.pop_back();
35 }
Eli Friedman65489b72008-05-27 03:08:09 +000036
37 TUDecl->Destroy(*this);
Chris Lattner4b009652007-07-25 00:24:17 +000038}
39
40void ASTContext::PrintStats() const {
41 fprintf(stderr, "*** AST Context Stats:\n");
42 fprintf(stderr, " %d types total.\n", (int)Types.size());
43 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
44 unsigned NumVector = 0, NumComplex = 0;
45 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
46
47 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000048 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
49 unsigned NumObjCQualifiedIds = 0;
Steve Naroffe0430632008-05-21 15:59:22 +000050 unsigned NumTypeOfTypes = 0, NumTypeOfExprs = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000051
52 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
53 Type *T = Types[i];
54 if (isa<BuiltinType>(T))
55 ++NumBuiltin;
56 else if (isa<PointerType>(T))
57 ++NumPointer;
58 else if (isa<ReferenceType>(T))
59 ++NumReference;
60 else if (isa<ComplexType>(T))
61 ++NumComplex;
62 else if (isa<ArrayType>(T))
63 ++NumArray;
64 else if (isa<VectorType>(T))
65 ++NumVector;
66 else if (isa<FunctionTypeNoProto>(T))
67 ++NumFunctionNP;
68 else if (isa<FunctionTypeProto>(T))
69 ++NumFunctionP;
70 else if (isa<TypedefType>(T))
71 ++NumTypeName;
72 else if (TagType *TT = dyn_cast<TagType>(T)) {
73 ++NumTagged;
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +000074 switch (TT->getDecl()->getTagKind()) {
Chris Lattner4b009652007-07-25 00:24:17 +000075 default: assert(0 && "Unknown tagged type!");
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +000076 case TagDecl::TK_struct: ++NumTagStruct; break;
77 case TagDecl::TK_union: ++NumTagUnion; break;
78 case TagDecl::TK_class: ++NumTagClass; break;
79 case TagDecl::TK_enum: ++NumTagEnum; break;
Chris Lattner4b009652007-07-25 00:24:17 +000080 }
Ted Kremenek42730c52008-01-07 19:49:32 +000081 } else if (isa<ObjCInterfaceType>(T))
82 ++NumObjCInterfaces;
83 else if (isa<ObjCQualifiedInterfaceType>(T))
84 ++NumObjCQualifiedInterfaces;
85 else if (isa<ObjCQualifiedIdType>(T))
86 ++NumObjCQualifiedIds;
Steve Naroffe0430632008-05-21 15:59:22 +000087 else if (isa<TypeOfType>(T))
88 ++NumTypeOfTypes;
89 else if (isa<TypeOfExpr>(T))
90 ++NumTypeOfExprs;
Steve Naroff948fd372007-09-17 14:16:13 +000091 else {
Chris Lattner8a35b462007-12-12 06:43:05 +000092 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +000093 assert(0 && "Unknown type!");
94 }
95 }
96
97 fprintf(stderr, " %d builtin types\n", NumBuiltin);
98 fprintf(stderr, " %d pointer types\n", NumPointer);
99 fprintf(stderr, " %d reference types\n", NumReference);
100 fprintf(stderr, " %d complex types\n", NumComplex);
101 fprintf(stderr, " %d array types\n", NumArray);
102 fprintf(stderr, " %d vector types\n", NumVector);
103 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
104 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
105 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
106 fprintf(stderr, " %d tagged types\n", NumTagged);
107 fprintf(stderr, " %d struct types\n", NumTagStruct);
108 fprintf(stderr, " %d union types\n", NumTagUnion);
109 fprintf(stderr, " %d class types\n", NumTagClass);
110 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000111 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000112 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000113 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000114 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000115 NumObjCQualifiedIds);
Steve Naroffe0430632008-05-21 15:59:22 +0000116 fprintf(stderr, " %d typeof types\n", NumTypeOfTypes);
117 fprintf(stderr, " %d typeof exprs\n", NumTypeOfExprs);
118
Chris Lattner4b009652007-07-25 00:24:17 +0000119 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
120 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
121 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
122 NumFunctionP*sizeof(FunctionTypeProto)+
123 NumFunctionNP*sizeof(FunctionTypeNoProto)+
Steve Naroffe0430632008-05-21 15:59:22 +0000124 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)+
125 NumTypeOfTypes*sizeof(TypeOfType)+NumTypeOfExprs*sizeof(TypeOfExpr)));
Chris Lattner4b009652007-07-25 00:24:17 +0000126}
127
128
129void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
130 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
131}
132
Chris Lattner4b009652007-07-25 00:24:17 +0000133void ASTContext::InitBuiltinTypes() {
134 assert(VoidTy.isNull() && "Context reinitialized?");
135
136 // C99 6.2.5p19.
137 InitBuiltinType(VoidTy, BuiltinType::Void);
138
139 // C99 6.2.5p2.
140 InitBuiltinType(BoolTy, BuiltinType::Bool);
141 // C99 6.2.5p3.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000142 if (Target.isCharSigned())
Chris Lattner4b009652007-07-25 00:24:17 +0000143 InitBuiltinType(CharTy, BuiltinType::Char_S);
144 else
145 InitBuiltinType(CharTy, BuiltinType::Char_U);
146 // C99 6.2.5p4.
147 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
148 InitBuiltinType(ShortTy, BuiltinType::Short);
149 InitBuiltinType(IntTy, BuiltinType::Int);
150 InitBuiltinType(LongTy, BuiltinType::Long);
151 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
152
153 // C99 6.2.5p6.
154 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
155 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
156 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
157 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
158 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
159
160 // C99 6.2.5p10.
161 InitBuiltinType(FloatTy, BuiltinType::Float);
162 InitBuiltinType(DoubleTy, BuiltinType::Double);
163 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000164
165 // C++ 3.9.1p5
166 InitBuiltinType(WCharTy, BuiltinType::WChar);
167
Chris Lattner4b009652007-07-25 00:24:17 +0000168 // C99 6.2.5p11.
169 FloatComplexTy = getComplexType(FloatTy);
170 DoubleComplexTy = getComplexType(DoubleTy);
171 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000172
173 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000174 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000175 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000176 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000177 ClassStructType = 0;
178
Ted Kremenek42730c52008-01-07 19:49:32 +0000179 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000180
181 // void * type
182 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000183}
184
185//===----------------------------------------------------------------------===//
186// Type Sizing and Analysis
187//===----------------------------------------------------------------------===//
188
Chris Lattner2a674dc2008-06-30 18:32:54 +0000189/// getFloatTypeSemantics - Return the APFloat 'semantics' for the specified
190/// scalar floating point type.
191const llvm::fltSemantics &ASTContext::getFloatTypeSemantics(QualType T) const {
192 const BuiltinType *BT = T->getAsBuiltinType();
193 assert(BT && "Not a floating point type!");
194 switch (BT->getKind()) {
195 default: assert(0 && "Not a floating point type!");
196 case BuiltinType::Float: return Target.getFloatFormat();
197 case BuiltinType::Double: return Target.getDoubleFormat();
198 case BuiltinType::LongDouble: return Target.getLongDoubleFormat();
199 }
200}
201
202
Chris Lattner4b009652007-07-25 00:24:17 +0000203/// getTypeSize - Return the size of the specified type, in bits. This method
204/// does not work on incomplete types.
205std::pair<uint64_t, unsigned>
Chris Lattner8cd0e932008-03-05 18:54:05 +0000206ASTContext::getTypeInfo(QualType T) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000207 T = getCanonicalType(T);
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000208 uint64_t Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000209 unsigned Align;
210 switch (T->getTypeClass()) {
211 case Type::TypeName: assert(0 && "Not a canonical type!");
212 case Type::FunctionNoProto:
213 case Type::FunctionProto:
214 default:
215 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000216 case Type::VariableArray:
217 assert(0 && "VLAs not implemented yet!");
218 case Type::ConstantArray: {
219 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
220
Chris Lattner8cd0e932008-03-05 18:54:05 +0000221 std::pair<uint64_t, unsigned> EltInfo = getTypeInfo(CAT->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000222 Width = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000223 Align = EltInfo.second;
224 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000225 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000226 case Type::ExtVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000227 case Type::Vector: {
228 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000229 getTypeInfo(cast<VectorType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000230 Width = EltInfo.first*cast<VectorType>(T)->getNumElements();
Eli Friedman5949a022008-05-30 09:31:38 +0000231 // FIXME: This isn't right for unusual vectors
232 Align = Width;
Chris Lattner4b009652007-07-25 00:24:17 +0000233 break;
234 }
235
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000236 case Type::Builtin:
Chris Lattner4b009652007-07-25 00:24:17 +0000237 switch (cast<BuiltinType>(T)->getKind()) {
238 default: assert(0 && "Unknown builtin type!");
239 case BuiltinType::Void:
240 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000241 case BuiltinType::Bool:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000242 Width = Target.getBoolWidth();
243 Align = Target.getBoolAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000244 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000245 case BuiltinType::Char_S:
246 case BuiltinType::Char_U:
247 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000248 case BuiltinType::SChar:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000249 Width = Target.getCharWidth();
250 Align = Target.getCharAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000251 break;
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +0000252 case BuiltinType::WChar:
253 Width = Target.getWCharWidth();
254 Align = Target.getWCharAlign();
255 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000256 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000257 case BuiltinType::Short:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000258 Width = Target.getShortWidth();
259 Align = Target.getShortAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000260 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000261 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000262 case BuiltinType::Int:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000263 Width = Target.getIntWidth();
264 Align = Target.getIntAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000265 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000266 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000267 case BuiltinType::Long:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000268 Width = Target.getLongWidth();
269 Align = Target.getLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000270 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000271 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000272 case BuiltinType::LongLong:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000273 Width = Target.getLongLongWidth();
274 Align = Target.getLongLongAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000275 break;
276 case BuiltinType::Float:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000277 Width = Target.getFloatWidth();
278 Align = Target.getFloatAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000279 break;
280 case BuiltinType::Double:
Chris Lattner1d78a862008-04-07 07:01:58 +0000281 Width = Target.getDoubleWidth();
282 Align = Target.getDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000283 break;
284 case BuiltinType::LongDouble:
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000285 Width = Target.getLongDoubleWidth();
286 Align = Target.getLongDoubleAlign();
Chris Lattnerb66237b2007-12-19 19:23:28 +0000287 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000288 }
289 break;
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000290 case Type::ASQual:
Chris Lattner8cd0e932008-03-05 18:54:05 +0000291 // FIXME: Pointers into different addr spaces could have different sizes and
292 // alignment requirements: getPointerInfo should take an AddrSpace.
293 return getTypeInfo(QualType(cast<ASQualType>(T)->getBaseType(), 0));
Ted Kremenek42730c52008-01-07 19:49:32 +0000294 case Type::ObjCQualifiedId:
Chris Lattner1d78a862008-04-07 07:01:58 +0000295 Width = Target.getPointerWidth(0);
Chris Lattner461a6c52008-03-08 08:34:58 +0000296 Align = Target.getPointerAlign(0);
Chris Lattnerb66237b2007-12-19 19:23:28 +0000297 break;
Chris Lattner461a6c52008-03-08 08:34:58 +0000298 case Type::Pointer: {
299 unsigned AS = cast<PointerType>(T)->getPointeeType().getAddressSpace();
Chris Lattner1d78a862008-04-07 07:01:58 +0000300 Width = Target.getPointerWidth(AS);
Chris Lattner461a6c52008-03-08 08:34:58 +0000301 Align = Target.getPointerAlign(AS);
302 break;
303 }
Chris Lattner4b009652007-07-25 00:24:17 +0000304 case Type::Reference:
305 // "When applied to a reference or a reference type, the result is the size
306 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000307 // FIXME: This is wrong for struct layout: a reference in a struct has
308 // pointer size.
Chris Lattnercfac88d2008-04-02 17:35:06 +0000309 return getTypeInfo(cast<ReferenceType>(T)->getPointeeType());
Chris Lattner4b009652007-07-25 00:24:17 +0000310
311 case Type::Complex: {
312 // Complex types have the same alignment as their elements, but twice the
313 // size.
314 std::pair<uint64_t, unsigned> EltInfo =
Chris Lattner8cd0e932008-03-05 18:54:05 +0000315 getTypeInfo(cast<ComplexType>(T)->getElementType());
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000316 Width = EltInfo.first*2;
Chris Lattner4b009652007-07-25 00:24:17 +0000317 Align = EltInfo.second;
318 break;
319 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000320 case Type::ObjCInterface: {
321 ObjCInterfaceType *ObjCI = cast<ObjCInterfaceType>(T);
322 const ASTRecordLayout &Layout = getASTObjCInterfaceLayout(ObjCI->getDecl());
323 Width = Layout.getSize();
324 Align = Layout.getAlignment();
325 break;
326 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000327 case Type::Tagged: {
328 if (EnumType *ET = dyn_cast<EnumType>(cast<TagType>(T)))
329 return getTypeInfo(ET->getDecl()->getIntegerType());
330
331 RecordType *RT = cast<RecordType>(T);
332 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl());
333 Width = Layout.getSize();
334 Align = Layout.getAlignment();
Chris Lattner4b009652007-07-25 00:24:17 +0000335 break;
336 }
Chris Lattner2bf1d6c2008-04-06 22:05:18 +0000337 }
Chris Lattner4b009652007-07-25 00:24:17 +0000338
339 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerfc18dcc2008-03-08 08:52:55 +0000340 return std::make_pair(Width, Align);
Chris Lattner4b009652007-07-25 00:24:17 +0000341}
342
Devang Patelbfe323c2008-06-04 21:22:16 +0000343/// LayoutField - Field layout.
344void ASTRecordLayout::LayoutField(const FieldDecl *FD, unsigned FieldNo,
345 bool IsUnion, bool StructIsPacked,
346 ASTContext &Context) {
347 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
348 uint64_t FieldOffset = IsUnion ? 0 : Size;
349 uint64_t FieldSize;
350 unsigned FieldAlign;
351
352 if (const Expr *BitWidthExpr = FD->getBitWidth()) {
353 // TODO: Need to check this algorithm on other targets!
354 // (tested on Linux-X86)
355 llvm::APSInt I(32);
356 bool BitWidthIsICE =
357 BitWidthExpr->isIntegerConstantExpr(I, Context);
358 assert (BitWidthIsICE && "Invalid BitField size expression");
359 FieldSize = I.getZExtValue();
360
361 std::pair<uint64_t, unsigned> FieldInfo =
362 Context.getTypeInfo(FD->getType());
363 uint64_t TypeSize = FieldInfo.first;
364
365 FieldAlign = FieldInfo.second;
366 if (FieldIsPacked)
367 FieldAlign = 1;
368 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
369 FieldAlign = std::max(FieldAlign, AA->getAlignment());
370
371 // Check if we need to add padding to give the field the correct
372 // alignment.
373 if (FieldSize == 0 || (FieldOffset & (FieldAlign-1)) + FieldSize > TypeSize)
374 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
375
376 // Padding members don't affect overall alignment
377 if (!FD->getIdentifier())
378 FieldAlign = 1;
379 } else {
380 if (FD->getType()->isIncompleteType()) {
381 // This must be a flexible array member; we can't directly
382 // query getTypeInfo about these, so we figure it out here.
383 // Flexible array members don't have any size, but they
384 // have to be aligned appropriately for their element type.
385 FieldSize = 0;
Chris Lattnera1923f62008-08-04 07:31:14 +0000386 const ArrayType* ATy = Context.getAsArrayType(FD->getType());
Devang Patelbfe323c2008-06-04 21:22:16 +0000387 FieldAlign = Context.getTypeAlign(ATy->getElementType());
388 } else {
389 std::pair<uint64_t, unsigned> FieldInfo =
390 Context.getTypeInfo(FD->getType());
391 FieldSize = FieldInfo.first;
392 FieldAlign = FieldInfo.second;
393 }
394
395 if (FieldIsPacked)
396 FieldAlign = 8;
397 if (const AlignedAttr *AA = FD->getAttr<AlignedAttr>())
398 FieldAlign = std::max(FieldAlign, AA->getAlignment());
399
400 // Round up the current record size to the field's alignment boundary.
401 FieldOffset = (FieldOffset + (FieldAlign-1)) & ~(FieldAlign-1);
402 }
403
404 // Place this field at the current location.
405 FieldOffsets[FieldNo] = FieldOffset;
406
407 // Reserve space for this field.
408 if (IsUnion) {
409 Size = std::max(Size, FieldSize);
410 } else {
411 Size = FieldOffset + FieldSize;
412 }
413
414 // Remember max struct/class alignment.
415 Alignment = std::max(Alignment, FieldAlign);
416}
417
Devang Patel4b6bf702008-06-04 21:54:36 +0000418
419/// getASTObjcInterfaceLayout - Get or compute information about the layout of the
420/// specified Objective C, which indicates its size and ivar
421/// position information.
422const ASTRecordLayout &
423ASTContext::getASTObjCInterfaceLayout(const ObjCInterfaceDecl *D) {
424 // Look up this layout, if already laid out, return what we have.
425 const ASTRecordLayout *&Entry = ASTObjCInterfaces[D];
426 if (Entry) return *Entry;
427
428 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
429 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
Devang Patel8682d882008-06-06 02:14:01 +0000430 ASTRecordLayout *NewEntry = NULL;
431 unsigned FieldCount = D->ivar_size();
432 if (ObjCInterfaceDecl *SD = D->getSuperClass()) {
433 FieldCount++;
434 const ASTRecordLayout &SL = getASTObjCInterfaceLayout(SD);
435 unsigned Alignment = SL.getAlignment();
436 uint64_t Size = SL.getSize();
437 NewEntry = new ASTRecordLayout(Size, Alignment);
438 NewEntry->InitializeLayout(FieldCount);
439 NewEntry->SetFieldOffset(0, 0); // Super class is at the beginning of the layout.
440 } else {
441 NewEntry = new ASTRecordLayout();
442 NewEntry->InitializeLayout(FieldCount);
443 }
Devang Patel4b6bf702008-06-04 21:54:36 +0000444 Entry = NewEntry;
445
Devang Patel4b6bf702008-06-04 21:54:36 +0000446 bool IsPacked = D->getAttr<PackedAttr>();
447
448 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
449 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
450 AA->getAlignment()));
451
452 // Layout each ivar sequentially.
453 unsigned i = 0;
454 for (ObjCInterfaceDecl::ivar_iterator IVI = D->ivar_begin(),
455 IVE = D->ivar_end(); IVI != IVE; ++IVI) {
456 const ObjCIvarDecl* Ivar = (*IVI);
457 NewEntry->LayoutField(Ivar, i++, false, IsPacked, *this);
458 }
459
460 // Finally, round the size of the total struct up to the alignment of the
461 // struct itself.
462 NewEntry->FinalizeLayout();
463 return *NewEntry;
464}
465
Devang Patel7a78e432007-11-01 19:11:01 +0000466/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000467/// specified record (struct/union/class), which indicates its size and field
468/// position information.
Chris Lattner8cd0e932008-03-05 18:54:05 +0000469const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D) {
Chris Lattner4b009652007-07-25 00:24:17 +0000470 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
Eli Friedman5949a022008-05-30 09:31:38 +0000471
Chris Lattner4b009652007-07-25 00:24:17 +0000472 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000473 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000474 if (Entry) return *Entry;
Eli Friedman5949a022008-05-30 09:31:38 +0000475
Devang Patel7a78e432007-11-01 19:11:01 +0000476 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
477 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
478 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000479 Entry = NewEntry;
Eli Friedman5949a022008-05-30 09:31:38 +0000480
Devang Patelbfe323c2008-06-04 21:22:16 +0000481 NewEntry->InitializeLayout(D->getNumMembers());
Eli Friedman5949a022008-05-30 09:31:38 +0000482 bool StructIsPacked = D->getAttr<PackedAttr>();
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +0000483 bool IsUnion = D->isUnion();
Chris Lattner4b009652007-07-25 00:24:17 +0000484
Eli Friedman5949a022008-05-30 09:31:38 +0000485 if (const AlignedAttr *AA = D->getAttr<AlignedAttr>())
Devang Patelbfe323c2008-06-04 21:22:16 +0000486 NewEntry->SetAlignment(std::max(NewEntry->getAlignment(),
487 AA->getAlignment()));
Anders Carlsson058237f2008-02-18 07:13:09 +0000488
Eli Friedman5949a022008-05-30 09:31:38 +0000489 // Layout each field, for now, just sequentially, respecting alignment. In
490 // the future, this will need to be tweakable by targets.
491 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
492 const FieldDecl *FD = D->getMember(i);
Devang Patelbfe323c2008-06-04 21:22:16 +0000493 NewEntry->LayoutField(FD, i, IsUnion, StructIsPacked, *this);
Chris Lattner4b009652007-07-25 00:24:17 +0000494 }
Eli Friedman5949a022008-05-30 09:31:38 +0000495
496 // Finally, round the size of the total struct up to the alignment of the
497 // struct itself.
Devang Patelbfe323c2008-06-04 21:22:16 +0000498 NewEntry->FinalizeLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000499 return *NewEntry;
500}
501
Chris Lattner4b009652007-07-25 00:24:17 +0000502//===----------------------------------------------------------------------===//
503// Type creation/memoization methods
504//===----------------------------------------------------------------------===//
505
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000506QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000507 QualType CanT = getCanonicalType(T);
508 if (CanT.getAddressSpace() == AddressSpace)
Chris Lattner35fef522008-02-20 20:55:12 +0000509 return T;
510
511 // Type's cannot have multiple ASQuals, therefore we know we only have to deal
512 // with CVR qualifiers from here on out.
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000513 assert(CanT.getAddressSpace() == 0 &&
Chris Lattner35fef522008-02-20 20:55:12 +0000514 "Type is already address space qualified");
515
516 // Check if we've already instantiated an address space qual'd type of this
517 // type.
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000518 llvm::FoldingSetNodeID ID;
Chris Lattner35fef522008-02-20 20:55:12 +0000519 ASQualType::Profile(ID, T.getTypePtr(), AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000520 void *InsertPos = 0;
521 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
522 return QualType(ASQy, 0);
523
524 // If the base type isn't canonical, this won't be a canonical type either,
525 // so fill in the canonical type field.
526 QualType Canonical;
527 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000528 Canonical = getASQualType(CanT, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000529
530 // Get the new insert position for the node we care about.
531 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
532 assert(NewIP == 0 && "Shouldn't be in the map!");
533 }
Chris Lattner35fef522008-02-20 20:55:12 +0000534 ASQualType *New = new ASQualType(T.getTypePtr(), Canonical, AddressSpace);
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000535 ASQualTypes.InsertNode(New, InsertPos);
536 Types.push_back(New);
Chris Lattner35fef522008-02-20 20:55:12 +0000537 return QualType(New, T.getCVRQualifiers());
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000538}
539
Chris Lattner4b009652007-07-25 00:24:17 +0000540
541/// getComplexType - Return the uniqued reference to the type for a complex
542/// number with the specified element type.
543QualType ASTContext::getComplexType(QualType T) {
544 // Unique pointers, to guarantee there is only one pointer of a particular
545 // structure.
546 llvm::FoldingSetNodeID ID;
547 ComplexType::Profile(ID, T);
548
549 void *InsertPos = 0;
550 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
551 return QualType(CT, 0);
552
553 // If the pointee type isn't canonical, this won't be a canonical type either,
554 // so fill in the canonical type field.
555 QualType Canonical;
556 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000557 Canonical = getComplexType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000558
559 // Get the new insert position for the node we care about.
560 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
561 assert(NewIP == 0 && "Shouldn't be in the map!");
562 }
563 ComplexType *New = new ComplexType(T, Canonical);
564 Types.push_back(New);
565 ComplexTypes.InsertNode(New, InsertPos);
566 return QualType(New, 0);
567}
568
569
570/// getPointerType - Return the uniqued reference to the type for a pointer to
571/// the specified type.
572QualType ASTContext::getPointerType(QualType T) {
573 // Unique pointers, to guarantee there is only one pointer of a particular
574 // structure.
575 llvm::FoldingSetNodeID ID;
576 PointerType::Profile(ID, T);
577
578 void *InsertPos = 0;
579 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
580 return QualType(PT, 0);
581
582 // If the pointee type isn't canonical, this won't be a canonical type either,
583 // so fill in the canonical type field.
584 QualType Canonical;
585 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000586 Canonical = getPointerType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000587
588 // Get the new insert position for the node we care about.
589 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
590 assert(NewIP == 0 && "Shouldn't be in the map!");
591 }
592 PointerType *New = new PointerType(T, Canonical);
593 Types.push_back(New);
594 PointerTypes.InsertNode(New, InsertPos);
595 return QualType(New, 0);
596}
597
598/// getReferenceType - Return the uniqued reference to the type for a reference
599/// to the specified type.
600QualType ASTContext::getReferenceType(QualType T) {
601 // Unique pointers, to guarantee there is only one pointer of a particular
602 // structure.
603 llvm::FoldingSetNodeID ID;
604 ReferenceType::Profile(ID, T);
605
606 void *InsertPos = 0;
607 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
608 return QualType(RT, 0);
609
610 // If the referencee type isn't canonical, this won't be a canonical type
611 // either, so fill in the canonical type field.
612 QualType Canonical;
613 if (!T->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000614 Canonical = getReferenceType(getCanonicalType(T));
Chris Lattner4b009652007-07-25 00:24:17 +0000615
616 // Get the new insert position for the node we care about.
617 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
618 assert(NewIP == 0 && "Shouldn't be in the map!");
619 }
620
621 ReferenceType *New = new ReferenceType(T, Canonical);
622 Types.push_back(New);
623 ReferenceTypes.InsertNode(New, InsertPos);
624 return QualType(New, 0);
625}
626
Steve Naroff83c13012007-08-30 01:06:46 +0000627/// getConstantArrayType - Return the unique reference to the type for an
628/// array of the specified element type.
629QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000630 const llvm::APInt &ArySize,
631 ArrayType::ArraySizeModifier ASM,
632 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000633 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000634 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000635
636 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000637 if (ConstantArrayType *ATP =
638 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000639 return QualType(ATP, 0);
640
641 // If the element type isn't canonical, this won't be a canonical type either,
642 // so fill in the canonical type field.
643 QualType Canonical;
644 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000645 Canonical = getConstantArrayType(getCanonicalType(EltTy), ArySize,
Steve Naroff24c9b982007-08-30 18:10:14 +0000646 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000647 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000648 ConstantArrayType *NewIP =
649 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
650
Chris Lattner4b009652007-07-25 00:24:17 +0000651 assert(NewIP == 0 && "Shouldn't be in the map!");
652 }
653
Steve Naroff24c9b982007-08-30 18:10:14 +0000654 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
655 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000656 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000657 Types.push_back(New);
658 return QualType(New, 0);
659}
660
Steve Naroffe2579e32007-08-30 18:14:25 +0000661/// getVariableArrayType - Returns a non-unique reference to the type for a
662/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000663QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
664 ArrayType::ArraySizeModifier ASM,
665 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000666 // Since we don't unique expressions, it isn't possible to unique VLA's
667 // that have an expression provided for their size.
668
669 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
670 ASM, EltTypeQuals);
671
672 VariableArrayTypes.push_back(New);
673 Types.push_back(New);
674 return QualType(New, 0);
675}
676
677QualType ASTContext::getIncompleteArrayType(QualType EltTy,
678 ArrayType::ArraySizeModifier ASM,
679 unsigned EltTypeQuals) {
680 llvm::FoldingSetNodeID ID;
681 IncompleteArrayType::Profile(ID, EltTy);
682
683 void *InsertPos = 0;
684 if (IncompleteArrayType *ATP =
685 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
686 return QualType(ATP, 0);
687
688 // If the element type isn't canonical, this won't be a canonical type
689 // either, so fill in the canonical type field.
690 QualType Canonical;
691
692 if (!EltTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000693 Canonical = getIncompleteArrayType(getCanonicalType(EltTy),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000694 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000695
696 // Get the new insert position for the node we care about.
697 IncompleteArrayType *NewIP =
698 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
699
700 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000701 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000702
703 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
704 ASM, EltTypeQuals);
705
706 IncompleteArrayTypes.InsertNode(New, InsertPos);
707 Types.push_back(New);
708 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000709}
710
Chris Lattner4b009652007-07-25 00:24:17 +0000711/// getVectorType - Return the unique reference to a vector type of
712/// the specified element type and size. VectorType must be a built-in type.
713QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
714 BuiltinType *baseType;
715
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000716 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Chris Lattner4b009652007-07-25 00:24:17 +0000717 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
718
719 // Check if we've already instantiated a vector of this type.
720 llvm::FoldingSetNodeID ID;
721 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
722 void *InsertPos = 0;
723 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
724 return QualType(VTP, 0);
725
726 // If the element type isn't canonical, this won't be a canonical type either,
727 // so fill in the canonical type field.
728 QualType Canonical;
729 if (!vecType->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000730 Canonical = getVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000731
732 // Get the new insert position for the node we care about.
733 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
734 assert(NewIP == 0 && "Shouldn't be in the map!");
735 }
736 VectorType *New = new VectorType(vecType, NumElts, Canonical);
737 VectorTypes.InsertNode(New, InsertPos);
738 Types.push_back(New);
739 return QualType(New, 0);
740}
741
Nate Begemanaf6ed502008-04-18 23:10:10 +0000742/// getExtVectorType - Return the unique reference to an extended vector type of
Chris Lattner4b009652007-07-25 00:24:17 +0000743/// the specified element type and size. VectorType must be a built-in type.
Nate Begemanaf6ed502008-04-18 23:10:10 +0000744QualType ASTContext::getExtVectorType(QualType vecType, unsigned NumElts) {
Chris Lattner4b009652007-07-25 00:24:17 +0000745 BuiltinType *baseType;
746
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000747 baseType = dyn_cast<BuiltinType>(getCanonicalType(vecType).getTypePtr());
Nate Begemanaf6ed502008-04-18 23:10:10 +0000748 assert(baseType != 0 && "getExtVectorType(): Expecting a built-in type");
Chris Lattner4b009652007-07-25 00:24:17 +0000749
750 // Check if we've already instantiated a vector of this type.
751 llvm::FoldingSetNodeID ID;
Nate Begemanaf6ed502008-04-18 23:10:10 +0000752 VectorType::Profile(ID, vecType, NumElts, Type::ExtVector);
Chris Lattner4b009652007-07-25 00:24:17 +0000753 void *InsertPos = 0;
754 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
755 return QualType(VTP, 0);
756
757 // If the element type isn't canonical, this won't be a canonical type either,
758 // so fill in the canonical type field.
759 QualType Canonical;
760 if (!vecType->isCanonical()) {
Nate Begemanaf6ed502008-04-18 23:10:10 +0000761 Canonical = getExtVectorType(getCanonicalType(vecType), NumElts);
Chris Lattner4b009652007-07-25 00:24:17 +0000762
763 // Get the new insert position for the node we care about.
764 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
765 assert(NewIP == 0 && "Shouldn't be in the map!");
766 }
Nate Begemanaf6ed502008-04-18 23:10:10 +0000767 ExtVectorType *New = new ExtVectorType(vecType, NumElts, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000768 VectorTypes.InsertNode(New, InsertPos);
769 Types.push_back(New);
770 return QualType(New, 0);
771}
772
773/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
774///
775QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
776 // Unique functions, to guarantee there is only one function of a particular
777 // structure.
778 llvm::FoldingSetNodeID ID;
779 FunctionTypeNoProto::Profile(ID, ResultTy);
780
781 void *InsertPos = 0;
782 if (FunctionTypeNoProto *FT =
783 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
784 return QualType(FT, 0);
785
786 QualType Canonical;
787 if (!ResultTy->isCanonical()) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000788 Canonical = getFunctionTypeNoProto(getCanonicalType(ResultTy));
Chris Lattner4b009652007-07-25 00:24:17 +0000789
790 // Get the new insert position for the node we care about.
791 FunctionTypeNoProto *NewIP =
792 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
793 assert(NewIP == 0 && "Shouldn't be in the map!");
794 }
795
796 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
797 Types.push_back(New);
Eli Friedmanaa0fdfd2008-02-25 22:11:40 +0000798 FunctionTypeNoProtos.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000799 return QualType(New, 0);
800}
801
802/// getFunctionType - Return a normal function type with a typed argument
803/// list. isVariadic indicates whether the argument list includes '...'.
804QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
805 unsigned NumArgs, bool isVariadic) {
806 // Unique functions, to guarantee there is only one function of a particular
807 // structure.
808 llvm::FoldingSetNodeID ID;
809 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
810
811 void *InsertPos = 0;
812 if (FunctionTypeProto *FTP =
813 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
814 return QualType(FTP, 0);
815
816 // Determine whether the type being created is already canonical or not.
817 bool isCanonical = ResultTy->isCanonical();
818 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
819 if (!ArgArray[i]->isCanonical())
820 isCanonical = false;
821
822 // If this type isn't canonical, get the canonical version of it.
823 QualType Canonical;
824 if (!isCanonical) {
825 llvm::SmallVector<QualType, 16> CanonicalArgs;
826 CanonicalArgs.reserve(NumArgs);
827 for (unsigned i = 0; i != NumArgs; ++i)
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000828 CanonicalArgs.push_back(getCanonicalType(ArgArray[i]));
Chris Lattner4b009652007-07-25 00:24:17 +0000829
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000830 Canonical = getFunctionType(getCanonicalType(ResultTy),
Chris Lattner4b009652007-07-25 00:24:17 +0000831 &CanonicalArgs[0], NumArgs,
832 isVariadic);
833
834 // Get the new insert position for the node we care about.
835 FunctionTypeProto *NewIP =
836 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
837 assert(NewIP == 0 && "Shouldn't be in the map!");
838 }
839
840 // FunctionTypeProto objects are not allocated with new because they have a
841 // variable size array (for parameter types) at the end of them.
842 FunctionTypeProto *FTP =
843 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
844 NumArgs*sizeof(QualType));
845 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
846 Canonical);
847 Types.push_back(FTP);
848 FunctionTypeProtos.InsertNode(FTP, InsertPos);
849 return QualType(FTP, 0);
850}
851
Douglas Gregor1d661552008-04-13 21:07:44 +0000852/// getTypeDeclType - Return the unique reference to the type for the
853/// specified type declaration.
854QualType ASTContext::getTypeDeclType(TypeDecl *Decl) {
855 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
856
857 if (TypedefDecl *Typedef = dyn_cast_or_null<TypedefDecl>(Decl))
858 return getTypedefType(Typedef);
859 else if (ObjCInterfaceDecl *ObjCInterface
860 = dyn_cast_or_null<ObjCInterfaceDecl>(Decl))
861 return getObjCInterfaceType(ObjCInterface);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000862
863 if (CXXRecordDecl *CXXRecord = dyn_cast_or_null<CXXRecordDecl>(Decl))
864 Decl->TypeForDecl = new CXXRecordType(CXXRecord);
865 else if (RecordDecl *Record = dyn_cast_or_null<RecordDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000866 Decl->TypeForDecl = new RecordType(Record);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000867 else if (EnumDecl *Enum = dyn_cast_or_null<EnumDecl>(Decl))
Douglas Gregor1d661552008-04-13 21:07:44 +0000868 Decl->TypeForDecl = new EnumType(Enum);
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000869 else
Douglas Gregor1d661552008-04-13 21:07:44 +0000870 assert(false && "TypeDecl without a type?");
Argiris Kirtzidisea29d1e2008-08-07 20:55:28 +0000871
872 Types.push_back(Decl->TypeForDecl);
873 return QualType(Decl->TypeForDecl, 0);
Douglas Gregor1d661552008-04-13 21:07:44 +0000874}
875
Chris Lattner4b009652007-07-25 00:24:17 +0000876/// getTypedefType - Return the unique reference to the type for the
877/// specified typename decl.
878QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
879 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
880
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000881 QualType Canonical = getCanonicalType(Decl->getUnderlyingType());
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000882 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000883 Types.push_back(Decl->TypeForDecl);
884 return QualType(Decl->TypeForDecl, 0);
885}
886
Ted Kremenek42730c52008-01-07 19:49:32 +0000887/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000888/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000889QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000890 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
891
Ted Kremenek42730c52008-01-07 19:49:32 +0000892 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000893 Types.push_back(Decl->TypeForDecl);
894 return QualType(Decl->TypeForDecl, 0);
895}
896
Chris Lattnere1352302008-04-07 04:56:42 +0000897/// CmpProtocolNames - Comparison predicate for sorting protocols
898/// alphabetically.
899static bool CmpProtocolNames(const ObjCProtocolDecl *LHS,
900 const ObjCProtocolDecl *RHS) {
901 return strcmp(LHS->getName(), RHS->getName()) < 0;
902}
903
904static void SortAndUniqueProtocols(ObjCProtocolDecl **&Protocols,
905 unsigned &NumProtocols) {
906 ObjCProtocolDecl **ProtocolsEnd = Protocols+NumProtocols;
907
908 // Sort protocols, keyed by name.
909 std::sort(Protocols, Protocols+NumProtocols, CmpProtocolNames);
910
911 // Remove duplicates.
912 ProtocolsEnd = std::unique(Protocols, ProtocolsEnd);
913 NumProtocols = ProtocolsEnd-Protocols;
914}
915
916
Chris Lattnerb0c6a1f2008-04-07 04:44:08 +0000917/// getObjCQualifiedInterfaceType - Return a ObjCQualifiedInterfaceType type for
918/// the given interface decl and the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000919QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
920 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000921 // Sort the protocol list alphabetically to canonicalize it.
922 SortAndUniqueProtocols(Protocols, NumProtocols);
923
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000924 llvm::FoldingSetNodeID ID;
Chris Lattner7cdcb252008-04-07 06:38:24 +0000925 ObjCQualifiedInterfaceType::Profile(ID, Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000926
927 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000928 if (ObjCQualifiedInterfaceType *QT =
929 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000930 return QualType(QT, 0);
931
932 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000933 ObjCQualifiedInterfaceType *QType =
934 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000935 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000936 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000937 return QualType(QType, 0);
938}
939
Chris Lattnere1352302008-04-07 04:56:42 +0000940/// getObjCQualifiedIdType - Return an ObjCQualifiedIdType for the 'id' decl
941/// and the conforming protocol list.
Chris Lattner4a68fe02008-07-26 00:46:50 +0000942QualType ASTContext::getObjCQualifiedIdType(ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000943 unsigned NumProtocols) {
Chris Lattnere1352302008-04-07 04:56:42 +0000944 // Sort the protocol list alphabetically to canonicalize it.
945 SortAndUniqueProtocols(Protocols, NumProtocols);
946
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000947 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000948 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000949
950 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000951 if (ObjCQualifiedIdType *QT =
Chris Lattner4a68fe02008-07-26 00:46:50 +0000952 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000953 return QualType(QT, 0);
954
955 // No Match;
Chris Lattner4a68fe02008-07-26 00:46:50 +0000956 ObjCQualifiedIdType *QType = new ObjCQualifiedIdType(Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000957 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000958 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000959 return QualType(QType, 0);
960}
961
Steve Naroff0604dd92007-08-01 18:02:17 +0000962/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
963/// TypeOfExpr AST's (since expression's are never shared). For example,
964/// multiple declarations that refer to "typeof(x)" all contain different
965/// DeclRefExpr's. This doesn't effect the type checker, since it operates
966/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000967QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000968 QualType Canonical = getCanonicalType(tofExpr->getType());
Steve Naroff0604dd92007-08-01 18:02:17 +0000969 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
970 Types.push_back(toe);
971 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000972}
973
Steve Naroff0604dd92007-08-01 18:02:17 +0000974/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
975/// TypeOfType AST's. The only motivation to unique these nodes would be
976/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
977/// an issue. This doesn't effect the type checker, since it operates
978/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000979QualType ASTContext::getTypeOfType(QualType tofType) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +0000980 QualType Canonical = getCanonicalType(tofType);
Steve Naroff0604dd92007-08-01 18:02:17 +0000981 TypeOfType *tot = new TypeOfType(tofType, Canonical);
982 Types.push_back(tot);
983 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000984}
985
Chris Lattner4b009652007-07-25 00:24:17 +0000986/// getTagDeclType - Return the unique reference to the type for the
987/// specified TagDecl (struct/union/class/enum) decl.
988QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000989 assert (Decl);
Douglas Gregor1d661552008-04-13 21:07:44 +0000990 return getTypeDeclType(Decl);
Chris Lattner4b009652007-07-25 00:24:17 +0000991}
992
993/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
994/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
995/// needs to agree with the definition in <stddef.h>.
996QualType ASTContext::getSizeType() const {
997 // On Darwin, size_t is defined as a "long unsigned int".
998 // FIXME: should derive from "Target".
999 return UnsignedLongTy;
1000}
1001
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001002/// getWCharType - Return the unique type for "wchar_t" (C99 7.17), the
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001003/// width of characters in wide strings, The value is target dependent and
1004/// needs to agree with the definition in <stddef.h>.
Argiris Kirtzidis2a4e1162008-08-09 17:20:01 +00001005QualType ASTContext::getWCharType() const {
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001006 if (LangOpts.CPlusPlus)
1007 return WCharTy;
1008
Eli Friedmanfdd35d72008-02-12 08:29:21 +00001009 // On Darwin, wchar_t is defined as a "int".
1010 // FIXME: should derive from "Target".
1011 return IntTy;
1012}
1013
Argiris Kirtzidis1ed03e72008-08-09 16:51:54 +00001014/// getSignedWCharType - Return the type of "signed wchar_t".
1015/// Used when in C++, as a GCC extension.
1016QualType ASTContext::getSignedWCharType() const {
1017 // FIXME: derive from "Target" ?
1018 return WCharTy;
1019}
1020
1021/// getUnsignedWCharType - Return the type of "unsigned wchar_t".
1022/// Used when in C++, as a GCC extension.
1023QualType ASTContext::getUnsignedWCharType() const {
1024 // FIXME: derive from "Target" ?
1025 return UnsignedIntTy;
1026}
1027
Chris Lattner4b009652007-07-25 00:24:17 +00001028/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
1029/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
1030QualType ASTContext::getPointerDiffType() const {
1031 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
1032 // FIXME: should derive from "Target".
1033 return IntTy;
1034}
1035
Chris Lattner19eb97e2008-04-02 05:18:44 +00001036//===----------------------------------------------------------------------===//
1037// Type Operators
1038//===----------------------------------------------------------------------===//
1039
Chris Lattner3dae6f42008-04-06 22:41:35 +00001040/// getCanonicalType - Return the canonical (structural) type corresponding to
1041/// the specified potentially non-canonical type. The non-canonical version
1042/// of a type may have many "decorated" versions of types. Decorators can
1043/// include typedefs, 'typeof' operators, etc. The returned type is guaranteed
1044/// to be free of any of these, allowing two canonical types to be compared
1045/// for exact equality with a simple pointer comparison.
1046QualType ASTContext::getCanonicalType(QualType T) {
1047 QualType CanType = T.getTypePtr()->getCanonicalTypeInternal();
Chris Lattnera1923f62008-08-04 07:31:14 +00001048
1049 // If the result has type qualifiers, make sure to canonicalize them as well.
1050 unsigned TypeQuals = T.getCVRQualifiers() | CanType.getCVRQualifiers();
1051 if (TypeQuals == 0) return CanType;
1052
1053 // If the type qualifiers are on an array type, get the canonical type of the
1054 // array with the qualifiers applied to the element type.
1055 ArrayType *AT = dyn_cast<ArrayType>(CanType);
1056 if (!AT)
1057 return CanType.getQualifiedType(TypeQuals);
1058
1059 // Get the canonical version of the element with the extra qualifiers on it.
1060 // This can recursively sink qualifiers through multiple levels of arrays.
1061 QualType NewEltTy=AT->getElementType().getWithAdditionalQualifiers(TypeQuals);
1062 NewEltTy = getCanonicalType(NewEltTy);
1063
1064 if (ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1065 return getConstantArrayType(NewEltTy, CAT->getSize(),CAT->getSizeModifier(),
1066 CAT->getIndexTypeQualifier());
1067 if (IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(AT))
1068 return getIncompleteArrayType(NewEltTy, IAT->getSizeModifier(),
1069 IAT->getIndexTypeQualifier());
1070
1071 // FIXME: What is the ownership of size expressions in VLAs?
1072 VariableArrayType *VAT = cast<VariableArrayType>(AT);
1073 return getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1074 VAT->getSizeModifier(),
1075 VAT->getIndexTypeQualifier());
1076}
1077
1078
1079const ArrayType *ASTContext::getAsArrayType(QualType T) {
1080 // Handle the non-qualified case efficiently.
1081 if (T.getCVRQualifiers() == 0) {
1082 // Handle the common positive case fast.
1083 if (const ArrayType *AT = dyn_cast<ArrayType>(T))
1084 return AT;
1085 }
1086
1087 // Handle the common negative case fast, ignoring CVR qualifiers.
1088 QualType CType = T->getCanonicalTypeInternal();
1089
1090 // Make sure to look through type qualifiers (like ASQuals) for the negative
1091 // test.
1092 if (!isa<ArrayType>(CType) &&
1093 !isa<ArrayType>(CType.getUnqualifiedType()))
1094 return 0;
1095
1096 // Apply any CVR qualifiers from the array type to the element type. This
1097 // implements C99 6.7.3p8: "If the specification of an array type includes
1098 // any type qualifiers, the element type is so qualified, not the array type."
1099
1100 // If we get here, we either have type qualifiers on the type, or we have
1101 // sugar such as a typedef in the way. If we have type qualifiers on the type
1102 // we must propagate them down into the elemeng type.
1103 unsigned CVRQuals = T.getCVRQualifiers();
1104 unsigned AddrSpace = 0;
1105 Type *Ty = T.getTypePtr();
1106
1107 // Rip through ASQualType's and typedefs to get to a concrete type.
1108 while (1) {
1109 if (const ASQualType *ASQT = dyn_cast<ASQualType>(Ty)) {
1110 AddrSpace = ASQT->getAddressSpace();
1111 Ty = ASQT->getBaseType();
1112 } else {
1113 T = Ty->getDesugaredType();
1114 if (T.getTypePtr() == Ty && T.getCVRQualifiers() == 0)
1115 break;
1116 CVRQuals |= T.getCVRQualifiers();
1117 Ty = T.getTypePtr();
1118 }
1119 }
1120
1121 // If we have a simple case, just return now.
1122 const ArrayType *ATy = dyn_cast<ArrayType>(Ty);
1123 if (ATy == 0 || (AddrSpace == 0 && CVRQuals == 0))
1124 return ATy;
1125
1126 // Otherwise, we have an array and we have qualifiers on it. Push the
1127 // qualifiers into the array element type and return a new array type.
1128 // Get the canonical version of the element with the extra qualifiers on it.
1129 // This can recursively sink qualifiers through multiple levels of arrays.
1130 QualType NewEltTy = ATy->getElementType();
1131 if (AddrSpace)
1132 NewEltTy = getASQualType(NewEltTy, AddrSpace);
1133 NewEltTy = NewEltTy.getWithAdditionalQualifiers(CVRQuals);
1134
1135 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(ATy))
1136 return cast<ArrayType>(getConstantArrayType(NewEltTy, CAT->getSize(),
1137 CAT->getSizeModifier(),
1138 CAT->getIndexTypeQualifier()));
1139 if (const IncompleteArrayType *IAT = dyn_cast<IncompleteArrayType>(ATy))
1140 return cast<ArrayType>(getIncompleteArrayType(NewEltTy,
1141 IAT->getSizeModifier(),
1142 IAT->getIndexTypeQualifier()));
1143
1144 // FIXME: What is the ownership of size expressions in VLAs?
1145 const VariableArrayType *VAT = cast<VariableArrayType>(ATy);
1146 return cast<ArrayType>(getVariableArrayType(NewEltTy, VAT->getSizeExpr(),
1147 VAT->getSizeModifier(),
1148 VAT->getIndexTypeQualifier()));
Chris Lattner3dae6f42008-04-06 22:41:35 +00001149}
1150
1151
Chris Lattner19eb97e2008-04-02 05:18:44 +00001152/// getArrayDecayedType - Return the properly qualified result of decaying the
1153/// specified array type to a pointer. This operation is non-trivial when
1154/// handling typedefs etc. The canonical type of "T" must be an array type,
1155/// this returns a pointer to a properly qualified element of the array.
1156///
1157/// See C99 6.7.5.3p7 and C99 6.3.2.1p3.
1158QualType ASTContext::getArrayDecayedType(QualType Ty) {
Chris Lattnera1923f62008-08-04 07:31:14 +00001159 // Get the element type with 'getAsArrayType' so that we don't lose any
1160 // typedefs in the element type of the array. This also handles propagation
1161 // of type qualifiers from the array type into the element type if present
1162 // (C99 6.7.3p8).
1163 const ArrayType *PrettyArrayType = getAsArrayType(Ty);
1164 assert(PrettyArrayType && "Not an array type!");
Chris Lattner19eb97e2008-04-02 05:18:44 +00001165
Chris Lattnera1923f62008-08-04 07:31:14 +00001166 QualType PtrTy = getPointerType(PrettyArrayType->getElementType());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001167
1168 // int x[restrict 4] -> int *restrict
Chris Lattnera1923f62008-08-04 07:31:14 +00001169 return PtrTy.getQualifiedType(PrettyArrayType->getIndexTypeQualifier());
Chris Lattner19eb97e2008-04-02 05:18:44 +00001170}
1171
Chris Lattner4b009652007-07-25 00:24:17 +00001172/// getFloatingRank - Return a relative rank for floating point types.
1173/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001174static FloatingRank getFloatingRank(QualType T) {
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001175 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +00001176 return getFloatingRank(CT->getElementType());
Chris Lattnerd7135b42008-04-06 23:38:49 +00001177
Christopher Lamb2a72bb32008-02-04 02:31:56 +00001178 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattnerd7135b42008-04-06 23:38:49 +00001179 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +00001180 case BuiltinType::Float: return FloatRank;
1181 case BuiltinType::Double: return DoubleRank;
1182 case BuiltinType::LongDouble: return LongDoubleRank;
1183 }
1184}
1185
Steve Narofffa0c4532007-08-27 01:41:48 +00001186/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
1187/// point or a complex type (based on typeDomain/typeSize).
1188/// 'typeDomain' is a real floating point or complex type.
1189/// 'typeSize' is a real floating point or complex type.
Chris Lattner7794ae22008-04-06 23:58:54 +00001190QualType ASTContext::getFloatingTypeOfSizeWithinDomain(QualType Size,
1191 QualType Domain) const {
1192 FloatingRank EltRank = getFloatingRank(Size);
1193 if (Domain->isComplexType()) {
1194 switch (EltRank) {
Steve Narofffa0c4532007-08-27 01:41:48 +00001195 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +00001196 case FloatRank: return FloatComplexTy;
1197 case DoubleRank: return DoubleComplexTy;
1198 case LongDoubleRank: return LongDoubleComplexTy;
1199 }
Chris Lattner4b009652007-07-25 00:24:17 +00001200 }
Chris Lattner7794ae22008-04-06 23:58:54 +00001201
1202 assert(Domain->isRealFloatingType() && "Unknown domain!");
1203 switch (EltRank) {
1204 default: assert(0 && "getFloatingRank(): illegal value for rank");
1205 case FloatRank: return FloatTy;
1206 case DoubleRank: return DoubleTy;
1207 case LongDoubleRank: return LongDoubleTy;
Steve Naroff3cf497f2007-08-27 01:27:54 +00001208 }
Chris Lattner4b009652007-07-25 00:24:17 +00001209}
1210
Chris Lattner51285d82008-04-06 23:55:33 +00001211/// getFloatingTypeOrder - Compare the rank of the two specified floating
1212/// point types, ignoring the domain of the type (i.e. 'double' ==
1213/// '_Complex double'). If LHS > RHS, return 1. If LHS == RHS, return 0. If
1214/// LHS < RHS, return -1.
Chris Lattnerd7135b42008-04-06 23:38:49 +00001215int ASTContext::getFloatingTypeOrder(QualType LHS, QualType RHS) {
1216 FloatingRank LHSR = getFloatingRank(LHS);
1217 FloatingRank RHSR = getFloatingRank(RHS);
1218
1219 if (LHSR == RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001220 return 0;
Chris Lattnerd7135b42008-04-06 23:38:49 +00001221 if (LHSR > RHSR)
Steve Naroff45fc9822007-08-27 15:30:22 +00001222 return 1;
1223 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +00001224}
1225
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001226/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
1227/// routine will assert if passed a built-in type that isn't an integer or enum,
1228/// or if it is not canonicalized.
1229static unsigned getIntegerRank(Type *T) {
1230 assert(T->isCanonical() && "T should be canonicalized");
1231 if (isa<EnumType>(T))
1232 return 4;
1233
1234 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner51285d82008-04-06 23:55:33 +00001235 default: assert(0 && "getIntegerRank(): not a built-in integer");
1236 case BuiltinType::Bool:
1237 return 1;
1238 case BuiltinType::Char_S:
1239 case BuiltinType::Char_U:
1240 case BuiltinType::SChar:
1241 case BuiltinType::UChar:
1242 return 2;
1243 case BuiltinType::Short:
1244 case BuiltinType::UShort:
1245 return 3;
1246 case BuiltinType::Int:
1247 case BuiltinType::UInt:
1248 return 4;
1249 case BuiltinType::Long:
1250 case BuiltinType::ULong:
1251 return 5;
1252 case BuiltinType::LongLong:
1253 case BuiltinType::ULongLong:
1254 return 6;
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001255 }
1256}
1257
Chris Lattner51285d82008-04-06 23:55:33 +00001258/// getIntegerTypeOrder - Returns the highest ranked integer type:
1259/// C99 6.3.1.8p1. If LHS > RHS, return 1. If LHS == RHS, return 0. If
1260/// LHS < RHS, return -1.
1261int ASTContext::getIntegerTypeOrder(QualType LHS, QualType RHS) {
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001262 Type *LHSC = getCanonicalType(LHS).getTypePtr();
1263 Type *RHSC = getCanonicalType(RHS).getTypePtr();
Chris Lattner51285d82008-04-06 23:55:33 +00001264 if (LHSC == RHSC) return 0;
Chris Lattner4b009652007-07-25 00:24:17 +00001265
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001266 bool LHSUnsigned = LHSC->isUnsignedIntegerType();
1267 bool RHSUnsigned = RHSC->isUnsignedIntegerType();
Chris Lattner4b009652007-07-25 00:24:17 +00001268
Chris Lattner51285d82008-04-06 23:55:33 +00001269 unsigned LHSRank = getIntegerRank(LHSC);
1270 unsigned RHSRank = getIntegerRank(RHSC);
Chris Lattner4b009652007-07-25 00:24:17 +00001271
Chris Lattner51285d82008-04-06 23:55:33 +00001272 if (LHSUnsigned == RHSUnsigned) { // Both signed or both unsigned.
1273 if (LHSRank == RHSRank) return 0;
1274 return LHSRank > RHSRank ? 1 : -1;
1275 }
Chris Lattner4b009652007-07-25 00:24:17 +00001276
Chris Lattner51285d82008-04-06 23:55:33 +00001277 // Otherwise, the LHS is signed and the RHS is unsigned or visa versa.
1278 if (LHSUnsigned) {
1279 // If the unsigned [LHS] type is larger, return it.
1280 if (LHSRank >= RHSRank)
1281 return 1;
1282
1283 // If the signed type can represent all values of the unsigned type, it
1284 // wins. Because we are dealing with 2's complement and types that are
1285 // powers of two larger than each other, this is always safe.
1286 return -1;
1287 }
Chris Lattnerc1b68db2008-04-06 22:59:24 +00001288
Chris Lattner51285d82008-04-06 23:55:33 +00001289 // If the unsigned [RHS] type is larger, return it.
1290 if (RHSRank >= LHSRank)
1291 return -1;
1292
1293 // If the signed type can represent all values of the unsigned type, it
1294 // wins. Because we are dealing with 2's complement and types that are
1295 // powers of two larger than each other, this is always safe.
1296 return 1;
Chris Lattner4b009652007-07-25 00:24:17 +00001297}
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001298
1299// getCFConstantStringType - Return the type used for constant CFStrings.
1300QualType ASTContext::getCFConstantStringType() {
1301 if (!CFConstantStringTypeDecl) {
Chris Lattnere4650482008-03-15 06:12:44 +00001302 CFConstantStringTypeDecl =
Argiris Kirtzidisc6cc7d52008-06-09 23:19:58 +00001303 RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
Chris Lattner58114f02008-03-15 21:32:50 +00001304 &Idents.get("NSConstantString"), 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001305 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001306
1307 // const int *isa;
1308 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001309 // int flags;
1310 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001311 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001312 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001313 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001314 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001315 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001316 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001317
Anders Carlssonbb2cf512007-11-19 00:25:30 +00001318 for (unsigned i = 0; i < 4; ++i)
Chris Lattnerf3874bc2008-04-06 04:47:34 +00001319 FieldDecls[i] = FieldDecl::Create(*this, SourceLocation(), 0,
Chris Lattner81db64a2008-03-16 00:16:02 +00001320 FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +00001321
1322 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1323 }
1324
1325 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001326}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001327
Anders Carlssone3f02572007-10-29 06:33:42 +00001328// This returns true if a type has been typedefed to BOOL:
1329// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001330static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001331 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001332 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001333
1334 return false;
1335}
1336
Ted Kremenek42730c52008-01-07 19:49:32 +00001337/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001338/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001339int ASTContext::getObjCEncodingTypeSize(QualType type) {
Chris Lattner8cd0e932008-03-05 18:54:05 +00001340 uint64_t sz = getTypeSize(type);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001341
1342 // Make all integer and enum types at least as large as an int
1343 if (sz > 0 && type->isIntegralType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001344 sz = std::max(sz, getTypeSize(IntTy));
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001345 // Treat arrays as pointers, since that's how they're passed in.
1346 else if (type->isArrayType())
Chris Lattner8cd0e932008-03-05 18:54:05 +00001347 sz = getTypeSize(VoidPtrTy);
1348 return sz / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001349}
1350
Ted Kremenek42730c52008-01-07 19:49:32 +00001351/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001352/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001353void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001354 std::string& S)
1355{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001356 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001357 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001358 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001359 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001360 // Compute size of all parameters.
1361 // Start with computing size of a pointer in number of bytes.
1362 // FIXME: There might(should) be a better way of doing this computation!
1363 SourceLocation Loc;
Chris Lattner8cd0e932008-03-05 18:54:05 +00001364 int PtrSize = getTypeSize(VoidPtrTy) / getTypeSize(CharTy);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001365 // The first two arguments (self and _cmd) are pointers; account for
1366 // their size.
1367 int ParmOffset = 2 * PtrSize;
1368 int NumOfParams = Decl->getNumParams();
1369 for (int i = 0; i < NumOfParams; i++) {
1370 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001371 int sz = getObjCEncodingTypeSize (PType);
1372 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001373 ParmOffset += sz;
1374 }
1375 S += llvm::utostr(ParmOffset);
1376 S += "@0:";
1377 S += llvm::utostr(PtrSize);
1378
1379 // Argument types.
1380 ParmOffset = 2 * PtrSize;
1381 for (int i = 0; i < NumOfParams; i++) {
1382 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001383 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001384 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001385 getObjCEncodingForTypeQualifier(
1386 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001387 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001388 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001389 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001390 }
1391}
1392
Fariborz Jahanian248db262008-01-22 22:44:46 +00001393void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
Chris Lattnera1923f62008-08-04 07:31:14 +00001394 llvm::SmallVector<const RecordType *, 8> &ERType) const {
Anders Carlssone3f02572007-10-29 06:33:42 +00001395 // FIXME: This currently doesn't encode:
1396 // @ An object (whether statically typed or typed id)
1397 // # A class object (Class)
1398 // : A method selector (SEL)
1399 // {name=type...} A structure
1400 // (name=type...) A union
1401 // bnum A bit field of num bits
1402
1403 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001404 char encoding;
1405 switch (BT->getKind()) {
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001406 default: assert(0 && "Unhandled builtin type kind");
1407 case BuiltinType::Void: encoding = 'v'; break;
1408 case BuiltinType::Bool: encoding = 'B'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001409 case BuiltinType::Char_U:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001410 case BuiltinType::UChar: encoding = 'C'; break;
1411 case BuiltinType::UShort: encoding = 'S'; break;
1412 case BuiltinType::UInt: encoding = 'I'; break;
1413 case BuiltinType::ULong: encoding = 'L'; break;
1414 case BuiltinType::ULongLong: encoding = 'Q'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001415 case BuiltinType::Char_S:
Chris Lattner2bf1d6c2008-04-06 22:05:18 +00001416 case BuiltinType::SChar: encoding = 'c'; break;
1417 case BuiltinType::Short: encoding = 's'; break;
1418 case BuiltinType::Int: encoding = 'i'; break;
1419 case BuiltinType::Long: encoding = 'l'; break;
1420 case BuiltinType::LongLong: encoding = 'q'; break;
1421 case BuiltinType::Float: encoding = 'f'; break;
1422 case BuiltinType::Double: encoding = 'd'; break;
1423 case BuiltinType::LongDouble: encoding = 'd'; break;
Anders Carlsson36f07d82007-10-29 05:01:08 +00001424 }
1425
1426 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001427 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001428 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001429 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001430 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001431
1432 }
1433 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001434 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001435 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001436 S += '@';
1437 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001438 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001439 S += '#';
1440 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001441 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001442 S += ':';
1443 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001444 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001445
1446 if (PointeeTy->isCharType()) {
1447 // char pointer types should be encoded as '*' unless it is a
1448 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001449 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001450 S += '*';
1451 return;
1452 }
1453 }
1454
1455 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001456 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Chris Lattnera1923f62008-08-04 07:31:14 +00001457 } else if (const ArrayType *AT =
1458 // Ignore type qualifiers etc.
1459 dyn_cast<ArrayType>(T->getCanonicalTypeInternal())) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001460 S += '[';
1461
1462 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1463 S += llvm::utostr(CAT->getSize().getZExtValue());
1464 else
1465 assert(0 && "Unhandled array type!");
1466
Fariborz Jahanian248db262008-01-22 22:44:46 +00001467 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001468 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001469 } else if (T->getAsFunctionType()) {
1470 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001471 } else if (const RecordType *RTy = T->getAsRecordType()) {
1472 RecordDecl *RDecl= RTy->getDecl();
1473 S += '{';
1474 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001475 bool found = false;
1476 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1477 if (ERType[i] == RTy) {
1478 found = true;
1479 break;
1480 }
1481 if (!found) {
1482 ERType.push_back(RTy);
1483 S += '=';
1484 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1485 FieldDecl *field = RDecl->getMember(i);
1486 getObjCEncodingForType(field->getType(), S, ERType);
1487 }
1488 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1489 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001490 }
1491 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001492 } else if (T->isEnumeralType()) {
1493 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001494 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001495 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001496}
1497
Ted Kremenek42730c52008-01-07 19:49:32 +00001498void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001499 std::string& S) const {
1500 if (QT & Decl::OBJC_TQ_In)
1501 S += 'n';
1502 if (QT & Decl::OBJC_TQ_Inout)
1503 S += 'N';
1504 if (QT & Decl::OBJC_TQ_Out)
1505 S += 'o';
1506 if (QT & Decl::OBJC_TQ_Bycopy)
1507 S += 'O';
1508 if (QT & Decl::OBJC_TQ_Byref)
1509 S += 'R';
1510 if (QT & Decl::OBJC_TQ_Oneway)
1511 S += 'V';
1512}
1513
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001514void ASTContext::setBuiltinVaListType(QualType T)
1515{
1516 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1517
1518 BuiltinVaListType = T;
1519}
1520
Ted Kremenek42730c52008-01-07 19:49:32 +00001521void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001522{
Ted Kremenek42730c52008-01-07 19:49:32 +00001523 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001524
Ted Kremenek42730c52008-01-07 19:49:32 +00001525 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001526
1527 // typedef struct objc_object *id;
1528 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1529 assert(ptr && "'id' incorrectly typed");
1530 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1531 assert(rec && "'id' incorrectly typed");
1532 IdStructType = rec;
1533}
1534
Ted Kremenek42730c52008-01-07 19:49:32 +00001535void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001536{
Ted Kremenek42730c52008-01-07 19:49:32 +00001537 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001538
Ted Kremenek42730c52008-01-07 19:49:32 +00001539 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001540
1541 // typedef struct objc_selector *SEL;
1542 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1543 assert(ptr && "'SEL' incorrectly typed");
1544 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1545 assert(rec && "'SEL' incorrectly typed");
1546 SelStructType = rec;
1547}
1548
Ted Kremenek42730c52008-01-07 19:49:32 +00001549void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001550{
Ted Kremenek42730c52008-01-07 19:49:32 +00001551 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1552 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001553}
1554
Ted Kremenek42730c52008-01-07 19:49:32 +00001555void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001556{
Ted Kremenek42730c52008-01-07 19:49:32 +00001557 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001558
Ted Kremenek42730c52008-01-07 19:49:32 +00001559 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001560
1561 // typedef struct objc_class *Class;
1562 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1563 assert(ptr && "'Class' incorrectly typed");
1564 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1565 assert(rec && "'Class' incorrectly typed");
1566 ClassStructType = rec;
1567}
1568
Ted Kremenek42730c52008-01-07 19:49:32 +00001569void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1570 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001571 "'NSConstantString' type already set!");
1572
Ted Kremenek42730c52008-01-07 19:49:32 +00001573 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001574}
1575
Ted Kremenek118930e2008-07-24 23:58:27 +00001576
1577//===----------------------------------------------------------------------===//
1578// Type Predicates.
1579//===----------------------------------------------------------------------===//
1580
1581/// isObjCObjectPointerType - Returns true if type is an Objective-C pointer
1582/// to an object type. This includes "id" and "Class" (two 'special' pointers
1583/// to struct), Interface* (pointer to ObjCInterfaceType) and id<P> (qualified
1584/// ID type).
1585bool ASTContext::isObjCObjectPointerType(QualType Ty) const {
1586 if (Ty->isObjCQualifiedIdType())
1587 return true;
1588
1589 if (!Ty->isPointerType())
1590 return false;
1591
1592 // Check to see if this is 'id' or 'Class', both of which are typedefs for
1593 // pointer types. This looks for the typedef specifically, not for the
1594 // underlying type.
1595 if (Ty == getObjCIdType() || Ty == getObjCClassType())
1596 return true;
1597
1598 // If this a pointer to an interface (e.g. NSString*), it is ok.
1599 return Ty->getAsPointerType()->getPointeeType()->isObjCInterfaceType();
1600}
1601
Chris Lattner6ff358b2008-04-07 06:51:04 +00001602//===----------------------------------------------------------------------===//
1603// Type Compatibility Testing
1604//===----------------------------------------------------------------------===//
Chris Lattner5003e8b2007-11-01 05:03:41 +00001605
Chris Lattner390564e2008-04-07 06:49:41 +00001606/// C99 6.2.7p1: If both are complete types, then the following additional
1607/// requirements apply.
1608/// FIXME (handle compatibility across source files).
1609static bool areCompatTagTypes(TagType *LHS, TagType *RHS,
1610 const ASTContext &C) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001611 // "Class" and "id" are compatible built-in structure types.
Chris Lattner390564e2008-04-07 06:49:41 +00001612 if (C.isObjCIdType(QualType(LHS, 0)) && C.isObjCClassType(QualType(RHS, 0)) ||
1613 C.isObjCClassType(QualType(LHS, 0)) && C.isObjCIdType(QualType(RHS, 0)))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001614 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001615
Chris Lattner390564e2008-04-07 06:49:41 +00001616 // Within a translation unit a tag type is only compatible with itself. Self
1617 // equality is already handled by the time we get here.
1618 assert(LHS != RHS && "Self equality not handled!");
1619 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001620}
1621
1622bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1623 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1624 // identically qualified and both shall be pointers to compatible types.
Chris Lattner35fef522008-02-20 20:55:12 +00001625 if (lhs.getCVRQualifiers() != rhs.getCVRQualifiers() ||
1626 lhs.getAddressSpace() != rhs.getAddressSpace())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001627 return false;
1628
Chris Lattner25168a52008-07-26 21:30:36 +00001629 QualType ltype = lhs->getAsPointerType()->getPointeeType();
1630 QualType rtype = rhs->getAsPointerType()->getPointeeType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001631
1632 return typesAreCompatible(ltype, rtype);
1633}
1634
Steve Naroff85f0dc52007-10-15 20:41:53 +00001635bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
Chris Lattner25168a52008-07-26 21:30:36 +00001636 const FunctionType *lbase = lhs->getAsFunctionType();
1637 const FunctionType *rbase = rhs->getAsFunctionType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001638 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1639 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1640
1641 // first check the return types (common between C99 and K&R).
1642 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1643 return false;
1644
1645 if (lproto && rproto) { // two C99 style function prototypes
1646 unsigned lproto_nargs = lproto->getNumArgs();
1647 unsigned rproto_nargs = rproto->getNumArgs();
1648
1649 if (lproto_nargs != rproto_nargs)
1650 return false;
1651
1652 // both prototypes have the same number of arguments.
1653 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1654 (rproto->isVariadic() && !lproto->isVariadic()))
1655 return false;
1656
1657 // The use of ellipsis agree...now check the argument types.
1658 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001659 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1660 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001661 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001662 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001663 return false;
1664 return true;
1665 }
Chris Lattner1d78a862008-04-07 07:01:58 +00001666
Steve Naroff85f0dc52007-10-15 20:41:53 +00001667 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1668 return true;
1669
1670 // we have a mixture of K&R style with C99 prototypes
1671 const FunctionTypeProto *proto = lproto ? lproto : rproto;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001672 if (proto->isVariadic())
1673 return false;
1674
1675 // FIXME: Each parameter type T in the prototype must be compatible with the
1676 // type resulting from applying the usual argument conversions to T.
1677 return true;
1678}
1679
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001680// C99 6.7.5.2p6
1681static bool areCompatArrayTypes(ArrayType *LHS, ArrayType *RHS, ASTContext &C) {
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001682 // Constant arrays must be the same size to be compatible.
1683 if (const ConstantArrayType* LCAT = dyn_cast<ConstantArrayType>(LHS))
1684 if (const ConstantArrayType* RCAT = dyn_cast<ConstantArrayType>(RHS))
1685 if (RCAT->getSize() != LCAT->getSize())
1686 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001687
Chris Lattnerc8971d72008-04-07 06:58:21 +00001688 // Compatible arrays must have compatible element types
1689 return C.typesAreCompatible(LHS->getElementType(), RHS->getElementType());
Steve Naroff85f0dc52007-10-15 20:41:53 +00001690}
1691
Chris Lattner6ff358b2008-04-07 06:51:04 +00001692/// areCompatVectorTypes - Return true if the two specified vector types are
1693/// compatible.
1694static bool areCompatVectorTypes(const VectorType *LHS,
1695 const VectorType *RHS) {
1696 assert(LHS->isCanonical() && RHS->isCanonical());
1697 return LHS->getElementType() == RHS->getElementType() &&
1698 LHS->getNumElements() == RHS->getNumElements();
1699}
1700
1701/// areCompatObjCInterfaces - Return true if the two interface types are
1702/// compatible for assignment from RHS to LHS. This handles validation of any
1703/// protocol qualifiers on the LHS or RHS.
1704///
Chris Lattner1d78a862008-04-07 07:01:58 +00001705static bool areCompatObjCInterfaces(const ObjCInterfaceType *LHS,
1706 const ObjCInterfaceType *RHS) {
Chris Lattner6ff358b2008-04-07 06:51:04 +00001707 // Verify that the base decls are compatible: the RHS must be a subclass of
1708 // the LHS.
1709 if (!LHS->getDecl()->isSuperClassOf(RHS->getDecl()))
1710 return false;
1711
1712 // RHS must have a superset of the protocols in the LHS. If the LHS is not
1713 // protocol qualified at all, then we are good.
1714 if (!isa<ObjCQualifiedInterfaceType>(LHS))
1715 return true;
1716
1717 // Okay, we know the LHS has protocol qualifiers. If the RHS doesn't, then it
1718 // isn't a superset.
1719 if (!isa<ObjCQualifiedInterfaceType>(RHS))
1720 return true; // FIXME: should return false!
1721
1722 // Finally, we must have two protocol-qualified interfaces.
1723 const ObjCQualifiedInterfaceType *LHSP =cast<ObjCQualifiedInterfaceType>(LHS);
1724 const ObjCQualifiedInterfaceType *RHSP =cast<ObjCQualifiedInterfaceType>(RHS);
1725 ObjCQualifiedInterfaceType::qual_iterator LHSPI = LHSP->qual_begin();
1726 ObjCQualifiedInterfaceType::qual_iterator LHSPE = LHSP->qual_end();
1727 ObjCQualifiedInterfaceType::qual_iterator RHSPI = RHSP->qual_begin();
1728 ObjCQualifiedInterfaceType::qual_iterator RHSPE = RHSP->qual_end();
1729
1730 // All protocols in LHS must have a presence in RHS. Since the protocol lists
1731 // are both sorted alphabetically and have no duplicates, we can scan RHS and
1732 // LHS in a single parallel scan until we run out of elements in LHS.
1733 assert(LHSPI != LHSPE && "Empty LHS protocol list?");
1734 ObjCProtocolDecl *LHSProto = *LHSPI;
1735
1736 while (RHSPI != RHSPE) {
1737 ObjCProtocolDecl *RHSProto = *RHSPI++;
1738 // If the RHS has a protocol that the LHS doesn't, ignore it.
1739 if (RHSProto != LHSProto)
1740 continue;
1741
1742 // Otherwise, the RHS does have this element.
1743 ++LHSPI;
1744 if (LHSPI == LHSPE)
1745 return true; // All protocols in LHS exist in RHS.
1746
1747 LHSProto = *LHSPI;
1748 }
1749
1750 // If we got here, we didn't find one of the LHS's protocols in the RHS list.
1751 return false;
1752}
1753
1754
Steve Naroff85f0dc52007-10-15 20:41:53 +00001755/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1756/// both shall have the identically qualified version of a compatible type.
1757/// C99 6.2.7p1: Two types have compatible types if their types are the
1758/// same. See 6.7.[2,3,5] for additional rules.
Chris Lattner855fed42008-04-07 04:07:56 +00001759bool ASTContext::typesAreCompatible(QualType LHS_NC, QualType RHS_NC) {
Chris Lattner25168a52008-07-26 21:30:36 +00001760 QualType LHS = getCanonicalType(LHS_NC);
1761 QualType RHS = getCanonicalType(RHS_NC);
Chris Lattner4d5670b2008-04-03 05:07:04 +00001762
Bill Wendling6a9d8542007-12-03 07:33:35 +00001763 // C++ [expr]: If an expression initially has the type "reference to T", the
1764 // type is adjusted to "T" prior to any further analysis, the expression
1765 // designates the object or function denoted by the reference, and the
1766 // expression is an lvalue.
Chris Lattner855fed42008-04-07 04:07:56 +00001767 if (ReferenceType *RT = dyn_cast<ReferenceType>(LHS))
1768 LHS = RT->getPointeeType();
1769 if (ReferenceType *RT = dyn_cast<ReferenceType>(RHS))
1770 RHS = RT->getPointeeType();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001771
Chris Lattnerd47d6042008-04-07 05:37:56 +00001772 // If two types are identical, they are compatible.
1773 if (LHS == RHS)
1774 return true;
1775
1776 // If qualifiers differ, the types are different.
Chris Lattnerb5709e22008-04-07 05:43:21 +00001777 unsigned LHSAS = LHS.getAddressSpace(), RHSAS = RHS.getAddressSpace();
1778 if (LHS.getCVRQualifiers() != RHS.getCVRQualifiers() || LHSAS != RHSAS)
Chris Lattnerd47d6042008-04-07 05:37:56 +00001779 return false;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001780
1781 // Strip off ASQual's if present.
1782 if (LHSAS) {
1783 LHS = LHS.getUnqualifiedType();
1784 RHS = RHS.getUnqualifiedType();
1785 }
Chris Lattnerd47d6042008-04-07 05:37:56 +00001786
Chris Lattner855fed42008-04-07 04:07:56 +00001787 Type::TypeClass LHSClass = LHS->getTypeClass();
1788 Type::TypeClass RHSClass = RHS->getTypeClass();
Chris Lattnerc38d4522008-01-14 05:45:46 +00001789
1790 // We want to consider the two function types to be the same for these
1791 // comparisons, just force one to the other.
1792 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1793 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001794
1795 // Same as above for arrays
Chris Lattnerb5709e22008-04-07 05:43:21 +00001796 if (LHSClass == Type::VariableArray || LHSClass == Type::IncompleteArray)
1797 LHSClass = Type::ConstantArray;
1798 if (RHSClass == Type::VariableArray || RHSClass == Type::IncompleteArray)
1799 RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001800
Nate Begemanaf6ed502008-04-18 23:10:10 +00001801 // Canonicalize ExtVector -> Vector.
1802 if (LHSClass == Type::ExtVector) LHSClass = Type::Vector;
1803 if (RHSClass == Type::ExtVector) RHSClass = Type::Vector;
Chris Lattnerb5709e22008-04-07 05:43:21 +00001804
Chris Lattner7cdcb252008-04-07 06:38:24 +00001805 // Consider qualified interfaces and interfaces the same.
1806 if (LHSClass == Type::ObjCQualifiedInterface) LHSClass = Type::ObjCInterface;
1807 if (RHSClass == Type::ObjCQualifiedInterface) RHSClass = Type::ObjCInterface;
1808
Chris Lattnerb5709e22008-04-07 05:43:21 +00001809 // If the canonical type classes don't match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001810 if (LHSClass != RHSClass) {
Chris Lattner7cdcb252008-04-07 06:38:24 +00001811 // ID is compatible with all interface types.
1812 if (isa<ObjCInterfaceType>(LHS))
1813 return isObjCIdType(RHS);
1814 if (isa<ObjCInterfaceType>(RHS))
1815 return isObjCIdType(LHS);
Steve Naroff44549772008-06-04 15:07:33 +00001816
1817 // ID is compatible with all qualified id types.
1818 if (isa<ObjCQualifiedIdType>(LHS)) {
1819 if (const PointerType *PT = RHS->getAsPointerType())
1820 return isObjCIdType(PT->getPointeeType());
1821 }
1822 if (isa<ObjCQualifiedIdType>(RHS)) {
1823 if (const PointerType *PT = LHS->getAsPointerType())
1824 return isObjCIdType(PT->getPointeeType());
1825 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001826 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1827 // a signed integer type, or an unsigned integer type.
Chris Lattner855fed42008-04-07 04:07:56 +00001828 if (LHS->isEnumeralType() && RHS->isIntegralType()) {
1829 EnumDecl* EDecl = cast<EnumType>(LHS)->getDecl();
1830 return EDecl->getIntegerType() == RHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001831 }
Chris Lattner855fed42008-04-07 04:07:56 +00001832 if (RHS->isEnumeralType() && LHS->isIntegralType()) {
1833 EnumDecl* EDecl = cast<EnumType>(RHS)->getDecl();
1834 return EDecl->getIntegerType() == LHS;
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001835 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001836
Steve Naroff85f0dc52007-10-15 20:41:53 +00001837 return false;
1838 }
Chris Lattnerb5709e22008-04-07 05:43:21 +00001839
Steve Naroffc88babe2008-01-09 22:43:08 +00001840 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001841 switch (LHSClass) {
Chris Lattnerb5709e22008-04-07 05:43:21 +00001842 case Type::ASQual:
1843 case Type::FunctionProto:
1844 case Type::VariableArray:
1845 case Type::IncompleteArray:
1846 case Type::Reference:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001847 case Type::ObjCQualifiedInterface:
Chris Lattnerb5709e22008-04-07 05:43:21 +00001848 assert(0 && "Canonicalized away above");
Chris Lattnerc38d4522008-01-14 05:45:46 +00001849 case Type::Pointer:
Chris Lattner855fed42008-04-07 04:07:56 +00001850 return pointerTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001851 case Type::ConstantArray:
Chris Lattnerf0d2ee02008-04-07 06:56:55 +00001852 return areCompatArrayTypes(cast<ArrayType>(LHS), cast<ArrayType>(RHS),
1853 *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001854 case Type::FunctionNoProto:
Chris Lattner855fed42008-04-07 04:07:56 +00001855 return functionTypesAreCompatible(LHS, RHS);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001856 case Type::Tagged: // handle structures, unions
Chris Lattner390564e2008-04-07 06:49:41 +00001857 return areCompatTagTypes(cast<TagType>(LHS), cast<TagType>(RHS), *this);
Chris Lattnerc38d4522008-01-14 05:45:46 +00001858 case Type::Builtin:
Chris Lattnerd1240fa2008-04-07 05:55:38 +00001859 // Only exactly equal builtin types are compatible, which is tested above.
1860 return false;
1861 case Type::Vector:
1862 return areCompatVectorTypes(cast<VectorType>(LHS), cast<VectorType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001863 case Type::ObjCInterface:
Chris Lattner7cdcb252008-04-07 06:38:24 +00001864 return areCompatObjCInterfaces(cast<ObjCInterfaceType>(LHS),
1865 cast<ObjCInterfaceType>(RHS));
Chris Lattnerc38d4522008-01-14 05:45:46 +00001866 default:
1867 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001868 }
1869 return true; // should never get here...
1870}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001871
Chris Lattner1d78a862008-04-07 07:01:58 +00001872//===----------------------------------------------------------------------===//
Eli Friedman0832dbc2008-06-28 06:23:08 +00001873// Integer Predicates
1874//===----------------------------------------------------------------------===//
1875unsigned ASTContext::getIntWidth(QualType T) {
1876 if (T == BoolTy)
1877 return 1;
1878 // At the moment, only bool has padding bits
1879 return (unsigned)getTypeSize(T);
1880}
1881
1882QualType ASTContext::getCorrespondingUnsignedType(QualType T) {
1883 assert(T->isSignedIntegerType() && "Unexpected type");
1884 if (const EnumType* ETy = T->getAsEnumType())
1885 T = ETy->getDecl()->getIntegerType();
1886 const BuiltinType* BTy = T->getAsBuiltinType();
1887 assert (BTy && "Unexpected signed integer type");
1888 switch (BTy->getKind()) {
1889 case BuiltinType::Char_S:
1890 case BuiltinType::SChar:
1891 return UnsignedCharTy;
1892 case BuiltinType::Short:
1893 return UnsignedShortTy;
1894 case BuiltinType::Int:
1895 return UnsignedIntTy;
1896 case BuiltinType::Long:
1897 return UnsignedLongTy;
1898 case BuiltinType::LongLong:
1899 return UnsignedLongLongTy;
1900 default:
1901 assert(0 && "Unexpected signed integer type");
1902 return QualType();
1903 }
1904}
1905
1906
1907//===----------------------------------------------------------------------===//
Chris Lattner1d78a862008-04-07 07:01:58 +00001908// Serialization Support
1909//===----------------------------------------------------------------------===//
1910
Ted Kremenek738e6c02007-10-31 17:10:13 +00001911/// Emit - Serialize an ASTContext object to Bitcode.
1912void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek842126e2008-06-04 15:55:15 +00001913 S.Emit(LangOpts);
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001914 S.EmitRef(SourceMgr);
1915 S.EmitRef(Target);
1916 S.EmitRef(Idents);
1917 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001918
Ted Kremenek68228a92007-10-31 22:44:07 +00001919 // Emit the size of the type vector so that we can reserve that size
1920 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001921 S.EmitInt(Types.size());
1922
Ted Kremenek034a78c2007-11-13 22:02:55 +00001923 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1924 I!=E;++I)
1925 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001926
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001927 S.EmitOwnedPtr(TUDecl);
1928
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001929 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001930}
1931
Ted Kremenekacba3612007-11-13 00:25:37 +00001932ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek842126e2008-06-04 15:55:15 +00001933
1934 // Read the language options.
1935 LangOptions LOpts;
1936 LOpts.Read(D);
1937
Ted Kremenek68228a92007-10-31 22:44:07 +00001938 SourceManager &SM = D.ReadRef<SourceManager>();
1939 TargetInfo &t = D.ReadRef<TargetInfo>();
1940 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1941 SelectorTable &sels = D.ReadRef<SelectorTable>();
Chris Lattnereee57c02008-04-04 06:12:32 +00001942
Ted Kremenek68228a92007-10-31 22:44:07 +00001943 unsigned size_reserve = D.ReadInt();
1944
Ted Kremenek842126e2008-06-04 15:55:15 +00001945 ASTContext* A = new ASTContext(LOpts, SM, t, idents, sels, size_reserve);
Ted Kremenek68228a92007-10-31 22:44:07 +00001946
Ted Kremenek034a78c2007-11-13 22:02:55 +00001947 for (unsigned i = 0; i < size_reserve; ++i)
1948 Type::Create(*A,i,D);
Chris Lattnereee57c02008-04-04 06:12:32 +00001949
Argiris Kirtzidisd3586002008-04-17 14:40:12 +00001950 A->TUDecl = cast<TranslationUnitDecl>(D.ReadOwnedPtr<Decl>(*A));
1951
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001952 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001953
1954 return A;
1955}