blob: f7779bc3c6692baad42244cf9ad11ba0495094ca [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:
Steve Narofffb22d962007-08-30 01:06:46 +000046 case VariableArray:
47 case ConstantArray:
Reid Spencer5f016e22007-07-11 17:01:13 +000048 case FunctionProto:
49 case FunctionNoProto:
50 case Reference:
51 return true;
52 case Tagged: {
53 const TagType *TT = cast<TagType>(CanonicalType);
54 const Decl::Kind Kind = TT->getDecl()->getKind();
55 return Kind == Decl::Struct || Kind == Decl::Union;
56 }
57 default:
58 return false;
59 }
60}
61
Chris Lattnerc8629632007-07-31 19:29:30 +000062bool Type::isStructureType() const {
63 if (const RecordType *RT = dyn_cast<RecordType>(this))
64 if (RT->getDecl()->getKind() == Decl::Struct)
65 return true;
66 return false;
67}
68bool Type::isUnionType() const {
69 if (const RecordType *RT = dyn_cast<RecordType>(this))
70 if (RT->getDecl()->getKind() == Decl::Union)
71 return true;
72 return false;
73}
Chris Lattnerc8629632007-07-31 19:29:30 +000074
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000075bool Type::isComplexType() const {
76 return isa<ComplexType>(CanonicalType);
77}
78
Steve Naroff77878cc2007-08-27 04:08:11 +000079const BuiltinType *Type::getAsBuiltinType() const {
80 // If this is directly a builtin type, return it.
81 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
82 return BTy;
83
84 // If this is a typedef for a builtin type, strip the typedef off without
85 // losing all typedef information.
86 if (isa<BuiltinType>(CanonicalType))
87 return cast<BuiltinType>(cast<TypedefType>(this)->LookThroughTypedefs());
88 return 0;
89}
90
Chris Lattnerc8629632007-07-31 19:29:30 +000091const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +000092 // If this is directly a function type, return it.
93 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
94 return FTy;
Chris Lattnerc8629632007-07-31 19:29:30 +000095
Steve Naroff7064f5c2007-07-26 18:32:01 +000096 // If this is a typedef for a function type, strip the typedef off without
97 // losing all typedef information.
98 if (isa<FunctionType>(CanonicalType))
99 return cast<FunctionType>(cast<TypedefType>(this)->LookThroughTypedefs());
100 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000101}
102
Chris Lattnerbefee482007-07-31 16:53:04 +0000103const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000104 // If this is directly a pointer type, return it.
105 if (const PointerType *PTy = dyn_cast<PointerType>(this))
106 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000107
108 // If this is a typedef for a pointer type, strip the typedef off without
109 // losing all typedef information.
110 if (isa<PointerType>(CanonicalType))
111 return cast<PointerType>(cast<TypedefType>(this)->LookThroughTypedefs());
Chris Lattner3acb1382007-07-16 00:13:25 +0000112 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000113}
114
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000115const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000116 // If this is directly a reference type, return it.
117 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
118 return RTy;
119
120 // If this is a typedef for a reference type, strip the typedef off without
121 // losing all typedef information.
122 if (isa<ReferenceType>(CanonicalType))
123 return cast<ReferenceType>(cast<TypedefType>(this)->LookThroughTypedefs());
124 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000125}
126
Chris Lattnerc8629632007-07-31 19:29:30 +0000127const ArrayType *Type::getAsArrayType() const {
Steve Naroffd7444aa2007-08-31 17:20:07 +0000128 // If this is directly an array type, return it.
Steve Naroff700204c2007-07-24 21:46:40 +0000129 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
130 return ATy;
131
132 // If this is a typedef for an array type, strip the typedef off without
133 // losing all typedef information.
134 if (isa<ArrayType>(CanonicalType))
135 return cast<ArrayType>(cast<TypedefType>(this)->LookThroughTypedefs());
136 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000137}
138
Steve Naroffd7444aa2007-08-31 17:20:07 +0000139const ConstantArrayType *Type::getAsConstantArrayType() const {
140 // If this is directly a constant array type, return it.
141 if (const ConstantArrayType *ATy = dyn_cast<ConstantArrayType>(this))
142 return ATy;
143
144 // If this is a typedef for an array type, strip the typedef off without
145 // losing all typedef information.
146 if (isa<ConstantArrayType>(CanonicalType))
147 return cast<ConstantArrayType>(cast<TypedefType>(this)->LookThroughTypedefs());
148 return 0;
149}
150
151const VariableArrayType *Type::getAsVariableArrayType() const {
152 // If this is directly a variable array type, return it.
153 if (const VariableArrayType *ATy = dyn_cast<VariableArrayType>(this))
154 return ATy;
155
156 // If this is a typedef for an array type, strip the typedef off without
157 // losing all typedef information.
158 if (isa<VariableArrayType>(CanonicalType))
159 return cast<VariableArrayType>(cast<TypedefType>(this)->LookThroughTypedefs());
160 return 0;
161}
162
163/// isVariablyModifiedType (C99 6.7.5.2p2) - Return true for variable array
164/// types that have a non-constant expression. This does not include "[]".
165bool Type::isVariablyModifiedType() const {
166 if (const VariableArrayType *VAT = getAsVariableArrayType()) {
167 if (VAT->getSizeExpr())
168 return true;
169 }
170 return false;
171}
172
173const VariableArrayType *Type::getAsVariablyModifiedType() const {
174 if (const VariableArrayType *VAT = getAsVariableArrayType()) {
175 if (VAT->getSizeExpr())
176 return VAT;
177 }
178 return 0;
179}
180
Chris Lattnerc8629632007-07-31 19:29:30 +0000181const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000182 // If this is directly a reference type, return it.
183 if (const RecordType *RTy = dyn_cast<RecordType>(this))
184 return RTy;
185
186 // If this is a typedef for an record type, strip the typedef off without
187 // losing all typedef information.
188 if (isa<RecordType>(CanonicalType))
189 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Steve Naroffadc01852007-07-26 03:18:02 +0000190 return 0;
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000191}
192
Chris Lattnerc8629632007-07-31 19:29:30 +0000193const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000194 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000195 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
196 if (RT->getDecl()->getKind() == Decl::Struct)
197 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000198 }
199 // If this is a typedef for a structure type, strip the typedef off without
200 // losing all typedef information.
Chris Lattnerc8629632007-07-31 19:29:30 +0000201 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
202 if (RT->getDecl()->getKind() == Decl::Struct)
203 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Reid Spencer5f016e22007-07-11 17:01:13 +0000204 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000205 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000206}
207
Chris Lattnerc8629632007-07-31 19:29:30 +0000208const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000209 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000210 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
211 if (RT->getDecl()->getKind() == Decl::Union)
212 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000213 }
214 // If this is a typedef for a union type, strip the typedef off without
215 // losing all typedef information.
Chris Lattnerc8629632007-07-31 19:29:30 +0000216 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
217 if (RT->getDecl()->getKind() == Decl::Union)
218 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Reid Spencer5f016e22007-07-11 17:01:13 +0000219 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000220 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000221}
222
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000223const ComplexType *Type::getAsComplexType() const {
224 // Are we directly a complex type?
225 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
226 return CTy;
227
228 // If this is a typedef for a complex type, strip the typedef off without
229 // losing all typedef information.
230 if (isa<ComplexType>(CanonicalType))
231 return cast<ComplexType>(cast<TypedefType>(this)->LookThroughTypedefs());
232
233 return 0;
Chris Lattner7a2e0472007-07-16 00:23:25 +0000234}
235
Chris Lattnerc8629632007-07-31 19:29:30 +0000236const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000237 // Are we directly a vector type?
238 if (const VectorType *VTy = dyn_cast<VectorType>(this))
239 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000240
241 // If this is a typedef for a vector type, strip the typedef off without
242 // losing all typedef information.
243 if (isa<VectorType>(CanonicalType))
244 return cast<VectorType>(cast<TypedefType>(this)->LookThroughTypedefs());
245
Chris Lattner7a2e0472007-07-16 00:23:25 +0000246 return 0;
247}
248
Chris Lattnerc8629632007-07-31 19:29:30 +0000249const OCUVectorType *Type::getAsOCUVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000250 // Are we directly an OpenCU vector type?
251 if (const OCUVectorType *VTy = dyn_cast<OCUVectorType>(this))
252 return VTy;
253
254 // If this is a typedef for an OpenCU vector type, strip the typedef off
255 // without losing all typedef information.
256 if (isa<OCUVectorType>(CanonicalType))
257 return cast<OCUVectorType>(cast<TypedefType>(this)->LookThroughTypedefs());
258
259 return 0;
260}
261
Steve Naroff77878cc2007-08-27 04:08:11 +0000262bool Type::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
263 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
264 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
265
266 return lBuiltin->getKind() == rBuiltin->getKind();
267}
Chris Lattner7a2e0472007-07-16 00:23:25 +0000268
Reid Spencer5f016e22007-07-11 17:01:13 +0000269// C99 6.2.7p1: If both are complete types, then the following additional
270// requirements apply...FIXME (handle compatibility across source files).
271bool Type::tagTypesAreCompatible(QualType lhs, QualType rhs) {
272 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
273 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
274
275 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
276 if (ldecl->getIdentifier() == rdecl->getIdentifier())
277 return true;
278 }
279 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
280 if (ldecl->getIdentifier() == rdecl->getIdentifier())
281 return true;
282 }
283 return false;
284}
285
286bool Type::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
287 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
288 // identically qualified and both shall be pointers to compatible types.
289 if (lhs.getQualifiers() != rhs.getQualifiers())
290 return false;
291
292 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
293 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
294
295 return typesAreCompatible(ltype, rtype);
296}
297
298// C++ 5.17p6: When the left opperand of an assignment operator denotes a
299// reference to T, the operation assigns to the object of type T denoted by the
300// reference.
301bool Type::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
302 QualType ltype = lhs;
303
304 if (lhs->isReferenceType())
305 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
306
307 QualType rtype = rhs;
308
309 if (rhs->isReferenceType())
310 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
311
312 return typesAreCompatible(ltype, rtype);
313}
314
315bool Type::functionTypesAreCompatible(QualType lhs, QualType rhs) {
316 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
317 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
318 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
319 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
320
321 // first check the return types (common between C99 and K&R).
322 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
323 return false;
324
325 if (lproto && rproto) { // two C99 style function prototypes
326 unsigned lproto_nargs = lproto->getNumArgs();
327 unsigned rproto_nargs = rproto->getNumArgs();
328
329 if (lproto_nargs != rproto_nargs)
330 return false;
331
332 // both prototypes have the same number of arguments.
333 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
334 (rproto->isVariadic() && !lproto->isVariadic()))
335 return false;
336
337 // The use of ellipsis agree...now check the argument types.
338 for (unsigned i = 0; i < lproto_nargs; i++)
339 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
340 return false;
341 return true;
342 }
343 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
344 return true;
345
346 // we have a mixture of K&R style with C99 prototypes
347 const FunctionTypeProto *proto = lproto ? lproto : rproto;
348
349 if (proto->isVariadic())
350 return false;
351
352 // FIXME: Each parameter type T in the prototype must be compatible with the
353 // type resulting from applying the usual argument conversions to T.
354 return true;
355}
356
357bool Type::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
358 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
359 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
360
361 if (!typesAreCompatible(ltype, rtype))
362 return false;
363
364 // FIXME: If both types specify constant sizes, then the sizes must also be
365 // the same. Even if the sizes are the same, GCC produces an error.
366 return true;
367}
368
369/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
370/// both shall have the identically qualified version of a compatible type.
371/// C99 6.2.7p1: Two types have compatible types if their types are the
372/// same. See 6.7.[2,3,5] for additional rules.
373bool Type::typesAreCompatible(QualType lhs, QualType rhs) {
374 QualType lcanon = lhs.getCanonicalType();
375 QualType rcanon = rhs.getCanonicalType();
376
377 // If two types are identical, they are are compatible
378 if (lcanon == rcanon)
379 return true;
380
381 // If the canonical type classes don't match, they can't be compatible
382 if (lcanon->getTypeClass() != rcanon->getTypeClass())
383 return false;
384
385 switch (lcanon->getTypeClass()) {
386 case Type::Pointer:
387 return pointerTypesAreCompatible(lcanon, rcanon);
388 case Type::Reference:
389 return referenceTypesAreCompatible(lcanon, rcanon);
Steve Narofffb22d962007-08-30 01:06:46 +0000390 case Type::ConstantArray:
391 case Type::VariableArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000392 return arrayTypesAreCompatible(lcanon, rcanon);
393 case Type::FunctionNoProto:
394 case Type::FunctionProto:
395 return functionTypesAreCompatible(lcanon, rcanon);
396 case Type::Tagged: // handle structures, unions
397 return tagTypesAreCompatible(lcanon, rcanon);
398 case Type::Builtin:
Steve Naroff77878cc2007-08-27 04:08:11 +0000399 return builtinTypesAreCompatible(lcanon, rcanon);
Reid Spencer5f016e22007-07-11 17:01:13 +0000400 default:
401 assert(0 && "unexpected type");
402 }
403 return true; // should never get here...
404}
405
406bool Type::isIntegerType() const {
407 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
408 return BT->getKind() >= BuiltinType::Bool &&
409 BT->getKind() <= BuiltinType::LongLong;
410 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
411 if (TT->getDecl()->getKind() == Decl::Enum)
412 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000413 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
414 return VT->getElementType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000415 return false;
416}
417
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000418bool Type::isEnumeralType() const {
419 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
420 return TT->getDecl()->getKind() == Decl::Enum;
421 return false;
422}
423
424bool Type::isBooleanType() const {
425 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
426 return BT->getKind() == BuiltinType::Bool;
427 return false;
428}
429
430bool Type::isCharType() const {
431 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
432 return BT->getKind() == BuiltinType::Char_U ||
433 BT->getKind() == BuiltinType::UChar ||
434 BT->getKind() == BuiltinType::Char_S;
435 return false;
436}
437
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000438/// isSignedIntegerType - Return true if this is an integer type that is
439/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
440/// an enum decl which has a signed representation, or a vector of signed
441/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000442bool Type::isSignedIntegerType() const {
443 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
444 return BT->getKind() >= BuiltinType::Char_S &&
445 BT->getKind() <= BuiltinType::LongLong;
446 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000447
448 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
449 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
450 return ED->getIntegerType()->isSignedIntegerType();
451
Steve Naroffc63b96a2007-07-12 21:46:55 +0000452 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
453 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000454 return false;
455}
456
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000457/// isUnsignedIntegerType - Return true if this is an integer type that is
458/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
459/// decl which has an unsigned representation, or a vector of unsigned integer
460/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000461bool Type::isUnsignedIntegerType() const {
462 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
463 return BT->getKind() >= BuiltinType::Bool &&
464 BT->getKind() <= BuiltinType::ULongLong;
465 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000466
467 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
468 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
469 return ED->getIntegerType()->isUnsignedIntegerType();
470
Steve Naroffc63b96a2007-07-12 21:46:55 +0000471 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
472 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000473 return false;
474}
475
476bool Type::isFloatingType() const {
477 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
478 return BT->getKind() >= BuiltinType::Float &&
479 BT->getKind() <= BuiltinType::LongDouble;
480 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000481 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000482 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
483 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000484 return false;
485}
486
487bool Type::isRealFloatingType() const {
488 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
489 return BT->getKind() >= BuiltinType::Float &&
490 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000491 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
492 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000493 return false;
494}
495
496bool Type::isRealType() const {
497 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
498 return BT->getKind() >= BuiltinType::Bool &&
499 BT->getKind() <= BuiltinType::LongDouble;
500 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
501 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000502 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
503 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 return false;
505}
506
Reid Spencer5f016e22007-07-11 17:01:13 +0000507bool Type::isArithmeticType() const {
508 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
509 return BT->getKind() != BuiltinType::Void;
510 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
511 if (TT->getDecl()->getKind() == Decl::Enum)
512 return true;
513 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
514}
515
516bool Type::isScalarType() const {
517 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
518 return BT->getKind() != BuiltinType::Void;
519 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
520 if (TT->getDecl()->getKind() == Decl::Enum)
521 return true;
522 return false;
523 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000524 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
525 isa<VectorType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000526}
527
528bool Type::isAggregateType() const {
529 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
530 if (TT->getDecl()->getKind() == Decl::Struct)
531 return true;
532 return false;
533 }
Steve Narofffb22d962007-08-30 01:06:46 +0000534 return CanonicalType->getTypeClass() == ConstantArray ||
535 CanonicalType->getTypeClass() == VariableArray;
Reid Spencer5f016e22007-07-11 17:01:13 +0000536}
537
538// The only variable size types are auto arrays within a function. Structures
539// cannot contain a VLA member. They can have a flexible array member, however
540// the structure is still constant size (C99 6.7.2.1p16).
Chris Lattner590b6642007-07-15 23:26:56 +0000541bool Type::isConstantSizeType(ASTContext &Ctx, SourceLocation *loc) const {
Steve Narofffb22d962007-08-30 01:06:46 +0000542 if (isa<VariableArrayType>(CanonicalType))
543 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 return true;
545}
546
547/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
548/// - a type that can describe objects, but which lacks information needed to
549/// determine its size.
550bool Type::isIncompleteType() const {
551 switch (CanonicalType->getTypeClass()) {
552 default: return false;
553 case Builtin:
554 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
555 // be completed.
556 return isVoidType();
557 case Tagged:
558 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
559 // forward declaration, but not a full definition (C99 6.2.5p22).
560 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Steve Narofffb22d962007-08-30 01:06:46 +0000561 case VariableArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000562 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Steve Narofffb22d962007-08-30 01:06:46 +0000563 return cast<VariableArrayType>(CanonicalType)->getSizeExpr() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 }
565}
566
567bool Type::isPromotableIntegerType() const {
568 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
569 if (!BT) return false;
570 switch (BT->getKind()) {
571 case BuiltinType::Bool:
572 case BuiltinType::Char_S:
573 case BuiltinType::Char_U:
574 case BuiltinType::SChar:
575 case BuiltinType::UChar:
576 case BuiltinType::Short:
577 case BuiltinType::UShort:
578 return true;
579 default:
580 return false;
581 }
582}
583
584const char *BuiltinType::getName() const {
585 switch (getKind()) {
586 default: assert(0 && "Unknown builtin type!");
587 case Void: return "void";
588 case Bool: return "_Bool";
589 case Char_S: return "char";
590 case Char_U: return "char";
591 case SChar: return "signed char";
592 case Short: return "short";
593 case Int: return "int";
594 case Long: return "long";
595 case LongLong: return "long long";
596 case UChar: return "unsigned char";
597 case UShort: return "unsigned short";
598 case UInt: return "unsigned int";
599 case ULong: return "unsigned long";
600 case ULongLong: return "unsigned long long";
601 case Float: return "float";
602 case Double: return "double";
603 case LongDouble: return "long double";
604 }
605}
606
Reid Spencer5f016e22007-07-11 17:01:13 +0000607void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000608 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000609 unsigned NumArgs, bool isVariadic) {
610 ID.AddPointer(Result.getAsOpaquePtr());
611 for (unsigned i = 0; i != NumArgs; ++i)
612 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
613 ID.AddInteger(isVariadic);
614}
615
616void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000617 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000618}
619
Chris Lattnera2c77672007-07-16 22:05:22 +0000620/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
621/// potentially looking through *all* consequtive typedefs. This returns the
622/// sum of the type qualifiers, so if you have:
623/// typedef const int A;
624/// typedef volatile A B;
625/// looking through the typedefs for B will give you "const volatile A".
626///
627QualType TypedefType::LookThroughTypedefs() const {
628 // Usually, there is only a single level of typedefs, be fast in that case.
629 QualType FirstType = getDecl()->getUnderlyingType();
630 if (!isa<TypedefType>(FirstType))
631 return FirstType;
632
633 // Otherwise, do the fully general loop.
634 unsigned TypeQuals = 0;
635 const TypedefType *TDT = this;
636 while (1) {
637 QualType CurType = TDT->getDecl()->getUnderlyingType();
638 TypeQuals |= CurType.getQualifiers();
639
640 TDT = dyn_cast<TypedefType>(CurType);
641 if (TDT == 0)
642 return QualType(CurType.getTypePtr(), TypeQuals);
643 }
644}
Reid Spencer5f016e22007-07-11 17:01:13 +0000645
646bool RecordType::classof(const Type *T) {
647 if (const TagType *TT = dyn_cast<TagType>(T))
648 return isa<RecordDecl>(TT->getDecl());
649 return false;
650}
651
652
653//===----------------------------------------------------------------------===//
654// Type Printing
655//===----------------------------------------------------------------------===//
656
657void QualType::dump(const char *msg) const {
658 std::string R = "foo";
659 getAsStringInternal(R);
660 if (msg)
661 fprintf(stderr, "%s: %s\n", msg, R.c_str());
662 else
663 fprintf(stderr, "%s\n", R.c_str());
664}
665
666static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
667 // Note: funkiness to ensure we get a space only between quals.
668 bool NonePrinted = true;
669 if (TypeQuals & QualType::Const)
670 S += "const", NonePrinted = false;
671 if (TypeQuals & QualType::Volatile)
672 S += (NonePrinted+" volatile"), NonePrinted = false;
673 if (TypeQuals & QualType::Restrict)
674 S += (NonePrinted+" restrict"), NonePrinted = false;
675}
676
677void QualType::getAsStringInternal(std::string &S) const {
678 if (isNull()) {
679 S += "NULL TYPE\n";
680 return;
681 }
682
683 // Print qualifiers as appropriate.
Anton Korobeynikovb7b50bc2007-07-13 00:48:55 +0000684 unsigned TQ = getQualifiers();
685 if (TQ) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000686 std::string TQS;
687 AppendTypeQualList(TQS, TQ);
688 if (!S.empty())
689 S = TQS + ' ' + S;
690 else
691 S = TQS;
692 }
693
694 getTypePtr()->getAsStringInternal(S);
695}
696
697void BuiltinType::getAsStringInternal(std::string &S) const {
698 if (S.empty()) {
699 S = getName();
700 } else {
701 // Prefix the basic type, e.g. 'int X'.
702 S = ' ' + S;
703 S = getName() + S;
704 }
705}
706
707void ComplexType::getAsStringInternal(std::string &S) const {
708 ElementType->getAsStringInternal(S);
709 S = "_Complex " + S;
710}
711
712void PointerType::getAsStringInternal(std::string &S) const {
713 S = '*' + S;
714
715 // Handle things like 'int (*A)[4];' correctly.
716 // FIXME: this should include vectors, but vectors use attributes I guess.
717 if (isa<ArrayType>(PointeeType.getTypePtr()))
718 S = '(' + S + ')';
719
720 PointeeType.getAsStringInternal(S);
721}
722
723void ReferenceType::getAsStringInternal(std::string &S) const {
724 S = '&' + S;
725
726 // Handle things like 'int (&A)[4];' correctly.
727 // FIXME: this should include vectors, but vectors use attributes I guess.
728 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
729 S = '(' + S + ')';
730
731 ReferenceeType.getAsStringInternal(S);
732}
733
Steve Narofffb22d962007-08-30 01:06:46 +0000734void ConstantArrayType::getAsStringInternal(std::string &S) const {
735 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000736 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000737 S += ']';
738
739 getElementType().getAsStringInternal(S);
740}
741
742void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000743 S += '[';
744
Steve Naroffc9406122007-08-30 18:10:14 +0000745 if (getIndexTypeQualifier()) {
746 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000747 S += ' ';
748 }
749
Steve Naroffc9406122007-08-30 18:10:14 +0000750 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000752 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 S += '*';
754
Steve Narofffb22d962007-08-30 01:06:46 +0000755 if (getSizeExpr()) {
756 std::ostringstream s;
757 getSizeExpr()->printPretty(s);
758 S += s.str();
759 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 S += ']';
761
Steve Narofffb22d962007-08-30 01:06:46 +0000762 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000763}
764
765void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattnere107b5d2007-07-13 21:01:17 +0000766 S += " __attribute__((vector_size(";
767 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000768 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000769 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 ElementType.getAsStringInternal(S);
771}
772
Steve Naroff31a45842007-07-28 23:10:27 +0000773void OCUVectorType::getAsStringInternal(std::string &S) const {
774 S += " __attribute__((ocu_vector_type(";
775 S += llvm::utostr_32(NumElements);
776 S += ")))";
777 ElementType.getAsStringInternal(S);
778}
779
Steve Naroffd1861fd2007-07-31 12:34:36 +0000780void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000781 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
782 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000783 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000784 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000785 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000786}
787
Steve Naroff363bcff2007-08-01 23:45:51 +0000788void TypeOfType::getAsStringInternal(std::string &InnerString) const {
789 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
790 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000791 std::string Tmp;
792 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000793 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000794}
795
Reid Spencer5f016e22007-07-11 17:01:13 +0000796void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
797 // If needed for precedence reasons, wrap the inner part in grouping parens.
798 if (!S.empty())
799 S = "(" + S + ")";
800
801 S += "()";
802 getResultType().getAsStringInternal(S);
803}
804
805void FunctionTypeProto::getAsStringInternal(std::string &S) const {
806 // If needed for precedence reasons, wrap the inner part in grouping parens.
807 if (!S.empty())
808 S = "(" + S + ")";
809
810 S += "(";
811 std::string Tmp;
812 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
813 if (i) S += ", ";
814 getArgType(i).getAsStringInternal(Tmp);
815 S += Tmp;
816 Tmp.clear();
817 }
818
819 if (isVariadic()) {
820 if (getNumArgs())
821 S += ", ";
822 S += "...";
823 } else if (getNumArgs() == 0) {
824 // Do not emit int() if we have a proto, emit 'int(void)'.
825 S += "void";
826 }
827
828 S += ")";
829 getResultType().getAsStringInternal(S);
830}
831
832
833void TypedefType::getAsStringInternal(std::string &InnerString) const {
834 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
835 InnerString = ' ' + InnerString;
836 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
837}
838
Steve Naroff3536b442007-09-06 21:24:23 +0000839void ObjcInterfaceType::getAsStringInternal(std::string &InnerString) const {
840 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
841 InnerString = ' ' + InnerString;
842 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
843}
844
Reid Spencer5f016e22007-07-11 17:01:13 +0000845void TagType::getAsStringInternal(std::string &InnerString) const {
846 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
847 InnerString = ' ' + InnerString;
848
849 const char *Kind = getDecl()->getKindName();
850 const char *ID;
851 if (const IdentifierInfo *II = getDecl()->getIdentifier())
852 ID = II->getName();
853 else
854 ID = "<anonymous>";
855
856 InnerString = std::string(Kind) + " " + ID + InnerString;
857}