blob: 1ae7222b5f2e8cba9a705b3cc29a6075f383476c [file] [log] [blame]
Chris Lattnerddc135e2006-11-10 06:34:16 +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"
Chris Lattnerd0342e52006-11-20 04:02:15 +000015#include "clang/AST/Decl.h"
Chris Lattnerddc135e2006-11-10 06:34:16 +000016#include "clang/Lex/Preprocessor.h"
Chris Lattner4dc8a6f2007-05-20 23:50:58 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattnerc6ad8132006-12-02 07:52:18 +000018#include "llvm/ADT/SmallVector.h"
Steve Naroff6fbf0dc2007-03-16 00:33:25 +000019
Chris Lattnerddc135e2006-11-10 06:34:16 +000020using namespace llvm;
21using namespace clang;
22
Steve Naroff0af91202007-04-27 21:51:21 +000023enum FloatingRank {
24 FloatRank, DoubleRank, LongDoubleRank
25};
26
Chris Lattnerd5973eb2006-11-12 00:53:46 +000027ASTContext::~ASTContext() {
28 // Deallocate all the types.
29 while (!Types.empty()) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +000030 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
31 // Destroy the object, but don't call delete. These are malloc'd.
32 FT->~FunctionTypeProto();
33 free(FT);
34 } else {
35 delete Types.back();
36 }
Chris Lattnerd5973eb2006-11-12 00:53:46 +000037 Types.pop_back();
38 }
39}
40
Chris Lattner4eb445d2007-01-26 01:27:23 +000041void ASTContext::PrintStats() const {
42 fprintf(stderr, "*** AST Context Stats:\n");
43 fprintf(stderr, " %d types total.\n", (int)Types.size());
44 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
45 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0;
46
47 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
48
49 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
50 Type *T = Types[i];
51 if (isa<BuiltinType>(T))
52 ++NumBuiltin;
53 else if (isa<PointerType>(T))
54 ++NumPointer;
55 else if (isa<ArrayType>(T))
56 ++NumArray;
57 else if (isa<FunctionTypeNoProto>(T))
58 ++NumFunctionNP;
59 else if (isa<FunctionTypeProto>(T))
60 ++NumFunctionP;
Chris Lattner32d920b2007-01-26 02:01:53 +000061 else if (isa<TypedefType>(T))
Chris Lattner4eb445d2007-01-26 01:27:23 +000062 ++NumTypeName;
Steve Narofff1e53692007-03-23 22:27:02 +000063 else if (TagType *TT = dyn_cast<TagType>(T)) {
Chris Lattner4eb445d2007-01-26 01:27:23 +000064 ++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 array types\n", NumArray);
80 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
81 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
82 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
83 fprintf(stderr, " %d tagged types\n", NumTagged);
84 fprintf(stderr, " %d struct types\n", NumTagStruct);
85 fprintf(stderr, " %d union types\n", NumTagUnion);
86 fprintf(stderr, " %d class types\n", NumTagClass);
87 fprintf(stderr, " %d enum types\n", NumTagEnum);
Chris Lattnerfc234de2007-05-24 00:40:54 +000088 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
Steve Narofff84d11f2007-05-23 21:48:04 +000089 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattnerfc234de2007-05-24 00:40:54 +000090 NumFunctionP*sizeof(FunctionTypeProto)+
91 NumFunctionNP*sizeof(FunctionTypeNoProto)+
92 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
Chris Lattner4eb445d2007-01-26 01:27:23 +000093}
94
95
Steve Naroffe5aa9be2007-04-05 22:36:20 +000096void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
97 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
Chris Lattnerd5973eb2006-11-12 00:53:46 +000098}
99
100
Chris Lattner970e54e2006-11-12 00:37:36 +0000101void ASTContext::InitBuiltinTypes() {
102 assert(VoidTy.isNull() && "Context reinitialized?");
103
104 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000105 InitBuiltinType(VoidTy, BuiltinType::Void);
Chris Lattner970e54e2006-11-12 00:37:36 +0000106
107 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000108 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000109 // C99 6.2.5p3.
Chris Lattner726f97b2006-12-03 02:57:32 +0000110 InitBuiltinType(CharTy, BuiltinType::Char);
Chris Lattner970e54e2006-11-12 00:37:36 +0000111 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000112 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
113 InitBuiltinType(ShortTy, BuiltinType::Short);
114 InitBuiltinType(IntTy, BuiltinType::Int);
115 InitBuiltinType(LongTy, BuiltinType::Long);
116 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000117
118 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000119 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
120 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
121 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
122 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
123 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000124
125 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000126 InitBuiltinType(FloatTy, BuiltinType::Float);
127 InitBuiltinType(DoubleTy, BuiltinType::Double);
128 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Chris Lattner970e54e2006-11-12 00:37:36 +0000129
130 // C99 6.2.5p11.
Chris Lattner726f97b2006-12-03 02:57:32 +0000131 InitBuiltinType(FloatComplexTy, BuiltinType::FloatComplex);
132 InitBuiltinType(DoubleComplexTy, BuiltinType::DoubleComplex);
133 InitBuiltinType(LongDoubleComplexTy, BuiltinType::LongDoubleComplex);
Chris Lattner970e54e2006-11-12 00:37:36 +0000134}
135
136/// getPointerType - Return the uniqued reference to the type for a pointer to
137/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000138QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000139 // Unique pointers, to guarantee there is only one pointer of a particular
140 // structure.
Chris Lattner67521df2007-01-27 01:29:36 +0000141 FoldingSetNodeID ID;
142 PointerType::Profile(ID, T);
143
144 void *InsertPos = 0;
145 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000146 return QualType(PT, 0);
Chris Lattner970e54e2006-11-12 00:37:36 +0000147
Chris Lattner7ccecb92006-11-12 08:50:50 +0000148 // If the pointee type isn't canonical, this won't be a canonical type either,
149 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000150 QualType Canonical;
Chris Lattner67521df2007-01-27 01:29:36 +0000151 if (!T->isCanonical()) {
Steve Naroffd50c88e2007-04-05 21:15:20 +0000152 Canonical = getPointerType(T.getCanonicalType());
Chris Lattner67521df2007-01-27 01:29:36 +0000153
154 // Get the new insert position for the node we care about.
155 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
156 assert(NewIP == 0 && "Shouldn't be in the map!");
157 }
Chris Lattner67521df2007-01-27 01:29:36 +0000158 PointerType *New = new PointerType(T, Canonical);
159 Types.push_back(New);
160 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000161 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +0000162}
163
Chris Lattner7ccecb92006-11-12 08:50:50 +0000164/// getArrayType - Return the unique reference to the type for an array of the
165/// specified element type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000166QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
167 unsigned EltTypeQuals, Expr *NumElts) {
Chris Lattner36f8e652007-01-27 08:31:04 +0000168 // Unique array types, to guarantee there is only one array of a particular
Chris Lattner7ccecb92006-11-12 08:50:50 +0000169 // structure.
Chris Lattner36f8e652007-01-27 08:31:04 +0000170 FoldingSetNodeID ID;
Steve Naroffb7d49242007-03-14 19:55:17 +0000171 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
Steve Naroff408451b2007-02-26 22:17:12 +0000172
Chris Lattner36f8e652007-01-27 08:31:04 +0000173 void *InsertPos = 0;
174 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000175 return QualType(ATP, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000176
177 // If the element type isn't canonical, this won't be a canonical type either,
178 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000179 QualType Canonical;
Chris Lattner36f8e652007-01-27 08:31:04 +0000180 if (!EltTy->isCanonical()) {
Chris Lattner7ccecb92006-11-12 08:50:50 +0000181 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000182 NumElts);
Chris Lattner36f8e652007-01-27 08:31:04 +0000183
184 // Get the new insert position for the node we care about.
185 ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
186 assert(NewIP == 0 && "Shouldn't be in the map!");
187 }
Chris Lattner7ccecb92006-11-12 08:50:50 +0000188
Steve Naroffb7d49242007-03-14 19:55:17 +0000189 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
Chris Lattner36f8e652007-01-27 08:31:04 +0000190 ArrayTypes.InsertNode(New, InsertPos);
191 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000192 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000193}
194
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000195/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
196///
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000197QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000198 // Unique functions, to guarantee there is only one function of a particular
199 // structure.
Chris Lattner47955de2007-01-27 08:37:20 +0000200 FoldingSetNodeID ID;
201 FunctionTypeNoProto::Profile(ID, ResultTy);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000202
Chris Lattner47955de2007-01-27 08:37:20 +0000203 void *InsertPos = 0;
204 if (FunctionTypeNoProto *FT =
205 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000206 return QualType(FT, 0);
Chris Lattner47955de2007-01-27 08:37:20 +0000207
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000208 QualType Canonical;
Chris Lattner47955de2007-01-27 08:37:20 +0000209 if (!ResultTy->isCanonical()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000210 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
Chris Lattner47955de2007-01-27 08:37:20 +0000211
212 // Get the new insert position for the node we care about.
213 FunctionTypeNoProto *NewIP =
214 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
215 assert(NewIP == 0 && "Shouldn't be in the map!");
216 }
217
218 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
219 Types.push_back(New);
220 FunctionTypeProtos.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000221 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000222}
223
224/// getFunctionType - Return a normal function type with a typed argument
225/// list. isVariadic indicates whether the argument list includes '...'.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000226QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
227 unsigned NumArgs, bool isVariadic) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000228 // Unique functions, to guarantee there is only one function of a particular
229 // structure.
Chris Lattnerfd4de792007-01-27 01:15:32 +0000230 FoldingSetNodeID ID;
231 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
232
233 void *InsertPos = 0;
234 if (FunctionTypeProto *FTP =
235 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000236 return QualType(FTP, 0);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000237
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000238 // Determine whether the type being created is already canonical or not.
239 bool isCanonical = ResultTy->isCanonical();
240 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
241 if (!ArgArray[i]->isCanonical())
242 isCanonical = false;
243
244 // If this type isn't canonical, get the canonical version of it.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000245 QualType Canonical;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000246 if (!isCanonical) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000247 SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000248 CanonicalArgs.reserve(NumArgs);
249 for (unsigned i = 0; i != NumArgs; ++i)
250 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
251
252 Canonical = getFunctionType(ResultTy.getCanonicalType(),
253 &CanonicalArgs[0], NumArgs,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000254 isVariadic);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000255
256 // Get the new insert position for the node we care about.
257 FunctionTypeProto *NewIP =
258 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
259 assert(NewIP == 0 && "Shouldn't be in the map!");
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000260 }
261
262 // FunctionTypeProto objects are not allocated with new because they have a
263 // variable size array (for parameter types) at the end of them.
264 FunctionTypeProto *FTP =
265 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000266 (NumArgs-1)*sizeof(QualType));
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000267 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
268 Canonical);
269
270 Types.push_back(FTP);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000271 FunctionTypeProtos.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000272 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000273}
Chris Lattneref51c202006-11-10 07:17:23 +0000274
Chris Lattner32d920b2007-01-26 02:01:53 +0000275/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +0000276/// specified typename decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000277QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
278 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000279
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000280 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Chris Lattnercceab1a2007-03-26 20:16:44 +0000281 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
282 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000283 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000284}
285
Chris Lattnerfb072462007-01-23 05:45:31 +0000286/// getTagDeclType - Return the unique reference to the type for the
287/// specified TagDecl (struct/union/class/enum) decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000288QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Chris Lattner733067d2007-01-26 01:42:24 +0000289 // The decl stores the type cache.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000290 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000291
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000292 Decl->TypeForDecl = new TagType(Decl, QualType());
Chris Lattnercceab1a2007-03-26 20:16:44 +0000293 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000294 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000295}
296
Steve Naroff92e30f82007-04-02 22:35:25 +0000297/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
Steve Naroff0f6256d2007-04-02 23:01:44 +0000298/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
Steve Naroff92e30f82007-04-02 22:35:25 +0000299/// needs to agree with the definition in <stddef.h>.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000300QualType ASTContext::getSizeType() const {
Steve Naroff92e30f82007-04-02 22:35:25 +0000301 // On Darwin, size_t is defined as a "long unsigned int".
302 // FIXME: should derive from "Target".
303 return UnsignedLongTy;
304}
Chris Lattnerfb072462007-01-23 05:45:31 +0000305
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000306/// getIntegerBitwidth - Return the bitwidth of the specified integer type
307/// according to the target. 'Loc' specifies the source location that
308/// requires evaluation of this property.
309unsigned ASTContext::getIntegerBitwidth(QualType T, SourceLocation Loc) {
310 if (const TagType *TT = dyn_cast<TagType>(T.getCanonicalType())) {
311 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
312 assert(0 && "FIXME: getIntegerBitwidth(enum) unimplemented!");
313 }
314
315 const BuiltinType *BT = cast<BuiltinType>(T.getCanonicalType());
316 switch (BT->getKind()) {
317 default: assert(0 && "getIntegerBitwidth(): not a built-in integer");
318 case BuiltinType::Bool: return Target.getBoolWidth(Loc);
319 case BuiltinType::Char:
320 case BuiltinType::SChar:
321 case BuiltinType::UChar: return Target.getCharWidth(Loc);
322 case BuiltinType::Short:
323 case BuiltinType::UShort: return Target.getShortWidth(Loc);
324 case BuiltinType::Int:
325 case BuiltinType::UInt: return Target.getIntWidth(Loc);
326 case BuiltinType::Long:
327 case BuiltinType::ULong: return Target.getLongWidth(Loc);
328 case BuiltinType::LongLong:
329 case BuiltinType::ULongLong: return Target.getLongLongWidth(Loc);
330 }
331}
332
Steve Naroff0af91202007-04-27 21:51:21 +0000333/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
334/// routine will assert if passed a built-in type that isn't an integer or enum.
335static int getIntegerRank(QualType t) {
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000336 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
337 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
338 return 4;
Steve Naroffe4718892007-04-27 18:30:00 +0000339 }
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000340
341 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
342 switch (BT->getKind()) {
343 default:
344 assert(0 && "getIntegerRank(): not a built-in integer");
345 case BuiltinType::Bool:
346 return 1;
347 case BuiltinType::Char:
348 case BuiltinType::SChar:
349 case BuiltinType::UChar:
350 return 2;
351 case BuiltinType::Short:
352 case BuiltinType::UShort:
353 return 3;
354 case BuiltinType::Int:
355 case BuiltinType::UInt:
356 return 4;
357 case BuiltinType::Long:
358 case BuiltinType::ULong:
359 return 5;
360 case BuiltinType::LongLong:
361 case BuiltinType::ULongLong:
362 return 6;
363 }
Steve Naroffe4718892007-04-27 18:30:00 +0000364}
365
Steve Naroff0af91202007-04-27 21:51:21 +0000366/// getFloatingRank - Return a relative rank for floating point types.
367/// This routine will assert if passed a built-in type that isn't a float.
368static int getFloatingRank(QualType t) {
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000369 switch (cast<BuiltinType>(t.getCanonicalType())->getKind()) {
Steve Naroffe4718892007-04-27 18:30:00 +0000370 default:
Steve Naroff0af91202007-04-27 21:51:21 +0000371 assert(0 && "getFloatingPointRank(): not a floating type");
372 case BuiltinType::Float:
373 case BuiltinType::FloatComplex:
374 return FloatRank;
375 case BuiltinType::Double:
376 case BuiltinType::DoubleComplex:
377 return DoubleRank;
378 case BuiltinType::LongDouble:
379 case BuiltinType::LongDoubleComplex:
380 return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +0000381 }
382}
383
384// maxComplexType - the following code handles 3 different combinations:
385// complex/complex, complex/float, float/complex.
386// When both operands are complex, the shorter operand is converted to the
387// type of the longer, and that is the type of the result. This corresponds
388// to what is done when combining two real floating-point operands.
389// The fun begins when size promotion occur across type domains. g
390// getFloatingRank & convertFloatingRankToComplexType handle this without
391// enumerating all permutations.
392// It also allows us to add new types without breakage.
393// From H&S 6.3.4: When one operand is complex and the other is a real
394// floating-point type, the less precise type is converted, within it's
395// real or complex domain, to the precision of the other type. For example,
396// when combining a "long double" with a "double _Complex", the
397// "double _Complex" is promoted to "long double _Complex".
398
Steve Naroff0af91202007-04-27 21:51:21 +0000399QualType ASTContext::maxComplexType(QualType lt, QualType rt) const {
400 switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) {
401 default:
402 assert(0 && "convertRankToComplex(): illegal value for rank");
403 case FloatRank:
404 return FloatComplexTy;
405 case DoubleRank:
406 return DoubleComplexTy;
407 case LongDoubleRank:
408 return LongDoubleComplexTy;
409 }
Steve Naroffe4718892007-04-27 18:30:00 +0000410}
411
412// maxFloatingType - handles the simple case, both operands are floats.
413QualType ASTContext::maxFloatingType(QualType lt, QualType rt) {
414 return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt;
415}
416
Steve Naroff0af91202007-04-27 21:51:21 +0000417// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
418// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
Steve Naroffe4718892007-04-27 18:30:00 +0000419QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
Steve Naroffe4718892007-04-27 18:30:00 +0000420 bool t1Unsigned = lhs->isUnsignedIntegerType();
421 bool t2Unsigned = rhs->isUnsignedIntegerType();
422
423 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
424 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
425
426 // We have two integer types with differing signs
427 QualType unsignedType = t1Unsigned ? lhs : rhs;
428 QualType signedType = t1Unsigned ? rhs : lhs;
429
430 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
431 return unsignedType;
Steve Naroff0af91202007-04-27 21:51:21 +0000432 else {
433 // FIXME: Need to check if the signed type can represent all values of the
434 // unsigned type. If it can, then the result is the signed type.
435 // If it can't, then the result is the unsigned version of the signed type.
436 // Should probably add a helper that returns a signed integer type from
437 // an unsigned (and vice versa). C99 6.3.1.8.
438 return signedType;
439 }
Steve Naroffe4718892007-04-27 18:30:00 +0000440}