blob: 8879336b43708d97b4d567bf7811bd86a6e1e0ac [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ASTContext.cpp - Context to hold long-lived AST nodes ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ASTContext interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
16#include "clang/Lex/Preprocessor.h"
17#include "clang/Basic/TargetInfo.h"
18#include "llvm/ADT/SmallVector.h"
19using namespace clang;
20
21enum FloatingRank {
22 FloatRank, DoubleRank, LongDoubleRank
23};
24
25ASTContext::~ASTContext() {
26 // Deallocate all the types.
27 while (!Types.empty()) {
28 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
29 // Destroy the object, but don't call delete. These are malloc'd.
30 FT->~FunctionTypeProto();
31 free(FT);
32 } else {
33 delete Types.back();
34 }
35 Types.pop_back();
36 }
37}
38
39void ASTContext::PrintStats() const {
40 fprintf(stderr, "*** AST Context Stats:\n");
41 fprintf(stderr, " %d types total.\n", (int)Types.size());
42 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Chris Lattner6d87fc62007-07-18 05:50:59 +000043 unsigned NumVector = 0, NumComplex = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000044 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
45
46 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
47
48 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
49 Type *T = Types[i];
50 if (isa<BuiltinType>(T))
51 ++NumBuiltin;
52 else if (isa<PointerType>(T))
53 ++NumPointer;
54 else if (isa<ReferenceType>(T))
55 ++NumReference;
Chris Lattner6d87fc62007-07-18 05:50:59 +000056 else if (isa<ComplexType>(T))
57 ++NumComplex;
Reid Spencer5f016e22007-07-11 17:01:13 +000058 else if (isa<ArrayType>(T))
59 ++NumArray;
Chris Lattner6d87fc62007-07-18 05:50:59 +000060 else if (isa<VectorType>(T))
61 ++NumVector;
Reid Spencer5f016e22007-07-11 17:01:13 +000062 else if (isa<FunctionTypeNoProto>(T))
63 ++NumFunctionNP;
64 else if (isa<FunctionTypeProto>(T))
65 ++NumFunctionP;
66 else if (isa<TypedefType>(T))
67 ++NumTypeName;
68 else if (TagType *TT = dyn_cast<TagType>(T)) {
69 ++NumTagged;
70 switch (TT->getDecl()->getKind()) {
71 default: assert(0 && "Unknown tagged type!");
72 case Decl::Struct: ++NumTagStruct; break;
73 case Decl::Union: ++NumTagUnion; break;
74 case Decl::Class: ++NumTagClass; break;
75 case Decl::Enum: ++NumTagEnum; break;
76 }
77 } else {
78 assert(0 && "Unknown type!");
79 }
80 }
81
82 fprintf(stderr, " %d builtin types\n", NumBuiltin);
83 fprintf(stderr, " %d pointer types\n", NumPointer);
84 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner6d87fc62007-07-18 05:50:59 +000085 fprintf(stderr, " %d complex types\n", NumComplex);
Reid Spencer5f016e22007-07-11 17:01:13 +000086 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner6d87fc62007-07-18 05:50:59 +000087 fprintf(stderr, " %d vector types\n", NumVector);
Reid Spencer5f016e22007-07-11 17:01:13 +000088 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
89 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
90 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
91 fprintf(stderr, " %d tagged types\n", NumTagged);
92 fprintf(stderr, " %d struct types\n", NumTagStruct);
93 fprintf(stderr, " %d union types\n", NumTagUnion);
94 fprintf(stderr, " %d class types\n", NumTagClass);
95 fprintf(stderr, " %d enum types\n", NumTagEnum);
96 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
97 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner6d87fc62007-07-18 05:50:59 +000098 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Reid Spencer5f016e22007-07-11 17:01:13 +000099 NumFunctionP*sizeof(FunctionTypeProto)+
100 NumFunctionNP*sizeof(FunctionTypeNoProto)+
101 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
102}
103
104
105void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
106 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
107}
108
109
110void ASTContext::InitBuiltinTypes() {
111 assert(VoidTy.isNull() && "Context reinitialized?");
112
113 // C99 6.2.5p19.
114 InitBuiltinType(VoidTy, BuiltinType::Void);
115
116 // C99 6.2.5p2.
117 InitBuiltinType(BoolTy, BuiltinType::Bool);
118 // C99 6.2.5p3.
119 if (Target.isCharSigned(SourceLocation()))
120 InitBuiltinType(CharTy, BuiltinType::Char_S);
121 else
122 InitBuiltinType(CharTy, BuiltinType::Char_U);
123 // C99 6.2.5p4.
124 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
125 InitBuiltinType(ShortTy, BuiltinType::Short);
126 InitBuiltinType(IntTy, BuiltinType::Int);
127 InitBuiltinType(LongTy, BuiltinType::Long);
128 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
129
130 // C99 6.2.5p6.
131 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
132 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
133 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
134 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
135 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
136
137 // C99 6.2.5p10.
138 InitBuiltinType(FloatTy, BuiltinType::Float);
139 InitBuiltinType(DoubleTy, BuiltinType::Double);
140 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
141
142 // C99 6.2.5p11.
143 FloatComplexTy = getComplexType(FloatTy);
144 DoubleComplexTy = getComplexType(DoubleTy);
145 LongDoubleComplexTy = getComplexType(LongDoubleTy);
146}
147
Chris Lattner464175b2007-07-18 17:52:12 +0000148//===----------------------------------------------------------------------===//
149// Type Sizing and Analysis
150//===----------------------------------------------------------------------===//
Chris Lattnera7674d82007-07-13 22:13:22 +0000151
152/// getTypeSize - Return the size of the specified type, in bits. This method
153/// does not work on incomplete types.
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000154std::pair<uint64_t, unsigned>
155ASTContext::getTypeInfo(QualType T, SourceLocation L) {
Chris Lattnera7674d82007-07-13 22:13:22 +0000156 T = T.getCanonicalType();
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000157 uint64_t Size;
158 unsigned Align;
Chris Lattnera7674d82007-07-13 22:13:22 +0000159 switch (T->getTypeClass()) {
Chris Lattner030d8842007-07-19 22:06:24 +0000160 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner692233e2007-07-13 22:27:08 +0000161 case Type::FunctionNoProto:
162 case Type::FunctionProto:
Chris Lattner5d2a6302007-07-18 18:26:58 +0000163 default:
Chris Lattnerb1c2df92007-07-20 18:13:33 +0000164 assert(0 && "Incomplete types have no size!");
Steve Narofffb22d962007-08-30 01:06:46 +0000165 case Type::VariableArray:
166 assert(0 && "VLAs not implemented yet!");
167 case Type::ConstantArray: {
168 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
169
Chris Lattner030d8842007-07-19 22:06:24 +0000170 std::pair<uint64_t, unsigned> EltInfo =
Steve Narofffb22d962007-08-30 01:06:46 +0000171 getTypeInfo(CAT->getElementType(), L);
172 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattner030d8842007-07-19 22:06:24 +0000173 Align = EltInfo.second;
174 break;
175 }
176 case Type::Vector: {
177 std::pair<uint64_t, unsigned> EltInfo =
178 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
179 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
180 // FIXME: Vector alignment is not the alignment of its elements.
181 Align = EltInfo.second;
182 break;
183 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000184
Chris Lattnera7674d82007-07-13 22:13:22 +0000185 case Type::Builtin: {
186 // FIXME: need to use TargetInfo to derive the target specific sizes. This
187 // implementation will suffice for play with vector support.
188 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner692233e2007-07-13 22:27:08 +0000189 default: assert(0 && "Unknown builtin type!");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000190 case BuiltinType::Void:
191 assert(0 && "Incomplete types have no size!");
192 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000193 case BuiltinType::Char_S:
194 case BuiltinType::Char_U:
195 case BuiltinType::UChar:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000196 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000197 case BuiltinType::UShort:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000198 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000199 case BuiltinType::UInt:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000200 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000201 case BuiltinType::ULong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000202 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
Chris Lattner692233e2007-07-13 22:27:08 +0000203 case BuiltinType::ULongLong:
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000204 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
205 case BuiltinType::Float: Target.getFloatInfo(Size, Align, L); break;
206 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, L); break;
207 case BuiltinType::LongDouble: Target.getLongDoubleInfo(Size, Align,L);break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000208 }
Chris Lattnerbfef6d72007-07-15 23:46:53 +0000209 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000210 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000211 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000212 case Type::Reference:
Chris Lattner7ab2ed82007-07-13 22:16:13 +0000213 // "When applied to a reference or a reference type, the result is the size
Chris Lattner5d2a6302007-07-18 18:26:58 +0000214 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
215 // FIXME: This is wrong for struct layout!
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000216 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner5d2a6302007-07-18 18:26:58 +0000217
218 case Type::Complex: {
219 // Complex types have the same alignment as their elements, but twice the
220 // size.
221 std::pair<uint64_t, unsigned> EltInfo =
222 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
223 Size = EltInfo.first*2;
224 Align = EltInfo.second;
225 break;
226 }
227 case Type::Tagged:
Chris Lattner6cd862c2007-08-27 17:38:00 +0000228 TagType *TT = cast<TagType>(T);
229 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
230 const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L);
231 Size = Layout.getSize();
232 Align = Layout.getAlignment();
233 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattnere00b18c2007-08-28 18:24:31 +0000234 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000235 } else {
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000236 assert(0 && "Unimplemented type sizes!");
Chris Lattner6cd862c2007-08-27 17:38:00 +0000237 }
Chris Lattnerdc0d73e2007-07-23 22:46:22 +0000238 break;
Chris Lattnera7674d82007-07-13 22:13:22 +0000239 }
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000240
Chris Lattner464175b2007-07-18 17:52:12 +0000241 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattnerd2d2a112007-07-14 01:29:45 +0000242 return std::make_pair(Size, Align);
Chris Lattnera7674d82007-07-13 22:13:22 +0000243}
244
Chris Lattner464175b2007-07-18 17:52:12 +0000245/// getRecordLayout - Get or compute information about the layout of the
246/// specified record (struct/union/class), which indicates its size and field
247/// position information.
248const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D,
249 SourceLocation L) {
250 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
251
252 // Look up this layout, if already laid out, return what we have.
253 const RecordLayout *&Entry = RecordLayoutInfo[D];
254 if (Entry) return *Entry;
255
256 // Allocate and assign into RecordLayoutInfo here. The "Entry" reference can
257 // be invalidated (dangle) if the RecordLayoutInfo hashtable is inserted into.
258 RecordLayout *NewEntry = new RecordLayout();
259 Entry = NewEntry;
260
261 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
262 uint64_t RecordSize = 0;
263 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
264
265 if (D->getKind() != Decl::Union) {
266 // Layout each field, for now, just sequentially, respecting alignment. In
267 // the future, this will need to be tweakable by targets.
268 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
269 const FieldDecl *FD = D->getMember(i);
270 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
271 uint64_t FieldSize = FieldInfo.first;
272 unsigned FieldAlign = FieldInfo.second;
273
274 // Round up the current record size to the field's alignment boundary.
275 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
276
277 // Place this field at the current location.
278 FieldOffsets[i] = RecordSize;
279
280 // Reserve space for this field.
281 RecordSize += FieldSize;
282
283 // Remember max struct/class alignment.
284 RecordAlign = std::max(RecordAlign, FieldAlign);
285 }
286
287 // Finally, round the size of the total struct up to the alignment of the
288 // struct itself.
289 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
290 } else {
291 // Union layout just puts each member at the start of the record.
292 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
293 const FieldDecl *FD = D->getMember(i);
294 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
295 uint64_t FieldSize = FieldInfo.first;
296 unsigned FieldAlign = FieldInfo.second;
297
298 // Round up the current record size to the field's alignment boundary.
299 RecordSize = std::max(RecordSize, FieldSize);
300
301 // Place this field at the start of the record.
302 FieldOffsets[i] = 0;
303
304 // Remember max struct/class alignment.
305 RecordAlign = std::max(RecordAlign, FieldAlign);
306 }
307 }
Chris Lattner5d2a6302007-07-18 18:26:58 +0000308
309 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
310 return *NewEntry;
Chris Lattner464175b2007-07-18 17:52:12 +0000311}
312
Chris Lattnera7674d82007-07-13 22:13:22 +0000313//===----------------------------------------------------------------------===//
314// Type creation/memoization methods
315//===----------------------------------------------------------------------===//
316
317
Reid Spencer5f016e22007-07-11 17:01:13 +0000318/// getComplexType - Return the uniqued reference to the type for a complex
319/// number with the specified element type.
320QualType ASTContext::getComplexType(QualType T) {
321 // Unique pointers, to guarantee there is only one pointer of a particular
322 // structure.
323 llvm::FoldingSetNodeID ID;
324 ComplexType::Profile(ID, T);
325
326 void *InsertPos = 0;
327 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
328 return QualType(CT, 0);
329
330 // If the pointee type isn't canonical, this won't be a canonical type either,
331 // so fill in the canonical type field.
332 QualType Canonical;
333 if (!T->isCanonical()) {
334 Canonical = getComplexType(T.getCanonicalType());
335
336 // Get the new insert position for the node we care about.
337 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
338 assert(NewIP == 0 && "Shouldn't be in the map!");
339 }
340 ComplexType *New = new ComplexType(T, Canonical);
341 Types.push_back(New);
342 ComplexTypes.InsertNode(New, InsertPos);
343 return QualType(New, 0);
344}
345
346
347/// getPointerType - Return the uniqued reference to the type for a pointer to
348/// the specified type.
349QualType ASTContext::getPointerType(QualType T) {
350 // Unique pointers, to guarantee there is only one pointer of a particular
351 // structure.
352 llvm::FoldingSetNodeID ID;
353 PointerType::Profile(ID, T);
354
355 void *InsertPos = 0;
356 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
357 return QualType(PT, 0);
358
359 // If the pointee type isn't canonical, this won't be a canonical type either,
360 // so fill in the canonical type field.
361 QualType Canonical;
362 if (!T->isCanonical()) {
363 Canonical = getPointerType(T.getCanonicalType());
364
365 // Get the new insert position for the node we care about.
366 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
367 assert(NewIP == 0 && "Shouldn't be in the map!");
368 }
369 PointerType *New = new PointerType(T, Canonical);
370 Types.push_back(New);
371 PointerTypes.InsertNode(New, InsertPos);
372 return QualType(New, 0);
373}
374
375/// getReferenceType - Return the uniqued reference to the type for a reference
376/// to the specified type.
377QualType ASTContext::getReferenceType(QualType T) {
378 // Unique pointers, to guarantee there is only one pointer of a particular
379 // structure.
380 llvm::FoldingSetNodeID ID;
381 ReferenceType::Profile(ID, T);
382
383 void *InsertPos = 0;
384 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
385 return QualType(RT, 0);
386
387 // If the referencee type isn't canonical, this won't be a canonical type
388 // either, so fill in the canonical type field.
389 QualType Canonical;
390 if (!T->isCanonical()) {
391 Canonical = getReferenceType(T.getCanonicalType());
392
393 // Get the new insert position for the node we care about.
394 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
395 assert(NewIP == 0 && "Shouldn't be in the map!");
396 }
397
398 ReferenceType *New = new ReferenceType(T, Canonical);
399 Types.push_back(New);
400 ReferenceTypes.InsertNode(New, InsertPos);
401 return QualType(New, 0);
402}
403
Steve Narofffb22d962007-08-30 01:06:46 +0000404/// getConstantArrayType - Return the unique reference to the type for an
405/// array of the specified element type.
406QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroffc9406122007-08-30 18:10:14 +0000407 const llvm::APInt &ArySize,
408 ArrayType::ArraySizeModifier ASM,
409 unsigned EltTypeQuals) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 llvm::FoldingSetNodeID ID;
Steve Narofffb22d962007-08-30 01:06:46 +0000411 ConstantArrayType::Profile(ID, EltTy, ArySize);
Reid Spencer5f016e22007-07-11 17:01:13 +0000412
413 void *InsertPos = 0;
Steve Narofffb22d962007-08-30 01:06:46 +0000414 if (ConstantArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 return QualType(ATP, 0);
416
417 // If the element type isn't canonical, this won't be a canonical type either,
418 // so fill in the canonical type field.
419 QualType Canonical;
420 if (!EltTy->isCanonical()) {
Steve Naroffc9406122007-08-30 18:10:14 +0000421 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
422 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 // Get the new insert position for the node we care about.
Steve Narofffb22d962007-08-30 01:06:46 +0000424 ConstantArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Reid Spencer5f016e22007-07-11 17:01:13 +0000425 assert(NewIP == 0 && "Shouldn't be in the map!");
426 }
427
Steve Naroffc9406122007-08-30 18:10:14 +0000428 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
429 ASM, EltTypeQuals);
Reid Spencer5f016e22007-07-11 17:01:13 +0000430 ArrayTypes.InsertNode(New, InsertPos);
431 Types.push_back(New);
432 return QualType(New, 0);
433}
434
Steve Naroffbdbf7b02007-08-30 18:14:25 +0000435/// getVariableArrayType - Returns a non-unique reference to the type for a
436/// variable array of the specified element type.
Steve Naroffc9406122007-08-30 18:10:14 +0000437QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
438 ArrayType::ArraySizeModifier ASM,
439 unsigned EltTypeQuals) {
440 // Since we don't unique expressions, it isn't possible to unique VLA's.
441 ArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
442 ASM, EltTypeQuals);
443 Types.push_back(New);
444 return QualType(New, 0);
Steve Narofffb22d962007-08-30 01:06:46 +0000445}
446
Steve Naroff73322922007-07-18 18:00:27 +0000447/// getVectorType - Return the unique reference to a vector type of
448/// the specified element type and size. VectorType must be a built-in type.
449QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000450 BuiltinType *baseType;
451
452 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff73322922007-07-18 18:00:27 +0000453 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Reid Spencer5f016e22007-07-11 17:01:13 +0000454
455 // Check if we've already instantiated a vector of this type.
456 llvm::FoldingSetNodeID ID;
Steve Naroff73322922007-07-18 18:00:27 +0000457 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 void *InsertPos = 0;
459 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
460 return QualType(VTP, 0);
461
462 // If the element type isn't canonical, this won't be a canonical type either,
463 // so fill in the canonical type field.
464 QualType Canonical;
465 if (!vecType->isCanonical()) {
Steve Naroff73322922007-07-18 18:00:27 +0000466 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Reid Spencer5f016e22007-07-11 17:01:13 +0000467
468 // Get the new insert position for the node we care about.
469 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
470 assert(NewIP == 0 && "Shouldn't be in the map!");
471 }
472 VectorType *New = new VectorType(vecType, NumElts, Canonical);
473 VectorTypes.InsertNode(New, InsertPos);
474 Types.push_back(New);
475 return QualType(New, 0);
476}
477
Steve Naroff73322922007-07-18 18:00:27 +0000478/// getOCUVectorType - Return the unique reference to an OCU vector type of
479/// the specified element type and size. VectorType must be a built-in type.
480QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
481 BuiltinType *baseType;
482
483 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
484 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
485
486 // Check if we've already instantiated a vector of this type.
487 llvm::FoldingSetNodeID ID;
488 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
489 void *InsertPos = 0;
490 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
491 return QualType(VTP, 0);
492
493 // If the element type isn't canonical, this won't be a canonical type either,
494 // so fill in the canonical type field.
495 QualType Canonical;
496 if (!vecType->isCanonical()) {
497 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
498
499 // Get the new insert position for the node we care about.
500 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
501 assert(NewIP == 0 && "Shouldn't be in the map!");
502 }
503 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
504 VectorTypes.InsertNode(New, InsertPos);
505 Types.push_back(New);
506 return QualType(New, 0);
507}
508
Reid Spencer5f016e22007-07-11 17:01:13 +0000509/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
510///
511QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
512 // Unique functions, to guarantee there is only one function of a particular
513 // structure.
514 llvm::FoldingSetNodeID ID;
515 FunctionTypeNoProto::Profile(ID, ResultTy);
516
517 void *InsertPos = 0;
518 if (FunctionTypeNoProto *FT =
519 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
520 return QualType(FT, 0);
521
522 QualType Canonical;
523 if (!ResultTy->isCanonical()) {
524 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
525
526 // Get the new insert position for the node we care about.
527 FunctionTypeNoProto *NewIP =
528 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
529 assert(NewIP == 0 && "Shouldn't be in the map!");
530 }
531
532 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
533 Types.push_back(New);
534 FunctionTypeProtos.InsertNode(New, InsertPos);
535 return QualType(New, 0);
536}
537
538/// getFunctionType - Return a normal function type with a typed argument
539/// list. isVariadic indicates whether the argument list includes '...'.
540QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
541 unsigned NumArgs, bool isVariadic) {
542 // Unique functions, to guarantee there is only one function of a particular
543 // structure.
544 llvm::FoldingSetNodeID ID;
545 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
546
547 void *InsertPos = 0;
548 if (FunctionTypeProto *FTP =
549 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
550 return QualType(FTP, 0);
551
552 // Determine whether the type being created is already canonical or not.
553 bool isCanonical = ResultTy->isCanonical();
554 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
555 if (!ArgArray[i]->isCanonical())
556 isCanonical = false;
557
558 // If this type isn't canonical, get the canonical version of it.
559 QualType Canonical;
560 if (!isCanonical) {
561 llvm::SmallVector<QualType, 16> CanonicalArgs;
562 CanonicalArgs.reserve(NumArgs);
563 for (unsigned i = 0; i != NumArgs; ++i)
564 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
565
566 Canonical = getFunctionType(ResultTy.getCanonicalType(),
567 &CanonicalArgs[0], NumArgs,
568 isVariadic);
569
570 // Get the new insert position for the node we care about.
571 FunctionTypeProto *NewIP =
572 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
573 assert(NewIP == 0 && "Shouldn't be in the map!");
574 }
575
576 // FunctionTypeProto objects are not allocated with new because they have a
577 // variable size array (for parameter types) at the end of them.
578 FunctionTypeProto *FTP =
579 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner942cfd32007-07-20 18:48:28 +0000580 NumArgs*sizeof(QualType));
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
582 Canonical);
583 Types.push_back(FTP);
584 FunctionTypeProtos.InsertNode(FTP, InsertPos);
585 return QualType(FTP, 0);
586}
587
588/// getTypedefType - Return the unique reference to the type for the
589/// specified typename decl.
590QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
591 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
592
593 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
594 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
595 Types.push_back(Decl->TypeForDecl);
596 return QualType(Decl->TypeForDecl, 0);
597}
598
Steve Naroff9752f252007-08-01 18:02:17 +0000599/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
600/// TypeOfExpr AST's (since expression's are never shared). For example,
601/// multiple declarations that refer to "typeof(x)" all contain different
602/// DeclRefExpr's. This doesn't effect the type checker, since it operates
603/// on canonical type's (which are always unique).
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000604QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffd1861fd2007-07-31 12:34:36 +0000605 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000606 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
607 Types.push_back(toe);
608 return QualType(toe, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000609}
610
Steve Naroff9752f252007-08-01 18:02:17 +0000611/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
612/// TypeOfType AST's. The only motivation to unique these nodes would be
613/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
614/// an issue. This doesn't effect the type checker, since it operates
615/// on canonical type's (which are always unique).
Steve Naroffd1861fd2007-07-31 12:34:36 +0000616QualType ASTContext::getTypeOfType(QualType tofType) {
617 QualType Canonical = tofType.getCanonicalType();
Steve Naroff9752f252007-08-01 18:02:17 +0000618 TypeOfType *tot = new TypeOfType(tofType, Canonical);
619 Types.push_back(tot);
620 return QualType(tot, 0);
Steve Naroffd1861fd2007-07-31 12:34:36 +0000621}
622
Reid Spencer5f016e22007-07-11 17:01:13 +0000623/// getTagDeclType - Return the unique reference to the type for the
624/// specified TagDecl (struct/union/class/enum) decl.
625QualType ASTContext::getTagDeclType(TagDecl *Decl) {
626 // The decl stores the type cache.
627 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
628
629 Decl->TypeForDecl = new TagType(Decl, QualType());
630 Types.push_back(Decl->TypeForDecl);
631 return QualType(Decl->TypeForDecl, 0);
632}
633
634/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
635/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
636/// needs to agree with the definition in <stddef.h>.
637QualType ASTContext::getSizeType() const {
638 // On Darwin, size_t is defined as a "long unsigned int".
639 // FIXME: should derive from "Target".
640 return UnsignedLongTy;
641}
642
Chris Lattner8b9023b2007-07-13 03:05:23 +0000643/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
644/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
645QualType ASTContext::getPointerDiffType() const {
646 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
647 // FIXME: should derive from "Target".
648 return IntTy;
649}
650
Reid Spencer5f016e22007-07-11 17:01:13 +0000651/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
652/// routine will assert if passed a built-in type that isn't an integer or enum.
653static int getIntegerRank(QualType t) {
654 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
655 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
656 return 4;
657 }
658
659 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
660 switch (BT->getKind()) {
661 default:
662 assert(0 && "getIntegerRank(): not a built-in integer");
663 case BuiltinType::Bool:
664 return 1;
665 case BuiltinType::Char_S:
666 case BuiltinType::Char_U:
667 case BuiltinType::SChar:
668 case BuiltinType::UChar:
669 return 2;
670 case BuiltinType::Short:
671 case BuiltinType::UShort:
672 return 3;
673 case BuiltinType::Int:
674 case BuiltinType::UInt:
675 return 4;
676 case BuiltinType::Long:
677 case BuiltinType::ULong:
678 return 5;
679 case BuiltinType::LongLong:
680 case BuiltinType::ULongLong:
681 return 6;
682 }
683}
684
685/// getFloatingRank - Return a relative rank for floating point types.
686/// This routine will assert if passed a built-in type that isn't a float.
687static int getFloatingRank(QualType T) {
688 T = T.getCanonicalType();
689 if (ComplexType *CT = dyn_cast<ComplexType>(T))
690 return getFloatingRank(CT->getElementType());
691
692 switch (cast<BuiltinType>(T)->getKind()) {
693 default: assert(0 && "getFloatingPointRank(): not a floating type");
694 case BuiltinType::Float: return FloatRank;
695 case BuiltinType::Double: return DoubleRank;
696 case BuiltinType::LongDouble: return LongDoubleRank;
697 }
698}
699
Steve Naroff716c7302007-08-27 01:41:48 +0000700/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
701/// point or a complex type (based on typeDomain/typeSize).
702/// 'typeDomain' is a real floating point or complex type.
703/// 'typeSize' is a real floating point or complex type.
Steve Narofff1448a02007-08-27 01:27:54 +0000704QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
705 QualType typeSize, QualType typeDomain) const {
706 if (typeDomain->isComplexType()) {
707 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000708 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000709 case FloatRank: return FloatComplexTy;
710 case DoubleRank: return DoubleComplexTy;
711 case LongDoubleRank: return LongDoubleComplexTy;
712 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000713 }
Steve Narofff1448a02007-08-27 01:27:54 +0000714 if (typeDomain->isRealFloatingType()) {
715 switch (getFloatingRank(typeSize)) {
Steve Naroff716c7302007-08-27 01:41:48 +0000716 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Narofff1448a02007-08-27 01:27:54 +0000717 case FloatRank: return FloatTy;
718 case DoubleRank: return DoubleTy;
719 case LongDoubleRank: return LongDoubleTy;
720 }
721 }
722 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Reid Spencer5f016e22007-07-11 17:01:13 +0000723}
724
Steve Narofffb0d4962007-08-27 15:30:22 +0000725/// compareFloatingType - Handles 3 different combos:
726/// float/float, float/complex, complex/complex.
727/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
728int ASTContext::compareFloatingType(QualType lt, QualType rt) {
729 if (getFloatingRank(lt) == getFloatingRank(rt))
730 return 0;
731 if (getFloatingRank(lt) > getFloatingRank(rt))
732 return 1;
733 return -1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000734}
735
736// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
737// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
738QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
739 if (lhs == rhs) return lhs;
740
741 bool t1Unsigned = lhs->isUnsignedIntegerType();
742 bool t2Unsigned = rhs->isUnsignedIntegerType();
743
744 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
745 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
746
747 // We have two integer types with differing signs
748 QualType unsignedType = t1Unsigned ? lhs : rhs;
749 QualType signedType = t1Unsigned ? rhs : lhs;
750
751 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
752 return unsignedType;
753 else {
754 // FIXME: Need to check if the signed type can represent all values of the
755 // unsigned type. If it can, then the result is the signed type.
756 // If it can't, then the result is the unsigned version of the signed type.
757 // Should probably add a helper that returns a signed integer type from
758 // an unsigned (and vice versa). C99 6.3.1.8.
759 return signedType;
760 }
761}
Anders Carlsson71993dd2007-08-17 05:31:46 +0000762
763// getCFConstantStringType - Return the type used for constant CFStrings.
764QualType ASTContext::getCFConstantStringType() {
765 if (!CFConstantStringTypeDecl) {
766 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
767 &Idents.get("__builtin_CFString"),
768 0);
769
770 QualType FieldTypes[4];
771
772 // const int *isa;
773 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
774 // int flags;
775 FieldTypes[1] = IntTy;
776 // const char *str;
777 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
778 // long length;
779 FieldTypes[3] = LongTy;
780 // Create fields
781 FieldDecl *FieldDecls[4];
782
783 for (unsigned i = 0; i < 4; ++i)
784 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i], 0);
785
786 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
787 }
788
789 return getTagDeclType(CFConstantStringTypeDecl);
Chris Lattner6cd862c2007-08-27 17:38:00 +0000790}