blob: f45051bf3659dfc6142939dfb23909f7cdb28d78 [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 Lattner692233e2007-07-13 22:27:08 +0000160 case Type::FunctionNoProto:
161 case Type::FunctionProto:
162 assert(0 && "Incomplete types have no size!");
Chris Lattner5d2a6302007-07-18 18:26:58 +0000163 default:
164 case Type::Array:
165 case Type::Vector:
166 case Type::TypeName:
167 assert(0 && "Unimplemented type sizes!");
168
Chris Lattnera7674d82007-07-13 22:13:22 +0000169 case Type::Builtin: {
170 // FIXME: need to use TargetInfo to derive the target specific sizes. This
171 // implementation will suffice for play with vector support.
172 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000173 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000174 case BuiltinType::Void:
175 assert(0 && "Incomplete types have no size!");
176 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000177 case BuiltinType::Char_S:
178 case BuiltinType::Char_U:
179 case BuiltinType::UChar:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000180 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000181 case BuiltinType::UShort:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000182 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000183 case BuiltinType::UInt:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000184 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000185 case BuiltinType::ULong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000186 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000187 case BuiltinType::ULongLong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000188 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
189 case BuiltinType::Float: Target.getFloatInfo(Size, Align, L); break;
190 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, L); break;
191 case BuiltinType::LongDouble: Target.getLongDoubleInfo(Size, Align,L);break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000192 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000193 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000194 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000195 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000196 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000197 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000198 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
199 // FIXME: This is wrong for struct layout!
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000200 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000201
202 case Type::Complex: {
203 // Complex types have the same alignment as their elements, but twice the
204 // size.
205 std::pair<uint64_t, unsigned> EltInfo =
206 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
207 Size = EltInfo.first*2;
208 Align = EltInfo.second;
209 break;
210 }
211 case Type::Tagged:
212 if (RecordType *RT = dyn_cast<RecordType>(cast<TagType>(T))) {
213 const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L);
214 Size = Layout.getSize();
215 Align = Layout.getAlignment();
216 break;
217 }
218 // FIXME: Handle enums.
219 assert(0 && "Unimplemented type sizes!");
Chris Lattnera7674d82007-07-13 22:13:22 +0000220 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000221
Chris Lattner464175b2007-07-18 17:52:12 +0000222 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000223 return std::make_pair(Size, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000224}
225
Chris Lattner464175b2007-07-18 17:52:12 +0000226/// getRecordLayout - Get or compute information about the layout of the
227/// specified record (struct/union/class), which indicates its size and field
228/// position information.
229const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D,
230 SourceLocation L) {
231 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
232
233 // Look up this layout, if already laid out, return what we have.
234 const RecordLayout *&Entry = RecordLayoutInfo[D];
235 if (Entry) return *Entry;
236
237 // Allocate and assign into RecordLayoutInfo here. The "Entry" reference can
238 // be invalidated (dangle) if the RecordLayoutInfo hashtable is inserted into.
239 RecordLayout *NewEntry = new RecordLayout();
240 Entry = NewEntry;
241
242 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
243 uint64_t RecordSize = 0;
244 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
245
246 if (D->getKind() != Decl::Union) {
247 // Layout each field, for now, just sequentially, respecting alignment. In
248 // the future, this will need to be tweakable by targets.
249 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
250 const FieldDecl *FD = D->getMember(i);
251 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
252 uint64_t FieldSize = FieldInfo.first;
253 unsigned FieldAlign = FieldInfo.second;
254
255 // Round up the current record size to the field's alignment boundary.
256 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
257
258 // Place this field at the current location.
259 FieldOffsets[i] = RecordSize;
260
261 // Reserve space for this field.
262 RecordSize += FieldSize;
263
264 // Remember max struct/class alignment.
265 RecordAlign = std::max(RecordAlign, FieldAlign);
266 }
267
268 // Finally, round the size of the total struct up to the alignment of the
269 // struct itself.
270 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
271 } else {
272 // Union layout just puts each member at the start of the record.
273 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
274 const FieldDecl *FD = D->getMember(i);
275 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
276 uint64_t FieldSize = FieldInfo.first;
277 unsigned FieldAlign = FieldInfo.second;
278
279 // Round up the current record size to the field's alignment boundary.
280 RecordSize = std::max(RecordSize, FieldSize);
281
282 // Place this field at the start of the record.
283 FieldOffsets[i] = 0;
284
285 // Remember max struct/class alignment.
286 RecordAlign = std::max(RecordAlign, FieldAlign);
287 }
288 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000289
290 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
291 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000292}
293
294
Chris Lattnera7674d82007-07-13 22:13:22 +0000295//===----------------------------------------------------------------------===//
296// Type creation/memoization methods
297//===----------------------------------------------------------------------===//
298
299
Reid Spencer5f016e22007-07-11 17:01:13 +0000300/// getComplexType - Return the uniqued reference to the type for a complex
301/// number with the specified element type.
302QualType ASTContext::getComplexType(QualType T) {
303 // Unique pointers, to guarantee there is only one pointer of a particular
304 // structure.
305 llvm::FoldingSetNodeID ID;
306 ComplexType::Profile(ID, T);
307
308 void *InsertPos = 0;
309 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
310 return QualType(CT, 0);
311
312 // If the pointee type isn't canonical, this won't be a canonical type either,
313 // so fill in the canonical type field.
314 QualType Canonical;
315 if (!T->isCanonical()) {
316 Canonical = getComplexType(T.getCanonicalType());
317
318 // Get the new insert position for the node we care about.
319 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
320 assert(NewIP == 0 && "Shouldn't be in the map!");
321 }
322 ComplexType *New = new ComplexType(T, Canonical);
323 Types.push_back(New);
324 ComplexTypes.InsertNode(New, InsertPos);
325 return QualType(New, 0);
326}
327
328
329/// getPointerType - Return the uniqued reference to the type for a pointer to
330/// the specified type.
331QualType ASTContext::getPointerType(QualType T) {
332 // Unique pointers, to guarantee there is only one pointer of a particular
333 // structure.
334 llvm::FoldingSetNodeID ID;
335 PointerType::Profile(ID, T);
336
337 void *InsertPos = 0;
338 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
339 return QualType(PT, 0);
340
341 // If the pointee type isn't canonical, this won't be a canonical type either,
342 // so fill in the canonical type field.
343 QualType Canonical;
344 if (!T->isCanonical()) {
345 Canonical = getPointerType(T.getCanonicalType());
346
347 // Get the new insert position for the node we care about.
348 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
349 assert(NewIP == 0 && "Shouldn't be in the map!");
350 }
351 PointerType *New = new PointerType(T, Canonical);
352 Types.push_back(New);
353 PointerTypes.InsertNode(New, InsertPos);
354 return QualType(New, 0);
355}
356
357/// getReferenceType - Return the uniqued reference to the type for a reference
358/// to the specified type.
359QualType ASTContext::getReferenceType(QualType T) {
360 // Unique pointers, to guarantee there is only one pointer of a particular
361 // structure.
362 llvm::FoldingSetNodeID ID;
363 ReferenceType::Profile(ID, T);
364
365 void *InsertPos = 0;
366 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
367 return QualType(RT, 0);
368
369 // If the referencee type isn't canonical, this won't be a canonical type
370 // either, so fill in the canonical type field.
371 QualType Canonical;
372 if (!T->isCanonical()) {
373 Canonical = getReferenceType(T.getCanonicalType());
374
375 // Get the new insert position for the node we care about.
376 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
377 assert(NewIP == 0 && "Shouldn't be in the map!");
378 }
379
380 ReferenceType *New = new ReferenceType(T, Canonical);
381 Types.push_back(New);
382 ReferenceTypes.InsertNode(New, InsertPos);
383 return QualType(New, 0);
384}
385
386/// getArrayType - Return the unique reference to the type for an array of the
387/// specified element type.
388QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
389 unsigned EltTypeQuals, Expr *NumElts) {
390 // Unique array types, to guarantee there is only one array of a particular
391 // structure.
392 llvm::FoldingSetNodeID ID;
393 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
394
395 void *InsertPos = 0;
396 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
397 return QualType(ATP, 0);
398
399 // If the element type isn't canonical, this won't be a canonical type either,
400 // so fill in the canonical type field.
401 QualType Canonical;
402 if (!EltTy->isCanonical()) {
403 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
404 NumElts);
405
406 // Get the new insert position for the node we care about.
407 ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
408 assert(NewIP == 0 && "Shouldn't be in the map!");
409 }
410
411 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
412 ArrayTypes.InsertNode(New, InsertPos);
413 Types.push_back(New);
414 return QualType(New, 0);
415}
416
Steve Naroff73322922007-07-18 18:00:27 +0000417/// getVectorType - Return the unique reference to a vector type of
418/// the specified element type and size. VectorType must be a built-in type.
419QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000420 BuiltinType *baseType;
421
422 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000423 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000424
425 // Check if we've already instantiated a vector of this type.
426 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000427 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000428 void *InsertPos = 0;
429 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
430 return QualType(VTP, 0);
431
432 // If the element type isn't canonical, this won't be a canonical type either,
433 // so fill in the canonical type field.
434 QualType Canonical;
435 if (!vecType->isCanonical()) {
Steve Naroff73322922007-07-18 18:00:27 +0000436 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000437
438 // Get the new insert position for the node we care about.
439 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
440 assert(NewIP == 0 && "Shouldn't be in the map!");
441 }
442 VectorType *New = new VectorType(vecType, NumElts, Canonical);
443 VectorTypes.InsertNode(New, InsertPos);
444 Types.push_back(New);
445 return QualType(New, 0);
446}
447
Steve Naroff73322922007-07-18 18:00:27 +0000448/// getOCUVectorType - Return the unique reference to an OCU vector type of
449/// the specified element type and size. VectorType must be a built-in type.
450QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
451 BuiltinType *baseType;
452
453 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
454 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
455
456 // Check if we've already instantiated a vector of this type.
457 llvm::FoldingSetNodeID ID;
458 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
459 void *InsertPos = 0;
460 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
461 return QualType(VTP, 0);
462
463 // If the element type isn't canonical, this won't be a canonical type either,
464 // so fill in the canonical type field.
465 QualType Canonical;
466 if (!vecType->isCanonical()) {
467 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
468
469 // Get the new insert position for the node we care about.
470 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
471 assert(NewIP == 0 && "Shouldn't be in the map!");
472 }
473 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
474 VectorTypes.InsertNode(New, InsertPos);
475 Types.push_back(New);
476 return QualType(New, 0);
477}
478
Reid Spencer5f016e22007-07-11 17:01:13 +0000479/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
480///
481QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
482 // Unique functions, to guarantee there is only one function of a particular
483 // structure.
484 llvm::FoldingSetNodeID ID;
485 FunctionTypeNoProto::Profile(ID, ResultTy);
486
487 void *InsertPos = 0;
488 if (FunctionTypeNoProto *FT =
489 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
490 return QualType(FT, 0);
491
492 QualType Canonical;
493 if (!ResultTy->isCanonical()) {
494 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
495
496 // Get the new insert position for the node we care about.
497 FunctionTypeNoProto *NewIP =
498 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
499 assert(NewIP == 0 && "Shouldn't be in the map!");
500 }
501
502 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
503 Types.push_back(New);
504 FunctionTypeProtos.InsertNode(New, InsertPos);
505 return QualType(New, 0);
506}
507
508/// getFunctionType - Return a normal function type with a typed argument
509/// list. isVariadic indicates whether the argument list includes '...'.
510QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
511 unsigned NumArgs, bool isVariadic) {
512 // Unique functions, to guarantee there is only one function of a particular
513 // structure.
514 llvm::FoldingSetNodeID ID;
515 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
516
517 void *InsertPos = 0;
518 if (FunctionTypeProto *FTP =
519 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
520 return QualType(FTP, 0);
521
522 // Determine whether the type being created is already canonical or not.
523 bool isCanonical = ResultTy->isCanonical();
524 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
525 if (!ArgArray[i]->isCanonical())
526 isCanonical = false;
527
528 // If this type isn't canonical, get the canonical version of it.
529 QualType Canonical;
530 if (!isCanonical) {
531 llvm::SmallVector<QualType, 16> CanonicalArgs;
532 CanonicalArgs.reserve(NumArgs);
533 for (unsigned i = 0; i != NumArgs; ++i)
534 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
535
536 Canonical = getFunctionType(ResultTy.getCanonicalType(),
537 &CanonicalArgs[0], NumArgs,
538 isVariadic);
539
540 // Get the new insert position for the node we care about.
541 FunctionTypeProto *NewIP =
542 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
543 assert(NewIP == 0 && "Shouldn't be in the map!");
544 }
545
546 // FunctionTypeProto objects are not allocated with new because they have a
547 // variable size array (for parameter types) at the end of them.
548 FunctionTypeProto *FTP =
549 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
550 (NumArgs-1)*sizeof(QualType));
551 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
552 Canonical);
553 Types.push_back(FTP);
554 FunctionTypeProtos.InsertNode(FTP, InsertPos);
555 return QualType(FTP, 0);
556}
557
558/// getTypedefType - Return the unique reference to the type for the
559/// specified typename decl.
560QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
561 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
562
563 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
564 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
565 Types.push_back(Decl->TypeForDecl);
566 return QualType(Decl->TypeForDecl, 0);
567}
568
569/// getTagDeclType - Return the unique reference to the type for the
570/// specified TagDecl (struct/union/class/enum) decl.
571QualType ASTContext::getTagDeclType(TagDecl *Decl) {
572 // The decl stores the type cache.
573 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
574
575 Decl->TypeForDecl = new TagType(Decl, QualType());
576 Types.push_back(Decl->TypeForDecl);
577 return QualType(Decl->TypeForDecl, 0);
578}
579
580/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
581/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
582/// needs to agree with the definition in <stddef.h>.
583QualType ASTContext::getSizeType() const {
584 // On Darwin, size_t is defined as a "long unsigned int".
585 // FIXME: should derive from "Target".
586 return UnsignedLongTy;
587}
588
Chris Lattner8b9023b2007-07-13 03:05:23 +0000589/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
590/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
591QualType ASTContext::getPointerDiffType() const {
592 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
593 // FIXME: should derive from "Target".
594 return IntTy;
595}
596
Reid Spencer5f016e22007-07-11 17:01:13 +0000597/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
598/// routine will assert if passed a built-in type that isn't an integer or enum.
599static int getIntegerRank(QualType t) {
600 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
601 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
602 return 4;
603 }
604
605 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
606 switch (BT->getKind()) {
607 default:
608 assert(0 && "getIntegerRank(): not a built-in integer");
609 case BuiltinType::Bool:
610 return 1;
611 case BuiltinType::Char_S:
612 case BuiltinType::Char_U:
613 case BuiltinType::SChar:
614 case BuiltinType::UChar:
615 return 2;
616 case BuiltinType::Short:
617 case BuiltinType::UShort:
618 return 3;
619 case BuiltinType::Int:
620 case BuiltinType::UInt:
621 return 4;
622 case BuiltinType::Long:
623 case BuiltinType::ULong:
624 return 5;
625 case BuiltinType::LongLong:
626 case BuiltinType::ULongLong:
627 return 6;
628 }
629}
630
631/// getFloatingRank - Return a relative rank for floating point types.
632/// This routine will assert if passed a built-in type that isn't a float.
633static int getFloatingRank(QualType T) {
634 T = T.getCanonicalType();
635 if (ComplexType *CT = dyn_cast<ComplexType>(T))
636 return getFloatingRank(CT->getElementType());
637
638 switch (cast<BuiltinType>(T)->getKind()) {
639 default: assert(0 && "getFloatingPointRank(): not a floating type");
640 case BuiltinType::Float: return FloatRank;
641 case BuiltinType::Double: return DoubleRank;
642 case BuiltinType::LongDouble: return LongDoubleRank;
643 }
644}
645
646// maxComplexType - the following code handles 3 different combinations:
647// complex/complex, complex/float, float/complex.
648// When both operands are complex, the shorter operand is converted to the
649// type of the longer, and that is the type of the result. This corresponds
650// to what is done when combining two real floating-point operands.
651// The fun begins when size promotion occur across type domains. g
652// getFloatingRank & convertFloatingRankToComplexType handle this without
653// enumerating all permutations.
654// It also allows us to add new types without breakage.
655// From H&S 6.3.4: When one operand is complex and the other is a real
656// floating-point type, the less precise type is converted, within it's
657// real or complex domain, to the precision of the other type. For example,
658// when combining a "long double" with a "double _Complex", the
659// "double _Complex" is promoted to "long double _Complex".
660
661QualType ASTContext::maxComplexType(QualType lt, QualType rt) const {
662 switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) {
663 default: assert(0 && "convertRankToComplex(): illegal value for rank");
664 case FloatRank: return FloatComplexTy;
665 case DoubleRank: return DoubleComplexTy;
666 case LongDoubleRank: return LongDoubleComplexTy;
667 }
668}
669
670// maxFloatingType - handles the simple case, both operands are floats.
671QualType ASTContext::maxFloatingType(QualType lt, QualType rt) {
672 return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt;
673}
674
675// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
676// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
677QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
678 if (lhs == rhs) return lhs;
679
680 bool t1Unsigned = lhs->isUnsignedIntegerType();
681 bool t2Unsigned = rhs->isUnsignedIntegerType();
682
683 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
684 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
685
686 // We have two integer types with differing signs
687 QualType unsignedType = t1Unsigned ? lhs : rhs;
688 QualType signedType = t1Unsigned ? rhs : lhs;
689
690 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
691 return unsignedType;
692 else {
693 // FIXME: Need to check if the signed type can represent all values of the
694 // unsigned type. If it can, then the result is the signed type.
695 // If it can't, then the result is the unsigned version of the signed type.
696 // Should probably add a helper that returns a signed integer type from
697 // an unsigned (and vice versa). C99 6.3.1.8.
698 return signedType;
699 }
700}