Commit 38da15ea authored by Tobias Tebbi's avatar Tobias Tebbi Committed by Commit Bot

[torque] refactor types to not be declarables

We already had to introduce TypeAlias to allow types to be const.
With TypeAlias, there is no need for types to be declarable themselves.

Change-Id: Ia718482f6c121b5316aca819368e6d048283e5e8
Reviewed-on: https://chromium-review.googlesource.com/1068734
Commit-Queue: Tobias Tebbi <tebbi@chromium.org>
Reviewed-by: 's avatarSigurd Schneider <sigurds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53282}
parent 74c341be
......@@ -25,8 +25,6 @@ class Declarable {
public:
virtual ~Declarable() {}
enum Kind {
kAbstractType,
kFunctionPointerType,
kVariable,
kParameter,
kMacro,
......@@ -39,9 +37,6 @@ class Declarable {
kConstant
};
Kind kind() const { return kind_; }
bool IsAbstractType() const { return kind() == kAbstractType; }
bool IsFunctionPointerType() const { return kind() == kFunctionPointerType; }
bool IsType() const { return IsAbstractType() || IsFunctionPointerType(); }
bool IsMacro() const { return kind() == kMacro; }
bool IsBuiltin() const { return kind() == kBuiltin; }
bool IsRuntimeFunction() const { return kind() == kRuntimeFunction; }
......@@ -59,7 +54,7 @@ class Declarable {
explicit Declarable(Kind kind) : kind_(kind) {}
private:
Kind kind_;
const Kind kind_;
};
#define DECLARE_DECLARABLE_BOILERPLATE(x, y) \
......@@ -83,105 +78,6 @@ class Declarable {
return static_cast<const x*>(declarable); \
}
class Type : public Declarable {
public:
DECLARE_DECLARABLE_BOILERPLATE(Type, type);
bool IsSubtypeOf(const Type* supertype) const;
virtual std::string ToString() const = 0;
virtual std::string MangledName() const = 0;
bool IsVoid() const { return IsAbstractName(VOID_TYPE_STRING); }
bool IsNever() const { return IsAbstractName(NEVER_TYPE_STRING); }
bool IsBool() const { return IsAbstractName(BOOL_TYPE_STRING); }
bool IsConstexprBool() const {
return IsAbstractName(CONSTEXPR_BOOL_TYPE_STRING);
}
bool IsVoidOrNever() const { return IsVoid() || IsNever(); }
virtual const std::string& GetGeneratedTypeName() const = 0;
virtual std::string GetGeneratedTNodeTypeName() const = 0;
virtual bool IsConstexpr() const = 0;
protected:
Type(Declarable::Kind kind, const Type* parent)
: Declarable(kind), parent_(parent) {}
const Type* parent() const { return parent_; }
private:
bool IsAbstractName(const std::string& name) const;
const Type* const parent_;
};
class AbstractType : public Type {
public:
DECLARE_DECLARABLE_BOILERPLATE(AbstractType, abstract_type);
AbstractType(const Type* parent, const std::string& name,
const std::string& generated_type)
: Type(Declarable::kAbstractType, parent),
name_(name),
generated_type_(generated_type) {}
const std::string& name() const { return name_; }
std::string ToString() const override { return name(); }
std::string MangledName() const override { return "AT" + name(); }
const std::string& GetGeneratedTypeName() const override {
return generated_type_;
}
std::string GetGeneratedTNodeTypeName() const override;
bool IsConstexpr() const override {
return name().substr(0, strlen(CONSTEXPR_TYPE_PREFIX)) ==
CONSTEXPR_TYPE_PREFIX;
}
private:
const std::string name_;
const std::string generated_type_;
};
// For now, function pointers are restricted to Code objects of Torque-defined
// builtins.
class FunctionPointerType : public Type {
public:
DECLARE_DECLARABLE_BOILERPLATE(FunctionPointerType, function_pointer_type);
FunctionPointerType(const Type* parent, TypeVector parameter_types,
const Type* return_type)
: Type(Declarable::kFunctionPointerType, parent),
parameter_types_(parameter_types),
return_type_(return_type) {}
std::string ToString() const override;
std::string MangledName() const override;
const std::string& GetGeneratedTypeName() const override {
return parent()->GetGeneratedTypeName();
}
std::string GetGeneratedTNodeTypeName() const override {
return parent()->GetGeneratedTNodeTypeName();
}
bool IsConstexpr() const override { return parent()->IsConstexpr(); }
const TypeVector& parameter_types() const { return parameter_types_; }
const Type* return_type() const { return return_type_; }
friend size_t hash_value(const FunctionPointerType& p) {
size_t result = base::hash_value(p.return_type_);
for (const Type* parameter : p.parameter_types_) {
result = base::hash_combine(result, parameter);
}
return result;
}
bool operator==(const FunctionPointerType& other) const {
return parameter_types_ == other.parameter_types_ &&
return_type_ == other.return_type_;
}
private:
const TypeVector parameter_types_;
const Type* const return_type_;
};
inline std::ostream& operator<<(std::ostream& os, const Type* t) {
os << t->ToString();
return os;
}
class Value : public Declarable {
public:
const std::string& name() const { return name_; }
......@@ -408,13 +304,13 @@ typedef std::pair<Generic*, TypeVector> SpecializationKey;
class TypeAlias : public Declarable {
public:
DECLARE_DECLARABLE_BOILERPLATE(TypeAlias, instantiated_type);
DECLARE_DECLARABLE_BOILERPLATE(TypeAlias, type_alias);
const Type* type() const { return type_; }
private:
friend class Declarations;
TypeAlias(const std::string& name, const Type* type)
explicit TypeAlias(const Type* type)
: Declarable(Declarable::kTypeAlias), type_(type) {}
const Type* type_;
......
......@@ -324,7 +324,7 @@ void DeclarationVisitor::DeclareSpecializedTypes(const SpecializationKey& key) {
for (auto type : key.second) {
std::string generic_type_name =
generic->declaration()->generic_parameters[i++];
declarations()->DeclareTypeAlias(generic_type_name, type);
declarations()->DeclareType(generic_type_name, type);
}
}
......
......@@ -44,9 +44,7 @@ void Declarations::CheckAlreadyDeclared(const std::string& name,
const Type* Declarations::LookupType(const std::string& name) {
Declarable* raw = Lookup(name);
if (raw->IsType()) {
return Type::cast(raw);
} else if (raw->IsTypeAlias()) {
if (raw->IsTypeAlias()) {
return TypeAlias::cast(raw)->type();
}
std::stringstream s;
......@@ -57,12 +55,21 @@ const Type* Declarations::LookupType(const std::string& name) {
const Type* Declarations::LookupGlobalType(const std::string& name) {
Declarable* raw = LookupGlobalScope(name);
if (!raw->IsType()) {
if (!raw->IsTypeAlias()) {
std::stringstream s;
s << "declaration \"" << name << "\" is not a Type";
ReportError(s.str());
}
return Type::cast(raw);
return TypeAlias::cast(raw)->type();
}
const AbstractType* Declarations::GetAbstractType(const Type* parent,
std::string name,
std::string generated) {
AbstractType* result =
new AbstractType(parent, std::move(name), std::move(generated));
nominal_types_.push_back(std::unique_ptr<AbstractType>(result));
return result;
}
const Type* Declarations::GetFunctionPointerType(TypeVector argument_types,
......@@ -179,23 +186,22 @@ const AbstractType* Declarations::DeclareAbstractType(
s << "cannot find parent type \"" << *parent << "\"";
ReportError(s.str());
}
if (!maybe_parent_type->IsType()) {
if (!maybe_parent_type->IsTypeAlias()) {
std::stringstream s;
s << "parent \"" << *parent << "\" of type \"" << name << "\""
<< " is not a type";
ReportError(s.str());
}
parent_type = Type::cast(maybe_parent_type);
parent_type = TypeAlias::cast(maybe_parent_type)->type();
}
AbstractType* result = new AbstractType(parent_type, name, generated);
Declare(name, std::unique_ptr<Declarable>(result));
return result;
const AbstractType* type = GetAbstractType(parent_type, name, generated);
DeclareType(name, type);
return type;
}
void Declarations::DeclareTypeAlias(const std::string& name,
const Type* aliased_type) {
CheckAlreadyDeclared(name, "aliased type");
TypeAlias* result = new TypeAlias(name, aliased_type);
void Declarations::DeclareType(const std::string& name, const Type* type) {
CheckAlreadyDeclared(name, "type");
TypeAlias* result = new TypeAlias(type);
Declare(name, std::unique_ptr<TypeAlias>(result));
}
......
......@@ -47,6 +47,8 @@ class Declarations {
const Type* LookupGlobalType(const std::string& name);
const Type* GetType(TypeExpression* type_expression);
const AbstractType* GetAbstractType(const Type* parent, std::string name,
std::string generated);
const Type* GetFunctionPointerType(TypeVector argument_types,
const Type* return_type);
......@@ -66,7 +68,7 @@ class Declarations {
const std::string& generated,
const std::string* parent = nullptr);
void DeclareTypeAlias(const std::string& name, const Type* aliased_type);
void DeclareType(const std::string& name, const Type* type);
Label* DeclareLabel(const std::string& name);
......@@ -134,6 +136,7 @@ class Declarations {
Statement* next_body_;
std::vector<std::unique_ptr<Declarable>> declarables_;
Deduplicator<FunctionPointerType> function_pointer_types_;
std::vector<std::unique_ptr<Type>> nominal_types_;
std::map<std::pair<const AstNode*, TypeVector>, Scope*> scopes_;
std::map<Generic*, ScopeChain::Snapshot> generic_declaration_scopes_;
};
......
......@@ -30,10 +30,148 @@ static const char* const CONST_INT32_TYPE_STRING = "constexpr int32";
static const char* const CONST_FLOAT64_TYPE_STRING = "constexpr float64";
class Label;
class Type;
class TypeBase {
public:
enum class Kind { kAbstractType, kFunctionPointerType };
virtual ~TypeBase() {}
bool IsAbstractType() const { return kind() == Kind::kAbstractType; }
bool IsFunctionPointerType() const {
return kind() == Kind::kFunctionPointerType;
}
protected:
explicit TypeBase(Kind kind) : kind_(kind) {}
Kind kind() const { return kind_; }
private:
const Kind kind_;
};
#define DECLARE_TYPE_BOILERPLATE(x) \
static x* cast(TypeBase* declarable) { \
DCHECK(declarable->Is##x()); \
return static_cast<x*>(declarable); \
} \
static const x* cast(const TypeBase* declarable) { \
DCHECK(declarable->Is##x()); \
return static_cast<const x*>(declarable); \
} \
static x* DynamicCast(TypeBase* declarable) { \
if (!declarable) return nullptr; \
if (!declarable->Is##x()) return nullptr; \
return static_cast<x*>(declarable); \
} \
static const x* DynamicCast(const TypeBase* declarable) { \
if (!declarable) return nullptr; \
if (!declarable->Is##x()) return nullptr; \
return static_cast<const x*>(declarable); \
}
class Type : public TypeBase {
public:
bool IsSubtypeOf(const Type* supertype) const;
virtual std::string ToString() const = 0;
virtual std::string MangledName() const = 0;
bool IsVoid() const { return IsAbstractName(VOID_TYPE_STRING); }
bool IsNever() const { return IsAbstractName(NEVER_TYPE_STRING); }
bool IsBool() const { return IsAbstractName(BOOL_TYPE_STRING); }
bool IsConstexprBool() const {
return IsAbstractName(CONSTEXPR_BOOL_TYPE_STRING);
}
bool IsVoidOrNever() const { return IsVoid() || IsNever(); }
virtual const std::string& GetGeneratedTypeName() const = 0;
virtual std::string GetGeneratedTNodeTypeName() const = 0;
virtual bool IsConstexpr() const = 0;
protected:
Type(TypeBase::Kind kind, const Type* parent)
: TypeBase(kind), parent_(parent) {}
const Type* parent() const { return parent_; }
private:
bool IsAbstractName(const std::string& name) const;
// If {parent_} is not nullptr, then this type is a subtype of {parent_}.
const Type* const parent_;
};
using TypeVector = std::vector<const Type*>;
class AbstractType final : public Type {
public:
DECLARE_TYPE_BOILERPLATE(AbstractType);
const std::string& name() const { return name_; }
std::string ToString() const override { return name(); }
std::string MangledName() const override { return "AT" + name(); }
const std::string& GetGeneratedTypeName() const override {
return generated_type_;
}
std::string GetGeneratedTNodeTypeName() const override;
bool IsConstexpr() const override {
return name().substr(0, strlen(CONSTEXPR_TYPE_PREFIX)) ==
CONSTEXPR_TYPE_PREFIX;
}
private:
friend class Declarations;
AbstractType(const Type* parent, const std::string& name,
const std::string& generated_type)
: Type(Kind::kAbstractType, parent),
name_(name),
generated_type_(generated_type) {}
const std::string name_;
const std::string generated_type_;
};
// For now, function pointers are restricted to Code objects of Torque-defined
// builtins.
class FunctionPointerType final : public Type {
public:
DECLARE_TYPE_BOILERPLATE(FunctionPointerType);
std::string ToString() const override;
std::string MangledName() const override;
const std::string& GetGeneratedTypeName() const override {
return parent()->GetGeneratedTypeName();
}
std::string GetGeneratedTNodeTypeName() const override {
return parent()->GetGeneratedTNodeTypeName();
}
bool IsConstexpr() const override { return parent()->IsConstexpr(); }
const TypeVector& parameter_types() const { return parameter_types_; }
const Type* return_type() const { return return_type_; }
friend size_t hash_value(const FunctionPointerType& p) {
size_t result = base::hash_value(p.return_type_);
for (const Type* parameter : p.parameter_types_) {
result = base::hash_combine(result, parameter);
}
return result;
}
bool operator==(const FunctionPointerType& other) const {
return parameter_types_ == other.parameter_types_ &&
return_type_ == other.return_type_;
}
private:
friend class Declarations;
FunctionPointerType(const Type* parent, TypeVector parameter_types,
const Type* return_type)
: Type(Kind::kFunctionPointerType, parent),
parameter_types_(parameter_types),
return_type_(return_type) {}
const TypeVector parameter_types_;
const Type* const return_type_;
};
inline std::ostream& operator<<(std::ostream& os, const Type* t) {
os << t->ToString();
return os;
}
class VisitResult {
public:
VisitResult() {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment