blob: 61e2dbea069e898afa3f1b6157e92178287cc7e3 [file] [log] [blame]
Chris Lattnerddc135e2006-11-10 06:34:16 +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"
Chris Lattnerd0342e52006-11-20 04:02:15 +000015#include "clang/AST/Decl.h"
Steve Naroff67391b82007-10-01 19:00:59 +000016#include "clang/AST/DeclObjC.h"
Chris Lattner4dc8a6f2007-05-20 23:50:58 +000017#include "clang/Basic/TargetInfo.h"
Chris Lattnerc6ad8132006-12-02 07:52:18 +000018#include "llvm/ADT/SmallVector.h"
Anders Carlssond8499822007-10-29 05:01:08 +000019#include "llvm/ADT/StringExtras.h"
20
Chris Lattnerddc135e2006-11-10 06:34:16 +000021using namespace clang;
22
Steve Naroff0af91202007-04-27 21:51:21 +000023enum FloatingRank {
24 FloatRank, DoubleRank, LongDoubleRank
25};
26
Chris Lattnerd5973eb2006-11-12 00:53:46 +000027ASTContext::~ASTContext() {
28 // Deallocate all the types.
29 while (!Types.empty()) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +000030 if (FunctionTypeProto *FT = dyn_cast<FunctionTypeProto>(Types.back())) {
31 // Destroy the object, but don't call delete. These are malloc'd.
32 FT->~FunctionTypeProto();
33 free(FT);
34 } else {
35 delete Types.back();
36 }
Chris Lattnerd5973eb2006-11-12 00:53:46 +000037 Types.pop_back();
38 }
39}
40
Chris Lattner4eb445d2007-01-26 01:27:23 +000041void ASTContext::PrintStats() const {
42 fprintf(stderr, "*** AST Context Stats:\n");
43 fprintf(stderr, " %d types total.\n", (int)Types.size());
44 unsigned NumBuiltin = 0, NumPointer = 0, NumArray = 0, NumFunctionP = 0;
Chris Lattner9c016772007-07-18 05:50:59 +000045 unsigned NumVector = 0, NumComplex = 0;
Bill Wendling3708c182007-05-27 10:15:43 +000046 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Chris Lattner4eb445d2007-01-26 01:27:23 +000047
48 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Steve Naroff73d534a2007-09-17 14:16:13 +000049 unsigned NumObjcInterfaces = 0;
Chris Lattner4eb445d2007-01-26 01:27:23 +000050
51 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
52 Type *T = Types[i];
53 if (isa<BuiltinType>(T))
54 ++NumBuiltin;
55 else if (isa<PointerType>(T))
56 ++NumPointer;
Bill Wendling3708c182007-05-27 10:15:43 +000057 else if (isa<ReferenceType>(T))
58 ++NumReference;
Chris Lattner9c016772007-07-18 05:50:59 +000059 else if (isa<ComplexType>(T))
60 ++NumComplex;
Chris Lattner4eb445d2007-01-26 01:27:23 +000061 else if (isa<ArrayType>(T))
62 ++NumArray;
Chris Lattner9c016772007-07-18 05:50:59 +000063 else if (isa<VectorType>(T))
64 ++NumVector;
Chris Lattner4eb445d2007-01-26 01:27:23 +000065 else if (isa<FunctionTypeNoProto>(T))
66 ++NumFunctionNP;
67 else if (isa<FunctionTypeProto>(T))
68 ++NumFunctionP;
Chris Lattner32d920b2007-01-26 02:01:53 +000069 else if (isa<TypedefType>(T))
Chris Lattner4eb445d2007-01-26 01:27:23 +000070 ++NumTypeName;
Steve Narofff1e53692007-03-23 22:27:02 +000071 else if (TagType *TT = dyn_cast<TagType>(T)) {
Chris Lattner4eb445d2007-01-26 01:27:23 +000072 ++NumTagged;
73 switch (TT->getDecl()->getKind()) {
74 default: assert(0 && "Unknown tagged type!");
75 case Decl::Struct: ++NumTagStruct; break;
76 case Decl::Union: ++NumTagUnion; break;
77 case Decl::Class: ++NumTagClass; break;
78 case Decl::Enum: ++NumTagEnum; break;
79 }
Steve Naroff73d534a2007-09-17 14:16:13 +000080 } else if (isa<ObjcInterfaceType>(T))
81 ++NumObjcInterfaces;
82 else {
Chris Lattner4eb445d2007-01-26 01:27:23 +000083 assert(0 && "Unknown type!");
84 }
85 }
86
87 fprintf(stderr, " %d builtin types\n", NumBuiltin);
88 fprintf(stderr, " %d pointer types\n", NumPointer);
Bill Wendling3708c182007-05-27 10:15:43 +000089 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner9c016772007-07-18 05:50:59 +000090 fprintf(stderr, " %d complex types\n", NumComplex);
Chris Lattner4eb445d2007-01-26 01:27:23 +000091 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner9c016772007-07-18 05:50:59 +000092 fprintf(stderr, " %d vector types\n", NumVector);
Chris Lattner4eb445d2007-01-26 01:27:23 +000093 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
94 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
95 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
96 fprintf(stderr, " %d tagged types\n", NumTagged);
97 fprintf(stderr, " %d struct types\n", NumTagStruct);
98 fprintf(stderr, " %d union types\n", NumTagUnion);
99 fprintf(stderr, " %d class types\n", NumTagClass);
100 fprintf(stderr, " %d enum types\n", NumTagEnum);
Steve Naroff73d534a2007-09-17 14:16:13 +0000101 fprintf(stderr, " %d interface types\n", NumObjcInterfaces);
Chris Lattnerfc234de2007-05-24 00:40:54 +0000102 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
Steve Narofff84d11f2007-05-23 21:48:04 +0000103 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner9c016772007-07-18 05:50:59 +0000104 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Chris Lattnerfc234de2007-05-24 00:40:54 +0000105 NumFunctionP*sizeof(FunctionTypeProto)+
106 NumFunctionNP*sizeof(FunctionTypeNoProto)+
107 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
Chris Lattner4eb445d2007-01-26 01:27:23 +0000108}
109
110
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000111void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
112 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000113}
114
Chris Lattner970e54e2006-11-12 00:37:36 +0000115void ASTContext::InitBuiltinTypes() {
116 assert(VoidTy.isNull() && "Context reinitialized?");
117
118 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000119 InitBuiltinType(VoidTy, BuiltinType::Void);
Chris Lattner970e54e2006-11-12 00:37:36 +0000120
121 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000122 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000123 // C99 6.2.5p3.
Chris Lattnerb16f4552007-06-03 07:25:34 +0000124 if (Target.isCharSigned(SourceLocation()))
125 InitBuiltinType(CharTy, BuiltinType::Char_S);
126 else
127 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000128 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000129 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
130 InitBuiltinType(ShortTy, BuiltinType::Short);
131 InitBuiltinType(IntTy, BuiltinType::Int);
132 InitBuiltinType(LongTy, BuiltinType::Long);
133 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000134
135 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000136 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
137 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
138 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
139 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
140 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000141
142 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000143 InitBuiltinType(FloatTy, BuiltinType::Float);
144 InitBuiltinType(DoubleTy, BuiltinType::Double);
145 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Chris Lattner970e54e2006-11-12 00:37:36 +0000146
147 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000148 FloatComplexTy = getComplexType(FloatTy);
149 DoubleComplexTy = getComplexType(DoubleTy);
150 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff66e9f332007-10-15 14:41:52 +0000151
152 BuiltinVaListType = QualType();
153 ObjcIdType = QualType();
154 IdStructType = 0;
Steve Narofff73b7842007-10-15 23:35:17 +0000155 ObjcConstantStringType = QualType();
Chris Lattner970e54e2006-11-12 00:37:36 +0000156}
157
Chris Lattner53cfe802007-07-18 17:52:12 +0000158//===----------------------------------------------------------------------===//
159// Type Sizing and Analysis
160//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000161
162/// getTypeSize - Return the size of the specified type, in bits. This method
163/// does not work on incomplete types.
Chris Lattner4481b422007-07-14 01:29:45 +0000164std::pair<uint64_t, unsigned>
165ASTContext::getTypeInfo(QualType T, SourceLocation L) {
Chris Lattner983a8bb2007-07-13 22:13:22 +0000166 T = T.getCanonicalType();
Chris Lattner4481b422007-07-14 01:29:45 +0000167 uint64_t Size;
168 unsigned Align;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000169 switch (T->getTypeClass()) {
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000170 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner355332d2007-07-13 22:27:08 +0000171 case Type::FunctionNoProto:
172 case Type::FunctionProto:
Chris Lattner647fb222007-07-18 18:26:58 +0000173 default:
Chris Lattnerab41b132007-07-20 18:13:33 +0000174 assert(0 && "Incomplete types have no size!");
Steve Naroff5c131802007-08-30 01:06:46 +0000175 case Type::VariableArray:
176 assert(0 && "VLAs not implemented yet!");
177 case Type::ConstantArray: {
178 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
179
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000180 std::pair<uint64_t, unsigned> EltInfo =
Steve Naroff5c131802007-08-30 01:06:46 +0000181 getTypeInfo(CAT->getElementType(), L);
182 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000183 Align = EltInfo.second;
184 break;
185 }
186 case Type::Vector: {
187 std::pair<uint64_t, unsigned> EltInfo =
188 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
189 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
190 // FIXME: Vector alignment is not the alignment of its elements.
191 Align = EltInfo.second;
192 break;
193 }
Chris Lattner647fb222007-07-18 18:26:58 +0000194
Chris Lattner983a8bb2007-07-13 22:13:22 +0000195 case Type::Builtin: {
196 // FIXME: need to use TargetInfo to derive the target specific sizes. This
197 // implementation will suffice for play with vector support.
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000198 const llvm::fltSemantics *F;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000199 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000200 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000201 case BuiltinType::Void:
202 assert(0 && "Incomplete types have no size!");
203 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000204 case BuiltinType::Char_S:
205 case BuiltinType::Char_U:
206 case BuiltinType::UChar:
Chris Lattner4481b422007-07-14 01:29:45 +0000207 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000208 case BuiltinType::UShort:
Chris Lattner4481b422007-07-14 01:29:45 +0000209 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000210 case BuiltinType::UInt:
Chris Lattner4481b422007-07-14 01:29:45 +0000211 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000212 case BuiltinType::ULong:
Chris Lattner4481b422007-07-14 01:29:45 +0000213 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000214 case BuiltinType::ULongLong:
Chris Lattner4481b422007-07-14 01:29:45 +0000215 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000216 case BuiltinType::Float: Target.getFloatInfo(Size, Align, F, L); break;
217 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, F, L);break;
218 case BuiltinType::LongDouble:Target.getLongDoubleInfo(Size,Align,F,L);break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000219 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000220 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000221 }
Chris Lattner4481b422007-07-14 01:29:45 +0000222 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000223 case Type::Reference:
Chris Lattner0523dc92007-07-13 22:16:13 +0000224 // "When applied to a reference or a reference type, the result is the size
Chris Lattner647fb222007-07-18 18:26:58 +0000225 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
226 // FIXME: This is wrong for struct layout!
Chris Lattner4481b422007-07-14 01:29:45 +0000227 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner647fb222007-07-18 18:26:58 +0000228
229 case Type::Complex: {
230 // Complex types have the same alignment as their elements, but twice the
231 // size.
232 std::pair<uint64_t, unsigned> EltInfo =
233 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
234 Size = EltInfo.first*2;
235 Align = EltInfo.second;
236 break;
237 }
238 case Type::Tagged:
Chris Lattnera9e79612007-08-27 17:38:00 +0000239 TagType *TT = cast<TagType>(T);
240 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
241 const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L);
242 Size = Layout.getSize();
243 Align = Layout.getAlignment();
244 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattner1c1f9322007-08-28 18:24:31 +0000245 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattnera9e79612007-08-27 17:38:00 +0000246 } else {
Chris Lattner49a953a2007-07-23 22:46:22 +0000247 assert(0 && "Unimplemented type sizes!");
Chris Lattnera9e79612007-08-27 17:38:00 +0000248 }
Chris Lattner49a953a2007-07-23 22:46:22 +0000249 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000250 }
Chris Lattner4481b422007-07-14 01:29:45 +0000251
Chris Lattner53cfe802007-07-18 17:52:12 +0000252 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner4481b422007-07-14 01:29:45 +0000253 return std::make_pair(Size, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000254}
255
Chris Lattner53cfe802007-07-18 17:52:12 +0000256/// getRecordLayout - Get or compute information about the layout of the
257/// specified record (struct/union/class), which indicates its size and field
258/// position information.
259const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D,
260 SourceLocation L) {
261 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
262
263 // Look up this layout, if already laid out, return what we have.
264 const RecordLayout *&Entry = RecordLayoutInfo[D];
265 if (Entry) return *Entry;
266
267 // Allocate and assign into RecordLayoutInfo here. The "Entry" reference can
268 // be invalidated (dangle) if the RecordLayoutInfo hashtable is inserted into.
269 RecordLayout *NewEntry = new RecordLayout();
270 Entry = NewEntry;
271
272 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
273 uint64_t RecordSize = 0;
274 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
275
276 if (D->getKind() != Decl::Union) {
277 // Layout each field, for now, just sequentially, respecting alignment. In
278 // the future, this will need to be tweakable by targets.
279 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
280 const FieldDecl *FD = D->getMember(i);
281 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
282 uint64_t FieldSize = FieldInfo.first;
283 unsigned FieldAlign = FieldInfo.second;
284
285 // Round up the current record size to the field's alignment boundary.
286 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
287
288 // Place this field at the current location.
289 FieldOffsets[i] = RecordSize;
290
291 // Reserve space for this field.
292 RecordSize += FieldSize;
293
294 // Remember max struct/class alignment.
295 RecordAlign = std::max(RecordAlign, FieldAlign);
296 }
297
298 // Finally, round the size of the total struct up to the alignment of the
299 // struct itself.
300 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
301 } else {
302 // Union layout just puts each member at the start of the record.
303 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
304 const FieldDecl *FD = D->getMember(i);
305 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
306 uint64_t FieldSize = FieldInfo.first;
307 unsigned FieldAlign = FieldInfo.second;
308
309 // Round up the current record size to the field's alignment boundary.
310 RecordSize = std::max(RecordSize, FieldSize);
311
312 // Place this field at the start of the record.
313 FieldOffsets[i] = 0;
314
315 // Remember max struct/class alignment.
316 RecordAlign = std::max(RecordAlign, FieldAlign);
317 }
318 }
Chris Lattner647fb222007-07-18 18:26:58 +0000319
320 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
321 return *NewEntry;
Chris Lattner53cfe802007-07-18 17:52:12 +0000322}
323
Chris Lattner983a8bb2007-07-13 22:13:22 +0000324//===----------------------------------------------------------------------===//
325// Type creation/memoization methods
326//===----------------------------------------------------------------------===//
327
328
Chris Lattnerc6395932007-06-22 20:56:16 +0000329/// getComplexType - Return the uniqued reference to the type for a complex
330/// number with the specified element type.
331QualType ASTContext::getComplexType(QualType T) {
332 // Unique pointers, to guarantee there is only one pointer of a particular
333 // structure.
334 llvm::FoldingSetNodeID ID;
335 ComplexType::Profile(ID, T);
336
337 void *InsertPos = 0;
338 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
339 return QualType(CT, 0);
340
341 // If the pointee type isn't canonical, this won't be a canonical type either,
342 // so fill in the canonical type field.
343 QualType Canonical;
344 if (!T->isCanonical()) {
345 Canonical = getComplexType(T.getCanonicalType());
346
347 // Get the new insert position for the node we care about.
348 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
349 assert(NewIP == 0 && "Shouldn't be in the map!");
350 }
351 ComplexType *New = new ComplexType(T, Canonical);
352 Types.push_back(New);
353 ComplexTypes.InsertNode(New, InsertPos);
354 return QualType(New, 0);
355}
356
357
Chris Lattner970e54e2006-11-12 00:37:36 +0000358/// getPointerType - Return the uniqued reference to the type for a pointer to
359/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000360QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000361 // Unique pointers, to guarantee there is only one pointer of a particular
362 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000363 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +0000364 PointerType::Profile(ID, T);
365
366 void *InsertPos = 0;
367 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000368 return QualType(PT, 0);
Chris Lattner970e54e2006-11-12 00:37:36 +0000369
Chris Lattner7ccecb92006-11-12 08:50:50 +0000370 // If the pointee type isn't canonical, this won't be a canonical type either,
371 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000372 QualType Canonical;
Chris Lattner67521df2007-01-27 01:29:36 +0000373 if (!T->isCanonical()) {
Steve Naroffd50c88e2007-04-05 21:15:20 +0000374 Canonical = getPointerType(T.getCanonicalType());
Chris Lattner67521df2007-01-27 01:29:36 +0000375
376 // Get the new insert position for the node we care about.
377 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
378 assert(NewIP == 0 && "Shouldn't be in the map!");
379 }
Chris Lattner67521df2007-01-27 01:29:36 +0000380 PointerType *New = new PointerType(T, Canonical);
381 Types.push_back(New);
382 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000383 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +0000384}
385
Bill Wendling3708c182007-05-27 10:15:43 +0000386/// getReferenceType - Return the uniqued reference to the type for a reference
387/// to the specified type.
388QualType ASTContext::getReferenceType(QualType T) {
389 // Unique pointers, to guarantee there is only one pointer of a particular
390 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000391 llvm::FoldingSetNodeID ID;
Bill Wendling3708c182007-05-27 10:15:43 +0000392 ReferenceType::Profile(ID, T);
393
394 void *InsertPos = 0;
395 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
396 return QualType(RT, 0);
397
398 // If the referencee type isn't canonical, this won't be a canonical type
399 // either, so fill in the canonical type field.
400 QualType Canonical;
401 if (!T->isCanonical()) {
402 Canonical = getReferenceType(T.getCanonicalType());
403
404 // Get the new insert position for the node we care about.
405 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
406 assert(NewIP == 0 && "Shouldn't be in the map!");
407 }
408
409 ReferenceType *New = new ReferenceType(T, Canonical);
410 Types.push_back(New);
411 ReferenceTypes.InsertNode(New, InsertPos);
412 return QualType(New, 0);
413}
414
Steve Naroff5c131802007-08-30 01:06:46 +0000415/// getConstantArrayType - Return the unique reference to the type for an
416/// array of the specified element type.
417QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff90dfdd52007-08-30 18:10:14 +0000418 const llvm::APInt &ArySize,
419 ArrayType::ArraySizeModifier ASM,
420 unsigned EltTypeQuals) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000421 llvm::FoldingSetNodeID ID;
Steve Naroff5c131802007-08-30 01:06:46 +0000422 ConstantArrayType::Profile(ID, EltTy, ArySize);
Steve Naroff408451b2007-02-26 22:17:12 +0000423
Chris Lattner36f8e652007-01-27 08:31:04 +0000424 void *InsertPos = 0;
Steve Naroff5c131802007-08-30 01:06:46 +0000425 if (ConstantArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000426 return QualType(ATP, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000427
428 // If the element type isn't canonical, this won't be a canonical type either,
429 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000430 QualType Canonical;
Chris Lattner36f8e652007-01-27 08:31:04 +0000431 if (!EltTy->isCanonical()) {
Steve Naroff90dfdd52007-08-30 18:10:14 +0000432 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
433 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +0000434 // Get the new insert position for the node we care about.
Steve Naroff5c131802007-08-30 01:06:46 +0000435 ConstantArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +0000436 assert(NewIP == 0 && "Shouldn't be in the map!");
437 }
Chris Lattner7ccecb92006-11-12 08:50:50 +0000438
Steve Naroff90dfdd52007-08-30 18:10:14 +0000439 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
440 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +0000441 ArrayTypes.InsertNode(New, InsertPos);
442 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000443 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000444}
445
Steve Naroffcadebd02007-08-30 18:14:25 +0000446/// getVariableArrayType - Returns a non-unique reference to the type for a
447/// variable array of the specified element type.
Steve Naroff90dfdd52007-08-30 18:10:14 +0000448QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
449 ArrayType::ArraySizeModifier ASM,
450 unsigned EltTypeQuals) {
451 // Since we don't unique expressions, it isn't possible to unique VLA's.
452 ArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
453 ASM, EltTypeQuals);
454 Types.push_back(New);
455 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +0000456}
457
Steve Naroff91fcddb2007-07-18 18:00:27 +0000458/// getVectorType - Return the unique reference to a vector type of
459/// the specified element type and size. VectorType must be a built-in type.
460QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000461 BuiltinType *baseType;
462
463 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff91fcddb2007-07-18 18:00:27 +0000464 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000465
466 // Check if we've already instantiated a vector of this type.
467 llvm::FoldingSetNodeID ID;
Steve Naroff91fcddb2007-07-18 18:00:27 +0000468 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000469 void *InsertPos = 0;
470 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
471 return QualType(VTP, 0);
472
473 // If the element type isn't canonical, this won't be a canonical type either,
474 // so fill in the canonical type field.
475 QualType Canonical;
476 if (!vecType->isCanonical()) {
Steve Naroff91fcddb2007-07-18 18:00:27 +0000477 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000478
479 // Get the new insert position for the node we care about.
480 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
481 assert(NewIP == 0 && "Shouldn't be in the map!");
482 }
483 VectorType *New = new VectorType(vecType, NumElts, Canonical);
484 VectorTypes.InsertNode(New, InsertPos);
485 Types.push_back(New);
486 return QualType(New, 0);
487}
488
Steve Naroff91fcddb2007-07-18 18:00:27 +0000489/// getOCUVectorType - Return the unique reference to an OCU vector type of
490/// the specified element type and size. VectorType must be a built-in type.
491QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
492 BuiltinType *baseType;
493
494 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
495 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
496
497 // Check if we've already instantiated a vector of this type.
498 llvm::FoldingSetNodeID ID;
499 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
500 void *InsertPos = 0;
501 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
502 return QualType(VTP, 0);
503
504 // If the element type isn't canonical, this won't be a canonical type either,
505 // so fill in the canonical type field.
506 QualType Canonical;
507 if (!vecType->isCanonical()) {
508 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
509
510 // Get the new insert position for the node we care about.
511 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
512 assert(NewIP == 0 && "Shouldn't be in the map!");
513 }
514 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
515 VectorTypes.InsertNode(New, InsertPos);
516 Types.push_back(New);
517 return QualType(New, 0);
518}
519
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000520/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
521///
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000522QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000523 // Unique functions, to guarantee there is only one function of a particular
524 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000525 llvm::FoldingSetNodeID ID;
Chris Lattner47955de2007-01-27 08:37:20 +0000526 FunctionTypeNoProto::Profile(ID, ResultTy);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000527
Chris Lattner47955de2007-01-27 08:37:20 +0000528 void *InsertPos = 0;
529 if (FunctionTypeNoProto *FT =
530 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000531 return QualType(FT, 0);
Chris Lattner47955de2007-01-27 08:37:20 +0000532
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000533 QualType Canonical;
Chris Lattner47955de2007-01-27 08:37:20 +0000534 if (!ResultTy->isCanonical()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000535 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
Chris Lattner47955de2007-01-27 08:37:20 +0000536
537 // Get the new insert position for the node we care about.
538 FunctionTypeNoProto *NewIP =
539 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
540 assert(NewIP == 0 && "Shouldn't be in the map!");
541 }
542
543 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
544 Types.push_back(New);
545 FunctionTypeProtos.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000546 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000547}
548
549/// getFunctionType - Return a normal function type with a typed argument
550/// list. isVariadic indicates whether the argument list includes '...'.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000551QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
552 unsigned NumArgs, bool isVariadic) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000553 // Unique functions, to guarantee there is only one function of a particular
554 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000555 llvm::FoldingSetNodeID ID;
Chris Lattnerfd4de792007-01-27 01:15:32 +0000556 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
557
558 void *InsertPos = 0;
559 if (FunctionTypeProto *FTP =
560 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000561 return QualType(FTP, 0);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000562
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000563 // Determine whether the type being created is already canonical or not.
564 bool isCanonical = ResultTy->isCanonical();
565 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
566 if (!ArgArray[i]->isCanonical())
567 isCanonical = false;
568
569 // If this type isn't canonical, get the canonical version of it.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000570 QualType Canonical;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000571 if (!isCanonical) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000572 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000573 CanonicalArgs.reserve(NumArgs);
574 for (unsigned i = 0; i != NumArgs; ++i)
575 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
576
577 Canonical = getFunctionType(ResultTy.getCanonicalType(),
578 &CanonicalArgs[0], NumArgs,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000579 isVariadic);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000580
581 // Get the new insert position for the node we care about.
582 FunctionTypeProto *NewIP =
583 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
584 assert(NewIP == 0 && "Shouldn't be in the map!");
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000585 }
586
587 // FunctionTypeProto objects are not allocated with new because they have a
588 // variable size array (for parameter types) at the end of them.
589 FunctionTypeProto *FTP =
590 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner98021442007-07-20 18:48:28 +0000591 NumArgs*sizeof(QualType));
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000592 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
593 Canonical);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000594 Types.push_back(FTP);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000595 FunctionTypeProtos.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000596 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000597}
Chris Lattneref51c202006-11-10 07:17:23 +0000598
Chris Lattner32d920b2007-01-26 02:01:53 +0000599/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +0000600/// specified typename decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000601QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
602 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000603
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000604 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Chris Lattnercceab1a2007-03-26 20:16:44 +0000605 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
606 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000607 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000608}
609
Steve Naroff09bf8152007-09-06 21:24:23 +0000610/// getObjcInterfaceType - Return the unique reference to the type for the
611/// specified ObjC interface decl.
612QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) {
613 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
614
615 Decl->TypeForDecl = new ObjcInterfaceType(Decl);
616 Types.push_back(Decl->TypeForDecl);
617 return QualType(Decl->TypeForDecl, 0);
618}
619
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000620/// getObjcQualifiedInterfaceType - Return a
621/// ObjcQualifiedInterfaceType type for the given interface decl and
622/// the conforming protocol list.
623QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl,
624 ObjcProtocolDecl **Protocols, unsigned NumProtocols) {
625 ObjcInterfaceType *IType =
626 cast<ObjcInterfaceType>(getObjcInterfaceType(Decl));
627
628 llvm::FoldingSetNodeID ID;
629 ObjcQualifiedInterfaceType::Profile(ID, IType, Protocols, NumProtocols);
630
631 void *InsertPos = 0;
632 if (ObjcQualifiedInterfaceType *QT =
633 ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
634 return QualType(QT, 0);
635
636 // No Match;
Chris Lattnerf709a142007-10-11 03:36:41 +0000637 ObjcQualifiedInterfaceType *QType =
638 new ObjcQualifiedInterfaceType(IType, Protocols, NumProtocols);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000639 Types.push_back(QType);
640 ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
641 return QualType(QType, 0);
642}
643
Steve Naroffa773cd52007-08-01 18:02:17 +0000644/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
645/// TypeOfExpr AST's (since expression's are never shared). For example,
646/// multiple declarations that refer to "typeof(x)" all contain different
647/// DeclRefExpr's. This doesn't effect the type checker, since it operates
648/// on canonical type's (which are always unique).
Steve Naroff236becb2007-08-01 17:20:42 +0000649QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffad373bd2007-07-31 12:34:36 +0000650 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroffa773cd52007-08-01 18:02:17 +0000651 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
652 Types.push_back(toe);
653 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +0000654}
655
Steve Naroffa773cd52007-08-01 18:02:17 +0000656/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
657/// TypeOfType AST's. The only motivation to unique these nodes would be
658/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
659/// an issue. This doesn't effect the type checker, since it operates
660/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +0000661QualType ASTContext::getTypeOfType(QualType tofType) {
662 QualType Canonical = tofType.getCanonicalType();
Steve Naroffa773cd52007-08-01 18:02:17 +0000663 TypeOfType *tot = new TypeOfType(tofType, Canonical);
664 Types.push_back(tot);
665 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +0000666}
667
Chris Lattnerfb072462007-01-23 05:45:31 +0000668/// getTagDeclType - Return the unique reference to the type for the
669/// specified TagDecl (struct/union/class/enum) decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000670QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Chris Lattner733067d2007-01-26 01:42:24 +0000671 // The decl stores the type cache.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000672 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000673
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000674 Decl->TypeForDecl = new TagType(Decl, QualType());
Chris Lattnercceab1a2007-03-26 20:16:44 +0000675 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000676 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000677}
678
Steve Naroff92e30f82007-04-02 22:35:25 +0000679/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
Steve Naroff0f6256d2007-04-02 23:01:44 +0000680/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
Steve Naroff92e30f82007-04-02 22:35:25 +0000681/// needs to agree with the definition in <stddef.h>.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000682QualType ASTContext::getSizeType() const {
Steve Naroff92e30f82007-04-02 22:35:25 +0000683 // On Darwin, size_t is defined as a "long unsigned int".
684 // FIXME: should derive from "Target".
685 return UnsignedLongTy;
686}
Chris Lattnerfb072462007-01-23 05:45:31 +0000687
Chris Lattnerd2b88ab2007-07-13 03:05:23 +0000688/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
689/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
690QualType ASTContext::getPointerDiffType() const {
691 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
692 // FIXME: should derive from "Target".
693 return IntTy;
694}
695
Steve Naroff0af91202007-04-27 21:51:21 +0000696/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
697/// routine will assert if passed a built-in type that isn't an integer or enum.
698static int getIntegerRank(QualType t) {
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000699 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
700 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
701 return 4;
Steve Naroffe4718892007-04-27 18:30:00 +0000702 }
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000703
704 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
705 switch (BT->getKind()) {
706 default:
707 assert(0 && "getIntegerRank(): not a built-in integer");
708 case BuiltinType::Bool:
709 return 1;
Chris Lattnerb16f4552007-06-03 07:25:34 +0000710 case BuiltinType::Char_S:
711 case BuiltinType::Char_U:
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000712 case BuiltinType::SChar:
713 case BuiltinType::UChar:
714 return 2;
715 case BuiltinType::Short:
716 case BuiltinType::UShort:
717 return 3;
718 case BuiltinType::Int:
719 case BuiltinType::UInt:
720 return 4;
721 case BuiltinType::Long:
722 case BuiltinType::ULong:
723 return 5;
724 case BuiltinType::LongLong:
725 case BuiltinType::ULongLong:
726 return 6;
727 }
Steve Naroffe4718892007-04-27 18:30:00 +0000728}
729
Steve Naroff0af91202007-04-27 21:51:21 +0000730/// getFloatingRank - Return a relative rank for floating point types.
731/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerc6395932007-06-22 20:56:16 +0000732static int getFloatingRank(QualType T) {
733 T = T.getCanonicalType();
734 if (ComplexType *CT = dyn_cast<ComplexType>(T))
735 return getFloatingRank(CT->getElementType());
736
737 switch (cast<BuiltinType>(T)->getKind()) {
738 default: assert(0 && "getFloatingPointRank(): not a floating type");
739 case BuiltinType::Float: return FloatRank;
740 case BuiltinType::Double: return DoubleRank;
741 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +0000742 }
743}
744
Steve Narofffc6ffa22007-08-27 01:41:48 +0000745/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
746/// point or a complex type (based on typeDomain/typeSize).
747/// 'typeDomain' is a real floating point or complex type.
748/// 'typeSize' is a real floating point or complex type.
Steve Naroff9091ef72007-08-27 01:27:54 +0000749QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
750 QualType typeSize, QualType typeDomain) const {
751 if (typeDomain->isComplexType()) {
752 switch (getFloatingRank(typeSize)) {
Steve Narofffc6ffa22007-08-27 01:41:48 +0000753 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +0000754 case FloatRank: return FloatComplexTy;
755 case DoubleRank: return DoubleComplexTy;
756 case LongDoubleRank: return LongDoubleComplexTy;
757 }
Steve Naroff0af91202007-04-27 21:51:21 +0000758 }
Steve Naroff9091ef72007-08-27 01:27:54 +0000759 if (typeDomain->isRealFloatingType()) {
760 switch (getFloatingRank(typeSize)) {
Steve Narofffc6ffa22007-08-27 01:41:48 +0000761 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +0000762 case FloatRank: return FloatTy;
763 case DoubleRank: return DoubleTy;
764 case LongDoubleRank: return LongDoubleTy;
765 }
766 }
767 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner793d10c2007-09-16 19:23:47 +0000768 //an invalid return value, but the assert
769 //will ensure that this code is never reached.
770 return VoidTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000771}
772
Steve Naroff7af82d42007-08-27 15:30:22 +0000773/// compareFloatingType - Handles 3 different combos:
774/// float/float, float/complex, complex/complex.
775/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
776int ASTContext::compareFloatingType(QualType lt, QualType rt) {
777 if (getFloatingRank(lt) == getFloatingRank(rt))
778 return 0;
779 if (getFloatingRank(lt) > getFloatingRank(rt))
780 return 1;
781 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +0000782}
783
Steve Naroff0af91202007-04-27 21:51:21 +0000784// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
785// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
Steve Naroffe4718892007-04-27 18:30:00 +0000786QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
Chris Lattnerb16f4552007-06-03 07:25:34 +0000787 if (lhs == rhs) return lhs;
788
Steve Naroffe4718892007-04-27 18:30:00 +0000789 bool t1Unsigned = lhs->isUnsignedIntegerType();
790 bool t2Unsigned = rhs->isUnsignedIntegerType();
791
792 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
793 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
794
795 // We have two integer types with differing signs
796 QualType unsignedType = t1Unsigned ? lhs : rhs;
797 QualType signedType = t1Unsigned ? rhs : lhs;
798
799 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
800 return unsignedType;
Steve Naroff0af91202007-04-27 21:51:21 +0000801 else {
802 // FIXME: Need to check if the signed type can represent all values of the
803 // unsigned type. If it can, then the result is the signed type.
804 // If it can't, then the result is the unsigned version of the signed type.
805 // Should probably add a helper that returns a signed integer type from
806 // an unsigned (and vice versa). C99 6.3.1.8.
807 return signedType;
808 }
Steve Naroffe4718892007-04-27 18:30:00 +0000809}
Anders Carlsson98f07902007-08-17 05:31:46 +0000810
811// getCFConstantStringType - Return the type used for constant CFStrings.
812QualType ASTContext::getCFConstantStringType() {
813 if (!CFConstantStringTypeDecl) {
814 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
815 &Idents.get("__builtin_CFString"),
816 0);
817
818 QualType FieldTypes[4];
819
820 // const int *isa;
821 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
822 // int flags;
823 FieldTypes[1] = IntTy;
824 // const char *str;
825 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
826 // long length;
827 FieldTypes[3] = LongTy;
828 // Create fields
829 FieldDecl *FieldDecls[4];
830
831 for (unsigned i = 0; i < 4; ++i)
Steve Naroff1d4b5eae2007-09-14 02:20:46 +0000832 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlsson98f07902007-08-17 05:31:46 +0000833
834 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
835 }
836
837 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +0000838}
Anders Carlsson87c149b2007-10-11 01:00:40 +0000839
Anders Carlssond8499822007-10-29 05:01:08 +0000840static bool isTypeTypedefedAsBOOL(QualType T)
841{
842 if (const PointerType *NCPT = T->getAsPointerType())
843 if (const TypedefType *TT = dyn_cast<TypedefType>(NCPT->getPointeeType()))
844 if (!strcmp(TT->getDecl()->getName(), "BOOL"))
845 return true;
846
847 return false;
848}
849
850void ASTContext::getObjcEncodingForType(QualType T, std::string& S) const
851{
852 QualType Ty = T.getCanonicalType();
853
854 if (const BuiltinType *BT = Ty->getAsBuiltinType()) {
855 char encoding;
856 switch (BT->getKind()) {
857 case BuiltinType::Void:
858 encoding = 'v';
859 break;
860 case BuiltinType::Bool:
861 encoding = 'B';
862 break;
863 case BuiltinType::Char_U:
864 case BuiltinType::UChar:
865 encoding = 'C';
866 break;
867 case BuiltinType::UShort:
868 encoding = 'S';
869 break;
870 case BuiltinType::UInt:
871 encoding = 'I';
872 break;
873 case BuiltinType::ULong:
874 encoding = 'L';
875 break;
876 case BuiltinType::ULongLong:
877 encoding = 'Q';
878 break;
879 case BuiltinType::Char_S:
880 case BuiltinType::SChar:
881 encoding = 'c';
882 break;
883 case BuiltinType::Short:
884 encoding = 's';
885 break;
886 case BuiltinType::Int:
887 encoding = 'i';
888 break;
889 case BuiltinType::Long:
890 encoding = 'l';
891 break;
892 case BuiltinType::LongLong:
893 encoding = 'q';
894 break;
895 case BuiltinType::Float:
896 encoding = 'f';
897 break;
898 case BuiltinType::Double:
899 encoding = 'd';
900 break;
901 case BuiltinType::LongDouble:
902 encoding = 'd';
903 break;
904 default:
905 assert(0 && "Unhandled builtin type kind");
906 }
907
908 S += encoding;
909 } else if (const PointerType *PT = Ty->getAsPointerType()) {
910 QualType PointeeTy = PT->getPointeeType();
911
912 if (PointeeTy->isCharType()) {
913 // char pointer types should be encoded as '*' unless it is a
914 // type that has been typedef'd to 'BOOL'.
915 if (isTypeTypedefedAsBOOL(T)) {
916 S += '*';
917 return;
918 }
919 }
920
921 S += '^';
922 getObjcEncodingForType(PT->getPointeeType(), S);
923 } else if (const ArrayType *AT = Ty->getAsArrayType()) {
924 S += '[';
925
926 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT))
927 S += llvm::utostr(CAT->getSize().getZExtValue());
928 else
929 assert(0 && "Unhandled array type!");
930
931 getObjcEncodingForType(AT->getElementType(), S);
932 S += ']';
933 } else
934 fprintf(stderr, "@encode for type %s not implemented!\n",
935 Ty.getAsString().c_str());
936}
937
Anders Carlsson87c149b2007-10-11 01:00:40 +0000938void ASTContext::setBuiltinVaListType(QualType T)
939{
940 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
941
942 BuiltinVaListType = T;
943}
944
Steve Naroff66e9f332007-10-15 14:41:52 +0000945void ASTContext::setObjcIdType(TypedefDecl *TD)
946{
947 assert(ObjcIdType.isNull() && "'id' type already set!");
948
949 ObjcIdType = getTypedefType(TD);
950
951 // typedef struct objc_object *id;
952 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
953 assert(ptr && "'id' incorrectly typed");
954 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
955 assert(rec && "'id' incorrectly typed");
956 IdStructType = rec;
957}
958
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000959void ASTContext::setObjcSelType(TypedefDecl *TD)
960{
961 assert(ObjcSelType.isNull() && "'SEL' type already set!");
962
963 ObjcSelType = getTypedefType(TD);
964
965 // typedef struct objc_selector *SEL;
966 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
967 assert(ptr && "'SEL' incorrectly typed");
968 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
969 assert(rec && "'SEL' incorrectly typed");
970 SelStructType = rec;
971}
972
Fariborz Jahaniana32aaef2007-10-17 16:58:11 +0000973void ASTContext::setObjcProtoType(TypedefDecl *TD)
974{
975 assert(ObjcProtoType.isNull() && "'Protocol' type already set!");
976
977 // typedef struct Protocol Protocol;
978 ObjcProtoType = TD->getUnderlyingType();
979 // Protocol * type
980 ObjcProtoType = getPointerType(ObjcProtoType);
981 ProtoStructType = TD->getUnderlyingType()->getAsStructureType();
982}
983
Steve Narofff73b7842007-10-15 23:35:17 +0000984void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) {
985 assert(ObjcConstantStringType.isNull() &&
986 "'NSConstantString' type already set!");
987
988 ObjcConstantStringType = getObjcInterfaceType(Decl);
989}
990
Steve Naroff32e44c02007-10-15 20:41:53 +0000991bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
992 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
993 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
994
995 return lBuiltin->getKind() == rBuiltin->getKind();
996}
997
998
999bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
1000 if (lhs->isObjcInterfaceType() && isObjcIdType(rhs))
1001 return true;
1002 else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType())
1003 return true;
1004 return false;
1005}
1006
1007bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
1008 return true; // FIXME: IMPLEMENT.
1009}
1010
1011// C99 6.2.7p1: If both are complete types, then the following additional
1012// requirements apply...FIXME (handle compatibility across source files).
1013bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
1014 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
1015 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
1016
1017 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
1018 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1019 return true;
1020 }
1021 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
1022 if (ldecl->getIdentifier() == rdecl->getIdentifier())
1023 return true;
1024 }
1025 return false;
1026}
1027
1028bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
1029 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
1030 // identically qualified and both shall be pointers to compatible types.
1031 if (lhs.getQualifiers() != rhs.getQualifiers())
1032 return false;
1033
1034 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
1035 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
1036
1037 return typesAreCompatible(ltype, rtype);
1038}
1039
1040// C++ 5.17p6: When the left opperand of an assignment operator denotes a
1041// reference to T, the operation assigns to the object of type T denoted by the
1042// reference.
1043bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
1044 QualType ltype = lhs;
1045
1046 if (lhs->isReferenceType())
1047 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
1048
1049 QualType rtype = rhs;
1050
1051 if (rhs->isReferenceType())
1052 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
1053
1054 return typesAreCompatible(ltype, rtype);
1055}
1056
1057bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
1058 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
1059 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
1060 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
1061 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
1062
1063 // first check the return types (common between C99 and K&R).
1064 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
1065 return false;
1066
1067 if (lproto && rproto) { // two C99 style function prototypes
1068 unsigned lproto_nargs = lproto->getNumArgs();
1069 unsigned rproto_nargs = rproto->getNumArgs();
1070
1071 if (lproto_nargs != rproto_nargs)
1072 return false;
1073
1074 // both prototypes have the same number of arguments.
1075 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
1076 (rproto->isVariadic() && !lproto->isVariadic()))
1077 return false;
1078
1079 // The use of ellipsis agree...now check the argument types.
1080 for (unsigned i = 0; i < lproto_nargs; i++)
1081 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
1082 return false;
1083 return true;
1084 }
1085 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
1086 return true;
1087
1088 // we have a mixture of K&R style with C99 prototypes
1089 const FunctionTypeProto *proto = lproto ? lproto : rproto;
1090
1091 if (proto->isVariadic())
1092 return false;
1093
1094 // FIXME: Each parameter type T in the prototype must be compatible with the
1095 // type resulting from applying the usual argument conversions to T.
1096 return true;
1097}
1098
1099bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
1100 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
1101 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
1102
1103 if (!typesAreCompatible(ltype, rtype))
1104 return false;
1105
1106 // FIXME: If both types specify constant sizes, then the sizes must also be
1107 // the same. Even if the sizes are the same, GCC produces an error.
1108 return true;
1109}
1110
1111/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1112/// both shall have the identically qualified version of a compatible type.
1113/// C99 6.2.7p1: Two types have compatible types if their types are the
1114/// same. See 6.7.[2,3,5] for additional rules.
1115bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
1116 QualType lcanon = lhs.getCanonicalType();
1117 QualType rcanon = rhs.getCanonicalType();
1118
1119 // If two types are identical, they are are compatible
1120 if (lcanon == rcanon)
1121 return true;
1122
1123 // If the canonical type classes don't match, they can't be compatible
1124 if (lcanon->getTypeClass() != rcanon->getTypeClass()) {
1125 // For Objective-C, it is possible for two types to be compatible
1126 // when their classes don't match (when dealing with "id"). If either type
1127 // is an interface, we defer to objcTypesAreCompatible().
1128 if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType())
1129 return objcTypesAreCompatible(lcanon, rcanon);
1130 return false;
1131 }
1132 switch (lcanon->getTypeClass()) {
1133 case Type::Pointer:
1134 return pointerTypesAreCompatible(lcanon, rcanon);
1135 case Type::Reference:
1136 return referenceTypesAreCompatible(lcanon, rcanon);
1137 case Type::ConstantArray:
1138 case Type::VariableArray:
1139 return arrayTypesAreCompatible(lcanon, rcanon);
1140 case Type::FunctionNoProto:
1141 case Type::FunctionProto:
1142 return functionTypesAreCompatible(lcanon, rcanon);
1143 case Type::Tagged: // handle structures, unions
1144 return tagTypesAreCompatible(lcanon, rcanon);
1145 case Type::Builtin:
1146 return builtinTypesAreCompatible(lcanon, rcanon);
1147 case Type::ObjcInterface:
1148 return interfaceTypesAreCompatible(lcanon, rcanon);
1149 default:
1150 assert(0 && "unexpected type");
1151 }
1152 return true; // should never get here...
1153}