blob: 8c4e7c3905b249fef3a13fe147741ad884e33b0e [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;
Bill Wendling3708c182007-05-27 10:15:43 +000045 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Chris Lattner4eb445d2007-01-26 01:27:23 +000046
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;
Bill Wendling3708c182007-05-27 10:15:43 +000055 else if (isa<ReferenceType>(T))
56 ++NumReference;
Chris Lattner4eb445d2007-01-26 01:27:23 +000057 else if (isa<ArrayType>(T))
58 ++NumArray;
59 else if (isa<FunctionTypeNoProto>(T))
60 ++NumFunctionNP;
61 else if (isa<FunctionTypeProto>(T))
62 ++NumFunctionP;
Chris Lattner32d920b2007-01-26 02:01:53 +000063 else if (isa<TypedefType>(T))
Chris Lattner4eb445d2007-01-26 01:27:23 +000064 ++NumTypeName;
Steve Narofff1e53692007-03-23 22:27:02 +000065 else if (TagType *TT = dyn_cast<TagType>(T)) {
Chris Lattner4eb445d2007-01-26 01:27:23 +000066 ++NumTagged;
67 switch (TT->getDecl()->getKind()) {
68 default: assert(0 && "Unknown tagged type!");
69 case Decl::Struct: ++NumTagStruct; break;
70 case Decl::Union: ++NumTagUnion; break;
71 case Decl::Class: ++NumTagClass; break;
72 case Decl::Enum: ++NumTagEnum; break;
73 }
74 } else {
75 assert(0 && "Unknown type!");
76 }
77 }
78
79 fprintf(stderr, " %d builtin types\n", NumBuiltin);
80 fprintf(stderr, " %d pointer types\n", NumPointer);
Bill Wendling3708c182007-05-27 10:15:43 +000081 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner4eb445d2007-01-26 01:27:23 +000082 fprintf(stderr, " %d array types\n", NumArray);
83 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
84 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
85 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
86 fprintf(stderr, " %d tagged types\n", NumTagged);
87 fprintf(stderr, " %d struct types\n", NumTagStruct);
88 fprintf(stderr, " %d union types\n", NumTagUnion);
89 fprintf(stderr, " %d class types\n", NumTagClass);
90 fprintf(stderr, " %d enum types\n", NumTagEnum);
Chris Lattnerfc234de2007-05-24 00:40:54 +000091 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
Steve Narofff84d11f2007-05-23 21:48:04 +000092 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattnerfc234de2007-05-24 00:40:54 +000093 NumFunctionP*sizeof(FunctionTypeProto)+
94 NumFunctionNP*sizeof(FunctionTypeNoProto)+
95 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
Chris Lattner4eb445d2007-01-26 01:27:23 +000096}
97
98
Steve Naroffe5aa9be2007-04-05 22:36:20 +000099void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
100 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000101}
102
103
Chris Lattner970e54e2006-11-12 00:37:36 +0000104void ASTContext::InitBuiltinTypes() {
105 assert(VoidTy.isNull() && "Context reinitialized?");
106
107 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000108 InitBuiltinType(VoidTy, BuiltinType::Void);
Chris Lattner970e54e2006-11-12 00:37:36 +0000109
110 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000111 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000112 // C99 6.2.5p3.
Chris Lattner726f97b2006-12-03 02:57:32 +0000113 InitBuiltinType(CharTy, BuiltinType::Char);
Chris Lattner970e54e2006-11-12 00:37:36 +0000114 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000115 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
116 InitBuiltinType(ShortTy, BuiltinType::Short);
117 InitBuiltinType(IntTy, BuiltinType::Int);
118 InitBuiltinType(LongTy, BuiltinType::Long);
119 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000120
121 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000122 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
123 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
124 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
125 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
126 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000127
128 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000129 InitBuiltinType(FloatTy, BuiltinType::Float);
130 InitBuiltinType(DoubleTy, BuiltinType::Double);
131 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Chris Lattner970e54e2006-11-12 00:37:36 +0000132
133 // C99 6.2.5p11.
Chris Lattner726f97b2006-12-03 02:57:32 +0000134 InitBuiltinType(FloatComplexTy, BuiltinType::FloatComplex);
135 InitBuiltinType(DoubleComplexTy, BuiltinType::DoubleComplex);
136 InitBuiltinType(LongDoubleComplexTy, BuiltinType::LongDoubleComplex);
Chris Lattner970e54e2006-11-12 00:37:36 +0000137}
138
139/// getPointerType - Return the uniqued reference to the type for a pointer to
140/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000141QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000142 // Unique pointers, to guarantee there is only one pointer of a particular
143 // structure.
Chris Lattner67521df2007-01-27 01:29:36 +0000144 FoldingSetNodeID ID;
145 PointerType::Profile(ID, T);
146
147 void *InsertPos = 0;
148 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000149 return QualType(PT, 0);
Chris Lattner970e54e2006-11-12 00:37:36 +0000150
Chris Lattner7ccecb92006-11-12 08:50:50 +0000151 // If the pointee type isn't canonical, this won't be a canonical type either,
152 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000153 QualType Canonical;
Chris Lattner67521df2007-01-27 01:29:36 +0000154 if (!T->isCanonical()) {
Steve Naroffd50c88e2007-04-05 21:15:20 +0000155 Canonical = getPointerType(T.getCanonicalType());
Chris Lattner67521df2007-01-27 01:29:36 +0000156
157 // Get the new insert position for the node we care about.
158 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
159 assert(NewIP == 0 && "Shouldn't be in the map!");
160 }
Chris Lattner67521df2007-01-27 01:29:36 +0000161 PointerType *New = new PointerType(T, Canonical);
162 Types.push_back(New);
163 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000164 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +0000165}
166
Bill Wendling3708c182007-05-27 10:15:43 +0000167/// getReferenceType - Return the uniqued reference to the type for a reference
168/// to the specified type.
169QualType ASTContext::getReferenceType(QualType T) {
170 // Unique pointers, to guarantee there is only one pointer of a particular
171 // structure.
172 FoldingSetNodeID ID;
173 ReferenceType::Profile(ID, T);
174
175 void *InsertPos = 0;
176 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
177 return QualType(RT, 0);
178
179 // If the referencee type isn't canonical, this won't be a canonical type
180 // either, so fill in the canonical type field.
181 QualType Canonical;
182 if (!T->isCanonical()) {
183 Canonical = getReferenceType(T.getCanonicalType());
184
185 // Get the new insert position for the node we care about.
186 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
187 assert(NewIP == 0 && "Shouldn't be in the map!");
188 }
189
190 ReferenceType *New = new ReferenceType(T, Canonical);
191 Types.push_back(New);
192 ReferenceTypes.InsertNode(New, InsertPos);
193 return QualType(New, 0);
194}
195
Chris Lattner7ccecb92006-11-12 08:50:50 +0000196/// getArrayType - Return the unique reference to the type for an array of the
197/// specified element type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000198QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
199 unsigned EltTypeQuals, Expr *NumElts) {
Chris Lattner36f8e652007-01-27 08:31:04 +0000200 // Unique array types, to guarantee there is only one array of a particular
Chris Lattner7ccecb92006-11-12 08:50:50 +0000201 // structure.
Chris Lattner36f8e652007-01-27 08:31:04 +0000202 FoldingSetNodeID ID;
Steve Naroffb7d49242007-03-14 19:55:17 +0000203 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
Steve Naroff408451b2007-02-26 22:17:12 +0000204
Chris Lattner36f8e652007-01-27 08:31:04 +0000205 void *InsertPos = 0;
206 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000207 return QualType(ATP, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000208
209 // If the element type isn't canonical, this won't be a canonical type either,
210 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000211 QualType Canonical;
Chris Lattner36f8e652007-01-27 08:31:04 +0000212 if (!EltTy->isCanonical()) {
Chris Lattner7ccecb92006-11-12 08:50:50 +0000213 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000214 NumElts);
Chris Lattner36f8e652007-01-27 08:31:04 +0000215
216 // Get the new insert position for the node we care about.
217 ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
218 assert(NewIP == 0 && "Shouldn't be in the map!");
219 }
Chris Lattner7ccecb92006-11-12 08:50:50 +0000220
Steve Naroffb7d49242007-03-14 19:55:17 +0000221 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
Chris Lattner36f8e652007-01-27 08:31:04 +0000222 ArrayTypes.InsertNode(New, InsertPos);
223 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000224 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000225}
226
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000227/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
228///
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000229QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000230 // Unique functions, to guarantee there is only one function of a particular
231 // structure.
Chris Lattner47955de2007-01-27 08:37:20 +0000232 FoldingSetNodeID ID;
233 FunctionTypeNoProto::Profile(ID, ResultTy);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000234
Chris Lattner47955de2007-01-27 08:37:20 +0000235 void *InsertPos = 0;
236 if (FunctionTypeNoProto *FT =
237 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000238 return QualType(FT, 0);
Chris Lattner47955de2007-01-27 08:37:20 +0000239
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000240 QualType Canonical;
Chris Lattner47955de2007-01-27 08:37:20 +0000241 if (!ResultTy->isCanonical()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000242 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
Chris Lattner47955de2007-01-27 08:37:20 +0000243
244 // Get the new insert position for the node we care about.
245 FunctionTypeNoProto *NewIP =
246 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
247 assert(NewIP == 0 && "Shouldn't be in the map!");
248 }
249
250 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
251 Types.push_back(New);
252 FunctionTypeProtos.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000253 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000254}
255
256/// getFunctionType - Return a normal function type with a typed argument
257/// list. isVariadic indicates whether the argument list includes '...'.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000258QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
259 unsigned NumArgs, bool isVariadic) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000260 // Unique functions, to guarantee there is only one function of a particular
261 // structure.
Chris Lattnerfd4de792007-01-27 01:15:32 +0000262 FoldingSetNodeID ID;
263 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
264
265 void *InsertPos = 0;
266 if (FunctionTypeProto *FTP =
267 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000268 return QualType(FTP, 0);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000269
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000270 // Determine whether the type being created is already canonical or not.
271 bool isCanonical = ResultTy->isCanonical();
272 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
273 if (!ArgArray[i]->isCanonical())
274 isCanonical = false;
275
276 // If this type isn't canonical, get the canonical version of it.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000277 QualType Canonical;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000278 if (!isCanonical) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000279 SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000280 CanonicalArgs.reserve(NumArgs);
281 for (unsigned i = 0; i != NumArgs; ++i)
282 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
283
284 Canonical = getFunctionType(ResultTy.getCanonicalType(),
285 &CanonicalArgs[0], NumArgs,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000286 isVariadic);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000287
288 // Get the new insert position for the node we care about.
289 FunctionTypeProto *NewIP =
290 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
291 assert(NewIP == 0 && "Shouldn't be in the map!");
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000292 }
293
294 // FunctionTypeProto objects are not allocated with new because they have a
295 // variable size array (for parameter types) at the end of them.
296 FunctionTypeProto *FTP =
297 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000298 (NumArgs-1)*sizeof(QualType));
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000299 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
300 Canonical);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000301 Types.push_back(FTP);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000302 FunctionTypeProtos.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000303 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000304}
Chris Lattneref51c202006-11-10 07:17:23 +0000305
Chris Lattner32d920b2007-01-26 02:01:53 +0000306/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +0000307/// specified typename decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000308QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
309 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000310
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000311 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Chris Lattnercceab1a2007-03-26 20:16:44 +0000312 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
313 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000314 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000315}
316
Chris Lattnerfb072462007-01-23 05:45:31 +0000317/// getTagDeclType - Return the unique reference to the type for the
318/// specified TagDecl (struct/union/class/enum) decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000319QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Chris Lattner733067d2007-01-26 01:42:24 +0000320 // The decl stores the type cache.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000321 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000322
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000323 Decl->TypeForDecl = new TagType(Decl, QualType());
Chris Lattnercceab1a2007-03-26 20:16:44 +0000324 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000325 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000326}
327
Steve Naroff92e30f82007-04-02 22:35:25 +0000328/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
Steve Naroff0f6256d2007-04-02 23:01:44 +0000329/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
Steve Naroff92e30f82007-04-02 22:35:25 +0000330/// needs to agree with the definition in <stddef.h>.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000331QualType ASTContext::getSizeType() const {
Steve Naroff92e30f82007-04-02 22:35:25 +0000332 // On Darwin, size_t is defined as a "long unsigned int".
333 // FIXME: should derive from "Target".
334 return UnsignedLongTy;
335}
Chris Lattnerfb072462007-01-23 05:45:31 +0000336
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000337/// getIntegerBitwidth - Return the bitwidth of the specified integer type
338/// according to the target. 'Loc' specifies the source location that
339/// requires evaluation of this property.
340unsigned ASTContext::getIntegerBitwidth(QualType T, SourceLocation Loc) {
341 if (const TagType *TT = dyn_cast<TagType>(T.getCanonicalType())) {
342 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
343 assert(0 && "FIXME: getIntegerBitwidth(enum) unimplemented!");
344 }
345
346 const BuiltinType *BT = cast<BuiltinType>(T.getCanonicalType());
347 switch (BT->getKind()) {
348 default: assert(0 && "getIntegerBitwidth(): not a built-in integer");
349 case BuiltinType::Bool: return Target.getBoolWidth(Loc);
350 case BuiltinType::Char:
351 case BuiltinType::SChar:
352 case BuiltinType::UChar: return Target.getCharWidth(Loc);
353 case BuiltinType::Short:
354 case BuiltinType::UShort: return Target.getShortWidth(Loc);
355 case BuiltinType::Int:
356 case BuiltinType::UInt: return Target.getIntWidth(Loc);
357 case BuiltinType::Long:
358 case BuiltinType::ULong: return Target.getLongWidth(Loc);
359 case BuiltinType::LongLong:
360 case BuiltinType::ULongLong: return Target.getLongLongWidth(Loc);
361 }
362}
363
Steve Naroff0af91202007-04-27 21:51:21 +0000364/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
365/// routine will assert if passed a built-in type that isn't an integer or enum.
366static int getIntegerRank(QualType t) {
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000367 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
368 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
369 return 4;
Steve Naroffe4718892007-04-27 18:30:00 +0000370 }
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000371
372 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
373 switch (BT->getKind()) {
374 default:
375 assert(0 && "getIntegerRank(): not a built-in integer");
376 case BuiltinType::Bool:
377 return 1;
378 case BuiltinType::Char:
379 case BuiltinType::SChar:
380 case BuiltinType::UChar:
381 return 2;
382 case BuiltinType::Short:
383 case BuiltinType::UShort:
384 return 3;
385 case BuiltinType::Int:
386 case BuiltinType::UInt:
387 return 4;
388 case BuiltinType::Long:
389 case BuiltinType::ULong:
390 return 5;
391 case BuiltinType::LongLong:
392 case BuiltinType::ULongLong:
393 return 6;
394 }
Steve Naroffe4718892007-04-27 18:30:00 +0000395}
396
Steve Naroff0af91202007-04-27 21:51:21 +0000397/// getFloatingRank - Return a relative rank for floating point types.
398/// This routine will assert if passed a built-in type that isn't a float.
399static int getFloatingRank(QualType t) {
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000400 switch (cast<BuiltinType>(t.getCanonicalType())->getKind()) {
Steve Naroffe4718892007-04-27 18:30:00 +0000401 default:
Steve Naroff0af91202007-04-27 21:51:21 +0000402 assert(0 && "getFloatingPointRank(): not a floating type");
403 case BuiltinType::Float:
404 case BuiltinType::FloatComplex:
405 return FloatRank;
406 case BuiltinType::Double:
407 case BuiltinType::DoubleComplex:
408 return DoubleRank;
409 case BuiltinType::LongDouble:
410 case BuiltinType::LongDoubleComplex:
411 return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +0000412 }
413}
414
415// maxComplexType - the following code handles 3 different combinations:
416// complex/complex, complex/float, float/complex.
417// When both operands are complex, the shorter operand is converted to the
418// type of the longer, and that is the type of the result. This corresponds
419// to what is done when combining two real floating-point operands.
420// The fun begins when size promotion occur across type domains. g
421// getFloatingRank & convertFloatingRankToComplexType handle this without
422// enumerating all permutations.
423// It also allows us to add new types without breakage.
424// From H&S 6.3.4: When one operand is complex and the other is a real
425// floating-point type, the less precise type is converted, within it's
426// real or complex domain, to the precision of the other type. For example,
427// when combining a "long double" with a "double _Complex", the
428// "double _Complex" is promoted to "long double _Complex".
429
Steve Naroff0af91202007-04-27 21:51:21 +0000430QualType ASTContext::maxComplexType(QualType lt, QualType rt) const {
431 switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) {
432 default:
433 assert(0 && "convertRankToComplex(): illegal value for rank");
434 case FloatRank:
435 return FloatComplexTy;
436 case DoubleRank:
437 return DoubleComplexTy;
438 case LongDoubleRank:
439 return LongDoubleComplexTy;
440 }
Steve Naroffe4718892007-04-27 18:30:00 +0000441}
442
443// maxFloatingType - handles the simple case, both operands are floats.
444QualType ASTContext::maxFloatingType(QualType lt, QualType rt) {
445 return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt;
446}
447
Steve Naroff0af91202007-04-27 21:51:21 +0000448// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
449// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
Steve Naroffe4718892007-04-27 18:30:00 +0000450QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
Steve Naroffe4718892007-04-27 18:30:00 +0000451 bool t1Unsigned = lhs->isUnsignedIntegerType();
452 bool t2Unsigned = rhs->isUnsignedIntegerType();
453
454 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
455 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
456
457 // We have two integer types with differing signs
458 QualType unsignedType = t1Unsigned ? lhs : rhs;
459 QualType signedType = t1Unsigned ? rhs : lhs;
460
461 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
462 return unsignedType;
Steve Naroff0af91202007-04-27 21:51:21 +0000463 else {
464 // FIXME: Need to check if the signed type can represent all values of the
465 // unsigned type. If it can, then the result is the signed type.
466 // If it can't, then the result is the unsigned version of the signed type.
467 // Should probably add a helper that returns a signed integer type from
468 // an unsigned (and vice versa). C99 6.3.1.8.
469 return signedType;
470 }
Steve Naroffe4718892007-04-27 18:30:00 +0000471}