blob: 96bf45db7b64af832e9a6695a59df83033231e45 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000020#include "llvm/Bitcode/Serialize.h"
21#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000022
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
25enum FloatingRank {
26 FloatRank, DoubleRank, LongDoubleRank
27};
28
29ASTContext::~ASTContext() {
30 // Deallocate all the types.
31 while (!Types.empty()) {
32 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
33 // Destroy the object, but don't call delete. These are malloc'd.
34 FT->~FunctionTypeProto();
35 free(FT);
36 } else {
37 delete Types.back();
38 }
39 Types.pop_back();
40 }
41}
42
43void ASTContext::PrintStats() const {
44 fprintf(stderr, "*** AST Context Stats:\n");
45 fprintf(stderr, " %d types total.\n", (int)Types.size());
46 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
47 unsigned NumVector = 0, NumComplex = 0;
48 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
49
50 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000051 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
52 unsigned NumObjCQualifiedIds = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000053
54 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
55 Type *T = Types[i];
56 if (isa<BuiltinType>(T))
57 ++NumBuiltin;
58 else if (isa<PointerType>(T))
59 ++NumPointer;
60 else if (isa<ReferenceType>(T))
61 ++NumReference;
62 else if (isa<ComplexType>(T))
63 ++NumComplex;
64 else if (isa<ArrayType>(T))
65 ++NumArray;
66 else if (isa<VectorType>(T))
67 ++NumVector;
68 else if (isa<FunctionTypeNoProto>(T))
69 ++NumFunctionNP;
70 else if (isa<FunctionTypeProto>(T))
71 ++NumFunctionP;
72 else if (isa<TypedefType>(T))
73 ++NumTypeName;
74 else if (TagType *TT = dyn_cast<TagType>(T)) {
75 ++NumTagged;
76 switch (TT->getDecl()->getKind()) {
77 default: assert(0 && "Unknown tagged type!");
78 case Decl::Struct: ++NumTagStruct; break;
79 case Decl::Union: ++NumTagUnion; break;
80 case Decl::Class: ++NumTagClass; break;
81 case Decl::Enum: ++NumTagEnum; break;
82 }
Ted Kremenek42730c52008-01-07 19:49:32 +000083 } else if (isa<ObjCInterfaceType>(T))
84 ++NumObjCInterfaces;
85 else if (isa<ObjCQualifiedInterfaceType>(T))
86 ++NumObjCQualifiedInterfaces;
87 else if (isa<ObjCQualifiedIdType>(T))
88 ++NumObjCQualifiedIds;
Steve Naroff948fd372007-09-17 14:16:13 +000089 else {
Chris Lattner8a35b462007-12-12 06:43:05 +000090 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +000091 assert(0 && "Unknown type!");
92 }
93 }
94
95 fprintf(stderr, " %d builtin types\n", NumBuiltin);
96 fprintf(stderr, " %d pointer types\n", NumPointer);
97 fprintf(stderr, " %d reference types\n", NumReference);
98 fprintf(stderr, " %d complex types\n", NumComplex);
99 fprintf(stderr, " %d array types\n", NumArray);
100 fprintf(stderr, " %d vector types\n", NumVector);
101 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
102 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
103 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
104 fprintf(stderr, " %d tagged types\n", NumTagged);
105 fprintf(stderr, " %d struct types\n", NumTagStruct);
106 fprintf(stderr, " %d union types\n", NumTagUnion);
107 fprintf(stderr, " %d class types\n", NumTagClass);
108 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000109 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000110 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000111 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000112 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000113 NumObjCQualifiedIds);
Chris Lattner4b009652007-07-25 00:24:17 +0000114 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
115 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
116 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
117 NumFunctionP*sizeof(FunctionTypeProto)+
118 NumFunctionNP*sizeof(FunctionTypeNoProto)+
119 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
120}
121
122
123void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
124 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
125}
126
Chris Lattner4b009652007-07-25 00:24:17 +0000127void ASTContext::InitBuiltinTypes() {
128 assert(VoidTy.isNull() && "Context reinitialized?");
129
130 // C99 6.2.5p19.
131 InitBuiltinType(VoidTy, BuiltinType::Void);
132
133 // C99 6.2.5p2.
134 InitBuiltinType(BoolTy, BuiltinType::Bool);
135 // C99 6.2.5p3.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000136 if (Target.isCharSigned(FullSourceLoc()))
Chris Lattner4b009652007-07-25 00:24:17 +0000137 InitBuiltinType(CharTy, BuiltinType::Char_S);
138 else
139 InitBuiltinType(CharTy, BuiltinType::Char_U);
140 // C99 6.2.5p4.
141 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
142 InitBuiltinType(ShortTy, BuiltinType::Short);
143 InitBuiltinType(IntTy, BuiltinType::Int);
144 InitBuiltinType(LongTy, BuiltinType::Long);
145 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
146
147 // C99 6.2.5p6.
148 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
149 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
150 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
151 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
152 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
153
154 // C99 6.2.5p10.
155 InitBuiltinType(FloatTy, BuiltinType::Float);
156 InitBuiltinType(DoubleTy, BuiltinType::Double);
157 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
158
159 // C99 6.2.5p11.
160 FloatComplexTy = getComplexType(FloatTy);
161 DoubleComplexTy = getComplexType(DoubleTy);
162 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000163
164 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000165 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000166 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000167 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000168 ClassStructType = 0;
169
Ted Kremenek42730c52008-01-07 19:49:32 +0000170 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000171
172 // void * type
173 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000174}
175
176//===----------------------------------------------------------------------===//
177// Type Sizing and Analysis
178//===----------------------------------------------------------------------===//
179
180/// getTypeSize - Return the size of the specified type, in bits. This method
181/// does not work on incomplete types.
182std::pair<uint64_t, unsigned>
183ASTContext::getTypeInfo(QualType T, SourceLocation L) {
184 T = T.getCanonicalType();
185 uint64_t Size;
186 unsigned Align;
187 switch (T->getTypeClass()) {
188 case Type::TypeName: assert(0 && "Not a canonical type!");
189 case Type::FunctionNoProto:
190 case Type::FunctionProto:
191 default:
192 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000193 case Type::VariableArray:
194 assert(0 && "VLAs not implemented yet!");
195 case Type::ConstantArray: {
196 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
197
Chris Lattner4b009652007-07-25 00:24:17 +0000198 std::pair<uint64_t, unsigned> EltInfo =
Steve Naroff83c13012007-08-30 01:06:46 +0000199 getTypeInfo(CAT->getElementType(), L);
200 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000201 Align = EltInfo.second;
202 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000203 }
204 case Type::OCUVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000205 case Type::Vector: {
206 std::pair<uint64_t, unsigned> EltInfo =
207 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
208 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
209 // FIXME: Vector alignment is not the alignment of its elements.
210 Align = EltInfo.second;
211 break;
212 }
213
214 case Type::Builtin: {
215 // FIXME: need to use TargetInfo to derive the target specific sizes. This
216 // implementation will suffice for play with vector support.
Chris Lattner858eece2007-09-22 18:29:59 +0000217 const llvm::fltSemantics *F;
Chris Lattner4b009652007-07-25 00:24:17 +0000218 switch (cast<BuiltinType>(T)->getKind()) {
219 default: assert(0 && "Unknown builtin type!");
220 case BuiltinType::Void:
221 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000222 case BuiltinType::Bool:
223 Target.getBoolInfo(Size, Align, getFullLoc(L));
224 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000225 case BuiltinType::Char_S:
226 case BuiltinType::Char_U:
227 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000228 case BuiltinType::SChar:
229 Target.getCharInfo(Size, Align, getFullLoc(L));
230 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000231 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000232 case BuiltinType::Short:
233 Target.getShortInfo(Size, Align, getFullLoc(L));
234 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000235 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000236 case BuiltinType::Int:
237 Target.getIntInfo(Size, Align, getFullLoc(L));
238 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000239 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000240 case BuiltinType::Long:
241 Target.getLongInfo(Size, Align, getFullLoc(L));
242 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000243 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000244 case BuiltinType::LongLong:
245 Target.getLongLongInfo(Size, Align, getFullLoc(L));
246 break;
247 case BuiltinType::Float:
248 Target.getFloatInfo(Size, Align, F, getFullLoc(L));
249 break;
250 case BuiltinType::Double:
251 Target.getDoubleInfo(Size, Align, F, getFullLoc(L));
252 break;
253 case BuiltinType::LongDouble:
254 Target.getLongDoubleInfo(Size, Align, F, getFullLoc(L));
255 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000256 }
257 break;
258 }
Ted Kremenek42730c52008-01-07 19:49:32 +0000259 case Type::ObjCQualifiedId:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000260 Target.getPointerInfo(Size, Align, getFullLoc(L));
261 break;
262 case Type::Pointer:
263 Target.getPointerInfo(Size, Align, getFullLoc(L));
264 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000265 case Type::Reference:
266 // "When applied to a reference or a reference type, the result is the size
267 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000268 // FIXME: This is wrong for struct layout: a reference in a struct has
269 // pointer size.
Chris Lattner4b009652007-07-25 00:24:17 +0000270 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
271
272 case Type::Complex: {
273 // Complex types have the same alignment as their elements, but twice the
274 // size.
275 std::pair<uint64_t, unsigned> EltInfo =
276 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
277 Size = EltInfo.first*2;
278 Align = EltInfo.second;
279 break;
280 }
281 case Type::Tagged:
Chris Lattnereb56d292007-08-27 17:38:00 +0000282 TagType *TT = cast<TagType>(T);
283 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
Devang Patel7a78e432007-11-01 19:11:01 +0000284 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl(), L);
Chris Lattnereb56d292007-08-27 17:38:00 +0000285 Size = Layout.getSize();
286 Align = Layout.getAlignment();
287 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattner90a018d2007-08-28 18:24:31 +0000288 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattnereb56d292007-08-27 17:38:00 +0000289 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000290 assert(0 && "Unimplemented type sizes!");
Chris Lattnereb56d292007-08-27 17:38:00 +0000291 }
Chris Lattner4b009652007-07-25 00:24:17 +0000292 break;
293 }
294
295 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
296 return std::make_pair(Size, Align);
297}
298
Devang Patel7a78e432007-11-01 19:11:01 +0000299/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000300/// specified record (struct/union/class), which indicates its size and field
301/// position information.
Devang Patel7a78e432007-11-01 19:11:01 +0000302const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D,
303 SourceLocation L) {
Chris Lattner4b009652007-07-25 00:24:17 +0000304 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
305
306 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000307 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000308 if (Entry) return *Entry;
309
Devang Patel7a78e432007-11-01 19:11:01 +0000310 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
311 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
312 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000313 Entry = NewEntry;
314
315 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
316 uint64_t RecordSize = 0;
317 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
318
319 if (D->getKind() != Decl::Union) {
320 // Layout each field, for now, just sequentially, respecting alignment. In
321 // the future, this will need to be tweakable by targets.
322 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
323 const FieldDecl *FD = D->getMember(i);
324 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
325 uint64_t FieldSize = FieldInfo.first;
326 unsigned FieldAlign = FieldInfo.second;
327
328 // Round up the current record size to the field's alignment boundary.
329 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
330
331 // Place this field at the current location.
332 FieldOffsets[i] = RecordSize;
333
334 // Reserve space for this field.
335 RecordSize += FieldSize;
336
337 // Remember max struct/class alignment.
338 RecordAlign = std::max(RecordAlign, FieldAlign);
339 }
340
341 // Finally, round the size of the total struct up to the alignment of the
342 // struct itself.
343 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
344 } else {
345 // Union layout just puts each member at the start of the record.
346 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
347 const FieldDecl *FD = D->getMember(i);
348 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
349 uint64_t FieldSize = FieldInfo.first;
350 unsigned FieldAlign = FieldInfo.second;
351
352 // Round up the current record size to the field's alignment boundary.
353 RecordSize = std::max(RecordSize, FieldSize);
354
355 // Place this field at the start of the record.
356 FieldOffsets[i] = 0;
357
358 // Remember max struct/class alignment.
359 RecordAlign = std::max(RecordAlign, FieldAlign);
360 }
361 }
362
363 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
364 return *NewEntry;
365}
366
Chris Lattner4b009652007-07-25 00:24:17 +0000367//===----------------------------------------------------------------------===//
368// Type creation/memoization methods
369//===----------------------------------------------------------------------===//
370
371
372/// getComplexType - Return the uniqued reference to the type for a complex
373/// number with the specified element type.
374QualType ASTContext::getComplexType(QualType T) {
375 // Unique pointers, to guarantee there is only one pointer of a particular
376 // structure.
377 llvm::FoldingSetNodeID ID;
378 ComplexType::Profile(ID, T);
379
380 void *InsertPos = 0;
381 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
382 return QualType(CT, 0);
383
384 // If the pointee type isn't canonical, this won't be a canonical type either,
385 // so fill in the canonical type field.
386 QualType Canonical;
387 if (!T->isCanonical()) {
388 Canonical = getComplexType(T.getCanonicalType());
389
390 // Get the new insert position for the node we care about.
391 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
392 assert(NewIP == 0 && "Shouldn't be in the map!");
393 }
394 ComplexType *New = new ComplexType(T, Canonical);
395 Types.push_back(New);
396 ComplexTypes.InsertNode(New, InsertPos);
397 return QualType(New, 0);
398}
399
400
401/// getPointerType - Return the uniqued reference to the type for a pointer to
402/// the specified type.
403QualType ASTContext::getPointerType(QualType T) {
404 // Unique pointers, to guarantee there is only one pointer of a particular
405 // structure.
406 llvm::FoldingSetNodeID ID;
407 PointerType::Profile(ID, T);
408
409 void *InsertPos = 0;
410 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
411 return QualType(PT, 0);
412
413 // If the pointee type isn't canonical, this won't be a canonical type either,
414 // so fill in the canonical type field.
415 QualType Canonical;
416 if (!T->isCanonical()) {
417 Canonical = getPointerType(T.getCanonicalType());
418
419 // Get the new insert position for the node we care about.
420 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
421 assert(NewIP == 0 && "Shouldn't be in the map!");
422 }
423 PointerType *New = new PointerType(T, Canonical);
424 Types.push_back(New);
425 PointerTypes.InsertNode(New, InsertPos);
426 return QualType(New, 0);
427}
428
429/// getReferenceType - Return the uniqued reference to the type for a reference
430/// to the specified type.
431QualType ASTContext::getReferenceType(QualType T) {
432 // Unique pointers, to guarantee there is only one pointer of a particular
433 // structure.
434 llvm::FoldingSetNodeID ID;
435 ReferenceType::Profile(ID, T);
436
437 void *InsertPos = 0;
438 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
439 return QualType(RT, 0);
440
441 // If the referencee type isn't canonical, this won't be a canonical type
442 // either, so fill in the canonical type field.
443 QualType Canonical;
444 if (!T->isCanonical()) {
445 Canonical = getReferenceType(T.getCanonicalType());
446
447 // Get the new insert position for the node we care about.
448 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
449 assert(NewIP == 0 && "Shouldn't be in the map!");
450 }
451
452 ReferenceType *New = new ReferenceType(T, Canonical);
453 Types.push_back(New);
454 ReferenceTypes.InsertNode(New, InsertPos);
455 return QualType(New, 0);
456}
457
Steve Naroff83c13012007-08-30 01:06:46 +0000458/// getConstantArrayType - Return the unique reference to the type for an
459/// array of the specified element type.
460QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000461 const llvm::APInt &ArySize,
462 ArrayType::ArraySizeModifier ASM,
463 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000464 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000465 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000466
467 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000468 if (ConstantArrayType *ATP =
469 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000470 return QualType(ATP, 0);
471
472 // If the element type isn't canonical, this won't be a canonical type either,
473 // so fill in the canonical type field.
474 QualType Canonical;
475 if (!EltTy->isCanonical()) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000476 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
477 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000478 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000479 ConstantArrayType *NewIP =
480 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
481
Chris Lattner4b009652007-07-25 00:24:17 +0000482 assert(NewIP == 0 && "Shouldn't be in the map!");
483 }
484
Steve Naroff24c9b982007-08-30 18:10:14 +0000485 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
486 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000487 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000488 Types.push_back(New);
489 return QualType(New, 0);
490}
491
Steve Naroffe2579e32007-08-30 18:14:25 +0000492/// getVariableArrayType - Returns a non-unique reference to the type for a
493/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000494QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
495 ArrayType::ArraySizeModifier ASM,
496 unsigned EltTypeQuals) {
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000497 if (NumElts) {
498 // Since we don't unique expressions, it isn't possible to unique VLA's
499 // that have an expression provided for their size.
500
Ted Kremenek2058dc42007-10-30 16:41:53 +0000501 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
502 ASM, EltTypeQuals);
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000503
Ted Kremenek2058dc42007-10-30 16:41:53 +0000504 CompleteVariableArrayTypes.push_back(New);
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000505 Types.push_back(New);
506 return QualType(New, 0);
507 }
508 else {
509 // No size is provided for the VLA. These we can unique.
510 llvm::FoldingSetNodeID ID;
511 VariableArrayType::Profile(ID, EltTy);
512
513 void *InsertPos = 0;
514 if (VariableArrayType *ATP =
515 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
516 return QualType(ATP, 0);
517
518 // If the element type isn't canonical, this won't be a canonical type
519 // either, so fill in the canonical type field.
520 QualType Canonical;
521
522 if (!EltTy->isCanonical()) {
523 Canonical = getVariableArrayType(EltTy.getCanonicalType(), NumElts,
524 ASM, EltTypeQuals);
525
526 // Get the new insert position for the node we care about.
527 VariableArrayType *NewIP =
528 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
529
530 assert(NewIP == 0 && "Shouldn't be in the map!");
531 }
532
533 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
534 ASM, EltTypeQuals);
535
536 IncompleteVariableArrayTypes.InsertNode(New, InsertPos);
537 Types.push_back(New);
538 return QualType(New, 0);
539 }
Steve Naroff83c13012007-08-30 01:06:46 +0000540}
541
Chris Lattner4b009652007-07-25 00:24:17 +0000542/// getVectorType - Return the unique reference to a vector type of
543/// the specified element type and size. VectorType must be a built-in type.
544QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
545 BuiltinType *baseType;
546
547 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
548 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
549
550 // Check if we've already instantiated a vector of this type.
551 llvm::FoldingSetNodeID ID;
552 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
553 void *InsertPos = 0;
554 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
555 return QualType(VTP, 0);
556
557 // If the element type isn't canonical, this won't be a canonical type either,
558 // so fill in the canonical type field.
559 QualType Canonical;
560 if (!vecType->isCanonical()) {
561 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
562
563 // Get the new insert position for the node we care about.
564 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
565 assert(NewIP == 0 && "Shouldn't be in the map!");
566 }
567 VectorType *New = new VectorType(vecType, NumElts, Canonical);
568 VectorTypes.InsertNode(New, InsertPos);
569 Types.push_back(New);
570 return QualType(New, 0);
571}
572
573/// getOCUVectorType - Return the unique reference to an OCU vector type of
574/// the specified element type and size. VectorType must be a built-in type.
575QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
576 BuiltinType *baseType;
577
578 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
579 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
580
581 // Check if we've already instantiated a vector of this type.
582 llvm::FoldingSetNodeID ID;
583 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
584 void *InsertPos = 0;
585 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
586 return QualType(VTP, 0);
587
588 // If the element type isn't canonical, this won't be a canonical type either,
589 // so fill in the canonical type field.
590 QualType Canonical;
591 if (!vecType->isCanonical()) {
592 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
593
594 // Get the new insert position for the node we care about.
595 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
596 assert(NewIP == 0 && "Shouldn't be in the map!");
597 }
598 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
599 VectorTypes.InsertNode(New, InsertPos);
600 Types.push_back(New);
601 return QualType(New, 0);
602}
603
604/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
605///
606QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
607 // Unique functions, to guarantee there is only one function of a particular
608 // structure.
609 llvm::FoldingSetNodeID ID;
610 FunctionTypeNoProto::Profile(ID, ResultTy);
611
612 void *InsertPos = 0;
613 if (FunctionTypeNoProto *FT =
614 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
615 return QualType(FT, 0);
616
617 QualType Canonical;
618 if (!ResultTy->isCanonical()) {
619 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
620
621 // Get the new insert position for the node we care about.
622 FunctionTypeNoProto *NewIP =
623 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
624 assert(NewIP == 0 && "Shouldn't be in the map!");
625 }
626
627 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
628 Types.push_back(New);
629 FunctionTypeProtos.InsertNode(New, InsertPos);
630 return QualType(New, 0);
631}
632
633/// getFunctionType - Return a normal function type with a typed argument
634/// list. isVariadic indicates whether the argument list includes '...'.
635QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
636 unsigned NumArgs, bool isVariadic) {
637 // Unique functions, to guarantee there is only one function of a particular
638 // structure.
639 llvm::FoldingSetNodeID ID;
640 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
641
642 void *InsertPos = 0;
643 if (FunctionTypeProto *FTP =
644 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
645 return QualType(FTP, 0);
646
647 // Determine whether the type being created is already canonical or not.
648 bool isCanonical = ResultTy->isCanonical();
649 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
650 if (!ArgArray[i]->isCanonical())
651 isCanonical = false;
652
653 // If this type isn't canonical, get the canonical version of it.
654 QualType Canonical;
655 if (!isCanonical) {
656 llvm::SmallVector<QualType, 16> CanonicalArgs;
657 CanonicalArgs.reserve(NumArgs);
658 for (unsigned i = 0; i != NumArgs; ++i)
659 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
660
661 Canonical = getFunctionType(ResultTy.getCanonicalType(),
662 &CanonicalArgs[0], NumArgs,
663 isVariadic);
664
665 // Get the new insert position for the node we care about.
666 FunctionTypeProto *NewIP =
667 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
668 assert(NewIP == 0 && "Shouldn't be in the map!");
669 }
670
671 // FunctionTypeProto objects are not allocated with new because they have a
672 // variable size array (for parameter types) at the end of them.
673 FunctionTypeProto *FTP =
674 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
675 NumArgs*sizeof(QualType));
676 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
677 Canonical);
678 Types.push_back(FTP);
679 FunctionTypeProtos.InsertNode(FTP, InsertPos);
680 return QualType(FTP, 0);
681}
682
683/// getTypedefType - Return the unique reference to the type for the
684/// specified typename decl.
685QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
686 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
687
688 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000689 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000690 Types.push_back(Decl->TypeForDecl);
691 return QualType(Decl->TypeForDecl, 0);
692}
693
Ted Kremenek42730c52008-01-07 19:49:32 +0000694/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000695/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000696QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000697 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
698
Ted Kremenek42730c52008-01-07 19:49:32 +0000699 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000700 Types.push_back(Decl->TypeForDecl);
701 return QualType(Decl->TypeForDecl, 0);
702}
703
Ted Kremenek42730c52008-01-07 19:49:32 +0000704/// getObjCQualifiedInterfaceType - Return a
705/// ObjCQualifiedInterfaceType type for the given interface decl and
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000706/// the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000707QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
708 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000709 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000710 ObjCQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000711
712 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000713 if (ObjCQualifiedInterfaceType *QT =
714 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000715 return QualType(QT, 0);
716
717 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000718 ObjCQualifiedInterfaceType *QType =
719 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000720 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000721 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000722 return QualType(QType, 0);
723}
724
Ted Kremenek42730c52008-01-07 19:49:32 +0000725/// getObjCQualifiedIdType - Return a
726/// getObjCQualifiedIdType type for the 'id' decl and
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000727/// the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000728QualType ASTContext::getObjCQualifiedIdType(QualType idType,
729 ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000730 unsigned NumProtocols) {
731 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000732 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000733
734 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000735 if (ObjCQualifiedIdType *QT =
736 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000737 return QualType(QT, 0);
738
739 // No Match;
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000740 QualType Canonical;
741 if (!idType->isCanonical()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000742 Canonical = getObjCQualifiedIdType(idType.getCanonicalType(),
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000743 Protocols, NumProtocols);
Ted Kremenek42730c52008-01-07 19:49:32 +0000744 ObjCQualifiedIdType *NewQT =
745 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000746 assert(NewQT == 0 && "Shouldn't be in the map!");
747 }
748
Ted Kremenek42730c52008-01-07 19:49:32 +0000749 ObjCQualifiedIdType *QType =
750 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000751 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000752 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000753 return QualType(QType, 0);
754}
755
Steve Naroff0604dd92007-08-01 18:02:17 +0000756/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
757/// TypeOfExpr AST's (since expression's are never shared). For example,
758/// multiple declarations that refer to "typeof(x)" all contain different
759/// DeclRefExpr's. This doesn't effect the type checker, since it operates
760/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000761QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroff7cbb1462007-07-31 12:34:36 +0000762 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000763 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
764 Types.push_back(toe);
765 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000766}
767
Steve Naroff0604dd92007-08-01 18:02:17 +0000768/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
769/// TypeOfType AST's. The only motivation to unique these nodes would be
770/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
771/// an issue. This doesn't effect the type checker, since it operates
772/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000773QualType ASTContext::getTypeOfType(QualType tofType) {
774 QualType Canonical = tofType.getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000775 TypeOfType *tot = new TypeOfType(tofType, Canonical);
776 Types.push_back(tot);
777 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000778}
779
Chris Lattner4b009652007-07-25 00:24:17 +0000780/// getTagDeclType - Return the unique reference to the type for the
781/// specified TagDecl (struct/union/class/enum) decl.
782QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000783 assert (Decl);
784
Ted Kremenekf05026d2007-11-14 00:03:20 +0000785 // The decl stores the type cache.
Ted Kremenekae8fa032007-11-26 21:16:01 +0000786 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekf05026d2007-11-14 00:03:20 +0000787
788 TagType* T = new TagType(Decl, QualType());
Ted Kremenekae8fa032007-11-26 21:16:01 +0000789 Types.push_back(T);
790 Decl->TypeForDecl = T;
Ted Kremenekf05026d2007-11-14 00:03:20 +0000791
792 return QualType(T, 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000793}
794
795/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
796/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
797/// needs to agree with the definition in <stddef.h>.
798QualType ASTContext::getSizeType() const {
799 // On Darwin, size_t is defined as a "long unsigned int".
800 // FIXME: should derive from "Target".
801 return UnsignedLongTy;
802}
803
804/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
805/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
806QualType ASTContext::getPointerDiffType() const {
807 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
808 // FIXME: should derive from "Target".
809 return IntTy;
810}
811
812/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
813/// routine will assert if passed a built-in type that isn't an integer or enum.
814static int getIntegerRank(QualType t) {
815 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
816 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
817 return 4;
818 }
819
820 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
821 switch (BT->getKind()) {
822 default:
823 assert(0 && "getIntegerRank(): not a built-in integer");
824 case BuiltinType::Bool:
825 return 1;
826 case BuiltinType::Char_S:
827 case BuiltinType::Char_U:
828 case BuiltinType::SChar:
829 case BuiltinType::UChar:
830 return 2;
831 case BuiltinType::Short:
832 case BuiltinType::UShort:
833 return 3;
834 case BuiltinType::Int:
835 case BuiltinType::UInt:
836 return 4;
837 case BuiltinType::Long:
838 case BuiltinType::ULong:
839 return 5;
840 case BuiltinType::LongLong:
841 case BuiltinType::ULongLong:
842 return 6;
843 }
844}
845
846/// getFloatingRank - Return a relative rank for floating point types.
847/// This routine will assert if passed a built-in type that isn't a float.
848static int getFloatingRank(QualType T) {
849 T = T.getCanonicalType();
850 if (ComplexType *CT = dyn_cast<ComplexType>(T))
851 return getFloatingRank(CT->getElementType());
852
853 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner5003e8b2007-11-01 05:03:41 +0000854 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +0000855 case BuiltinType::Float: return FloatRank;
856 case BuiltinType::Double: return DoubleRank;
857 case BuiltinType::LongDouble: return LongDoubleRank;
858 }
859}
860
Steve Narofffa0c4532007-08-27 01:41:48 +0000861/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
862/// point or a complex type (based on typeDomain/typeSize).
863/// 'typeDomain' is a real floating point or complex type.
864/// 'typeSize' is a real floating point or complex type.
Steve Naroff3cf497f2007-08-27 01:27:54 +0000865QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
866 QualType typeSize, QualType typeDomain) const {
867 if (typeDomain->isComplexType()) {
868 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000869 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000870 case FloatRank: return FloatComplexTy;
871 case DoubleRank: return DoubleComplexTy;
872 case LongDoubleRank: return LongDoubleComplexTy;
873 }
Chris Lattner4b009652007-07-25 00:24:17 +0000874 }
Steve Naroff3cf497f2007-08-27 01:27:54 +0000875 if (typeDomain->isRealFloatingType()) {
876 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000877 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000878 case FloatRank: return FloatTy;
879 case DoubleRank: return DoubleTy;
880 case LongDoubleRank: return LongDoubleTy;
881 }
882 }
883 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000884 //an invalid return value, but the assert
885 //will ensure that this code is never reached.
886 return VoidTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000887}
888
Steve Naroff45fc9822007-08-27 15:30:22 +0000889/// compareFloatingType - Handles 3 different combos:
890/// float/float, float/complex, complex/complex.
891/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
892int ASTContext::compareFloatingType(QualType lt, QualType rt) {
893 if (getFloatingRank(lt) == getFloatingRank(rt))
894 return 0;
895 if (getFloatingRank(lt) > getFloatingRank(rt))
896 return 1;
897 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +0000898}
899
900// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
901// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
902QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
903 if (lhs == rhs) return lhs;
904
905 bool t1Unsigned = lhs->isUnsignedIntegerType();
906 bool t2Unsigned = rhs->isUnsignedIntegerType();
907
908 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
909 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
910
911 // We have two integer types with differing signs
912 QualType unsignedType = t1Unsigned ? lhs : rhs;
913 QualType signedType = t1Unsigned ? rhs : lhs;
914
915 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
916 return unsignedType;
917 else {
918 // FIXME: Need to check if the signed type can represent all values of the
919 // unsigned type. If it can, then the result is the signed type.
920 // If it can't, then the result is the unsigned version of the signed type.
921 // Should probably add a helper that returns a signed integer type from
922 // an unsigned (and vice versa). C99 6.3.1.8.
923 return signedType;
924 }
925}
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000926
927// getCFConstantStringType - Return the type used for constant CFStrings.
928QualType ASTContext::getCFConstantStringType() {
929 if (!CFConstantStringTypeDecl) {
930 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroff0add5d22007-11-03 11:27:19 +0000931 &Idents.get("NSConstantString"),
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000932 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000933 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000934
935 // const int *isa;
936 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000937 // int flags;
938 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000939 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000940 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000941 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000942 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000943 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000944 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000945
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000946 for (unsigned i = 0; i < 4; ++i)
Steve Naroffdc1ad762007-09-14 02:20:46 +0000947 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000948
949 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
950 }
951
952 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +0000953}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +0000954
Anders Carlssone3f02572007-10-29 06:33:42 +0000955// This returns true if a type has been typedefed to BOOL:
956// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +0000957static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +0000958 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +0000959 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +0000960
961 return false;
962}
963
Ted Kremenek42730c52008-01-07 19:49:32 +0000964/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000965/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +0000966int ASTContext::getObjCEncodingTypeSize(QualType type) {
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000967 SourceLocation Loc;
968 uint64_t sz = getTypeSize(type, Loc);
969
970 // Make all integer and enum types at least as large as an int
971 if (sz > 0 && type->isIntegralType())
972 sz = std::max(sz, getTypeSize(IntTy, Loc));
973 // Treat arrays as pointers, since that's how they're passed in.
974 else if (type->isArrayType())
975 sz = getTypeSize(VoidPtrTy, Loc);
976 return sz / getTypeSize(CharTy, Loc);
977}
978
Ted Kremenek42730c52008-01-07 19:49:32 +0000979/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000980/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +0000981void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000982 std::string& S)
983{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +0000984 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +0000985 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000986 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +0000987 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000988 // Compute size of all parameters.
989 // Start with computing size of a pointer in number of bytes.
990 // FIXME: There might(should) be a better way of doing this computation!
991 SourceLocation Loc;
992 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
993 // The first two arguments (self and _cmd) are pointers; account for
994 // their size.
995 int ParmOffset = 2 * PtrSize;
996 int NumOfParams = Decl->getNumParams();
997 for (int i = 0; i < NumOfParams; i++) {
998 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000999 int sz = getObjCEncodingTypeSize (PType);
1000 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001001 ParmOffset += sz;
1002 }
1003 S += llvm::utostr(ParmOffset);
1004 S += "@0:";
1005 S += llvm::utostr(PtrSize);
1006
1007 // Argument types.
1008 ParmOffset = 2 * PtrSize;
1009 for (int i = 0; i < NumOfParams; i++) {
1010 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001011 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001012 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001013 getObjCEncodingForTypeQualifier(
1014 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001015 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001016 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001017 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001018 }
1019}
1020
Fariborz Jahanian248db262008-01-22 22:44:46 +00001021void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1022 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson36f07d82007-10-29 05:01:08 +00001023{
Anders Carlssone3f02572007-10-29 06:33:42 +00001024 // FIXME: This currently doesn't encode:
1025 // @ An object (whether statically typed or typed id)
1026 // # A class object (Class)
1027 // : A method selector (SEL)
1028 // {name=type...} A structure
1029 // (name=type...) A union
1030 // bnum A bit field of num bits
1031
1032 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001033 char encoding;
1034 switch (BT->getKind()) {
1035 case BuiltinType::Void:
1036 encoding = 'v';
1037 break;
1038 case BuiltinType::Bool:
1039 encoding = 'B';
1040 break;
1041 case BuiltinType::Char_U:
1042 case BuiltinType::UChar:
1043 encoding = 'C';
1044 break;
1045 case BuiltinType::UShort:
1046 encoding = 'S';
1047 break;
1048 case BuiltinType::UInt:
1049 encoding = 'I';
1050 break;
1051 case BuiltinType::ULong:
1052 encoding = 'L';
1053 break;
1054 case BuiltinType::ULongLong:
1055 encoding = 'Q';
1056 break;
1057 case BuiltinType::Char_S:
1058 case BuiltinType::SChar:
1059 encoding = 'c';
1060 break;
1061 case BuiltinType::Short:
1062 encoding = 's';
1063 break;
1064 case BuiltinType::Int:
1065 encoding = 'i';
1066 break;
1067 case BuiltinType::Long:
1068 encoding = 'l';
1069 break;
1070 case BuiltinType::LongLong:
1071 encoding = 'q';
1072 break;
1073 case BuiltinType::Float:
1074 encoding = 'f';
1075 break;
1076 case BuiltinType::Double:
1077 encoding = 'd';
1078 break;
1079 case BuiltinType::LongDouble:
1080 encoding = 'd';
1081 break;
1082 default:
1083 assert(0 && "Unhandled builtin type kind");
1084 }
1085
1086 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001087 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001088 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001089 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001090 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001091
1092 }
1093 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001094 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001095 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001096 S += '@';
1097 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001098 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001099 S += '#';
1100 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001101 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001102 S += ':';
1103 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001104 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001105
1106 if (PointeeTy->isCharType()) {
1107 // char pointer types should be encoded as '*' unless it is a
1108 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001109 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001110 S += '*';
1111 return;
1112 }
1113 }
1114
1115 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001116 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone3f02572007-10-29 06:33:42 +00001117 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001118 S += '[';
1119
1120 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1121 S += llvm::utostr(CAT->getSize().getZExtValue());
1122 else
1123 assert(0 && "Unhandled array type!");
1124
Fariborz Jahanian248db262008-01-22 22:44:46 +00001125 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001126 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001127 } else if (T->getAsFunctionType()) {
1128 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001129 } else if (const RecordType *RTy = T->getAsRecordType()) {
1130 RecordDecl *RDecl= RTy->getDecl();
1131 S += '{';
1132 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001133 bool found = false;
1134 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1135 if (ERType[i] == RTy) {
1136 found = true;
1137 break;
1138 }
1139 if (!found) {
1140 ERType.push_back(RTy);
1141 S += '=';
1142 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1143 FieldDecl *field = RDecl->getMember(i);
1144 getObjCEncodingForType(field->getType(), S, ERType);
1145 }
1146 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1147 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001148 }
1149 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001150 } else if (T->isEnumeralType()) {
1151 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001152 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001153 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001154}
1155
Ted Kremenek42730c52008-01-07 19:49:32 +00001156void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001157 std::string& S) const {
1158 if (QT & Decl::OBJC_TQ_In)
1159 S += 'n';
1160 if (QT & Decl::OBJC_TQ_Inout)
1161 S += 'N';
1162 if (QT & Decl::OBJC_TQ_Out)
1163 S += 'o';
1164 if (QT & Decl::OBJC_TQ_Bycopy)
1165 S += 'O';
1166 if (QT & Decl::OBJC_TQ_Byref)
1167 S += 'R';
1168 if (QT & Decl::OBJC_TQ_Oneway)
1169 S += 'V';
1170}
1171
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001172void ASTContext::setBuiltinVaListType(QualType T)
1173{
1174 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1175
1176 BuiltinVaListType = T;
1177}
1178
Ted Kremenek42730c52008-01-07 19:49:32 +00001179void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001180{
Ted Kremenek42730c52008-01-07 19:49:32 +00001181 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001182
Ted Kremenek42730c52008-01-07 19:49:32 +00001183 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001184
1185 // typedef struct objc_object *id;
1186 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1187 assert(ptr && "'id' incorrectly typed");
1188 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1189 assert(rec && "'id' incorrectly typed");
1190 IdStructType = rec;
1191}
1192
Ted Kremenek42730c52008-01-07 19:49:32 +00001193void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001194{
Ted Kremenek42730c52008-01-07 19:49:32 +00001195 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001196
Ted Kremenek42730c52008-01-07 19:49:32 +00001197 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001198
1199 // typedef struct objc_selector *SEL;
1200 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1201 assert(ptr && "'SEL' incorrectly typed");
1202 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1203 assert(rec && "'SEL' incorrectly typed");
1204 SelStructType = rec;
1205}
1206
Ted Kremenek42730c52008-01-07 19:49:32 +00001207void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001208{
Ted Kremenek42730c52008-01-07 19:49:32 +00001209 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1210 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001211}
1212
Ted Kremenek42730c52008-01-07 19:49:32 +00001213void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001214{
Ted Kremenek42730c52008-01-07 19:49:32 +00001215 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001216
Ted Kremenek42730c52008-01-07 19:49:32 +00001217 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001218
1219 // typedef struct objc_class *Class;
1220 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1221 assert(ptr && "'Class' incorrectly typed");
1222 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1223 assert(rec && "'Class' incorrectly typed");
1224 ClassStructType = rec;
1225}
1226
Ted Kremenek42730c52008-01-07 19:49:32 +00001227void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1228 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001229 "'NSConstantString' type already set!");
1230
Ted Kremenek42730c52008-01-07 19:49:32 +00001231 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001232}
1233
Steve Naroff85f0dc52007-10-15 20:41:53 +00001234bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1235 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1236 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1237
1238 return lBuiltin->getKind() == rBuiltin->getKind();
1239}
1240
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001241/// objcTypesAreCompatible - This routine is called when two types
1242/// are of different class; one is interface type or is
1243/// a qualified interface type and the other type is of a different class.
1244/// Example, II or II<P>.
Steve Naroff85f0dc52007-10-15 20:41:53 +00001245bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001246 if (lhs->isObjCInterfaceType() && isObjCIdType(rhs))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001247 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001248 else if (isObjCIdType(lhs) && rhs->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001249 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001250 if (ObjCInterfaceType *lhsIT =
1251 dyn_cast<ObjCInterfaceType>(lhs.getCanonicalType().getTypePtr())) {
1252 ObjCQualifiedInterfaceType *rhsQI =
1253 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001254 return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl());
1255 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001256 else if (ObjCInterfaceType *rhsIT =
1257 dyn_cast<ObjCInterfaceType>(rhs.getCanonicalType().getTypePtr())) {
1258 ObjCQualifiedInterfaceType *lhsQI =
1259 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001260 return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl());
1261 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00001262 return false;
1263}
1264
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001265/// Check that 'lhs' and 'rhs' are compatible interface types. Both types
1266/// must be canonical types.
Steve Naroff85f0dc52007-10-15 20:41:53 +00001267bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001268 assert (lhs->isCanonical() &&
1269 "interfaceTypesAreCompatible strip typedefs of lhs");
1270 assert (rhs->isCanonical() &&
1271 "interfaceTypesAreCompatible strip typedefs of rhs");
Fariborz Jahaniance2de812007-12-20 22:37:58 +00001272 if (lhs == rhs)
1273 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001274 ObjCInterfaceType *lhsIT = cast<ObjCInterfaceType>(lhs.getTypePtr());
1275 ObjCInterfaceType *rhsIT = cast<ObjCInterfaceType>(rhs.getTypePtr());
1276 ObjCInterfaceDecl *rhsIDecl = rhsIT->getDecl();
1277 ObjCInterfaceDecl *lhsIDecl = lhsIT->getDecl();
Fariborz Jahaniance2de812007-12-20 22:37:58 +00001278 // rhs is derived from lhs it is OK; else it is not OK.
1279 while (rhsIDecl != NULL) {
1280 if (rhsIDecl == lhsIDecl)
1281 return true;
1282 rhsIDecl = rhsIDecl->getSuperClass();
1283 }
1284 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001285}
1286
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001287bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1288 QualType rhs) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001289 ObjCQualifiedInterfaceType *lhsQI =
1290 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001291 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
Ted Kremenek42730c52008-01-07 19:49:32 +00001292 ObjCQualifiedInterfaceType *rhsQI =
1293 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001294 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001295 if (!interfaceTypesAreCompatible(
1296 getObjCInterfaceType(lhsQI->getDecl()).getCanonicalType(),
1297 getObjCInterfaceType(rhsQI->getDecl()).getCanonicalType()))
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001298 return false;
1299 /* All protocols in lhs must have a presense in rhs. */
1300 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1301 bool match = false;
Ted Kremenek42730c52008-01-07 19:49:32 +00001302 ObjCProtocolDecl *lhsProto = lhsQI->getProtocols(i);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001303 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001304 ObjCProtocolDecl *rhsProto = rhsQI->getProtocols(j);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001305 if (lhsProto == rhsProto) {
1306 match = true;
1307 break;
1308 }
1309 }
1310 if (!match)
1311 return false;
1312 }
1313 return true;
1314}
1315
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001316/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
1317/// inheritance hierarchy of 'rProto'.
Ted Kremenek42730c52008-01-07 19:49:32 +00001318static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
1319 ObjCProtocolDecl *rProto) {
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001320 if (lProto == rProto)
1321 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001322 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001323 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
1324 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
1325 return true;
1326 return false;
1327}
1328
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001329/// ClassImplementsProtocol - Checks that 'lProto' protocol
1330/// has been implemented in IDecl class, its super class or categories (if
1331/// lookupCategory is true).
Ted Kremenek42730c52008-01-07 19:49:32 +00001332static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1333 ObjCInterfaceDecl *IDecl,
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001334 bool lookupCategory) {
1335
1336 // 1st, look up the class.
Ted Kremenek42730c52008-01-07 19:49:32 +00001337 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001338 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
1339 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1340 return true;
1341 }
1342
1343 // 2nd, look up the category.
1344 if (lookupCategory)
Ted Kremenek42730c52008-01-07 19:49:32 +00001345 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001346 CDecl = CDecl->getNextClassCategory()) {
1347 protoList = CDecl->getReferencedProtocols();
1348 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
1349 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1350 return true;
1351 }
1352 }
1353
1354 // 3rd, look up the super class(s)
1355 if (IDecl->getSuperClass())
1356 return
1357 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
1358
1359 return false;
1360}
1361
Ted Kremenek42730c52008-01-07 19:49:32 +00001362/// ObjCQualifiedIdTypesAreCompatible - Compares two types, at least
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001363/// one of which is a protocol qualified 'id' type. When 'compare'
1364/// is true it is for comparison; when false, for assignment/initialization.
Ted Kremenek42730c52008-01-07 19:49:32 +00001365bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs,
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001366 QualType rhs,
1367 bool compare) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001368 // match id<P..> with an 'id' type in all cases.
1369 if (const PointerType *PT = lhs->getAsPointerType()) {
1370 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001371 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001372 return true;
1373
1374 }
1375 else if (const PointerType *PT = rhs->getAsPointerType()) {
1376 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001377 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001378 return true;
1379
1380 }
1381
Ted Kremenek42730c52008-01-07 19:49:32 +00001382 ObjCQualifiedInterfaceType *lhsQI = 0;
1383 ObjCQualifiedInterfaceType *rhsQI = 0;
1384 ObjCInterfaceDecl *lhsID = 0;
1385 ObjCInterfaceDecl *rhsID = 0;
1386 ObjCQualifiedIdType *lhsQID = dyn_cast<ObjCQualifiedIdType>(lhs);
1387 ObjCQualifiedIdType *rhsQID = dyn_cast<ObjCQualifiedIdType>(rhs);
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001388
1389 if (lhsQID) {
1390 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1391 QualType rtype =
1392 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1393 rhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001394 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001395 rtype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001396 if (!rhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001397 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001398 rtype.getCanonicalType().getTypePtr());
1399 if (IT)
1400 rhsID = IT->getDecl();
1401 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001402 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001403 if (!rhsQI && !rhsQID && !rhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001404 return false;
1405
Fariborz Jahaniance5528d2008-01-03 20:01:35 +00001406 unsigned numRhsProtocols = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001407 ObjCProtocolDecl **rhsProtoList = 0;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001408 if (rhsQI) {
1409 numRhsProtocols = rhsQI->getNumProtocols();
1410 rhsProtoList = rhsQI->getReferencedProtocols();
1411 }
1412 else if (rhsQID) {
1413 numRhsProtocols = rhsQID->getNumProtocols();
1414 rhsProtoList = rhsQID->getReferencedProtocols();
1415 }
1416
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001417 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001418 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001419 bool match = false;
1420
1421 // when comparing an id<P> on lhs with a static type on rhs,
1422 // see if static class implements all of id's protocols, directly or
1423 // through its super class and categories.
1424 if (rhsID) {
1425 if (ClassImplementsProtocol(lhsProto, rhsID, true))
1426 match = true;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001427 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001428 else for (unsigned j = 0; j < numRhsProtocols; j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001429 ObjCProtocolDecl *rhsProto = rhsProtoList[j];
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001430 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1431 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001432 match = true;
1433 break;
1434 }
1435 }
1436 if (!match)
1437 return false;
1438 }
1439 }
1440 else if (rhsQID) {
1441 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1442 QualType ltype =
1443 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1444 lhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001445 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001446 ltype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001447 if (!lhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001448 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001449 ltype.getCanonicalType().getTypePtr());
1450 if (IT)
1451 lhsID = IT->getDecl();
1452 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001453 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001454 if (!lhsQI && !lhsQID && !lhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001455 return false;
Fariborz Jahanian87829072007-12-20 19:24:10 +00001456
Fariborz Jahaniance5528d2008-01-03 20:01:35 +00001457 unsigned numLhsProtocols = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001458 ObjCProtocolDecl **lhsProtoList = 0;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001459 if (lhsQI) {
1460 numLhsProtocols = lhsQI->getNumProtocols();
1461 lhsProtoList = lhsQI->getReferencedProtocols();
1462 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001463 else if (lhsQID) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001464 numLhsProtocols = lhsQID->getNumProtocols();
1465 lhsProtoList = lhsQID->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001466 }
1467 bool match = false;
1468 // for static type vs. qualified 'id' type, check that class implements
1469 // one of 'id's protocols.
1470 if (lhsID) {
1471 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001472 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001473 if (ClassImplementsProtocol(rhsProto, lhsID, compare)) {
1474 match = true;
1475 break;
1476 }
1477 }
1478 }
1479 else for (unsigned i =0; i < numLhsProtocols; i++) {
1480 match = false;
Ted Kremenek42730c52008-01-07 19:49:32 +00001481 ObjCProtocolDecl *lhsProto = lhsProtoList[i];
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001482 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001483 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001484 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1485 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001486 match = true;
1487 break;
1488 }
1489 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001490 }
1491 if (!match)
1492 return false;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001493 }
1494 return true;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001495}
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001496
Chris Lattner5003e8b2007-11-01 05:03:41 +00001497bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1498 const VectorType *lVector = lhs->getAsVectorType();
1499 const VectorType *rVector = rhs->getAsVectorType();
1500
1501 if ((lVector->getElementType().getCanonicalType() ==
1502 rVector->getElementType().getCanonicalType()) &&
1503 (lVector->getNumElements() == rVector->getNumElements()))
1504 return true;
1505 return false;
1506}
1507
Steve Naroff85f0dc52007-10-15 20:41:53 +00001508// C99 6.2.7p1: If both are complete types, then the following additional
1509// requirements apply...FIXME (handle compatibility across source files).
1510bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
1511 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
1512 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
1513
1514 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
1515 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1516 return true;
1517 }
1518 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
1519 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1520 return true;
1521 }
Steve Naroff4a5e2072007-11-07 06:03:51 +00001522 // "Class" and "id" are compatible built-in structure types.
Ted Kremenek42730c52008-01-07 19:49:32 +00001523 if (isObjCIdType(lhs) && isObjCClassType(rhs) ||
1524 isObjCClassType(lhs) && isObjCIdType(rhs))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001525 return true;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001526 return false;
1527}
1528
1529bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1530 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1531 // identically qualified and both shall be pointers to compatible types.
1532 if (lhs.getQualifiers() != rhs.getQualifiers())
1533 return false;
1534
1535 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1536 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1537
1538 return typesAreCompatible(ltype, rtype);
1539}
1540
Bill Wendling6a9d8542007-12-03 07:33:35 +00001541// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroff85f0dc52007-10-15 20:41:53 +00001542// reference to T, the operation assigns to the object of type T denoted by the
1543// reference.
1544bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1545 QualType ltype = lhs;
1546
1547 if (lhs->isReferenceType())
1548 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1549
1550 QualType rtype = rhs;
1551
1552 if (rhs->isReferenceType())
1553 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1554
1555 return typesAreCompatible(ltype, rtype);
1556}
1557
1558bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1559 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1560 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1561 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1562 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1563
1564 // first check the return types (common between C99 and K&R).
1565 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1566 return false;
1567
1568 if (lproto && rproto) { // two C99 style function prototypes
1569 unsigned lproto_nargs = lproto->getNumArgs();
1570 unsigned rproto_nargs = rproto->getNumArgs();
1571
1572 if (lproto_nargs != rproto_nargs)
1573 return false;
1574
1575 // both prototypes have the same number of arguments.
1576 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1577 (rproto->isVariadic() && !lproto->isVariadic()))
1578 return false;
1579
1580 // The use of ellipsis agree...now check the argument types.
1581 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001582 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1583 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001584 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001585 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001586 return false;
1587 return true;
1588 }
1589 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1590 return true;
1591
1592 // we have a mixture of K&R style with C99 prototypes
1593 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1594
1595 if (proto->isVariadic())
1596 return false;
1597
1598 // FIXME: Each parameter type T in the prototype must be compatible with the
1599 // type resulting from applying the usual argument conversions to T.
1600 return true;
1601}
1602
1603bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
1604 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
1605 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
1606
1607 if (!typesAreCompatible(ltype, rtype))
1608 return false;
1609
1610 // FIXME: If both types specify constant sizes, then the sizes must also be
1611 // the same. Even if the sizes are the same, GCC produces an error.
1612 return true;
1613}
1614
1615/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1616/// both shall have the identically qualified version of a compatible type.
1617/// C99 6.2.7p1: Two types have compatible types if their types are the
1618/// same. See 6.7.[2,3,5] for additional rules.
1619bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroff577f9722008-01-29 18:58:14 +00001620 if (lhs.getQualifiers() != rhs.getQualifiers())
1621 return false;
1622
Steve Naroff85f0dc52007-10-15 20:41:53 +00001623 QualType lcanon = lhs.getCanonicalType();
1624 QualType rcanon = rhs.getCanonicalType();
1625
1626 // If two types are identical, they are are compatible
1627 if (lcanon == rcanon)
1628 return true;
Bill Wendling6a9d8542007-12-03 07:33:35 +00001629
1630 // C++ [expr]: If an expression initially has the type "reference to T", the
1631 // type is adjusted to "T" prior to any further analysis, the expression
1632 // designates the object or function denoted by the reference, and the
1633 // expression is an lvalue.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001634 if (ReferenceType *RT = dyn_cast<ReferenceType>(lcanon))
1635 lcanon = RT->getReferenceeType();
1636 if (ReferenceType *RT = dyn_cast<ReferenceType>(rcanon))
1637 rcanon = RT->getReferenceeType();
1638
1639 Type::TypeClass LHSClass = lcanon->getTypeClass();
1640 Type::TypeClass RHSClass = rcanon->getTypeClass();
1641
1642 // We want to consider the two function types to be the same for these
1643 // comparisons, just force one to the other.
1644 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1645 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001646
Steve Naroffc88babe2008-01-09 22:43:08 +00001647 // If the canonical type classes don't match...
Chris Lattnerc38d4522008-01-14 05:45:46 +00001648 if (LHSClass != RHSClass) {
Steve Naroff85f0dc52007-10-15 20:41:53 +00001649 // For Objective-C, it is possible for two types to be compatible
1650 // when their classes don't match (when dealing with "id"). If either type
1651 // is an interface, we defer to objcTypesAreCompatible().
Ted Kremenek42730c52008-01-07 19:49:32 +00001652 if (lcanon->isObjCInterfaceType() || rcanon->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001653 return objcTypesAreCompatible(lcanon, rcanon);
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001654
Chris Lattnerc38d4522008-01-14 05:45:46 +00001655 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1656 // a signed integer type, or an unsigned integer type.
1657 // FIXME: need to check the size and ensure it's the same.
1658 if ((lcanon->isEnumeralType() && rcanon->isIntegralType()) ||
1659 (rcanon->isEnumeralType() && lcanon->isIntegralType()))
1660 return true;
1661
Steve Naroff85f0dc52007-10-15 20:41:53 +00001662 return false;
1663 }
Steve Naroffc88babe2008-01-09 22:43:08 +00001664 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001665 switch (LHSClass) {
1666 case Type::FunctionProto: assert(0 && "Canonicalized away above");
1667 case Type::Pointer:
1668 return pointerTypesAreCompatible(lcanon, rcanon);
1669 case Type::ConstantArray:
1670 case Type::VariableArray:
1671 return arrayTypesAreCompatible(lcanon, rcanon);
1672 case Type::FunctionNoProto:
1673 return functionTypesAreCompatible(lcanon, rcanon);
1674 case Type::Tagged: // handle structures, unions
1675 return tagTypesAreCompatible(lcanon, rcanon);
1676 case Type::Builtin:
1677 return builtinTypesAreCompatible(lcanon, rcanon);
1678 case Type::ObjCInterface:
1679 return interfaceTypesAreCompatible(lcanon, rcanon);
1680 case Type::Vector:
1681 case Type::OCUVector:
1682 return vectorTypesAreCompatible(lcanon, rcanon);
1683 case Type::ObjCQualifiedInterface:
1684 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
1685 default:
1686 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001687 }
1688 return true; // should never get here...
1689}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001690
Ted Kremenek738e6c02007-10-31 17:10:13 +00001691/// Emit - Serialize an ASTContext object to Bitcode.
1692void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001693 S.EmitRef(SourceMgr);
1694 S.EmitRef(Target);
1695 S.EmitRef(Idents);
1696 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001697
Ted Kremenek68228a92007-10-31 22:44:07 +00001698 // Emit the size of the type vector so that we can reserve that size
1699 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001700 S.EmitInt(Types.size());
1701
Ted Kremenek034a78c2007-11-13 22:02:55 +00001702 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1703 I!=E;++I)
1704 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001705
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001706 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001707}
1708
Ted Kremenekacba3612007-11-13 00:25:37 +00001709ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek68228a92007-10-31 22:44:07 +00001710 SourceManager &SM = D.ReadRef<SourceManager>();
1711 TargetInfo &t = D.ReadRef<TargetInfo>();
1712 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1713 SelectorTable &sels = D.ReadRef<SelectorTable>();
1714
1715 unsigned size_reserve = D.ReadInt();
1716
1717 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1718
Ted Kremenek034a78c2007-11-13 22:02:55 +00001719 for (unsigned i = 0; i < size_reserve; ++i)
1720 Type::Create(*A,i,D);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001721
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001722 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001723
1724 return A;
1725}