blob: 57c1c44dbe31cb7d3807914d05baa39215f45860 [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,
407 const llvm::APInt &ArySize) {
Chris Lattner4b009652007-07-25 00:24:17 +0000408 llvm::FoldingSetNodeID ID;
Steve Naroff83c13012007-08-30 01:06:46 +0000409 ConstantArrayType::Profile(ID, EltTy, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000410
411 void *InsertPos = 0;
Steve Naroff83c13012007-08-30 01:06:46 +0000412 if (ConstantArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Chris Lattner4b009652007-07-25 00:24:17 +0000413 return QualType(ATP, 0);
414
415 // If the element type isn't canonical, this won't be a canonical type either,
416 // so fill in the canonical type field.
417 QualType Canonical;
418 if (!EltTy->isCanonical()) {
Steve Naroff83c13012007-08-30 01:06:46 +0000419 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000420
421 // Get the new insert position for the node we care about.
Steve Naroff83c13012007-08-30 01:06:46 +0000422 ConstantArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner4b009652007-07-25 00:24:17 +0000423 assert(NewIP == 0 && "Shouldn't be in the map!");
424 }
425
Steve Naroff83c13012007-08-30 01:06:46 +0000426 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize);
Chris Lattner4b009652007-07-25 00:24:17 +0000427 ArrayTypes.InsertNode(New, InsertPos);
428 Types.push_back(New);
429 return QualType(New, 0);
430}
431
Steve Naroff83c13012007-08-30 01:06:46 +0000432/// getArrayType - If NumElts is a constant expression, we return a unique
433/// reference to an AST node of type ConstantArrayType. If NumElts is not
434/// a constant expression, we return an instance of VaribleLengthArrayType.
435QualType ASTContext::getArrayType(QualType EltTy,
436 ArrayType::ArraySizeModifier ASM,
437 unsigned EltTypeQuals, Expr *NumElts) {
438 llvm::APSInt ArySize(32);
439 // If no expression was provided, we consider it a VLA.
440 if (!NumElts || !NumElts->isIntegerConstantExpr(ArySize, *this)) {
441 // Since we don't unique expressions, it isn't possible to unique VLA's.
442 ArrayType *New = new VariableArrayType(EltTy, ASM, EltTypeQuals,
443 QualType(), NumElts);
444 Types.push_back(New);
445 return QualType(New, 0);
446 }
447 // Unique constant array types, to guarantee there is only one array of a
448 // particular structure.
449 // FIXME: should we warn if ASM != ArrayType::Normal or EltTypeQuals != 0?
450 return getConstantArrayType(EltTy, ArySize);
451}
452
Chris Lattner4b009652007-07-25 00:24:17 +0000453/// getVectorType - Return the unique reference to a vector type of
454/// the specified element type and size. VectorType must be a built-in type.
455QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
456 BuiltinType *baseType;
457
458 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
459 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
460
461 // Check if we've already instantiated a vector of this type.
462 llvm::FoldingSetNodeID ID;
463 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
464 void *InsertPos = 0;
465 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
466 return QualType(VTP, 0);
467
468 // If the element type isn't canonical, this won't be a canonical type either,
469 // so fill in the canonical type field.
470 QualType Canonical;
471 if (!vecType->isCanonical()) {
472 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
473
474 // Get the new insert position for the node we care about.
475 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
476 assert(NewIP == 0 && "Shouldn't be in the map!");
477 }
478 VectorType *New = new VectorType(vecType, NumElts, Canonical);
479 VectorTypes.InsertNode(New, InsertPos);
480 Types.push_back(New);
481 return QualType(New, 0);
482}
483
484/// getOCUVectorType - Return the unique reference to an OCU vector type of
485/// the specified element type and size. VectorType must be a built-in type.
486QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
487 BuiltinType *baseType;
488
489 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
490 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
491
492 // Check if we've already instantiated a vector of this type.
493 llvm::FoldingSetNodeID ID;
494 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
495 void *InsertPos = 0;
496 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
497 return QualType(VTP, 0);
498
499 // If the element type isn't canonical, this won't be a canonical type either,
500 // so fill in the canonical type field.
501 QualType Canonical;
502 if (!vecType->isCanonical()) {
503 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
504
505 // Get the new insert position for the node we care about.
506 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
507 assert(NewIP == 0 && "Shouldn't be in the map!");
508 }
509 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
510 VectorTypes.InsertNode(New, InsertPos);
511 Types.push_back(New);
512 return QualType(New, 0);
513}
514
515/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
516///
517QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
518 // Unique functions, to guarantee there is only one function of a particular
519 // structure.
520 llvm::FoldingSetNodeID ID;
521 FunctionTypeNoProto::Profile(ID, ResultTy);
522
523 void *InsertPos = 0;
524 if (FunctionTypeNoProto *FT =
525 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
526 return QualType(FT, 0);
527
528 QualType Canonical;
529 if (!ResultTy->isCanonical()) {
530 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
531
532 // Get the new insert position for the node we care about.
533 FunctionTypeNoProto *NewIP =
534 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
535 assert(NewIP == 0 && "Shouldn't be in the map!");
536 }
537
538 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
539 Types.push_back(New);
540 FunctionTypeProtos.InsertNode(New, InsertPos);
541 return QualType(New, 0);
542}
543
544/// getFunctionType - Return a normal function type with a typed argument
545/// list. isVariadic indicates whether the argument list includes '...'.
546QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
547 unsigned NumArgs, bool isVariadic) {
548 // Unique functions, to guarantee there is only one function of a particular
549 // structure.
550 llvm::FoldingSetNodeID ID;
551 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
552
553 void *InsertPos = 0;
554 if (FunctionTypeProto *FTP =
555 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
556 return QualType(FTP, 0);
557
558 // Determine whether the type being created is already canonical or not.
559 bool isCanonical = ResultTy->isCanonical();
560 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
561 if (!ArgArray[i]->isCanonical())
562 isCanonical = false;
563
564 // If this type isn't canonical, get the canonical version of it.
565 QualType Canonical;
566 if (!isCanonical) {
567 llvm::SmallVector<QualType, 16> CanonicalArgs;
568 CanonicalArgs.reserve(NumArgs);
569 for (unsigned i = 0; i != NumArgs; ++i)
570 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
571
572 Canonical = getFunctionType(ResultTy.getCanonicalType(),
573 &CanonicalArgs[0], NumArgs,
574 isVariadic);
575
576 // Get the new insert position for the node we care about.
577 FunctionTypeProto *NewIP =
578 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
579 assert(NewIP == 0 && "Shouldn't be in the map!");
580 }
581
582 // FunctionTypeProto objects are not allocated with new because they have a
583 // variable size array (for parameter types) at the end of them.
584 FunctionTypeProto *FTP =
585 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
586 NumArgs*sizeof(QualType));
587 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
588 Canonical);
589 Types.push_back(FTP);
590 FunctionTypeProtos.InsertNode(FTP, InsertPos);
591 return QualType(FTP, 0);
592}
593
594/// getTypedefType - Return the unique reference to the type for the
595/// specified typename decl.
596QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
597 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
598
599 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
600 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
601 Types.push_back(Decl->TypeForDecl);
602 return QualType(Decl->TypeForDecl, 0);
603}
604
Steve Naroff0604dd92007-08-01 18:02:17 +0000605/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
606/// TypeOfExpr AST's (since expression's are never shared). For example,
607/// multiple declarations that refer to "typeof(x)" all contain different
608/// DeclRefExpr's. This doesn't effect the type checker, since it operates
609/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000610QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroff7cbb1462007-07-31 12:34:36 +0000611 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000612 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
613 Types.push_back(toe);
614 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000615}
616
Steve Naroff0604dd92007-08-01 18:02:17 +0000617/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
618/// TypeOfType AST's. The only motivation to unique these nodes would be
619/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
620/// an issue. This doesn't effect the type checker, since it operates
621/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000622QualType ASTContext::getTypeOfType(QualType tofType) {
623 QualType Canonical = tofType.getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000624 TypeOfType *tot = new TypeOfType(tofType, Canonical);
625 Types.push_back(tot);
626 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000627}
628
Chris Lattner4b009652007-07-25 00:24:17 +0000629/// getTagDeclType - Return the unique reference to the type for the
630/// specified TagDecl (struct/union/class/enum) decl.
631QualType ASTContext::getTagDeclType(TagDecl *Decl) {
632 // The decl stores the type cache.
633 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
634
635 Decl->TypeForDecl = new TagType(Decl, QualType());
636 Types.push_back(Decl->TypeForDecl);
637 return QualType(Decl->TypeForDecl, 0);
638}
639
640/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
641/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
642/// needs to agree with the definition in <stddef.h>.
643QualType ASTContext::getSizeType() const {
644 // On Darwin, size_t is defined as a "long unsigned int".
645 // FIXME: should derive from "Target".
646 return UnsignedLongTy;
647}
648
649/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
650/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
651QualType ASTContext::getPointerDiffType() const {
652 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
653 // FIXME: should derive from "Target".
654 return IntTy;
655}
656
657/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
658/// routine will assert if passed a built-in type that isn't an integer or enum.
659static int getIntegerRank(QualType t) {
660 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
661 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
662 return 4;
663 }
664
665 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
666 switch (BT->getKind()) {
667 default:
668 assert(0 && "getIntegerRank(): not a built-in integer");
669 case BuiltinType::Bool:
670 return 1;
671 case BuiltinType::Char_S:
672 case BuiltinType::Char_U:
673 case BuiltinType::SChar:
674 case BuiltinType::UChar:
675 return 2;
676 case BuiltinType::Short:
677 case BuiltinType::UShort:
678 return 3;
679 case BuiltinType::Int:
680 case BuiltinType::UInt:
681 return 4;
682 case BuiltinType::Long:
683 case BuiltinType::ULong:
684 return 5;
685 case BuiltinType::LongLong:
686 case BuiltinType::ULongLong:
687 return 6;
688 }
689}
690
691/// getFloatingRank - Return a relative rank for floating point types.
692/// This routine will assert if passed a built-in type that isn't a float.
693static int getFloatingRank(QualType T) {
694 T = T.getCanonicalType();
695 if (ComplexType *CT = dyn_cast<ComplexType>(T))
696 return getFloatingRank(CT->getElementType());
697
698 switch (cast<BuiltinType>(T)->getKind()) {
699 default: assert(0 && "getFloatingPointRank(): not a floating type");
700 case BuiltinType::Float: return FloatRank;
701 case BuiltinType::Double: return DoubleRank;
702 case BuiltinType::LongDouble: return LongDoubleRank;
703 }
704}
705
Steve Narofffa0c4532007-08-27 01:41:48 +0000706/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
707/// point or a complex type (based on typeDomain/typeSize).
708/// 'typeDomain' is a real floating point or complex type.
709/// 'typeSize' is a real floating point or complex type.
Steve Naroff3cf497f2007-08-27 01:27:54 +0000710QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
711 QualType typeSize, QualType typeDomain) const {
712 if (typeDomain->isComplexType()) {
713 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000714 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000715 case FloatRank: return FloatComplexTy;
716 case DoubleRank: return DoubleComplexTy;
717 case LongDoubleRank: return LongDoubleComplexTy;
718 }
Chris Lattner4b009652007-07-25 00:24:17 +0000719 }
Steve Naroff3cf497f2007-08-27 01:27:54 +0000720 if (typeDomain->isRealFloatingType()) {
721 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000722 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000723 case FloatRank: return FloatTy;
724 case DoubleRank: return DoubleTy;
725 case LongDoubleRank: return LongDoubleTy;
726 }
727 }
728 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner4b009652007-07-25 00:24:17 +0000729}
730
Steve Naroff45fc9822007-08-27 15:30:22 +0000731/// compareFloatingType - Handles 3 different combos:
732/// float/float, float/complex, complex/complex.
733/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
734int ASTContext::compareFloatingType(QualType lt, QualType rt) {
735 if (getFloatingRank(lt) == getFloatingRank(rt))
736 return 0;
737 if (getFloatingRank(lt) > getFloatingRank(rt))
738 return 1;
739 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +0000740}
741
742// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
743// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
744QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
745 if (lhs == rhs) return lhs;
746
747 bool t1Unsigned = lhs->isUnsignedIntegerType();
748 bool t2Unsigned = rhs->isUnsignedIntegerType();
749
750 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
751 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
752
753 // We have two integer types with differing signs
754 QualType unsignedType = t1Unsigned ? lhs : rhs;
755 QualType signedType = t1Unsigned ? rhs : lhs;
756
757 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
758 return unsignedType;
759 else {
760 // FIXME: Need to check if the signed type can represent all values of the
761 // unsigned type. If it can, then the result is the signed type.
762 // If it can't, then the result is the unsigned version of the signed type.
763 // Should probably add a helper that returns a signed integer type from
764 // an unsigned (and vice versa). C99 6.3.1.8.
765 return signedType;
766 }
767}
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000768
769// getCFConstantStringType - Return the type used for constant CFStrings.
770QualType ASTContext::getCFConstantStringType() {
771 if (!CFConstantStringTypeDecl) {
772 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
773 &Idents.get("__builtin_CFString"),
774 0);
775
776 QualType FieldTypes[4];
777
778 // const int *isa;
779 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
780 // int flags;
781 FieldTypes[1] = IntTy;
782 // const char *str;
783 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
784 // long length;
785 FieldTypes[3] = LongTy;
786 // Create fields
787 FieldDecl *FieldDecls[4];
788
789 for (unsigned i = 0; i < 4; ++i)
790 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i], 0);
791
792 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
793 }
794
795 return getTagDeclType(CFConstantStringTypeDecl);
Chris Lattnereb56d292007-08-27 17:38:00 +0000796}