blob: 6e9208bbb5df792b7ef92ac5a9e8f186f5619543 [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.
143unsigned ASTContext::getTypeSize(QualType T) {
144 T = T.getCanonicalType();
145 switch (T->getTypeClass()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000146 default:
147 case Type::Complex:
148 case Type::Array:
149 case Type::Vector:
150 case Type::TypeName:
151 case Type::Tagged:
152 assert(0 && "Unimplemented type sizes!");
153 case Type::FunctionNoProto:
154 case Type::FunctionProto:
155 assert(0 && "Incomplete types have no size!");
Chris Lattnera7674d82007-07-13 22:13:22 +0000156 case Type::Builtin: {
157 // FIXME: need to use TargetInfo to derive the target specific sizes. This
158 // implementation will suffice for play with vector support.
159 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000160 default: assert(0 && "Unknown builtin type!");
161 case BuiltinType::Void: assert(0 && "Incomplete types have no size!");
162 case BuiltinType::Bool: return Target.getBoolWidth(SourceLocation());
163 case BuiltinType::Char_S:
164 case BuiltinType::Char_U:
165 case BuiltinType::UChar:
166 case BuiltinType::SChar: return Target.getCharWidth(SourceLocation());
167 case BuiltinType::UShort:
168 case BuiltinType::Short: return Target.getShortWidth(SourceLocation());
169 case BuiltinType::UInt:
170 case BuiltinType::Int: return Target.getIntWidth(SourceLocation());
171 case BuiltinType::ULong:
172 case BuiltinType::Long: return Target.getLongWidth(SourceLocation());
173 case BuiltinType::ULongLong:
174 case BuiltinType::LongLong:return Target.getLongLongWidth(SourceLocation());
175 case BuiltinType::Float: return Target.getFloatWidth(SourceLocation());
176 case BuiltinType::Double: return Target.getDoubleWidth(SourceLocation());
177 case BuiltinType::LongDouble:
178 return Target.getLongDoubleWidth(SourceLocation());
Chris Lattnera7674d82007-07-13 22:13:22 +0000179 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000180 }
181 case Type::Pointer:
Chris Lattner692233e2007-07-13 22:27:08 +0000182 return Target.getPointerWidth(SourceLocation());
Chris Lattnera7674d82007-07-13 22:13:22 +0000183 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000184 // "When applied to a reference or a reference type, the result is the size
185 // of the referenced type." C++98 5.3.3p2: expr.sizeof
Chris Lattnera7674d82007-07-13 22:13:22 +0000186 return getTypeSize(cast<ReferenceType>(T)->getReferenceeType());
Chris Lattnera7674d82007-07-13 22:13:22 +0000187 }
Chris Lattnera7674d82007-07-13 22:13:22 +0000188}
189
190//===----------------------------------------------------------------------===//
191// Type creation/memoization methods
192//===----------------------------------------------------------------------===//
193
194
Reid Spencer5f016e22007-07-11 17:01:13 +0000195/// getComplexType - Return the uniqued reference to the type for a complex
196/// number with the specified element type.
197QualType ASTContext::getComplexType(QualType T) {
198 // Unique pointers, to guarantee there is only one pointer of a particular
199 // structure.
200 llvm::FoldingSetNodeID ID;
201 ComplexType::Profile(ID, T);
202
203 void *InsertPos = 0;
204 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
205 return QualType(CT, 0);
206
207 // If the pointee type isn't canonical, this won't be a canonical type either,
208 // so fill in the canonical type field.
209 QualType Canonical;
210 if (!T->isCanonical()) {
211 Canonical = getComplexType(T.getCanonicalType());
212
213 // Get the new insert position for the node we care about.
214 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
215 assert(NewIP == 0 && "Shouldn't be in the map!");
216 }
217 ComplexType *New = new ComplexType(T, Canonical);
218 Types.push_back(New);
219 ComplexTypes.InsertNode(New, InsertPos);
220 return QualType(New, 0);
221}
222
223
224/// getPointerType - Return the uniqued reference to the type for a pointer to
225/// the specified type.
226QualType ASTContext::getPointerType(QualType T) {
227 // Unique pointers, to guarantee there is only one pointer of a particular
228 // structure.
229 llvm::FoldingSetNodeID ID;
230 PointerType::Profile(ID, T);
231
232 void *InsertPos = 0;
233 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
234 return QualType(PT, 0);
235
236 // If the pointee type isn't canonical, this won't be a canonical type either,
237 // so fill in the canonical type field.
238 QualType Canonical;
239 if (!T->isCanonical()) {
240 Canonical = getPointerType(T.getCanonicalType());
241
242 // Get the new insert position for the node we care about.
243 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
244 assert(NewIP == 0 && "Shouldn't be in the map!");
245 }
246 PointerType *New = new PointerType(T, Canonical);
247 Types.push_back(New);
248 PointerTypes.InsertNode(New, InsertPos);
249 return QualType(New, 0);
250}
251
252/// getReferenceType - Return the uniqued reference to the type for a reference
253/// to the specified type.
254QualType ASTContext::getReferenceType(QualType T) {
255 // Unique pointers, to guarantee there is only one pointer of a particular
256 // structure.
257 llvm::FoldingSetNodeID ID;
258 ReferenceType::Profile(ID, T);
259
260 void *InsertPos = 0;
261 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
262 return QualType(RT, 0);
263
264 // If the referencee type isn't canonical, this won't be a canonical type
265 // either, so fill in the canonical type field.
266 QualType Canonical;
267 if (!T->isCanonical()) {
268 Canonical = getReferenceType(T.getCanonicalType());
269
270 // Get the new insert position for the node we care about.
271 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
272 assert(NewIP == 0 && "Shouldn't be in the map!");
273 }
274
275 ReferenceType *New = new ReferenceType(T, Canonical);
276 Types.push_back(New);
277 ReferenceTypes.InsertNode(New, InsertPos);
278 return QualType(New, 0);
279}
280
281/// getArrayType - Return the unique reference to the type for an array of the
282/// specified element type.
283QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
284 unsigned EltTypeQuals, Expr *NumElts) {
285 // Unique array types, to guarantee there is only one array of a particular
286 // structure.
287 llvm::FoldingSetNodeID ID;
288 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
289
290 void *InsertPos = 0;
291 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
292 return QualType(ATP, 0);
293
294 // If the element type isn't canonical, this won't be a canonical type either,
295 // so fill in the canonical type field.
296 QualType Canonical;
297 if (!EltTy->isCanonical()) {
298 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
299 NumElts);
300
301 // Get the new insert position for the node we care about.
302 ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
303 assert(NewIP == 0 && "Shouldn't be in the map!");
304 }
305
306 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
307 ArrayTypes.InsertNode(New, InsertPos);
308 Types.push_back(New);
309 return QualType(New, 0);
310}
311
312/// convertToVectorType - Return the unique reference to a vector type of
313/// the specified element type and size. VectorType can be a pointer, array,
314/// function, or built-in type (i.e. _Bool, integer, or float).
315QualType ASTContext::convertToVectorType(QualType vecType, unsigned NumElts) {
316 BuiltinType *baseType;
317
318 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
319 assert(baseType != 0 &&
320 "convertToVectorType(): Complex vector types unimplemented");
321
322 // Check if we've already instantiated a vector of this type.
323 llvm::FoldingSetNodeID ID;
324 VectorType::Profile(ID, vecType, NumElts);
325 void *InsertPos = 0;
326 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
327 return QualType(VTP, 0);
328
329 // If the element type isn't canonical, this won't be a canonical type either,
330 // so fill in the canonical type field.
331 QualType Canonical;
332 if (!vecType->isCanonical()) {
333 Canonical = convertToVectorType(vecType.getCanonicalType(), NumElts);
334
335 // Get the new insert position for the node we care about.
336 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
337 assert(NewIP == 0 && "Shouldn't be in the map!");
338 }
339 VectorType *New = new VectorType(vecType, NumElts, Canonical);
340 VectorTypes.InsertNode(New, InsertPos);
341 Types.push_back(New);
342 return QualType(New, 0);
343}
344
345/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
346///
347QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
348 // Unique functions, to guarantee there is only one function of a particular
349 // structure.
350 llvm::FoldingSetNodeID ID;
351 FunctionTypeNoProto::Profile(ID, ResultTy);
352
353 void *InsertPos = 0;
354 if (FunctionTypeNoProto *FT =
355 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
356 return QualType(FT, 0);
357
358 QualType Canonical;
359 if (!ResultTy->isCanonical()) {
360 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
361
362 // Get the new insert position for the node we care about.
363 FunctionTypeNoProto *NewIP =
364 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
365 assert(NewIP == 0 && "Shouldn't be in the map!");
366 }
367
368 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
369 Types.push_back(New);
370 FunctionTypeProtos.InsertNode(New, InsertPos);
371 return QualType(New, 0);
372}
373
374/// getFunctionType - Return a normal function type with a typed argument
375/// list. isVariadic indicates whether the argument list includes '...'.
376QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
377 unsigned NumArgs, bool isVariadic) {
378 // Unique functions, to guarantee there is only one function of a particular
379 // structure.
380 llvm::FoldingSetNodeID ID;
381 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
382
383 void *InsertPos = 0;
384 if (FunctionTypeProto *FTP =
385 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
386 return QualType(FTP, 0);
387
388 // Determine whether the type being created is already canonical or not.
389 bool isCanonical = ResultTy->isCanonical();
390 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
391 if (!ArgArray[i]->isCanonical())
392 isCanonical = false;
393
394 // If this type isn't canonical, get the canonical version of it.
395 QualType Canonical;
396 if (!isCanonical) {
397 llvm::SmallVector<QualType, 16> CanonicalArgs;
398 CanonicalArgs.reserve(NumArgs);
399 for (unsigned i = 0; i != NumArgs; ++i)
400 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
401
402 Canonical = getFunctionType(ResultTy.getCanonicalType(),
403 &CanonicalArgs[0], NumArgs,
404 isVariadic);
405
406 // Get the new insert position for the node we care about.
407 FunctionTypeProto *NewIP =
408 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
409 assert(NewIP == 0 && "Shouldn't be in the map!");
410 }
411
412 // FunctionTypeProto objects are not allocated with new because they have a
413 // variable size array (for parameter types) at the end of them.
414 FunctionTypeProto *FTP =
415 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
416 (NumArgs-1)*sizeof(QualType));
417 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
418 Canonical);
419 Types.push_back(FTP);
420 FunctionTypeProtos.InsertNode(FTP, InsertPos);
421 return QualType(FTP, 0);
422}
423
424/// getTypedefType - Return the unique reference to the type for the
425/// specified typename decl.
426QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
427 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
428
429 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
430 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
431 Types.push_back(Decl->TypeForDecl);
432 return QualType(Decl->TypeForDecl, 0);
433}
434
435/// getTagDeclType - Return the unique reference to the type for the
436/// specified TagDecl (struct/union/class/enum) decl.
437QualType ASTContext::getTagDeclType(TagDecl *Decl) {
438 // The decl stores the type cache.
439 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
440
441 Decl->TypeForDecl = new TagType(Decl, QualType());
442 Types.push_back(Decl->TypeForDecl);
443 return QualType(Decl->TypeForDecl, 0);
444}
445
446/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
447/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
448/// needs to agree with the definition in <stddef.h>.
449QualType ASTContext::getSizeType() const {
450 // On Darwin, size_t is defined as a "long unsigned int".
451 // FIXME: should derive from "Target".
452 return UnsignedLongTy;
453}
454
Chris Lattner8b9023b2007-07-13 03:05:23 +0000455/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
456/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
457QualType ASTContext::getPointerDiffType() const {
458 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
459 // FIXME: should derive from "Target".
460 return IntTy;
461}
462
Reid Spencer5f016e22007-07-11 17:01:13 +0000463/// getIntegerBitwidth - Return the bitwidth of the specified integer type
464/// according to the target. 'Loc' specifies the source location that
465/// requires evaluation of this property.
466unsigned ASTContext::getIntegerBitwidth(QualType T, SourceLocation Loc) {
467 if (const TagType *TT = dyn_cast<TagType>(T.getCanonicalType())) {
468 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
469 assert(0 && "FIXME: getIntegerBitwidth(enum) unimplemented!");
470 }
471
472 const BuiltinType *BT = cast<BuiltinType>(T.getCanonicalType());
473 switch (BT->getKind()) {
474 default: assert(0 && "getIntegerBitwidth(): not a built-in integer");
475 case BuiltinType::Bool: return Target.getBoolWidth(Loc);
476 case BuiltinType::Char_S:
477 case BuiltinType::Char_U:
478 case BuiltinType::SChar:
479 case BuiltinType::UChar: return Target.getCharWidth(Loc);
480 case BuiltinType::Short:
481 case BuiltinType::UShort: return Target.getShortWidth(Loc);
482 case BuiltinType::Int:
483 case BuiltinType::UInt: return Target.getIntWidth(Loc);
484 case BuiltinType::Long:
485 case BuiltinType::ULong: return Target.getLongWidth(Loc);
486 case BuiltinType::LongLong:
487 case BuiltinType::ULongLong: return Target.getLongLongWidth(Loc);
488 }
489}
490
491/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
492/// routine will assert if passed a built-in type that isn't an integer or enum.
493static int getIntegerRank(QualType t) {
494 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
495 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
496 return 4;
497 }
498
499 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
500 switch (BT->getKind()) {
501 default:
502 assert(0 && "getIntegerRank(): not a built-in integer");
503 case BuiltinType::Bool:
504 return 1;
505 case BuiltinType::Char_S:
506 case BuiltinType::Char_U:
507 case BuiltinType::SChar:
508 case BuiltinType::UChar:
509 return 2;
510 case BuiltinType::Short:
511 case BuiltinType::UShort:
512 return 3;
513 case BuiltinType::Int:
514 case BuiltinType::UInt:
515 return 4;
516 case BuiltinType::Long:
517 case BuiltinType::ULong:
518 return 5;
519 case BuiltinType::LongLong:
520 case BuiltinType::ULongLong:
521 return 6;
522 }
523}
524
525/// getFloatingRank - Return a relative rank for floating point types.
526/// This routine will assert if passed a built-in type that isn't a float.
527static int getFloatingRank(QualType T) {
528 T = T.getCanonicalType();
529 if (ComplexType *CT = dyn_cast<ComplexType>(T))
530 return getFloatingRank(CT->getElementType());
531
532 switch (cast<BuiltinType>(T)->getKind()) {
533 default: assert(0 && "getFloatingPointRank(): not a floating type");
534 case BuiltinType::Float: return FloatRank;
535 case BuiltinType::Double: return DoubleRank;
536 case BuiltinType::LongDouble: return LongDoubleRank;
537 }
538}
539
540// maxComplexType - the following code handles 3 different combinations:
541// complex/complex, complex/float, float/complex.
542// When both operands are complex, the shorter operand is converted to the
543// type of the longer, and that is the type of the result. This corresponds
544// to what is done when combining two real floating-point operands.
545// The fun begins when size promotion occur across type domains. g
546// getFloatingRank & convertFloatingRankToComplexType handle this without
547// enumerating all permutations.
548// It also allows us to add new types without breakage.
549// From H&S 6.3.4: When one operand is complex and the other is a real
550// floating-point type, the less precise type is converted, within it's
551// real or complex domain, to the precision of the other type. For example,
552// when combining a "long double" with a "double _Complex", the
553// "double _Complex" is promoted to "long double _Complex".
554
555QualType ASTContext::maxComplexType(QualType lt, QualType rt) const {
556 switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) {
557 default: assert(0 && "convertRankToComplex(): illegal value for rank");
558 case FloatRank: return FloatComplexTy;
559 case DoubleRank: return DoubleComplexTy;
560 case LongDoubleRank: return LongDoubleComplexTy;
561 }
562}
563
564// maxFloatingType - handles the simple case, both operands are floats.
565QualType ASTContext::maxFloatingType(QualType lt, QualType rt) {
566 return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt;
567}
568
569// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
570// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
571QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
572 if (lhs == rhs) return lhs;
573
574 bool t1Unsigned = lhs->isUnsignedIntegerType();
575 bool t2Unsigned = rhs->isUnsignedIntegerType();
576
577 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
578 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
579
580 // We have two integer types with differing signs
581 QualType unsignedType = t1Unsigned ? lhs : rhs;
582 QualType signedType = t1Unsigned ? rhs : lhs;
583
584 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
585 return unsignedType;
586 else {
587 // FIXME: Need to check if the signed type can represent all values of the
588 // unsigned type. If it can, then the result is the signed type.
589 // If it can't, then the result is the unsigned version of the signed type.
590 // Should probably add a helper that returns a signed integer type from
591 // an unsigned (and vice versa). C99 6.3.1.8.
592 return signedType;
593 }
594}