blob: 6e6849f0b240f5ac430b19f34a7cef384cafcff8 [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;
43 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
44
45 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
46
47 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
48 Type *T = Types[i];
49 if (isa<BuiltinType>(T))
50 ++NumBuiltin;
51 else if (isa<PointerType>(T))
52 ++NumPointer;
53 else if (isa<ReferenceType>(T))
54 ++NumReference;
55 else if (isa<ArrayType>(T))
56 ++NumArray;
57 else if (isa<FunctionTypeNoProto>(T))
58 ++NumFunctionNP;
59 else if (isa<FunctionTypeProto>(T))
60 ++NumFunctionP;
61 else if (isa<TypedefType>(T))
62 ++NumTypeName;
63 else if (TagType *TT = dyn_cast<TagType>(T)) {
64 ++NumTagged;
65 switch (TT->getDecl()->getKind()) {
66 default: assert(0 && "Unknown tagged type!");
67 case Decl::Struct: ++NumTagStruct; break;
68 case Decl::Union: ++NumTagUnion; break;
69 case Decl::Class: ++NumTagClass; break;
70 case Decl::Enum: ++NumTagEnum; break;
71 }
72 } else {
73 assert(0 && "Unknown type!");
74 }
75 }
76
77 fprintf(stderr, " %d builtin types\n", NumBuiltin);
78 fprintf(stderr, " %d pointer types\n", NumPointer);
79 fprintf(stderr, " %d reference types\n", NumReference);
80 fprintf(stderr, " %d array types\n", NumArray);
81 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
82 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
83 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
84 fprintf(stderr, " %d tagged types\n", NumTagged);
85 fprintf(stderr, " %d struct types\n", NumTagStruct);
86 fprintf(stderr, " %d union types\n", NumTagUnion);
87 fprintf(stderr, " %d class types\n", NumTagClass);
88 fprintf(stderr, " %d enum types\n", NumTagEnum);
89 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
90 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
91 NumFunctionP*sizeof(FunctionTypeProto)+
92 NumFunctionNP*sizeof(FunctionTypeNoProto)+
93 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
94}
95
96
97void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
98 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
99}
100
101
102void ASTContext::InitBuiltinTypes() {
103 assert(VoidTy.isNull() && "Context reinitialized?");
104
105 // C99 6.2.5p19.
106 InitBuiltinType(VoidTy, BuiltinType::Void);
107
108 // C99 6.2.5p2.
109 InitBuiltinType(BoolTy, BuiltinType::Bool);
110 // C99 6.2.5p3.
111 if (Target.isCharSigned(SourceLocation()))
112 InitBuiltinType(CharTy, BuiltinType::Char_S);
113 else
114 InitBuiltinType(CharTy, BuiltinType::Char_U);
115 // C99 6.2.5p4.
116 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
117 InitBuiltinType(ShortTy, BuiltinType::Short);
118 InitBuiltinType(IntTy, BuiltinType::Int);
119 InitBuiltinType(LongTy, BuiltinType::Long);
120 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
121
122 // C99 6.2.5p6.
123 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
124 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
125 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
126 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
127 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
128
129 // C99 6.2.5p10.
130 InitBuiltinType(FloatTy, BuiltinType::Float);
131 InitBuiltinType(DoubleTy, BuiltinType::Double);
132 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
133
134 // C99 6.2.5p11.
135 FloatComplexTy = getComplexType(FloatTy);
136 DoubleComplexTy = getComplexType(DoubleTy);
137 LongDoubleComplexTy = getComplexType(LongDoubleTy);
138}
139
Chris Lattnera7674d82007-07-13 22:13:22 +0000140
141/// getTypeSize - Return the size of the specified type, in bits. This method
142/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000143std::pair<uint64_t, unsigned>
144ASTContext::getTypeInfo(QualType T, SourceLocation L) {
Chris Lattnera7674d82007-07-13 22:13:22 +0000145 T = T.getCanonicalType();
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000146 uint64_t Size;
147 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000148 switch (T->getTypeClass()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000149 default:
150 case Type::Complex:
151 case Type::Array:
152 case Type::Vector:
153 case Type::TypeName:
154 case Type::Tagged:
155 assert(0 && "Unimplemented type sizes!");
156 case Type::FunctionNoProto:
157 case Type::FunctionProto:
158 assert(0 && "Incomplete types have no size!");
Chris Lattnera7674d82007-07-13 22:13:22 +0000159 case Type::Builtin: {
160 // FIXME: need to use TargetInfo to derive the target specific sizes. This
161 // implementation will suffice for play with vector support.
162 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000163 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000164 case BuiltinType::Void:
165 assert(0 && "Incomplete types have no size!");
166 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000167 case BuiltinType::Char_S:
168 case BuiltinType::Char_U:
169 case BuiltinType::UChar:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000170 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000171 case BuiltinType::UShort:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000172 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000173 case BuiltinType::UInt:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000174 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000175 case BuiltinType::ULong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000176 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000177 case BuiltinType::ULongLong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000178 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
179 case BuiltinType::Float: Target.getFloatInfo(Size, Align, L); break;
180 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, L); break;
181 case BuiltinType::LongDouble: Target.getLongDoubleInfo(Size, Align,L);break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000182 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000183 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000184 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000185 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000186 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000187 // "When applied to a reference or a reference type, the result is the size
188 // of the referenced type." C++98 5.3.3p2: expr.sizeof
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000189 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattnera7674d82007-07-13 22:13:22 +0000190 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000191
192 return std::make_pair(Size, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000193}
194
195//===----------------------------------------------------------------------===//
196// Type creation/memoization methods
197//===----------------------------------------------------------------------===//
198
199
Reid Spencer5f016e22007-07-11 17:01:13 +0000200/// getComplexType - Return the uniqued reference to the type for a complex
201/// number with the specified element type.
202QualType ASTContext::getComplexType(QualType T) {
203 // Unique pointers, to guarantee there is only one pointer of a particular
204 // structure.
205 llvm::FoldingSetNodeID ID;
206 ComplexType::Profile(ID, T);
207
208 void *InsertPos = 0;
209 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
210 return QualType(CT, 0);
211
212 // If the pointee type isn't canonical, this won't be a canonical type either,
213 // so fill in the canonical type field.
214 QualType Canonical;
215 if (!T->isCanonical()) {
216 Canonical = getComplexType(T.getCanonicalType());
217
218 // Get the new insert position for the node we care about.
219 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
220 assert(NewIP == 0 && "Shouldn't be in the map!");
221 }
222 ComplexType *New = new ComplexType(T, Canonical);
223 Types.push_back(New);
224 ComplexTypes.InsertNode(New, InsertPos);
225 return QualType(New, 0);
226}
227
228
229/// getPointerType - Return the uniqued reference to the type for a pointer to
230/// the specified type.
231QualType ASTContext::getPointerType(QualType T) {
232 // Unique pointers, to guarantee there is only one pointer of a particular
233 // structure.
234 llvm::FoldingSetNodeID ID;
235 PointerType::Profile(ID, T);
236
237 void *InsertPos = 0;
238 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
239 return QualType(PT, 0);
240
241 // If the pointee type isn't canonical, this won't be a canonical type either,
242 // so fill in the canonical type field.
243 QualType Canonical;
244 if (!T->isCanonical()) {
245 Canonical = getPointerType(T.getCanonicalType());
246
247 // Get the new insert position for the node we care about.
248 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
249 assert(NewIP == 0 && "Shouldn't be in the map!");
250 }
251 PointerType *New = new PointerType(T, Canonical);
252 Types.push_back(New);
253 PointerTypes.InsertNode(New, InsertPos);
254 return QualType(New, 0);
255}
256
257/// getReferenceType - Return the uniqued reference to the type for a reference
258/// to the specified type.
259QualType ASTContext::getReferenceType(QualType T) {
260 // Unique pointers, to guarantee there is only one pointer of a particular
261 // structure.
262 llvm::FoldingSetNodeID ID;
263 ReferenceType::Profile(ID, T);
264
265 void *InsertPos = 0;
266 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
267 return QualType(RT, 0);
268
269 // If the referencee type isn't canonical, this won't be a canonical type
270 // either, so fill in the canonical type field.
271 QualType Canonical;
272 if (!T->isCanonical()) {
273 Canonical = getReferenceType(T.getCanonicalType());
274
275 // Get the new insert position for the node we care about.
276 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
277 assert(NewIP == 0 && "Shouldn't be in the map!");
278 }
279
280 ReferenceType *New = new ReferenceType(T, Canonical);
281 Types.push_back(New);
282 ReferenceTypes.InsertNode(New, InsertPos);
283 return QualType(New, 0);
284}
285
286/// getArrayType - Return the unique reference to the type for an array of the
287/// specified element type.
288QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
289 unsigned EltTypeQuals, Expr *NumElts) {
290 // Unique array types, to guarantee there is only one array of a particular
291 // structure.
292 llvm::FoldingSetNodeID ID;
293 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
294
295 void *InsertPos = 0;
296 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
297 return QualType(ATP, 0);
298
299 // If the element type isn't canonical, this won't be a canonical type either,
300 // so fill in the canonical type field.
301 QualType Canonical;
302 if (!EltTy->isCanonical()) {
303 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
304 NumElts);
305
306 // Get the new insert position for the node we care about.
307 ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
308 assert(NewIP == 0 && "Shouldn't be in the map!");
309 }
310
311 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
312 ArrayTypes.InsertNode(New, InsertPos);
313 Types.push_back(New);
314 return QualType(New, 0);
315}
316
317/// convertToVectorType - Return the unique reference to a vector type of
318/// the specified element type and size. VectorType can be a pointer, array,
319/// function, or built-in type (i.e. _Bool, integer, or float).
320QualType ASTContext::convertToVectorType(QualType vecType, unsigned NumElts) {
321 BuiltinType *baseType;
322
323 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
324 assert(baseType != 0 &&
325 "convertToVectorType(): Complex vector types unimplemented");
326
327 // Check if we've already instantiated a vector of this type.
328 llvm::FoldingSetNodeID ID;
329 VectorType::Profile(ID, vecType, NumElts);
330 void *InsertPos = 0;
331 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
332 return QualType(VTP, 0);
333
334 // If the element type isn't canonical, this won't be a canonical type either,
335 // so fill in the canonical type field.
336 QualType Canonical;
337 if (!vecType->isCanonical()) {
338 Canonical = convertToVectorType(vecType.getCanonicalType(), NumElts);
339
340 // Get the new insert position for the node we care about.
341 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
342 assert(NewIP == 0 && "Shouldn't be in the map!");
343 }
344 VectorType *New = new VectorType(vecType, NumElts, Canonical);
345 VectorTypes.InsertNode(New, InsertPos);
346 Types.push_back(New);
347 return QualType(New, 0);
348}
349
350/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
351///
352QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
353 // Unique functions, to guarantee there is only one function of a particular
354 // structure.
355 llvm::FoldingSetNodeID ID;
356 FunctionTypeNoProto::Profile(ID, ResultTy);
357
358 void *InsertPos = 0;
359 if (FunctionTypeNoProto *FT =
360 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
361 return QualType(FT, 0);
362
363 QualType Canonical;
364 if (!ResultTy->isCanonical()) {
365 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
366
367 // Get the new insert position for the node we care about.
368 FunctionTypeNoProto *NewIP =
369 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
370 assert(NewIP == 0 && "Shouldn't be in the map!");
371 }
372
373 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
374 Types.push_back(New);
375 FunctionTypeProtos.InsertNode(New, InsertPos);
376 return QualType(New, 0);
377}
378
379/// getFunctionType - Return a normal function type with a typed argument
380/// list. isVariadic indicates whether the argument list includes '...'.
381QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
382 unsigned NumArgs, bool isVariadic) {
383 // Unique functions, to guarantee there is only one function of a particular
384 // structure.
385 llvm::FoldingSetNodeID ID;
386 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
387
388 void *InsertPos = 0;
389 if (FunctionTypeProto *FTP =
390 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
391 return QualType(FTP, 0);
392
393 // Determine whether the type being created is already canonical or not.
394 bool isCanonical = ResultTy->isCanonical();
395 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
396 if (!ArgArray[i]->isCanonical())
397 isCanonical = false;
398
399 // If this type isn't canonical, get the canonical version of it.
400 QualType Canonical;
401 if (!isCanonical) {
402 llvm::SmallVector<QualType, 16> CanonicalArgs;
403 CanonicalArgs.reserve(NumArgs);
404 for (unsigned i = 0; i != NumArgs; ++i)
405 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
406
407 Canonical = getFunctionType(ResultTy.getCanonicalType(),
408 &CanonicalArgs[0], NumArgs,
409 isVariadic);
410
411 // Get the new insert position for the node we care about.
412 FunctionTypeProto *NewIP =
413 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
414 assert(NewIP == 0 && "Shouldn't be in the map!");
415 }
416
417 // FunctionTypeProto objects are not allocated with new because they have a
418 // variable size array (for parameter types) at the end of them.
419 FunctionTypeProto *FTP =
420 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
421 (NumArgs-1)*sizeof(QualType));
422 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
423 Canonical);
424 Types.push_back(FTP);
425 FunctionTypeProtos.InsertNode(FTP, InsertPos);
426 return QualType(FTP, 0);
427}
428
429/// getTypedefType - Return the unique reference to the type for the
430/// specified typename decl.
431QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
432 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
433
434 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
435 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
436 Types.push_back(Decl->TypeForDecl);
437 return QualType(Decl->TypeForDecl, 0);
438}
439
440/// getTagDeclType - Return the unique reference to the type for the
441/// specified TagDecl (struct/union/class/enum) decl.
442QualType ASTContext::getTagDeclType(TagDecl *Decl) {
443 // The decl stores the type cache.
444 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
445
446 Decl->TypeForDecl = new TagType(Decl, QualType());
447 Types.push_back(Decl->TypeForDecl);
448 return QualType(Decl->TypeForDecl, 0);
449}
450
451/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
452/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
453/// needs to agree with the definition in <stddef.h>.
454QualType ASTContext::getSizeType() const {
455 // On Darwin, size_t is defined as a "long unsigned int".
456 // FIXME: should derive from "Target".
457 return UnsignedLongTy;
458}
459
Chris Lattner8b9023b2007-07-13 03:05:23 +0000460/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
461/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
462QualType ASTContext::getPointerDiffType() const {
463 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
464 // FIXME: should derive from "Target".
465 return IntTy;
466}
467
Reid Spencer5f016e22007-07-11 17:01:13 +0000468/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
469/// routine will assert if passed a built-in type that isn't an integer or enum.
470static int getIntegerRank(QualType t) {
471 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
472 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
473 return 4;
474 }
475
476 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
477 switch (BT->getKind()) {
478 default:
479 assert(0 && "getIntegerRank(): not a built-in integer");
480 case BuiltinType::Bool:
481 return 1;
482 case BuiltinType::Char_S:
483 case BuiltinType::Char_U:
484 case BuiltinType::SChar:
485 case BuiltinType::UChar:
486 return 2;
487 case BuiltinType::Short:
488 case BuiltinType::UShort:
489 return 3;
490 case BuiltinType::Int:
491 case BuiltinType::UInt:
492 return 4;
493 case BuiltinType::Long:
494 case BuiltinType::ULong:
495 return 5;
496 case BuiltinType::LongLong:
497 case BuiltinType::ULongLong:
498 return 6;
499 }
500}
501
502/// getFloatingRank - Return a relative rank for floating point types.
503/// This routine will assert if passed a built-in type that isn't a float.
504static int getFloatingRank(QualType T) {
505 T = T.getCanonicalType();
506 if (ComplexType *CT = dyn_cast<ComplexType>(T))
507 return getFloatingRank(CT->getElementType());
508
509 switch (cast<BuiltinType>(T)->getKind()) {
510 default: assert(0 && "getFloatingPointRank(): not a floating type");
511 case BuiltinType::Float: return FloatRank;
512 case BuiltinType::Double: return DoubleRank;
513 case BuiltinType::LongDouble: return LongDoubleRank;
514 }
515}
516
517// maxComplexType - the following code handles 3 different combinations:
518// complex/complex, complex/float, float/complex.
519// When both operands are complex, the shorter operand is converted to the
520// type of the longer, and that is the type of the result. This corresponds
521// to what is done when combining two real floating-point operands.
522// The fun begins when size promotion occur across type domains. g
523// getFloatingRank & convertFloatingRankToComplexType handle this without
524// enumerating all permutations.
525// It also allows us to add new types without breakage.
526// From H&S 6.3.4: When one operand is complex and the other is a real
527// floating-point type, the less precise type is converted, within it's
528// real or complex domain, to the precision of the other type. For example,
529// when combining a "long double" with a "double _Complex", the
530// "double _Complex" is promoted to "long double _Complex".
531
532QualType ASTContext::maxComplexType(QualType lt, QualType rt) const {
533 switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) {
534 default: assert(0 && "convertRankToComplex(): illegal value for rank");
535 case FloatRank: return FloatComplexTy;
536 case DoubleRank: return DoubleComplexTy;
537 case LongDoubleRank: return LongDoubleComplexTy;
538 }
539}
540
541// maxFloatingType - handles the simple case, both operands are floats.
542QualType ASTContext::maxFloatingType(QualType lt, QualType rt) {
543 return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt;
544}
545
546// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
547// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
548QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
549 if (lhs == rhs) return lhs;
550
551 bool t1Unsigned = lhs->isUnsignedIntegerType();
552 bool t2Unsigned = rhs->isUnsignedIntegerType();
553
554 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
555 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
556
557 // We have two integer types with differing signs
558 QualType unsignedType = t1Unsigned ? lhs : rhs;
559 QualType signedType = t1Unsigned ? rhs : lhs;
560
561 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
562 return unsignedType;
563 else {
564 // FIXME: Need to check if the signed type can represent all values of the
565 // unsigned type. If it can, then the result is the signed type.
566 // If it can't, then the result is the unsigned version of the signed type.
567 // Should probably add a helper that returns a signed integer type from
568 // an unsigned (and vice versa). C99 6.3.1.8.
569 return signedType;
570 }
571}