blob: 39646b7529a7df7f35d59ef2dd8078e303e5f270 [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"
Steve Naroff980e5082007-10-01 19:00:59 +000017#include "clang/AST/DeclObjC.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/AST/Expr.h"
19#include "clang/Basic/TargetInfo.h"
20#include "llvm/Support/Streams.h"
21#include "llvm/ADT/StringExtras.h"
Steve Naroff8d1a3b82007-08-01 17:20:42 +000022#include <sstream>
23
Reid Spencer5f016e22007-07-11 17:01:13 +000024using namespace clang;
25
26Type::~Type() {}
27
28/// isVoidType - Helper method to determine if this is the 'void' type.
29bool Type::isVoidType() const {
30 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
31 return BT->getKind() == BuiltinType::Void;
32 return false;
33}
34
35bool Type::isObjectType() const {
36 if (isa<FunctionType>(CanonicalType))
37 return false;
38 else if (CanonicalType->isIncompleteType())
39 return false;
40 else
41 return true;
42}
43
44bool Type::isDerivedType() const {
45 switch (CanonicalType->getTypeClass()) {
46 case Pointer:
Steve Narofffb22d962007-08-30 01:06:46 +000047 case VariableArray:
48 case ConstantArray:
Reid Spencer5f016e22007-07-11 17:01:13 +000049 case FunctionProto:
50 case FunctionNoProto:
51 case Reference:
52 return true;
53 case Tagged: {
54 const TagType *TT = cast<TagType>(CanonicalType);
55 const Decl::Kind Kind = TT->getDecl()->getKind();
56 return Kind == Decl::Struct || Kind == Decl::Union;
57 }
58 default:
59 return false;
60 }
61}
62
Chris Lattnerc8629632007-07-31 19:29:30 +000063bool Type::isStructureType() const {
64 if (const RecordType *RT = dyn_cast<RecordType>(this))
65 if (RT->getDecl()->getKind() == Decl::Struct)
66 return true;
67 return false;
68}
69bool Type::isUnionType() const {
70 if (const RecordType *RT = dyn_cast<RecordType>(this))
71 if (RT->getDecl()->getKind() == Decl::Union)
72 return true;
73 return false;
74}
Chris Lattnerc8629632007-07-31 19:29:30 +000075
Chris Lattnerc6fb90a2007-08-21 16:54:08 +000076bool Type::isComplexType() const {
77 return isa<ComplexType>(CanonicalType);
78}
79
Steve Naroff77878cc2007-08-27 04:08:11 +000080const BuiltinType *Type::getAsBuiltinType() const {
81 // If this is directly a builtin type, return it.
82 if (const BuiltinType *BTy = dyn_cast<BuiltinType>(this))
83 return BTy;
84
85 // If this is a typedef for a builtin type, strip the typedef off without
86 // losing all typedef information.
87 if (isa<BuiltinType>(CanonicalType))
88 return cast<BuiltinType>(cast<TypedefType>(this)->LookThroughTypedefs());
89 return 0;
90}
91
Chris Lattnerc8629632007-07-31 19:29:30 +000092const FunctionType *Type::getAsFunctionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +000093 // If this is directly a function type, return it.
94 if (const FunctionType *FTy = dyn_cast<FunctionType>(this))
95 return FTy;
Chris Lattnerc8629632007-07-31 19:29:30 +000096
Steve Naroff7064f5c2007-07-26 18:32:01 +000097 // If this is a typedef for a function type, strip the typedef off without
98 // losing all typedef information.
99 if (isa<FunctionType>(CanonicalType))
100 return cast<FunctionType>(cast<TypedefType>(this)->LookThroughTypedefs());
101 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000102}
103
Chris Lattnerbefee482007-07-31 16:53:04 +0000104const PointerType *Type::getAsPointerType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000105 // If this is directly a pointer type, return it.
106 if (const PointerType *PTy = dyn_cast<PointerType>(this))
107 return PTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000108
109 // If this is a typedef for a pointer type, strip the typedef off without
110 // losing all typedef information.
111 if (isa<PointerType>(CanonicalType))
112 return cast<PointerType>(cast<TypedefType>(this)->LookThroughTypedefs());
Chris Lattner3acb1382007-07-16 00:13:25 +0000113 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000114}
115
Chris Lattnera1d9fde2007-07-31 16:56:34 +0000116const ReferenceType *Type::getAsReferenceType() const {
Bill Wendlingea5e79f2007-07-17 04:16:47 +0000117 // If this is directly a reference type, return it.
118 if (const ReferenceType *RTy = dyn_cast<ReferenceType>(this))
119 return RTy;
120
121 // If this is a typedef for a reference type, strip the typedef off without
122 // losing all typedef information.
123 if (isa<ReferenceType>(CanonicalType))
124 return cast<ReferenceType>(cast<TypedefType>(this)->LookThroughTypedefs());
125 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000126}
127
Chris Lattnerc8629632007-07-31 19:29:30 +0000128const ArrayType *Type::getAsArrayType() const {
Steve Naroffd7444aa2007-08-31 17:20:07 +0000129 // If this is directly an array type, return it.
Steve Naroff700204c2007-07-24 21:46:40 +0000130 if (const ArrayType *ATy = dyn_cast<ArrayType>(this))
131 return ATy;
132
133 // If this is a typedef for an array type, strip the typedef off without
134 // losing all typedef information.
135 if (isa<ArrayType>(CanonicalType))
136 return cast<ArrayType>(cast<TypedefType>(this)->LookThroughTypedefs());
137 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000138}
139
Steve Naroffd7444aa2007-08-31 17:20:07 +0000140const ConstantArrayType *Type::getAsConstantArrayType() const {
141 // If this is directly a constant array type, return it.
142 if (const ConstantArrayType *ATy = dyn_cast<ConstantArrayType>(this))
143 return ATy;
144
145 // If this is a typedef for an array type, strip the typedef off without
146 // losing all typedef information.
147 if (isa<ConstantArrayType>(CanonicalType))
148 return cast<ConstantArrayType>(cast<TypedefType>(this)->LookThroughTypedefs());
149 return 0;
150}
151
152const VariableArrayType *Type::getAsVariableArrayType() const {
153 // If this is directly a variable array type, return it.
154 if (const VariableArrayType *ATy = dyn_cast<VariableArrayType>(this))
155 return ATy;
156
157 // If this is a typedef for an array type, strip the typedef off without
158 // losing all typedef information.
159 if (isa<VariableArrayType>(CanonicalType))
160 return cast<VariableArrayType>(cast<TypedefType>(this)->LookThroughTypedefs());
161 return 0;
162}
163
164/// isVariablyModifiedType (C99 6.7.5.2p2) - Return true for variable array
165/// types that have a non-constant expression. This does not include "[]".
166bool Type::isVariablyModifiedType() const {
167 if (const VariableArrayType *VAT = getAsVariableArrayType()) {
168 if (VAT->getSizeExpr())
169 return true;
170 }
171 return false;
172}
173
174const VariableArrayType *Type::getAsVariablyModifiedType() const {
175 if (const VariableArrayType *VAT = getAsVariableArrayType()) {
176 if (VAT->getSizeExpr())
177 return VAT;
178 }
179 return 0;
180}
181
Chris Lattnerc8629632007-07-31 19:29:30 +0000182const RecordType *Type::getAsRecordType() const {
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000183 // If this is directly a reference type, return it.
184 if (const RecordType *RTy = dyn_cast<RecordType>(this))
185 return RTy;
186
187 // If this is a typedef for an record type, strip the typedef off without
188 // losing all typedef information.
189 if (isa<RecordType>(CanonicalType))
190 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Steve Naroffadc01852007-07-26 03:18:02 +0000191 return 0;
Steve Naroffdfa6aae2007-07-26 03:11:44 +0000192}
193
Chris Lattnerc8629632007-07-31 19:29:30 +0000194const RecordType *Type::getAsStructureType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000195 // If this is directly a structure type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000196 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
197 if (RT->getDecl()->getKind() == Decl::Struct)
198 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000199 }
200 // If this is a typedef for a structure type, strip the typedef off without
201 // losing all typedef information.
Chris Lattnerc8629632007-07-31 19:29:30 +0000202 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
203 if (RT->getDecl()->getKind() == Decl::Struct)
204 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000206 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000207}
208
Chris Lattnerc8629632007-07-31 19:29:30 +0000209const RecordType *Type::getAsUnionType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000210 // If this is directly a union type, return it.
Chris Lattnerc8629632007-07-31 19:29:30 +0000211 if (const RecordType *RT = dyn_cast<RecordType>(this)) {
212 if (RT->getDecl()->getKind() == Decl::Union)
213 return RT;
Steve Naroff7064f5c2007-07-26 18:32:01 +0000214 }
215 // If this is a typedef for a union type, strip the typedef off without
216 // losing all typedef information.
Chris Lattnerc8629632007-07-31 19:29:30 +0000217 if (const RecordType *RT = dyn_cast<RecordType>(CanonicalType)) {
218 if (RT->getDecl()->getKind() == Decl::Union)
219 return cast<RecordType>(cast<TypedefType>(this)->LookThroughTypedefs());
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 }
Steve Naroff7064f5c2007-07-26 18:32:01 +0000221 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000222}
223
Chris Lattnerc6fb90a2007-08-21 16:54:08 +0000224const ComplexType *Type::getAsComplexType() const {
225 // Are we directly a complex type?
226 if (const ComplexType *CTy = dyn_cast<ComplexType>(this))
227 return CTy;
228
229 // If this is a typedef for a complex type, strip the typedef off without
230 // losing all typedef information.
231 if (isa<ComplexType>(CanonicalType))
232 return cast<ComplexType>(cast<TypedefType>(this)->LookThroughTypedefs());
233
234 return 0;
Chris Lattner7a2e0472007-07-16 00:23:25 +0000235}
236
Chris Lattnerc8629632007-07-31 19:29:30 +0000237const VectorType *Type::getAsVectorType() const {
Chris Lattner7a2e0472007-07-16 00:23:25 +0000238 // Are we directly a vector type?
239 if (const VectorType *VTy = dyn_cast<VectorType>(this))
240 return VTy;
Chris Lattnera2c77672007-07-16 22:05:22 +0000241
242 // If this is a typedef for a vector type, strip the typedef off without
243 // losing all typedef information.
244 if (isa<VectorType>(CanonicalType))
245 return cast<VectorType>(cast<TypedefType>(this)->LookThroughTypedefs());
246
Chris Lattner7a2e0472007-07-16 00:23:25 +0000247 return 0;
248}
249
Chris Lattnerc8629632007-07-31 19:29:30 +0000250const OCUVectorType *Type::getAsOCUVectorType() const {
Steve Naroff7064f5c2007-07-26 18:32:01 +0000251 // Are we directly an OpenCU vector type?
252 if (const OCUVectorType *VTy = dyn_cast<OCUVectorType>(this))
253 return VTy;
254
255 // If this is a typedef for an OpenCU vector type, strip the typedef off
256 // without losing all typedef information.
257 if (isa<OCUVectorType>(CanonicalType))
258 return cast<OCUVectorType>(cast<TypedefType>(this)->LookThroughTypedefs());
259
260 return 0;
261}
262
Steve Naroff77878cc2007-08-27 04:08:11 +0000263bool Type::builtinTypesAreCompatible(QualType lhs, QualType rhs) {
264 const BuiltinType *lBuiltin = lhs->getAsBuiltinType();
265 const BuiltinType *rBuiltin = rhs->getAsBuiltinType();
266
267 return lBuiltin->getKind() == rBuiltin->getKind();
268}
Chris Lattner7a2e0472007-07-16 00:23:25 +0000269
Reid Spencer5f016e22007-07-11 17:01:13 +0000270// C99 6.2.7p1: If both are complete types, then the following additional
271// requirements apply...FIXME (handle compatibility across source files).
272bool Type::tagTypesAreCompatible(QualType lhs, QualType rhs) {
273 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
274 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
275
276 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
277 if (ldecl->getIdentifier() == rdecl->getIdentifier())
278 return true;
279 }
280 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
281 if (ldecl->getIdentifier() == rdecl->getIdentifier())
282 return true;
283 }
284 return false;
285}
286
287bool Type::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
288 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
289 // identically qualified and both shall be pointers to compatible types.
290 if (lhs.getQualifiers() != rhs.getQualifiers())
291 return false;
292
293 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
294 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
295
296 return typesAreCompatible(ltype, rtype);
297}
298
299// C++ 5.17p6: When the left opperand of an assignment operator denotes a
300// reference to T, the operation assigns to the object of type T denoted by the
301// reference.
302bool Type::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
303 QualType ltype = lhs;
304
305 if (lhs->isReferenceType())
306 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
307
308 QualType rtype = rhs;
309
310 if (rhs->isReferenceType())
311 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
312
313 return typesAreCompatible(ltype, rtype);
314}
315
316bool Type::functionTypesAreCompatible(QualType lhs, QualType rhs) {
317 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
318 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
319 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
320 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
321
322 // first check the return types (common between C99 and K&R).
323 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
324 return false;
325
326 if (lproto && rproto) { // two C99 style function prototypes
327 unsigned lproto_nargs = lproto->getNumArgs();
328 unsigned rproto_nargs = rproto->getNumArgs();
329
330 if (lproto_nargs != rproto_nargs)
331 return false;
332
333 // both prototypes have the same number of arguments.
334 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
335 (rproto->isVariadic() && !lproto->isVariadic()))
336 return false;
337
338 // The use of ellipsis agree...now check the argument types.
339 for (unsigned i = 0; i < lproto_nargs; i++)
340 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
341 return false;
342 return true;
343 }
344 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
345 return true;
346
347 // we have a mixture of K&R style with C99 prototypes
348 const FunctionTypeProto *proto = lproto ? lproto : rproto;
349
350 if (proto->isVariadic())
351 return false;
352
353 // FIXME: Each parameter type T in the prototype must be compatible with the
354 // type resulting from applying the usual argument conversions to T.
355 return true;
356}
357
358bool Type::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
359 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
360 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
361
362 if (!typesAreCompatible(ltype, rtype))
363 return false;
364
365 // FIXME: If both types specify constant sizes, then the sizes must also be
366 // the same. Even if the sizes are the same, GCC produces an error.
367 return true;
368}
369
370/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
371/// both shall have the identically qualified version of a compatible type.
372/// C99 6.2.7p1: Two types have compatible types if their types are the
373/// same. See 6.7.[2,3,5] for additional rules.
374bool Type::typesAreCompatible(QualType lhs, QualType rhs) {
375 QualType lcanon = lhs.getCanonicalType();
376 QualType rcanon = rhs.getCanonicalType();
377
378 // If two types are identical, they are are compatible
379 if (lcanon == rcanon)
380 return true;
381
382 // If the canonical type classes don't match, they can't be compatible
383 if (lcanon->getTypeClass() != rcanon->getTypeClass())
384 return false;
385
386 switch (lcanon->getTypeClass()) {
387 case Type::Pointer:
388 return pointerTypesAreCompatible(lcanon, rcanon);
389 case Type::Reference:
390 return referenceTypesAreCompatible(lcanon, rcanon);
Steve Narofffb22d962007-08-30 01:06:46 +0000391 case Type::ConstantArray:
392 case Type::VariableArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 return arrayTypesAreCompatible(lcanon, rcanon);
394 case Type::FunctionNoProto:
395 case Type::FunctionProto:
396 return functionTypesAreCompatible(lcanon, rcanon);
397 case Type::Tagged: // handle structures, unions
398 return tagTypesAreCompatible(lcanon, rcanon);
399 case Type::Builtin:
Steve Naroff77878cc2007-08-27 04:08:11 +0000400 return builtinTypesAreCompatible(lcanon, rcanon);
Reid Spencer5f016e22007-07-11 17:01:13 +0000401 default:
402 assert(0 && "unexpected type");
403 }
404 return true; // should never get here...
405}
406
407bool Type::isIntegerType() const {
408 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
409 return BT->getKind() >= BuiltinType::Bool &&
410 BT->getKind() <= BuiltinType::LongLong;
411 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
412 if (TT->getDecl()->getKind() == Decl::Enum)
413 return true;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000414 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
415 return VT->getElementType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000416 return false;
417}
418
Steve Naroff13b7c5f2007-08-08 22:15:55 +0000419bool Type::isEnumeralType() const {
420 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
421 return TT->getDecl()->getKind() == Decl::Enum;
422 return false;
423}
424
425bool Type::isBooleanType() const {
426 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
427 return BT->getKind() == BuiltinType::Bool;
428 return false;
429}
430
431bool Type::isCharType() const {
432 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
433 return BT->getKind() == BuiltinType::Char_U ||
434 BT->getKind() == BuiltinType::UChar ||
435 BT->getKind() == BuiltinType::Char_S;
436 return false;
437}
438
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000439/// isSignedIntegerType - Return true if this is an integer type that is
440/// signed, according to C99 6.2.5p4 [char, signed char, short, int, long..],
441/// an enum decl which has a signed representation, or a vector of signed
442/// integer element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000443bool Type::isSignedIntegerType() const {
444 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
445 return BT->getKind() >= BuiltinType::Char_S &&
446 BT->getKind() <= BuiltinType::LongLong;
447 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000448
449 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
450 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
451 return ED->getIntegerType()->isSignedIntegerType();
452
Steve Naroffc63b96a2007-07-12 21:46:55 +0000453 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
454 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000455 return false;
456}
457
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000458/// isUnsignedIntegerType - Return true if this is an integer type that is
459/// unsigned, according to C99 6.2.5p6 [which returns true for _Bool], an enum
460/// decl which has an unsigned representation, or a vector of unsigned integer
461/// element type.
Reid Spencer5f016e22007-07-11 17:01:13 +0000462bool Type::isUnsignedIntegerType() const {
463 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
464 return BT->getKind() >= BuiltinType::Bool &&
465 BT->getKind() <= BuiltinType::ULongLong;
466 }
Chris Lattnerd5bbce42007-08-29 17:48:46 +0000467
468 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
469 if (const EnumDecl *ED = dyn_cast<EnumDecl>(TT->getDecl()))
470 return ED->getIntegerType()->isUnsignedIntegerType();
471
Steve Naroffc63b96a2007-07-12 21:46:55 +0000472 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
473 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000474 return false;
475}
476
477bool Type::isFloatingType() const {
478 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
479 return BT->getKind() >= BuiltinType::Float &&
480 BT->getKind() <= BuiltinType::LongDouble;
481 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
Chris Lattner729a2132007-08-30 06:19:11 +0000482 return CT->getElementType()->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000483 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
484 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000485 return false;
486}
487
488bool Type::isRealFloatingType() const {
489 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
490 return BT->getKind() >= BuiltinType::Float &&
491 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000492 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
493 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000494 return false;
495}
496
497bool Type::isRealType() const {
498 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
499 return BT->getKind() >= BuiltinType::Bool &&
500 BT->getKind() <= BuiltinType::LongDouble;
501 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
502 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000503 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
504 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000505 return false;
506}
507
Reid Spencer5f016e22007-07-11 17:01:13 +0000508bool Type::isArithmeticType() const {
509 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
510 return BT->getKind() != BuiltinType::Void;
511 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
512 if (TT->getDecl()->getKind() == Decl::Enum)
513 return true;
514 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
515}
516
517bool Type::isScalarType() const {
518 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
519 return BT->getKind() != BuiltinType::Void;
520 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
521 if (TT->getDecl()->getKind() == Decl::Enum)
522 return true;
523 return false;
524 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000525 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
526 isa<VectorType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000527}
528
529bool Type::isAggregateType() const {
530 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
531 if (TT->getDecl()->getKind() == Decl::Struct)
532 return true;
533 return false;
534 }
Steve Narofffb22d962007-08-30 01:06:46 +0000535 return CanonicalType->getTypeClass() == ConstantArray ||
536 CanonicalType->getTypeClass() == VariableArray;
Reid Spencer5f016e22007-07-11 17:01:13 +0000537}
538
539// The only variable size types are auto arrays within a function. Structures
540// cannot contain a VLA member. They can have a flexible array member, however
541// the structure is still constant size (C99 6.7.2.1p16).
Chris Lattner590b6642007-07-15 23:26:56 +0000542bool Type::isConstantSizeType(ASTContext &Ctx, SourceLocation *loc) const {
Steve Narofffb22d962007-08-30 01:06:46 +0000543 if (isa<VariableArrayType>(CanonicalType))
544 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000545 return true;
546}
547
548/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
549/// - a type that can describe objects, but which lacks information needed to
550/// determine its size.
551bool Type::isIncompleteType() const {
552 switch (CanonicalType->getTypeClass()) {
553 default: return false;
554 case Builtin:
555 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
556 // be completed.
557 return isVoidType();
558 case Tagged:
559 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
560 // forward declaration, but not a full definition (C99 6.2.5p22).
561 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
Steve Narofffb22d962007-08-30 01:06:46 +0000562 case VariableArray:
Reid Spencer5f016e22007-07-11 17:01:13 +0000563 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Steve Narofffb22d962007-08-30 01:06:46 +0000564 return cast<VariableArrayType>(CanonicalType)->getSizeExpr() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 }
566}
567
568bool Type::isPromotableIntegerType() const {
569 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
570 if (!BT) return false;
571 switch (BT->getKind()) {
572 case BuiltinType::Bool:
573 case BuiltinType::Char_S:
574 case BuiltinType::Char_U:
575 case BuiltinType::SChar:
576 case BuiltinType::UChar:
577 case BuiltinType::Short:
578 case BuiltinType::UShort:
579 return true;
580 default:
581 return false;
582 }
583}
584
585const char *BuiltinType::getName() const {
586 switch (getKind()) {
587 default: assert(0 && "Unknown builtin type!");
588 case Void: return "void";
589 case Bool: return "_Bool";
590 case Char_S: return "char";
591 case Char_U: return "char";
592 case SChar: return "signed char";
593 case Short: return "short";
594 case Int: return "int";
595 case Long: return "long";
596 case LongLong: return "long long";
597 case UChar: return "unsigned char";
598 case UShort: return "unsigned short";
599 case UInt: return "unsigned int";
600 case ULong: return "unsigned long";
601 case ULongLong: return "unsigned long long";
602 case Float: return "float";
603 case Double: return "double";
604 case LongDouble: return "long double";
605 }
606}
607
Reid Spencer5f016e22007-07-11 17:01:13 +0000608void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000609 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 unsigned NumArgs, bool isVariadic) {
611 ID.AddPointer(Result.getAsOpaquePtr());
612 for (unsigned i = 0; i != NumArgs; ++i)
613 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
614 ID.AddInteger(isVariadic);
615}
616
617void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000618 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000619}
620
Chris Lattnera2c77672007-07-16 22:05:22 +0000621/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
622/// potentially looking through *all* consequtive typedefs. This returns the
623/// sum of the type qualifiers, so if you have:
624/// typedef const int A;
625/// typedef volatile A B;
626/// looking through the typedefs for B will give you "const volatile A".
627///
628QualType TypedefType::LookThroughTypedefs() const {
629 // Usually, there is only a single level of typedefs, be fast in that case.
630 QualType FirstType = getDecl()->getUnderlyingType();
631 if (!isa<TypedefType>(FirstType))
632 return FirstType;
633
634 // Otherwise, do the fully general loop.
635 unsigned TypeQuals = 0;
636 const TypedefType *TDT = this;
637 while (1) {
638 QualType CurType = TDT->getDecl()->getUnderlyingType();
639 TypeQuals |= CurType.getQualifiers();
640
641 TDT = dyn_cast<TypedefType>(CurType);
642 if (TDT == 0)
643 return QualType(CurType.getTypePtr(), TypeQuals);
644 }
645}
Reid Spencer5f016e22007-07-11 17:01:13 +0000646
647bool RecordType::classof(const Type *T) {
648 if (const TagType *TT = dyn_cast<TagType>(T))
649 return isa<RecordDecl>(TT->getDecl());
650 return false;
651}
652
653
654//===----------------------------------------------------------------------===//
655// Type Printing
656//===----------------------------------------------------------------------===//
657
658void QualType::dump(const char *msg) const {
659 std::string R = "foo";
660 getAsStringInternal(R);
661 if (msg)
662 fprintf(stderr, "%s: %s\n", msg, R.c_str());
663 else
664 fprintf(stderr, "%s\n", R.c_str());
665}
666
667static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
668 // Note: funkiness to ensure we get a space only between quals.
669 bool NonePrinted = true;
670 if (TypeQuals & QualType::Const)
671 S += "const", NonePrinted = false;
672 if (TypeQuals & QualType::Volatile)
673 S += (NonePrinted+" volatile"), NonePrinted = false;
674 if (TypeQuals & QualType::Restrict)
675 S += (NonePrinted+" restrict"), NonePrinted = false;
676}
677
678void QualType::getAsStringInternal(std::string &S) const {
679 if (isNull()) {
680 S += "NULL TYPE\n";
681 return;
682 }
683
684 // Print qualifiers as appropriate.
Anton Korobeynikovb7b50bc2007-07-13 00:48:55 +0000685 unsigned TQ = getQualifiers();
686 if (TQ) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 std::string TQS;
688 AppendTypeQualList(TQS, TQ);
689 if (!S.empty())
690 S = TQS + ' ' + S;
691 else
692 S = TQS;
693 }
694
695 getTypePtr()->getAsStringInternal(S);
696}
697
698void BuiltinType::getAsStringInternal(std::string &S) const {
699 if (S.empty()) {
700 S = getName();
701 } else {
702 // Prefix the basic type, e.g. 'int X'.
703 S = ' ' + S;
704 S = getName() + S;
705 }
706}
707
708void ComplexType::getAsStringInternal(std::string &S) const {
709 ElementType->getAsStringInternal(S);
710 S = "_Complex " + S;
711}
712
713void PointerType::getAsStringInternal(std::string &S) const {
714 S = '*' + S;
715
716 // Handle things like 'int (*A)[4];' correctly.
717 // FIXME: this should include vectors, but vectors use attributes I guess.
718 if (isa<ArrayType>(PointeeType.getTypePtr()))
719 S = '(' + S + ')';
720
721 PointeeType.getAsStringInternal(S);
722}
723
724void ReferenceType::getAsStringInternal(std::string &S) const {
725 S = '&' + S;
726
727 // Handle things like 'int (&A)[4];' correctly.
728 // FIXME: this should include vectors, but vectors use attributes I guess.
729 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
730 S = '(' + S + ')';
731
732 ReferenceeType.getAsStringInternal(S);
733}
734
Steve Narofffb22d962007-08-30 01:06:46 +0000735void ConstantArrayType::getAsStringInternal(std::string &S) const {
736 S += '[';
Steve Naroff6b91cd92007-08-30 18:45:57 +0000737 S += llvm::utostr(getSize().getZExtValue());
Steve Narofffb22d962007-08-30 01:06:46 +0000738 S += ']';
739
740 getElementType().getAsStringInternal(S);
741}
742
743void VariableArrayType::getAsStringInternal(std::string &S) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 S += '[';
745
Steve Naroffc9406122007-08-30 18:10:14 +0000746 if (getIndexTypeQualifier()) {
747 AppendTypeQualList(S, getIndexTypeQualifier());
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 S += ' ';
749 }
750
Steve Naroffc9406122007-08-30 18:10:14 +0000751 if (getSizeModifier() == Static)
Reid Spencer5f016e22007-07-11 17:01:13 +0000752 S += "static";
Steve Naroffc9406122007-08-30 18:10:14 +0000753 else if (getSizeModifier() == Star)
Reid Spencer5f016e22007-07-11 17:01:13 +0000754 S += '*';
755
Steve Narofffb22d962007-08-30 01:06:46 +0000756 if (getSizeExpr()) {
757 std::ostringstream s;
758 getSizeExpr()->printPretty(s);
759 S += s.str();
760 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000761 S += ']';
762
Steve Narofffb22d962007-08-30 01:06:46 +0000763 getElementType().getAsStringInternal(S);
Reid Spencer5f016e22007-07-11 17:01:13 +0000764}
765
766void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattnere107b5d2007-07-13 21:01:17 +0000767 S += " __attribute__((vector_size(";
768 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000769 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000770 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000771 ElementType.getAsStringInternal(S);
772}
773
Steve Naroff31a45842007-07-28 23:10:27 +0000774void OCUVectorType::getAsStringInternal(std::string &S) const {
775 S += " __attribute__((ocu_vector_type(";
776 S += llvm::utostr_32(NumElements);
777 S += ")))";
778 ElementType.getAsStringInternal(S);
779}
780
Steve Naroffd1861fd2007-07-31 12:34:36 +0000781void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000782 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
783 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000784 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000785 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000786 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000787}
788
Steve Naroff363bcff2007-08-01 23:45:51 +0000789void TypeOfType::getAsStringInternal(std::string &InnerString) const {
790 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
791 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000792 std::string Tmp;
793 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000794 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000795}
796
Reid Spencer5f016e22007-07-11 17:01:13 +0000797void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
798 // If needed for precedence reasons, wrap the inner part in grouping parens.
799 if (!S.empty())
800 S = "(" + S + ")";
801
802 S += "()";
803 getResultType().getAsStringInternal(S);
804}
805
806void FunctionTypeProto::getAsStringInternal(std::string &S) const {
807 // If needed for precedence reasons, wrap the inner part in grouping parens.
808 if (!S.empty())
809 S = "(" + S + ")";
810
811 S += "(";
812 std::string Tmp;
813 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
814 if (i) S += ", ";
815 getArgType(i).getAsStringInternal(Tmp);
816 S += Tmp;
817 Tmp.clear();
818 }
819
820 if (isVariadic()) {
821 if (getNumArgs())
822 S += ", ";
823 S += "...";
824 } else if (getNumArgs() == 0) {
825 // Do not emit int() if we have a proto, emit 'int(void)'.
826 S += "void";
827 }
828
829 S += ")";
830 getResultType().getAsStringInternal(S);
831}
832
833
834void TypedefType::getAsStringInternal(std::string &InnerString) const {
835 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
836 InnerString = ' ' + InnerString;
837 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
838}
839
Steve Naroff3536b442007-09-06 21:24:23 +0000840void ObjcInterfaceType::getAsStringInternal(std::string &InnerString) const {
841 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
842 InnerString = ' ' + InnerString;
843 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
844}
845
Reid Spencer5f016e22007-07-11 17:01:13 +0000846void TagType::getAsStringInternal(std::string &InnerString) const {
847 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
848 InnerString = ' ' + InnerString;
849
850 const char *Kind = getDecl()->getKindName();
851 const char *ID;
852 if (const IdentifierInfo *II = getDecl()->getIdentifier())
853 ID = II->getName();
854 else
855 ID = "<anonymous>";
856
857 InnerString = std::string(Kind) + " " + ID + InnerString;
858}