blob: 7e9f5e96b03dc3b5e9e3f6a55b8327a378632431 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +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 NumVector = 0, NumComplex = 0;
44 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
45
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;
54 else if (isa<ReferenceType>(T))
55 ++NumReference;
56 else if (isa<ComplexType>(T))
57 ++NumComplex;
58 else if (isa<ArrayType>(T))
59 ++NumArray;
60 else if (isa<VectorType>(T))
61 ++NumVector;
62 else if (isa<FunctionTypeNoProto>(T))
63 ++NumFunctionNP;
64 else if (isa<FunctionTypeProto>(T))
65 ++NumFunctionP;
66 else if (isa<TypedefType>(T))
67 ++NumTypeName;
68 else if (TagType *TT = dyn_cast<TagType>(T)) {
69 ++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);
84 fprintf(stderr, " %d reference types\n", NumReference);
85 fprintf(stderr, " %d complex types\n", NumComplex);
86 fprintf(stderr, " %d array types\n", NumArray);
87 fprintf(stderr, " %d vector types\n", NumVector);
88 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);
96 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
97 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
98 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
99 NumFunctionP*sizeof(FunctionTypeProto)+
100 NumFunctionNP*sizeof(FunctionTypeNoProto)+
101 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
102}
103
104
105void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
106 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
107}
108
109
110void ASTContext::InitBuiltinTypes() {
111 assert(VoidTy.isNull() && "Context reinitialized?");
112
113 // C99 6.2.5p19.
114 InitBuiltinType(VoidTy, BuiltinType::Void);
115
116 // C99 6.2.5p2.
117 InitBuiltinType(BoolTy, BuiltinType::Bool);
118 // C99 6.2.5p3.
119 if (Target.isCharSigned(SourceLocation()))
120 InitBuiltinType(CharTy, BuiltinType::Char_S);
121 else
122 InitBuiltinType(CharTy, BuiltinType::Char_U);
123 // C99 6.2.5p4.
124 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
125 InitBuiltinType(ShortTy, BuiltinType::Short);
126 InitBuiltinType(IntTy, BuiltinType::Int);
127 InitBuiltinType(LongTy, BuiltinType::Long);
128 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
129
130 // C99 6.2.5p6.
131 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
132 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
133 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
134 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
135 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
136
137 // C99 6.2.5p10.
138 InitBuiltinType(FloatTy, BuiltinType::Float);
139 InitBuiltinType(DoubleTy, BuiltinType::Double);
140 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
141
142 // C99 6.2.5p11.
143 FloatComplexTy = getComplexType(FloatTy);
144 DoubleComplexTy = getComplexType(DoubleTy);
145 LongDoubleComplexTy = getComplexType(LongDoubleTy);
146}
147
148//===----------------------------------------------------------------------===//
149// Type Sizing and Analysis
150//===----------------------------------------------------------------------===//
151
152/// getTypeSize - Return the size of the specified type, in bits. This method
153/// does not work on incomplete types.
154std::pair<uint64_t, unsigned>
155ASTContext::getTypeInfo(QualType T, SourceLocation L) {
156 T = T.getCanonicalType();
157 uint64_t Size;
158 unsigned Align;
159 switch (T->getTypeClass()) {
160 case Type::TypeName: assert(0 && "Not a canonical type!");
161 case Type::FunctionNoProto:
162 case Type::FunctionProto:
163 default:
164 assert(0 && "Incomplete types have no size!");
Steve Naroff83c13012007-08-30 01:06:46 +0000165 case Type::VariableArray:
166 assert(0 && "VLAs not implemented yet!");
167 case Type::ConstantArray: {
168 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
169
Chris Lattner4b009652007-07-25 00:24:17 +0000170 std::pair<uint64_t, unsigned> EltInfo =
Steve Naroff83c13012007-08-30 01:06:46 +0000171 getTypeInfo(CAT->getElementType(), L);
172 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner4b009652007-07-25 00:24:17 +0000173 Align = EltInfo.second;
174 break;
175 }
176 case Type::Vector: {
177 std::pair<uint64_t, unsigned> EltInfo =
178 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
179 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
180 // FIXME: Vector alignment is not the alignment of its elements.
181 Align = EltInfo.second;
182 break;
183 }
184
185 case Type::Builtin: {
186 // FIXME: need to use TargetInfo to derive the target specific sizes. This
187 // implementation will suffice for play with vector support.
188 switch (cast<BuiltinType>(T)->getKind()) {
189 default: assert(0 && "Unknown builtin type!");
190 case BuiltinType::Void:
191 assert(0 && "Incomplete types have no size!");
192 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
193 case BuiltinType::Char_S:
194 case BuiltinType::Char_U:
195 case BuiltinType::UChar:
196 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
197 case BuiltinType::UShort:
198 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
199 case BuiltinType::UInt:
200 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
201 case BuiltinType::ULong:
202 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
203 case BuiltinType::ULongLong:
204 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
205 case BuiltinType::Float: Target.getFloatInfo(Size, Align, L); break;
206 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, L); break;
207 case BuiltinType::LongDouble: Target.getLongDoubleInfo(Size, Align,L);break;
208 }
209 break;
210 }
211 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
212 case Type::Reference:
213 // "When applied to a reference or a reference type, the result is the size
214 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
215 // FIXME: This is wrong for struct layout!
216 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
217
218 case Type::Complex: {
219 // Complex types have the same alignment as their elements, but twice the
220 // size.
221 std::pair<uint64_t, unsigned> EltInfo =
222 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
223 Size = EltInfo.first*2;
224 Align = EltInfo.second;
225 break;
226 }
227 case Type::Tagged:
Chris Lattnereb56d292007-08-27 17:38:00 +0000228 TagType *TT = cast<TagType>(T);
229 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
230 const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L);
231 Size = Layout.getSize();
232 Align = Layout.getAlignment();
233 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattner90a018d2007-08-28 18:24:31 +0000234 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattnereb56d292007-08-27 17:38:00 +0000235 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000236 assert(0 && "Unimplemented type sizes!");
Chris Lattnereb56d292007-08-27 17:38:00 +0000237 }
Chris Lattner4b009652007-07-25 00:24:17 +0000238 break;
239 }
240
241 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
242 return std::make_pair(Size, Align);
243}
244
245/// getRecordLayout - Get or compute information about the layout of the
246/// specified record (struct/union/class), which indicates its size and field
247/// position information.
248const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D,
249 SourceLocation L) {
250 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
251
252 // Look up this layout, if already laid out, return what we have.
253 const RecordLayout *&Entry = RecordLayoutInfo[D];
254 if (Entry) return *Entry;
255
256 // Allocate and assign into RecordLayoutInfo here. The "Entry" reference can
257 // be invalidated (dangle) if the RecordLayoutInfo hashtable is inserted into.
258 RecordLayout *NewEntry = new RecordLayout();
259 Entry = NewEntry;
260
261 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
262 uint64_t RecordSize = 0;
263 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
264
265 if (D->getKind() != Decl::Union) {
266 // Layout each field, for now, just sequentially, respecting alignment. In
267 // the future, this will need to be tweakable by targets.
268 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
269 const FieldDecl *FD = D->getMember(i);
270 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
271 uint64_t FieldSize = FieldInfo.first;
272 unsigned FieldAlign = FieldInfo.second;
273
274 // Round up the current record size to the field's alignment boundary.
275 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
276
277 // Place this field at the current location.
278 FieldOffsets[i] = RecordSize;
279
280 // Reserve space for this field.
281 RecordSize += FieldSize;
282
283 // Remember max struct/class alignment.
284 RecordAlign = std::max(RecordAlign, FieldAlign);
285 }
286
287 // Finally, round the size of the total struct up to the alignment of the
288 // struct itself.
289 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
290 } else {
291 // Union layout just puts each member at the start of the record.
292 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
293 const FieldDecl *FD = D->getMember(i);
294 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
295 uint64_t FieldSize = FieldInfo.first;
296 unsigned FieldAlign = FieldInfo.second;
297
298 // Round up the current record size to the field's alignment boundary.
299 RecordSize = std::max(RecordSize, FieldSize);
300
301 // Place this field at the start of the record.
302 FieldOffsets[i] = 0;
303
304 // Remember max struct/class alignment.
305 RecordAlign = std::max(RecordAlign, FieldAlign);
306 }
307 }
308
309 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
310 return *NewEntry;
311}
312
Chris Lattner4b009652007-07-25 00:24:17 +0000313//===----------------------------------------------------------------------===//
314// Type creation/memoization methods
315//===----------------------------------------------------------------------===//
316
317
318/// getComplexType - Return the uniqued reference to the type for a complex
319/// number with the specified element type.
320QualType ASTContext::getComplexType(QualType T) {
321 // Unique pointers, to guarantee there is only one pointer of a particular
322 // structure.
323 llvm::FoldingSetNodeID ID;
324 ComplexType::Profile(ID, T);
325
326 void *InsertPos = 0;
327 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
328 return QualType(CT, 0);
329
330 // If the pointee type isn't canonical, this won't be a canonical type either,
331 // so fill in the canonical type field.
332 QualType Canonical;
333 if (!T->isCanonical()) {
334 Canonical = getComplexType(T.getCanonicalType());
335
336 // Get the new insert position for the node we care about.
337 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
338 assert(NewIP == 0 && "Shouldn't be in the map!");
339 }
340 ComplexType *New = new ComplexType(T, Canonical);
341 Types.push_back(New);
342 ComplexTypes.InsertNode(New, InsertPos);
343 return QualType(New, 0);
344}
345
346
347/// getPointerType - Return the uniqued reference to the type for a pointer to
348/// the specified type.
349QualType ASTContext::getPointerType(QualType T) {
350 // Unique pointers, to guarantee there is only one pointer of a particular
351 // structure.
352 llvm::FoldingSetNodeID ID;
353 PointerType::Profile(ID, T);
354
355 void *InsertPos = 0;
356 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
357 return QualType(PT, 0);
358
359 // If the pointee type isn't canonical, this won't be a canonical type either,
360 // so fill in the canonical type field.
361 QualType Canonical;
362 if (!T->isCanonical()) {
363 Canonical = getPointerType(T.getCanonicalType());
364
365 // Get the new insert position for the node we care about.
366 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
367 assert(NewIP == 0 && "Shouldn't be in the map!");
368 }
369 PointerType *New = new PointerType(T, Canonical);
370 Types.push_back(New);
371 PointerTypes.InsertNode(New, InsertPos);
372 return QualType(New, 0);
373}
374
375/// getReferenceType - Return the uniqued reference to the type for a reference
376/// to the specified type.
377QualType ASTContext::getReferenceType(QualType T) {
378 // Unique pointers, to guarantee there is only one pointer of a particular
379 // structure.
380 llvm::FoldingSetNodeID ID;
381 ReferenceType::Profile(ID, T);
382
383 void *InsertPos = 0;
384 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
385 return QualType(RT, 0);
386
387 // If the referencee type isn't canonical, this won't be a canonical type
388 // either, so fill in the canonical type field.
389 QualType Canonical;
390 if (!T->isCanonical()) {
391 Canonical = getReferenceType(T.getCanonicalType());
392
393 // Get the new insert position for the node we care about.
394 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
395 assert(NewIP == 0 && "Shouldn't be in the map!");
396 }
397
398 ReferenceType *New = new ReferenceType(T, Canonical);
399 Types.push_back(New);
400 ReferenceTypes.InsertNode(New, InsertPos);
401 return QualType(New, 0);
402}
403
Steve Naroff83c13012007-08-30 01:06:46 +0000404/// getConstantArrayType - Return the unique reference to the type for an
405/// array of the specified element type.
406QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff24c9b982007-08-30 18:10:14 +0000407 const llvm::APInt &ArySize,
408 ArrayType::ArraySizeModifier ASM,
409 unsigned EltTypeQuals) {
Chris Lattner4b009652007-07-25 00:24:17 +0000410 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000411 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000412
413 void *InsertPos = 0;
Steve Naroff83c13012007-08-30 01:06:46 +0000414 if (ConstantArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000415 return QualType(ATP, 0);
416
417 // If the element type isn't canonical, this won't be a canonical type either,
418 // so fill in the canonical type field.
419 QualType Canonical;
420 if (!EltTy->isCanonical()) {
Steve Naroff24c9b982007-08-30 18:10:14 +0000421 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
422 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000423 // Get the new insert position for the node we care about.
Steve Naroff83c13012007-08-30 01:06:46 +0000424 ConstantArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000425 assert(NewIP == 0 && "Shouldn't be in the map!");
426 }
427
Steve Naroff24c9b982007-08-30 18:10:14 +0000428 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
429 ASM, EltTypeQuals);
Chris Lattner4b009652007-07-25 00:24:17 +0000430 ArrayTypes.InsertNode(New, InsertPos);
431 Types.push_back(New);
432 return QualType(New, 0);
433}
434
Steve Naroffe2579e32007-08-30 18:14:25 +0000435/// getVariableArrayType - Returns a non-unique reference to the type for a
436/// variable array of the specified element type.
Steve Naroff24c9b982007-08-30 18:10:14 +0000437QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
438 ArrayType::ArraySizeModifier ASM,
439 unsigned EltTypeQuals) {
440 // Since we don't unique expressions, it isn't possible to unique VLA's.
441 ArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
442 ASM, EltTypeQuals);
443 Types.push_back(New);
444 return QualType(New, 0);
Steve Naroff83c13012007-08-30 01:06:46 +0000445}
446
Chris Lattner4b009652007-07-25 00:24:17 +0000447/// getVectorType - Return the unique reference to a vector type of
448/// the specified element type and size. VectorType must be a built-in type.
449QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
450 BuiltinType *baseType;
451
452 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
453 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
454
455 // Check if we've already instantiated a vector of this type.
456 llvm::FoldingSetNodeID ID;
457 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
458 void *InsertPos = 0;
459 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
460 return QualType(VTP, 0);
461
462 // If the element type isn't canonical, this won't be a canonical type either,
463 // so fill in the canonical type field.
464 QualType Canonical;
465 if (!vecType->isCanonical()) {
466 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
467
468 // Get the new insert position for the node we care about.
469 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
470 assert(NewIP == 0 && "Shouldn't be in the map!");
471 }
472 VectorType *New = new VectorType(vecType, NumElts, Canonical);
473 VectorTypes.InsertNode(New, InsertPos);
474 Types.push_back(New);
475 return QualType(New, 0);
476}
477
478/// getOCUVectorType - Return the unique reference to an OCU vector type of
479/// the specified element type and size. VectorType must be a built-in type.
480QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
481 BuiltinType *baseType;
482
483 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
484 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
485
486 // Check if we've already instantiated a vector of this type.
487 llvm::FoldingSetNodeID ID;
488 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
489 void *InsertPos = 0;
490 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
491 return QualType(VTP, 0);
492
493 // If the element type isn't canonical, this won't be a canonical type either,
494 // so fill in the canonical type field.
495 QualType Canonical;
496 if (!vecType->isCanonical()) {
497 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
498
499 // Get the new insert position for the node we care about.
500 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
501 assert(NewIP == 0 && "Shouldn't be in the map!");
502 }
503 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
504 VectorTypes.InsertNode(New, InsertPos);
505 Types.push_back(New);
506 return QualType(New, 0);
507}
508
509/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
510///
511QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
512 // Unique functions, to guarantee there is only one function of a particular
513 // structure.
514 llvm::FoldingSetNodeID ID;
515 FunctionTypeNoProto::Profile(ID, ResultTy);
516
517 void *InsertPos = 0;
518 if (FunctionTypeNoProto *FT =
519 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
520 return QualType(FT, 0);
521
522 QualType Canonical;
523 if (!ResultTy->isCanonical()) {
524 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
525
526 // Get the new insert position for the node we care about.
527 FunctionTypeNoProto *NewIP =
528 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
529 assert(NewIP == 0 && "Shouldn't be in the map!");
530 }
531
532 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
533 Types.push_back(New);
534 FunctionTypeProtos.InsertNode(New, InsertPos);
535 return QualType(New, 0);
536}
537
538/// getFunctionType - Return a normal function type with a typed argument
539/// list. isVariadic indicates whether the argument list includes '...'.
540QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
541 unsigned NumArgs, bool isVariadic) {
542 // Unique functions, to guarantee there is only one function of a particular
543 // structure.
544 llvm::FoldingSetNodeID ID;
545 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
546
547 void *InsertPos = 0;
548 if (FunctionTypeProto *FTP =
549 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
550 return QualType(FTP, 0);
551
552 // Determine whether the type being created is already canonical or not.
553 bool isCanonical = ResultTy->isCanonical();
554 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
555 if (!ArgArray[i]->isCanonical())
556 isCanonical = false;
557
558 // If this type isn't canonical, get the canonical version of it.
559 QualType Canonical;
560 if (!isCanonical) {
561 llvm::SmallVector<QualType, 16> CanonicalArgs;
562 CanonicalArgs.reserve(NumArgs);
563 for (unsigned i = 0; i != NumArgs; ++i)
564 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
565
566 Canonical = getFunctionType(ResultTy.getCanonicalType(),
567 &CanonicalArgs[0], NumArgs,
568 isVariadic);
569
570 // Get the new insert position for the node we care about.
571 FunctionTypeProto *NewIP =
572 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
573 assert(NewIP == 0 && "Shouldn't be in the map!");
574 }
575
576 // FunctionTypeProto objects are not allocated with new because they have a
577 // variable size array (for parameter types) at the end of them.
578 FunctionTypeProto *FTP =
579 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
580 NumArgs*sizeof(QualType));
581 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
582 Canonical);
583 Types.push_back(FTP);
584 FunctionTypeProtos.InsertNode(FTP, InsertPos);
585 return QualType(FTP, 0);
586}
587
588/// getTypedefType - Return the unique reference to the type for the
589/// specified typename decl.
590QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
591 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
592
593 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
594 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
595 Types.push_back(Decl->TypeForDecl);
596 return QualType(Decl->TypeForDecl, 0);
597}
598
Steve Naroff81f1bba2007-09-06 21:24:23 +0000599/// getObjcInterfaceType - Return the unique reference to the type for the
600/// specified ObjC interface decl.
601QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) {
602 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
603
604 Decl->TypeForDecl = new ObjcInterfaceType(Decl);
605 Types.push_back(Decl->TypeForDecl);
606 return QualType(Decl->TypeForDecl, 0);
607}
608
Steve Naroff0604dd92007-08-01 18:02:17 +0000609/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
610/// TypeOfExpr AST's (since expression's are never shared). For example,
611/// multiple declarations that refer to "typeof(x)" all contain different
612/// DeclRefExpr's. This doesn't effect the type checker, since it operates
613/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000614QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroff7cbb1462007-07-31 12:34:36 +0000615 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000616 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
617 Types.push_back(toe);
618 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000619}
620
Steve Naroff0604dd92007-08-01 18:02:17 +0000621/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
622/// TypeOfType AST's. The only motivation to unique these nodes would be
623/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
624/// an issue. This doesn't effect the type checker, since it operates
625/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000626QualType ASTContext::getTypeOfType(QualType tofType) {
627 QualType Canonical = tofType.getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000628 TypeOfType *tot = new TypeOfType(tofType, Canonical);
629 Types.push_back(tot);
630 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000631}
632
Chris Lattner4b009652007-07-25 00:24:17 +0000633/// getTagDeclType - Return the unique reference to the type for the
634/// specified TagDecl (struct/union/class/enum) decl.
635QualType ASTContext::getTagDeclType(TagDecl *Decl) {
636 // The decl stores the type cache.
637 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
638
639 Decl->TypeForDecl = new TagType(Decl, QualType());
640 Types.push_back(Decl->TypeForDecl);
641 return QualType(Decl->TypeForDecl, 0);
642}
643
644/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
645/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
646/// needs to agree with the definition in <stddef.h>.
647QualType ASTContext::getSizeType() const {
648 // On Darwin, size_t is defined as a "long unsigned int".
649 // FIXME: should derive from "Target".
650 return UnsignedLongTy;
651}
652
653/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
654/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
655QualType ASTContext::getPointerDiffType() const {
656 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
657 // FIXME: should derive from "Target".
658 return IntTy;
659}
660
661/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
662/// routine will assert if passed a built-in type that isn't an integer or enum.
663static int getIntegerRank(QualType t) {
664 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
665 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
666 return 4;
667 }
668
669 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
670 switch (BT->getKind()) {
671 default:
672 assert(0 && "getIntegerRank(): not a built-in integer");
673 case BuiltinType::Bool:
674 return 1;
675 case BuiltinType::Char_S:
676 case BuiltinType::Char_U:
677 case BuiltinType::SChar:
678 case BuiltinType::UChar:
679 return 2;
680 case BuiltinType::Short:
681 case BuiltinType::UShort:
682 return 3;
683 case BuiltinType::Int:
684 case BuiltinType::UInt:
685 return 4;
686 case BuiltinType::Long:
687 case BuiltinType::ULong:
688 return 5;
689 case BuiltinType::LongLong:
690 case BuiltinType::ULongLong:
691 return 6;
692 }
693}
694
695/// getFloatingRank - Return a relative rank for floating point types.
696/// This routine will assert if passed a built-in type that isn't a float.
697static int getFloatingRank(QualType T) {
698 T = T.getCanonicalType();
699 if (ComplexType *CT = dyn_cast<ComplexType>(T))
700 return getFloatingRank(CT->getElementType());
701
702 switch (cast<BuiltinType>(T)->getKind()) {
703 default: assert(0 && "getFloatingPointRank(): not a floating type");
704 case BuiltinType::Float: return FloatRank;
705 case BuiltinType::Double: return DoubleRank;
706 case BuiltinType::LongDouble: return LongDoubleRank;
707 }
708}
709
Steve Narofffa0c4532007-08-27 01:41:48 +0000710/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
711/// point or a complex type (based on typeDomain/typeSize).
712/// 'typeDomain' is a real floating point or complex type.
713/// 'typeSize' is a real floating point or complex type.
Steve Naroff3cf497f2007-08-27 01:27:54 +0000714QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
715 QualType typeSize, QualType typeDomain) const {
716 if (typeDomain->isComplexType()) {
717 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000718 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000719 case FloatRank: return FloatComplexTy;
720 case DoubleRank: return DoubleComplexTy;
721 case LongDoubleRank: return LongDoubleComplexTy;
722 }
Chris Lattner4b009652007-07-25 00:24:17 +0000723 }
Steve Naroff3cf497f2007-08-27 01:27:54 +0000724 if (typeDomain->isRealFloatingType()) {
725 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000726 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000727 case FloatRank: return FloatTy;
728 case DoubleRank: return DoubleTy;
729 case LongDoubleRank: return LongDoubleTy;
730 }
731 }
732 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner4b009652007-07-25 00:24:17 +0000733}
734
Steve Naroff45fc9822007-08-27 15:30:22 +0000735/// compareFloatingType - Handles 3 different combos:
736/// float/float, float/complex, complex/complex.
737/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
738int ASTContext::compareFloatingType(QualType lt, QualType rt) {
739 if (getFloatingRank(lt) == getFloatingRank(rt))
740 return 0;
741 if (getFloatingRank(lt) > getFloatingRank(rt))
742 return 1;
743 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +0000744}
745
746// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
747// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
748QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
749 if (lhs == rhs) return lhs;
750
751 bool t1Unsigned = lhs->isUnsignedIntegerType();
752 bool t2Unsigned = rhs->isUnsignedIntegerType();
753
754 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
755 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
756
757 // We have two integer types with differing signs
758 QualType unsignedType = t1Unsigned ? lhs : rhs;
759 QualType signedType = t1Unsigned ? rhs : lhs;
760
761 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
762 return unsignedType;
763 else {
764 // FIXME: Need to check if the signed type can represent all values of the
765 // unsigned type. If it can, then the result is the signed type.
766 // If it can't, then the result is the unsigned version of the signed type.
767 // Should probably add a helper that returns a signed integer type from
768 // an unsigned (and vice versa). C99 6.3.1.8.
769 return signedType;
770 }
771}
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000772
773// getCFConstantStringType - Return the type used for constant CFStrings.
774QualType ASTContext::getCFConstantStringType() {
775 if (!CFConstantStringTypeDecl) {
776 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
777 &Idents.get("__builtin_CFString"),
778 0);
779
780 QualType FieldTypes[4];
781
782 // const int *isa;
783 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
784 // int flags;
785 FieldTypes[1] = IntTy;
786 // const char *str;
787 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
788 // long length;
789 FieldTypes[3] = LongTy;
790 // Create fields
791 FieldDecl *FieldDecls[4];
792
793 for (unsigned i = 0; i < 4; ++i)
794 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i], 0);
795
796 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
797 }
798
799 return getTagDeclType(CFConstantStringTypeDecl);
Chris Lattnereb56d292007-08-27 17:38:00 +0000800}