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-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-22 05:08:52 +08:00
|
|
|
bool Type::isFunctionType() const {
|
|
|
|
return isa<FunctionType>(CanonicalType) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isPointerType() const {
|
|
|
|
return isa<PointerType>(CanonicalType) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isArrayType() const {
|
|
|
|
return isa<ArrayType>(CanonicalType) ? true : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isIntegralType() const {
|
|
|
|
switch (CanonicalType->getTypeClass()) {
|
|
|
|
default: return false;
|
|
|
|
case Builtin:
|
|
|
|
const BuiltinType *BT = static_cast<BuiltinType*>(CanonicalType);
|
|
|
|
return BT->getKind() >= BuiltinType::Bool &&
|
|
|
|
BT->getKind() <= BuiltinType::ULongLong;
|
|
|
|
case Tagged:
|
2007-03-24 06:27:02 +08:00
|
|
|
const TagType *TT = static_cast<TagType*>(CanonicalType);
|
2007-03-22 05:08:52 +08:00
|
|
|
if (TT->getDecl()->getKind() == Decl::Enum)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isFloatingType() const {
|
|
|
|
switch (CanonicalType->getTypeClass()) {
|
|
|
|
default: return false;
|
|
|
|
case Builtin:
|
|
|
|
const BuiltinType *BT = static_cast<BuiltinType*>(CanonicalType);
|
|
|
|
return BT->getKind() >= BuiltinType::Float &&
|
|
|
|
BT->getKind() <= BuiltinType::LongDoubleComplex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isArithmeticType() const {
|
|
|
|
switch (CanonicalType->getTypeClass()) {
|
|
|
|
default: return false;
|
|
|
|
case Builtin:
|
|
|
|
const BuiltinType *BT = static_cast<BuiltinType*>(CanonicalType);
|
|
|
|
return BT->getKind() >= BuiltinType::Bool &&
|
|
|
|
BT->getKind() <= BuiltinType::LongDoubleComplex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isScalarType() const {
|
|
|
|
switch (CanonicalType->getTypeClass()) {
|
|
|
|
default: return false;
|
|
|
|
case Builtin:
|
|
|
|
const BuiltinType *BT = static_cast<BuiltinType*>(CanonicalType);
|
|
|
|
return BT->getKind() >= BuiltinType::Bool &&
|
|
|
|
BT->getKind() <= BuiltinType::LongDoubleComplex;
|
|
|
|
case Pointer:
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Type::isAggregateType() const {
|
|
|
|
switch (CanonicalType->getTypeClass()) {
|
|
|
|
default: return false;
|
|
|
|
case Array:
|
|
|
|
return true;
|
|
|
|
case Tagged:
|
2007-03-24 06:27:02 +08:00
|
|
|
const TagType *TT = static_cast<TagType*>(CanonicalType);
|
2007-03-22 05:08:52 +08:00
|
|
|
if (TT->getDecl()->getKind() == Decl::Struct)
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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-01-27 09:15:32 +08:00
|
|
|
void FunctionTypeProto::Profile(FoldingSetNodeID &ID, TypeRef Result,
|
|
|
|
TypeRef* ArgTys,
|
|
|
|
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
|
|
|
|
2006-11-12 07:56:48 +08:00
|
|
|
void TypeRef::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;
|
|
|
|
if (TypeQuals & TypeRef::Const)
|
2006-11-13 14:22:30 +08:00
|
|
|
S += "const", NonePrinted = false;
|
2006-11-12 16:50:50 +08:00
|
|
|
if (TypeQuals & TypeRef::Volatile)
|
2006-11-13 14:22:30 +08:00
|
|
|
S += (NonePrinted+" volatile"), NonePrinted = false;
|
2006-11-12 16:50:50 +08:00
|
|
|
if (TypeQuals & TypeRef::Restrict)
|
2006-11-13 14:22:30 +08:00
|
|
|
S += (NonePrinted+" restrict"), NonePrinted = false;
|
|
|
|
}
|
2006-11-12 16:50:50 +08:00
|
|
|
|
2006-11-13 15:38:09 +08:00
|
|
|
void TypeRef::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
|
|
|
}
|