blob: 86cf13c4f6e1236ee1c9ce9cc558cb845b2afeb5 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
Steve Naroff980e5082007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000017#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21enum FloatingRank {
22 FloatRank, DoubleRank, LongDoubleRank
23};
24
25ASTContext::~ASTContext() {
26 // Deallocate all the types.
27 while (!Types.empty()) {
28 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
29 // Destroy the object, but don't call delete. These are malloc'd.
30 FT->~FunctionTypeProto();
31 free(FT);
32 } else {
33 delete Types.back();
34 }
35 Types.pop_back();
36 }
37}
38
39void ASTContext::PrintStats() const {
40 fprintf(stderr, "*** AST Context Stats:\n");
41 fprintf(stderr, " %d types total.\n", (int)Types.size());
42 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Chris Lattner6d87fc62007-07-18 05:50:59 +000043 unsigned NumVector = 0, NumComplex = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000044 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
45
46 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000047 unsigned NumObjcInterfaces = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000048
49 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
50 Type *T = Types[i];
51 if (isa<BuiltinType>(T))
52 ++NumBuiltin;
53 else if (isa<PointerType>(T))
54 ++NumPointer;
55 else if (isa<ReferenceType>(T))
56 ++NumReference;
Chris Lattner6d87fc62007-07-18 05:50:59 +000057 else if (isa<ComplexType>(T))
58 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +000059 else if (isa<ArrayType>(T))
60 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +000061 else if (isa<VectorType>(T))
62 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +000063 else if (isa<FunctionTypeNoProto>(T))
64 ++NumFunctionNP;
65 else if (isa<FunctionTypeProto>(T))
66 ++NumFunctionP;
67 else if (isa<TypedefType>(T))
68 ++NumTypeName;
69 else if (TagType *TT = dyn_cast<TagType>(T)) {
70 ++NumTagged;
71 switch (TT->getDecl()->getKind()) {
72 default: assert(0 && "Unknown tagged type!");
73 case Decl::Struct: ++NumTagStruct; break;
74 case Decl::Union: ++NumTagUnion; break;
75 case Decl::Class: ++NumTagClass; break;
76 case Decl::Enum: ++NumTagEnum; break;
77 }
Steve Naroff3f128ad2007-09-17 14:16:13 +000078 } else if (isa<ObjcInterfaceType>(T))
79 ++NumObjcInterfaces;
80 else {
Reid Spencer5f016e22007-07-11 17:01:13 +000081 assert(0 && "Unknown type!");
82 }
83 }
84
85 fprintf(stderr, " %d builtin types\n", NumBuiltin);
86 fprintf(stderr, " %d pointer types\n", NumPointer);
87 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner6d87fc62007-07-18 05:50:59 +000088 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +000089 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +000090 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +000091 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
92 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
93 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
94 fprintf(stderr, " %d tagged types\n", NumTagged);
95 fprintf(stderr, " %d struct types\n", NumTagStruct);
96 fprintf(stderr, " %d union types\n", NumTagUnion);
97 fprintf(stderr, " %d class types\n", NumTagClass);
98 fprintf(stderr, " %d enum types\n", NumTagEnum);
Steve Naroff3f128ad2007-09-17 14:16:13 +000099 fprintf(stderr, " %d interface types\n", NumObjcInterfaces);
Reid Spencer5f016e22007-07-11 17:01:13 +0000100 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
101 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000102 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 NumFunctionP*sizeof(FunctionTypeProto)+
104 NumFunctionNP*sizeof(FunctionTypeNoProto)+
105 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
106}
107
108
109void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
110 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
111}
112
Reid Spencer5f016e22007-07-11 17:01:13 +0000113void ASTContext::InitBuiltinTypes() {
114 assert(VoidTy.isNull() && "Context reinitialized?");
115
116 // C99 6.2.5p19.
117 InitBuiltinType(VoidTy, BuiltinType::Void);
118
119 // C99 6.2.5p2.
120 InitBuiltinType(BoolTy, BuiltinType::Bool);
121 // C99 6.2.5p3.
122 if (Target.isCharSigned(SourceLocation()))
123 InitBuiltinType(CharTy, BuiltinType::Char_S);
124 else
125 InitBuiltinType(CharTy, BuiltinType::Char_U);
126 // C99 6.2.5p4.
127 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
128 InitBuiltinType(ShortTy, BuiltinType::Short);
129 InitBuiltinType(IntTy, BuiltinType::Int);
130 InitBuiltinType(LongTy, BuiltinType::Long);
131 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
132
133 // C99 6.2.5p6.
134 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
135 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
136 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
137 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
138 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
139
140 // C99 6.2.5p10.
141 InitBuiltinType(FloatTy, BuiltinType::Float);
142 InitBuiltinType(DoubleTy, BuiltinType::Double);
143 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
144
145 // C99 6.2.5p11.
146 FloatComplexTy = getComplexType(FloatTy);
147 DoubleComplexTy = getComplexType(DoubleTy);
148 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff7e219e42007-10-15 14:41:52 +0000149
150 BuiltinVaListType = QualType();
151 ObjcIdType = QualType();
152 IdStructType = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000153}
154
Chris Lattner464175b2007-07-18 17:52:12 +0000155//===----------------------------------------------------------------------===//
156// Type Sizing and Analysis
157//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000158
159/// getTypeSize - Return the size of the specified type, in bits. This method
160/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000161std::pair<uint64_t, unsigned>
162ASTContext::getTypeInfo(QualType T, SourceLocation L) {
Chris Lattnera7674d82007-07-13 22:13:22 +0000163 T = T.getCanonicalType();
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000164 uint64_t Size;
165 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000166 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000167 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000168 case Type::FunctionNoProto:
169 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000170 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000171 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000172 case Type::VariableArray:
173 assert(0 && "VLAs not implemented yet!");
174 case Type::ConstantArray: {
175 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
176
Chris Lattner030d8842007-07-19 22:06:24 +0000177 std::pair<uint64_t, unsigned> EltInfo =
Steve Narofffb22d962007-08-30 01:06:46 +0000178 getTypeInfo(CAT->getElementType(), L);
179 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000180 Align = EltInfo.second;
181 break;
182 }
183 case Type::Vector: {
184 std::pair<uint64_t, unsigned> EltInfo =
185 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
186 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
187 // FIXME: Vector alignment is not the alignment of its elements.
188 Align = EltInfo.second;
189 break;
190 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000191
Chris Lattnera7674d82007-07-13 22:13:22 +0000192 case Type::Builtin: {
193 // FIXME: need to use TargetInfo to derive the target specific sizes. This
194 // implementation will suffice for play with vector support.
Chris Lattner525a0502007-09-22 18:29:59 +0000195 const llvm::fltSemantics *F;
Chris Lattnera7674d82007-07-13 22:13:22 +0000196 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000197 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000198 case BuiltinType::Void:
199 assert(0 && "Incomplete types have no size!");
200 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000201 case BuiltinType::Char_S:
202 case BuiltinType::Char_U:
203 case BuiltinType::UChar:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000204 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000205 case BuiltinType::UShort:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000206 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000207 case BuiltinType::UInt:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000208 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000209 case BuiltinType::ULong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000210 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000211 case BuiltinType::ULongLong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000212 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
Chris Lattner525a0502007-09-22 18:29:59 +0000213 case BuiltinType::Float: Target.getFloatInfo(Size, Align, F, L); break;
214 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, F, L);break;
215 case BuiltinType::LongDouble:Target.getLongDoubleInfo(Size,Align,F,L);break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000216 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000217 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000218 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000219 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000220 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000221 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000222 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
223 // FIXME: This is wrong for struct layout!
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000224 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000225
226 case Type::Complex: {
227 // Complex types have the same alignment as their elements, but twice the
228 // size.
229 std::pair<uint64_t, unsigned> EltInfo =
230 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
231 Size = EltInfo.first*2;
232 Align = EltInfo.second;
233 break;
234 }
235 case Type::Tagged:
Chris Lattner6cd862c2007-08-27 17:38:00 +0000236 TagType *TT = cast<TagType>(T);
237 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
238 const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L);
239 Size = Layout.getSize();
240 Align = Layout.getAlignment();
241 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattnere00b18c2007-08-28 18:24:31 +0000242 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000243 } else {
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000244 assert(0 && "Unimplemented type sizes!");
Chris Lattner6cd862c2007-08-27 17:38:00 +0000245 }
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000246 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000247 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000248
Chris Lattner464175b2007-07-18 17:52:12 +0000249 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000250 return std::make_pair(Size, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000251}
252
Chris Lattner464175b2007-07-18 17:52:12 +0000253/// getRecordLayout - Get or compute information about the layout of the
254/// specified record (struct/union/class), which indicates its size and field
255/// position information.
256const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D,
257 SourceLocation L) {
258 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
259
260 // Look up this layout, if already laid out, return what we have.
261 const RecordLayout *&Entry = RecordLayoutInfo[D];
262 if (Entry) return *Entry;
263
264 // Allocate and assign into RecordLayoutInfo here. The "Entry" reference can
265 // be invalidated (dangle) if the RecordLayoutInfo hashtable is inserted into.
266 RecordLayout *NewEntry = new RecordLayout();
267 Entry = NewEntry;
268
269 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
270 uint64_t RecordSize = 0;
271 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
272
273 if (D->getKind() != Decl::Union) {
274 // Layout each field, for now, just sequentially, respecting alignment. In
275 // the future, this will need to be tweakable by targets.
276 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
277 const FieldDecl *FD = D->getMember(i);
278 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
279 uint64_t FieldSize = FieldInfo.first;
280 unsigned FieldAlign = FieldInfo.second;
281
282 // Round up the current record size to the field's alignment boundary.
283 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
284
285 // Place this field at the current location.
286 FieldOffsets[i] = RecordSize;
287
288 // Reserve space for this field.
289 RecordSize += FieldSize;
290
291 // Remember max struct/class alignment.
292 RecordAlign = std::max(RecordAlign, FieldAlign);
293 }
294
295 // Finally, round the size of the total struct up to the alignment of the
296 // struct itself.
297 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
298 } else {
299 // Union layout just puts each member at the start of the record.
300 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
301 const FieldDecl *FD = D->getMember(i);
302 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
303 uint64_t FieldSize = FieldInfo.first;
304 unsigned FieldAlign = FieldInfo.second;
305
306 // Round up the current record size to the field's alignment boundary.
307 RecordSize = std::max(RecordSize, FieldSize);
308
309 // Place this field at the start of the record.
310 FieldOffsets[i] = 0;
311
312 // Remember max struct/class alignment.
313 RecordAlign = std::max(RecordAlign, FieldAlign);
314 }
315 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000316
317 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
318 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000319}
320
Chris Lattnera7674d82007-07-13 22:13:22 +0000321//===----------------------------------------------------------------------===//
322// Type creation/memoization methods
323//===----------------------------------------------------------------------===//
324
325
Reid Spencer5f016e22007-07-11 17:01:13 +0000326/// getComplexType - Return the uniqued reference to the type for a complex
327/// number with the specified element type.
328QualType ASTContext::getComplexType(QualType T) {
329 // Unique pointers, to guarantee there is only one pointer of a particular
330 // structure.
331 llvm::FoldingSetNodeID ID;
332 ComplexType::Profile(ID, T);
333
334 void *InsertPos = 0;
335 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
336 return QualType(CT, 0);
337
338 // If the pointee type isn't canonical, this won't be a canonical type either,
339 // so fill in the canonical type field.
340 QualType Canonical;
341 if (!T->isCanonical()) {
342 Canonical = getComplexType(T.getCanonicalType());
343
344 // Get the new insert position for the node we care about.
345 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
346 assert(NewIP == 0 && "Shouldn't be in the map!");
347 }
348 ComplexType *New = new ComplexType(T, Canonical);
349 Types.push_back(New);
350 ComplexTypes.InsertNode(New, InsertPos);
351 return QualType(New, 0);
352}
353
354
355/// getPointerType - Return the uniqued reference to the type for a pointer to
356/// the specified type.
357QualType ASTContext::getPointerType(QualType T) {
358 // Unique pointers, to guarantee there is only one pointer of a particular
359 // structure.
360 llvm::FoldingSetNodeID ID;
361 PointerType::Profile(ID, T);
362
363 void *InsertPos = 0;
364 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
365 return QualType(PT, 0);
366
367 // If the pointee type isn't canonical, this won't be a canonical type either,
368 // so fill in the canonical type field.
369 QualType Canonical;
370 if (!T->isCanonical()) {
371 Canonical = getPointerType(T.getCanonicalType());
372
373 // Get the new insert position for the node we care about.
374 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
375 assert(NewIP == 0 && "Shouldn't be in the map!");
376 }
377 PointerType *New = new PointerType(T, Canonical);
378 Types.push_back(New);
379 PointerTypes.InsertNode(New, InsertPos);
380 return QualType(New, 0);
381}
382
383/// getReferenceType - Return the uniqued reference to the type for a reference
384/// to the specified type.
385QualType ASTContext::getReferenceType(QualType T) {
386 // Unique pointers, to guarantee there is only one pointer of a particular
387 // structure.
388 llvm::FoldingSetNodeID ID;
389 ReferenceType::Profile(ID, T);
390
391 void *InsertPos = 0;
392 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
393 return QualType(RT, 0);
394
395 // If the referencee type isn't canonical, this won't be a canonical type
396 // either, so fill in the canonical type field.
397 QualType Canonical;
398 if (!T->isCanonical()) {
399 Canonical = getReferenceType(T.getCanonicalType());
400
401 // Get the new insert position for the node we care about.
402 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
403 assert(NewIP == 0 && "Shouldn't be in the map!");
404 }
405
406 ReferenceType *New = new ReferenceType(T, Canonical);
407 Types.push_back(New);
408 ReferenceTypes.InsertNode(New, InsertPos);
409 return QualType(New, 0);
410}
411
Steve Narofffb22d962007-08-30 01:06:46 +0000412/// getConstantArrayType - Return the unique reference to the type for an
413/// array of the specified element type.
414QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000415 const llvm::APInt &ArySize,
416 ArrayType::ArraySizeModifier ASM,
417 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000418 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000419 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000420
421 void *InsertPos = 0;
Steve Narofffb22d962007-08-30 01:06:46 +0000422 if (ConstantArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 return QualType(ATP, 0);
424
425 // If the element 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 (!EltTy->isCanonical()) {
Steve Naroffc9406122007-08-30 18:10:14 +0000429 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
430 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 // Get the new insert position for the node we care about.
Steve Narofffb22d962007-08-30 01:06:46 +0000432 ConstantArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000433 assert(NewIP == 0 && "Shouldn't be in the map!");
434 }
435
Steve Naroffc9406122007-08-30 18:10:14 +0000436 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
437 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 ArrayTypes.InsertNode(New, InsertPos);
439 Types.push_back(New);
440 return QualType(New, 0);
441}
442
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000443/// getVariableArrayType - Returns a non-unique reference to the type for a
444/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000445QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
446 ArrayType::ArraySizeModifier ASM,
447 unsigned EltTypeQuals) {
448 // Since we don't unique expressions, it isn't possible to unique VLA's.
449 ArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
450 ASM, EltTypeQuals);
451 Types.push_back(New);
452 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000453}
454
Steve Naroff73322922007-07-18 18:00:27 +0000455/// getVectorType - Return the unique reference to a vector type of
456/// the specified element type and size. VectorType must be a built-in type.
457QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 BuiltinType *baseType;
459
460 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000461 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000462
463 // Check if we've already instantiated a vector of this type.
464 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000465 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000466 void *InsertPos = 0;
467 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
468 return QualType(VTP, 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 (!vecType->isCanonical()) {
Steve Naroff73322922007-07-18 18:00:27 +0000474 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000475
476 // Get the new insert position for the node we care about.
477 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
478 assert(NewIP == 0 && "Shouldn't be in the map!");
479 }
480 VectorType *New = new VectorType(vecType, NumElts, Canonical);
481 VectorTypes.InsertNode(New, InsertPos);
482 Types.push_back(New);
483 return QualType(New, 0);
484}
485
Steve Naroff73322922007-07-18 18:00:27 +0000486/// getOCUVectorType - Return the unique reference to an OCU vector type of
487/// the specified element type and size. VectorType must be a built-in type.
488QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
489 BuiltinType *baseType;
490
491 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
492 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
493
494 // Check if we've already instantiated a vector of this type.
495 llvm::FoldingSetNodeID ID;
496 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
497 void *InsertPos = 0;
498 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
499 return QualType(VTP, 0);
500
501 // If the element type isn't canonical, this won't be a canonical type either,
502 // so fill in the canonical type field.
503 QualType Canonical;
504 if (!vecType->isCanonical()) {
505 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
506
507 // Get the new insert position for the node we care about.
508 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
509 assert(NewIP == 0 && "Shouldn't be in the map!");
510 }
511 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
512 VectorTypes.InsertNode(New, InsertPos);
513 Types.push_back(New);
514 return QualType(New, 0);
515}
516
Reid Spencer5f016e22007-07-11 17:01:13 +0000517/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
518///
519QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
520 // Unique functions, to guarantee there is only one function of a particular
521 // structure.
522 llvm::FoldingSetNodeID ID;
523 FunctionTypeNoProto::Profile(ID, ResultTy);
524
525 void *InsertPos = 0;
526 if (FunctionTypeNoProto *FT =
527 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
528 return QualType(FT, 0);
529
530 QualType Canonical;
531 if (!ResultTy->isCanonical()) {
532 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
533
534 // Get the new insert position for the node we care about.
535 FunctionTypeNoProto *NewIP =
536 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
537 assert(NewIP == 0 && "Shouldn't be in the map!");
538 }
539
540 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
541 Types.push_back(New);
542 FunctionTypeProtos.InsertNode(New, InsertPos);
543 return QualType(New, 0);
544}
545
546/// getFunctionType - Return a normal function type with a typed argument
547/// list. isVariadic indicates whether the argument list includes '...'.
548QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
549 unsigned NumArgs, bool isVariadic) {
550 // Unique functions, to guarantee there is only one function of a particular
551 // structure.
552 llvm::FoldingSetNodeID ID;
553 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
554
555 void *InsertPos = 0;
556 if (FunctionTypeProto *FTP =
557 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
558 return QualType(FTP, 0);
559
560 // Determine whether the type being created is already canonical or not.
561 bool isCanonical = ResultTy->isCanonical();
562 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
563 if (!ArgArray[i]->isCanonical())
564 isCanonical = false;
565
566 // If this type isn't canonical, get the canonical version of it.
567 QualType Canonical;
568 if (!isCanonical) {
569 llvm::SmallVector<QualType, 16> CanonicalArgs;
570 CanonicalArgs.reserve(NumArgs);
571 for (unsigned i = 0; i != NumArgs; ++i)
572 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
573
574 Canonical = getFunctionType(ResultTy.getCanonicalType(),
575 &CanonicalArgs[0], NumArgs,
576 isVariadic);
577
578 // Get the new insert position for the node we care about.
579 FunctionTypeProto *NewIP =
580 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
581 assert(NewIP == 0 && "Shouldn't be in the map!");
582 }
583
584 // FunctionTypeProto objects are not allocated with new because they have a
585 // variable size array (for parameter types) at the end of them.
586 FunctionTypeProto *FTP =
587 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000588 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
590 Canonical);
591 Types.push_back(FTP);
592 FunctionTypeProtos.InsertNode(FTP, InsertPos);
593 return QualType(FTP, 0);
594}
595
596/// getTypedefType - Return the unique reference to the type for the
597/// specified typename decl.
598QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
599 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
600
601 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
602 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
603 Types.push_back(Decl->TypeForDecl);
604 return QualType(Decl->TypeForDecl, 0);
605}
606
Steve Naroff3536b442007-09-06 21:24:23 +0000607/// getObjcInterfaceType - Return the unique reference to the type for the
608/// specified ObjC interface decl.
609QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) {
610 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
611
612 Decl->TypeForDecl = new ObjcInterfaceType(Decl);
613 Types.push_back(Decl->TypeForDecl);
614 return QualType(Decl->TypeForDecl, 0);
615}
616
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000617/// getObjcQualifiedInterfaceType - Return a
618/// ObjcQualifiedInterfaceType type for the given interface decl and
619/// the conforming protocol list.
620QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl,
621 ObjcProtocolDecl **Protocols, unsigned NumProtocols) {
622 ObjcInterfaceType *IType =
623 cast<ObjcInterfaceType>(getObjcInterfaceType(Decl));
624
625 llvm::FoldingSetNodeID ID;
626 ObjcQualifiedInterfaceType::Profile(ID, IType, Protocols, NumProtocols);
627
628 void *InsertPos = 0;
629 if (ObjcQualifiedInterfaceType *QT =
630 ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
631 return QualType(QT, 0);
632
633 // No Match;
Chris Lattner00bb2832007-10-11 03:36:41 +0000634 ObjcQualifiedInterfaceType *QType =
635 new ObjcQualifiedInterfaceType(IType, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000636 Types.push_back(QType);
637 ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
638 return QualType(QType, 0);
639}
640
Steve Naroff9752f252007-08-01 18:02:17 +0000641/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
642/// TypeOfExpr AST's (since expression's are never shared). For example,
643/// multiple declarations that refer to "typeof(x)" all contain different
644/// DeclRefExpr's. This doesn't effect the type checker, since it operates
645/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000646QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffd1861fd2007-07-31 12:34:36 +0000647 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000648 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
649 Types.push_back(toe);
650 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000651}
652
Steve Naroff9752f252007-08-01 18:02:17 +0000653/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
654/// TypeOfType AST's. The only motivation to unique these nodes would be
655/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
656/// an issue. This doesn't effect the type checker, since it operates
657/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000658QualType ASTContext::getTypeOfType(QualType tofType) {
659 QualType Canonical = tofType.getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000660 TypeOfType *tot = new TypeOfType(tofType, Canonical);
661 Types.push_back(tot);
662 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000663}
664
Reid Spencer5f016e22007-07-11 17:01:13 +0000665/// getTagDeclType - Return the unique reference to the type for the
666/// specified TagDecl (struct/union/class/enum) decl.
667QualType ASTContext::getTagDeclType(TagDecl *Decl) {
668 // The decl stores the type cache.
669 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
670
671 Decl->TypeForDecl = new TagType(Decl, QualType());
672 Types.push_back(Decl->TypeForDecl);
673 return QualType(Decl->TypeForDecl, 0);
674}
675
676/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
677/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
678/// needs to agree with the definition in <stddef.h>.
679QualType ASTContext::getSizeType() const {
680 // On Darwin, size_t is defined as a "long unsigned int".
681 // FIXME: should derive from "Target".
682 return UnsignedLongTy;
683}
684
Chris Lattner8b9023b2007-07-13 03:05:23 +0000685/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
686/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
687QualType ASTContext::getPointerDiffType() const {
688 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
689 // FIXME: should derive from "Target".
690 return IntTy;
691}
692
Reid Spencer5f016e22007-07-11 17:01:13 +0000693/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
694/// routine will assert if passed a built-in type that isn't an integer or enum.
695static int getIntegerRank(QualType t) {
696 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
697 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
698 return 4;
699 }
700
701 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
702 switch (BT->getKind()) {
703 default:
704 assert(0 && "getIntegerRank(): not a built-in integer");
705 case BuiltinType::Bool:
706 return 1;
707 case BuiltinType::Char_S:
708 case BuiltinType::Char_U:
709 case BuiltinType::SChar:
710 case BuiltinType::UChar:
711 return 2;
712 case BuiltinType::Short:
713 case BuiltinType::UShort:
714 return 3;
715 case BuiltinType::Int:
716 case BuiltinType::UInt:
717 return 4;
718 case BuiltinType::Long:
719 case BuiltinType::ULong:
720 return 5;
721 case BuiltinType::LongLong:
722 case BuiltinType::ULongLong:
723 return 6;
724 }
725}
726
727/// getFloatingRank - Return a relative rank for floating point types.
728/// This routine will assert if passed a built-in type that isn't a float.
729static int getFloatingRank(QualType T) {
730 T = T.getCanonicalType();
731 if (ComplexType *CT = dyn_cast<ComplexType>(T))
732 return getFloatingRank(CT->getElementType());
733
734 switch (cast<BuiltinType>(T)->getKind()) {
735 default: assert(0 && "getFloatingPointRank(): not a floating type");
736 case BuiltinType::Float: return FloatRank;
737 case BuiltinType::Double: return DoubleRank;
738 case BuiltinType::LongDouble: return LongDoubleRank;
739 }
740}
741
Steve Naroff716c7302007-08-27 01:41:48 +0000742/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
743/// point or a complex type (based on typeDomain/typeSize).
744/// 'typeDomain' is a real floating point or complex type.
745/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +0000746QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
747 QualType typeSize, QualType typeDomain) const {
748 if (typeDomain->isComplexType()) {
749 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000750 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000751 case FloatRank: return FloatComplexTy;
752 case DoubleRank: return DoubleComplexTy;
753 case LongDoubleRank: return LongDoubleComplexTy;
754 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 }
Steve Narofff1448a02007-08-27 01:27:54 +0000756 if (typeDomain->isRealFloatingType()) {
757 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000758 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000759 case FloatRank: return FloatTy;
760 case DoubleRank: return DoubleTy;
761 case LongDoubleRank: return LongDoubleTy;
762 }
763 }
764 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000765 //an invalid return value, but the assert
766 //will ensure that this code is never reached.
767 return VoidTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000768}
769
Steve Narofffb0d4962007-08-27 15:30:22 +0000770/// compareFloatingType - Handles 3 different combos:
771/// float/float, float/complex, complex/complex.
772/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
773int ASTContext::compareFloatingType(QualType lt, QualType rt) {
774 if (getFloatingRank(lt) == getFloatingRank(rt))
775 return 0;
776 if (getFloatingRank(lt) > getFloatingRank(rt))
777 return 1;
778 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000779}
780
781// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
782// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
783QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
784 if (lhs == rhs) return lhs;
785
786 bool t1Unsigned = lhs->isUnsignedIntegerType();
787 bool t2Unsigned = rhs->isUnsignedIntegerType();
788
789 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
790 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
791
792 // We have two integer types with differing signs
793 QualType unsignedType = t1Unsigned ? lhs : rhs;
794 QualType signedType = t1Unsigned ? rhs : lhs;
795
796 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
797 return unsignedType;
798 else {
799 // FIXME: Need to check if the signed type can represent all values of the
800 // unsigned type. If it can, then the result is the signed type.
801 // If it can't, then the result is the unsigned version of the signed type.
802 // Should probably add a helper that returns a signed integer type from
803 // an unsigned (and vice versa). C99 6.3.1.8.
804 return signedType;
805 }
806}
Anders Carlsson71993dd2007-08-17 05:31:46 +0000807
808// getCFConstantStringType - Return the type used for constant CFStrings.
809QualType ASTContext::getCFConstantStringType() {
810 if (!CFConstantStringTypeDecl) {
811 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
812 &Idents.get("__builtin_CFString"),
813 0);
814
815 QualType FieldTypes[4];
816
817 // const int *isa;
818 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
819 // int flags;
820 FieldTypes[1] = IntTy;
821 // const char *str;
822 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
823 // long length;
824 FieldTypes[3] = LongTy;
825 // Create fields
826 FieldDecl *FieldDecls[4];
827
828 for (unsigned i = 0; i < 4; ++i)
Steve Narofff38661e2007-09-14 02:20:46 +0000829 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +0000830
831 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
832 }
833
834 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +0000835}
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000836
837void ASTContext::setBuiltinVaListType(QualType T)
838{
839 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
840
841 BuiltinVaListType = T;
842}
843
Steve Naroff7e219e42007-10-15 14:41:52 +0000844void ASTContext::setObjcIdType(TypedefDecl *TD)
845{
846 assert(ObjcIdType.isNull() && "'id' type already set!");
847
848 ObjcIdType = getTypedefType(TD);
849
850 // typedef struct objc_object *id;
851 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
852 assert(ptr && "'id' incorrectly typed");
853 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
854 assert(rec && "'id' incorrectly typed");
855 IdStructType = rec;
856}
857
Steve Naroffec0550f2007-10-15 20:41:53 +0000858bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
859 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
860 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
861
862 return lBuiltin->getKind() == rBuiltin->getKind();
863}
864
865
866bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
867 if (lhs->isObjcInterfaceType() && isObjcIdType(rhs))
868 return true;
869 else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType())
870 return true;
871 return false;
872}
873
874bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
875 return true; // FIXME: IMPLEMENT.
876}
877
878// C99 6.2.7p1: If both are complete types, then the following additional
879// requirements apply...FIXME (handle compatibility across source files).
880bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
881 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
882 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
883
884 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
885 if (ldecl->getIdentifier() == rdecl->getIdentifier())
886 return true;
887 }
888 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
889 if (ldecl->getIdentifier() == rdecl->getIdentifier())
890 return true;
891 }
892 return false;
893}
894
895bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
896 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
897 // identically qualified and both shall be pointers to compatible types.
898 if (lhs.getQualifiers() != rhs.getQualifiers())
899 return false;
900
901 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
902 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
903
904 return typesAreCompatible(ltype, rtype);
905}
906
907// C++ 5.17p6: When the left opperand of an assignment operator denotes a
908// reference to T, the operation assigns to the object of type T denoted by the
909// reference.
910bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
911 QualType ltype = lhs;
912
913 if (lhs->isReferenceType())
914 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
915
916 QualType rtype = rhs;
917
918 if (rhs->isReferenceType())
919 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
920
921 return typesAreCompatible(ltype, rtype);
922}
923
924bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
925 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
926 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
927 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
928 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
929
930 // first check the return types (common between C99 and K&R).
931 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
932 return false;
933
934 if (lproto && rproto) { // two C99 style function prototypes
935 unsigned lproto_nargs = lproto->getNumArgs();
936 unsigned rproto_nargs = rproto->getNumArgs();
937
938 if (lproto_nargs != rproto_nargs)
939 return false;
940
941 // both prototypes have the same number of arguments.
942 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
943 (rproto->isVariadic() && !lproto->isVariadic()))
944 return false;
945
946 // The use of ellipsis agree...now check the argument types.
947 for (unsigned i = 0; i < lproto_nargs; i++)
948 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
949 return false;
950 return true;
951 }
952 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
953 return true;
954
955 // we have a mixture of K&R style with C99 prototypes
956 const FunctionTypeProto *proto = lproto ? lproto : rproto;
957
958 if (proto->isVariadic())
959 return false;
960
961 // FIXME: Each parameter type T in the prototype must be compatible with the
962 // type resulting from applying the usual argument conversions to T.
963 return true;
964}
965
966bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
967 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
968 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
969
970 if (!typesAreCompatible(ltype, rtype))
971 return false;
972
973 // FIXME: If both types specify constant sizes, then the sizes must also be
974 // the same. Even if the sizes are the same, GCC produces an error.
975 return true;
976}
977
978/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
979/// both shall have the identically qualified version of a compatible type.
980/// C99 6.2.7p1: Two types have compatible types if their types are the
981/// same. See 6.7.[2,3,5] for additional rules.
982bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
983 QualType lcanon = lhs.getCanonicalType();
984 QualType rcanon = rhs.getCanonicalType();
985
986 // If two types are identical, they are are compatible
987 if (lcanon == rcanon)
988 return true;
989
990 // If the canonical type classes don't match, they can't be compatible
991 if (lcanon->getTypeClass() != rcanon->getTypeClass()) {
992 // For Objective-C, it is possible for two types to be compatible
993 // when their classes don't match (when dealing with "id"). If either type
994 // is an interface, we defer to objcTypesAreCompatible().
995 if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType())
996 return objcTypesAreCompatible(lcanon, rcanon);
997 return false;
998 }
999 switch (lcanon->getTypeClass()) {
1000 case Type::Pointer:
1001 return pointerTypesAreCompatible(lcanon, rcanon);
1002 case Type::Reference:
1003 return referenceTypesAreCompatible(lcanon, rcanon);
1004 case Type::ConstantArray:
1005 case Type::VariableArray:
1006 return arrayTypesAreCompatible(lcanon, rcanon);
1007 case Type::FunctionNoProto:
1008 case Type::FunctionProto:
1009 return functionTypesAreCompatible(lcanon, rcanon);
1010 case Type::Tagged: // handle structures, unions
1011 return tagTypesAreCompatible(lcanon, rcanon);
1012 case Type::Builtin:
1013 return builtinTypesAreCompatible(lcanon, rcanon);
1014 case Type::ObjcInterface:
1015 return interfaceTypesAreCompatible(lcanon, rcanon);
1016 default:
1017 assert(0 && "unexpected type");
1018 }
1019 return true; // should never get here...
1020}