blob: 4351b89e9609151e0a3e66cb06ea03f3fc5831fa [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.
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000131 if (Target.isCharSigned(FullSourceLoc()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 InitBuiltinType(CharTy, BuiltinType::Char_S);
133 else
134 InitBuiltinType(CharTy, BuiltinType::Char_U);
135 // C99 6.2.5p4.
136 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
137 InitBuiltinType(ShortTy, BuiltinType::Short);
138 InitBuiltinType(IntTy, BuiltinType::Int);
139 InitBuiltinType(LongTy, BuiltinType::Long);
140 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
141
142 // C99 6.2.5p6.
143 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
144 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
145 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
146 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
147 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
148
149 // C99 6.2.5p10.
150 InitBuiltinType(FloatTy, BuiltinType::Float);
151 InitBuiltinType(DoubleTy, BuiltinType::Double);
152 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
153
154 // C99 6.2.5p11.
155 FloatComplexTy = getComplexType(FloatTy);
156 DoubleComplexTy = getComplexType(DoubleTy);
157 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve 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!");
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000216 case BuiltinType::Bool: Target.getBoolInfo(Size,Align,getFullLoc(L));
217 break;
218
Chris Lattner692233e2007-07-13 22:27:08 +0000219 case BuiltinType::Char_S:
220 case BuiltinType::Char_U:
221 case BuiltinType::UChar:
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000222 case BuiltinType::SChar: Target.getCharInfo(Size,Align,getFullLoc(L));
223 break;
224
Chris Lattner692233e2007-07-13 22:27:08 +0000225 case BuiltinType::UShort:
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000226 case BuiltinType::Short: Target.getShortInfo(Size,Align,getFullLoc(L));
227 break;
228
Chris Lattner692233e2007-07-13 22:27:08 +0000229 case BuiltinType::UInt:
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000230 case BuiltinType::Int: Target.getIntInfo(Size,Align,getFullLoc(L));
231 break;
232
Chris Lattner692233e2007-07-13 22:27:08 +0000233 case BuiltinType::ULong:
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000234 case BuiltinType::Long: Target.getLongInfo(Size,Align,getFullLoc(L));
235 break;
236
Chris Lattner692233e2007-07-13 22:27:08 +0000237 case BuiltinType::ULongLong:
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000238 case BuiltinType::LongLong: Target.getLongLongInfo(Size,Align,
239 getFullLoc(L));
240 break;
241
242 case BuiltinType::Float: Target.getFloatInfo(Size,Align,F,
243 getFullLoc(L));
244 break;
245
246 case BuiltinType::Double: Target.getDoubleInfo(Size,Align,F,
247 getFullLoc(L));
248 break;
249
250 case BuiltinType::LongDouble: Target.getLongDoubleInfo(Size,Align,F,
251 getFullLoc(L));
252 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000253 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000254 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000255 }
Ted Kremenek9c728dc2007-12-12 22:39:36 +0000256 case Type::Pointer: Target.getPointerInfo(Size, Align, getFullLoc(L)); break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000257 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000258 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000259 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
260 // FIXME: This is wrong for struct layout!
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000261 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000262
263 case Type::Complex: {
264 // Complex types have the same alignment as their elements, but twice the
265 // size.
266 std::pair<uint64_t, unsigned> EltInfo =
267 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
268 Size = EltInfo.first*2;
269 Align = EltInfo.second;
270 break;
271 }
272 case Type::Tagged:
Chris Lattner6cd862c2007-08-27 17:38:00 +0000273 TagType *TT = cast<TagType>(T);
274 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
Devang Patel88a981b2007-11-01 19:11:01 +0000275 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000276 Size = Layout.getSize();
277 Align = Layout.getAlignment();
278 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattnere00b18c2007-08-28 18:24:31 +0000279 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000280 } else {
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000281 assert(0 && "Unimplemented type sizes!");
Chris Lattner6cd862c2007-08-27 17:38:00 +0000282 }
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000283 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000284 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000285
Chris Lattner464175b2007-07-18 17:52:12 +0000286 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000287 return std::make_pair(Size, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000288}
289
Devang Patel88a981b2007-11-01 19:11:01 +0000290/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner464175b2007-07-18 17:52:12 +0000291/// specified record (struct/union/class), which indicates its size and field
292/// position information.
Devang Patel88a981b2007-11-01 19:11:01 +0000293const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D,
294 SourceLocation L) {
Chris Lattner464175b2007-07-18 17:52:12 +0000295 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
296
297 // Look up this layout, if already laid out, return what we have.
Devang Patel88a981b2007-11-01 19:11:01 +0000298 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner464175b2007-07-18 17:52:12 +0000299 if (Entry) return *Entry;
300
Devang Patel88a981b2007-11-01 19:11:01 +0000301 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
302 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
303 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner464175b2007-07-18 17:52:12 +0000304 Entry = NewEntry;
305
306 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
307 uint64_t RecordSize = 0;
308 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
309
310 if (D->getKind() != Decl::Union) {
311 // Layout each field, for now, just sequentially, respecting alignment. In
312 // the future, this will need to be tweakable by targets.
313 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
314 const FieldDecl *FD = D->getMember(i);
315 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
316 uint64_t FieldSize = FieldInfo.first;
317 unsigned FieldAlign = FieldInfo.second;
318
319 // Round up the current record size to the field's alignment boundary.
320 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
321
322 // Place this field at the current location.
323 FieldOffsets[i] = RecordSize;
324
325 // Reserve space for this field.
326 RecordSize += FieldSize;
327
328 // Remember max struct/class alignment.
329 RecordAlign = std::max(RecordAlign, FieldAlign);
330 }
331
332 // Finally, round the size of the total struct up to the alignment of the
333 // struct itself.
334 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
335 } else {
336 // Union layout just puts each member at the start of the record.
337 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
338 const FieldDecl *FD = D->getMember(i);
339 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
340 uint64_t FieldSize = FieldInfo.first;
341 unsigned FieldAlign = FieldInfo.second;
342
343 // Round up the current record size to the field's alignment boundary.
344 RecordSize = std::max(RecordSize, FieldSize);
345
346 // Place this field at the start of the record.
347 FieldOffsets[i] = 0;
348
349 // Remember max struct/class alignment.
350 RecordAlign = std::max(RecordAlign, FieldAlign);
351 }
352 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000353
354 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
355 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000356}
357
Chris Lattnera7674d82007-07-13 22:13:22 +0000358//===----------------------------------------------------------------------===//
359// Type creation/memoization methods
360//===----------------------------------------------------------------------===//
361
362
Reid Spencer5f016e22007-07-11 17:01:13 +0000363/// getComplexType - Return the uniqued reference to the type for a complex
364/// number with the specified element type.
365QualType ASTContext::getComplexType(QualType T) {
366 // Unique pointers, to guarantee there is only one pointer of a particular
367 // structure.
368 llvm::FoldingSetNodeID ID;
369 ComplexType::Profile(ID, T);
370
371 void *InsertPos = 0;
372 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
373 return QualType(CT, 0);
374
375 // If the pointee type isn't canonical, this won't be a canonical type either,
376 // so fill in the canonical type field.
377 QualType Canonical;
378 if (!T->isCanonical()) {
379 Canonical = getComplexType(T.getCanonicalType());
380
381 // Get the new insert position for the node we care about.
382 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
383 assert(NewIP == 0 && "Shouldn't be in the map!");
384 }
385 ComplexType *New = new ComplexType(T, Canonical);
386 Types.push_back(New);
387 ComplexTypes.InsertNode(New, InsertPos);
388 return QualType(New, 0);
389}
390
391
392/// getPointerType - Return the uniqued reference to the type for a pointer to
393/// the specified type.
394QualType ASTContext::getPointerType(QualType T) {
395 // Unique pointers, to guarantee there is only one pointer of a particular
396 // structure.
397 llvm::FoldingSetNodeID ID;
398 PointerType::Profile(ID, T);
399
400 void *InsertPos = 0;
401 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
402 return QualType(PT, 0);
403
404 // If the pointee type isn't canonical, this won't be a canonical type either,
405 // so fill in the canonical type field.
406 QualType Canonical;
407 if (!T->isCanonical()) {
408 Canonical = getPointerType(T.getCanonicalType());
409
410 // Get the new insert position for the node we care about.
411 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
412 assert(NewIP == 0 && "Shouldn't be in the map!");
413 }
414 PointerType *New = new PointerType(T, Canonical);
415 Types.push_back(New);
416 PointerTypes.InsertNode(New, InsertPos);
417 return QualType(New, 0);
418}
419
420/// getReferenceType - Return the uniqued reference to the type for a reference
421/// to the specified type.
422QualType ASTContext::getReferenceType(QualType T) {
423 // Unique pointers, to guarantee there is only one pointer of a particular
424 // structure.
425 llvm::FoldingSetNodeID ID;
426 ReferenceType::Profile(ID, T);
427
428 void *InsertPos = 0;
429 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
430 return QualType(RT, 0);
431
432 // If the referencee type isn't canonical, this won't be a canonical type
433 // either, so fill in the canonical type field.
434 QualType Canonical;
435 if (!T->isCanonical()) {
436 Canonical = getReferenceType(T.getCanonicalType());
437
438 // Get the new insert position for the node we care about.
439 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
440 assert(NewIP == 0 && "Shouldn't be in the map!");
441 }
442
443 ReferenceType *New = new ReferenceType(T, Canonical);
444 Types.push_back(New);
445 ReferenceTypes.InsertNode(New, InsertPos);
446 return QualType(New, 0);
447}
448
Steve Narofffb22d962007-08-30 01:06:46 +0000449/// getConstantArrayType - Return the unique reference to the type for an
450/// array of the specified element type.
451QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000452 const llvm::APInt &ArySize,
453 ArrayType::ArraySizeModifier ASM,
454 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000455 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000456 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000457
458 void *InsertPos = 0;
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000459 if (ConstantArrayType *ATP =
460 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 return QualType(ATP, 0);
462
463 // If the element type isn't canonical, this won't be a canonical type either,
464 // so fill in the canonical type field.
465 QualType Canonical;
466 if (!EltTy->isCanonical()) {
Steve Naroffc9406122007-08-30 18:10:14 +0000467 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
468 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 // Get the new insert position for the node we care about.
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000470 ConstantArrayType *NewIP =
471 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
472
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 assert(NewIP == 0 && "Shouldn't be in the map!");
474 }
475
Steve Naroffc9406122007-08-30 18:10:14 +0000476 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
477 ASM, EltTypeQuals);
Ted Kremenek7192f8e2007-10-31 17:10:13 +0000478 ConstantArrayTypes.InsertNode(New, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 Types.push_back(New);
480 return QualType(New, 0);
481}
482
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000483/// getVariableArrayType - Returns a non-unique reference to the type for a
484/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000485QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
486 ArrayType::ArraySizeModifier ASM,
487 unsigned EltTypeQuals) {
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000488 if (NumElts) {
489 // Since we don't unique expressions, it isn't possible to unique VLA's
490 // that have an expression provided for their size.
491
Ted Kremenek347b9f32007-10-30 16:41:53 +0000492 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
493 ASM, EltTypeQuals);
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000494
Ted Kremenek347b9f32007-10-30 16:41:53 +0000495 CompleteVariableArrayTypes.push_back(New);
Ted Kremenek2bd24ba2007-10-29 23:37:31 +0000496 Types.push_back(New);
497 return QualType(New, 0);
498 }
499 else {
500 // No size is provided for the VLA. These we can unique.
501 llvm::FoldingSetNodeID ID;
502 VariableArrayType::Profile(ID, EltTy);
503
504 void *InsertPos = 0;
505 if (VariableArrayType *ATP =
506 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
507 return QualType(ATP, 0);
508
509 // If the element type isn't canonical, this won't be a canonical type
510 // either, so fill in the canonical type field.
511 QualType Canonical;
512
513 if (!EltTy->isCanonical()) {
514 Canonical = getVariableArrayType(EltTy.getCanonicalType(), NumElts,
515 ASM, EltTypeQuals);
516
517 // Get the new insert position for the node we care about.
518 VariableArrayType *NewIP =
519 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
520
521 assert(NewIP == 0 && "Shouldn't be in the map!");
522 }
523
524 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
525 ASM, EltTypeQuals);
526
527 IncompleteVariableArrayTypes.InsertNode(New, InsertPos);
528 Types.push_back(New);
529 return QualType(New, 0);
530 }
Steve Narofffb22d962007-08-30 01:06:46 +0000531}
532
Steve Naroff73322922007-07-18 18:00:27 +0000533/// getVectorType - Return the unique reference to a vector type of
534/// the specified element type and size. VectorType must be a built-in type.
535QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000536 BuiltinType *baseType;
537
538 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000539 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000540
541 // Check if we've already instantiated a vector of this type.
542 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000543 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 void *InsertPos = 0;
545 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
546 return QualType(VTP, 0);
547
548 // If the element type isn't canonical, this won't be a canonical type either,
549 // so fill in the canonical type field.
550 QualType Canonical;
551 if (!vecType->isCanonical()) {
Steve Naroff73322922007-07-18 18:00:27 +0000552 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000553
554 // Get the new insert position for the node we care about.
555 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
556 assert(NewIP == 0 && "Shouldn't be in the map!");
557 }
558 VectorType *New = new VectorType(vecType, NumElts, Canonical);
559 VectorTypes.InsertNode(New, InsertPos);
560 Types.push_back(New);
561 return QualType(New, 0);
562}
563
Steve Naroff73322922007-07-18 18:00:27 +0000564/// getOCUVectorType - Return the unique reference to an OCU vector type of
565/// the specified element type and size. VectorType must be a built-in type.
566QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
567 BuiltinType *baseType;
568
569 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
570 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
571
572 // Check if we've already instantiated a vector of this type.
573 llvm::FoldingSetNodeID ID;
574 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
575 void *InsertPos = 0;
576 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
577 return QualType(VTP, 0);
578
579 // If the element type isn't canonical, this won't be a canonical type either,
580 // so fill in the canonical type field.
581 QualType Canonical;
582 if (!vecType->isCanonical()) {
583 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
584
585 // Get the new insert position for the node we care about.
586 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
587 assert(NewIP == 0 && "Shouldn't be in the map!");
588 }
589 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
590 VectorTypes.InsertNode(New, InsertPos);
591 Types.push_back(New);
592 return QualType(New, 0);
593}
594
Reid Spencer5f016e22007-07-11 17:01:13 +0000595/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
596///
597QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
598 // Unique functions, to guarantee there is only one function of a particular
599 // structure.
600 llvm::FoldingSetNodeID ID;
601 FunctionTypeNoProto::Profile(ID, ResultTy);
602
603 void *InsertPos = 0;
604 if (FunctionTypeNoProto *FT =
605 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
606 return QualType(FT, 0);
607
608 QualType Canonical;
609 if (!ResultTy->isCanonical()) {
610 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
611
612 // Get the new insert position for the node we care about.
613 FunctionTypeNoProto *NewIP =
614 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
615 assert(NewIP == 0 && "Shouldn't be in the map!");
616 }
617
618 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
619 Types.push_back(New);
620 FunctionTypeProtos.InsertNode(New, InsertPos);
621 return QualType(New, 0);
622}
623
624/// getFunctionType - Return a normal function type with a typed argument
625/// list. isVariadic indicates whether the argument list includes '...'.
626QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
627 unsigned NumArgs, bool isVariadic) {
628 // Unique functions, to guarantee there is only one function of a particular
629 // structure.
630 llvm::FoldingSetNodeID ID;
631 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
632
633 void *InsertPos = 0;
634 if (FunctionTypeProto *FTP =
635 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
636 return QualType(FTP, 0);
637
638 // Determine whether the type being created is already canonical or not.
639 bool isCanonical = ResultTy->isCanonical();
640 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
641 if (!ArgArray[i]->isCanonical())
642 isCanonical = false;
643
644 // If this type isn't canonical, get the canonical version of it.
645 QualType Canonical;
646 if (!isCanonical) {
647 llvm::SmallVector<QualType, 16> CanonicalArgs;
648 CanonicalArgs.reserve(NumArgs);
649 for (unsigned i = 0; i != NumArgs; ++i)
650 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
651
652 Canonical = getFunctionType(ResultTy.getCanonicalType(),
653 &CanonicalArgs[0], NumArgs,
654 isVariadic);
655
656 // Get the new insert position for the node we care about.
657 FunctionTypeProto *NewIP =
658 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
659 assert(NewIP == 0 && "Shouldn't be in the map!");
660 }
661
662 // FunctionTypeProto objects are not allocated with new because they have a
663 // variable size array (for parameter types) at the end of them.
664 FunctionTypeProto *FTP =
665 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000666 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000667 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
668 Canonical);
669 Types.push_back(FTP);
670 FunctionTypeProtos.InsertNode(FTP, InsertPos);
671 return QualType(FTP, 0);
672}
673
674/// getTypedefType - Return the unique reference to the type for the
675/// specified typename decl.
676QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
677 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
678
679 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
680 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
681 Types.push_back(Decl->TypeForDecl);
682 return QualType(Decl->TypeForDecl, 0);
683}
684
Steve Naroff3536b442007-09-06 21:24:23 +0000685/// getObjcInterfaceType - Return the unique reference to the type for the
686/// specified ObjC interface decl.
687QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) {
688 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
689
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000690 Decl->TypeForDecl = new ObjcInterfaceType(Type::ObjcInterface, Decl);
Steve Naroff3536b442007-09-06 21:24:23 +0000691 Types.push_back(Decl->TypeForDecl);
692 return QualType(Decl->TypeForDecl, 0);
693}
694
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000695/// getObjcQualifiedInterfaceType - Return a
696/// ObjcQualifiedInterfaceType type for the given interface decl and
697/// the conforming protocol list.
698QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl,
699 ObjcProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000700 llvm::FoldingSetNodeID ID;
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000701 ObjcQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000702
703 void *InsertPos = 0;
704 if (ObjcQualifiedInterfaceType *QT =
705 ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
706 return QualType(QT, 0);
707
708 // No Match;
Chris Lattner00bb2832007-10-11 03:36:41 +0000709 ObjcQualifiedInterfaceType *QType =
Fariborz Jahanian06cef252007-12-13 20:47:42 +0000710 new ObjcQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000711 Types.push_back(QType);
712 ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
713 return QualType(QType, 0);
714}
715
Steve Naroff9752f252007-08-01 18:02:17 +0000716/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
717/// TypeOfExpr AST's (since expression's are never shared). For example,
718/// multiple declarations that refer to "typeof(x)" all contain different
719/// DeclRefExpr's. This doesn't effect the type checker, since it operates
720/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000721QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffd1861fd2007-07-31 12:34:36 +0000722 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000723 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
724 Types.push_back(toe);
725 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000726}
727
Steve Naroff9752f252007-08-01 18:02:17 +0000728/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
729/// TypeOfType AST's. The only motivation to unique these nodes would be
730/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
731/// an issue. This doesn't effect the type checker, since it operates
732/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000733QualType ASTContext::getTypeOfType(QualType tofType) {
734 QualType Canonical = tofType.getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000735 TypeOfType *tot = new TypeOfType(tofType, Canonical);
736 Types.push_back(tot);
737 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000738}
739
Reid Spencer5f016e22007-07-11 17:01:13 +0000740/// getTagDeclType - Return the unique reference to the type for the
741/// specified TagDecl (struct/union/class/enum) decl.
742QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekd778f882007-11-26 21:16:01 +0000743 assert (Decl);
744
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000745 // The decl stores the type cache.
Ted Kremenekd778f882007-11-26 21:16:01 +0000746 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000747
748 TagType* T = new TagType(Decl, QualType());
Ted Kremenekd778f882007-11-26 21:16:01 +0000749 Types.push_back(T);
750 Decl->TypeForDecl = T;
Ted Kremenekea0c6fb2007-11-14 00:03:20 +0000751
752 return QualType(T, 0);
Reid Spencer5f016e22007-07-11 17:01:13 +0000753}
754
755/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
756/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
757/// needs to agree with the definition in <stddef.h>.
758QualType ASTContext::getSizeType() const {
759 // On Darwin, size_t is defined as a "long unsigned int".
760 // FIXME: should derive from "Target".
761 return UnsignedLongTy;
762}
763
Chris Lattner8b9023b2007-07-13 03:05:23 +0000764/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
765/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
766QualType ASTContext::getPointerDiffType() const {
767 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
768 // FIXME: should derive from "Target".
769 return IntTy;
770}
771
Reid Spencer5f016e22007-07-11 17:01:13 +0000772/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
773/// routine will assert if passed a built-in type that isn't an integer or enum.
774static int getIntegerRank(QualType t) {
775 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
776 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
777 return 4;
778 }
779
780 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
781 switch (BT->getKind()) {
782 default:
783 assert(0 && "getIntegerRank(): not a built-in integer");
784 case BuiltinType::Bool:
785 return 1;
786 case BuiltinType::Char_S:
787 case BuiltinType::Char_U:
788 case BuiltinType::SChar:
789 case BuiltinType::UChar:
790 return 2;
791 case BuiltinType::Short:
792 case BuiltinType::UShort:
793 return 3;
794 case BuiltinType::Int:
795 case BuiltinType::UInt:
796 return 4;
797 case BuiltinType::Long:
798 case BuiltinType::ULong:
799 return 5;
800 case BuiltinType::LongLong:
801 case BuiltinType::ULongLong:
802 return 6;
803 }
804}
805
806/// getFloatingRank - Return a relative rank for floating point types.
807/// This routine will assert if passed a built-in type that isn't a float.
808static int getFloatingRank(QualType T) {
809 T = T.getCanonicalType();
810 if (ComplexType *CT = dyn_cast<ComplexType>(T))
811 return getFloatingRank(CT->getElementType());
812
813 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner770951b2007-11-01 05:03:41 +0000814 default: assert(0 && "getFloatingRank(): not a floating type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000815 case BuiltinType::Float: return FloatRank;
816 case BuiltinType::Double: return DoubleRank;
817 case BuiltinType::LongDouble: return LongDoubleRank;
818 }
819}
820
Steve Naroff716c7302007-08-27 01:41:48 +0000821/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
822/// point or a complex type (based on typeDomain/typeSize).
823/// 'typeDomain' is a real floating point or complex type.
824/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +0000825QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
826 QualType typeSize, QualType typeDomain) const {
827 if (typeDomain->isComplexType()) {
828 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000829 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000830 case FloatRank: return FloatComplexTy;
831 case DoubleRank: return DoubleComplexTy;
832 case LongDoubleRank: return LongDoubleComplexTy;
833 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000834 }
Steve Narofff1448a02007-08-27 01:27:54 +0000835 if (typeDomain->isRealFloatingType()) {
836 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000837 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000838 case FloatRank: return FloatTy;
839 case DoubleRank: return DoubleTy;
840 case LongDoubleRank: return LongDoubleTy;
841 }
842 }
843 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000844 //an invalid return value, but the assert
845 //will ensure that this code is never reached.
846 return VoidTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000847}
848
Steve Narofffb0d4962007-08-27 15:30:22 +0000849/// compareFloatingType - Handles 3 different combos:
850/// float/float, float/complex, complex/complex.
851/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
852int ASTContext::compareFloatingType(QualType lt, QualType rt) {
853 if (getFloatingRank(lt) == getFloatingRank(rt))
854 return 0;
855 if (getFloatingRank(lt) > getFloatingRank(rt))
856 return 1;
857 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000858}
859
860// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
861// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
862QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
863 if (lhs == rhs) return lhs;
864
865 bool t1Unsigned = lhs->isUnsignedIntegerType();
866 bool t2Unsigned = rhs->isUnsignedIntegerType();
867
868 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
869 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
870
871 // We have two integer types with differing signs
872 QualType unsignedType = t1Unsigned ? lhs : rhs;
873 QualType signedType = t1Unsigned ? rhs : lhs;
874
875 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
876 return unsignedType;
877 else {
878 // FIXME: Need to check if the signed type can represent all values of the
879 // unsigned type. If it can, then the result is the signed type.
880 // If it can't, then the result is the unsigned version of the signed type.
881 // Should probably add a helper that returns a signed integer type from
882 // an unsigned (and vice versa). C99 6.3.1.8.
883 return signedType;
884 }
885}
Anders Carlsson71993dd2007-08-17 05:31:46 +0000886
887// getCFConstantStringType - Return the type used for constant CFStrings.
888QualType ASTContext::getCFConstantStringType() {
889 if (!CFConstantStringTypeDecl) {
890 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroffbeaf2992007-11-03 11:27:19 +0000891 &Idents.get("NSConstantString"),
Anders Carlsson71993dd2007-08-17 05:31:46 +0000892 0);
Anders Carlssonf06273f2007-11-19 00:25:30 +0000893 QualType FieldTypes[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +0000894
895 // const int *isa;
896 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonf06273f2007-11-19 00:25:30 +0000897 // int flags;
898 FieldTypes[1] = IntTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000899 // const char *str;
Anders Carlssonf06273f2007-11-19 00:25:30 +0000900 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson71993dd2007-08-17 05:31:46 +0000901 // long length;
Anders Carlssonf06273f2007-11-19 00:25:30 +0000902 FieldTypes[3] = LongTy;
Anders Carlsson71993dd2007-08-17 05:31:46 +0000903 // Create fields
Anders Carlssonf06273f2007-11-19 00:25:30 +0000904 FieldDecl *FieldDecls[4];
Anders Carlsson71993dd2007-08-17 05:31:46 +0000905
Anders Carlssonf06273f2007-11-19 00:25:30 +0000906 for (unsigned i = 0; i < 4; ++i)
Steve Narofff38661e2007-09-14 02:20:46 +0000907 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +0000908
909 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
910 }
911
912 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +0000913}
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000914
Anders Carlssone8c49532007-10-29 06:33:42 +0000915// This returns true if a type has been typedefed to BOOL:
916// typedef <type> BOOL;
Chris Lattner2d998332007-10-30 20:27:44 +0000917static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone8c49532007-10-29 06:33:42 +0000918 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattner2d998332007-10-30 20:27:44 +0000919 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000920
921 return false;
922}
923
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000924/// getObjcEncodingTypeSize returns size of type for objective-c encoding
925/// purpose.
926int ASTContext::getObjcEncodingTypeSize(QualType type) {
927 SourceLocation Loc;
928 uint64_t sz = getTypeSize(type, Loc);
929
930 // Make all integer and enum types at least as large as an int
931 if (sz > 0 && type->isIntegralType())
932 sz = std::max(sz, getTypeSize(IntTy, Loc));
933 // Treat arrays as pointers, since that's how they're passed in.
934 else if (type->isArrayType())
935 sz = getTypeSize(VoidPtrTy, Loc);
936 return sz / getTypeSize(CharTy, Loc);
937}
938
939/// getObjcEncodingForMethodDecl - Return the encoded type for this method
940/// declaration.
941void ASTContext::getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl,
942 std::string& S)
943{
Fariborz Jahanianecb01e62007-11-01 17:18:37 +0000944 // Encode type qualifer, 'in', 'inout', etc. for the return type.
945 getObjcEncodingForTypeQualifier(Decl->getObjcDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000946 // Encode result type.
947 getObjcEncodingForType(Decl->getResultType(), S);
948 // Compute size of all parameters.
949 // Start with computing size of a pointer in number of bytes.
950 // FIXME: There might(should) be a better way of doing this computation!
951 SourceLocation Loc;
952 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
953 // The first two arguments (self and _cmd) are pointers; account for
954 // their size.
955 int ParmOffset = 2 * PtrSize;
956 int NumOfParams = Decl->getNumParams();
957 for (int i = 0; i < NumOfParams; i++) {
958 QualType PType = Decl->getParamDecl(i)->getType();
959 int sz = getObjcEncodingTypeSize (PType);
960 assert (sz > 0 && "getObjcEncodingForMethodDecl - Incomplete param type");
961 ParmOffset += sz;
962 }
963 S += llvm::utostr(ParmOffset);
964 S += "@0:";
965 S += llvm::utostr(PtrSize);
966
967 // Argument types.
968 ParmOffset = 2 * PtrSize;
969 for (int i = 0; i < NumOfParams; i++) {
970 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianecb01e62007-11-01 17:18:37 +0000971 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000972 // 'in', 'inout', etc.
Fariborz Jahanianecb01e62007-11-01 17:18:37 +0000973 getObjcEncodingForTypeQualifier(
974 Decl->getParamDecl(i)->getObjcDeclQualifier(), S);
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000975 getObjcEncodingForType(PType, S);
976 S += llvm::utostr(ParmOffset);
977 ParmOffset += getObjcEncodingTypeSize(PType);
978 }
979}
980
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000981void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const
982{
Anders Carlssone8c49532007-10-29 06:33:42 +0000983 // FIXME: This currently doesn't encode:
984 // @ An object (whether statically typed or typed id)
985 // # A class object (Class)
986 // : A method selector (SEL)
987 // {name=type...} A structure
988 // (name=type...) A union
989 // bnum A bit field of num bits
990
991 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000992 char encoding;
993 switch (BT->getKind()) {
994 case BuiltinType::Void:
995 encoding = 'v';
996 break;
997 case BuiltinType::Bool:
998 encoding = 'B';
999 break;
1000 case BuiltinType::Char_U:
1001 case BuiltinType::UChar:
1002 encoding = 'C';
1003 break;
1004 case BuiltinType::UShort:
1005 encoding = 'S';
1006 break;
1007 case BuiltinType::UInt:
1008 encoding = 'I';
1009 break;
1010 case BuiltinType::ULong:
1011 encoding = 'L';
1012 break;
1013 case BuiltinType::ULongLong:
1014 encoding = 'Q';
1015 break;
1016 case BuiltinType::Char_S:
1017 case BuiltinType::SChar:
1018 encoding = 'c';
1019 break;
1020 case BuiltinType::Short:
1021 encoding = 's';
1022 break;
1023 case BuiltinType::Int:
1024 encoding = 'i';
1025 break;
1026 case BuiltinType::Long:
1027 encoding = 'l';
1028 break;
1029 case BuiltinType::LongLong:
1030 encoding = 'q';
1031 break;
1032 case BuiltinType::Float:
1033 encoding = 'f';
1034 break;
1035 case BuiltinType::Double:
1036 encoding = 'd';
1037 break;
1038 case BuiltinType::LongDouble:
1039 encoding = 'd';
1040 break;
1041 default:
1042 assert(0 && "Unhandled builtin type kind");
1043 }
1044
1045 S += encoding;
Anders Carlssone8c49532007-10-29 06:33:42 +00001046 } else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001047 QualType PointeeTy = PT->getPointeeType();
Anders Carlsson8baaca52007-10-31 02:53:19 +00001048 if (isObjcIdType(PointeeTy) || PointeeTy->isObjcInterfaceType()) {
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001049 S += '@';
1050 return;
Anders Carlsson8baaca52007-10-31 02:53:19 +00001051 } else if (isObjcClassType(PointeeTy)) {
1052 S += '#';
1053 return;
1054 } else if (isObjcSelType(PointeeTy)) {
1055 S += ':';
1056 return;
Fariborz Jahanianc2939bc2007-10-30 17:06:23 +00001057 }
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001058
1059 if (PointeeTy->isCharType()) {
1060 // char pointer types should be encoded as '*' unless it is a
1061 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +00001062 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001063 S += '*';
1064 return;
1065 }
1066 }
1067
1068 S += '^';
1069 getObjcEncodingForType(PT->getPointeeType(), S);
Anders Carlssone8c49532007-10-29 06:33:42 +00001070 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001071 S += '[';
1072
1073 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1074 S += llvm::utostr(CAT->getSize().getZExtValue());
1075 else
1076 assert(0 && "Unhandled array type!");
1077
1078 getObjcEncodingForType(AT->getElementType(), S);
1079 S += ']';
Anders Carlssonc0a87b72007-10-30 00:06:20 +00001080 } else if (T->getAsFunctionType()) {
1081 S += '?';
Fariborz Jahanian6de88a82007-11-13 23:21:38 +00001082 } else if (const RecordType *RTy = T->getAsRecordType()) {
1083 RecordDecl *RDecl= RTy->getDecl();
1084 S += '{';
1085 S += RDecl->getName();
1086 S += '=';
1087 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1088 FieldDecl *field = RDecl->getMember(i);
1089 getObjcEncodingForType(field->getType(), S);
1090 }
1091 S += '}';
Steve Naroff5e711242007-12-12 22:30:11 +00001092 } else if (T->isEnumeralType()) {
1093 S += 'i';
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001094 } else
Steve Naroff5e711242007-12-12 22:30:11 +00001095 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +00001096}
1097
Fariborz Jahanianecb01e62007-11-01 17:18:37 +00001098void ASTContext::getObjcEncodingForTypeQualifier(Decl::ObjcDeclQualifier QT,
1099 std::string& S) const {
1100 if (QT & Decl::OBJC_TQ_In)
1101 S += 'n';
1102 if (QT & Decl::OBJC_TQ_Inout)
1103 S += 'N';
1104 if (QT & Decl::OBJC_TQ_Out)
1105 S += 'o';
1106 if (QT & Decl::OBJC_TQ_Bycopy)
1107 S += 'O';
1108 if (QT & Decl::OBJC_TQ_Byref)
1109 S += 'R';
1110 if (QT & Decl::OBJC_TQ_Oneway)
1111 S += 'V';
1112}
1113
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001114void ASTContext::setBuiltinVaListType(QualType T)
1115{
1116 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1117
1118 BuiltinVaListType = T;
1119}
1120
Steve Naroff7e219e42007-10-15 14:41:52 +00001121void ASTContext::setObjcIdType(TypedefDecl *TD)
1122{
1123 assert(ObjcIdType.isNull() && "'id' type already set!");
1124
1125 ObjcIdType = getTypedefType(TD);
1126
1127 // typedef struct objc_object *id;
1128 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1129 assert(ptr && "'id' incorrectly typed");
1130 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1131 assert(rec && "'id' incorrectly typed");
1132 IdStructType = rec;
1133}
1134
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001135void ASTContext::setObjcSelType(TypedefDecl *TD)
1136{
1137 assert(ObjcSelType.isNull() && "'SEL' type already set!");
1138
1139 ObjcSelType = getTypedefType(TD);
1140
1141 // typedef struct objc_selector *SEL;
1142 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1143 assert(ptr && "'SEL' incorrectly typed");
1144 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1145 assert(rec && "'SEL' incorrectly typed");
1146 SelStructType = rec;
1147}
1148
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +00001149void ASTContext::setObjcProtoType(QualType QT)
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001150{
1151 assert(ObjcProtoType.isNull() && "'Protocol' type already set!");
Fariborz Jahanian66c5dfc2007-12-07 00:18:54 +00001152 ObjcProtoType = QT;
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001153}
1154
Anders Carlsson8baaca52007-10-31 02:53:19 +00001155void ASTContext::setObjcClassType(TypedefDecl *TD)
1156{
1157 assert(ObjcClassType.isNull() && "'Class' type already set!");
1158
1159 ObjcClassType = getTypedefType(TD);
1160
1161 // typedef struct objc_class *Class;
1162 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1163 assert(ptr && "'Class' incorrectly typed");
1164 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1165 assert(rec && "'Class' incorrectly typed");
1166 ClassStructType = rec;
1167}
1168
Steve Naroff21988912007-10-15 23:35:17 +00001169void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) {
1170 assert(ObjcConstantStringType.isNull() &&
1171 "'NSConstantString' type already set!");
1172
1173 ObjcConstantStringType = getObjcInterfaceType(Decl);
1174}
1175
Steve Naroffec0550f2007-10-15 20:41:53 +00001176bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1177 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1178 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1179
1180 return lBuiltin->getKind() == rBuiltin->getKind();
1181}
1182
1183
1184bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
1185 if (lhs->isObjcInterfaceType() && isObjcIdType(rhs))
1186 return true;
1187 else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType())
1188 return true;
1189 return false;
1190}
1191
1192bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
1193 return true; // FIXME: IMPLEMENT.
1194}
1195
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001196bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1197 QualType rhs) {
1198 ObjcQualifiedInterfaceType *lhsQI =
1199 dyn_cast<ObjcQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
1200 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
1201 ObjcQualifiedInterfaceType *rhsQI =
1202 dyn_cast<ObjcQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
1203 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanian06cef252007-12-13 20:47:42 +00001204 if (!interfaceTypesAreCompatible(getObjcInterfaceType(lhsQI->getDecl()),
1205 getObjcInterfaceType(rhsQI->getDecl())))
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001206 return false;
1207 /* All protocols in lhs must have a presense in rhs. */
1208 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1209 bool match = false;
1210 ObjcProtocolDecl *lhsProto = lhsQI->getProtocols(i);
1211 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
1212 ObjcProtocolDecl *rhsProto = rhsQI->getProtocols(j);
1213 if (lhsProto == rhsProto) {
1214 match = true;
1215 break;
1216 }
1217 }
1218 if (!match)
1219 return false;
1220 }
1221 return true;
1222}
1223
Chris Lattner770951b2007-11-01 05:03:41 +00001224bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1225 const VectorType *lVector = lhs->getAsVectorType();
1226 const VectorType *rVector = rhs->getAsVectorType();
1227
1228 if ((lVector->getElementType().getCanonicalType() ==
1229 rVector->getElementType().getCanonicalType()) &&
1230 (lVector->getNumElements() == rVector->getNumElements()))
1231 return true;
1232 return false;
1233}
1234
Steve Naroffec0550f2007-10-15 20:41:53 +00001235// C99 6.2.7p1: If both are complete types, then the following additional
1236// requirements apply...FIXME (handle compatibility across source files).
1237bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
1238 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
1239 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
1240
1241 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
1242 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1243 return true;
1244 }
1245 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
1246 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1247 return true;
1248 }
Steve Naroffab373092007-11-07 06:03:51 +00001249 // "Class" and "id" are compatible built-in structure types.
1250 if (isObjcIdType(lhs) && isObjcClassType(rhs) ||
1251 isObjcClassType(lhs) && isObjcIdType(rhs))
1252 return true;
Steve Naroffec0550f2007-10-15 20:41:53 +00001253 return false;
1254}
1255
1256bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1257 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1258 // identically qualified and both shall be pointers to compatible types.
1259 if (lhs.getQualifiers() != rhs.getQualifiers())
1260 return false;
1261
1262 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1263 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1264
1265 return typesAreCompatible(ltype, rtype);
1266}
1267
Bill Wendling43d69752007-12-03 07:33:35 +00001268// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroffec0550f2007-10-15 20:41:53 +00001269// reference to T, the operation assigns to the object of type T denoted by the
1270// reference.
1271bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1272 QualType ltype = lhs;
1273
1274 if (lhs->isReferenceType())
1275 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1276
1277 QualType rtype = rhs;
1278
1279 if (rhs->isReferenceType())
1280 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1281
1282 return typesAreCompatible(ltype, rtype);
1283}
1284
1285bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1286 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1287 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1288 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1289 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1290
1291 // first check the return types (common between C99 and K&R).
1292 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1293 return false;
1294
1295 if (lproto && rproto) { // two C99 style function prototypes
1296 unsigned lproto_nargs = lproto->getNumArgs();
1297 unsigned rproto_nargs = rproto->getNumArgs();
1298
1299 if (lproto_nargs != rproto_nargs)
1300 return false;
1301
1302 // both prototypes have the same number of arguments.
1303 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1304 (rproto->isVariadic() && !lproto->isVariadic()))
1305 return false;
1306
1307 // The use of ellipsis agree...now check the argument types.
1308 for (unsigned i = 0; i < lproto_nargs; i++)
1309 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
1310 return false;
1311 return true;
1312 }
1313 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1314 return true;
1315
1316 // we have a mixture of K&R style with C99 prototypes
1317 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1318
1319 if (proto->isVariadic())
1320 return false;
1321
1322 // FIXME: Each parameter type T in the prototype must be compatible with the
1323 // type resulting from applying the usual argument conversions to T.
1324 return true;
1325}
1326
1327bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
1328 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
1329 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
1330
1331 if (!typesAreCompatible(ltype, rtype))
1332 return false;
1333
1334 // FIXME: If both types specify constant sizes, then the sizes must also be
1335 // the same. Even if the sizes are the same, GCC produces an error.
1336 return true;
1337}
1338
1339/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1340/// both shall have the identically qualified version of a compatible type.
1341/// C99 6.2.7p1: Two types have compatible types if their types are the
1342/// same. See 6.7.[2,3,5] for additional rules.
1343bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
1344 QualType lcanon = lhs.getCanonicalType();
1345 QualType rcanon = rhs.getCanonicalType();
1346
1347 // If two types are identical, they are are compatible
1348 if (lcanon == rcanon)
1349 return true;
Bill Wendling43d69752007-12-03 07:33:35 +00001350
1351 // C++ [expr]: If an expression initially has the type "reference to T", the
1352 // type is adjusted to "T" prior to any further analysis, the expression
1353 // designates the object or function denoted by the reference, and the
1354 // expression is an lvalue.
1355 if (lcanon->getTypeClass() == Type::Reference)
1356 lcanon = cast<ReferenceType>(lcanon)->getReferenceeType();
1357 if (rcanon->getTypeClass() == Type::Reference)
1358 rcanon = cast<ReferenceType>(rcanon)->getReferenceeType();
Steve Naroffec0550f2007-10-15 20:41:53 +00001359
1360 // If the canonical type classes don't match, they can't be compatible
1361 if (lcanon->getTypeClass() != rcanon->getTypeClass()) {
1362 // For Objective-C, it is possible for two types to be compatible
1363 // when their classes don't match (when dealing with "id"). If either type
1364 // is an interface, we defer to objcTypesAreCompatible().
1365 if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType())
1366 return objcTypesAreCompatible(lcanon, rcanon);
1367 return false;
1368 }
1369 switch (lcanon->getTypeClass()) {
1370 case Type::Pointer:
1371 return pointerTypesAreCompatible(lcanon, rcanon);
Steve Naroffec0550f2007-10-15 20:41:53 +00001372 case Type::ConstantArray:
1373 case Type::VariableArray:
1374 return arrayTypesAreCompatible(lcanon, rcanon);
1375 case Type::FunctionNoProto:
1376 case Type::FunctionProto:
1377 return functionTypesAreCompatible(lcanon, rcanon);
1378 case Type::Tagged: // handle structures, unions
1379 return tagTypesAreCompatible(lcanon, rcanon);
1380 case Type::Builtin:
1381 return builtinTypesAreCompatible(lcanon, rcanon);
1382 case Type::ObjcInterface:
1383 return interfaceTypesAreCompatible(lcanon, rcanon);
Chris Lattner770951b2007-11-01 05:03:41 +00001384 case Type::Vector:
1385 case Type::OCUVector:
1386 return vectorTypesAreCompatible(lcanon, rcanon);
Fariborz Jahanian4ffc5412007-12-12 01:00:23 +00001387 case Type::ObjcQualifiedInterface:
1388 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
Steve Naroffec0550f2007-10-15 20:41:53 +00001389 default:
1390 assert(0 && "unexpected type");
1391 }
1392 return true; // should never get here...
1393}
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001394
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001395/// Emit - Serialize an ASTContext object to Bitcode.
1396void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek54513502007-10-31 20:00:03 +00001397 S.EmitRef(SourceMgr);
1398 S.EmitRef(Target);
1399 S.EmitRef(Idents);
1400 S.EmitRef(Selectors);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001401
Ted Kremenekfee04522007-10-31 22:44:07 +00001402 // Emit the size of the type vector so that we can reserve that size
1403 // when we reconstitute the ASTContext object.
Ted Kremeneka4559c32007-11-06 22:26:16 +00001404 S.EmitInt(Types.size());
1405
Ted Kremenek03ed4402007-11-13 22:02:55 +00001406 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1407 I!=E;++I)
1408 (*I)->Emit(S);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001409
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001410 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek7192f8e2007-10-31 17:10:13 +00001411}
1412
Ted Kremenek0f84c002007-11-13 00:25:37 +00001413ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenekfee04522007-10-31 22:44:07 +00001414 SourceManager &SM = D.ReadRef<SourceManager>();
1415 TargetInfo &t = D.ReadRef<TargetInfo>();
1416 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1417 SelectorTable &sels = D.ReadRef<SelectorTable>();
1418
1419 unsigned size_reserve = D.ReadInt();
1420
1421 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1422
Ted Kremenek03ed4402007-11-13 22:02:55 +00001423 for (unsigned i = 0; i < size_reserve; ++i)
1424 Type::Create(*A,i,D);
Ted Kremeneka4559c32007-11-06 22:26:16 +00001425
Ted Kremeneka9a4a242007-11-01 18:11:32 +00001426 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenekfee04522007-10-31 22:44:07 +00001427
1428 return A;
1429}