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