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