blob: e2447b972a93623150f74e719c4e27256d1dfea2 [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
395/// convertToVectorType - Return the unique reference to a vector type of
396/// the specified element type and size. VectorType can be a pointer, array,
397/// function, or built-in type (i.e. _Bool, integer, or float).
398QualType ASTContext::convertToVectorType(QualType vecType, unsigned NumElts) {
399 BuiltinType *baseType;
400
401 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
402 assert(baseType != 0 &&
403 "convertToVectorType(): Complex vector types unimplemented");
404
405 // Check if we've already instantiated a vector of this type.
406 llvm::FoldingSetNodeID ID;
407 VectorType::Profile(ID, vecType, NumElts);
408 void *InsertPos = 0;
409 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
410 return QualType(VTP, 0);
411
412 // If the element type isn't canonical, this won't be a canonical type either,
413 // so fill in the canonical type field.
414 QualType Canonical;
415 if (!vecType->isCanonical()) {
416 Canonical = convertToVectorType(vecType.getCanonicalType(), NumElts);
417
418 // Get the new insert position for the node we care about.
419 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
420 assert(NewIP == 0 && "Shouldn't be in the map!");
421 }
422 VectorType *New = new VectorType(vecType, NumElts, Canonical);
423 VectorTypes.InsertNode(New, InsertPos);
424 Types.push_back(New);
425 return QualType(New, 0);
426}
427
428/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
429///
430QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
431 // Unique functions, to guarantee there is only one function of a particular
432 // structure.
433 llvm::FoldingSetNodeID ID;
434 FunctionTypeNoProto::Profile(ID, ResultTy);
435
436 void *InsertPos = 0;
437 if (FunctionTypeNoProto *FT =
438 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
439 return QualType(FT, 0);
440
441 QualType Canonical;
442 if (!ResultTy->isCanonical()) {
443 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
444
445 // Get the new insert position for the node we care about.
446 FunctionTypeNoProto *NewIP =
447 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
448 assert(NewIP == 0 && "Shouldn't be in the map!");
449 }
450
451 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
452 Types.push_back(New);
453 FunctionTypeProtos.InsertNode(New, InsertPos);
454 return QualType(New, 0);
455}
456
457/// getFunctionType - Return a normal function type with a typed argument
458/// list. isVariadic indicates whether the argument list includes '...'.
459QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
460 unsigned NumArgs, bool isVariadic) {
461 // Unique functions, to guarantee there is only one function of a particular
462 // structure.
463 llvm::FoldingSetNodeID ID;
464 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
465
466 void *InsertPos = 0;
467 if (FunctionTypeProto *FTP =
468 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
469 return QualType(FTP, 0);
470
471 // Determine whether the type being created is already canonical or not.
472 bool isCanonical = ResultTy->isCanonical();
473 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
474 if (!ArgArray[i]->isCanonical())
475 isCanonical = false;
476
477 // If this type isn't canonical, get the canonical version of it.
478 QualType Canonical;
479 if (!isCanonical) {
480 llvm::SmallVector<QualType, 16> CanonicalArgs;
481 CanonicalArgs.reserve(NumArgs);
482 for (unsigned i = 0; i != NumArgs; ++i)
483 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
484
485 Canonical = getFunctionType(ResultTy.getCanonicalType(),
486 &CanonicalArgs[0], NumArgs,
487 isVariadic);
488
489 // Get the new insert position for the node we care about.
490 FunctionTypeProto *NewIP =
491 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
492 assert(NewIP == 0 && "Shouldn't be in the map!");
493 }
494
495 // FunctionTypeProto objects are not allocated with new because they have a
496 // variable size array (for parameter types) at the end of them.
497 FunctionTypeProto *FTP =
498 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
499 (NumArgs-1)*sizeof(QualType));
500 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
501 Canonical);
502 Types.push_back(FTP);
503 FunctionTypeProtos.InsertNode(FTP, InsertPos);
504 return QualType(FTP, 0);
505}
506
507/// getTypedefType - Return the unique reference to the type for the
508/// specified typename decl.
509QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
510 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
511
512 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
513 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
514 Types.push_back(Decl->TypeForDecl);
515 return QualType(Decl->TypeForDecl, 0);
516}
517
518/// getTagDeclType - Return the unique reference to the type for the
519/// specified TagDecl (struct/union/class/enum) decl.
520QualType ASTContext::getTagDeclType(TagDecl *Decl) {
521 // The decl stores the type cache.
522 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
523
524 Decl->TypeForDecl = new TagType(Decl, QualType());
525 Types.push_back(Decl->TypeForDecl);
526 return QualType(Decl->TypeForDecl, 0);
527}
528
529/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
530/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
531/// needs to agree with the definition in <stddef.h>.
532QualType ASTContext::getSizeType() const {
533 // On Darwin, size_t is defined as a "long unsigned int".
534 // FIXME: should derive from "Target".
535 return UnsignedLongTy;
536}
537
Chris Lattner8b9023b2007-07-13 03:05:23 +0000538/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
539/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
540QualType ASTContext::getPointerDiffType() const {
541 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
542 // FIXME: should derive from "Target".
543 return IntTy;
544}
545
Reid Spencer5f016e22007-07-11 17:01:13 +0000546/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
547/// routine will assert if passed a built-in type that isn't an integer or enum.
548static int getIntegerRank(QualType t) {
549 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
550 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
551 return 4;
552 }
553
554 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
555 switch (BT->getKind()) {
556 default:
557 assert(0 && "getIntegerRank(): not a built-in integer");
558 case BuiltinType::Bool:
559 return 1;
560 case BuiltinType::Char_S:
561 case BuiltinType::Char_U:
562 case BuiltinType::SChar:
563 case BuiltinType::UChar:
564 return 2;
565 case BuiltinType::Short:
566 case BuiltinType::UShort:
567 return 3;
568 case BuiltinType::Int:
569 case BuiltinType::UInt:
570 return 4;
571 case BuiltinType::Long:
572 case BuiltinType::ULong:
573 return 5;
574 case BuiltinType::LongLong:
575 case BuiltinType::ULongLong:
576 return 6;
577 }
578}
579
580/// getFloatingRank - Return a relative rank for floating point types.
581/// This routine will assert if passed a built-in type that isn't a float.
582static int getFloatingRank(QualType T) {
583 T = T.getCanonicalType();
584 if (ComplexType *CT = dyn_cast<ComplexType>(T))
585 return getFloatingRank(CT->getElementType());
586
587 switch (cast<BuiltinType>(T)->getKind()) {
588 default: assert(0 && "getFloatingPointRank(): not a floating type");
589 case BuiltinType::Float: return FloatRank;
590 case BuiltinType::Double: return DoubleRank;
591 case BuiltinType::LongDouble: return LongDoubleRank;
592 }
593}
594
595// maxComplexType - the following code handles 3 different combinations:
596// complex/complex, complex/float, float/complex.
597// When both operands are complex, the shorter operand is converted to the
598// type of the longer, and that is the type of the result. This corresponds
599// to what is done when combining two real floating-point operands.
600// The fun begins when size promotion occur across type domains. g
601// getFloatingRank & convertFloatingRankToComplexType handle this without
602// enumerating all permutations.
603// It also allows us to add new types without breakage.
604// From H&S 6.3.4: When one operand is complex and the other is a real
605// floating-point type, the less precise type is converted, within it's
606// real or complex domain, to the precision of the other type. For example,
607// when combining a "long double" with a "double _Complex", the
608// "double _Complex" is promoted to "long double _Complex".
609
610QualType ASTContext::maxComplexType(QualType lt, QualType rt) const {
611 switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) {
612 default: assert(0 && "convertRankToComplex(): illegal value for rank");
613 case FloatRank: return FloatComplexTy;
614 case DoubleRank: return DoubleComplexTy;
615 case LongDoubleRank: return LongDoubleComplexTy;
616 }
617}
618
619// maxFloatingType - handles the simple case, both operands are floats.
620QualType ASTContext::maxFloatingType(QualType lt, QualType rt) {
621 return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt;
622}
623
624// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
625// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
626QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
627 if (lhs == rhs) return lhs;
628
629 bool t1Unsigned = lhs->isUnsignedIntegerType();
630 bool t2Unsigned = rhs->isUnsignedIntegerType();
631
632 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
633 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
634
635 // We have two integer types with differing signs
636 QualType unsignedType = t1Unsigned ? lhs : rhs;
637 QualType signedType = t1Unsigned ? rhs : lhs;
638
639 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
640 return unsignedType;
641 else {
642 // FIXME: Need to check if the signed type can represent all values of the
643 // unsigned type. If it can, then the result is the signed type.
644 // If it can't, then the result is the unsigned version of the signed type.
645 // Should probably add a helper that returns a signed integer type from
646 // an unsigned (and vice versa). C99 6.3.1.8.
647 return signedType;
648 }
649}