blob: 126f3d5db98f6e68722f619b2bb22dfe2295f899 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Type.cpp - Type representation and manipulation ------------------===//
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 type-related functionality.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/IdentifierTable.h"
15#include "clang/AST/Type.h"
16#include "clang/AST/Decl.h"
17#include "clang/AST/Expr.h"
18#include "clang/Basic/TargetInfo.h"
19#include "llvm/Support/Streams.h"
20#include "llvm/ADT/StringExtras.h"
Steve Naroff8d1a3b82007-08-01 17:20:42 +000021#include <sstream>
22
Reid Spencer5f016e22007-07-11 17:01:13 +000023using namespace clang;
24
25Type::~Type() {}
26
27/// isVoidType - Helper method to determine if this is the 'void' type.
28bool Type::isVoidType() const {
29 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
30 return BT->getKind() == BuiltinType::Void;
31 return false;
32}
33
34bool Type::isObjectType() const {
35 if (isa<FunctionType>(CanonicalType))
36 return false;
37 else if (CanonicalType->isIncompleteType())
38 return false;
39 else
40 return true;
41}
42
43bool Type::isDerivedType() const {
44 switch (CanonicalType->getTypeClass()) {
45 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +000046 case VariableArray:
47 case ConstantArray:
Reid Spencer5f016e22007-07-11 17:01:13 +000048 case FunctionProto:
49 case FunctionNoProto:
50 case Reference:
51 return true;
52 case Tagged: {
53 const TagType *TT = cast<TagType>(CanonicalType);
54 const Decl::Kind Kind = TT->getDecl()->getKind();
55 return Kind == Decl::Struct || Kind == Decl::Union;
56 }
57 default:
58 return false;
59 }
60}
61
Chris Lattnerc8629632007-07-31 19:29:30 +000062bool Type::isStructureType() const {
63 if (const RecordType *RT = dyn_cast<RecordType>(this))
64 if (RT->getDecl()->getKind() == Decl::Struct)
65 return true;
66 return false;
67}
68bool Type::isUnionType() const {
69 if (const RecordType *RT = dyn_cast<RecordType>(this))
70 if (RT->getDecl()->getKind() == Decl::Union)
71 return true;
72 return false;
73}
Chris Lattnerc8629632007-07-31 19:29:30 +000074
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000075bool Type::isComplexType() const {
76 return isa<ComplexType>(CanonicalType);
77}
78
Steve Naroff77878cc2007-08-27 04:08:11 +000079const BuiltinType *Type::getAsBuiltinType() const {
80 // If this is directly a builtin type, return it.
81 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
82 return BTy;
83
84 // If this is a typedef for a builtin type, strip the typedef off without
85 // losing all typedef information.
86 if (isa<BuiltinType>(CanonicalType))
87 return cast<BuiltinType>(cast<TypedefType>(this)->LookThroughTypedefs());
88 return 0;
89}
90
Chris Lattnerc8629632007-07-31 19:29:30 +000091const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +000092 // If this is directly a function type, return it.
93 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
94 return FTy;
Chris Lattnerc8629632007-07-31 19:29:30 +000095
Steve Naroff7064f5c2007-07-26 18:32:01 +000096 // If this is a typedef for a function type, strip the typedef off without
97 // losing all typedef information.
98 if (isa<FunctionType>(CanonicalType))
99 return cast<FunctionType>(cast<TypedefType>(this)->LookThroughTypedefs());
100 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000101}
102
Chris Lattnerbefee482007-07-31 16:53:04 +0000103const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000104 // If this is directly a pointer type, return it.
105 if (const PointerType *PTy = dyn_cast<PointerType>(this))
106 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000107
108 // If this is a typedef for a pointer type, strip the typedef off without
109 // losing all typedef information.
110 if (isa<PointerType>(CanonicalType))
111 return cast<PointerType>(cast<TypedefType>(this)->LookThroughTypedefs());
Chris Lattner3acb1382007-07-16 00:13:25 +0000112 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000113}
114
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000115const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000116 // If this is directly a reference type, return it.
117 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
118 return RTy;
119
120 // If this is a typedef for a reference type, strip the typedef off without
121 // losing all typedef information.
122 if (isa<ReferenceType>(CanonicalType))
123 return cast<ReferenceType>(cast<TypedefType>(this)->LookThroughTypedefs());
124 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000125}
126
Chris Lattnerc8629632007-07-31 19:29:30 +0000127const ArrayType *Type::getAsArrayType() const {
Steve Naroff700204c2007-07-24 21:46:40 +0000128 // If this is directly a reference type, return it.
129 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
130 return ATy;
131
132 // If this is a typedef for an array type, strip the typedef off without
133 // losing all typedef information.
134 if (isa<ArrayType>(CanonicalType))
135 return cast<ArrayType>(cast<TypedefType>(this)->LookThroughTypedefs());
136 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000137}
138
Chris Lattnerc8629632007-07-31 19:29:30 +0000139const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000140 // If this is directly a reference type, return it.
141 if (const RecordType *RTy = dyn_cast<RecordType>(this))
142 return RTy;
143
144 // If this is a typedef for an record type, strip the typedef off without
145 // losing all typedef information.
146 if (isa<RecordType>(CanonicalType))
147 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Steve Naroffadc01852007-07-26 03:18:02 +0000148 return 0;
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000149}
150
Chris Lattnerc8629632007-07-31 19:29:30 +0000151const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000152 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000153 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
154 if (RT->getDecl()->getKind() == Decl::Struct)
155 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000156 }
157 // If this is a typedef for a structure type, strip the typedef off without
158 // losing all typedef information.
Chris Lattnerc8629632007-07-31 19:29:30 +0000159 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
160 if (RT->getDecl()->getKind() == Decl::Struct)
161 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Reid Spencer5f016e22007-07-11 17:01:13 +0000162 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000163 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000164}
165
Chris Lattnerc8629632007-07-31 19:29:30 +0000166const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000167 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000168 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
169 if (RT->getDecl()->getKind() == Decl::Union)
170 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000171 }
172 // If this is a typedef for a union type, strip the typedef off without
173 // losing all typedef information.
Chris Lattnerc8629632007-07-31 19:29:30 +0000174 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
175 if (RT->getDecl()->getKind() == Decl::Union)
176 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Reid Spencer5f016e22007-07-11 17:01:13 +0000177 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000178 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000179}
180
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000181const ComplexType *Type::getAsComplexType() const {
182 // Are we directly a complex type?
183 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
184 return CTy;
185
186 // If this is a typedef for a complex type, strip the typedef off without
187 // losing all typedef information.
188 if (isa<ComplexType>(CanonicalType))
189 return cast<ComplexType>(cast<TypedefType>(this)->LookThroughTypedefs());
190
191 return 0;
Chris Lattner7a2e0472007-07-16 00:23:25 +0000192}
193
Chris Lattnerc8629632007-07-31 19:29:30 +0000194const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000195 // Are we directly a vector type?
196 if (const VectorType *VTy = dyn_cast<VectorType>(this))
197 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000198
199 // If this is a typedef for a vector type, strip the typedef off without
200 // losing all typedef information.
201 if (isa<VectorType>(CanonicalType))
202 return cast<VectorType>(cast<TypedefType>(this)->LookThroughTypedefs());
203
Chris Lattner7a2e0472007-07-16 00:23:25 +0000204 return 0;
205}
206
Chris Lattnerc8629632007-07-31 19:29:30 +0000207const OCUVectorType *Type::getAsOCUVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000208 // Are we directly an OpenCU vector type?
209 if (const OCUVectorType *VTy = dyn_cast<OCUVectorType>(this))
210 return VTy;
211
212 // If this is a typedef for an OpenCU vector type, strip the typedef off
213 // without losing all typedef information.
214 if (isa<OCUVectorType>(CanonicalType))
215 return cast<OCUVectorType>(cast<TypedefType>(this)->LookThroughTypedefs());
216
217 return 0;
218}
219
Steve Naroff77878cc2007-08-27 04:08:11 +0000220bool Type::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
221 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
222 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
223
224 return lBuiltin->getKind() == rBuiltin->getKind();
225}
Chris Lattner7a2e0472007-07-16 00:23:25 +0000226
Reid Spencer5f016e22007-07-11 17:01:13 +0000227// C99 6.2.7p1: If both are complete types, then the following additional
228// requirements apply...FIXME (handle compatibility across source files).
229bool Type::tagTypesAreCompatible(QualType lhs, QualType rhs) {
230 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
231 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
232
233 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
234 if (ldecl->getIdentifier() == rdecl->getIdentifier())
235 return true;
236 }
237 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
238 if (ldecl->getIdentifier() == rdecl->getIdentifier())
239 return true;
240 }
241 return false;
242}
243
244bool Type::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
245 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
246 // identically qualified and both shall be pointers to compatible types.
247 if (lhs.getQualifiers() != rhs.getQualifiers())
248 return false;
249
250 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
251 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
252
253 return typesAreCompatible(ltype, rtype);
254}
255
256// C++ 5.17p6: When the left opperand of an assignment operator denotes a
257// reference to T, the operation assigns to the object of type T denoted by the
258// reference.
259bool Type::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
260 QualType ltype = lhs;
261
262 if (lhs->isReferenceType())
263 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
264
265 QualType rtype = rhs;
266
267 if (rhs->isReferenceType())
268 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
269
270 return typesAreCompatible(ltype, rtype);
271}
272
273bool Type::functionTypesAreCompatible(QualType lhs, QualType rhs) {
274 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
275 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
276 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
277 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
278
279 // first check the return types (common between C99 and K&R).
280 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
281 return false;
282
283 if (lproto && rproto) { // two C99 style function prototypes
284 unsigned lproto_nargs = lproto->getNumArgs();
285 unsigned rproto_nargs = rproto->getNumArgs();
286
287 if (lproto_nargs != rproto_nargs)
288 return false;
289
290 // both prototypes have the same number of arguments.
291 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
292 (rproto->isVariadic() && !lproto->isVariadic()))
293 return false;
294
295 // The use of ellipsis agree...now check the argument types.
296 for (unsigned i = 0; i < lproto_nargs; i++)
297 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
298 return false;
299 return true;
300 }
301 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
302 return true;
303
304 // we have a mixture of K&R style with C99 prototypes
305 const FunctionTypeProto *proto = lproto ? lproto : rproto;
306
307 if (proto->isVariadic())
308 return false;
309
310 // FIXME: Each parameter type T in the prototype must be compatible with the
311 // type resulting from applying the usual argument conversions to T.
312 return true;
313}
314
315bool Type::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
316 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
317 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
318
319 if (!typesAreCompatible(ltype, rtype))
320 return false;
321
322 // FIXME: If both types specify constant sizes, then the sizes must also be
323 // the same. Even if the sizes are the same, GCC produces an error.
324 return true;
325}
326
327/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
328/// both shall have the identically qualified version of a compatible type.
329/// C99 6.2.7p1: Two types have compatible types if their types are the
330/// same. See 6.7.[2,3,5] for additional rules.
331bool Type::typesAreCompatible(QualType lhs, QualType rhs) {
332 QualType lcanon = lhs.getCanonicalType();
333 QualType rcanon = rhs.getCanonicalType();
334
335 // If two types are identical, they are are compatible
336 if (lcanon == rcanon)
337 return true;
338
339 // If the canonical type classes don't match, they can't be compatible
340 if (lcanon->getTypeClass() != rcanon->getTypeClass())
341 return false;
342
343 switch (lcanon->getTypeClass()) {
344 case Type::Pointer:
345 return pointerTypesAreCompatible(lcanon, rcanon);
346 case Type::Reference:
347 return referenceTypesAreCompatible(lcanon, rcanon);
Steve Narofffb22d962007-08-30 01:06:46 +0000348 case Type::ConstantArray:
349 case Type::VariableArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 return arrayTypesAreCompatible(lcanon, rcanon);
351 case Type::FunctionNoProto:
352 case Type::FunctionProto:
353 return functionTypesAreCompatible(lcanon, rcanon);
354 case Type::Tagged: // handle structures, unions
355 return tagTypesAreCompatible(lcanon, rcanon);
356 case Type::Builtin:
Steve Naroff77878cc2007-08-27 04:08:11 +0000357 return builtinTypesAreCompatible(lcanon, rcanon);
Reid Spencer5f016e22007-07-11 17:01:13 +0000358 default:
359 assert(0 && "unexpected type");
360 }
361 return true; // should never get here...
362}
363
364bool Type::isIntegerType() const {
365 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
366 return BT->getKind() >= BuiltinType::Bool &&
367 BT->getKind() <= BuiltinType::LongLong;
368 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
369 if (TT->getDecl()->getKind() == Decl::Enum)
370 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000371 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
372 return VT->getElementType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000373 return false;
374}
375
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000376bool Type::isEnumeralType() const {
377 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
378 return TT->getDecl()->getKind() == Decl::Enum;
379 return false;
380}
381
382bool Type::isBooleanType() const {
383 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
384 return BT->getKind() == BuiltinType::Bool;
385 return false;
386}
387
388bool Type::isCharType() const {
389 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
390 return BT->getKind() == BuiltinType::Char_U ||
391 BT->getKind() == BuiltinType::UChar ||
392 BT->getKind() == BuiltinType::Char_S;
393 return false;
394}
395
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000396/// isSignedIntegerType - Return true if this is an integer type that is
397/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
398/// an enum decl which has a signed representation, or a vector of signed
399/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000400bool Type::isSignedIntegerType() const {
401 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
402 return BT->getKind() >= BuiltinType::Char_S &&
403 BT->getKind() <= BuiltinType::LongLong;
404 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000405
406 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
407 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
408 return ED->getIntegerType()->isSignedIntegerType();
409
Steve Naroffc63b96a2007-07-12 21:46:55 +0000410 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
411 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 return false;
413}
414
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000415/// isUnsignedIntegerType - Return true if this is an integer type that is
416/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
417/// decl which has an unsigned representation, or a vector of unsigned integer
418/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000419bool Type::isUnsignedIntegerType() const {
420 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
421 return BT->getKind() >= BuiltinType::Bool &&
422 BT->getKind() <= BuiltinType::ULongLong;
423 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000424
425 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
426 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
427 return ED->getIntegerType()->isUnsignedIntegerType();
428
Steve Naroffc63b96a2007-07-12 21:46:55 +0000429 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
430 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 return false;
432}
433
434bool Type::isFloatingType() const {
435 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
436 return BT->getKind() >= BuiltinType::Float &&
437 BT->getKind() <= BuiltinType::LongDouble;
438 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000439 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000440 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
441 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 return false;
443}
444
445bool Type::isRealFloatingType() const {
446 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
447 return BT->getKind() >= BuiltinType::Float &&
448 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000449 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
450 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000451 return false;
452}
453
454bool Type::isRealType() const {
455 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
456 return BT->getKind() >= BuiltinType::Bool &&
457 BT->getKind() <= BuiltinType::LongDouble;
458 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
459 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000460 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
461 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000462 return false;
463}
464
Reid Spencer5f016e22007-07-11 17:01:13 +0000465bool Type::isArithmeticType() const {
466 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
467 return BT->getKind() != BuiltinType::Void;
468 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
469 if (TT->getDecl()->getKind() == Decl::Enum)
470 return true;
471 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
472}
473
474bool Type::isScalarType() const {
475 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
476 return BT->getKind() != BuiltinType::Void;
477 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
478 if (TT->getDecl()->getKind() == Decl::Enum)
479 return true;
480 return false;
481 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000482 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
483 isa<VectorType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000484}
485
486bool Type::isAggregateType() const {
487 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
488 if (TT->getDecl()->getKind() == Decl::Struct)
489 return true;
490 return false;
491 }
Steve Narofffb22d962007-08-30 01:06:46 +0000492 return CanonicalType->getTypeClass() == ConstantArray ||
493 CanonicalType->getTypeClass() == VariableArray;
Reid Spencer5f016e22007-07-11 17:01:13 +0000494}
495
496// The only variable size types are auto arrays within a function. Structures
497// cannot contain a VLA member. They can have a flexible array member, however
498// the structure is still constant size (C99 6.7.2.1p16).
Chris Lattner590b6642007-07-15 23:26:56 +0000499bool Type::isConstantSizeType(ASTContext &Ctx, SourceLocation *loc) const {
Steve Narofffb22d962007-08-30 01:06:46 +0000500 if (isa<VariableArrayType>(CanonicalType))
501 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 return true;
503}
504
505/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
506/// - a type that can describe objects, but which lacks information needed to
507/// determine its size.
508bool Type::isIncompleteType() const {
509 switch (CanonicalType->getTypeClass()) {
510 default: return false;
511 case Builtin:
512 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
513 // be completed.
514 return isVoidType();
515 case Tagged:
516 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
517 // forward declaration, but not a full definition (C99 6.2.5p22).
518 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Steve Narofffb22d962007-08-30 01:06:46 +0000519 case VariableArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000520 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Steve Narofffb22d962007-08-30 01:06:46 +0000521 return cast<VariableArrayType>(CanonicalType)->getSizeExpr() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 }
523}
524
525bool Type::isPromotableIntegerType() const {
526 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
527 if (!BT) return false;
528 switch (BT->getKind()) {
529 case BuiltinType::Bool:
530 case BuiltinType::Char_S:
531 case BuiltinType::Char_U:
532 case BuiltinType::SChar:
533 case BuiltinType::UChar:
534 case BuiltinType::Short:
535 case BuiltinType::UShort:
536 return true;
537 default:
538 return false;
539 }
540}
541
542const char *BuiltinType::getName() const {
543 switch (getKind()) {
544 default: assert(0 && "Unknown builtin type!");
545 case Void: return "void";
546 case Bool: return "_Bool";
547 case Char_S: return "char";
548 case Char_U: return "char";
549 case SChar: return "signed char";
550 case Short: return "short";
551 case Int: return "int";
552 case Long: return "long";
553 case LongLong: return "long long";
554 case UChar: return "unsigned char";
555 case UShort: return "unsigned short";
556 case UInt: return "unsigned int";
557 case ULong: return "unsigned long";
558 case ULongLong: return "unsigned long long";
559 case Float: return "float";
560 case Double: return "double";
561 case LongDouble: return "long double";
562 }
563}
564
Reid Spencer5f016e22007-07-11 17:01:13 +0000565void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000566 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 unsigned NumArgs, bool isVariadic) {
568 ID.AddPointer(Result.getAsOpaquePtr());
569 for (unsigned i = 0; i != NumArgs; ++i)
570 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
571 ID.AddInteger(isVariadic);
572}
573
574void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000575 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000576}
577
Chris Lattnera2c77672007-07-16 22:05:22 +0000578/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
579/// potentially looking through *all* consequtive typedefs. This returns the
580/// sum of the type qualifiers, so if you have:
581/// typedef const int A;
582/// typedef volatile A B;
583/// looking through the typedefs for B will give you "const volatile A".
584///
585QualType TypedefType::LookThroughTypedefs() const {
586 // Usually, there is only a single level of typedefs, be fast in that case.
587 QualType FirstType = getDecl()->getUnderlyingType();
588 if (!isa<TypedefType>(FirstType))
589 return FirstType;
590
591 // Otherwise, do the fully general loop.
592 unsigned TypeQuals = 0;
593 const TypedefType *TDT = this;
594 while (1) {
595 QualType CurType = TDT->getDecl()->getUnderlyingType();
596 TypeQuals |= CurType.getQualifiers();
597
598 TDT = dyn_cast<TypedefType>(CurType);
599 if (TDT == 0)
600 return QualType(CurType.getTypePtr(), TypeQuals);
601 }
602}
Reid Spencer5f016e22007-07-11 17:01:13 +0000603
604bool RecordType::classof(const Type *T) {
605 if (const TagType *TT = dyn_cast<TagType>(T))
606 return isa<RecordDecl>(TT->getDecl());
607 return false;
608}
609
610
611//===----------------------------------------------------------------------===//
612// Type Printing
613//===----------------------------------------------------------------------===//
614
615void QualType::dump(const char *msg) const {
616 std::string R = "foo";
617 getAsStringInternal(R);
618 if (msg)
619 fprintf(stderr, "%s: %s\n", msg, R.c_str());
620 else
621 fprintf(stderr, "%s\n", R.c_str());
622}
623
624static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
625 // Note: funkiness to ensure we get a space only between quals.
626 bool NonePrinted = true;
627 if (TypeQuals & QualType::Const)
628 S += "const", NonePrinted = false;
629 if (TypeQuals & QualType::Volatile)
630 S += (NonePrinted+" volatile"), NonePrinted = false;
631 if (TypeQuals & QualType::Restrict)
632 S += (NonePrinted+" restrict"), NonePrinted = false;
633}
634
635void QualType::getAsStringInternal(std::string &S) const {
636 if (isNull()) {
637 S += "NULL TYPE\n";
638 return;
639 }
640
641 // Print qualifiers as appropriate.
Anton Korobeynikovb7b50bc2007-07-13 00:48:55 +0000642 unsigned TQ = getQualifiers();
643 if (TQ) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 std::string TQS;
645 AppendTypeQualList(TQS, TQ);
646 if (!S.empty())
647 S = TQS + ' ' + S;
648 else
649 S = TQS;
650 }
651
652 getTypePtr()->getAsStringInternal(S);
653}
654
655void BuiltinType::getAsStringInternal(std::string &S) const {
656 if (S.empty()) {
657 S = getName();
658 } else {
659 // Prefix the basic type, e.g. 'int X'.
660 S = ' ' + S;
661 S = getName() + S;
662 }
663}
664
665void ComplexType::getAsStringInternal(std::string &S) const {
666 ElementType->getAsStringInternal(S);
667 S = "_Complex " + S;
668}
669
670void PointerType::getAsStringInternal(std::string &S) const {
671 S = '*' + S;
672
673 // Handle things like 'int (*A)[4];' correctly.
674 // FIXME: this should include vectors, but vectors use attributes I guess.
675 if (isa<ArrayType>(PointeeType.getTypePtr()))
676 S = '(' + S + ')';
677
678 PointeeType.getAsStringInternal(S);
679}
680
681void ReferenceType::getAsStringInternal(std::string &S) const {
682 S = '&' + S;
683
684 // Handle things like 'int (&A)[4];' correctly.
685 // FIXME: this should include vectors, but vectors use attributes I guess.
686 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
687 S = '(' + S + ')';
688
689 ReferenceeType.getAsStringInternal(S);
690}
691
Steve Narofffb22d962007-08-30 01:06:46 +0000692void ConstantArrayType::getAsStringInternal(std::string &S) const {
693 S += '[';
694 S += llvm::utostr_32(getSize().getZExtValue());
695 S += ']';
696
697 getElementType().getAsStringInternal(S);
698}
699
700void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000701 S += '[';
702
Steve Naroffc9406122007-08-30 18:10:14 +0000703 if (getIndexTypeQualifier()) {
704 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000705 S += ' ';
706 }
707
Steve Naroffc9406122007-08-30 18:10:14 +0000708 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000709 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000710 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 S += '*';
712
Steve Narofffb22d962007-08-30 01:06:46 +0000713 if (getSizeExpr()) {
714 std::ostringstream s;
715 getSizeExpr()->printPretty(s);
716 S += s.str();
717 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 S += ']';
719
Steve Narofffb22d962007-08-30 01:06:46 +0000720 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000721}
722
723void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattnere107b5d2007-07-13 21:01:17 +0000724 S += " __attribute__((vector_size(";
725 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000726 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000727 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 ElementType.getAsStringInternal(S);
729}
730
Steve Naroff31a45842007-07-28 23:10:27 +0000731void OCUVectorType::getAsStringInternal(std::string &S) const {
732 S += " __attribute__((ocu_vector_type(";
733 S += llvm::utostr_32(NumElements);
734 S += ")))";
735 ElementType.getAsStringInternal(S);
736}
737
Steve Naroffd1861fd2007-07-31 12:34:36 +0000738void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000739 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
740 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000741 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000742 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000743 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000744}
745
Steve Naroff363bcff2007-08-01 23:45:51 +0000746void TypeOfType::getAsStringInternal(std::string &InnerString) const {
747 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
748 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000749 std::string Tmp;
750 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000751 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000752}
753
Reid Spencer5f016e22007-07-11 17:01:13 +0000754void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
755 // If needed for precedence reasons, wrap the inner part in grouping parens.
756 if (!S.empty())
757 S = "(" + S + ")";
758
759 S += "()";
760 getResultType().getAsStringInternal(S);
761}
762
763void FunctionTypeProto::getAsStringInternal(std::string &S) const {
764 // If needed for precedence reasons, wrap the inner part in grouping parens.
765 if (!S.empty())
766 S = "(" + S + ")";
767
768 S += "(";
769 std::string Tmp;
770 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
771 if (i) S += ", ";
772 getArgType(i).getAsStringInternal(Tmp);
773 S += Tmp;
774 Tmp.clear();
775 }
776
777 if (isVariadic()) {
778 if (getNumArgs())
779 S += ", ";
780 S += "...";
781 } else if (getNumArgs() == 0) {
782 // Do not emit int() if we have a proto, emit 'int(void)'.
783 S += "void";
784 }
785
786 S += ")";
787 getResultType().getAsStringInternal(S);
788}
789
790
791void TypedefType::getAsStringInternal(std::string &InnerString) const {
792 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
793 InnerString = ' ' + InnerString;
794 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
795}
796
797void TagType::getAsStringInternal(std::string &InnerString) const {
798 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
799 InnerString = ' ' + InnerString;
800
801 const char *Kind = getDecl()->getKindName();
802 const char *ID;
803 if (const IdentifierInfo *II = getDecl()->getIdentifier())
804 ID = II->getName();
805 else
806 ID = "<anonymous>";
807
808 InnerString = std::string(Kind) + " " + ID + InnerString;
809}