blob: 82ff36245e1d6e9be199f80477e43324f083c64d [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
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 Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek7192f8e2007-10-31 17:10:13 +000020#include "llvm/Bitcode/Serialize.h"
21#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000022
Reid Spencer5f016e22007-07-11 17:01:13 +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;
Chris Lattner6d87fc62007-07-18 05:50:59 +000047 unsigned NumVector = 0, NumComplex = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000048 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
49
50 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Chris Lattnerbeb66362007-12-12 06:43:05 +000051 unsigned NumObjcInterfaces = 0, NumObjcQualifiedInterfaces = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +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;
Chris Lattner6d87fc62007-07-18 05:50:59 +000061 else if (isa<ComplexType>(T))
62 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +000063 else if (isa<ArrayType>(T))
64 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +000065 else if (isa<VectorType>(T))
66 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +000067 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 Naroff3f128ad2007-09-17 14:16:13 +000082 } else if (isa<ObjcInterfaceType>(T))
83 ++NumObjcInterfaces;
Chris Lattnerbeb66362007-12-12 06:43:05 +000084 else if (isa<ObjcQualifiedInterfaceType>(T))
85 ++NumObjcQualifiedInterfaces;
Steve Naroff3f128ad2007-09-17 14:16:13 +000086 else {
Chris Lattnerbeb66362007-12-12 06:43:05 +000087 QualType(T, 0).dump();
Reid Spencer5f016e22007-07-11 17:01:13 +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);
Chris Lattner6d87fc62007-07-18 05:50:59 +000095 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +000096 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +000097 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +000098 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 Naroff3f128ad2007-09-17 14:16:13 +0000106 fprintf(stderr, " %d interface types\n", NumObjcInterfaces);
Chris Lattnerbeb66362007-12-12 06:43:05 +0000107 fprintf(stderr, " %d protocol qualified interface types\n",
108 NumObjcQualifiedInterfaces);
Reid Spencer5f016e22007-07-11 17:01:13 +0000109 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
110 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000111 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000112 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
Reid Spencer5f016e22007-07-11 17:01:13 +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.
131 if (Target.isCharSigned(SourceLocation()))
132 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 Naroff7e219e42007-10-15 14:41:52 +0000158
159 BuiltinVaListType = QualType();
160 ObjcIdType = QualType();
161 IdStructType = 0;
Anders Carlsson8baaca52007-10-31 02:53:19 +0000162 ObjcClassType = QualType();
163 ClassStructType = 0;
164
Steve Naroff21988912007-10-15 23:35:17 +0000165 ObjcConstantStringType = QualType();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000166
167 // void * type
168 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000169}
170
Chris Lattner464175b2007-07-18 17:52:12 +0000171//===----------------------------------------------------------------------===//
172// Type Sizing and Analysis
173//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000174
175/// getTypeSize - Return the size of the specified type, in bits. This method
176/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000177std::pair<uint64_t, unsigned>
178ASTContext::getTypeInfo(QualType T, SourceLocation L) {
Chris Lattnera7674d82007-07-13 22:13:22 +0000179 T = T.getCanonicalType();
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000180 uint64_t Size;
181 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000182 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000183 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000184 case Type::FunctionNoProto:
185 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000186 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000187 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-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 Lattner030d8842007-07-19 22:06:24 +0000193 std::pair<uint64_t, unsigned> EltInfo =
Steve Narofffb22d962007-08-30 01:06:46 +0000194 getTypeInfo(CAT->getElementType(), L);
195 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +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 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000207
Chris Lattnera7674d82007-07-13 22:13:22 +0000208 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 Lattner525a0502007-09-22 18:29:59 +0000211 const llvm::fltSemantics *F;
Chris Lattnera7674d82007-07-13 22:13:22 +0000212 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000213 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000214 case BuiltinType::Void:
215 assert(0 && "Incomplete types have no size!");
216 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000217 case BuiltinType::Char_S:
218 case BuiltinType::Char_U:
219 case BuiltinType::UChar:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000220 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000221 case BuiltinType::UShort:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000222 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000223 case BuiltinType::UInt:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000224 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000225 case BuiltinType::ULong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000226 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000227 case BuiltinType::ULongLong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000228 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
Chris Lattner525a0502007-09-22 18:29:59 +0000229 case BuiltinType::Float: Target.getFloatInfo(Size, Align, F, L); break;
230 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, F, L);break;
231 case BuiltinType::LongDouble:Target.getLongDoubleInfo(Size,Align,F,L);break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000232 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000233 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000234 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000235 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000236 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000237 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000238 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
239 // FIXME: This is wrong for struct layout!
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000240 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000241
242 case Type::Complex: {
243 // Complex types have the same alignment as their elements, but twice the
244 // size.
245 std::pair<uint64_t, unsigned> EltInfo =
246 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
247 Size = EltInfo.first*2;
248 Align = EltInfo.second;
249 break;
250 }
251 case Type::Tagged:
Chris Lattner6cd862c2007-08-27 17:38:00 +0000252 TagType *TT = cast<TagType>(T);
253 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
Devang Patel88a981b2007-11-01 19:11:01 +0000254 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000255 Size = Layout.getSize();
256 Align = Layout.getAlignment();
257 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattnere00b18c2007-08-28 18:24:31 +0000258 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000259 } else {
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000260 assert(0 && "Unimplemented type sizes!");
Chris Lattner6cd862c2007-08-27 17:38:00 +0000261 }
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000262 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000263 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000264
Chris Lattner464175b2007-07-18 17:52:12 +0000265 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000266 return std::make_pair(Size, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000267}
268
Devang Patel88a981b2007-11-01 19:11:01 +0000269/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000270/// specified record (struct/union/class), which indicates its size and field
271/// position information.
Devang Patel88a981b2007-11-01 19:11:01 +0000272const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D,
273 SourceLocation L) {
Chris Lattner464175b2007-07-18 17:52:12 +0000274 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
275
276 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000277 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000278 if (Entry) return *Entry;
279
Devang Patel88a981b2007-11-01 19:11:01 +0000280 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
281 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
282 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000283 Entry = NewEntry;
284
285 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
286 uint64_t RecordSize = 0;
287 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
288
289 if (D->getKind() != Decl::Union) {
290 // Layout each field, for now, just sequentially, respecting alignment. In
291 // the future, this will need to be tweakable by targets.
292 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
293 const FieldDecl *FD = D->getMember(i);
294 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
295 uint64_t FieldSize = FieldInfo.first;
296 unsigned FieldAlign = FieldInfo.second;
297
298 // Round up the current record size to the field's alignment boundary.
299 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
300
301 // Place this field at the current location.
302 FieldOffsets[i] = RecordSize;
303
304 // Reserve space for this field.
305 RecordSize += FieldSize;
306
307 // Remember max struct/class alignment.
308 RecordAlign = std::max(RecordAlign, FieldAlign);
309 }
310
311 // Finally, round the size of the total struct up to the alignment of the
312 // struct itself.
313 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
314 } else {
315 // Union layout just puts each member at the start of the record.
316 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
317 const FieldDecl *FD = D->getMember(i);
318 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
319 uint64_t FieldSize = FieldInfo.first;
320 unsigned FieldAlign = FieldInfo.second;
321
322 // Round up the current record size to the field's alignment boundary.
323 RecordSize = std::max(RecordSize, FieldSize);
324
325 // Place this field at the start of the record.
326 FieldOffsets[i] = 0;
327
328 // Remember max struct/class alignment.
329 RecordAlign = std::max(RecordAlign, FieldAlign);
330 }
331 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000332
333 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
334 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000335}
336
Chris Lattnera7674d82007-07-13 22:13:22 +0000337//===----------------------------------------------------------------------===//
338// Type creation/memoization methods
339//===----------------------------------------------------------------------===//
340
341
Reid Spencer5f016e22007-07-11 17:01:13 +0000342/// getComplexType - Return the uniqued reference to the type for a complex
343/// number with the specified element type.
344QualType ASTContext::getComplexType(QualType T) {
345 // Unique pointers, to guarantee there is only one pointer of a particular
346 // structure.
347 llvm::FoldingSetNodeID ID;
348 ComplexType::Profile(ID, T);
349
350 void *InsertPos = 0;
351 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
352 return QualType(CT, 0);
353
354 // If the pointee type isn't canonical, this won't be a canonical type either,
355 // so fill in the canonical type field.
356 QualType Canonical;
357 if (!T->isCanonical()) {
358 Canonical = getComplexType(T.getCanonicalType());
359
360 // Get the new insert position for the node we care about.
361 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
362 assert(NewIP == 0 && "Shouldn't be in the map!");
363 }
364 ComplexType *New = new ComplexType(T, Canonical);
365 Types.push_back(New);
366 ComplexTypes.InsertNode(New, InsertPos);
367 return QualType(New, 0);
368}
369
370
371/// getPointerType - Return the uniqued reference to the type for a pointer to
372/// the specified type.
373QualType ASTContext::getPointerType(QualType T) {
374 // Unique pointers, to guarantee there is only one pointer of a particular
375 // structure.
376 llvm::FoldingSetNodeID ID;
377 PointerType::Profile(ID, T);
378
379 void *InsertPos = 0;
380 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
381 return QualType(PT, 0);
382
383 // If the pointee type isn't canonical, this won't be a canonical type either,
384 // so fill in the canonical type field.
385 QualType Canonical;
386 if (!T->isCanonical()) {
387 Canonical = getPointerType(T.getCanonicalType());
388
389 // Get the new insert position for the node we care about.
390 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
391 assert(NewIP == 0 && "Shouldn't be in the map!");
392 }
393 PointerType *New = new PointerType(T, Canonical);
394 Types.push_back(New);
395 PointerTypes.InsertNode(New, InsertPos);
396 return QualType(New, 0);
397}
398
399/// getReferenceType - Return the uniqued reference to the type for a reference
400/// to the specified type.
401QualType ASTContext::getReferenceType(QualType T) {
402 // Unique pointers, to guarantee there is only one pointer of a particular
403 // structure.
404 llvm::FoldingSetNodeID ID;
405 ReferenceType::Profile(ID, T);
406
407 void *InsertPos = 0;
408 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
409 return QualType(RT, 0);
410
411 // If the referencee type isn't canonical, this won't be a canonical type
412 // either, so fill in the canonical type field.
413 QualType Canonical;
414 if (!T->isCanonical()) {
415 Canonical = getReferenceType(T.getCanonicalType());
416
417 // Get the new insert position for the node we care about.
418 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
419 assert(NewIP == 0 && "Shouldn't be in the map!");
420 }
421
422 ReferenceType *New = new ReferenceType(T, Canonical);
423 Types.push_back(New);
424 ReferenceTypes.InsertNode(New, InsertPos);
425 return QualType(New, 0);
426}
427
Steve Narofffb22d962007-08-30 01:06:46 +0000428/// getConstantArrayType - Return the unique reference to the type for an
429/// array of the specified element type.
430QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000431 const llvm::APInt &ArySize,
432 ArrayType::ArraySizeModifier ASM,
433 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000435 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000436
437 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000438 if (ConstantArrayType *ATP =
439 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000440 return QualType(ATP, 0);
441
442 // If the element type isn't canonical, this won't be a canonical type either,
443 // so fill in the canonical type field.
444 QualType Canonical;
445 if (!EltTy->isCanonical()) {
Steve Naroffc9406122007-08-30 18:10:14 +0000446 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
447 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000448 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000449 ConstantArrayType *NewIP =
450 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
451
Reid Spencer5f016e22007-07-11 17:01:13 +0000452 assert(NewIP == 0 && "Shouldn't be in the map!");
453 }
454
Steve Naroffc9406122007-08-30 18:10:14 +0000455 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
456 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000457 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 Types.push_back(New);
459 return QualType(New, 0);
460}
461
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000462/// getVariableArrayType - Returns a non-unique reference to the type for a
463/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000464QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
465 ArrayType::ArraySizeModifier ASM,
466 unsigned EltTypeQuals) {
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000467 if (NumElts) {
468 // Since we don't unique expressions, it isn't possible to unique VLA's
469 // that have an expression provided for their size.
470
Ted Kremenek347b9f32007-10-30 16:41:53 +0000471 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
472 ASM, EltTypeQuals);
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000473
Ted Kremenek347b9f32007-10-30 16:41:53 +0000474 CompleteVariableArrayTypes.push_back(New);
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000475 Types.push_back(New);
476 return QualType(New, 0);
477 }
478 else {
479 // No size is provided for the VLA. These we can unique.
480 llvm::FoldingSetNodeID ID;
481 VariableArrayType::Profile(ID, EltTy);
482
483 void *InsertPos = 0;
484 if (VariableArrayType *ATP =
485 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
486 return QualType(ATP, 0);
487
488 // If the element type isn't canonical, this won't be a canonical type
489 // either, so fill in the canonical type field.
490 QualType Canonical;
491
492 if (!EltTy->isCanonical()) {
493 Canonical = getVariableArrayType(EltTy.getCanonicalType(), NumElts,
494 ASM, EltTypeQuals);
495
496 // Get the new insert position for the node we care about.
497 VariableArrayType *NewIP =
498 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
499
500 assert(NewIP == 0 && "Shouldn't be in the map!");
501 }
502
503 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
504 ASM, EltTypeQuals);
505
506 IncompleteVariableArrayTypes.InsertNode(New, InsertPos);
507 Types.push_back(New);
508 return QualType(New, 0);
509 }
Steve Narofffb22d962007-08-30 01:06:46 +0000510}
511
Steve Naroff73322922007-07-18 18:00:27 +0000512/// getVectorType - Return the unique reference to a vector type of
513/// the specified element type and size. VectorType must be a built-in type.
514QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000515 BuiltinType *baseType;
516
517 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000518 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000519
520 // Check if we've already instantiated a vector of this type.
521 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000522 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000523 void *InsertPos = 0;
524 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
525 return QualType(VTP, 0);
526
527 // If the element type isn't canonical, this won't be a canonical type either,
528 // so fill in the canonical type field.
529 QualType Canonical;
530 if (!vecType->isCanonical()) {
Steve Naroff73322922007-07-18 18:00:27 +0000531 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000532
533 // Get the new insert position for the node we care about.
534 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
535 assert(NewIP == 0 && "Shouldn't be in the map!");
536 }
537 VectorType *New = new VectorType(vecType, NumElts, Canonical);
538 VectorTypes.InsertNode(New, InsertPos);
539 Types.push_back(New);
540 return QualType(New, 0);
541}
542
Steve Naroff73322922007-07-18 18:00:27 +0000543/// getOCUVectorType - Return the unique reference to an OCU vector type of
544/// the specified element type and size. VectorType must be a built-in type.
545QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
546 BuiltinType *baseType;
547
548 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
549 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
550
551 // Check if we've already instantiated a vector of this type.
552 llvm::FoldingSetNodeID ID;
553 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
554 void *InsertPos = 0;
555 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
556 return QualType(VTP, 0);
557
558 // If the element type isn't canonical, this won't be a canonical type either,
559 // so fill in the canonical type field.
560 QualType Canonical;
561 if (!vecType->isCanonical()) {
562 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
563
564 // Get the new insert position for the node we care about.
565 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
566 assert(NewIP == 0 && "Shouldn't be in the map!");
567 }
568 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
569 VectorTypes.InsertNode(New, InsertPos);
570 Types.push_back(New);
571 return QualType(New, 0);
572}
573
Reid Spencer5f016e22007-07-11 17:01:13 +0000574/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
575///
576QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
577 // Unique functions, to guarantee there is only one function of a particular
578 // structure.
579 llvm::FoldingSetNodeID ID;
580 FunctionTypeNoProto::Profile(ID, ResultTy);
581
582 void *InsertPos = 0;
583 if (FunctionTypeNoProto *FT =
584 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
585 return QualType(FT, 0);
586
587 QualType Canonical;
588 if (!ResultTy->isCanonical()) {
589 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
590
591 // Get the new insert position for the node we care about.
592 FunctionTypeNoProto *NewIP =
593 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
594 assert(NewIP == 0 && "Shouldn't be in the map!");
595 }
596
597 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
598 Types.push_back(New);
599 FunctionTypeProtos.InsertNode(New, InsertPos);
600 return QualType(New, 0);
601}
602
603/// getFunctionType - Return a normal function type with a typed argument
604/// list. isVariadic indicates whether the argument list includes '...'.
605QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
606 unsigned NumArgs, bool isVariadic) {
607 // Unique functions, to guarantee there is only one function of a particular
608 // structure.
609 llvm::FoldingSetNodeID ID;
610 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
611
612 void *InsertPos = 0;
613 if (FunctionTypeProto *FTP =
614 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
615 return QualType(FTP, 0);
616
617 // Determine whether the type being created is already canonical or not.
618 bool isCanonical = ResultTy->isCanonical();
619 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
620 if (!ArgArray[i]->isCanonical())
621 isCanonical = false;
622
623 // If this type isn't canonical, get the canonical version of it.
624 QualType Canonical;
625 if (!isCanonical) {
626 llvm::SmallVector<QualType, 16> CanonicalArgs;
627 CanonicalArgs.reserve(NumArgs);
628 for (unsigned i = 0; i != NumArgs; ++i)
629 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
630
631 Canonical = getFunctionType(ResultTy.getCanonicalType(),
632 &CanonicalArgs[0], NumArgs,
633 isVariadic);
634
635 // Get the new insert position for the node we care about.
636 FunctionTypeProto *NewIP =
637 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
638 assert(NewIP == 0 && "Shouldn't be in the map!");
639 }
640
641 // FunctionTypeProto objects are not allocated with new because they have a
642 // variable size array (for parameter types) at the end of them.
643 FunctionTypeProto *FTP =
644 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000645 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
647 Canonical);
648 Types.push_back(FTP);
649 FunctionTypeProtos.InsertNode(FTP, InsertPos);
650 return QualType(FTP, 0);
651}
652
653/// getTypedefType - Return the unique reference to the type for the
654/// specified typename decl.
655QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
656 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
657
658 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
659 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
660 Types.push_back(Decl->TypeForDecl);
661 return QualType(Decl->TypeForDecl, 0);
662}
663
Steve Naroff3536b442007-09-06 21:24:23 +0000664/// getObjcInterfaceType - Return the unique reference to the type for the
665/// specified ObjC interface decl.
666QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) {
667 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
668
669 Decl->TypeForDecl = new ObjcInterfaceType(Decl);
670 Types.push_back(Decl->TypeForDecl);
671 return QualType(Decl->TypeForDecl, 0);
672}
673
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000674/// getObjcQualifiedInterfaceType - Return a
675/// ObjcQualifiedInterfaceType type for the given interface decl and
676/// the conforming protocol list.
677QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl,
678 ObjcProtocolDecl **Protocols, unsigned NumProtocols) {
679 ObjcInterfaceType *IType =
680 cast<ObjcInterfaceType>(getObjcInterfaceType(Decl));
681
682 llvm::FoldingSetNodeID ID;
683 ObjcQualifiedInterfaceType::Profile(ID, IType, Protocols, NumProtocols);
684
685 void *InsertPos = 0;
686 if (ObjcQualifiedInterfaceType *QT =
687 ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
688 return QualType(QT, 0);
689
690 // No Match;
Chris Lattner00bb2832007-10-11 03:36:41 +0000691 ObjcQualifiedInterfaceType *QType =
692 new ObjcQualifiedInterfaceType(IType, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000693 Types.push_back(QType);
694 ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
695 return QualType(QType, 0);
696}
697
Steve Naroff9752f252007-08-01 18:02:17 +0000698/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
699/// TypeOfExpr AST's (since expression's are never shared). For example,
700/// multiple declarations that refer to "typeof(x)" all contain different
701/// DeclRefExpr's. This doesn't effect the type checker, since it operates
702/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000703QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffd1861fd2007-07-31 12:34:36 +0000704 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000705 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
706 Types.push_back(toe);
707 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000708}
709
Steve Naroff9752f252007-08-01 18:02:17 +0000710/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
711/// TypeOfType AST's. The only motivation to unique these nodes would be
712/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
713/// an issue. This doesn't effect the type checker, since it operates
714/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000715QualType ASTContext::getTypeOfType(QualType tofType) {
716 QualType Canonical = tofType.getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000717 TypeOfType *tot = new TypeOfType(tofType, Canonical);
718 Types.push_back(tot);
719 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000720}
721
Reid Spencer5f016e22007-07-11 17:01:13 +0000722/// getTagDeclType - Return the unique reference to the type for the
723/// specified TagDecl (struct/union/class/enum) decl.
724QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +0000725 assert (Decl);
726
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000727 // The decl stores the type cache.
Ted Kremenekd778f882007-11-26 21:16:01 +0000728 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000729
730 TagType* T = new TagType(Decl, QualType());
Ted Kremenekd778f882007-11-26 21:16:01 +0000731 Types.push_back(T);
732 Decl->TypeForDecl = T;
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000733
734 return QualType(T, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000735}
736
737/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
738/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
739/// needs to agree with the definition in <stddef.h>.
740QualType ASTContext::getSizeType() const {
741 // On Darwin, size_t is defined as a "long unsigned int".
742 // FIXME: should derive from "Target".
743 return UnsignedLongTy;
744}
745
Chris Lattner8b9023b2007-07-13 03:05:23 +0000746/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
747/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
748QualType ASTContext::getPointerDiffType() const {
749 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
750 // FIXME: should derive from "Target".
751 return IntTy;
752}
753
Reid Spencer5f016e22007-07-11 17:01:13 +0000754/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
755/// routine will assert if passed a built-in type that isn't an integer or enum.
756static int getIntegerRank(QualType t) {
757 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
758 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
759 return 4;
760 }
761
762 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
763 switch (BT->getKind()) {
764 default:
765 assert(0 && "getIntegerRank(): not a built-in integer");
766 case BuiltinType::Bool:
767 return 1;
768 case BuiltinType::Char_S:
769 case BuiltinType::Char_U:
770 case BuiltinType::SChar:
771 case BuiltinType::UChar:
772 return 2;
773 case BuiltinType::Short:
774 case BuiltinType::UShort:
775 return 3;
776 case BuiltinType::Int:
777 case BuiltinType::UInt:
778 return 4;
779 case BuiltinType::Long:
780 case BuiltinType::ULong:
781 return 5;
782 case BuiltinType::LongLong:
783 case BuiltinType::ULongLong:
784 return 6;
785 }
786}
787
788/// getFloatingRank - Return a relative rank for floating point types.
789/// This routine will assert if passed a built-in type that isn't a float.
790static int getFloatingRank(QualType T) {
791 T = T.getCanonicalType();
792 if (ComplexType *CT = dyn_cast<ComplexType>(T))
793 return getFloatingRank(CT->getElementType());
794
795 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner770951b2007-11-01 05:03:41 +0000796 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000797 case BuiltinType::Float: return FloatRank;
798 case BuiltinType::Double: return DoubleRank;
799 case BuiltinType::LongDouble: return LongDoubleRank;
800 }
801}
802
Steve Naroff716c7302007-08-27 01:41:48 +0000803/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
804/// point or a complex type (based on typeDomain/typeSize).
805/// 'typeDomain' is a real floating point or complex type.
806/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +0000807QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
808 QualType typeSize, QualType typeDomain) const {
809 if (typeDomain->isComplexType()) {
810 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000811 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000812 case FloatRank: return FloatComplexTy;
813 case DoubleRank: return DoubleComplexTy;
814 case LongDoubleRank: return LongDoubleComplexTy;
815 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000816 }
Steve Narofff1448a02007-08-27 01:27:54 +0000817 if (typeDomain->isRealFloatingType()) {
818 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000819 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000820 case FloatRank: return FloatTy;
821 case DoubleRank: return DoubleTy;
822 case LongDoubleRank: return LongDoubleTy;
823 }
824 }
825 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000826 //an invalid return value, but the assert
827 //will ensure that this code is never reached.
828 return VoidTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000829}
830
Steve Narofffb0d4962007-08-27 15:30:22 +0000831/// compareFloatingType - Handles 3 different combos:
832/// float/float, float/complex, complex/complex.
833/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
834int ASTContext::compareFloatingType(QualType lt, QualType rt) {
835 if (getFloatingRank(lt) == getFloatingRank(rt))
836 return 0;
837 if (getFloatingRank(lt) > getFloatingRank(rt))
838 return 1;
839 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000840}
841
842// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
843// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
844QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
845 if (lhs == rhs) return lhs;
846
847 bool t1Unsigned = lhs->isUnsignedIntegerType();
848 bool t2Unsigned = rhs->isUnsignedIntegerType();
849
850 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
851 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
852
853 // We have two integer types with differing signs
854 QualType unsignedType = t1Unsigned ? lhs : rhs;
855 QualType signedType = t1Unsigned ? rhs : lhs;
856
857 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
858 return unsignedType;
859 else {
860 // FIXME: Need to check if the signed type can represent all values of the
861 // unsigned type. If it can, then the result is the signed type.
862 // If it can't, then the result is the unsigned version of the signed type.
863 // Should probably add a helper that returns a signed integer type from
864 // an unsigned (and vice versa). C99 6.3.1.8.
865 return signedType;
866 }
867}
Anders Carlsson71993dd2007-08-17 05:31:46 +0000868
869// getCFConstantStringType - Return the type used for constant CFStrings.
870QualType ASTContext::getCFConstantStringType() {
871 if (!CFConstantStringTypeDecl) {
872 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroffbeaf2992007-11-03 11:27:19 +0000873 &Idents.get("NSConstantString"),
Anders Carlsson71993dd2007-08-17 05:31:46 +0000874 0);
Anders Carlssonf06273f2007-11-19 00:25:30 +0000875 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +0000876
877 // const int *isa;
878 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +0000879 // int flags;
880 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000881 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +0000882 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +0000883 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +0000884 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000885 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +0000886 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +0000887
Anders Carlssonf06273f2007-11-19 00:25:30 +0000888 for (unsigned i = 0; i < 4; ++i)
Steve Narofff38661e2007-09-14 02:20:46 +0000889 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +0000890
891 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
892 }
893
894 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +0000895}
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000896
Anders Carlssone8c49532007-10-29 06:33:42 +0000897// This returns true if a type has been typedefed to BOOL:
898// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +0000899static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +0000900 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +0000901 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000902
903 return false;
904}
905
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000906/// getObjcEncodingTypeSize returns size of type for objective-c encoding
907/// purpose.
908int ASTContext::getObjcEncodingTypeSize(QualType type) {
909 SourceLocation Loc;
910 uint64_t sz = getTypeSize(type, Loc);
911
912 // Make all integer and enum types at least as large as an int
913 if (sz > 0 && type->isIntegralType())
914 sz = std::max(sz, getTypeSize(IntTy, Loc));
915 // Treat arrays as pointers, since that's how they're passed in.
916 else if (type->isArrayType())
917 sz = getTypeSize(VoidPtrTy, Loc);
918 return sz / getTypeSize(CharTy, Loc);
919}
920
921/// getObjcEncodingForMethodDecl - Return the encoded type for this method
922/// declaration.
923void ASTContext::getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl,
924 std::string& S)
925{
Fariborz Jahanianecb01e62007-11-01 17:18:37 +0000926 // Encode type qualifer, 'in', 'inout', etc. for the return type.
927 getObjcEncodingForTypeQualifier(Decl->getObjcDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000928 // Encode result type.
929 getObjcEncodingForType(Decl->getResultType(), S);
930 // Compute size of all parameters.
931 // Start with computing size of a pointer in number of bytes.
932 // FIXME: There might(should) be a better way of doing this computation!
933 SourceLocation Loc;
934 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
935 // The first two arguments (self and _cmd) are pointers; account for
936 // their size.
937 int ParmOffset = 2 * PtrSize;
938 int NumOfParams = Decl->getNumParams();
939 for (int i = 0; i < NumOfParams; i++) {
940 QualType PType = Decl->getParamDecl(i)->getType();
941 int sz = getObjcEncodingTypeSize (PType);
942 assert (sz > 0 && "getObjcEncodingForMethodDecl - Incomplete param type");
943 ParmOffset += sz;
944 }
945 S += llvm::utostr(ParmOffset);
946 S += "@0:";
947 S += llvm::utostr(PtrSize);
948
949 // Argument types.
950 ParmOffset = 2 * PtrSize;
951 for (int i = 0; i < NumOfParams; i++) {
952 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +0000953 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000954 // 'in', 'inout', etc.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +0000955 getObjcEncodingForTypeQualifier(
956 Decl->getParamDecl(i)->getObjcDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000957 getObjcEncodingForType(PType, S);
958 S += llvm::utostr(ParmOffset);
959 ParmOffset += getObjcEncodingTypeSize(PType);
960 }
961}
962
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000963void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const
964{
Anders Carlssone8c49532007-10-29 06:33:42 +0000965 // FIXME: This currently doesn't encode:
966 // @ An object (whether statically typed or typed id)
967 // # A class object (Class)
968 // : A method selector (SEL)
969 // {name=type...} A structure
970 // (name=type...) A union
971 // bnum A bit field of num bits
972
973 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000974 char encoding;
975 switch (BT->getKind()) {
976 case BuiltinType::Void:
977 encoding = 'v';
978 break;
979 case BuiltinType::Bool:
980 encoding = 'B';
981 break;
982 case BuiltinType::Char_U:
983 case BuiltinType::UChar:
984 encoding = 'C';
985 break;
986 case BuiltinType::UShort:
987 encoding = 'S';
988 break;
989 case BuiltinType::UInt:
990 encoding = 'I';
991 break;
992 case BuiltinType::ULong:
993 encoding = 'L';
994 break;
995 case BuiltinType::ULongLong:
996 encoding = 'Q';
997 break;
998 case BuiltinType::Char_S:
999 case BuiltinType::SChar:
1000 encoding = 'c';
1001 break;
1002 case BuiltinType::Short:
1003 encoding = 's';
1004 break;
1005 case BuiltinType::Int:
1006 encoding = 'i';
1007 break;
1008 case BuiltinType::Long:
1009 encoding = 'l';
1010 break;
1011 case BuiltinType::LongLong:
1012 encoding = 'q';
1013 break;
1014 case BuiltinType::Float:
1015 encoding = 'f';
1016 break;
1017 case BuiltinType::Double:
1018 encoding = 'd';
1019 break;
1020 case BuiltinType::LongDouble:
1021 encoding = 'd';
1022 break;
1023 default:
1024 assert(0 && "Unhandled builtin type kind");
1025 }
1026
1027 S += encoding;
Anders Carlssone8c49532007-10-29 06:33:42 +00001028 } else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001029 QualType PointeeTy = PT->getPointeeType();
Anders Carlsson8baaca52007-10-31 02:53:19 +00001030 if (isObjcIdType(PointeeTy) || PointeeTy->isObjcInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001031 S += '@';
1032 return;
Anders Carlsson8baaca52007-10-31 02:53:19 +00001033 } else if (isObjcClassType(PointeeTy)) {
1034 S += '#';
1035 return;
1036 } else if (isObjcSelType(PointeeTy)) {
1037 S += ':';
1038 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001039 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001040
1041 if (PointeeTy->isCharType()) {
1042 // char pointer types should be encoded as '*' unless it is a
1043 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001044 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001045 S += '*';
1046 return;
1047 }
1048 }
1049
1050 S += '^';
1051 getObjcEncodingForType(PT->getPointeeType(), S);
Anders Carlssone8c49532007-10-29 06:33:42 +00001052 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001053 S += '[';
1054
1055 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1056 S += llvm::utostr(CAT->getSize().getZExtValue());
1057 else
1058 assert(0 && "Unhandled array type!");
1059
1060 getObjcEncodingForType(AT->getElementType(), S);
1061 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001062 } else if (T->getAsFunctionType()) {
1063 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001064 } else if (const RecordType *RTy = T->getAsRecordType()) {
1065 RecordDecl *RDecl= RTy->getDecl();
1066 S += '{';
1067 S += RDecl->getName();
1068 S += '=';
1069 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1070 FieldDecl *field = RDecl->getMember(i);
1071 getObjcEncodingForType(field->getType(), S);
1072 }
1073 S += '}';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001074 } else
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001075 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001076}
1077
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001078void ASTContext::getObjcEncodingForTypeQualifier(Decl::ObjcDeclQualifier QT,
1079 std::string& S) const {
1080 if (QT & Decl::OBJC_TQ_In)
1081 S += 'n';
1082 if (QT & Decl::OBJC_TQ_Inout)
1083 S += 'N';
1084 if (QT & Decl::OBJC_TQ_Out)
1085 S += 'o';
1086 if (QT & Decl::OBJC_TQ_Bycopy)
1087 S += 'O';
1088 if (QT & Decl::OBJC_TQ_Byref)
1089 S += 'R';
1090 if (QT & Decl::OBJC_TQ_Oneway)
1091 S += 'V';
1092}
1093
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001094void ASTContext::setBuiltinVaListType(QualType T)
1095{
1096 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1097
1098 BuiltinVaListType = T;
1099}
1100
Steve Naroff7e219e42007-10-15 14:41:52 +00001101void ASTContext::setObjcIdType(TypedefDecl *TD)
1102{
1103 assert(ObjcIdType.isNull() && "'id' type already set!");
1104
1105 ObjcIdType = getTypedefType(TD);
1106
1107 // typedef struct objc_object *id;
1108 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1109 assert(ptr && "'id' incorrectly typed");
1110 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1111 assert(rec && "'id' incorrectly typed");
1112 IdStructType = rec;
1113}
1114
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001115void ASTContext::setObjcSelType(TypedefDecl *TD)
1116{
1117 assert(ObjcSelType.isNull() && "'SEL' type already set!");
1118
1119 ObjcSelType = getTypedefType(TD);
1120
1121 // typedef struct objc_selector *SEL;
1122 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1123 assert(ptr && "'SEL' incorrectly typed");
1124 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1125 assert(rec && "'SEL' incorrectly typed");
1126 SelStructType = rec;
1127}
1128
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +00001129void ASTContext::setObjcProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001130{
1131 assert(ObjcProtoType.isNull() && "'Protocol' type already set!");
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +00001132 ObjcProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001133}
1134
Anders Carlsson8baaca52007-10-31 02:53:19 +00001135void ASTContext::setObjcClassType(TypedefDecl *TD)
1136{
1137 assert(ObjcClassType.isNull() && "'Class' type already set!");
1138
1139 ObjcClassType = getTypedefType(TD);
1140
1141 // typedef struct objc_class *Class;
1142 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1143 assert(ptr && "'Class' incorrectly typed");
1144 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1145 assert(rec && "'Class' incorrectly typed");
1146 ClassStructType = rec;
1147}
1148
Steve Naroff21988912007-10-15 23:35:17 +00001149void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) {
1150 assert(ObjcConstantStringType.isNull() &&
1151 "'NSConstantString' type already set!");
1152
1153 ObjcConstantStringType = getObjcInterfaceType(Decl);
1154}
1155
Steve Naroffec0550f2007-10-15 20:41:53 +00001156bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1157 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1158 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1159
1160 return lBuiltin->getKind() == rBuiltin->getKind();
1161}
1162
1163
1164bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
1165 if (lhs->isObjcInterfaceType() && isObjcIdType(rhs))
1166 return true;
1167 else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType())
1168 return true;
1169 return false;
1170}
1171
1172bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
1173 return true; // FIXME: IMPLEMENT.
1174}
1175
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001176bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1177 QualType rhs) {
1178 ObjcQualifiedInterfaceType *lhsQI =
1179 dyn_cast<ObjcQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
1180 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
1181 ObjcQualifiedInterfaceType *rhsQI =
1182 dyn_cast<ObjcQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
1183 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
1184 if (!interfaceTypesAreCompatible(QualType(lhsQI->getInterfaceType(), 0),
1185 QualType(rhsQI->getInterfaceType(), 0)))
1186 return false;
1187 /* All protocols in lhs must have a presense in rhs. */
1188 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1189 bool match = false;
1190 ObjcProtocolDecl *lhsProto = lhsQI->getProtocols(i);
1191 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
1192 ObjcProtocolDecl *rhsProto = rhsQI->getProtocols(j);
1193 if (lhsProto == rhsProto) {
1194 match = true;
1195 break;
1196 }
1197 }
1198 if (!match)
1199 return false;
1200 }
1201 return true;
1202}
1203
Chris Lattner770951b2007-11-01 05:03:41 +00001204bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1205 const VectorType *lVector = lhs->getAsVectorType();
1206 const VectorType *rVector = rhs->getAsVectorType();
1207
1208 if ((lVector->getElementType().getCanonicalType() ==
1209 rVector->getElementType().getCanonicalType()) &&
1210 (lVector->getNumElements() == rVector->getNumElements()))
1211 return true;
1212 return false;
1213}
1214
Steve Naroffec0550f2007-10-15 20:41:53 +00001215// C99 6.2.7p1: If both are complete types, then the following additional
1216// requirements apply...FIXME (handle compatibility across source files).
1217bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
1218 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
1219 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
1220
1221 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
1222 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1223 return true;
1224 }
1225 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
1226 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1227 return true;
1228 }
Steve Naroffab373092007-11-07 06:03:51 +00001229 // "Class" and "id" are compatible built-in structure types.
1230 if (isObjcIdType(lhs) && isObjcClassType(rhs) ||
1231 isObjcClassType(lhs) && isObjcIdType(rhs))
1232 return true;
Steve Naroffec0550f2007-10-15 20:41:53 +00001233 return false;
1234}
1235
1236bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1237 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1238 // identically qualified and both shall be pointers to compatible types.
1239 if (lhs.getQualifiers() != rhs.getQualifiers())
1240 return false;
1241
1242 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1243 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1244
1245 return typesAreCompatible(ltype, rtype);
1246}
1247
Bill Wendling43d69752007-12-03 07:33:35 +00001248// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroffec0550f2007-10-15 20:41:53 +00001249// reference to T, the operation assigns to the object of type T denoted by the
1250// reference.
1251bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1252 QualType ltype = lhs;
1253
1254 if (lhs->isReferenceType())
1255 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1256
1257 QualType rtype = rhs;
1258
1259 if (rhs->isReferenceType())
1260 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1261
1262 return typesAreCompatible(ltype, rtype);
1263}
1264
1265bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1266 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1267 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1268 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1269 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1270
1271 // first check the return types (common between C99 and K&R).
1272 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1273 return false;
1274
1275 if (lproto && rproto) { // two C99 style function prototypes
1276 unsigned lproto_nargs = lproto->getNumArgs();
1277 unsigned rproto_nargs = rproto->getNumArgs();
1278
1279 if (lproto_nargs != rproto_nargs)
1280 return false;
1281
1282 // both prototypes have the same number of arguments.
1283 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1284 (rproto->isVariadic() && !lproto->isVariadic()))
1285 return false;
1286
1287 // The use of ellipsis agree...now check the argument types.
1288 for (unsigned i = 0; i < lproto_nargs; i++)
1289 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
1290 return false;
1291 return true;
1292 }
1293 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1294 return true;
1295
1296 // we have a mixture of K&R style with C99 prototypes
1297 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1298
1299 if (proto->isVariadic())
1300 return false;
1301
1302 // FIXME: Each parameter type T in the prototype must be compatible with the
1303 // type resulting from applying the usual argument conversions to T.
1304 return true;
1305}
1306
1307bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
1308 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
1309 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
1310
1311 if (!typesAreCompatible(ltype, rtype))
1312 return false;
1313
1314 // FIXME: If both types specify constant sizes, then the sizes must also be
1315 // the same. Even if the sizes are the same, GCC produces an error.
1316 return true;
1317}
1318
1319/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1320/// both shall have the identically qualified version of a compatible type.
1321/// C99 6.2.7p1: Two types have compatible types if their types are the
1322/// same. See 6.7.[2,3,5] for additional rules.
1323bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
1324 QualType lcanon = lhs.getCanonicalType();
1325 QualType rcanon = rhs.getCanonicalType();
1326
1327 // If two types are identical, they are are compatible
1328 if (lcanon == rcanon)
1329 return true;
Bill Wendling43d69752007-12-03 07:33:35 +00001330
1331 // C++ [expr]: If an expression initially has the type "reference to T", the
1332 // type is adjusted to "T" prior to any further analysis, the expression
1333 // designates the object or function denoted by the reference, and the
1334 // expression is an lvalue.
1335 if (lcanon->getTypeClass() == Type::Reference)
1336 lcanon = cast<ReferenceType>(lcanon)->getReferenceeType();
1337 if (rcanon->getTypeClass() == Type::Reference)
1338 rcanon = cast<ReferenceType>(rcanon)->getReferenceeType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001339
1340 // If the canonical type classes don't match, they can't be compatible
1341 if (lcanon->getTypeClass() != rcanon->getTypeClass()) {
1342 // For Objective-C, it is possible for two types to be compatible
1343 // when their classes don't match (when dealing with "id"). If either type
1344 // is an interface, we defer to objcTypesAreCompatible().
1345 if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType())
1346 return objcTypesAreCompatible(lcanon, rcanon);
1347 return false;
1348 }
1349 switch (lcanon->getTypeClass()) {
1350 case Type::Pointer:
1351 return pointerTypesAreCompatible(lcanon, rcanon);
Steve Naroffec0550f2007-10-15 20:41:53 +00001352 case Type::ConstantArray:
1353 case Type::VariableArray:
1354 return arrayTypesAreCompatible(lcanon, rcanon);
1355 case Type::FunctionNoProto:
1356 case Type::FunctionProto:
1357 return functionTypesAreCompatible(lcanon, rcanon);
1358 case Type::Tagged: // handle structures, unions
1359 return tagTypesAreCompatible(lcanon, rcanon);
1360 case Type::Builtin:
1361 return builtinTypesAreCompatible(lcanon, rcanon);
1362 case Type::ObjcInterface:
1363 return interfaceTypesAreCompatible(lcanon, rcanon);
Chris Lattner770951b2007-11-01 05:03:41 +00001364 case Type::Vector:
1365 case Type::OCUVector:
1366 return vectorTypesAreCompatible(lcanon, rcanon);
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001367 case Type::ObjcQualifiedInterface:
1368 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
Steve Naroffec0550f2007-10-15 20:41:53 +00001369 default:
1370 assert(0 && "unexpected type");
1371 }
1372 return true; // should never get here...
1373}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001374
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001375/// Emit - Serialize an ASTContext object to Bitcode.
1376void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek54513502007-10-31 20:00:03 +00001377 S.EmitRef(SourceMgr);
1378 S.EmitRef(Target);
1379 S.EmitRef(Idents);
1380 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001381
Ted Kremenekfee04522007-10-31 22:44:07 +00001382 // Emit the size of the type vector so that we can reserve that size
1383 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00001384 S.EmitInt(Types.size());
1385
Ted Kremenek03ed4402007-11-13 22:02:55 +00001386 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1387 I!=E;++I)
1388 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001389
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001390 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001391}
1392
Ted Kremenek0f84c002007-11-13 00:25:37 +00001393ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenekfee04522007-10-31 22:44:07 +00001394 SourceManager &SM = D.ReadRef<SourceManager>();
1395 TargetInfo &t = D.ReadRef<TargetInfo>();
1396 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1397 SelectorTable &sels = D.ReadRef<SelectorTable>();
1398
1399 unsigned size_reserve = D.ReadInt();
1400
1401 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1402
Ted Kremenek03ed4402007-11-13 22:02:55 +00001403 for (unsigned i = 0; i < size_reserve; ++i)
1404 Type::Create(*A,i,D);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001405
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001406 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00001407
1408 return A;
1409}