blob: cf617d190eebd873c47843f5ee4866a452d77b0e [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;
Steve Naroff21988912007-10-15 23:35:17 +0000153 ObjcConstantStringType = QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000154}
155
Chris Lattner464175b2007-07-18 17:52:12 +0000156//===----------------------------------------------------------------------===//
157// Type Sizing and Analysis
158//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000159
160/// getTypeSize - Return the size of the specified type, in bits. This method
161/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000162std::pair<uint64_t, unsigned>
163ASTContext::getTypeInfo(QualType T, SourceLocation L) {
Chris Lattnera7674d82007-07-13 22:13:22 +0000164 T = T.getCanonicalType();
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000165 uint64_t Size;
166 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000167 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000168 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000169 case Type::FunctionNoProto:
170 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000171 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000172 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000173 case Type::VariableArray:
174 assert(0 && "VLAs not implemented yet!");
175 case Type::ConstantArray: {
176 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
177
Chris Lattner030d8842007-07-19 22:06:24 +0000178 std::pair<uint64_t, unsigned> EltInfo =
Steve Narofffb22d962007-08-30 01:06:46 +0000179 getTypeInfo(CAT->getElementType(), L);
180 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000181 Align = EltInfo.second;
182 break;
183 }
184 case Type::Vector: {
185 std::pair<uint64_t, unsigned> EltInfo =
186 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
187 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
188 // FIXME: Vector alignment is not the alignment of its elements.
189 Align = EltInfo.second;
190 break;
191 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000192
Chris Lattnera7674d82007-07-13 22:13:22 +0000193 case Type::Builtin: {
194 // FIXME: need to use TargetInfo to derive the target specific sizes. This
195 // implementation will suffice for play with vector support.
Chris Lattner525a0502007-09-22 18:29:59 +0000196 const llvm::fltSemantics *F;
Chris Lattnera7674d82007-07-13 22:13:22 +0000197 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000198 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000199 case BuiltinType::Void:
200 assert(0 && "Incomplete types have no size!");
201 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000202 case BuiltinType::Char_S:
203 case BuiltinType::Char_U:
204 case BuiltinType::UChar:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000205 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000206 case BuiltinType::UShort:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000207 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000208 case BuiltinType::UInt:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000209 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000210 case BuiltinType::ULong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000211 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000212 case BuiltinType::ULongLong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000213 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
Chris Lattner525a0502007-09-22 18:29:59 +0000214 case BuiltinType::Float: Target.getFloatInfo(Size, Align, F, L); break;
215 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, F, L);break;
216 case BuiltinType::LongDouble:Target.getLongDoubleInfo(Size,Align,F,L);break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000217 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000218 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000219 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000220 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000221 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000222 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000223 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
224 // FIXME: This is wrong for struct layout!
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000225 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000226
227 case Type::Complex: {
228 // Complex types have the same alignment as their elements, but twice the
229 // size.
230 std::pair<uint64_t, unsigned> EltInfo =
231 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
232 Size = EltInfo.first*2;
233 Align = EltInfo.second;
234 break;
235 }
236 case Type::Tagged:
Chris Lattner6cd862c2007-08-27 17:38:00 +0000237 TagType *TT = cast<TagType>(T);
238 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
239 const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L);
240 Size = Layout.getSize();
241 Align = Layout.getAlignment();
242 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattnere00b18c2007-08-28 18:24:31 +0000243 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000244 } else {
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000245 assert(0 && "Unimplemented type sizes!");
Chris Lattner6cd862c2007-08-27 17:38:00 +0000246 }
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000247 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000248 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000249
Chris Lattner464175b2007-07-18 17:52:12 +0000250 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000251 return std::make_pair(Size, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000252}
253
Chris Lattner464175b2007-07-18 17:52:12 +0000254/// getRecordLayout - Get or compute information about the layout of the
255/// specified record (struct/union/class), which indicates its size and field
256/// position information.
257const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D,
258 SourceLocation L) {
259 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
260
261 // Look up this layout, if already laid out, return what we have.
262 const RecordLayout *&Entry = RecordLayoutInfo[D];
263 if (Entry) return *Entry;
264
265 // Allocate and assign into RecordLayoutInfo here. The "Entry" reference can
266 // be invalidated (dangle) if the RecordLayoutInfo hashtable is inserted into.
267 RecordLayout *NewEntry = new RecordLayout();
268 Entry = NewEntry;
269
270 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
271 uint64_t RecordSize = 0;
272 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
273
274 if (D->getKind() != Decl::Union) {
275 // Layout each field, for now, just sequentially, respecting alignment. In
276 // the future, this will need to be tweakable by targets.
277 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
278 const FieldDecl *FD = D->getMember(i);
279 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
280 uint64_t FieldSize = FieldInfo.first;
281 unsigned FieldAlign = FieldInfo.second;
282
283 // Round up the current record size to the field's alignment boundary.
284 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
285
286 // Place this field at the current location.
287 FieldOffsets[i] = RecordSize;
288
289 // Reserve space for this field.
290 RecordSize += FieldSize;
291
292 // Remember max struct/class alignment.
293 RecordAlign = std::max(RecordAlign, FieldAlign);
294 }
295
296 // Finally, round the size of the total struct up to the alignment of the
297 // struct itself.
298 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
299 } else {
300 // Union layout just puts each member at the start of the record.
301 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
302 const FieldDecl *FD = D->getMember(i);
303 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
304 uint64_t FieldSize = FieldInfo.first;
305 unsigned FieldAlign = FieldInfo.second;
306
307 // Round up the current record size to the field's alignment boundary.
308 RecordSize = std::max(RecordSize, FieldSize);
309
310 // Place this field at the start of the record.
311 FieldOffsets[i] = 0;
312
313 // Remember max struct/class alignment.
314 RecordAlign = std::max(RecordAlign, FieldAlign);
315 }
316 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000317
318 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
319 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000320}
321
Chris Lattnera7674d82007-07-13 22:13:22 +0000322//===----------------------------------------------------------------------===//
323// Type creation/memoization methods
324//===----------------------------------------------------------------------===//
325
326
Reid Spencer5f016e22007-07-11 17:01:13 +0000327/// getComplexType - Return the uniqued reference to the type for a complex
328/// number with the specified element type.
329QualType ASTContext::getComplexType(QualType T) {
330 // Unique pointers, to guarantee there is only one pointer of a particular
331 // structure.
332 llvm::FoldingSetNodeID ID;
333 ComplexType::Profile(ID, T);
334
335 void *InsertPos = 0;
336 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
337 return QualType(CT, 0);
338
339 // If the pointee type isn't canonical, this won't be a canonical type either,
340 // so fill in the canonical type field.
341 QualType Canonical;
342 if (!T->isCanonical()) {
343 Canonical = getComplexType(T.getCanonicalType());
344
345 // Get the new insert position for the node we care about.
346 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
347 assert(NewIP == 0 && "Shouldn't be in the map!");
348 }
349 ComplexType *New = new ComplexType(T, Canonical);
350 Types.push_back(New);
351 ComplexTypes.InsertNode(New, InsertPos);
352 return QualType(New, 0);
353}
354
355
356/// getPointerType - Return the uniqued reference to the type for a pointer to
357/// the specified type.
358QualType ASTContext::getPointerType(QualType T) {
359 // Unique pointers, to guarantee there is only one pointer of a particular
360 // structure.
361 llvm::FoldingSetNodeID ID;
362 PointerType::Profile(ID, T);
363
364 void *InsertPos = 0;
365 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
366 return QualType(PT, 0);
367
368 // If the pointee type isn't canonical, this won't be a canonical type either,
369 // so fill in the canonical type field.
370 QualType Canonical;
371 if (!T->isCanonical()) {
372 Canonical = getPointerType(T.getCanonicalType());
373
374 // Get the new insert position for the node we care about.
375 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
376 assert(NewIP == 0 && "Shouldn't be in the map!");
377 }
378 PointerType *New = new PointerType(T, Canonical);
379 Types.push_back(New);
380 PointerTypes.InsertNode(New, InsertPos);
381 return QualType(New, 0);
382}
383
384/// getReferenceType - Return the uniqued reference to the type for a reference
385/// to the specified type.
386QualType ASTContext::getReferenceType(QualType T) {
387 // Unique pointers, to guarantee there is only one pointer of a particular
388 // structure.
389 llvm::FoldingSetNodeID ID;
390 ReferenceType::Profile(ID, T);
391
392 void *InsertPos = 0;
393 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
394 return QualType(RT, 0);
395
396 // If the referencee type isn't canonical, this won't be a canonical type
397 // either, so fill in the canonical type field.
398 QualType Canonical;
399 if (!T->isCanonical()) {
400 Canonical = getReferenceType(T.getCanonicalType());
401
402 // Get the new insert position for the node we care about.
403 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
404 assert(NewIP == 0 && "Shouldn't be in the map!");
405 }
406
407 ReferenceType *New = new ReferenceType(T, Canonical);
408 Types.push_back(New);
409 ReferenceTypes.InsertNode(New, InsertPos);
410 return QualType(New, 0);
411}
412
Steve Narofffb22d962007-08-30 01:06:46 +0000413/// getConstantArrayType - Return the unique reference to the type for an
414/// array of the specified element type.
415QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000416 const llvm::APInt &ArySize,
417 ArrayType::ArraySizeModifier ASM,
418 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000419 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000420 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000421
422 void *InsertPos = 0;
Steve Narofffb22d962007-08-30 01:06:46 +0000423 if (ConstantArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 return QualType(ATP, 0);
425
426 // If the element type isn't canonical, this won't be a canonical type either,
427 // so fill in the canonical type field.
428 QualType Canonical;
429 if (!EltTy->isCanonical()) {
Steve Naroffc9406122007-08-30 18:10:14 +0000430 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
431 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 // Get the new insert position for the node we care about.
Steve Narofffb22d962007-08-30 01:06:46 +0000433 ConstantArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 assert(NewIP == 0 && "Shouldn't be in the map!");
435 }
436
Steve Naroffc9406122007-08-30 18:10:14 +0000437 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
438 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 ArrayTypes.InsertNode(New, InsertPos);
440 Types.push_back(New);
441 return QualType(New, 0);
442}
443
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000444/// getVariableArrayType - Returns a non-unique reference to the type for a
445/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000446QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
447 ArrayType::ArraySizeModifier ASM,
448 unsigned EltTypeQuals) {
449 // Since we don't unique expressions, it isn't possible to unique VLA's.
450 ArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
451 ASM, EltTypeQuals);
452 Types.push_back(New);
453 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000454}
455
Steve Naroff73322922007-07-18 18:00:27 +0000456/// getVectorType - Return the unique reference to a vector type of
457/// the specified element type and size. VectorType must be a built-in type.
458QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000459 BuiltinType *baseType;
460
461 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000462 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000463
464 // Check if we've already instantiated a vector of this type.
465 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000466 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 void *InsertPos = 0;
468 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
469 return QualType(VTP, 0);
470
471 // If the element type isn't canonical, this won't be a canonical type either,
472 // so fill in the canonical type field.
473 QualType Canonical;
474 if (!vecType->isCanonical()) {
Steve Naroff73322922007-07-18 18:00:27 +0000475 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000476
477 // Get the new insert position for the node we care about.
478 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
479 assert(NewIP == 0 && "Shouldn't be in the map!");
480 }
481 VectorType *New = new VectorType(vecType, NumElts, Canonical);
482 VectorTypes.InsertNode(New, InsertPos);
483 Types.push_back(New);
484 return QualType(New, 0);
485}
486
Steve Naroff73322922007-07-18 18:00:27 +0000487/// getOCUVectorType - Return the unique reference to an OCU vector type of
488/// the specified element type and size. VectorType must be a built-in type.
489QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
490 BuiltinType *baseType;
491
492 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
493 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
494
495 // Check if we've already instantiated a vector of this type.
496 llvm::FoldingSetNodeID ID;
497 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
498 void *InsertPos = 0;
499 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
500 return QualType(VTP, 0);
501
502 // If the element type isn't canonical, this won't be a canonical type either,
503 // so fill in the canonical type field.
504 QualType Canonical;
505 if (!vecType->isCanonical()) {
506 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
507
508 // Get the new insert position for the node we care about.
509 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
510 assert(NewIP == 0 && "Shouldn't be in the map!");
511 }
512 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
513 VectorTypes.InsertNode(New, InsertPos);
514 Types.push_back(New);
515 return QualType(New, 0);
516}
517
Reid Spencer5f016e22007-07-11 17:01:13 +0000518/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
519///
520QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
521 // Unique functions, to guarantee there is only one function of a particular
522 // structure.
523 llvm::FoldingSetNodeID ID;
524 FunctionTypeNoProto::Profile(ID, ResultTy);
525
526 void *InsertPos = 0;
527 if (FunctionTypeNoProto *FT =
528 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
529 return QualType(FT, 0);
530
531 QualType Canonical;
532 if (!ResultTy->isCanonical()) {
533 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
534
535 // Get the new insert position for the node we care about.
536 FunctionTypeNoProto *NewIP =
537 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
538 assert(NewIP == 0 && "Shouldn't be in the map!");
539 }
540
541 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
542 Types.push_back(New);
543 FunctionTypeProtos.InsertNode(New, InsertPos);
544 return QualType(New, 0);
545}
546
547/// getFunctionType - Return a normal function type with a typed argument
548/// list. isVariadic indicates whether the argument list includes '...'.
549QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
550 unsigned NumArgs, bool isVariadic) {
551 // Unique functions, to guarantee there is only one function of a particular
552 // structure.
553 llvm::FoldingSetNodeID ID;
554 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
555
556 void *InsertPos = 0;
557 if (FunctionTypeProto *FTP =
558 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
559 return QualType(FTP, 0);
560
561 // Determine whether the type being created is already canonical or not.
562 bool isCanonical = ResultTy->isCanonical();
563 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
564 if (!ArgArray[i]->isCanonical())
565 isCanonical = false;
566
567 // If this type isn't canonical, get the canonical version of it.
568 QualType Canonical;
569 if (!isCanonical) {
570 llvm::SmallVector<QualType, 16> CanonicalArgs;
571 CanonicalArgs.reserve(NumArgs);
572 for (unsigned i = 0; i != NumArgs; ++i)
573 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
574
575 Canonical = getFunctionType(ResultTy.getCanonicalType(),
576 &CanonicalArgs[0], NumArgs,
577 isVariadic);
578
579 // Get the new insert position for the node we care about.
580 FunctionTypeProto *NewIP =
581 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
582 assert(NewIP == 0 && "Shouldn't be in the map!");
583 }
584
585 // FunctionTypeProto objects are not allocated with new because they have a
586 // variable size array (for parameter types) at the end of them.
587 FunctionTypeProto *FTP =
588 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000589 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000590 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
591 Canonical);
592 Types.push_back(FTP);
593 FunctionTypeProtos.InsertNode(FTP, InsertPos);
594 return QualType(FTP, 0);
595}
596
597/// getTypedefType - Return the unique reference to the type for the
598/// specified typename decl.
599QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
600 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
601
602 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
603 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
604 Types.push_back(Decl->TypeForDecl);
605 return QualType(Decl->TypeForDecl, 0);
606}
607
Steve Naroff3536b442007-09-06 21:24:23 +0000608/// getObjcInterfaceType - Return the unique reference to the type for the
609/// specified ObjC interface decl.
610QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) {
611 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
612
613 Decl->TypeForDecl = new ObjcInterfaceType(Decl);
614 Types.push_back(Decl->TypeForDecl);
615 return QualType(Decl->TypeForDecl, 0);
616}
617
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000618/// getObjcQualifiedInterfaceType - Return a
619/// ObjcQualifiedInterfaceType type for the given interface decl and
620/// the conforming protocol list.
621QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl,
622 ObjcProtocolDecl **Protocols, unsigned NumProtocols) {
623 ObjcInterfaceType *IType =
624 cast<ObjcInterfaceType>(getObjcInterfaceType(Decl));
625
626 llvm::FoldingSetNodeID ID;
627 ObjcQualifiedInterfaceType::Profile(ID, IType, Protocols, NumProtocols);
628
629 void *InsertPos = 0;
630 if (ObjcQualifiedInterfaceType *QT =
631 ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
632 return QualType(QT, 0);
633
634 // No Match;
Chris Lattner00bb2832007-10-11 03:36:41 +0000635 ObjcQualifiedInterfaceType *QType =
636 new ObjcQualifiedInterfaceType(IType, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000637 Types.push_back(QType);
638 ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
639 return QualType(QType, 0);
640}
641
Steve Naroff9752f252007-08-01 18:02:17 +0000642/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
643/// TypeOfExpr AST's (since expression's are never shared). For example,
644/// multiple declarations that refer to "typeof(x)" all contain different
645/// DeclRefExpr's. This doesn't effect the type checker, since it operates
646/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000647QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffd1861fd2007-07-31 12:34:36 +0000648 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000649 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
650 Types.push_back(toe);
651 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000652}
653
Steve Naroff9752f252007-08-01 18:02:17 +0000654/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
655/// TypeOfType AST's. The only motivation to unique these nodes would be
656/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
657/// an issue. This doesn't effect the type checker, since it operates
658/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000659QualType ASTContext::getTypeOfType(QualType tofType) {
660 QualType Canonical = tofType.getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000661 TypeOfType *tot = new TypeOfType(tofType, Canonical);
662 Types.push_back(tot);
663 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000664}
665
Reid Spencer5f016e22007-07-11 17:01:13 +0000666/// getTagDeclType - Return the unique reference to the type for the
667/// specified TagDecl (struct/union/class/enum) decl.
668QualType ASTContext::getTagDeclType(TagDecl *Decl) {
669 // The decl stores the type cache.
670 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
671
672 Decl->TypeForDecl = new TagType(Decl, QualType());
673 Types.push_back(Decl->TypeForDecl);
674 return QualType(Decl->TypeForDecl, 0);
675}
676
677/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
678/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
679/// needs to agree with the definition in <stddef.h>.
680QualType ASTContext::getSizeType() const {
681 // On Darwin, size_t is defined as a "long unsigned int".
682 // FIXME: should derive from "Target".
683 return UnsignedLongTy;
684}
685
Chris Lattner8b9023b2007-07-13 03:05:23 +0000686/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
687/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
688QualType ASTContext::getPointerDiffType() const {
689 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
690 // FIXME: should derive from "Target".
691 return IntTy;
692}
693
Reid Spencer5f016e22007-07-11 17:01:13 +0000694/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
695/// routine will assert if passed a built-in type that isn't an integer or enum.
696static int getIntegerRank(QualType t) {
697 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
698 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
699 return 4;
700 }
701
702 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
703 switch (BT->getKind()) {
704 default:
705 assert(0 && "getIntegerRank(): not a built-in integer");
706 case BuiltinType::Bool:
707 return 1;
708 case BuiltinType::Char_S:
709 case BuiltinType::Char_U:
710 case BuiltinType::SChar:
711 case BuiltinType::UChar:
712 return 2;
713 case BuiltinType::Short:
714 case BuiltinType::UShort:
715 return 3;
716 case BuiltinType::Int:
717 case BuiltinType::UInt:
718 return 4;
719 case BuiltinType::Long:
720 case BuiltinType::ULong:
721 return 5;
722 case BuiltinType::LongLong:
723 case BuiltinType::ULongLong:
724 return 6;
725 }
726}
727
728/// getFloatingRank - Return a relative rank for floating point types.
729/// This routine will assert if passed a built-in type that isn't a float.
730static int getFloatingRank(QualType T) {
731 T = T.getCanonicalType();
732 if (ComplexType *CT = dyn_cast<ComplexType>(T))
733 return getFloatingRank(CT->getElementType());
734
735 switch (cast<BuiltinType>(T)->getKind()) {
736 default: assert(0 && "getFloatingPointRank(): not a floating type");
737 case BuiltinType::Float: return FloatRank;
738 case BuiltinType::Double: return DoubleRank;
739 case BuiltinType::LongDouble: return LongDoubleRank;
740 }
741}
742
Steve Naroff716c7302007-08-27 01:41:48 +0000743/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
744/// point or a complex type (based on typeDomain/typeSize).
745/// 'typeDomain' is a real floating point or complex type.
746/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +0000747QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
748 QualType typeSize, QualType typeDomain) const {
749 if (typeDomain->isComplexType()) {
750 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000751 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000752 case FloatRank: return FloatComplexTy;
753 case DoubleRank: return DoubleComplexTy;
754 case LongDoubleRank: return LongDoubleComplexTy;
755 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000756 }
Steve Narofff1448a02007-08-27 01:27:54 +0000757 if (typeDomain->isRealFloatingType()) {
758 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000759 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000760 case FloatRank: return FloatTy;
761 case DoubleRank: return DoubleTy;
762 case LongDoubleRank: return LongDoubleTy;
763 }
764 }
765 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000766 //an invalid return value, but the assert
767 //will ensure that this code is never reached.
768 return VoidTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000769}
770
Steve Narofffb0d4962007-08-27 15:30:22 +0000771/// compareFloatingType - Handles 3 different combos:
772/// float/float, float/complex, complex/complex.
773/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
774int ASTContext::compareFloatingType(QualType lt, QualType rt) {
775 if (getFloatingRank(lt) == getFloatingRank(rt))
776 return 0;
777 if (getFloatingRank(lt) > getFloatingRank(rt))
778 return 1;
779 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000780}
781
782// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
783// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
784QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
785 if (lhs == rhs) return lhs;
786
787 bool t1Unsigned = lhs->isUnsignedIntegerType();
788 bool t2Unsigned = rhs->isUnsignedIntegerType();
789
790 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
791 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
792
793 // We have two integer types with differing signs
794 QualType unsignedType = t1Unsigned ? lhs : rhs;
795 QualType signedType = t1Unsigned ? rhs : lhs;
796
797 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
798 return unsignedType;
799 else {
800 // FIXME: Need to check if the signed type can represent all values of the
801 // unsigned type. If it can, then the result is the signed type.
802 // If it can't, then the result is the unsigned version of the signed type.
803 // Should probably add a helper that returns a signed integer type from
804 // an unsigned (and vice versa). C99 6.3.1.8.
805 return signedType;
806 }
807}
Anders Carlsson71993dd2007-08-17 05:31:46 +0000808
809// getCFConstantStringType - Return the type used for constant CFStrings.
810QualType ASTContext::getCFConstantStringType() {
811 if (!CFConstantStringTypeDecl) {
812 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
813 &Idents.get("__builtin_CFString"),
814 0);
815
816 QualType FieldTypes[4];
817
818 // const int *isa;
819 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
820 // int flags;
821 FieldTypes[1] = IntTy;
822 // const char *str;
823 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
824 // long length;
825 FieldTypes[3] = LongTy;
826 // Create fields
827 FieldDecl *FieldDecls[4];
828
829 for (unsigned i = 0; i < 4; ++i)
Steve Narofff38661e2007-09-14 02:20:46 +0000830 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +0000831
832 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
833 }
834
835 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +0000836}
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000837
838void ASTContext::setBuiltinVaListType(QualType T)
839{
840 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
841
842 BuiltinVaListType = T;
843}
844
Steve Naroff7e219e42007-10-15 14:41:52 +0000845void ASTContext::setObjcIdType(TypedefDecl *TD)
846{
847 assert(ObjcIdType.isNull() && "'id' type already set!");
848
849 ObjcIdType = getTypedefType(TD);
850
851 // typedef struct objc_object *id;
852 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
853 assert(ptr && "'id' incorrectly typed");
854 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
855 assert(rec && "'id' incorrectly typed");
856 IdStructType = rec;
857}
858
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000859void ASTContext::setObjcSelType(TypedefDecl *TD)
860{
861 assert(ObjcSelType.isNull() && "'SEL' type already set!");
862
863 ObjcSelType = getTypedefType(TD);
864
865 // typedef struct objc_selector *SEL;
866 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
867 assert(ptr && "'SEL' incorrectly typed");
868 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
869 assert(rec && "'SEL' incorrectly typed");
870 SelStructType = rec;
871}
872
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000873void ASTContext::setObjcProtoType(TypedefDecl *TD)
874{
875 assert(ObjcProtoType.isNull() && "'Protocol' type already set!");
876
877 // typedef struct Protocol Protocol;
878 ObjcProtoType = TD->getUnderlyingType();
879 // Protocol * type
880 ObjcProtoType = getPointerType(ObjcProtoType);
881 ProtoStructType = TD->getUnderlyingType()->getAsStructureType();
882}
883
Steve Naroff21988912007-10-15 23:35:17 +0000884void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) {
885 assert(ObjcConstantStringType.isNull() &&
886 "'NSConstantString' type already set!");
887
888 ObjcConstantStringType = getObjcInterfaceType(Decl);
889}
890
Steve Naroffec0550f2007-10-15 20:41:53 +0000891bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
892 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
893 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
894
895 return lBuiltin->getKind() == rBuiltin->getKind();
896}
897
898
899bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
900 if (lhs->isObjcInterfaceType() && isObjcIdType(rhs))
901 return true;
902 else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType())
903 return true;
904 return false;
905}
906
907bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
908 return true; // FIXME: IMPLEMENT.
909}
910
911// C99 6.2.7p1: If both are complete types, then the following additional
912// requirements apply...FIXME (handle compatibility across source files).
913bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
914 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
915 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
916
917 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
918 if (ldecl->getIdentifier() == rdecl->getIdentifier())
919 return true;
920 }
921 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
922 if (ldecl->getIdentifier() == rdecl->getIdentifier())
923 return true;
924 }
925 return false;
926}
927
928bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
929 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
930 // identically qualified and both shall be pointers to compatible types.
931 if (lhs.getQualifiers() != rhs.getQualifiers())
932 return false;
933
934 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
935 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
936
937 return typesAreCompatible(ltype, rtype);
938}
939
940// C++ 5.17p6: When the left opperand of an assignment operator denotes a
941// reference to T, the operation assigns to the object of type T denoted by the
942// reference.
943bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
944 QualType ltype = lhs;
945
946 if (lhs->isReferenceType())
947 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
948
949 QualType rtype = rhs;
950
951 if (rhs->isReferenceType())
952 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
953
954 return typesAreCompatible(ltype, rtype);
955}
956
957bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
958 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
959 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
960 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
961 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
962
963 // first check the return types (common between C99 and K&R).
964 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
965 return false;
966
967 if (lproto && rproto) { // two C99 style function prototypes
968 unsigned lproto_nargs = lproto->getNumArgs();
969 unsigned rproto_nargs = rproto->getNumArgs();
970
971 if (lproto_nargs != rproto_nargs)
972 return false;
973
974 // both prototypes have the same number of arguments.
975 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
976 (rproto->isVariadic() && !lproto->isVariadic()))
977 return false;
978
979 // The use of ellipsis agree...now check the argument types.
980 for (unsigned i = 0; i < lproto_nargs; i++)
981 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
982 return false;
983 return true;
984 }
985 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
986 return true;
987
988 // we have a mixture of K&R style with C99 prototypes
989 const FunctionTypeProto *proto = lproto ? lproto : rproto;
990
991 if (proto->isVariadic())
992 return false;
993
994 // FIXME: Each parameter type T in the prototype must be compatible with the
995 // type resulting from applying the usual argument conversions to T.
996 return true;
997}
998
999bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
1000 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
1001 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
1002
1003 if (!typesAreCompatible(ltype, rtype))
1004 return false;
1005
1006 // FIXME: If both types specify constant sizes, then the sizes must also be
1007 // the same. Even if the sizes are the same, GCC produces an error.
1008 return true;
1009}
1010
1011/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1012/// both shall have the identically qualified version of a compatible type.
1013/// C99 6.2.7p1: Two types have compatible types if their types are the
1014/// same. See 6.7.[2,3,5] for additional rules.
1015bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
1016 QualType lcanon = lhs.getCanonicalType();
1017 QualType rcanon = rhs.getCanonicalType();
1018
1019 // If two types are identical, they are are compatible
1020 if (lcanon == rcanon)
1021 return true;
1022
1023 // If the canonical type classes don't match, they can't be compatible
1024 if (lcanon->getTypeClass() != rcanon->getTypeClass()) {
1025 // For Objective-C, it is possible for two types to be compatible
1026 // when their classes don't match (when dealing with "id"). If either type
1027 // is an interface, we defer to objcTypesAreCompatible().
1028 if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType())
1029 return objcTypesAreCompatible(lcanon, rcanon);
1030 return false;
1031 }
1032 switch (lcanon->getTypeClass()) {
1033 case Type::Pointer:
1034 return pointerTypesAreCompatible(lcanon, rcanon);
1035 case Type::Reference:
1036 return referenceTypesAreCompatible(lcanon, rcanon);
1037 case Type::ConstantArray:
1038 case Type::VariableArray:
1039 return arrayTypesAreCompatible(lcanon, rcanon);
1040 case Type::FunctionNoProto:
1041 case Type::FunctionProto:
1042 return functionTypesAreCompatible(lcanon, rcanon);
1043 case Type::Tagged: // handle structures, unions
1044 return tagTypesAreCompatible(lcanon, rcanon);
1045 case Type::Builtin:
1046 return builtinTypesAreCompatible(lcanon, rcanon);
1047 case Type::ObjcInterface:
1048 return interfaceTypesAreCompatible(lcanon, rcanon);
1049 default:
1050 assert(0 && "unexpected type");
1051 }
1052 return true; // should never get here...
1053}