blob: b1c20c98e52248bae950734d5d5693802f928f7f [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +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"
15#include "clang/AST/Decl.h"
16#include "clang/Lex/Preprocessor.h"
17#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21enum FloatingRank {
22 FloatRank, DoubleRank, LongDoubleRank
23};
24
25ASTContext::~ASTContext() {
26 // Deallocate all the types.
27 while (!Types.empty()) {
28 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 }
35 Types.pop_back();
36 }
37}
38
39void 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;
43 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
44
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;
53 else if (isa<ReferenceType>(T))
54 ++NumReference;
55 else if (isa<ArrayType>(T))
56 ++NumArray;
57 else if (isa<FunctionTypeNoProto>(T))
58 ++NumFunctionNP;
59 else if (isa<FunctionTypeProto>(T))
60 ++NumFunctionP;
61 else if (isa<TypedefType>(T))
62 ++NumTypeName;
63 else if (TagType *TT = dyn_cast<TagType>(T)) {
64 ++NumTagged;
65 switch (TT->getDecl()->getKind()) {
66 default: assert(0 && "Unknown tagged type!");
67 case Decl::Struct: ++NumTagStruct; break;
68 case Decl::Union: ++NumTagUnion; break;
69 case Decl::Class: ++NumTagClass; break;
70 case Decl::Enum: ++NumTagEnum; break;
71 }
72 } else {
73 assert(0 && "Unknown type!");
74 }
75 }
76
77 fprintf(stderr, " %d builtin types\n", NumBuiltin);
78 fprintf(stderr, " %d pointer types\n", NumPointer);
79 fprintf(stderr, " %d reference types\n", NumReference);
80 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);
89 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
90 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
91 NumFunctionP*sizeof(FunctionTypeProto)+
92 NumFunctionNP*sizeof(FunctionTypeNoProto)+
93 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
94}
95
96
97void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
98 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
99}
100
101
102void ASTContext::InitBuiltinTypes() {
103 assert(VoidTy.isNull() && "Context reinitialized?");
104
105 // C99 6.2.5p19.
106 InitBuiltinType(VoidTy, BuiltinType::Void);
107
108 // C99 6.2.5p2.
109 InitBuiltinType(BoolTy, BuiltinType::Bool);
110 // C99 6.2.5p3.
111 if (Target.isCharSigned(SourceLocation()))
112 InitBuiltinType(CharTy, BuiltinType::Char_S);
113 else
114 InitBuiltinType(CharTy, BuiltinType::Char_U);
115 // C99 6.2.5p4.
116 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
117 InitBuiltinType(ShortTy, BuiltinType::Short);
118 InitBuiltinType(IntTy, BuiltinType::Int);
119 InitBuiltinType(LongTy, BuiltinType::Long);
120 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
121
122 // C99 6.2.5p6.
123 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
124 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
125 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
126 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
127 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
128
129 // C99 6.2.5p10.
130 InitBuiltinType(FloatTy, BuiltinType::Float);
131 InitBuiltinType(DoubleTy, BuiltinType::Double);
132 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
133
134 // C99 6.2.5p11.
135 FloatComplexTy = getComplexType(FloatTy);
136 DoubleComplexTy = getComplexType(DoubleTy);
137 LongDoubleComplexTy = getComplexType(LongDoubleTy);
138}
139
140/// 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
169/// getPointerType - Return the uniqued reference to the type for a pointer to
170/// the specified type.
171QualType ASTContext::getPointerType(QualType T) {
172 // Unique pointers, to guarantee there is only one pointer of a particular
173 // structure.
174 llvm::FoldingSetNodeID ID;
175 PointerType::Profile(ID, T);
176
177 void *InsertPos = 0;
178 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
179 return QualType(PT, 0);
180
181 // If the pointee type isn't canonical, this won't be a canonical type either,
182 // so fill in the canonical type field.
183 QualType Canonical;
184 if (!T->isCanonical()) {
185 Canonical = getPointerType(T.getCanonicalType());
186
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 }
191 PointerType *New = new PointerType(T, Canonical);
192 Types.push_back(New);
193 PointerTypes.InsertNode(New, InsertPos);
194 return QualType(New, 0);
195}
196
197/// 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.
202 llvm::FoldingSetNodeID ID;
203 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
226/// getArrayType - Return the unique reference to the type for an array of the
227/// specified element type.
228QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
229 unsigned EltTypeQuals, Expr *NumElts) {
230 // Unique array types, to guarantee there is only one array of a particular
231 // structure.
232 llvm::FoldingSetNodeID ID;
233 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
234
235 void *InsertPos = 0;
236 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
237 return QualType(ATP, 0);
238
239 // If the element type isn't canonical, this won't be a canonical type either,
240 // so fill in the canonical type field.
241 QualType Canonical;
242 if (!EltTy->isCanonical()) {
243 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
244 NumElts);
245
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 }
250
251 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
252 ArrayTypes.InsertNode(New, InsertPos);
253 Types.push_back(New);
254 return QualType(New, 0);
255}
256
257/// convertToVectorType - Return the unique reference to a vector type of
258/// the specified element type and size. VectorType can be a pointer, array,
259/// function, or built-in type (i.e. _Bool, integer, or float).
260QualType ASTContext::convertToVectorType(QualType vecType, unsigned NumElts) {
261 BuiltinType *baseType;
262
263 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
264 assert(baseType != 0 &&
265 "convertToVectorType(): Complex vector types unimplemented");
266
267 // Check if we've already instantiated a vector of this type.
268 llvm::FoldingSetNodeID ID;
269 VectorType::Profile(ID, vecType, NumElts);
270 void *InsertPos = 0;
271 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
272 return QualType(VTP, 0);
273
274 // If the element type isn't canonical, this won't be a canonical type either,
275 // so fill in the canonical type field.
276 QualType Canonical;
277 if (!vecType->isCanonical()) {
278 Canonical = convertToVectorType(vecType.getCanonicalType(), NumElts);
279
280 // Get the new insert position for the node we care about.
281 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
282 assert(NewIP == 0 && "Shouldn't be in the map!");
283 }
284 VectorType *New = new VectorType(vecType, NumElts, Canonical);
285 VectorTypes.InsertNode(New, InsertPos);
286 Types.push_back(New);
287 return QualType(New, 0);
288}
289
290/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
291///
292QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
293 // Unique functions, to guarantee there is only one function of a particular
294 // structure.
295 llvm::FoldingSetNodeID ID;
296 FunctionTypeNoProto::Profile(ID, ResultTy);
297
298 void *InsertPos = 0;
299 if (FunctionTypeNoProto *FT =
300 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
301 return QualType(FT, 0);
302
303 QualType Canonical;
304 if (!ResultTy->isCanonical()) {
305 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
306
307 // Get the new insert position for the node we care about.
308 FunctionTypeNoProto *NewIP =
309 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
310 assert(NewIP == 0 && "Shouldn't be in the map!");
311 }
312
313 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
314 Types.push_back(New);
315 FunctionTypeProtos.InsertNode(New, InsertPos);
316 return QualType(New, 0);
317}
318
319/// getFunctionType - Return a normal function type with a typed argument
320/// list. isVariadic indicates whether the argument list includes '...'.
321QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
322 unsigned NumArgs, bool isVariadic) {
323 // Unique functions, to guarantee there is only one function of a particular
324 // structure.
325 llvm::FoldingSetNodeID ID;
326 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
327
328 void *InsertPos = 0;
329 if (FunctionTypeProto *FTP =
330 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
331 return QualType(FTP, 0);
332
333 // Determine whether the type being created is already canonical or not.
334 bool isCanonical = ResultTy->isCanonical();
335 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
336 if (!ArgArray[i]->isCanonical())
337 isCanonical = false;
338
339 // If this type isn't canonical, get the canonical version of it.
340 QualType Canonical;
341 if (!isCanonical) {
342 llvm::SmallVector<QualType, 16> CanonicalArgs;
343 CanonicalArgs.reserve(NumArgs);
344 for (unsigned i = 0; i != NumArgs; ++i)
345 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
346
347 Canonical = getFunctionType(ResultTy.getCanonicalType(),
348 &CanonicalArgs[0], NumArgs,
349 isVariadic);
350
351 // Get the new insert position for the node we care about.
352 FunctionTypeProto *NewIP =
353 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
354 assert(NewIP == 0 && "Shouldn't be in the map!");
355 }
356
357 // FunctionTypeProto objects are not allocated with new because they have a
358 // variable size array (for parameter types) at the end of them.
359 FunctionTypeProto *FTP =
360 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
361 (NumArgs-1)*sizeof(QualType));
362 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
363 Canonical);
364 Types.push_back(FTP);
365 FunctionTypeProtos.InsertNode(FTP, InsertPos);
366 return QualType(FTP, 0);
367}
368
369/// getTypedefType - Return the unique reference to the type for the
370/// specified typename decl.
371QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
372 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
373
374 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
375 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
376 Types.push_back(Decl->TypeForDecl);
377 return QualType(Decl->TypeForDecl, 0);
378}
379
380/// getTagDeclType - Return the unique reference to the type for the
381/// specified TagDecl (struct/union/class/enum) decl.
382QualType ASTContext::getTagDeclType(TagDecl *Decl) {
383 // The decl stores the type cache.
384 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
385
386 Decl->TypeForDecl = new TagType(Decl, QualType());
387 Types.push_back(Decl->TypeForDecl);
388 return QualType(Decl->TypeForDecl, 0);
389}
390
391/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
392/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
393/// needs to agree with the definition in <stddef.h>.
394QualType ASTContext::getSizeType() const {
395 // On Darwin, size_t is defined as a "long unsigned int".
396 // FIXME: should derive from "Target".
397 return UnsignedLongTy;
398}
399
400/// getIntegerBitwidth - Return the bitwidth of the specified integer type
401/// according to the target. 'Loc' specifies the source location that
402/// requires evaluation of this property.
403unsigned ASTContext::getIntegerBitwidth(QualType T, SourceLocation Loc) {
404 if (const TagType *TT = dyn_cast<TagType>(T.getCanonicalType())) {
405 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
406 assert(0 && "FIXME: getIntegerBitwidth(enum) unimplemented!");
407 }
408
409 const BuiltinType *BT = cast<BuiltinType>(T.getCanonicalType());
410 switch (BT->getKind()) {
411 default: assert(0 && "getIntegerBitwidth(): not a built-in integer");
412 case BuiltinType::Bool: return Target.getBoolWidth(Loc);
413 case BuiltinType::Char_S:
414 case BuiltinType::Char_U:
415 case BuiltinType::SChar:
416 case BuiltinType::UChar: return Target.getCharWidth(Loc);
417 case BuiltinType::Short:
418 case BuiltinType::UShort: return Target.getShortWidth(Loc);
419 case BuiltinType::Int:
420 case BuiltinType::UInt: return Target.getIntWidth(Loc);
421 case BuiltinType::Long:
422 case BuiltinType::ULong: return Target.getLongWidth(Loc);
423 case BuiltinType::LongLong:
424 case BuiltinType::ULongLong: return Target.getLongLongWidth(Loc);
425 }
426}
427
428/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
429/// routine will assert if passed a built-in type that isn't an integer or enum.
430static int getIntegerRank(QualType t) {
431 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
432 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
433 return 4;
434 }
435
436 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
437 switch (BT->getKind()) {
438 default:
439 assert(0 && "getIntegerRank(): not a built-in integer");
440 case BuiltinType::Bool:
441 return 1;
442 case BuiltinType::Char_S:
443 case BuiltinType::Char_U:
444 case BuiltinType::SChar:
445 case BuiltinType::UChar:
446 return 2;
447 case BuiltinType::Short:
448 case BuiltinType::UShort:
449 return 3;
450 case BuiltinType::Int:
451 case BuiltinType::UInt:
452 return 4;
453 case BuiltinType::Long:
454 case BuiltinType::ULong:
455 return 5;
456 case BuiltinType::LongLong:
457 case BuiltinType::ULongLong:
458 return 6;
459 }
460}
461
462/// getFloatingRank - Return a relative rank for floating point types.
463/// This routine will assert if passed a built-in type that isn't a float.
464static int getFloatingRank(QualType T) {
465 T = T.getCanonicalType();
466 if (ComplexType *CT = dyn_cast<ComplexType>(T))
467 return getFloatingRank(CT->getElementType());
468
469 switch (cast<BuiltinType>(T)->getKind()) {
470 default: assert(0 && "getFloatingPointRank(): not a floating type");
471 case BuiltinType::Float: return FloatRank;
472 case BuiltinType::Double: return DoubleRank;
473 case BuiltinType::LongDouble: return LongDoubleRank;
474 }
475}
476
477// maxComplexType - the following code handles 3 different combinations:
478// complex/complex, complex/float, float/complex.
479// When both operands are complex, the shorter operand is converted to the
480// type of the longer, and that is the type of the result. This corresponds
481// to what is done when combining two real floating-point operands.
482// The fun begins when size promotion occur across type domains. g
483// getFloatingRank & convertFloatingRankToComplexType handle this without
484// enumerating all permutations.
485// It also allows us to add new types without breakage.
486// From H&S 6.3.4: When one operand is complex and the other is a real
487// floating-point type, the less precise type is converted, within it's
488// real or complex domain, to the precision of the other type. For example,
489// when combining a "long double" with a "double _Complex", the
490// "double _Complex" is promoted to "long double _Complex".
491
492QualType ASTContext::maxComplexType(QualType lt, QualType rt) const {
493 switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) {
494 default: assert(0 && "convertRankToComplex(): illegal value for rank");
495 case FloatRank: return FloatComplexTy;
496 case DoubleRank: return DoubleComplexTy;
497 case LongDoubleRank: return LongDoubleComplexTy;
498 }
499}
500
501// maxFloatingType - handles the simple case, both operands are floats.
502QualType ASTContext::maxFloatingType(QualType lt, QualType rt) {
503 return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt;
504}
505
506// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
507// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
508QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
509 if (lhs == rhs) return lhs;
510
511 bool t1Unsigned = lhs->isUnsignedIntegerType();
512 bool t2Unsigned = rhs->isUnsignedIntegerType();
513
514 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
515 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
516
517 // We have two integer types with differing signs
518 QualType unsignedType = t1Unsigned ? lhs : rhs;
519 QualType signedType = t1Unsigned ? rhs : lhs;
520
521 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
522 return unsignedType;
523 else {
524 // FIXME: Need to check if the signed type can represent all values of the
525 // unsigned type. If it can, then the result is the signed type.
526 // If it can't, then the result is the unsigned version of the signed type.
527 // Should probably add a helper that returns a signed integer type from
528 // an unsigned (and vice versa). C99 6.3.1.8.
529 return signedType;
530 }
531}