blob: a68ebb5be6517bb2b8892378eb933d268e9e38a3 [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!");
165 case Type::Array: {
166 std::pair<uint64_t, unsigned> EltInfo =
167 getTypeInfo(cast<ArrayType>(T)->getElementType(), L);
168
169 // Get the size of the array.
170 llvm::APSInt Sz(32);
171 if (!cast<ArrayType>(T)->getSizeExpr()->isIntegerConstantExpr(Sz, *this))
172 assert(0 && "VLAs not implemented yet!");
173
174 Size = EltInfo.first*Sz.getZExtValue();
175 Align = EltInfo.second;
176 break;
177 }
178 case Type::Vector: {
179 std::pair<uint64_t, unsigned> EltInfo =
180 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
181 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
182 // FIXME: Vector alignment is not the alignment of its elements.
183 Align = EltInfo.second;
184 break;
185 }
186
187 case Type::Builtin: {
188 // FIXME: need to use TargetInfo to derive the target specific sizes. This
189 // implementation will suffice for play with vector support.
190 switch (cast<BuiltinType>(T)->getKind()) {
191 default: assert(0 && "Unknown builtin type!");
192 case BuiltinType::Void:
193 assert(0 && "Incomplete types have no size!");
194 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
195 case BuiltinType::Char_S:
196 case BuiltinType::Char_U:
197 case BuiltinType::UChar:
198 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
199 case BuiltinType::UShort:
200 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
201 case BuiltinType::UInt:
202 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
203 case BuiltinType::ULong:
204 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
205 case BuiltinType::ULongLong:
206 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
207 case BuiltinType::Float: Target.getFloatInfo(Size, Align, L); break;
208 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, L); break;
209 case BuiltinType::LongDouble: Target.getLongDoubleInfo(Size, Align,L);break;
210 }
211 break;
212 }
213 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
214 case Type::Reference:
215 // "When applied to a reference or a reference type, the result is the size
216 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
217 // FIXME: This is wrong for struct layout!
218 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
219
220 case Type::Complex: {
221 // Complex types have the same alignment as their elements, but twice the
222 // size.
223 std::pair<uint64_t, unsigned> EltInfo =
224 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
225 Size = EltInfo.first*2;
226 Align = EltInfo.second;
227 break;
228 }
229 case Type::Tagged:
Chris Lattnereb56d292007-08-27 17:38:00 +0000230 TagType *TT = cast<TagType>(T);
231 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
232 const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L);
233 Size = Layout.getSize();
234 Align = Layout.getAlignment();
235 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattner90a018d2007-08-28 18:24:31 +0000236 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattnereb56d292007-08-27 17:38:00 +0000237 } else {
Chris Lattner4b009652007-07-25 00:24:17 +0000238 assert(0 && "Unimplemented type sizes!");
Chris Lattnereb56d292007-08-27 17:38:00 +0000239 }
Chris Lattner4b009652007-07-25 00:24:17 +0000240 break;
241 }
242
243 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
244 return std::make_pair(Size, Align);
245}
246
247/// getRecordLayout - Get or compute information about the layout of the
248/// specified record (struct/union/class), which indicates its size and field
249/// position information.
250const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D,
251 SourceLocation L) {
252 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
253
254 // Look up this layout, if already laid out, return what we have.
255 const RecordLayout *&Entry = RecordLayoutInfo[D];
256 if (Entry) return *Entry;
257
258 // Allocate and assign into RecordLayoutInfo here. The "Entry" reference can
259 // be invalidated (dangle) if the RecordLayoutInfo hashtable is inserted into.
260 RecordLayout *NewEntry = new RecordLayout();
261 Entry = NewEntry;
262
263 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
264 uint64_t RecordSize = 0;
265 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
266
267 if (D->getKind() != Decl::Union) {
268 // Layout each field, for now, just sequentially, respecting alignment. In
269 // the future, this will need to be tweakable by targets.
270 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
271 const FieldDecl *FD = D->getMember(i);
272 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
273 uint64_t FieldSize = FieldInfo.first;
274 unsigned FieldAlign = FieldInfo.second;
275
276 // Round up the current record size to the field's alignment boundary.
277 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
278
279 // Place this field at the current location.
280 FieldOffsets[i] = RecordSize;
281
282 // Reserve space for this field.
283 RecordSize += FieldSize;
284
285 // Remember max struct/class alignment.
286 RecordAlign = std::max(RecordAlign, FieldAlign);
287 }
288
289 // Finally, round the size of the total struct up to the alignment of the
290 // struct itself.
291 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
292 } else {
293 // Union layout just puts each member at the start of the record.
294 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
295 const FieldDecl *FD = D->getMember(i);
296 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
297 uint64_t FieldSize = FieldInfo.first;
298 unsigned FieldAlign = FieldInfo.second;
299
300 // Round up the current record size to the field's alignment boundary.
301 RecordSize = std::max(RecordSize, FieldSize);
302
303 // Place this field at the start of the record.
304 FieldOffsets[i] = 0;
305
306 // Remember max struct/class alignment.
307 RecordAlign = std::max(RecordAlign, FieldAlign);
308 }
309 }
310
311 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
312 return *NewEntry;
313}
314
Chris Lattner4b009652007-07-25 00:24:17 +0000315//===----------------------------------------------------------------------===//
316// Type creation/memoization methods
317//===----------------------------------------------------------------------===//
318
319
320/// getComplexType - Return the uniqued reference to the type for a complex
321/// number with the specified element type.
322QualType ASTContext::getComplexType(QualType T) {
323 // Unique pointers, to guarantee there is only one pointer of a particular
324 // structure.
325 llvm::FoldingSetNodeID ID;
326 ComplexType::Profile(ID, T);
327
328 void *InsertPos = 0;
329 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
330 return QualType(CT, 0);
331
332 // If the pointee type isn't canonical, this won't be a canonical type either,
333 // so fill in the canonical type field.
334 QualType Canonical;
335 if (!T->isCanonical()) {
336 Canonical = getComplexType(T.getCanonicalType());
337
338 // Get the new insert position for the node we care about.
339 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
340 assert(NewIP == 0 && "Shouldn't be in the map!");
341 }
342 ComplexType *New = new ComplexType(T, Canonical);
343 Types.push_back(New);
344 ComplexTypes.InsertNode(New, InsertPos);
345 return QualType(New, 0);
346}
347
348
349/// getPointerType - Return the uniqued reference to the type for a pointer to
350/// the specified type.
351QualType ASTContext::getPointerType(QualType T) {
352 // Unique pointers, to guarantee there is only one pointer of a particular
353 // structure.
354 llvm::FoldingSetNodeID ID;
355 PointerType::Profile(ID, T);
356
357 void *InsertPos = 0;
358 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
359 return QualType(PT, 0);
360
361 // If the pointee type isn't canonical, this won't be a canonical type either,
362 // so fill in the canonical type field.
363 QualType Canonical;
364 if (!T->isCanonical()) {
365 Canonical = getPointerType(T.getCanonicalType());
366
367 // Get the new insert position for the node we care about.
368 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
369 assert(NewIP == 0 && "Shouldn't be in the map!");
370 }
371 PointerType *New = new PointerType(T, Canonical);
372 Types.push_back(New);
373 PointerTypes.InsertNode(New, InsertPos);
374 return QualType(New, 0);
375}
376
377/// getReferenceType - Return the uniqued reference to the type for a reference
378/// to the specified type.
379QualType ASTContext::getReferenceType(QualType T) {
380 // Unique pointers, to guarantee there is only one pointer of a particular
381 // structure.
382 llvm::FoldingSetNodeID ID;
383 ReferenceType::Profile(ID, T);
384
385 void *InsertPos = 0;
386 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
387 return QualType(RT, 0);
388
389 // If the referencee type isn't canonical, this won't be a canonical type
390 // either, so fill in the canonical type field.
391 QualType Canonical;
392 if (!T->isCanonical()) {
393 Canonical = getReferenceType(T.getCanonicalType());
394
395 // Get the new insert position for the node we care about.
396 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
397 assert(NewIP == 0 && "Shouldn't be in the map!");
398 }
399
400 ReferenceType *New = new ReferenceType(T, Canonical);
401 Types.push_back(New);
402 ReferenceTypes.InsertNode(New, InsertPos);
403 return QualType(New, 0);
404}
405
406/// getArrayType - Return the unique reference to the type for an array of the
407/// specified element type.
408QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
409 unsigned EltTypeQuals, Expr *NumElts) {
410 // Unique array types, to guarantee there is only one array of a particular
411 // structure.
412 llvm::FoldingSetNodeID ID;
413 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
414
415 void *InsertPos = 0;
416 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
417 return QualType(ATP, 0);
418
419 // If the element type isn't canonical, this won't be a canonical type either,
420 // so fill in the canonical type field.
421 QualType Canonical;
422 if (!EltTy->isCanonical()) {
423 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
424 NumElts);
425
426 // Get the new insert position for the node we care about.
427 ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
428 assert(NewIP == 0 && "Shouldn't be in the map!");
429 }
430
431 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
432 ArrayTypes.InsertNode(New, InsertPos);
433 Types.push_back(New);
434 return QualType(New, 0);
435}
436
437/// getVectorType - Return the unique reference to a vector type of
438/// the specified element type and size. VectorType must be a built-in type.
439QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
440 BuiltinType *baseType;
441
442 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
443 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
444
445 // Check if we've already instantiated a vector of this type.
446 llvm::FoldingSetNodeID ID;
447 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
448 void *InsertPos = 0;
449 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
450 return QualType(VTP, 0);
451
452 // If the element type isn't canonical, this won't be a canonical type either,
453 // so fill in the canonical type field.
454 QualType Canonical;
455 if (!vecType->isCanonical()) {
456 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
457
458 // Get the new insert position for the node we care about.
459 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
460 assert(NewIP == 0 && "Shouldn't be in the map!");
461 }
462 VectorType *New = new VectorType(vecType, NumElts, Canonical);
463 VectorTypes.InsertNode(New, InsertPos);
464 Types.push_back(New);
465 return QualType(New, 0);
466}
467
468/// getOCUVectorType - Return the unique reference to an OCU vector type of
469/// the specified element type and size. VectorType must be a built-in type.
470QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
471 BuiltinType *baseType;
472
473 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
474 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
475
476 // Check if we've already instantiated a vector of this type.
477 llvm::FoldingSetNodeID ID;
478 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
479 void *InsertPos = 0;
480 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
481 return QualType(VTP, 0);
482
483 // If the element type isn't canonical, this won't be a canonical type either,
484 // so fill in the canonical type field.
485 QualType Canonical;
486 if (!vecType->isCanonical()) {
487 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
488
489 // Get the new insert position for the node we care about.
490 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
491 assert(NewIP == 0 && "Shouldn't be in the map!");
492 }
493 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
494 VectorTypes.InsertNode(New, InsertPos);
495 Types.push_back(New);
496 return QualType(New, 0);
497}
498
499/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
500///
501QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
502 // Unique functions, to guarantee there is only one function of a particular
503 // structure.
504 llvm::FoldingSetNodeID ID;
505 FunctionTypeNoProto::Profile(ID, ResultTy);
506
507 void *InsertPos = 0;
508 if (FunctionTypeNoProto *FT =
509 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
510 return QualType(FT, 0);
511
512 QualType Canonical;
513 if (!ResultTy->isCanonical()) {
514 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
515
516 // Get the new insert position for the node we care about.
517 FunctionTypeNoProto *NewIP =
518 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
519 assert(NewIP == 0 && "Shouldn't be in the map!");
520 }
521
522 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
523 Types.push_back(New);
524 FunctionTypeProtos.InsertNode(New, InsertPos);
525 return QualType(New, 0);
526}
527
528/// getFunctionType - Return a normal function type with a typed argument
529/// list. isVariadic indicates whether the argument list includes '...'.
530QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
531 unsigned NumArgs, bool isVariadic) {
532 // Unique functions, to guarantee there is only one function of a particular
533 // structure.
534 llvm::FoldingSetNodeID ID;
535 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
536
537 void *InsertPos = 0;
538 if (FunctionTypeProto *FTP =
539 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
540 return QualType(FTP, 0);
541
542 // Determine whether the type being created is already canonical or not.
543 bool isCanonical = ResultTy->isCanonical();
544 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
545 if (!ArgArray[i]->isCanonical())
546 isCanonical = false;
547
548 // If this type isn't canonical, get the canonical version of it.
549 QualType Canonical;
550 if (!isCanonical) {
551 llvm::SmallVector<QualType, 16> CanonicalArgs;
552 CanonicalArgs.reserve(NumArgs);
553 for (unsigned i = 0; i != NumArgs; ++i)
554 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
555
556 Canonical = getFunctionType(ResultTy.getCanonicalType(),
557 &CanonicalArgs[0], NumArgs,
558 isVariadic);
559
560 // Get the new insert position for the node we care about.
561 FunctionTypeProto *NewIP =
562 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
563 assert(NewIP == 0 && "Shouldn't be in the map!");
564 }
565
566 // FunctionTypeProto objects are not allocated with new because they have a
567 // variable size array (for parameter types) at the end of them.
568 FunctionTypeProto *FTP =
569 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
570 NumArgs*sizeof(QualType));
571 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
572 Canonical);
573 Types.push_back(FTP);
574 FunctionTypeProtos.InsertNode(FTP, InsertPos);
575 return QualType(FTP, 0);
576}
577
578/// getTypedefType - Return the unique reference to the type for the
579/// specified typename decl.
580QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
581 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
582
583 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
584 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
585 Types.push_back(Decl->TypeForDecl);
586 return QualType(Decl->TypeForDecl, 0);
587}
588
Steve Naroff0604dd92007-08-01 18:02:17 +0000589/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
590/// TypeOfExpr AST's (since expression's are never shared). For example,
591/// multiple declarations that refer to "typeof(x)" all contain different
592/// DeclRefExpr's. This doesn't effect the type checker, since it operates
593/// on canonical type's (which are always unique).
Steve Naroff11b649c2007-08-01 17:20:42 +0000594QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroff7cbb1462007-07-31 12:34:36 +0000595 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000596 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
597 Types.push_back(toe);
598 return QualType(toe, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000599}
600
Steve Naroff0604dd92007-08-01 18:02:17 +0000601/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
602/// TypeOfType AST's. The only motivation to unique these nodes would be
603/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
604/// an issue. This doesn't effect the type checker, since it operates
605/// on canonical type's (which are always unique).
Steve Naroff7cbb1462007-07-31 12:34:36 +0000606QualType ASTContext::getTypeOfType(QualType tofType) {
607 QualType Canonical = tofType.getCanonicalType();
Steve Naroff0604dd92007-08-01 18:02:17 +0000608 TypeOfType *tot = new TypeOfType(tofType, Canonical);
609 Types.push_back(tot);
610 return QualType(tot, 0);
Steve Naroff7cbb1462007-07-31 12:34:36 +0000611}
612
Chris Lattner4b009652007-07-25 00:24:17 +0000613/// getTagDeclType - Return the unique reference to the type for the
614/// specified TagDecl (struct/union/class/enum) decl.
615QualType ASTContext::getTagDeclType(TagDecl *Decl) {
616 // The decl stores the type cache.
617 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
618
619 Decl->TypeForDecl = new TagType(Decl, QualType());
620 Types.push_back(Decl->TypeForDecl);
621 return QualType(Decl->TypeForDecl, 0);
622}
623
624/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
625/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
626/// needs to agree with the definition in <stddef.h>.
627QualType ASTContext::getSizeType() const {
628 // On Darwin, size_t is defined as a "long unsigned int".
629 // FIXME: should derive from "Target".
630 return UnsignedLongTy;
631}
632
633/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
634/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
635QualType ASTContext::getPointerDiffType() const {
636 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
637 // FIXME: should derive from "Target".
638 return IntTy;
639}
640
641/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
642/// routine will assert if passed a built-in type that isn't an integer or enum.
643static int getIntegerRank(QualType t) {
644 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
645 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
646 return 4;
647 }
648
649 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
650 switch (BT->getKind()) {
651 default:
652 assert(0 && "getIntegerRank(): not a built-in integer");
653 case BuiltinType::Bool:
654 return 1;
655 case BuiltinType::Char_S:
656 case BuiltinType::Char_U:
657 case BuiltinType::SChar:
658 case BuiltinType::UChar:
659 return 2;
660 case BuiltinType::Short:
661 case BuiltinType::UShort:
662 return 3;
663 case BuiltinType::Int:
664 case BuiltinType::UInt:
665 return 4;
666 case BuiltinType::Long:
667 case BuiltinType::ULong:
668 return 5;
669 case BuiltinType::LongLong:
670 case BuiltinType::ULongLong:
671 return 6;
672 }
673}
674
675/// getFloatingRank - Return a relative rank for floating point types.
676/// This routine will assert if passed a built-in type that isn't a float.
677static int getFloatingRank(QualType T) {
678 T = T.getCanonicalType();
679 if (ComplexType *CT = dyn_cast<ComplexType>(T))
680 return getFloatingRank(CT->getElementType());
681
682 switch (cast<BuiltinType>(T)->getKind()) {
683 default: assert(0 && "getFloatingPointRank(): not a floating type");
684 case BuiltinType::Float: return FloatRank;
685 case BuiltinType::Double: return DoubleRank;
686 case BuiltinType::LongDouble: return LongDoubleRank;
687 }
688}
689
Steve Narofffa0c4532007-08-27 01:41:48 +0000690/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
691/// point or a complex type (based on typeDomain/typeSize).
692/// 'typeDomain' is a real floating point or complex type.
693/// 'typeSize' is a real floating point or complex type.
Steve Naroff3cf497f2007-08-27 01:27:54 +0000694QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
695 QualType typeSize, QualType typeDomain) const {
696 if (typeDomain->isComplexType()) {
697 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000698 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000699 case FloatRank: return FloatComplexTy;
700 case DoubleRank: return DoubleComplexTy;
701 case LongDoubleRank: return LongDoubleComplexTy;
702 }
Chris Lattner4b009652007-07-25 00:24:17 +0000703 }
Steve Naroff3cf497f2007-08-27 01:27:54 +0000704 if (typeDomain->isRealFloatingType()) {
705 switch (getFloatingRank(typeSize)) {
Steve Narofffa0c4532007-08-27 01:41:48 +0000706 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff3cf497f2007-08-27 01:27:54 +0000707 case FloatRank: return FloatTy;
708 case DoubleRank: return DoubleTy;
709 case LongDoubleRank: return LongDoubleTy;
710 }
711 }
712 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner4b009652007-07-25 00:24:17 +0000713}
714
Steve Naroff45fc9822007-08-27 15:30:22 +0000715/// compareFloatingType - Handles 3 different combos:
716/// float/float, float/complex, complex/complex.
717/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
718int ASTContext::compareFloatingType(QualType lt, QualType rt) {
719 if (getFloatingRank(lt) == getFloatingRank(rt))
720 return 0;
721 if (getFloatingRank(lt) > getFloatingRank(rt))
722 return 1;
723 return -1;
Chris Lattner4b009652007-07-25 00:24:17 +0000724}
725
726// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
727// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
728QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
729 if (lhs == rhs) return lhs;
730
731 bool t1Unsigned = lhs->isUnsignedIntegerType();
732 bool t2Unsigned = rhs->isUnsignedIntegerType();
733
734 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
735 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
736
737 // We have two integer types with differing signs
738 QualType unsignedType = t1Unsigned ? lhs : rhs;
739 QualType signedType = t1Unsigned ? rhs : lhs;
740
741 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
742 return unsignedType;
743 else {
744 // FIXME: Need to check if the signed type can represent all values of the
745 // unsigned type. If it can, then the result is the signed type.
746 // If it can't, then the result is the unsigned version of the signed type.
747 // Should probably add a helper that returns a signed integer type from
748 // an unsigned (and vice versa). C99 6.3.1.8.
749 return signedType;
750 }
751}
Anders Carlssone7e7aa22007-08-17 05:31:46 +0000752
753// getCFConstantStringType - Return the type used for constant CFStrings.
754QualType ASTContext::getCFConstantStringType() {
755 if (!CFConstantStringTypeDecl) {
756 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
757 &Idents.get("__builtin_CFString"),
758 0);
759
760 QualType FieldTypes[4];
761
762 // const int *isa;
763 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
764 // int flags;
765 FieldTypes[1] = IntTy;
766 // const char *str;
767 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
768 // long length;
769 FieldTypes[3] = LongTy;
770 // Create fields
771 FieldDecl *FieldDecls[4];
772
773 for (unsigned i = 0; i < 4; ++i)
774 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i], 0);
775
776 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
777 }
778
779 return getTagDeclType(CFConstantStringTypeDecl);
Chris Lattnereb56d292007-08-27 17:38:00 +0000780}