blob: 1ca675298f13d9b3f3c424203ac81c1414c1a0a1 [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 Lattnerc6395932007-06-22 20:56:16 +0000135 FloatComplexTy = getComplexType(FloatTy);
136 DoubleComplexTy = getComplexType(DoubleTy);
137 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Chris Lattner970e54e2006-11-12 00:37:36 +0000138}
139
Chris Lattnerc6395932007-06-22 20:56:16 +0000140/// getComplexType - Return the uniqued reference to the type for a complex
141/// number with the specified element type.
142QualType ASTContext::getComplexType(QualType T) {
143 // Unique pointers, to guarantee there is only one pointer of a particular
144 // structure.
145 llvm::FoldingSetNodeID ID;
146 ComplexType::Profile(ID, T);
147
148 void *InsertPos = 0;
149 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
150 return QualType(CT, 0);
151
152 // If the pointee type isn't canonical, this won't be a canonical type either,
153 // so fill in the canonical type field.
154 QualType Canonical;
155 if (!T->isCanonical()) {
156 Canonical = getComplexType(T.getCanonicalType());
157
158 // Get the new insert position for the node we care about.
159 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
160 assert(NewIP == 0 && "Shouldn't be in the map!");
161 }
162 ComplexType *New = new ComplexType(T, Canonical);
163 Types.push_back(New);
164 ComplexTypes.InsertNode(New, InsertPos);
165 return QualType(New, 0);
166}
167
168
Chris Lattner970e54e2006-11-12 00:37:36 +0000169/// getPointerType - Return the uniqued reference to the type for a pointer to
170/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000171QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000172 // Unique pointers, to guarantee there is only one pointer of a particular
173 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000174 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +0000175 PointerType::Profile(ID, T);
176
177 void *InsertPos = 0;
178 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000179 return QualType(PT, 0);
Chris Lattner970e54e2006-11-12 00:37:36 +0000180
Chris Lattner7ccecb92006-11-12 08:50:50 +0000181 // If the pointee type isn't canonical, this won't be a canonical type either,
182 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000183 QualType Canonical;
Chris Lattner67521df2007-01-27 01:29:36 +0000184 if (!T->isCanonical()) {
Steve Naroffd50c88e2007-04-05 21:15:20 +0000185 Canonical = getPointerType(T.getCanonicalType());
Chris Lattner67521df2007-01-27 01:29:36 +0000186
187 // Get the new insert position for the node we care about.
188 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
189 assert(NewIP == 0 && "Shouldn't be in the map!");
190 }
Chris Lattner67521df2007-01-27 01:29:36 +0000191 PointerType *New = new PointerType(T, Canonical);
192 Types.push_back(New);
193 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000194 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +0000195}
196
Bill Wendling3708c182007-05-27 10:15:43 +0000197/// getReferenceType - Return the uniqued reference to the type for a reference
198/// to the specified type.
199QualType ASTContext::getReferenceType(QualType T) {
200 // Unique pointers, to guarantee there is only one pointer of a particular
201 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000202 llvm::FoldingSetNodeID ID;
Bill Wendling3708c182007-05-27 10:15:43 +0000203 ReferenceType::Profile(ID, T);
204
205 void *InsertPos = 0;
206 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
207 return QualType(RT, 0);
208
209 // If the referencee type isn't canonical, this won't be a canonical type
210 // either, so fill in the canonical type field.
211 QualType Canonical;
212 if (!T->isCanonical()) {
213 Canonical = getReferenceType(T.getCanonicalType());
214
215 // Get the new insert position for the node we care about.
216 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
217 assert(NewIP == 0 && "Shouldn't be in the map!");
218 }
219
220 ReferenceType *New = new ReferenceType(T, Canonical);
221 Types.push_back(New);
222 ReferenceTypes.InsertNode(New, InsertPos);
223 return QualType(New, 0);
224}
225
Chris Lattner7ccecb92006-11-12 08:50:50 +0000226/// getArrayType - Return the unique reference to the type for an array of the
227/// specified element type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000228QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
229 unsigned EltTypeQuals, Expr *NumElts) {
Chris Lattner36f8e652007-01-27 08:31:04 +0000230 // Unique array types, to guarantee there is only one array of a particular
Chris Lattner7ccecb92006-11-12 08:50:50 +0000231 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000232 llvm::FoldingSetNodeID ID;
Steve Naroffb7d49242007-03-14 19:55:17 +0000233 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
Steve Naroff408451b2007-02-26 22:17:12 +0000234
Chris Lattner36f8e652007-01-27 08:31:04 +0000235 void *InsertPos = 0;
236 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000237 return QualType(ATP, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000238
239 // If the element type isn't canonical, this won't be a canonical type either,
240 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000241 QualType Canonical;
Chris Lattner36f8e652007-01-27 08:31:04 +0000242 if (!EltTy->isCanonical()) {
Chris Lattner7ccecb92006-11-12 08:50:50 +0000243 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000244 NumElts);
Chris Lattner36f8e652007-01-27 08:31:04 +0000245
246 // Get the new insert position for the node we care about.
247 ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
248 assert(NewIP == 0 && "Shouldn't be in the map!");
249 }
Chris Lattner7ccecb92006-11-12 08:50:50 +0000250
Steve Naroffb7d49242007-03-14 19:55:17 +0000251 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
Chris Lattner36f8e652007-01-27 08:31:04 +0000252 ArrayTypes.InsertNode(New, InsertPos);
253 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000254 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000255}
256
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000257/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
258///
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000259QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000260 // Unique functions, to guarantee there is only one function of a particular
261 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000262 llvm::FoldingSetNodeID ID;
Chris Lattner47955de2007-01-27 08:37:20 +0000263 FunctionTypeNoProto::Profile(ID, ResultTy);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000264
Chris Lattner47955de2007-01-27 08:37:20 +0000265 void *InsertPos = 0;
266 if (FunctionTypeNoProto *FT =
267 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000268 return QualType(FT, 0);
Chris Lattner47955de2007-01-27 08:37:20 +0000269
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000270 QualType Canonical;
Chris Lattner47955de2007-01-27 08:37:20 +0000271 if (!ResultTy->isCanonical()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000272 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
Chris Lattner47955de2007-01-27 08:37:20 +0000273
274 // Get the new insert position for the node we care about.
275 FunctionTypeNoProto *NewIP =
276 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
277 assert(NewIP == 0 && "Shouldn't be in the map!");
278 }
279
280 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
281 Types.push_back(New);
282 FunctionTypeProtos.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000283 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000284}
285
286/// getFunctionType - Return a normal function type with a typed argument
287/// list. isVariadic indicates whether the argument list includes '...'.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000288QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
289 unsigned NumArgs, bool isVariadic) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000290 // Unique functions, to guarantee there is only one function of a particular
291 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000292 llvm::FoldingSetNodeID ID;
Chris Lattnerfd4de792007-01-27 01:15:32 +0000293 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
294
295 void *InsertPos = 0;
296 if (FunctionTypeProto *FTP =
297 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000298 return QualType(FTP, 0);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000299
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000300 // Determine whether the type being created is already canonical or not.
301 bool isCanonical = ResultTy->isCanonical();
302 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
303 if (!ArgArray[i]->isCanonical())
304 isCanonical = false;
305
306 // If this type isn't canonical, get the canonical version of it.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000307 QualType Canonical;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000308 if (!isCanonical) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000309 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000310 CanonicalArgs.reserve(NumArgs);
311 for (unsigned i = 0; i != NumArgs; ++i)
312 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
313
314 Canonical = getFunctionType(ResultTy.getCanonicalType(),
315 &CanonicalArgs[0], NumArgs,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000316 isVariadic);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000317
318 // Get the new insert position for the node we care about.
319 FunctionTypeProto *NewIP =
320 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
321 assert(NewIP == 0 && "Shouldn't be in the map!");
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000322 }
323
324 // FunctionTypeProto objects are not allocated with new because they have a
325 // variable size array (for parameter types) at the end of them.
326 FunctionTypeProto *FTP =
327 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000328 (NumArgs-1)*sizeof(QualType));
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000329 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
330 Canonical);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000331 Types.push_back(FTP);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000332 FunctionTypeProtos.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000333 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000334}
Chris Lattneref51c202006-11-10 07:17:23 +0000335
Chris Lattner32d920b2007-01-26 02:01:53 +0000336/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +0000337/// specified typename decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000338QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
339 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000340
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000341 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Chris Lattnercceab1a2007-03-26 20:16:44 +0000342 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
343 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000344 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000345}
346
Chris Lattnerfb072462007-01-23 05:45:31 +0000347/// getTagDeclType - Return the unique reference to the type for the
348/// specified TagDecl (struct/union/class/enum) decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000349QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Chris Lattner733067d2007-01-26 01:42:24 +0000350 // The decl stores the type cache.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000351 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000352
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000353 Decl->TypeForDecl = new TagType(Decl, QualType());
Chris Lattnercceab1a2007-03-26 20:16:44 +0000354 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000355 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000356}
357
Steve Naroff92e30f82007-04-02 22:35:25 +0000358/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
Steve Naroff0f6256d2007-04-02 23:01:44 +0000359/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
Steve Naroff92e30f82007-04-02 22:35:25 +0000360/// needs to agree with the definition in <stddef.h>.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000361QualType ASTContext::getSizeType() const {
Steve Naroff92e30f82007-04-02 22:35:25 +0000362 // On Darwin, size_t is defined as a "long unsigned int".
363 // FIXME: should derive from "Target".
364 return UnsignedLongTy;
365}
Chris Lattnerfb072462007-01-23 05:45:31 +0000366
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000367/// getIntegerBitwidth - Return the bitwidth of the specified integer type
368/// according to the target. 'Loc' specifies the source location that
369/// requires evaluation of this property.
370unsigned ASTContext::getIntegerBitwidth(QualType T, SourceLocation Loc) {
371 if (const TagType *TT = dyn_cast<TagType>(T.getCanonicalType())) {
372 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
373 assert(0 && "FIXME: getIntegerBitwidth(enum) unimplemented!");
374 }
375
376 const BuiltinType *BT = cast<BuiltinType>(T.getCanonicalType());
377 switch (BT->getKind()) {
378 default: assert(0 && "getIntegerBitwidth(): not a built-in integer");
379 case BuiltinType::Bool: return Target.getBoolWidth(Loc);
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: return Target.getCharWidth(Loc);
384 case BuiltinType::Short:
385 case BuiltinType::UShort: return Target.getShortWidth(Loc);
386 case BuiltinType::Int:
387 case BuiltinType::UInt: return Target.getIntWidth(Loc);
388 case BuiltinType::Long:
389 case BuiltinType::ULong: return Target.getLongWidth(Loc);
390 case BuiltinType::LongLong:
391 case BuiltinType::ULongLong: return Target.getLongLongWidth(Loc);
392 }
393}
394
Steve Naroff0af91202007-04-27 21:51:21 +0000395/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
396/// routine will assert if passed a built-in type that isn't an integer or enum.
397static int getIntegerRank(QualType t) {
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000398 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
399 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
400 return 4;
Steve Naroffe4718892007-04-27 18:30:00 +0000401 }
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000402
403 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
404 switch (BT->getKind()) {
405 default:
406 assert(0 && "getIntegerRank(): not a built-in integer");
407 case BuiltinType::Bool:
408 return 1;
Chris Lattnerb16f4552007-06-03 07:25:34 +0000409 case BuiltinType::Char_S:
410 case BuiltinType::Char_U:
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000411 case BuiltinType::SChar:
412 case BuiltinType::UChar:
413 return 2;
414 case BuiltinType::Short:
415 case BuiltinType::UShort:
416 return 3;
417 case BuiltinType::Int:
418 case BuiltinType::UInt:
419 return 4;
420 case BuiltinType::Long:
421 case BuiltinType::ULong:
422 return 5;
423 case BuiltinType::LongLong:
424 case BuiltinType::ULongLong:
425 return 6;
426 }
Steve Naroffe4718892007-04-27 18:30:00 +0000427}
428
Steve Naroff0af91202007-04-27 21:51:21 +0000429/// getFloatingRank - Return a relative rank for floating point types.
430/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerc6395932007-06-22 20:56:16 +0000431static int getFloatingRank(QualType T) {
432 T = T.getCanonicalType();
433 if (ComplexType *CT = dyn_cast<ComplexType>(T))
434 return getFloatingRank(CT->getElementType());
435
436 switch (cast<BuiltinType>(T)->getKind()) {
437 default: assert(0 && "getFloatingPointRank(): not a floating type");
438 case BuiltinType::Float: return FloatRank;
439 case BuiltinType::Double: return DoubleRank;
440 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +0000441 }
442}
443
444// maxComplexType - the following code handles 3 different combinations:
445// complex/complex, complex/float, float/complex.
446// When both operands are complex, the shorter operand is converted to the
447// type of the longer, and that is the type of the result. This corresponds
448// to what is done when combining two real floating-point operands.
449// The fun begins when size promotion occur across type domains. g
450// getFloatingRank & convertFloatingRankToComplexType handle this without
451// enumerating all permutations.
452// It also allows us to add new types without breakage.
453// From H&S 6.3.4: When one operand is complex and the other is a real
454// floating-point type, the less precise type is converted, within it's
455// real or complex domain, to the precision of the other type. For example,
456// when combining a "long double" with a "double _Complex", the
457// "double _Complex" is promoted to "long double _Complex".
458
Steve Naroff0af91202007-04-27 21:51:21 +0000459QualType ASTContext::maxComplexType(QualType lt, QualType rt) const {
460 switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) {
Chris Lattnerc6395932007-06-22 20:56:16 +0000461 default: assert(0 && "convertRankToComplex(): illegal value for rank");
462 case FloatRank: return FloatComplexTy;
463 case DoubleRank: return DoubleComplexTy;
464 case LongDoubleRank: return LongDoubleComplexTy;
Steve Naroff0af91202007-04-27 21:51:21 +0000465 }
Steve Naroffe4718892007-04-27 18:30:00 +0000466}
467
468// maxFloatingType - handles the simple case, both operands are floats.
469QualType ASTContext::maxFloatingType(QualType lt, QualType rt) {
470 return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt;
471}
472
Steve Naroff0af91202007-04-27 21:51:21 +0000473// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
474// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
Steve Naroffe4718892007-04-27 18:30:00 +0000475QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
Chris Lattnerb16f4552007-06-03 07:25:34 +0000476 if (lhs == rhs) return lhs;
477
Steve Naroffe4718892007-04-27 18:30:00 +0000478 bool t1Unsigned = lhs->isUnsignedIntegerType();
479 bool t2Unsigned = rhs->isUnsignedIntegerType();
480
481 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
482 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
483
484 // We have two integer types with differing signs
485 QualType unsignedType = t1Unsigned ? lhs : rhs;
486 QualType signedType = t1Unsigned ? rhs : lhs;
487
488 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
489 return unsignedType;
Steve Naroff0af91202007-04-27 21:51:21 +0000490 else {
491 // FIXME: Need to check if the signed type can represent all values of the
492 // unsigned type. If it can, then the result is the signed type.
493 // If it can't, then the result is the unsigned version of the signed type.
494 // Should probably add a helper that returns a signed integer type from
495 // an unsigned (and vice versa). C99 6.3.1.8.
496 return signedType;
497 }
Steve Naroffe4718892007-04-27 18:30:00 +0000498}