blob: cd06c7e7832bbad9d4c847e8353fff2c4079f5a1 [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"
21using namespace clang;
22
23Type::~Type() {}
24
25/// isVoidType - Helper method to determine if this is the 'void' type.
26bool Type::isVoidType() const {
27 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
28 return BT->getKind() == BuiltinType::Void;
29 return false;
30}
31
32bool Type::isObjectType() const {
33 if (isa<FunctionType>(CanonicalType))
34 return false;
35 else if (CanonicalType->isIncompleteType())
36 return false;
37 else
38 return true;
39}
40
41bool Type::isDerivedType() const {
42 switch (CanonicalType->getTypeClass()) {
43 case Pointer:
44 case Array:
45 case FunctionProto:
46 case FunctionNoProto:
47 case Reference:
48 return true;
49 case Tagged: {
50 const TagType *TT = cast<TagType>(CanonicalType);
51 const Decl::Kind Kind = TT->getDecl()->getKind();
52 return Kind == Decl::Struct || Kind == Decl::Union;
53 }
54 default:
55 return false;
56 }
57}
58
59bool Type::isFunctionType() const {
60 return isa<FunctionType>(CanonicalType);
61}
62
63bool Type::isPointerType() const {
64 return isa<PointerType>(CanonicalType);
65}
66
67bool Type::isReferenceType() const {
68 return isa<ReferenceType>(CanonicalType);
69}
70
71bool Type::isArrayType() const {
72 return isa<ArrayType>(CanonicalType);
73}
74
75bool Type::isStructureType() const {
76 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
77 if (TT->getDecl()->getKind() == Decl::Struct)
78 return true;
79 }
80 return false;
81}
82
83bool Type::isUnionType() const {
84 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
85 if (TT->getDecl()->getKind() == Decl::Union)
86 return true;
87 }
88 return false;
89}
90
91// C99 6.2.7p1: If both are complete types, then the following additional
92// requirements apply...FIXME (handle compatibility across source files).
93bool Type::tagTypesAreCompatible(QualType lhs, QualType rhs) {
94 TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
95 TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
96
97 if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
98 if (ldecl->getIdentifier() == rdecl->getIdentifier())
99 return true;
100 }
101 if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
102 if (ldecl->getIdentifier() == rdecl->getIdentifier())
103 return true;
104 }
105 return false;
106}
107
108bool Type::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
109 // C99 6.7.5.1p2: For two pointer types to be compatible, both shall be
110 // identically qualified and both shall be pointers to compatible types.
111 if (lhs.getQualifiers() != rhs.getQualifiers())
112 return false;
113
114 QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
115 QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
116
117 return typesAreCompatible(ltype, rtype);
118}
119
120// C++ 5.17p6: When the left opperand of an assignment operator denotes a
121// reference to T, the operation assigns to the object of type T denoted by the
122// reference.
123bool Type::referenceTypesAreCompatible(QualType lhs, QualType rhs) {
124 QualType ltype = lhs;
125
126 if (lhs->isReferenceType())
127 ltype = cast<ReferenceType>(lhs.getCanonicalType())->getReferenceeType();
128
129 QualType rtype = rhs;
130
131 if (rhs->isReferenceType())
132 rtype = cast<ReferenceType>(rhs.getCanonicalType())->getReferenceeType();
133
134 return typesAreCompatible(ltype, rtype);
135}
136
137bool Type::functionTypesAreCompatible(QualType lhs, QualType rhs) {
138 const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
139 const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
140 const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
141 const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
142
143 // first check the return types (common between C99 and K&R).
144 if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
145 return false;
146
147 if (lproto && rproto) { // two C99 style function prototypes
148 unsigned lproto_nargs = lproto->getNumArgs();
149 unsigned rproto_nargs = rproto->getNumArgs();
150
151 if (lproto_nargs != rproto_nargs)
152 return false;
153
154 // both prototypes have the same number of arguments.
155 if ((lproto->isVariadic() && !rproto->isVariadic()) ||
156 (rproto->isVariadic() && !lproto->isVariadic()))
157 return false;
158
159 // The use of ellipsis agree...now check the argument types.
160 for (unsigned i = 0; i < lproto_nargs; i++)
161 if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
162 return false;
163 return true;
164 }
165 if (!lproto && !rproto) // two K&R style function decls, nothing to do.
166 return true;
167
168 // we have a mixture of K&R style with C99 prototypes
169 const FunctionTypeProto *proto = lproto ? lproto : rproto;
170
171 if (proto->isVariadic())
172 return false;
173
174 // FIXME: Each parameter type T in the prototype must be compatible with the
175 // type resulting from applying the usual argument conversions to T.
176 return true;
177}
178
179bool Type::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
180 QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
181 QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
182
183 if (!typesAreCompatible(ltype, rtype))
184 return false;
185
186 // FIXME: If both types specify constant sizes, then the sizes must also be
187 // the same. Even if the sizes are the same, GCC produces an error.
188 return true;
189}
190
191/// typesAreCompatible - C99 6.7.3p9: For two qualified types to be compatible,
192/// both shall have the identically qualified version of a compatible type.
193/// C99 6.2.7p1: Two types have compatible types if their types are the
194/// same. See 6.7.[2,3,5] for additional rules.
195bool Type::typesAreCompatible(QualType lhs, QualType rhs) {
196 QualType lcanon = lhs.getCanonicalType();
197 QualType rcanon = rhs.getCanonicalType();
198
199 // If two types are identical, they are are compatible
200 if (lcanon == rcanon)
201 return true;
202
203 // If the canonical type classes don't match, they can't be compatible
204 if (lcanon->getTypeClass() != rcanon->getTypeClass())
205 return false;
206
207 switch (lcanon->getTypeClass()) {
208 case Type::Pointer:
209 return pointerTypesAreCompatible(lcanon, rcanon);
210 case Type::Reference:
211 return referenceTypesAreCompatible(lcanon, rcanon);
212 case Type::Array:
213 return arrayTypesAreCompatible(lcanon, rcanon);
214 case Type::FunctionNoProto:
215 case Type::FunctionProto:
216 return functionTypesAreCompatible(lcanon, rcanon);
217 case Type::Tagged: // handle structures, unions
218 return tagTypesAreCompatible(lcanon, rcanon);
219 case Type::Builtin:
220 return false;
221 default:
222 assert(0 && "unexpected type");
223 }
224 return true; // should never get here...
225}
226
227bool Type::isIntegerType() const {
228 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
229 return BT->getKind() >= BuiltinType::Bool &&
230 BT->getKind() <= BuiltinType::LongLong;
231 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
232 if (TT->getDecl()->getKind() == Decl::Enum)
233 return true;
234 return false;
235}
236
237bool Type::isSignedIntegerType() const {
238 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
239 return BT->getKind() >= BuiltinType::Char_S &&
240 BT->getKind() <= BuiltinType::LongLong;
241 }
242 return false;
243}
244
245bool Type::isUnsignedIntegerType() const {
246 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
247 return BT->getKind() >= BuiltinType::Bool &&
248 BT->getKind() <= BuiltinType::ULongLong;
249 }
250 return false;
251}
252
253bool Type::isFloatingType() const {
254 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
255 return BT->getKind() >= BuiltinType::Float &&
256 BT->getKind() <= BuiltinType::LongDouble;
257 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
258 return CT->isFloatingType();
259 return false;
260}
261
262bool Type::isRealFloatingType() const {
263 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
264 return BT->getKind() >= BuiltinType::Float &&
265 BT->getKind() <= BuiltinType::LongDouble;
266 return false;
267}
268
269bool Type::isRealType() const {
270 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
271 return BT->getKind() >= BuiltinType::Bool &&
272 BT->getKind() <= BuiltinType::LongDouble;
273 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
274 return TT->getDecl()->getKind() == Decl::Enum;
275 return false;
276}
277
278bool Type::isComplexType() const {
279 return isa<ComplexType>(CanonicalType);
280}
281
282bool Type::isVectorType() const {
283 return isa<VectorType>(CanonicalType);
284}
285
286bool Type::isArithmeticType() const {
287 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
288 return BT->getKind() != BuiltinType::Void;
289 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
290 if (TT->getDecl()->getKind() == Decl::Enum)
291 return true;
292 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
293}
294
295bool Type::isScalarType() const {
296 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
297 return BT->getKind() != BuiltinType::Void;
298 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
299 if (TT->getDecl()->getKind() == Decl::Enum)
300 return true;
301 return false;
302 }
303 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType);
304}
305
306bool Type::isAggregateType() const {
307 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
308 if (TT->getDecl()->getKind() == Decl::Struct)
309 return true;
310 return false;
311 }
312 return CanonicalType->getTypeClass() == Array;
313}
314
315// The only variable size types are auto arrays within a function. Structures
316// cannot contain a VLA member. They can have a flexible array member, however
317// the structure is still constant size (C99 6.7.2.1p16).
318bool Type::isConstantSizeType(SourceLocation *loc) const {
319 if (const ArrayType *Ary = dyn_cast<ArrayType>(CanonicalType)) {
320 assert(Ary->getSize() && "Incomplete types don't have a size at all!");
321 return Ary->getSize()->isIntegerConstantExpr(loc); // Variable Length Array?
322 }
323 return true;
324}
325
326/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
327/// - a type that can describe objects, but which lacks information needed to
328/// determine its size.
329bool Type::isIncompleteType() const {
330 switch (CanonicalType->getTypeClass()) {
331 default: return false;
332 case Builtin:
333 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
334 // be completed.
335 return isVoidType();
336 case Tagged:
337 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
338 // forward declaration, but not a full definition (C99 6.2.5p22).
339 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
340 case Array:
341 // An array of unknown size is an incomplete type (C99 6.2.5p22).
342 return cast<ArrayType>(CanonicalType)->getSize() == 0;
343 }
344}
345
346bool Type::isPromotableIntegerType() const {
347 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
348 if (!BT) return false;
349 switch (BT->getKind()) {
350 case BuiltinType::Bool:
351 case BuiltinType::Char_S:
352 case BuiltinType::Char_U:
353 case BuiltinType::SChar:
354 case BuiltinType::UChar:
355 case BuiltinType::Short:
356 case BuiltinType::UShort:
357 return true;
358 default:
359 return false;
360 }
361}
362
363const char *BuiltinType::getName() const {
364 switch (getKind()) {
365 default: assert(0 && "Unknown builtin type!");
366 case Void: return "void";
367 case Bool: return "_Bool";
368 case Char_S: return "char";
369 case Char_U: return "char";
370 case SChar: return "signed char";
371 case Short: return "short";
372 case Int: return "int";
373 case Long: return "long";
374 case LongLong: return "long long";
375 case UChar: return "unsigned char";
376 case UShort: return "unsigned short";
377 case UInt: return "unsigned int";
378 case ULong: return "unsigned long";
379 case ULongLong: return "unsigned long long";
380 case Float: return "float";
381 case Double: return "double";
382 case LongDouble: return "long double";
383 }
384}
385
386// FIXME: need to use TargetInfo to derive the target specific sizes. This
387// implementation will suffice for play with vector support.
388unsigned BuiltinType::getSize() const {
389 switch (getKind()) {
390 default: assert(0 && "Unknown builtin type!");
391 case Void: return 0;
392 case Bool:
393 case Char_S:
394 case Char_U: return sizeof(char) * 8;
395 case SChar: return sizeof(signed char) * 8;
396 case Short: return sizeof(short) * 8;
397 case Int: return sizeof(int) * 8;
398 case Long: return sizeof(long) * 8;
399 case LongLong: return sizeof(long long) * 8;
400 case UChar: return sizeof(unsigned char) * 8;
401 case UShort: return sizeof(unsigned short) * 8;
402 case UInt: return sizeof(unsigned int) * 8;
403 case ULong: return sizeof(unsigned long) * 8;
404 case ULongLong: return sizeof(unsigned long long) * 8;
405 case Float: return sizeof(float) * 8;
406 case Double: return sizeof(double) * 8;
407 case LongDouble: return sizeof(long double) * 8;
408 }
409}
410
411void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
412 QualType* ArgTys,
413 unsigned NumArgs, bool isVariadic) {
414 ID.AddPointer(Result.getAsOpaquePtr());
415 for (unsigned i = 0; i != NumArgs; ++i)
416 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
417 ID.AddInteger(isVariadic);
418}
419
420void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
421 Profile(ID, getResultType(), ArgInfo, NumArgs, isVariadic());
422}
423
424
425bool RecordType::classof(const Type *T) {
426 if (const TagType *TT = dyn_cast<TagType>(T))
427 return isa<RecordDecl>(TT->getDecl());
428 return false;
429}
430
431
432//===----------------------------------------------------------------------===//
433// Type Printing
434//===----------------------------------------------------------------------===//
435
436void QualType::dump(const char *msg) const {
437 std::string R = "foo";
438 getAsStringInternal(R);
439 if (msg)
440 fprintf(stderr, "%s: %s\n", msg, R.c_str());
441 else
442 fprintf(stderr, "%s\n", R.c_str());
443}
444
445static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
446 // Note: funkiness to ensure we get a space only between quals.
447 bool NonePrinted = true;
448 if (TypeQuals & QualType::Const)
449 S += "const", NonePrinted = false;
450 if (TypeQuals & QualType::Volatile)
451 S += (NonePrinted+" volatile"), NonePrinted = false;
452 if (TypeQuals & QualType::Restrict)
453 S += (NonePrinted+" restrict"), NonePrinted = false;
454}
455
456void QualType::getAsStringInternal(std::string &S) const {
457 if (isNull()) {
458 S += "NULL TYPE\n";
459 return;
460 }
461
462 // Print qualifiers as appropriate.
463 if (unsigned TQ = getQualifiers()) {
464 std::string TQS;
465 AppendTypeQualList(TQS, TQ);
466 if (!S.empty())
467 S = TQS + ' ' + S;
468 else
469 S = TQS;
470 }
471
472 getTypePtr()->getAsStringInternal(S);
473}
474
475void BuiltinType::getAsStringInternal(std::string &S) const {
476 if (S.empty()) {
477 S = getName();
478 } else {
479 // Prefix the basic type, e.g. 'int X'.
480 S = ' ' + S;
481 S = getName() + S;
482 }
483}
484
485void ComplexType::getAsStringInternal(std::string &S) const {
486 ElementType->getAsStringInternal(S);
487 S = "_Complex " + S;
488}
489
490void PointerType::getAsStringInternal(std::string &S) const {
491 S = '*' + S;
492
493 // Handle things like 'int (*A)[4];' correctly.
494 // FIXME: this should include vectors, but vectors use attributes I guess.
495 if (isa<ArrayType>(PointeeType.getTypePtr()))
496 S = '(' + S + ')';
497
498 PointeeType.getAsStringInternal(S);
499}
500
501void ReferenceType::getAsStringInternal(std::string &S) const {
502 S = '&' + S;
503
504 // Handle things like 'int (&A)[4];' correctly.
505 // FIXME: this should include vectors, but vectors use attributes I guess.
506 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
507 S = '(' + S + ')';
508
509 ReferenceeType.getAsStringInternal(S);
510}
511
512void ArrayType::getAsStringInternal(std::string &S) const {
513 S += '[';
514
515 if (IndexTypeQuals) {
516 AppendTypeQualList(S, IndexTypeQuals);
517 S += ' ';
518 }
519
520 if (SizeModifier == Static)
521 S += "static";
522 else if (SizeModifier == Star)
523 S += '*';
524
525 S += ']';
526
527 ElementType.getAsStringInternal(S);
528}
529
530void VectorType::getAsStringInternal(std::string &S) const {
531 S += " __attribute__(( vector_size(";
532 // FIXME: handle types that are != 32 bits.
533 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
534 S += ") ))";
535 ElementType.getAsStringInternal(S);
536}
537
538void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
539 // If needed for precedence reasons, wrap the inner part in grouping parens.
540 if (!S.empty())
541 S = "(" + S + ")";
542
543 S += "()";
544 getResultType().getAsStringInternal(S);
545}
546
547void FunctionTypeProto::getAsStringInternal(std::string &S) const {
548 // If needed for precedence reasons, wrap the inner part in grouping parens.
549 if (!S.empty())
550 S = "(" + S + ")";
551
552 S += "(";
553 std::string Tmp;
554 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
555 if (i) S += ", ";
556 getArgType(i).getAsStringInternal(Tmp);
557 S += Tmp;
558 Tmp.clear();
559 }
560
561 if (isVariadic()) {
562 if (getNumArgs())
563 S += ", ";
564 S += "...";
565 } else if (getNumArgs() == 0) {
566 // Do not emit int() if we have a proto, emit 'int(void)'.
567 S += "void";
568 }
569
570 S += ")";
571 getResultType().getAsStringInternal(S);
572}
573
574
575void TypedefType::getAsStringInternal(std::string &InnerString) const {
576 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
577 InnerString = ' ' + InnerString;
578 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
579}
580
581void TagType::getAsStringInternal(std::string &InnerString) const {
582 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
583 InnerString = ' ' + InnerString;
584
585 const char *Kind = getDecl()->getKindName();
586 const char *ID;
587 if (const IdentifierInfo *II = getDecl()->getIdentifier())
588 ID = II->getName();
589 else
590 ID = "<anonymous>";
591
592 InnerString = std::string(Kind) + " " + ID + InnerString;
593}