blob: 61e2dbea069e898afa3f1b6157e92178287cc7e3 [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"
Anders Carlsson85f9bce2007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
20
Reid Spencer5f016e22007-07-11 17:01:13 +000021using namespace clang;
22
23enum FloatingRank {
24 FloatRank, DoubleRank, LongDoubleRank
25};
26
27ASTContext::~ASTContext() {
28 // Deallocate all the types.
29 while (!Types.empty()) {
30 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
31 // Destroy the object, but don't call delete. These are malloc'd.
32 FT->~FunctionTypeProto();
33 free(FT);
34 } else {
35 delete Types.back();
36 }
37 Types.pop_back();
38 }
39}
40
41void ASTContext::PrintStats() const {
42 fprintf(stderr, "*** AST Context Stats:\n");
43 fprintf(stderr, " %d types total.\n", (int)Types.size());
44 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Chris Lattner6d87fc62007-07-18 05:50:59 +000045 unsigned NumVector = 0, NumComplex = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000046 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
47
48 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Steve Naroff3f128ad2007-09-17 14:16:13 +000049 unsigned NumObjcInterfaces = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000050
51 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
52 Type *T = Types[i];
53 if (isa<BuiltinType>(T))
54 ++NumBuiltin;
55 else if (isa<PointerType>(T))
56 ++NumPointer;
57 else if (isa<ReferenceType>(T))
58 ++NumReference;
Chris Lattner6d87fc62007-07-18 05:50:59 +000059 else if (isa<ComplexType>(T))
60 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +000061 else if (isa<ArrayType>(T))
62 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +000063 else if (isa<VectorType>(T))
64 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +000065 else if (isa<FunctionTypeNoProto>(T))
66 ++NumFunctionNP;
67 else if (isa<FunctionTypeProto>(T))
68 ++NumFunctionP;
69 else if (isa<TypedefType>(T))
70 ++NumTypeName;
71 else if (TagType *TT = dyn_cast<TagType>(T)) {
72 ++NumTagged;
73 switch (TT->getDecl()->getKind()) {
74 default: assert(0 && "Unknown tagged type!");
75 case Decl::Struct: ++NumTagStruct; break;
76 case Decl::Union: ++NumTagUnion; break;
77 case Decl::Class: ++NumTagClass; break;
78 case Decl::Enum: ++NumTagEnum; break;
79 }
Steve Naroff3f128ad2007-09-17 14:16:13 +000080 } else if (isa<ObjcInterfaceType>(T))
81 ++NumObjcInterfaces;
82 else {
Reid Spencer5f016e22007-07-11 17:01:13 +000083 assert(0 && "Unknown type!");
84 }
85 }
86
87 fprintf(stderr, " %d builtin types\n", NumBuiltin);
88 fprintf(stderr, " %d pointer types\n", NumPointer);
89 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner6d87fc62007-07-18 05:50:59 +000090 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +000091 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +000092 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +000093 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
94 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
95 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
96 fprintf(stderr, " %d tagged types\n", NumTagged);
97 fprintf(stderr, " %d struct types\n", NumTagStruct);
98 fprintf(stderr, " %d union types\n", NumTagUnion);
99 fprintf(stderr, " %d class types\n", NumTagClass);
100 fprintf(stderr, " %d enum types\n", NumTagEnum);
Steve Naroff3f128ad2007-09-17 14:16:13 +0000101 fprintf(stderr, " %d interface types\n", NumObjcInterfaces);
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
103 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +0000104 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +0000105 NumFunctionP*sizeof(FunctionTypeProto)+
106 NumFunctionNP*sizeof(FunctionTypeNoProto)+
107 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
108}
109
110
111void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
112 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
113}
114
Reid Spencer5f016e22007-07-11 17:01:13 +0000115void ASTContext::InitBuiltinTypes() {
116 assert(VoidTy.isNull() && "Context reinitialized?");
117
118 // C99 6.2.5p19.
119 InitBuiltinType(VoidTy, BuiltinType::Void);
120
121 // C99 6.2.5p2.
122 InitBuiltinType(BoolTy, BuiltinType::Bool);
123 // C99 6.2.5p3.
124 if (Target.isCharSigned(SourceLocation()))
125 InitBuiltinType(CharTy, BuiltinType::Char_S);
126 else
127 InitBuiltinType(CharTy, BuiltinType::Char_U);
128 // C99 6.2.5p4.
129 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
130 InitBuiltinType(ShortTy, BuiltinType::Short);
131 InitBuiltinType(IntTy, BuiltinType::Int);
132 InitBuiltinType(LongTy, BuiltinType::Long);
133 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
134
135 // C99 6.2.5p6.
136 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
137 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
138 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
139 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
140 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
141
142 // C99 6.2.5p10.
143 InitBuiltinType(FloatTy, BuiltinType::Float);
144 InitBuiltinType(DoubleTy, BuiltinType::Double);
145 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
146
147 // C99 6.2.5p11.
148 FloatComplexTy = getComplexType(FloatTy);
149 DoubleComplexTy = getComplexType(DoubleTy);
150 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff7e219e42007-10-15 14:41:52 +0000151
152 BuiltinVaListType = QualType();
153 ObjcIdType = QualType();
154 IdStructType = 0;
Steve Naroff21988912007-10-15 23:35:17 +0000155 ObjcConstantStringType = QualType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000156}
157
Chris Lattner464175b2007-07-18 17:52:12 +0000158//===----------------------------------------------------------------------===//
159// Type Sizing and Analysis
160//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000161
162/// getTypeSize - Return the size of the specified type, in bits. This method
163/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000164std::pair<uint64_t, unsigned>
165ASTContext::getTypeInfo(QualType T, SourceLocation L) {
Chris Lattnera7674d82007-07-13 22:13:22 +0000166 T = T.getCanonicalType();
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000167 uint64_t Size;
168 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000169 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000170 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000171 case Type::FunctionNoProto:
172 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000173 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000174 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000175 case Type::VariableArray:
176 assert(0 && "VLAs not implemented yet!");
177 case Type::ConstantArray: {
178 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
179
Chris Lattner030d8842007-07-19 22:06:24 +0000180 std::pair<uint64_t, unsigned> EltInfo =
Steve Narofffb22d962007-08-30 01:06:46 +0000181 getTypeInfo(CAT->getElementType(), L);
182 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000183 Align = EltInfo.second;
184 break;
185 }
186 case Type::Vector: {
187 std::pair<uint64_t, unsigned> EltInfo =
188 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
189 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
190 // FIXME: Vector alignment is not the alignment of its elements.
191 Align = EltInfo.second;
192 break;
193 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000194
Chris Lattnera7674d82007-07-13 22:13:22 +0000195 case Type::Builtin: {
196 // FIXME: need to use TargetInfo to derive the target specific sizes. This
197 // implementation will suffice for play with vector support.
Chris Lattner525a0502007-09-22 18:29:59 +0000198 const llvm::fltSemantics *F;
Chris Lattnera7674d82007-07-13 22:13:22 +0000199 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000200 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000201 case BuiltinType::Void:
202 assert(0 && "Incomplete types have no size!");
203 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000204 case BuiltinType::Char_S:
205 case BuiltinType::Char_U:
206 case BuiltinType::UChar:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000207 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000208 case BuiltinType::UShort:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000209 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000210 case BuiltinType::UInt:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000211 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000212 case BuiltinType::ULong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000213 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000214 case BuiltinType::ULongLong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000215 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
Chris Lattner525a0502007-09-22 18:29:59 +0000216 case BuiltinType::Float: Target.getFloatInfo(Size, Align, F, L); break;
217 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, F, L);break;
218 case BuiltinType::LongDouble:Target.getLongDoubleInfo(Size,Align,F,L);break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000219 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000220 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000221 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000222 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000223 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000224 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000225 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
226 // FIXME: This is wrong for struct layout!
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000227 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000228
229 case Type::Complex: {
230 // Complex types have the same alignment as their elements, but twice the
231 // size.
232 std::pair<uint64_t, unsigned> EltInfo =
233 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
234 Size = EltInfo.first*2;
235 Align = EltInfo.second;
236 break;
237 }
238 case Type::Tagged:
Chris Lattner6cd862c2007-08-27 17:38:00 +0000239 TagType *TT = cast<TagType>(T);
240 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
241 const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L);
242 Size = Layout.getSize();
243 Align = Layout.getAlignment();
244 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattnere00b18c2007-08-28 18:24:31 +0000245 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000246 } else {
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000247 assert(0 && "Unimplemented type sizes!");
Chris Lattner6cd862c2007-08-27 17:38:00 +0000248 }
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000249 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000250 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000251
Chris Lattner464175b2007-07-18 17:52:12 +0000252 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000253 return std::make_pair(Size, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000254}
255
Chris Lattner464175b2007-07-18 17:52:12 +0000256/// getRecordLayout - Get or compute information about the layout of the
257/// specified record (struct/union/class), which indicates its size and field
258/// position information.
259const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D,
260 SourceLocation L) {
261 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
262
263 // Look up this layout, if already laid out, return what we have.
264 const RecordLayout *&Entry = RecordLayoutInfo[D];
265 if (Entry) return *Entry;
266
267 // Allocate and assign into RecordLayoutInfo here. The "Entry" reference can
268 // be invalidated (dangle) if the RecordLayoutInfo hashtable is inserted into.
269 RecordLayout *NewEntry = new RecordLayout();
270 Entry = NewEntry;
271
272 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
273 uint64_t RecordSize = 0;
274 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
275
276 if (D->getKind() != Decl::Union) {
277 // Layout each field, for now, just sequentially, respecting alignment. In
278 // the future, this will need to be tweakable by targets.
279 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
280 const FieldDecl *FD = D->getMember(i);
281 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
282 uint64_t FieldSize = FieldInfo.first;
283 unsigned FieldAlign = FieldInfo.second;
284
285 // Round up the current record size to the field's alignment boundary.
286 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
287
288 // Place this field at the current location.
289 FieldOffsets[i] = RecordSize;
290
291 // Reserve space for this field.
292 RecordSize += FieldSize;
293
294 // Remember max struct/class alignment.
295 RecordAlign = std::max(RecordAlign, FieldAlign);
296 }
297
298 // Finally, round the size of the total struct up to the alignment of the
299 // struct itself.
300 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
301 } else {
302 // Union layout just puts each member at the start of the record.
303 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
304 const FieldDecl *FD = D->getMember(i);
305 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
306 uint64_t FieldSize = FieldInfo.first;
307 unsigned FieldAlign = FieldInfo.second;
308
309 // Round up the current record size to the field's alignment boundary.
310 RecordSize = std::max(RecordSize, FieldSize);
311
312 // Place this field at the start of the record.
313 FieldOffsets[i] = 0;
314
315 // Remember max struct/class alignment.
316 RecordAlign = std::max(RecordAlign, FieldAlign);
317 }
318 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000319
320 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
321 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000322}
323
Chris Lattnera7674d82007-07-13 22:13:22 +0000324//===----------------------------------------------------------------------===//
325// Type creation/memoization methods
326//===----------------------------------------------------------------------===//
327
328
Reid Spencer5f016e22007-07-11 17:01:13 +0000329/// getComplexType - Return the uniqued reference to the type for a complex
330/// number with the specified element type.
331QualType ASTContext::getComplexType(QualType T) {
332 // Unique pointers, to guarantee there is only one pointer of a particular
333 // structure.
334 llvm::FoldingSetNodeID ID;
335 ComplexType::Profile(ID, T);
336
337 void *InsertPos = 0;
338 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
339 return QualType(CT, 0);
340
341 // If the pointee type isn't canonical, this won't be a canonical type either,
342 // so fill in the canonical type field.
343 QualType Canonical;
344 if (!T->isCanonical()) {
345 Canonical = getComplexType(T.getCanonicalType());
346
347 // Get the new insert position for the node we care about.
348 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
349 assert(NewIP == 0 && "Shouldn't be in the map!");
350 }
351 ComplexType *New = new ComplexType(T, Canonical);
352 Types.push_back(New);
353 ComplexTypes.InsertNode(New, InsertPos);
354 return QualType(New, 0);
355}
356
357
358/// getPointerType - Return the uniqued reference to the type for a pointer to
359/// the specified type.
360QualType ASTContext::getPointerType(QualType T) {
361 // Unique pointers, to guarantee there is only one pointer of a particular
362 // structure.
363 llvm::FoldingSetNodeID ID;
364 PointerType::Profile(ID, T);
365
366 void *InsertPos = 0;
367 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
368 return QualType(PT, 0);
369
370 // If the pointee type isn't canonical, this won't be a canonical type either,
371 // so fill in the canonical type field.
372 QualType Canonical;
373 if (!T->isCanonical()) {
374 Canonical = getPointerType(T.getCanonicalType());
375
376 // Get the new insert position for the node we care about.
377 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
378 assert(NewIP == 0 && "Shouldn't be in the map!");
379 }
380 PointerType *New = new PointerType(T, Canonical);
381 Types.push_back(New);
382 PointerTypes.InsertNode(New, InsertPos);
383 return QualType(New, 0);
384}
385
386/// getReferenceType - Return the uniqued reference to the type for a reference
387/// to the specified type.
388QualType ASTContext::getReferenceType(QualType T) {
389 // Unique pointers, to guarantee there is only one pointer of a particular
390 // structure.
391 llvm::FoldingSetNodeID ID;
392 ReferenceType::Profile(ID, T);
393
394 void *InsertPos = 0;
395 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
396 return QualType(RT, 0);
397
398 // If the referencee type isn't canonical, this won't be a canonical type
399 // either, so fill in the canonical type field.
400 QualType Canonical;
401 if (!T->isCanonical()) {
402 Canonical = getReferenceType(T.getCanonicalType());
403
404 // Get the new insert position for the node we care about.
405 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
406 assert(NewIP == 0 && "Shouldn't be in the map!");
407 }
408
409 ReferenceType *New = new ReferenceType(T, Canonical);
410 Types.push_back(New);
411 ReferenceTypes.InsertNode(New, InsertPos);
412 return QualType(New, 0);
413}
414
Steve Narofffb22d962007-08-30 01:06:46 +0000415/// getConstantArrayType - Return the unique reference to the type for an
416/// array of the specified element type.
417QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000418 const llvm::APInt &ArySize,
419 ArrayType::ArraySizeModifier ASM,
420 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000421 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000422 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000423
424 void *InsertPos = 0;
Steve Narofffb22d962007-08-30 01:06:46 +0000425 if (ConstantArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000426 return QualType(ATP, 0);
427
428 // If the element type isn't canonical, this won't be a canonical type either,
429 // so fill in the canonical type field.
430 QualType Canonical;
431 if (!EltTy->isCanonical()) {
Steve Naroffc9406122007-08-30 18:10:14 +0000432 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
433 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000434 // Get the new insert position for the node we care about.
Steve Narofffb22d962007-08-30 01:06:46 +0000435 ConstantArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000436 assert(NewIP == 0 && "Shouldn't be in the map!");
437 }
438
Steve Naroffc9406122007-08-30 18:10:14 +0000439 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
440 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000441 ArrayTypes.InsertNode(New, InsertPos);
442 Types.push_back(New);
443 return QualType(New, 0);
444}
445
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000446/// getVariableArrayType - Returns a non-unique reference to the type for a
447/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000448QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
449 ArrayType::ArraySizeModifier ASM,
450 unsigned EltTypeQuals) {
451 // Since we don't unique expressions, it isn't possible to unique VLA's.
452 ArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
453 ASM, EltTypeQuals);
454 Types.push_back(New);
455 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000456}
457
Steve Naroff73322922007-07-18 18:00:27 +0000458/// getVectorType - Return the unique reference to a vector type of
459/// the specified element type and size. VectorType must be a built-in type.
460QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 BuiltinType *baseType;
462
463 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000464 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000465
466 // Check if we've already instantiated a vector of this type.
467 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000468 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000469 void *InsertPos = 0;
470 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
471 return QualType(VTP, 0);
472
473 // If the element type isn't canonical, this won't be a canonical type either,
474 // so fill in the canonical type field.
475 QualType Canonical;
476 if (!vecType->isCanonical()) {
Steve Naroff73322922007-07-18 18:00:27 +0000477 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000478
479 // Get the new insert position for the node we care about.
480 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
481 assert(NewIP == 0 && "Shouldn't be in the map!");
482 }
483 VectorType *New = new VectorType(vecType, NumElts, Canonical);
484 VectorTypes.InsertNode(New, InsertPos);
485 Types.push_back(New);
486 return QualType(New, 0);
487}
488
Steve Naroff73322922007-07-18 18:00:27 +0000489/// getOCUVectorType - Return the unique reference to an OCU vector type of
490/// the specified element type and size. VectorType must be a built-in type.
491QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
492 BuiltinType *baseType;
493
494 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
495 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
496
497 // Check if we've already instantiated a vector of this type.
498 llvm::FoldingSetNodeID ID;
499 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
500 void *InsertPos = 0;
501 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
502 return QualType(VTP, 0);
503
504 // If the element type isn't canonical, this won't be a canonical type either,
505 // so fill in the canonical type field.
506 QualType Canonical;
507 if (!vecType->isCanonical()) {
508 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
509
510 // Get the new insert position for the node we care about.
511 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
512 assert(NewIP == 0 && "Shouldn't be in the map!");
513 }
514 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
515 VectorTypes.InsertNode(New, InsertPos);
516 Types.push_back(New);
517 return QualType(New, 0);
518}
519
Reid Spencer5f016e22007-07-11 17:01:13 +0000520/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
521///
522QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
523 // Unique functions, to guarantee there is only one function of a particular
524 // structure.
525 llvm::FoldingSetNodeID ID;
526 FunctionTypeNoProto::Profile(ID, ResultTy);
527
528 void *InsertPos = 0;
529 if (FunctionTypeNoProto *FT =
530 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
531 return QualType(FT, 0);
532
533 QualType Canonical;
534 if (!ResultTy->isCanonical()) {
535 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
536
537 // Get the new insert position for the node we care about.
538 FunctionTypeNoProto *NewIP =
539 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
540 assert(NewIP == 0 && "Shouldn't be in the map!");
541 }
542
543 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
544 Types.push_back(New);
545 FunctionTypeProtos.InsertNode(New, InsertPos);
546 return QualType(New, 0);
547}
548
549/// getFunctionType - Return a normal function type with a typed argument
550/// list. isVariadic indicates whether the argument list includes '...'.
551QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
552 unsigned NumArgs, bool isVariadic) {
553 // Unique functions, to guarantee there is only one function of a particular
554 // structure.
555 llvm::FoldingSetNodeID ID;
556 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
557
558 void *InsertPos = 0;
559 if (FunctionTypeProto *FTP =
560 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
561 return QualType(FTP, 0);
562
563 // Determine whether the type being created is already canonical or not.
564 bool isCanonical = ResultTy->isCanonical();
565 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
566 if (!ArgArray[i]->isCanonical())
567 isCanonical = false;
568
569 // If this type isn't canonical, get the canonical version of it.
570 QualType Canonical;
571 if (!isCanonical) {
572 llvm::SmallVector<QualType, 16> CanonicalArgs;
573 CanonicalArgs.reserve(NumArgs);
574 for (unsigned i = 0; i != NumArgs; ++i)
575 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
576
577 Canonical = getFunctionType(ResultTy.getCanonicalType(),
578 &CanonicalArgs[0], NumArgs,
579 isVariadic);
580
581 // Get the new insert position for the node we care about.
582 FunctionTypeProto *NewIP =
583 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
584 assert(NewIP == 0 && "Shouldn't be in the map!");
585 }
586
587 // FunctionTypeProto objects are not allocated with new because they have a
588 // variable size array (for parameter types) at the end of them.
589 FunctionTypeProto *FTP =
590 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000591 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000592 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
593 Canonical);
594 Types.push_back(FTP);
595 FunctionTypeProtos.InsertNode(FTP, InsertPos);
596 return QualType(FTP, 0);
597}
598
599/// getTypedefType - Return the unique reference to the type for the
600/// specified typename decl.
601QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
602 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
603
604 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
605 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
606 Types.push_back(Decl->TypeForDecl);
607 return QualType(Decl->TypeForDecl, 0);
608}
609
Steve Naroff3536b442007-09-06 21:24:23 +0000610/// getObjcInterfaceType - Return the unique reference to the type for the
611/// specified ObjC interface decl.
612QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) {
613 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
614
615 Decl->TypeForDecl = new ObjcInterfaceType(Decl);
616 Types.push_back(Decl->TypeForDecl);
617 return QualType(Decl->TypeForDecl, 0);
618}
619
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000620/// getObjcQualifiedInterfaceType - Return a
621/// ObjcQualifiedInterfaceType type for the given interface decl and
622/// the conforming protocol list.
623QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl,
624 ObjcProtocolDecl **Protocols, unsigned NumProtocols) {
625 ObjcInterfaceType *IType =
626 cast<ObjcInterfaceType>(getObjcInterfaceType(Decl));
627
628 llvm::FoldingSetNodeID ID;
629 ObjcQualifiedInterfaceType::Profile(ID, IType, Protocols, NumProtocols);
630
631 void *InsertPos = 0;
632 if (ObjcQualifiedInterfaceType *QT =
633 ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
634 return QualType(QT, 0);
635
636 // No Match;
Chris Lattner00bb2832007-10-11 03:36:41 +0000637 ObjcQualifiedInterfaceType *QType =
638 new ObjcQualifiedInterfaceType(IType, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000639 Types.push_back(QType);
640 ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
641 return QualType(QType, 0);
642}
643
Steve Naroff9752f252007-08-01 18:02:17 +0000644/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
645/// TypeOfExpr AST's (since expression's are never shared). For example,
646/// multiple declarations that refer to "typeof(x)" all contain different
647/// DeclRefExpr's. This doesn't effect the type checker, since it operates
648/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000649QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffd1861fd2007-07-31 12:34:36 +0000650 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000651 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
652 Types.push_back(toe);
653 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000654}
655
Steve Naroff9752f252007-08-01 18:02:17 +0000656/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
657/// TypeOfType AST's. The only motivation to unique these nodes would be
658/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
659/// an issue. This doesn't effect the type checker, since it operates
660/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000661QualType ASTContext::getTypeOfType(QualType tofType) {
662 QualType Canonical = tofType.getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000663 TypeOfType *tot = new TypeOfType(tofType, Canonical);
664 Types.push_back(tot);
665 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000666}
667
Reid Spencer5f016e22007-07-11 17:01:13 +0000668/// getTagDeclType - Return the unique reference to the type for the
669/// specified TagDecl (struct/union/class/enum) decl.
670QualType ASTContext::getTagDeclType(TagDecl *Decl) {
671 // The decl stores the type cache.
672 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
673
674 Decl->TypeForDecl = new TagType(Decl, QualType());
675 Types.push_back(Decl->TypeForDecl);
676 return QualType(Decl->TypeForDecl, 0);
677}
678
679/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
680/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
681/// needs to agree with the definition in <stddef.h>.
682QualType ASTContext::getSizeType() const {
683 // On Darwin, size_t is defined as a "long unsigned int".
684 // FIXME: should derive from "Target".
685 return UnsignedLongTy;
686}
687
Chris Lattner8b9023b2007-07-13 03:05:23 +0000688/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
689/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
690QualType ASTContext::getPointerDiffType() const {
691 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
692 // FIXME: should derive from "Target".
693 return IntTy;
694}
695
Reid Spencer5f016e22007-07-11 17:01:13 +0000696/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
697/// routine will assert if passed a built-in type that isn't an integer or enum.
698static int getIntegerRank(QualType t) {
699 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
700 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
701 return 4;
702 }
703
704 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
705 switch (BT->getKind()) {
706 default:
707 assert(0 && "getIntegerRank(): not a built-in integer");
708 case BuiltinType::Bool:
709 return 1;
710 case BuiltinType::Char_S:
711 case BuiltinType::Char_U:
712 case BuiltinType::SChar:
713 case BuiltinType::UChar:
714 return 2;
715 case BuiltinType::Short:
716 case BuiltinType::UShort:
717 return 3;
718 case BuiltinType::Int:
719 case BuiltinType::UInt:
720 return 4;
721 case BuiltinType::Long:
722 case BuiltinType::ULong:
723 return 5;
724 case BuiltinType::LongLong:
725 case BuiltinType::ULongLong:
726 return 6;
727 }
728}
729
730/// getFloatingRank - Return a relative rank for floating point types.
731/// This routine will assert if passed a built-in type that isn't a float.
732static int getFloatingRank(QualType T) {
733 T = T.getCanonicalType();
734 if (ComplexType *CT = dyn_cast<ComplexType>(T))
735 return getFloatingRank(CT->getElementType());
736
737 switch (cast<BuiltinType>(T)->getKind()) {
738 default: assert(0 && "getFloatingPointRank(): not a floating type");
739 case BuiltinType::Float: return FloatRank;
740 case BuiltinType::Double: return DoubleRank;
741 case BuiltinType::LongDouble: return LongDoubleRank;
742 }
743}
744
Steve Naroff716c7302007-08-27 01:41:48 +0000745/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
746/// point or a complex type (based on typeDomain/typeSize).
747/// 'typeDomain' is a real floating point or complex type.
748/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +0000749QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
750 QualType typeSize, QualType typeDomain) const {
751 if (typeDomain->isComplexType()) {
752 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000753 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000754 case FloatRank: return FloatComplexTy;
755 case DoubleRank: return DoubleComplexTy;
756 case LongDoubleRank: return LongDoubleComplexTy;
757 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 }
Steve Narofff1448a02007-08-27 01:27:54 +0000759 if (typeDomain->isRealFloatingType()) {
760 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000761 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000762 case FloatRank: return FloatTy;
763 case DoubleRank: return DoubleTy;
764 case LongDoubleRank: return LongDoubleTy;
765 }
766 }
767 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000768 //an invalid return value, but the assert
769 //will ensure that this code is never reached.
770 return VoidTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000771}
772
Steve Narofffb0d4962007-08-27 15:30:22 +0000773/// compareFloatingType - Handles 3 different combos:
774/// float/float, float/complex, complex/complex.
775/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
776int ASTContext::compareFloatingType(QualType lt, QualType rt) {
777 if (getFloatingRank(lt) == getFloatingRank(rt))
778 return 0;
779 if (getFloatingRank(lt) > getFloatingRank(rt))
780 return 1;
781 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000782}
783
784// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
785// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
786QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
787 if (lhs == rhs) return lhs;
788
789 bool t1Unsigned = lhs->isUnsignedIntegerType();
790 bool t2Unsigned = rhs->isUnsignedIntegerType();
791
792 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
793 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
794
795 // We have two integer types with differing signs
796 QualType unsignedType = t1Unsigned ? lhs : rhs;
797 QualType signedType = t1Unsigned ? rhs : lhs;
798
799 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
800 return unsignedType;
801 else {
802 // FIXME: Need to check if the signed type can represent all values of the
803 // unsigned type. If it can, then the result is the signed type.
804 // If it can't, then the result is the unsigned version of the signed type.
805 // Should probably add a helper that returns a signed integer type from
806 // an unsigned (and vice versa). C99 6.3.1.8.
807 return signedType;
808 }
809}
Anders Carlsson71993dd2007-08-17 05:31:46 +0000810
811// getCFConstantStringType - Return the type used for constant CFStrings.
812QualType ASTContext::getCFConstantStringType() {
813 if (!CFConstantStringTypeDecl) {
814 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
815 &Idents.get("__builtin_CFString"),
816 0);
817
818 QualType FieldTypes[4];
819
820 // const int *isa;
821 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
822 // int flags;
823 FieldTypes[1] = IntTy;
824 // const char *str;
825 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
826 // long length;
827 FieldTypes[3] = LongTy;
828 // Create fields
829 FieldDecl *FieldDecls[4];
830
831 for (unsigned i = 0; i < 4; ++i)
Steve Narofff38661e2007-09-14 02:20:46 +0000832 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +0000833
834 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
835 }
836
837 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +0000838}
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000839
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000840static bool isTypeTypedefedAsBOOL(QualType T)
841{
842 if (const PointerType *NCPT = T->getAsPointerType())
843 if (const TypedefType *TT = dyn_cast<TypedefType>(NCPT->getPointeeType()))
844 if (!strcmp(TT->getDecl()->getName(), "BOOL"))
845 return true;
846
847 return false;
848}
849
850void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const
851{
852 QualType Ty = T.getCanonicalType();
853
854 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
855 char encoding;
856 switch (BT->getKind()) {
857 case BuiltinType::Void:
858 encoding = 'v';
859 break;
860 case BuiltinType::Bool:
861 encoding = 'B';
862 break;
863 case BuiltinType::Char_U:
864 case BuiltinType::UChar:
865 encoding = 'C';
866 break;
867 case BuiltinType::UShort:
868 encoding = 'S';
869 break;
870 case BuiltinType::UInt:
871 encoding = 'I';
872 break;
873 case BuiltinType::ULong:
874 encoding = 'L';
875 break;
876 case BuiltinType::ULongLong:
877 encoding = 'Q';
878 break;
879 case BuiltinType::Char_S:
880 case BuiltinType::SChar:
881 encoding = 'c';
882 break;
883 case BuiltinType::Short:
884 encoding = 's';
885 break;
886 case BuiltinType::Int:
887 encoding = 'i';
888 break;
889 case BuiltinType::Long:
890 encoding = 'l';
891 break;
892 case BuiltinType::LongLong:
893 encoding = 'q';
894 break;
895 case BuiltinType::Float:
896 encoding = 'f';
897 break;
898 case BuiltinType::Double:
899 encoding = 'd';
900 break;
901 case BuiltinType::LongDouble:
902 encoding = 'd';
903 break;
904 default:
905 assert(0 && "Unhandled builtin type kind");
906 }
907
908 S += encoding;
909 } else if (const PointerType *PT = Ty->getAsPointerType()) {
910 QualType PointeeTy = PT->getPointeeType();
911
912 if (PointeeTy->isCharType()) {
913 // char pointer types should be encoded as '*' unless it is a
914 // type that has been typedef'd to 'BOOL'.
915 if (isTypeTypedefedAsBOOL(T)) {
916 S += '*';
917 return;
918 }
919 }
920
921 S += '^';
922 getObjcEncodingForType(PT->getPointeeType(), S);
923 } else if (const ArrayType *AT = Ty->getAsArrayType()) {
924 S += '[';
925
926 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
927 S += llvm::utostr(CAT->getSize().getZExtValue());
928 else
929 assert(0 && "Unhandled array type!");
930
931 getObjcEncodingForType(AT->getElementType(), S);
932 S += ']';
933 } else
934 fprintf(stderr, "@encode for type %s not implemented!\n",
935 Ty.getAsString().c_str());
936}
937
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000938void ASTContext::setBuiltinVaListType(QualType T)
939{
940 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
941
942 BuiltinVaListType = T;
943}
944
Steve Naroff7e219e42007-10-15 14:41:52 +0000945void ASTContext::setObjcIdType(TypedefDecl *TD)
946{
947 assert(ObjcIdType.isNull() && "'id' type already set!");
948
949 ObjcIdType = getTypedefType(TD);
950
951 // typedef struct objc_object *id;
952 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
953 assert(ptr && "'id' incorrectly typed");
954 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
955 assert(rec && "'id' incorrectly typed");
956 IdStructType = rec;
957}
958
Fariborz Jahanianb62f6812007-10-16 20:40:23 +0000959void ASTContext::setObjcSelType(TypedefDecl *TD)
960{
961 assert(ObjcSelType.isNull() && "'SEL' type already set!");
962
963 ObjcSelType = getTypedefType(TD);
964
965 // typedef struct objc_selector *SEL;
966 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
967 assert(ptr && "'SEL' incorrectly typed");
968 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
969 assert(rec && "'SEL' incorrectly typed");
970 SelStructType = rec;
971}
972
Fariborz Jahanian390d50a2007-10-17 16:58:11 +0000973void ASTContext::setObjcProtoType(TypedefDecl *TD)
974{
975 assert(ObjcProtoType.isNull() && "'Protocol' type already set!");
976
977 // typedef struct Protocol Protocol;
978 ObjcProtoType = TD->getUnderlyingType();
979 // Protocol * type
980 ObjcProtoType = getPointerType(ObjcProtoType);
981 ProtoStructType = TD->getUnderlyingType()->getAsStructureType();
982}
983
Steve Naroff21988912007-10-15 23:35:17 +0000984void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) {
985 assert(ObjcConstantStringType.isNull() &&
986 "'NSConstantString' type already set!");
987
988 ObjcConstantStringType = getObjcInterfaceType(Decl);
989}
990
Steve Naroffec0550f2007-10-15 20:41:53 +0000991bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
992 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
993 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
994
995 return lBuiltin->getKind() == rBuiltin->getKind();
996}
997
998
999bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
1000 if (lhs->isObjcInterfaceType() && isObjcIdType(rhs))
1001 return true;
1002 else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType())
1003 return true;
1004 return false;
1005}
1006
1007bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
1008 return true; // FIXME: IMPLEMENT.
1009}
1010
1011// C99 6.2.7p1: If both are complete types, then the following additional
1012// requirements apply...FIXME (handle compatibility across source files).
1013bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
1014 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
1015 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
1016
1017 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
1018 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1019 return true;
1020 }
1021 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
1022 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1023 return true;
1024 }
1025 return false;
1026}
1027
1028bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1029 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1030 // identically qualified and both shall be pointers to compatible types.
1031 if (lhs.getQualifiers() != rhs.getQualifiers())
1032 return false;
1033
1034 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1035 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1036
1037 return typesAreCompatible(ltype, rtype);
1038}
1039
1040// C++ 5.17p6: When the left opperand of an assignment operator denotes a
1041// reference to T, the operation assigns to the object of type T denoted by the
1042// reference.
1043bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1044 QualType ltype = lhs;
1045
1046 if (lhs->isReferenceType())
1047 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1048
1049 QualType rtype = rhs;
1050
1051 if (rhs->isReferenceType())
1052 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1053
1054 return typesAreCompatible(ltype, rtype);
1055}
1056
1057bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1058 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1059 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1060 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1061 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1062
1063 // first check the return types (common between C99 and K&R).
1064 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1065 return false;
1066
1067 if (lproto && rproto) { // two C99 style function prototypes
1068 unsigned lproto_nargs = lproto->getNumArgs();
1069 unsigned rproto_nargs = rproto->getNumArgs();
1070
1071 if (lproto_nargs != rproto_nargs)
1072 return false;
1073
1074 // both prototypes have the same number of arguments.
1075 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1076 (rproto->isVariadic() && !lproto->isVariadic()))
1077 return false;
1078
1079 // The use of ellipsis agree...now check the argument types.
1080 for (unsigned i = 0; i < lproto_nargs; i++)
1081 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
1082 return false;
1083 return true;
1084 }
1085 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1086 return true;
1087
1088 // we have a mixture of K&R style with C99 prototypes
1089 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1090
1091 if (proto->isVariadic())
1092 return false;
1093
1094 // FIXME: Each parameter type T in the prototype must be compatible with the
1095 // type resulting from applying the usual argument conversions to T.
1096 return true;
1097}
1098
1099bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
1100 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
1101 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
1102
1103 if (!typesAreCompatible(ltype, rtype))
1104 return false;
1105
1106 // FIXME: If both types specify constant sizes, then the sizes must also be
1107 // the same. Even if the sizes are the same, GCC produces an error.
1108 return true;
1109}
1110
1111/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1112/// both shall have the identically qualified version of a compatible type.
1113/// C99 6.2.7p1: Two types have compatible types if their types are the
1114/// same. See 6.7.[2,3,5] for additional rules.
1115bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
1116 QualType lcanon = lhs.getCanonicalType();
1117 QualType rcanon = rhs.getCanonicalType();
1118
1119 // If two types are identical, they are are compatible
1120 if (lcanon == rcanon)
1121 return true;
1122
1123 // If the canonical type classes don't match, they can't be compatible
1124 if (lcanon->getTypeClass() != rcanon->getTypeClass()) {
1125 // For Objective-C, it is possible for two types to be compatible
1126 // when their classes don't match (when dealing with "id"). If either type
1127 // is an interface, we defer to objcTypesAreCompatible().
1128 if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType())
1129 return objcTypesAreCompatible(lcanon, rcanon);
1130 return false;
1131 }
1132 switch (lcanon->getTypeClass()) {
1133 case Type::Pointer:
1134 return pointerTypesAreCompatible(lcanon, rcanon);
1135 case Type::Reference:
1136 return referenceTypesAreCompatible(lcanon, rcanon);
1137 case Type::ConstantArray:
1138 case Type::VariableArray:
1139 return arrayTypesAreCompatible(lcanon, rcanon);
1140 case Type::FunctionNoProto:
1141 case Type::FunctionProto:
1142 return functionTypesAreCompatible(lcanon, rcanon);
1143 case Type::Tagged: // handle structures, unions
1144 return tagTypesAreCompatible(lcanon, rcanon);
1145 case Type::Builtin:
1146 return builtinTypesAreCompatible(lcanon, rcanon);
1147 case Type::ObjcInterface:
1148 return interfaceTypesAreCompatible(lcanon, rcanon);
1149 default:
1150 assert(0 && "unexpected type");
1151 }
1152 return true; // should never get here...
1153}