blob: afdd3cc1f7ebbedbe4977f6ea402e1e2667501a7 [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
74const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +000075 // If this is directly a function type, return it.
76 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
77 return FTy;
Chris Lattnerc8629632007-07-31 19:29:30 +000078
Steve Naroff7064f5c2007-07-26 18:32:01 +000079 // If this is a typedef for a function type, strip the typedef off without
80 // losing all typedef information.
81 if (isa<FunctionType>(CanonicalType))
82 return cast<FunctionType>(cast<TypedefType>(this)->LookThroughTypedefs());
83 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000084}
85
Chris Lattnerbefee482007-07-31 16:53:04 +000086const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +000087 // If this is directly a pointer type, return it.
88 if (const PointerType *PTy = dyn_cast<PointerType>(this))
89 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +000090
91 // If this is a typedef for a pointer type, strip the typedef off without
92 // losing all typedef information.
93 if (isa<PointerType>(CanonicalType))
94 return cast<PointerType>(cast<TypedefType>(this)->LookThroughTypedefs());
Chris Lattner3acb1382007-07-16 00:13:25 +000095 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000096}
97
Chris Lattnera1d9fde2007-07-31 16:56:34 +000098const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +000099 // If this is directly a reference type, return it.
100 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
101 return RTy;
102
103 // If this is a typedef for a reference type, strip the typedef off without
104 // losing all typedef information.
105 if (isa<ReferenceType>(CanonicalType))
106 return cast<ReferenceType>(cast<TypedefType>(this)->LookThroughTypedefs());
107 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000108}
109
Chris Lattnerc8629632007-07-31 19:29:30 +0000110const ArrayType *Type::getAsArrayType() const {
Steve Naroff700204c2007-07-24 21:46:40 +0000111 // If this is directly a reference type, return it.
112 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
113 return ATy;
114
115 // If this is a typedef for an array type, strip the typedef off without
116 // losing all typedef information.
117 if (isa<ArrayType>(CanonicalType))
118 return cast<ArrayType>(cast<TypedefType>(this)->LookThroughTypedefs());
119 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000120}
121
Chris Lattnerc8629632007-07-31 19:29:30 +0000122const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000123 // If this is directly a reference type, return it.
124 if (const RecordType *RTy = dyn_cast<RecordType>(this))
125 return RTy;
126
127 // If this is a typedef for an record type, strip the typedef off without
128 // losing all typedef information.
129 if (isa<RecordType>(CanonicalType))
130 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Steve Naroffadc01852007-07-26 03:18:02 +0000131 return 0;
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000132}
133
Chris Lattnerc8629632007-07-31 19:29:30 +0000134const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000135 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000136 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
137 if (RT->getDecl()->getKind() == Decl::Struct)
138 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000139 }
140 // If this is a typedef for a structure type, strip the typedef off without
141 // losing all typedef information.
Chris Lattnerc8629632007-07-31 19:29:30 +0000142 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
143 if (RT->getDecl()->getKind() == Decl::Struct)
144 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Reid Spencer5f016e22007-07-11 17:01:13 +0000145 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000146 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000147}
148
Chris Lattnerc8629632007-07-31 19:29:30 +0000149const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000150 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000151 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
152 if (RT->getDecl()->getKind() == Decl::Union)
153 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000154 }
155 // If this is a typedef for a union type, strip the typedef off without
156 // losing all typedef information.
Chris Lattnerc8629632007-07-31 19:29:30 +0000157 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
158 if (RT->getDecl()->getKind() == Decl::Union)
159 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000161 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000162}
163
Chris Lattner7a2e0472007-07-16 00:23:25 +0000164bool Type::isComplexType() const {
165 return isa<ComplexType>(CanonicalType);
166}
167
Chris Lattnerc8629632007-07-31 19:29:30 +0000168const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000169 // Are we directly a vector type?
170 if (const VectorType *VTy = dyn_cast<VectorType>(this))
171 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000172
173 // If this is a typedef for a vector type, strip the typedef off without
174 // losing all typedef information.
175 if (isa<VectorType>(CanonicalType))
176 return cast<VectorType>(cast<TypedefType>(this)->LookThroughTypedefs());
177
Chris Lattner7a2e0472007-07-16 00:23:25 +0000178 return 0;
179}
180
Chris Lattnerc8629632007-07-31 19:29:30 +0000181const OCUVectorType *Type::getAsOCUVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000182 // Are we directly an OpenCU vector type?
183 if (const OCUVectorType *VTy = dyn_cast<OCUVectorType>(this))
184 return VTy;
185
186 // If this is a typedef for an OpenCU vector type, strip the typedef off
187 // without losing all typedef information.
188 if (isa<OCUVectorType>(CanonicalType))
189 return cast<OCUVectorType>(cast<TypedefType>(this)->LookThroughTypedefs());
190
191 return 0;
192}
193
Chris Lattner7a2e0472007-07-16 00:23:25 +0000194
Reid Spencer5f016e22007-07-11 17:01:13 +0000195// C99 6.2.7p1: If both are complete types, then the following additional
196// requirements apply...FIXME (handle compatibility across source files).
197bool Type::tagTypesAreCompatible(QualType lhs, QualType rhs) {
198 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
199 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
200
201 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
202 if (ldecl->getIdentifier() == rdecl->getIdentifier())
203 return true;
204 }
205 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
206 if (ldecl->getIdentifier() == rdecl->getIdentifier())
207 return true;
208 }
209 return false;
210}
211
212bool Type::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
213 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
214 // identically qualified and both shall be pointers to compatible types.
215 if (lhs.getQualifiers() != rhs.getQualifiers())
216 return false;
217
218 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
219 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
220
221 return typesAreCompatible(ltype, rtype);
222}
223
224// C++ 5.17p6: When the left opperand of an assignment operator denotes a
225// reference to T, the operation assigns to the object of type T denoted by the
226// reference.
227bool Type::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
228 QualType ltype = lhs;
229
230 if (lhs->isReferenceType())
231 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
232
233 QualType rtype = rhs;
234
235 if (rhs->isReferenceType())
236 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
237
238 return typesAreCompatible(ltype, rtype);
239}
240
241bool Type::functionTypesAreCompatible(QualType lhs, QualType rhs) {
242 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
243 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
244 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
245 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
246
247 // first check the return types (common between C99 and K&R).
248 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
249 return false;
250
251 if (lproto && rproto) { // two C99 style function prototypes
252 unsigned lproto_nargs = lproto->getNumArgs();
253 unsigned rproto_nargs = rproto->getNumArgs();
254
255 if (lproto_nargs != rproto_nargs)
256 return false;
257
258 // both prototypes have the same number of arguments.
259 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
260 (rproto->isVariadic() && !lproto->isVariadic()))
261 return false;
262
263 // The use of ellipsis agree...now check the argument types.
264 for (unsigned i = 0; i < lproto_nargs; i++)
265 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
266 return false;
267 return true;
268 }
269 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
270 return true;
271
272 // we have a mixture of K&R style with C99 prototypes
273 const FunctionTypeProto *proto = lproto ? lproto : rproto;
274
275 if (proto->isVariadic())
276 return false;
277
278 // FIXME: Each parameter type T in the prototype must be compatible with the
279 // type resulting from applying the usual argument conversions to T.
280 return true;
281}
282
283bool Type::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
284 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
285 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
286
287 if (!typesAreCompatible(ltype, rtype))
288 return false;
289
290 // FIXME: If both types specify constant sizes, then the sizes must also be
291 // the same. Even if the sizes are the same, GCC produces an error.
292 return true;
293}
294
295/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
296/// both shall have the identically qualified version of a compatible type.
297/// C99 6.2.7p1: Two types have compatible types if their types are the
298/// same. See 6.7.[2,3,5] for additional rules.
299bool Type::typesAreCompatible(QualType lhs, QualType rhs) {
300 QualType lcanon = lhs.getCanonicalType();
301 QualType rcanon = rhs.getCanonicalType();
302
303 // If two types are identical, they are are compatible
304 if (lcanon == rcanon)
305 return true;
306
307 // If the canonical type classes don't match, they can't be compatible
308 if (lcanon->getTypeClass() != rcanon->getTypeClass())
309 return false;
310
311 switch (lcanon->getTypeClass()) {
312 case Type::Pointer:
313 return pointerTypesAreCompatible(lcanon, rcanon);
314 case Type::Reference:
315 return referenceTypesAreCompatible(lcanon, rcanon);
316 case Type::Array:
317 return arrayTypesAreCompatible(lcanon, rcanon);
318 case Type::FunctionNoProto:
319 case Type::FunctionProto:
320 return functionTypesAreCompatible(lcanon, rcanon);
321 case Type::Tagged: // handle structures, unions
322 return tagTypesAreCompatible(lcanon, rcanon);
323 case Type::Builtin:
324 return false;
325 default:
326 assert(0 && "unexpected type");
327 }
328 return true; // should never get here...
329}
330
331bool Type::isIntegerType() const {
332 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
333 return BT->getKind() >= BuiltinType::Bool &&
334 BT->getKind() <= BuiltinType::LongLong;
335 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
336 if (TT->getDecl()->getKind() == Decl::Enum)
337 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000338 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
339 return VT->getElementType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000340 return false;
341}
342
343bool Type::isSignedIntegerType() const {
344 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
345 return BT->getKind() >= BuiltinType::Char_S &&
346 BT->getKind() <= BuiltinType::LongLong;
347 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000348 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
349 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 return false;
351}
352
353bool Type::isUnsignedIntegerType() const {
354 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
355 return BT->getKind() >= BuiltinType::Bool &&
356 BT->getKind() <= BuiltinType::ULongLong;
357 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000358 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
359 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000360 return false;
361}
362
363bool Type::isFloatingType() const {
364 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
365 return BT->getKind() >= BuiltinType::Float &&
366 BT->getKind() <= BuiltinType::LongDouble;
367 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
368 return CT->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000369 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
370 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 return false;
372}
373
374bool Type::isRealFloatingType() const {
375 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
376 return BT->getKind() >= BuiltinType::Float &&
377 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000378 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
379 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000380 return false;
381}
382
383bool Type::isRealType() const {
384 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
385 return BT->getKind() >= BuiltinType::Bool &&
386 BT->getKind() <= BuiltinType::LongDouble;
387 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
388 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000389 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
390 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000391 return false;
392}
393
Reid Spencer5f016e22007-07-11 17:01:13 +0000394bool Type::isArithmeticType() const {
395 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
396 return BT->getKind() != BuiltinType::Void;
397 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
398 if (TT->getDecl()->getKind() == Decl::Enum)
399 return true;
400 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
401}
402
403bool Type::isScalarType() const {
404 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
405 return BT->getKind() != BuiltinType::Void;
406 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
407 if (TT->getDecl()->getKind() == Decl::Enum)
408 return true;
409 return false;
410 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000411 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
412 isa<VectorType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000413}
414
415bool Type::isAggregateType() const {
416 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
417 if (TT->getDecl()->getKind() == Decl::Struct)
418 return true;
419 return false;
420 }
421 return CanonicalType->getTypeClass() == Array;
422}
423
424// The only variable size types are auto arrays within a function. Structures
425// cannot contain a VLA member. They can have a flexible array member, however
426// the structure is still constant size (C99 6.7.2.1p16).
Chris Lattner590b6642007-07-15 23:26:56 +0000427bool Type::isConstantSizeType(ASTContext &Ctx, SourceLocation *loc) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000428 if (const ArrayType *Ary = dyn_cast<ArrayType>(CanonicalType)) {
Chris Lattner8b9023b2007-07-13 03:05:23 +0000429 assert(Ary->getSizeExpr() && "Incomplete types don't have a size at all!");
430 // Variable Length Array?
Chris Lattner590b6642007-07-15 23:26:56 +0000431 return Ary->getSizeExpr()->isIntegerConstantExpr(Ctx, loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000432 }
433 return true;
434}
435
436/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
437/// - a type that can describe objects, but which lacks information needed to
438/// determine its size.
439bool Type::isIncompleteType() const {
440 switch (CanonicalType->getTypeClass()) {
441 default: return false;
442 case Builtin:
443 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
444 // be completed.
445 return isVoidType();
446 case Tagged:
447 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
448 // forward declaration, but not a full definition (C99 6.2.5p22).
449 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
450 case Array:
451 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Chris Lattner8b9023b2007-07-13 03:05:23 +0000452 return cast<ArrayType>(CanonicalType)->getSizeExpr() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000453 }
454}
455
456bool Type::isPromotableIntegerType() const {
457 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
458 if (!BT) return false;
459 switch (BT->getKind()) {
460 case BuiltinType::Bool:
461 case BuiltinType::Char_S:
462 case BuiltinType::Char_U:
463 case BuiltinType::SChar:
464 case BuiltinType::UChar:
465 case BuiltinType::Short:
466 case BuiltinType::UShort:
467 return true;
468 default:
469 return false;
470 }
471}
472
473const char *BuiltinType::getName() const {
474 switch (getKind()) {
475 default: assert(0 && "Unknown builtin type!");
476 case Void: return "void";
477 case Bool: return "_Bool";
478 case Char_S: return "char";
479 case Char_U: return "char";
480 case SChar: return "signed char";
481 case Short: return "short";
482 case Int: return "int";
483 case Long: return "long";
484 case LongLong: return "long long";
485 case UChar: return "unsigned char";
486 case UShort: return "unsigned short";
487 case UInt: return "unsigned int";
488 case ULong: return "unsigned long";
489 case ULongLong: return "unsigned long long";
490 case Float: return "float";
491 case Double: return "double";
492 case LongDouble: return "long double";
493 }
494}
495
Reid Spencer5f016e22007-07-11 17:01:13 +0000496void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000497 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000498 unsigned NumArgs, bool isVariadic) {
499 ID.AddPointer(Result.getAsOpaquePtr());
500 for (unsigned i = 0; i != NumArgs; ++i)
501 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
502 ID.AddInteger(isVariadic);
503}
504
505void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000506 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000507}
508
Chris Lattnera2c77672007-07-16 22:05:22 +0000509/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
510/// potentially looking through *all* consequtive typedefs. This returns the
511/// sum of the type qualifiers, so if you have:
512/// typedef const int A;
513/// typedef volatile A B;
514/// looking through the typedefs for B will give you "const volatile A".
515///
516QualType TypedefType::LookThroughTypedefs() const {
517 // Usually, there is only a single level of typedefs, be fast in that case.
518 QualType FirstType = getDecl()->getUnderlyingType();
519 if (!isa<TypedefType>(FirstType))
520 return FirstType;
521
522 // Otherwise, do the fully general loop.
523 unsigned TypeQuals = 0;
524 const TypedefType *TDT = this;
525 while (1) {
526 QualType CurType = TDT->getDecl()->getUnderlyingType();
527 TypeQuals |= CurType.getQualifiers();
528
529 TDT = dyn_cast<TypedefType>(CurType);
530 if (TDT == 0)
531 return QualType(CurType.getTypePtr(), TypeQuals);
532 }
533}
Reid Spencer5f016e22007-07-11 17:01:13 +0000534
535bool RecordType::classof(const Type *T) {
536 if (const TagType *TT = dyn_cast<TagType>(T))
537 return isa<RecordDecl>(TT->getDecl());
538 return false;
539}
540
541
542//===----------------------------------------------------------------------===//
543// Type Printing
544//===----------------------------------------------------------------------===//
545
546void QualType::dump(const char *msg) const {
547 std::string R = "foo";
548 getAsStringInternal(R);
549 if (msg)
550 fprintf(stderr, "%s: %s\n", msg, R.c_str());
551 else
552 fprintf(stderr, "%s\n", R.c_str());
553}
554
555static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
556 // Note: funkiness to ensure we get a space only between quals.
557 bool NonePrinted = true;
558 if (TypeQuals & QualType::Const)
559 S += "const", NonePrinted = false;
560 if (TypeQuals & QualType::Volatile)
561 S += (NonePrinted+" volatile"), NonePrinted = false;
562 if (TypeQuals & QualType::Restrict)
563 S += (NonePrinted+" restrict"), NonePrinted = false;
564}
565
566void QualType::getAsStringInternal(std::string &S) const {
567 if (isNull()) {
568 S += "NULL TYPE\n";
569 return;
570 }
571
572 // Print qualifiers as appropriate.
Anton Korobeynikovb7b50bc2007-07-13 00:48:55 +0000573 unsigned TQ = getQualifiers();
574 if (TQ) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 std::string TQS;
576 AppendTypeQualList(TQS, TQ);
577 if (!S.empty())
578 S = TQS + ' ' + S;
579 else
580 S = TQS;
581 }
582
583 getTypePtr()->getAsStringInternal(S);
584}
585
586void BuiltinType::getAsStringInternal(std::string &S) const {
587 if (S.empty()) {
588 S = getName();
589 } else {
590 // Prefix the basic type, e.g. 'int X'.
591 S = ' ' + S;
592 S = getName() + S;
593 }
594}
595
596void ComplexType::getAsStringInternal(std::string &S) const {
597 ElementType->getAsStringInternal(S);
598 S = "_Complex " + S;
599}
600
601void PointerType::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>(PointeeType.getTypePtr()))
607 S = '(' + S + ')';
608
609 PointeeType.getAsStringInternal(S);
610}
611
612void ReferenceType::getAsStringInternal(std::string &S) const {
613 S = '&' + S;
614
615 // Handle things like 'int (&A)[4];' correctly.
616 // FIXME: this should include vectors, but vectors use attributes I guess.
617 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
618 S = '(' + S + ')';
619
620 ReferenceeType.getAsStringInternal(S);
621}
622
623void ArrayType::getAsStringInternal(std::string &S) const {
624 S += '[';
625
626 if (IndexTypeQuals) {
627 AppendTypeQualList(S, IndexTypeQuals);
628 S += ' ';
629 }
630
631 if (SizeModifier == Static)
632 S += "static";
633 else if (SizeModifier == Star)
634 S += '*';
635
636 S += ']';
637
638 ElementType.getAsStringInternal(S);
639}
640
641void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattnere107b5d2007-07-13 21:01:17 +0000642 S += " __attribute__((vector_size(";
643 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000645 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 ElementType.getAsStringInternal(S);
647}
648
Steve Naroff31a45842007-07-28 23:10:27 +0000649void OCUVectorType::getAsStringInternal(std::string &S) const {
650 S += " __attribute__((ocu_vector_type(";
651 S += llvm::utostr_32(NumElements);
652 S += ")))";
653 ElementType.getAsStringInternal(S);
654}
655
Steve Naroffd1861fd2007-07-31 12:34:36 +0000656void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000657 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
658 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000659 std::ostringstream s;
660 getUnderlyingExpr()->print(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000661 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000662}
663
Steve Naroff363bcff2007-08-01 23:45:51 +0000664void TypeOfType::getAsStringInternal(std::string &InnerString) const {
665 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
666 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000667 std::string Tmp;
668 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000669 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000670}
671
Reid Spencer5f016e22007-07-11 17:01:13 +0000672void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
673 // If needed for precedence reasons, wrap the inner part in grouping parens.
674 if (!S.empty())
675 S = "(" + S + ")";
676
677 S += "()";
678 getResultType().getAsStringInternal(S);
679}
680
681void FunctionTypeProto::getAsStringInternal(std::string &S) const {
682 // If needed for precedence reasons, wrap the inner part in grouping parens.
683 if (!S.empty())
684 S = "(" + S + ")";
685
686 S += "(";
687 std::string Tmp;
688 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
689 if (i) S += ", ";
690 getArgType(i).getAsStringInternal(Tmp);
691 S += Tmp;
692 Tmp.clear();
693 }
694
695 if (isVariadic()) {
696 if (getNumArgs())
697 S += ", ";
698 S += "...";
699 } else if (getNumArgs() == 0) {
700 // Do not emit int() if we have a proto, emit 'int(void)'.
701 S += "void";
702 }
703
704 S += ")";
705 getResultType().getAsStringInternal(S);
706}
707
708
709void TypedefType::getAsStringInternal(std::string &InnerString) const {
710 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
711 InnerString = ' ' + InnerString;
712 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
713}
714
715void TagType::getAsStringInternal(std::string &InnerString) const {
716 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
717 InnerString = ' ' + InnerString;
718
719 const char *Kind = getDecl()->getKindName();
720 const char *ID;
721 if (const IdentifierInfo *II = getDecl()->getIdentifier())
722 ID = II->getName();
723 else
724 ID = "<anonymous>";
725
726 InnerString = std::string(Kind) + " " + ID + InnerString;
727}