blob: f4c5aa748f2cfd1460012696357dc5453c07d181 [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
Reid Spencer5f016e22007-07-11 17:01:13 +0000394bool Type::isSignedIntegerType() const {
395 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
396 return BT->getKind() >= BuiltinType::Char_S &&
397 BT->getKind() <= BuiltinType::LongLong;
398 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000399 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
400 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000401 return false;
402}
403
404bool Type::isUnsignedIntegerType() const {
405 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
406 return BT->getKind() >= BuiltinType::Bool &&
407 BT->getKind() <= BuiltinType::ULongLong;
408 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000409 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
410 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 return false;
412}
413
414bool Type::isFloatingType() const {
415 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
416 return BT->getKind() >= BuiltinType::Float &&
417 BT->getKind() <= BuiltinType::LongDouble;
418 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
419 return CT->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000420 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
421 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000422 return false;
423}
424
425bool Type::isRealFloatingType() const {
426 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
427 return BT->getKind() >= BuiltinType::Float &&
428 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000429 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
430 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000431 return false;
432}
433
434bool Type::isRealType() const {
435 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
436 return BT->getKind() >= BuiltinType::Bool &&
437 BT->getKind() <= BuiltinType::LongDouble;
438 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
439 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000440 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
441 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000442 return false;
443}
444
Reid Spencer5f016e22007-07-11 17:01:13 +0000445bool Type::isArithmeticType() const {
446 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
447 return BT->getKind() != BuiltinType::Void;
448 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
449 if (TT->getDecl()->getKind() == Decl::Enum)
450 return true;
451 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
452}
453
454bool Type::isScalarType() const {
455 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
456 return BT->getKind() != BuiltinType::Void;
457 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
458 if (TT->getDecl()->getKind() == Decl::Enum)
459 return true;
460 return false;
461 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000462 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
463 isa<VectorType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000464}
465
466bool Type::isAggregateType() const {
467 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
468 if (TT->getDecl()->getKind() == Decl::Struct)
469 return true;
470 return false;
471 }
472 return CanonicalType->getTypeClass() == Array;
473}
474
475// The only variable size types are auto arrays within a function. Structures
476// cannot contain a VLA member. They can have a flexible array member, however
477// the structure is still constant size (C99 6.7.2.1p16).
Chris Lattner590b6642007-07-15 23:26:56 +0000478bool Type::isConstantSizeType(ASTContext &Ctx, SourceLocation *loc) const {
Reid Spencer5f016e22007-07-11 17:01:13 +0000479 if (const ArrayType *Ary = dyn_cast<ArrayType>(CanonicalType)) {
Chris Lattner8b9023b2007-07-13 03:05:23 +0000480 assert(Ary->getSizeExpr() && "Incomplete types don't have a size at all!");
481 // Variable Length Array?
Chris Lattner590b6642007-07-15 23:26:56 +0000482 return Ary->getSizeExpr()->isIntegerConstantExpr(Ctx, loc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000483 }
484 return true;
485}
486
487/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
488/// - a type that can describe objects, but which lacks information needed to
489/// determine its size.
490bool Type::isIncompleteType() const {
491 switch (CanonicalType->getTypeClass()) {
492 default: return false;
493 case Builtin:
494 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
495 // be completed.
496 return isVoidType();
497 case Tagged:
498 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
499 // forward declaration, but not a full definition (C99 6.2.5p22).
500 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
501 case Array:
502 // An array of unknown size is an incomplete type (C99 6.2.5p22).
Chris Lattner8b9023b2007-07-13 03:05:23 +0000503 return cast<ArrayType>(CanonicalType)->getSizeExpr() == 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000504 }
505}
506
507bool Type::isPromotableIntegerType() const {
508 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
509 if (!BT) return false;
510 switch (BT->getKind()) {
511 case BuiltinType::Bool:
512 case BuiltinType::Char_S:
513 case BuiltinType::Char_U:
514 case BuiltinType::SChar:
515 case BuiltinType::UChar:
516 case BuiltinType::Short:
517 case BuiltinType::UShort:
518 return true;
519 default:
520 return false;
521 }
522}
523
524const char *BuiltinType::getName() const {
525 switch (getKind()) {
526 default: assert(0 && "Unknown builtin type!");
527 case Void: return "void";
528 case Bool: return "_Bool";
529 case Char_S: return "char";
530 case Char_U: return "char";
531 case SChar: return "signed char";
532 case Short: return "short";
533 case Int: return "int";
534 case Long: return "long";
535 case LongLong: return "long long";
536 case UChar: return "unsigned char";
537 case UShort: return "unsigned short";
538 case UInt: return "unsigned int";
539 case ULong: return "unsigned long";
540 case ULongLong: return "unsigned long long";
541 case Float: return "float";
542 case Double: return "double";
543 case LongDouble: return "long double";
544 }
545}
546
Reid Spencer5f016e22007-07-11 17:01:13 +0000547void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
Chris Lattner942cfd32007-07-20 18:48:28 +0000548 arg_type_iterator ArgTys,
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 unsigned NumArgs, bool isVariadic) {
550 ID.AddPointer(Result.getAsOpaquePtr());
551 for (unsigned i = 0; i != NumArgs; ++i)
552 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
553 ID.AddInteger(isVariadic);
554}
555
556void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
Chris Lattner942cfd32007-07-20 18:48:28 +0000557 Profile(ID, getResultType(), arg_type_begin(), NumArgs, isVariadic());
Reid Spencer5f016e22007-07-11 17:01:13 +0000558}
559
Chris Lattnera2c77672007-07-16 22:05:22 +0000560/// LookThroughTypedefs - Return the ultimate type this typedef corresponds to
561/// potentially looking through *all* consequtive typedefs. This returns the
562/// sum of the type qualifiers, so if you have:
563/// typedef const int A;
564/// typedef volatile A B;
565/// looking through the typedefs for B will give you "const volatile A".
566///
567QualType TypedefType::LookThroughTypedefs() const {
568 // Usually, there is only a single level of typedefs, be fast in that case.
569 QualType FirstType = getDecl()->getUnderlyingType();
570 if (!isa<TypedefType>(FirstType))
571 return FirstType;
572
573 // Otherwise, do the fully general loop.
574 unsigned TypeQuals = 0;
575 const TypedefType *TDT = this;
576 while (1) {
577 QualType CurType = TDT->getDecl()->getUnderlyingType();
578 TypeQuals |= CurType.getQualifiers();
579
580 TDT = dyn_cast<TypedefType>(CurType);
581 if (TDT == 0)
582 return QualType(CurType.getTypePtr(), TypeQuals);
583 }
584}
Reid Spencer5f016e22007-07-11 17:01:13 +0000585
586bool RecordType::classof(const Type *T) {
587 if (const TagType *TT = dyn_cast<TagType>(T))
588 return isa<RecordDecl>(TT->getDecl());
589 return false;
590}
591
592
593//===----------------------------------------------------------------------===//
594// Type Printing
595//===----------------------------------------------------------------------===//
596
597void QualType::dump(const char *msg) const {
598 std::string R = "foo";
599 getAsStringInternal(R);
600 if (msg)
601 fprintf(stderr, "%s: %s\n", msg, R.c_str());
602 else
603 fprintf(stderr, "%s\n", R.c_str());
604}
605
606static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
607 // Note: funkiness to ensure we get a space only between quals.
608 bool NonePrinted = true;
609 if (TypeQuals & QualType::Const)
610 S += "const", NonePrinted = false;
611 if (TypeQuals & QualType::Volatile)
612 S += (NonePrinted+" volatile"), NonePrinted = false;
613 if (TypeQuals & QualType::Restrict)
614 S += (NonePrinted+" restrict"), NonePrinted = false;
615}
616
617void QualType::getAsStringInternal(std::string &S) const {
618 if (isNull()) {
619 S += "NULL TYPE\n";
620 return;
621 }
622
623 // Print qualifiers as appropriate.
Anton Korobeynikovb7b50bc2007-07-13 00:48:55 +0000624 unsigned TQ = getQualifiers();
625 if (TQ) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000626 std::string TQS;
627 AppendTypeQualList(TQS, TQ);
628 if (!S.empty())
629 S = TQS + ' ' + S;
630 else
631 S = TQS;
632 }
633
634 getTypePtr()->getAsStringInternal(S);
635}
636
637void BuiltinType::getAsStringInternal(std::string &S) const {
638 if (S.empty()) {
639 S = getName();
640 } else {
641 // Prefix the basic type, e.g. 'int X'.
642 S = ' ' + S;
643 S = getName() + S;
644 }
645}
646
647void ComplexType::getAsStringInternal(std::string &S) const {
648 ElementType->getAsStringInternal(S);
649 S = "_Complex " + S;
650}
651
652void PointerType::getAsStringInternal(std::string &S) const {
653 S = '*' + S;
654
655 // Handle things like 'int (*A)[4];' correctly.
656 // FIXME: this should include vectors, but vectors use attributes I guess.
657 if (isa<ArrayType>(PointeeType.getTypePtr()))
658 S = '(' + S + ')';
659
660 PointeeType.getAsStringInternal(S);
661}
662
663void ReferenceType::getAsStringInternal(std::string &S) const {
664 S = '&' + S;
665
666 // Handle things like 'int (&A)[4];' correctly.
667 // FIXME: this should include vectors, but vectors use attributes I guess.
668 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
669 S = '(' + S + ')';
670
671 ReferenceeType.getAsStringInternal(S);
672}
673
674void ArrayType::getAsStringInternal(std::string &S) const {
675 S += '[';
676
677 if (IndexTypeQuals) {
678 AppendTypeQualList(S, IndexTypeQuals);
679 S += ' ';
680 }
681
682 if (SizeModifier == Static)
683 S += "static";
684 else if (SizeModifier == Star)
685 S += '*';
686
687 S += ']';
688
689 ElementType.getAsStringInternal(S);
690}
691
692void VectorType::getAsStringInternal(std::string &S) const {
Chris Lattnere107b5d2007-07-13 21:01:17 +0000693 S += " __attribute__((vector_size(";
694 // FIXME: should multiply by element size somehow.
Reid Spencer5f016e22007-07-11 17:01:13 +0000695 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
Chris Lattnere107b5d2007-07-13 21:01:17 +0000696 S += ")))";
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 ElementType.getAsStringInternal(S);
698}
699
Steve Naroff31a45842007-07-28 23:10:27 +0000700void OCUVectorType::getAsStringInternal(std::string &S) const {
701 S += " __attribute__((ocu_vector_type(";
702 S += llvm::utostr_32(NumElements);
703 S += ")))";
704 ElementType.getAsStringInternal(S);
705}
706
Steve Naroffd1861fd2007-07-31 12:34:36 +0000707void TypeOfExpr::getAsStringInternal(std::string &InnerString) const {
Steve Naroff363bcff2007-08-01 23:45:51 +0000708 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(e) X'.
709 InnerString = ' ' + InnerString;
Steve Naroff8d1a3b82007-08-01 17:20:42 +0000710 std::ostringstream s;
Chris Lattner6000dac2007-08-08 22:51:59 +0000711 getUnderlyingExpr()->printPretty(s);
Steve Naroff1bfd5cc2007-08-05 03:24:45 +0000712 InnerString = "typeof(" + s.str() + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000713}
714
Steve Naroff363bcff2007-08-01 23:45:51 +0000715void TypeOfType::getAsStringInternal(std::string &InnerString) const {
716 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typeof(t) X'.
717 InnerString = ' ' + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000718 std::string Tmp;
719 getUnderlyingType().getAsStringInternal(Tmp);
Steve Naroff363bcff2007-08-01 23:45:51 +0000720 InnerString = "typeof(" + Tmp + ")" + InnerString;
Steve Naroffd1861fd2007-07-31 12:34:36 +0000721}
722
Reid Spencer5f016e22007-07-11 17:01:13 +0000723void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
724 // If needed for precedence reasons, wrap the inner part in grouping parens.
725 if (!S.empty())
726 S = "(" + S + ")";
727
728 S += "()";
729 getResultType().getAsStringInternal(S);
730}
731
732void FunctionTypeProto::getAsStringInternal(std::string &S) const {
733 // If needed for precedence reasons, wrap the inner part in grouping parens.
734 if (!S.empty())
735 S = "(" + S + ")";
736
737 S += "(";
738 std::string Tmp;
739 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
740 if (i) S += ", ";
741 getArgType(i).getAsStringInternal(Tmp);
742 S += Tmp;
743 Tmp.clear();
744 }
745
746 if (isVariadic()) {
747 if (getNumArgs())
748 S += ", ";
749 S += "...";
750 } else if (getNumArgs() == 0) {
751 // Do not emit int() if we have a proto, emit 'int(void)'.
752 S += "void";
753 }
754
755 S += ")";
756 getResultType().getAsStringInternal(S);
757}
758
759
760void TypedefType::getAsStringInternal(std::string &InnerString) const {
761 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
762 InnerString = ' ' + InnerString;
763 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
764}
765
766void TagType::getAsStringInternal(std::string &InnerString) const {
767 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
768 InnerString = ' ' + InnerString;
769
770 const char *Kind = getDecl()->getKindName();
771 const char *ID;
772 if (const IdentifierInfo *II = getDecl()->getIdentifier())
773 ID = II->getName();
774 else
775 ID = "<anonymous>";
776
777 InnerString = std::string(Kind) + " " + ID + InnerString;
778}