blob: 65ba90a75d9c69b038a7f6efbe74891492ef1425 [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:
46 case Array:
47 case FunctionProto:
48 case FunctionNoProto:
49 case Reference:
50 return true;
51 case Tagged: {
52 const TagType *TT = cast<TagType>(CanonicalType);
53 const Decl::Kind Kind = TT->getDecl()->getKind();
54 return Kind == Decl::Struct || Kind == Decl::Union;
55 }
56 default:
57 return false;
58 }
59}
60
Chris Lattnerc8629632007-07-31 19:29:30 +000061bool Type::isStructureType() const {
62 if (const RecordType *RT = dyn_cast<RecordType>(this))
63 if (RT->getDecl()->getKind() == Decl::Struct)
64 return true;
65 return false;
66}
67bool Type::isUnionType() const {
68 if (const RecordType *RT = dyn_cast<RecordType>(this))
69 if (RT->getDecl()->getKind() == Decl::Union)
70 return true;
71 return false;
72}
Chris Lattnerc8629632007-07-31 19:29:30 +000073
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000074bool Type::isComplexType() const {
75 return isa<ComplexType>(CanonicalType);
76}
77
Chris Lattnerc8629632007-07-31 19:29:30 +000078const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +000079 // If this is directly a function type, return it.
80 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
81 return FTy;
Chris Lattnerc8629632007-07-31 19:29:30 +000082
Steve Naroff7064f5c2007-07-26 18:32:01 +000083 // If this is a typedef for a function type, strip the typedef off without
84 // losing all typedef information.
85 if (isa<FunctionType>(CanonicalType))
86 return cast<FunctionType>(cast<TypedefType>(this)->LookThroughTypedefs());
87 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000088}
89
Chris Lattnerbefee482007-07-31 16:53:04 +000090const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +000091 // If this is directly a pointer type, return it.
92 if (const PointerType *PTy = dyn_cast<PointerType>(this))
93 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +000094
95 // If this is a typedef for a pointer type, strip the typedef off without
96 // losing all typedef information.
97 if (isa<PointerType>(CanonicalType))
98 return cast<PointerType>(cast<TypedefType>(this)->LookThroughTypedefs());
Chris Lattner3acb1382007-07-16 00:13:25 +000099 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000100}
101
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000102const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000103 // If this is directly a reference type, return it.
104 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
105 return RTy;
106
107 // If this is a typedef for a reference type, strip the typedef off without
108 // losing all typedef information.
109 if (isa<ReferenceType>(CanonicalType))
110 return cast<ReferenceType>(cast<TypedefType>(this)->LookThroughTypedefs());
111 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000112}
113
Chris Lattnerc8629632007-07-31 19:29:30 +0000114const ArrayType *Type::getAsArrayType() const {
Steve Naroff700204c2007-07-24 21:46:40 +0000115 // If this is directly a reference type, return it.
116 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
117 return ATy;
118
119 // If this is a typedef for an array type, strip the typedef off without
120 // losing all typedef information.
121 if (isa<ArrayType>(CanonicalType))
122 return cast<ArrayType>(cast<TypedefType>(this)->LookThroughTypedefs());
123 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000124}
125
Chris Lattnerc8629632007-07-31 19:29:30 +0000126const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000127 // If this is directly a reference type, return it.
128 if (const RecordType *RTy = dyn_cast<RecordType>(this))
129 return RTy;
130
131 // If this is a typedef for an record type, strip the typedef off without
132 // losing all typedef information.
133 if (isa<RecordType>(CanonicalType))
134 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Steve Naroffadc01852007-07-26 03:18:02 +0000135 return 0;
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000136}
137
Chris Lattnerc8629632007-07-31 19:29:30 +0000138const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000139 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000140 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
141 if (RT->getDecl()->getKind() == Decl::Struct)
142 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000143 }
144 // If this is a typedef for a structure type, strip the typedef off without
145 // losing all typedef information.
Chris Lattnerc8629632007-07-31 19:29:30 +0000146 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
147 if (RT->getDecl()->getKind() == Decl::Struct)
148 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Reid Spencer5f016e22007-07-11 17:01:13 +0000149 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000150 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000151}
152
Chris Lattnerc8629632007-07-31 19:29:30 +0000153const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000154 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000155 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
156 if (RT->getDecl()->getKind() == Decl::Union)
157 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000158 }
159 // If this is a typedef for a union type, strip the typedef off without
160 // losing all typedef information.
Chris Lattnerc8629632007-07-31 19:29:30 +0000161 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
162 if (RT->getDecl()->getKind() == Decl::Union)
163 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000165 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000166}
167
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000168const ComplexType *Type::getAsComplexType() const {
169 // Are we directly a complex type?
170 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
171 return CTy;
172
173 // If this is a typedef for a complex type, strip the typedef off without
174 // losing all typedef information.
175 if (isa<ComplexType>(CanonicalType))
176 return cast<ComplexType>(cast<TypedefType>(this)->LookThroughTypedefs());
177
178 return 0;
Chris Lattner7a2e0472007-07-16 00:23:25 +0000179}
180
Chris Lattnerc8629632007-07-31 19:29:30 +0000181const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000182 // Are we directly a vector type?
183 if (const VectorType *VTy = dyn_cast<VectorType>(this))
184 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000185
186 // If this is a typedef for a vector type, strip the typedef off without
187 // losing all typedef information.
188 if (isa<VectorType>(CanonicalType))
189 return cast<VectorType>(cast<TypedefType>(this)->LookThroughTypedefs());
190
Chris Lattner7a2e0472007-07-16 00:23:25 +0000191 return 0;
192}
193
Chris Lattnerc8629632007-07-31 19:29:30 +0000194const OCUVectorType *Type::getAsOCUVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000195 // Are we directly an OpenCU vector type?
196 if (const OCUVectorType *VTy = dyn_cast<OCUVectorType>(this))
197 return VTy;
198
199 // If this is a typedef for an OpenCU vector type, strip the typedef off
200 // without losing all typedef information.
201 if (isa<OCUVectorType>(CanonicalType))
202 return cast<OCUVectorType>(cast<TypedefType>(this)->LookThroughTypedefs());
203
204 return 0;
205}
206
Chris Lattner7a2e0472007-07-16 00:23:25 +0000207
Reid Spencer5f016e22007-07-11 17:01:13 +0000208// C99 6.2.7p1: If both are complete types, then the following additional
209// requirements apply...FIXME (handle compatibility across source files).
210bool Type::tagTypesAreCompatible(QualType lhs, QualType rhs) {
211 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
212 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
213
214 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
215 if (ldecl->getIdentifier() == rdecl->getIdentifier())
216 return true;
217 }
218 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
219 if (ldecl->getIdentifier() == rdecl->getIdentifier())
220 return true;
221 }
222 return false;
223}
224
225bool Type::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
226 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
227 // identically qualified and both shall be pointers to compatible types.
228 if (lhs.getQualifiers() != rhs.getQualifiers())
229 return false;
230
231 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
232 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
233
234 return typesAreCompatible(ltype, rtype);
235}
236
237// C++ 5.17p6: When the left opperand of an assignment operator denotes a
238// reference to T, the operation assigns to the object of type T denoted by the
239// reference.
240bool Type::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
241 QualType ltype = lhs;
242
243 if (lhs->isReferenceType())
244 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
245
246 QualType rtype = rhs;
247
248 if (rhs->isReferenceType())
249 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
250
251 return typesAreCompatible(ltype, rtype);
252}
253
254bool Type::functionTypesAreCompatible(QualType lhs, QualType rhs) {
255 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
256 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
257 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
258 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
259
260 // first check the return types (common between C99 and K&R).
261 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
262 return false;
263
264 if (lproto && rproto) { // two C99 style function prototypes
265 unsigned lproto_nargs = lproto->getNumArgs();
266 unsigned rproto_nargs = rproto->getNumArgs();
267
268 if (lproto_nargs != rproto_nargs)
269 return false;
270
271 // both prototypes have the same number of arguments.
272 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
273 (rproto->isVariadic() && !lproto->isVariadic()))
274 return false;
275
276 // The use of ellipsis agree...now check the argument types.
277 for (unsigned i = 0; i < lproto_nargs; i++)
278 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
279 return false;
280 return true;
281 }
282 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
283 return true;
284
285 // we have a mixture of K&R style with C99 prototypes
286 const FunctionTypeProto *proto = lproto ? lproto : rproto;
287
288 if (proto->isVariadic())
289 return false;
290
291 // FIXME: Each parameter type T in the prototype must be compatible with the
292 // type resulting from applying the usual argument conversions to T.
293 return true;
294}
295
296bool Type::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
297 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
298 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
299
300 if (!typesAreCompatible(ltype, rtype))
301 return false;
302
303 // FIXME: If both types specify constant sizes, then the sizes must also be
304 // the same. Even if the sizes are the same, GCC produces an error.
305 return true;
306}
307
308/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
309/// both shall have the identically qualified version of a compatible type.
310/// C99 6.2.7p1: Two types have compatible types if their types are the
311/// same. See 6.7.[2,3,5] for additional rules.
312bool Type::typesAreCompatible(QualType lhs, QualType rhs) {
313 QualType lcanon = lhs.getCanonicalType();
314 QualType rcanon = rhs.getCanonicalType();
315
316 // If two types are identical, they are are compatible
317 if (lcanon == rcanon)
318 return true;
319
320 // If the canonical type classes don't match, they can't be compatible
321 if (lcanon->getTypeClass() != rcanon->getTypeClass())
322 return false;
323
324 switch (lcanon->getTypeClass()) {
325 case Type::Pointer:
326 return pointerTypesAreCompatible(lcanon, rcanon);
327 case Type::Reference:
328 return referenceTypesAreCompatible(lcanon, rcanon);
329 case Type::Array:
330 return arrayTypesAreCompatible(lcanon, rcanon);
331 case Type::FunctionNoProto:
332 case Type::FunctionProto:
333 return functionTypesAreCompatible(lcanon, rcanon);
334 case Type::Tagged: // handle structures, unions
335 return tagTypesAreCompatible(lcanon, rcanon);
336 case Type::Builtin:
337 return false;
338 default:
339 assert(0 && "unexpected type");
340 }
341 return true; // should never get here...
342}
343
344bool Type::isIntegerType() const {
345 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
346 return BT->getKind() >= BuiltinType::Bool &&
347 BT->getKind() <= BuiltinType::LongLong;
348 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
349 if (TT->getDecl()->getKind() == Decl::Enum)
350 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000351 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
352 return VT->getElementType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000353 return false;
354}
355
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000356bool Type::isEnumeralType() const {
357 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
358 return TT->getDecl()->getKind() == Decl::Enum;
359 return false;
360}
361
362bool Type::isBooleanType() const {
363 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
364 return BT->getKind() == BuiltinType::Bool;
365 return false;
366}
367
368bool Type::isCharType() const {
369 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
370 return BT->getKind() == BuiltinType::Char_U ||
371 BT->getKind() == BuiltinType::UChar ||
372 BT->getKind() == BuiltinType::Char_S;
373 return false;
374}
375
Reid Spencer5f016e22007-07-11 17:01:13 +0000376bool Type::isSignedIntegerType() const {
377 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
378 return BT->getKind() >= BuiltinType::Char_S &&
379 BT->getKind() <= BuiltinType::LongLong;
380 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000381 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
382 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000383 return false;
384}
385
386bool Type::isUnsignedIntegerType() const {
387 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
388 return BT->getKind() >= BuiltinType::Bool &&
389 BT->getKind() <= BuiltinType::ULongLong;
390 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000391 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
392 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 return false;
394}
395
396bool Type::isFloatingType() const {
397 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
398 return BT->getKind() >= BuiltinType::Float &&
399 BT->getKind() <= BuiltinType::LongDouble;
400 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
401 return CT->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000402 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
403 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000404 return false;
405}
406
407bool Type::isRealFloatingType() const {
408 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
409 return BT->getKind() >= BuiltinType::Float &&
410 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000411 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
412 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000413 return false;
414}
415
416bool Type::isRealType() const {
417 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
418 return BT->getKind() >= BuiltinType::Bool &&
419 BT->getKind() <= BuiltinType::LongDouble;
420 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
421 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000422 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
423 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 return false;
425}
426
Reid Spencer5f016e22007-07-11 17:01:13 +0000427bool Type::isArithmeticType() const {
428 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
429 return BT->getKind() != BuiltinType::Void;
430 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
431 if (TT->getDecl()->getKind() == Decl::Enum)
432 return true;
433 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
434}
435
436bool Type::isScalarType() const {
437 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
438 return BT->getKind() != BuiltinType::Void;
439 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
440 if (TT->getDecl()->getKind() == Decl::Enum)
441 return true;
442 return false;
443 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000444 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
445 isa<VectorType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000446}
447
448bool Type::isAggregateType() const {
449 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
450 if (TT->getDecl()->getKind() == Decl::Struct)
451 return true;
452 return false;
453 }
454 return CanonicalType->getTypeClass() == Array;
455}
456
457// The only variable size types are auto arrays within a function. Structures
458// cannot contain a VLA member. They can have a flexible array member, however
459// the structure is still constant size (C99 6.7.2.1p16).
Chris Lattner590b6642007-07-15 23:26:56 +0000460bool Type::isConstantSizeType(ASTContext &Ctx, SourceLocation *loc) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000461 if (const ArrayType *Ary = dyn_cast<ArrayType>(CanonicalType)) {
Chris Lattner8b9023b2007-07-13 03:05:23 +0000462 assert(Ary->getSizeExpr() && "Incomplete types don't have a size at all!");
463 // Variable Length Array?
Chris Lattner590b6642007-07-15 23:26:56 +0000464 return Ary->getSizeExpr()->isIntegerConstantExpr(Ctx, loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000465 }
466 return true;
467}
468
469/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
470/// - a type that can describe objects, but which lacks information needed to
471/// determine its size.
472bool Type::isIncompleteType() const {
473 switch (CanonicalType->getTypeClass()) {
474 default: return false;
475 case Builtin:
476 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
477 // be completed.
478 return isVoidType();
479 case Tagged:
480 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
481 // forward declaration, but not a full definition (C99 6.2.5p22).
482 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
483 case Array:
484 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Chris Lattner8b9023b2007-07-13 03:05:23 +0000485 return cast<ArrayType>(CanonicalType)->getSizeExpr() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000486 }
487}
488
489bool Type::isPromotableIntegerType() const {
490 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
491 if (!BT) return false;
492 switch (BT->getKind()) {
493 case BuiltinType::Bool:
494 case BuiltinType::Char_S:
495 case BuiltinType::Char_U:
496 case BuiltinType::SChar:
497 case BuiltinType::UChar:
498 case BuiltinType::Short:
499 case BuiltinType::UShort:
500 return true;
501 default:
502 return false;
503 }
504}
505
506const char *BuiltinType::getName() const {
507 switch (getKind()) {
508 default: assert(0 && "Unknown builtin type!");
509 case Void: return "void";
510 case Bool: return "_Bool";
511 case Char_S: return "char";
512 case Char_U: return "char";
513 case SChar: return "signed char";
514 case Short: return "short";
515 case Int: return "int";
516 case Long: return "long";
517 case LongLong: return "long long";
518 case UChar: return "unsigned char";
519 case UShort: return "unsigned short";
520 case UInt: return "unsigned int";
521 case ULong: return "unsigned long";
522 case ULongLong: return "unsigned long long";
523 case Float: return "float";
524 case Double: return "double";
525 case LongDouble: return "long double";
526 }
527}
528
Reid Spencer5f016e22007-07-11 17:01:13 +0000529void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000530 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000531 unsigned NumArgs, bool isVariadic) {
532 ID.AddPointer(Result.getAsOpaquePtr());
533 for (unsigned i = 0; i != NumArgs; ++i)
534 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
535 ID.AddInteger(isVariadic);
536}
537
538void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000539 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000540}
541
Chris Lattnera2c77672007-07-16 22:05:22 +0000542/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
543/// potentially looking through *all* consequtive typedefs. This returns the
544/// sum of the type qualifiers, so if you have:
545/// typedef const int A;
546/// typedef volatile A B;
547/// looking through the typedefs for B will give you "const volatile A".
548///
549QualType TypedefType::LookThroughTypedefs() const {
550 // Usually, there is only a single level of typedefs, be fast in that case.
551 QualType FirstType = getDecl()->getUnderlyingType();
552 if (!isa<TypedefType>(FirstType))
553 return FirstType;
554
555 // Otherwise, do the fully general loop.
556 unsigned TypeQuals = 0;
557 const TypedefType *TDT = this;
558 while (1) {
559 QualType CurType = TDT->getDecl()->getUnderlyingType();
560 TypeQuals |= CurType.getQualifiers();
561
562 TDT = dyn_cast<TypedefType>(CurType);
563 if (TDT == 0)
564 return QualType(CurType.getTypePtr(), TypeQuals);
565 }
566}
Reid Spencer5f016e22007-07-11 17:01:13 +0000567
568bool RecordType::classof(const Type *T) {
569 if (const TagType *TT = dyn_cast<TagType>(T))
570 return isa<RecordDecl>(TT->getDecl());
571 return false;
572}
573
574
575//===----------------------------------------------------------------------===//
576// Type Printing
577//===----------------------------------------------------------------------===//
578
579void QualType::dump(const char *msg) const {
580 std::string R = "foo";
581 getAsStringInternal(R);
582 if (msg)
583 fprintf(stderr, "%s: %s\n", msg, R.c_str());
584 else
585 fprintf(stderr, "%s\n", R.c_str());
586}
587
588static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
589 // Note: funkiness to ensure we get a space only between quals.
590 bool NonePrinted = true;
591 if (TypeQuals & QualType::Const)
592 S += "const", NonePrinted = false;
593 if (TypeQuals & QualType::Volatile)
594 S += (NonePrinted+" volatile"), NonePrinted = false;
595 if (TypeQuals & QualType::Restrict)
596 S += (NonePrinted+" restrict"), NonePrinted = false;
597}
598
599void QualType::getAsStringInternal(std::string &S) const {
600 if (isNull()) {
601 S += "NULL TYPE\n";
602 return;
603 }
604
605 // Print qualifiers as appropriate.
Anton Korobeynikovb7b50bc2007-07-13 00:48:55 +0000606 unsigned TQ = getQualifiers();
607 if (TQ) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000608 std::string TQS;
609 AppendTypeQualList(TQS, TQ);
610 if (!S.empty())
611 S = TQS + ' ' + S;
612 else
613 S = TQS;
614 }
615
616 getTypePtr()->getAsStringInternal(S);
617}
618
619void BuiltinType::getAsStringInternal(std::string &S) const {
620 if (S.empty()) {
621 S = getName();
622 } else {
623 // Prefix the basic type, e.g. 'int X'.
624 S = ' ' + S;
625 S = getName() + S;
626 }
627}
628
629void ComplexType::getAsStringInternal(std::string &S) const {
630 ElementType->getAsStringInternal(S);
631 S = "_Complex " + S;
632}
633
634void PointerType::getAsStringInternal(std::string &S) const {
635 S = '*' + S;
636
637 // Handle things like 'int (*A)[4];' correctly.
638 // FIXME: this should include vectors, but vectors use attributes I guess.
639 if (isa<ArrayType>(PointeeType.getTypePtr()))
640 S = '(' + S + ')';
641
642 PointeeType.getAsStringInternal(S);
643}
644
645void ReferenceType::getAsStringInternal(std::string &S) const {
646 S = '&' + S;
647
648 // Handle things like 'int (&A)[4];' correctly.
649 // FIXME: this should include vectors, but vectors use attributes I guess.
650 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
651 S = '(' + S + ')';
652
653 ReferenceeType.getAsStringInternal(S);
654}
655
656void ArrayType::getAsStringInternal(std::string &S) const {
657 S += '[';
658
659 if (IndexTypeQuals) {
660 AppendTypeQualList(S, IndexTypeQuals);
661 S += ' ';
662 }
663
664 if (SizeModifier == Static)
665 S += "static";
666 else if (SizeModifier == Star)
667 S += '*';
668
669 S += ']';
670
671 ElementType.getAsStringInternal(S);
672}
673
674void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattnere107b5d2007-07-13 21:01:17 +0000675 S += " __attribute__((vector_size(";
676 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000678 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 ElementType.getAsStringInternal(S);
680}
681
Steve Naroff31a45842007-07-28 23:10:27 +0000682void OCUVectorType::getAsStringInternal(std::string &S) const {
683 S += " __attribute__((ocu_vector_type(";
684 S += llvm::utostr_32(NumElements);
685 S += ")))";
686 ElementType.getAsStringInternal(S);
687}
688
Steve Naroffd1861fd2007-07-31 12:34:36 +0000689void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000690 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
691 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000692 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000693 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000694 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000695}
696
Steve Naroff363bcff2007-08-01 23:45:51 +0000697void TypeOfType::getAsStringInternal(std::string &InnerString) const {
698 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
699 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000700 std::string Tmp;
701 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000702 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000703}
704
Reid Spencer5f016e22007-07-11 17:01:13 +0000705void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
706 // If needed for precedence reasons, wrap the inner part in grouping parens.
707 if (!S.empty())
708 S = "(" + S + ")";
709
710 S += "()";
711 getResultType().getAsStringInternal(S);
712}
713
714void FunctionTypeProto::getAsStringInternal(std::string &S) const {
715 // If needed for precedence reasons, wrap the inner part in grouping parens.
716 if (!S.empty())
717 S = "(" + S + ")";
718
719 S += "(";
720 std::string Tmp;
721 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
722 if (i) S += ", ";
723 getArgType(i).getAsStringInternal(Tmp);
724 S += Tmp;
725 Tmp.clear();
726 }
727
728 if (isVariadic()) {
729 if (getNumArgs())
730 S += ", ";
731 S += "...";
732 } else if (getNumArgs() == 0) {
733 // Do not emit int() if we have a proto, emit 'int(void)'.
734 S += "void";
735 }
736
737 S += ")";
738 getResultType().getAsStringInternal(S);
739}
740
741
742void TypedefType::getAsStringInternal(std::string &InnerString) const {
743 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
744 InnerString = ' ' + InnerString;
745 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
746}
747
748void TagType::getAsStringInternal(std::string &InnerString) const {
749 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
750 InnerString = ' ' + InnerString;
751
752 const char *Kind = getDecl()->getKindName();
753 const char *ID;
754 if (const IdentifierInfo *II = getDecl()->getIdentifier())
755 ID = II->getName();
756 else
757 ID = "<anonymous>";
758
759 InnerString = std::string(Kind) + " " + ID + InnerString;
760}