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