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