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