blob: 93d4232bf2a59f8d43e3e98ab45eff8da2b4a610 [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();
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000156
157 // void * type
158 VoidPtrTy = getPointerType(VoidTy);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159}
160
Chris Lattner464175b2007-07-18 17:52:12 +0000161//===----------------------------------------------------------------------===//
162// Type Sizing and Analysis
163//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000164
165/// getTypeSize - Return the size of the specified type, in bits. This method
166/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000167std::pair<uint64_t, unsigned>
168ASTContext::getTypeInfo(QualType T, SourceLocation L) {
Chris Lattnera7674d82007-07-13 22:13:22 +0000169 T = T.getCanonicalType();
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000170 uint64_t Size;
171 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000172 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000173 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000174 case Type::FunctionNoProto:
175 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000176 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000177 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000178 case Type::VariableArray:
179 assert(0 && "VLAs not implemented yet!");
180 case Type::ConstantArray: {
181 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
182
Chris Lattner030d8842007-07-19 22:06:24 +0000183 std::pair<uint64_t, unsigned> EltInfo =
Steve Narofffb22d962007-08-30 01:06:46 +0000184 getTypeInfo(CAT->getElementType(), L);
185 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000186 Align = EltInfo.second;
187 break;
188 }
189 case Type::Vector: {
190 std::pair<uint64_t, unsigned> EltInfo =
191 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
192 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
193 // FIXME: Vector alignment is not the alignment of its elements.
194 Align = EltInfo.second;
195 break;
196 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000197
Chris Lattnera7674d82007-07-13 22:13:22 +0000198 case Type::Builtin: {
199 // FIXME: need to use TargetInfo to derive the target specific sizes. This
200 // implementation will suffice for play with vector support.
Chris Lattner525a0502007-09-22 18:29:59 +0000201 const llvm::fltSemantics *F;
Chris Lattnera7674d82007-07-13 22:13:22 +0000202 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000203 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000204 case BuiltinType::Void:
205 assert(0 && "Incomplete types have no size!");
206 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000207 case BuiltinType::Char_S:
208 case BuiltinType::Char_U:
209 case BuiltinType::UChar:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000210 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000211 case BuiltinType::UShort:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000212 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000213 case BuiltinType::UInt:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000214 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000215 case BuiltinType::ULong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000216 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000217 case BuiltinType::ULongLong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000218 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
Chris Lattner525a0502007-09-22 18:29:59 +0000219 case BuiltinType::Float: Target.getFloatInfo(Size, Align, F, L); break;
220 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, F, L);break;
221 case BuiltinType::LongDouble:Target.getLongDoubleInfo(Size,Align,F,L);break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000222 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000223 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000224 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000225 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000226 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000227 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000228 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
229 // FIXME: This is wrong for struct layout!
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000230 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000231
232 case Type::Complex: {
233 // Complex types have the same alignment as their elements, but twice the
234 // size.
235 std::pair<uint64_t, unsigned> EltInfo =
236 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
237 Size = EltInfo.first*2;
238 Align = EltInfo.second;
239 break;
240 }
241 case Type::Tagged:
Chris Lattner6cd862c2007-08-27 17:38:00 +0000242 TagType *TT = cast<TagType>(T);
243 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
244 const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L);
245 Size = Layout.getSize();
246 Align = Layout.getAlignment();
247 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattnere00b18c2007-08-28 18:24:31 +0000248 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000249 } else {
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000250 assert(0 && "Unimplemented type sizes!");
Chris Lattner6cd862c2007-08-27 17:38:00 +0000251 }
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000252 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000253 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000254
Chris Lattner464175b2007-07-18 17:52:12 +0000255 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000256 return std::make_pair(Size, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000257}
258
Chris Lattner464175b2007-07-18 17:52:12 +0000259/// getRecordLayout - Get or compute information about the layout of the
260/// specified record (struct/union/class), which indicates its size and field
261/// position information.
262const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D,
263 SourceLocation L) {
264 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
265
266 // Look up this layout, if already laid out, return what we have.
267 const RecordLayout *&Entry = RecordLayoutInfo[D];
268 if (Entry) return *Entry;
269
270 // Allocate and assign into RecordLayoutInfo here. The "Entry" reference can
271 // be invalidated (dangle) if the RecordLayoutInfo hashtable is inserted into.
272 RecordLayout *NewEntry = new RecordLayout();
273 Entry = NewEntry;
274
275 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
276 uint64_t RecordSize = 0;
277 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
278
279 if (D->getKind() != Decl::Union) {
280 // Layout each field, for now, just sequentially, respecting alignment. In
281 // the future, this will need to be tweakable by targets.
282 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
283 const FieldDecl *FD = D->getMember(i);
284 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
285 uint64_t FieldSize = FieldInfo.first;
286 unsigned FieldAlign = FieldInfo.second;
287
288 // Round up the current record size to the field's alignment boundary.
289 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
290
291 // Place this field at the current location.
292 FieldOffsets[i] = RecordSize;
293
294 // Reserve space for this field.
295 RecordSize += FieldSize;
296
297 // Remember max struct/class alignment.
298 RecordAlign = std::max(RecordAlign, FieldAlign);
299 }
300
301 // Finally, round the size of the total struct up to the alignment of the
302 // struct itself.
303 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
304 } else {
305 // Union layout just puts each member at the start of the record.
306 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
307 const FieldDecl *FD = D->getMember(i);
308 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
309 uint64_t FieldSize = FieldInfo.first;
310 unsigned FieldAlign = FieldInfo.second;
311
312 // Round up the current record size to the field's alignment boundary.
313 RecordSize = std::max(RecordSize, FieldSize);
314
315 // Place this field at the start of the record.
316 FieldOffsets[i] = 0;
317
318 // Remember max struct/class alignment.
319 RecordAlign = std::max(RecordAlign, FieldAlign);
320 }
321 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000322
323 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
324 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000325}
326
Chris Lattnera7674d82007-07-13 22:13:22 +0000327//===----------------------------------------------------------------------===//
328// Type creation/memoization methods
329//===----------------------------------------------------------------------===//
330
331
Reid Spencer5f016e22007-07-11 17:01:13 +0000332/// getComplexType - Return the uniqued reference to the type for a complex
333/// number with the specified element type.
334QualType ASTContext::getComplexType(QualType T) {
335 // Unique pointers, to guarantee there is only one pointer of a particular
336 // structure.
337 llvm::FoldingSetNodeID ID;
338 ComplexType::Profile(ID, T);
339
340 void *InsertPos = 0;
341 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
342 return QualType(CT, 0);
343
344 // If the pointee type isn't canonical, this won't be a canonical type either,
345 // so fill in the canonical type field.
346 QualType Canonical;
347 if (!T->isCanonical()) {
348 Canonical = getComplexType(T.getCanonicalType());
349
350 // Get the new insert position for the node we care about.
351 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
352 assert(NewIP == 0 && "Shouldn't be in the map!");
353 }
354 ComplexType *New = new ComplexType(T, Canonical);
355 Types.push_back(New);
356 ComplexTypes.InsertNode(New, InsertPos);
357 return QualType(New, 0);
358}
359
360
361/// getPointerType - Return the uniqued reference to the type for a pointer to
362/// the specified type.
363QualType ASTContext::getPointerType(QualType T) {
364 // Unique pointers, to guarantee there is only one pointer of a particular
365 // structure.
366 llvm::FoldingSetNodeID ID;
367 PointerType::Profile(ID, T);
368
369 void *InsertPos = 0;
370 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
371 return QualType(PT, 0);
372
373 // If the pointee type isn't canonical, this won't be a canonical type either,
374 // so fill in the canonical type field.
375 QualType Canonical;
376 if (!T->isCanonical()) {
377 Canonical = getPointerType(T.getCanonicalType());
378
379 // Get the new insert position for the node we care about.
380 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
381 assert(NewIP == 0 && "Shouldn't be in the map!");
382 }
383 PointerType *New = new PointerType(T, Canonical);
384 Types.push_back(New);
385 PointerTypes.InsertNode(New, InsertPos);
386 return QualType(New, 0);
387}
388
389/// getReferenceType - Return the uniqued reference to the type for a reference
390/// to the specified type.
391QualType ASTContext::getReferenceType(QualType T) {
392 // Unique pointers, to guarantee there is only one pointer of a particular
393 // structure.
394 llvm::FoldingSetNodeID ID;
395 ReferenceType::Profile(ID, T);
396
397 void *InsertPos = 0;
398 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
399 return QualType(RT, 0);
400
401 // If the referencee type isn't canonical, this won't be a canonical type
402 // either, so fill in the canonical type field.
403 QualType Canonical;
404 if (!T->isCanonical()) {
405 Canonical = getReferenceType(T.getCanonicalType());
406
407 // Get the new insert position for the node we care about.
408 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
409 assert(NewIP == 0 && "Shouldn't be in the map!");
410 }
411
412 ReferenceType *New = new ReferenceType(T, Canonical);
413 Types.push_back(New);
414 ReferenceTypes.InsertNode(New, InsertPos);
415 return QualType(New, 0);
416}
417
Steve Narofffb22d962007-08-30 01:06:46 +0000418/// getConstantArrayType - Return the unique reference to the type for an
419/// array of the specified element type.
420QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000421 const llvm::APInt &ArySize,
422 ArrayType::ArraySizeModifier ASM,
423 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000425 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000426
427 void *InsertPos = 0;
Steve Narofffb22d962007-08-30 01:06:46 +0000428 if (ConstantArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 return QualType(ATP, 0);
430
431 // If the element type isn't canonical, this won't be a canonical type either,
432 // so fill in the canonical type field.
433 QualType Canonical;
434 if (!EltTy->isCanonical()) {
Steve Naroffc9406122007-08-30 18:10:14 +0000435 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
436 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000437 // Get the new insert position for the node we care about.
Steve Narofffb22d962007-08-30 01:06:46 +0000438 ConstantArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000439 assert(NewIP == 0 && "Shouldn't be in the map!");
440 }
441
Steve Naroffc9406122007-08-30 18:10:14 +0000442 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
443 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000444 ArrayTypes.InsertNode(New, InsertPos);
445 Types.push_back(New);
446 return QualType(New, 0);
447}
448
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000449/// getVariableArrayType - Returns a non-unique reference to the type for a
450/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000451QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
452 ArrayType::ArraySizeModifier ASM,
453 unsigned EltTypeQuals) {
454 // Since we don't unique expressions, it isn't possible to unique VLA's.
455 ArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
456 ASM, EltTypeQuals);
457 Types.push_back(New);
458 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000459}
460
Steve Naroff73322922007-07-18 18:00:27 +0000461/// getVectorType - Return the unique reference to a vector type of
462/// the specified element type and size. VectorType must be a built-in type.
463QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000464 BuiltinType *baseType;
465
466 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000467 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000468
469 // Check if we've already instantiated a vector of this type.
470 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000471 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000472 void *InsertPos = 0;
473 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
474 return QualType(VTP, 0);
475
476 // If the element type isn't canonical, this won't be a canonical type either,
477 // so fill in the canonical type field.
478 QualType Canonical;
479 if (!vecType->isCanonical()) {
Steve Naroff73322922007-07-18 18:00:27 +0000480 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000481
482 // Get the new insert position for the node we care about.
483 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
484 assert(NewIP == 0 && "Shouldn't be in the map!");
485 }
486 VectorType *New = new VectorType(vecType, NumElts, Canonical);
487 VectorTypes.InsertNode(New, InsertPos);
488 Types.push_back(New);
489 return QualType(New, 0);
490}
491
Steve Naroff73322922007-07-18 18:00:27 +0000492/// getOCUVectorType - Return the unique reference to an OCU vector type of
493/// the specified element type and size. VectorType must be a built-in type.
494QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
495 BuiltinType *baseType;
496
497 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
498 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
499
500 // Check if we've already instantiated a vector of this type.
501 llvm::FoldingSetNodeID ID;
502 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
503 void *InsertPos = 0;
504 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
505 return QualType(VTP, 0);
506
507 // If the element type isn't canonical, this won't be a canonical type either,
508 // so fill in the canonical type field.
509 QualType Canonical;
510 if (!vecType->isCanonical()) {
511 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
512
513 // Get the new insert position for the node we care about.
514 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
515 assert(NewIP == 0 && "Shouldn't be in the map!");
516 }
517 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
518 VectorTypes.InsertNode(New, InsertPos);
519 Types.push_back(New);
520 return QualType(New, 0);
521}
522
Reid Spencer5f016e22007-07-11 17:01:13 +0000523/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
524///
525QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
526 // Unique functions, to guarantee there is only one function of a particular
527 // structure.
528 llvm::FoldingSetNodeID ID;
529 FunctionTypeNoProto::Profile(ID, ResultTy);
530
531 void *InsertPos = 0;
532 if (FunctionTypeNoProto *FT =
533 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
534 return QualType(FT, 0);
535
536 QualType Canonical;
537 if (!ResultTy->isCanonical()) {
538 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
539
540 // Get the new insert position for the node we care about.
541 FunctionTypeNoProto *NewIP =
542 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
543 assert(NewIP == 0 && "Shouldn't be in the map!");
544 }
545
546 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
547 Types.push_back(New);
548 FunctionTypeProtos.InsertNode(New, InsertPos);
549 return QualType(New, 0);
550}
551
552/// getFunctionType - Return a normal function type with a typed argument
553/// list. isVariadic indicates whether the argument list includes '...'.
554QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
555 unsigned NumArgs, bool isVariadic) {
556 // Unique functions, to guarantee there is only one function of a particular
557 // structure.
558 llvm::FoldingSetNodeID ID;
559 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
560
561 void *InsertPos = 0;
562 if (FunctionTypeProto *FTP =
563 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
564 return QualType(FTP, 0);
565
566 // Determine whether the type being created is already canonical or not.
567 bool isCanonical = ResultTy->isCanonical();
568 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
569 if (!ArgArray[i]->isCanonical())
570 isCanonical = false;
571
572 // If this type isn't canonical, get the canonical version of it.
573 QualType Canonical;
574 if (!isCanonical) {
575 llvm::SmallVector<QualType, 16> CanonicalArgs;
576 CanonicalArgs.reserve(NumArgs);
577 for (unsigned i = 0; i != NumArgs; ++i)
578 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
579
580 Canonical = getFunctionType(ResultTy.getCanonicalType(),
581 &CanonicalArgs[0], NumArgs,
582 isVariadic);
583
584 // Get the new insert position for the node we care about.
585 FunctionTypeProto *NewIP =
586 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
587 assert(NewIP == 0 && "Shouldn't be in the map!");
588 }
589
590 // FunctionTypeProto objects are not allocated with new because they have a
591 // variable size array (for parameter types) at the end of them.
592 FunctionTypeProto *FTP =
593 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000594 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000595 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
596 Canonical);
597 Types.push_back(FTP);
598 FunctionTypeProtos.InsertNode(FTP, InsertPos);
599 return QualType(FTP, 0);
600}
601
602/// getTypedefType - Return the unique reference to the type for the
603/// specified typename decl.
604QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
605 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
606
607 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
608 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
609 Types.push_back(Decl->TypeForDecl);
610 return QualType(Decl->TypeForDecl, 0);
611}
612
Steve Naroff3536b442007-09-06 21:24:23 +0000613/// getObjcInterfaceType - Return the unique reference to the type for the
614/// specified ObjC interface decl.
615QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) {
616 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
617
618 Decl->TypeForDecl = new ObjcInterfaceType(Decl);
619 Types.push_back(Decl->TypeForDecl);
620 return QualType(Decl->TypeForDecl, 0);
621}
622
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000623/// getObjcQualifiedInterfaceType - Return a
624/// ObjcQualifiedInterfaceType type for the given interface decl and
625/// the conforming protocol list.
626QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl,
627 ObjcProtocolDecl **Protocols, unsigned NumProtocols) {
628 ObjcInterfaceType *IType =
629 cast<ObjcInterfaceType>(getObjcInterfaceType(Decl));
630
631 llvm::FoldingSetNodeID ID;
632 ObjcQualifiedInterfaceType::Profile(ID, IType, Protocols, NumProtocols);
633
634 void *InsertPos = 0;
635 if (ObjcQualifiedInterfaceType *QT =
636 ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
637 return QualType(QT, 0);
638
639 // No Match;
Chris Lattner00bb2832007-10-11 03:36:41 +0000640 ObjcQualifiedInterfaceType *QType =
641 new ObjcQualifiedInterfaceType(IType, Protocols, NumProtocols);
Fariborz Jahanian4b6c9052007-10-11 00:55:41 +0000642 Types.push_back(QType);
643 ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
644 return QualType(QType, 0);
645}
646
Steve Naroff9752f252007-08-01 18:02:17 +0000647/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
648/// TypeOfExpr AST's (since expression's are never shared). For example,
649/// multiple declarations that refer to "typeof(x)" all contain different
650/// DeclRefExpr's. This doesn't effect the type checker, since it operates
651/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000652QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffd1861fd2007-07-31 12:34:36 +0000653 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000654 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
655 Types.push_back(toe);
656 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000657}
658
Steve Naroff9752f252007-08-01 18:02:17 +0000659/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
660/// TypeOfType AST's. The only motivation to unique these nodes would be
661/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
662/// an issue. This doesn't effect the type checker, since it operates
663/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000664QualType ASTContext::getTypeOfType(QualType tofType) {
665 QualType Canonical = tofType.getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000666 TypeOfType *tot = new TypeOfType(tofType, Canonical);
667 Types.push_back(tot);
668 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000669}
670
Reid Spencer5f016e22007-07-11 17:01:13 +0000671/// getTagDeclType - Return the unique reference to the type for the
672/// specified TagDecl (struct/union/class/enum) decl.
673QualType ASTContext::getTagDeclType(TagDecl *Decl) {
674 // The decl stores the type cache.
675 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
676
677 Decl->TypeForDecl = new TagType(Decl, QualType());
678 Types.push_back(Decl->TypeForDecl);
679 return QualType(Decl->TypeForDecl, 0);
680}
681
682/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
683/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
684/// needs to agree with the definition in <stddef.h>.
685QualType ASTContext::getSizeType() const {
686 // On Darwin, size_t is defined as a "long unsigned int".
687 // FIXME: should derive from "Target".
688 return UnsignedLongTy;
689}
690
Chris Lattner8b9023b2007-07-13 03:05:23 +0000691/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
692/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
693QualType ASTContext::getPointerDiffType() const {
694 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
695 // FIXME: should derive from "Target".
696 return IntTy;
697}
698
Reid Spencer5f016e22007-07-11 17:01:13 +0000699/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
700/// routine will assert if passed a built-in type that isn't an integer or enum.
701static int getIntegerRank(QualType t) {
702 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
703 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
704 return 4;
705 }
706
707 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
708 switch (BT->getKind()) {
709 default:
710 assert(0 && "getIntegerRank(): not a built-in integer");
711 case BuiltinType::Bool:
712 return 1;
713 case BuiltinType::Char_S:
714 case BuiltinType::Char_U:
715 case BuiltinType::SChar:
716 case BuiltinType::UChar:
717 return 2;
718 case BuiltinType::Short:
719 case BuiltinType::UShort:
720 return 3;
721 case BuiltinType::Int:
722 case BuiltinType::UInt:
723 return 4;
724 case BuiltinType::Long:
725 case BuiltinType::ULong:
726 return 5;
727 case BuiltinType::LongLong:
728 case BuiltinType::ULongLong:
729 return 6;
730 }
731}
732
733/// getFloatingRank - Return a relative rank for floating point types.
734/// This routine will assert if passed a built-in type that isn't a float.
735static int getFloatingRank(QualType T) {
736 T = T.getCanonicalType();
737 if (ComplexType *CT = dyn_cast<ComplexType>(T))
738 return getFloatingRank(CT->getElementType());
739
740 switch (cast<BuiltinType>(T)->getKind()) {
741 default: assert(0 && "getFloatingPointRank(): not a floating type");
742 case BuiltinType::Float: return FloatRank;
743 case BuiltinType::Double: return DoubleRank;
744 case BuiltinType::LongDouble: return LongDoubleRank;
745 }
746}
747
Steve Naroff716c7302007-08-27 01:41:48 +0000748/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
749/// point or a complex type (based on typeDomain/typeSize).
750/// 'typeDomain' is a real floating point or complex type.
751/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +0000752QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
753 QualType typeSize, QualType typeDomain) const {
754 if (typeDomain->isComplexType()) {
755 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000756 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000757 case FloatRank: return FloatComplexTy;
758 case DoubleRank: return DoubleComplexTy;
759 case LongDoubleRank: return LongDoubleComplexTy;
760 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 }
Steve Narofff1448a02007-08-27 01:27:54 +0000762 if (typeDomain->isRealFloatingType()) {
763 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000764 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000765 case FloatRank: return FloatTy;
766 case DoubleRank: return DoubleTy;
767 case LongDoubleRank: return LongDoubleTy;
768 }
769 }
770 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattnerb1776cb2007-09-16 19:23:47 +0000771 //an invalid return value, but the assert
772 //will ensure that this code is never reached.
773 return VoidTy;
Reid Spencer5f016e22007-07-11 17:01:13 +0000774}
775
Steve Narofffb0d4962007-08-27 15:30:22 +0000776/// compareFloatingType - Handles 3 different combos:
777/// float/float, float/complex, complex/complex.
778/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
779int ASTContext::compareFloatingType(QualType lt, QualType rt) {
780 if (getFloatingRank(lt) == getFloatingRank(rt))
781 return 0;
782 if (getFloatingRank(lt) > getFloatingRank(rt))
783 return 1;
784 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000785}
786
787// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
788// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
789QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
790 if (lhs == rhs) return lhs;
791
792 bool t1Unsigned = lhs->isUnsignedIntegerType();
793 bool t2Unsigned = rhs->isUnsignedIntegerType();
794
795 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
796 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
797
798 // We have two integer types with differing signs
799 QualType unsignedType = t1Unsigned ? lhs : rhs;
800 QualType signedType = t1Unsigned ? rhs : lhs;
801
802 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
803 return unsignedType;
804 else {
805 // FIXME: Need to check if the signed type can represent all values of the
806 // unsigned type. If it can, then the result is the signed type.
807 // If it can't, then the result is the unsigned version of the signed type.
808 // Should probably add a helper that returns a signed integer type from
809 // an unsigned (and vice versa). C99 6.3.1.8.
810 return signedType;
811 }
812}
Anders Carlsson71993dd2007-08-17 05:31:46 +0000813
814// getCFConstantStringType - Return the type used for constant CFStrings.
815QualType ASTContext::getCFConstantStringType() {
816 if (!CFConstantStringTypeDecl) {
817 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
818 &Idents.get("__builtin_CFString"),
819 0);
820
821 QualType FieldTypes[4];
822
823 // const int *isa;
824 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
825 // int flags;
826 FieldTypes[1] = IntTy;
827 // const char *str;
828 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
829 // long length;
830 FieldTypes[3] = LongTy;
831 // Create fields
832 FieldDecl *FieldDecls[4];
833
834 for (unsigned i = 0; i < 4; ++i)
Steve Narofff38661e2007-09-14 02:20:46 +0000835 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlsson71993dd2007-08-17 05:31:46 +0000836
837 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
838 }
839
840 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif84675832007-09-11 15:32:40 +0000841}
Anders Carlssonb2cf3572007-10-11 01:00:40 +0000842
Anders Carlssone8c49532007-10-29 06:33:42 +0000843// This returns true if a type has been typedefed to BOOL:
844// typedef <type> BOOL;
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000845static bool isTypeTypedefedAsBOOL(QualType T)
846{
Anders Carlssone8c49532007-10-29 06:33:42 +0000847 if (const TypedefType *TT = dyn_cast<TypedefType>(T))
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000848 if (!strcmp(TT->getDecl()->getName(), "BOOL"))
849 return true;
850
851 return false;
852}
853
Fariborz Jahanian33e1d642007-10-29 22:57:28 +0000854/// getObjcEncodingTypeSize returns size of type for objective-c encoding
855/// purpose.
856int ASTContext::getObjcEncodingTypeSize(QualType type) {
857 SourceLocation Loc;
858 uint64_t sz = getTypeSize(type, Loc);
859
860 // Make all integer and enum types at least as large as an int
861 if (sz > 0 && type->isIntegralType())
862 sz = std::max(sz, getTypeSize(IntTy, Loc));
863 // Treat arrays as pointers, since that's how they're passed in.
864 else if (type->isArrayType())
865 sz = getTypeSize(VoidPtrTy, Loc);
866 return sz / getTypeSize(CharTy, Loc);
867}
868
869/// getObjcEncodingForMethodDecl - Return the encoded type for this method
870/// declaration.
871void ASTContext::getObjcEncodingForMethodDecl(ObjcMethodDecl *Decl,
872 std::string& S)
873{
874 // TODO: First encode type qualifer, 'in', 'inout', etc. for the return type.
875 // Encode result type.
876 getObjcEncodingForType(Decl->getResultType(), S);
877 // Compute size of all parameters.
878 // Start with computing size of a pointer in number of bytes.
879 // FIXME: There might(should) be a better way of doing this computation!
880 SourceLocation Loc;
881 int PtrSize = getTypeSize(VoidPtrTy, Loc) / getTypeSize(CharTy, Loc);
882 // The first two arguments (self and _cmd) are pointers; account for
883 // their size.
884 int ParmOffset = 2 * PtrSize;
885 int NumOfParams = Decl->getNumParams();
886 for (int i = 0; i < NumOfParams; i++) {
887 QualType PType = Decl->getParamDecl(i)->getType();
888 int sz = getObjcEncodingTypeSize (PType);
889 assert (sz > 0 && "getObjcEncodingForMethodDecl - Incomplete param type");
890 ParmOffset += sz;
891 }
892 S += llvm::utostr(ParmOffset);
893 S += "@0:";
894 S += llvm::utostr(PtrSize);
895
896 // Argument types.
897 ParmOffset = 2 * PtrSize;
898 for (int i = 0; i < NumOfParams; i++) {
899 QualType PType = Decl->getParamDecl(i)->getType();
900 // TODO: Process argument qualifiers for user supplied arguments; such as,
901 // 'in', 'inout', etc.
902 getObjcEncodingForType(PType, S);
903 S += llvm::utostr(ParmOffset);
904 ParmOffset += getObjcEncodingTypeSize(PType);
905 }
906}
907
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000908void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const
909{
Anders Carlssone8c49532007-10-29 06:33:42 +0000910 // FIXME: This currently doesn't encode:
911 // @ An object (whether statically typed or typed id)
912 // # A class object (Class)
913 // : A method selector (SEL)
914 // {name=type...} A structure
915 // (name=type...) A union
916 // bnum A bit field of num bits
917
918 if (const BuiltinType *BT = T->getAsBuiltinType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000919 char encoding;
920 switch (BT->getKind()) {
921 case BuiltinType::Void:
922 encoding = 'v';
923 break;
924 case BuiltinType::Bool:
925 encoding = 'B';
926 break;
927 case BuiltinType::Char_U:
928 case BuiltinType::UChar:
929 encoding = 'C';
930 break;
931 case BuiltinType::UShort:
932 encoding = 'S';
933 break;
934 case BuiltinType::UInt:
935 encoding = 'I';
936 break;
937 case BuiltinType::ULong:
938 encoding = 'L';
939 break;
940 case BuiltinType::ULongLong:
941 encoding = 'Q';
942 break;
943 case BuiltinType::Char_S:
944 case BuiltinType::SChar:
945 encoding = 'c';
946 break;
947 case BuiltinType::Short:
948 encoding = 's';
949 break;
950 case BuiltinType::Int:
951 encoding = 'i';
952 break;
953 case BuiltinType::Long:
954 encoding = 'l';
955 break;
956 case BuiltinType::LongLong:
957 encoding = 'q';
958 break;
959 case BuiltinType::Float:
960 encoding = 'f';
961 break;
962 case BuiltinType::Double:
963 encoding = 'd';
964 break;
965 case BuiltinType::LongDouble:
966 encoding = 'd';
967 break;
968 default:
969 assert(0 && "Unhandled builtin type kind");
970 }
971
972 S += encoding;
Anders Carlssone8c49532007-10-29 06:33:42 +0000973 } else if (const PointerType *PT = T->getAsPointerType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000974 QualType PointeeTy = PT->getPointeeType();
975
976 if (PointeeTy->isCharType()) {
977 // char pointer types should be encoded as '*' unless it is a
978 // type that has been typedef'd to 'BOOL'.
Anders Carlssone8c49532007-10-29 06:33:42 +0000979 if (!isTypeTypedefedAsBOOL(PointeeTy)) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000980 S += '*';
981 return;
982 }
983 }
984
985 S += '^';
986 getObjcEncodingForType(PT->getPointeeType(), S);
Anders Carlssone8c49532007-10-29 06:33:42 +0000987 } else if (const ArrayType *AT = T->getAsArrayType()) {
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000988 S += '[';
989
990 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
991 S += llvm::utostr(CAT->getSize().getZExtValue());
992 else
993 assert(0 && "Unhandled array type!");
994
995 getObjcEncodingForType(AT->getElementType(), S);
996 S += ']';
997 } else
Anders Carlssone8c49532007-10-29 06:33:42 +0000998 assert(0 && "@encode for type not implemented!");
Anders Carlsson85f9bce2007-10-29 05:01:08 +0000999}
1000
Anders Carlssonb2cf3572007-10-11 01:00:40 +00001001void ASTContext::setBuiltinVaListType(QualType T)
1002{
1003 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
1004
1005 BuiltinVaListType = T;
1006}
1007
Steve Naroff7e219e42007-10-15 14:41:52 +00001008void ASTContext::setObjcIdType(TypedefDecl *TD)
1009{
1010 assert(ObjcIdType.isNull() && "'id' type already set!");
1011
1012 ObjcIdType = getTypedefType(TD);
1013
1014 // typedef struct objc_object *id;
1015 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1016 assert(ptr && "'id' incorrectly typed");
1017 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1018 assert(rec && "'id' incorrectly typed");
1019 IdStructType = rec;
1020}
1021
Fariborz Jahanianb62f6812007-10-16 20:40:23 +00001022void ASTContext::setObjcSelType(TypedefDecl *TD)
1023{
1024 assert(ObjcSelType.isNull() && "'SEL' type already set!");
1025
1026 ObjcSelType = getTypedefType(TD);
1027
1028 // typedef struct objc_selector *SEL;
1029 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
1030 assert(ptr && "'SEL' incorrectly typed");
1031 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
1032 assert(rec && "'SEL' incorrectly typed");
1033 SelStructType = rec;
1034}
1035
Fariborz Jahanian390d50a2007-10-17 16:58:11 +00001036void ASTContext::setObjcProtoType(TypedefDecl *TD)
1037{
1038 assert(ObjcProtoType.isNull() && "'Protocol' type already set!");
1039
1040 // typedef struct Protocol Protocol;
1041 ObjcProtoType = TD->getUnderlyingType();
1042 // Protocol * type
1043 ObjcProtoType = getPointerType(ObjcProtoType);
1044 ProtoStructType = TD->getUnderlyingType()->getAsStructureType();
1045}
1046
Steve Naroff21988912007-10-15 23:35:17 +00001047void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) {
1048 assert(ObjcConstantStringType.isNull() &&
1049 "'NSConstantString' type already set!");
1050
1051 ObjcConstantStringType = getObjcInterfaceType(Decl);
1052}
1053
Steve Naroffec0550f2007-10-15 20:41:53 +00001054bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
1055 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
1056 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
1057
1058 return lBuiltin->getKind() == rBuiltin->getKind();
1059}
1060
1061
1062bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
1063 if (lhs->isObjcInterfaceType() && isObjcIdType(rhs))
1064 return true;
1065 else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType())
1066 return true;
1067 return false;
1068}
1069
1070bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
1071 return true; // FIXME: IMPLEMENT.
1072}
1073
1074// C99 6.2.7p1: If both are complete types, then the following additional
1075// requirements apply...FIXME (handle compatibility across source files).
1076bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
1077 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
1078 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
1079
1080 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
1081 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1082 return true;
1083 }
1084 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
1085 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1086 return true;
1087 }
1088 return false;
1089}
1090
1091bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1092 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1093 // identically qualified and both shall be pointers to compatible types.
1094 if (lhs.getQualifiers() != rhs.getQualifiers())
1095 return false;
1096
1097 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1098 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1099
1100 return typesAreCompatible(ltype, rtype);
1101}
1102
1103// C++ 5.17p6: When the left opperand of an assignment operator denotes a
1104// reference to T, the operation assigns to the object of type T denoted by the
1105// reference.
1106bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1107 QualType ltype = lhs;
1108
1109 if (lhs->isReferenceType())
1110 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1111
1112 QualType rtype = rhs;
1113
1114 if (rhs->isReferenceType())
1115 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1116
1117 return typesAreCompatible(ltype, rtype);
1118}
1119
1120bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1121 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1122 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1123 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1124 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1125
1126 // first check the return types (common between C99 and K&R).
1127 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1128 return false;
1129
1130 if (lproto && rproto) { // two C99 style function prototypes
1131 unsigned lproto_nargs = lproto->getNumArgs();
1132 unsigned rproto_nargs = rproto->getNumArgs();
1133
1134 if (lproto_nargs != rproto_nargs)
1135 return false;
1136
1137 // both prototypes have the same number of arguments.
1138 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1139 (rproto->isVariadic() && !lproto->isVariadic()))
1140 return false;
1141
1142 // The use of ellipsis agree...now check the argument types.
1143 for (unsigned i = 0; i < lproto_nargs; i++)
1144 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
1145 return false;
1146 return true;
1147 }
1148 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1149 return true;
1150
1151 // we have a mixture of K&R style with C99 prototypes
1152 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1153
1154 if (proto->isVariadic())
1155 return false;
1156
1157 // FIXME: Each parameter type T in the prototype must be compatible with the
1158 // type resulting from applying the usual argument conversions to T.
1159 return true;
1160}
1161
1162bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
1163 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
1164 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
1165
1166 if (!typesAreCompatible(ltype, rtype))
1167 return false;
1168
1169 // FIXME: If both types specify constant sizes, then the sizes must also be
1170 // the same. Even if the sizes are the same, GCC produces an error.
1171 return true;
1172}
1173
1174/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1175/// both shall have the identically qualified version of a compatible type.
1176/// C99 6.2.7p1: Two types have compatible types if their types are the
1177/// same. See 6.7.[2,3,5] for additional rules.
1178bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
1179 QualType lcanon = lhs.getCanonicalType();
1180 QualType rcanon = rhs.getCanonicalType();
1181
1182 // If two types are identical, they are are compatible
1183 if (lcanon == rcanon)
1184 return true;
1185
1186 // If the canonical type classes don't match, they can't be compatible
1187 if (lcanon->getTypeClass() != rcanon->getTypeClass()) {
1188 // For Objective-C, it is possible for two types to be compatible
1189 // when their classes don't match (when dealing with "id"). If either type
1190 // is an interface, we defer to objcTypesAreCompatible().
1191 if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType())
1192 return objcTypesAreCompatible(lcanon, rcanon);
1193 return false;
1194 }
1195 switch (lcanon->getTypeClass()) {
1196 case Type::Pointer:
1197 return pointerTypesAreCompatible(lcanon, rcanon);
1198 case Type::Reference:
1199 return referenceTypesAreCompatible(lcanon, rcanon);
1200 case Type::ConstantArray:
1201 case Type::VariableArray:
1202 return arrayTypesAreCompatible(lcanon, rcanon);
1203 case Type::FunctionNoProto:
1204 case Type::FunctionProto:
1205 return functionTypesAreCompatible(lcanon, rcanon);
1206 case Type::Tagged: // handle structures, unions
1207 return tagTypesAreCompatible(lcanon, rcanon);
1208 case Type::Builtin:
1209 return builtinTypesAreCompatible(lcanon, rcanon);
1210 case Type::ObjcInterface:
1211 return interfaceTypesAreCompatible(lcanon, rcanon);
1212 default:
1213 assert(0 && "unexpected type");
1214 }
1215 return true; // should never get here...
1216}