blob: f7ae1a0e3acfda5e46b1122e0cd2b2bdfa9bb5f8 [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;
Chris Lattner9c016772007-07-18 05:50:59 +000043 unsigned NumVector = 0, NumComplex = 0;
Bill Wendling3708c182007-05-27 10:15:43 +000044 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Chris Lattner4eb445d2007-01-26 01:27:23 +000045
46 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
47
48 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
49 Type *T = Types[i];
50 if (isa<BuiltinType>(T))
51 ++NumBuiltin;
52 else if (isa<PointerType>(T))
53 ++NumPointer;
Bill Wendling3708c182007-05-27 10:15:43 +000054 else if (isa<ReferenceType>(T))
55 ++NumReference;
Chris Lattner9c016772007-07-18 05:50:59 +000056 else if (isa<ComplexType>(T))
57 ++NumComplex;
Chris Lattner4eb445d2007-01-26 01:27:23 +000058 else if (isa<ArrayType>(T))
59 ++NumArray;
Chris Lattner9c016772007-07-18 05:50:59 +000060 else if (isa<VectorType>(T))
61 ++NumVector;
Chris Lattner4eb445d2007-01-26 01:27:23 +000062 else if (isa<FunctionTypeNoProto>(T))
63 ++NumFunctionNP;
64 else if (isa<FunctionTypeProto>(T))
65 ++NumFunctionP;
Chris Lattner32d920b2007-01-26 02:01:53 +000066 else if (isa<TypedefType>(T))
Chris Lattner4eb445d2007-01-26 01:27:23 +000067 ++NumTypeName;
Steve Narofff1e53692007-03-23 22:27:02 +000068 else if (TagType *TT = dyn_cast<TagType>(T)) {
Chris Lattner4eb445d2007-01-26 01:27:23 +000069 ++NumTagged;
70 switch (TT->getDecl()->getKind()) {
71 default: assert(0 && "Unknown tagged type!");
72 case Decl::Struct: ++NumTagStruct; break;
73 case Decl::Union: ++NumTagUnion; break;
74 case Decl::Class: ++NumTagClass; break;
75 case Decl::Enum: ++NumTagEnum; break;
76 }
77 } else {
78 assert(0 && "Unknown type!");
79 }
80 }
81
82 fprintf(stderr, " %d builtin types\n", NumBuiltin);
83 fprintf(stderr, " %d pointer types\n", NumPointer);
Bill Wendling3708c182007-05-27 10:15:43 +000084 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner9c016772007-07-18 05:50:59 +000085 fprintf(stderr, " %d complex types\n", NumComplex);
Chris Lattner4eb445d2007-01-26 01:27:23 +000086 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner9c016772007-07-18 05:50:59 +000087 fprintf(stderr, " %d vector types\n", NumVector);
Chris Lattner4eb445d2007-01-26 01:27:23 +000088 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
89 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
90 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
91 fprintf(stderr, " %d tagged types\n", NumTagged);
92 fprintf(stderr, " %d struct types\n", NumTagStruct);
93 fprintf(stderr, " %d union types\n", NumTagUnion);
94 fprintf(stderr, " %d class types\n", NumTagClass);
95 fprintf(stderr, " %d enum types\n", NumTagEnum);
Chris Lattnerfc234de2007-05-24 00:40:54 +000096 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
Steve Narofff84d11f2007-05-23 21:48:04 +000097 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner9c016772007-07-18 05:50:59 +000098 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Chris Lattnerfc234de2007-05-24 00:40:54 +000099 NumFunctionP*sizeof(FunctionTypeProto)+
100 NumFunctionNP*sizeof(FunctionTypeNoProto)+
101 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
Chris Lattner4eb445d2007-01-26 01:27:23 +0000102}
103
104
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000105void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
106 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000107}
108
109
Chris Lattner970e54e2006-11-12 00:37:36 +0000110void ASTContext::InitBuiltinTypes() {
111 assert(VoidTy.isNull() && "Context reinitialized?");
112
113 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000114 InitBuiltinType(VoidTy, BuiltinType::Void);
Chris Lattner970e54e2006-11-12 00:37:36 +0000115
116 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000117 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000118 // C99 6.2.5p3.
Chris Lattnerb16f4552007-06-03 07:25:34 +0000119 if (Target.isCharSigned(SourceLocation()))
120 InitBuiltinType(CharTy, BuiltinType::Char_S);
121 else
122 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000123 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000124 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
125 InitBuiltinType(ShortTy, BuiltinType::Short);
126 InitBuiltinType(IntTy, BuiltinType::Int);
127 InitBuiltinType(LongTy, BuiltinType::Long);
128 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000129
130 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000131 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
132 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
133 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
134 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
135 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000136
137 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000138 InitBuiltinType(FloatTy, BuiltinType::Float);
139 InitBuiltinType(DoubleTy, BuiltinType::Double);
140 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Chris Lattner970e54e2006-11-12 00:37:36 +0000141
142 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000143 FloatComplexTy = getComplexType(FloatTy);
144 DoubleComplexTy = getComplexType(DoubleTy);
145 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Chris Lattner970e54e2006-11-12 00:37:36 +0000146}
147
Chris Lattner983a8bb2007-07-13 22:13:22 +0000148
149/// getTypeSize - Return the size of the specified type, in bits. This method
150/// does not work on incomplete types.
Chris Lattner4481b422007-07-14 01:29:45 +0000151std::pair<uint64_t, unsigned>
152ASTContext::getTypeInfo(QualType T, SourceLocation L) {
Chris Lattner983a8bb2007-07-13 22:13:22 +0000153 T = T.getCanonicalType();
Chris Lattner4481b422007-07-14 01:29:45 +0000154 uint64_t Size;
155 unsigned Align;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000156 switch (T->getTypeClass()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000157 default:
158 case Type::Complex:
159 case Type::Array:
160 case Type::Vector:
161 case Type::TypeName:
162 case Type::Tagged:
163 assert(0 && "Unimplemented type sizes!");
164 case Type::FunctionNoProto:
165 case Type::FunctionProto:
166 assert(0 && "Incomplete types have no size!");
Chris Lattner983a8bb2007-07-13 22:13:22 +0000167 case Type::Builtin: {
168 // FIXME: need to use TargetInfo to derive the target specific sizes. This
169 // implementation will suffice for play with vector support.
170 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000171 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000172 case BuiltinType::Void:
173 assert(0 && "Incomplete types have no size!");
174 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000175 case BuiltinType::Char_S:
176 case BuiltinType::Char_U:
177 case BuiltinType::UChar:
Chris Lattner4481b422007-07-14 01:29:45 +0000178 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000179 case BuiltinType::UShort:
Chris Lattner4481b422007-07-14 01:29:45 +0000180 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000181 case BuiltinType::UInt:
Chris Lattner4481b422007-07-14 01:29:45 +0000182 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000183 case BuiltinType::ULong:
Chris Lattner4481b422007-07-14 01:29:45 +0000184 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000185 case BuiltinType::ULongLong:
Chris Lattner4481b422007-07-14 01:29:45 +0000186 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
187 case BuiltinType::Float: Target.getFloatInfo(Size, Align, L); break;
188 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, L); break;
189 case BuiltinType::LongDouble: Target.getLongDoubleInfo(Size, Align,L);break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000190 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000191 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000192 }
Chris Lattner4481b422007-07-14 01:29:45 +0000193 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000194 case Type::Reference:
Chris Lattner0523dc92007-07-13 22:16:13 +0000195 // "When applied to a reference or a reference type, the result is the size
196 // of the referenced type." C++98 5.3.3p2: expr.sizeof
Chris Lattner4481b422007-07-14 01:29:45 +0000197 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000198 }
Chris Lattner4481b422007-07-14 01:29:45 +0000199
200 return std::make_pair(Size, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000201}
202
203//===----------------------------------------------------------------------===//
204// Type creation/memoization methods
205//===----------------------------------------------------------------------===//
206
207
Chris Lattnerc6395932007-06-22 20:56:16 +0000208/// getComplexType - Return the uniqued reference to the type for a complex
209/// number with the specified element type.
210QualType ASTContext::getComplexType(QualType T) {
211 // Unique pointers, to guarantee there is only one pointer of a particular
212 // structure.
213 llvm::FoldingSetNodeID ID;
214 ComplexType::Profile(ID, T);
215
216 void *InsertPos = 0;
217 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
218 return QualType(CT, 0);
219
220 // If the pointee type isn't canonical, this won't be a canonical type either,
221 // so fill in the canonical type field.
222 QualType Canonical;
223 if (!T->isCanonical()) {
224 Canonical = getComplexType(T.getCanonicalType());
225
226 // Get the new insert position for the node we care about.
227 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
228 assert(NewIP == 0 && "Shouldn't be in the map!");
229 }
230 ComplexType *New = new ComplexType(T, Canonical);
231 Types.push_back(New);
232 ComplexTypes.InsertNode(New, InsertPos);
233 return QualType(New, 0);
234}
235
236
Chris Lattner970e54e2006-11-12 00:37:36 +0000237/// getPointerType - Return the uniqued reference to the type for a pointer to
238/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000239QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000240 // Unique pointers, to guarantee there is only one pointer of a particular
241 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000242 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +0000243 PointerType::Profile(ID, T);
244
245 void *InsertPos = 0;
246 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000247 return QualType(PT, 0);
Chris Lattner970e54e2006-11-12 00:37:36 +0000248
Chris Lattner7ccecb92006-11-12 08:50:50 +0000249 // If the pointee type isn't canonical, this won't be a canonical type either,
250 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000251 QualType Canonical;
Chris Lattner67521df2007-01-27 01:29:36 +0000252 if (!T->isCanonical()) {
Steve Naroffd50c88e2007-04-05 21:15:20 +0000253 Canonical = getPointerType(T.getCanonicalType());
Chris Lattner67521df2007-01-27 01:29:36 +0000254
255 // Get the new insert position for the node we care about.
256 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
257 assert(NewIP == 0 && "Shouldn't be in the map!");
258 }
Chris Lattner67521df2007-01-27 01:29:36 +0000259 PointerType *New = new PointerType(T, Canonical);
260 Types.push_back(New);
261 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000262 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +0000263}
264
Bill Wendling3708c182007-05-27 10:15:43 +0000265/// getReferenceType - Return the uniqued reference to the type for a reference
266/// to the specified type.
267QualType ASTContext::getReferenceType(QualType T) {
268 // Unique pointers, to guarantee there is only one pointer of a particular
269 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000270 llvm::FoldingSetNodeID ID;
Bill Wendling3708c182007-05-27 10:15:43 +0000271 ReferenceType::Profile(ID, T);
272
273 void *InsertPos = 0;
274 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
275 return QualType(RT, 0);
276
277 // If the referencee type isn't canonical, this won't be a canonical type
278 // either, so fill in the canonical type field.
279 QualType Canonical;
280 if (!T->isCanonical()) {
281 Canonical = getReferenceType(T.getCanonicalType());
282
283 // Get the new insert position for the node we care about.
284 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
285 assert(NewIP == 0 && "Shouldn't be in the map!");
286 }
287
288 ReferenceType *New = new ReferenceType(T, Canonical);
289 Types.push_back(New);
290 ReferenceTypes.InsertNode(New, InsertPos);
291 return QualType(New, 0);
292}
293
Chris Lattner7ccecb92006-11-12 08:50:50 +0000294/// getArrayType - Return the unique reference to the type for an array of the
295/// specified element type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000296QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
297 unsigned EltTypeQuals, Expr *NumElts) {
Chris Lattner36f8e652007-01-27 08:31:04 +0000298 // Unique array types, to guarantee there is only one array of a particular
Chris Lattner7ccecb92006-11-12 08:50:50 +0000299 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000300 llvm::FoldingSetNodeID ID;
Steve Naroffb7d49242007-03-14 19:55:17 +0000301 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
Steve Naroff408451b2007-02-26 22:17:12 +0000302
Chris Lattner36f8e652007-01-27 08:31:04 +0000303 void *InsertPos = 0;
304 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000305 return QualType(ATP, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000306
307 // If the element type isn't canonical, this won't be a canonical type either,
308 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000309 QualType Canonical;
Chris Lattner36f8e652007-01-27 08:31:04 +0000310 if (!EltTy->isCanonical()) {
Chris Lattner7ccecb92006-11-12 08:50:50 +0000311 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000312 NumElts);
Chris Lattner36f8e652007-01-27 08:31:04 +0000313
314 // Get the new insert position for the node we care about.
315 ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
316 assert(NewIP == 0 && "Shouldn't be in the map!");
317 }
Chris Lattner7ccecb92006-11-12 08:50:50 +0000318
Steve Naroffb7d49242007-03-14 19:55:17 +0000319 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
Chris Lattner36f8e652007-01-27 08:31:04 +0000320 ArrayTypes.InsertNode(New, InsertPos);
321 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000322 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000323}
324
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000325/// convertToVectorType - Return the unique reference to a vector type of
326/// the specified element type and size. VectorType can be a pointer, array,
327/// function, or built-in type (i.e. _Bool, integer, or float).
328QualType ASTContext::convertToVectorType(QualType vecType, unsigned NumElts) {
329 BuiltinType *baseType;
330
331 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
332 assert(baseType != 0 &&
333 "convertToVectorType(): Complex vector types unimplemented");
334
335 // Check if we've already instantiated a vector of this type.
336 llvm::FoldingSetNodeID ID;
337 VectorType::Profile(ID, vecType, NumElts);
338 void *InsertPos = 0;
339 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
340 return QualType(VTP, 0);
341
342 // If the element type isn't canonical, this won't be a canonical type either,
343 // so fill in the canonical type field.
344 QualType Canonical;
345 if (!vecType->isCanonical()) {
346 Canonical = convertToVectorType(vecType.getCanonicalType(), NumElts);
347
348 // Get the new insert position for the node we care about.
349 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
350 assert(NewIP == 0 && "Shouldn't be in the map!");
351 }
352 VectorType *New = new VectorType(vecType, NumElts, Canonical);
353 VectorTypes.InsertNode(New, InsertPos);
354 Types.push_back(New);
355 return QualType(New, 0);
356}
357
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000358/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
359///
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000360QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000361 // Unique functions, to guarantee there is only one function of a particular
362 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000363 llvm::FoldingSetNodeID ID;
Chris Lattner47955de2007-01-27 08:37:20 +0000364 FunctionTypeNoProto::Profile(ID, ResultTy);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000365
Chris Lattner47955de2007-01-27 08:37:20 +0000366 void *InsertPos = 0;
367 if (FunctionTypeNoProto *FT =
368 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000369 return QualType(FT, 0);
Chris Lattner47955de2007-01-27 08:37:20 +0000370
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000371 QualType Canonical;
Chris Lattner47955de2007-01-27 08:37:20 +0000372 if (!ResultTy->isCanonical()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000373 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
Chris Lattner47955de2007-01-27 08:37:20 +0000374
375 // Get the new insert position for the node we care about.
376 FunctionTypeNoProto *NewIP =
377 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
378 assert(NewIP == 0 && "Shouldn't be in the map!");
379 }
380
381 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
382 Types.push_back(New);
383 FunctionTypeProtos.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000384 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000385}
386
387/// getFunctionType - Return a normal function type with a typed argument
388/// list. isVariadic indicates whether the argument list includes '...'.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000389QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
390 unsigned NumArgs, bool isVariadic) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000391 // Unique functions, to guarantee there is only one function of a particular
392 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000393 llvm::FoldingSetNodeID ID;
Chris Lattnerfd4de792007-01-27 01:15:32 +0000394 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
395
396 void *InsertPos = 0;
397 if (FunctionTypeProto *FTP =
398 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000399 return QualType(FTP, 0);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000400
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000401 // Determine whether the type being created is already canonical or not.
402 bool isCanonical = ResultTy->isCanonical();
403 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
404 if (!ArgArray[i]->isCanonical())
405 isCanonical = false;
406
407 // If this type isn't canonical, get the canonical version of it.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000408 QualType Canonical;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000409 if (!isCanonical) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000410 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000411 CanonicalArgs.reserve(NumArgs);
412 for (unsigned i = 0; i != NumArgs; ++i)
413 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
414
415 Canonical = getFunctionType(ResultTy.getCanonicalType(),
416 &CanonicalArgs[0], NumArgs,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000417 isVariadic);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000418
419 // Get the new insert position for the node we care about.
420 FunctionTypeProto *NewIP =
421 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
422 assert(NewIP == 0 && "Shouldn't be in the map!");
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000423 }
424
425 // FunctionTypeProto objects are not allocated with new because they have a
426 // variable size array (for parameter types) at the end of them.
427 FunctionTypeProto *FTP =
428 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000429 (NumArgs-1)*sizeof(QualType));
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000430 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
431 Canonical);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000432 Types.push_back(FTP);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000433 FunctionTypeProtos.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000434 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000435}
Chris Lattneref51c202006-11-10 07:17:23 +0000436
Chris Lattner32d920b2007-01-26 02:01:53 +0000437/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +0000438/// specified typename decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000439QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
440 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000441
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000442 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Chris Lattnercceab1a2007-03-26 20:16:44 +0000443 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
444 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000445 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000446}
447
Chris Lattnerfb072462007-01-23 05:45:31 +0000448/// getTagDeclType - Return the unique reference to the type for the
449/// specified TagDecl (struct/union/class/enum) decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000450QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Chris Lattner733067d2007-01-26 01:42:24 +0000451 // The decl stores the type cache.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000452 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000453
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000454 Decl->TypeForDecl = new TagType(Decl, QualType());
Chris Lattnercceab1a2007-03-26 20:16:44 +0000455 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000456 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000457}
458
Steve Naroff92e30f82007-04-02 22:35:25 +0000459/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
Steve Naroff0f6256d2007-04-02 23:01:44 +0000460/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
Steve Naroff92e30f82007-04-02 22:35:25 +0000461/// needs to agree with the definition in <stddef.h>.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000462QualType ASTContext::getSizeType() const {
Steve Naroff92e30f82007-04-02 22:35:25 +0000463 // On Darwin, size_t is defined as a "long unsigned int".
464 // FIXME: should derive from "Target".
465 return UnsignedLongTy;
466}
Chris Lattnerfb072462007-01-23 05:45:31 +0000467
Chris Lattnerd2b88ab2007-07-13 03:05:23 +0000468/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
469/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
470QualType ASTContext::getPointerDiffType() const {
471 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
472 // FIXME: should derive from "Target".
473 return IntTy;
474}
475
Steve Naroff0af91202007-04-27 21:51:21 +0000476/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
477/// routine will assert if passed a built-in type that isn't an integer or enum.
478static int getIntegerRank(QualType t) {
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000479 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
480 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
481 return 4;
Steve Naroffe4718892007-04-27 18:30:00 +0000482 }
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000483
484 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
485 switch (BT->getKind()) {
486 default:
487 assert(0 && "getIntegerRank(): not a built-in integer");
488 case BuiltinType::Bool:
489 return 1;
Chris Lattnerb16f4552007-06-03 07:25:34 +0000490 case BuiltinType::Char_S:
491 case BuiltinType::Char_U:
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000492 case BuiltinType::SChar:
493 case BuiltinType::UChar:
494 return 2;
495 case BuiltinType::Short:
496 case BuiltinType::UShort:
497 return 3;
498 case BuiltinType::Int:
499 case BuiltinType::UInt:
500 return 4;
501 case BuiltinType::Long:
502 case BuiltinType::ULong:
503 return 5;
504 case BuiltinType::LongLong:
505 case BuiltinType::ULongLong:
506 return 6;
507 }
Steve Naroffe4718892007-04-27 18:30:00 +0000508}
509
Steve Naroff0af91202007-04-27 21:51:21 +0000510/// getFloatingRank - Return a relative rank for floating point types.
511/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerc6395932007-06-22 20:56:16 +0000512static int getFloatingRank(QualType T) {
513 T = T.getCanonicalType();
514 if (ComplexType *CT = dyn_cast<ComplexType>(T))
515 return getFloatingRank(CT->getElementType());
516
517 switch (cast<BuiltinType>(T)->getKind()) {
518 default: assert(0 && "getFloatingPointRank(): not a floating type");
519 case BuiltinType::Float: return FloatRank;
520 case BuiltinType::Double: return DoubleRank;
521 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +0000522 }
523}
524
525// maxComplexType - the following code handles 3 different combinations:
526// complex/complex, complex/float, float/complex.
527// When both operands are complex, the shorter operand is converted to the
528// type of the longer, and that is the type of the result. This corresponds
529// to what is done when combining two real floating-point operands.
530// The fun begins when size promotion occur across type domains. g
531// getFloatingRank & convertFloatingRankToComplexType handle this without
532// enumerating all permutations.
533// It also allows us to add new types without breakage.
534// From H&S 6.3.4: When one operand is complex and the other is a real
535// floating-point type, the less precise type is converted, within it's
536// real or complex domain, to the precision of the other type. For example,
537// when combining a "long double" with a "double _Complex", the
538// "double _Complex" is promoted to "long double _Complex".
539
Steve Naroff0af91202007-04-27 21:51:21 +0000540QualType ASTContext::maxComplexType(QualType lt, QualType rt) const {
541 switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) {
Chris Lattnerc6395932007-06-22 20:56:16 +0000542 default: assert(0 && "convertRankToComplex(): illegal value for rank");
543 case FloatRank: return FloatComplexTy;
544 case DoubleRank: return DoubleComplexTy;
545 case LongDoubleRank: return LongDoubleComplexTy;
Steve Naroff0af91202007-04-27 21:51:21 +0000546 }
Steve Naroffe4718892007-04-27 18:30:00 +0000547}
548
549// maxFloatingType - handles the simple case, both operands are floats.
550QualType ASTContext::maxFloatingType(QualType lt, QualType rt) {
551 return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt;
552}
553
Steve Naroff0af91202007-04-27 21:51:21 +0000554// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
555// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
Steve Naroffe4718892007-04-27 18:30:00 +0000556QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
Chris Lattnerb16f4552007-06-03 07:25:34 +0000557 if (lhs == rhs) return lhs;
558
Steve Naroffe4718892007-04-27 18:30:00 +0000559 bool t1Unsigned = lhs->isUnsignedIntegerType();
560 bool t2Unsigned = rhs->isUnsignedIntegerType();
561
562 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
563 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
564
565 // We have two integer types with differing signs
566 QualType unsignedType = t1Unsigned ? lhs : rhs;
567 QualType signedType = t1Unsigned ? rhs : lhs;
568
569 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
570 return unsignedType;
Steve Naroff0af91202007-04-27 21:51:21 +0000571 else {
572 // FIXME: Need to check if the signed type can represent all values of the
573 // unsigned type. If it can, then the result is the signed type.
574 // If it can't, then the result is the unsigned version of the signed type.
575 // Should probably add a helper that returns a signed integer type from
576 // an unsigned (and vice versa). C99 6.3.1.8.
577 return signedType;
578 }
Steve Naroffe4718892007-04-27 18:30:00 +0000579}