2006-11-12 06:59:23 +08:00
|
|
|
//===--- Type.cpp - Type representation and manipulation ------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements type-related functionality.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2006-11-20 12:02:15 +08:00
|
|
|
#include "clang/Lex/IdentifierTable.h"
|
2006-11-12 06:59:23 +08:00
|
|
|
#include "clang/AST/Type.h"
|
2006-11-20 12:02:15 +08:00
|
|
|
#include "clang/AST/Decl.h"
|
2007-05-09 05:09:37 +08:00
|
|
|
#include "clang/AST/Expr.h"
|
2007-03-22 05:08:52 +08:00
|
|
|
|
2006-11-12 08:37:36 +08:00
|
|
|
#include <iostream>
|
2007-03-22 05:08:52 +08:00
|
|
|
|
2006-11-12 06:59:23 +08:00
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
Type::~Type() {}
|
|
|
|
|
2006-12-03 10:43:54 +08:00
|
|
|
/// isVoidType - Helper method to determine if this is the 'void' type.
|
|
|
|
bool Type::isVoidType() const {
|
2007-03-22 05:08:52 +08:00
|
|
|
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
|
2006-12-03 10:57:32 +08:00
|
|
|
return BT->getKind() == BuiltinType::Void;
|
2006-12-03 10:43:54 +08:00
|
|
|
return false;
|
|
|
|
}
|
2006-11-12 08:37:36 +08:00
|
|
|
|
2007-03-29 05:49:40 +08:00
|
|
|
bool Type::isObjectType() const {
|
|
|
|
if (isa<FunctionType>(CanonicalType))
|
|
|
|
return false;
|
|
|
|
else if (CanonicalType->isIncompleteType())
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isDerivedType() const {
|
|
|
|
return isPointerType() || isArrayType() || isFunctionType() ||
|
|
|
|
isStructureType() || isUnionType();
|
|
|
|
}
|
|
|
|
|
2007-03-22 05:08:52 +08:00
|
|
|
bool Type::isFunctionType() const {
|
2007-03-31 04:09:34 +08:00
|
|
|
return isa<FunctionType>(CanonicalType);
|
2007-03-22 05:08:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isPointerType() const {
|
2007-03-31 04:09:34 +08:00
|
|
|
return isa<PointerType>(CanonicalType);
|
2007-03-22 05:08:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isArrayType() const {
|
2007-03-31 04:09:34 +08:00
|
|
|
return isa<ArrayType>(CanonicalType);
|
2007-03-22 05:08:52 +08:00
|
|
|
}
|
|
|
|
|
2007-03-27 07:09:51 +08:00
|
|
|
bool Type::isStructureType() const {
|
2007-03-24 06:27:02 +08:00
|
|
|
if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
|
2007-03-22 05:08:52 +08:00
|
|
|
if (TT->getDecl()->getKind() == Decl::Struct)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isUnionType() const {
|
2007-03-24 06:27:02 +08:00
|
|
|
if (const TagType *TT = dyn_cast<TagType>(CanonicalType)) {
|
2007-03-22 05:08:52 +08:00
|
|
|
if (TT->getDecl()->getKind() == Decl::Union)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-05-02 10:47:33 +08:00
|
|
|
// C99 6.2.7p1: If both are complete types, then the following additional
|
|
|
|
// requirements apply...FIXME (handle compatibility across source files).
|
|
|
|
bool Type::structureTypesAreCompatible(QualType lhs, QualType rhs) {
|
|
|
|
TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
|
|
|
|
TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
|
|
|
|
|
|
|
|
if (ldecl->getKind() == Decl::Struct && rdecl->getKind() == Decl::Struct) {
|
|
|
|
if (ldecl->getIdentifier() == rdecl->getIdentifier())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::unionTypesAreCompatible(QualType lhs, QualType rhs) {
|
|
|
|
TagDecl *ldecl = cast<TagType>(lhs.getCanonicalType())->getDecl();
|
|
|
|
TagDecl *rdecl = cast<TagType>(rhs.getCanonicalType())->getDecl();
|
|
|
|
|
|
|
|
if (ldecl->getKind() == Decl::Union && rdecl->getKind() == Decl::Union) {
|
|
|
|
if (ldecl->getIdentifier() == rdecl->getIdentifier())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::pointerTypesAreCompatible(QualType lhs, QualType rhs) {
|
|
|
|
QualType ltype = cast<PointerType>(lhs.getCanonicalType())->getPointeeType();
|
|
|
|
QualType rtype = cast<PointerType>(rhs.getCanonicalType())->getPointeeType();
|
|
|
|
|
|
|
|
// handle void first (not sure this is the best place to check for this).
|
|
|
|
if (ltype->isVoidType() || rtype->isVoidType())
|
|
|
|
return true;
|
|
|
|
return typesAreCompatible(ltype, rtype);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::functionTypesAreCompatible(QualType lhs, QualType rhs) {
|
2007-05-03 03:22:16 +08:00
|
|
|
const FunctionType *lbase = cast<FunctionType>(lhs.getCanonicalType());
|
|
|
|
const FunctionType *rbase = cast<FunctionType>(rhs.getCanonicalType());
|
|
|
|
const FunctionTypeProto *lproto = dyn_cast<FunctionTypeProto>(lbase);
|
|
|
|
const FunctionTypeProto *rproto = dyn_cast<FunctionTypeProto>(rbase);
|
|
|
|
|
|
|
|
// first check the return types (common between C99 and K&R).
|
|
|
|
if (!typesAreCompatible(lbase->getResultType(), rbase->getResultType()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (lproto && rproto) { // two C99 style function prototypes
|
|
|
|
unsigned lproto_nargs = lproto->getNumArgs();
|
|
|
|
unsigned rproto_nargs = rproto->getNumArgs();
|
|
|
|
|
|
|
|
if (lproto_nargs != rproto_nargs)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// both prototypes have the same number of arguments.
|
|
|
|
if ((lproto->isVariadic() && !rproto->isVariadic()) ||
|
|
|
|
(rproto->isVariadic() && !lproto->isVariadic()))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// The use of ellipsis agree...now check the argument types.
|
|
|
|
for (unsigned i = 0; i < lproto_nargs; i++)
|
|
|
|
if (!typesAreCompatible(lproto->getArgType(i), rproto->getArgType(i)))
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!lproto && !rproto) // two K&R style function decls, nothing to do.
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// we have a mixture of K&R style with C99 prototypes
|
|
|
|
const FunctionTypeProto *proto = lproto ? lproto : rproto;
|
|
|
|
|
|
|
|
if (proto->isVariadic())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: Each parameter type T in the prototype must be compatible with the
|
|
|
|
// type resulting from applying the usual argument conversions to T.
|
|
|
|
return true;
|
2007-05-02 10:47:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::arrayTypesAreCompatible(QualType lhs, QualType rhs) {
|
|
|
|
QualType ltype = cast<ArrayType>(lhs.getCanonicalType())->getElementType();
|
|
|
|
QualType rtype = cast<ArrayType>(rhs.getCanonicalType())->getElementType();
|
|
|
|
|
|
|
|
if (!typesAreCompatible(ltype, rtype))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// FIXME: If both types specify constant sizes, then the sizes must also be
|
|
|
|
// the same. Even if the sizes are the same, GCC produces an error.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::typesAreCompatible(QualType lcanon, QualType rcanon) {
|
|
|
|
// If two types are identical, they are are compatible
|
2007-05-03 03:22:16 +08:00
|
|
|
// C99 6.7.3p9: For two qualified types to be compatible, both shall have
|
|
|
|
// the identically qualified version of a compatible type.
|
2007-05-02 10:47:33 +08:00
|
|
|
if (lcanon == rcanon)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (lcanon->isStructureType() && rcanon->isStructureType())
|
|
|
|
return structureTypesAreCompatible(lcanon, rcanon);
|
|
|
|
|
|
|
|
if (lcanon->isPointerType() && rcanon->isPointerType())
|
|
|
|
return pointerTypesAreCompatible(lcanon, rcanon);
|
|
|
|
|
|
|
|
if (lcanon->isArrayType() && rcanon->isArrayType())
|
|
|
|
return arrayTypesAreCompatible(lcanon, rcanon);
|
|
|
|
|
|
|
|
if (lcanon->isFunctionType() && rcanon->isFunctionType())
|
|
|
|
return functionTypesAreCompatible(lcanon, rcanon);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-04-24 08:23:05 +08:00
|
|
|
bool Type::isIntegerType() const {
|
2007-04-26 05:10:52 +08:00
|
|
|
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
|
2007-03-22 05:08:52 +08:00
|
|
|
return BT->getKind() >= BuiltinType::Bool &&
|
|
|
|
BT->getKind() <= BuiltinType::ULongLong;
|
2007-04-26 05:10:52 +08:00
|
|
|
if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
|
2007-03-22 05:08:52 +08:00
|
|
|
if (TT->getDecl()->getKind() == Decl::Enum)
|
|
|
|
return true;
|
2007-04-26 05:10:52 +08:00
|
|
|
return false;
|
2007-03-22 05:08:52 +08:00
|
|
|
}
|
|
|
|
|
2007-04-24 08:23:05 +08:00
|
|
|
bool Type::isSignedIntegerType() const {
|
2007-04-25 04:56:26 +08:00
|
|
|
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
|
2007-04-24 08:23:05 +08:00
|
|
|
return BT->getKind() >= BuiltinType::SChar &&
|
|
|
|
BT->getKind() <= BuiltinType::LongLong;
|
2007-04-25 04:56:26 +08:00
|
|
|
return false;
|
2007-04-24 08:23:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isUnsignedIntegerType() const {
|
2007-04-25 04:56:26 +08:00
|
|
|
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
|
2007-04-24 08:23:05 +08:00
|
|
|
return BT->getKind() >= BuiltinType::UChar &&
|
|
|
|
BT->getKind() <= BuiltinType::ULongLong;
|
2007-04-25 04:56:26 +08:00
|
|
|
return false;
|
2007-04-24 08:23:05 +08:00
|
|
|
}
|
|
|
|
|
2007-03-22 05:08:52 +08:00
|
|
|
bool Type::isFloatingType() const {
|
2007-04-25 04:56:26 +08:00
|
|
|
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
|
2007-03-22 05:08:52 +08:00
|
|
|
return BT->getKind() >= BuiltinType::Float &&
|
|
|
|
BT->getKind() <= BuiltinType::LongDoubleComplex;
|
2007-04-25 04:56:26 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-03-31 04:09:34 +08:00
|
|
|
bool Type::isRealFloatingType() const {
|
2007-04-25 04:56:26 +08:00
|
|
|
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
|
2007-03-31 04:09:34 +08:00
|
|
|
return BT->getKind() >= BuiltinType::Float &&
|
|
|
|
BT->getKind() <= BuiltinType::LongDouble;
|
2007-04-25 04:56:26 +08:00
|
|
|
return false;
|
2007-03-31 04:09:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isRealType() const {
|
2007-04-26 05:10:52 +08:00
|
|
|
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
|
2007-03-31 04:09:34 +08:00
|
|
|
return BT->getKind() >= BuiltinType::Bool &&
|
|
|
|
BT->getKind() <= BuiltinType::LongDouble;
|
2007-04-26 05:10:52 +08:00
|
|
|
if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
|
2007-03-31 04:09:34 +08:00
|
|
|
if (TT->getDecl()->getKind() == Decl::Enum)
|
|
|
|
return true;
|
2007-04-26 05:10:52 +08:00
|
|
|
return false;
|
2007-03-31 04:09:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isComplexType() const {
|
2007-04-25 04:56:26 +08:00
|
|
|
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
|
2007-03-31 04:09:34 +08:00
|
|
|
return BT->getKind() >= BuiltinType::FloatComplex &&
|
|
|
|
BT->getKind() <= BuiltinType::LongDoubleComplex;
|
2007-04-25 04:56:26 +08:00
|
|
|
return false;
|
2007-03-31 04:09:34 +08:00
|
|
|
}
|
|
|
|
|
2007-03-22 05:08:52 +08:00
|
|
|
bool Type::isArithmeticType() const {
|
2007-04-25 04:56:26 +08:00
|
|
|
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
|
2007-03-22 05:08:52 +08:00
|
|
|
return BT->getKind() >= BuiltinType::Bool &&
|
|
|
|
BT->getKind() <= BuiltinType::LongDoubleComplex;
|
2007-04-27 05:44:56 +08:00
|
|
|
if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
|
|
|
|
if (TT->getDecl()->getKind() == Decl::Enum)
|
|
|
|
return true;
|
2007-04-25 04:56:26 +08:00
|
|
|
return false;
|
2007-03-22 05:08:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isScalarType() const {
|
2007-04-26 05:10:52 +08:00
|
|
|
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType))
|
2007-03-22 05:08:52 +08:00
|
|
|
return BT->getKind() >= BuiltinType::Bool &&
|
|
|
|
BT->getKind() <= BuiltinType::LongDoubleComplex;
|
2007-04-27 05:44:56 +08:00
|
|
|
if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
|
|
|
|
if (TT->getDecl()->getKind() == Decl::Enum)
|
|
|
|
return true;
|
2007-04-26 05:10:52 +08:00
|
|
|
return CanonicalType->getTypeClass() == Pointer;
|
2007-03-22 05:08:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isAggregateType() const {
|
2007-04-26 05:10:52 +08:00
|
|
|
if (const TagType *TT = dyn_cast<TagType>(CanonicalType))
|
2007-03-22 05:08:52 +08:00
|
|
|
if (TT->getDecl()->getKind() == Decl::Struct)
|
|
|
|
return true;
|
2007-04-26 05:10:52 +08:00
|
|
|
return CanonicalType->getTypeClass() == Array;
|
2007-03-22 05:08:52 +08:00
|
|
|
}
|
|
|
|
|
2007-05-09 05:09:37 +08:00
|
|
|
// The only variable size types are auto arrays within a function. Structures
|
|
|
|
// cannot contain a VLA member. They can have a flexible array member, however
|
|
|
|
// the structure is still constant size (C99 6.7.2.1p16).
|
|
|
|
bool Type::isConstantSizeType() const {
|
|
|
|
if (const ArrayType *ary = dyn_cast<ArrayType>(CanonicalType)) {
|
|
|
|
if (Expr *size = ary->getSize()) {
|
|
|
|
if (!size->isConstantExpr())
|
|
|
|
return false; // Variable Length Array
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 05:08:52 +08:00
|
|
|
|
2007-01-24 05:58:16 +08:00
|
|
|
/// isIncompleteType - Return true if this is an incomplete type (C99 6.2.5p1)
|
|
|
|
/// - a type that can describe objects, but which lacks information needed to
|
|
|
|
/// determine its size.
|
2007-03-22 05:08:52 +08:00
|
|
|
bool Type::isIncompleteType() const {
|
|
|
|
switch (CanonicalType->getTypeClass()) {
|
2007-01-24 05:58:16 +08:00
|
|
|
default: return false;
|
|
|
|
case Builtin:
|
|
|
|
// Void is the only incomplete builtin type. Per C99 6.2.5p19, it can never
|
|
|
|
// be completed.
|
|
|
|
return isVoidType();
|
|
|
|
case Tagged:
|
|
|
|
// A tagged type (struct/union/enum/class) is incomplete if the decl is a
|
|
|
|
// forward declaration, but not a full definition (C99 6.2.5p22).
|
2007-03-24 06:27:02 +08:00
|
|
|
return !cast<TagType>(CanonicalType)->getDecl()->isDefinition();
|
2007-01-24 05:58:16 +08:00
|
|
|
case Array:
|
|
|
|
// An array of unknown size is an incomplete type (C99 6.2.5p22).
|
2007-03-22 05:08:52 +08:00
|
|
|
return cast<ArrayType>(CanonicalType)->getSize() == 0;
|
2007-01-24 05:58:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-31 04:09:34 +08:00
|
|
|
/// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or
|
|
|
|
/// an incomplete type other than void.
|
|
|
|
bool Type::isLvalue() const {
|
|
|
|
if (isObjectType())
|
|
|
|
return true;
|
|
|
|
else if (isIncompleteType())
|
|
|
|
return isVoidType() ? false : true;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-04-21 06:26:17 +08:00
|
|
|
bool Type::isPromotableIntegerType() const {
|
2007-04-26 05:10:52 +08:00
|
|
|
if (const BuiltinType *BT = dyn_cast<BuiltinType>(CanonicalType)) {
|
2007-04-21 06:26:17 +08:00
|
|
|
switch (BT->getKind()) {
|
|
|
|
case BuiltinType::Bool:
|
|
|
|
case BuiltinType::Char:
|
|
|
|
case BuiltinType::SChar:
|
|
|
|
case BuiltinType::UChar:
|
|
|
|
case BuiltinType::Short:
|
|
|
|
case BuiltinType::UShort:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-03-31 04:09:34 +08:00
|
|
|
/// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type,
|
|
|
|
/// does not have an incomplete type, does not have a const-qualified type, and
|
|
|
|
/// if it is a structure or union, does not have any member (including,
|
|
|
|
/// recursively, any member or element of all contained aggregates or unions)
|
|
|
|
/// with a const-qualified type.
|
|
|
|
|
2007-04-06 06:36:20 +08:00
|
|
|
bool QualType::isModifiableLvalue() const {
|
2007-03-31 04:09:34 +08:00
|
|
|
if (isConstQualified())
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return getTypePtr()->isModifiableLvalue();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isModifiableLvalue() const {
|
|
|
|
if (!isLvalue())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (isArrayType())
|
|
|
|
return false;
|
|
|
|
if (isIncompleteType())
|
|
|
|
return false;
|
|
|
|
if (const RecordType *r = dyn_cast<RecordType>(this))
|
|
|
|
return r->isModifiableLvalue();
|
|
|
|
return true;
|
|
|
|
}
|
2007-01-24 05:58:16 +08:00
|
|
|
|
2006-12-03 10:57:32 +08:00
|
|
|
const char *BuiltinType::getName() const {
|
|
|
|
switch (getKind()) {
|
|
|
|
default: assert(0 && "Unknown builtin type!");
|
|
|
|
case Void: return "void";
|
|
|
|
case Bool: return "_Bool";
|
|
|
|
case Char: return "char";
|
|
|
|
case SChar: return "signed char";
|
|
|
|
case Short: return "short";
|
|
|
|
case Int: return "int";
|
|
|
|
case Long: return "long";
|
|
|
|
case LongLong: return "long long";
|
|
|
|
case UChar: return "unsigned char";
|
|
|
|
case UShort: return "unsigned short";
|
|
|
|
case UInt: return "unsigned int";
|
|
|
|
case ULong: return "unsigned long";
|
|
|
|
case ULongLong: return "unsigned long long";
|
|
|
|
case Float: return "float";
|
|
|
|
case Double: return "double";
|
|
|
|
case LongDouble: return "long double";
|
|
|
|
case FloatComplex: return "float _Complex";
|
|
|
|
case DoubleComplex: return "double _Complex";
|
|
|
|
case LongDoubleComplex: return "long double _Complex";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-06 06:36:20 +08:00
|
|
|
void FunctionTypeProto::Profile(FoldingSetNodeID &ID, QualType Result,
|
|
|
|
QualType* ArgTys,
|
2007-01-27 09:15:32 +08:00
|
|
|
unsigned NumArgs, bool isVariadic) {
|
|
|
|
ID.AddPointer(Result.getAsOpaquePtr());
|
|
|
|
for (unsigned i = 0; i != NumArgs; ++i)
|
|
|
|
ID.AddPointer(ArgTys[i].getAsOpaquePtr());
|
|
|
|
ID.AddInteger(isVariadic);
|
|
|
|
}
|
|
|
|
|
2007-01-31 13:05:57 +08:00
|
|
|
void FunctionTypeProto::Profile(FoldingSetNodeID &ID) {
|
|
|
|
Profile(ID, getResultType(), ArgInfo, NumArgs, isVariadic());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-25 08:44:24 +08:00
|
|
|
bool RecordType::classof(const Type *T) {
|
2007-03-24 06:27:02 +08:00
|
|
|
if (const TagType *TT = dyn_cast<TagType>(T))
|
2007-01-25 08:44:24 +08:00
|
|
|
return isa<RecordDecl>(TT->getDecl());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-11-12 08:37:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Type Printing
|
|
|
|
//===----------------------------------------------------------------------===//
|
2006-11-12 06:59:23 +08:00
|
|
|
|
2007-04-06 06:36:20 +08:00
|
|
|
void QualType::dump() const {
|
2006-11-13 15:38:09 +08:00
|
|
|
std::string R = "foo";
|
|
|
|
getAsString(R);
|
2006-11-13 14:22:30 +08:00
|
|
|
std::cerr << R << "\n";
|
2006-11-12 08:37:36 +08:00
|
|
|
}
|
|
|
|
|
2006-11-13 14:22:30 +08:00
|
|
|
static void AppendTypeQualList(std::string &S, unsigned TypeQuals) {
|
2006-11-12 16:50:50 +08:00
|
|
|
// Note: funkiness to ensure we get a space only between quals.
|
|
|
|
bool NonePrinted = true;
|
2007-04-06 06:36:20 +08:00
|
|
|
if (TypeQuals & QualType::Const)
|
2006-11-13 14:22:30 +08:00
|
|
|
S += "const", NonePrinted = false;
|
2007-04-06 06:36:20 +08:00
|
|
|
if (TypeQuals & QualType::Volatile)
|
2006-11-13 14:22:30 +08:00
|
|
|
S += (NonePrinted+" volatile"), NonePrinted = false;
|
2007-04-06 06:36:20 +08:00
|
|
|
if (TypeQuals & QualType::Restrict)
|
2006-11-13 14:22:30 +08:00
|
|
|
S += (NonePrinted+" restrict"), NonePrinted = false;
|
|
|
|
}
|
2006-11-12 16:50:50 +08:00
|
|
|
|
2007-04-06 06:36:20 +08:00
|
|
|
void QualType::getAsString(std::string &S) const {
|
2006-11-12 07:56:48 +08:00
|
|
|
if (isNull()) {
|
2006-11-13 14:22:30 +08:00
|
|
|
S += "NULL TYPE\n";
|
2006-11-12 07:56:48 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print qualifiers as appropriate.
|
2006-11-12 16:50:50 +08:00
|
|
|
if (unsigned TQ = getQualifiers()) {
|
2006-11-13 15:38:09 +08:00
|
|
|
std::string TQS;
|
|
|
|
AppendTypeQualList(TQS, TQ);
|
|
|
|
S = TQS + ' ' + S;
|
2006-11-12 16:50:50 +08:00
|
|
|
}
|
2006-11-13 15:38:09 +08:00
|
|
|
|
|
|
|
getTypePtr()->getAsString(S);
|
2006-11-12 08:37:36 +08:00
|
|
|
}
|
|
|
|
|
2006-11-13 15:38:09 +08:00
|
|
|
void BuiltinType::getAsString(std::string &S) const {
|
|
|
|
if (S.empty()) {
|
2006-12-03 10:57:32 +08:00
|
|
|
S = getName();
|
2006-11-13 15:38:09 +08:00
|
|
|
} else {
|
|
|
|
// Prefix the basic type, e.g. 'int X'.
|
|
|
|
S = ' ' + S;
|
2006-12-03 10:57:32 +08:00
|
|
|
S = getName() + S;
|
2006-11-13 15:38:09 +08:00
|
|
|
}
|
2006-11-12 08:37:36 +08:00
|
|
|
}
|
|
|
|
|
2006-11-13 15:38:09 +08:00
|
|
|
void PointerType::getAsString(std::string &S) const {
|
|
|
|
S = '*' + S;
|
|
|
|
|
|
|
|
// Handle things like 'int (*A)[4];' correctly.
|
2006-11-19 09:31:06 +08:00
|
|
|
// FIXME: this should include vectors, but vectors use attributes I guess.
|
2006-11-13 15:38:09 +08:00
|
|
|
if (isa<ArrayType>(PointeeType.getTypePtr()))
|
|
|
|
S = '(' + S + ')';
|
|
|
|
|
|
|
|
PointeeType.getAsString(S);
|
2006-11-12 07:56:48 +08:00
|
|
|
}
|
2006-11-12 16:50:50 +08:00
|
|
|
|
2006-11-13 15:38:09 +08:00
|
|
|
void ArrayType::getAsString(std::string &S) const {
|
2006-11-13 14:22:30 +08:00
|
|
|
S += '[';
|
2006-11-12 16:50:50 +08:00
|
|
|
|
|
|
|
if (IndexTypeQuals) {
|
2006-11-13 14:22:30 +08:00
|
|
|
AppendTypeQualList(S, IndexTypeQuals);
|
|
|
|
S += ' ';
|
2006-11-12 16:50:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (SizeModifier == Static)
|
2006-11-13 14:22:30 +08:00
|
|
|
S += "static";
|
2006-11-12 16:50:50 +08:00
|
|
|
else if (SizeModifier == Star)
|
2006-11-13 14:22:30 +08:00
|
|
|
S += '*';
|
2006-11-12 16:50:50 +08:00
|
|
|
|
2006-11-13 14:22:30 +08:00
|
|
|
S += ']';
|
2006-11-13 15:38:09 +08:00
|
|
|
|
|
|
|
ElementType.getAsString(S);
|
2006-11-12 16:50:50 +08:00
|
|
|
}
|
2006-11-20 12:02:15 +08:00
|
|
|
|
2006-12-02 15:52:18 +08:00
|
|
|
void FunctionTypeNoProto::getAsString(std::string &S) const {
|
|
|
|
// If needed for precedence reasons, wrap the inner part in grouping parens.
|
|
|
|
if (!S.empty())
|
|
|
|
S = "(" + S + ")";
|
|
|
|
|
|
|
|
S += "()";
|
|
|
|
getResultType().getAsString(S);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionTypeProto::getAsString(std::string &S) const {
|
|
|
|
// If needed for precedence reasons, wrap the inner part in grouping parens.
|
|
|
|
if (!S.empty())
|
|
|
|
S = "(" + S + ")";
|
|
|
|
|
|
|
|
S += "(";
|
|
|
|
std::string Tmp;
|
|
|
|
for (unsigned i = 0, e = getNumArgs(); i != e; ++i) {
|
|
|
|
if (i) S += ", ";
|
|
|
|
getArgType(i).getAsString(Tmp);
|
|
|
|
S += Tmp;
|
|
|
|
Tmp.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isVariadic()) {
|
|
|
|
if (getNumArgs())
|
|
|
|
S += ", ";
|
|
|
|
S += "...";
|
2006-12-03 10:03:33 +08:00
|
|
|
} else if (getNumArgs() == 0) {
|
|
|
|
// Do not emit int() if we have a proto, emit 'int(void)'.
|
|
|
|
S += "void";
|
2006-12-02 15:52:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
S += ")";
|
|
|
|
getResultType().getAsString(S);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-01-26 10:01:53 +08:00
|
|
|
void TypedefType::getAsString(std::string &InnerString) const {
|
2007-01-23 13:45:31 +08:00
|
|
|
if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
|
2006-11-20 12:17:27 +08:00
|
|
|
InnerString = ' ' + InnerString;
|
2007-01-23 13:45:31 +08:00
|
|
|
InnerString = getDecl()->getIdentifier()->getName() + InnerString;
|
|
|
|
}
|
|
|
|
|
2007-03-24 06:27:02 +08:00
|
|
|
void TagType::getAsString(std::string &InnerString) const {
|
2007-01-23 13:45:31 +08:00
|
|
|
if (!InnerString.empty()) // Prefix the basic type, e.g. 'typedefname X'.
|
|
|
|
InnerString = ' ' + InnerString;
|
|
|
|
|
|
|
|
const char *Kind = getDecl()->getKindName();
|
|
|
|
const char *ID;
|
|
|
|
if (const IdentifierInfo *II = getDecl()->getIdentifier())
|
|
|
|
ID = II->getName();
|
|
|
|
else
|
|
|
|
ID = "<anonymous>";
|
|
|
|
|
|
|
|
InnerString = std::string(Kind) + " " + ID + InnerString;
|
2006-11-20 12:02:15 +08:00
|
|
|
}
|