blob: 6e6849f0b240f5ac430b19f34a7cef384cafcff8 [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 Lattner983a8bb2007-07-13 22:13:22 +0000140
141/// getTypeSize - Return the size of the specified type, in bits. This method
142/// does not work on incomplete types.
Chris Lattner4481b422007-07-14 01:29:45 +0000143std::pair<uint64_t, unsigned>
144ASTContext::getTypeInfo(QualType T, SourceLocation L) {
Chris Lattner983a8bb2007-07-13 22:13:22 +0000145 T = T.getCanonicalType();
Chris Lattner4481b422007-07-14 01:29:45 +0000146 uint64_t Size;
147 unsigned Align;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000148 switch (T->getTypeClass()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000149 default:
150 case Type::Complex:
151 case Type::Array:
152 case Type::Vector:
153 case Type::TypeName:
154 case Type::Tagged:
155 assert(0 && "Unimplemented type sizes!");
156 case Type::FunctionNoProto:
157 case Type::FunctionProto:
158 assert(0 && "Incomplete types have no size!");
Chris Lattner983a8bb2007-07-13 22:13:22 +0000159 case Type::Builtin: {
160 // FIXME: need to use TargetInfo to derive the target specific sizes. This
161 // implementation will suffice for play with vector support.
162 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000163 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000164 case BuiltinType::Void:
165 assert(0 && "Incomplete types have no size!");
166 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000167 case BuiltinType::Char_S:
168 case BuiltinType::Char_U:
169 case BuiltinType::UChar:
Chris Lattner4481b422007-07-14 01:29:45 +0000170 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000171 case BuiltinType::UShort:
Chris Lattner4481b422007-07-14 01:29:45 +0000172 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000173 case BuiltinType::UInt:
Chris Lattner4481b422007-07-14 01:29:45 +0000174 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000175 case BuiltinType::ULong:
Chris Lattner4481b422007-07-14 01:29:45 +0000176 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000177 case BuiltinType::ULongLong:
Chris Lattner4481b422007-07-14 01:29:45 +0000178 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
179 case BuiltinType::Float: Target.getFloatInfo(Size, Align, L); break;
180 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, L); break;
181 case BuiltinType::LongDouble: Target.getLongDoubleInfo(Size, Align,L);break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000182 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000183 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000184 }
Chris Lattner4481b422007-07-14 01:29:45 +0000185 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000186 case Type::Reference:
Chris Lattner0523dc92007-07-13 22:16:13 +0000187 // "When applied to a reference or a reference type, the result is the size
188 // of the referenced type." C++98 5.3.3p2: expr.sizeof
Chris Lattner4481b422007-07-14 01:29:45 +0000189 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000190 }
Chris Lattner4481b422007-07-14 01:29:45 +0000191
192 return std::make_pair(Size, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000193}
194
195//===----------------------------------------------------------------------===//
196// Type creation/memoization methods
197//===----------------------------------------------------------------------===//
198
199
Chris Lattnerc6395932007-06-22 20:56:16 +0000200/// getComplexType - Return the uniqued reference to the type for a complex
201/// number with the specified element type.
202QualType ASTContext::getComplexType(QualType T) {
203 // Unique pointers, to guarantee there is only one pointer of a particular
204 // structure.
205 llvm::FoldingSetNodeID ID;
206 ComplexType::Profile(ID, T);
207
208 void *InsertPos = 0;
209 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
210 return QualType(CT, 0);
211
212 // If the pointee type isn't canonical, this won't be a canonical type either,
213 // so fill in the canonical type field.
214 QualType Canonical;
215 if (!T->isCanonical()) {
216 Canonical = getComplexType(T.getCanonicalType());
217
218 // Get the new insert position for the node we care about.
219 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
220 assert(NewIP == 0 && "Shouldn't be in the map!");
221 }
222 ComplexType *New = new ComplexType(T, Canonical);
223 Types.push_back(New);
224 ComplexTypes.InsertNode(New, InsertPos);
225 return QualType(New, 0);
226}
227
228
Chris Lattner970e54e2006-11-12 00:37:36 +0000229/// getPointerType - Return the uniqued reference to the type for a pointer to
230/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000231QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000232 // Unique pointers, to guarantee there is only one pointer of a particular
233 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000234 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +0000235 PointerType::Profile(ID, T);
236
237 void *InsertPos = 0;
238 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000239 return QualType(PT, 0);
Chris Lattner970e54e2006-11-12 00:37:36 +0000240
Chris Lattner7ccecb92006-11-12 08:50:50 +0000241 // If the pointee type isn't canonical, this won't be a canonical type either,
242 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000243 QualType Canonical;
Chris Lattner67521df2007-01-27 01:29:36 +0000244 if (!T->isCanonical()) {
Steve Naroffd50c88e2007-04-05 21:15:20 +0000245 Canonical = getPointerType(T.getCanonicalType());
Chris Lattner67521df2007-01-27 01:29:36 +0000246
247 // Get the new insert position for the node we care about.
248 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
249 assert(NewIP == 0 && "Shouldn't be in the map!");
250 }
Chris Lattner67521df2007-01-27 01:29:36 +0000251 PointerType *New = new PointerType(T, Canonical);
252 Types.push_back(New);
253 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000254 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +0000255}
256
Bill Wendling3708c182007-05-27 10:15:43 +0000257/// getReferenceType - Return the uniqued reference to the type for a reference
258/// to the specified type.
259QualType ASTContext::getReferenceType(QualType T) {
260 // Unique pointers, to guarantee there is only one pointer of a particular
261 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000262 llvm::FoldingSetNodeID ID;
Bill Wendling3708c182007-05-27 10:15:43 +0000263 ReferenceType::Profile(ID, T);
264
265 void *InsertPos = 0;
266 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
267 return QualType(RT, 0);
268
269 // If the referencee type isn't canonical, this won't be a canonical type
270 // either, so fill in the canonical type field.
271 QualType Canonical;
272 if (!T->isCanonical()) {
273 Canonical = getReferenceType(T.getCanonicalType());
274
275 // Get the new insert position for the node we care about.
276 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
277 assert(NewIP == 0 && "Shouldn't be in the map!");
278 }
279
280 ReferenceType *New = new ReferenceType(T, Canonical);
281 Types.push_back(New);
282 ReferenceTypes.InsertNode(New, InsertPos);
283 return QualType(New, 0);
284}
285
Chris Lattner7ccecb92006-11-12 08:50:50 +0000286/// getArrayType - Return the unique reference to the type for an array of the
287/// specified element type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000288QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
289 unsigned EltTypeQuals, Expr *NumElts) {
Chris Lattner36f8e652007-01-27 08:31:04 +0000290 // Unique array types, to guarantee there is only one array of a particular
Chris Lattner7ccecb92006-11-12 08:50:50 +0000291 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000292 llvm::FoldingSetNodeID ID;
Steve Naroffb7d49242007-03-14 19:55:17 +0000293 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
Steve Naroff408451b2007-02-26 22:17:12 +0000294
Chris Lattner36f8e652007-01-27 08:31:04 +0000295 void *InsertPos = 0;
296 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000297 return QualType(ATP, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000298
299 // If the element type isn't canonical, this won't be a canonical type either,
300 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000301 QualType Canonical;
Chris Lattner36f8e652007-01-27 08:31:04 +0000302 if (!EltTy->isCanonical()) {
Chris Lattner7ccecb92006-11-12 08:50:50 +0000303 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000304 NumElts);
Chris Lattner36f8e652007-01-27 08:31:04 +0000305
306 // Get the new insert position for the node we care about.
307 ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
308 assert(NewIP == 0 && "Shouldn't be in the map!");
309 }
Chris Lattner7ccecb92006-11-12 08:50:50 +0000310
Steve Naroffb7d49242007-03-14 19:55:17 +0000311 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
Chris Lattner36f8e652007-01-27 08:31:04 +0000312 ArrayTypes.InsertNode(New, InsertPos);
313 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000314 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000315}
316
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000317/// convertToVectorType - Return the unique reference to a vector type of
318/// the specified element type and size. VectorType can be a pointer, array,
319/// function, or built-in type (i.e. _Bool, integer, or float).
320QualType ASTContext::convertToVectorType(QualType vecType, unsigned NumElts) {
321 BuiltinType *baseType;
322
323 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
324 assert(baseType != 0 &&
325 "convertToVectorType(): Complex vector types unimplemented");
326
327 // Check if we've already instantiated a vector of this type.
328 llvm::FoldingSetNodeID ID;
329 VectorType::Profile(ID, vecType, NumElts);
330 void *InsertPos = 0;
331 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
332 return QualType(VTP, 0);
333
334 // If the element type isn't canonical, this won't be a canonical type either,
335 // so fill in the canonical type field.
336 QualType Canonical;
337 if (!vecType->isCanonical()) {
338 Canonical = convertToVectorType(vecType.getCanonicalType(), NumElts);
339
340 // Get the new insert position for the node we care about.
341 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
342 assert(NewIP == 0 && "Shouldn't be in the map!");
343 }
344 VectorType *New = new VectorType(vecType, NumElts, Canonical);
345 VectorTypes.InsertNode(New, InsertPos);
346 Types.push_back(New);
347 return QualType(New, 0);
348}
349
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000350/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
351///
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000352QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000353 // Unique functions, to guarantee there is only one function of a particular
354 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000355 llvm::FoldingSetNodeID ID;
Chris Lattner47955de2007-01-27 08:37:20 +0000356 FunctionTypeNoProto::Profile(ID, ResultTy);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000357
Chris Lattner47955de2007-01-27 08:37:20 +0000358 void *InsertPos = 0;
359 if (FunctionTypeNoProto *FT =
360 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000361 return QualType(FT, 0);
Chris Lattner47955de2007-01-27 08:37:20 +0000362
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000363 QualType Canonical;
Chris Lattner47955de2007-01-27 08:37:20 +0000364 if (!ResultTy->isCanonical()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000365 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
Chris Lattner47955de2007-01-27 08:37:20 +0000366
367 // Get the new insert position for the node we care about.
368 FunctionTypeNoProto *NewIP =
369 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
370 assert(NewIP == 0 && "Shouldn't be in the map!");
371 }
372
373 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
374 Types.push_back(New);
375 FunctionTypeProtos.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000376 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000377}
378
379/// getFunctionType - Return a normal function type with a typed argument
380/// list. isVariadic indicates whether the argument list includes '...'.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000381QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
382 unsigned NumArgs, bool isVariadic) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000383 // Unique functions, to guarantee there is only one function of a particular
384 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000385 llvm::FoldingSetNodeID ID;
Chris Lattnerfd4de792007-01-27 01:15:32 +0000386 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
387
388 void *InsertPos = 0;
389 if (FunctionTypeProto *FTP =
390 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000391 return QualType(FTP, 0);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000392
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000393 // Determine whether the type being created is already canonical or not.
394 bool isCanonical = ResultTy->isCanonical();
395 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
396 if (!ArgArray[i]->isCanonical())
397 isCanonical = false;
398
399 // If this type isn't canonical, get the canonical version of it.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000400 QualType Canonical;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000401 if (!isCanonical) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000402 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000403 CanonicalArgs.reserve(NumArgs);
404 for (unsigned i = 0; i != NumArgs; ++i)
405 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
406
407 Canonical = getFunctionType(ResultTy.getCanonicalType(),
408 &CanonicalArgs[0], NumArgs,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000409 isVariadic);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000410
411 // Get the new insert position for the node we care about.
412 FunctionTypeProto *NewIP =
413 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
414 assert(NewIP == 0 && "Shouldn't be in the map!");
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000415 }
416
417 // FunctionTypeProto objects are not allocated with new because they have a
418 // variable size array (for parameter types) at the end of them.
419 FunctionTypeProto *FTP =
420 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000421 (NumArgs-1)*sizeof(QualType));
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000422 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
423 Canonical);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000424 Types.push_back(FTP);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000425 FunctionTypeProtos.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000426 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000427}
Chris Lattneref51c202006-11-10 07:17:23 +0000428
Chris Lattner32d920b2007-01-26 02:01:53 +0000429/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +0000430/// specified typename decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000431QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
432 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000433
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000434 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Chris Lattnercceab1a2007-03-26 20:16:44 +0000435 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
436 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000437 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000438}
439
Chris Lattnerfb072462007-01-23 05:45:31 +0000440/// getTagDeclType - Return the unique reference to the type for the
441/// specified TagDecl (struct/union/class/enum) decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000442QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Chris Lattner733067d2007-01-26 01:42:24 +0000443 // The decl stores the type cache.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000444 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000445
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000446 Decl->TypeForDecl = new TagType(Decl, QualType());
Chris Lattnercceab1a2007-03-26 20:16:44 +0000447 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000448 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000449}
450
Steve Naroff92e30f82007-04-02 22:35:25 +0000451/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
Steve Naroff0f6256d2007-04-02 23:01:44 +0000452/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
Steve Naroff92e30f82007-04-02 22:35:25 +0000453/// needs to agree with the definition in <stddef.h>.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000454QualType ASTContext::getSizeType() const {
Steve Naroff92e30f82007-04-02 22:35:25 +0000455 // On Darwin, size_t is defined as a "long unsigned int".
456 // FIXME: should derive from "Target".
457 return UnsignedLongTy;
458}
Chris Lattnerfb072462007-01-23 05:45:31 +0000459
Chris Lattnerd2b88ab2007-07-13 03:05:23 +0000460/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
461/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
462QualType ASTContext::getPointerDiffType() const {
463 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
464 // FIXME: should derive from "Target".
465 return IntTy;
466}
467
Steve Naroff0af91202007-04-27 21:51:21 +0000468/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
469/// routine will assert if passed a built-in type that isn't an integer or enum.
470static int getIntegerRank(QualType t) {
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000471 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
472 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
473 return 4;
Steve Naroffe4718892007-04-27 18:30:00 +0000474 }
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000475
476 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
477 switch (BT->getKind()) {
478 default:
479 assert(0 && "getIntegerRank(): not a built-in integer");
480 case BuiltinType::Bool:
481 return 1;
Chris Lattnerb16f4552007-06-03 07:25:34 +0000482 case BuiltinType::Char_S:
483 case BuiltinType::Char_U:
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000484 case BuiltinType::SChar:
485 case BuiltinType::UChar:
486 return 2;
487 case BuiltinType::Short:
488 case BuiltinType::UShort:
489 return 3;
490 case BuiltinType::Int:
491 case BuiltinType::UInt:
492 return 4;
493 case BuiltinType::Long:
494 case BuiltinType::ULong:
495 return 5;
496 case BuiltinType::LongLong:
497 case BuiltinType::ULongLong:
498 return 6;
499 }
Steve Naroffe4718892007-04-27 18:30:00 +0000500}
501
Steve Naroff0af91202007-04-27 21:51:21 +0000502/// getFloatingRank - Return a relative rank for floating point types.
503/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerc6395932007-06-22 20:56:16 +0000504static int getFloatingRank(QualType T) {
505 T = T.getCanonicalType();
506 if (ComplexType *CT = dyn_cast<ComplexType>(T))
507 return getFloatingRank(CT->getElementType());
508
509 switch (cast<BuiltinType>(T)->getKind()) {
510 default: assert(0 && "getFloatingPointRank(): not a floating type");
511 case BuiltinType::Float: return FloatRank;
512 case BuiltinType::Double: return DoubleRank;
513 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +0000514 }
515}
516
517// maxComplexType - the following code handles 3 different combinations:
518// complex/complex, complex/float, float/complex.
519// When both operands are complex, the shorter operand is converted to the
520// type of the longer, and that is the type of the result. This corresponds
521// to what is done when combining two real floating-point operands.
522// The fun begins when size promotion occur across type domains. g
523// getFloatingRank & convertFloatingRankToComplexType handle this without
524// enumerating all permutations.
525// It also allows us to add new types without breakage.
526// From H&S 6.3.4: When one operand is complex and the other is a real
527// floating-point type, the less precise type is converted, within it's
528// real or complex domain, to the precision of the other type. For example,
529// when combining a "long double" with a "double _Complex", the
530// "double _Complex" is promoted to "long double _Complex".
531
Steve Naroff0af91202007-04-27 21:51:21 +0000532QualType ASTContext::maxComplexType(QualType lt, QualType rt) const {
533 switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) {
Chris Lattnerc6395932007-06-22 20:56:16 +0000534 default: assert(0 && "convertRankToComplex(): illegal value for rank");
535 case FloatRank: return FloatComplexTy;
536 case DoubleRank: return DoubleComplexTy;
537 case LongDoubleRank: return LongDoubleComplexTy;
Steve Naroff0af91202007-04-27 21:51:21 +0000538 }
Steve Naroffe4718892007-04-27 18:30:00 +0000539}
540
541// maxFloatingType - handles the simple case, both operands are floats.
542QualType ASTContext::maxFloatingType(QualType lt, QualType rt) {
543 return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt;
544}
545
Steve Naroff0af91202007-04-27 21:51:21 +0000546// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
547// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
Steve Naroffe4718892007-04-27 18:30:00 +0000548QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
Chris Lattnerb16f4552007-06-03 07:25:34 +0000549 if (lhs == rhs) return lhs;
550
Steve Naroffe4718892007-04-27 18:30:00 +0000551 bool t1Unsigned = lhs->isUnsignedIntegerType();
552 bool t2Unsigned = rhs->isUnsignedIntegerType();
553
554 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
555 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
556
557 // We have two integer types with differing signs
558 QualType unsignedType = t1Unsigned ? lhs : rhs;
559 QualType signedType = t1Unsigned ? rhs : lhs;
560
561 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
562 return unsignedType;
Steve Naroff0af91202007-04-27 21:51:21 +0000563 else {
564 // FIXME: Need to check if the signed type can represent all values of the
565 // unsigned type. If it can, then the result is the signed type.
566 // If it can't, then the result is the unsigned version of the signed type.
567 // Should probably add a helper that returns a signed integer type from
568 // an unsigned (and vice versa). C99 6.3.1.8.
569 return signedType;
570 }
Steve Naroffe4718892007-04-27 18:30:00 +0000571}