blob: 60391318991ba920520970a94455640e2b9d66cc [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
Steve Naroff3fafa102007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Chris Lattner4b009652007-07-25 00:24:17 +000017#include "clang/Lex/Preprocessor.h"
18#include "clang/Basic/TargetInfo.h"
19#include "llvm/ADT/SmallVector.h"
Steve Naroff948fd372007-09-17 14:16:13 +000020#include "clang/Lex/IdentifierTable.h"
Chris Lattner4b009652007-07-25 00:24:17 +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;
45 unsigned NumVector = 0, NumComplex = 0;
46 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
47
48 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Steve Naroff948fd372007-09-17 14:16:13 +000049 unsigned NumObjcInterfaces = 0;
Chris Lattner4b009652007-07-25 00:24:17 +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;
59 else if (isa<ComplexType>(T))
60 ++NumComplex;
61 else if (isa<ArrayType>(T))
62 ++NumArray;
63 else if (isa<VectorType>(T))
64 ++NumVector;
65 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 Naroff948fd372007-09-17 14:16:13 +000080 } else if (isa<ObjcInterfaceType>(T))
81 ++NumObjcInterfaces;
82 else {
Chris Lattner4b009652007-07-25 00:24:17 +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);
90 fprintf(stderr, " %d complex types\n", NumComplex);
91 fprintf(stderr, " %d array types\n", NumArray);
92 fprintf(stderr, " %d vector types\n", NumVector);
93 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 Naroff948fd372007-09-17 14:16:13 +0000101 fprintf(stderr, " %d interface types\n", NumObjcInterfaces);
Chris Lattner4b009652007-07-25 00:24:17 +0000102 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
103 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
104 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
105 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
115
116void ASTContext::InitBuiltinTypes() {
117 assert(VoidTy.isNull() && "Context reinitialized?");
118
119 // C99 6.2.5p19.
120 InitBuiltinType(VoidTy, BuiltinType::Void);
121
122 // C99 6.2.5p2.
123 InitBuiltinType(BoolTy, BuiltinType::Bool);
124 // C99 6.2.5p3.
125 if (Target.isCharSigned(SourceLocation()))
126 InitBuiltinType(CharTy, BuiltinType::Char_S);
127 else
128 InitBuiltinType(CharTy, BuiltinType::Char_U);
129 // C99 6.2.5p4.
130 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
131 InitBuiltinType(ShortTy, BuiltinType::Short);
132 InitBuiltinType(IntTy, BuiltinType::Int);
133 InitBuiltinType(LongTy, BuiltinType::Long);
134 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
135
136 // C99 6.2.5p6.
137 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
138 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
139 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
140 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
141 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
142
143 // C99 6.2.5p10.
144 InitBuiltinType(FloatTy, BuiltinType::Float);
145 InitBuiltinType(DoubleTy, BuiltinType::Double);
146 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
147
148 // C99 6.2.5p11.
149 FloatComplexTy = getComplexType(FloatTy);
150 DoubleComplexTy = getComplexType(DoubleTy);
151 LongDoubleComplexTy = getComplexType(LongDoubleTy);
152}
153
154//===----------------------------------------------------------------------===//
155// Type Sizing and Analysis
156//===----------------------------------------------------------------------===//
157
158/// getTypeSize - Return the size of the specified type, in bits. This method
159/// does not work on incomplete types.
160std::pair<uint64_t, unsigned>
161ASTContext::getTypeInfo(QualType T, SourceLocation L) {
162 T = T.getCanonicalType();
163 uint64_t Size;
164 unsigned Align;
165 switch (T->getTypeClass()) {
166 case Type::TypeName: assert(0 && "Not a canonical type!");
167 case Type::FunctionNoProto:
168 case Type::FunctionProto:
169 default:
170 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000171 case Type::VariableArray:
172 assert(0 && "VLAs not implemented yet!");
173 case Type::ConstantArray: {
174 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
175
Chris Lattner4b009652007-07-25 00:24:17 +0000176 std::pair<uint64_t, unsigned> EltInfo =
Steve Naroff83c13012007-08-30 01:06:46 +0000177 getTypeInfo(CAT->getElementType(), L);
178 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000179 Align = EltInfo.second;
180 break;
181 }
182 case Type::Vector: {
183 std::pair<uint64_t, unsigned> EltInfo =
184 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
185 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
186 // FIXME: Vector alignment is not the alignment of its elements.
187 Align = EltInfo.second;
188 break;
189 }
190
191 case Type::Builtin: {
192 // FIXME: need to use TargetInfo to derive the target specific sizes. This
193 // implementation will suffice for play with vector support.
Chris Lattner858eece2007-09-22 18:29:59 +0000194 const llvm::fltSemantics *F;
Chris Lattner4b009652007-07-25 00:24:17 +0000195 switch (cast<BuiltinType>(T)->getKind()) {
196 default: assert(0 && "Unknown builtin type!");
197 case BuiltinType::Void:
198 assert(0 && "Incomplete types have no size!");
199 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
200 case BuiltinType::Char_S:
201 case BuiltinType::Char_U:
202 case BuiltinType::UChar:
203 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
204 case BuiltinType::UShort:
205 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
206 case BuiltinType::UInt:
207 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
208 case BuiltinType::ULong:
209 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
210 case BuiltinType::ULongLong:
211 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
Chris Lattner858eece2007-09-22 18:29:59 +0000212 case BuiltinType::Float: Target.getFloatInfo(Size, Align, F, L); break;
213 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, F, L);break;
214 case BuiltinType::LongDouble:Target.getLongDoubleInfo(Size,Align,F,L);break;
Chris Lattner4b009652007-07-25 00:24:17 +0000215 }
216 break;
217 }
218 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
219 case Type::Reference:
220 // "When applied to a reference or a reference type, the result is the size
221 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
222 // FIXME: This is wrong for struct layout!
223 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
224
225 case Type::Complex: {
226 // Complex types have the same alignment as their elements, but twice the
227 // size.
228 std::pair<uint64_t, unsigned> EltInfo =
229 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
230 Size = EltInfo.first*2;
231 Align = EltInfo.second;
232 break;
233 }
234 case Type::Tagged:
Chris Lattnereb56d292007-08-27 17:38:00 +0000235 TagType *TT = cast<TagType>(T);
236 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
237 const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L);
238 Size = Layout.getSize();
239 Align = Layout.getAlignment();
240 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattner90a018d2007-08-28 18:24:31 +0000241 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattnereb56d292007-08-27 17:38:00 +0000242 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000243 assert(0 && "Unimplemented type sizes!");
Chris Lattnereb56d292007-08-27 17:38:00 +0000244 }
Chris Lattner4b009652007-07-25 00:24:17 +0000245 break;
246 }
247
248 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
249 return std::make_pair(Size, Align);
250}
251
252/// getRecordLayout - Get or compute information about the layout of the
253/// specified record (struct/union/class), which indicates its size and field
254/// position information.
255const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D,
256 SourceLocation L) {
257 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
258
259 // Look up this layout, if already laid out, return what we have.
260 const RecordLayout *&Entry = RecordLayoutInfo[D];
261 if (Entry) return *Entry;
262
263 // Allocate and assign into RecordLayoutInfo here. The "Entry" reference can
264 // be invalidated (dangle) if the RecordLayoutInfo hashtable is inserted into.
265 RecordLayout *NewEntry = new RecordLayout();
266 Entry = NewEntry;
267
268 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
269 uint64_t RecordSize = 0;
270 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
271
272 if (D->getKind() != Decl::Union) {
273 // Layout each field, for now, just sequentially, respecting alignment. In
274 // the future, this will need to be tweakable by targets.
275 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
276 const FieldDecl *FD = D->getMember(i);
277 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
278 uint64_t FieldSize = FieldInfo.first;
279 unsigned FieldAlign = FieldInfo.second;
280
281 // Round up the current record size to the field's alignment boundary.
282 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
283
284 // Place this field at the current location.
285 FieldOffsets[i] = RecordSize;
286
287 // Reserve space for this field.
288 RecordSize += FieldSize;
289
290 // Remember max struct/class alignment.
291 RecordAlign = std::max(RecordAlign, FieldAlign);
292 }
293
294 // Finally, round the size of the total struct up to the alignment of the
295 // struct itself.
296 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
297 } else {
298 // Union layout just puts each member at the start of the record.
299 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
300 const FieldDecl *FD = D->getMember(i);
301 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
302 uint64_t FieldSize = FieldInfo.first;
303 unsigned FieldAlign = FieldInfo.second;
304
305 // Round up the current record size to the field's alignment boundary.
306 RecordSize = std::max(RecordSize, FieldSize);
307
308 // Place this field at the start of the record.
309 FieldOffsets[i] = 0;
310
311 // Remember max struct/class alignment.
312 RecordAlign = std::max(RecordAlign, FieldAlign);
313 }
314 }
315
316 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
317 return *NewEntry;
318}
319
Chris Lattner4b009652007-07-25 00:24:17 +0000320//===----------------------------------------------------------------------===//
321// Type creation/memoization methods
322//===----------------------------------------------------------------------===//
323
324
325/// getComplexType - Return the uniqued reference to the type for a complex
326/// number with the specified element type.
327QualType ASTContext::getComplexType(QualType T) {
328 // Unique pointers, to guarantee there is only one pointer of a particular
329 // structure.
330 llvm::FoldingSetNodeID ID;
331 ComplexType::Profile(ID, T);
332
333 void *InsertPos = 0;
334 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
335 return QualType(CT, 0);
336
337 // If the pointee type isn't canonical, this won't be a canonical type either,
338 // so fill in the canonical type field.
339 QualType Canonical;
340 if (!T->isCanonical()) {
341 Canonical = getComplexType(T.getCanonicalType());
342
343 // Get the new insert position for the node we care about.
344 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
345 assert(NewIP == 0 && "Shouldn't be in the map!");
346 }
347 ComplexType *New = new ComplexType(T, Canonical);
348 Types.push_back(New);
349 ComplexTypes.InsertNode(New, InsertPos);
350 return QualType(New, 0);
351}
352
353
354/// getPointerType - Return the uniqued reference to the type for a pointer to
355/// the specified type.
356QualType ASTContext::getPointerType(QualType T) {
357 // Unique pointers, to guarantee there is only one pointer of a particular
358 // structure.
359 llvm::FoldingSetNodeID ID;
360 PointerType::Profile(ID, T);
361
362 void *InsertPos = 0;
363 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
364 return QualType(PT, 0);
365
366 // If the pointee type isn't canonical, this won't be a canonical type either,
367 // so fill in the canonical type field.
368 QualType Canonical;
369 if (!T->isCanonical()) {
370 Canonical = getPointerType(T.getCanonicalType());
371
372 // Get the new insert position for the node we care about.
373 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
374 assert(NewIP == 0 && "Shouldn't be in the map!");
375 }
376 PointerType *New = new PointerType(T, Canonical);
377 Types.push_back(New);
378 PointerTypes.InsertNode(New, InsertPos);
379 return QualType(New, 0);
380}
381
382/// getReferenceType - Return the uniqued reference to the type for a reference
383/// to the specified type.
384QualType ASTContext::getReferenceType(QualType T) {
385 // Unique pointers, to guarantee there is only one pointer of a particular
386 // structure.
387 llvm::FoldingSetNodeID ID;
388 ReferenceType::Profile(ID, T);
389
390 void *InsertPos = 0;
391 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
392 return QualType(RT, 0);
393
394 // If the referencee type isn't canonical, this won't be a canonical type
395 // either, so fill in the canonical type field.
396 QualType Canonical;
397 if (!T->isCanonical()) {
398 Canonical = getReferenceType(T.getCanonicalType());
399
400 // Get the new insert position for the node we care about.
401 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
402 assert(NewIP == 0 && "Shouldn't be in the map!");
403 }
404
405 ReferenceType *New = new ReferenceType(T, Canonical);
406 Types.push_back(New);
407 ReferenceTypes.InsertNode(New, InsertPos);
408 return QualType(New, 0);
409}
410
Steve Naroff83c13012007-08-30 01:06:46 +0000411/// getConstantArrayType - Return the unique reference to the type for an
412/// array of the specified element type.
413QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000414 const llvm::APInt &ArySize,
415 ArrayType::ArraySizeModifier ASM,
416 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000417 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000418 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000419
420 void *InsertPos = 0;
Steve Naroff83c13012007-08-30 01:06:46 +0000421 if (ConstantArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000422 return QualType(ATP, 0);
423
424 // If the element type isn't canonical, this won't be a canonical type either,
425 // so fill in the canonical type field.
426 QualType Canonical;
427 if (!EltTy->isCanonical()) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000428 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
429 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000430 // Get the new insert position for the node we care about.
Steve Naroff83c13012007-08-30 01:06:46 +0000431 ConstantArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000432 assert(NewIP == 0 && "Shouldn't be in the map!");
433 }
434
Steve Naroff24c9b982007-08-30 18:10:14 +0000435 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
436 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000437 ArrayTypes.InsertNode(New, InsertPos);
438 Types.push_back(New);
439 return QualType(New, 0);
440}
441
Steve Naroffe2579e32007-08-30 18:14:25 +0000442/// getVariableArrayType - Returns a non-unique reference to the type for a
443/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000444QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
445 ArrayType::ArraySizeModifier ASM,
446 unsigned EltTypeQuals) {
447 // Since we don't unique expressions, it isn't possible to unique VLA's.
448 ArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
449 ASM, EltTypeQuals);
450 Types.push_back(New);
451 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000452}
453
Chris Lattner4b009652007-07-25 00:24:17 +0000454/// getVectorType - Return the unique reference to a vector type of
455/// the specified element type and size. VectorType must be a built-in type.
456QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
457 BuiltinType *baseType;
458
459 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
460 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
461
462 // Check if we've already instantiated a vector of this type.
463 llvm::FoldingSetNodeID ID;
464 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
465 void *InsertPos = 0;
466 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
467 return QualType(VTP, 0);
468
469 // If the element type isn't canonical, this won't be a canonical type either,
470 // so fill in the canonical type field.
471 QualType Canonical;
472 if (!vecType->isCanonical()) {
473 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
474
475 // Get the new insert position for the node we care about.
476 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
477 assert(NewIP == 0 && "Shouldn't be in the map!");
478 }
479 VectorType *New = new VectorType(vecType, NumElts, Canonical);
480 VectorTypes.InsertNode(New, InsertPos);
481 Types.push_back(New);
482 return QualType(New, 0);
483}
484
485/// getOCUVectorType - Return the unique reference to an OCU vector type of
486/// the specified element type and size. VectorType must be a built-in type.
487QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
488 BuiltinType *baseType;
489
490 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
491 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
492
493 // Check if we've already instantiated a vector of this type.
494 llvm::FoldingSetNodeID ID;
495 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
496 void *InsertPos = 0;
497 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
498 return QualType(VTP, 0);
499
500 // If the element type isn't canonical, this won't be a canonical type either,
501 // so fill in the canonical type field.
502 QualType Canonical;
503 if (!vecType->isCanonical()) {
504 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
505
506 // Get the new insert position for the node we care about.
507 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
508 assert(NewIP == 0 && "Shouldn't be in the map!");
509 }
510 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
511 VectorTypes.InsertNode(New, InsertPos);
512 Types.push_back(New);
513 return QualType(New, 0);
514}
515
516/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
517///
518QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
519 // Unique functions, to guarantee there is only one function of a particular
520 // structure.
521 llvm::FoldingSetNodeID ID;
522 FunctionTypeNoProto::Profile(ID, ResultTy);
523
524 void *InsertPos = 0;
525 if (FunctionTypeNoProto *FT =
526 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
527 return QualType(FT, 0);
528
529 QualType Canonical;
530 if (!ResultTy->isCanonical()) {
531 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
532
533 // Get the new insert position for the node we care about.
534 FunctionTypeNoProto *NewIP =
535 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
536 assert(NewIP == 0 && "Shouldn't be in the map!");
537 }
538
539 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
540 Types.push_back(New);
541 FunctionTypeProtos.InsertNode(New, InsertPos);
542 return QualType(New, 0);
543}
544
545/// getFunctionType - Return a normal function type with a typed argument
546/// list. isVariadic indicates whether the argument list includes '...'.
547QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
548 unsigned NumArgs, bool isVariadic) {
549 // Unique functions, to guarantee there is only one function of a particular
550 // structure.
551 llvm::FoldingSetNodeID ID;
552 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
553
554 void *InsertPos = 0;
555 if (FunctionTypeProto *FTP =
556 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
557 return QualType(FTP, 0);
558
559 // Determine whether the type being created is already canonical or not.
560 bool isCanonical = ResultTy->isCanonical();
561 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
562 if (!ArgArray[i]->isCanonical())
563 isCanonical = false;
564
565 // If this type isn't canonical, get the canonical version of it.
566 QualType Canonical;
567 if (!isCanonical) {
568 llvm::SmallVector<QualType, 16> CanonicalArgs;
569 CanonicalArgs.reserve(NumArgs);
570 for (unsigned i = 0; i != NumArgs; ++i)
571 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
572
573 Canonical = getFunctionType(ResultTy.getCanonicalType(),
574 &CanonicalArgs[0], NumArgs,
575 isVariadic);
576
577 // Get the new insert position for the node we care about.
578 FunctionTypeProto *NewIP =
579 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
580 assert(NewIP == 0 && "Shouldn't be in the map!");
581 }
582
583 // FunctionTypeProto objects are not allocated with new because they have a
584 // variable size array (for parameter types) at the end of them.
585 FunctionTypeProto *FTP =
586 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
587 NumArgs*sizeof(QualType));
588 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
589 Canonical);
590 Types.push_back(FTP);
591 FunctionTypeProtos.InsertNode(FTP, InsertPos);
592 return QualType(FTP, 0);
593}
594
595/// getTypedefType - Return the unique reference to the type for the
596/// specified typename decl.
597QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
598 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
599
600 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
601 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
602 Types.push_back(Decl->TypeForDecl);
603 return QualType(Decl->TypeForDecl, 0);
604}
605
Steve Naroff81f1bba2007-09-06 21:24:23 +0000606/// getObjcInterfaceType - Return the unique reference to the type for the
607/// specified ObjC interface decl.
608QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) {
609 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
610
611 Decl->TypeForDecl = new ObjcInterfaceType(Decl);
612 Types.push_back(Decl->TypeForDecl);
613 return QualType(Decl->TypeForDecl, 0);
614}
615
Steve Naroff0604dd92007-08-01 18:02:17 +0000616/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
617/// TypeOfExpr AST's (since expression's are never shared). For example,
618/// multiple declarations that refer to "typeof(x)" all contain different
619/// DeclRefExpr's. This doesn't effect the type checker, since it operates
620/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000621QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroff7cbb1462007-07-31 12:34:36 +0000622 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000623 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
624 Types.push_back(toe);
625 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000626}
627
Steve Naroff0604dd92007-08-01 18:02:17 +0000628/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
629/// TypeOfType AST's. The only motivation to unique these nodes would be
630/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
631/// an issue. This doesn't effect the type checker, since it operates
632/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000633QualType ASTContext::getTypeOfType(QualType tofType) {
634 QualType Canonical = tofType.getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000635 TypeOfType *tot = new TypeOfType(tofType, Canonical);
636 Types.push_back(tot);
637 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000638}
639
Chris Lattner4b009652007-07-25 00:24:17 +0000640/// getTagDeclType - Return the unique reference to the type for the
641/// specified TagDecl (struct/union/class/enum) decl.
642QualType ASTContext::getTagDeclType(TagDecl *Decl) {
643 // The decl stores the type cache.
644 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
645
646 Decl->TypeForDecl = new TagType(Decl, QualType());
647 Types.push_back(Decl->TypeForDecl);
648 return QualType(Decl->TypeForDecl, 0);
649}
650
651/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
652/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
653/// needs to agree with the definition in <stddef.h>.
654QualType ASTContext::getSizeType() const {
655 // On Darwin, size_t is defined as a "long unsigned int".
656 // FIXME: should derive from "Target".
657 return UnsignedLongTy;
658}
659
660/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
661/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
662QualType ASTContext::getPointerDiffType() const {
663 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
664 // FIXME: should derive from "Target".
665 return IntTy;
666}
667
668/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
669/// routine will assert if passed a built-in type that isn't an integer or enum.
670static int getIntegerRank(QualType t) {
671 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
672 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
673 return 4;
674 }
675
676 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
677 switch (BT->getKind()) {
678 default:
679 assert(0 && "getIntegerRank(): not a built-in integer");
680 case BuiltinType::Bool:
681 return 1;
682 case BuiltinType::Char_S:
683 case BuiltinType::Char_U:
684 case BuiltinType::SChar:
685 case BuiltinType::UChar:
686 return 2;
687 case BuiltinType::Short:
688 case BuiltinType::UShort:
689 return 3;
690 case BuiltinType::Int:
691 case BuiltinType::UInt:
692 return 4;
693 case BuiltinType::Long:
694 case BuiltinType::ULong:
695 return 5;
696 case BuiltinType::LongLong:
697 case BuiltinType::ULongLong:
698 return 6;
699 }
700}
701
702/// getFloatingRank - Return a relative rank for floating point types.
703/// This routine will assert if passed a built-in type that isn't a float.
704static int getFloatingRank(QualType T) {
705 T = T.getCanonicalType();
706 if (ComplexType *CT = dyn_cast<ComplexType>(T))
707 return getFloatingRank(CT->getElementType());
708
709 switch (cast<BuiltinType>(T)->getKind()) {
710 default: assert(0 && "getFloatingPointRank(): not a floating type");
711 case BuiltinType::Float: return FloatRank;
712 case BuiltinType::Double: return DoubleRank;
713 case BuiltinType::LongDouble: return LongDoubleRank;
714 }
715}
716
Steve Narofffa0c4532007-08-27 01:41:48 +0000717/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
718/// point or a complex type (based on typeDomain/typeSize).
719/// 'typeDomain' is a real floating point or complex type.
720/// 'typeSize' is a real floating point or complex type.
Steve Naroff3cf497f2007-08-27 01:27:54 +0000721QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
722 QualType typeSize, QualType typeDomain) const {
723 if (typeDomain->isComplexType()) {
724 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000725 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000726 case FloatRank: return FloatComplexTy;
727 case DoubleRank: return DoubleComplexTy;
728 case LongDoubleRank: return LongDoubleComplexTy;
729 }
Chris Lattner4b009652007-07-25 00:24:17 +0000730 }
Steve Naroff3cf497f2007-08-27 01:27:54 +0000731 if (typeDomain->isRealFloatingType()) {
732 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000733 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000734 case FloatRank: return FloatTy;
735 case DoubleRank: return DoubleTy;
736 case LongDoubleRank: return LongDoubleTy;
737 }
738 }
739 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner1d2b4612007-09-16 19:23:47 +0000740 //an invalid return value, but the assert
741 //will ensure that this code is never reached.
742 return VoidTy;
Chris Lattner4b009652007-07-25 00:24:17 +0000743}
744
Steve Naroff45fc9822007-08-27 15:30:22 +0000745/// compareFloatingType - Handles 3 different combos:
746/// float/float, float/complex, complex/complex.
747/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
748int ASTContext::compareFloatingType(QualType lt, QualType rt) {
749 if (getFloatingRank(lt) == getFloatingRank(rt))
750 return 0;
751 if (getFloatingRank(lt) > getFloatingRank(rt))
752 return 1;
753 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +0000754}
755
756// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
757// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
758QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
759 if (lhs == rhs) return lhs;
760
761 bool t1Unsigned = lhs->isUnsignedIntegerType();
762 bool t2Unsigned = rhs->isUnsignedIntegerType();
763
764 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
765 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
766
767 // We have two integer types with differing signs
768 QualType unsignedType = t1Unsigned ? lhs : rhs;
769 QualType signedType = t1Unsigned ? rhs : lhs;
770
771 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
772 return unsignedType;
773 else {
774 // FIXME: Need to check if the signed type can represent all values of the
775 // unsigned type. If it can, then the result is the signed type.
776 // If it can't, then the result is the unsigned version of the signed type.
777 // Should probably add a helper that returns a signed integer type from
778 // an unsigned (and vice versa). C99 6.3.1.8.
779 return signedType;
780 }
781}
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000782
783// getCFConstantStringType - Return the type used for constant CFStrings.
784QualType ASTContext::getCFConstantStringType() {
785 if (!CFConstantStringTypeDecl) {
786 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
787 &Idents.get("__builtin_CFString"),
788 0);
789
790 QualType FieldTypes[4];
791
792 // const int *isa;
793 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
794 // int flags;
795 FieldTypes[1] = IntTy;
796 // const char *str;
797 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
798 // long length;
799 FieldTypes[3] = LongTy;
800 // Create fields
801 FieldDecl *FieldDecls[4];
802
803 for (unsigned i = 0; i < 4; ++i)
Steve Naroffdc1ad762007-09-14 02:20:46 +0000804 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000805
806 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
807 }
808
809 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif61ce98c2007-09-11 15:32:40 +0000810}