blob: 31d9255a4bae54b2bc91e9afbe3b89e10978571b [file] [log] [blame]
Chris Lattnerddc135e2006-11-10 06:34:16 +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"
Chris Lattnerd0342e52006-11-20 04:02:15 +000015#include "clang/AST/Decl.h"
Steve Naroff67391b82007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Chris Lattner4dc8a6f2007-05-20 23:50:58 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattnerc6ad8132006-12-02 07:52:18 +000018#include "llvm/ADT/SmallVector.h"
Anders Carlssond8499822007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenekfc581a92007-10-31 17:10:13 +000020#include "llvm/Bitcode/Serialize.h"
21#include "llvm/Bitcode/Deserialize.h"
Anders Carlssond8499822007-10-29 05:01:08 +000022
Chris Lattnerddc135e2006-11-10 06:34:16 +000023using namespace clang;
24
Steve Naroff0af91202007-04-27 21:51:21 +000025enum FloatingRank {
26 FloatRank, DoubleRank, LongDoubleRank
27};
28
Chris Lattnerd5973eb2006-11-12 00:53:46 +000029ASTContext::~ASTContext() {
30 // Deallocate all the types.
31 while (!Types.empty()) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +000032 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 }
Chris Lattnerd5973eb2006-11-12 00:53:46 +000039 Types.pop_back();
40 }
41}
42
Chris Lattner4eb445d2007-01-26 01:27:23 +000043void 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 Lattner9c016772007-07-18 05:50:59 +000047 unsigned NumVector = 0, NumComplex = 0;
Bill Wendling3708c182007-05-27 10:15:43 +000048 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Chris Lattner4eb445d2007-01-26 01:27:23 +000049
50 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Chris Lattner6d513f32007-12-12 06:43:05 +000051 unsigned NumObjcInterfaces = 0, NumObjcQualifiedInterfaces = 0;
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +000052 unsigned NumObjcQualifiedIds = 0;
Chris Lattner4eb445d2007-01-26 01:27:23 +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;
Bill Wendling3708c182007-05-27 10:15:43 +000060 else if (isa<ReferenceType>(T))
61 ++NumReference;
Chris Lattner9c016772007-07-18 05:50:59 +000062 else if (isa<ComplexType>(T))
63 ++NumComplex;
Chris Lattner4eb445d2007-01-26 01:27:23 +000064 else if (isa<ArrayType>(T))
65 ++NumArray;
Chris Lattner9c016772007-07-18 05:50:59 +000066 else if (isa<VectorType>(T))
67 ++NumVector;
Chris Lattner4eb445d2007-01-26 01:27:23 +000068 else if (isa<FunctionTypeNoProto>(T))
69 ++NumFunctionNP;
70 else if (isa<FunctionTypeProto>(T))
71 ++NumFunctionP;
Chris Lattner32d920b2007-01-26 02:01:53 +000072 else if (isa<TypedefType>(T))
Chris Lattner4eb445d2007-01-26 01:27:23 +000073 ++NumTypeName;
Steve Narofff1e53692007-03-23 22:27:02 +000074 else if (TagType *TT = dyn_cast<TagType>(T)) {
Chris Lattner4eb445d2007-01-26 01:27:23 +000075 ++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 }
Steve Naroff73d534a2007-09-17 14:16:13 +000083 } else if (isa<ObjcInterfaceType>(T))
84 ++NumObjcInterfaces;
Chris Lattner6d513f32007-12-12 06:43:05 +000085 else if (isa<ObjcQualifiedInterfaceType>(T))
86 ++NumObjcQualifiedInterfaces;
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +000087 else if (isa<ObjcQualifiedIdType>(T))
88 ++NumObjcQualifiedIds;
Steve Naroff73d534a2007-09-17 14:16:13 +000089 else {
Chris Lattner6d513f32007-12-12 06:43:05 +000090 QualType(T, 0).dump();
Chris Lattner4eb445d2007-01-26 01:27:23 +000091 assert(0 && "Unknown type!");
92 }
93 }
94
95 fprintf(stderr, " %d builtin types\n", NumBuiltin);
96 fprintf(stderr, " %d pointer types\n", NumPointer);
Bill Wendling3708c182007-05-27 10:15:43 +000097 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner9c016772007-07-18 05:50:59 +000098 fprintf(stderr, " %d complex types\n", NumComplex);
Chris Lattner4eb445d2007-01-26 01:27:23 +000099 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner9c016772007-07-18 05:50:59 +0000100 fprintf(stderr, " %d vector types\n", NumVector);
Chris Lattner4eb445d2007-01-26 01:27:23 +0000101 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);
Steve Naroff73d534a2007-09-17 14:16:13 +0000109 fprintf(stderr, " %d interface types\n", NumObjcInterfaces);
Chris Lattner6d513f32007-12-12 06:43:05 +0000110 fprintf(stderr, " %d protocol qualified interface types\n",
111 NumObjcQualifiedInterfaces);
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +0000112 fprintf(stderr, " %d protocol qualified id types\n",
113 NumObjcQualifiedIds);
Chris Lattnerfc234de2007-05-24 00:40:54 +0000114 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
Steve Narofff84d11f2007-05-23 21:48:04 +0000115 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner9c016772007-07-18 05:50:59 +0000116 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Chris Lattnerfc234de2007-05-24 00:40:54 +0000117 NumFunctionP*sizeof(FunctionTypeProto)+
118 NumFunctionNP*sizeof(FunctionTypeNoProto)+
119 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
Chris Lattner4eb445d2007-01-26 01:27:23 +0000120}
121
122
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000123void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
124 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000125}
126
Chris Lattner970e54e2006-11-12 00:37:36 +0000127void ASTContext::InitBuiltinTypes() {
128 assert(VoidTy.isNull() && "Context reinitialized?");
129
130 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000131 InitBuiltinType(VoidTy, BuiltinType::Void);
Chris Lattner970e54e2006-11-12 00:37:36 +0000132
133 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000134 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000135 // C99 6.2.5p3.
Ted Kremenek1daa3cf2007-12-12 22:39:36 +0000136 if (Target.isCharSigned(FullSourceLoc()))
Chris Lattnerb16f4552007-06-03 07:25:34 +0000137 InitBuiltinType(CharTy, BuiltinType::Char_S);
138 else
139 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000140 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000141 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
142 InitBuiltinType(ShortTy, BuiltinType::Short);
143 InitBuiltinType(IntTy, BuiltinType::Int);
144 InitBuiltinType(LongTy, BuiltinType::Long);
145 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000146
147 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000148 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
149 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
150 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
151 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
152 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000153
154 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000155 InitBuiltinType(FloatTy, BuiltinType::Float);
156 InitBuiltinType(DoubleTy, BuiltinType::Double);
157 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Chris Lattner970e54e2006-11-12 00:37:36 +0000158
159 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000160 FloatComplexTy = getComplexType(FloatTy);
161 DoubleComplexTy = getComplexType(DoubleTy);
162 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff66e9f332007-10-15 14:41:52 +0000163
164 BuiltinVaListType = QualType();
165 ObjcIdType = QualType();
166 IdStructType = 0;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +0000167 ObjcClassType = QualType();
168 ClassStructType = 0;
169
Steve Narofff73b7842007-10-15 23:35:17 +0000170 ObjcConstantStringType = QualType();
Fariborz Jahanian797f24c2007-10-29 22:57:28 +0000171
172 // void * type
173 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner970e54e2006-11-12 00:37:36 +0000174}
175
Chris Lattner53cfe802007-07-18 17:52:12 +0000176//===----------------------------------------------------------------------===//
177// Type Sizing and Analysis
178//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000179
180/// getTypeSize - Return the size of the specified type, in bits. This method
181/// does not work on incomplete types.
Chris Lattner4481b422007-07-14 01:29:45 +0000182std::pair<uint64_t, unsigned>
183ASTContext::getTypeInfo(QualType T, SourceLocation L) {
Chris Lattner983a8bb2007-07-13 22:13:22 +0000184 T = T.getCanonicalType();
Chris Lattner4481b422007-07-14 01:29:45 +0000185 uint64_t Size;
186 unsigned Align;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000187 switch (T->getTypeClass()) {
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000188 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner355332d2007-07-13 22:27:08 +0000189 case Type::FunctionNoProto:
190 case Type::FunctionProto:
Chris Lattner647fb222007-07-18 18:26:58 +0000191 default:
Chris Lattnerab41b132007-07-20 18:13:33 +0000192 assert(0 && "Incomplete types have no size!");
Steve Naroff5c131802007-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 Lattnerf2e101f2007-07-19 22:06:24 +0000198 std::pair<uint64_t, unsigned> EltInfo =
Steve Naroff5c131802007-08-30 01:06:46 +0000199 getTypeInfo(CAT->getElementType(), L);
200 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000201 Align = EltInfo.second;
202 break;
203 }
204 case Type::Vector: {
205 std::pair<uint64_t, unsigned> EltInfo =
206 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
207 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
208 // FIXME: Vector alignment is not the alignment of its elements.
209 Align = EltInfo.second;
210 break;
211 }
Chris Lattner647fb222007-07-18 18:26:58 +0000212
Chris Lattner983a8bb2007-07-13 22:13:22 +0000213 case Type::Builtin: {
214 // FIXME: need to use TargetInfo to derive the target specific sizes. This
215 // implementation will suffice for play with vector support.
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000216 const llvm::fltSemantics *F;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000217 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000218 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000219 case BuiltinType::Void:
220 assert(0 && "Incomplete types have no size!");
Chris Lattner6a4f7452007-12-19 19:23:28 +0000221 case BuiltinType::Bool:
222 Target.getBoolInfo(Size, Align, getFullLoc(L));
223 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000224 case BuiltinType::Char_S:
225 case BuiltinType::Char_U:
226 case BuiltinType::UChar:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000227 case BuiltinType::SChar:
228 Target.getCharInfo(Size, Align, getFullLoc(L));
229 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000230 case BuiltinType::UShort:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000231 case BuiltinType::Short:
232 Target.getShortInfo(Size, Align, getFullLoc(L));
233 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000234 case BuiltinType::UInt:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000235 case BuiltinType::Int:
236 Target.getIntInfo(Size, Align, getFullLoc(L));
237 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000238 case BuiltinType::ULong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000239 case BuiltinType::Long:
240 Target.getLongInfo(Size, Align, getFullLoc(L));
241 break;
Chris Lattner355332d2007-07-13 22:27:08 +0000242 case BuiltinType::ULongLong:
Chris Lattner6a4f7452007-12-19 19:23:28 +0000243 case BuiltinType::LongLong:
244 Target.getLongLongInfo(Size, Align, getFullLoc(L));
245 break;
246 case BuiltinType::Float:
247 Target.getFloatInfo(Size, Align, F, getFullLoc(L));
248 break;
249 case BuiltinType::Double:
250 Target.getDoubleInfo(Size, Align, F, getFullLoc(L));
251 break;
252 case BuiltinType::LongDouble:
253 Target.getLongDoubleInfo(Size, Align, F, getFullLoc(L));
254 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000255 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000256 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000257 }
Chris Lattner6a4f7452007-12-19 19:23:28 +0000258 case Type::ObjcQualifiedId:
259 Target.getPointerInfo(Size, Align, getFullLoc(L));
260 break;
261 case Type::Pointer:
262 Target.getPointerInfo(Size, Align, getFullLoc(L));
263 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000264 case Type::Reference:
Chris Lattner0523dc92007-07-13 22:16:13 +0000265 // "When applied to a reference or a reference type, the result is the size
Chris Lattner647fb222007-07-18 18:26:58 +0000266 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattner6a4f7452007-12-19 19:23:28 +0000267 // FIXME: This is wrong for struct layout: a reference in a struct has
268 // pointer size.
Chris Lattner4481b422007-07-14 01:29:45 +0000269 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner647fb222007-07-18 18:26:58 +0000270
271 case Type::Complex: {
272 // Complex types have the same alignment as their elements, but twice the
273 // size.
274 std::pair<uint64_t, unsigned> EltInfo =
275 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
276 Size = EltInfo.first*2;
277 Align = EltInfo.second;
278 break;
279 }
280 case Type::Tagged:
Chris Lattnera9e79612007-08-27 17:38:00 +0000281 TagType *TT = cast<TagType>(T);
282 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
Devang Patele11664a2007-11-01 19:11:01 +0000283 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl(), L);
Chris Lattnera9e79612007-08-27 17:38:00 +0000284 Size = Layout.getSize();
285 Align = Layout.getAlignment();
286 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattner1c1f9322007-08-28 18:24:31 +0000287 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattnera9e79612007-08-27 17:38:00 +0000288 } else {
Chris Lattner49a953a2007-07-23 22:46:22 +0000289 assert(0 && "Unimplemented type sizes!");
Chris Lattnera9e79612007-08-27 17:38:00 +0000290 }
Chris Lattner49a953a2007-07-23 22:46:22 +0000291 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000292 }
Chris Lattner4481b422007-07-14 01:29:45 +0000293
Chris Lattner53cfe802007-07-18 17:52:12 +0000294 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner4481b422007-07-14 01:29:45 +0000295 return std::make_pair(Size, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000296}
297
Devang Patele11664a2007-11-01 19:11:01 +0000298/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner53cfe802007-07-18 17:52:12 +0000299/// specified record (struct/union/class), which indicates its size and field
300/// position information.
Devang Patele11664a2007-11-01 19:11:01 +0000301const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D,
302 SourceLocation L) {
Chris Lattner53cfe802007-07-18 17:52:12 +0000303 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
304
305 // Look up this layout, if already laid out, return what we have.
Devang Patele11664a2007-11-01 19:11:01 +0000306 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner53cfe802007-07-18 17:52:12 +0000307 if (Entry) return *Entry;
308
Devang Patele11664a2007-11-01 19:11:01 +0000309 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
310 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
311 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner53cfe802007-07-18 17:52:12 +0000312 Entry = NewEntry;
313
314 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
315 uint64_t RecordSize = 0;
316 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
317
318 if (D->getKind() != Decl::Union) {
319 // Layout each field, for now, just sequentially, respecting alignment. In
320 // the future, this will need to be tweakable by targets.
321 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
322 const FieldDecl *FD = D->getMember(i);
323 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
324 uint64_t FieldSize = FieldInfo.first;
325 unsigned FieldAlign = FieldInfo.second;
326
327 // Round up the current record size to the field's alignment boundary.
328 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
329
330 // Place this field at the current location.
331 FieldOffsets[i] = RecordSize;
332
333 // Reserve space for this field.
334 RecordSize += FieldSize;
335
336 // Remember max struct/class alignment.
337 RecordAlign = std::max(RecordAlign, FieldAlign);
338 }
339
340 // Finally, round the size of the total struct up to the alignment of the
341 // struct itself.
342 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
343 } else {
344 // Union layout just puts each member at the start of the record.
345 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
346 const FieldDecl *FD = D->getMember(i);
347 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
348 uint64_t FieldSize = FieldInfo.first;
349 unsigned FieldAlign = FieldInfo.second;
350
351 // Round up the current record size to the field's alignment boundary.
352 RecordSize = std::max(RecordSize, FieldSize);
353
354 // Place this field at the start of the record.
355 FieldOffsets[i] = 0;
356
357 // Remember max struct/class alignment.
358 RecordAlign = std::max(RecordAlign, FieldAlign);
359 }
360 }
Chris Lattner647fb222007-07-18 18:26:58 +0000361
362 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
363 return *NewEntry;
Chris Lattner53cfe802007-07-18 17:52:12 +0000364}
365
Chris Lattner983a8bb2007-07-13 22:13:22 +0000366//===----------------------------------------------------------------------===//
367// Type creation/memoization methods
368//===----------------------------------------------------------------------===//
369
370
Chris Lattnerc6395932007-06-22 20:56:16 +0000371/// getComplexType - Return the uniqued reference to the type for a complex
372/// number with the specified element type.
373QualType ASTContext::getComplexType(QualType T) {
374 // Unique pointers, to guarantee there is only one pointer of a particular
375 // structure.
376 llvm::FoldingSetNodeID ID;
377 ComplexType::Profile(ID, T);
378
379 void *InsertPos = 0;
380 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
381 return QualType(CT, 0);
382
383 // If the pointee type isn't canonical, this won't be a canonical type either,
384 // so fill in the canonical type field.
385 QualType Canonical;
386 if (!T->isCanonical()) {
387 Canonical = getComplexType(T.getCanonicalType());
388
389 // Get the new insert position for the node we care about.
390 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
391 assert(NewIP == 0 && "Shouldn't be in the map!");
392 }
393 ComplexType *New = new ComplexType(T, Canonical);
394 Types.push_back(New);
395 ComplexTypes.InsertNode(New, InsertPos);
396 return QualType(New, 0);
397}
398
399
Chris Lattner970e54e2006-11-12 00:37:36 +0000400/// getPointerType - Return the uniqued reference to the type for a pointer to
401/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000402QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000403 // Unique pointers, to guarantee there is only one pointer of a particular
404 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000405 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +0000406 PointerType::Profile(ID, T);
407
408 void *InsertPos = 0;
409 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000410 return QualType(PT, 0);
Chris Lattner970e54e2006-11-12 00:37:36 +0000411
Chris Lattner7ccecb92006-11-12 08:50:50 +0000412 // If the pointee type isn't canonical, this won't be a canonical type either,
413 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000414 QualType Canonical;
Chris Lattner67521df2007-01-27 01:29:36 +0000415 if (!T->isCanonical()) {
Steve Naroffd50c88e2007-04-05 21:15:20 +0000416 Canonical = getPointerType(T.getCanonicalType());
Chris Lattner67521df2007-01-27 01:29:36 +0000417
418 // Get the new insert position for the node we care about.
419 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
420 assert(NewIP == 0 && "Shouldn't be in the map!");
421 }
Chris Lattner67521df2007-01-27 01:29:36 +0000422 PointerType *New = new PointerType(T, Canonical);
423 Types.push_back(New);
424 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000425 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +0000426}
427
Bill Wendling3708c182007-05-27 10:15:43 +0000428/// getReferenceType - Return the uniqued reference to the type for a reference
429/// to the specified type.
430QualType ASTContext::getReferenceType(QualType T) {
431 // Unique pointers, to guarantee there is only one pointer of a particular
432 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000433 llvm::FoldingSetNodeID ID;
Bill Wendling3708c182007-05-27 10:15:43 +0000434 ReferenceType::Profile(ID, T);
435
436 void *InsertPos = 0;
437 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
438 return QualType(RT, 0);
439
440 // If the referencee type isn't canonical, this won't be a canonical type
441 // either, so fill in the canonical type field.
442 QualType Canonical;
443 if (!T->isCanonical()) {
444 Canonical = getReferenceType(T.getCanonicalType());
445
446 // Get the new insert position for the node we care about.
447 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
448 assert(NewIP == 0 && "Shouldn't be in the map!");
449 }
450
451 ReferenceType *New = new ReferenceType(T, Canonical);
452 Types.push_back(New);
453 ReferenceTypes.InsertNode(New, InsertPos);
454 return QualType(New, 0);
455}
456
Steve Naroff5c131802007-08-30 01:06:46 +0000457/// getConstantArrayType - Return the unique reference to the type for an
458/// array of the specified element type.
459QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff90dfdd52007-08-30 18:10:14 +0000460 const llvm::APInt &ArySize,
461 ArrayType::ArraySizeModifier ASM,
462 unsigned EltTypeQuals) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000463 llvm::FoldingSetNodeID ID;
Steve Naroff5c131802007-08-30 01:06:46 +0000464 ConstantArrayType::Profile(ID, EltTy, ArySize);
Steve Naroff408451b2007-02-26 22:17:12 +0000465
Chris Lattner36f8e652007-01-27 08:31:04 +0000466 void *InsertPos = 0;
Ted Kremenekfc581a92007-10-31 17:10:13 +0000467 if (ConstantArrayType *ATP =
468 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000469 return QualType(ATP, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000470
471 // If the element type isn't canonical, this won't be a canonical type either,
472 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000473 QualType Canonical;
Chris Lattner36f8e652007-01-27 08:31:04 +0000474 if (!EltTy->isCanonical()) {
Steve Naroff90dfdd52007-08-30 18:10:14 +0000475 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
476 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +0000477 // Get the new insert position for the node we care about.
Ted Kremenekfc581a92007-10-31 17:10:13 +0000478 ConstantArrayType *NewIP =
479 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
480
Chris Lattner36f8e652007-01-27 08:31:04 +0000481 assert(NewIP == 0 && "Shouldn't be in the map!");
482 }
Chris Lattner7ccecb92006-11-12 08:50:50 +0000483
Steve Naroff90dfdd52007-08-30 18:10:14 +0000484 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
485 ASM, EltTypeQuals);
Ted Kremenekfc581a92007-10-31 17:10:13 +0000486 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +0000487 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000488 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000489}
490
Steve Naroffcadebd02007-08-30 18:14:25 +0000491/// getVariableArrayType - Returns a non-unique reference to the type for a
492/// variable array of the specified element type.
Steve Naroff90dfdd52007-08-30 18:10:14 +0000493QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
494 ArrayType::ArraySizeModifier ASM,
495 unsigned EltTypeQuals) {
Ted Kremenek843ebedd2007-10-29 23:37:31 +0000496 if (NumElts) {
497 // Since we don't unique expressions, it isn't possible to unique VLA's
498 // that have an expression provided for their size.
499
Ted Kremenek91393102007-10-30 16:41:53 +0000500 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
501 ASM, EltTypeQuals);
Ted Kremenek843ebedd2007-10-29 23:37:31 +0000502
Ted Kremenek91393102007-10-30 16:41:53 +0000503 CompleteVariableArrayTypes.push_back(New);
Ted Kremenek843ebedd2007-10-29 23:37:31 +0000504 Types.push_back(New);
505 return QualType(New, 0);
506 }
507 else {
508 // No size is provided for the VLA. These we can unique.
509 llvm::FoldingSetNodeID ID;
510 VariableArrayType::Profile(ID, EltTy);
511
512 void *InsertPos = 0;
513 if (VariableArrayType *ATP =
514 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
515 return QualType(ATP, 0);
516
517 // If the element type isn't canonical, this won't be a canonical type
518 // either, so fill in the canonical type field.
519 QualType Canonical;
520
521 if (!EltTy->isCanonical()) {
522 Canonical = getVariableArrayType(EltTy.getCanonicalType(), NumElts,
523 ASM, EltTypeQuals);
524
525 // Get the new insert position for the node we care about.
526 VariableArrayType *NewIP =
527 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
528
529 assert(NewIP == 0 && "Shouldn't be in the map!");
530 }
531
532 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
533 ASM, EltTypeQuals);
534
535 IncompleteVariableArrayTypes.InsertNode(New, InsertPos);
536 Types.push_back(New);
537 return QualType(New, 0);
538 }
Steve Naroff5c131802007-08-30 01:06:46 +0000539}
540
Steve Naroff91fcddb2007-07-18 18:00:27 +0000541/// getVectorType - Return the unique reference to a vector type of
542/// the specified element type and size. VectorType must be a built-in type.
543QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000544 BuiltinType *baseType;
545
546 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff91fcddb2007-07-18 18:00:27 +0000547 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000548
549 // Check if we've already instantiated a vector of this type.
550 llvm::FoldingSetNodeID ID;
Steve Naroff91fcddb2007-07-18 18:00:27 +0000551 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000552 void *InsertPos = 0;
553 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
554 return QualType(VTP, 0);
555
556 // If the element type isn't canonical, this won't be a canonical type either,
557 // so fill in the canonical type field.
558 QualType Canonical;
559 if (!vecType->isCanonical()) {
Steve Naroff91fcddb2007-07-18 18:00:27 +0000560 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000561
562 // Get the new insert position for the node we care about.
563 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
564 assert(NewIP == 0 && "Shouldn't be in the map!");
565 }
566 VectorType *New = new VectorType(vecType, NumElts, Canonical);
567 VectorTypes.InsertNode(New, InsertPos);
568 Types.push_back(New);
569 return QualType(New, 0);
570}
571
Steve Naroff91fcddb2007-07-18 18:00:27 +0000572/// getOCUVectorType - Return the unique reference to an OCU vector type of
573/// the specified element type and size. VectorType must be a built-in type.
574QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
575 BuiltinType *baseType;
576
577 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
578 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
579
580 // Check if we've already instantiated a vector of this type.
581 llvm::FoldingSetNodeID ID;
582 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
583 void *InsertPos = 0;
584 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
585 return QualType(VTP, 0);
586
587 // If the element type isn't canonical, this won't be a canonical type either,
588 // so fill in the canonical type field.
589 QualType Canonical;
590 if (!vecType->isCanonical()) {
591 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
592
593 // Get the new insert position for the node we care about.
594 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
595 assert(NewIP == 0 && "Shouldn't be in the map!");
596 }
597 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
598 VectorTypes.InsertNode(New, InsertPos);
599 Types.push_back(New);
600 return QualType(New, 0);
601}
602
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000603/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
604///
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000605QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000606 // Unique functions, to guarantee there is only one function of a particular
607 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000608 llvm::FoldingSetNodeID ID;
Chris Lattner47955de2007-01-27 08:37:20 +0000609 FunctionTypeNoProto::Profile(ID, ResultTy);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000610
Chris Lattner47955de2007-01-27 08:37:20 +0000611 void *InsertPos = 0;
612 if (FunctionTypeNoProto *FT =
613 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000614 return QualType(FT, 0);
Chris Lattner47955de2007-01-27 08:37:20 +0000615
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000616 QualType Canonical;
Chris Lattner47955de2007-01-27 08:37:20 +0000617 if (!ResultTy->isCanonical()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000618 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
Chris Lattner47955de2007-01-27 08:37:20 +0000619
620 // Get the new insert position for the node we care about.
621 FunctionTypeNoProto *NewIP =
622 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
623 assert(NewIP == 0 && "Shouldn't be in the map!");
624 }
625
626 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
627 Types.push_back(New);
628 FunctionTypeProtos.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000629 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000630}
631
632/// getFunctionType - Return a normal function type with a typed argument
633/// list. isVariadic indicates whether the argument list includes '...'.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000634QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
635 unsigned NumArgs, bool isVariadic) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000636 // Unique functions, to guarantee there is only one function of a particular
637 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000638 llvm::FoldingSetNodeID ID;
Chris Lattnerfd4de792007-01-27 01:15:32 +0000639 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
640
641 void *InsertPos = 0;
642 if (FunctionTypeProto *FTP =
643 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000644 return QualType(FTP, 0);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000645
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000646 // Determine whether the type being created is already canonical or not.
647 bool isCanonical = ResultTy->isCanonical();
648 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
649 if (!ArgArray[i]->isCanonical())
650 isCanonical = false;
651
652 // If this type isn't canonical, get the canonical version of it.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000653 QualType Canonical;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000654 if (!isCanonical) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000655 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000656 CanonicalArgs.reserve(NumArgs);
657 for (unsigned i = 0; i != NumArgs; ++i)
658 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
659
660 Canonical = getFunctionType(ResultTy.getCanonicalType(),
661 &CanonicalArgs[0], NumArgs,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000662 isVariadic);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000663
664 // Get the new insert position for the node we care about.
665 FunctionTypeProto *NewIP =
666 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
667 assert(NewIP == 0 && "Shouldn't be in the map!");
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000668 }
669
670 // FunctionTypeProto objects are not allocated with new because they have a
671 // variable size array (for parameter types) at the end of them.
672 FunctionTypeProto *FTP =
673 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner98021442007-07-20 18:48:28 +0000674 NumArgs*sizeof(QualType));
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000675 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
676 Canonical);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000677 Types.push_back(FTP);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000678 FunctionTypeProtos.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000679 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000680}
Chris Lattneref51c202006-11-10 07:17:23 +0000681
Chris Lattner32d920b2007-01-26 02:01:53 +0000682/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +0000683/// specified typename decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000684QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
685 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000686
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000687 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +0000688 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattnercceab1a2007-03-26 20:16:44 +0000689 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000690 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000691}
692
Steve Naroff09bf8152007-09-06 21:24:23 +0000693/// getObjcInterfaceType - Return the unique reference to the type for the
694/// specified ObjC interface decl.
695QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) {
696 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
697
Fariborz Jahanianc47dc4f2007-12-13 20:47:42 +0000698 Decl->TypeForDecl = new ObjcInterfaceType(Type::ObjcInterface, Decl);
Steve Naroff09bf8152007-09-06 21:24:23 +0000699 Types.push_back(Decl->TypeForDecl);
700 return QualType(Decl->TypeForDecl, 0);
701}
702
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000703/// getObjcQualifiedInterfaceType - Return a
704/// ObjcQualifiedInterfaceType type for the given interface decl and
705/// the conforming protocol list.
706QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl,
707 ObjcProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000708 llvm::FoldingSetNodeID ID;
Fariborz Jahanianc47dc4f2007-12-13 20:47:42 +0000709 ObjcQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000710
711 void *InsertPos = 0;
712 if (ObjcQualifiedInterfaceType *QT =
713 ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
714 return QualType(QT, 0);
715
716 // No Match;
Chris Lattnerf709a142007-10-11 03:36:41 +0000717 ObjcQualifiedInterfaceType *QType =
Fariborz Jahanianc47dc4f2007-12-13 20:47:42 +0000718 new ObjcQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000719 Types.push_back(QType);
720 ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
721 return QualType(QType, 0);
722}
723
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +0000724/// getObjcQualifiedIdType - Return a
Fariborz Jahanian9b482ba2007-12-17 21:48:49 +0000725/// getObjcQualifiedIdType type for the 'id' decl and
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +0000726/// the conforming protocol list.
Fariborz Jahanian9f0e3102007-12-18 21:33:44 +0000727QualType ASTContext::getObjcQualifiedIdType(QualType idType,
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +0000728 ObjcProtocolDecl **Protocols,
729 unsigned NumProtocols) {
730 llvm::FoldingSetNodeID ID;
731 ObjcQualifiedIdType::Profile(ID, Protocols, NumProtocols);
732
733 void *InsertPos = 0;
734 if (ObjcQualifiedIdType *QT =
735 ObjcQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
736 return QualType(QT, 0);
737
738 // No Match;
Fariborz Jahanian9f0e3102007-12-18 21:33:44 +0000739 QualType Canonical;
740 if (!idType->isCanonical()) {
741 Canonical = getObjcQualifiedIdType(idType.getCanonicalType(),
742 Protocols, NumProtocols);
743 ObjcQualifiedIdType *NewQT =
744 ObjcQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
745 assert(NewQT == 0 && "Shouldn't be in the map!");
746 }
747
748 ObjcQualifiedIdType *QType =
749 new ObjcQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +0000750 Types.push_back(QType);
751 ObjcQualifiedIdTypes.InsertNode(QType, InsertPos);
752 return QualType(QType, 0);
753}
754
Steve Naroffa773cd52007-08-01 18:02:17 +0000755/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
756/// TypeOfExpr AST's (since expression's are never shared). For example,
757/// multiple declarations that refer to "typeof(x)" all contain different
758/// DeclRefExpr's. This doesn't effect the type checker, since it operates
759/// on canonical type's (which are always unique).
Steve Naroff236becb2007-08-01 17:20:42 +0000760QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffad373bd2007-07-31 12:34:36 +0000761 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroffa773cd52007-08-01 18:02:17 +0000762 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
763 Types.push_back(toe);
764 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +0000765}
766
Steve Naroffa773cd52007-08-01 18:02:17 +0000767/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
768/// TypeOfType AST's. The only motivation to unique these nodes would be
769/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
770/// an issue. This doesn't effect the type checker, since it operates
771/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +0000772QualType ASTContext::getTypeOfType(QualType tofType) {
773 QualType Canonical = tofType.getCanonicalType();
Steve Naroffa773cd52007-08-01 18:02:17 +0000774 TypeOfType *tot = new TypeOfType(tofType, Canonical);
775 Types.push_back(tot);
776 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +0000777}
778
Chris Lattnerfb072462007-01-23 05:45:31 +0000779/// getTagDeclType - Return the unique reference to the type for the
780/// specified TagDecl (struct/union/class/enum) decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000781QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenek2b0ce112007-11-26 21:16:01 +0000782 assert (Decl);
783
Ted Kremenek919858a2007-11-14 00:03:20 +0000784 // The decl stores the type cache.
Ted Kremenek2b0ce112007-11-26 21:16:01 +0000785 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenek919858a2007-11-14 00:03:20 +0000786
787 TagType* T = new TagType(Decl, QualType());
Ted Kremenek2b0ce112007-11-26 21:16:01 +0000788 Types.push_back(T);
789 Decl->TypeForDecl = T;
Ted Kremenek919858a2007-11-14 00:03:20 +0000790
791 return QualType(T, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000792}
793
Steve Naroff92e30f82007-04-02 22:35:25 +0000794/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
Steve Naroff0f6256d2007-04-02 23:01:44 +0000795/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
Steve Naroff92e30f82007-04-02 22:35:25 +0000796/// needs to agree with the definition in <stddef.h>.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000797QualType ASTContext::getSizeType() const {
Steve Naroff92e30f82007-04-02 22:35:25 +0000798 // On Darwin, size_t is defined as a "long unsigned int".
799 // FIXME: should derive from "Target".
800 return UnsignedLongTy;
801}
Chris Lattnerfb072462007-01-23 05:45:31 +0000802
Chris Lattnerd2b88ab2007-07-13 03:05:23 +0000803/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
804/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
805QualType ASTContext::getPointerDiffType() const {
806 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
807 // FIXME: should derive from "Target".
808 return IntTy;
809}
810
Steve Naroff0af91202007-04-27 21:51:21 +0000811/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
812/// routine will assert if passed a built-in type that isn't an integer or enum.
813static int getIntegerRank(QualType t) {
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000814 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
815 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
816 return 4;
Steve Naroffe4718892007-04-27 18:30:00 +0000817 }
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000818
819 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
820 switch (BT->getKind()) {
821 default:
822 assert(0 && "getIntegerRank(): not a built-in integer");
823 case BuiltinType::Bool:
824 return 1;
Chris Lattnerb16f4552007-06-03 07:25:34 +0000825 case BuiltinType::Char_S:
826 case BuiltinType::Char_U:
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000827 case BuiltinType::SChar:
828 case BuiltinType::UChar:
829 return 2;
830 case BuiltinType::Short:
831 case BuiltinType::UShort:
832 return 3;
833 case BuiltinType::Int:
834 case BuiltinType::UInt:
835 return 4;
836 case BuiltinType::Long:
837 case BuiltinType::ULong:
838 return 5;
839 case BuiltinType::LongLong:
840 case BuiltinType::ULongLong:
841 return 6;
842 }
Steve Naroffe4718892007-04-27 18:30:00 +0000843}
844
Steve Naroff0af91202007-04-27 21:51:21 +0000845/// getFloatingRank - Return a relative rank for floating point types.
846/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerc6395932007-06-22 20:56:16 +0000847static int getFloatingRank(QualType T) {
848 T = T.getCanonicalType();
849 if (ComplexType *CT = dyn_cast<ComplexType>(T))
850 return getFloatingRank(CT->getElementType());
851
852 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattnerb338a6b2007-11-01 05:03:41 +0000853 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattnerc6395932007-06-22 20:56:16 +0000854 case BuiltinType::Float: return FloatRank;
855 case BuiltinType::Double: return DoubleRank;
856 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +0000857 }
858}
859
Steve Narofffc6ffa22007-08-27 01:41:48 +0000860/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
861/// point or a complex type (based on typeDomain/typeSize).
862/// 'typeDomain' is a real floating point or complex type.
863/// 'typeSize' is a real floating point or complex type.
Steve Naroff9091ef72007-08-27 01:27:54 +0000864QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
865 QualType typeSize, QualType typeDomain) const {
866 if (typeDomain->isComplexType()) {
867 switch (getFloatingRank(typeSize)) {
Steve Narofffc6ffa22007-08-27 01:41:48 +0000868 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +0000869 case FloatRank: return FloatComplexTy;
870 case DoubleRank: return DoubleComplexTy;
871 case LongDoubleRank: return LongDoubleComplexTy;
872 }
Steve Naroff0af91202007-04-27 21:51:21 +0000873 }
Steve Naroff9091ef72007-08-27 01:27:54 +0000874 if (typeDomain->isRealFloatingType()) {
875 switch (getFloatingRank(typeSize)) {
Steve Narofffc6ffa22007-08-27 01:41:48 +0000876 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +0000877 case FloatRank: return FloatTy;
878 case DoubleRank: return DoubleTy;
879 case LongDoubleRank: return LongDoubleTy;
880 }
881 }
882 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner793d10c2007-09-16 19:23:47 +0000883 //an invalid return value, but the assert
884 //will ensure that this code is never reached.
885 return VoidTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000886}
887
Steve Naroff7af82d42007-08-27 15:30:22 +0000888/// compareFloatingType - Handles 3 different combos:
889/// float/float, float/complex, complex/complex.
890/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
891int ASTContext::compareFloatingType(QualType lt, QualType rt) {
892 if (getFloatingRank(lt) == getFloatingRank(rt))
893 return 0;
894 if (getFloatingRank(lt) > getFloatingRank(rt))
895 return 1;
896 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +0000897}
898
Steve Naroff0af91202007-04-27 21:51:21 +0000899// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
900// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
Steve Naroffe4718892007-04-27 18:30:00 +0000901QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
Chris Lattnerb16f4552007-06-03 07:25:34 +0000902 if (lhs == rhs) return lhs;
903
Steve Naroffe4718892007-04-27 18:30:00 +0000904 bool t1Unsigned = lhs->isUnsignedIntegerType();
905 bool t2Unsigned = rhs->isUnsignedIntegerType();
906
907 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
908 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
909
910 // We have two integer types with differing signs
911 QualType unsignedType = t1Unsigned ? lhs : rhs;
912 QualType signedType = t1Unsigned ? rhs : lhs;
913
914 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
915 return unsignedType;
Steve Naroff0af91202007-04-27 21:51:21 +0000916 else {
917 // FIXME: Need to check if the signed type can represent all values of the
918 // unsigned type. If it can, then the result is the signed type.
919 // If it can't, then the result is the unsigned version of the signed type.
920 // Should probably add a helper that returns a signed integer type from
921 // an unsigned (and vice versa). C99 6.3.1.8.
922 return signedType;
923 }
Steve Naroffe4718892007-04-27 18:30:00 +0000924}
Anders Carlsson98f07902007-08-17 05:31:46 +0000925
926// getCFConstantStringType - Return the type used for constant CFStrings.
927QualType ASTContext::getCFConstantStringType() {
928 if (!CFConstantStringTypeDecl) {
929 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroffa397efd2007-11-03 11:27:19 +0000930 &Idents.get("NSConstantString"),
Anders Carlsson98f07902007-08-17 05:31:46 +0000931 0);
Anders Carlsson9c1011c2007-11-19 00:25:30 +0000932 QualType FieldTypes[4];
Anders Carlsson98f07902007-08-17 05:31:46 +0000933
934 // const int *isa;
935 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlsson9c1011c2007-11-19 00:25:30 +0000936 // int flags;
937 FieldTypes[1] = IntTy;
Anders Carlsson98f07902007-08-17 05:31:46 +0000938 // const char *str;
Anders Carlsson9c1011c2007-11-19 00:25:30 +0000939 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlsson98f07902007-08-17 05:31:46 +0000940 // long length;
Anders Carlsson9c1011c2007-11-19 00:25:30 +0000941 FieldTypes[3] = LongTy;
Anders Carlsson98f07902007-08-17 05:31:46 +0000942 // Create fields
Anders Carlsson9c1011c2007-11-19 00:25:30 +0000943 FieldDecl *FieldDecls[4];
Anders Carlsson98f07902007-08-17 05:31:46 +0000944
Anders Carlsson9c1011c2007-11-19 00:25:30 +0000945 for (unsigned i = 0; i < 4; ++i)
Steve Naroff1d4b5eae2007-09-14 02:20:46 +0000946 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlsson98f07902007-08-17 05:31:46 +0000947
948 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
949 }
950
951 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +0000952}
Anders Carlsson87c149b2007-10-11 01:00:40 +0000953
Anders Carlsson18acd442007-10-29 06:33:42 +0000954// This returns true if a type has been typedefed to BOOL:
955// typedef <type> BOOL;
Chris Lattnere0218992007-10-30 20:27:44 +0000956static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlsson18acd442007-10-29 06:33:42 +0000957 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnere0218992007-10-30 20:27:44 +0000958 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlssond8499822007-10-29 05:01:08 +0000959
960 return false;
961}
962
Fariborz Jahanian797f24c2007-10-29 22:57:28 +0000963/// getObjcEncodingTypeSize returns size of type for objective-c encoding
964/// purpose.
965int ASTContext::getObjcEncodingTypeSize(QualType type) {
966 SourceLocation Loc;
967 uint64_t sz = getTypeSize(type, Loc);
968
969 // Make all integer and enum types at least as large as an int
970 if (sz > 0 && type->isIntegralType())
971 sz = std::max(sz, getTypeSize(IntTy, Loc));
972 // Treat arrays as pointers, since that's how they're passed in.
973 else if (type->isArrayType())
974 sz = getTypeSize(VoidPtrTy, Loc);
975 return sz / getTypeSize(CharTy, Loc);
976}
977
978/// getObjcEncodingForMethodDecl - Return the encoded type for this method
979/// declaration.
980void ASTContext::getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl,
981 std::string& S)
982{
Fariborz Jahanianac73ff82007-11-01 17:18:37 +0000983 // Encode type qualifer, 'in', 'inout', etc. for the return type.
984 getObjcEncodingForTypeQualifier(Decl->getObjcDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +0000985 // Encode result type.
986 getObjcEncodingForType(Decl->getResultType(), S);
987 // Compute size of all parameters.
988 // Start with computing size of a pointer in number of bytes.
989 // FIXME: There might(should) be a better way of doing this computation!
990 SourceLocation Loc;
991 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
992 // The first two arguments (self and _cmd) are pointers; account for
993 // their size.
994 int ParmOffset = 2 * PtrSize;
995 int NumOfParams = Decl->getNumParams();
996 for (int i = 0; i < NumOfParams; i++) {
997 QualType PType = Decl->getParamDecl(i)->getType();
998 int sz = getObjcEncodingTypeSize (PType);
999 assert (sz > 0 && "getObjcEncodingForMethodDecl - Incomplete param type");
1000 ParmOffset += sz;
1001 }
1002 S += llvm::utostr(ParmOffset);
1003 S += "@0:";
1004 S += llvm::utostr(PtrSize);
1005
1006 // Argument types.
1007 ParmOffset = 2 * PtrSize;
1008 for (int i = 0; i < NumOfParams; i++) {
1009 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00001010 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00001011 // 'in', 'inout', etc.
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00001012 getObjcEncodingForTypeQualifier(
1013 Decl->getParamDecl(i)->getObjcDeclQualifier(), S);
Fariborz Jahanian797f24c2007-10-29 22:57:28 +00001014 getObjcEncodingForType(PType, S);
1015 S += llvm::utostr(ParmOffset);
1016 ParmOffset += getObjcEncodingTypeSize(PType);
1017 }
1018}
1019
Anders Carlssond8499822007-10-29 05:01:08 +00001020void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const
1021{
Anders Carlsson18acd442007-10-29 06:33:42 +00001022 // FIXME: This currently doesn't encode:
1023 // @ An object (whether statically typed or typed id)
1024 // # A class object (Class)
1025 // : A method selector (SEL)
1026 // {name=type...} A structure
1027 // (name=type...) A union
1028 // bnum A bit field of num bits
1029
1030 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlssond8499822007-10-29 05:01:08 +00001031 char encoding;
1032 switch (BT->getKind()) {
1033 case BuiltinType::Void:
1034 encoding = 'v';
1035 break;
1036 case BuiltinType::Bool:
1037 encoding = 'B';
1038 break;
1039 case BuiltinType::Char_U:
1040 case BuiltinType::UChar:
1041 encoding = 'C';
1042 break;
1043 case BuiltinType::UShort:
1044 encoding = 'S';
1045 break;
1046 case BuiltinType::UInt:
1047 encoding = 'I';
1048 break;
1049 case BuiltinType::ULong:
1050 encoding = 'L';
1051 break;
1052 case BuiltinType::ULongLong:
1053 encoding = 'Q';
1054 break;
1055 case BuiltinType::Char_S:
1056 case BuiltinType::SChar:
1057 encoding = 'c';
1058 break;
1059 case BuiltinType::Short:
1060 encoding = 's';
1061 break;
1062 case BuiltinType::Int:
1063 encoding = 'i';
1064 break;
1065 case BuiltinType::Long:
1066 encoding = 'l';
1067 break;
1068 case BuiltinType::LongLong:
1069 encoding = 'q';
1070 break;
1071 case BuiltinType::Float:
1072 encoding = 'f';
1073 break;
1074 case BuiltinType::Double:
1075 encoding = 'd';
1076 break;
1077 case BuiltinType::LongDouble:
1078 encoding = 'd';
1079 break;
1080 default:
1081 assert(0 && "Unhandled builtin type kind");
1082 }
1083
1084 S += encoding;
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +00001085 }
Fariborz Jahanian9f0e3102007-12-18 21:33:44 +00001086 else if (T->isObjcQualifiedIdType()) {
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +00001087 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian9f0e3102007-12-18 21:33:44 +00001088 return getObjcEncodingForType(getObjcIdType(), S);
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +00001089
1090 }
1091 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlssond8499822007-10-29 05:01:08 +00001092 QualType PointeeTy = PT->getPointeeType();
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00001093 if (isObjcIdType(PointeeTy) || PointeeTy->isObjcInterfaceType()) {
Fariborz Jahanian509d8d62007-10-30 17:06:23 +00001094 S += '@';
1095 return;
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00001096 } else if (isObjcClassType(PointeeTy)) {
1097 S += '#';
1098 return;
1099 } else if (isObjcSelType(PointeeTy)) {
1100 S += ':';
1101 return;
Fariborz Jahanian509d8d62007-10-30 17:06:23 +00001102 }
Anders Carlssond8499822007-10-29 05:01:08 +00001103
1104 if (PointeeTy->isCharType()) {
1105 // char pointer types should be encoded as '*' unless it is a
1106 // type that has been typedef'd to 'BOOL'.
Anders Carlsson18acd442007-10-29 06:33:42 +00001107 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlssond8499822007-10-29 05:01:08 +00001108 S += '*';
1109 return;
1110 }
1111 }
1112
1113 S += '^';
1114 getObjcEncodingForType(PT->getPointeeType(), S);
Anders Carlsson18acd442007-10-29 06:33:42 +00001115 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlssond8499822007-10-29 05:01:08 +00001116 S += '[';
1117
1118 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1119 S += llvm::utostr(CAT->getSize().getZExtValue());
1120 else
1121 assert(0 && "Unhandled array type!");
1122
1123 getObjcEncodingForType(AT->getElementType(), S);
1124 S += ']';
Anders Carlssondf4cc612007-10-30 00:06:20 +00001125 } else if (T->getAsFunctionType()) {
1126 S += '?';
Fariborz Jahanianbc92fd72007-11-13 23:21:38 +00001127 } else if (const RecordType *RTy = T->getAsRecordType()) {
1128 RecordDecl *RDecl= RTy->getDecl();
1129 S += '{';
1130 S += RDecl->getName();
1131 S += '=';
1132 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1133 FieldDecl *field = RDecl->getMember(i);
1134 getObjcEncodingForType(field->getType(), S);
1135 }
1136 S += '}';
Steve Narofff44cb632007-12-12 22:30:11 +00001137 } else if (T->isEnumeralType()) {
1138 S += 'i';
Anders Carlssond8499822007-10-29 05:01:08 +00001139 } else
Steve Narofff44cb632007-12-12 22:30:11 +00001140 assert(0 && "@encode for type not implemented!");
Anders Carlssond8499822007-10-29 05:01:08 +00001141}
1142
Fariborz Jahanianac73ff82007-11-01 17:18:37 +00001143void ASTContext::getObjcEncodingForTypeQualifier(Decl::ObjcDeclQualifier QT,
1144 std::string& S) const {
1145 if (QT & Decl::OBJC_TQ_In)
1146 S += 'n';
1147 if (QT & Decl::OBJC_TQ_Inout)
1148 S += 'N';
1149 if (QT & Decl::OBJC_TQ_Out)
1150 S += 'o';
1151 if (QT & Decl::OBJC_TQ_Bycopy)
1152 S += 'O';
1153 if (QT & Decl::OBJC_TQ_Byref)
1154 S += 'R';
1155 if (QT & Decl::OBJC_TQ_Oneway)
1156 S += 'V';
1157}
1158
Anders Carlsson87c149b2007-10-11 01:00:40 +00001159void ASTContext::setBuiltinVaListType(QualType T)
1160{
1161 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1162
1163 BuiltinVaListType = T;
1164}
1165
Steve Naroff66e9f332007-10-15 14:41:52 +00001166void ASTContext::setObjcIdType(TypedefDecl *TD)
1167{
1168 assert(ObjcIdType.isNull() && "'id' type already set!");
1169
1170 ObjcIdType = getTypedefType(TD);
1171
1172 // typedef struct objc_object *id;
1173 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1174 assert(ptr && "'id' incorrectly typed");
1175 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1176 assert(rec && "'id' incorrectly typed");
1177 IdStructType = rec;
1178}
1179
Fariborz Jahanian4bef4622007-10-16 20:40:23 +00001180void ASTContext::setObjcSelType(TypedefDecl *TD)
1181{
1182 assert(ObjcSelType.isNull() && "'SEL' type already set!");
1183
1184 ObjcSelType = getTypedefType(TD);
1185
1186 // typedef struct objc_selector *SEL;
1187 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1188 assert(ptr && "'SEL' incorrectly typed");
1189 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1190 assert(rec && "'SEL' incorrectly typed");
1191 SelStructType = rec;
1192}
1193
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +00001194void ASTContext::setObjcProtoType(QualType QT)
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001195{
1196 assert(ObjcProtoType.isNull() && "'Protocol' type already set!");
Fariborz Jahanian2bbd03a2007-12-07 00:18:54 +00001197 ObjcProtoType = QT;
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +00001198}
1199
Anders Carlssonf56a7ae2007-10-31 02:53:19 +00001200void ASTContext::setObjcClassType(TypedefDecl *TD)
1201{
1202 assert(ObjcClassType.isNull() && "'Class' type already set!");
1203
1204 ObjcClassType = getTypedefType(TD);
1205
1206 // typedef struct objc_class *Class;
1207 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1208 assert(ptr && "'Class' incorrectly typed");
1209 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1210 assert(rec && "'Class' incorrectly typed");
1211 ClassStructType = rec;
1212}
1213
Steve Narofff73b7842007-10-15 23:35:17 +00001214void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) {
1215 assert(ObjcConstantStringType.isNull() &&
1216 "'NSConstantString' type already set!");
1217
1218 ObjcConstantStringType = getObjcInterfaceType(Decl);
1219}
1220
Steve Naroff32e44c02007-10-15 20:41:53 +00001221bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1222 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1223 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1224
1225 return lBuiltin->getKind() == rBuiltin->getKind();
1226}
1227
Fariborz Jahanian56b5c962007-12-21 17:34:43 +00001228/// objcTypesAreCompatible - This routine is called when two types
1229/// are of different class; one is interface type or is
1230/// a qualified interface type and the other type is of a different class.
1231/// Example, II or II<P>.
Steve Naroff32e44c02007-10-15 20:41:53 +00001232bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
1233 if (lhs->isObjcInterfaceType() && isObjcIdType(rhs))
1234 return true;
1235 else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType())
1236 return true;
Fariborz Jahanian56b5c962007-12-21 17:34:43 +00001237 if (ObjcInterfaceType *lhsIT =
1238 dyn_cast<ObjcInterfaceType>(lhs.getCanonicalType().getTypePtr())) {
1239 ObjcQualifiedInterfaceType *rhsQI =
1240 dyn_cast<ObjcQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
1241 return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl());
1242 }
1243 else if (ObjcInterfaceType *rhsIT =
1244 dyn_cast<ObjcInterfaceType>(rhs.getCanonicalType().getTypePtr())) {
1245 ObjcQualifiedInterfaceType *lhsQI =
1246 dyn_cast<ObjcQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
1247 return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl());
1248 }
Steve Naroff32e44c02007-10-15 20:41:53 +00001249 return false;
1250}
1251
1252bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
Fariborz Jahanian4368af02007-12-20 22:37:58 +00001253 if (lhs == rhs)
1254 return true;
1255 ObjcInterfaceType *lhsIT = cast<ObjcInterfaceType>(lhs.getTypePtr());
1256 ObjcInterfaceType *rhsIT = cast<ObjcInterfaceType>(rhs.getTypePtr());
1257 ObjcInterfaceDecl *rhsIDecl = rhsIT->getDecl();
1258 ObjcInterfaceDecl *lhsIDecl = lhsIT->getDecl();
1259 // rhs is derived from lhs it is OK; else it is not OK.
1260 while (rhsIDecl != NULL) {
1261 if (rhsIDecl == lhsIDecl)
1262 return true;
1263 rhsIDecl = rhsIDecl->getSuperClass();
1264 }
1265 return false;
Steve Naroff32e44c02007-10-15 20:41:53 +00001266}
1267
Fariborz Jahanianc98d9562007-12-12 01:00:23 +00001268bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1269 QualType rhs) {
1270 ObjcQualifiedInterfaceType *lhsQI =
1271 dyn_cast<ObjcQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
1272 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
1273 ObjcQualifiedInterfaceType *rhsQI =
1274 dyn_cast<ObjcQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
1275 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanianc47dc4f2007-12-13 20:47:42 +00001276 if (!interfaceTypesAreCompatible(getObjcInterfaceType(lhsQI->getDecl()),
1277 getObjcInterfaceType(rhsQI->getDecl())))
Fariborz Jahanianc98d9562007-12-12 01:00:23 +00001278 return false;
1279 /* All protocols in lhs must have a presense in rhs. */
1280 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1281 bool match = false;
1282 ObjcProtocolDecl *lhsProto = lhsQI->getProtocols(i);
1283 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
1284 ObjcProtocolDecl *rhsProto = rhsQI->getProtocols(j);
1285 if (lhsProto == rhsProto) {
1286 match = true;
1287 break;
1288 }
1289 }
1290 if (!match)
1291 return false;
1292 }
1293 return true;
1294}
1295
Fariborz Jahanianff7d2bf2007-12-21 00:33:59 +00001296/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
1297/// inheritance hierarchy of 'rProto'.
1298static bool ProtocolCompatibleWithProtocol(ObjcProtocolDecl *lProto,
1299 ObjcProtocolDecl *rProto) {
1300 if (lProto == rProto)
1301 return true;
1302 ObjcProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
1303 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
1304 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
1305 return true;
1306 return false;
1307}
1308
Fariborz Jahaniana7c705d2007-12-21 22:22:33 +00001309/// ClassImplementsProtocol - Checks that 'lProto' protocol
1310/// has been implemented in IDecl class, its super class or categories (if
1311/// lookupCategory is true).
1312static bool ClassImplementsProtocol(ObjcProtocolDecl *lProto,
1313 ObjcInterfaceDecl *IDecl,
1314 bool lookupCategory) {
1315
1316 // 1st, look up the class.
1317 ObjcProtocolDecl **protoList = IDecl->getReferencedProtocols();
1318 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
1319 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1320 return true;
1321 }
1322
1323 // 2nd, look up the category.
1324 if (lookupCategory)
1325 for (ObjcCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
1326 CDecl = CDecl->getNextClassCategory()) {
1327 protoList = CDecl->getReferencedProtocols();
1328 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
1329 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1330 return true;
1331 }
1332 }
1333
1334 // 3rd, look up the super class(s)
1335 if (IDecl->getSuperClass())
1336 return
1337 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
1338
1339 return false;
1340}
1341
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001342/// ObjcQualifiedIdTypesAreCompatible - Compares two types, at least
Fariborz Jahanianff7d2bf2007-12-21 00:33:59 +00001343/// one of which is a protocol qualified 'id' type. When 'compare'
1344/// is true it is for comparison; when false, for assignment/initialization.
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001345bool ASTContext::ObjcQualifiedIdTypesAreCompatible(QualType lhs,
Fariborz Jahanianff7d2bf2007-12-21 00:33:59 +00001346 QualType rhs,
1347 bool compare) {
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001348 // match id<P..> with an 'id' type in all cases.
1349 if (const PointerType *PT = lhs->getAsPointerType()) {
1350 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahanian63b19f12007-12-20 19:24:10 +00001351 if (isObjcIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001352 return true;
1353
1354 }
1355 else if (const PointerType *PT = rhs->getAsPointerType()) {
1356 QualType PointeeTy = PT->getPointeeType();
Fariborz Jahanian63b19f12007-12-20 19:24:10 +00001357 if (isObjcIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001358 return true;
1359
1360 }
1361
1362 ObjcQualifiedInterfaceType *lhsQI = 0;
1363 ObjcQualifiedInterfaceType *rhsQI = 0;
Fariborz Jahanian63b19f12007-12-20 19:24:10 +00001364 ObjcInterfaceDecl *lhsID = 0;
1365 ObjcInterfaceDecl *rhsID = 0;
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001366 ObjcQualifiedIdType *lhsQID = dyn_cast<ObjcQualifiedIdType>(lhs);
1367 ObjcQualifiedIdType *rhsQID = dyn_cast<ObjcQualifiedIdType>(rhs);
1368
1369 if (lhsQID) {
1370 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1371 QualType rtype =
1372 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1373 rhsQI =
1374 dyn_cast<ObjcQualifiedInterfaceType>(
1375 rtype.getCanonicalType().getTypePtr());
Fariborz Jahanian63b19f12007-12-20 19:24:10 +00001376 if (!rhsQI) {
1377 ObjcInterfaceType *IT = dyn_cast<ObjcInterfaceType>(
1378 rtype.getCanonicalType().getTypePtr());
1379 if (IT)
1380 rhsID = IT->getDecl();
1381 }
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001382 }
Fariborz Jahanian63b19f12007-12-20 19:24:10 +00001383 if (!rhsQI && !rhsQID && !rhsID)
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001384 return false;
1385
Fariborz Jahaniana7c705d2007-12-21 22:22:33 +00001386 unsigned numRhsProtocols;
1387 ObjcProtocolDecl **rhsProtoList;
1388 if (rhsQI) {
1389 numRhsProtocols = rhsQI->getNumProtocols();
1390 rhsProtoList = rhsQI->getReferencedProtocols();
1391 }
1392 else if (rhsQID) {
1393 numRhsProtocols = rhsQID->getNumProtocols();
1394 rhsProtoList = rhsQID->getReferencedProtocols();
1395 }
1396
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001397 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001398 ObjcProtocolDecl *lhsProto = lhsQID->getProtocols(i);
Fariborz Jahaniana7c705d2007-12-21 22:22:33 +00001399 bool match = false;
1400
1401 // when comparing an id<P> on lhs with a static type on rhs,
1402 // see if static class implements all of id's protocols, directly or
1403 // through its super class and categories.
1404 if (rhsID) {
1405 if (ClassImplementsProtocol(lhsProto, rhsID, true))
1406 match = true;
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001407 }
Fariborz Jahaniana7c705d2007-12-21 22:22:33 +00001408 else for (unsigned j = 0; j < numRhsProtocols; j++) {
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001409 ObjcProtocolDecl *rhsProto = rhsProtoList[j];
Fariborz Jahanianff7d2bf2007-12-21 00:33:59 +00001410 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1411 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001412 match = true;
1413 break;
1414 }
1415 }
1416 if (!match)
1417 return false;
1418 }
1419 }
1420 else if (rhsQID) {
1421 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1422 QualType ltype =
1423 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1424 lhsQI =
1425 dyn_cast<ObjcQualifiedInterfaceType>(
1426 ltype.getCanonicalType().getTypePtr());
Fariborz Jahanian63b19f12007-12-20 19:24:10 +00001427 if (!lhsQI) {
1428 ObjcInterfaceType *IT = dyn_cast<ObjcInterfaceType>(
1429 ltype.getCanonicalType().getTypePtr());
1430 if (IT)
1431 lhsID = IT->getDecl();
1432 }
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001433 }
Fariborz Jahanian63b19f12007-12-20 19:24:10 +00001434 if (!lhsQI && !lhsQID && !lhsID)
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001435 return false;
Fariborz Jahanian63b19f12007-12-20 19:24:10 +00001436
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001437 unsigned numLhsProtocols;
1438 ObjcProtocolDecl **lhsProtoList;
1439 if (lhsQI) {
1440 numLhsProtocols = lhsQI->getNumProtocols();
1441 lhsProtoList = lhsQI->getReferencedProtocols();
1442 }
Fariborz Jahanian63b19f12007-12-20 19:24:10 +00001443 else if (lhsQID) {
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001444 numLhsProtocols = lhsQID->getNumProtocols();
1445 lhsProtoList = lhsQID->getReferencedProtocols();
Fariborz Jahaniana7c705d2007-12-21 22:22:33 +00001446 }
1447 bool match = false;
1448 // for static type vs. qualified 'id' type, check that class implements
1449 // one of 'id's protocols.
1450 if (lhsID) {
1451 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
1452 ObjcProtocolDecl *rhsProto = rhsQID->getProtocols(j);
1453 if (ClassImplementsProtocol(rhsProto, lhsID, compare)) {
1454 match = true;
1455 break;
1456 }
1457 }
1458 }
1459 else for (unsigned i =0; i < numLhsProtocols; i++) {
1460 match = false;
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001461 ObjcProtocolDecl *lhsProto = lhsProtoList[i];
1462 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
1463 ObjcProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanianff7d2bf2007-12-21 00:33:59 +00001464 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1465 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001466 match = true;
1467 break;
1468 }
1469 }
Fariborz Jahaniana7c705d2007-12-21 22:22:33 +00001470 }
1471 if (!match)
1472 return false;
Fariborz Jahaniana1e34202007-12-19 17:45:58 +00001473 }
1474 return true;
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +00001475}
Fariborz Jahanian24cb52c2007-12-17 21:03:50 +00001476
Chris Lattnerb338a6b2007-11-01 05:03:41 +00001477bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1478 const VectorType *lVector = lhs->getAsVectorType();
1479 const VectorType *rVector = rhs->getAsVectorType();
1480
1481 if ((lVector->getElementType().getCanonicalType() ==
1482 rVector->getElementType().getCanonicalType()) &&
1483 (lVector->getNumElements() == rVector->getNumElements()))
1484 return true;
1485 return false;
1486}
1487
Steve Naroff32e44c02007-10-15 20:41:53 +00001488// C99 6.2.7p1: If both are complete types, then the following additional
1489// requirements apply...FIXME (handle compatibility across source files).
1490bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
1491 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
1492 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
1493
1494 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
1495 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1496 return true;
1497 }
1498 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
1499 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1500 return true;
1501 }
Steve Naroff4d7b3672007-11-07 06:03:51 +00001502 // "Class" and "id" are compatible built-in structure types.
1503 if (isObjcIdType(lhs) && isObjcClassType(rhs) ||
1504 isObjcClassType(lhs) && isObjcIdType(rhs))
1505 return true;
Steve Naroff32e44c02007-10-15 20:41:53 +00001506 return false;
1507}
1508
1509bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1510 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1511 // identically qualified and both shall be pointers to compatible types.
1512 if (lhs.getQualifiers() != rhs.getQualifiers())
1513 return false;
1514
1515 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1516 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1517
1518 return typesAreCompatible(ltype, rtype);
1519}
1520
Bill Wendlingdb4e3492007-12-03 07:33:35 +00001521// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroff32e44c02007-10-15 20:41:53 +00001522// reference to T, the operation assigns to the object of type T denoted by the
1523// reference.
1524bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1525 QualType ltype = lhs;
1526
1527 if (lhs->isReferenceType())
1528 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1529
1530 QualType rtype = rhs;
1531
1532 if (rhs->isReferenceType())
1533 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1534
1535 return typesAreCompatible(ltype, rtype);
1536}
1537
1538bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1539 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1540 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1541 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1542 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1543
1544 // first check the return types (common between C99 and K&R).
1545 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1546 return false;
1547
1548 if (lproto && rproto) { // two C99 style function prototypes
1549 unsigned lproto_nargs = lproto->getNumArgs();
1550 unsigned rproto_nargs = rproto->getNumArgs();
1551
1552 if (lproto_nargs != rproto_nargs)
1553 return false;
1554
1555 // both prototypes have the same number of arguments.
1556 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1557 (rproto->isVariadic() && !lproto->isVariadic()))
1558 return false;
1559
1560 // The use of ellipsis agree...now check the argument types.
1561 for (unsigned i = 0; i < lproto_nargs; i++)
1562 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
1563 return false;
1564 return true;
1565 }
1566 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1567 return true;
1568
1569 // we have a mixture of K&R style with C99 prototypes
1570 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1571
1572 if (proto->isVariadic())
1573 return false;
1574
1575 // FIXME: Each parameter type T in the prototype must be compatible with the
1576 // type resulting from applying the usual argument conversions to T.
1577 return true;
1578}
1579
1580bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
1581 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
1582 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
1583
1584 if (!typesAreCompatible(ltype, rtype))
1585 return false;
1586
1587 // FIXME: If both types specify constant sizes, then the sizes must also be
1588 // the same. Even if the sizes are the same, GCC produces an error.
1589 return true;
1590}
1591
1592/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1593/// both shall have the identically qualified version of a compatible type.
1594/// C99 6.2.7p1: Two types have compatible types if their types are the
1595/// same. See 6.7.[2,3,5] for additional rules.
1596bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
1597 QualType lcanon = lhs.getCanonicalType();
1598 QualType rcanon = rhs.getCanonicalType();
1599
1600 // If two types are identical, they are are compatible
1601 if (lcanon == rcanon)
1602 return true;
Bill Wendlingdb4e3492007-12-03 07:33:35 +00001603
1604 // C++ [expr]: If an expression initially has the type "reference to T", the
1605 // type is adjusted to "T" prior to any further analysis, the expression
1606 // designates the object or function denoted by the reference, and the
1607 // expression is an lvalue.
1608 if (lcanon->getTypeClass() == Type::Reference)
1609 lcanon = cast<ReferenceType>(lcanon)->getReferenceeType();
1610 if (rcanon->getTypeClass() == Type::Reference)
1611 rcanon = cast<ReferenceType>(rcanon)->getReferenceeType();
Steve Naroff32e44c02007-10-15 20:41:53 +00001612
1613 // If the canonical type classes don't match, they can't be compatible
1614 if (lcanon->getTypeClass() != rcanon->getTypeClass()) {
1615 // For Objective-C, it is possible for two types to be compatible
1616 // when their classes don't match (when dealing with "id"). If either type
1617 // is an interface, we defer to objcTypesAreCompatible().
1618 if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType())
1619 return objcTypesAreCompatible(lcanon, rcanon);
1620 return false;
1621 }
1622 switch (lcanon->getTypeClass()) {
1623 case Type::Pointer:
1624 return pointerTypesAreCompatible(lcanon, rcanon);
Steve Naroff32e44c02007-10-15 20:41:53 +00001625 case Type::ConstantArray:
1626 case Type::VariableArray:
1627 return arrayTypesAreCompatible(lcanon, rcanon);
1628 case Type::FunctionNoProto:
1629 case Type::FunctionProto:
1630 return functionTypesAreCompatible(lcanon, rcanon);
1631 case Type::Tagged: // handle structures, unions
1632 return tagTypesAreCompatible(lcanon, rcanon);
1633 case Type::Builtin:
1634 return builtinTypesAreCompatible(lcanon, rcanon);
1635 case Type::ObjcInterface:
1636 return interfaceTypesAreCompatible(lcanon, rcanon);
Chris Lattnerb338a6b2007-11-01 05:03:41 +00001637 case Type::Vector:
1638 case Type::OCUVector:
1639 return vectorTypesAreCompatible(lcanon, rcanon);
Fariborz Jahanianc98d9562007-12-12 01:00:23 +00001640 case Type::ObjcQualifiedInterface:
1641 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
Steve Naroff32e44c02007-10-15 20:41:53 +00001642 default:
1643 assert(0 && "unexpected type");
1644 }
1645 return true; // should never get here...
1646}
Ted Kremenekfc581a92007-10-31 17:10:13 +00001647
Ted Kremenekfc581a92007-10-31 17:10:13 +00001648/// Emit - Serialize an ASTContext object to Bitcode.
1649void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenekc55cd9a2007-10-31 20:00:03 +00001650 S.EmitRef(SourceMgr);
1651 S.EmitRef(Target);
1652 S.EmitRef(Idents);
1653 S.EmitRef(Selectors);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001654
Ted Kremenek6aff8712007-10-31 22:44:07 +00001655 // Emit the size of the type vector so that we can reserve that size
1656 // when we reconstitute the ASTContext object.
Ted Kremenek453ab7d2007-11-06 22:26:16 +00001657 S.EmitInt(Types.size());
1658
Ted Kremenek24726c32007-11-13 22:02:55 +00001659 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1660 I!=E;++I)
1661 (*I)->Emit(S);
Ted Kremenek453ab7d2007-11-06 22:26:16 +00001662
Ted Kremenek26a7f3f2007-11-01 18:11:32 +00001663 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenekfc581a92007-10-31 17:10:13 +00001664}
1665
Ted Kremeneke9b83bb2007-11-13 00:25:37 +00001666ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek6aff8712007-10-31 22:44:07 +00001667 SourceManager &SM = D.ReadRef<SourceManager>();
1668 TargetInfo &t = D.ReadRef<TargetInfo>();
1669 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1670 SelectorTable &sels = D.ReadRef<SelectorTable>();
1671
1672 unsigned size_reserve = D.ReadInt();
1673
1674 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1675
Ted Kremenek24726c32007-11-13 22:02:55 +00001676 for (unsigned i = 0; i < size_reserve; ++i)
1677 Type::Create(*A,i,D);
Ted Kremenek453ab7d2007-11-06 22:26:16 +00001678
Ted Kremenek26a7f3f2007-11-01 18:11:32 +00001679 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek6aff8712007-10-31 22:44:07 +00001680
1681 return A;
1682}