blob: 7d7decb6ff0989111513ed7ee1f25fc48a35f4c3 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
Ted Kremenek738e6c02007-10-31 17:10:13 +000020#include "llvm/Bitcode/Serialize.h"
21#include "llvm/Bitcode/Deserialize.h"
Anders Carlsson36f07d82007-10-29 05:01:08 +000022
Chris Lattner4b009652007-07-25 00:24:17 +000023using namespace clang;
24
25enum FloatingRank {
26 FloatRank, DoubleRank, LongDoubleRank
27};
28
29ASTContext::~ASTContext() {
30 // Deallocate all the types.
31 while (!Types.empty()) {
32 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
33 // Destroy the object, but don't call delete. These are malloc'd.
34 FT->~FunctionTypeProto();
35 free(FT);
36 } else {
37 delete Types.back();
38 }
39 Types.pop_back();
40 }
41}
42
43void ASTContext::PrintStats() const {
44 fprintf(stderr, "*** AST Context Stats:\n");
45 fprintf(stderr, " %d types total.\n", (int)Types.size());
46 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
47 unsigned NumVector = 0, NumComplex = 0;
48 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
49
50 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +000051 unsigned NumObjCInterfaces = 0, NumObjCQualifiedInterfaces = 0;
52 unsigned NumObjCQualifiedIds = 0;
Chris Lattner4b009652007-07-25 00:24:17 +000053
54 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
55 Type *T = Types[i];
56 if (isa<BuiltinType>(T))
57 ++NumBuiltin;
58 else if (isa<PointerType>(T))
59 ++NumPointer;
60 else if (isa<ReferenceType>(T))
61 ++NumReference;
62 else if (isa<ComplexType>(T))
63 ++NumComplex;
64 else if (isa<ArrayType>(T))
65 ++NumArray;
66 else if (isa<VectorType>(T))
67 ++NumVector;
68 else if (isa<FunctionTypeNoProto>(T))
69 ++NumFunctionNP;
70 else if (isa<FunctionTypeProto>(T))
71 ++NumFunctionP;
72 else if (isa<TypedefType>(T))
73 ++NumTypeName;
74 else if (TagType *TT = dyn_cast<TagType>(T)) {
75 ++NumTagged;
76 switch (TT->getDecl()->getKind()) {
77 default: assert(0 && "Unknown tagged type!");
78 case Decl::Struct: ++NumTagStruct; break;
79 case Decl::Union: ++NumTagUnion; break;
80 case Decl::Class: ++NumTagClass; break;
81 case Decl::Enum: ++NumTagEnum; break;
82 }
Ted Kremenek42730c52008-01-07 19:49:32 +000083 } else if (isa<ObjCInterfaceType>(T))
84 ++NumObjCInterfaces;
85 else if (isa<ObjCQualifiedInterfaceType>(T))
86 ++NumObjCQualifiedInterfaces;
87 else if (isa<ObjCQualifiedIdType>(T))
88 ++NumObjCQualifiedIds;
Steve Naroff948fd372007-09-17 14:16:13 +000089 else {
Chris Lattner8a35b462007-12-12 06:43:05 +000090 QualType(T, 0).dump();
Chris Lattner4b009652007-07-25 00:24:17 +000091 assert(0 && "Unknown type!");
92 }
93 }
94
95 fprintf(stderr, " %d builtin types\n", NumBuiltin);
96 fprintf(stderr, " %d pointer types\n", NumPointer);
97 fprintf(stderr, " %d reference types\n", NumReference);
98 fprintf(stderr, " %d complex types\n", NumComplex);
99 fprintf(stderr, " %d array types\n", NumArray);
100 fprintf(stderr, " %d vector types\n", NumVector);
101 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
102 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
103 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
104 fprintf(stderr, " %d tagged types\n", NumTagged);
105 fprintf(stderr, " %d struct types\n", NumTagStruct);
106 fprintf(stderr, " %d union types\n", NumTagUnion);
107 fprintf(stderr, " %d class types\n", NumTagClass);
108 fprintf(stderr, " %d enum types\n", NumTagEnum);
Ted Kremenek42730c52008-01-07 19:49:32 +0000109 fprintf(stderr, " %d interface types\n", NumObjCInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000110 fprintf(stderr, " %d protocol qualified interface types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000111 NumObjCQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000112 fprintf(stderr, " %d protocol qualified id types\n",
Ted Kremenek42730c52008-01-07 19:49:32 +0000113 NumObjCQualifiedIds);
Chris Lattner4b009652007-07-25 00:24:17 +0000114 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
115 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
116 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
117 NumFunctionP*sizeof(FunctionTypeProto)+
118 NumFunctionNP*sizeof(FunctionTypeNoProto)+
119 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
120}
121
122
123void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
124 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
125}
126
Chris Lattner4b009652007-07-25 00:24:17 +0000127void ASTContext::InitBuiltinTypes() {
128 assert(VoidTy.isNull() && "Context reinitialized?");
129
130 // C99 6.2.5p19.
131 InitBuiltinType(VoidTy, BuiltinType::Void);
132
133 // C99 6.2.5p2.
134 InitBuiltinType(BoolTy, BuiltinType::Bool);
135 // C99 6.2.5p3.
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000136 if (Target.isCharSigned(FullSourceLoc()))
Chris Lattner4b009652007-07-25 00:24:17 +0000137 InitBuiltinType(CharTy, BuiltinType::Char_S);
138 else
139 InitBuiltinType(CharTy, BuiltinType::Char_U);
140 // C99 6.2.5p4.
141 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
142 InitBuiltinType(ShortTy, BuiltinType::Short);
143 InitBuiltinType(IntTy, BuiltinType::Int);
144 InitBuiltinType(LongTy, BuiltinType::Long);
145 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
146
147 // C99 6.2.5p6.
148 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
149 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
150 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
151 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
152 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
153
154 // C99 6.2.5p10.
155 InitBuiltinType(FloatTy, BuiltinType::Float);
156 InitBuiltinType(DoubleTy, BuiltinType::Double);
157 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
158
159 // C99 6.2.5p11.
160 FloatComplexTy = getComplexType(FloatTy);
161 DoubleComplexTy = getComplexType(DoubleTy);
162 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff9d12c902007-10-15 14:41:52 +0000163
164 BuiltinVaListType = QualType();
Ted Kremenek42730c52008-01-07 19:49:32 +0000165 ObjCIdType = QualType();
Steve Naroff9d12c902007-10-15 14:41:52 +0000166 IdStructType = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000167 ObjCClassType = QualType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000168 ClassStructType = 0;
169
Ted Kremenek42730c52008-01-07 19:49:32 +0000170 ObjCConstantStringType = QualType();
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000171
172 // void * type
173 VoidPtrTy = getPointerType(VoidTy);
Chris Lattner4b009652007-07-25 00:24:17 +0000174}
175
176//===----------------------------------------------------------------------===//
177// Type Sizing and Analysis
178//===----------------------------------------------------------------------===//
179
180/// getTypeSize - Return the size of the specified type, in bits. This method
181/// does not work on incomplete types.
182std::pair<uint64_t, unsigned>
183ASTContext::getTypeInfo(QualType T, SourceLocation L) {
184 T = T.getCanonicalType();
185 uint64_t Size;
186 unsigned Align;
187 switch (T->getTypeClass()) {
188 case Type::TypeName: assert(0 && "Not a canonical type!");
189 case Type::FunctionNoProto:
190 case Type::FunctionProto:
191 default:
192 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000193 case Type::VariableArray:
194 assert(0 && "VLAs not implemented yet!");
195 case Type::ConstantArray: {
196 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
197
Chris Lattner4b009652007-07-25 00:24:17 +0000198 std::pair<uint64_t, unsigned> EltInfo =
Steve Naroff83c13012007-08-30 01:06:46 +0000199 getTypeInfo(CAT->getElementType(), L);
200 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000201 Align = EltInfo.second;
202 break;
Christopher Lamb82c758b2007-12-29 05:10:55 +0000203 }
204 case Type::OCUVector:
Chris Lattner4b009652007-07-25 00:24:17 +0000205 case Type::Vector: {
206 std::pair<uint64_t, unsigned> EltInfo =
207 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
208 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
209 // FIXME: Vector alignment is not the alignment of its elements.
210 Align = EltInfo.second;
211 break;
212 }
213
214 case Type::Builtin: {
215 // FIXME: need to use TargetInfo to derive the target specific sizes. This
216 // implementation will suffice for play with vector support.
Chris Lattner858eece2007-09-22 18:29:59 +0000217 const llvm::fltSemantics *F;
Chris Lattner4b009652007-07-25 00:24:17 +0000218 switch (cast<BuiltinType>(T)->getKind()) {
219 default: assert(0 && "Unknown builtin type!");
220 case BuiltinType::Void:
221 assert(0 && "Incomplete types have no size!");
Chris Lattnerb66237b2007-12-19 19:23:28 +0000222 case BuiltinType::Bool:
223 Target.getBoolInfo(Size, Align, getFullLoc(L));
224 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000225 case BuiltinType::Char_S:
226 case BuiltinType::Char_U:
227 case BuiltinType::UChar:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000228 case BuiltinType::SChar:
229 Target.getCharInfo(Size, Align, getFullLoc(L));
230 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000231 case BuiltinType::UShort:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000232 case BuiltinType::Short:
233 Target.getShortInfo(Size, Align, getFullLoc(L));
234 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000235 case BuiltinType::UInt:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000236 case BuiltinType::Int:
237 Target.getIntInfo(Size, Align, getFullLoc(L));
238 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000239 case BuiltinType::ULong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000240 case BuiltinType::Long:
241 Target.getLongInfo(Size, Align, getFullLoc(L));
242 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000243 case BuiltinType::ULongLong:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000244 case BuiltinType::LongLong:
245 Target.getLongLongInfo(Size, Align, getFullLoc(L));
246 break;
247 case BuiltinType::Float:
248 Target.getFloatInfo(Size, Align, F, getFullLoc(L));
249 break;
250 case BuiltinType::Double:
251 Target.getDoubleInfo(Size, Align, F, getFullLoc(L));
252 break;
253 case BuiltinType::LongDouble:
254 Target.getLongDoubleInfo(Size, Align, F, getFullLoc(L));
255 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000256 }
257 break;
258 }
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000259 case Type::ASQual:
260 return getTypeInfo(cast<ASQualType>(T)->getBaseType(), L);
Ted Kremenek42730c52008-01-07 19:49:32 +0000261 case Type::ObjCQualifiedId:
Chris Lattnerb66237b2007-12-19 19:23:28 +0000262 Target.getPointerInfo(Size, Align, getFullLoc(L));
263 break;
264 case Type::Pointer:
265 Target.getPointerInfo(Size, Align, getFullLoc(L));
266 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000267 case Type::Reference:
268 // "When applied to a reference or a reference type, the result is the size
269 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
Chris Lattnerb66237b2007-12-19 19:23:28 +0000270 // FIXME: This is wrong for struct layout: a reference in a struct has
271 // pointer size.
Chris Lattner4b009652007-07-25 00:24:17 +0000272 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
273
274 case Type::Complex: {
275 // Complex types have the same alignment as their elements, but twice the
276 // size.
277 std::pair<uint64_t, unsigned> EltInfo =
278 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
279 Size = EltInfo.first*2;
280 Align = EltInfo.second;
281 break;
282 }
283 case Type::Tagged:
Chris Lattnereb56d292007-08-27 17:38:00 +0000284 TagType *TT = cast<TagType>(T);
285 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
Devang Patel7a78e432007-11-01 19:11:01 +0000286 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl(), L);
Chris Lattnereb56d292007-08-27 17:38:00 +0000287 Size = Layout.getSize();
288 Align = Layout.getAlignment();
289 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattner90a018d2007-08-28 18:24:31 +0000290 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattnereb56d292007-08-27 17:38:00 +0000291 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000292 assert(0 && "Unimplemented type sizes!");
Chris Lattnereb56d292007-08-27 17:38:00 +0000293 }
Chris Lattner4b009652007-07-25 00:24:17 +0000294 break;
295 }
296
297 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
298 return std::make_pair(Size, Align);
299}
300
Devang Patel7a78e432007-11-01 19:11:01 +0000301/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000302/// specified record (struct/union/class), which indicates its size and field
303/// position information.
Devang Patel7a78e432007-11-01 19:11:01 +0000304const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D,
305 SourceLocation L) {
Chris Lattner4b009652007-07-25 00:24:17 +0000306 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
307
308 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000309 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000310 if (Entry) return *Entry;
311
Devang Patel7a78e432007-11-01 19:11:01 +0000312 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
313 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
314 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000315 Entry = NewEntry;
316
317 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
318 uint64_t RecordSize = 0;
319 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
320
321 if (D->getKind() != Decl::Union) {
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000322 bool StructIsPacked = D->getAttr<PackedAttr>();
323
Chris Lattner4b009652007-07-25 00:24:17 +0000324 // Layout each field, for now, just sequentially, respecting alignment. In
325 // the future, this will need to be tweakable by targets.
326 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
327 const FieldDecl *FD = D->getMember(i);
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000328 bool FieldIsPacked = StructIsPacked || FD->getAttr<PackedAttr>();
Eli Friedman67571ac2008-02-06 05:33:51 +0000329 uint64_t FieldSize;
330 unsigned FieldAlign;
331 if (FD->getType()->isIncompleteType()) {
332 // This must be a flexible array member; we can't directly
333 // query getTypeInfo about these, so we figure it out here.
334 // Flexible array members don't have any size, but they
335 // have to be aligned appropriately for their element type.
336 const ArrayType* ATy = FD->getType()->getAsArrayType();
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000337 FieldAlign = FieldIsPacked ? 8 : getTypeAlign(ATy->getElementType(), L);
Eli Friedman67571ac2008-02-06 05:33:51 +0000338 FieldSize = 0;
339 } else {
340 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
341 FieldSize = FieldInfo.first;
Anders Carlsson8d2b2b72008-02-16 01:20:23 +0000342 FieldAlign = FieldIsPacked ? 8 : FieldInfo.second;
Eli Friedman67571ac2008-02-06 05:33:51 +0000343 }
344
Chris Lattner4b009652007-07-25 00:24:17 +0000345 // Round up the current record size to the field's alignment boundary.
346 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
347
348 // Place this field at the current location.
349 FieldOffsets[i] = RecordSize;
350
351 // Reserve space for this field.
352 RecordSize += FieldSize;
353
354 // Remember max struct/class alignment.
355 RecordAlign = std::max(RecordAlign, FieldAlign);
356 }
357
358 // Finally, round the size of the total struct up to the alignment of the
359 // struct itself.
360 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
361 } else {
362 // Union layout just puts each member at the start of the record.
363 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
364 const FieldDecl *FD = D->getMember(i);
365 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
366 uint64_t FieldSize = FieldInfo.first;
367 unsigned FieldAlign = FieldInfo.second;
368
369 // Round up the current record size to the field's alignment boundary.
370 RecordSize = std::max(RecordSize, FieldSize);
371
372 // Place this field at the start of the record.
373 FieldOffsets[i] = 0;
374
375 // Remember max struct/class alignment.
376 RecordAlign = std::max(RecordAlign, FieldAlign);
377 }
378 }
379
380 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
381 return *NewEntry;
382}
383
Chris Lattner4b009652007-07-25 00:24:17 +0000384//===----------------------------------------------------------------------===//
385// Type creation/memoization methods
386//===----------------------------------------------------------------------===//
387
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000388QualType ASTContext::getASQualType(QualType T, unsigned AddressSpace) {
389 // Check if we've already instantiated an address space qual'd type of this type.
390 llvm::FoldingSetNodeID ID;
391 ASQualType::Profile(ID, T, AddressSpace);
392 void *InsertPos = 0;
393 if (ASQualType *ASQy = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos))
394 return QualType(ASQy, 0);
395
396 // If the base type isn't canonical, this won't be a canonical type either,
397 // so fill in the canonical type field.
398 QualType Canonical;
399 if (!T->isCanonical()) {
400 Canonical = getASQualType(T.getCanonicalType(), AddressSpace);
401
402 // Get the new insert position for the node we care about.
403 ASQualType *NewIP = ASQualTypes.FindNodeOrInsertPos(ID, InsertPos);
404 assert(NewIP == 0 && "Shouldn't be in the map!");
405 }
406 ASQualType *New = new ASQualType(T, Canonical, AddressSpace);
407 ASQualTypes.InsertNode(New, InsertPos);
408 Types.push_back(New);
409 return QualType(New, 0);
410}
411
Chris Lattner4b009652007-07-25 00:24:17 +0000412
413/// getComplexType - Return the uniqued reference to the type for a complex
414/// number with the specified element type.
415QualType ASTContext::getComplexType(QualType T) {
416 // Unique pointers, to guarantee there is only one pointer of a particular
417 // structure.
418 llvm::FoldingSetNodeID ID;
419 ComplexType::Profile(ID, T);
420
421 void *InsertPos = 0;
422 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
423 return QualType(CT, 0);
424
425 // If the pointee type isn't canonical, this won't be a canonical type either,
426 // so fill in the canonical type field.
427 QualType Canonical;
428 if (!T->isCanonical()) {
429 Canonical = getComplexType(T.getCanonicalType());
430
431 // Get the new insert position for the node we care about.
432 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
433 assert(NewIP == 0 && "Shouldn't be in the map!");
434 }
435 ComplexType *New = new ComplexType(T, Canonical);
436 Types.push_back(New);
437 ComplexTypes.InsertNode(New, InsertPos);
438 return QualType(New, 0);
439}
440
441
442/// getPointerType - Return the uniqued reference to the type for a pointer to
443/// the specified type.
444QualType ASTContext::getPointerType(QualType T) {
445 // Unique pointers, to guarantee there is only one pointer of a particular
446 // structure.
447 llvm::FoldingSetNodeID ID;
448 PointerType::Profile(ID, T);
449
450 void *InsertPos = 0;
451 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
452 return QualType(PT, 0);
453
454 // If the pointee type isn't canonical, this won't be a canonical type either,
455 // so fill in the canonical type field.
456 QualType Canonical;
457 if (!T->isCanonical()) {
458 Canonical = getPointerType(T.getCanonicalType());
459
460 // Get the new insert position for the node we care about.
461 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
462 assert(NewIP == 0 && "Shouldn't be in the map!");
463 }
464 PointerType *New = new PointerType(T, Canonical);
465 Types.push_back(New);
466 PointerTypes.InsertNode(New, InsertPos);
467 return QualType(New, 0);
468}
469
470/// getReferenceType - Return the uniqued reference to the type for a reference
471/// to the specified type.
472QualType ASTContext::getReferenceType(QualType T) {
473 // Unique pointers, to guarantee there is only one pointer of a particular
474 // structure.
475 llvm::FoldingSetNodeID ID;
476 ReferenceType::Profile(ID, T);
477
478 void *InsertPos = 0;
479 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
480 return QualType(RT, 0);
481
482 // If the referencee type isn't canonical, this won't be a canonical type
483 // either, so fill in the canonical type field.
484 QualType Canonical;
485 if (!T->isCanonical()) {
486 Canonical = getReferenceType(T.getCanonicalType());
487
488 // Get the new insert position for the node we care about.
489 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
490 assert(NewIP == 0 && "Shouldn't be in the map!");
491 }
492
493 ReferenceType *New = new ReferenceType(T, Canonical);
494 Types.push_back(New);
495 ReferenceTypes.InsertNode(New, InsertPos);
496 return QualType(New, 0);
497}
498
Steve Naroff83c13012007-08-30 01:06:46 +0000499/// getConstantArrayType - Return the unique reference to the type for an
500/// array of the specified element type.
501QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000502 const llvm::APInt &ArySize,
503 ArrayType::ArraySizeModifier ASM,
504 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000505 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000506 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000507
508 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000509 if (ConstantArrayType *ATP =
510 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000511 return QualType(ATP, 0);
512
513 // If the element type isn't canonical, this won't be a canonical type either,
514 // so fill in the canonical type field.
515 QualType Canonical;
516 if (!EltTy->isCanonical()) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000517 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
518 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000519 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000520 ConstantArrayType *NewIP =
521 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
522
Chris Lattner4b009652007-07-25 00:24:17 +0000523 assert(NewIP == 0 && "Shouldn't be in the map!");
524 }
525
Steve Naroff24c9b982007-08-30 18:10:14 +0000526 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
527 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000528 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000529 Types.push_back(New);
530 return QualType(New, 0);
531}
532
Steve Naroffe2579e32007-08-30 18:14:25 +0000533/// getVariableArrayType - Returns a non-unique reference to the type for a
534/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000535QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
536 ArrayType::ArraySizeModifier ASM,
537 unsigned EltTypeQuals) {
Eli Friedman8ff07782008-02-15 18:16:39 +0000538 // Since we don't unique expressions, it isn't possible to unique VLA's
539 // that have an expression provided for their size.
540
541 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
542 ASM, EltTypeQuals);
543
544 VariableArrayTypes.push_back(New);
545 Types.push_back(New);
546 return QualType(New, 0);
547}
548
549QualType ASTContext::getIncompleteArrayType(QualType EltTy,
550 ArrayType::ArraySizeModifier ASM,
551 unsigned EltTypeQuals) {
552 llvm::FoldingSetNodeID ID;
553 IncompleteArrayType::Profile(ID, EltTy);
554
555 void *InsertPos = 0;
556 if (IncompleteArrayType *ATP =
557 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
558 return QualType(ATP, 0);
559
560 // If the element type isn't canonical, this won't be a canonical type
561 // either, so fill in the canonical type field.
562 QualType Canonical;
563
564 if (!EltTy->isCanonical()) {
565 Canonical = getIncompleteArrayType(EltTy.getCanonicalType(),
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000566 ASM, EltTypeQuals);
Eli Friedman8ff07782008-02-15 18:16:39 +0000567
568 // Get the new insert position for the node we care about.
569 IncompleteArrayType *NewIP =
570 IncompleteArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
571
572 assert(NewIP == 0 && "Shouldn't be in the map!");
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000573 }
Eli Friedman8ff07782008-02-15 18:16:39 +0000574
575 IncompleteArrayType *New = new IncompleteArrayType(EltTy, Canonical,
576 ASM, EltTypeQuals);
577
578 IncompleteArrayTypes.InsertNode(New, InsertPos);
579 Types.push_back(New);
580 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000581}
582
Chris Lattner4b009652007-07-25 00:24:17 +0000583/// getVectorType - Return the unique reference to a vector type of
584/// the specified element type and size. VectorType must be a built-in type.
585QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
586 BuiltinType *baseType;
587
588 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
589 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
590
591 // Check if we've already instantiated a vector of this type.
592 llvm::FoldingSetNodeID ID;
593 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
594 void *InsertPos = 0;
595 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
596 return QualType(VTP, 0);
597
598 // If the element type isn't canonical, this won't be a canonical type either,
599 // so fill in the canonical type field.
600 QualType Canonical;
601 if (!vecType->isCanonical()) {
602 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
603
604 // Get the new insert position for the node we care about.
605 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
606 assert(NewIP == 0 && "Shouldn't be in the map!");
607 }
608 VectorType *New = new VectorType(vecType, NumElts, Canonical);
609 VectorTypes.InsertNode(New, InsertPos);
610 Types.push_back(New);
611 return QualType(New, 0);
612}
613
614/// getOCUVectorType - Return the unique reference to an OCU vector type of
615/// the specified element type and size. VectorType must be a built-in type.
616QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
617 BuiltinType *baseType;
618
619 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
620 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
621
622 // Check if we've already instantiated a vector of this type.
623 llvm::FoldingSetNodeID ID;
624 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
625 void *InsertPos = 0;
626 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
627 return QualType(VTP, 0);
628
629 // If the element type isn't canonical, this won't be a canonical type either,
630 // so fill in the canonical type field.
631 QualType Canonical;
632 if (!vecType->isCanonical()) {
633 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
634
635 // Get the new insert position for the node we care about.
636 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
637 assert(NewIP == 0 && "Shouldn't be in the map!");
638 }
639 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
640 VectorTypes.InsertNode(New, InsertPos);
641 Types.push_back(New);
642 return QualType(New, 0);
643}
644
645/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
646///
647QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
648 // Unique functions, to guarantee there is only one function of a particular
649 // structure.
650 llvm::FoldingSetNodeID ID;
651 FunctionTypeNoProto::Profile(ID, ResultTy);
652
653 void *InsertPos = 0;
654 if (FunctionTypeNoProto *FT =
655 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
656 return QualType(FT, 0);
657
658 QualType Canonical;
659 if (!ResultTy->isCanonical()) {
660 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
661
662 // Get the new insert position for the node we care about.
663 FunctionTypeNoProto *NewIP =
664 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
665 assert(NewIP == 0 && "Shouldn't be in the map!");
666 }
667
668 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
669 Types.push_back(New);
670 FunctionTypeProtos.InsertNode(New, InsertPos);
671 return QualType(New, 0);
672}
673
674/// getFunctionType - Return a normal function type with a typed argument
675/// list. isVariadic indicates whether the argument list includes '...'.
676QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
677 unsigned NumArgs, bool isVariadic) {
678 // Unique functions, to guarantee there is only one function of a particular
679 // structure.
680 llvm::FoldingSetNodeID ID;
681 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
682
683 void *InsertPos = 0;
684 if (FunctionTypeProto *FTP =
685 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
686 return QualType(FTP, 0);
687
688 // Determine whether the type being created is already canonical or not.
689 bool isCanonical = ResultTy->isCanonical();
690 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
691 if (!ArgArray[i]->isCanonical())
692 isCanonical = false;
693
694 // If this type isn't canonical, get the canonical version of it.
695 QualType Canonical;
696 if (!isCanonical) {
697 llvm::SmallVector<QualType, 16> CanonicalArgs;
698 CanonicalArgs.reserve(NumArgs);
699 for (unsigned i = 0; i != NumArgs; ++i)
700 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
701
702 Canonical = getFunctionType(ResultTy.getCanonicalType(),
703 &CanonicalArgs[0], NumArgs,
704 isVariadic);
705
706 // Get the new insert position for the node we care about.
707 FunctionTypeProto *NewIP =
708 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
709 assert(NewIP == 0 && "Shouldn't be in the map!");
710 }
711
712 // FunctionTypeProto objects are not allocated with new because they have a
713 // variable size array (for parameter types) at the end of them.
714 FunctionTypeProto *FTP =
715 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
716 NumArgs*sizeof(QualType));
717 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
718 Canonical);
719 Types.push_back(FTP);
720 FunctionTypeProtos.InsertNode(FTP, InsertPos);
721 return QualType(FTP, 0);
722}
723
724/// getTypedefType - Return the unique reference to the type for the
725/// specified typename decl.
726QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
727 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
728
729 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000730 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000731 Types.push_back(Decl->TypeForDecl);
732 return QualType(Decl->TypeForDecl, 0);
733}
734
Ted Kremenek42730c52008-01-07 19:49:32 +0000735/// getObjCInterfaceType - Return the unique reference to the type for the
Steve Naroff81f1bba2007-09-06 21:24:23 +0000736/// specified ObjC interface decl.
Ted Kremenek42730c52008-01-07 19:49:32 +0000737QualType ASTContext::getObjCInterfaceType(ObjCInterfaceDecl *Decl) {
Steve Naroff81f1bba2007-09-06 21:24:23 +0000738 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
739
Ted Kremenek42730c52008-01-07 19:49:32 +0000740 Decl->TypeForDecl = new ObjCInterfaceType(Type::ObjCInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000741 Types.push_back(Decl->TypeForDecl);
742 return QualType(Decl->TypeForDecl, 0);
743}
744
Ted Kremenek42730c52008-01-07 19:49:32 +0000745/// getObjCQualifiedInterfaceType - Return a
746/// ObjCQualifiedInterfaceType type for the given interface decl and
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000747/// the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000748QualType ASTContext::getObjCQualifiedInterfaceType(ObjCInterfaceDecl *Decl,
749 ObjCProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000750 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000751 ObjCQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000752
753 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000754 if (ObjCQualifiedInterfaceType *QT =
755 ObjCQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000756 return QualType(QT, 0);
757
758 // No Match;
Ted Kremenek42730c52008-01-07 19:49:32 +0000759 ObjCQualifiedInterfaceType *QType =
760 new ObjCQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000761 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000762 ObjCQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000763 return QualType(QType, 0);
764}
765
Ted Kremenek42730c52008-01-07 19:49:32 +0000766/// getObjCQualifiedIdType - Return a
767/// getObjCQualifiedIdType type for the 'id' decl and
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000768/// the conforming protocol list.
Ted Kremenek42730c52008-01-07 19:49:32 +0000769QualType ASTContext::getObjCQualifiedIdType(QualType idType,
770 ObjCProtocolDecl **Protocols,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000771 unsigned NumProtocols) {
772 llvm::FoldingSetNodeID ID;
Ted Kremenek42730c52008-01-07 19:49:32 +0000773 ObjCQualifiedIdType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000774
775 void *InsertPos = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +0000776 if (ObjCQualifiedIdType *QT =
777 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000778 return QualType(QT, 0);
779
780 // No Match;
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000781 QualType Canonical;
782 if (!idType->isCanonical()) {
Ted Kremenek42730c52008-01-07 19:49:32 +0000783 Canonical = getObjCQualifiedIdType(idType.getCanonicalType(),
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000784 Protocols, NumProtocols);
Ted Kremenek42730c52008-01-07 19:49:32 +0000785 ObjCQualifiedIdType *NewQT =
786 ObjCQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000787 assert(NewQT == 0 && "Shouldn't be in the map!");
788 }
789
Ted Kremenek42730c52008-01-07 19:49:32 +0000790 ObjCQualifiedIdType *QType =
791 new ObjCQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000792 Types.push_back(QType);
Ted Kremenek42730c52008-01-07 19:49:32 +0000793 ObjCQualifiedIdTypes.InsertNode(QType, InsertPos);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000794 return QualType(QType, 0);
795}
796
Steve Naroff0604dd92007-08-01 18:02:17 +0000797/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
798/// TypeOfExpr AST's (since expression's are never shared). For example,
799/// multiple declarations that refer to "typeof(x)" all contain different
800/// DeclRefExpr's. This doesn't effect the type checker, since it operates
801/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000802QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroff7cbb1462007-07-31 12:34:36 +0000803 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000804 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
805 Types.push_back(toe);
806 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000807}
808
Steve Naroff0604dd92007-08-01 18:02:17 +0000809/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
810/// TypeOfType AST's. The only motivation to unique these nodes would be
811/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
812/// an issue. This doesn't effect the type checker, since it operates
813/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000814QualType ASTContext::getTypeOfType(QualType tofType) {
815 QualType Canonical = tofType.getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000816 TypeOfType *tot = new TypeOfType(tofType, Canonical);
817 Types.push_back(tot);
818 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000819}
820
Chris Lattner4b009652007-07-25 00:24:17 +0000821/// getTagDeclType - Return the unique reference to the type for the
822/// specified TagDecl (struct/union/class/enum) decl.
823QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000824 assert (Decl);
825
Ted Kremenekf05026d2007-11-14 00:03:20 +0000826 // The decl stores the type cache.
Ted Kremenekae8fa032007-11-26 21:16:01 +0000827 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekf05026d2007-11-14 00:03:20 +0000828
829 TagType* T = new TagType(Decl, QualType());
Ted Kremenekae8fa032007-11-26 21:16:01 +0000830 Types.push_back(T);
831 Decl->TypeForDecl = T;
Ted Kremenekf05026d2007-11-14 00:03:20 +0000832
833 return QualType(T, 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000834}
835
836/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
837/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
838/// needs to agree with the definition in <stddef.h>.
839QualType ASTContext::getSizeType() const {
840 // On Darwin, size_t is defined as a "long unsigned int".
841 // FIXME: should derive from "Target".
842 return UnsignedLongTy;
843}
844
Eli Friedmanfdd35d72008-02-12 08:29:21 +0000845/// getWcharType - Return the unique type for "wchar_t" (C99 7.17), the
846/// width of characters in wide strings, The value is target dependent and
847/// needs to agree with the definition in <stddef.h>.
848QualType ASTContext::getWcharType() const {
849 // On Darwin, wchar_t is defined as a "int".
850 // FIXME: should derive from "Target".
851 return IntTy;
852}
853
Chris Lattner4b009652007-07-25 00:24:17 +0000854/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
855/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
856QualType ASTContext::getPointerDiffType() const {
857 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
858 // FIXME: should derive from "Target".
859 return IntTy;
860}
861
862/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
863/// routine will assert if passed a built-in type that isn't an integer or enum.
864static int getIntegerRank(QualType t) {
865 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
866 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
867 return 4;
868 }
869
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000870 const BuiltinType *BT = t.getCanonicalType()->getAsBuiltinType();
Chris Lattner4b009652007-07-25 00:24:17 +0000871 switch (BT->getKind()) {
872 default:
873 assert(0 && "getIntegerRank(): not a built-in integer");
874 case BuiltinType::Bool:
875 return 1;
876 case BuiltinType::Char_S:
877 case BuiltinType::Char_U:
878 case BuiltinType::SChar:
879 case BuiltinType::UChar:
880 return 2;
881 case BuiltinType::Short:
882 case BuiltinType::UShort:
883 return 3;
884 case BuiltinType::Int:
885 case BuiltinType::UInt:
886 return 4;
887 case BuiltinType::Long:
888 case BuiltinType::ULong:
889 return 5;
890 case BuiltinType::LongLong:
891 case BuiltinType::ULongLong:
892 return 6;
893 }
894}
895
896/// getFloatingRank - Return a relative rank for floating point types.
897/// This routine will assert if passed a built-in type that isn't a float.
898static int getFloatingRank(QualType T) {
899 T = T.getCanonicalType();
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000900 if (const ComplexType *CT = T->getAsComplexType())
Chris Lattner4b009652007-07-25 00:24:17 +0000901 return getFloatingRank(CT->getElementType());
902
Christopher Lamb2a72bb32008-02-04 02:31:56 +0000903 switch (T->getAsBuiltinType()->getKind()) {
Chris Lattner5003e8b2007-11-01 05:03:41 +0000904 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +0000905 case BuiltinType::Float: return FloatRank;
906 case BuiltinType::Double: return DoubleRank;
907 case BuiltinType::LongDouble: return LongDoubleRank;
908 }
909}
910
Steve Narofffa0c4532007-08-27 01:41:48 +0000911/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
912/// point or a complex type (based on typeDomain/typeSize).
913/// 'typeDomain' is a real floating point or complex type.
914/// 'typeSize' is a real floating point or complex type.
Steve Naroff3cf497f2007-08-27 01:27:54 +0000915QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
916 QualType typeSize, QualType typeDomain) const {
917 if (typeDomain->isComplexType()) {
918 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000919 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000920 case FloatRank: return FloatComplexTy;
921 case DoubleRank: return DoubleComplexTy;
922 case LongDoubleRank: return LongDoubleComplexTy;
923 }
Chris Lattner4b009652007-07-25 00:24:17 +0000924 }
Steve Naroff3cf497f2007-08-27 01:27:54 +0000925 if (typeDomain->isRealFloatingType()) {
926 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000927 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000928 case FloatRank: return FloatTy;
929 case DoubleRank: return DoubleTy;
930 case LongDoubleRank: return LongDoubleTy;
931 }
932 }
933 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000934 //an invalid return value, but the assert
935 //will ensure that this code is never reached.
936 return VoidTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000937}
938
Steve Naroff45fc9822007-08-27 15:30:22 +0000939/// compareFloatingType - Handles 3 different combos:
940/// float/float, float/complex, complex/complex.
941/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
942int ASTContext::compareFloatingType(QualType lt, QualType rt) {
943 if (getFloatingRank(lt) == getFloatingRank(rt))
944 return 0;
945 if (getFloatingRank(lt) > getFloatingRank(rt))
946 return 1;
947 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +0000948}
949
950// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
951// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
952QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
953 if (lhs == rhs) return lhs;
954
955 bool t1Unsigned = lhs->isUnsignedIntegerType();
956 bool t2Unsigned = rhs->isUnsignedIntegerType();
957
958 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
959 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
960
961 // We have two integer types with differing signs
962 QualType unsignedType = t1Unsigned ? lhs : rhs;
963 QualType signedType = t1Unsigned ? rhs : lhs;
964
965 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
966 return unsignedType;
967 else {
968 // FIXME: Need to check if the signed type can represent all values of the
969 // unsigned type. If it can, then the result is the signed type.
970 // If it can't, then the result is the unsigned version of the signed type.
971 // Should probably add a helper that returns a signed integer type from
972 // an unsigned (and vice versa). C99 6.3.1.8.
973 return signedType;
974 }
975}
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000976
977// getCFConstantStringType - Return the type used for constant CFStrings.
978QualType ASTContext::getCFConstantStringType() {
979 if (!CFConstantStringTypeDecl) {
980 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroff0add5d22007-11-03 11:27:19 +0000981 &Idents.get("NSConstantString"),
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000982 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000983 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000984
985 // const int *isa;
986 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000987 // int flags;
988 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000989 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000990 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000991 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000992 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000993 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000994 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000995
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000996 for (unsigned i = 0; i < 4; ++i)
Steve Naroffdc1ad762007-09-14 02:20:46 +0000997 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000998
999 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
1000 }
1001
1002 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +00001003}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001004
Anders Carlssone3f02572007-10-29 06:33:42 +00001005// This returns true if a type has been typedefed to BOOL:
1006// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +00001007static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +00001008 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +00001009 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001010
1011 return false;
1012}
1013
Ted Kremenek42730c52008-01-07 19:49:32 +00001014/// getObjCEncodingTypeSize returns size of type for objective-c encoding
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001015/// purpose.
Ted Kremenek42730c52008-01-07 19:49:32 +00001016int ASTContext::getObjCEncodingTypeSize(QualType type) {
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001017 SourceLocation Loc;
1018 uint64_t sz = getTypeSize(type, Loc);
1019
1020 // Make all integer and enum types at least as large as an int
1021 if (sz > 0 && type->isIntegralType())
1022 sz = std::max(sz, getTypeSize(IntTy, Loc));
1023 // Treat arrays as pointers, since that's how they're passed in.
1024 else if (type->isArrayType())
1025 sz = getTypeSize(VoidPtrTy, Loc);
1026 return sz / getTypeSize(CharTy, Loc);
1027}
1028
Ted Kremenek42730c52008-01-07 19:49:32 +00001029/// getObjCEncodingForMethodDecl - Return the encoded type for this method
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001030/// declaration.
Ted Kremenek42730c52008-01-07 19:49:32 +00001031void ASTContext::getObjCEncodingForMethodDecl(ObjCMethodDecl *Decl,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001032 std::string& S)
1033{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001034 // Encode type qualifer, 'in', 'inout', etc. for the return type.
Ted Kremenek42730c52008-01-07 19:49:32 +00001035 getObjCEncodingForTypeQualifier(Decl->getObjCDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001036 // Encode result type.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001037 getObjCEncodingForType(Decl->getResultType(), S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001038 // Compute size of all parameters.
1039 // Start with computing size of a pointer in number of bytes.
1040 // FIXME: There might(should) be a better way of doing this computation!
1041 SourceLocation Loc;
1042 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
1043 // The first two arguments (self and _cmd) are pointers; account for
1044 // their size.
1045 int ParmOffset = 2 * PtrSize;
1046 int NumOfParams = Decl->getNumParams();
1047 for (int i = 0; i < NumOfParams; i++) {
1048 QualType PType = Decl->getParamDecl(i)->getType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001049 int sz = getObjCEncodingTypeSize (PType);
1050 assert (sz > 0 && "getObjCEncodingForMethodDecl - Incomplete param type");
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001051 ParmOffset += sz;
1052 }
1053 S += llvm::utostr(ParmOffset);
1054 S += "@0:";
1055 S += llvm::utostr(PtrSize);
1056
1057 // Argument types.
1058 ParmOffset = 2 * PtrSize;
1059 for (int i = 0; i < NumOfParams; i++) {
1060 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001061 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001062 // 'in', 'inout', etc.
Ted Kremenek42730c52008-01-07 19:49:32 +00001063 getObjCEncodingForTypeQualifier(
1064 Decl->getParamDecl(i)->getObjCDeclQualifier(), S);
Fariborz Jahanian248db262008-01-22 22:44:46 +00001065 getObjCEncodingForType(PType, S, EncodingRecordTypes);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001066 S += llvm::utostr(ParmOffset);
Ted Kremenek42730c52008-01-07 19:49:32 +00001067 ParmOffset += getObjCEncodingTypeSize(PType);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001068 }
1069}
1070
Fariborz Jahanian248db262008-01-22 22:44:46 +00001071void ASTContext::getObjCEncodingForType(QualType T, std::string& S,
1072 llvm::SmallVector<const RecordType *, 8> &ERType) const
Anders Carlsson36f07d82007-10-29 05:01:08 +00001073{
Anders Carlssone3f02572007-10-29 06:33:42 +00001074 // FIXME: This currently doesn't encode:
1075 // @ An object (whether statically typed or typed id)
1076 // # A class object (Class)
1077 // : A method selector (SEL)
1078 // {name=type...} A structure
1079 // (name=type...) A union
1080 // bnum A bit field of num bits
1081
1082 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001083 char encoding;
1084 switch (BT->getKind()) {
1085 case BuiltinType::Void:
1086 encoding = 'v';
1087 break;
1088 case BuiltinType::Bool:
1089 encoding = 'B';
1090 break;
1091 case BuiltinType::Char_U:
1092 case BuiltinType::UChar:
1093 encoding = 'C';
1094 break;
1095 case BuiltinType::UShort:
1096 encoding = 'S';
1097 break;
1098 case BuiltinType::UInt:
1099 encoding = 'I';
1100 break;
1101 case BuiltinType::ULong:
1102 encoding = 'L';
1103 break;
1104 case BuiltinType::ULongLong:
1105 encoding = 'Q';
1106 break;
1107 case BuiltinType::Char_S:
1108 case BuiltinType::SChar:
1109 encoding = 'c';
1110 break;
1111 case BuiltinType::Short:
1112 encoding = 's';
1113 break;
1114 case BuiltinType::Int:
1115 encoding = 'i';
1116 break;
1117 case BuiltinType::Long:
1118 encoding = 'l';
1119 break;
1120 case BuiltinType::LongLong:
1121 encoding = 'q';
1122 break;
1123 case BuiltinType::Float:
1124 encoding = 'f';
1125 break;
1126 case BuiltinType::Double:
1127 encoding = 'd';
1128 break;
1129 case BuiltinType::LongDouble:
1130 encoding = 'd';
1131 break;
1132 default:
1133 assert(0 && "Unhandled builtin type kind");
1134 }
1135
1136 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001137 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001138 else if (T->isObjCQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001139 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahanian248db262008-01-22 22:44:46 +00001140 return getObjCEncodingForType(getObjCIdType(), S, ERType);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001141
1142 }
1143 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001144 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001145 if (isObjCIdType(PointeeTy) || PointeeTy->isObjCInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001146 S += '@';
1147 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001148 } else if (isObjCClassType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001149 S += '#';
1150 return;
Ted Kremenek42730c52008-01-07 19:49:32 +00001151 } else if (isObjCSelType(PointeeTy)) {
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001152 S += ':';
1153 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001154 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001155
1156 if (PointeeTy->isCharType()) {
1157 // char pointer types should be encoded as '*' unless it is a
1158 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001159 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001160 S += '*';
1161 return;
1162 }
1163 }
1164
1165 S += '^';
Fariborz Jahanian248db262008-01-22 22:44:46 +00001166 getObjCEncodingForType(PT->getPointeeType(), S, ERType);
Anders Carlssone3f02572007-10-29 06:33:42 +00001167 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001168 S += '[';
1169
1170 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1171 S += llvm::utostr(CAT->getSize().getZExtValue());
1172 else
1173 assert(0 && "Unhandled array type!");
1174
Fariborz Jahanian248db262008-01-22 22:44:46 +00001175 getObjCEncodingForType(AT->getElementType(), S, ERType);
Anders Carlsson36f07d82007-10-29 05:01:08 +00001176 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001177 } else if (T->getAsFunctionType()) {
1178 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001179 } else if (const RecordType *RTy = T->getAsRecordType()) {
1180 RecordDecl *RDecl= RTy->getDecl();
1181 S += '{';
1182 S += RDecl->getName();
Fariborz Jahanian248db262008-01-22 22:44:46 +00001183 bool found = false;
1184 for (unsigned i = 0, e = ERType.size(); i != e; ++i)
1185 if (ERType[i] == RTy) {
1186 found = true;
1187 break;
1188 }
1189 if (!found) {
1190 ERType.push_back(RTy);
1191 S += '=';
1192 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1193 FieldDecl *field = RDecl->getMember(i);
1194 getObjCEncodingForType(field->getType(), S, ERType);
1195 }
1196 assert(ERType.back() == RTy && "Record Type stack mismatch.");
1197 ERType.pop_back();
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001198 }
1199 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001200 } else if (T->isEnumeralType()) {
1201 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001202 } else
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001203 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001204}
1205
Ted Kremenek42730c52008-01-07 19:49:32 +00001206void ASTContext::getObjCEncodingForTypeQualifier(Decl::ObjCDeclQualifier QT,
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001207 std::string& S) const {
1208 if (QT & Decl::OBJC_TQ_In)
1209 S += 'n';
1210 if (QT & Decl::OBJC_TQ_Inout)
1211 S += 'N';
1212 if (QT & Decl::OBJC_TQ_Out)
1213 S += 'o';
1214 if (QT & Decl::OBJC_TQ_Bycopy)
1215 S += 'O';
1216 if (QT & Decl::OBJC_TQ_Byref)
1217 S += 'R';
1218 if (QT & Decl::OBJC_TQ_Oneway)
1219 S += 'V';
1220}
1221
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001222void ASTContext::setBuiltinVaListType(QualType T)
1223{
1224 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1225
1226 BuiltinVaListType = T;
1227}
1228
Ted Kremenek42730c52008-01-07 19:49:32 +00001229void ASTContext::setObjCIdType(TypedefDecl *TD)
Steve Naroff9d12c902007-10-15 14:41:52 +00001230{
Ted Kremenek42730c52008-01-07 19:49:32 +00001231 assert(ObjCIdType.isNull() && "'id' type already set!");
Steve Naroff9d12c902007-10-15 14:41:52 +00001232
Ted Kremenek42730c52008-01-07 19:49:32 +00001233 ObjCIdType = getTypedefType(TD);
Steve Naroff9d12c902007-10-15 14:41:52 +00001234
1235 // typedef struct objc_object *id;
1236 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1237 assert(ptr && "'id' incorrectly typed");
1238 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1239 assert(rec && "'id' incorrectly typed");
1240 IdStructType = rec;
1241}
1242
Ted Kremenek42730c52008-01-07 19:49:32 +00001243void ASTContext::setObjCSelType(TypedefDecl *TD)
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001244{
Ted Kremenek42730c52008-01-07 19:49:32 +00001245 assert(ObjCSelType.isNull() && "'SEL' type already set!");
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001246
Ted Kremenek42730c52008-01-07 19:49:32 +00001247 ObjCSelType = getTypedefType(TD);
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001248
1249 // typedef struct objc_selector *SEL;
1250 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1251 assert(ptr && "'SEL' incorrectly typed");
1252 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1253 assert(rec && "'SEL' incorrectly typed");
1254 SelStructType = rec;
1255}
1256
Ted Kremenek42730c52008-01-07 19:49:32 +00001257void ASTContext::setObjCProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001258{
Ted Kremenek42730c52008-01-07 19:49:32 +00001259 assert(ObjCProtoType.isNull() && "'Protocol' type already set!");
1260 ObjCProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001261}
1262
Ted Kremenek42730c52008-01-07 19:49:32 +00001263void ASTContext::setObjCClassType(TypedefDecl *TD)
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001264{
Ted Kremenek42730c52008-01-07 19:49:32 +00001265 assert(ObjCClassType.isNull() && "'Class' type already set!");
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001266
Ted Kremenek42730c52008-01-07 19:49:32 +00001267 ObjCClassType = getTypedefType(TD);
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001268
1269 // typedef struct objc_class *Class;
1270 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1271 assert(ptr && "'Class' incorrectly typed");
1272 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1273 assert(rec && "'Class' incorrectly typed");
1274 ClassStructType = rec;
1275}
1276
Ted Kremenek42730c52008-01-07 19:49:32 +00001277void ASTContext::setObjCConstantStringInterface(ObjCInterfaceDecl *Decl) {
1278 assert(ObjCConstantStringType.isNull() &&
Steve Narofff2e30312007-10-15 23:35:17 +00001279 "'NSConstantString' type already set!");
1280
Ted Kremenek42730c52008-01-07 19:49:32 +00001281 ObjCConstantStringType = getObjCInterfaceType(Decl);
Steve Narofff2e30312007-10-15 23:35:17 +00001282}
1283
Steve Naroff85f0dc52007-10-15 20:41:53 +00001284bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1285 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1286 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1287
1288 return lBuiltin->getKind() == rBuiltin->getKind();
1289}
1290
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001291/// objcTypesAreCompatible - This routine is called when two types
1292/// are of different class; one is interface type or is
1293/// a qualified interface type and the other type is of a different class.
1294/// Example, II or II<P>.
Steve Naroff85f0dc52007-10-15 20:41:53 +00001295bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001296 if (lhs->isObjCInterfaceType() && isObjCIdType(rhs))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001297 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001298 else if (isObjCIdType(lhs) && rhs->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001299 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001300 if (ObjCInterfaceType *lhsIT =
1301 dyn_cast<ObjCInterfaceType>(lhs.getCanonicalType().getTypePtr())) {
1302 ObjCQualifiedInterfaceType *rhsQI =
1303 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001304 return rhsQI && (lhsIT->getDecl() == rhsQI->getDecl());
1305 }
Ted Kremenek42730c52008-01-07 19:49:32 +00001306 else if (ObjCInterfaceType *rhsIT =
1307 dyn_cast<ObjCInterfaceType>(rhs.getCanonicalType().getTypePtr())) {
1308 ObjCQualifiedInterfaceType *lhsQI =
1309 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian274dbf02007-12-21 17:34:43 +00001310 return lhsQI && (rhsIT->getDecl() == lhsQI->getDecl());
1311 }
Steve Naroff85f0dc52007-10-15 20:41:53 +00001312 return false;
1313}
1314
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001315/// Check that 'lhs' and 'rhs' are compatible interface types. Both types
1316/// must be canonical types.
Steve Naroff85f0dc52007-10-15 20:41:53 +00001317bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001318 assert (lhs->isCanonical() &&
1319 "interfaceTypesAreCompatible strip typedefs of lhs");
1320 assert (rhs->isCanonical() &&
1321 "interfaceTypesAreCompatible strip typedefs of rhs");
Fariborz Jahaniance2de812007-12-20 22:37:58 +00001322 if (lhs == rhs)
1323 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001324 ObjCInterfaceType *lhsIT = cast<ObjCInterfaceType>(lhs.getTypePtr());
1325 ObjCInterfaceType *rhsIT = cast<ObjCInterfaceType>(rhs.getTypePtr());
1326 ObjCInterfaceDecl *rhsIDecl = rhsIT->getDecl();
1327 ObjCInterfaceDecl *lhsIDecl = lhsIT->getDecl();
Fariborz Jahaniance2de812007-12-20 22:37:58 +00001328 // rhs is derived from lhs it is OK; else it is not OK.
1329 while (rhsIDecl != NULL) {
1330 if (rhsIDecl == lhsIDecl)
1331 return true;
1332 rhsIDecl = rhsIDecl->getSuperClass();
1333 }
1334 return false;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001335}
1336
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001337bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1338 QualType rhs) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001339 ObjCQualifiedInterfaceType *lhsQI =
1340 dyn_cast<ObjCQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001341 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
Ted Kremenek42730c52008-01-07 19:49:32 +00001342 ObjCQualifiedInterfaceType *rhsQI =
1343 dyn_cast<ObjCQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001344 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanian9b842422008-01-07 20:12:21 +00001345 if (!interfaceTypesAreCompatible(
1346 getObjCInterfaceType(lhsQI->getDecl()).getCanonicalType(),
1347 getObjCInterfaceType(rhsQI->getDecl()).getCanonicalType()))
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001348 return false;
1349 /* All protocols in lhs must have a presense in rhs. */
1350 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1351 bool match = false;
Ted Kremenek42730c52008-01-07 19:49:32 +00001352 ObjCProtocolDecl *lhsProto = lhsQI->getProtocols(i);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001353 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001354 ObjCProtocolDecl *rhsProto = rhsQI->getProtocols(j);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001355 if (lhsProto == rhsProto) {
1356 match = true;
1357 break;
1358 }
1359 }
1360 if (!match)
1361 return false;
1362 }
1363 return true;
1364}
1365
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001366/// ProtocolCompatibleWithProtocol - return 'true' if 'lProto' is in the
1367/// inheritance hierarchy of 'rProto'.
Ted Kremenek42730c52008-01-07 19:49:32 +00001368static bool ProtocolCompatibleWithProtocol(ObjCProtocolDecl *lProto,
1369 ObjCProtocolDecl *rProto) {
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001370 if (lProto == rProto)
1371 return true;
Ted Kremenek42730c52008-01-07 19:49:32 +00001372 ObjCProtocolDecl** RefPDecl = rProto->getReferencedProtocols();
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001373 for (unsigned i = 0; i < rProto->getNumReferencedProtocols(); i++)
1374 if (ProtocolCompatibleWithProtocol(lProto, RefPDecl[i]))
1375 return true;
1376 return false;
1377}
1378
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001379/// ClassImplementsProtocol - Checks that 'lProto' protocol
1380/// has been implemented in IDecl class, its super class or categories (if
1381/// lookupCategory is true).
Ted Kremenek42730c52008-01-07 19:49:32 +00001382static bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1383 ObjCInterfaceDecl *IDecl,
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001384 bool lookupCategory) {
1385
1386 // 1st, look up the class.
Ted Kremenek42730c52008-01-07 19:49:32 +00001387 ObjCProtocolDecl **protoList = IDecl->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001388 for (unsigned i = 0; i < IDecl->getNumIntfRefProtocols(); i++) {
1389 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1390 return true;
1391 }
1392
1393 // 2nd, look up the category.
1394 if (lookupCategory)
Ted Kremenek42730c52008-01-07 19:49:32 +00001395 for (ObjCCategoryDecl *CDecl = IDecl->getCategoryList(); CDecl;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001396 CDecl = CDecl->getNextClassCategory()) {
1397 protoList = CDecl->getReferencedProtocols();
1398 for (unsigned i = 0; i < CDecl->getNumReferencedProtocols(); i++) {
1399 if (ProtocolCompatibleWithProtocol(lProto, protoList[i]))
1400 return true;
1401 }
1402 }
1403
1404 // 3rd, look up the super class(s)
1405 if (IDecl->getSuperClass())
1406 return
1407 ClassImplementsProtocol(lProto, IDecl->getSuperClass(), lookupCategory);
1408
1409 return false;
1410}
1411
Ted Kremenek42730c52008-01-07 19:49:32 +00001412/// ObjCQualifiedIdTypesAreCompatible - Compares two types, at least
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001413/// one of which is a protocol qualified 'id' type. When 'compare'
1414/// is true it is for comparison; when false, for assignment/initialization.
Ted Kremenek42730c52008-01-07 19:49:32 +00001415bool ASTContext::ObjCQualifiedIdTypesAreCompatible(QualType lhs,
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001416 QualType rhs,
1417 bool compare) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001418 // match id<P..> with an 'id' type in all cases.
1419 if (const PointerType *PT = lhs->getAsPointerType()) {
1420 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001421 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001422 return true;
1423
1424 }
1425 else if (const PointerType *PT = rhs->getAsPointerType()) {
1426 QualType PointeeTy = PT->getPointeeType();
Ted Kremenek42730c52008-01-07 19:49:32 +00001427 if (isObjCIdType(PointeeTy) || PointeeTy->isVoidType())
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001428 return true;
1429
1430 }
1431
Ted Kremenek42730c52008-01-07 19:49:32 +00001432 ObjCQualifiedInterfaceType *lhsQI = 0;
1433 ObjCQualifiedInterfaceType *rhsQI = 0;
1434 ObjCInterfaceDecl *lhsID = 0;
1435 ObjCInterfaceDecl *rhsID = 0;
1436 ObjCQualifiedIdType *lhsQID = dyn_cast<ObjCQualifiedIdType>(lhs);
1437 ObjCQualifiedIdType *rhsQID = dyn_cast<ObjCQualifiedIdType>(rhs);
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001438
1439 if (lhsQID) {
1440 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1441 QualType rtype =
1442 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1443 rhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001444 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001445 rtype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001446 if (!rhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001447 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001448 rtype.getCanonicalType().getTypePtr());
1449 if (IT)
1450 rhsID = IT->getDecl();
1451 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001452 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001453 if (!rhsQI && !rhsQID && !rhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001454 return false;
1455
Fariborz Jahaniance5528d2008-01-03 20:01:35 +00001456 unsigned numRhsProtocols = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001457 ObjCProtocolDecl **rhsProtoList = 0;
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001458 if (rhsQI) {
1459 numRhsProtocols = rhsQI->getNumProtocols();
1460 rhsProtoList = rhsQI->getReferencedProtocols();
1461 }
1462 else if (rhsQID) {
1463 numRhsProtocols = rhsQID->getNumProtocols();
1464 rhsProtoList = rhsQID->getReferencedProtocols();
1465 }
1466
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001467 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001468 ObjCProtocolDecl *lhsProto = lhsQID->getProtocols(i);
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001469 bool match = false;
1470
1471 // when comparing an id<P> on lhs with a static type on rhs,
1472 // see if static class implements all of id's protocols, directly or
1473 // through its super class and categories.
1474 if (rhsID) {
1475 if (ClassImplementsProtocol(lhsProto, rhsID, true))
1476 match = true;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001477 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001478 else for (unsigned j = 0; j < numRhsProtocols; j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001479 ObjCProtocolDecl *rhsProto = rhsProtoList[j];
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001480 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1481 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001482 match = true;
1483 break;
1484 }
1485 }
1486 if (!match)
1487 return false;
1488 }
1489 }
1490 else if (rhsQID) {
1491 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1492 QualType ltype =
1493 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1494 lhsQI =
Ted Kremenek42730c52008-01-07 19:49:32 +00001495 dyn_cast<ObjCQualifiedInterfaceType>(
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001496 ltype.getCanonicalType().getTypePtr());
Fariborz Jahanian87829072007-12-20 19:24:10 +00001497 if (!lhsQI) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001498 ObjCInterfaceType *IT = dyn_cast<ObjCInterfaceType>(
Fariborz Jahanian87829072007-12-20 19:24:10 +00001499 ltype.getCanonicalType().getTypePtr());
1500 if (IT)
1501 lhsID = IT->getDecl();
1502 }
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001503 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001504 if (!lhsQI && !lhsQID && !lhsID)
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001505 return false;
Fariborz Jahanian87829072007-12-20 19:24:10 +00001506
Fariborz Jahaniance5528d2008-01-03 20:01:35 +00001507 unsigned numLhsProtocols = 0;
Ted Kremenek42730c52008-01-07 19:49:32 +00001508 ObjCProtocolDecl **lhsProtoList = 0;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001509 if (lhsQI) {
1510 numLhsProtocols = lhsQI->getNumProtocols();
1511 lhsProtoList = lhsQI->getReferencedProtocols();
1512 }
Fariborz Jahanian87829072007-12-20 19:24:10 +00001513 else if (lhsQID) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001514 numLhsProtocols = lhsQID->getNumProtocols();
1515 lhsProtoList = lhsQID->getReferencedProtocols();
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001516 }
1517 bool match = false;
1518 // for static type vs. qualified 'id' type, check that class implements
1519 // one of 'id's protocols.
1520 if (lhsID) {
1521 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001522 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001523 if (ClassImplementsProtocol(rhsProto, lhsID, compare)) {
1524 match = true;
1525 break;
1526 }
1527 }
1528 }
1529 else for (unsigned i =0; i < numLhsProtocols; i++) {
1530 match = false;
Ted Kremenek42730c52008-01-07 19:49:32 +00001531 ObjCProtocolDecl *lhsProto = lhsProtoList[i];
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001532 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
Ted Kremenek42730c52008-01-07 19:49:32 +00001533 ObjCProtocolDecl *rhsProto = rhsQID->getProtocols(j);
Fariborz Jahaniancd71bf42007-12-21 00:33:59 +00001534 if (ProtocolCompatibleWithProtocol(lhsProto, rhsProto) ||
1535 compare && ProtocolCompatibleWithProtocol(rhsProto, lhsProto)) {
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001536 match = true;
1537 break;
1538 }
1539 }
Fariborz Jahanianf4e68042007-12-21 22:22:33 +00001540 }
1541 if (!match)
1542 return false;
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001543 }
1544 return true;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001545}
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001546
Chris Lattner5003e8b2007-11-01 05:03:41 +00001547bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1548 const VectorType *lVector = lhs->getAsVectorType();
1549 const VectorType *rVector = rhs->getAsVectorType();
1550
1551 if ((lVector->getElementType().getCanonicalType() ==
1552 rVector->getElementType().getCanonicalType()) &&
1553 (lVector->getNumElements() == rVector->getNumElements()))
1554 return true;
1555 return false;
1556}
1557
Steve Naroff85f0dc52007-10-15 20:41:53 +00001558// C99 6.2.7p1: If both are complete types, then the following additional
1559// requirements apply...FIXME (handle compatibility across source files).
1560bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroff4a5e2072007-11-07 06:03:51 +00001561 // "Class" and "id" are compatible built-in structure types.
Ted Kremenek42730c52008-01-07 19:49:32 +00001562 if (isObjCIdType(lhs) && isObjCClassType(rhs) ||
1563 isObjCClassType(lhs) && isObjCIdType(rhs))
Steve Naroff4a5e2072007-11-07 06:03:51 +00001564 return true;
Eli Friedmane7fb03a2008-02-15 06:03:44 +00001565
1566 // Within a translation unit a tag type is
1567 // only compatible with itself.
1568 return lhs.getCanonicalType() == rhs.getCanonicalType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001569}
1570
1571bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1572 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1573 // identically qualified and both shall be pointers to compatible types.
1574 if (lhs.getQualifiers() != rhs.getQualifiers())
1575 return false;
1576
1577 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1578 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1579
1580 return typesAreCompatible(ltype, rtype);
1581}
1582
Bill Wendling6a9d8542007-12-03 07:33:35 +00001583// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroff85f0dc52007-10-15 20:41:53 +00001584// reference to T, the operation assigns to the object of type T denoted by the
1585// reference.
1586bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1587 QualType ltype = lhs;
1588
1589 if (lhs->isReferenceType())
1590 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1591
1592 QualType rtype = rhs;
1593
1594 if (rhs->isReferenceType())
1595 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1596
1597 return typesAreCompatible(ltype, rtype);
1598}
1599
1600bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1601 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1602 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1603 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1604 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1605
1606 // first check the return types (common between C99 and K&R).
1607 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1608 return false;
1609
1610 if (lproto && rproto) { // two C99 style function prototypes
1611 unsigned lproto_nargs = lproto->getNumArgs();
1612 unsigned rproto_nargs = rproto->getNumArgs();
1613
1614 if (lproto_nargs != rproto_nargs)
1615 return false;
1616
1617 // both prototypes have the same number of arguments.
1618 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1619 (rproto->isVariadic() && !lproto->isVariadic()))
1620 return false;
1621
1622 // The use of ellipsis agree...now check the argument types.
1623 for (unsigned i = 0; i < lproto_nargs; i++)
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001624 // C99 6.7.5.3p15: ...and each parameter declared with qualified type
1625 // is taken as having the unqualified version of it's declared type.
Steve Naroffdec17fe2008-01-29 00:15:50 +00001626 if (!typesAreCompatible(lproto->getArgType(i).getUnqualifiedType(),
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001627 rproto->getArgType(i).getUnqualifiedType()))
Steve Naroff85f0dc52007-10-15 20:41:53 +00001628 return false;
1629 return true;
1630 }
1631 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1632 return true;
1633
1634 // we have a mixture of K&R style with C99 prototypes
1635 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1636
1637 if (proto->isVariadic())
1638 return false;
1639
1640 // FIXME: Each parameter type T in the prototype must be compatible with the
1641 // type resulting from applying the usual argument conversions to T.
1642 return true;
1643}
1644
1645bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
Eli Friedman1e7537832008-02-06 04:53:22 +00001646 // Compatible arrays must have compatible element types
1647 QualType ltype = lhs->getAsArrayType()->getElementType();
1648 QualType rtype = rhs->getAsArrayType()->getElementType();
1649
Steve Naroff85f0dc52007-10-15 20:41:53 +00001650 if (!typesAreCompatible(ltype, rtype))
1651 return false;
Eli Friedman1e7537832008-02-06 04:53:22 +00001652
1653 // Compatible arrays must be the same size
1654 if (const ConstantArrayType* LCAT = lhs->getAsConstantArrayType())
1655 if (const ConstantArrayType* RCAT = rhs->getAsConstantArrayType())
1656 return RCAT->getSize() == LCAT->getSize();
1657
Steve Naroff85f0dc52007-10-15 20:41:53 +00001658 return true;
1659}
1660
1661/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1662/// both shall have the identically qualified version of a compatible type.
1663/// C99 6.2.7p1: Two types have compatible types if their types are the
1664/// same. See 6.7.[2,3,5] for additional rules.
1665bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
Steve Naroff577f9722008-01-29 18:58:14 +00001666 if (lhs.getQualifiers() != rhs.getQualifiers())
1667 return false;
1668
Steve Naroff85f0dc52007-10-15 20:41:53 +00001669 QualType lcanon = lhs.getCanonicalType();
1670 QualType rcanon = rhs.getCanonicalType();
1671
1672 // If two types are identical, they are are compatible
1673 if (lcanon == rcanon)
1674 return true;
Bill Wendling6a9d8542007-12-03 07:33:35 +00001675
1676 // C++ [expr]: If an expression initially has the type "reference to T", the
1677 // type is adjusted to "T" prior to any further analysis, the expression
1678 // designates the object or function denoted by the reference, and the
1679 // expression is an lvalue.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001680 if (ReferenceType *RT = dyn_cast<ReferenceType>(lcanon))
1681 lcanon = RT->getReferenceeType();
1682 if (ReferenceType *RT = dyn_cast<ReferenceType>(rcanon))
1683 rcanon = RT->getReferenceeType();
1684
1685 Type::TypeClass LHSClass = lcanon->getTypeClass();
1686 Type::TypeClass RHSClass = rcanon->getTypeClass();
1687
1688 // We want to consider the two function types to be the same for these
1689 // comparisons, just force one to the other.
1690 if (LHSClass == Type::FunctionProto) LHSClass = Type::FunctionNoProto;
1691 if (RHSClass == Type::FunctionProto) RHSClass = Type::FunctionNoProto;
Eli Friedman398837e2008-02-12 08:23:06 +00001692
1693 // Same as above for arrays
1694 if (LHSClass == Type::VariableArray) LHSClass = Type::ConstantArray;
1695 if (RHSClass == Type::VariableArray) RHSClass = Type::ConstantArray;
Eli Friedman8ff07782008-02-15 18:16:39 +00001696 if (LHSClass == Type::IncompleteArray) LHSClass = Type::ConstantArray;
1697 if (RHSClass == Type::IncompleteArray) RHSClass = Type::ConstantArray;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001698
Steve Naroffc88babe2008-01-09 22:43:08 +00001699 // If the canonical type classes don't match...
Chris Lattnerc38d4522008-01-14 05:45:46 +00001700 if (LHSClass != RHSClass) {
Steve Naroff85f0dc52007-10-15 20:41:53 +00001701 // For Objective-C, it is possible for two types to be compatible
1702 // when their classes don't match (when dealing with "id"). If either type
1703 // is an interface, we defer to objcTypesAreCompatible().
Ted Kremenek42730c52008-01-07 19:49:32 +00001704 if (lcanon->isObjCInterfaceType() || rcanon->isObjCInterfaceType())
Steve Naroff85f0dc52007-10-15 20:41:53 +00001705 return objcTypesAreCompatible(lcanon, rcanon);
Steve Naroff53b6f4c2008-01-30 19:17:43 +00001706
Chris Lattnerc38d4522008-01-14 05:45:46 +00001707 // C99 6.7.2.2p4: Each enumerated type shall be compatible with char,
1708 // a signed integer type, or an unsigned integer type.
Eli Friedmanad6c06c2008-02-12 08:46:17 +00001709 if (lcanon->isEnumeralType() && rcanon->isIntegralType()) {
1710 EnumDecl* EDecl = cast<EnumDecl>(cast<TagType>(lcanon)->getDecl());
1711 return EDecl->getIntegerType() == rcanon;
1712 }
1713 if (rcanon->isEnumeralType() && lcanon->isIntegralType()) {
1714 EnumDecl* EDecl = cast<EnumDecl>(cast<TagType>(rcanon)->getDecl());
1715 return EDecl->getIntegerType() == lcanon;
1716 }
Chris Lattnerc38d4522008-01-14 05:45:46 +00001717
Steve Naroff85f0dc52007-10-15 20:41:53 +00001718 return false;
1719 }
Steve Naroffc88babe2008-01-09 22:43:08 +00001720 // The canonical type classes match.
Chris Lattnerc38d4522008-01-14 05:45:46 +00001721 switch (LHSClass) {
1722 case Type::FunctionProto: assert(0 && "Canonicalized away above");
1723 case Type::Pointer:
1724 return pointerTypesAreCompatible(lcanon, rcanon);
1725 case Type::ConstantArray:
1726 case Type::VariableArray:
Eli Friedman8ff07782008-02-15 18:16:39 +00001727 case Type::IncompleteArray:
Chris Lattnerc38d4522008-01-14 05:45:46 +00001728 return arrayTypesAreCompatible(lcanon, rcanon);
1729 case Type::FunctionNoProto:
1730 return functionTypesAreCompatible(lcanon, rcanon);
1731 case Type::Tagged: // handle structures, unions
1732 return tagTypesAreCompatible(lcanon, rcanon);
1733 case Type::Builtin:
1734 return builtinTypesAreCompatible(lcanon, rcanon);
1735 case Type::ObjCInterface:
1736 return interfaceTypesAreCompatible(lcanon, rcanon);
1737 case Type::Vector:
1738 case Type::OCUVector:
1739 return vectorTypesAreCompatible(lcanon, rcanon);
1740 case Type::ObjCQualifiedInterface:
1741 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
1742 default:
1743 assert(0 && "unexpected type");
Steve Naroff85f0dc52007-10-15 20:41:53 +00001744 }
1745 return true; // should never get here...
1746}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001747
Ted Kremenek738e6c02007-10-31 17:10:13 +00001748/// Emit - Serialize an ASTContext object to Bitcode.
1749void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001750 S.EmitRef(SourceMgr);
1751 S.EmitRef(Target);
1752 S.EmitRef(Idents);
1753 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001754
Ted Kremenek68228a92007-10-31 22:44:07 +00001755 // Emit the size of the type vector so that we can reserve that size
1756 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001757 S.EmitInt(Types.size());
1758
Ted Kremenek034a78c2007-11-13 22:02:55 +00001759 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1760 I!=E;++I)
1761 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001762
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001763 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001764}
1765
Ted Kremenekacba3612007-11-13 00:25:37 +00001766ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek68228a92007-10-31 22:44:07 +00001767 SourceManager &SM = D.ReadRef<SourceManager>();
1768 TargetInfo &t = D.ReadRef<TargetInfo>();
1769 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1770 SelectorTable &sels = D.ReadRef<SelectorTable>();
1771
1772 unsigned size_reserve = D.ReadInt();
1773
1774 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1775
Ted Kremenek034a78c2007-11-13 22:02:55 +00001776 for (unsigned i = 0; i < size_reserve; ++i)
1777 Type::Create(*A,i,D);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001778
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001779 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001780
1781 return A;
1782}