blob: 1c61c681cee1687f24e028ae3c8a589654a342cb [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"
21using namespace clang;
22
23Type::~Type() {}
24
25/// isVoidType - Helper method to determine if this is the 'void' type.
26bool Type::isVoidType() const {
27 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
28 return BT->getKind() == BuiltinType::Void;
29 return false;
30}
31
32bool Type::isObjectType() const {
33 if (isa<FunctionType>(CanonicalType))
34 return false;
35 else if (CanonicalType->isIncompleteType())
36 return false;
37 else
38 return true;
39}
40
41bool Type::isDerivedType() const {
42 switch (CanonicalType->getTypeClass()) {
43 case Pointer:
44 case Array:
45 case FunctionProto:
46 case FunctionNoProto:
47 case Reference:
48 return true;
49 case Tagged: {
50 const TagType *TT = cast<TagType>(CanonicalType);
51 const Decl::Kind Kind = TT->getDecl()->getKind();
52 return Kind == Decl::Struct || Kind == Decl::Union;
53 }
54 default:
55 return false;
56 }
57}
58
Steve Naroff7064f5c2007-07-26 18:32:01 +000059const FunctionType *Type::isFunctionType() const {
60 // If this is directly a function type, return it.
61 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
62 return FTy;
63
64 // If this is a typedef for a function type, strip the typedef off without
65 // losing all typedef information.
66 if (isa<FunctionType>(CanonicalType))
67 return cast<FunctionType>(cast<TypedefType>(this)->LookThroughTypedefs());
68 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000069}
70
Chris Lattnerbefee482007-07-31 16:53:04 +000071// FIXME: move inline
72bool Type::isPointerType() const { return isa<PointerType>(CanonicalType); }
Chris Lattnera1d9fde2007-07-31 16:56:34 +000073bool Type::isReferenceType() const { return isa<ReferenceType>(CanonicalType); }
Chris Lattnerbefee482007-07-31 16:53:04 +000074
75const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +000076 // If this is directly a pointer type, return it.
77 if (const PointerType *PTy = dyn_cast<PointerType>(this))
78 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +000079
80 // If this is a typedef for a pointer type, strip the typedef off without
81 // losing all typedef information.
82 if (isa<PointerType>(CanonicalType))
83 return cast<PointerType>(cast<TypedefType>(this)->LookThroughTypedefs());
Chris Lattner3acb1382007-07-16 00:13:25 +000084 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000085}
86
Chris Lattnera1d9fde2007-07-31 16:56:34 +000087const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +000088 // If this is directly a reference type, return it.
89 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
90 return RTy;
91
92 // If this is a typedef for a reference type, strip the typedef off without
93 // losing all typedef information.
94 if (isa<ReferenceType>(CanonicalType))
95 return cast<ReferenceType>(cast<TypedefType>(this)->LookThroughTypedefs());
96 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000097}
98
Steve Naroff700204c2007-07-24 21:46:40 +000099const ArrayType *Type::isArrayType() const {
100 // If this is directly a reference type, return it.
101 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
102 return ATy;
103
104 // If this is a typedef for an array type, strip the typedef off without
105 // losing all typedef information.
106 if (isa<ArrayType>(CanonicalType))
107 return cast<ArrayType>(cast<TypedefType>(this)->LookThroughTypedefs());
108 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000109}
110
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000111const RecordType *Type::isRecordType() const {
112 // If this is directly a reference type, return it.
113 if (const RecordType *RTy = dyn_cast<RecordType>(this))
114 return RTy;
115
116 // If this is a typedef for an record type, strip the typedef off without
117 // losing all typedef information.
118 if (isa<RecordType>(CanonicalType))
119 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Steve Naroffadc01852007-07-26 03:18:02 +0000120 return 0;
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000121}
122
Steve Naroff7064f5c2007-07-26 18:32:01 +0000123const TagType *Type::isStructureType() const {
124 // If this is directly a structure type, return it.
125 if (const TagType *TT = dyn_cast<TagType>(this)) {
126 if (TT->getDecl()->getKind() == Decl::Struct)
127 return TT;
128 }
129 // If this is a typedef for a structure type, strip the typedef off without
130 // losing all typedef information.
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
132 if (TT->getDecl()->getKind() == Decl::Struct)
Steve Naroff7064f5c2007-07-26 18:32:01 +0000133 return cast<TagType>(cast<TypedefType>(this)->LookThroughTypedefs());
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000135 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000136}
137
Steve Naroff7064f5c2007-07-26 18:32:01 +0000138const TagType *Type::isUnionType() const {
139 // If this is directly a union type, return it.
140 if (const TagType *TT = dyn_cast<TagType>(this)) {
141 if (TT->getDecl()->getKind() == Decl::Union)
142 return TT;
143 }
144 // If this is a typedef for a union type, strip the typedef off without
145 // losing all typedef information.
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
147 if (TT->getDecl()->getKind() == Decl::Union)
Steve Naroff7064f5c2007-07-26 18:32:01 +0000148 return cast<TagType>(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 Lattner7a2e0472007-07-16 00:23:25 +0000153bool Type::isComplexType() const {
154 return isa<ComplexType>(CanonicalType);
155}
156
157const VectorType *Type::isVectorType() const {
158 // Are we directly a vector type?
159 if (const VectorType *VTy = dyn_cast<VectorType>(this))
160 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000161
162 // If this is a typedef for a vector type, strip the typedef off without
163 // losing all typedef information.
164 if (isa<VectorType>(CanonicalType))
165 return cast<VectorType>(cast<TypedefType>(this)->LookThroughTypedefs());
166
Chris Lattner7a2e0472007-07-16 00:23:25 +0000167 return 0;
168}
169
Steve Naroff7064f5c2007-07-26 18:32:01 +0000170const OCUVectorType *Type::isOCUVectorType() const {
171 // Are we directly an OpenCU vector type?
172 if (const OCUVectorType *VTy = dyn_cast<OCUVectorType>(this))
173 return VTy;
174
175 // If this is a typedef for an OpenCU vector type, strip the typedef off
176 // without losing all typedef information.
177 if (isa<OCUVectorType>(CanonicalType))
178 return cast<OCUVectorType>(cast<TypedefType>(this)->LookThroughTypedefs());
179
180 return 0;
181}
182
Chris Lattner7a2e0472007-07-16 00:23:25 +0000183
Reid Spencer5f016e22007-07-11 17:01:13 +0000184// C99 6.2.7p1: If both are complete types, then the following additional
185// requirements apply...FIXME (handle compatibility across source files).
186bool Type::tagTypesAreCompatible(QualType lhs, QualType rhs) {
187 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
188 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
189
190 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
191 if (ldecl->getIdentifier() == rdecl->getIdentifier())
192 return true;
193 }
194 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
195 if (ldecl->getIdentifier() == rdecl->getIdentifier())
196 return true;
197 }
198 return false;
199}
200
201bool Type::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
202 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
203 // identically qualified and both shall be pointers to compatible types.
204 if (lhs.getQualifiers() != rhs.getQualifiers())
205 return false;
206
207 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
208 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
209
210 return typesAreCompatible(ltype, rtype);
211}
212
213// C++ 5.17p6: When the left opperand of an assignment operator denotes a
214// reference to T, the operation assigns to the object of type T denoted by the
215// reference.
216bool Type::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
217 QualType ltype = lhs;
218
219 if (lhs->isReferenceType())
220 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
221
222 QualType rtype = rhs;
223
224 if (rhs->isReferenceType())
225 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
226
227 return typesAreCompatible(ltype, rtype);
228}
229
230bool Type::functionTypesAreCompatible(QualType lhs, QualType rhs) {
231 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
232 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
233 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
234 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
235
236 // first check the return types (common between C99 and K&R).
237 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
238 return false;
239
240 if (lproto && rproto) { // two C99 style function prototypes
241 unsigned lproto_nargs = lproto->getNumArgs();
242 unsigned rproto_nargs = rproto->getNumArgs();
243
244 if (lproto_nargs != rproto_nargs)
245 return false;
246
247 // both prototypes have the same number of arguments.
248 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
249 (rproto->isVariadic() && !lproto->isVariadic()))
250 return false;
251
252 // The use of ellipsis agree...now check the argument types.
253 for (unsigned i = 0; i < lproto_nargs; i++)
254 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
255 return false;
256 return true;
257 }
258 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
259 return true;
260
261 // we have a mixture of K&R style with C99 prototypes
262 const FunctionTypeProto *proto = lproto ? lproto : rproto;
263
264 if (proto->isVariadic())
265 return false;
266
267 // FIXME: Each parameter type T in the prototype must be compatible with the
268 // type resulting from applying the usual argument conversions to T.
269 return true;
270}
271
272bool Type::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
273 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
274 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
275
276 if (!typesAreCompatible(ltype, rtype))
277 return false;
278
279 // FIXME: If both types specify constant sizes, then the sizes must also be
280 // the same. Even if the sizes are the same, GCC produces an error.
281 return true;
282}
283
284/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
285/// both shall have the identically qualified version of a compatible type.
286/// C99 6.2.7p1: Two types have compatible types if their types are the
287/// same. See 6.7.[2,3,5] for additional rules.
288bool Type::typesAreCompatible(QualType lhs, QualType rhs) {
289 QualType lcanon = lhs.getCanonicalType();
290 QualType rcanon = rhs.getCanonicalType();
291
292 // If two types are identical, they are are compatible
293 if (lcanon == rcanon)
294 return true;
295
296 // If the canonical type classes don't match, they can't be compatible
297 if (lcanon->getTypeClass() != rcanon->getTypeClass())
298 return false;
299
300 switch (lcanon->getTypeClass()) {
301 case Type::Pointer:
302 return pointerTypesAreCompatible(lcanon, rcanon);
303 case Type::Reference:
304 return referenceTypesAreCompatible(lcanon, rcanon);
305 case Type::Array:
306 return arrayTypesAreCompatible(lcanon, rcanon);
307 case Type::FunctionNoProto:
308 case Type::FunctionProto:
309 return functionTypesAreCompatible(lcanon, rcanon);
310 case Type::Tagged: // handle structures, unions
311 return tagTypesAreCompatible(lcanon, rcanon);
312 case Type::Builtin:
313 return false;
314 default:
315 assert(0 && "unexpected type");
316 }
317 return true; // should never get here...
318}
319
320bool Type::isIntegerType() const {
321 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
322 return BT->getKind() >= BuiltinType::Bool &&
323 BT->getKind() <= BuiltinType::LongLong;
324 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
325 if (TT->getDecl()->getKind() == Decl::Enum)
326 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000327 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
328 return VT->getElementType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000329 return false;
330}
331
332bool Type::isSignedIntegerType() const {
333 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
334 return BT->getKind() >= BuiltinType::Char_S &&
335 BT->getKind() <= BuiltinType::LongLong;
336 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000337 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
338 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000339 return false;
340}
341
342bool Type::isUnsignedIntegerType() const {
343 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
344 return BT->getKind() >= BuiltinType::Bool &&
345 BT->getKind() <= BuiltinType::ULongLong;
346 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000347 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
348 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 return false;
350}
351
352bool Type::isFloatingType() const {
353 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
354 return BT->getKind() >= BuiltinType::Float &&
355 BT->getKind() <= BuiltinType::LongDouble;
356 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
357 return CT->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000358 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
359 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 return false;
361}
362
363bool Type::isRealFloatingType() const {
364 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
365 return BT->getKind() >= BuiltinType::Float &&
366 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000367 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
368 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000369 return false;
370}
371
372bool Type::isRealType() const {
373 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
374 return BT->getKind() >= BuiltinType::Bool &&
375 BT->getKind() <= BuiltinType::LongDouble;
376 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
377 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000378 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
379 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 return false;
381}
382
Reid Spencer5f016e22007-07-11 17:01:13 +0000383bool Type::isArithmeticType() const {
384 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
385 return BT->getKind() != BuiltinType::Void;
386 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
387 if (TT->getDecl()->getKind() == Decl::Enum)
388 return true;
389 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
390}
391
392bool Type::isScalarType() const {
393 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
394 return BT->getKind() != BuiltinType::Void;
395 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
396 if (TT->getDecl()->getKind() == Decl::Enum)
397 return true;
398 return false;
399 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000400 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
401 isa<VectorType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000402}
403
404bool Type::isAggregateType() const {
405 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
406 if (TT->getDecl()->getKind() == Decl::Struct)
407 return true;
408 return false;
409 }
410 return CanonicalType->getTypeClass() == Array;
411}
412
413// The only variable size types are auto arrays within a function. Structures
414// cannot contain a VLA member. They can have a flexible array member, however
415// the structure is still constant size (C99 6.7.2.1p16).
Chris Lattner590b6642007-07-15 23:26:56 +0000416bool Type::isConstantSizeType(ASTContext &Ctx, SourceLocation *loc) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000417 if (const ArrayType *Ary = dyn_cast<ArrayType>(CanonicalType)) {
Chris Lattner8b9023b2007-07-13 03:05:23 +0000418 assert(Ary->getSizeExpr() && "Incomplete types don't have a size at all!");
419 // Variable Length Array?
Chris Lattner590b6642007-07-15 23:26:56 +0000420 return Ary->getSizeExpr()->isIntegerConstantExpr(Ctx, loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000421 }
422 return true;
423}
424
425/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
426/// - a type that can describe objects, but which lacks information needed to
427/// determine its size.
428bool Type::isIncompleteType() const {
429 switch (CanonicalType->getTypeClass()) {
430 default: return false;
431 case Builtin:
432 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
433 // be completed.
434 return isVoidType();
435 case Tagged:
436 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
437 // forward declaration, but not a full definition (C99 6.2.5p22).
438 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
439 case Array:
440 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Chris Lattner8b9023b2007-07-13 03:05:23 +0000441 return cast<ArrayType>(CanonicalType)->getSizeExpr() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 }
443}
444
445bool Type::isPromotableIntegerType() const {
446 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
447 if (!BT) return false;
448 switch (BT->getKind()) {
449 case BuiltinType::Bool:
450 case BuiltinType::Char_S:
451 case BuiltinType::Char_U:
452 case BuiltinType::SChar:
453 case BuiltinType::UChar:
454 case BuiltinType::Short:
455 case BuiltinType::UShort:
456 return true;
457 default:
458 return false;
459 }
460}
461
462const char *BuiltinType::getName() const {
463 switch (getKind()) {
464 default: assert(0 && "Unknown builtin type!");
465 case Void: return "void";
466 case Bool: return "_Bool";
467 case Char_S: return "char";
468 case Char_U: return "char";
469 case SChar: return "signed char";
470 case Short: return "short";
471 case Int: return "int";
472 case Long: return "long";
473 case LongLong: return "long long";
474 case UChar: return "unsigned char";
475 case UShort: return "unsigned short";
476 case UInt: return "unsigned int";
477 case ULong: return "unsigned long";
478 case ULongLong: return "unsigned long long";
479 case Float: return "float";
480 case Double: return "double";
481 case LongDouble: return "long double";
482 }
483}
484
Reid Spencer5f016e22007-07-11 17:01:13 +0000485void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000486 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000487 unsigned NumArgs, bool isVariadic) {
488 ID.AddPointer(Result.getAsOpaquePtr());
489 for (unsigned i = 0; i != NumArgs; ++i)
490 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
491 ID.AddInteger(isVariadic);
492}
493
494void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000495 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000496}
497
Chris Lattnera2c77672007-07-16 22:05:22 +0000498/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
499/// potentially looking through *all* consequtive typedefs. This returns the
500/// sum of the type qualifiers, so if you have:
501/// typedef const int A;
502/// typedef volatile A B;
503/// looking through the typedefs for B will give you "const volatile A".
504///
505QualType TypedefType::LookThroughTypedefs() const {
506 // Usually, there is only a single level of typedefs, be fast in that case.
507 QualType FirstType = getDecl()->getUnderlyingType();
508 if (!isa<TypedefType>(FirstType))
509 return FirstType;
510
511 // Otherwise, do the fully general loop.
512 unsigned TypeQuals = 0;
513 const TypedefType *TDT = this;
514 while (1) {
515 QualType CurType = TDT->getDecl()->getUnderlyingType();
516 TypeQuals |= CurType.getQualifiers();
517
518 TDT = dyn_cast<TypedefType>(CurType);
519 if (TDT == 0)
520 return QualType(CurType.getTypePtr(), TypeQuals);
521 }
522}
Reid Spencer5f016e22007-07-11 17:01:13 +0000523
524bool RecordType::classof(const Type *T) {
525 if (const TagType *TT = dyn_cast<TagType>(T))
526 return isa<RecordDecl>(TT->getDecl());
527 return false;
528}
529
530
531//===----------------------------------------------------------------------===//
532// Type Printing
533//===----------------------------------------------------------------------===//
534
535void QualType::dump(const char *msg) const {
536 std::string R = "foo";
537 getAsStringInternal(R);
538 if (msg)
539 fprintf(stderr, "%s: %s\n", msg, R.c_str());
540 else
541 fprintf(stderr, "%s\n", R.c_str());
542}
543
544static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
545 // Note: funkiness to ensure we get a space only between quals.
546 bool NonePrinted = true;
547 if (TypeQuals & QualType::Const)
548 S += "const", NonePrinted = false;
549 if (TypeQuals & QualType::Volatile)
550 S += (NonePrinted+" volatile"), NonePrinted = false;
551 if (TypeQuals & QualType::Restrict)
552 S += (NonePrinted+" restrict"), NonePrinted = false;
553}
554
555void QualType::getAsStringInternal(std::string &S) const {
556 if (isNull()) {
557 S += "NULL TYPE\n";
558 return;
559 }
560
561 // Print qualifiers as appropriate.
Anton Korobeynikovb7b50bc2007-07-13 00:48:55 +0000562 unsigned TQ = getQualifiers();
563 if (TQ) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 std::string TQS;
565 AppendTypeQualList(TQS, TQ);
566 if (!S.empty())
567 S = TQS + ' ' + S;
568 else
569 S = TQS;
570 }
571
572 getTypePtr()->getAsStringInternal(S);
573}
574
575void BuiltinType::getAsStringInternal(std::string &S) const {
576 if (S.empty()) {
577 S = getName();
578 } else {
579 // Prefix the basic type, e.g. 'int X'.
580 S = ' ' + S;
581 S = getName() + S;
582 }
583}
584
585void ComplexType::getAsStringInternal(std::string &S) const {
586 ElementType->getAsStringInternal(S);
587 S = "_Complex " + S;
588}
589
590void PointerType::getAsStringInternal(std::string &S) const {
591 S = '*' + S;
592
593 // Handle things like 'int (*A)[4];' correctly.
594 // FIXME: this should include vectors, but vectors use attributes I guess.
595 if (isa<ArrayType>(PointeeType.getTypePtr()))
596 S = '(' + S + ')';
597
598 PointeeType.getAsStringInternal(S);
599}
600
601void ReferenceType::getAsStringInternal(std::string &S) const {
602 S = '&' + S;
603
604 // Handle things like 'int (&A)[4];' correctly.
605 // FIXME: this should include vectors, but vectors use attributes I guess.
606 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
607 S = '(' + S + ')';
608
609 ReferenceeType.getAsStringInternal(S);
610}
611
612void ArrayType::getAsStringInternal(std::string &S) const {
613 S += '[';
614
615 if (IndexTypeQuals) {
616 AppendTypeQualList(S, IndexTypeQuals);
617 S += ' ';
618 }
619
620 if (SizeModifier == Static)
621 S += "static";
622 else if (SizeModifier == Star)
623 S += '*';
624
625 S += ']';
626
627 ElementType.getAsStringInternal(S);
628}
629
630void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattnere107b5d2007-07-13 21:01:17 +0000631 S += " __attribute__((vector_size(";
632 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000633 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000634 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 ElementType.getAsStringInternal(S);
636}
637
Steve Naroff31a45842007-07-28 23:10:27 +0000638void OCUVectorType::getAsStringInternal(std::string &S) const {
639 S += " __attribute__((ocu_vector_type(";
640 S += llvm::utostr_32(NumElements);
641 S += ")))";
642 ElementType.getAsStringInternal(S);
643}
644
Steve Naroffd1861fd2007-07-31 12:34:36 +0000645void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
646 // FIXME: output expression, getUnderlyingExpr()->print().
647 // At the moment, Stmt::print(std::ostream) doesn't work for us here.
648 InnerString = "typeof(<expr>) " + InnerString;
649}
650
651void TypeOfType::getAsStringInternal(std::string &S) const {
652 std::string Tmp;
653 getUnderlyingType().getAsStringInternal(Tmp);
654 S += "typeof(" + Tmp + ")";
655}
656
Reid Spencer5f016e22007-07-11 17:01:13 +0000657void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
658 // If needed for precedence reasons, wrap the inner part in grouping parens.
659 if (!S.empty())
660 S = "(" + S + ")";
661
662 S += "()";
663 getResultType().getAsStringInternal(S);
664}
665
666void FunctionTypeProto::getAsStringInternal(std::string &S) const {
667 // If needed for precedence reasons, wrap the inner part in grouping parens.
668 if (!S.empty())
669 S = "(" + S + ")";
670
671 S += "(";
672 std::string Tmp;
673 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
674 if (i) S += ", ";
675 getArgType(i).getAsStringInternal(Tmp);
676 S += Tmp;
677 Tmp.clear();
678 }
679
680 if (isVariadic()) {
681 if (getNumArgs())
682 S += ", ";
683 S += "...";
684 } else if (getNumArgs() == 0) {
685 // Do not emit int() if we have a proto, emit 'int(void)'.
686 S += "void";
687 }
688
689 S += ")";
690 getResultType().getAsStringInternal(S);
691}
692
693
694void TypedefType::getAsStringInternal(std::string &InnerString) const {
695 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
696 InnerString = ' ' + InnerString;
697 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
698}
699
700void TagType::getAsStringInternal(std::string &InnerString) const {
701 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
702 InnerString = ' ' + InnerString;
703
704 const char *Kind = getDecl()->getKindName();
705 const char *ID;
706 if (const IdentifierInfo *II = getDecl()->getIdentifier())
707 ID = II->getName();
708 else
709 ID = "<anonymous>";
710
711 InnerString = std::string(Kind) + " " + ID + InnerString;
712}