blob: ded7af4d6231fdca5c66cf106bb1230e9435775f [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;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000234 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
235 return VT->getElementType()->isIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000236 return false;
237}
238
239bool Type::isSignedIntegerType() const {
240 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
241 return BT->getKind() >= BuiltinType::Char_S &&
242 BT->getKind() <= BuiltinType::LongLong;
243 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000244 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
245 return VT->getElementType()->isSignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000246 return false;
247}
248
249bool Type::isUnsignedIntegerType() const {
250 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
251 return BT->getKind() >= BuiltinType::Bool &&
252 BT->getKind() <= BuiltinType::ULongLong;
253 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000254 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
255 return VT->getElementType()->isUnsignedIntegerType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000256 return false;
257}
258
259bool Type::isFloatingType() const {
260 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
261 return BT->getKind() >= BuiltinType::Float &&
262 BT->getKind() <= BuiltinType::LongDouble;
263 if (const ComplexType *CT = dyn_cast<ComplexType>(CanonicalType))
264 return CT->isFloatingType();
Steve Naroffc63b96a2007-07-12 21:46:55 +0000265 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
266 return VT->getElementType()->isFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000267 return false;
268}
269
270bool Type::isRealFloatingType() const {
271 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
272 return BT->getKind() >= BuiltinType::Float &&
273 BT->getKind() <= BuiltinType::LongDouble;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000274 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
275 return VT->getElementType()->isRealFloatingType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000276 return false;
277}
278
279bool Type::isRealType() const {
280 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
281 return BT->getKind() >= BuiltinType::Bool &&
282 BT->getKind() <= BuiltinType::LongDouble;
283 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
284 return TT->getDecl()->getKind() == Decl::Enum;
Steve Naroffc63b96a2007-07-12 21:46:55 +0000285 if (const VectorType *VT = dyn_cast<VectorType>(CanonicalType))
286 return VT->getElementType()->isRealType();
Reid Spencer5f016e22007-07-11 17:01:13 +0000287 return false;
288}
289
290bool Type::isComplexType() const {
291 return isa<ComplexType>(CanonicalType);
292}
293
294bool Type::isVectorType() const {
295 return isa<VectorType>(CanonicalType);
296}
297
298bool Type::isArithmeticType() const {
299 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
300 return BT->getKind() != BuiltinType::Void;
301 if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
302 if (TT->getDecl()->getKind() == Decl::Enum)
303 return true;
304 return isa<ComplexType>(CanonicalType) || isa<VectorType>(CanonicalType);
305}
306
307bool Type::isScalarType() const {
308 if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
309 return BT->getKind() != BuiltinType::Void;
310 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
311 if (TT->getDecl()->getKind() == Decl::Enum)
312 return true;
313 return false;
314 }
Steve Naroffc63b96a2007-07-12 21:46:55 +0000315 return isa<PointerType>(CanonicalType) || isa<ComplexType>(CanonicalType) ||
316 isa<VectorType>(CanonicalType);
Reid Spencer5f016e22007-07-11 17:01:13 +0000317}
318
319bool Type::isAggregateType() const {
320 if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
321 if (TT->getDecl()->getKind() == Decl::Struct)
322 return true;
323 return false;
324 }
325 return CanonicalType->getTypeClass() == Array;
326}
327
328// The only variable size types are auto arrays within a function. Structures
329// cannot contain a VLA member. They can have a flexible array member, however
330// the structure is still constant size (C99 6.7.2.1p16).
331bool Type::isConstantSizeType(SourceLocation *loc) const {
332 if (const ArrayType *Ary = dyn_cast<ArrayType>(CanonicalType)) {
333 assert(Ary->getSize() && "Incomplete types don't have a size at all!");
334 return Ary->getSize()->isIntegerConstantExpr(loc); // Variable Length Array?
335 }
336 return true;
337}
338
339/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
340/// - a type that can describe objects, but which lacks information needed to
341/// determine its size.
342bool Type::isIncompleteType() const {
343 switch (CanonicalType->getTypeClass()) {
344 default: return false;
345 case Builtin:
346 // Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
347 // be completed.
348 return isVoidType();
349 case Tagged:
350 // A tagged type (struct/union/enum/class) is incomplete if the decl is a
351 // forward declaration, but not a full definition (C99 6.2.5p22).
352 return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
353 case Array:
354 // An array of unknown size is an incomplete type (C99 6.2.5p22).
355 return cast<ArrayType>(CanonicalType)->getSize() == 0;
356 }
357}
358
359bool Type::isPromotableIntegerType() const {
360 const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType);
361 if (!BT) return false;
362 switch (BT->getKind()) {
363 case BuiltinType::Bool:
364 case BuiltinType::Char_S:
365 case BuiltinType::Char_U:
366 case BuiltinType::SChar:
367 case BuiltinType::UChar:
368 case BuiltinType::Short:
369 case BuiltinType::UShort:
370 return true;
371 default:
372 return false;
373 }
374}
375
376const char *BuiltinType::getName() const {
377 switch (getKind()) {
378 default: assert(0 && "Unknown builtin type!");
379 case Void: return "void";
380 case Bool: return "_Bool";
381 case Char_S: return "char";
382 case Char_U: return "char";
383 case SChar: return "signed char";
384 case Short: return "short";
385 case Int: return "int";
386 case Long: return "long";
387 case LongLong: return "long long";
388 case UChar: return "unsigned char";
389 case UShort: return "unsigned short";
390 case UInt: return "unsigned int";
391 case ULong: return "unsigned long";
392 case ULongLong: return "unsigned long long";
393 case Float: return "float";
394 case Double: return "double";
395 case LongDouble: return "long double";
396 }
397}
398
399// FIXME: need to use TargetInfo to derive the target specific sizes. This
400// implementation will suffice for play with vector support.
401unsigned BuiltinType::getSize() const {
402 switch (getKind()) {
403 default: assert(0 && "Unknown builtin type!");
404 case Void: return 0;
405 case Bool:
406 case Char_S:
407 case Char_U: return sizeof(char) * 8;
408 case SChar: return sizeof(signed char) * 8;
409 case Short: return sizeof(short) * 8;
410 case Int: return sizeof(int) * 8;
411 case Long: return sizeof(long) * 8;
412 case LongLong: return sizeof(long long) * 8;
413 case UChar: return sizeof(unsigned char) * 8;
414 case UShort: return sizeof(unsigned short) * 8;
415 case UInt: return sizeof(unsigned int) * 8;
416 case ULong: return sizeof(unsigned long) * 8;
417 case ULongLong: return sizeof(unsigned long long) * 8;
418 case Float: return sizeof(float) * 8;
419 case Double: return sizeof(double) * 8;
420 case LongDouble: return sizeof(long double) * 8;
421 }
422}
423
424void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID, QualType Result,
425 QualType* ArgTys,
426 unsigned NumArgs, bool isVariadic) {
427 ID.AddPointer(Result.getAsOpaquePtr());
428 for (unsigned i = 0; i != NumArgs; ++i)
429 ID.AddPointer(ArgTys[i].getAsOpaquePtr());
430 ID.AddInteger(isVariadic);
431}
432
433void FunctionTypeProto::Profile(llvm::FoldingSetNodeID &ID) {
434 Profile(ID, getResultType(), ArgInfo, NumArgs, isVariadic());
435}
436
437
438bool RecordType::classof(const Type *T) {
439 if (const TagType *TT = dyn_cast<TagType>(T))
440 return isa<RecordDecl>(TT->getDecl());
441 return false;
442}
443
444
445//===----------------------------------------------------------------------===//
446// Type Printing
447//===----------------------------------------------------------------------===//
448
449void QualType::dump(const char *msg) const {
450 std::string R = "foo";
451 getAsStringInternal(R);
452 if (msg)
453 fprintf(stderr, "%s: %s\n", msg, R.c_str());
454 else
455 fprintf(stderr, "%s\n", R.c_str());
456}
457
458static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
459 // Note: funkiness to ensure we get a space only between quals.
460 bool NonePrinted = true;
461 if (TypeQuals & QualType::Const)
462 S += "const", NonePrinted = false;
463 if (TypeQuals & QualType::Volatile)
464 S += (NonePrinted+" volatile"), NonePrinted = false;
465 if (TypeQuals & QualType::Restrict)
466 S += (NonePrinted+" restrict"), NonePrinted = false;
467}
468
469void QualType::getAsStringInternal(std::string &S) const {
470 if (isNull()) {
471 S += "NULL TYPE\n";
472 return;
473 }
474
475 // Print qualifiers as appropriate.
476 if (unsigned TQ = getQualifiers()) {
477 std::string TQS;
478 AppendTypeQualList(TQS, TQ);
479 if (!S.empty())
480 S = TQS + ' ' + S;
481 else
482 S = TQS;
483 }
484
485 getTypePtr()->getAsStringInternal(S);
486}
487
488void BuiltinType::getAsStringInternal(std::string &S) const {
489 if (S.empty()) {
490 S = getName();
491 } else {
492 // Prefix the basic type, e.g. 'int X'.
493 S = ' ' + S;
494 S = getName() + S;
495 }
496}
497
498void ComplexType::getAsStringInternal(std::string &S) const {
499 ElementType->getAsStringInternal(S);
500 S = "_Complex " + S;
501}
502
503void PointerType::getAsStringInternal(std::string &S) const {
504 S = '*' + S;
505
506 // Handle things like 'int (*A)[4];' correctly.
507 // FIXME: this should include vectors, but vectors use attributes I guess.
508 if (isa<ArrayType>(PointeeType.getTypePtr()))
509 S = '(' + S + ')';
510
511 PointeeType.getAsStringInternal(S);
512}
513
514void ReferenceType::getAsStringInternal(std::string &S) const {
515 S = '&' + S;
516
517 // Handle things like 'int (&A)[4];' correctly.
518 // FIXME: this should include vectors, but vectors use attributes I guess.
519 if (isa<ArrayType>(ReferenceeType.getTypePtr()))
520 S = '(' + S + ')';
521
522 ReferenceeType.getAsStringInternal(S);
523}
524
525void ArrayType::getAsStringInternal(std::string &S) const {
526 S += '[';
527
528 if (IndexTypeQuals) {
529 AppendTypeQualList(S, IndexTypeQuals);
530 S += ' ';
531 }
532
533 if (SizeModifier == Static)
534 S += "static";
535 else if (SizeModifier == Star)
536 S += '*';
537
538 S += ']';
539
540 ElementType.getAsStringInternal(S);
541}
542
543void VectorType::getAsStringInternal(std::string &S) const {
544 S += " __attribute__(( vector_size(";
545 // FIXME: handle types that are != 32 bits.
546 S += llvm::utostr_32(NumElements*4); // convert back to bytes.
547 S += ") ))";
548 ElementType.getAsStringInternal(S);
549}
550
551void FunctionTypeNoProto::getAsStringInternal(std::string &S) const {
552 // If needed for precedence reasons, wrap the inner part in grouping parens.
553 if (!S.empty())
554 S = "(" + S + ")";
555
556 S += "()";
557 getResultType().getAsStringInternal(S);
558}
559
560void FunctionTypeProto::getAsStringInternal(std::string &S) const {
561 // If needed for precedence reasons, wrap the inner part in grouping parens.
562 if (!S.empty())
563 S = "(" + S + ")";
564
565 S += "(";
566 std::string Tmp;
567 for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
568 if (i) S += ", ";
569 getArgType(i).getAsStringInternal(Tmp);
570 S += Tmp;
571 Tmp.clear();
572 }
573
574 if (isVariadic()) {
575 if (getNumArgs())
576 S += ", ";
577 S += "...";
578 } else if (getNumArgs() == 0) {
579 // Do not emit int() if we have a proto, emit 'int(void)'.
580 S += "void";
581 }
582
583 S += ")";
584 getResultType().getAsStringInternal(S);
585}
586
587
588void TypedefType::getAsStringInternal(std::string &InnerString) const {
589 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
590 InnerString = ' ' + InnerString;
591 InnerString = getDecl()->getIdentifier()->getName() + InnerString;
592}
593
594void TagType::getAsStringInternal(std::string &InnerString) const {
595 if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
596 InnerString = ' ' + InnerString;
597
598 const char *Kind = getDecl()->getKindName();
599 const char *ID;
600 if (const IdentifierInfo *II = getDecl()->getIdentifier())
601 ID = II->getName();
602 else
603 ID = "<anonymous>";
604
605 InnerString = std::string(Kind) + " " + ID + InnerString;
606}