blob: b0b963c4c801a48ae5640b6f34d7db33e0715dcc [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"
16#include "clang/Lex/Preprocessor.h"
17#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21enum FloatingRank {
22 FloatRank, DoubleRank, LongDoubleRank
23};
24
25ASTContext::~ASTContext() {
26 // Deallocate all the types.
27 while (!Types.empty()) {
28 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
29 // Destroy the object, but don't call delete. These are malloc'd.
30 FT->~FunctionTypeProto();
31 free(FT);
32 } else {
33 delete Types.back();
34 }
35 Types.pop_back();
36 }
37}
38
39void ASTContext::PrintStats() const {
40 fprintf(stderr, "*** AST Context Stats:\n");
41 fprintf(stderr, " %d types total.\n", (int)Types.size());
42 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Chris Lattner6d87fc62007-07-18 05:50:59 +000043 unsigned NumVector = 0, NumComplex = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000044 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
45
46 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
47
48 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
49 Type *T = Types[i];
50 if (isa<BuiltinType>(T))
51 ++NumBuiltin;
52 else if (isa<PointerType>(T))
53 ++NumPointer;
54 else if (isa<ReferenceType>(T))
55 ++NumReference;
Chris Lattner6d87fc62007-07-18 05:50:59 +000056 else if (isa<ComplexType>(T))
57 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +000058 else if (isa<ArrayType>(T))
59 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +000060 else if (isa<VectorType>(T))
61 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +000062 else if (isa<FunctionTypeNoProto>(T))
63 ++NumFunctionNP;
64 else if (isa<FunctionTypeProto>(T))
65 ++NumFunctionP;
66 else if (isa<TypedefType>(T))
67 ++NumTypeName;
68 else if (TagType *TT = dyn_cast<TagType>(T)) {
69 ++NumTagged;
70 switch (TT->getDecl()->getKind()) {
71 default: assert(0 && "Unknown tagged type!");
72 case Decl::Struct: ++NumTagStruct; break;
73 case Decl::Union: ++NumTagUnion; break;
74 case Decl::Class: ++NumTagClass; break;
75 case Decl::Enum: ++NumTagEnum; break;
76 }
77 } else {
78 assert(0 && "Unknown type!");
79 }
80 }
81
82 fprintf(stderr, " %d builtin types\n", NumBuiltin);
83 fprintf(stderr, " %d pointer types\n", NumPointer);
84 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner6d87fc62007-07-18 05:50:59 +000085 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +000086 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +000087 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +000088 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
89 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
90 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
91 fprintf(stderr, " %d tagged types\n", NumTagged);
92 fprintf(stderr, " %d struct types\n", NumTagStruct);
93 fprintf(stderr, " %d union types\n", NumTagUnion);
94 fprintf(stderr, " %d class types\n", NumTagClass);
95 fprintf(stderr, " %d enum types\n", NumTagEnum);
96 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
97 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +000098 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +000099 NumFunctionP*sizeof(FunctionTypeProto)+
100 NumFunctionNP*sizeof(FunctionTypeNoProto)+
101 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
102}
103
104
105void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
106 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
107}
108
109
110void ASTContext::InitBuiltinTypes() {
111 assert(VoidTy.isNull() && "Context reinitialized?");
112
113 // C99 6.2.5p19.
114 InitBuiltinType(VoidTy, BuiltinType::Void);
115
116 // C99 6.2.5p2.
117 InitBuiltinType(BoolTy, BuiltinType::Bool);
118 // C99 6.2.5p3.
119 if (Target.isCharSigned(SourceLocation()))
120 InitBuiltinType(CharTy, BuiltinType::Char_S);
121 else
122 InitBuiltinType(CharTy, BuiltinType::Char_U);
123 // C99 6.2.5p4.
124 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
125 InitBuiltinType(ShortTy, BuiltinType::Short);
126 InitBuiltinType(IntTy, BuiltinType::Int);
127 InitBuiltinType(LongTy, BuiltinType::Long);
128 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
129
130 // C99 6.2.5p6.
131 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
132 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
133 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
134 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
135 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
136
137 // C99 6.2.5p10.
138 InitBuiltinType(FloatTy, BuiltinType::Float);
139 InitBuiltinType(DoubleTy, BuiltinType::Double);
140 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
141
142 // C99 6.2.5p11.
143 FloatComplexTy = getComplexType(FloatTy);
144 DoubleComplexTy = getComplexType(DoubleTy);
145 LongDoubleComplexTy = getComplexType(LongDoubleTy);
146}
147
Chris Lattner464175b2007-07-18 17:52:12 +0000148//===----------------------------------------------------------------------===//
149// Type Sizing and Analysis
150//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000151
152/// getTypeSize - Return the size of the specified type, in bits. This method
153/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000154std::pair<uint64_t, unsigned>
155ASTContext::getTypeInfo(QualType T, SourceLocation L) {
Chris Lattnera7674d82007-07-13 22:13:22 +0000156 T = T.getCanonicalType();
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000157 uint64_t Size;
158 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000159 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000160 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000161 case Type::FunctionNoProto:
162 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000163 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000164 assert(0 && "Incomplete types have no size!");
Chris Lattner030d8842007-07-19 22:06:24 +0000165 case Type::Array: {
166 std::pair<uint64_t, unsigned> EltInfo =
167 getTypeInfo(cast<ArrayType>(T)->getElementType(), L);
168
169 // Get the size of the array.
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000170 llvm::APSInt Sz(32);
171 if (!cast<ArrayType>(T)->getSizeExpr()->isIntegerConstantExpr(Sz, *this))
Chris Lattner030d8842007-07-19 22:06:24 +0000172 assert(0 && "VLAs not implemented yet!");
173
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000174 Size = EltInfo.first*Sz.getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000175 Align = EltInfo.second;
176 break;
177 }
178 case Type::Vector: {
179 std::pair<uint64_t, unsigned> EltInfo =
180 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
181 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
182 // FIXME: Vector alignment is not the alignment of its elements.
183 Align = EltInfo.second;
184 break;
185 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000186
Chris Lattnera7674d82007-07-13 22:13:22 +0000187 case Type::Builtin: {
188 // FIXME: need to use TargetInfo to derive the target specific sizes. This
189 // implementation will suffice for play with vector support.
190 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000191 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000192 case BuiltinType::Void:
193 assert(0 && "Incomplete types have no size!");
194 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000195 case BuiltinType::Char_S:
196 case BuiltinType::Char_U:
197 case BuiltinType::UChar:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000198 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000199 case BuiltinType::UShort:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000200 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000201 case BuiltinType::UInt:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000202 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000203 case BuiltinType::ULong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000204 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000205 case BuiltinType::ULongLong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000206 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
207 case BuiltinType::Float: Target.getFloatInfo(Size, Align, L); break;
208 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, L); break;
209 case BuiltinType::LongDouble: Target.getLongDoubleInfo(Size, Align,L);break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000210 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000211 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000212 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000213 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000214 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000215 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000216 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
217 // FIXME: This is wrong for struct layout!
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000218 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000219
220 case Type::Complex: {
221 // Complex types have the same alignment as their elements, but twice the
222 // size.
223 std::pair<uint64_t, unsigned> EltInfo =
224 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
225 Size = EltInfo.first*2;
226 Align = EltInfo.second;
227 break;
228 }
229 case Type::Tagged:
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000230 RecordType *RT = dyn_cast<RecordType>(cast<TagType>(T));
231 if (!RT)
232 // FIXME: Handle enums.
233 assert(0 && "Unimplemented type sizes!");
234 const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L);
235 Size = Layout.getSize();
236 Align = Layout.getAlignment();
237 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000238 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000239
Chris Lattner464175b2007-07-18 17:52:12 +0000240 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000241 return std::make_pair(Size, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000242}
243
Chris Lattner464175b2007-07-18 17:52:12 +0000244/// getRecordLayout - Get or compute information about the layout of the
245/// specified record (struct/union/class), which indicates its size and field
246/// position information.
247const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D,
248 SourceLocation L) {
249 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
250
251 // Look up this layout, if already laid out, return what we have.
252 const RecordLayout *&Entry = RecordLayoutInfo[D];
253 if (Entry) return *Entry;
254
255 // Allocate and assign into RecordLayoutInfo here. The "Entry" reference can
256 // be invalidated (dangle) if the RecordLayoutInfo hashtable is inserted into.
257 RecordLayout *NewEntry = new RecordLayout();
258 Entry = NewEntry;
259
260 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
261 uint64_t RecordSize = 0;
262 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
263
264 if (D->getKind() != Decl::Union) {
265 // Layout each field, for now, just sequentially, respecting alignment. In
266 // the future, this will need to be tweakable by targets.
267 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
268 const FieldDecl *FD = D->getMember(i);
269 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
270 uint64_t FieldSize = FieldInfo.first;
271 unsigned FieldAlign = FieldInfo.second;
272
273 // Round up the current record size to the field's alignment boundary.
274 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
275
276 // Place this field at the current location.
277 FieldOffsets[i] = RecordSize;
278
279 // Reserve space for this field.
280 RecordSize += FieldSize;
281
282 // Remember max struct/class alignment.
283 RecordAlign = std::max(RecordAlign, FieldAlign);
284 }
285
286 // Finally, round the size of the total struct up to the alignment of the
287 // struct itself.
288 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
289 } else {
290 // Union layout just puts each member at the start of the record.
291 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
292 const FieldDecl *FD = D->getMember(i);
293 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
294 uint64_t FieldSize = FieldInfo.first;
295 unsigned FieldAlign = FieldInfo.second;
296
297 // Round up the current record size to the field's alignment boundary.
298 RecordSize = std::max(RecordSize, FieldSize);
299
300 // Place this field at the start of the record.
301 FieldOffsets[i] = 0;
302
303 // Remember max struct/class alignment.
304 RecordAlign = std::max(RecordAlign, FieldAlign);
305 }
306 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000307
308 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
309 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000310}
311
312
Chris Lattnera7674d82007-07-13 22:13:22 +0000313//===----------------------------------------------------------------------===//
314// Type creation/memoization methods
315//===----------------------------------------------------------------------===//
316
317
Reid Spencer5f016e22007-07-11 17:01:13 +0000318/// getComplexType - Return the uniqued reference to the type for a complex
319/// number with the specified element type.
320QualType ASTContext::getComplexType(QualType T) {
321 // Unique pointers, to guarantee there is only one pointer of a particular
322 // structure.
323 llvm::FoldingSetNodeID ID;
324 ComplexType::Profile(ID, T);
325
326 void *InsertPos = 0;
327 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
328 return QualType(CT, 0);
329
330 // If the pointee type isn't canonical, this won't be a canonical type either,
331 // so fill in the canonical type field.
332 QualType Canonical;
333 if (!T->isCanonical()) {
334 Canonical = getComplexType(T.getCanonicalType());
335
336 // Get the new insert position for the node we care about.
337 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
338 assert(NewIP == 0 && "Shouldn't be in the map!");
339 }
340 ComplexType *New = new ComplexType(T, Canonical);
341 Types.push_back(New);
342 ComplexTypes.InsertNode(New, InsertPos);
343 return QualType(New, 0);
344}
345
346
347/// getPointerType - Return the uniqued reference to the type for a pointer to
348/// the specified type.
349QualType ASTContext::getPointerType(QualType T) {
350 // Unique pointers, to guarantee there is only one pointer of a particular
351 // structure.
352 llvm::FoldingSetNodeID ID;
353 PointerType::Profile(ID, T);
354
355 void *InsertPos = 0;
356 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
357 return QualType(PT, 0);
358
359 // If the pointee type isn't canonical, this won't be a canonical type either,
360 // so fill in the canonical type field.
361 QualType Canonical;
362 if (!T->isCanonical()) {
363 Canonical = getPointerType(T.getCanonicalType());
364
365 // Get the new insert position for the node we care about.
366 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
367 assert(NewIP == 0 && "Shouldn't be in the map!");
368 }
369 PointerType *New = new PointerType(T, Canonical);
370 Types.push_back(New);
371 PointerTypes.InsertNode(New, InsertPos);
372 return QualType(New, 0);
373}
374
375/// getReferenceType - Return the uniqued reference to the type for a reference
376/// to the specified type.
377QualType ASTContext::getReferenceType(QualType T) {
378 // Unique pointers, to guarantee there is only one pointer of a particular
379 // structure.
380 llvm::FoldingSetNodeID ID;
381 ReferenceType::Profile(ID, T);
382
383 void *InsertPos = 0;
384 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
385 return QualType(RT, 0);
386
387 // If the referencee type isn't canonical, this won't be a canonical type
388 // either, so fill in the canonical type field.
389 QualType Canonical;
390 if (!T->isCanonical()) {
391 Canonical = getReferenceType(T.getCanonicalType());
392
393 // Get the new insert position for the node we care about.
394 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
395 assert(NewIP == 0 && "Shouldn't be in the map!");
396 }
397
398 ReferenceType *New = new ReferenceType(T, Canonical);
399 Types.push_back(New);
400 ReferenceTypes.InsertNode(New, InsertPos);
401 return QualType(New, 0);
402}
403
404/// getArrayType - Return the unique reference to the type for an array of the
405/// specified element type.
406QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
407 unsigned EltTypeQuals, Expr *NumElts) {
408 // Unique array types, to guarantee there is only one array of a particular
409 // structure.
410 llvm::FoldingSetNodeID ID;
411 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
412
413 void *InsertPos = 0;
414 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
415 return QualType(ATP, 0);
416
417 // If the element type isn't canonical, this won't be a canonical type either,
418 // so fill in the canonical type field.
419 QualType Canonical;
420 if (!EltTy->isCanonical()) {
421 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
422 NumElts);
423
424 // Get the new insert position for the node we care about.
425 ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
426 assert(NewIP == 0 && "Shouldn't be in the map!");
427 }
428
429 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
430 ArrayTypes.InsertNode(New, InsertPos);
431 Types.push_back(New);
432 return QualType(New, 0);
433}
434
Steve Naroff73322922007-07-18 18:00:27 +0000435/// getVectorType - Return the unique reference to a vector type of
436/// the specified element type and size. VectorType must be a built-in type.
437QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000438 BuiltinType *baseType;
439
440 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000441 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000442
443 // Check if we've already instantiated a vector of this type.
444 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000445 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000446 void *InsertPos = 0;
447 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
448 return QualType(VTP, 0);
449
450 // If the element type isn't canonical, this won't be a canonical type either,
451 // so fill in the canonical type field.
452 QualType Canonical;
453 if (!vecType->isCanonical()) {
Steve Naroff73322922007-07-18 18:00:27 +0000454 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000455
456 // Get the new insert position for the node we care about.
457 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
458 assert(NewIP == 0 && "Shouldn't be in the map!");
459 }
460 VectorType *New = new VectorType(vecType, NumElts, Canonical);
461 VectorTypes.InsertNode(New, InsertPos);
462 Types.push_back(New);
463 return QualType(New, 0);
464}
465
Steve Naroff73322922007-07-18 18:00:27 +0000466/// getOCUVectorType - Return the unique reference to an OCU vector type of
467/// the specified element type and size. VectorType must be a built-in type.
468QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
469 BuiltinType *baseType;
470
471 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
472 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
473
474 // Check if we've already instantiated a vector of this type.
475 llvm::FoldingSetNodeID ID;
476 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
477 void *InsertPos = 0;
478 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
479 return QualType(VTP, 0);
480
481 // If the element type isn't canonical, this won't be a canonical type either,
482 // so fill in the canonical type field.
483 QualType Canonical;
484 if (!vecType->isCanonical()) {
485 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
486
487 // Get the new insert position for the node we care about.
488 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
489 assert(NewIP == 0 && "Shouldn't be in the map!");
490 }
491 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
492 VectorTypes.InsertNode(New, InsertPos);
493 Types.push_back(New);
494 return QualType(New, 0);
495}
496
Reid Spencer5f016e22007-07-11 17:01:13 +0000497/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
498///
499QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
500 // Unique functions, to guarantee there is only one function of a particular
501 // structure.
502 llvm::FoldingSetNodeID ID;
503 FunctionTypeNoProto::Profile(ID, ResultTy);
504
505 void *InsertPos = 0;
506 if (FunctionTypeNoProto *FT =
507 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
508 return QualType(FT, 0);
509
510 QualType Canonical;
511 if (!ResultTy->isCanonical()) {
512 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
513
514 // Get the new insert position for the node we care about.
515 FunctionTypeNoProto *NewIP =
516 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
517 assert(NewIP == 0 && "Shouldn't be in the map!");
518 }
519
520 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
521 Types.push_back(New);
522 FunctionTypeProtos.InsertNode(New, InsertPos);
523 return QualType(New, 0);
524}
525
526/// getFunctionType - Return a normal function type with a typed argument
527/// list. isVariadic indicates whether the argument list includes '...'.
528QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
529 unsigned NumArgs, bool isVariadic) {
530 // Unique functions, to guarantee there is only one function of a particular
531 // structure.
532 llvm::FoldingSetNodeID ID;
533 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
534
535 void *InsertPos = 0;
536 if (FunctionTypeProto *FTP =
537 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
538 return QualType(FTP, 0);
539
540 // Determine whether the type being created is already canonical or not.
541 bool isCanonical = ResultTy->isCanonical();
542 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
543 if (!ArgArray[i]->isCanonical())
544 isCanonical = false;
545
546 // If this type isn't canonical, get the canonical version of it.
547 QualType Canonical;
548 if (!isCanonical) {
549 llvm::SmallVector<QualType, 16> CanonicalArgs;
550 CanonicalArgs.reserve(NumArgs);
551 for (unsigned i = 0; i != NumArgs; ++i)
552 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
553
554 Canonical = getFunctionType(ResultTy.getCanonicalType(),
555 &CanonicalArgs[0], NumArgs,
556 isVariadic);
557
558 // Get the new insert position for the node we care about.
559 FunctionTypeProto *NewIP =
560 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
561 assert(NewIP == 0 && "Shouldn't be in the map!");
562 }
563
564 // FunctionTypeProto objects are not allocated with new because they have a
565 // variable size array (for parameter types) at the end of them.
566 FunctionTypeProto *FTP =
567 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000568 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
570 Canonical);
571 Types.push_back(FTP);
572 FunctionTypeProtos.InsertNode(FTP, InsertPos);
573 return QualType(FTP, 0);
574}
575
576/// getTypedefType - Return the unique reference to the type for the
577/// specified typename decl.
578QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
579 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
580
581 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
582 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
583 Types.push_back(Decl->TypeForDecl);
584 return QualType(Decl->TypeForDecl, 0);
585}
586
Steve Naroff9752f252007-08-01 18:02:17 +0000587/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
588/// TypeOfExpr AST's (since expression's are never shared). For example,
589/// multiple declarations that refer to "typeof(x)" all contain different
590/// DeclRefExpr's. This doesn't effect the type checker, since it operates
591/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000592QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffd1861fd2007-07-31 12:34:36 +0000593 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000594 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
595 Types.push_back(toe);
596 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000597}
598
Steve Naroff9752f252007-08-01 18:02:17 +0000599/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
600/// TypeOfType AST's. The only motivation to unique these nodes would be
601/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
602/// an issue. This doesn't effect the type checker, since it operates
603/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000604QualType ASTContext::getTypeOfType(QualType tofType) {
605 QualType Canonical = tofType.getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000606 TypeOfType *tot = new TypeOfType(tofType, Canonical);
607 Types.push_back(tot);
608 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000609}
610
Reid Spencer5f016e22007-07-11 17:01:13 +0000611/// getTagDeclType - Return the unique reference to the type for the
612/// specified TagDecl (struct/union/class/enum) decl.
613QualType ASTContext::getTagDeclType(TagDecl *Decl) {
614 // The decl stores the type cache.
615 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
616
617 Decl->TypeForDecl = new TagType(Decl, QualType());
618 Types.push_back(Decl->TypeForDecl);
619 return QualType(Decl->TypeForDecl, 0);
620}
621
622/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
623/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
624/// needs to agree with the definition in <stddef.h>.
625QualType ASTContext::getSizeType() const {
626 // On Darwin, size_t is defined as a "long unsigned int".
627 // FIXME: should derive from "Target".
628 return UnsignedLongTy;
629}
630
Chris Lattner8b9023b2007-07-13 03:05:23 +0000631/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
632/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
633QualType ASTContext::getPointerDiffType() const {
634 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
635 // FIXME: should derive from "Target".
636 return IntTy;
637}
638
Reid Spencer5f016e22007-07-11 17:01:13 +0000639/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
640/// routine will assert if passed a built-in type that isn't an integer or enum.
641static int getIntegerRank(QualType t) {
642 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
643 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
644 return 4;
645 }
646
647 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
648 switch (BT->getKind()) {
649 default:
650 assert(0 && "getIntegerRank(): not a built-in integer");
651 case BuiltinType::Bool:
652 return 1;
653 case BuiltinType::Char_S:
654 case BuiltinType::Char_U:
655 case BuiltinType::SChar:
656 case BuiltinType::UChar:
657 return 2;
658 case BuiltinType::Short:
659 case BuiltinType::UShort:
660 return 3;
661 case BuiltinType::Int:
662 case BuiltinType::UInt:
663 return 4;
664 case BuiltinType::Long:
665 case BuiltinType::ULong:
666 return 5;
667 case BuiltinType::LongLong:
668 case BuiltinType::ULongLong:
669 return 6;
670 }
671}
672
673/// getFloatingRank - Return a relative rank for floating point types.
674/// This routine will assert if passed a built-in type that isn't a float.
675static int getFloatingRank(QualType T) {
676 T = T.getCanonicalType();
677 if (ComplexType *CT = dyn_cast<ComplexType>(T))
678 return getFloatingRank(CT->getElementType());
679
680 switch (cast<BuiltinType>(T)->getKind()) {
681 default: assert(0 && "getFloatingPointRank(): not a floating type");
682 case BuiltinType::Float: return FloatRank;
683 case BuiltinType::Double: return DoubleRank;
684 case BuiltinType::LongDouble: return LongDoubleRank;
685 }
686}
687
Steve Naroff716c7302007-08-27 01:41:48 +0000688/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
689/// point or a complex type (based on typeDomain/typeSize).
690/// 'typeDomain' is a real floating point or complex type.
691/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +0000692QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
693 QualType typeSize, QualType typeDomain) const {
694 if (typeDomain->isComplexType()) {
695 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000696 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000697 case FloatRank: return FloatComplexTy;
698 case DoubleRank: return DoubleComplexTy;
699 case LongDoubleRank: return LongDoubleComplexTy;
700 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 }
Steve Narofff1448a02007-08-27 01:27:54 +0000702 if (typeDomain->isRealFloatingType()) {
703 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000704 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000705 case FloatRank: return FloatTy;
706 case DoubleRank: return DoubleTy;
707 case LongDoubleRank: return LongDoubleTy;
708 }
709 }
710 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Reid Spencer5f016e22007-07-11 17:01:13 +0000711}
712
713// maxFloatingType - handles the simple case, both operands are floats.
714QualType ASTContext::maxFloatingType(QualType lt, QualType rt) {
715 return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt;
716}
717
718// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
719// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
720QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
721 if (lhs == rhs) return lhs;
722
723 bool t1Unsigned = lhs->isUnsignedIntegerType();
724 bool t2Unsigned = rhs->isUnsignedIntegerType();
725
726 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
727 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
728
729 // We have two integer types with differing signs
730 QualType unsignedType = t1Unsigned ? lhs : rhs;
731 QualType signedType = t1Unsigned ? rhs : lhs;
732
733 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
734 return unsignedType;
735 else {
736 // FIXME: Need to check if the signed type can represent all values of the
737 // unsigned type. If it can, then the result is the signed type.
738 // If it can't, then the result is the unsigned version of the signed type.
739 // Should probably add a helper that returns a signed integer type from
740 // an unsigned (and vice versa). C99 6.3.1.8.
741 return signedType;
742 }
743}
Anders Carlsson71993dd2007-08-17 05:31:46 +0000744
745// getCFConstantStringType - Return the type used for constant CFStrings.
746QualType ASTContext::getCFConstantStringType() {
747 if (!CFConstantStringTypeDecl) {
748 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
749 &Idents.get("__builtin_CFString"),
750 0);
751
752 QualType FieldTypes[4];
753
754 // const int *isa;
755 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
756 // int flags;
757 FieldTypes[1] = IntTy;
758 // const char *str;
759 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
760 // long length;
761 FieldTypes[3] = LongTy;
762 // Create fields
763 FieldDecl *FieldDecls[4];
764
765 for (unsigned i = 0; i < 4; ++i)
766 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i], 0);
767
768 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
769 }
770
771 return getTagDeclType(CFConstantStringTypeDecl);
772}