blob: 06d6888e19e3e8355df6b8689287c54ec724eb5d [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//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
Steve 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;
Chris Lattner8a35b462007-12-12 06:43:05 +000051 unsigned NumObjcInterfaces = 0, NumObjcQualifiedInterfaces = 0;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +000052 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 }
Steve Naroff948fd372007-09-17 14:16:13 +000083 } else if (isa<ObjcInterfaceType>(T))
84 ++NumObjcInterfaces;
Chris Lattner8a35b462007-12-12 06:43:05 +000085 else if (isa<ObjcQualifiedInterfaceType>(T))
86 ++NumObjcQualifiedInterfaces;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +000087 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);
Steve Naroff948fd372007-09-17 14:16:13 +0000109 fprintf(stderr, " %d interface types\n", NumObjcInterfaces);
Chris Lattner8a35b462007-12-12 06:43:05 +0000110 fprintf(stderr, " %d protocol qualified interface types\n",
111 NumObjcQualifiedInterfaces);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000112 fprintf(stderr, " %d protocol qualified id types\n",
113 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();
165 ObjcIdType = QualType();
166 IdStructType = 0;
Anders Carlsson7f23e3d2007-10-31 02:53:19 +0000167 ObjcClassType = QualType();
168 ClassStructType = 0;
169
Steve Narofff2e30312007-10-15 23:35:17 +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;
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 }
212
213 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 Lattner858eece2007-09-22 18:29:59 +0000216 const llvm::fltSemantics *F;
Chris Lattner4b009652007-07-25 00:24:17 +0000217 switch (cast<BuiltinType>(T)->getKind()) {
218 default: assert(0 && "Unknown builtin type!");
219 case BuiltinType::Void:
220 assert(0 && "Incomplete types have no size!");
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000221 case BuiltinType::Bool: Target.getBoolInfo(Size,Align,getFullLoc(L));
222 break;
223
Chris Lattner4b009652007-07-25 00:24:17 +0000224 case BuiltinType::Char_S:
225 case BuiltinType::Char_U:
226 case BuiltinType::UChar:
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000227 case BuiltinType::SChar: Target.getCharInfo(Size,Align,getFullLoc(L));
228 break;
229
Chris Lattner4b009652007-07-25 00:24:17 +0000230 case BuiltinType::UShort:
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000231 case BuiltinType::Short: Target.getShortInfo(Size,Align,getFullLoc(L));
232 break;
233
Chris Lattner4b009652007-07-25 00:24:17 +0000234 case BuiltinType::UInt:
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000235 case BuiltinType::Int: Target.getIntInfo(Size,Align,getFullLoc(L));
236 break;
237
Chris Lattner4b009652007-07-25 00:24:17 +0000238 case BuiltinType::ULong:
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000239 case BuiltinType::Long: Target.getLongInfo(Size,Align,getFullLoc(L));
240 break;
241
Chris Lattner4b009652007-07-25 00:24:17 +0000242 case BuiltinType::ULongLong:
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000243 case BuiltinType::LongLong: Target.getLongLongInfo(Size,Align,
244 getFullLoc(L));
245 break;
246
247 case BuiltinType::Float: Target.getFloatInfo(Size,Align,F,
248 getFullLoc(L));
249 break;
250
251 case BuiltinType::Double: Target.getDoubleInfo(Size,Align,F,
252 getFullLoc(L));
253 break;
254
255 case BuiltinType::LongDouble: Target.getLongDoubleInfo(Size,Align,F,
256 getFullLoc(L));
257 break;
Chris Lattner4b009652007-07-25 00:24:17 +0000258 }
259 break;
260 }
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000261 case Type::ObjcQualifiedId: Target.getPointerInfo(Size, Align, getFullLoc(L));
262 break;
Ted Kremenekd7f64cd2007-12-12 22:39:36 +0000263 case Type::Pointer: Target.getPointerInfo(Size, Align, getFullLoc(L)); break;
Chris Lattner4b009652007-07-25 00:24:17 +0000264 case Type::Reference:
265 // "When applied to a reference or a reference type, the result is the size
266 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
267 // FIXME: This is wrong for struct layout!
268 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
269
270 case Type::Complex: {
271 // Complex types have the same alignment as their elements, but twice the
272 // size.
273 std::pair<uint64_t, unsigned> EltInfo =
274 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
275 Size = EltInfo.first*2;
276 Align = EltInfo.second;
277 break;
278 }
279 case Type::Tagged:
Chris Lattnereb56d292007-08-27 17:38:00 +0000280 TagType *TT = cast<TagType>(T);
281 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
Devang Patel7a78e432007-11-01 19:11:01 +0000282 const ASTRecordLayout &Layout = getASTRecordLayout(RT->getDecl(), L);
Chris Lattnereb56d292007-08-27 17:38:00 +0000283 Size = Layout.getSize();
284 Align = Layout.getAlignment();
285 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattner90a018d2007-08-28 18:24:31 +0000286 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattnereb56d292007-08-27 17:38:00 +0000287 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000288 assert(0 && "Unimplemented type sizes!");
Chris Lattnereb56d292007-08-27 17:38:00 +0000289 }
Chris Lattner4b009652007-07-25 00:24:17 +0000290 break;
291 }
292
293 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
294 return std::make_pair(Size, Align);
295}
296
Devang Patel7a78e432007-11-01 19:11:01 +0000297/// getASTRecordLayout - Get or compute information about the layout of the
Chris Lattner4b009652007-07-25 00:24:17 +0000298/// specified record (struct/union/class), which indicates its size and field
299/// position information.
Devang Patel7a78e432007-11-01 19:11:01 +0000300const ASTRecordLayout &ASTContext::getASTRecordLayout(const RecordDecl *D,
301 SourceLocation L) {
Chris Lattner4b009652007-07-25 00:24:17 +0000302 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
303
304 // Look up this layout, if already laid out, return what we have.
Devang Patel7a78e432007-11-01 19:11:01 +0000305 const ASTRecordLayout *&Entry = ASTRecordLayouts[D];
Chris Lattner4b009652007-07-25 00:24:17 +0000306 if (Entry) return *Entry;
307
Devang Patel7a78e432007-11-01 19:11:01 +0000308 // Allocate and assign into ASTRecordLayouts here. The "Entry" reference can
309 // be invalidated (dangle) if the ASTRecordLayouts hashtable is inserted into.
310 ASTRecordLayout *NewEntry = new ASTRecordLayout();
Chris Lattner4b009652007-07-25 00:24:17 +0000311 Entry = NewEntry;
312
313 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
314 uint64_t RecordSize = 0;
315 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
316
317 if (D->getKind() != Decl::Union) {
318 // Layout each field, for now, just sequentially, respecting alignment. In
319 // the future, this will need to be tweakable by targets.
320 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
321 const FieldDecl *FD = D->getMember(i);
322 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
323 uint64_t FieldSize = FieldInfo.first;
324 unsigned FieldAlign = FieldInfo.second;
325
326 // Round up the current record size to the field's alignment boundary.
327 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
328
329 // Place this field at the current location.
330 FieldOffsets[i] = RecordSize;
331
332 // Reserve space for this field.
333 RecordSize += FieldSize;
334
335 // Remember max struct/class alignment.
336 RecordAlign = std::max(RecordAlign, FieldAlign);
337 }
338
339 // Finally, round the size of the total struct up to the alignment of the
340 // struct itself.
341 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
342 } else {
343 // Union layout just puts each member at the start of the record.
344 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
345 const FieldDecl *FD = D->getMember(i);
346 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
347 uint64_t FieldSize = FieldInfo.first;
348 unsigned FieldAlign = FieldInfo.second;
349
350 // Round up the current record size to the field's alignment boundary.
351 RecordSize = std::max(RecordSize, FieldSize);
352
353 // Place this field at the start of the record.
354 FieldOffsets[i] = 0;
355
356 // Remember max struct/class alignment.
357 RecordAlign = std::max(RecordAlign, FieldAlign);
358 }
359 }
360
361 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
362 return *NewEntry;
363}
364
Chris Lattner4b009652007-07-25 00:24:17 +0000365//===----------------------------------------------------------------------===//
366// Type creation/memoization methods
367//===----------------------------------------------------------------------===//
368
369
370/// getComplexType - Return the uniqued reference to the type for a complex
371/// number with the specified element type.
372QualType ASTContext::getComplexType(QualType T) {
373 // Unique pointers, to guarantee there is only one pointer of a particular
374 // structure.
375 llvm::FoldingSetNodeID ID;
376 ComplexType::Profile(ID, T);
377
378 void *InsertPos = 0;
379 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
380 return QualType(CT, 0);
381
382 // If the pointee type isn't canonical, this won't be a canonical type either,
383 // so fill in the canonical type field.
384 QualType Canonical;
385 if (!T->isCanonical()) {
386 Canonical = getComplexType(T.getCanonicalType());
387
388 // Get the new insert position for the node we care about.
389 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
390 assert(NewIP == 0 && "Shouldn't be in the map!");
391 }
392 ComplexType *New = new ComplexType(T, Canonical);
393 Types.push_back(New);
394 ComplexTypes.InsertNode(New, InsertPos);
395 return QualType(New, 0);
396}
397
398
399/// getPointerType - Return the uniqued reference to the type for a pointer to
400/// the specified type.
401QualType ASTContext::getPointerType(QualType T) {
402 // Unique pointers, to guarantee there is only one pointer of a particular
403 // structure.
404 llvm::FoldingSetNodeID ID;
405 PointerType::Profile(ID, T);
406
407 void *InsertPos = 0;
408 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
409 return QualType(PT, 0);
410
411 // If the pointee type isn't canonical, this won't be a canonical type either,
412 // so fill in the canonical type field.
413 QualType Canonical;
414 if (!T->isCanonical()) {
415 Canonical = getPointerType(T.getCanonicalType());
416
417 // Get the new insert position for the node we care about.
418 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
419 assert(NewIP == 0 && "Shouldn't be in the map!");
420 }
421 PointerType *New = new PointerType(T, Canonical);
422 Types.push_back(New);
423 PointerTypes.InsertNode(New, InsertPos);
424 return QualType(New, 0);
425}
426
427/// getReferenceType - Return the uniqued reference to the type for a reference
428/// to the specified type.
429QualType ASTContext::getReferenceType(QualType T) {
430 // Unique pointers, to guarantee there is only one pointer of a particular
431 // structure.
432 llvm::FoldingSetNodeID ID;
433 ReferenceType::Profile(ID, T);
434
435 void *InsertPos = 0;
436 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
437 return QualType(RT, 0);
438
439 // If the referencee type isn't canonical, this won't be a canonical type
440 // either, so fill in the canonical type field.
441 QualType Canonical;
442 if (!T->isCanonical()) {
443 Canonical = getReferenceType(T.getCanonicalType());
444
445 // Get the new insert position for the node we care about.
446 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
447 assert(NewIP == 0 && "Shouldn't be in the map!");
448 }
449
450 ReferenceType *New = new ReferenceType(T, Canonical);
451 Types.push_back(New);
452 ReferenceTypes.InsertNode(New, InsertPos);
453 return QualType(New, 0);
454}
455
Steve Naroff83c13012007-08-30 01:06:46 +0000456/// getConstantArrayType - Return the unique reference to the type for an
457/// array of the specified element type.
458QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000459 const llvm::APInt &ArySize,
460 ArrayType::ArraySizeModifier ASM,
461 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000462 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000463 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000464
465 void *InsertPos = 0;
Ted Kremenek738e6c02007-10-31 17:10:13 +0000466 if (ConstantArrayType *ATP =
467 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000468 return QualType(ATP, 0);
469
470 // If the element type isn't canonical, this won't be a canonical type either,
471 // so fill in the canonical type field.
472 QualType Canonical;
473 if (!EltTy->isCanonical()) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000474 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
475 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000476 // Get the new insert position for the node we care about.
Ted Kremenek738e6c02007-10-31 17:10:13 +0000477 ConstantArrayType *NewIP =
478 ConstantArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
479
Chris Lattner4b009652007-07-25 00:24:17 +0000480 assert(NewIP == 0 && "Shouldn't be in the map!");
481 }
482
Steve Naroff24c9b982007-08-30 18:10:14 +0000483 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
484 ASM, EltTypeQuals);
Ted Kremenek738e6c02007-10-31 17:10:13 +0000485 ConstantArrayTypes.InsertNode(New, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000486 Types.push_back(New);
487 return QualType(New, 0);
488}
489
Steve Naroffe2579e32007-08-30 18:14:25 +0000490/// getVariableArrayType - Returns a non-unique reference to the type for a
491/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000492QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
493 ArrayType::ArraySizeModifier ASM,
494 unsigned EltTypeQuals) {
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000495 if (NumElts) {
496 // Since we don't unique expressions, it isn't possible to unique VLA's
497 // that have an expression provided for their size.
498
Ted Kremenek2058dc42007-10-30 16:41:53 +0000499 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
500 ASM, EltTypeQuals);
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000501
Ted Kremenek2058dc42007-10-30 16:41:53 +0000502 CompleteVariableArrayTypes.push_back(New);
Ted Kremenek3793e1a2007-10-29 23:37:31 +0000503 Types.push_back(New);
504 return QualType(New, 0);
505 }
506 else {
507 // No size is provided for the VLA. These we can unique.
508 llvm::FoldingSetNodeID ID;
509 VariableArrayType::Profile(ID, EltTy);
510
511 void *InsertPos = 0;
512 if (VariableArrayType *ATP =
513 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
514 return QualType(ATP, 0);
515
516 // If the element type isn't canonical, this won't be a canonical type
517 // either, so fill in the canonical type field.
518 QualType Canonical;
519
520 if (!EltTy->isCanonical()) {
521 Canonical = getVariableArrayType(EltTy.getCanonicalType(), NumElts,
522 ASM, EltTypeQuals);
523
524 // Get the new insert position for the node we care about.
525 VariableArrayType *NewIP =
526 IncompleteVariableArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
527
528 assert(NewIP == 0 && "Shouldn't be in the map!");
529 }
530
531 VariableArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
532 ASM, EltTypeQuals);
533
534 IncompleteVariableArrayTypes.InsertNode(New, InsertPos);
535 Types.push_back(New);
536 return QualType(New, 0);
537 }
Steve Naroff83c13012007-08-30 01:06:46 +0000538}
539
Chris Lattner4b009652007-07-25 00:24:17 +0000540/// getVectorType - Return the unique reference to a vector type of
541/// the specified element type and size. VectorType must be a built-in type.
542QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
543 BuiltinType *baseType;
544
545 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
546 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
547
548 // Check if we've already instantiated a vector of this type.
549 llvm::FoldingSetNodeID ID;
550 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
551 void *InsertPos = 0;
552 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
553 return QualType(VTP, 0);
554
555 // If the element type isn't canonical, this won't be a canonical type either,
556 // so fill in the canonical type field.
557 QualType Canonical;
558 if (!vecType->isCanonical()) {
559 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
560
561 // Get the new insert position for the node we care about.
562 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
563 assert(NewIP == 0 && "Shouldn't be in the map!");
564 }
565 VectorType *New = new VectorType(vecType, NumElts, Canonical);
566 VectorTypes.InsertNode(New, InsertPos);
567 Types.push_back(New);
568 return QualType(New, 0);
569}
570
571/// getOCUVectorType - Return the unique reference to an OCU vector type of
572/// the specified element type and size. VectorType must be a built-in type.
573QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
574 BuiltinType *baseType;
575
576 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
577 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
578
579 // Check if we've already instantiated a vector of this type.
580 llvm::FoldingSetNodeID ID;
581 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
582 void *InsertPos = 0;
583 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
584 return QualType(VTP, 0);
585
586 // If the element type isn't canonical, this won't be a canonical type either,
587 // so fill in the canonical type field.
588 QualType Canonical;
589 if (!vecType->isCanonical()) {
590 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
591
592 // Get the new insert position for the node we care about.
593 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
594 assert(NewIP == 0 && "Shouldn't be in the map!");
595 }
596 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
597 VectorTypes.InsertNode(New, InsertPos);
598 Types.push_back(New);
599 return QualType(New, 0);
600}
601
602/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
603///
604QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
605 // Unique functions, to guarantee there is only one function of a particular
606 // structure.
607 llvm::FoldingSetNodeID ID;
608 FunctionTypeNoProto::Profile(ID, ResultTy);
609
610 void *InsertPos = 0;
611 if (FunctionTypeNoProto *FT =
612 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
613 return QualType(FT, 0);
614
615 QualType Canonical;
616 if (!ResultTy->isCanonical()) {
617 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
618
619 // Get the new insert position for the node we care about.
620 FunctionTypeNoProto *NewIP =
621 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
622 assert(NewIP == 0 && "Shouldn't be in the map!");
623 }
624
625 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
626 Types.push_back(New);
627 FunctionTypeProtos.InsertNode(New, InsertPos);
628 return QualType(New, 0);
629}
630
631/// getFunctionType - Return a normal function type with a typed argument
632/// list. isVariadic indicates whether the argument list includes '...'.
633QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
634 unsigned NumArgs, bool isVariadic) {
635 // Unique functions, to guarantee there is only one function of a particular
636 // structure.
637 llvm::FoldingSetNodeID ID;
638 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
639
640 void *InsertPos = 0;
641 if (FunctionTypeProto *FTP =
642 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
643 return QualType(FTP, 0);
644
645 // Determine whether the type being created is already canonical or not.
646 bool isCanonical = ResultTy->isCanonical();
647 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
648 if (!ArgArray[i]->isCanonical())
649 isCanonical = false;
650
651 // If this type isn't canonical, get the canonical version of it.
652 QualType Canonical;
653 if (!isCanonical) {
654 llvm::SmallVector<QualType, 16> CanonicalArgs;
655 CanonicalArgs.reserve(NumArgs);
656 for (unsigned i = 0; i != NumArgs; ++i)
657 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
658
659 Canonical = getFunctionType(ResultTy.getCanonicalType(),
660 &CanonicalArgs[0], NumArgs,
661 isVariadic);
662
663 // Get the new insert position for the node we care about.
664 FunctionTypeProto *NewIP =
665 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
666 assert(NewIP == 0 && "Shouldn't be in the map!");
667 }
668
669 // FunctionTypeProto objects are not allocated with new because they have a
670 // variable size array (for parameter types) at the end of them.
671 FunctionTypeProto *FTP =
672 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
673 NumArgs*sizeof(QualType));
674 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
675 Canonical);
676 Types.push_back(FTP);
677 FunctionTypeProtos.InsertNode(FTP, InsertPos);
678 return QualType(FTP, 0);
679}
680
681/// getTypedefType - Return the unique reference to the type for the
682/// specified typename decl.
683QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
684 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
685
686 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000687 Decl->TypeForDecl = new TypedefType(Type::TypeName, Decl, Canonical);
Chris Lattner4b009652007-07-25 00:24:17 +0000688 Types.push_back(Decl->TypeForDecl);
689 return QualType(Decl->TypeForDecl, 0);
690}
691
Steve Naroff81f1bba2007-09-06 21:24:23 +0000692/// getObjcInterfaceType - Return the unique reference to the type for the
693/// specified ObjC interface decl.
694QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) {
695 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
696
Fariborz Jahanian0c2f2142007-12-13 20:47:42 +0000697 Decl->TypeForDecl = new ObjcInterfaceType(Type::ObjcInterface, Decl);
Steve Naroff81f1bba2007-09-06 21:24:23 +0000698 Types.push_back(Decl->TypeForDecl);
699 return QualType(Decl->TypeForDecl, 0);
700}
701
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000702/// getObjcQualifiedInterfaceType - Return a
703/// ObjcQualifiedInterfaceType type for the given interface decl and
704/// the conforming protocol list.
705QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl,
706 ObjcProtocolDecl **Protocols, unsigned NumProtocols) {
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000707 llvm::FoldingSetNodeID ID;
Fariborz Jahanian0c2f2142007-12-13 20:47:42 +0000708 ObjcQualifiedInterfaceType::Profile(ID, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000709
710 void *InsertPos = 0;
711 if (ObjcQualifiedInterfaceType *QT =
712 ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
713 return QualType(QT, 0);
714
715 // No Match;
Chris Lattnerd855a6e2007-10-11 03:36:41 +0000716 ObjcQualifiedInterfaceType *QType =
Fariborz Jahanian0c2f2142007-12-13 20:47:42 +0000717 new ObjcQualifiedInterfaceType(Decl, Protocols, NumProtocols);
Fariborz Jahanian91193f62007-10-11 00:55:41 +0000718 Types.push_back(QType);
719 ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
720 return QualType(QType, 0);
721}
722
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000723/// getObjcQualifiedIdType - Return a
Fariborz Jahanian2a0e5432007-12-17 21:48:49 +0000724/// getObjcQualifiedIdType type for the 'id' decl and
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000725/// the conforming protocol list.
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000726QualType ASTContext::getObjcQualifiedIdType(QualType idType,
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000727 ObjcProtocolDecl **Protocols,
728 unsigned NumProtocols) {
729 llvm::FoldingSetNodeID ID;
730 ObjcQualifiedIdType::Profile(ID, Protocols, NumProtocols);
731
732 void *InsertPos = 0;
733 if (ObjcQualifiedIdType *QT =
734 ObjcQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos))
735 return QualType(QT, 0);
736
737 // No Match;
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +0000738 QualType Canonical;
739 if (!idType->isCanonical()) {
740 Canonical = getObjcQualifiedIdType(idType.getCanonicalType(),
741 Protocols, NumProtocols);
742 ObjcQualifiedIdType *NewQT =
743 ObjcQualifiedIdTypes.FindNodeOrInsertPos(ID, InsertPos);
744 assert(NewQT == 0 && "Shouldn't be in the map!");
745 }
746
747 ObjcQualifiedIdType *QType =
748 new ObjcQualifiedIdType(Canonical, Protocols, NumProtocols);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +0000749 Types.push_back(QType);
750 ObjcQualifiedIdTypes.InsertNode(QType, InsertPos);
751 return QualType(QType, 0);
752}
753
Steve Naroff0604dd92007-08-01 18:02:17 +0000754/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
755/// TypeOfExpr AST's (since expression's are never shared). For example,
756/// multiple declarations that refer to "typeof(x)" all contain different
757/// DeclRefExpr's. This doesn't effect the type checker, since it operates
758/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000759QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroff7cbb1462007-07-31 12:34:36 +0000760 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000761 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
762 Types.push_back(toe);
763 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000764}
765
Steve Naroff0604dd92007-08-01 18:02:17 +0000766/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
767/// TypeOfType AST's. The only motivation to unique these nodes would be
768/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
769/// an issue. This doesn't effect the type checker, since it operates
770/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000771QualType ASTContext::getTypeOfType(QualType tofType) {
772 QualType Canonical = tofType.getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000773 TypeOfType *tot = new TypeOfType(tofType, Canonical);
774 Types.push_back(tot);
775 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000776}
777
Chris Lattner4b009652007-07-25 00:24:17 +0000778/// getTagDeclType - Return the unique reference to the type for the
779/// specified TagDecl (struct/union/class/enum) decl.
780QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Ted Kremenekae8fa032007-11-26 21:16:01 +0000781 assert (Decl);
782
Ted Kremenekf05026d2007-11-14 00:03:20 +0000783 // The decl stores the type cache.
Ted Kremenekae8fa032007-11-26 21:16:01 +0000784 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Ted Kremenekf05026d2007-11-14 00:03:20 +0000785
786 TagType* T = new TagType(Decl, QualType());
Ted Kremenekae8fa032007-11-26 21:16:01 +0000787 Types.push_back(T);
788 Decl->TypeForDecl = T;
Ted Kremenekf05026d2007-11-14 00:03:20 +0000789
790 return QualType(T, 0);
Chris Lattner4b009652007-07-25 00:24:17 +0000791}
792
793/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
794/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
795/// needs to agree with the definition in <stddef.h>.
796QualType ASTContext::getSizeType() const {
797 // On Darwin, size_t is defined as a "long unsigned int".
798 // FIXME: should derive from "Target".
799 return UnsignedLongTy;
800}
801
802/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
803/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
804QualType ASTContext::getPointerDiffType() const {
805 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
806 // FIXME: should derive from "Target".
807 return IntTy;
808}
809
810/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
811/// routine will assert if passed a built-in type that isn't an integer or enum.
812static int getIntegerRank(QualType t) {
813 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
814 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
815 return 4;
816 }
817
818 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
819 switch (BT->getKind()) {
820 default:
821 assert(0 && "getIntegerRank(): not a built-in integer");
822 case BuiltinType::Bool:
823 return 1;
824 case BuiltinType::Char_S:
825 case BuiltinType::Char_U:
826 case BuiltinType::SChar:
827 case BuiltinType::UChar:
828 return 2;
829 case BuiltinType::Short:
830 case BuiltinType::UShort:
831 return 3;
832 case BuiltinType::Int:
833 case BuiltinType::UInt:
834 return 4;
835 case BuiltinType::Long:
836 case BuiltinType::ULong:
837 return 5;
838 case BuiltinType::LongLong:
839 case BuiltinType::ULongLong:
840 return 6;
841 }
842}
843
844/// getFloatingRank - Return a relative rank for floating point types.
845/// This routine will assert if passed a built-in type that isn't a float.
846static int getFloatingRank(QualType T) {
847 T = T.getCanonicalType();
848 if (ComplexType *CT = dyn_cast<ComplexType>(T))
849 return getFloatingRank(CT->getElementType());
850
851 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner5003e8b2007-11-01 05:03:41 +0000852 default: assert(0 && "getFloatingRank(): not a floating type");
Chris Lattner4b009652007-07-25 00:24:17 +0000853 case BuiltinType::Float: return FloatRank;
854 case BuiltinType::Double: return DoubleRank;
855 case BuiltinType::LongDouble: return LongDoubleRank;
856 }
857}
858
Steve Narofffa0c4532007-08-27 01:41:48 +0000859/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
860/// point or a complex type (based on typeDomain/typeSize).
861/// 'typeDomain' is a real floating point or complex type.
862/// 'typeSize' is a real floating point or complex type.
Steve Naroff3cf497f2007-08-27 01:27:54 +0000863QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
864 QualType typeSize, QualType typeDomain) const {
865 if (typeDomain->isComplexType()) {
866 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000867 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000868 case FloatRank: return FloatComplexTy;
869 case DoubleRank: return DoubleComplexTy;
870 case LongDoubleRank: return LongDoubleComplexTy;
871 }
Chris Lattner4b009652007-07-25 00:24:17 +0000872 }
Steve Naroff3cf497f2007-08-27 01:27:54 +0000873 if (typeDomain->isRealFloatingType()) {
874 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000875 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000876 case FloatRank: return FloatTy;
877 case DoubleRank: return DoubleTy;
878 case LongDoubleRank: return LongDoubleTy;
879 }
880 }
881 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000882 //an invalid return value, but the assert
883 //will ensure that this code is never reached.
884 return VoidTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000885}
886
Steve Naroff45fc9822007-08-27 15:30:22 +0000887/// compareFloatingType - Handles 3 different combos:
888/// float/float, float/complex, complex/complex.
889/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
890int ASTContext::compareFloatingType(QualType lt, QualType rt) {
891 if (getFloatingRank(lt) == getFloatingRank(rt))
892 return 0;
893 if (getFloatingRank(lt) > getFloatingRank(rt))
894 return 1;
895 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +0000896}
897
898// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
899// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
900QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
901 if (lhs == rhs) return lhs;
902
903 bool t1Unsigned = lhs->isUnsignedIntegerType();
904 bool t2Unsigned = rhs->isUnsignedIntegerType();
905
906 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
907 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
908
909 // We have two integer types with differing signs
910 QualType unsignedType = t1Unsigned ? lhs : rhs;
911 QualType signedType = t1Unsigned ? rhs : lhs;
912
913 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
914 return unsignedType;
915 else {
916 // FIXME: Need to check if the signed type can represent all values of the
917 // unsigned type. If it can, then the result is the signed type.
918 // If it can't, then the result is the unsigned version of the signed type.
919 // Should probably add a helper that returns a signed integer type from
920 // an unsigned (and vice versa). C99 6.3.1.8.
921 return signedType;
922 }
923}
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000924
925// getCFConstantStringType - Return the type used for constant CFStrings.
926QualType ASTContext::getCFConstantStringType() {
927 if (!CFConstantStringTypeDecl) {
928 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
Steve Naroff0add5d22007-11-03 11:27:19 +0000929 &Idents.get("NSConstantString"),
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000930 0);
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000931 QualType FieldTypes[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000932
933 // const int *isa;
934 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000935 // int flags;
936 FieldTypes[1] = IntTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000937 // const char *str;
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000938 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000939 // long length;
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000940 FieldTypes[3] = LongTy;
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000941 // Create fields
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000942 FieldDecl *FieldDecls[4];
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000943
Anders Carlssonbb2cf512007-11-19 00:25:30 +0000944 for (unsigned i = 0; i < 4; ++i)
Steve Naroffdc1ad762007-09-14 02:20:46 +0000945 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000946
947 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
948 }
949
950 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +0000951}
Anders Carlssonfb5b1e82007-10-11 01:00:40 +0000952
Anders Carlssone3f02572007-10-29 06:33:42 +0000953// This returns true if a type has been typedefed to BOOL:
954// typedef <type> BOOL;
Chris Lattnercb034cb2007-10-30 20:27:44 +0000955static bool isTypeTypedefedAsBOOL(QualType T) {
Anders Carlssone3f02572007-10-29 06:33:42 +0000956 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Chris Lattnercb034cb2007-10-30 20:27:44 +0000957 return !strcmp(TT->getDecl()->getName(), "BOOL");
Anders Carlsson36f07d82007-10-29 05:01:08 +0000958
959 return false;
960}
961
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000962/// getObjcEncodingTypeSize returns size of type for objective-c encoding
963/// purpose.
964int ASTContext::getObjcEncodingTypeSize(QualType type) {
965 SourceLocation Loc;
966 uint64_t sz = getTypeSize(type, Loc);
967
968 // Make all integer and enum types at least as large as an int
969 if (sz > 0 && type->isIntegralType())
970 sz = std::max(sz, getTypeSize(IntTy, Loc));
971 // Treat arrays as pointers, since that's how they're passed in.
972 else if (type->isArrayType())
973 sz = getTypeSize(VoidPtrTy, Loc);
974 return sz / getTypeSize(CharTy, Loc);
975}
976
977/// getObjcEncodingForMethodDecl - Return the encoded type for this method
978/// declaration.
979void ASTContext::getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl,
980 std::string& S)
981{
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +0000982 // Encode type qualifer, 'in', 'inout', etc. for the return type.
983 getObjcEncodingForTypeQualifier(Decl->getObjcDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +0000984 // Encode result type.
985 getObjcEncodingForType(Decl->getResultType(), S);
986 // Compute size of all parameters.
987 // Start with computing size of a pointer in number of bytes.
988 // FIXME: There might(should) be a better way of doing this computation!
989 SourceLocation Loc;
990 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
991 // The first two arguments (self and _cmd) are pointers; account for
992 // their size.
993 int ParmOffset = 2 * PtrSize;
994 int NumOfParams = Decl->getNumParams();
995 for (int i = 0; i < NumOfParams; i++) {
996 QualType PType = Decl->getParamDecl(i)->getType();
997 int sz = getObjcEncodingTypeSize (PType);
998 assert (sz > 0 && "getObjcEncodingForMethodDecl - Incomplete param type");
999 ParmOffset += sz;
1000 }
1001 S += llvm::utostr(ParmOffset);
1002 S += "@0:";
1003 S += llvm::utostr(PtrSize);
1004
1005 // Argument types.
1006 ParmOffset = 2 * PtrSize;
1007 for (int i = 0; i < NumOfParams; i++) {
1008 QualType PType = Decl->getParamDecl(i)->getType();
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001009 // Process argument qualifiers for user supplied arguments; such as,
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001010 // 'in', 'inout', etc.
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001011 getObjcEncodingForTypeQualifier(
1012 Decl->getParamDecl(i)->getObjcDeclQualifier(), S);
Fariborz Jahanianc81f3162007-10-29 22:57:28 +00001013 getObjcEncodingForType(PType, S);
1014 S += llvm::utostr(ParmOffset);
1015 ParmOffset += getObjcEncodingTypeSize(PType);
1016 }
1017}
1018
Anders Carlsson36f07d82007-10-29 05:01:08 +00001019void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const
1020{
Anders Carlssone3f02572007-10-29 06:33:42 +00001021 // FIXME: This currently doesn't encode:
1022 // @ An object (whether statically typed or typed id)
1023 // # A class object (Class)
1024 // : A method selector (SEL)
1025 // {name=type...} A structure
1026 // (name=type...) A union
1027 // bnum A bit field of num bits
1028
1029 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001030 char encoding;
1031 switch (BT->getKind()) {
1032 case BuiltinType::Void:
1033 encoding = 'v';
1034 break;
1035 case BuiltinType::Bool:
1036 encoding = 'B';
1037 break;
1038 case BuiltinType::Char_U:
1039 case BuiltinType::UChar:
1040 encoding = 'C';
1041 break;
1042 case BuiltinType::UShort:
1043 encoding = 'S';
1044 break;
1045 case BuiltinType::UInt:
1046 encoding = 'I';
1047 break;
1048 case BuiltinType::ULong:
1049 encoding = 'L';
1050 break;
1051 case BuiltinType::ULongLong:
1052 encoding = 'Q';
1053 break;
1054 case BuiltinType::Char_S:
1055 case BuiltinType::SChar:
1056 encoding = 'c';
1057 break;
1058 case BuiltinType::Short:
1059 encoding = 's';
1060 break;
1061 case BuiltinType::Int:
1062 encoding = 'i';
1063 break;
1064 case BuiltinType::Long:
1065 encoding = 'l';
1066 break;
1067 case BuiltinType::LongLong:
1068 encoding = 'q';
1069 break;
1070 case BuiltinType::Float:
1071 encoding = 'f';
1072 break;
1073 case BuiltinType::Double:
1074 encoding = 'd';
1075 break;
1076 case BuiltinType::LongDouble:
1077 encoding = 'd';
1078 break;
1079 default:
1080 assert(0 && "Unhandled builtin type kind");
1081 }
1082
1083 S += encoding;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001084 }
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +00001085 else if (T->isObjcQualifiedIdType()) {
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001086 // Treat id<P...> same as 'id' for encoding purposes.
Fariborz Jahaniandcb2b1e2007-12-18 21:33:44 +00001087 return getObjcEncodingForType(getObjcIdType(), S);
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001088
1089 }
1090 else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001091 QualType PointeeTy = PT->getPointeeType();
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001092 if (isObjcIdType(PointeeTy) || PointeeTy->isObjcInterfaceType()) {
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001093 S += '@';
1094 return;
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001095 } else if (isObjcClassType(PointeeTy)) {
1096 S += '#';
1097 return;
1098 } else if (isObjcSelType(PointeeTy)) {
1099 S += ':';
1100 return;
Fariborz Jahanian80faffa2007-10-30 17:06:23 +00001101 }
Anders Carlsson36f07d82007-10-29 05:01:08 +00001102
1103 if (PointeeTy->isCharType()) {
1104 // char pointer types should be encoded as '*' unless it is a
1105 // type that has been typedef'd to 'BOOL'.
Anders Carlssone3f02572007-10-29 06:33:42 +00001106 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001107 S += '*';
1108 return;
1109 }
1110 }
1111
1112 S += '^';
1113 getObjcEncodingForType(PT->getPointeeType(), S);
Anders Carlssone3f02572007-10-29 06:33:42 +00001114 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson36f07d82007-10-29 05:01:08 +00001115 S += '[';
1116
1117 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
1118 S += llvm::utostr(CAT->getSize().getZExtValue());
1119 else
1120 assert(0 && "Unhandled array type!");
1121
1122 getObjcEncodingForType(AT->getElementType(), S);
1123 S += ']';
Anders Carlsson5695bb72007-10-30 00:06:20 +00001124 } else if (T->getAsFunctionType()) {
1125 S += '?';
Fariborz Jahanianc8ba2bd2007-11-13 23:21:38 +00001126 } else if (const RecordType *RTy = T->getAsRecordType()) {
1127 RecordDecl *RDecl= RTy->getDecl();
1128 S += '{';
1129 S += RDecl->getName();
1130 S += '=';
1131 for (int i = 0; i < RDecl->getNumMembers(); i++) {
1132 FieldDecl *field = RDecl->getMember(i);
1133 getObjcEncodingForType(field->getType(), S);
1134 }
1135 S += '}';
Steve Naroff49af3f32007-12-12 22:30:11 +00001136 } else if (T->isEnumeralType()) {
1137 S += 'i';
Anders Carlsson36f07d82007-10-29 05:01:08 +00001138 } else
Steve Naroff49af3f32007-12-12 22:30:11 +00001139 assert(0 && "@encode for type not implemented!");
Anders Carlsson36f07d82007-10-29 05:01:08 +00001140}
1141
Fariborz Jahanian65e7eb52007-11-01 17:18:37 +00001142void ASTContext::getObjcEncodingForTypeQualifier(Decl::ObjcDeclQualifier QT,
1143 std::string& S) const {
1144 if (QT & Decl::OBJC_TQ_In)
1145 S += 'n';
1146 if (QT & Decl::OBJC_TQ_Inout)
1147 S += 'N';
1148 if (QT & Decl::OBJC_TQ_Out)
1149 S += 'o';
1150 if (QT & Decl::OBJC_TQ_Bycopy)
1151 S += 'O';
1152 if (QT & Decl::OBJC_TQ_Byref)
1153 S += 'R';
1154 if (QT & Decl::OBJC_TQ_Oneway)
1155 S += 'V';
1156}
1157
Anders Carlssonfb5b1e82007-10-11 01:00:40 +00001158void ASTContext::setBuiltinVaListType(QualType T)
1159{
1160 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1161
1162 BuiltinVaListType = T;
1163}
1164
Steve Naroff9d12c902007-10-15 14:41:52 +00001165void ASTContext::setObjcIdType(TypedefDecl *TD)
1166{
1167 assert(ObjcIdType.isNull() && "'id' type already set!");
1168
1169 ObjcIdType = getTypedefType(TD);
1170
1171 // typedef struct objc_object *id;
1172 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1173 assert(ptr && "'id' incorrectly typed");
1174 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1175 assert(rec && "'id' incorrectly typed");
1176 IdStructType = rec;
1177}
1178
Fariborz Jahanianf807c202007-10-16 20:40:23 +00001179void ASTContext::setObjcSelType(TypedefDecl *TD)
1180{
1181 assert(ObjcSelType.isNull() && "'SEL' type already set!");
1182
1183 ObjcSelType = getTypedefType(TD);
1184
1185 // typedef struct objc_selector *SEL;
1186 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1187 assert(ptr && "'SEL' incorrectly typed");
1188 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1189 assert(rec && "'SEL' incorrectly typed");
1190 SelStructType = rec;
1191}
1192
Fariborz Jahanianb4452ed2007-12-07 00:18:54 +00001193void ASTContext::setObjcProtoType(QualType QT)
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001194{
1195 assert(ObjcProtoType.isNull() && "'Protocol' type already set!");
Fariborz Jahanianb4452ed2007-12-07 00:18:54 +00001196 ObjcProtoType = QT;
Fariborz Jahanianb391e6e2007-10-17 16:58:11 +00001197}
1198
Anders Carlsson7f23e3d2007-10-31 02:53:19 +00001199void ASTContext::setObjcClassType(TypedefDecl *TD)
1200{
1201 assert(ObjcClassType.isNull() && "'Class' type already set!");
1202
1203 ObjcClassType = getTypedefType(TD);
1204
1205 // typedef struct objc_class *Class;
1206 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1207 assert(ptr && "'Class' incorrectly typed");
1208 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1209 assert(rec && "'Class' incorrectly typed");
1210 ClassStructType = rec;
1211}
1212
Steve Narofff2e30312007-10-15 23:35:17 +00001213void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) {
1214 assert(ObjcConstantStringType.isNull() &&
1215 "'NSConstantString' type already set!");
1216
1217 ObjcConstantStringType = getObjcInterfaceType(Decl);
1218}
1219
Steve Naroff85f0dc52007-10-15 20:41:53 +00001220bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1221 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1222 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1223
1224 return lBuiltin->getKind() == rBuiltin->getKind();
1225}
1226
1227
1228bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
1229 if (lhs->isObjcInterfaceType() && isObjcIdType(rhs))
1230 return true;
1231 else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType())
1232 return true;
1233 return false;
1234}
1235
1236bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
1237 return true; // FIXME: IMPLEMENT.
1238}
1239
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001240bool ASTContext::QualifiedInterfaceTypesAreCompatible(QualType lhs,
1241 QualType rhs) {
1242 ObjcQualifiedInterfaceType *lhsQI =
1243 dyn_cast<ObjcQualifiedInterfaceType>(lhs.getCanonicalType().getTypePtr());
1244 assert(lhsQI && "QualifiedInterfaceTypesAreCompatible - bad lhs type");
1245 ObjcQualifiedInterfaceType *rhsQI =
1246 dyn_cast<ObjcQualifiedInterfaceType>(rhs.getCanonicalType().getTypePtr());
1247 assert(rhsQI && "QualifiedInterfaceTypesAreCompatible - bad rhs type");
Fariborz Jahanian0c2f2142007-12-13 20:47:42 +00001248 if (!interfaceTypesAreCompatible(getObjcInterfaceType(lhsQI->getDecl()),
1249 getObjcInterfaceType(rhsQI->getDecl())))
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001250 return false;
1251 /* All protocols in lhs must have a presense in rhs. */
1252 for (unsigned i =0; i < lhsQI->getNumProtocols(); i++) {
1253 bool match = false;
1254 ObjcProtocolDecl *lhsProto = lhsQI->getProtocols(i);
1255 for (unsigned j = 0; j < rhsQI->getNumProtocols(); j++) {
1256 ObjcProtocolDecl *rhsProto = rhsQI->getProtocols(j);
1257 if (lhsProto == rhsProto) {
1258 match = true;
1259 break;
1260 }
1261 }
1262 if (!match)
1263 return false;
1264 }
1265 return true;
1266}
1267
Fariborz Jahanian957442d2007-12-19 17:45:58 +00001268/// ObjcQualifiedIdTypesAreCompatible - Compares two types, at least
1269/// one of which is a protocol qualified 'id' type.
1270bool ASTContext::ObjcQualifiedIdTypesAreCompatible(QualType lhs,
1271 QualType rhs) {
1272 // match id<P..> with an 'id' type in all cases.
1273 if (const PointerType *PT = lhs->getAsPointerType()) {
1274 QualType PointeeTy = PT->getPointeeType();
1275 if (isObjcIdType(PointeeTy))
1276 return true;
1277
1278 }
1279 else if (const PointerType *PT = rhs->getAsPointerType()) {
1280 QualType PointeeTy = PT->getPointeeType();
1281 if (isObjcIdType(PointeeTy))
1282 return true;
1283
1284 }
1285
1286 ObjcQualifiedInterfaceType *lhsQI = 0;
1287 ObjcQualifiedInterfaceType *rhsQI = 0;
1288 ObjcQualifiedIdType *lhsQID = dyn_cast<ObjcQualifiedIdType>(lhs);
1289 ObjcQualifiedIdType *rhsQID = dyn_cast<ObjcQualifiedIdType>(rhs);
1290
1291 if (lhsQID) {
1292 if (!rhsQID && rhs->getTypeClass() == Type::Pointer) {
1293 QualType rtype =
1294 cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1295 rhsQI =
1296 dyn_cast<ObjcQualifiedInterfaceType>(
1297 rtype.getCanonicalType().getTypePtr());
1298 }
1299 if (!rhsQI && !rhsQID)
1300 return false;
1301
1302 for (unsigned i =0; i < lhsQID->getNumProtocols(); i++) {
1303 bool match = false;
1304 ObjcProtocolDecl *lhsProto = lhsQID->getProtocols(i);
1305 unsigned numRhsProtocols;
1306 ObjcProtocolDecl **rhsProtoList;
1307 if (rhsQI) {
1308 numRhsProtocols = rhsQI->getNumProtocols();
1309 rhsProtoList = rhsQI->getReferencedProtocols();
1310 }
1311 else {
1312 numRhsProtocols = rhsQID->getNumProtocols();
1313 rhsProtoList = rhsQID->getReferencedProtocols();
1314 }
1315 for (unsigned j = 0; j < numRhsProtocols; j++) {
1316 ObjcProtocolDecl *rhsProto = rhsProtoList[j];
1317 if (lhsProto == rhsProto) {
1318 match = true;
1319 break;
1320 }
1321 }
1322 if (!match)
1323 return false;
1324 }
1325 }
1326 else if (rhsQID) {
1327 if (!lhsQID && lhs->getTypeClass() == Type::Pointer) {
1328 QualType ltype =
1329 cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1330 lhsQI =
1331 dyn_cast<ObjcQualifiedInterfaceType>(
1332 ltype.getCanonicalType().getTypePtr());
1333 }
1334 if (!lhsQI && !lhsQID)
1335 return false;
1336 unsigned numLhsProtocols;
1337 ObjcProtocolDecl **lhsProtoList;
1338 if (lhsQI) {
1339 numLhsProtocols = lhsQI->getNumProtocols();
1340 lhsProtoList = lhsQI->getReferencedProtocols();
1341 }
1342 else {
1343 numLhsProtocols = lhsQID->getNumProtocols();
1344 lhsProtoList = lhsQID->getReferencedProtocols();
1345 }
1346 for (unsigned i =0; i < numLhsProtocols; i++) {
1347 bool match = false;
1348 ObjcProtocolDecl *lhsProto = lhsProtoList[i];
1349 for (unsigned j = 0; j < rhsQID->getNumProtocols(); j++) {
1350 ObjcProtocolDecl *rhsProto = rhsQID->getProtocols(j);
1351 if (lhsProto == rhsProto) {
1352 match = true;
1353 break;
1354 }
1355 }
1356 if (!match)
1357 return false;
1358 }
1359 }
1360 return true;
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001361}
Fariborz Jahaniane76e8412007-12-17 21:03:50 +00001362
Chris Lattner5003e8b2007-11-01 05:03:41 +00001363bool ASTContext::vectorTypesAreCompatible(QualType lhs, QualType rhs) {
1364 const VectorType *lVector = lhs->getAsVectorType();
1365 const VectorType *rVector = rhs->getAsVectorType();
1366
1367 if ((lVector->getElementType().getCanonicalType() ==
1368 rVector->getElementType().getCanonicalType()) &&
1369 (lVector->getNumElements() == rVector->getNumElements()))
1370 return true;
1371 return false;
1372}
1373
Steve Naroff85f0dc52007-10-15 20:41:53 +00001374// C99 6.2.7p1: If both are complete types, then the following additional
1375// requirements apply...FIXME (handle compatibility across source files).
1376bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
1377 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
1378 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
1379
1380 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
1381 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1382 return true;
1383 }
1384 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
1385 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1386 return true;
1387 }
Steve Naroff4a5e2072007-11-07 06:03:51 +00001388 // "Class" and "id" are compatible built-in structure types.
1389 if (isObjcIdType(lhs) && isObjcClassType(rhs) ||
1390 isObjcClassType(lhs) && isObjcIdType(rhs))
1391 return true;
Steve Naroff85f0dc52007-10-15 20:41:53 +00001392 return false;
1393}
1394
1395bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1396 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1397 // identically qualified and both shall be pointers to compatible types.
1398 if (lhs.getQualifiers() != rhs.getQualifiers())
1399 return false;
1400
1401 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1402 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1403
1404 return typesAreCompatible(ltype, rtype);
1405}
1406
Bill Wendling6a9d8542007-12-03 07:33:35 +00001407// C++ 5.17p6: When the left operand of an assignment operator denotes a
Steve Naroff85f0dc52007-10-15 20:41:53 +00001408// reference to T, the operation assigns to the object of type T denoted by the
1409// reference.
1410bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1411 QualType ltype = lhs;
1412
1413 if (lhs->isReferenceType())
1414 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1415
1416 QualType rtype = rhs;
1417
1418 if (rhs->isReferenceType())
1419 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1420
1421 return typesAreCompatible(ltype, rtype);
1422}
1423
1424bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1425 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1426 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1427 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1428 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1429
1430 // first check the return types (common between C99 and K&R).
1431 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1432 return false;
1433
1434 if (lproto && rproto) { // two C99 style function prototypes
1435 unsigned lproto_nargs = lproto->getNumArgs();
1436 unsigned rproto_nargs = rproto->getNumArgs();
1437
1438 if (lproto_nargs != rproto_nargs)
1439 return false;
1440
1441 // both prototypes have the same number of arguments.
1442 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1443 (rproto->isVariadic() && !lproto->isVariadic()))
1444 return false;
1445
1446 // The use of ellipsis agree...now check the argument types.
1447 for (unsigned i = 0; i < lproto_nargs; i++)
1448 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
1449 return false;
1450 return true;
1451 }
1452 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1453 return true;
1454
1455 // we have a mixture of K&R style with C99 prototypes
1456 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1457
1458 if (proto->isVariadic())
1459 return false;
1460
1461 // FIXME: Each parameter type T in the prototype must be compatible with the
1462 // type resulting from applying the usual argument conversions to T.
1463 return true;
1464}
1465
1466bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
1467 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
1468 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
1469
1470 if (!typesAreCompatible(ltype, rtype))
1471 return false;
1472
1473 // FIXME: If both types specify constant sizes, then the sizes must also be
1474 // the same. Even if the sizes are the same, GCC produces an error.
1475 return true;
1476}
1477
1478/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1479/// both shall have the identically qualified version of a compatible type.
1480/// C99 6.2.7p1: Two types have compatible types if their types are the
1481/// same. See 6.7.[2,3,5] for additional rules.
1482bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
1483 QualType lcanon = lhs.getCanonicalType();
1484 QualType rcanon = rhs.getCanonicalType();
1485
1486 // If two types are identical, they are are compatible
1487 if (lcanon == rcanon)
1488 return true;
Bill Wendling6a9d8542007-12-03 07:33:35 +00001489
1490 // C++ [expr]: If an expression initially has the type "reference to T", the
1491 // type is adjusted to "T" prior to any further analysis, the expression
1492 // designates the object or function denoted by the reference, and the
1493 // expression is an lvalue.
1494 if (lcanon->getTypeClass() == Type::Reference)
1495 lcanon = cast<ReferenceType>(lcanon)->getReferenceeType();
1496 if (rcanon->getTypeClass() == Type::Reference)
1497 rcanon = cast<ReferenceType>(rcanon)->getReferenceeType();
Steve Naroff85f0dc52007-10-15 20:41:53 +00001498
1499 // If the canonical type classes don't match, they can't be compatible
1500 if (lcanon->getTypeClass() != rcanon->getTypeClass()) {
1501 // For Objective-C, it is possible for two types to be compatible
1502 // when their classes don't match (when dealing with "id"). If either type
1503 // is an interface, we defer to objcTypesAreCompatible().
1504 if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType())
1505 return objcTypesAreCompatible(lcanon, rcanon);
1506 return false;
1507 }
1508 switch (lcanon->getTypeClass()) {
1509 case Type::Pointer:
1510 return pointerTypesAreCompatible(lcanon, rcanon);
Steve Naroff85f0dc52007-10-15 20:41:53 +00001511 case Type::ConstantArray:
1512 case Type::VariableArray:
1513 return arrayTypesAreCompatible(lcanon, rcanon);
1514 case Type::FunctionNoProto:
1515 case Type::FunctionProto:
1516 return functionTypesAreCompatible(lcanon, rcanon);
1517 case Type::Tagged: // handle structures, unions
1518 return tagTypesAreCompatible(lcanon, rcanon);
1519 case Type::Builtin:
1520 return builtinTypesAreCompatible(lcanon, rcanon);
1521 case Type::ObjcInterface:
1522 return interfaceTypesAreCompatible(lcanon, rcanon);
Chris Lattner5003e8b2007-11-01 05:03:41 +00001523 case Type::Vector:
1524 case Type::OCUVector:
1525 return vectorTypesAreCompatible(lcanon, rcanon);
Fariborz Jahanian12519d42007-12-12 01:00:23 +00001526 case Type::ObjcQualifiedInterface:
1527 return QualifiedInterfaceTypesAreCompatible(lcanon, rcanon);
Steve Naroff85f0dc52007-10-15 20:41:53 +00001528 default:
1529 assert(0 && "unexpected type");
1530 }
1531 return true; // should never get here...
1532}
Ted Kremenek738e6c02007-10-31 17:10:13 +00001533
Ted Kremenek738e6c02007-10-31 17:10:13 +00001534/// Emit - Serialize an ASTContext object to Bitcode.
1535void ASTContext::Emit(llvm::Serializer& S) const {
Ted Kremenek9af4d5c2007-10-31 20:00:03 +00001536 S.EmitRef(SourceMgr);
1537 S.EmitRef(Target);
1538 S.EmitRef(Idents);
1539 S.EmitRef(Selectors);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001540
Ted Kremenek68228a92007-10-31 22:44:07 +00001541 // Emit the size of the type vector so that we can reserve that size
1542 // when we reconstitute the ASTContext object.
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001543 S.EmitInt(Types.size());
1544
Ted Kremenek034a78c2007-11-13 22:02:55 +00001545 for (std::vector<Type*>::const_iterator I=Types.begin(), E=Types.end();
1546 I!=E;++I)
1547 (*I)->Emit(S);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001548
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001549 // FIXME: S.EmitOwnedPtr(CFConstantStringTypeDecl);
Ted Kremenek738e6c02007-10-31 17:10:13 +00001550}
1551
Ted Kremenekacba3612007-11-13 00:25:37 +00001552ASTContext* ASTContext::Create(llvm::Deserializer& D) {
Ted Kremenek68228a92007-10-31 22:44:07 +00001553 SourceManager &SM = D.ReadRef<SourceManager>();
1554 TargetInfo &t = D.ReadRef<TargetInfo>();
1555 IdentifierTable &idents = D.ReadRef<IdentifierTable>();
1556 SelectorTable &sels = D.ReadRef<SelectorTable>();
1557
1558 unsigned size_reserve = D.ReadInt();
1559
1560 ASTContext* A = new ASTContext(SM,t,idents,sels,size_reserve);
1561
Ted Kremenek034a78c2007-11-13 22:02:55 +00001562 for (unsigned i = 0; i < size_reserve; ++i)
1563 Type::Create(*A,i,D);
Ted Kremenek0199d9f2007-11-06 22:26:16 +00001564
Ted Kremeneke1fed7a2007-11-01 18:11:32 +00001565 // FIXME: A->CFConstantStringTypeDecl = D.ReadOwnedPtr<RecordDecl>();
Ted Kremenek68228a92007-10-31 22:44:07 +00001566
1567 return A;
1568}