blob: 36eda88ed1775ba0da7793927b3d445912926e82 [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 Lattnerb16f4552007-06-03 07:25:34 +0000113 if (Target.isCharSigned(SourceLocation()))
114 InitBuiltinType(CharTy, BuiltinType::Char_S);
115 else
116 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000117 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000118 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
119 InitBuiltinType(ShortTy, BuiltinType::Short);
120 InitBuiltinType(IntTy, BuiltinType::Int);
121 InitBuiltinType(LongTy, BuiltinType::Long);
122 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000123
124 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000125 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
126 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
127 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
128 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
129 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000130
131 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000132 InitBuiltinType(FloatTy, BuiltinType::Float);
133 InitBuiltinType(DoubleTy, BuiltinType::Double);
134 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Chris Lattner970e54e2006-11-12 00:37:36 +0000135
136 // C99 6.2.5p11.
Chris Lattner726f97b2006-12-03 02:57:32 +0000137 InitBuiltinType(FloatComplexTy, BuiltinType::FloatComplex);
138 InitBuiltinType(DoubleComplexTy, BuiltinType::DoubleComplex);
139 InitBuiltinType(LongDoubleComplexTy, BuiltinType::LongDoubleComplex);
Chris Lattner970e54e2006-11-12 00:37:36 +0000140}
141
142/// getPointerType - Return the uniqued reference to the type for a pointer to
143/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000144QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000145 // Unique pointers, to guarantee there is only one pointer of a particular
146 // structure.
Chris Lattner67521df2007-01-27 01:29:36 +0000147 FoldingSetNodeID ID;
148 PointerType::Profile(ID, T);
149
150 void *InsertPos = 0;
151 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000152 return QualType(PT, 0);
Chris Lattner970e54e2006-11-12 00:37:36 +0000153
Chris Lattner7ccecb92006-11-12 08:50:50 +0000154 // If the pointee type isn't canonical, this won't be a canonical type either,
155 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000156 QualType Canonical;
Chris Lattner67521df2007-01-27 01:29:36 +0000157 if (!T->isCanonical()) {
Steve Naroffd50c88e2007-04-05 21:15:20 +0000158 Canonical = getPointerType(T.getCanonicalType());
Chris Lattner67521df2007-01-27 01:29:36 +0000159
160 // Get the new insert position for the node we care about.
161 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
162 assert(NewIP == 0 && "Shouldn't be in the map!");
163 }
Chris Lattner67521df2007-01-27 01:29:36 +0000164 PointerType *New = new PointerType(T, Canonical);
165 Types.push_back(New);
166 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000167 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +0000168}
169
Bill Wendling3708c182007-05-27 10:15:43 +0000170/// getReferenceType - Return the uniqued reference to the type for a reference
171/// to the specified type.
172QualType ASTContext::getReferenceType(QualType T) {
173 // Unique pointers, to guarantee there is only one pointer of a particular
174 // structure.
175 FoldingSetNodeID ID;
176 ReferenceType::Profile(ID, T);
177
178 void *InsertPos = 0;
179 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
180 return QualType(RT, 0);
181
182 // If the referencee type isn't canonical, this won't be a canonical type
183 // either, so fill in the canonical type field.
184 QualType Canonical;
185 if (!T->isCanonical()) {
186 Canonical = getReferenceType(T.getCanonicalType());
187
188 // Get the new insert position for the node we care about.
189 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
190 assert(NewIP == 0 && "Shouldn't be in the map!");
191 }
192
193 ReferenceType *New = new ReferenceType(T, Canonical);
194 Types.push_back(New);
195 ReferenceTypes.InsertNode(New, InsertPos);
196 return QualType(New, 0);
197}
198
Chris Lattner7ccecb92006-11-12 08:50:50 +0000199/// getArrayType - Return the unique reference to the type for an array of the
200/// specified element type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000201QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
202 unsigned EltTypeQuals, Expr *NumElts) {
Chris Lattner36f8e652007-01-27 08:31:04 +0000203 // Unique array types, to guarantee there is only one array of a particular
Chris Lattner7ccecb92006-11-12 08:50:50 +0000204 // structure.
Chris Lattner36f8e652007-01-27 08:31:04 +0000205 FoldingSetNodeID ID;
Steve Naroffb7d49242007-03-14 19:55:17 +0000206 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
Steve Naroff408451b2007-02-26 22:17:12 +0000207
Chris Lattner36f8e652007-01-27 08:31:04 +0000208 void *InsertPos = 0;
209 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000210 return QualType(ATP, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000211
212 // If the element type isn't canonical, this won't be a canonical type either,
213 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000214 QualType Canonical;
Chris Lattner36f8e652007-01-27 08:31:04 +0000215 if (!EltTy->isCanonical()) {
Chris Lattner7ccecb92006-11-12 08:50:50 +0000216 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000217 NumElts);
Chris Lattner36f8e652007-01-27 08:31:04 +0000218
219 // Get the new insert position for the node we care about.
220 ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
221 assert(NewIP == 0 && "Shouldn't be in the map!");
222 }
Chris Lattner7ccecb92006-11-12 08:50:50 +0000223
Steve Naroffb7d49242007-03-14 19:55:17 +0000224 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
Chris Lattner36f8e652007-01-27 08:31:04 +0000225 ArrayTypes.InsertNode(New, InsertPos);
226 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000227 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000228}
229
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000230/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
231///
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000232QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000233 // Unique functions, to guarantee there is only one function of a particular
234 // structure.
Chris Lattner47955de2007-01-27 08:37:20 +0000235 FoldingSetNodeID ID;
236 FunctionTypeNoProto::Profile(ID, ResultTy);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000237
Chris Lattner47955de2007-01-27 08:37:20 +0000238 void *InsertPos = 0;
239 if (FunctionTypeNoProto *FT =
240 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000241 return QualType(FT, 0);
Chris Lattner47955de2007-01-27 08:37:20 +0000242
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000243 QualType Canonical;
Chris Lattner47955de2007-01-27 08:37:20 +0000244 if (!ResultTy->isCanonical()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000245 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
Chris Lattner47955de2007-01-27 08:37:20 +0000246
247 // Get the new insert position for the node we care about.
248 FunctionTypeNoProto *NewIP =
249 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
250 assert(NewIP == 0 && "Shouldn't be in the map!");
251 }
252
253 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
254 Types.push_back(New);
255 FunctionTypeProtos.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000256 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000257}
258
259/// getFunctionType - Return a normal function type with a typed argument
260/// list. isVariadic indicates whether the argument list includes '...'.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000261QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
262 unsigned NumArgs, bool isVariadic) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000263 // Unique functions, to guarantee there is only one function of a particular
264 // structure.
Chris Lattnerfd4de792007-01-27 01:15:32 +0000265 FoldingSetNodeID ID;
266 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
267
268 void *InsertPos = 0;
269 if (FunctionTypeProto *FTP =
270 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000271 return QualType(FTP, 0);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000272
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000273 // Determine whether the type being created is already canonical or not.
274 bool isCanonical = ResultTy->isCanonical();
275 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
276 if (!ArgArray[i]->isCanonical())
277 isCanonical = false;
278
279 // If this type isn't canonical, get the canonical version of it.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000280 QualType Canonical;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000281 if (!isCanonical) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000282 SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000283 CanonicalArgs.reserve(NumArgs);
284 for (unsigned i = 0; i != NumArgs; ++i)
285 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
286
287 Canonical = getFunctionType(ResultTy.getCanonicalType(),
288 &CanonicalArgs[0], NumArgs,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000289 isVariadic);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000290
291 // Get the new insert position for the node we care about.
292 FunctionTypeProto *NewIP =
293 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
294 assert(NewIP == 0 && "Shouldn't be in the map!");
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000295 }
296
297 // FunctionTypeProto objects are not allocated with new because they have a
298 // variable size array (for parameter types) at the end of them.
299 FunctionTypeProto *FTP =
300 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000301 (NumArgs-1)*sizeof(QualType));
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000302 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
303 Canonical);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000304 Types.push_back(FTP);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000305 FunctionTypeProtos.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000306 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000307}
Chris Lattneref51c202006-11-10 07:17:23 +0000308
Chris Lattner32d920b2007-01-26 02:01:53 +0000309/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +0000310/// specified typename decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000311QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
312 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000313
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000314 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Chris Lattnercceab1a2007-03-26 20:16:44 +0000315 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
316 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000317 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000318}
319
Chris Lattnerfb072462007-01-23 05:45:31 +0000320/// getTagDeclType - Return the unique reference to the type for the
321/// specified TagDecl (struct/union/class/enum) decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000322QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Chris Lattner733067d2007-01-26 01:42:24 +0000323 // The decl stores the type cache.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000324 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000325
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000326 Decl->TypeForDecl = new TagType(Decl, QualType());
Chris Lattnercceab1a2007-03-26 20:16:44 +0000327 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000328 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000329}
330
Steve Naroff92e30f82007-04-02 22:35:25 +0000331/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
Steve Naroff0f6256d2007-04-02 23:01:44 +0000332/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
Steve Naroff92e30f82007-04-02 22:35:25 +0000333/// needs to agree with the definition in <stddef.h>.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000334QualType ASTContext::getSizeType() const {
Steve Naroff92e30f82007-04-02 22:35:25 +0000335 // On Darwin, size_t is defined as a "long unsigned int".
336 // FIXME: should derive from "Target".
337 return UnsignedLongTy;
338}
Chris Lattnerfb072462007-01-23 05:45:31 +0000339
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000340/// getIntegerBitwidth - Return the bitwidth of the specified integer type
341/// according to the target. 'Loc' specifies the source location that
342/// requires evaluation of this property.
343unsigned ASTContext::getIntegerBitwidth(QualType T, SourceLocation Loc) {
344 if (const TagType *TT = dyn_cast<TagType>(T.getCanonicalType())) {
345 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
346 assert(0 && "FIXME: getIntegerBitwidth(enum) unimplemented!");
347 }
348
349 const BuiltinType *BT = cast<BuiltinType>(T.getCanonicalType());
350 switch (BT->getKind()) {
351 default: assert(0 && "getIntegerBitwidth(): not a built-in integer");
352 case BuiltinType::Bool: return Target.getBoolWidth(Loc);
Chris Lattnerb16f4552007-06-03 07:25:34 +0000353 case BuiltinType::Char_S:
354 case BuiltinType::Char_U:
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000355 case BuiltinType::SChar:
356 case BuiltinType::UChar: return Target.getCharWidth(Loc);
357 case BuiltinType::Short:
358 case BuiltinType::UShort: return Target.getShortWidth(Loc);
359 case BuiltinType::Int:
360 case BuiltinType::UInt: return Target.getIntWidth(Loc);
361 case BuiltinType::Long:
362 case BuiltinType::ULong: return Target.getLongWidth(Loc);
363 case BuiltinType::LongLong:
364 case BuiltinType::ULongLong: return Target.getLongLongWidth(Loc);
365 }
366}
367
Steve Naroff0af91202007-04-27 21:51:21 +0000368/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
369/// routine will assert if passed a built-in type that isn't an integer or enum.
370static int getIntegerRank(QualType t) {
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000371 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
372 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
373 return 4;
Steve Naroffe4718892007-04-27 18:30:00 +0000374 }
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000375
376 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
377 switch (BT->getKind()) {
378 default:
379 assert(0 && "getIntegerRank(): not a built-in integer");
380 case BuiltinType::Bool:
381 return 1;
Chris Lattnerb16f4552007-06-03 07:25:34 +0000382 case BuiltinType::Char_S:
383 case BuiltinType::Char_U:
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000384 case BuiltinType::SChar:
385 case BuiltinType::UChar:
386 return 2;
387 case BuiltinType::Short:
388 case BuiltinType::UShort:
389 return 3;
390 case BuiltinType::Int:
391 case BuiltinType::UInt:
392 return 4;
393 case BuiltinType::Long:
394 case BuiltinType::ULong:
395 return 5;
396 case BuiltinType::LongLong:
397 case BuiltinType::ULongLong:
398 return 6;
399 }
Steve Naroffe4718892007-04-27 18:30:00 +0000400}
401
Steve Naroff0af91202007-04-27 21:51:21 +0000402/// getFloatingRank - Return a relative rank for floating point types.
403/// This routine will assert if passed a built-in type that isn't a float.
404static int getFloatingRank(QualType t) {
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000405 switch (cast<BuiltinType>(t.getCanonicalType())->getKind()) {
Steve Naroffe4718892007-04-27 18:30:00 +0000406 default:
Steve Naroff0af91202007-04-27 21:51:21 +0000407 assert(0 && "getFloatingPointRank(): not a floating type");
408 case BuiltinType::Float:
409 case BuiltinType::FloatComplex:
410 return FloatRank;
411 case BuiltinType::Double:
412 case BuiltinType::DoubleComplex:
413 return DoubleRank;
414 case BuiltinType::LongDouble:
415 case BuiltinType::LongDoubleComplex:
416 return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +0000417 }
418}
419
420// maxComplexType - the following code handles 3 different combinations:
421// complex/complex, complex/float, float/complex.
422// When both operands are complex, the shorter operand is converted to the
423// type of the longer, and that is the type of the result. This corresponds
424// to what is done when combining two real floating-point operands.
425// The fun begins when size promotion occur across type domains. g
426// getFloatingRank & convertFloatingRankToComplexType handle this without
427// enumerating all permutations.
428// It also allows us to add new types without breakage.
429// From H&S 6.3.4: When one operand is complex and the other is a real
430// floating-point type, the less precise type is converted, within it's
431// real or complex domain, to the precision of the other type. For example,
432// when combining a "long double" with a "double _Complex", the
433// "double _Complex" is promoted to "long double _Complex".
434
Steve Naroff0af91202007-04-27 21:51:21 +0000435QualType ASTContext::maxComplexType(QualType lt, QualType rt) const {
436 switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) {
437 default:
438 assert(0 && "convertRankToComplex(): illegal value for rank");
439 case FloatRank:
440 return FloatComplexTy;
441 case DoubleRank:
442 return DoubleComplexTy;
443 case LongDoubleRank:
444 return LongDoubleComplexTy;
445 }
Steve Naroffe4718892007-04-27 18:30:00 +0000446}
447
448// maxFloatingType - handles the simple case, both operands are floats.
449QualType ASTContext::maxFloatingType(QualType lt, QualType rt) {
450 return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt;
451}
452
Steve Naroff0af91202007-04-27 21:51:21 +0000453// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
454// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
Steve Naroffe4718892007-04-27 18:30:00 +0000455QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
Chris Lattnerb16f4552007-06-03 07:25:34 +0000456 if (lhs == rhs) return lhs;
457
Steve Naroffe4718892007-04-27 18:30:00 +0000458 bool t1Unsigned = lhs->isUnsignedIntegerType();
459 bool t2Unsigned = rhs->isUnsignedIntegerType();
460
461 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
462 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
463
464 // We have two integer types with differing signs
465 QualType unsignedType = t1Unsigned ? lhs : rhs;
466 QualType signedType = t1Unsigned ? rhs : lhs;
467
468 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
469 return unsignedType;
Steve Naroff0af91202007-04-27 21:51:21 +0000470 else {
471 // FIXME: Need to check if the signed type can represent all values of the
472 // unsigned type. If it can, then the result is the signed type.
473 // If it can't, then the result is the unsigned version of the signed type.
474 // Should probably add a helper that returns a signed integer type from
475 // an unsigned (and vice versa). C99 6.3.1.8.
476 return signedType;
477 }
Steve Naroffe4718892007-04-27 18:30:00 +0000478}