diff options
-rw-r--r-- | gcc/rust/ast/rust-item.h | 3 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-implitem.h | 11 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-item.h | 23 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-implitem.h | 2 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-item.h | 9 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-item.h | 44 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-item.h | 14 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-implitem.h | 11 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-item.h | 5 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-toplevel.h | 22 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-tycheck-dump.h | 4 |
11 files changed, 111 insertions, 37 deletions
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 35d1827..773bcaf 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -1447,8 +1447,6 @@ class Function : public VisItem, public InherentImplItem, public TraitImplItem Location locus; public: - std::vector<LetStmt *> locals; - std::string as_string () const override; // Returns whether function has generic parameters. @@ -1550,7 +1548,6 @@ public: return function_body == nullptr; } - // TODO: this mutable getter seems really dodgy. Think up better way. std::vector<FunctionParam> &get_function_params () { return function_params; } const std::vector<FunctionParam> &get_function_params () const { diff --git a/gcc/rust/backend/rust-compile-implitem.h b/gcc/rust/backend/rust-compile-implitem.h index 1b6651a..9370e9c 100644 --- a/gcc/rust/backend/rust-compile-implitem.h +++ b/gcc/rust/backend/rust-compile-implitem.h @@ -95,7 +95,7 @@ public: unsigned int flags = 0; std::string fn_identifier - = self->get_name () + "_" + function.function_name; + = self->get_name () + "_" + function.get_function_name (); // if its the main fn or pub visibility mark its as DECL_PUBLIC // please see https://github.com/Rust-GCC/gccrs/pull/137 @@ -116,7 +116,8 @@ public: size_t i = 0; for (auto &it : fntype->get_params ()) { - HIR::FunctionParam &referenced_param = function.function_params.at (i); + HIR::FunctionParam &referenced_param + = function.get_function_params ().at (i); auto param_tyty = it.second; auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty); @@ -147,7 +148,7 @@ public: } // lookup locals - auto block_expr = function.function_body.get (); + auto block_expr = function.get_definition ().get (); auto body_mappings = block_expr->get_mappings (); Resolver::Rib *rib = nullptr; @@ -180,7 +181,7 @@ public: Bblock *enclosing_scope = toplevel_item ? NULL : ctx->peek_enclosing_scope (); - HIR::BlockExpr *function_body = function.function_body.get (); + HIR::BlockExpr *function_body = function.get_definition ().get (); Location start_location = function_body->get_locus (); Location end_location = function_body->get_closing_locus (); @@ -206,7 +207,7 @@ public: ctx->push_fn (fndecl, return_address); - compile_function_body (fndecl, function.function_body, + compile_function_body (fndecl, function.get_definition (), function.has_function_return_type ()); ctx->pop_block (); diff --git a/gcc/rust/backend/rust-compile-item.h b/gcc/rust/backend/rust-compile-item.h index 5279218..384bf94 100644 --- a/gcc/rust/backend/rust-compile-item.h +++ b/gcc/rust/backend/rust-compile-item.h @@ -105,7 +105,8 @@ public: if (!ctx->get_tyctx ()->lookup_type (function.get_mappings ().get_hirid (), &fntype_tyty)) { - rust_fatal_error (function.locus, "failed to lookup function type"); + rust_fatal_error (function.get_locus (), + "failed to lookup function type"); return; } @@ -120,23 +121,24 @@ public: ::Btype *compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype); unsigned int flags = 0; - bool is_main_fn = function.function_name.compare ("main") == 0; + bool is_main_fn = function.get_function_name ().compare ("main") == 0; // if its the main fn or pub visibility mark its as DECL_PUBLIC // please see https://github.com/Rust-GCC/gccrs/pull/137 if (is_main_fn || function.has_visibility ()) flags |= Backend::function_is_visible; - std::string asm_name = function.function_name; + std::string asm_name = function.get_function_name (); if (!is_main_fn) { // FIXME need name mangling - asm_name = "__" + function.function_name; + asm_name = "__" + function.get_function_name (); } Bfunction *fndecl - = ctx->get_backend ()->function (compiled_fn_type, function.function_name, - asm_name, flags, function.get_locus ()); + = ctx->get_backend ()->function (compiled_fn_type, + function.get_function_name (), asm_name, + flags, function.get_locus ()); ctx->insert_function_decl (function.get_mappings ().get_hirid (), fndecl); // setup the params @@ -147,7 +149,8 @@ public: size_t i = 0; for (auto &it : fntype->get_params ()) { - HIR::FunctionParam &referenced_param = function.function_params.at (i); + HIR::FunctionParam &referenced_param + = function.get_function_params ().at (i); auto param_tyty = it.second; auto compiled_param_type = TyTyResolveCompile::compile (ctx, param_tyty); @@ -178,7 +181,7 @@ public: } // lookup locals - auto block_expr = function.function_body.get (); + auto block_expr = function.get_definition ().get (); auto body_mappings = block_expr->get_mappings (); Resolver::Rib *rib = nullptr; @@ -211,7 +214,7 @@ public: Bblock *enclosing_scope = toplevel_item ? NULL : ctx->peek_enclosing_scope (); - HIR::BlockExpr *function_body = function.function_body.get (); + HIR::BlockExpr *function_body = function.get_definition ().get (); Location start_location = function_body->get_locus (); Location end_location = function_body->get_closing_locus (); @@ -237,7 +240,7 @@ public: ctx->push_fn (fndecl, return_address); - compile_function_body (fndecl, function.function_body, + compile_function_body (fndecl, function.get_definition (), function.has_function_return_type ()); ctx->pop_block (); diff --git a/gcc/rust/hir/rust-ast-lower-implitem.h b/gcc/rust/hir/rust-ast-lower-implitem.h index de4d55d..8a7fc36 100644 --- a/gcc/rust/hir/rust-ast-lower-implitem.h +++ b/gcc/rust/hir/rust-ast-lower-implitem.h @@ -147,7 +147,7 @@ public: function.get_locus ()); // add the mappings for the function params at the end - for (auto ¶m : fn->function_params) + for (auto ¶m : fn->get_function_params ()) { mappings->insert_hir_param (mapping.get_crate_num (), param.get_mappings ().get_hirid (), ¶m); diff --git a/gcc/rust/hir/rust-ast-lower-item.h b/gcc/rust/hir/rust-ast-lower-item.h index 2865a6a..e19e9f6 100644 --- a/gcc/rust/hir/rust-ast-lower-item.h +++ b/gcc/rust/hir/rust-ast-lower-item.h @@ -218,7 +218,6 @@ public: void visit (AST::Function &function) { // ignore for now and leave empty - std::vector<std::unique_ptr<HIR::GenericParam> > generic_params; std::vector<HIR::Attribute> outer_attrs; std::vector<std::unique_ptr<HIR::WhereClauseItem> > where_clause_items; HIR::WhereClause where_clause (std::move (where_clause_items)); @@ -227,6 +226,12 @@ public: HIR::Visibility vis = HIR::Visibility::create_public (); // need + std::vector<std::unique_ptr<HIR::GenericParam> > generic_params; + if (function.has_generics ()) + { + generic_params = lower_generic_params (function.get_generic_params ()); + } + Identifier function_name = function.get_function_name (); Location locus = function.get_locus (); @@ -284,7 +289,7 @@ public: function.get_locus ()); // add the mappings for the function params at the end - for (auto ¶m : fn->function_params) + for (auto ¶m : fn->get_function_params ()) { mappings->insert_hir_param (mapping.get_crate_num (), param.get_mappings ().get_hirid (), ¶m); diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index 1415727..34ea2c3 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -1255,7 +1255,6 @@ class LetStmt; // Rust function declaration HIR node class Function : public VisItem, public InherentImplItem, public TraitImplItem { -public: FunctionQualifiers qualifiers; Identifier function_name; @@ -1277,6 +1276,7 @@ public: Location locus; +public: std::string as_string () const override; // Returns whether function has generic parameters. @@ -1361,6 +1361,48 @@ public: return get_mappings (); }; + std::vector<FunctionParam> &get_function_params () { return function_params; } + const std::vector<FunctionParam> &get_function_params () const + { + return function_params; + } + + std::vector<std::unique_ptr<GenericParam> > &get_generic_params () + { + return generic_params; + } + const std::vector<std::unique_ptr<GenericParam> > &get_generic_params () const + { + return generic_params; + } + + // TODO: is this better? Or is a "vis_block" better? + std::unique_ptr<BlockExpr> &get_definition () + { + rust_assert (function_body != nullptr); + return function_body; + } + + FunctionQualifiers get_qualifiers () const { return qualifiers; } + + Identifier get_function_name () const { return function_name; } + + // TODO: is this better? Or is a "vis_block" better? + WhereClause &get_where_clause () + { + rust_assert (has_where_clause ()); + return where_clause; + } + + bool has_return_type () const { return return_type != nullptr; } + + // TODO: is this better? Or is a "vis_block" better? + std::unique_ptr<Type> &get_return_type () + { + rust_assert (has_return_type ()); + return return_type; + } + protected: /* Use covariance to implement clone function as returning this object * rather than base */ diff --git a/gcc/rust/resolve/rust-ast-resolve-item.h b/gcc/rust/resolve/rust-ast-resolve-item.h index 3e279b5..1e98232 100644 --- a/gcc/rust/resolve/rust-ast-resolve-item.h +++ b/gcc/rust/resolve/rust-ast-resolve-item.h @@ -107,10 +107,6 @@ public: void visit (AST::Function &function) { - if (function.has_return_type ()) - ResolveType::go (function.get_return_type ().get (), - function.get_node_id ()); - NodeId scope_node_id = function.get_node_id (); resolver->get_name_scope ().push (scope_node_id); resolver->get_type_scope ().push (scope_node_id); @@ -119,6 +115,16 @@ public: resolver->push_new_type_rib (resolver->get_type_scope ().peek ()); resolver->push_new_label_rib (resolver->get_type_scope ().peek ()); + if (function.has_generics ()) + { + for (auto &generic : function.get_generic_params ()) + ResolveGenericParam::go (generic.get (), function.get_node_id ()); + } + + if (function.has_return_type ()) + ResolveType::go (function.get_return_type ().get (), + function.get_node_id ()); + // we make a new scope so the names of parameters are resolved and shadowed // correctly for (auto ¶m : function.get_function_params ()) diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.h b/gcc/rust/typecheck/rust-hir-type-check-implitem.h index 0354055..0bc45c3 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-implitem.h +++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.h @@ -53,7 +53,8 @@ public: ret_type = new TyTy::UnitType (function.get_mappings ().get_hirid ()); else { - auto resolved = TypeCheckType::Resolve (function.return_type.get ()); + auto resolved + = TypeCheckType::Resolve (function.get_return_type ().get ()); if (resolved == nullptr) { rust_error_at (function.get_locus (), @@ -62,11 +63,12 @@ public: } ret_type = resolved->clone (); - ret_type->set_ref (function.return_type->get_mappings ().get_hirid ()); + ret_type->set_ref ( + function.get_return_type ()->get_mappings ().get_hirid ()); } std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *> > params; - for (auto ¶m : function.function_params) + for (auto ¶m : function.get_function_params ()) { // get the name as well required for later on auto param_tyty = TypeCheckType::Resolve (param.get_type ()); @@ -173,7 +175,8 @@ public: auto expected_ret_tyty = resolve_fn_type->get_return_type (); context->push_return_type (expected_ret_tyty); - auto result = TypeCheckExpr::Resolve (function.function_body.get (), false); + auto result + = TypeCheckExpr::Resolve (function.get_definition ().get (), false); auto ret_resolved = expected_ret_tyty->unify (result); if (ret_resolved == nullptr) return; diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.h b/gcc/rust/typecheck/rust-hir-type-check-item.h index 54fc3df..6b1a8c4 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-item.h +++ b/gcc/rust/typecheck/rust-hir-type-check-item.h @@ -58,7 +58,7 @@ public: TyTy::BaseType *lookup; if (!context->lookup_type (function.get_mappings ().get_hirid (), &lookup)) { - rust_error_at (function.locus, "failed to lookup function type"); + rust_error_at (function.get_locus (), "failed to lookup function type"); return; } @@ -75,7 +75,8 @@ public: auto expected_ret_tyty = resolve_fn_type->get_return_type (); context->push_return_type (expected_ret_tyty); - auto result = TypeCheckExpr::Resolve (function.function_body.get (), false); + auto result + = TypeCheckExpr::Resolve (function.get_definition ().get (), false); auto ret_resolved = expected_ret_tyty->unify (result); if (ret_resolved == nullptr) return; diff --git a/gcc/rust/typecheck/rust-hir-type-check-toplevel.h b/gcc/rust/typecheck/rust-hir-type-check-toplevel.h index a729cfe..893424f 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-toplevel.h +++ b/gcc/rust/typecheck/rust-hir-type-check-toplevel.h @@ -134,12 +134,27 @@ public: void visit (HIR::Function &function) { + std::vector<TyTy::SubstitionMapping> substitions; + if (function.has_generics ()) + { + for (auto &generic_param : function.get_generic_params ()) + { + auto param_type + = TypeResolveGenericParam::Resolve (generic_param.get ()); + context->insert_type (generic_param->get_mappings (), param_type); + + substitions.push_back ( + TyTy::SubstitionMapping (generic_param, param_type)); + } + } + TyTy::BaseType *ret_type = nullptr; if (!function.has_function_return_type ()) ret_type = new TyTy::UnitType (function.get_mappings ().get_hirid ()); else { - auto resolved = TypeCheckType::Resolve (function.return_type.get ()); + auto resolved + = TypeCheckType::Resolve (function.get_return_type ().get ()); if (resolved == nullptr) { rust_error_at (function.get_locus (), @@ -148,11 +163,12 @@ public: } ret_type = resolved->clone (); - ret_type->set_ref (function.return_type->get_mappings ().get_hirid ()); + ret_type->set_ref ( + function.get_return_type ()->get_mappings ().get_hirid ()); } std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *> > params; - for (auto ¶m : function.function_params) + for (auto ¶m : function.get_function_params ()) { // get the name as well required for later on auto param_tyty = TypeCheckType::Resolve (param.get_type ()); diff --git a/gcc/rust/typecheck/rust-tycheck-dump.h b/gcc/rust/typecheck/rust-tycheck-dump.h index 5bb33c9..c6dabe2 100644 --- a/gcc/rust/typecheck/rust-tycheck-dump.h +++ b/gcc/rust/typecheck/rust-tycheck-dump.h @@ -72,11 +72,11 @@ public: void visit (HIR::Function &function) override { - dump += indent () + "fn " + function.function_name + " " + dump += indent () + "fn " + function.get_function_name () + " " + type_string (function.get_mappings ()) + "\n"; dump += indent () + "{\n"; - HIR::BlockExpr *function_body = function.function_body.get (); + HIR::BlockExpr *function_body = function.get_definition ().get (); function_body->accept_vis (*this); dump += indent () + "}\n"; |