diff options
author | Ian Lance Taylor <iant@golang.org> | 2023-06-21 11:04:04 -0700 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2023-06-21 11:04:04 -0700 |
commit | 97e31a0a2a2d2273687fcdb4e5416aab1a2186e1 (patch) | |
tree | d5c1cae4de436a0fe54a5f0a2a197d309f3d654c /gcc/rust/backend | |
parent | 6612f4f8cb9b0d5af18ec69ad04e56debc3e6ced (diff) | |
parent | 577223aebc7acdd31e62b33c1682fe54a622ae27 (diff) | |
download | gcc-97e31a0a2a2d2273687fcdb4e5416aab1a2186e1.zip gcc-97e31a0a2a2d2273687fcdb4e5416aab1a2186e1.tar.gz gcc-97e31a0a2a2d2273687fcdb4e5416aab1a2186e1.tar.bz2 |
Merge from trunk revision 577223aebc7acdd31e62b33c1682fe54a622ae27.
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r-- | gcc/rust/backend/rust-compile-base.cc | 4 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.cc | 30 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-fnparam.h | 1 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-pattern.cc | 129 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-pattern.h | 78 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-resolve-path.h | 1 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-stmt.cc | 31 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-var-decl.h | 33 |
8 files changed, 225 insertions, 82 deletions
diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 568abf9..c108661 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -457,9 +457,7 @@ HIRCompileBase::compile_locals_for_block (Context *ctx, Resolver::Rib &rib, // compile the local tree type = TyTyResolveCompile::compile (ctx, tyty); - Bvariable *compiled - = CompileVarDecl::compile (fndecl, type, pattern, ctx); - locals.push_back (compiled); + CompileVarDecl::compile (fndecl, type, pattern, locals, ctx); } return locals; } diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 436fc92..d7945db 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -26,6 +26,7 @@ #include "rust-compile-block.h" #include "rust-compile-implitem.h" #include "rust-constexpr.h" +#include "rust-unify.h" #include "rust-gcc.h" #include "fold-const.h" @@ -1341,30 +1342,6 @@ CompileExpr::visit (HIR::MatchExpr &expr) // SWITCH_ALL_CASES_P is true if the switch includes a default label or the // case label ranges cover all possible values of the condition expression - /* Switch expression. - - TREE_TYPE is the original type of the condition, before any - language required type conversions. It may be NULL, in which case - the original type and final types are assumed to be the same. - - Operand 0 is the expression used to perform the branch, - Operand 1 is the body of the switch, which probably contains - CASE_LABEL_EXPRs. It may also be NULL, in which case operand 2 - must not be NULL. */ - // DEFTREECODE (SWITCH_EXPR, "switch_expr", tcc_statement, 2) - - /* Used to represent a case label. - - Operand 0 is CASE_LOW. It may be NULL_TREE, in which case the label - is a 'default' label. - Operand 1 is CASE_HIGH. If it is NULL_TREE, the label is a simple - (one-value) case label. If it is non-NULL_TREE, the case is a range. - Operand 2 is CASE_LABEL, which has the corresponding LABEL_DECL. - Operand 3 is CASE_CHAIN. This operand is only used in tree-cfg.cc to - speed up the lookup of case labels which use a particular edge in - the control flow graph. */ - // DEFTREECODE (CASE_LABEL_EXPR, "case_label_expr", tcc_statement, 4) - TyTy::TypeKind scrutinee_kind = check_match_scrutinee (expr, ctx); if (scrutinee_kind == TyTy::TypeKind::ERROR) { @@ -2030,7 +2007,10 @@ CompileExpr::resolve_method_address (TyTy::FnType *fntype, HirId ref, { TyTy::BaseType *infer_impl_call = candidate_call->infer_substitions (expr_locus); - monomorphized = infer_impl_call->unify (fntype); + monomorphized = Resolver::UnifyRules::Resolve ( + TyTy::TyWithLocation (infer_impl_call), + TyTy::TyWithLocation (fntype), expr_locus, true /* commit */, + true /* emit_errors */); } return CompileInherentImplItem::Compile (impl_item, ctx, monomorphized); diff --git a/gcc/rust/backend/rust-compile-fnparam.h b/gcc/rust/backend/rust-compile-fnparam.h index 0d99814..10e8b67 100644 --- a/gcc/rust/backend/rust-compile-fnparam.h +++ b/gcc/rust/backend/rust-compile-fnparam.h @@ -39,7 +39,6 @@ public: void visit (HIR::TupleStructPattern &) override; // Empty visit for unused Pattern HIR nodes. - void visit (HIR::GroupedPattern &) override {} void visit (HIR::LiteralPattern &) override {} void visit (HIR::PathInExpression &) override {} void visit (HIR::QualifiedPathInExpression &) override {} diff --git a/gcc/rust/backend/rust-compile-pattern.cc b/gcc/rust/backend/rust-compile-pattern.cc index fc70d4b..d98f7be 100644 --- a/gcc/rust/backend/rust-compile-pattern.cc +++ b/gcc/rust/backend/rust-compile-pattern.cc @@ -329,5 +329,134 @@ CompilePatternBindings::visit (HIR::StructPattern &pattern) } } +void +CompilePatternLet::visit (HIR::IdentifierPattern &pattern) +{ + Bvariable *var = nullptr; + rust_assert ( + ctx->lookup_var_decl (pattern.get_pattern_mappings ().get_hirid (), &var)); + + auto fnctx = ctx->peek_fn (); + if (ty->is_unit ()) + { + ctx->add_statement (init_expr); + + tree stmt_type = TyTyResolveCompile::compile (ctx, ty); + + auto unit_type_init_expr + = ctx->get_backend ()->constructor_expression (stmt_type, false, {}, -1, + rval_locus); + auto s = ctx->get_backend ()->init_statement (fnctx.fndecl, var, + unit_type_init_expr); + ctx->add_statement (s); + } + else + { + auto s + = ctx->get_backend ()->init_statement (fnctx.fndecl, var, init_expr); + ctx->add_statement (s); + } +} + +void +CompilePatternLet::visit (HIR::WildcardPattern &pattern) +{ + tree init_stmt = NULL; + tree stmt_type = TyTyResolveCompile::compile (ctx, ty); + + ctx->get_backend ()->temporary_variable (ctx->peek_fn ().fndecl, NULL_TREE, + stmt_type, init_expr, false, + pattern.get_locus (), &init_stmt); + + ctx->add_statement (init_stmt); +} + +void +CompilePatternLet::visit (HIR::TuplePattern &pattern) +{ + rust_assert (pattern.has_tuple_pattern_items ()); + + tree tuple_type = TyTyResolveCompile::compile (ctx, ty); + tree init_stmt; + Bvariable *tmp_var + = ctx->get_backend ()->temporary_variable (ctx->peek_fn ().fndecl, + NULL_TREE, tuple_type, init_expr, + false, pattern.get_locus (), + &init_stmt); + tree access_expr + = ctx->get_backend ()->var_expression (tmp_var, pattern.get_locus ()); + ctx->add_statement (init_stmt); + + switch (pattern.get_items ()->get_pattern_type ()) + { + case HIR::TuplePatternItems::TuplePatternItemType::RANGED: { + size_t tuple_idx = 0; + auto &items + = static_cast<HIR::TuplePatternItemsRanged &> (*pattern.get_items ()); + + auto &items_lower = items.get_lower_patterns (); + auto &items_upper = items.get_upper_patterns (); + + for (auto &sub : items_lower) + { + TyTy::BaseType *ty_sub = nullptr; + HirId pattern_id = pattern.get_pattern_mappings ().get_hirid (); + bool ok = ctx->get_tyctx ()->lookup_type (pattern_id, &ty_sub); + rust_assert (ok); + + tree sub_init = ctx->get_backend ()->struct_field_expression ( + access_expr, tuple_idx, sub->get_locus ()); + CompilePatternLet::Compile (sub.get (), sub_init, ty_sub, + rval_locus, ctx); + tuple_idx++; + } + + rust_assert (ty->get_kind () == TyTy::TypeKind::TUPLE); + tuple_idx = static_cast<TyTy::TupleType &> (*ty).num_fields () + - items_upper.size (); + + for (auto &sub : items_upper) + { + TyTy::BaseType *ty_sub = nullptr; + HirId pattern_id = pattern.get_pattern_mappings ().get_hirid (); + bool ok = ctx->get_tyctx ()->lookup_type (pattern_id, &ty_sub); + rust_assert (ok); + + tree sub_init = ctx->get_backend ()->struct_field_expression ( + access_expr, tuple_idx, sub->get_locus ()); + CompilePatternLet::Compile (sub.get (), sub_init, ty_sub, + rval_locus, ctx); + tuple_idx++; + } + + return; + } + case HIR::TuplePatternItems::TuplePatternItemType::MULTIPLE: { + size_t tuple_idx = 0; + auto &items = static_cast<HIR::TuplePatternItemsMultiple &> ( + *pattern.get_items ()); + + for (auto &sub : items.get_patterns ()) + { + TyTy::BaseType *ty_sub = nullptr; + HirId pattern_id = pattern.get_pattern_mappings ().get_hirid (); + bool ok = ctx->get_tyctx ()->lookup_type (pattern_id, &ty_sub); + rust_assert (ok); + + tree sub_init = ctx->get_backend ()->struct_field_expression ( + access_expr, tuple_idx, sub->get_locus ()); + CompilePatternLet::Compile (sub.get (), sub_init, ty_sub, + rval_locus, ctx); + tuple_idx++; + } + + return; + } + default: { + gcc_unreachable (); + } + } +} + } // namespace Compile } // namespace Rust diff --git a/gcc/rust/backend/rust-compile-pattern.h b/gcc/rust/backend/rust-compile-pattern.h index 22812a4..8f44b7b 100644 --- a/gcc/rust/backend/rust-compile-pattern.h +++ b/gcc/rust/backend/rust-compile-pattern.h @@ -40,7 +40,6 @@ public: void visit (HIR::RangePattern &pattern) override; // Empty visit for unused Pattern HIR nodes. - void visit (HIR::GroupedPattern &) override {} void visit (HIR::IdentifierPattern &) override {} void visit (HIR::LiteralPattern &) override; void visit (HIR::QualifiedPathInExpression &) override {} @@ -72,7 +71,6 @@ public: void visit (HIR::TupleStructPattern &pattern) override; // Empty visit for unused Pattern HIR nodes. - void visit (HIR::GroupedPattern &) override {} void visit (HIR::IdentifierPattern &) override {} void visit (HIR::LiteralPattern &) override {} void visit (HIR::PathInExpression &) override {} @@ -91,5 +89,81 @@ protected: tree match_scrutinee_expr; }; +class CompilePatternLet : public HIRCompileBase, public HIR::HIRPatternVisitor +{ +public: + static void Compile (HIR::Pattern *pattern, tree init_expr, + TyTy::BaseType *ty, Location rval_locus, Context *ctx) + { + CompilePatternLet compiler (ctx, init_expr, ty, rval_locus); + pattern->accept_vis (compiler); + } + + void visit (HIR::IdentifierPattern &) override; + void visit (HIR::WildcardPattern &) override; + void visit (HIR::TuplePattern &) override; + + // check for unimplemented Pattern HIR nodes. + void visit (HIR::LiteralPattern &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "literal pattern let statements not supported"); + } + + void visit (HIR::PathInExpression &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "path-in-expression pattern let statements not supported"); + } + + void visit (HIR::QualifiedPathInExpression &pattern) override + { + rust_sorry_at ( + pattern.get_locus (), + "qualified-path-in-expression pattern let statements not supported"); + } + + void visit (HIR::RangePattern &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "range pattern let statements not supported"); + } + + void visit (HIR::ReferencePattern &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "reference pattern let statements not supported"); + } + + void visit (HIR::SlicePattern &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "slice pattern let statements not supported"); + } + + void visit (HIR::StructPattern &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "struct pattern let statements not supported"); + } + + void visit (HIR::TupleStructPattern &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "tuple-struct pattern let statements not supported"); + } + +protected: + CompilePatternLet (Context *ctx, tree init_expr, TyTy::BaseType *ty, + Location rval_locus) + : HIRCompileBase (ctx), init_expr (init_expr), ty (ty), + rval_locus (rval_locus) + {} + + tree init_expr; + TyTy::BaseType *ty; + Location rval_locus; +}; + } // namespace Compile } // namespace Rust diff --git a/gcc/rust/backend/rust-compile-resolve-path.h b/gcc/rust/backend/rust-compile-resolve-path.h index 67ff7ee..6aec7be 100644 --- a/gcc/rust/backend/rust-compile-resolve-path.h +++ b/gcc/rust/backend/rust-compile-resolve-path.h @@ -45,7 +45,6 @@ public: void visit (HIR::QualifiedPathInExpression &expr) override; // Empty visit for unused Pattern HIR nodes. - void visit (HIR::GroupedPattern &) override {} void visit (HIR::IdentifierPattern &) override {} void visit (HIR::LiteralPattern &) override {} void visit (HIR::RangePattern &) override {} diff --git a/gcc/rust/backend/rust-compile-stmt.cc b/gcc/rust/backend/rust-compile-stmt.cc index 3fc2528..6bb4ead 100644 --- a/gcc/rust/backend/rust-compile-stmt.cc +++ b/gcc/rust/backend/rust-compile-stmt.cc @@ -16,6 +16,7 @@ // along with GCC; see the file COPYING3. If not see // <http://www.gnu.org/licenses/>. +#include "rust-compile-pattern.h" #include "rust-compile-stmt.h" #include "rust-compile-expr.h" @@ -53,7 +54,7 @@ CompileStmt::visit (HIR::LetStmt &stmt) if (!stmt.has_init_expr ()) return; - const HIR::Pattern &stmt_pattern = *stmt.get_pattern (); + HIR::Pattern &stmt_pattern = *stmt.get_pattern (); HirId stmt_id = stmt_pattern.get_pattern_mappings ().get_hirid (); TyTy::BaseType *ty = nullptr; @@ -65,15 +66,6 @@ CompileStmt::visit (HIR::LetStmt &stmt) return; } - Bvariable *var = nullptr; - if (!ctx->lookup_var_decl (stmt_id, &var)) - { - // FIXME this should be an assertion instead and use error mark node - rust_fatal_error (stmt.get_locus (), - "failed to lookup compiled variable declaration"); - return; - } - tree init = CompileExpr::Compile (stmt.get_init_expr (), ctx); // FIXME use error_mark_node, check that CompileExpr returns error_mark_node // on failure and make this an assertion @@ -84,7 +76,6 @@ CompileStmt::visit (HIR::LetStmt &stmt) bool ok = ctx->get_tyctx ()->lookup_type ( stmt.get_init_expr ()->get_mappings ().get_hirid (), &actual); rust_assert (ok); - tree stmt_type = TyTyResolveCompile::compile (ctx, ty); Location lvalue_locus = stmt.get_pattern ()->get_locus (); Location rvalue_locus = stmt.get_init_expr ()->get_locus (); @@ -92,23 +83,7 @@ CompileStmt::visit (HIR::LetStmt &stmt) init = coercion_site (stmt.get_mappings ().get_hirid (), init, actual, expected, lvalue_locus, rvalue_locus); - auto fnctx = ctx->peek_fn (); - if (ty->is_unit ()) - { - ctx->add_statement (init); - - auto unit_type_init_expr - = ctx->get_backend ()->constructor_expression (stmt_type, false, {}, -1, - rvalue_locus); - auto s = ctx->get_backend ()->init_statement (fnctx.fndecl, var, - unit_type_init_expr); - ctx->add_statement (s); - } - else - { - auto s = ctx->get_backend ()->init_statement (fnctx.fndecl, var, init); - ctx->add_statement (s); - } + CompilePatternLet::Compile (&stmt_pattern, init, ty, rvalue_locus, ctx); } } // namespace Compile diff --git a/gcc/rust/backend/rust-compile-var-decl.h b/gcc/rust/backend/rust-compile-var-decl.h index 00146a4..13b429d 100644 --- a/gcc/rust/backend/rust-compile-var-decl.h +++ b/gcc/rust/backend/rust-compile-var-decl.h @@ -30,12 +30,11 @@ class CompileVarDecl : public HIRCompileBase, public HIR::HIRPatternVisitor using HIR::HIRPatternVisitor::visit; public: - static ::Bvariable *compile (tree fndecl, tree translated_type, - HIR::Pattern *pattern, Context *ctx) + static void compile (tree fndecl, tree translated_type, HIR::Pattern *pattern, + std::vector<Bvariable *> &locals, Context *ctx) { - CompileVarDecl compiler (ctx, fndecl, translated_type); + CompileVarDecl compiler (ctx, fndecl, translated_type, locals); pattern->accept_vis (compiler); - return compiler.compiled_variable; } void visit (HIR::IdentifierPattern &pattern) override @@ -43,30 +42,18 @@ public: if (!pattern.is_mut ()) translated_type = ctx->get_backend ()->immutable_type (translated_type); - compiled_variable + Bvariable *var = ctx->get_backend ()->local_variable (fndecl, pattern.get_identifier (), translated_type, NULL /*decl_var*/, pattern.get_locus ()); HirId stmt_id = pattern.get_pattern_mappings ().get_hirid (); - ctx->insert_var_decl (stmt_id, compiled_variable); - } - - void visit (HIR::WildcardPattern &pattern) override - { - translated_type = ctx->get_backend ()->immutable_type (translated_type); + ctx->insert_var_decl (stmt_id, var); - compiled_variable - = ctx->get_backend ()->local_variable (fndecl, "_", translated_type, - NULL /*decl_var*/, - pattern.get_locus ()); - - HirId stmt_id = pattern.get_pattern_mappings ().get_hirid (); - ctx->insert_var_decl (stmt_id, compiled_variable); + locals.push_back (var); } // Empty visit for unused Pattern HIR nodes. - void visit (HIR::GroupedPattern &) override {} void visit (HIR::LiteralPattern &) override {} void visit (HIR::PathInExpression &) override {} void visit (HIR::QualifiedPathInExpression &) override {} @@ -76,17 +63,19 @@ public: void visit (HIR::StructPattern &) override {} void visit (HIR::TuplePattern &) override {} void visit (HIR::TupleStructPattern &) override {} + void visit (HIR::WildcardPattern &) override {} private: - CompileVarDecl (Context *ctx, tree fndecl, tree translated_type) + CompileVarDecl (Context *ctx, tree fndecl, tree translated_type, + std::vector<Bvariable *> &locals) : HIRCompileBase (ctx), fndecl (fndecl), translated_type (translated_type), - compiled_variable (ctx->get_backend ()->error_variable ()) + locals (locals) {} tree fndecl; tree translated_type; - Bvariable *compiled_variable; + std::vector<Bvariable *> &locals; }; } // namespace Compile |