blob: 9fca35a66e38198ea6d5d32436b4e65e090e74a5 [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
Steve Naroff77878cc2007-08-27 04:08:11 +000078const BuiltinType *Type::getAsBuiltinType() const {
79 // If this is directly a builtin type, return it.
80 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
81 return BTy;
82
83 // If this is a typedef for a builtin type, strip the typedef off without
84 // losing all typedef information.
85 if (isa<BuiltinType>(CanonicalType))
86 return cast<BuiltinType>(cast<TypedefType>(this)->LookThroughTypedefs());
87 return 0;
88}
89
Chris Lattnerc8629632007-07-31 19:29:30 +000090const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +000091 // If this is directly a function type, return it.
92 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
93 return FTy;
Chris Lattnerc8629632007-07-31 19:29:30 +000094
Steve Naroff7064f5c2007-07-26 18:32:01 +000095 // If this is a typedef for a function type, strip the typedef off without
96 // losing all typedef information.
97 if (isa<FunctionType>(CanonicalType))
98 return cast<FunctionType>(cast<TypedefType>(this)->LookThroughTypedefs());
99 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000100}
101
Chris Lattnerbefee482007-07-31 16:53:04 +0000102const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000103 // If this is directly a pointer type, return it.
104 if (const PointerType *PTy = dyn_cast<PointerType>(this))
105 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000106
107 // If this is a typedef for a pointer type, strip the typedef off without
108 // losing all typedef information.
109 if (isa<PointerType>(CanonicalType))
110 return cast<PointerType>(cast<TypedefType>(this)->LookThroughTypedefs());
Chris Lattner3acb1382007-07-16 00:13:25 +0000111 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000112}
113
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000114const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000115 // If this is directly a reference type, return it.
116 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
117 return RTy;
118
119 // If this is a typedef for a reference type, strip the typedef off without
120 // losing all typedef information.
121 if (isa<ReferenceType>(CanonicalType))
122 return cast<ReferenceType>(cast<TypedefType>(this)->LookThroughTypedefs());
123 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000124}
125
Chris Lattnerc8629632007-07-31 19:29:30 +0000126const ArrayType *Type::getAsArrayType() const {
Steve Naroff700204c2007-07-24 21:46:40 +0000127 // If this is directly a reference type, return it.
128 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
129 return ATy;
130
131 // If this is a typedef for an array type, strip the typedef off without
132 // losing all typedef information.
133 if (isa<ArrayType>(CanonicalType))
134 return cast<ArrayType>(cast<TypedefType>(this)->LookThroughTypedefs());
135 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000136}
137
Chris Lattnerc8629632007-07-31 19:29:30 +0000138const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000139 // If this is directly a reference type, return it.
140 if (const RecordType *RTy = dyn_cast<RecordType>(this))
141 return RTy;
142
143 // If this is a typedef for an record type, strip the typedef off without
144 // losing all typedef information.
145 if (isa<RecordType>(CanonicalType))
146 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Steve Naroffadc01852007-07-26 03:18:02 +0000147 return 0;
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000148}
149
Chris Lattnerc8629632007-07-31 19:29:30 +0000150const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000151 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000152 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
153 if (RT->getDecl()->getKind() == Decl::Struct)
154 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000155 }
156 // If this is a typedef for a structure type, strip the typedef off without
157 // losing all typedef information.
Chris Lattnerc8629632007-07-31 19:29:30 +0000158 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
159 if (RT->getDecl()->getKind() == Decl::Struct)
160 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000162 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000163}
164
Chris Lattnerc8629632007-07-31 19:29:30 +0000165const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000166 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000167 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
168 if (RT->getDecl()->getKind() == Decl::Union)
169 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000170 }
171 // If this is a typedef for a union type, strip the typedef off without
172 // losing all typedef information.
Chris Lattnerc8629632007-07-31 19:29:30 +0000173 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
174 if (RT->getDecl()->getKind() == Decl::Union)
175 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Reid Spencer5f016e22007-07-11 17:01:13 +0000176 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000177 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000178}
179
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000180const ComplexType *Type::getAsComplexType() const {
181 // Are we directly a complex type?
182 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
183 return CTy;
184
185 // If this is a typedef for a complex type, strip the typedef off without
186 // losing all typedef information.
187 if (isa<ComplexType>(CanonicalType))
188 return cast<ComplexType>(cast<TypedefType>(this)->LookThroughTypedefs());
189
190 return 0;
Chris Lattner7a2e0472007-07-16 00:23:25 +0000191}
192
Chris Lattnerc8629632007-07-31 19:29:30 +0000193const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000194 // Are we directly a vector type?
195 if (const VectorType *VTy = dyn_cast<VectorType>(this))
196 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000197
198 // If this is a typedef for a vector type, strip the typedef off without
199 // losing all typedef information.
200 if (isa<VectorType>(CanonicalType))
201 return cast<VectorType>(cast<TypedefType>(this)->LookThroughTypedefs());
202
Chris Lattner7a2e0472007-07-16 00:23:25 +0000203 return 0;
204}
205
Chris Lattnerc8629632007-07-31 19:29:30 +0000206const OCUVectorType *Type::getAsOCUVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000207 // Are we directly an OpenCU vector type?
208 if (const OCUVectorType *VTy = dyn_cast<OCUVectorType>(this))
209 return VTy;
210
211 // If this is a typedef for an OpenCU vector type, strip the typedef off
212 // without losing all typedef information.
213 if (isa<OCUVectorType>(CanonicalType))
214 return cast<OCUVectorType>(cast<TypedefType>(this)->LookThroughTypedefs());
215
216 return 0;
217}
218
Steve Naroff77878cc2007-08-27 04:08:11 +0000219bool Type::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
220 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
221 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
222
223 return lBuiltin->getKind() == rBuiltin->getKind();
224}
Chris Lattner7a2e0472007-07-16 00:23:25 +0000225
Reid Spencer5f016e22007-07-11 17:01:13 +0000226// C99 6.2.7p1: If both are complete types, then the following additional
227// requirements apply...FIXME (handle compatibility across source files).
228bool Type::tagTypesAreCompatible(QualType lhs, QualType rhs) {
229 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
230 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
231
232 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
233 if (ldecl->getIdentifier() == rdecl->getIdentifier())
234 return true;
235 }
236 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
237 if (ldecl->getIdentifier() == rdecl->getIdentifier())
238 return true;
239 }
240 return false;
241}
242
243bool Type::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
244 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
245 // identically qualified and both shall be pointers to compatible types.
246 if (lhs.getQualifiers() != rhs.getQualifiers())
247 return false;
248
249 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
250 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
251
252 return typesAreCompatible(ltype, rtype);
253}
254
255// C++ 5.17p6: When the left opperand of an assignment operator denotes a
256// reference to T, the operation assigns to the object of type T denoted by the
257// reference.
258bool Type::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
259 QualType ltype = lhs;
260
261 if (lhs->isReferenceType())
262 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
263
264 QualType rtype = rhs;
265
266 if (rhs->isReferenceType())
267 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
268
269 return typesAreCompatible(ltype, rtype);
270}
271
272bool Type::functionTypesAreCompatible(QualType lhs, QualType rhs) {
273 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
274 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
275 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
276 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
277
278 // first check the return types (common between C99 and K&R).
279 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
280 return false;
281
282 if (lproto && rproto) { // two C99 style function prototypes
283 unsigned lproto_nargs = lproto->getNumArgs();
284 unsigned rproto_nargs = rproto->getNumArgs();
285
286 if (lproto_nargs != rproto_nargs)
287 return false;
288
289 // both prototypes have the same number of arguments.
290 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
291 (rproto->isVariadic() && !lproto->isVariadic()))
292 return false;
293
294 // The use of ellipsis agree...now check the argument types.
295 for (unsigned i = 0; i < lproto_nargs; i++)
296 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
297 return false;
298 return true;
299 }
300 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
301 return true;
302
303 // we have a mixture of K&R style with C99 prototypes
304 const FunctionTypeProto *proto = lproto ? lproto : rproto;
305
306 if (proto->isVariadic())
307 return false;
308
309 // FIXME: Each parameter type T in the prototype must be compatible with the
310 // type resulting from applying the usual argument conversions to T.
311 return true;
312}
313
314bool Type::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
315 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
316 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
317
318 if (!typesAreCompatible(ltype, rtype))
319 return false;
320
321 // FIXME: If both types specify constant sizes, then the sizes must also be
322 // the same. Even if the sizes are the same, GCC produces an error.
323 return true;
324}
325
326/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
327/// both shall have the identically qualified version of a compatible type.
328/// C99 6.2.7p1: Two types have compatible types if their types are the
329/// same. See 6.7.[2,3,5] for additional rules.
330bool Type::typesAreCompatible(QualType lhs, QualType rhs) {
331 QualType lcanon = lhs.getCanonicalType();
332 QualType rcanon = rhs.getCanonicalType();
333
334 // If two types are identical, they are are compatible
335 if (lcanon == rcanon)
336 return true;
337
338 // If the canonical type classes don't match, they can't be compatible
339 if (lcanon->getTypeClass() != rcanon->getTypeClass())
340 return false;
341
342 switch (lcanon->getTypeClass()) {
343 case Type::Pointer:
344 return pointerTypesAreCompatible(lcanon, rcanon);
345 case Type::Reference:
346 return referenceTypesAreCompatible(lcanon, rcanon);
347 case Type::Array:
348 return arrayTypesAreCompatible(lcanon, rcanon);
349 case Type::FunctionNoProto:
350 case Type::FunctionProto:
351 return functionTypesAreCompatible(lcanon, rcanon);
352 case Type::Tagged: // handle structures, unions
353 return tagTypesAreCompatible(lcanon, rcanon);
354 case Type::Builtin:
Steve Naroff77878cc2007-08-27 04:08:11 +0000355 return builtinTypesAreCompatible(lcanon, rcanon);
Reid Spencer5f016e22007-07-11 17:01:13 +0000356 default:
357 assert(0 && "unexpected type");
358 }
359 return true; // should never get here...
360}
361
362bool Type::isIntegerType() const {
363 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
364 return BT->getKind() >= BuiltinType::Bool &&
365 BT->getKind() <= BuiltinType::LongLong;
366 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
367 if (TT->getDecl()->getKind() == Decl::Enum)
368 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000369 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
370 return VT->getElementType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 return false;
372}
373
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000374bool Type::isEnumeralType() const {
375 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
376 return TT->getDecl()->getKind() == Decl::Enum;
377 return false;
378}
379
380bool Type::isBooleanType() const {
381 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
382 return BT->getKind() == BuiltinType::Bool;
383 return false;
384}
385
386bool Type::isCharType() const {
387 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
388 return BT->getKind() == BuiltinType::Char_U ||
389 BT->getKind() == BuiltinType::UChar ||
390 BT->getKind() == BuiltinType::Char_S;
391 return false;
392}
393
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000394/// isSignedIntegerType - Return true if this is an integer type that is
395/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
396/// an enum decl which has a signed representation, or a vector of signed
397/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000398bool Type::isSignedIntegerType() const {
399 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
400 return BT->getKind() >= BuiltinType::Char_S &&
401 BT->getKind() <= BuiltinType::LongLong;
402 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000403
404 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
405 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
406 return ED->getIntegerType()->isSignedIntegerType();
407
Steve Naroffc63b96a2007-07-12 21:46:55 +0000408 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
409 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000410 return false;
411}
412
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000413/// isUnsignedIntegerType - Return true if this is an integer type that is
414/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
415/// decl which has an unsigned representation, or a vector of unsigned integer
416/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000417bool Type::isUnsignedIntegerType() const {
418 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
419 return BT->getKind() >= BuiltinType::Bool &&
420 BT->getKind() <= BuiltinType::ULongLong;
421 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000422
423 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
424 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
425 return ED->getIntegerType()->isUnsignedIntegerType();
426
Steve Naroffc63b96a2007-07-12 21:46:55 +0000427 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
428 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000429 return false;
430}
431
432bool Type::isFloatingType() const {
433 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
434 return BT->getKind() >= BuiltinType::Float &&
435 BT->getKind() <= BuiltinType::LongDouble;
436 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
437 return CT->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000438 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
439 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000440 return false;
441}
442
443bool Type::isRealFloatingType() const {
444 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
445 return BT->getKind() >= BuiltinType::Float &&
446 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000447 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
448 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000449 return false;
450}
451
452bool Type::isRealType() const {
453 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
454 return BT->getKind() >= BuiltinType::Bool &&
455 BT->getKind() <= BuiltinType::LongDouble;
456 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
457 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000458 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
459 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000460 return false;
461}
462
Reid Spencer5f016e22007-07-11 17:01:13 +0000463bool Type::isArithmeticType() const {
464 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
465 return BT->getKind() != BuiltinType::Void;
466 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
467 if (TT->getDecl()->getKind() == Decl::Enum)
468 return true;
469 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
470}
471
472bool Type::isScalarType() const {
473 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
474 return BT->getKind() != BuiltinType::Void;
475 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
476 if (TT->getDecl()->getKind() == Decl::Enum)
477 return true;
478 return false;
479 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000480 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
481 isa<VectorType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000482}
483
484bool Type::isAggregateType() const {
485 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
486 if (TT->getDecl()->getKind() == Decl::Struct)
487 return true;
488 return false;
489 }
490 return CanonicalType->getTypeClass() == Array;
491}
492
493// The only variable size types are auto arrays within a function. Structures
494// cannot contain a VLA member. They can have a flexible array member, however
495// the structure is still constant size (C99 6.7.2.1p16).
Chris Lattner590b6642007-07-15 23:26:56 +0000496bool Type::isConstantSizeType(ASTContext &Ctx, SourceLocation *loc) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000497 if (const ArrayType *Ary = dyn_cast<ArrayType>(CanonicalType)) {
Chris Lattner8b9023b2007-07-13 03:05:23 +0000498 assert(Ary->getSizeExpr() && "Incomplete types don't have a size at all!");
499 // Variable Length Array?
Chris Lattner590b6642007-07-15 23:26:56 +0000500 return Ary->getSizeExpr()->isIntegerConstantExpr(Ctx, loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000501 }
502 return true;
503}
504
505/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
506/// - a type that can describe objects, but which lacks information needed to
507/// determine its size.
508bool Type::isIncompleteType() const {
509 switch (CanonicalType->getTypeClass()) {
510 default: return false;
511 case Builtin:
512 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
513 // be completed.
514 return isVoidType();
515 case Tagged:
516 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
517 // forward declaration, but not a full definition (C99 6.2.5p22).
518 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
519 case Array:
520 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Chris Lattner8b9023b2007-07-13 03:05:23 +0000521 return cast<ArrayType>(CanonicalType)->getSizeExpr() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000522 }
523}
524
525bool Type::isPromotableIntegerType() const {
526 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
527 if (!BT) return false;
528 switch (BT->getKind()) {
529 case BuiltinType::Bool:
530 case BuiltinType::Char_S:
531 case BuiltinType::Char_U:
532 case BuiltinType::SChar:
533 case BuiltinType::UChar:
534 case BuiltinType::Short:
535 case BuiltinType::UShort:
536 return true;
537 default:
538 return false;
539 }
540}
541
542const char *BuiltinType::getName() const {
543 switch (getKind()) {
544 default: assert(0 && "Unknown builtin type!");
545 case Void: return "void";
546 case Bool: return "_Bool";
547 case Char_S: return "char";
548 case Char_U: return "char";
549 case SChar: return "signed char";
550 case Short: return "short";
551 case Int: return "int";
552 case Long: return "long";
553 case LongLong: return "long long";
554 case UChar: return "unsigned char";
555 case UShort: return "unsigned short";
556 case UInt: return "unsigned int";
557 case ULong: return "unsigned long";
558 case ULongLong: return "unsigned long long";
559 case Float: return "float";
560 case Double: return "double";
561 case LongDouble: return "long double";
562 }
563}
564
Reid Spencer5f016e22007-07-11 17:01:13 +0000565void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000566 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000567 unsigned NumArgs, bool isVariadic) {
568 ID.AddPointer(Result.getAsOpaquePtr());
569 for (unsigned i = 0; i != NumArgs; ++i)
570 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
571 ID.AddInteger(isVariadic);
572}
573
574void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000575 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000576}
577
Chris Lattnera2c77672007-07-16 22:05:22 +0000578/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
579/// potentially looking through *all* consequtive typedefs. This returns the
580/// sum of the type qualifiers, so if you have:
581/// typedef const int A;
582/// typedef volatile A B;
583/// looking through the typedefs for B will give you "const volatile A".
584///
585QualType TypedefType::LookThroughTypedefs() const {
586 // Usually, there is only a single level of typedefs, be fast in that case.
587 QualType FirstType = getDecl()->getUnderlyingType();
588 if (!isa<TypedefType>(FirstType))
589 return FirstType;
590
591 // Otherwise, do the fully general loop.
592 unsigned TypeQuals = 0;
593 const TypedefType *TDT = this;
594 while (1) {
595 QualType CurType = TDT->getDecl()->getUnderlyingType();
596 TypeQuals |= CurType.getQualifiers();
597
598 TDT = dyn_cast<TypedefType>(CurType);
599 if (TDT == 0)
600 return QualType(CurType.getTypePtr(), TypeQuals);
601 }
602}
Reid Spencer5f016e22007-07-11 17:01:13 +0000603
604bool RecordType::classof(const Type *T) {
605 if (const TagType *TT = dyn_cast<TagType>(T))
606 return isa<RecordDecl>(TT->getDecl());
607 return false;
608}
609
610
611//===----------------------------------------------------------------------===//
612// Type Printing
613//===----------------------------------------------------------------------===//
614
615void QualType::dump(const char *msg) const {
616 std::string R = "foo";
617 getAsStringInternal(R);
618 if (msg)
619 fprintf(stderr, "%s: %s\n", msg, R.c_str());
620 else
621 fprintf(stderr, "%s\n", R.c_str());
622}
623
624static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
625 // Note: funkiness to ensure we get a space only between quals.
626 bool NonePrinted = true;
627 if (TypeQuals & QualType::Const)
628 S += "const", NonePrinted = false;
629 if (TypeQuals & QualType::Volatile)
630 S += (NonePrinted+" volatile"), NonePrinted = false;
631 if (TypeQuals & QualType::Restrict)
632 S += (NonePrinted+" restrict"), NonePrinted = false;
633}
634
635void QualType::getAsStringInternal(std::string &S) const {
636 if (isNull()) {
637 S += "NULL TYPE\n";
638 return;
639 }
640
641 // Print qualifiers as appropriate.
Anton Korobeynikovb7b50bc2007-07-13 00:48:55 +0000642 unsigned TQ = getQualifiers();
643 if (TQ) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 std::string TQS;
645 AppendTypeQualList(TQS, TQ);
646 if (!S.empty())
647 S = TQS + ' ' + S;
648 else
649 S = TQS;
650 }
651
652 getTypePtr()->getAsStringInternal(S);
653}
654
655void BuiltinType::getAsStringInternal(std::string &S) const {
656 if (S.empty()) {
657 S = getName();
658 } else {
659 // Prefix the basic type, e.g. 'int X'.
660 S = ' ' + S;
661 S = getName() + S;
662 }
663}
664
665void ComplexType::getAsStringInternal(std::string &S) const {
666 ElementType->getAsStringInternal(S);
667 S = "_Complex " + S;
668}
669
670void PointerType::getAsStringInternal(std::string &S) const {
671 S = '*' + S;
672
673 // Handle things like 'int (*A)[4];' correctly.
674 // FIXME: this should include vectors, but vectors use attributes I guess.
675 if (isa<ArrayType>(PointeeType.getTypePtr()))
676 S = '(' + S + ')';
677
678 PointeeType.getAsStringInternal(S);
679}
680
681void ReferenceType::getAsStringInternal(std::string &S) const {
682 S = '&' + S;
683
684 // Handle things like 'int (&A)[4];' correctly.
685 // FIXME: this should include vectors, but vectors use attributes I guess.
686 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
687 S = '(' + S + ')';
688
689 ReferenceeType.getAsStringInternal(S);
690}
691
692void ArrayType::getAsStringInternal(std::string &S) const {
693 S += '[';
694
695 if (IndexTypeQuals) {
696 AppendTypeQualList(S, IndexTypeQuals);
697 S += ' ';
698 }
699
700 if (SizeModifier == Static)
701 S += "static";
702 else if (SizeModifier == Star)
703 S += '*';
704
705 S += ']';
706
707 ElementType.getAsStringInternal(S);
708}
709
710void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattnere107b5d2007-07-13 21:01:17 +0000711 S += " __attribute__((vector_size(";
712 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000713 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000714 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000715 ElementType.getAsStringInternal(S);
716}
717
Steve Naroff31a45842007-07-28 23:10:27 +0000718void OCUVectorType::getAsStringInternal(std::string &S) const {
719 S += " __attribute__((ocu_vector_type(";
720 S += llvm::utostr_32(NumElements);
721 S += ")))";
722 ElementType.getAsStringInternal(S);
723}
724
Steve Naroffd1861fd2007-07-31 12:34:36 +0000725void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000726 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
727 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000728 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000729 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000730 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000731}
732
Steve Naroff363bcff2007-08-01 23:45:51 +0000733void TypeOfType::getAsStringInternal(std::string &InnerString) const {
734 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
735 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000736 std::string Tmp;
737 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000738 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000739}
740
Reid Spencer5f016e22007-07-11 17:01:13 +0000741void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
742 // If needed for precedence reasons, wrap the inner part in grouping parens.
743 if (!S.empty())
744 S = "(" + S + ")";
745
746 S += "()";
747 getResultType().getAsStringInternal(S);
748}
749
750void FunctionTypeProto::getAsStringInternal(std::string &S) const {
751 // If needed for precedence reasons, wrap the inner part in grouping parens.
752 if (!S.empty())
753 S = "(" + S + ")";
754
755 S += "(";
756 std::string Tmp;
757 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
758 if (i) S += ", ";
759 getArgType(i).getAsStringInternal(Tmp);
760 S += Tmp;
761 Tmp.clear();
762 }
763
764 if (isVariadic()) {
765 if (getNumArgs())
766 S += ", ";
767 S += "...";
768 } else if (getNumArgs() == 0) {
769 // Do not emit int() if we have a proto, emit 'int(void)'.
770 S += "void";
771 }
772
773 S += ")";
774 getResultType().getAsStringInternal(S);
775}
776
777
778void TypedefType::getAsStringInternal(std::string &InnerString) const {
779 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
780 InnerString = ' ' + InnerString;
781 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
782}
783
784void TagType::getAsStringInternal(std::string &InnerString) const {
785 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
786 InnerString = ' ' + InnerString;
787
788 const char *Kind = getDecl()->getKindName();
789 const char *ID;
790 if (const IdentifierInfo *II = getDecl()->getIdentifier())
791 ID = II->getName();
792 else
793 ID = "<anonymous>";
794
795 InnerString = std::string(Kind) + " " + ID + InnerString;
796}