blob: 4c06e7c88cb4fca586e963a6e3ce4341894497fb [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"
Chris Lattnerddc135e2006-11-10 06:34:16 +000019using namespace clang;
20
Steve Naroff0af91202007-04-27 21:51:21 +000021enum FloatingRank {
22 FloatRank, DoubleRank, LongDoubleRank
23};
24
Chris Lattnerd5973eb2006-11-12 00:53:46 +000025ASTContext::~ASTContext() {
26 // Deallocate all the types.
27 while (!Types.empty()) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +000028 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 }
Chris Lattnerd5973eb2006-11-12 00:53:46 +000035 Types.pop_back();
36 }
37}
38
Chris Lattner4eb445d2007-01-26 01:27:23 +000039void 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 Lattner9c016772007-07-18 05:50:59 +000043 unsigned NumVector = 0, NumComplex = 0;
Bill Wendling3708c182007-05-27 10:15:43 +000044 unsigned NumFunctionNP = 0, NumTypeName = 0, NumTagged = 0, NumReference = 0;
Chris Lattner4eb445d2007-01-26 01:27:23 +000045
46 unsigned NumTagStruct = 0, NumTagUnion = 0, NumTagEnum = 0, NumTagClass = 0;
Steve Naroff73d534a2007-09-17 14:16:13 +000047 unsigned NumObjcInterfaces = 0;
Chris Lattner4eb445d2007-01-26 01:27:23 +000048
49 for (unsigned i = 0, e = Types.size(); i != e; ++i) {
50 Type *T = Types[i];
51 if (isa<BuiltinType>(T))
52 ++NumBuiltin;
53 else if (isa<PointerType>(T))
54 ++NumPointer;
Bill Wendling3708c182007-05-27 10:15:43 +000055 else if (isa<ReferenceType>(T))
56 ++NumReference;
Chris Lattner9c016772007-07-18 05:50:59 +000057 else if (isa<ComplexType>(T))
58 ++NumComplex;
Chris Lattner4eb445d2007-01-26 01:27:23 +000059 else if (isa<ArrayType>(T))
60 ++NumArray;
Chris Lattner9c016772007-07-18 05:50:59 +000061 else if (isa<VectorType>(T))
62 ++NumVector;
Chris Lattner4eb445d2007-01-26 01:27:23 +000063 else if (isa<FunctionTypeNoProto>(T))
64 ++NumFunctionNP;
65 else if (isa<FunctionTypeProto>(T))
66 ++NumFunctionP;
Chris Lattner32d920b2007-01-26 02:01:53 +000067 else if (isa<TypedefType>(T))
Chris Lattner4eb445d2007-01-26 01:27:23 +000068 ++NumTypeName;
Steve Narofff1e53692007-03-23 22:27:02 +000069 else if (TagType *TT = dyn_cast<TagType>(T)) {
Chris Lattner4eb445d2007-01-26 01:27:23 +000070 ++NumTagged;
71 switch (TT->getDecl()->getKind()) {
72 default: assert(0 && "Unknown tagged type!");
73 case Decl::Struct: ++NumTagStruct; break;
74 case Decl::Union: ++NumTagUnion; break;
75 case Decl::Class: ++NumTagClass; break;
76 case Decl::Enum: ++NumTagEnum; break;
77 }
Steve Naroff73d534a2007-09-17 14:16:13 +000078 } else if (isa<ObjcInterfaceType>(T))
79 ++NumObjcInterfaces;
80 else {
Chris Lattner4eb445d2007-01-26 01:27:23 +000081 assert(0 && "Unknown type!");
82 }
83 }
84
85 fprintf(stderr, " %d builtin types\n", NumBuiltin);
86 fprintf(stderr, " %d pointer types\n", NumPointer);
Bill Wendling3708c182007-05-27 10:15:43 +000087 fprintf(stderr, " %d reference types\n", NumReference);
Chris Lattner9c016772007-07-18 05:50:59 +000088 fprintf(stderr, " %d complex types\n", NumComplex);
Chris Lattner4eb445d2007-01-26 01:27:23 +000089 fprintf(stderr, " %d array types\n", NumArray);
Chris Lattner9c016772007-07-18 05:50:59 +000090 fprintf(stderr, " %d vector types\n", NumVector);
Chris Lattner4eb445d2007-01-26 01:27:23 +000091 fprintf(stderr, " %d function types with proto\n", NumFunctionP);
92 fprintf(stderr, " %d function types with no proto\n", NumFunctionNP);
93 fprintf(stderr, " %d typename (typedef) types\n", NumTypeName);
94 fprintf(stderr, " %d tagged types\n", NumTagged);
95 fprintf(stderr, " %d struct types\n", NumTagStruct);
96 fprintf(stderr, " %d union types\n", NumTagUnion);
97 fprintf(stderr, " %d class types\n", NumTagClass);
98 fprintf(stderr, " %d enum types\n", NumTagEnum);
Steve Naroff73d534a2007-09-17 14:16:13 +000099 fprintf(stderr, " %d interface types\n", NumObjcInterfaces);
Chris Lattnerfc234de2007-05-24 00:40:54 +0000100 fprintf(stderr, "Total bytes = %d\n", int(NumBuiltin*sizeof(BuiltinType)+
Steve Narofff84d11f2007-05-23 21:48:04 +0000101 NumPointer*sizeof(PointerType)+NumArray*sizeof(ArrayType)+
Chris Lattner9c016772007-07-18 05:50:59 +0000102 NumComplex*sizeof(ComplexType)+NumVector*sizeof(VectorType)+
Chris Lattnerfc234de2007-05-24 00:40:54 +0000103 NumFunctionP*sizeof(FunctionTypeProto)+
104 NumFunctionNP*sizeof(FunctionTypeNoProto)+
105 NumTypeName*sizeof(TypedefType)+NumTagged*sizeof(TagType)));
Chris Lattner4eb445d2007-01-26 01:27:23 +0000106}
107
108
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000109void ASTContext::InitBuiltinType(QualType &R, BuiltinType::Kind K) {
110 Types.push_back((R = QualType(new BuiltinType(K),0)).getTypePtr());
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000111}
112
Chris Lattner970e54e2006-11-12 00:37:36 +0000113void ASTContext::InitBuiltinTypes() {
114 assert(VoidTy.isNull() && "Context reinitialized?");
115
116 // C99 6.2.5p19.
Chris Lattner726f97b2006-12-03 02:57:32 +0000117 InitBuiltinType(VoidTy, BuiltinType::Void);
Chris Lattner970e54e2006-11-12 00:37:36 +0000118
119 // C99 6.2.5p2.
Chris Lattner726f97b2006-12-03 02:57:32 +0000120 InitBuiltinType(BoolTy, BuiltinType::Bool);
Chris Lattner970e54e2006-11-12 00:37:36 +0000121 // C99 6.2.5p3.
Chris Lattnerb16f4552007-06-03 07:25:34 +0000122 if (Target.isCharSigned(SourceLocation()))
123 InitBuiltinType(CharTy, BuiltinType::Char_S);
124 else
125 InitBuiltinType(CharTy, BuiltinType::Char_U);
Chris Lattner970e54e2006-11-12 00:37:36 +0000126 // C99 6.2.5p4.
Chris Lattner726f97b2006-12-03 02:57:32 +0000127 InitBuiltinType(SignedCharTy, BuiltinType::SChar);
128 InitBuiltinType(ShortTy, BuiltinType::Short);
129 InitBuiltinType(IntTy, BuiltinType::Int);
130 InitBuiltinType(LongTy, BuiltinType::Long);
131 InitBuiltinType(LongLongTy, BuiltinType::LongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000132
133 // C99 6.2.5p6.
Chris Lattner726f97b2006-12-03 02:57:32 +0000134 InitBuiltinType(UnsignedCharTy, BuiltinType::UChar);
135 InitBuiltinType(UnsignedShortTy, BuiltinType::UShort);
136 InitBuiltinType(UnsignedIntTy, BuiltinType::UInt);
137 InitBuiltinType(UnsignedLongTy, BuiltinType::ULong);
138 InitBuiltinType(UnsignedLongLongTy, BuiltinType::ULongLong);
Chris Lattner970e54e2006-11-12 00:37:36 +0000139
140 // C99 6.2.5p10.
Chris Lattner726f97b2006-12-03 02:57:32 +0000141 InitBuiltinType(FloatTy, BuiltinType::Float);
142 InitBuiltinType(DoubleTy, BuiltinType::Double);
143 InitBuiltinType(LongDoubleTy, BuiltinType::LongDouble);
Chris Lattner970e54e2006-11-12 00:37:36 +0000144
145 // C99 6.2.5p11.
Chris Lattnerc6395932007-06-22 20:56:16 +0000146 FloatComplexTy = getComplexType(FloatTy);
147 DoubleComplexTy = getComplexType(DoubleTy);
148 LongDoubleComplexTy = getComplexType(LongDoubleTy);
Steve Naroff66e9f332007-10-15 14:41:52 +0000149
150 BuiltinVaListType = QualType();
151 ObjcIdType = QualType();
152 IdStructType = 0;
Steve Narofff73b7842007-10-15 23:35:17 +0000153 ObjcConstantStringType = QualType();
Chris Lattner970e54e2006-11-12 00:37:36 +0000154}
155
Chris Lattner53cfe802007-07-18 17:52:12 +0000156//===----------------------------------------------------------------------===//
157// Type Sizing and Analysis
158//===----------------------------------------------------------------------===//
Chris Lattner983a8bb2007-07-13 22:13:22 +0000159
160/// getTypeSize - Return the size of the specified type, in bits. This method
161/// does not work on incomplete types.
Chris Lattner4481b422007-07-14 01:29:45 +0000162std::pair<uint64_t, unsigned>
163ASTContext::getTypeInfo(QualType T, SourceLocation L) {
Chris Lattner983a8bb2007-07-13 22:13:22 +0000164 T = T.getCanonicalType();
Chris Lattner4481b422007-07-14 01:29:45 +0000165 uint64_t Size;
166 unsigned Align;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000167 switch (T->getTypeClass()) {
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000168 case Type::TypeName: assert(0 && "Not a canonical type!");
Chris Lattner355332d2007-07-13 22:27:08 +0000169 case Type::FunctionNoProto:
170 case Type::FunctionProto:
Chris Lattner647fb222007-07-18 18:26:58 +0000171 default:
Chris Lattnerab41b132007-07-20 18:13:33 +0000172 assert(0 && "Incomplete types have no size!");
Steve Naroff5c131802007-08-30 01:06:46 +0000173 case Type::VariableArray:
174 assert(0 && "VLAs not implemented yet!");
175 case Type::ConstantArray: {
176 ConstantArrayType *CAT = cast<ConstantArrayType>(T);
177
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000178 std::pair<uint64_t, unsigned> EltInfo =
Steve Naroff5c131802007-08-30 01:06:46 +0000179 getTypeInfo(CAT->getElementType(), L);
180 Size = EltInfo.first*CAT->getSize().getZExtValue();
Chris Lattnerf2e101f2007-07-19 22:06:24 +0000181 Align = EltInfo.second;
182 break;
183 }
184 case Type::Vector: {
185 std::pair<uint64_t, unsigned> EltInfo =
186 getTypeInfo(cast<VectorType>(T)->getElementType(), L);
187 Size = EltInfo.first*cast<VectorType>(T)->getNumElements();
188 // FIXME: Vector alignment is not the alignment of its elements.
189 Align = EltInfo.second;
190 break;
191 }
Chris Lattner647fb222007-07-18 18:26:58 +0000192
Chris Lattner983a8bb2007-07-13 22:13:22 +0000193 case Type::Builtin: {
194 // FIXME: need to use TargetInfo to derive the target specific sizes. This
195 // implementation will suffice for play with vector support.
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000196 const llvm::fltSemantics *F;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000197 switch (cast<BuiltinType>(T)->getKind()) {
Chris Lattner355332d2007-07-13 22:27:08 +0000198 default: assert(0 && "Unknown builtin type!");
Chris Lattner4481b422007-07-14 01:29:45 +0000199 case BuiltinType::Void:
200 assert(0 && "Incomplete types have no size!");
201 case BuiltinType::Bool: Target.getBoolInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000202 case BuiltinType::Char_S:
203 case BuiltinType::Char_U:
204 case BuiltinType::UChar:
Chris Lattner4481b422007-07-14 01:29:45 +0000205 case BuiltinType::SChar: Target.getCharInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000206 case BuiltinType::UShort:
Chris Lattner4481b422007-07-14 01:29:45 +0000207 case BuiltinType::Short: Target.getShortInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000208 case BuiltinType::UInt:
Chris Lattner4481b422007-07-14 01:29:45 +0000209 case BuiltinType::Int: Target.getIntInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000210 case BuiltinType::ULong:
Chris Lattner4481b422007-07-14 01:29:45 +0000211 case BuiltinType::Long: Target.getLongInfo(Size, Align, L); break;
Chris Lattner355332d2007-07-13 22:27:08 +0000212 case BuiltinType::ULongLong:
Chris Lattner4481b422007-07-14 01:29:45 +0000213 case BuiltinType::LongLong: Target.getLongLongInfo(Size, Align, L); break;
Chris Lattnerec0a6d92007-09-22 18:29:59 +0000214 case BuiltinType::Float: Target.getFloatInfo(Size, Align, F, L); break;
215 case BuiltinType::Double: Target.getDoubleInfo(Size, Align, F, L);break;
216 case BuiltinType::LongDouble:Target.getLongDoubleInfo(Size,Align,F,L);break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000217 }
Chris Lattner48f84b82007-07-15 23:46:53 +0000218 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000219 }
Chris Lattner4481b422007-07-14 01:29:45 +0000220 case Type::Pointer: Target.getPointerInfo(Size, Align, L); break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000221 case Type::Reference:
Chris Lattner0523dc92007-07-13 22:16:13 +0000222 // "When applied to a reference or a reference type, the result is the size
Chris Lattner647fb222007-07-18 18:26:58 +0000223 // of the referenced type." C++98 5.3.3p2: expr.sizeof.
224 // FIXME: This is wrong for struct layout!
Chris Lattner4481b422007-07-14 01:29:45 +0000225 return getTypeInfo(cast<ReferenceType>(T)->getReferenceeType(), L);
Chris Lattner647fb222007-07-18 18:26:58 +0000226
227 case Type::Complex: {
228 // Complex types have the same alignment as their elements, but twice the
229 // size.
230 std::pair<uint64_t, unsigned> EltInfo =
231 getTypeInfo(cast<ComplexType>(T)->getElementType(), L);
232 Size = EltInfo.first*2;
233 Align = EltInfo.second;
234 break;
235 }
236 case Type::Tagged:
Chris Lattnera9e79612007-08-27 17:38:00 +0000237 TagType *TT = cast<TagType>(T);
238 if (RecordType *RT = dyn_cast<RecordType>(TT)) {
239 const RecordLayout &Layout = getRecordLayout(RT->getDecl(), L);
240 Size = Layout.getSize();
241 Align = Layout.getAlignment();
242 } else if (EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl())) {
Chris Lattner1c1f9322007-08-28 18:24:31 +0000243 return getTypeInfo(ED->getIntegerType(), L);
Chris Lattnera9e79612007-08-27 17:38:00 +0000244 } else {
Chris Lattner49a953a2007-07-23 22:46:22 +0000245 assert(0 && "Unimplemented type sizes!");
Chris Lattnera9e79612007-08-27 17:38:00 +0000246 }
Chris Lattner49a953a2007-07-23 22:46:22 +0000247 break;
Chris Lattner983a8bb2007-07-13 22:13:22 +0000248 }
Chris Lattner4481b422007-07-14 01:29:45 +0000249
Chris Lattner53cfe802007-07-18 17:52:12 +0000250 assert(Align && (Align & (Align-1)) == 0 && "Alignment must be power of 2");
Chris Lattner4481b422007-07-14 01:29:45 +0000251 return std::make_pair(Size, Align);
Chris Lattner983a8bb2007-07-13 22:13:22 +0000252}
253
Chris Lattner53cfe802007-07-18 17:52:12 +0000254/// getRecordLayout - Get or compute information about the layout of the
255/// specified record (struct/union/class), which indicates its size and field
256/// position information.
257const RecordLayout &ASTContext::getRecordLayout(const RecordDecl *D,
258 SourceLocation L) {
259 assert(D->isDefinition() && "Cannot get layout of forward declarations!");
260
261 // Look up this layout, if already laid out, return what we have.
262 const RecordLayout *&Entry = RecordLayoutInfo[D];
263 if (Entry) return *Entry;
264
265 // Allocate and assign into RecordLayoutInfo here. The "Entry" reference can
266 // be invalidated (dangle) if the RecordLayoutInfo hashtable is inserted into.
267 RecordLayout *NewEntry = new RecordLayout();
268 Entry = NewEntry;
269
270 uint64_t *FieldOffsets = new uint64_t[D->getNumMembers()];
271 uint64_t RecordSize = 0;
272 unsigned RecordAlign = 8; // Default alignment = 1 byte = 8 bits.
273
274 if (D->getKind() != Decl::Union) {
275 // Layout each field, for now, just sequentially, respecting alignment. In
276 // the future, this will need to be tweakable by targets.
277 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
278 const FieldDecl *FD = D->getMember(i);
279 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
280 uint64_t FieldSize = FieldInfo.first;
281 unsigned FieldAlign = FieldInfo.second;
282
283 // Round up the current record size to the field's alignment boundary.
284 RecordSize = (RecordSize+FieldAlign-1) & ~(FieldAlign-1);
285
286 // Place this field at the current location.
287 FieldOffsets[i] = RecordSize;
288
289 // Reserve space for this field.
290 RecordSize += FieldSize;
291
292 // Remember max struct/class alignment.
293 RecordAlign = std::max(RecordAlign, FieldAlign);
294 }
295
296 // Finally, round the size of the total struct up to the alignment of the
297 // struct itself.
298 RecordSize = (RecordSize+RecordAlign-1) & ~(RecordAlign-1);
299 } else {
300 // Union layout just puts each member at the start of the record.
301 for (unsigned i = 0, e = D->getNumMembers(); i != e; ++i) {
302 const FieldDecl *FD = D->getMember(i);
303 std::pair<uint64_t, unsigned> FieldInfo = getTypeInfo(FD->getType(), L);
304 uint64_t FieldSize = FieldInfo.first;
305 unsigned FieldAlign = FieldInfo.second;
306
307 // Round up the current record size to the field's alignment boundary.
308 RecordSize = std::max(RecordSize, FieldSize);
309
310 // Place this field at the start of the record.
311 FieldOffsets[i] = 0;
312
313 // Remember max struct/class alignment.
314 RecordAlign = std::max(RecordAlign, FieldAlign);
315 }
316 }
Chris Lattner647fb222007-07-18 18:26:58 +0000317
318 NewEntry->SetLayout(RecordSize, RecordAlign, FieldOffsets);
319 return *NewEntry;
Chris Lattner53cfe802007-07-18 17:52:12 +0000320}
321
Chris Lattner983a8bb2007-07-13 22:13:22 +0000322//===----------------------------------------------------------------------===//
323// Type creation/memoization methods
324//===----------------------------------------------------------------------===//
325
326
Chris Lattnerc6395932007-06-22 20:56:16 +0000327/// getComplexType - Return the uniqued reference to the type for a complex
328/// number with the specified element type.
329QualType ASTContext::getComplexType(QualType T) {
330 // Unique pointers, to guarantee there is only one pointer of a particular
331 // structure.
332 llvm::FoldingSetNodeID ID;
333 ComplexType::Profile(ID, T);
334
335 void *InsertPos = 0;
336 if (ComplexType *CT = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos))
337 return QualType(CT, 0);
338
339 // If the pointee type isn't canonical, this won't be a canonical type either,
340 // so fill in the canonical type field.
341 QualType Canonical;
342 if (!T->isCanonical()) {
343 Canonical = getComplexType(T.getCanonicalType());
344
345 // Get the new insert position for the node we care about.
346 ComplexType *NewIP = ComplexTypes.FindNodeOrInsertPos(ID, InsertPos);
347 assert(NewIP == 0 && "Shouldn't be in the map!");
348 }
349 ComplexType *New = new ComplexType(T, Canonical);
350 Types.push_back(New);
351 ComplexTypes.InsertNode(New, InsertPos);
352 return QualType(New, 0);
353}
354
355
Chris Lattner970e54e2006-11-12 00:37:36 +0000356/// getPointerType - Return the uniqued reference to the type for a pointer to
357/// the specified type.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000358QualType ASTContext::getPointerType(QualType T) {
Chris Lattnerd5973eb2006-11-12 00:53:46 +0000359 // Unique pointers, to guarantee there is only one pointer of a particular
360 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000361 llvm::FoldingSetNodeID ID;
Chris Lattner67521df2007-01-27 01:29:36 +0000362 PointerType::Profile(ID, T);
363
364 void *InsertPos = 0;
365 if (PointerType *PT = PointerTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000366 return QualType(PT, 0);
Chris Lattner970e54e2006-11-12 00:37:36 +0000367
Chris Lattner7ccecb92006-11-12 08:50:50 +0000368 // If the pointee type isn't canonical, this won't be a canonical type either,
369 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000370 QualType Canonical;
Chris Lattner67521df2007-01-27 01:29:36 +0000371 if (!T->isCanonical()) {
Steve Naroffd50c88e2007-04-05 21:15:20 +0000372 Canonical = getPointerType(T.getCanonicalType());
Chris Lattner67521df2007-01-27 01:29:36 +0000373
374 // Get the new insert position for the node we care about.
375 PointerType *NewIP = PointerTypes.FindNodeOrInsertPos(ID, InsertPos);
376 assert(NewIP == 0 && "Shouldn't be in the map!");
377 }
Chris Lattner67521df2007-01-27 01:29:36 +0000378 PointerType *New = new PointerType(T, Canonical);
379 Types.push_back(New);
380 PointerTypes.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000381 return QualType(New, 0);
Chris Lattnerddc135e2006-11-10 06:34:16 +0000382}
383
Bill Wendling3708c182007-05-27 10:15:43 +0000384/// getReferenceType - Return the uniqued reference to the type for a reference
385/// to the specified type.
386QualType ASTContext::getReferenceType(QualType T) {
387 // Unique pointers, to guarantee there is only one pointer of a particular
388 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000389 llvm::FoldingSetNodeID ID;
Bill Wendling3708c182007-05-27 10:15:43 +0000390 ReferenceType::Profile(ID, T);
391
392 void *InsertPos = 0;
393 if (ReferenceType *RT = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos))
394 return QualType(RT, 0);
395
396 // If the referencee type isn't canonical, this won't be a canonical type
397 // either, so fill in the canonical type field.
398 QualType Canonical;
399 if (!T->isCanonical()) {
400 Canonical = getReferenceType(T.getCanonicalType());
401
402 // Get the new insert position for the node we care about.
403 ReferenceType *NewIP = ReferenceTypes.FindNodeOrInsertPos(ID, InsertPos);
404 assert(NewIP == 0 && "Shouldn't be in the map!");
405 }
406
407 ReferenceType *New = new ReferenceType(T, Canonical);
408 Types.push_back(New);
409 ReferenceTypes.InsertNode(New, InsertPos);
410 return QualType(New, 0);
411}
412
Steve Naroff5c131802007-08-30 01:06:46 +0000413/// getConstantArrayType - Return the unique reference to the type for an
414/// array of the specified element type.
415QualType ASTContext::getConstantArrayType(QualType EltTy,
Steve Naroff90dfdd52007-08-30 18:10:14 +0000416 const llvm::APInt &ArySize,
417 ArrayType::ArraySizeModifier ASM,
418 unsigned EltTypeQuals) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000419 llvm::FoldingSetNodeID ID;
Steve Naroff5c131802007-08-30 01:06:46 +0000420 ConstantArrayType::Profile(ID, EltTy, ArySize);
Steve Naroff408451b2007-02-26 22:17:12 +0000421
Chris Lattner36f8e652007-01-27 08:31:04 +0000422 void *InsertPos = 0;
Steve Naroff5c131802007-08-30 01:06:46 +0000423 if (ConstantArrayType *ATP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000424 return QualType(ATP, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000425
426 // If the element type isn't canonical, this won't be a canonical type either,
427 // so fill in the canonical type field.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000428 QualType Canonical;
Chris Lattner36f8e652007-01-27 08:31:04 +0000429 if (!EltTy->isCanonical()) {
Steve Naroff90dfdd52007-08-30 18:10:14 +0000430 Canonical = getConstantArrayType(EltTy.getCanonicalType(), ArySize,
431 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +0000432 // Get the new insert position for the node we care about.
Steve Naroff5c131802007-08-30 01:06:46 +0000433 ConstantArrayType *NewIP = ArrayTypes.FindNodeOrInsertPos(ID, InsertPos);
Chris Lattner36f8e652007-01-27 08:31:04 +0000434 assert(NewIP == 0 && "Shouldn't be in the map!");
435 }
Chris Lattner7ccecb92006-11-12 08:50:50 +0000436
Steve Naroff90dfdd52007-08-30 18:10:14 +0000437 ConstantArrayType *New = new ConstantArrayType(EltTy, Canonical, ArySize,
438 ASM, EltTypeQuals);
Chris Lattner36f8e652007-01-27 08:31:04 +0000439 ArrayTypes.InsertNode(New, InsertPos);
440 Types.push_back(New);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000441 return QualType(New, 0);
Chris Lattner7ccecb92006-11-12 08:50:50 +0000442}
443
Steve Naroffcadebd02007-08-30 18:14:25 +0000444/// getVariableArrayType - Returns a non-unique reference to the type for a
445/// variable array of the specified element type.
Steve Naroff90dfdd52007-08-30 18:10:14 +0000446QualType ASTContext::getVariableArrayType(QualType EltTy, Expr *NumElts,
447 ArrayType::ArraySizeModifier ASM,
448 unsigned EltTypeQuals) {
449 // Since we don't unique expressions, it isn't possible to unique VLA's.
450 ArrayType *New = new VariableArrayType(EltTy, QualType(), NumElts,
451 ASM, EltTypeQuals);
452 Types.push_back(New);
453 return QualType(New, 0);
Steve Naroff5c131802007-08-30 01:06:46 +0000454}
455
Steve Naroff91fcddb2007-07-18 18:00:27 +0000456/// getVectorType - Return the unique reference to a vector type of
457/// the specified element type and size. VectorType must be a built-in type.
458QualType ASTContext::getVectorType(QualType vecType, unsigned NumElts) {
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000459 BuiltinType *baseType;
460
461 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
Steve Naroff91fcddb2007-07-18 18:00:27 +0000462 assert(baseType != 0 && "getVectorType(): Expecting a built-in type");
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000463
464 // Check if we've already instantiated a vector of this type.
465 llvm::FoldingSetNodeID ID;
Steve Naroff91fcddb2007-07-18 18:00:27 +0000466 VectorType::Profile(ID, vecType, NumElts, Type::Vector);
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000467 void *InsertPos = 0;
468 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
469 return QualType(VTP, 0);
470
471 // If the element type isn't canonical, this won't be a canonical type either,
472 // so fill in the canonical type field.
473 QualType Canonical;
474 if (!vecType->isCanonical()) {
Steve Naroff91fcddb2007-07-18 18:00:27 +0000475 Canonical = getVectorType(vecType.getCanonicalType(), NumElts);
Steve Naroff4ae0ac62007-07-06 23:09:18 +0000476
477 // Get the new insert position for the node we care about.
478 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
479 assert(NewIP == 0 && "Shouldn't be in the map!");
480 }
481 VectorType *New = new VectorType(vecType, NumElts, Canonical);
482 VectorTypes.InsertNode(New, InsertPos);
483 Types.push_back(New);
484 return QualType(New, 0);
485}
486
Steve Naroff91fcddb2007-07-18 18:00:27 +0000487/// getOCUVectorType - Return the unique reference to an OCU vector type of
488/// the specified element type and size. VectorType must be a built-in type.
489QualType ASTContext::getOCUVectorType(QualType vecType, unsigned NumElts) {
490 BuiltinType *baseType;
491
492 baseType = dyn_cast<BuiltinType>(vecType.getCanonicalType().getTypePtr());
493 assert(baseType != 0 && "getOCUVectorType(): Expecting a built-in type");
494
495 // Check if we've already instantiated a vector of this type.
496 llvm::FoldingSetNodeID ID;
497 VectorType::Profile(ID, vecType, NumElts, Type::OCUVector);
498 void *InsertPos = 0;
499 if (VectorType *VTP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos))
500 return QualType(VTP, 0);
501
502 // If the element type isn't canonical, this won't be a canonical type either,
503 // so fill in the canonical type field.
504 QualType Canonical;
505 if (!vecType->isCanonical()) {
506 Canonical = getOCUVectorType(vecType.getCanonicalType(), NumElts);
507
508 // Get the new insert position for the node we care about.
509 VectorType *NewIP = VectorTypes.FindNodeOrInsertPos(ID, InsertPos);
510 assert(NewIP == 0 && "Shouldn't be in the map!");
511 }
512 OCUVectorType *New = new OCUVectorType(vecType, NumElts, Canonical);
513 VectorTypes.InsertNode(New, InsertPos);
514 Types.push_back(New);
515 return QualType(New, 0);
516}
517
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000518/// getFunctionTypeNoProto - Return a K&R style C function type like 'int()'.
519///
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000520QualType ASTContext::getFunctionTypeNoProto(QualType ResultTy) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000521 // Unique functions, to guarantee there is only one function of a particular
522 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000523 llvm::FoldingSetNodeID ID;
Chris Lattner47955de2007-01-27 08:37:20 +0000524 FunctionTypeNoProto::Profile(ID, ResultTy);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000525
Chris Lattner47955de2007-01-27 08:37:20 +0000526 void *InsertPos = 0;
527 if (FunctionTypeNoProto *FT =
528 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000529 return QualType(FT, 0);
Chris Lattner47955de2007-01-27 08:37:20 +0000530
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000531 QualType Canonical;
Chris Lattner47955de2007-01-27 08:37:20 +0000532 if (!ResultTy->isCanonical()) {
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000533 Canonical = getFunctionTypeNoProto(ResultTy.getCanonicalType());
Chris Lattner47955de2007-01-27 08:37:20 +0000534
535 // Get the new insert position for the node we care about.
536 FunctionTypeNoProto *NewIP =
537 FunctionTypeNoProtos.FindNodeOrInsertPos(ID, InsertPos);
538 assert(NewIP == 0 && "Shouldn't be in the map!");
539 }
540
541 FunctionTypeNoProto *New = new FunctionTypeNoProto(ResultTy, Canonical);
542 Types.push_back(New);
543 FunctionTypeProtos.InsertNode(New, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000544 return QualType(New, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000545}
546
547/// getFunctionType - Return a normal function type with a typed argument
548/// list. isVariadic indicates whether the argument list includes '...'.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000549QualType ASTContext::getFunctionType(QualType ResultTy, QualType *ArgArray,
550 unsigned NumArgs, bool isVariadic) {
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000551 // Unique functions, to guarantee there is only one function of a particular
552 // structure.
Chris Lattner23b7eb62007-06-15 23:05:46 +0000553 llvm::FoldingSetNodeID ID;
Chris Lattnerfd4de792007-01-27 01:15:32 +0000554 FunctionTypeProto::Profile(ID, ResultTy, ArgArray, NumArgs, isVariadic);
555
556 void *InsertPos = 0;
557 if (FunctionTypeProto *FTP =
558 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos))
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000559 return QualType(FTP, 0);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000560
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000561 // Determine whether the type being created is already canonical or not.
562 bool isCanonical = ResultTy->isCanonical();
563 for (unsigned i = 0; i != NumArgs && isCanonical; ++i)
564 if (!ArgArray[i]->isCanonical())
565 isCanonical = false;
566
567 // If this type isn't canonical, get the canonical version of it.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000568 QualType Canonical;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000569 if (!isCanonical) {
Chris Lattner23b7eb62007-06-15 23:05:46 +0000570 llvm::SmallVector<QualType, 16> CanonicalArgs;
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000571 CanonicalArgs.reserve(NumArgs);
572 for (unsigned i = 0; i != NumArgs; ++i)
573 CanonicalArgs.push_back(ArgArray[i].getCanonicalType());
574
575 Canonical = getFunctionType(ResultTy.getCanonicalType(),
576 &CanonicalArgs[0], NumArgs,
Steve Naroffd50c88e2007-04-05 21:15:20 +0000577 isVariadic);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000578
579 // Get the new insert position for the node we care about.
580 FunctionTypeProto *NewIP =
581 FunctionTypeProtos.FindNodeOrInsertPos(ID, InsertPos);
582 assert(NewIP == 0 && "Shouldn't be in the map!");
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000583 }
584
585 // FunctionTypeProto objects are not allocated with new because they have a
586 // variable size array (for parameter types) at the end of them.
587 FunctionTypeProto *FTP =
588 (FunctionTypeProto*)malloc(sizeof(FunctionTypeProto) +
Chris Lattner98021442007-07-20 18:48:28 +0000589 NumArgs*sizeof(QualType));
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000590 new (FTP) FunctionTypeProto(ResultTy, ArgArray, NumArgs, isVariadic,
591 Canonical);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000592 Types.push_back(FTP);
Chris Lattnerfd4de792007-01-27 01:15:32 +0000593 FunctionTypeProtos.InsertNode(FTP, InsertPos);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000594 return QualType(FTP, 0);
Chris Lattnerc6ad8132006-12-02 07:52:18 +0000595}
Chris Lattneref51c202006-11-10 07:17:23 +0000596
Chris Lattner32d920b2007-01-26 02:01:53 +0000597/// getTypedefType - Return the unique reference to the type for the
Chris Lattnerd0342e52006-11-20 04:02:15 +0000598/// specified typename decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000599QualType ASTContext::getTypedefType(TypedefDecl *Decl) {
600 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000601
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000602 QualType Canonical = Decl->getUnderlyingType().getCanonicalType();
Chris Lattnercceab1a2007-03-26 20:16:44 +0000603 Decl->TypeForDecl = new TypedefType(Decl, Canonical);
604 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000605 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerd0342e52006-11-20 04:02:15 +0000606}
607
Steve Naroff09bf8152007-09-06 21:24:23 +0000608/// getObjcInterfaceType - Return the unique reference to the type for the
609/// specified ObjC interface decl.
610QualType ASTContext::getObjcInterfaceType(ObjcInterfaceDecl *Decl) {
611 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
612
613 Decl->TypeForDecl = new ObjcInterfaceType(Decl);
614 Types.push_back(Decl->TypeForDecl);
615 return QualType(Decl->TypeForDecl, 0);
616}
617
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000618/// getObjcQualifiedInterfaceType - Return a
619/// ObjcQualifiedInterfaceType type for the given interface decl and
620/// the conforming protocol list.
621QualType ASTContext::getObjcQualifiedInterfaceType(ObjcInterfaceDecl *Decl,
622 ObjcProtocolDecl **Protocols, unsigned NumProtocols) {
623 ObjcInterfaceType *IType =
624 cast<ObjcInterfaceType>(getObjcInterfaceType(Decl));
625
626 llvm::FoldingSetNodeID ID;
627 ObjcQualifiedInterfaceType::Profile(ID, IType, Protocols, NumProtocols);
628
629 void *InsertPos = 0;
630 if (ObjcQualifiedInterfaceType *QT =
631 ObjcQualifiedInterfaceTypes.FindNodeOrInsertPos(ID, InsertPos))
632 return QualType(QT, 0);
633
634 // No Match;
Chris Lattnerf709a142007-10-11 03:36:41 +0000635 ObjcQualifiedInterfaceType *QType =
636 new ObjcQualifiedInterfaceType(IType, Protocols, NumProtocols);
Fariborz Jahanian70e8f102007-10-11 00:55:41 +0000637 Types.push_back(QType);
638 ObjcQualifiedInterfaceTypes.InsertNode(QType, InsertPos);
639 return QualType(QType, 0);
640}
641
Steve Naroffa773cd52007-08-01 18:02:17 +0000642/// getTypeOfExpr - Unlike many "get<Type>" functions, we can't unique
643/// TypeOfExpr AST's (since expression's are never shared). For example,
644/// multiple declarations that refer to "typeof(x)" all contain different
645/// DeclRefExpr's. This doesn't effect the type checker, since it operates
646/// on canonical type's (which are always unique).
Steve Naroff236becb2007-08-01 17:20:42 +0000647QualType ASTContext::getTypeOfExpr(Expr *tofExpr) {
Steve Naroffad373bd2007-07-31 12:34:36 +0000648 QualType Canonical = tofExpr->getType().getCanonicalType();
Steve Naroffa773cd52007-08-01 18:02:17 +0000649 TypeOfExpr *toe = new TypeOfExpr(tofExpr, Canonical);
650 Types.push_back(toe);
651 return QualType(toe, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +0000652}
653
Steve Naroffa773cd52007-08-01 18:02:17 +0000654/// getTypeOfType - Unlike many "get<Type>" functions, we don't unique
655/// TypeOfType AST's. The only motivation to unique these nodes would be
656/// memory savings. Since typeof(t) is fairly uncommon, space shouldn't be
657/// an issue. This doesn't effect the type checker, since it operates
658/// on canonical type's (which are always unique).
Steve Naroffad373bd2007-07-31 12:34:36 +0000659QualType ASTContext::getTypeOfType(QualType tofType) {
660 QualType Canonical = tofType.getCanonicalType();
Steve Naroffa773cd52007-08-01 18:02:17 +0000661 TypeOfType *tot = new TypeOfType(tofType, Canonical);
662 Types.push_back(tot);
663 return QualType(tot, 0);
Steve Naroffad373bd2007-07-31 12:34:36 +0000664}
665
Chris Lattnerfb072462007-01-23 05:45:31 +0000666/// getTagDeclType - Return the unique reference to the type for the
667/// specified TagDecl (struct/union/class/enum) decl.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000668QualType ASTContext::getTagDeclType(TagDecl *Decl) {
Chris Lattner733067d2007-01-26 01:42:24 +0000669 // The decl stores the type cache.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000670 if (Decl->TypeForDecl) return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000671
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000672 Decl->TypeForDecl = new TagType(Decl, QualType());
Chris Lattnercceab1a2007-03-26 20:16:44 +0000673 Types.push_back(Decl->TypeForDecl);
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000674 return QualType(Decl->TypeForDecl, 0);
Chris Lattnerfb072462007-01-23 05:45:31 +0000675}
676
Steve Naroff92e30f82007-04-02 22:35:25 +0000677/// getSizeType - Return the unique type for "size_t" (C99 7.17), the result
Steve Naroff0f6256d2007-04-02 23:01:44 +0000678/// of the sizeof operator (C99 6.5.3.4p4). The value is target dependent and
Steve Naroff92e30f82007-04-02 22:35:25 +0000679/// needs to agree with the definition in <stddef.h>.
Steve Naroffe5aa9be2007-04-05 22:36:20 +0000680QualType ASTContext::getSizeType() const {
Steve Naroff92e30f82007-04-02 22:35:25 +0000681 // On Darwin, size_t is defined as a "long unsigned int".
682 // FIXME: should derive from "Target".
683 return UnsignedLongTy;
684}
Chris Lattnerfb072462007-01-23 05:45:31 +0000685
Chris Lattnerd2b88ab2007-07-13 03:05:23 +0000686/// getPointerDiffType - Return the unique type for "ptrdiff_t" (ref?)
687/// defined in <stddef.h>. Pointer - pointer requires this (C99 6.5.6p9).
688QualType ASTContext::getPointerDiffType() const {
689 // On Darwin, ptrdiff_t is defined as a "int". This seems like a bug...
690 // FIXME: should derive from "Target".
691 return IntTy;
692}
693
Steve Naroff0af91202007-04-27 21:51:21 +0000694/// getIntegerRank - Return an integer conversion rank (C99 6.3.1.1p1). This
695/// routine will assert if passed a built-in type that isn't an integer or enum.
696static int getIntegerRank(QualType t) {
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000697 if (const TagType *TT = dyn_cast<TagType>(t.getCanonicalType())) {
698 assert(TT->getDecl()->getKind() == Decl::Enum && "not an int or enum");
699 return 4;
Steve Naroffe4718892007-04-27 18:30:00 +0000700 }
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000701
702 const BuiltinType *BT = cast<BuiltinType>(t.getCanonicalType());
703 switch (BT->getKind()) {
704 default:
705 assert(0 && "getIntegerRank(): not a built-in integer");
706 case BuiltinType::Bool:
707 return 1;
Chris Lattnerb16f4552007-06-03 07:25:34 +0000708 case BuiltinType::Char_S:
709 case BuiltinType::Char_U:
Chris Lattner4dc8a6f2007-05-20 23:50:58 +0000710 case BuiltinType::SChar:
711 case BuiltinType::UChar:
712 return 2;
713 case BuiltinType::Short:
714 case BuiltinType::UShort:
715 return 3;
716 case BuiltinType::Int:
717 case BuiltinType::UInt:
718 return 4;
719 case BuiltinType::Long:
720 case BuiltinType::ULong:
721 return 5;
722 case BuiltinType::LongLong:
723 case BuiltinType::ULongLong:
724 return 6;
725 }
Steve Naroffe4718892007-04-27 18:30:00 +0000726}
727
Steve Naroff0af91202007-04-27 21:51:21 +0000728/// getFloatingRank - Return a relative rank for floating point types.
729/// This routine will assert if passed a built-in type that isn't a float.
Chris Lattnerc6395932007-06-22 20:56:16 +0000730static int getFloatingRank(QualType T) {
731 T = T.getCanonicalType();
732 if (ComplexType *CT = dyn_cast<ComplexType>(T))
733 return getFloatingRank(CT->getElementType());
734
735 switch (cast<BuiltinType>(T)->getKind()) {
736 default: assert(0 && "getFloatingPointRank(): not a floating type");
737 case BuiltinType::Float: return FloatRank;
738 case BuiltinType::Double: return DoubleRank;
739 case BuiltinType::LongDouble: return LongDoubleRank;
Steve Naroffe4718892007-04-27 18:30:00 +0000740 }
741}
742
Steve Narofffc6ffa22007-08-27 01:41:48 +0000743/// getFloatingTypeOfSizeWithinDomain - Returns a real floating
744/// point or a complex type (based on typeDomain/typeSize).
745/// 'typeDomain' is a real floating point or complex type.
746/// 'typeSize' is a real floating point or complex type.
Steve Naroff9091ef72007-08-27 01:27:54 +0000747QualType ASTContext::getFloatingTypeOfSizeWithinDomain(
748 QualType typeSize, QualType typeDomain) const {
749 if (typeDomain->isComplexType()) {
750 switch (getFloatingRank(typeSize)) {
Steve Narofffc6ffa22007-08-27 01:41:48 +0000751 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +0000752 case FloatRank: return FloatComplexTy;
753 case DoubleRank: return DoubleComplexTy;
754 case LongDoubleRank: return LongDoubleComplexTy;
755 }
Steve Naroff0af91202007-04-27 21:51:21 +0000756 }
Steve Naroff9091ef72007-08-27 01:27:54 +0000757 if (typeDomain->isRealFloatingType()) {
758 switch (getFloatingRank(typeSize)) {
Steve Narofffc6ffa22007-08-27 01:41:48 +0000759 default: assert(0 && "getFloatingRank(): illegal value for rank");
Steve Naroff9091ef72007-08-27 01:27:54 +0000760 case FloatRank: return FloatTy;
761 case DoubleRank: return DoubleTy;
762 case LongDoubleRank: return LongDoubleTy;
763 }
764 }
765 assert(0 && "getFloatingTypeOfSizeWithinDomain(): illegal domain");
Chris Lattner793d10c2007-09-16 19:23:47 +0000766 //an invalid return value, but the assert
767 //will ensure that this code is never reached.
768 return VoidTy;
Steve Naroffe4718892007-04-27 18:30:00 +0000769}
770
Steve Naroff7af82d42007-08-27 15:30:22 +0000771/// compareFloatingType - Handles 3 different combos:
772/// float/float, float/complex, complex/complex.
773/// If lt > rt, return 1. If lt == rt, return 0. If lt < rt, return -1.
774int ASTContext::compareFloatingType(QualType lt, QualType rt) {
775 if (getFloatingRank(lt) == getFloatingRank(rt))
776 return 0;
777 if (getFloatingRank(lt) > getFloatingRank(rt))
778 return 1;
779 return -1;
Steve Naroffe4718892007-04-27 18:30:00 +0000780}
781
Steve Naroff0af91202007-04-27 21:51:21 +0000782// maxIntegerType - Returns the highest ranked integer type. Handles 3 case:
783// unsigned/unsigned, signed/signed, signed/unsigned. C99 6.3.1.8p1.
Steve Naroffe4718892007-04-27 18:30:00 +0000784QualType ASTContext::maxIntegerType(QualType lhs, QualType rhs) {
Chris Lattnerb16f4552007-06-03 07:25:34 +0000785 if (lhs == rhs) return lhs;
786
Steve Naroffe4718892007-04-27 18:30:00 +0000787 bool t1Unsigned = lhs->isUnsignedIntegerType();
788 bool t2Unsigned = rhs->isUnsignedIntegerType();
789
790 if ((t1Unsigned && t2Unsigned) || (!t1Unsigned && !t2Unsigned))
791 return getIntegerRank(lhs) >= getIntegerRank(rhs) ? lhs : rhs;
792
793 // We have two integer types with differing signs
794 QualType unsignedType = t1Unsigned ? lhs : rhs;
795 QualType signedType = t1Unsigned ? rhs : lhs;
796
797 if (getIntegerRank(unsignedType) >= getIntegerRank(signedType))
798 return unsignedType;
Steve Naroff0af91202007-04-27 21:51:21 +0000799 else {
800 // FIXME: Need to check if the signed type can represent all values of the
801 // unsigned type. If it can, then the result is the signed type.
802 // If it can't, then the result is the unsigned version of the signed type.
803 // Should probably add a helper that returns a signed integer type from
804 // an unsigned (and vice versa). C99 6.3.1.8.
805 return signedType;
806 }
Steve Naroffe4718892007-04-27 18:30:00 +0000807}
Anders Carlsson98f07902007-08-17 05:31:46 +0000808
809// getCFConstantStringType - Return the type used for constant CFStrings.
810QualType ASTContext::getCFConstantStringType() {
811 if (!CFConstantStringTypeDecl) {
812 CFConstantStringTypeDecl = new RecordDecl(Decl::Struct, SourceLocation(),
813 &Idents.get("__builtin_CFString"),
814 0);
815
816 QualType FieldTypes[4];
817
818 // const int *isa;
819 FieldTypes[0] = getPointerType(IntTy.getQualifiedType(QualType::Const));
820 // int flags;
821 FieldTypes[1] = IntTy;
822 // const char *str;
823 FieldTypes[2] = getPointerType(CharTy.getQualifiedType(QualType::Const));
824 // long length;
825 FieldTypes[3] = LongTy;
826 // Create fields
827 FieldDecl *FieldDecls[4];
828
829 for (unsigned i = 0; i < 4; ++i)
Steve Naroff1d4b5eae2007-09-14 02:20:46 +0000830 FieldDecls[i] = new FieldDecl(SourceLocation(), 0, FieldTypes[i]);
Anders Carlsson98f07902007-08-17 05:31:46 +0000831
832 CFConstantStringTypeDecl->defineBody(FieldDecls, 4);
833 }
834
835 return getTagDeclType(CFConstantStringTypeDecl);
Gabor Greif412af032007-09-11 15:32:40 +0000836}
Anders Carlsson87c149b2007-10-11 01:00:40 +0000837
838void ASTContext::setBuiltinVaListType(QualType T)
839{
840 assert(BuiltinVaListType.isNull() && "__builtin_va_list type already set!");
841
842 BuiltinVaListType = T;
843}
844
Steve Naroff66e9f332007-10-15 14:41:52 +0000845void ASTContext::setObjcIdType(TypedefDecl *TD)
846{
847 assert(ObjcIdType.isNull() && "'id' type already set!");
848
849 ObjcIdType = getTypedefType(TD);
850
851 // typedef struct objc_object *id;
852 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
853 assert(ptr && "'id' incorrectly typed");
854 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
855 assert(rec && "'id' incorrectly typed");
856 IdStructType = rec;
857}
858
Fariborz Jahanian4bef4622007-10-16 20:40:23 +0000859void ASTContext::setObjcSelType(TypedefDecl *TD)
860{
861 assert(ObjcSelType.isNull() && "'SEL' type already set!");
862
863 ObjcSelType = getTypedefType(TD);
864
865 // typedef struct objc_selector *SEL;
866 const PointerType *ptr = TD->getUnderlyingType()->getAsPointerType();
867 assert(ptr && "'SEL' incorrectly typed");
868 const RecordType *rec = ptr->getPointeeType()->getAsStructureType();
869 assert(rec && "'SEL' incorrectly typed");
870 SelStructType = rec;
871}
872
Steve Narofff73b7842007-10-15 23:35:17 +0000873void ASTContext::setObjcConstantStringInterface(ObjcInterfaceDecl *Decl) {
874 assert(ObjcConstantStringType.isNull() &&
875 "'NSConstantString' type already set!");
876
877 ObjcConstantStringType = getObjcInterfaceType(Decl);
878}
879
Steve Naroff32e44c02007-10-15 20:41:53 +0000880bool ASTContext::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
881 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
882 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
883
884 return lBuiltin->getKind() == rBuiltin->getKind();
885}
886
887
888bool ASTContext::objcTypesAreCompatible(QualType lhs, QualType rhs) {
889 if (lhs->isObjcInterfaceType() && isObjcIdType(rhs))
890 return true;
891 else if (isObjcIdType(lhs) && rhs->isObjcInterfaceType())
892 return true;
893 return false;
894}
895
896bool ASTContext::interfaceTypesAreCompatible(QualType lhs, QualType rhs) {
897 return true; // FIXME: IMPLEMENT.
898}
899
900// C99 6.2.7p1: If both are complete types, then the following additional
901// requirements apply...FIXME (handle compatibility across source files).
902bool ASTContext::tagTypesAreCompatible(QualType lhs, QualType rhs) {
903 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
904 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
905
906 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
907 if (ldecl->getIdentifier() == rdecl->getIdentifier())
908 return true;
909 }
910 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
911 if (ldecl->getIdentifier() == rdecl->getIdentifier())
912 return true;
913 }
914 return false;
915}
916
917bool ASTContext::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
918 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
919 // identically qualified and both shall be pointers to compatible types.
920 if (lhs.getQualifiers() != rhs.getQualifiers())
921 return false;
922
923 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
924 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
925
926 return typesAreCompatible(ltype, rtype);
927}
928
929// C++ 5.17p6: When the left opperand of an assignment operator denotes a
930// reference to T, the operation assigns to the object of type T denoted by the
931// reference.
932bool ASTContext::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
933 QualType ltype = lhs;
934
935 if (lhs->isReferenceType())
936 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
937
938 QualType rtype = rhs;
939
940 if (rhs->isReferenceType())
941 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
942
943 return typesAreCompatible(ltype, rtype);
944}
945
946bool ASTContext::functionTypesAreCompatible(QualType lhs, QualType rhs) {
947 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
948 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
949 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
950 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
951
952 // first check the return types (common between C99 and K&R).
953 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
954 return false;
955
956 if (lproto && rproto) { // two C99 style function prototypes
957 unsigned lproto_nargs = lproto->getNumArgs();
958 unsigned rproto_nargs = rproto->getNumArgs();
959
960 if (lproto_nargs != rproto_nargs)
961 return false;
962
963 // both prototypes have the same number of arguments.
964 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
965 (rproto->isVariadic() && !lproto->isVariadic()))
966 return false;
967
968 // The use of ellipsis agree...now check the argument types.
969 for (unsigned i = 0; i < lproto_nargs; i++)
970 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
971 return false;
972 return true;
973 }
974 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
975 return true;
976
977 // we have a mixture of K&R style with C99 prototypes
978 const FunctionTypeProto *proto = lproto ? lproto : rproto;
979
980 if (proto->isVariadic())
981 return false;
982
983 // FIXME: Each parameter type T in the prototype must be compatible with the
984 // type resulting from applying the usual argument conversions to T.
985 return true;
986}
987
988bool ASTContext::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
989 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
990 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
991
992 if (!typesAreCompatible(ltype, rtype))
993 return false;
994
995 // FIXME: If both types specify constant sizes, then the sizes must also be
996 // the same. Even if the sizes are the same, GCC produces an error.
997 return true;
998}
999
1000/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
1001/// both shall have the identically qualified version of a compatible type.
1002/// C99 6.2.7p1: Two types have compatible types if their types are the
1003/// same. See 6.7.[2,3,5] for additional rules.
1004bool ASTContext::typesAreCompatible(QualType lhs, QualType rhs) {
1005 QualType lcanon = lhs.getCanonicalType();
1006 QualType rcanon = rhs.getCanonicalType();
1007
1008 // If two types are identical, they are are compatible
1009 if (lcanon == rcanon)
1010 return true;
1011
1012 // If the canonical type classes don't match, they can't be compatible
1013 if (lcanon->getTypeClass() != rcanon->getTypeClass()) {
1014 // For Objective-C, it is possible for two types to be compatible
1015 // when their classes don't match (when dealing with "id"). If either type
1016 // is an interface, we defer to objcTypesAreCompatible().
1017 if (lcanon->isObjcInterfaceType() || rcanon->isObjcInterfaceType())
1018 return objcTypesAreCompatible(lcanon, rcanon);
1019 return false;
1020 }
1021 switch (lcanon->getTypeClass()) {
1022 case Type::Pointer:
1023 return pointerTypesAreCompatible(lcanon, rcanon);
1024 case Type::Reference:
1025 return referenceTypesAreCompatible(lcanon, rcanon);
1026 case Type::ConstantArray:
1027 case Type::VariableArray:
1028 return arrayTypesAreCompatible(lcanon, rcanon);
1029 case Type::FunctionNoProto:
1030 case Type::FunctionProto:
1031 return functionTypesAreCompatible(lcanon, rcanon);
1032 case Type::Tagged: // handle structures, unions
1033 return tagTypesAreCompatible(lcanon, rcanon);
1034 case Type::Builtin:
1035 return builtinTypesAreCompatible(lcanon, rcanon);
1036 case Type::ObjcInterface:
1037 return interfaceTypesAreCompatible(lcanon, rcanon);
1038 default:
1039 assert(0 && "unexpected type");
1040 }
1041 return true; // should never get here...
1042}