blob: bc737483d2b6d28f0ce19c4cb48ef6a6e940234a [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 Lattner6cd862c2007-08-27 17:38:00 +0000230 TagType *TT = cast<TagType>(T);
231 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
232 const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L);
233 Size = Layout.getSize();
234 Align = Layout.getAlignment();
235 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
236 return getTypeInfo(getEnumDeclIntegerType(ED), L);
237 } else {
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000238 assert(0 && "Unimplemented type sizes!");
Chris Lattner6cd862c2007-08-27 17:38:00 +0000239 }
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000240 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000241 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000242
Chris Lattner464175b2007-07-18 17:52:12 +0000243 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000244 return std::make_pair(Size, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000245}
246
Chris Lattner464175b2007-07-18 17:52:12 +0000247/// getRecordLayout - Get or compute information about the layout of the
248/// specified record (struct/union/class), which indicates its size and field
249/// position information.
250const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D,
251 SourceLocation L) {
252 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
253
254 // Look up this layout, if already laid out, return what we have.
255 const RecordLayout *&Entry = RecordLayoutInfo[D];
256 if (Entry) return *Entry;
257
258 // Allocate and assign into RecordLayoutInfo here. The "Entry" reference can
259 // be invalidated (dangle) if the RecordLayoutInfo hashtable is inserted into.
260 RecordLayout *NewEntry = new RecordLayout();
261 Entry = NewEntry;
262
263 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
264 uint64_t RecordSize = 0;
265 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
266
267 if (D->getKind() != Decl::Union) {
268 // Layout each field, for now, just sequentially, respecting alignment. In
269 // the future, this will need to be tweakable by targets.
270 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
271 const FieldDecl *FD = D->getMember(i);
272 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
273 uint64_t FieldSize = FieldInfo.first;
274 unsigned FieldAlign = FieldInfo.second;
275
276 // Round up the current record size to the field's alignment boundary.
277 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
278
279 // Place this field at the current location.
280 FieldOffsets[i] = RecordSize;
281
282 // Reserve space for this field.
283 RecordSize += FieldSize;
284
285 // Remember max struct/class alignment.
286 RecordAlign = std::max(RecordAlign, FieldAlign);
287 }
288
289 // Finally, round the size of the total struct up to the alignment of the
290 // struct itself.
291 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
292 } else {
293 // Union layout just puts each member at the start of the record.
294 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
295 const FieldDecl *FD = D->getMember(i);
296 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
297 uint64_t FieldSize = FieldInfo.first;
298 unsigned FieldAlign = FieldInfo.second;
299
300 // Round up the current record size to the field's alignment boundary.
301 RecordSize = std::max(RecordSize, FieldSize);
302
303 // Place this field at the start of the record.
304 FieldOffsets[i] = 0;
305
306 // Remember max struct/class alignment.
307 RecordAlign = std::max(RecordAlign, FieldAlign);
308 }
309 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000310
311 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
312 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000313}
314
Chris Lattner6cd862c2007-08-27 17:38:00 +0000315/// getEnumDeclIntegerType - returns the integer type compatible with the
316/// given enum type.
Chris Lattnerfb071532007-08-27 17:56:57 +0000317QualType ASTContext::getEnumDeclIntegerType(const EnumDecl *ED) const {
318 if (const EnumConstantDecl *C = ED->getEnumConstantList())
Chris Lattner6cd862c2007-08-27 17:38:00 +0000319 return C->getType();
320
321 // If the enum list is empty, it is typed as if it contained a single zero
322 // element [C++ dcl.enum] and is illegal in C (as an extension, we treat it
323 // the same as C++ does).
324 switch (Target.getEnumTypePolicy(ED->getLocation())) {
325 default: assert(0 && "Unknown enum layout policy");
326 case TargetInfo::AlwaysInt: return UnsignedIntTy; // 0 -> unsigned
327 case TargetInfo::ShortestType: return UnsignedCharTy; // 0 -> unsigned char
328 }
329}
330
Chris Lattner464175b2007-07-18 17:52:12 +0000331
Chris Lattnera7674d82007-07-13 22:13:22 +0000332//===----------------------------------------------------------------------===//
333// Type creation/memoization methods
334//===----------------------------------------------------------------------===//
335
336
Reid Spencer5f016e22007-07-11 17:01:13 +0000337/// getComplexType - Return the uniqued reference to the type for a complex
338/// number with the specified element type.
339QualType ASTContext::getComplexType(QualType T) {
340 // Unique pointers, to guarantee there is only one pointer of a particular
341 // structure.
342 llvm::FoldingSetNodeID ID;
343 ComplexType::Profile(ID, T);
344
345 void *InsertPos = 0;
346 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
347 return QualType(CT, 0);
348
349 // If the pointee type isn't canonical, this won't be a canonical type either,
350 // so fill in the canonical type field.
351 QualType Canonical;
352 if (!T->isCanonical()) {
353 Canonical = getComplexType(T.getCanonicalType());
354
355 // Get the new insert position for the node we care about.
356 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
357 assert(NewIP == 0 && "Shouldn't be in the map!");
358 }
359 ComplexType *New = new ComplexType(T, Canonical);
360 Types.push_back(New);
361 ComplexTypes.InsertNode(New, InsertPos);
362 return QualType(New, 0);
363}
364
365
366/// getPointerType - Return the uniqued reference to the type for a pointer to
367/// the specified type.
368QualType ASTContext::getPointerType(QualType T) {
369 // Unique pointers, to guarantee there is only one pointer of a particular
370 // structure.
371 llvm::FoldingSetNodeID ID;
372 PointerType::Profile(ID, T);
373
374 void *InsertPos = 0;
375 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
376 return QualType(PT, 0);
377
378 // If the pointee type isn't canonical, this won't be a canonical type either,
379 // so fill in the canonical type field.
380 QualType Canonical;
381 if (!T->isCanonical()) {
382 Canonical = getPointerType(T.getCanonicalType());
383
384 // Get the new insert position for the node we care about.
385 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
386 assert(NewIP == 0 && "Shouldn't be in the map!");
387 }
388 PointerType *New = new PointerType(T, Canonical);
389 Types.push_back(New);
390 PointerTypes.InsertNode(New, InsertPos);
391 return QualType(New, 0);
392}
393
394/// getReferenceType - Return the uniqued reference to the type for a reference
395/// to the specified type.
396QualType ASTContext::getReferenceType(QualType T) {
397 // Unique pointers, to guarantee there is only one pointer of a particular
398 // structure.
399 llvm::FoldingSetNodeID ID;
400 ReferenceType::Profile(ID, T);
401
402 void *InsertPos = 0;
403 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
404 return QualType(RT, 0);
405
406 // If the referencee type isn't canonical, this won't be a canonical type
407 // either, so fill in the canonical type field.
408 QualType Canonical;
409 if (!T->isCanonical()) {
410 Canonical = getReferenceType(T.getCanonicalType());
411
412 // Get the new insert position for the node we care about.
413 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
414 assert(NewIP == 0 && "Shouldn't be in the map!");
415 }
416
417 ReferenceType *New = new ReferenceType(T, Canonical);
418 Types.push_back(New);
419 ReferenceTypes.InsertNode(New, InsertPos);
420 return QualType(New, 0);
421}
422
423/// getArrayType - Return the unique reference to the type for an array of the
424/// specified element type.
425QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
426 unsigned EltTypeQuals, Expr *NumElts) {
427 // Unique array types, to guarantee there is only one array of a particular
428 // structure.
429 llvm::FoldingSetNodeID ID;
430 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
431
432 void *InsertPos = 0;
433 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
434 return QualType(ATP, 0);
435
436 // If the element type isn't canonical, this won't be a canonical type either,
437 // so fill in the canonical type field.
438 QualType Canonical;
439 if (!EltTy->isCanonical()) {
440 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
441 NumElts);
442
443 // Get the new insert position for the node we care about.
444 ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
445 assert(NewIP == 0 && "Shouldn't be in the map!");
446 }
447
448 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
449 ArrayTypes.InsertNode(New, InsertPos);
450 Types.push_back(New);
451 return QualType(New, 0);
452}
453
Steve Naroff73322922007-07-18 18:00:27 +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) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000457 BuiltinType *baseType;
458
459 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000460 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000461
462 // Check if we've already instantiated a vector of this type.
463 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000464 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 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()) {
Steve Naroff73322922007-07-18 18:00:27 +0000473 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000474
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
Steve Naroff73322922007-07-18 18:00:27 +0000485/// 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
Reid Spencer5f016e22007-07-11 17:01:13 +0000516/// 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) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000587 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 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 Naroff9752f252007-08-01 18:02:17 +0000606/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
607/// TypeOfExpr AST's (since expression's are never shared). For example,
608/// multiple declarations that refer to "typeof(x)" all contain different
609/// DeclRefExpr's. This doesn't effect the type checker, since it operates
610/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000611QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffd1861fd2007-07-31 12:34:36 +0000612 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000613 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
614 Types.push_back(toe);
615 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000616}
617
Steve Naroff9752f252007-08-01 18:02:17 +0000618/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
619/// TypeOfType AST's. The only motivation to unique these nodes would be
620/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
621/// an issue. This doesn't effect the type checker, since it operates
622/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000623QualType ASTContext::getTypeOfType(QualType tofType) {
624 QualType Canonical = tofType.getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000625 TypeOfType *tot = new TypeOfType(tofType, Canonical);
626 Types.push_back(tot);
627 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000628}
629
Reid Spencer5f016e22007-07-11 17:01:13 +0000630/// getTagDeclType - Return the unique reference to the type for the
631/// specified TagDecl (struct/union/class/enum) decl.
632QualType ASTContext::getTagDeclType(TagDecl *Decl) {
633 // The decl stores the type cache.
634 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
635
636 Decl->TypeForDecl = new TagType(Decl, QualType());
637 Types.push_back(Decl->TypeForDecl);
638 return QualType(Decl->TypeForDecl, 0);
639}
640
641/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
642/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
643/// needs to agree with the definition in <stddef.h>.
644QualType ASTContext::getSizeType() const {
645 // On Darwin, size_t is defined as a "long unsigned int".
646 // FIXME: should derive from "Target".
647 return UnsignedLongTy;
648}
649
Chris Lattner8b9023b2007-07-13 03:05:23 +0000650/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
651/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
652QualType ASTContext::getPointerDiffType() const {
653 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
654 // FIXME: should derive from "Target".
655 return IntTy;
656}
657
Reid Spencer5f016e22007-07-11 17:01:13 +0000658/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
659/// routine will assert if passed a built-in type that isn't an integer or enum.
660static int getIntegerRank(QualType t) {
661 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
662 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
663 return 4;
664 }
665
666 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
667 switch (BT->getKind()) {
668 default:
669 assert(0 && "getIntegerRank(): not a built-in integer");
670 case BuiltinType::Bool:
671 return 1;
672 case BuiltinType::Char_S:
673 case BuiltinType::Char_U:
674 case BuiltinType::SChar:
675 case BuiltinType::UChar:
676 return 2;
677 case BuiltinType::Short:
678 case BuiltinType::UShort:
679 return 3;
680 case BuiltinType::Int:
681 case BuiltinType::UInt:
682 return 4;
683 case BuiltinType::Long:
684 case BuiltinType::ULong:
685 return 5;
686 case BuiltinType::LongLong:
687 case BuiltinType::ULongLong:
688 return 6;
689 }
690}
691
692/// getFloatingRank - Return a relative rank for floating point types.
693/// This routine will assert if passed a built-in type that isn't a float.
694static int getFloatingRank(QualType T) {
695 T = T.getCanonicalType();
696 if (ComplexType *CT = dyn_cast<ComplexType>(T))
697 return getFloatingRank(CT->getElementType());
698
699 switch (cast<BuiltinType>(T)->getKind()) {
700 default: assert(0 && "getFloatingPointRank(): not a floating type");
701 case BuiltinType::Float: return FloatRank;
702 case BuiltinType::Double: return DoubleRank;
703 case BuiltinType::LongDouble: return LongDoubleRank;
704 }
705}
706
Steve Naroff716c7302007-08-27 01:41:48 +0000707/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
708/// point or a complex type (based on typeDomain/typeSize).
709/// 'typeDomain' is a real floating point or complex type.
710/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +0000711QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
712 QualType typeSize, QualType typeDomain) const {
713 if (typeDomain->isComplexType()) {
714 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000715 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000716 case FloatRank: return FloatComplexTy;
717 case DoubleRank: return DoubleComplexTy;
718 case LongDoubleRank: return LongDoubleComplexTy;
719 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 }
Steve Narofff1448a02007-08-27 01:27:54 +0000721 if (typeDomain->isRealFloatingType()) {
722 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000723 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000724 case FloatRank: return FloatTy;
725 case DoubleRank: return DoubleTy;
726 case LongDoubleRank: return LongDoubleTy;
727 }
728 }
729 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Reid Spencer5f016e22007-07-11 17:01:13 +0000730}
731
Steve Narofffb0d4962007-08-27 15:30:22 +0000732/// compareFloatingType - Handles 3 different combos:
733/// float/float, float/complex, complex/complex.
734/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
735int ASTContext::compareFloatingType(QualType lt, QualType rt) {
736 if (getFloatingRank(lt) == getFloatingRank(rt))
737 return 0;
738 if (getFloatingRank(lt) > getFloatingRank(rt))
739 return 1;
740 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000741}
742
743// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
744// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
745QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
746 if (lhs == rhs) return lhs;
747
748 bool t1Unsigned = lhs->isUnsignedIntegerType();
749 bool t2Unsigned = rhs->isUnsignedIntegerType();
750
751 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
752 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
753
754 // We have two integer types with differing signs
755 QualType unsignedType = t1Unsigned ? lhs : rhs;
756 QualType signedType = t1Unsigned ? rhs : lhs;
757
758 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
759 return unsignedType;
760 else {
761 // FIXME: Need to check if the signed type can represent all values of the
762 // unsigned type. If it can, then the result is the signed type.
763 // If it can't, then the result is the unsigned version of the signed type.
764 // Should probably add a helper that returns a signed integer type from
765 // an unsigned (and vice versa). C99 6.3.1.8.
766 return signedType;
767 }
768}
Anders Carlsson71993dd2007-08-17 05:31:46 +0000769
770// getCFConstantStringType - Return the type used for constant CFStrings.
771QualType ASTContext::getCFConstantStringType() {
772 if (!CFConstantStringTypeDecl) {
773 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
774 &Idents.get("__builtin_CFString"),
775 0);
776
777 QualType FieldTypes[4];
778
779 // const int *isa;
780 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
781 // int flags;
782 FieldTypes[1] = IntTy;
783 // const char *str;
784 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
785 // long length;
786 FieldTypes[3] = LongTy;
787 // Create fields
788 FieldDecl *FieldDecls[4];
789
790 for (unsigned i = 0; i < 4; ++i)
791 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i], 0);
792
793 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
794 }
795
796 return getTagDeclType(CFConstantStringTypeDecl);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000797}