blob: 322f34ccf2dc4f0995d5adcbe10d09a06f292a8d [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000020#include "llvm/Bitcode/Serialize.h"
21#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000022
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
25enum FloatingRank {
26 FloatRank, DoubleRank, LongDoubleRank
27};
28
29ASTContext::~ASTContext() {
30 // Deallocate all the types.
31 while (!Types.empty()) {
32 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
33 // Destroy the object, but don't call delete. These are malloc'd.
34 FT->~FunctionTypeProto();
35 free(FT);
36 } else {
37 delete Types.back();
38 }
39 Types.pop_back();
40 }
41}
42
43void ASTContext::PrintStats() const {
44 fprintf(stderr, "*** AST Context Stats:\n");
45 fprintf(stderr, " %d types total.\n", (int)Types.size());
46 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
47 unsigned NumVector = 0, NumComplex = 0;
48 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
49
50 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Chris Lattner8a35b462007-12-12 06:43:05 +000051 unsigned NumObjcInterfaces = 0, NumObjcQualifiedInterfaces = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000052
53 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
54 Type *T = Types[i];
55 if (isa<BuiltinType>(T))
56 ++NumBuiltin;
57 else if (isa<PointerType>(T))
58 ++NumPointer;
59 else if (isa<ReferenceType>(T))
60 ++NumReference;
61 else if (isa<ComplexType>(T))
62 ++NumComplex;
63 else if (isa<ArrayType>(T))
64 ++NumArray;
65 else if (isa<VectorType>(T))
66 ++NumVector;
67 else if (isa<FunctionTypeNoProto>(T))
68 ++NumFunctionNP;
69 else if (isa<FunctionTypeProto>(T))
70 ++NumFunctionP;
71 else if (isa<TypedefType>(T))
72 ++NumTypeName;
73 else if (TagType *TT = dyn_cast<TagType>(T)) {
74 ++NumTagged;
75 switch (TT->getDecl()->getKind()) {
76 default: assert(0 && "Unknown tagged type!");
77 case Decl::Struct: ++NumTagStruct; break;
78 case Decl::Union: ++NumTagUnion; break;
79 case Decl::Class: ++NumTagClass; break;
80 case Decl::Enum: ++NumTagEnum; break;
81 }
Steve Naroff948fd372007-09-17 14:16:13 +000082 } else if (isa<ObjcInterfaceType>(T))
83 ++NumObjcInterfaces;
Chris Lattner8a35b462007-12-12 06:43:05 +000084 else if (isa<ObjcQualifiedInterfaceType>(T))
85 ++NumObjcQualifiedInterfaces;
Steve Naroff948fd372007-09-17 14:16:13 +000086 else {
Chris Lattner8a35b462007-12-12 06:43:05 +000087 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +000088 assert(0 && "Unknown type!");
89 }
90 }
91
92 fprintf(stderr, " %d builtin types\n", NumBuiltin);
93 fprintf(stderr, " %d pointer types\n", NumPointer);
94 fprintf(stderr, " %d reference types\n", NumReference);
95 fprintf(stderr, " %d complex types\n", NumComplex);
96 fprintf(stderr, " %d array types\n", NumArray);
97 fprintf(stderr, " %d vector types\n", NumVector);
98 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
99 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
100 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
101 fprintf(stderr, " %d tagged types\n", NumTagged);
102 fprintf(stderr, " %d struct types\n", NumTagStruct);
103 fprintf(stderr, " %d union types\n", NumTagUnion);
104 fprintf(stderr, " %d class types\n", NumTagClass);
105 fprintf(stderr, " %d enum types\n", NumTagEnum);
Steve Naroff948fd372007-09-17 14:16:13 +0000106 fprintf(stderr, " %d interface types\n", NumObjcInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000107 fprintf(stderr, " %d protocol qualified interface types\n",
108 NumObjcQualifiedInterfaces);
Chris Lattner4b009652007-07-25 00:24:17 +0000109 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
110 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
111 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
112 NumFunctionP*sizeof(FunctionTypeProto)+
113 NumFunctionNP*sizeof(FunctionTypeNoProto)+
114 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
115}
116
117
118void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
119 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
120}
121
Chris Lattner4b009652007-07-25 00:24:17 +0000122void ASTContext::InitBuiltinTypes() {
123 assert(VoidTy.isNull() && "Context reinitialized?");
124
125 // C99 6.2.5p19.
126 InitBuiltinType(VoidTy, BuiltinType::Void);
127
128 // C99 6.2.5p2.
129 InitBuiltinType(BoolTy, BuiltinType::Bool);
130 // C99 6.2.5p3.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000131 if (Target.isCharSigned(FullSourceLoc()))
Chris Lattner4b009652007-07-25 00:24:17 +0000132 InitBuiltinType(CharTy, BuiltinType::Char_S);
133 else
134 InitBuiltinType(CharTy, BuiltinType::Char_U);
135 // C99 6.2.5p4.
136 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
137 InitBuiltinType(ShortTy, BuiltinType::Short);
138 InitBuiltinType(IntTy, BuiltinType::Int);
139 InitBuiltinType(LongTy, BuiltinType::Long);
140 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
141
142 // C99 6.2.5p6.
143 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
144 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
145 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
146 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
147 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
148
149 // C99 6.2.5p10.
150 InitBuiltinType(FloatTy, BuiltinType::Float);
151 InitBuiltinType(DoubleTy, BuiltinType::Double);
152 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
153
154 // C99 6.2.5p11.
155 FloatComplexTy = getComplexType(FloatTy);
156 DoubleComplexTy = getComplexType(DoubleTy);
157 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000158
159 BuiltinVaListType = QualType();
160 ObjcIdType = QualType();
161 IdStructType = 0;
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000162 ObjcClassType = QualType();
163 ClassStructType = 0;
164
Steve Narofff2e30312007-10-15 23:35:17 +0000165 ObjcConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000166
167 // void * type
168 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000169}
170
171//===----------------------------------------------------------------------===//
172// Type Sizing and Analysis
173//===----------------------------------------------------------------------===//
174
175/// getTypeSize - Return the size of the specified type, in bits. This method
176/// does not work on incomplete types.
177std::pair<uint64_t, unsigned>
178ASTContext::getTypeInfo(QualType T, SourceLocation L) {
179 T = T.getCanonicalType();
180 uint64_t Size;
181 unsigned Align;
182 switch (T->getTypeClass()) {
183 case Type::TypeName: assert(0 && "Not a canonical type!");
184 case Type::FunctionNoProto:
185 case Type::FunctionProto:
186 default:
187 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000188 case Type::VariableArray:
189 assert(0 && "VLAs not implemented yet!");
190 case Type::ConstantArray: {
191 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
192
Chris Lattner4b009652007-07-25 00:24:17 +0000193 std::pair<uint64_t, unsigned> EltInfo =
Steve Naroff83c13012007-08-30 01:06:46 +0000194 getTypeInfo(CAT->getElementType(), L);
195 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000196 Align = EltInfo.second;
197 break;
198 }
199 case Type::Vector: {
200 std::pair<uint64_t, unsigned> EltInfo =
201 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
202 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
203 // FIXME: Vector alignment is not the alignment of its elements.
204 Align = EltInfo.second;
205 break;
206 }
207
208 case Type::Builtin: {
209 // FIXME: need to use TargetInfo to derive the target specific sizes. This
210 // implementation will suffice for play with vector support.
Chris Lattner858eece2007-09-22 18:29:59 +0000211 const llvm::fltSemantics *F;
Chris Lattner4b009652007-07-25 00:24:17 +0000212 switch (cast<BuiltinType>(T)->getKind()) {
213 default: assert(0 && "Unknown builtin type!");
214 case BuiltinType::Void:
215 assert(0 && "Incomplete types have no size!");
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000216 case BuiltinType::Bool: Target.getBoolInfo(Size,Align,getFullLoc(L));
217 break;
218
Chris Lattner4b009652007-07-25 00:24:17 +0000219 case BuiltinType::Char_S:
220 case BuiltinType::Char_U:
221 case BuiltinType::UChar:
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000222 case BuiltinType::SChar: Target.getCharInfo(Size,Align,getFullLoc(L));
223 break;
224
Chris Lattner4b009652007-07-25 00:24:17 +0000225 case BuiltinType::UShort:
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000226 case BuiltinType::Short: Target.getShortInfo(Size,Align,getFullLoc(L));
227 break;
228
Chris Lattner4b009652007-07-25 00:24:17 +0000229 case BuiltinType::UInt:
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000230 case BuiltinType::Int: Target.getIntInfo(Size,Align,getFullLoc(L));
231 break;
232
Chris Lattner4b009652007-07-25 00:24:17 +0000233 case BuiltinType::ULong:
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000234 case BuiltinType::Long: Target.getLongInfo(Size,Align,getFullLoc(L));
235 break;
236
Chris Lattner4b009652007-07-25 00:24:17 +0000237 case BuiltinType::ULongLong:
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000238 case BuiltinType::LongLong: Target.getLongLongInfo(Size,Align,
239 getFullLoc(L));
240 break;
241
242 case BuiltinType::Float: Target.getFloatInfo(Size,Align,F,
243 getFullLoc(L));
244 break;
245
246 case BuiltinType::Double: Target.getDoubleInfo(Size,Align,F,
247 getFullLoc(L));
248 break;
249
250 case BuiltinType::LongDouble: Target.getLongDoubleInfo(Size,Align,F,
251 getFullLoc(L));
252 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000253 }
254 break;
255 }
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000256 case Type::Pointer: Target.getPointerInfo(Size, Align, getFullLoc(L)); break;
Chris Lattner4b009652007-07-25 00:24:17 +0000257 case Type::Reference:
258 // "When applied to a reference or a reference type, the result is the size
259 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
260 // FIXME: This is wrong for struct layout!
261 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
262
263 case Type::Complex: {
264 // Complex types have the same alignment as their elements, but twice the
265 // size.
266 std::pair<uint64_t, unsigned> EltInfo =
267 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
268 Size = EltInfo.first*2;
269 Align = EltInfo.second;
270 break;
271 }
272 case Type::Tagged:
Chris Lattnereb56d292007-08-27 17:38:00 +0000273 TagType *TT = cast<TagType>(T);
274 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
Devang Patel7a78e432007-11-01 19:11:01 +0000275 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl(), L);
Chris Lattnereb56d292007-08-27 17:38:00 +0000276 Size = Layout.getSize();
277 Align = Layout.getAlignment();
278 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattner90a018d2007-08-28 18:24:31 +0000279 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattnereb56d292007-08-27 17:38:00 +0000280 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000281 assert(0 && "Unimplemented type sizes!");
Chris Lattnereb56d292007-08-27 17:38:00 +0000282 }
Chris Lattner4b009652007-07-25 00:24:17 +0000283 break;
284 }
285
286 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
287 return std::make_pair(Size, Align);
288}
289
Devang Patel7a78e432007-11-01 19:11:01 +0000290/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000291/// specified record (struct/union/class), which indicates its size and field
292/// position information.
Devang Patel7a78e432007-11-01 19:11:01 +0000293const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D,
294 SourceLocation L) {
Chris Lattner4b009652007-07-25 00:24:17 +0000295 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
296
297 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000298 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000299 if (Entry) return *Entry;
300
Devang Patel7a78e432007-11-01 19:11:01 +0000301 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
302 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
303 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000304 Entry = NewEntry;
305
306 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
307 uint64_t RecordSize = 0;
308 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
309
310 if (D->getKind() != Decl::Union) {
311 // Layout each field, for now, just sequentially, respecting alignment. In
312 // the future, this will need to be tweakable by targets.
313 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
314 const FieldDecl *FD = D->getMember(i);
315 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
316 uint64_t FieldSize = FieldInfo.first;
317 unsigned FieldAlign = FieldInfo.second;
318
319 // Round up the current record size to the field's alignment boundary.
320 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
321
322 // Place this field at the current location.
323 FieldOffsets[i] = RecordSize;
324
325 // Reserve space for this field.
326 RecordSize += FieldSize;
327
328 // Remember max struct/class alignment.
329 RecordAlign = std::max(RecordAlign, FieldAlign);
330 }
331
332 // Finally, round the size of the total struct up to the alignment of the
333 // struct itself.
334 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
335 } else {
336 // Union layout just puts each member at the start of the record.
337 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
338 const FieldDecl *FD = D->getMember(i);
339 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
340 uint64_t FieldSize = FieldInfo.first;
341 unsigned FieldAlign = FieldInfo.second;
342
343 // Round up the current record size to the field's alignment boundary.
344 RecordSize = std::max(RecordSize, FieldSize);
345
346 // Place this field at the start of the record.
347 FieldOffsets[i] = 0;
348
349 // Remember max struct/class alignment.
350 RecordAlign = std::max(RecordAlign, FieldAlign);
351 }
352 }
353
354 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
355 return *NewEntry;
356}
357
Chris Lattner4b009652007-07-25 00:24:17 +0000358//===----------------------------------------------------------------------===//
359// Type creation/memoization methods
360//===----------------------------------------------------------------------===//
361
362
363/// getComplexType - Return the uniqued reference to the type for a complex
364/// number with the specified element type.
365QualType ASTContext::getComplexType(QualType T) {
366 // Unique pointers, to guarantee there is only one pointer of a particular
367 // structure.
368 llvm::FoldingSetNodeID ID;
369 ComplexType::Profile(ID, T);
370
371 void *InsertPos = 0;
372 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
373 return QualType(CT, 0);
374
375 // If the pointee type isn't canonical, this won't be a canonical type either,
376 // so fill in the canonical type field.
377 QualType Canonical;
378 if (!T->isCanonical()) {
379 Canonical = getComplexType(T.getCanonicalType());
380
381 // Get the new insert position for the node we care about.
382 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
383 assert(NewIP == 0 && "Shouldn't be in the map!");
384 }
385 ComplexType *New = new ComplexType(T, Canonical);
386 Types.push_back(New);
387 ComplexTypes.InsertNode(New, InsertPos);
388 return QualType(New, 0);
389}
390
391
392/// getPointerType - Return the uniqued reference to the type for a pointer to
393/// the specified type.
394QualType ASTContext::getPointerType(QualType T) {
395 // Unique pointers, to guarantee there is only one pointer of a particular
396 // structure.
397 llvm::FoldingSetNodeID ID;
398 PointerType::Profile(ID, T);
399
400 void *InsertPos = 0;
401 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
402 return QualType(PT, 0);
403
404 // If the pointee type isn't canonical, this won't be a canonical type either,
405 // so fill in the canonical type field.
406 QualType Canonical;
407 if (!T->isCanonical()) {
408 Canonical = getPointerType(T.getCanonicalType());
409
410 // Get the new insert position for the node we care about.
411 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
412 assert(NewIP == 0 && "Shouldn't be in the map!");
413 }
414 PointerType *New = new PointerType(T, Canonical);
415 Types.push_back(New);
416 PointerTypes.InsertNode(New, InsertPos);
417 return QualType(New, 0);
418}
419
420/// getReferenceType - Return the uniqued reference to the type for a reference
421/// to the specified type.
422QualType ASTContext::getReferenceType(QualType T) {
423 // Unique pointers, to guarantee there is only one pointer of a particular
424 // structure.
425 llvm::FoldingSetNodeID ID;
426 ReferenceType::Profile(ID, T);
427
428 void *InsertPos = 0;
429 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
430 return QualType(RT, 0);
431
432 // If the referencee type isn't canonical, this won't be a canonical type
433 // either, so fill in the canonical type field.
434 QualType Canonical;
435 if (!T->isCanonical()) {
436 Canonical = getReferenceType(T.getCanonicalType());
437
438 // Get the new insert position for the node we care about.
439 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
440 assert(NewIP == 0 && "Shouldn't be in the map!");
441 }
442
443 ReferenceType *New = new ReferenceType(T, Canonical);
444 Types.push_back(New);
445 ReferenceTypes.InsertNode(New, InsertPos);
446 return QualType(New, 0);
447}
448
Steve Naroff83c13012007-08-30 01:06:46 +0000449/// getConstantArrayType - Return the unique reference to the type for an
450/// array of the specified element type.
451QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000452 const llvm::APInt &ArySize,
453 ArrayType::ArraySizeModifier ASM,
454 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000455 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000456 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000457
458 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000459 if (ConstantArrayType *ATP =
460 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000461 return QualType(ATP, 0);
462
463 // If the element type isn't canonical, this won't be a canonical type either,
464 // so fill in the canonical type field.
465 QualType Canonical;
466 if (!EltTy->isCanonical()) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000467 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
468 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000469 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000470 ConstantArrayType *NewIP =
471 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
472
Chris Lattner4b009652007-07-25 00:24:17 +0000473 assert(NewIP == 0 && "Shouldn't be in the map!");
474 }
475
Steve Naroff24c9b982007-08-30 18:10:14 +0000476 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
477 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000478 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000479 Types.push_back(New);
480 return QualType(New, 0);
481}
482
Steve Naroffe2579e32007-08-30 18:14:25 +0000483/// getVariableArrayType - Returns a non-unique reference to the type for a
484/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000485QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
486 ArrayType::ArraySizeModifier ASM,
487 unsigned EltTypeQuals) {
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000488 if (NumElts) {
489 // Since we don't unique expressions, it isn't possible to unique VLA's
490 // that have an expression provided for their size.
491
Ted Kremenek2058dc42007-10-30 16:41:53 +0000492 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
493 ASM, EltTypeQuals);
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000494
Ted Kremenek2058dc42007-10-30 16:41:53 +0000495 CompleteVariableArrayTypes.push_back(New);
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000496 Types.push_back(New);
497 return QualType(New, 0);
498 }
499 else {
500 // No size is provided for the VLA. These we can unique.
501 llvm::FoldingSetNodeID ID;
502 VariableArrayType::Profile(ID, EltTy);
503
504 void *InsertPos = 0;
505 if (VariableArrayType *ATP =
506 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
507 return QualType(ATP, 0);
508
509 // If the element type isn't canonical, this won't be a canonical type
510 // either, so fill in the canonical type field.
511 QualType Canonical;
512
513 if (!EltTy->isCanonical()) {
514 Canonical = getVariableArrayType(EltTy.getCanonicalType(), NumElts,
515 ASM, EltTypeQuals);
516
517 // Get the new insert position for the node we care about.
518 VariableArrayType *NewIP =
519 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
520
521 assert(NewIP == 0 && "Shouldn't be in the map!");
522 }
523
524 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
525 ASM, EltTypeQuals);
526
527 IncompleteVariableArrayTypes.InsertNode(New, InsertPos);
528 Types.push_back(New);
529 return QualType(New, 0);
530 }
Steve Naroff83c13012007-08-30 01:06:46 +0000531}
532
Chris Lattner4b009652007-07-25 00:24:17 +0000533/// getVectorType - Return the unique reference to a vector type of
534/// the specified element type and size. VectorType must be a built-in type.
535QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
536 BuiltinType *baseType;
537
538 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
539 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
540
541 // Check if we've already instantiated a vector of this type.
542 llvm::FoldingSetNodeID ID;
543 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
544 void *InsertPos = 0;
545 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
546 return QualType(VTP, 0);
547
548 // If the element type isn't canonical, this won't be a canonical type either,
549 // so fill in the canonical type field.
550 QualType Canonical;
551 if (!vecType->isCanonical()) {
552 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
553
554 // Get the new insert position for the node we care about.
555 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
556 assert(NewIP == 0 && "Shouldn't be in the map!");
557 }
558 VectorType *New = new VectorType(vecType, NumElts, Canonical);
559 VectorTypes.InsertNode(New, InsertPos);
560 Types.push_back(New);
561 return QualType(New, 0);
562}
563
564/// getOCUVectorType - Return the unique reference to an OCU vector type of
565/// the specified element type and size. VectorType must be a built-in type.
566QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
567 BuiltinType *baseType;
568
569 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
570 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
571
572 // Check if we've already instantiated a vector of this type.
573 llvm::FoldingSetNodeID ID;
574 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
575 void *InsertPos = 0;
576 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
577 return QualType(VTP, 0);
578
579 // If the element type isn't canonical, this won't be a canonical type either,
580 // so fill in the canonical type field.
581 QualType Canonical;
582 if (!vecType->isCanonical()) {
583 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
584
585 // Get the new insert position for the node we care about.
586 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
587 assert(NewIP == 0 && "Shouldn't be in the map!");
588 }
589 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
590 VectorTypes.InsertNode(New, InsertPos);
591 Types.push_back(New);
592 return QualType(New, 0);
593}
594
595/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
596///
597QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
598 // Unique functions, to guarantee there is only one function of a particular
599 // structure.
600 llvm::FoldingSetNodeID ID;
601 FunctionTypeNoProto::Profile(ID, ResultTy);
602
603 void *InsertPos = 0;
604 if (FunctionTypeNoProto *FT =
605 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
606 return QualType(FT, 0);
607
608 QualType Canonical;
609 if (!ResultTy->isCanonical()) {
610 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
611
612 // Get the new insert position for the node we care about.
613 FunctionTypeNoProto *NewIP =
614 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
615 assert(NewIP == 0 && "Shouldn't be in the map!");
616 }
617
618 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
619 Types.push_back(New);
620 FunctionTypeProtos.InsertNode(New, InsertPos);
621 return QualType(New, 0);
622}
623
624/// getFunctionType - Return a normal function type with a typed argument
625/// list. isVariadic indicates whether the argument list includes '...'.
626QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
627 unsigned NumArgs, bool isVariadic) {
628 // Unique functions, to guarantee there is only one function of a particular
629 // structure.
630 llvm::FoldingSetNodeID ID;
631 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
632
633 void *InsertPos = 0;
634 if (FunctionTypeProto *FTP =
635 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
636 return QualType(FTP, 0);
637
638 // Determine whether the type being created is already canonical or not.
639 bool isCanonical = ResultTy->isCanonical();
640 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
641 if (!ArgArray[i]->isCanonical())
642 isCanonical = false;
643
644 // If this type isn't canonical, get the canonical version of it.
645 QualType Canonical;
646 if (!isCanonical) {
647 llvm::SmallVector<QualType, 16> CanonicalArgs;
648 CanonicalArgs.reserve(NumArgs);
649 for (unsigned i = 0; i != NumArgs; ++i)
650 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
651
652 Canonical = getFunctionType(ResultTy.getCanonicalType(),
653 &CanonicalArgs[0], NumArgs,
654 isVariadic);
655
656 // Get the new insert position for the node we care about.
657 FunctionTypeProto *NewIP =
658 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
659 assert(NewIP == 0 && "Shouldn't be in the map!");
660 }
661
662 // FunctionTypeProto objects are not allocated with new because they have a
663 // variable size array (for parameter types) at the end of them.
664 FunctionTypeProto *FTP =
665 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
666 NumArgs*sizeof(QualType));
667 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
668 Canonical);
669 Types.push_back(FTP);
670 FunctionTypeProtos.InsertNode(FTP, InsertPos);
671 return QualType(FTP, 0);
672}
673
674/// getTypedefType - Return the unique reference to the type for the
675/// specified typename decl.
676QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
677 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
678
679 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
680 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
681 Types.push_back(Decl->TypeForDecl);
682 return QualType(Decl->TypeForDecl, 0);
683}
684
Steve Naroff81f1bba2007-09-06 21:24:23 +0000685/// getObjcInterfaceType - Return the unique reference to the type for the
686/// specified ObjC interface decl.
687QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) {
688 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
689
690 Decl->TypeForDecl = new ObjcInterfaceType(Decl);
691 Types.push_back(Decl->TypeForDecl);
692 return QualType(Decl->TypeForDecl, 0);
693}
694
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000695/// getObjcQualifiedInterfaceType - Return a
696/// ObjcQualifiedInterfaceType type for the given interface decl and
697/// the conforming protocol list.
698QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl,
699 ObjcProtocolDecl **Protocols, unsigned NumProtocols) {
700 ObjcInterfaceType *IType =
701 cast<ObjcInterfaceType>(getObjcInterfaceType(Decl));
702
703 llvm::FoldingSetNodeID ID;
704 ObjcQualifiedInterfaceType::Profile(ID, IType, Protocols, NumProtocols);
705
706 void *InsertPos = 0;
707 if (ObjcQualifiedInterfaceType *QT =
708 ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
709 return QualType(QT, 0);
710
711 // No Match;
Chris Lattnerd855a6e2007-10-11 03:36:41 +0000712 ObjcQualifiedInterfaceType *QType =
713 new ObjcQualifiedInterfaceType(IType, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000714 Types.push_back(QType);
715 ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
716 return QualType(QType, 0);
717}
718
Steve Naroff0604dd92007-08-01 18:02:17 +0000719/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
720/// TypeOfExpr AST's (since expression's are never shared). For example,
721/// multiple declarations that refer to "typeof(x)" all contain different
722/// DeclRefExpr's. This doesn't effect the type checker, since it operates
723/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000724QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroff7cbb1462007-07-31 12:34:36 +0000725 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000726 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
727 Types.push_back(toe);
728 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000729}
730
Steve Naroff0604dd92007-08-01 18:02:17 +0000731/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
732/// TypeOfType AST's. The only motivation to unique these nodes would be
733/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
734/// an issue. This doesn't effect the type checker, since it operates
735/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000736QualType ASTContext::getTypeOfType(QualType tofType) {
737 QualType Canonical = tofType.getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000738 TypeOfType *tot = new TypeOfType(tofType, Canonical);
739 Types.push_back(tot);
740 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000741}
742
Chris Lattner4b009652007-07-25 00:24:17 +0000743/// getTagDeclType - Return the unique reference to the type for the
744/// specified TagDecl (struct/union/class/enum) decl.
745QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000746 assert (Decl);
747
Ted Kremenekf05026d2007-11-14 00:03:20 +0000748 // The decl stores the type cache.
Ted Kremenekae8fa032007-11-26 21:16:01 +0000749 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekf05026d2007-11-14 00:03:20 +0000750
751 TagType* T = new TagType(Decl, QualType());
Ted Kremenekae8fa032007-11-26 21:16:01 +0000752 Types.push_back(T);
753 Decl->TypeForDecl = T;
Ted Kremenekf05026d2007-11-14 00:03:20 +0000754
755 return QualType(T, 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000756}
757
758/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
759/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
760/// needs to agree with the definition in <stddef.h>.
761QualType ASTContext::getSizeType() const {
762 // On Darwin, size_t is defined as a "long unsigned int".
763 // FIXME: should derive from "Target".
764 return UnsignedLongTy;
765}
766
767/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
768/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
769QualType ASTContext::getPointerDiffType() const {
770 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
771 // FIXME: should derive from "Target".
772 return IntTy;
773}
774
775/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
776/// routine will assert if passed a built-in type that isn't an integer or enum.
777static int getIntegerRank(QualType t) {
778 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
779 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
780 return 4;
781 }
782
783 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
784 switch (BT->getKind()) {
785 default:
786 assert(0 && "getIntegerRank(): not a built-in integer");
787 case BuiltinType::Bool:
788 return 1;
789 case BuiltinType::Char_S:
790 case BuiltinType::Char_U:
791 case BuiltinType::SChar:
792 case BuiltinType::UChar:
793 return 2;
794 case BuiltinType::Short:
795 case BuiltinType::UShort:
796 return 3;
797 case BuiltinType::Int:
798 case BuiltinType::UInt:
799 return 4;
800 case BuiltinType::Long:
801 case BuiltinType::ULong:
802 return 5;
803 case BuiltinType::LongLong:
804 case BuiltinType::ULongLong:
805 return 6;
806 }
807}
808
809/// getFloatingRank - Return a relative rank for floating point types.
810/// This routine will assert if passed a built-in type that isn't a float.
811static int getFloatingRank(QualType T) {
812 T = T.getCanonicalType();
813 if (ComplexType *CT = dyn_cast<ComplexType>(T))
814 return getFloatingRank(CT->getElementType());
815
816 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner5003e8b2007-11-01 05:03:41 +0000817 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +0000818 case BuiltinType::Float: return FloatRank;
819 case BuiltinType::Double: return DoubleRank;
820 case BuiltinType::LongDouble: return LongDoubleRank;
821 }
822}
823
Steve Narofffa0c4532007-08-27 01:41:48 +0000824/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
825/// point or a complex type (based on typeDomain/typeSize).
826/// 'typeDomain' is a real floating point or complex type.
827/// 'typeSize' is a real floating point or complex type.
Steve Naroff3cf497f2007-08-27 01:27:54 +0000828QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
829 QualType typeSize, QualType typeDomain) const {
830 if (typeDomain->isComplexType()) {
831 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000832 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000833 case FloatRank: return FloatComplexTy;
834 case DoubleRank: return DoubleComplexTy;
835 case LongDoubleRank: return LongDoubleComplexTy;
836 }
Chris Lattner4b009652007-07-25 00:24:17 +0000837 }
Steve Naroff3cf497f2007-08-27 01:27:54 +0000838 if (typeDomain->isRealFloatingType()) {
839 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000840 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000841 case FloatRank: return FloatTy;
842 case DoubleRank: return DoubleTy;
843 case LongDoubleRank: return LongDoubleTy;
844 }
845 }
846 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000847 //an invalid return value, but the assert
848 //will ensure that this code is never reached.
849 return VoidTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000850}
851
Steve Naroff45fc9822007-08-27 15:30:22 +0000852/// compareFloatingType - Handles 3 different combos:
853/// float/float, float/complex, complex/complex.
854/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
855int ASTContext::compareFloatingType(QualType lt, QualType rt) {
856 if (getFloatingRank(lt) == getFloatingRank(rt))
857 return 0;
858 if (getFloatingRank(lt) > getFloatingRank(rt))
859 return 1;
860 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +0000861}
862
863// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
864// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
865QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
866 if (lhs == rhs) return lhs;
867
868 bool t1Unsigned = lhs->isUnsignedIntegerType();
869 bool t2Unsigned = rhs->isUnsignedIntegerType();
870
871 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
872 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
873
874 // We have two integer types with differing signs
875 QualType unsignedType = t1Unsigned ? lhs : rhs;
876 QualType signedType = t1Unsigned ? rhs : lhs;
877
878 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
879 return unsignedType;
880 else {
881 // FIXME: Need to check if the signed type can represent all values of the
882 // unsigned type. If it can, then the result is the signed type.
883 // If it can't, then the result is the unsigned version of the signed type.
884 // Should probably add a helper that returns a signed integer type from
885 // an unsigned (and vice versa). C99 6.3.1.8.
886 return signedType;
887 }
888}
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000889
890// getCFConstantStringType - Return the type used for constant CFStrings.
891QualType ASTContext::getCFConstantStringType() {
892 if (!CFConstantStringTypeDecl) {
893 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroff0add5d22007-11-03 11:27:19 +0000894 &Idents.get("NSConstantString"),
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000895 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000896 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000897
898 // const int *isa;
899 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000900 // int flags;
901 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000902 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000903 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000904 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000905 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000906 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000907 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000908
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000909 for (unsigned i = 0; i < 4; ++i)
Steve Naroffdc1ad762007-09-14 02:20:46 +0000910 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000911
912 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
913 }
914
915 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +0000916}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +0000917
Anders Carlssone3f02572007-10-29 06:33:42 +0000918// This returns true if a type has been typedefed to BOOL:
919// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +0000920static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +0000921 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +0000922 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +0000923
924 return false;
925}
926
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000927/// getObjcEncodingTypeSize returns size of type for objective-c encoding
928/// purpose.
929int ASTContext::getObjcEncodingTypeSize(QualType type) {
930 SourceLocation Loc;
931 uint64_t sz = getTypeSize(type, Loc);
932
933 // Make all integer and enum types at least as large as an int
934 if (sz > 0 && type->isIntegralType())
935 sz = std::max(sz, getTypeSize(IntTy, Loc));
936 // Treat arrays as pointers, since that's how they're passed in.
937 else if (type->isArrayType())
938 sz = getTypeSize(VoidPtrTy, Loc);
939 return sz / getTypeSize(CharTy, Loc);
940}
941
942/// getObjcEncodingForMethodDecl - Return the encoded type for this method
943/// declaration.
944void ASTContext::getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl,
945 std::string& S)
946{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +0000947 // Encode type qualifer, 'in', 'inout', etc. for the return type.
948 getObjcEncodingForTypeQualifier(Decl->getObjcDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000949 // Encode result type.
950 getObjcEncodingForType(Decl->getResultType(), S);
951 // Compute size of all parameters.
952 // Start with computing size of a pointer in number of bytes.
953 // FIXME: There might(should) be a better way of doing this computation!
954 SourceLocation Loc;
955 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
956 // The first two arguments (self and _cmd) are pointers; account for
957 // their size.
958 int ParmOffset = 2 * PtrSize;
959 int NumOfParams = Decl->getNumParams();
960 for (int i = 0; i < NumOfParams; i++) {
961 QualType PType = Decl->getParamDecl(i)->getType();
962 int sz = getObjcEncodingTypeSize (PType);
963 assert (sz > 0 && "getObjcEncodingForMethodDecl - Incomplete param type");
964 ParmOffset += sz;
965 }
966 S += llvm::utostr(ParmOffset);
967 S += "@0:";
968 S += llvm::utostr(PtrSize);
969
970 // Argument types.
971 ParmOffset = 2 * PtrSize;
972 for (int i = 0; i < NumOfParams; i++) {
973 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +0000974 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000975 // 'in', 'inout', etc.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +0000976 getObjcEncodingForTypeQualifier(
977 Decl->getParamDecl(i)->getObjcDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000978 getObjcEncodingForType(PType, S);
979 S += llvm::utostr(ParmOffset);
980 ParmOffset += getObjcEncodingTypeSize(PType);
981 }
982}
983
Anders Carlsson36f07d82007-10-29 05:01:08 +0000984void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const
985{
Anders Carlssone3f02572007-10-29 06:33:42 +0000986 // FIXME: This currently doesn't encode:
987 // @ An object (whether statically typed or typed id)
988 // # A class object (Class)
989 // : A method selector (SEL)
990 // {name=type...} A structure
991 // (name=type...) A union
992 // bnum A bit field of num bits
993
994 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +0000995 char encoding;
996 switch (BT->getKind()) {
997 case BuiltinType::Void:
998 encoding = 'v';
999 break;
1000 case BuiltinType::Bool:
1001 encoding = 'B';
1002 break;
1003 case BuiltinType::Char_U:
1004 case BuiltinType::UChar:
1005 encoding = 'C';
1006 break;
1007 case BuiltinType::UShort:
1008 encoding = 'S';
1009 break;
1010 case BuiltinType::UInt:
1011 encoding = 'I';
1012 break;
1013 case BuiltinType::ULong:
1014 encoding = 'L';
1015 break;
1016 case BuiltinType::ULongLong:
1017 encoding = 'Q';
1018 break;
1019 case BuiltinType::Char_S:
1020 case BuiltinType::SChar:
1021 encoding = 'c';
1022 break;
1023 case BuiltinType::Short:
1024 encoding = 's';
1025 break;
1026 case BuiltinType::Int:
1027 encoding = 'i';
1028 break;
1029 case BuiltinType::Long:
1030 encoding = 'l';
1031 break;
1032 case BuiltinType::LongLong:
1033 encoding = 'q';
1034 break;
1035 case BuiltinType::Float:
1036 encoding = 'f';
1037 break;
1038 case BuiltinType::Double:
1039 encoding = 'd';
1040 break;
1041 case BuiltinType::LongDouble:
1042 encoding = 'd';
1043 break;
1044 default:
1045 assert(0 && "Unhandled builtin type kind");
1046 }
1047
1048 S += encoding;
Anders Carlssone3f02572007-10-29 06:33:42 +00001049 } else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001050 QualType PointeeTy = PT->getPointeeType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001051 if (isObjcIdType(PointeeTy) || PointeeTy->isObjcInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001052 S += '@';
1053 return;
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001054 } else if (isObjcClassType(PointeeTy)) {
1055 S += '#';
1056 return;
1057 } else if (isObjcSelType(PointeeTy)) {
1058 S += ':';
1059 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001060 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001061
1062 if (PointeeTy->isCharType()) {
1063 // char pointer types should be encoded as '*' unless it is a
1064 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001065 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001066 S += '*';
1067 return;
1068 }
1069 }
1070
1071 S += '^';
1072 getObjcEncodingForType(PT->getPointeeType(), S);
Anders Carlssone3f02572007-10-29 06:33:42 +00001073 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001074 S += '[';
1075
1076 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1077 S += llvm::utostr(CAT->getSize().getZExtValue());
1078 else
1079 assert(0 && "Unhandled array type!");
1080
1081 getObjcEncodingForType(AT->getElementType(), S);
1082 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001083 } else if (T->getAsFunctionType()) {
1084 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001085 } else if (const RecordType *RTy = T->getAsRecordType()) {
1086 RecordDecl *RDecl= RTy->getDecl();
1087 S += '{';
1088 S += RDecl->getName();
1089 S += '=';
1090 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1091 FieldDecl *field = RDecl->getMember(i);
1092 getObjcEncodingForType(field->getType(), S);
1093 }
1094 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001095 } else if (T->isEnumeralType()) {
1096 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001097 } else
Steve Naroff49af3f32007-12-12 22:30:11 +00001098 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001099}
1100
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001101void ASTContext::getObjcEncodingForTypeQualifier(Decl::ObjcDeclQualifier QT,
1102 std::string& S) const {
1103 if (QT & Decl::OBJC_TQ_In)
1104 S += 'n';
1105 if (QT & Decl::OBJC_TQ_Inout)
1106 S += 'N';
1107 if (QT & Decl::OBJC_TQ_Out)
1108 S += 'o';
1109 if (QT & Decl::OBJC_TQ_Bycopy)
1110 S += 'O';
1111 if (QT & Decl::OBJC_TQ_Byref)
1112 S += 'R';
1113 if (QT & Decl::OBJC_TQ_Oneway)
1114 S += 'V';
1115}
1116
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001117void ASTContext::setBuiltinVaListType(QualType T)
1118{
1119 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1120
1121 BuiltinVaListType = T;
1122}
1123
Steve Naroff9d12c902007-10-15 14:41:52 +00001124void ASTContext::setObjcIdType(TypedefDecl *TD)
1125{
1126 assert(ObjcIdType.isNull() && "'id' type already set!");
1127
1128 ObjcIdType = getTypedefType(TD);
1129
1130 // typedef struct objc_object *id;
1131 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1132 assert(ptr && "'id' incorrectly typed");
1133 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1134 assert(rec && "'id' incorrectly typed");
1135 IdStructType = rec;
1136}
1137
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001138void ASTContext::setObjcSelType(TypedefDecl *TD)
1139{
1140 assert(ObjcSelType.isNull() && "'SEL' type already set!");
1141
1142 ObjcSelType = getTypedefType(TD);
1143
1144 // typedef struct objc_selector *SEL;
1145 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1146 assert(ptr && "'SEL' incorrectly typed");
1147 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1148 assert(rec && "'SEL' incorrectly typed");
1149 SelStructType = rec;
1150}
1151
Fariborz Jahanianb4452ed2007-12-07 00:18:54 +00001152void ASTContext::setObjcProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001153{
1154 assert(ObjcProtoType.isNull() && "'Protocol' type already set!");
Fariborz Jahanianb4452ed2007-12-07 00:18:54 +00001155 ObjcProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001156}
1157
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001158void ASTContext::setObjcClassType(TypedefDecl *TD)
1159{
1160 assert(ObjcClassType.isNull() && "'Class' type already set!");
1161
1162 ObjcClassType = getTypedefType(TD);
1163
1164 // typedef struct objc_class *Class;
1165 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1166 assert(ptr && "'Class' incorrectly typed");
1167 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1168 assert(rec && "'Class' incorrectly typed");
1169 ClassStructType = rec;
1170}
1171
Steve Narofff2e30312007-10-15 23:35:17 +00001172void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) {
1173 assert(ObjcConstantStringType.isNull() &&
1174 "'NSConstantString' type already set!");
1175
1176 ObjcConstantStringType = getObjcInterfaceType(Decl);
1177}
1178
Steve Naroff85f0dc52007-10-15 20:41:53 +00001179bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1180 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1181 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1182
1183 return lBuiltin->getKind() == rBuiltin->getKind();
1184}
1185
1186
1187bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
1188 if (lhs->isObjcInterfaceType() && isObjcIdType(rhs))
1189 return true;
1190 else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType())
1191 return true;
1192 return false;
1193}
1194
1195bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
1196 return true; // FIXME: IMPLEMENT.
1197}
1198
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001199bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1200 QualType rhs) {
1201 ObjcQualifiedInterfaceType *lhsQI =
1202 dyn_cast<ObjcQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
1203 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
1204 ObjcQualifiedInterfaceType *rhsQI =
1205 dyn_cast<ObjcQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
1206 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
1207 if (!interfaceTypesAreCompatible(QualType(lhsQI->getInterfaceType(), 0),
1208 QualType(rhsQI->getInterfaceType(), 0)))
1209 return false;
1210 /* All protocols in lhs must have a presense in rhs. */
1211 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1212 bool match = false;
1213 ObjcProtocolDecl *lhsProto = lhsQI->getProtocols(i);
1214 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
1215 ObjcProtocolDecl *rhsProto = rhsQI->getProtocols(j);
1216 if (lhsProto == rhsProto) {
1217 match = true;
1218 break;
1219 }
1220 }
1221 if (!match)
1222 return false;
1223 }
1224 return true;
1225}
1226
Chris Lattner5003e8b2007-11-01 05:03:41 +00001227bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1228 const VectorType *lVector = lhs->getAsVectorType();
1229 const VectorType *rVector = rhs->getAsVectorType();
1230
1231 if ((lVector->getElementType().getCanonicalType() ==
1232 rVector->getElementType().getCanonicalType()) &&
1233 (lVector->getNumElements() == rVector->getNumElements()))
1234 return true;
1235 return false;
1236}
1237
Steve Naroff85f0dc52007-10-15 20:41:53 +00001238// C99 6.2.7p1: If both are complete types, then the following additional
1239// requirements apply...FIXME (handle compatibility across source files).
1240bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
1241 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
1242 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
1243
1244 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
1245 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1246 return true;
1247 }
1248 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
1249 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1250 return true;
1251 }
Steve Naroff4a5e2072007-11-07 06:03:51 +00001252 // "Class" and "id" are compatible built-in structure types.
1253 if (isObjcIdType(lhs) && isObjcClassType(rhs) ||
1254 isObjcClassType(lhs) && isObjcIdType(rhs))
1255 return true;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001256 return false;
1257}
1258
1259bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1260 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1261 // identically qualified and both shall be pointers to compatible types.
1262 if (lhs.getQualifiers() != rhs.getQualifiers())
1263 return false;
1264
1265 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1266 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1267
1268 return typesAreCompatible(ltype, rtype);
1269}
1270
Bill Wendling6a9d8542007-12-03 07:33:35 +00001271// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroff85f0dc52007-10-15 20:41:53 +00001272// reference to T, the operation assigns to the object of type T denoted by the
1273// reference.
1274bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1275 QualType ltype = lhs;
1276
1277 if (lhs->isReferenceType())
1278 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1279
1280 QualType rtype = rhs;
1281
1282 if (rhs->isReferenceType())
1283 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1284
1285 return typesAreCompatible(ltype, rtype);
1286}
1287
1288bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1289 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1290 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1291 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1292 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1293
1294 // first check the return types (common between C99 and K&R).
1295 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1296 return false;
1297
1298 if (lproto && rproto) { // two C99 style function prototypes
1299 unsigned lproto_nargs = lproto->getNumArgs();
1300 unsigned rproto_nargs = rproto->getNumArgs();
1301
1302 if (lproto_nargs != rproto_nargs)
1303 return false;
1304
1305 // both prototypes have the same number of arguments.
1306 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1307 (rproto->isVariadic() && !lproto->isVariadic()))
1308 return false;
1309
1310 // The use of ellipsis agree...now check the argument types.
1311 for (unsigned i = 0; i < lproto_nargs; i++)
1312 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
1313 return false;
1314 return true;
1315 }
1316 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1317 return true;
1318
1319 // we have a mixture of K&R style with C99 prototypes
1320 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1321
1322 if (proto->isVariadic())
1323 return false;
1324
1325 // FIXME: Each parameter type T in the prototype must be compatible with the
1326 // type resulting from applying the usual argument conversions to T.
1327 return true;
1328}
1329
1330bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
1331 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
1332 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
1333
1334 if (!typesAreCompatible(ltype, rtype))
1335 return false;
1336
1337 // FIXME: If both types specify constant sizes, then the sizes must also be
1338 // the same. Even if the sizes are the same, GCC produces an error.
1339 return true;
1340}
1341
1342/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1343/// both shall have the identically qualified version of a compatible type.
1344/// C99 6.2.7p1: Two types have compatible types if their types are the
1345/// same. See 6.7.[2,3,5] for additional rules.
1346bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
1347 QualType lcanon = lhs.getCanonicalType();
1348 QualType rcanon = rhs.getCanonicalType();
1349
1350 // If two types are identical, they are are compatible
1351 if (lcanon == rcanon)
1352 return true;
Bill Wendling6a9d8542007-12-03 07:33:35 +00001353
1354 // C++ [expr]: If an expression initially has the type "reference to T", the
1355 // type is adjusted to "T" prior to any further analysis, the expression
1356 // designates the object or function denoted by the reference, and the
1357 // expression is an lvalue.
1358 if (lcanon->getTypeClass() == Type::Reference)
1359 lcanon = cast<ReferenceType>(lcanon)->getReferenceeType();
1360 if (rcanon->getTypeClass() == Type::Reference)
1361 rcanon = cast<ReferenceType>(rcanon)->getReferenceeType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001362
1363 // If the canonical type classes don't match, they can't be compatible
1364 if (lcanon->getTypeClass() != rcanon->getTypeClass()) {
1365 // For Objective-C, it is possible for two types to be compatible
1366 // when their classes don't match (when dealing with "id"). If either type
1367 // is an interface, we defer to objcTypesAreCompatible().
1368 if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType())
1369 return objcTypesAreCompatible(lcanon, rcanon);
1370 return false;
1371 }
1372 switch (lcanon->getTypeClass()) {
1373 case Type::Pointer:
1374 return pointerTypesAreCompatible(lcanon, rcanon);
Steve Naroff85f0dc52007-10-15 20:41:53 +00001375 case Type::ConstantArray:
1376 case Type::VariableArray:
1377 return arrayTypesAreCompatible(lcanon, rcanon);
1378 case Type::FunctionNoProto:
1379 case Type::FunctionProto:
1380 return functionTypesAreCompatible(lcanon, rcanon);
1381 case Type::Tagged: // handle structures, unions
1382 return tagTypesAreCompatible(lcanon, rcanon);
1383 case Type::Builtin:
1384 return builtinTypesAreCompatible(lcanon, rcanon);
1385 case Type::ObjcInterface:
1386 return interfaceTypesAreCompatible(lcanon, rcanon);
Chris Lattner5003e8b2007-11-01 05:03:41 +00001387 case Type::Vector:
1388 case Type::OCUVector:
1389 return vectorTypesAreCompatible(lcanon, rcanon);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001390 case Type::ObjcQualifiedInterface:
1391 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
Steve Naroff85f0dc52007-10-15 20:41:53 +00001392 default:
1393 assert(0 && "unexpected type");
1394 }
1395 return true; // should never get here...
1396}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001397
Ted Kremenek738e6c02007-10-31 17:10:13 +00001398/// Emit - Serialize an ASTContext object to Bitcode.
1399void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001400 S.EmitRef(SourceMgr);
1401 S.EmitRef(Target);
1402 S.EmitRef(Idents);
1403 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001404
Ted Kremenek68228a92007-10-31 22:44:07 +00001405 // Emit the size of the type vector so that we can reserve that size
1406 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001407 S.EmitInt(Types.size());
1408
Ted Kremenek034a78c2007-11-13 22:02:55 +00001409 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1410 I!=E;++I)
1411 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001412
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001413 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001414}
1415
Ted Kremenekacba3612007-11-13 00:25:37 +00001416ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek68228a92007-10-31 22:44:07 +00001417 SourceManager &SM = D.ReadRef<SourceManager>();
1418 TargetInfo &t = D.ReadRef<TargetInfo>();
1419 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1420 SelectorTable &sels = D.ReadRef<SelectorTable>();
1421
1422 unsigned size_reserve = D.ReadInt();
1423
1424 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1425
Ted Kremenek034a78c2007-11-13 22:02:55 +00001426 for (unsigned i = 0; i < size_reserve; ++i)
1427 Type::Create(*A,i,D);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001428
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001429 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001430
1431 return A;
1432}