blob: f50929de935ec685d464655b0b5280f59837c3ad [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 += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001074 } else if (T->isEnumeralType()) {
1075 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001076 } else
Steve Naroff5e711242007-12-12 22:30:11 +00001077 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001078}
1079
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001080void ASTContext::getObjcEncodingForTypeQualifier(Decl::ObjcDeclQualifier QT,
1081 std::string& S) const {
1082 if (QT & Decl::OBJC_TQ_In)
1083 S += 'n';
1084 if (QT & Decl::OBJC_TQ_Inout)
1085 S += 'N';
1086 if (QT & Decl::OBJC_TQ_Out)
1087 S += 'o';
1088 if (QT & Decl::OBJC_TQ_Bycopy)
1089 S += 'O';
1090 if (QT & Decl::OBJC_TQ_Byref)
1091 S += 'R';
1092 if (QT & Decl::OBJC_TQ_Oneway)
1093 S += 'V';
1094}
1095
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001096void ASTContext::setBuiltinVaListType(QualType T)
1097{
1098 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1099
1100 BuiltinVaListType = T;
1101}
1102
Steve Naroff7e219e42007-10-15 14:41:52 +00001103void ASTContext::setObjcIdType(TypedefDecl *TD)
1104{
1105 assert(ObjcIdType.isNull() && "'id' type already set!");
1106
1107 ObjcIdType = getTypedefType(TD);
1108
1109 // typedef struct objc_object *id;
1110 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1111 assert(ptr && "'id' incorrectly typed");
1112 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1113 assert(rec && "'id' incorrectly typed");
1114 IdStructType = rec;
1115}
1116
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001117void ASTContext::setObjcSelType(TypedefDecl *TD)
1118{
1119 assert(ObjcSelType.isNull() && "'SEL' type already set!");
1120
1121 ObjcSelType = getTypedefType(TD);
1122
1123 // typedef struct objc_selector *SEL;
1124 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1125 assert(ptr && "'SEL' incorrectly typed");
1126 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1127 assert(rec && "'SEL' incorrectly typed");
1128 SelStructType = rec;
1129}
1130
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +00001131void ASTContext::setObjcProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001132{
1133 assert(ObjcProtoType.isNull() && "'Protocol' type already set!");
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +00001134 ObjcProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001135}
1136
Anders Carlsson8baaca52007-10-31 02:53:19 +00001137void ASTContext::setObjcClassType(TypedefDecl *TD)
1138{
1139 assert(ObjcClassType.isNull() && "'Class' type already set!");
1140
1141 ObjcClassType = getTypedefType(TD);
1142
1143 // typedef struct objc_class *Class;
1144 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1145 assert(ptr && "'Class' incorrectly typed");
1146 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1147 assert(rec && "'Class' incorrectly typed");
1148 ClassStructType = rec;
1149}
1150
Steve Naroff21988912007-10-15 23:35:17 +00001151void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) {
1152 assert(ObjcConstantStringType.isNull() &&
1153 "'NSConstantString' type already set!");
1154
1155 ObjcConstantStringType = getObjcInterfaceType(Decl);
1156}
1157
Steve Naroffec0550f2007-10-15 20:41:53 +00001158bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1159 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1160 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1161
1162 return lBuiltin->getKind() == rBuiltin->getKind();
1163}
1164
1165
1166bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
1167 if (lhs->isObjcInterfaceType() && isObjcIdType(rhs))
1168 return true;
1169 else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType())
1170 return true;
1171 return false;
1172}
1173
1174bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
1175 return true; // FIXME: IMPLEMENT.
1176}
1177
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001178bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1179 QualType rhs) {
1180 ObjcQualifiedInterfaceType *lhsQI =
1181 dyn_cast<ObjcQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
1182 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
1183 ObjcQualifiedInterfaceType *rhsQI =
1184 dyn_cast<ObjcQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
1185 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
1186 if (!interfaceTypesAreCompatible(QualType(lhsQI->getInterfaceType(), 0),
1187 QualType(rhsQI->getInterfaceType(), 0)))
1188 return false;
1189 /* All protocols in lhs must have a presense in rhs. */
1190 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1191 bool match = false;
1192 ObjcProtocolDecl *lhsProto = lhsQI->getProtocols(i);
1193 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
1194 ObjcProtocolDecl *rhsProto = rhsQI->getProtocols(j);
1195 if (lhsProto == rhsProto) {
1196 match = true;
1197 break;
1198 }
1199 }
1200 if (!match)
1201 return false;
1202 }
1203 return true;
1204}
1205
Chris Lattner770951b2007-11-01 05:03:41 +00001206bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1207 const VectorType *lVector = lhs->getAsVectorType();
1208 const VectorType *rVector = rhs->getAsVectorType();
1209
1210 if ((lVector->getElementType().getCanonicalType() ==
1211 rVector->getElementType().getCanonicalType()) &&
1212 (lVector->getNumElements() == rVector->getNumElements()))
1213 return true;
1214 return false;
1215}
1216
Steve Naroffec0550f2007-10-15 20:41:53 +00001217// C99 6.2.7p1: If both are complete types, then the following additional
1218// requirements apply...FIXME (handle compatibility across source files).
1219bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
1220 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
1221 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
1222
1223 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
1224 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1225 return true;
1226 }
1227 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
1228 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1229 return true;
1230 }
Steve Naroffab373092007-11-07 06:03:51 +00001231 // "Class" and "id" are compatible built-in structure types.
1232 if (isObjcIdType(lhs) && isObjcClassType(rhs) ||
1233 isObjcClassType(lhs) && isObjcIdType(rhs))
1234 return true;
Steve Naroffec0550f2007-10-15 20:41:53 +00001235 return false;
1236}
1237
1238bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1239 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1240 // identically qualified and both shall be pointers to compatible types.
1241 if (lhs.getQualifiers() != rhs.getQualifiers())
1242 return false;
1243
1244 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1245 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1246
1247 return typesAreCompatible(ltype, rtype);
1248}
1249
Bill Wendling43d69752007-12-03 07:33:35 +00001250// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroffec0550f2007-10-15 20:41:53 +00001251// reference to T, the operation assigns to the object of type T denoted by the
1252// reference.
1253bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1254 QualType ltype = lhs;
1255
1256 if (lhs->isReferenceType())
1257 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1258
1259 QualType rtype = rhs;
1260
1261 if (rhs->isReferenceType())
1262 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1263
1264 return typesAreCompatible(ltype, rtype);
1265}
1266
1267bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1268 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1269 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1270 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1271 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1272
1273 // first check the return types (common between C99 and K&R).
1274 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1275 return false;
1276
1277 if (lproto && rproto) { // two C99 style function prototypes
1278 unsigned lproto_nargs = lproto->getNumArgs();
1279 unsigned rproto_nargs = rproto->getNumArgs();
1280
1281 if (lproto_nargs != rproto_nargs)
1282 return false;
1283
1284 // both prototypes have the same number of arguments.
1285 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1286 (rproto->isVariadic() && !lproto->isVariadic()))
1287 return false;
1288
1289 // The use of ellipsis agree...now check the argument types.
1290 for (unsigned i = 0; i < lproto_nargs; i++)
1291 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
1292 return false;
1293 return true;
1294 }
1295 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1296 return true;
1297
1298 // we have a mixture of K&R style with C99 prototypes
1299 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1300
1301 if (proto->isVariadic())
1302 return false;
1303
1304 // FIXME: Each parameter type T in the prototype must be compatible with the
1305 // type resulting from applying the usual argument conversions to T.
1306 return true;
1307}
1308
1309bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
1310 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
1311 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
1312
1313 if (!typesAreCompatible(ltype, rtype))
1314 return false;
1315
1316 // FIXME: If both types specify constant sizes, then the sizes must also be
1317 // the same. Even if the sizes are the same, GCC produces an error.
1318 return true;
1319}
1320
1321/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1322/// both shall have the identically qualified version of a compatible type.
1323/// C99 6.2.7p1: Two types have compatible types if their types are the
1324/// same. See 6.7.[2,3,5] for additional rules.
1325bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
1326 QualType lcanon = lhs.getCanonicalType();
1327 QualType rcanon = rhs.getCanonicalType();
1328
1329 // If two types are identical, they are are compatible
1330 if (lcanon == rcanon)
1331 return true;
Bill Wendling43d69752007-12-03 07:33:35 +00001332
1333 // C++ [expr]: If an expression initially has the type "reference to T", the
1334 // type is adjusted to "T" prior to any further analysis, the expression
1335 // designates the object or function denoted by the reference, and the
1336 // expression is an lvalue.
1337 if (lcanon->getTypeClass() == Type::Reference)
1338 lcanon = cast<ReferenceType>(lcanon)->getReferenceeType();
1339 if (rcanon->getTypeClass() == Type::Reference)
1340 rcanon = cast<ReferenceType>(rcanon)->getReferenceeType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001341
1342 // If the canonical type classes don't match, they can't be compatible
1343 if (lcanon->getTypeClass() != rcanon->getTypeClass()) {
1344 // For Objective-C, it is possible for two types to be compatible
1345 // when their classes don't match (when dealing with "id"). If either type
1346 // is an interface, we defer to objcTypesAreCompatible().
1347 if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType())
1348 return objcTypesAreCompatible(lcanon, rcanon);
1349 return false;
1350 }
1351 switch (lcanon->getTypeClass()) {
1352 case Type::Pointer:
1353 return pointerTypesAreCompatible(lcanon, rcanon);
Steve Naroffec0550f2007-10-15 20:41:53 +00001354 case Type::ConstantArray:
1355 case Type::VariableArray:
1356 return arrayTypesAreCompatible(lcanon, rcanon);
1357 case Type::FunctionNoProto:
1358 case Type::FunctionProto:
1359 return functionTypesAreCompatible(lcanon, rcanon);
1360 case Type::Tagged: // handle structures, unions
1361 return tagTypesAreCompatible(lcanon, rcanon);
1362 case Type::Builtin:
1363 return builtinTypesAreCompatible(lcanon, rcanon);
1364 case Type::ObjcInterface:
1365 return interfaceTypesAreCompatible(lcanon, rcanon);
Chris Lattner770951b2007-11-01 05:03:41 +00001366 case Type::Vector:
1367 case Type::OCUVector:
1368 return vectorTypesAreCompatible(lcanon, rcanon);
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001369 case Type::ObjcQualifiedInterface:
1370 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
Steve Naroffec0550f2007-10-15 20:41:53 +00001371 default:
1372 assert(0 && "unexpected type");
1373 }
1374 return true; // should never get here...
1375}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001376
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001377/// Emit - Serialize an ASTContext object to Bitcode.
1378void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek54513502007-10-31 20:00:03 +00001379 S.EmitRef(SourceMgr);
1380 S.EmitRef(Target);
1381 S.EmitRef(Idents);
1382 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001383
Ted Kremenekfee04522007-10-31 22:44:07 +00001384 // Emit the size of the type vector so that we can reserve that size
1385 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00001386 S.EmitInt(Types.size());
1387
Ted Kremenek03ed4402007-11-13 22:02:55 +00001388 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1389 I!=E;++I)
1390 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001391
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001392 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001393}
1394
Ted Kremenek0f84c002007-11-13 00:25:37 +00001395ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenekfee04522007-10-31 22:44:07 +00001396 SourceManager &SM = D.ReadRef<SourceManager>();
1397 TargetInfo &t = D.ReadRef<TargetInfo>();
1398 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1399 SelectorTable &sels = D.ReadRef<SelectorTable>();
1400
1401 unsigned size_reserve = D.ReadInt();
1402
1403 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1404
Ted Kremenek03ed4402007-11-13 22:02:55 +00001405 for (unsigned i = 0; i < size_reserve; ++i)
1406 Type::Create(*A,i,D);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001407
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001408 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00001409
1410 return A;
1411}