blob: 6bf01473554382f4aca87b37cd692cffe573ec8f [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"
Chris Lattnerddc135e2006-11-10 06:34:16 +000019using namespace clang;
20
Steve Naroff0af91202007-04-27 21:51:21 +000021enum FloatingRank {
22 FloatRank, DoubleRank, LongDoubleRank
23};
24
Chris Lattnerd5973eb2006-11-12 00:53:46 +000025ASTContext::~ASTContext() {
26 // Deallocate all the types.
27 while (!Types.empty()) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +000028 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 }
Chris Lattnerd5973eb2006-11-12 00:53:46 +000035 Types.pop_back();
36 }
37}
38
Chris Lattner4eb445d2007-01-26 01:27:23 +000039void 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;
Bill Wendling3708c182007-05-27 10:15:43 +000043 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Chris Lattner4eb445d2007-01-26 01:27:23 +000044
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;
Bill Wendling3708c182007-05-27 10:15:43 +000053 else if (isa<ReferenceType>(T))
54 ++NumReference;
Chris Lattner4eb445d2007-01-26 01:27:23 +000055 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);
Bill Wendling3708c182007-05-27 10:15:43 +000079 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner4eb445d2007-01-26 01:27:23 +000080 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);
Chris Lattnerfc234de2007-05-24 00:40:54 +000089 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
Steve Narofff84d11f2007-05-23 21:48:04 +000090 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattnerfc234de2007-05-24 00:40:54 +000091 NumFunctionP*sizeof(FunctionTypeProto)+
92 NumFunctionNP*sizeof(FunctionTypeNoProto)+
93 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
Chris Lattner4eb445d2007-01-26 01:27:23 +000094}
95
96
Steve Naroffe5aa9be2007-04-05 22:36:20 +000097void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
98 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
Chris Lattnerd5973eb2006-11-12 00:53:46 +000099}
100
101
Chris Lattner970e54e2006-11-12 00:37:36 +0000102void ASTContext::InitBuiltinTypes() {
103 assert(VoidTy.isNull() && "Context reinitialized?");
104
105 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000106 InitBuiltinType(VoidTy, BuiltinType::Void);
Chris Lattner970e54e2006-11-12 00:37:36 +0000107
108 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000109 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000110 // C99 6.2.5p3.
Chris Lattnerb16f4552007-06-03 07:25:34 +0000111 if (Target.isCharSigned(SourceLocation()))
112 InitBuiltinType(CharTy, BuiltinType::Char_S);
113 else
114 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000115 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000116 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
117 InitBuiltinType(ShortTy, BuiltinType::Short);
118 InitBuiltinType(IntTy, BuiltinType::Int);
119 InitBuiltinType(LongTy, BuiltinType::Long);
120 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000121
122 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000123 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
124 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
125 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
126 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
127 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000128
129 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000130 InitBuiltinType(FloatTy, BuiltinType::Float);
131 InitBuiltinType(DoubleTy, BuiltinType::Double);
132 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Chris Lattner970e54e2006-11-12 00:37:36 +0000133
134 // C99 6.2.5p11.
Chris Lattner726f97b2006-12-03 02:57:32 +0000135 InitBuiltinType(FloatComplexTy, BuiltinType::FloatComplex);
136 InitBuiltinType(DoubleComplexTy, BuiltinType::DoubleComplex);
137 InitBuiltinType(LongDoubleComplexTy, BuiltinType::LongDoubleComplex);
Chris Lattner970e54e2006-11-12 00:37:36 +0000138}
139
140/// getPointerType - Return the uniqued reference to the type for a pointer to
141/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000142QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000143 // Unique pointers, to guarantee there is only one pointer of a particular
144 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000145 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +0000146 PointerType::Profile(ID, T);
147
148 void *InsertPos = 0;
149 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000150 return QualType(PT, 0);
Chris Lattner970e54e2006-11-12 00:37:36 +0000151
Chris Lattner7ccecb92006-11-12 08:50:50 +0000152 // If the pointee type isn't canonical, this won't be a canonical type either,
153 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000154 QualType Canonical;
Chris Lattner67521df2007-01-27 01:29:36 +0000155 if (!T->isCanonical()) {
Steve Naroffd50c88e2007-04-05 21:15:20 +0000156 Canonical = getPointerType(T.getCanonicalType());
Chris Lattner67521df2007-01-27 01:29:36 +0000157
158 // Get the new insert position for the node we care about.
159 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
160 assert(NewIP == 0 && "Shouldn't be in the map!");
161 }
Chris Lattner67521df2007-01-27 01:29:36 +0000162 PointerType *New = new PointerType(T, Canonical);
163 Types.push_back(New);
164 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000165 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +0000166}
167
Bill Wendling3708c182007-05-27 10:15:43 +0000168/// getReferenceType - Return the uniqued reference to the type for a reference
169/// to the specified type.
170QualType ASTContext::getReferenceType(QualType T) {
171 // Unique pointers, to guarantee there is only one pointer of a particular
172 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000173 llvm::FoldingSetNodeID ID;
Bill Wendling3708c182007-05-27 10:15:43 +0000174 ReferenceType::Profile(ID, T);
175
176 void *InsertPos = 0;
177 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
178 return QualType(RT, 0);
179
180 // If the referencee type isn't canonical, this won't be a canonical type
181 // either, so fill in the canonical type field.
182 QualType Canonical;
183 if (!T->isCanonical()) {
184 Canonical = getReferenceType(T.getCanonicalType());
185
186 // Get the new insert position for the node we care about.
187 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
188 assert(NewIP == 0 && "Shouldn't be in the map!");
189 }
190
191 ReferenceType *New = new ReferenceType(T, Canonical);
192 Types.push_back(New);
193 ReferenceTypes.InsertNode(New, InsertPos);
194 return QualType(New, 0);
195}
196
Chris Lattner7ccecb92006-11-12 08:50:50 +0000197/// getArrayType - Return the unique reference to the type for an array of the
198/// specified element type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000199QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
200 unsigned EltTypeQuals, Expr *NumElts) {
Chris Lattner36f8e652007-01-27 08:31:04 +0000201 // Unique array types, to guarantee there is only one array of a particular
Chris Lattner7ccecb92006-11-12 08:50:50 +0000202 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000203 llvm::FoldingSetNodeID ID;
Steve Naroffb7d49242007-03-14 19:55:17 +0000204 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
Steve Naroff408451b2007-02-26 22:17:12 +0000205
Chris Lattner36f8e652007-01-27 08:31:04 +0000206 void *InsertPos = 0;
207 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000208 return QualType(ATP, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000209
210 // If the element type isn't canonical, this won't be a canonical type either,
211 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000212 QualType Canonical;
Chris Lattner36f8e652007-01-27 08:31:04 +0000213 if (!EltTy->isCanonical()) {
Chris Lattner7ccecb92006-11-12 08:50:50 +0000214 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000215 NumElts);
Chris Lattner36f8e652007-01-27 08:31:04 +0000216
217 // Get the new insert position for the node we care about.
218 ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
219 assert(NewIP == 0 && "Shouldn't be in the map!");
220 }
Chris Lattner7ccecb92006-11-12 08:50:50 +0000221
Steve Naroffb7d49242007-03-14 19:55:17 +0000222 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
Chris Lattner36f8e652007-01-27 08:31:04 +0000223 ArrayTypes.InsertNode(New, InsertPos);
224 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000225 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000226}
227
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000228/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
229///
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000230QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000231 // Unique functions, to guarantee there is only one function of a particular
232 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000233 llvm::FoldingSetNodeID ID;
Chris Lattner47955de2007-01-27 08:37:20 +0000234 FunctionTypeNoProto::Profile(ID, ResultTy);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000235
Chris Lattner47955de2007-01-27 08:37:20 +0000236 void *InsertPos = 0;
237 if (FunctionTypeNoProto *FT =
238 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000239 return QualType(FT, 0);
Chris Lattner47955de2007-01-27 08:37:20 +0000240
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000241 QualType Canonical;
Chris Lattner47955de2007-01-27 08:37:20 +0000242 if (!ResultTy->isCanonical()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000243 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
Chris Lattner47955de2007-01-27 08:37:20 +0000244
245 // Get the new insert position for the node we care about.
246 FunctionTypeNoProto *NewIP =
247 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
248 assert(NewIP == 0 && "Shouldn't be in the map!");
249 }
250
251 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
252 Types.push_back(New);
253 FunctionTypeProtos.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000254 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000255}
256
257/// getFunctionType - Return a normal function type with a typed argument
258/// list. isVariadic indicates whether the argument list includes '...'.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000259QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
260 unsigned NumArgs, bool isVariadic) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000261 // Unique functions, to guarantee there is only one function of a particular
262 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000263 llvm::FoldingSetNodeID ID;
Chris Lattnerfd4de792007-01-27 01:15:32 +0000264 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
265
266 void *InsertPos = 0;
267 if (FunctionTypeProto *FTP =
268 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000269 return QualType(FTP, 0);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000270
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000271 // Determine whether the type being created is already canonical or not.
272 bool isCanonical = ResultTy->isCanonical();
273 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
274 if (!ArgArray[i]->isCanonical())
275 isCanonical = false;
276
277 // If this type isn't canonical, get the canonical version of it.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000278 QualType Canonical;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000279 if (!isCanonical) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000280 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000281 CanonicalArgs.reserve(NumArgs);
282 for (unsigned i = 0; i != NumArgs; ++i)
283 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
284
285 Canonical = getFunctionType(ResultTy.getCanonicalType(),
286 &CanonicalArgs[0], NumArgs,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000287 isVariadic);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000288
289 // Get the new insert position for the node we care about.
290 FunctionTypeProto *NewIP =
291 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
292 assert(NewIP == 0 && "Shouldn't be in the map!");
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000293 }
294
295 // FunctionTypeProto objects are not allocated with new because they have a
296 // variable size array (for parameter types) at the end of them.
297 FunctionTypeProto *FTP =
298 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000299 (NumArgs-1)*sizeof(QualType));
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000300 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
301 Canonical);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000302 Types.push_back(FTP);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000303 FunctionTypeProtos.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000304 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000305}
Chris Lattneref51c202006-11-10 07:17:23 +0000306
Chris Lattner32d920b2007-01-26 02:01:53 +0000307/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +0000308/// specified typename decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000309QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
310 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000311
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000312 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Chris Lattnercceab1a2007-03-26 20:16:44 +0000313 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
314 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000315 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000316}
317
Chris Lattnerfb072462007-01-23 05:45:31 +0000318/// getTagDeclType - Return the unique reference to the type for the
319/// specified TagDecl (struct/union/class/enum) decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000320QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Chris Lattner733067d2007-01-26 01:42:24 +0000321 // The decl stores the type cache.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000322 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000323
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000324 Decl->TypeForDecl = new TagType(Decl, QualType());
Chris Lattnercceab1a2007-03-26 20:16:44 +0000325 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000326 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000327}
328
Steve Naroff92e30f82007-04-02 22:35:25 +0000329/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
Steve Naroff0f6256d2007-04-02 23:01:44 +0000330/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
Steve Naroff92e30f82007-04-02 22:35:25 +0000331/// needs to agree with the definition in <stddef.h>.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000332QualType ASTContext::getSizeType() const {
Steve Naroff92e30f82007-04-02 22:35:25 +0000333 // On Darwin, size_t is defined as a "long unsigned int".
334 // FIXME: should derive from "Target".
335 return UnsignedLongTy;
336}
Chris Lattnerfb072462007-01-23 05:45:31 +0000337
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000338/// getIntegerBitwidth - Return the bitwidth of the specified integer type
339/// according to the target. 'Loc' specifies the source location that
340/// requires evaluation of this property.
341unsigned ASTContext::getIntegerBitwidth(QualType T, SourceLocation Loc) {
342 if (const TagType *TT = dyn_cast<TagType>(T.getCanonicalType())) {
343 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
344 assert(0 && "FIXME: getIntegerBitwidth(enum) unimplemented!");
345 }
346
347 const BuiltinType *BT = cast<BuiltinType>(T.getCanonicalType());
348 switch (BT->getKind()) {
349 default: assert(0 && "getIntegerBitwidth(): not a built-in integer");
350 case BuiltinType::Bool: return Target.getBoolWidth(Loc);
Chris Lattnerb16f4552007-06-03 07:25:34 +0000351 case BuiltinType::Char_S:
352 case BuiltinType::Char_U:
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000353 case BuiltinType::SChar:
354 case BuiltinType::UChar: return Target.getCharWidth(Loc);
355 case BuiltinType::Short:
356 case BuiltinType::UShort: return Target.getShortWidth(Loc);
357 case BuiltinType::Int:
358 case BuiltinType::UInt: return Target.getIntWidth(Loc);
359 case BuiltinType::Long:
360 case BuiltinType::ULong: return Target.getLongWidth(Loc);
361 case BuiltinType::LongLong:
362 case BuiltinType::ULongLong: return Target.getLongLongWidth(Loc);
363 }
364}
365
Steve Naroff0af91202007-04-27 21:51:21 +0000366/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
367/// routine will assert if passed a built-in type that isn't an integer or enum.
368static int getIntegerRank(QualType t) {
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000369 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
370 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
371 return 4;
Steve Naroffe4718892007-04-27 18:30:00 +0000372 }
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000373
374 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
375 switch (BT->getKind()) {
376 default:
377 assert(0 && "getIntegerRank(): not a built-in integer");
378 case BuiltinType::Bool:
379 return 1;
Chris Lattnerb16f4552007-06-03 07:25:34 +0000380 case BuiltinType::Char_S:
381 case BuiltinType::Char_U:
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000382 case BuiltinType::SChar:
383 case BuiltinType::UChar:
384 return 2;
385 case BuiltinType::Short:
386 case BuiltinType::UShort:
387 return 3;
388 case BuiltinType::Int:
389 case BuiltinType::UInt:
390 return 4;
391 case BuiltinType::Long:
392 case BuiltinType::ULong:
393 return 5;
394 case BuiltinType::LongLong:
395 case BuiltinType::ULongLong:
396 return 6;
397 }
Steve Naroffe4718892007-04-27 18:30:00 +0000398}
399
Steve Naroff0af91202007-04-27 21:51:21 +0000400/// getFloatingRank - Return a relative rank for floating point types.
401/// This routine will assert if passed a built-in type that isn't a float.
402static int getFloatingRank(QualType t) {
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000403 switch (cast<BuiltinType>(t.getCanonicalType())->getKind()) {
Steve Naroffe4718892007-04-27 18:30:00 +0000404 default:
Steve Naroff0af91202007-04-27 21:51:21 +0000405 assert(0 && "getFloatingPointRank(): not a floating type");
406 case BuiltinType::Float:
407 case BuiltinType::FloatComplex:
408 return FloatRank;
409 case BuiltinType::Double:
410 case BuiltinType::DoubleComplex:
411 return DoubleRank;
412 case BuiltinType::LongDouble:
413 case BuiltinType::LongDoubleComplex:
414 return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +0000415 }
416}
417
418// maxComplexType - the following code handles 3 different combinations:
419// complex/complex, complex/float, float/complex.
420// When both operands are complex, the shorter operand is converted to the
421// type of the longer, and that is the type of the result. This corresponds
422// to what is done when combining two real floating-point operands.
423// The fun begins when size promotion occur across type domains. g
424// getFloatingRank & convertFloatingRankToComplexType handle this without
425// enumerating all permutations.
426// It also allows us to add new types without breakage.
427// From H&S 6.3.4: When one operand is complex and the other is a real
428// floating-point type, the less precise type is converted, within it's
429// real or complex domain, to the precision of the other type. For example,
430// when combining a "long double" with a "double _Complex", the
431// "double _Complex" is promoted to "long double _Complex".
432
Steve Naroff0af91202007-04-27 21:51:21 +0000433QualType ASTContext::maxComplexType(QualType lt, QualType rt) const {
434 switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) {
435 default:
436 assert(0 && "convertRankToComplex(): illegal value for rank");
437 case FloatRank:
438 return FloatComplexTy;
439 case DoubleRank:
440 return DoubleComplexTy;
441 case LongDoubleRank:
442 return LongDoubleComplexTy;
443 }
Steve Naroffe4718892007-04-27 18:30:00 +0000444}
445
446// maxFloatingType - handles the simple case, both operands are floats.
447QualType ASTContext::maxFloatingType(QualType lt, QualType rt) {
448 return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt;
449}
450
Steve Naroff0af91202007-04-27 21:51:21 +0000451// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
452// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
Steve Naroffe4718892007-04-27 18:30:00 +0000453QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
Chris Lattnerb16f4552007-06-03 07:25:34 +0000454 if (lhs == rhs) return lhs;
455
Steve Naroffe4718892007-04-27 18:30:00 +0000456 bool t1Unsigned = lhs->isUnsignedIntegerType();
457 bool t2Unsigned = rhs->isUnsignedIntegerType();
458
459 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
460 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
461
462 // We have two integer types with differing signs
463 QualType unsignedType = t1Unsigned ? lhs : rhs;
464 QualType signedType = t1Unsigned ? rhs : lhs;
465
466 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
467 return unsignedType;
Steve Naroff0af91202007-04-27 21:51:21 +0000468 else {
469 // FIXME: Need to check if the signed type can represent all values of the
470 // unsigned type. If it can, then the result is the signed type.
471 // If it can't, then the result is the unsigned version of the signed type.
472 // Should probably add a helper that returns a signed integer type from
473 // an unsigned (and vice versa). C99 6.3.1.8.
474 return signedType;
475 }
Steve Naroffe4718892007-04-27 18:30:00 +0000476}