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