aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2021-01-21 17:04:14 +0000
committerPhilip Herron <herron.philip@googlemail.com>2021-01-26 11:59:39 +0000
commit5294dfe7dce7a58fbaf2131c7589d115008ebf0e (patch)
tree04ea7976bb99c19de924eab28310a913434d5d70 /gcc
parentbe371b9f49e0c4fd7752cad6708fed8b1f2719a3 (diff)
downloadgcc-5294dfe7dce7a58fbaf2131c7589d115008ebf0e.zip
gcc-5294dfe7dce7a58fbaf2131c7589d115008ebf0e.tar.gz
gcc-5294dfe7dce7a58fbaf2131c7589d115008ebf0e.tar.bz2
Remove TyTy::ParamType this was wrongly used to represent FunctionParams
TyTy::ParamType is meant to be used to represent Generic Types which is not handled yet as part of this current milestone.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/backend/rust-compile-context.h56
-rw-r--r--gcc/rust/backend/rust-compile-item.h39
-rw-r--r--gcc/rust/backend/rust-compile-tyty.h93
-rw-r--r--gcc/rust/hir/tree/rust-hir-item.h2
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-item.h1
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-toplevel.h11
-rw-r--r--gcc/rust/typecheck/rust-tyty-call.h1
-rw-r--r--gcc/rust/typecheck/rust-tyty-resolver.h1
-rw-r--r--gcc/rust/typecheck/rust-tyty-rules.h22
-rw-r--r--gcc/rust/typecheck/rust-tyty-visitor.h1
-rw-r--r--gcc/rust/typecheck/rust-tyty.cc38
-rw-r--r--gcc/rust/typecheck/rust-tyty.h50
12 files changed, 72 insertions, 243 deletions
diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h
index 6516c71..df29e36 100644
--- a/gcc/rust/backend/rust-compile-context.h
+++ b/gcc/rust/backend/rust-compile-context.h
@@ -26,6 +26,7 @@
#include "rust-backend.h"
#include "rust-compile-tyty.h"
#include "rust-ast-full.h"
+#include "rust-hir-full.h"
namespace Rust {
namespace Compile {
@@ -227,8 +228,6 @@ public:
void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); }
- void visit (TyTy::ParamType &type) override { gcc_unreachable (); }
-
void visit (TyTy::FnType &type) override
{
Backend::Btyped_identifier receiver;
@@ -244,14 +243,14 @@ public:
ctx->get_mappings ()->lookup_location (hir_type->get_ref ())));
}
- for (size_t i = 0; i < type.num_params (); i++)
+ for (auto &param_pair : type.get_params ())
{
- auto param_tyty = type.param_at (i);
+ auto param_tyty = param_pair.second;
auto compiled_param_type
- = TyTyResolveCompile::compile (ctx, param_tyty->get_base_type ());
+ = TyTyResolveCompile::compile (ctx, param_tyty);
auto compiled_param = Backend::Btyped_identifier (
- param_tyty->get_identifier (), compiled_param_type,
+ param_pair.first->as_string (), compiled_param_type,
ctx->get_mappings ()->lookup_location (param_tyty->get_ref ()));
parameters.push_back (compiled_param);
@@ -351,51 +350,6 @@ private:
::Btype *translated;
};
-class TyTyCompileParam : public TyTy::TyVisitor
-{
-public:
- static ::Bvariable *compile (Context *ctx, Bfunction *fndecl,
- TyTy::TyBase *ty)
- {
- TyTyCompileParam compiler (ctx, fndecl);
- ty->accept_vis (compiler);
- rust_assert (compiler.translated != nullptr);
- return compiler.translated;
- }
-
- ~TyTyCompileParam () {}
-
- void visit (TyTy::UnitType &type) override { gcc_unreachable (); }
- void visit (TyTy::InferType &type) override { gcc_unreachable (); }
- void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); }
- void visit (TyTy::ADTType &type) override { gcc_unreachable (); }
- void visit (TyTy::FnType &type) override { gcc_unreachable (); }
- void visit (TyTy::ArrayType &type) override { gcc_unreachable (); }
- void visit (TyTy::BoolType &type) override { gcc_unreachable (); }
- void visit (TyTy::IntType &type) override { gcc_unreachable (); }
- void visit (TyTy::UintType &type) override { gcc_unreachable (); }
- void visit (TyTy::FloatType &type) override { gcc_unreachable (); }
- void visit (TyTy::ErrorType &type) override { gcc_unreachable (); }
-
- void visit (TyTy::ParamType &type) override
- {
- auto btype = TyTyResolveCompile::compile (ctx, type.get_base_type ());
- bool tree_addressable = false;
- translated = ctx->get_backend ()->parameter_variable (
- fndecl, type.get_identifier (), btype, tree_addressable,
- ctx->get_mappings ()->lookup_location (type.get_ref ()));
- }
-
-private:
- TyTyCompileParam (Context *ctx, ::Bfunction *fndecl)
- : ctx (ctx), fndecl (fndecl), translated (nullptr)
- {}
-
- Context *ctx;
- ::Bfunction *fndecl;
- ::Bvariable *translated;
-};
-
} // namespace Compile
} // namespace Rust
diff --git a/gcc/rust/backend/rust-compile-item.h b/gcc/rust/backend/rust-compile-item.h
index 82dbc9b..bf899d8 100644
--- a/gcc/rust/backend/rust-compile-item.h
+++ b/gcc/rust/backend/rust-compile-item.h
@@ -127,14 +127,21 @@ public:
return;
}
- TyTy::TyBase *fntype;
+ TyTy::TyBase *fntype_tyty;
if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (),
- &fntype))
+ &fntype_tyty))
{
rust_fatal_error (function.locus, "failed to lookup function type");
return;
}
+ if (fntype_tyty->get_kind () != TyTy::TypeKind::FNDEF)
+ {
+ rust_error_at (function.get_locus (), "invalid TyTy for function item");
+ return;
+ }
+
+ TyTy::FnType *fntype = (TyTy::FnType *) fntype_tyty;
// convert to the actual function type
::Btype *compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
@@ -159,17 +166,31 @@ public:
ctx->insert_function_decl (function.get_mappings ().get_hirid (), fndecl);
// setup the params
- TyTy::TyBase *tyret = TyTyExtractRetFromFnType::compile (fntype);
- std::vector<TyTy::ParamType *> typarams
- = TyTyExtractParamsFromFnType::compile (fntype);
+
+ TyTy::TyBase *tyret = fntype->return_type ();
std::vector<Bvariable *> param_vars;
- for (auto &it : typarams)
+ size_t i = 0;
+ for (auto &it : fntype->get_params ())
{
- auto compiled_param = TyTyCompileParam::compile (ctx, fndecl, it);
- param_vars.push_back (compiled_param);
+ HIR::FunctionParam &referenced_param = function.function_params.at (i);
+ auto param_pattern = it.first;
+ auto param_tyty = it.second;
+
+ auto compiled_param_type
+ = TyTyResolveCompile::compile (ctx, param_tyty);
+
+ bool tree_addressable = false;
+ auto compiled_param_var = ctx->get_backend ()->parameter_variable (
+ fndecl, param_pattern->as_string (), compiled_param_type,
+ tree_addressable,
+ ctx->get_mappings ()->lookup_location (param_tyty->get_ref ()));
+
+ param_vars.push_back (compiled_param_var);
- ctx->insert_var_decl (it->get_ref (), compiled_param);
+ ctx->insert_var_decl (referenced_param.get_mappings ().get_hirid (),
+ compiled_param_var);
+ i++;
}
if (!ctx->get_backend ()->function_set_parameters (fndecl, param_vars))
diff --git a/gcc/rust/backend/rust-compile-tyty.h b/gcc/rust/backend/rust-compile-tyty.h
index 3eb2ca5..f74b4ac 100644
--- a/gcc/rust/backend/rust-compile-tyty.h
+++ b/gcc/rust/backend/rust-compile-tyty.h
@@ -26,6 +26,7 @@
#include "rust-tyty.h"
#include "rust-tyty-visitor.h"
#include "rust-hir-map.h"
+#include "rust-hir-full.h"
namespace Rust {
namespace Compile {
@@ -49,8 +50,6 @@ public:
void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); }
- void visit (TyTy::ParamType &type) override { gcc_unreachable (); }
-
void visit (TyTy::ADTType &type) override { gcc_unreachable (); }
void visit (TyTy::ArrayType &type) override { gcc_unreachable (); }
@@ -74,13 +73,14 @@ public:
"_", ret, mappings->lookup_location (hir_type->get_ref ())));
}
- for (size_t i = 0; i < type.num_params (); i++)
+ for (auto &params : type.get_params ())
{
- auto param_tyty = type.param_at (i);
- auto compiled_param_type
- = TyTyCompile::compile (backend, param_tyty->get_base_type ());
+ auto param_pattern = params.first;
+ auto param_tyty = params.second;
+ auto compiled_param_type = TyTyCompile::compile (backend, param_tyty);
+
auto compiled_param = Backend::Btyped_identifier (
- param_tyty->get_identifier (), compiled_param_type,
+ param_pattern->as_string (), compiled_param_type,
mappings->lookup_location (param_tyty->get_ref ()));
parameters.push_back (compiled_param);
@@ -199,85 +199,6 @@ private:
Analysis::Mappings *mappings;
};
-class TyTyExtractParamsFromFnType : public TyTy::TyVisitor
-{
-public:
- static std::vector<TyTy::ParamType *> compile (TyTy::TyBase *ty)
- {
- TyTyExtractParamsFromFnType compiler;
- ty->accept_vis (compiler);
- rust_assert (compiler.ok);
- return compiler.translated;
- }
-
- ~TyTyExtractParamsFromFnType () {}
-
- void visit (TyTy::UnitType &type) override { gcc_unreachable (); }
- void visit (TyTy::InferType &type) override { gcc_unreachable (); }
- void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); }
- void visit (TyTy::ADTType &type) override { gcc_unreachable (); }
- void visit (TyTy::ParamType &type) override { gcc_unreachable (); }
- void visit (TyTy::ArrayType &type) override { gcc_unreachable (); }
- void visit (TyTy::BoolType &type) override { gcc_unreachable (); }
- void visit (TyTy::IntType &type) override { gcc_unreachable (); }
- void visit (TyTy::UintType &type) override { gcc_unreachable (); }
- void visit (TyTy::FloatType &type) override { gcc_unreachable (); }
- void visit (TyTy::ErrorType &type) override { gcc_unreachable (); }
-
- void visit (TyTy::FnType &type) override
- {
- ok = true;
- for (size_t i = 0; i < type.num_params (); i++)
- {
- translated.push_back (type.param_at (i));
- }
- }
-
-private:
- TyTyExtractParamsFromFnType () : ok (false) {}
-
- bool ok;
- std::vector<TyTy::ParamType *> translated;
-};
-
-class TyTyExtractRetFromFnType : public TyTy::TyVisitor
-{
-public:
- static TyTy::TyBase *compile (TyTy::TyBase *ty)
- {
- TyTyExtractRetFromFnType compiler;
- ty->accept_vis (compiler);
- rust_assert (compiler.ok);
- return compiler.translated;
- }
-
- ~TyTyExtractRetFromFnType () {}
-
- void visit (TyTy::UnitType &type) override { gcc_unreachable (); }
- void visit (TyTy::InferType &type) override { gcc_unreachable (); }
- void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); }
- void visit (TyTy::ADTType &type) override { gcc_unreachable (); }
- void visit (TyTy::ParamType &type) override { gcc_unreachable (); }
- void visit (TyTy::ArrayType &type) override { gcc_unreachable (); }
- void visit (TyTy::BoolType &type) override { gcc_unreachable (); }
- void visit (TyTy::IntType &type) override { gcc_unreachable (); }
- void visit (TyTy::UintType &type) override { gcc_unreachable (); }
- void visit (TyTy::FloatType &type) override { gcc_unreachable (); }
- void visit (TyTy::ErrorType &type) override { gcc_unreachable (); }
-
- void visit (TyTy::FnType &type) override
- {
- ok = true;
- translated = type.get_return_type ();
- }
-
-private:
- TyTyExtractRetFromFnType () : ok (false), translated (nullptr) {}
-
- bool ok;
- TyTy::TyBase *translated;
-};
-
} // namespace Compile
} // namespace Rust
diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h
index 00608bd..e99dd38 100644
--- a/gcc/rust/hir/tree/rust-hir-item.h
+++ b/gcc/rust/hir/tree/rust-hir-item.h
@@ -404,13 +404,13 @@ public:
// A function parameter
struct FunctionParam
{
-public:
std::unique_ptr<Pattern> param_name;
std::unique_ptr<Type> type;
Location locus;
Analysis::NodeMapping mappings;
+public:
FunctionParam (Analysis::NodeMapping mappings,
std::unique_ptr<Pattern> param_name,
std::unique_ptr<Type> param_type, Location locus)
diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.h b/gcc/rust/typecheck/rust-hir-type-check-item.h
index 20f5adb..566d43e 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-item.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-item.h
@@ -47,7 +47,6 @@ public:
void visit (TyTy::InferType &type) override { gcc_unreachable (); }
void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); }
void visit (TyTy::ADTType &type) override { gcc_unreachable (); }
- void visit (TyTy::ParamType &type) override { gcc_unreachable (); }
void visit (TyTy::ArrayType &type) override { gcc_unreachable (); }
void visit (TyTy::BoolType &type) override { gcc_unreachable (); }
void visit (TyTy::IntType &type) override { gcc_unreachable (); }
diff --git a/gcc/rust/typecheck/rust-hir-type-check-toplevel.h b/gcc/rust/typecheck/rust-hir-type-check-toplevel.h
index e2bc3ed..fcaf724 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-toplevel.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-toplevel.h
@@ -113,15 +113,14 @@ public:
ret_type->set_ref (function.return_type->get_mappings ().get_hirid ());
}
- std::vector<TyTy::ParamType *> params;
+ std::vector<std::pair<HIR::Pattern *, TyTy::TyBase *> > params;
for (auto &param : function.function_params)
{
// get the name as well required for later on
- auto param_type = TypeCheckType::Resolve (param.type.get ());
- auto param_tyty
- = new TyTy::ParamType (param.get_mappings ().get_hirid (),
- param.param_name->as_string (), param_type);
- params.push_back (param_tyty);
+ auto param_tyty = TypeCheckType::Resolve (param.get_type ());
+ params.push_back (
+ std::pair<HIR::Pattern *, TyTy::TyBase *> (param.get_param_name (),
+ param_tyty));
context->insert_type (param.get_mappings (), param_tyty);
}
diff --git a/gcc/rust/typecheck/rust-tyty-call.h b/gcc/rust/typecheck/rust-tyty-call.h
index 06918c2..2e98cbb 100644
--- a/gcc/rust/typecheck/rust-tyty-call.h
+++ b/gcc/rust/typecheck/rust-tyty-call.h
@@ -41,7 +41,6 @@ public:
void visit (UnitType &type) override { gcc_unreachable (); }
void visit (InferType &type) override { gcc_unreachable (); }
void visit (StructFieldType &type) override { gcc_unreachable (); }
- void visit (ParamType &type) override { gcc_unreachable (); }
void visit (ArrayType &type) override { gcc_unreachable (); }
void visit (BoolType &type) override { gcc_unreachable (); }
void visit (IntType &type) override { gcc_unreachable (); }
diff --git a/gcc/rust/typecheck/rust-tyty-resolver.h b/gcc/rust/typecheck/rust-tyty-resolver.h
index a5906d8..d48952c 100644
--- a/gcc/rust/typecheck/rust-tyty-resolver.h
+++ b/gcc/rust/typecheck/rust-tyty-resolver.h
@@ -193,7 +193,6 @@ public:
void visit (TyTy::InferType &type) override { gcc_unreachable (); }
void visit (TyTy::StructFieldType &type) override { gcc_unreachable (); }
void visit (TyTy::ADTType &type) override { gcc_unreachable (); }
- void visit (TyTy::ParamType &type) override { gcc_unreachable (); }
void visit (TyTy::FnType &type) override { gcc_unreachable (); }
void visit (TyTy::BoolType &type) override { gcc_unreachable (); }
void visit (TyTy::IntType &type) override { gcc_unreachable (); }
diff --git a/gcc/rust/typecheck/rust-tyty-rules.h b/gcc/rust/typecheck/rust-tyty-rules.h
index 1eaf3e9..241b9be 100644
--- a/gcc/rust/typecheck/rust-tyty-rules.h
+++ b/gcc/rust/typecheck/rust-tyty-rules.h
@@ -68,11 +68,6 @@ public:
rust_error_at (def_locus, "declared here");
}
- virtual void visit (ParamType &type) override
- {
- type.get_base_type ()->accept_vis (*this);
- }
-
virtual void visit (ArrayType &type) override
{
Location ref_locus = mappings->lookup_location (type.get_ref ());
@@ -172,8 +167,6 @@ public:
void visit (FloatType &type) override { resolved = type.clone (); }
- void visit (ParamType &type) override { resolved = type.clone (); }
-
void visit (ArrayType &type) override { resolved = type.clone (); }
void visit (ADTType &type) override { resolved = type.clone (); }
@@ -243,21 +236,6 @@ private:
FnType *base;
};
-class ParamRules : protected BaseRules
-{
-public:
- ParamRules (ParamType *base) : BaseRules (base), base (base) {}
-
- TyBase *combine (TyBase *other)
- {
- // we only case about the base type of a param
- return base->get_base_type ()->combine (other);
- }
-
-private:
- ParamType *base;
-};
-
class ArrayRules : protected BaseRules
{
public:
diff --git a/gcc/rust/typecheck/rust-tyty-visitor.h b/gcc/rust/typecheck/rust-tyty-visitor.h
index def43cd..b6759dd 100644
--- a/gcc/rust/typecheck/rust-tyty-visitor.h
+++ b/gcc/rust/typecheck/rust-tyty-visitor.h
@@ -32,7 +32,6 @@ public:
virtual void visit (StructFieldType &type) = 0;
virtual void visit (ADTType &type) = 0;
virtual void visit (FnType &type) = 0;
- virtual void visit (ParamType &type) = 0;
virtual void visit (ArrayType &type) = 0;
virtual void visit (BoolType &type) = 0;
virtual void visit (IntType &type) = 0;
diff --git a/gcc/rust/typecheck/rust-tyty.cc b/gcc/rust/typecheck/rust-tyty.cc
index 7ca60c8..2e09de6 100644
--- a/gcc/rust/typecheck/rust-tyty.cc
+++ b/gcc/rust/typecheck/rust-tyty.cc
@@ -173,7 +173,9 @@ FnType::as_string () const
std::string params_str = "";
for (auto &param : params)
{
- params_str += param->as_string ();
+ auto pattern = param.first;
+ auto ty = param.second;
+ params_str += pattern->as_string () + " " + ty->as_string ();
params_str += ",";
}
@@ -191,41 +193,16 @@ FnType::combine (TyBase *other)
TyBase *
FnType::clone ()
{
- std::vector<ParamType *> cloned_params;
+ std::vector<std::pair<HIR::Pattern *, TyBase *> > cloned_params;
for (auto &p : params)
- cloned_params.push_back ((ParamType *) p->clone ());
+ cloned_params.push_back (
+ std::pair<HIR::Pattern *, TyBase *> (p.first, p.second->clone ()));
return new FnType (get_ref (), get_ty_ref (), cloned_params,
get_return_type ()->clone ());
}
void
-ParamType::accept_vis (TyVisitor &vis)
-{
- vis.visit (*this);
-}
-
-std::string
-ParamType::as_string () const
-{
- return "(" + identifier + " :" + type->as_string () + ")";
-}
-
-TyBase *
-ParamType::combine (TyBase *other)
-{
- ParamRules r (this);
- return r.combine (other);
-}
-
-TyBase *
-ParamType::clone ()
-{
- return new ParamType (get_ref (), get_ty_ref (), get_identifier (),
- get_base_type ()->clone ());
-}
-
-void
ArrayType::accept_vis (TyVisitor &vis)
{
vis.visit (*this);
@@ -449,7 +426,7 @@ TypeCheckCallExpr::visit (FnType &type)
size_t i = 0;
call.iterate_params ([&] (HIR::Expr *p) mutable -> bool {
- TyBase *pt = type.param_at (i);
+ auto fnparam = type.param_at (i);
auto t = Resolver::TypeCheckExpr::Resolve (p);
if (t == nullptr)
{
@@ -457,6 +434,7 @@ TypeCheckCallExpr::visit (FnType &type)
return false;
}
+ auto pt = fnparam.second;
auto res = pt->combine (t);
if (res == nullptr)
return false;
diff --git a/gcc/rust/typecheck/rust-tyty.h b/gcc/rust/typecheck/rust-tyty.h
index c32ac02..a73a344 100644
--- a/gcc/rust/typecheck/rust-tyty.h
+++ b/gcc/rust/typecheck/rust-tyty.h
@@ -234,43 +234,17 @@ private:
std::vector<StructFieldType *> fields;
};
-class ParamType : public TyBase
-{
-public:
- ParamType (HirId ref, std::string identifier, TyBase *type)
- : TyBase (ref, ref, TypeKind::PARAM), identifier (identifier), type (type)
- {}
-
- ParamType (HirId ref, HirId ty_ref, std::string identifier, TyBase *type)
- : TyBase (ref, ty_ref, TypeKind::PARAM), identifier (identifier),
- type (type)
- {}
-
- void accept_vis (TyVisitor &vis) override;
-
- std::string as_string () const override;
-
- TyBase *combine (TyBase *other) override;
-
- std::string get_identifier () const { return identifier; }
-
- TyBase *get_base_type () { return type; }
-
- TyBase *clone () final override;
-
-private:
- std::string identifier;
- TyBase *type;
-};
-
class FnType : public TyBase
{
public:
- FnType (HirId ref, std::vector<ParamType *> params, TyBase *type)
- : TyBase (ref, ref, TypeKind::FNDEF), params (params), type (type)
+ FnType (HirId ref, std::vector<std::pair<HIR::Pattern *, TyBase *> > params,
+ TyBase *type)
+ : TyBase (ref, ref, TypeKind::FNDEF), params (std::move (params)),
+ type (type)
{}
- FnType (HirId ref, HirId ty_ref, std::vector<ParamType *> params,
+ FnType (HirId ref, HirId ty_ref,
+ std::vector<std::pair<HIR::Pattern *, TyBase *> > params,
TyBase *type)
: TyBase (ref, ty_ref, TypeKind::FNDEF), params (params), type (type)
{}
@@ -285,14 +259,22 @@ public:
size_t num_params () const { return params.size (); }
- ParamType *param_at (size_t idx) { return params[idx]; }
+ std::vector<std::pair<HIR::Pattern *, TyBase *> > &get_params ()
+ {
+ return params;
+ }
+
+ std::pair<HIR::Pattern *, TyBase *> &param_at (size_t idx)
+ {
+ return params[idx];
+ }
TyBase *get_return_type () { return type; }
TyBase *clone () final override;
private:
- std::vector<ParamType *> params;
+ std::vector<std::pair<HIR::Pattern *, TyBase *> > params;
TyBase *type;
};