blob: a9fb81faad817109afc7f320eb7b82f449133da3 [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:
230 RecordType *RT = dyn_cast<RecordType>(cast<TagType>(T));
231 if (!RT)
232 // FIXME: Handle enums.
233 assert(0 && "Unimplemented type sizes!");
234 const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L);
235 Size = Layout.getSize();
236 Align = Layout.getAlignment();
237 break;
238 }
239
240 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
241 return std::make_pair(Size, Align);
242}
243
244/// getRecordLayout - Get or compute information about the layout of the
245/// specified record (struct/union/class), which indicates its size and field
246/// position information.
247const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D,
248 SourceLocation L) {
249 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
250
251 // Look up this layout, if already laid out, return what we have.
252 const RecordLayout *&Entry = RecordLayoutInfo[D];
253 if (Entry) return *Entry;
254
255 // Allocate and assign into RecordLayoutInfo here. The "Entry" reference can
256 // be invalidated (dangle) if the RecordLayoutInfo hashtable is inserted into.
257 RecordLayout *NewEntry = new RecordLayout();
258 Entry = NewEntry;
259
260 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
261 uint64_t RecordSize = 0;
262 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
263
264 if (D->getKind() != Decl::Union) {
265 // Layout each field, for now, just sequentially, respecting alignment. In
266 // the future, this will need to be tweakable by targets.
267 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
268 const FieldDecl *FD = D->getMember(i);
269 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
270 uint64_t FieldSize = FieldInfo.first;
271 unsigned FieldAlign = FieldInfo.second;
272
273 // Round up the current record size to the field's alignment boundary.
274 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
275
276 // Place this field at the current location.
277 FieldOffsets[i] = RecordSize;
278
279 // Reserve space for this field.
280 RecordSize += FieldSize;
281
282 // Remember max struct/class alignment.
283 RecordAlign = std::max(RecordAlign, FieldAlign);
284 }
285
286 // Finally, round the size of the total struct up to the alignment of the
287 // struct itself.
288 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
289 } else {
290 // Union layout just puts each member at the start of the record.
291 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
292 const FieldDecl *FD = D->getMember(i);
293 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
294 uint64_t FieldSize = FieldInfo.first;
295 unsigned FieldAlign = FieldInfo.second;
296
297 // Round up the current record size to the field's alignment boundary.
298 RecordSize = std::max(RecordSize, FieldSize);
299
300 // Place this field at the start of the record.
301 FieldOffsets[i] = 0;
302
303 // Remember max struct/class alignment.
304 RecordAlign = std::max(RecordAlign, FieldAlign);
305 }
306 }
307
308 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
309 return *NewEntry;
310}
311
312
313//===----------------------------------------------------------------------===//
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
404/// getArrayType - Return the unique reference to the type for an array of the
405/// specified element type.
406QualType ASTContext::getArrayType(QualType EltTy,ArrayType::ArraySizeModifier ASM,
407 unsigned EltTypeQuals, Expr *NumElts) {
408 // Unique array types, to guarantee there is only one array of a particular
409 // structure.
410 llvm::FoldingSetNodeID ID;
411 ArrayType::Profile(ID, ASM, EltTypeQuals, EltTy, NumElts);
412
413 void *InsertPos = 0;
414 if (ArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
415 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()) {
421 Canonical = getArrayType(EltTy.getCanonicalType(), ASM, EltTypeQuals,
422 NumElts);
423
424 // Get the new insert position for the node we care about.
425 ArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
426 assert(NewIP == 0 && "Shouldn't be in the map!");
427 }
428
429 ArrayType *New = new ArrayType(EltTy, ASM, EltTypeQuals, Canonical, NumElts);
430 ArrayTypes.InsertNode(New, InsertPos);
431 Types.push_back(New);
432 return QualType(New, 0);
433}
434
435/// getVectorType - Return the unique reference to a vector type of
436/// the specified element type and size. VectorType must be a built-in type.
437QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
438 BuiltinType *baseType;
439
440 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
441 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
442
443 // Check if we've already instantiated a vector of this type.
444 llvm::FoldingSetNodeID ID;
445 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
446 void *InsertPos = 0;
447 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
448 return QualType(VTP, 0);
449
450 // If the element type isn't canonical, this won't be a canonical type either,
451 // so fill in the canonical type field.
452 QualType Canonical;
453 if (!vecType->isCanonical()) {
454 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
455
456 // Get the new insert position for the node we care about.
457 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
458 assert(NewIP == 0 && "Shouldn't be in the map!");
459 }
460 VectorType *New = new VectorType(vecType, NumElts, Canonical);
461 VectorTypes.InsertNode(New, InsertPos);
462 Types.push_back(New);
463 return QualType(New, 0);
464}
465
466/// getOCUVectorType - Return the unique reference to an OCU vector type of
467/// the specified element type and size. VectorType must be a built-in type.
468QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
469 BuiltinType *baseType;
470
471 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
472 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
473
474 // Check if we've already instantiated a vector of this type.
475 llvm::FoldingSetNodeID ID;
476 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
477 void *InsertPos = 0;
478 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
479 return QualType(VTP, 0);
480
481 // If the element type isn't canonical, this won't be a canonical type either,
482 // so fill in the canonical type field.
483 QualType Canonical;
484 if (!vecType->isCanonical()) {
485 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
486
487 // Get the new insert position for the node we care about.
488 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
489 assert(NewIP == 0 && "Shouldn't be in the map!");
490 }
491 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
492 VectorTypes.InsertNode(New, InsertPos);
493 Types.push_back(New);
494 return QualType(New, 0);
495}
496
497/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
498///
499QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
500 // Unique functions, to guarantee there is only one function of a particular
501 // structure.
502 llvm::FoldingSetNodeID ID;
503 FunctionTypeNoProto::Profile(ID, ResultTy);
504
505 void *InsertPos = 0;
506 if (FunctionTypeNoProto *FT =
507 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
508 return QualType(FT, 0);
509
510 QualType Canonical;
511 if (!ResultTy->isCanonical()) {
512 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
513
514 // Get the new insert position for the node we care about.
515 FunctionTypeNoProto *NewIP =
516 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
517 assert(NewIP == 0 && "Shouldn't be in the map!");
518 }
519
520 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
521 Types.push_back(New);
522 FunctionTypeProtos.InsertNode(New, InsertPos);
523 return QualType(New, 0);
524}
525
526/// getFunctionType - Return a normal function type with a typed argument
527/// list. isVariadic indicates whether the argument list includes '...'.
528QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
529 unsigned NumArgs, bool isVariadic) {
530 // Unique functions, to guarantee there is only one function of a particular
531 // structure.
532 llvm::FoldingSetNodeID ID;
533 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
534
535 void *InsertPos = 0;
536 if (FunctionTypeProto *FTP =
537 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
538 return QualType(FTP, 0);
539
540 // Determine whether the type being created is already canonical or not.
541 bool isCanonical = ResultTy->isCanonical();
542 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
543 if (!ArgArray[i]->isCanonical())
544 isCanonical = false;
545
546 // If this type isn't canonical, get the canonical version of it.
547 QualType Canonical;
548 if (!isCanonical) {
549 llvm::SmallVector<QualType, 16> CanonicalArgs;
550 CanonicalArgs.reserve(NumArgs);
551 for (unsigned i = 0; i != NumArgs; ++i)
552 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
553
554 Canonical = getFunctionType(ResultTy.getCanonicalType(),
555 &CanonicalArgs[0], NumArgs,
556 isVariadic);
557
558 // Get the new insert position for the node we care about.
559 FunctionTypeProto *NewIP =
560 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
561 assert(NewIP == 0 && "Shouldn't be in the map!");
562 }
563
564 // FunctionTypeProto objects are not allocated with new because they have a
565 // variable size array (for parameter types) at the end of them.
566 FunctionTypeProto *FTP =
567 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
568 NumArgs*sizeof(QualType));
569 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
570 Canonical);
571 Types.push_back(FTP);
572 FunctionTypeProtos.InsertNode(FTP, InsertPos);
573 return QualType(FTP, 0);
574}
575
576/// getTypedefType - Return the unique reference to the type for the
577/// specified typename decl.
578QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
579 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
580
581 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
582 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
583 Types.push_back(Decl->TypeForDecl);
584 return QualType(Decl->TypeForDecl, 0);
585}
586
587/// getTagDeclType - Return the unique reference to the type for the
588/// specified TagDecl (struct/union/class/enum) decl.
589QualType ASTContext::getTagDeclType(TagDecl *Decl) {
590 // The decl stores the type cache.
591 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
592
593 Decl->TypeForDecl = new TagType(Decl, QualType());
594 Types.push_back(Decl->TypeForDecl);
595 return QualType(Decl->TypeForDecl, 0);
596}
597
598/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
599/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
600/// needs to agree with the definition in <stddef.h>.
601QualType ASTContext::getSizeType() const {
602 // On Darwin, size_t is defined as a "long unsigned int".
603 // FIXME: should derive from "Target".
604 return UnsignedLongTy;
605}
606
607/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
608/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
609QualType ASTContext::getPointerDiffType() const {
610 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
611 // FIXME: should derive from "Target".
612 return IntTy;
613}
614
615/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
616/// routine will assert if passed a built-in type that isn't an integer or enum.
617static int getIntegerRank(QualType t) {
618 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
619 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
620 return 4;
621 }
622
623 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
624 switch (BT->getKind()) {
625 default:
626 assert(0 && "getIntegerRank(): not a built-in integer");
627 case BuiltinType::Bool:
628 return 1;
629 case BuiltinType::Char_S:
630 case BuiltinType::Char_U:
631 case BuiltinType::SChar:
632 case BuiltinType::UChar:
633 return 2;
634 case BuiltinType::Short:
635 case BuiltinType::UShort:
636 return 3;
637 case BuiltinType::Int:
638 case BuiltinType::UInt:
639 return 4;
640 case BuiltinType::Long:
641 case BuiltinType::ULong:
642 return 5;
643 case BuiltinType::LongLong:
644 case BuiltinType::ULongLong:
645 return 6;
646 }
647}
648
649/// getFloatingRank - Return a relative rank for floating point types.
650/// This routine will assert if passed a built-in type that isn't a float.
651static int getFloatingRank(QualType T) {
652 T = T.getCanonicalType();
653 if (ComplexType *CT = dyn_cast<ComplexType>(T))
654 return getFloatingRank(CT->getElementType());
655
656 switch (cast<BuiltinType>(T)->getKind()) {
657 default: assert(0 && "getFloatingPointRank(): not a floating type");
658 case BuiltinType::Float: return FloatRank;
659 case BuiltinType::Double: return DoubleRank;
660 case BuiltinType::LongDouble: return LongDoubleRank;
661 }
662}
663
664// maxComplexType - the following code handles 3 different combinations:
665// complex/complex, complex/float, float/complex.
666// When both operands are complex, the shorter operand is converted to the
667// type of the longer, and that is the type of the result. This corresponds
668// to what is done when combining two real floating-point operands.
669// The fun begins when size promotion occur across type domains. g
670// getFloatingRank & convertFloatingRankToComplexType handle this without
671// enumerating all permutations.
672// It also allows us to add new types without breakage.
673// From H&S 6.3.4: When one operand is complex and the other is a real
674// floating-point type, the less precise type is converted, within it's
675// real or complex domain, to the precision of the other type. For example,
676// when combining a "long double" with a "double _Complex", the
677// "double _Complex" is promoted to "long double _Complex".
678
679QualType ASTContext::maxComplexType(QualType lt, QualType rt) const {
680 switch (std::max(getFloatingRank(lt), getFloatingRank(rt))) {
681 default: assert(0 && "convertRankToComplex(): illegal value for rank");
682 case FloatRank: return FloatComplexTy;
683 case DoubleRank: return DoubleComplexTy;
684 case LongDoubleRank: return LongDoubleComplexTy;
685 }
686}
687
688// maxFloatingType - handles the simple case, both operands are floats.
689QualType ASTContext::maxFloatingType(QualType lt, QualType rt) {
690 return getFloatingRank(lt) > getFloatingRank(rt) ? lt : rt;
691}
692
693// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
694// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
695QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
696 if (lhs == rhs) return lhs;
697
698 bool t1Unsigned = lhs->isUnsignedIntegerType();
699 bool t2Unsigned = rhs->isUnsignedIntegerType();
700
701 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
702 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
703
704 // We have two integer types with differing signs
705 QualType unsignedType = t1Unsigned ? lhs : rhs;
706 QualType signedType = t1Unsigned ? rhs : lhs;
707
708 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
709 return unsignedType;
710 else {
711 // FIXME: Need to check if the signed type can represent all values of the
712 // unsigned type. If it can, then the result is the signed type.
713 // If it can't, then the result is the unsigned version of the signed type.
714 // Should probably add a helper that returns a signed integer type from
715 // an unsigned (and vice versa). C99 6.3.1.8.
716 return signedType;
717 }
718}