From 881ed7fb273c8898310a1475aa2162bbd1030ee0 Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Wed, 11 Jan 2023 12:05:39 -0500 Subject: gccrs: Change how CompileVarDecl outputs Bvariable's This allows patterns to declare multiple/no variables gcc/rust/ChangeLog: * backend/rust-compile-base.cc (HIRCompileBase::compile_locals_for_block): Allow patterns to declare zero or multiple variables. * backend/rust-compile-var-decl.h: Change function declaration. Signed-off-by: Owen Avery --- gcc/rust/backend/rust-compile-base.cc | 4 +--- gcc/rust/backend/rust-compile-var-decl.h | 26 +++++++++++++++----------- 2 files changed, 16 insertions(+), 14 deletions(-) (limited to 'gcc/rust/backend') 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-var-decl.h b/gcc/rust/backend/rust-compile-var-decl.h index 00146a4..791ee9c 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 &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,26 +42,30 @@ 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); + ctx->insert_var_decl (stmt_id, var); + + locals.push_back (var); } void visit (HIR::WildcardPattern &pattern) override { translated_type = ctx->get_backend ()->immutable_type (translated_type); - compiled_variable + Bvariable *var = 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); + ctx->insert_var_decl (stmt_id, var); + + locals.push_back (var); } // Empty visit for unused Pattern HIR nodes. @@ -78,15 +81,16 @@ public: void visit (HIR::TupleStructPattern &) override {} private: - CompileVarDecl (Context *ctx, tree fndecl, tree translated_type) + CompileVarDecl (Context *ctx, tree fndecl, tree translated_type, + std::vector &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 &locals; }; } // namespace Compile -- cgit v1.1 From 221650c280c4a02ff472436a32bb267c7b5f8d22 Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Wed, 11 Jan 2023 11:57:28 -0500 Subject: gccrs: Added missing GroupedPattern visitors for code generation gcc/rust/ChangeLog: * backend/rust-compile-pattern.cc (CompilePatternCaseLabelExpr::visit): Add proper visitor. (CompilePatternBindings::visit): Likewise. * backend/rust-compile-pattern.h: Declare them. Signed-off-by: Owen Avery --- gcc/rust/backend/rust-compile-pattern.cc | 12 ++++++++++++ gcc/rust/backend/rust-compile-pattern.h | 4 ++-- 2 files changed, 14 insertions(+), 2 deletions(-) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-pattern.cc b/gcc/rust/backend/rust-compile-pattern.cc index fc70d4b..9218b81 100644 --- a/gcc/rust/backend/rust-compile-pattern.cc +++ b/gcc/rust/backend/rust-compile-pattern.cc @@ -161,6 +161,12 @@ CompilePatternCaseLabelExpr::visit (HIR::RangePattern &pattern) case_label_expr = build_case_label (lower, upper, associated_case_label); } +void +CompilePatternCaseLabelExpr::visit (HIR::GroupedPattern &pattern) +{ + pattern.get_item ()->accept_vis (*this); +} + // setup the bindings void @@ -329,5 +335,11 @@ CompilePatternBindings::visit (HIR::StructPattern &pattern) } } +void +CompilePatternBindings::visit (HIR::GroupedPattern &pattern) +{ + pattern.get_item ()->accept_vis (*this); +} + } // namespace Compile } // namespace Rust diff --git a/gcc/rust/backend/rust-compile-pattern.h b/gcc/rust/backend/rust-compile-pattern.h index 22812a4..43334af 100644 --- a/gcc/rust/backend/rust-compile-pattern.h +++ b/gcc/rust/backend/rust-compile-pattern.h @@ -38,9 +38,9 @@ public: void visit (HIR::TupleStructPattern &pattern) override; void visit (HIR::WildcardPattern &pattern) override; void visit (HIR::RangePattern &pattern) override; + void visit (HIR::GroupedPattern &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 {} @@ -70,9 +70,9 @@ public: void visit (HIR::StructPattern &pattern) override; void visit (HIR::TupleStructPattern &pattern) override; + void visit (HIR::GroupedPattern &) 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 {} -- cgit v1.1 From ad9d75f50804a983a06e2fdf69772188432359ae Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Fri, 27 Jan 2023 11:47:30 -0500 Subject: gccrs: Create and use CompilePatternLet visitor for compiling let statments gcc/rust/ChangeLog: * backend/rust-compile-pattern.cc (CompilePatternLet::visit): New function. * backend/rust-compile-stmt.cc (CompileStmt::visit): Likewise. * backend/rust-compile-pattern.h (class CompilePatternLet): New visitor. Signed-off-by: Owen Avery --- gcc/rust/backend/rust-compile-pattern.cc | 58 +++++++++++++++++++++ gcc/rust/backend/rust-compile-pattern.h | 87 ++++++++++++++++++++++++++++++++ gcc/rust/backend/rust-compile-stmt.cc | 31 ++---------- 3 files changed, 148 insertions(+), 28 deletions(-) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-pattern.cc b/gcc/rust/backend/rust-compile-pattern.cc index 9218b81..e13d6ca 100644 --- a/gcc/rust/backend/rust-compile-pattern.cc +++ b/gcc/rust/backend/rust-compile-pattern.cc @@ -341,5 +341,63 @@ CompilePatternBindings::visit (HIR::GroupedPattern &pattern) pattern.get_item ()->accept_vis (*this); } +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) +{ + 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); + } +} + } // namespace Compile } // namespace Rust diff --git a/gcc/rust/backend/rust-compile-pattern.h b/gcc/rust/backend/rust-compile-pattern.h index 43334af..317a345 100644 --- a/gcc/rust/backend/rust-compile-pattern.h +++ b/gcc/rust/backend/rust-compile-pattern.h @@ -91,5 +91,92 @@ 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; + + // check for unimplemented Pattern HIR nodes. + void visit (HIR::GroupedPattern &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "grouped pattern let statements not supported"); + } + + 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::TuplePattern &pattern) override + { + rust_sorry_at (pattern.get_locus (), + "tuple 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-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 // . +#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 -- cgit v1.1 From 95efd84db13b9ac2b27654e86d1cacefe0637415 Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Mon, 30 Jan 2023 20:14:34 -0500 Subject: gccrs: Remove HIR::GroupedPattern gcc/rust/ChangeLog: * backend/rust-compile-fnparam.h (CompileFnParam::visit): Remove HIR::GroupedPattern visitor. * backend/rust-compile-pattern.cc (CompilePatternCaseLabelExpr::visit): Remove HIR::GroupedPattern visitor. (CompilePatternBindings::visit): Remove HIR::GroupedPattern visitor. * backend/rust-compile-pattern.h (CompilePatternCaseLabelExpr::visit): Remove HIR::GroupedPattern visitor. (CompilePatternBindings::visit): Remove HIR::GroupedPattern visitor. (CompilePatternLet::visit): Remove HIR::GroupedPattern visitor. * backend/rust-compile-resolve-path.h (ResolvePathRef::visit): Remove HIR::GroupedPattern visitor. * backend/rust-compile-var-decl.h (CompileVarDecl::visit): Remove HIR::GroupedPattern visitor. * checks/errors/rust-const-checker.cc (ConstChecker::visit): Remove HIR::GroupedPattern visitor. * checks/errors/rust-const-checker.h (ConstChecker::visit): Remove HIR::GroupedPattern visitor. * checks/errors/rust-unsafe-checker.cc (UnsafeChecker::visit): Remove HIR::GroupedPattern visitor. * checks/errors/rust-unsafe-checker.h (UnsafeChecker::visit): Remove HIR::GroupedPattern visitor. * hir/rust-hir-dump.cc (Dump::visit): Remove HIR::GroupedPattern visitor. * hir/rust-hir-dump.h (Dump::visit): Remove HIR::GroupedPattern visitor. * hir/tree/rust-hir-full-decls.h (class GroupedPattern): Remove class. * hir/tree/rust-hir-full-test.cc (GroupedPattern::accept_vis): Remove method. * hir/tree/rust-hir-pattern.h (class GroupedPattern): Remove class. * hir/tree/rust-hir-visitor.h (HIRFullVisitor::visit): Remove HIR::GroupedPattern visitor. (HIRFullVisitorBase::visit): Remove HIR::GroupedPattern visitor. (HIRPatternVisitor::visit): Remove HIR::GroupedPattern visitor. * typecheck/rust-hir-type-check-pattern.cc (TypeCheckPattern::visit): Remove HIR::GroupedPattern visitor. * typecheck/rust-hir-type-check-pattern.h (TypeCheckPattern::visit): Remove HIR::GroupedPattern visitor. Signed-off-by: Owen Avery --- gcc/rust/backend/rust-compile-fnparam.h | 1 - gcc/rust/backend/rust-compile-pattern.cc | 12 ------------ gcc/rust/backend/rust-compile-pattern.h | 8 -------- gcc/rust/backend/rust-compile-resolve-path.h | 1 - gcc/rust/backend/rust-compile-var-decl.h | 1 - 5 files changed, 23 deletions(-) (limited to 'gcc/rust/backend') 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 e13d6ca..bad98be 100644 --- a/gcc/rust/backend/rust-compile-pattern.cc +++ b/gcc/rust/backend/rust-compile-pattern.cc @@ -161,12 +161,6 @@ CompilePatternCaseLabelExpr::visit (HIR::RangePattern &pattern) case_label_expr = build_case_label (lower, upper, associated_case_label); } -void -CompilePatternCaseLabelExpr::visit (HIR::GroupedPattern &pattern) -{ - pattern.get_item ()->accept_vis (*this); -} - // setup the bindings void @@ -336,12 +330,6 @@ CompilePatternBindings::visit (HIR::StructPattern &pattern) } void -CompilePatternBindings::visit (HIR::GroupedPattern &pattern) -{ - pattern.get_item ()->accept_vis (*this); -} - -void CompilePatternLet::visit (HIR::IdentifierPattern &pattern) { Bvariable *var = nullptr; diff --git a/gcc/rust/backend/rust-compile-pattern.h b/gcc/rust/backend/rust-compile-pattern.h index 317a345..ddf5b14 100644 --- a/gcc/rust/backend/rust-compile-pattern.h +++ b/gcc/rust/backend/rust-compile-pattern.h @@ -38,7 +38,6 @@ public: void visit (HIR::TupleStructPattern &pattern) override; void visit (HIR::WildcardPattern &pattern) override; void visit (HIR::RangePattern &pattern) override; - void visit (HIR::GroupedPattern &pattern) override; // Empty visit for unused Pattern HIR nodes. void visit (HIR::IdentifierPattern &) override {} @@ -70,7 +69,6 @@ public: void visit (HIR::StructPattern &pattern) override; void visit (HIR::TupleStructPattern &pattern) override; - void visit (HIR::GroupedPattern &) override; // Empty visit for unused Pattern HIR nodes. void visit (HIR::IdentifierPattern &) override {} @@ -105,12 +103,6 @@ public: void visit (HIR::WildcardPattern &) override; // check for unimplemented Pattern HIR nodes. - void visit (HIR::GroupedPattern &pattern) override - { - rust_sorry_at (pattern.get_locus (), - "grouped pattern let statements not supported"); - } - void visit (HIR::LiteralPattern &pattern) override { rust_sorry_at (pattern.get_locus (), 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-var-decl.h b/gcc/rust/backend/rust-compile-var-decl.h index 791ee9c..9e07999 100644 --- a/gcc/rust/backend/rust-compile-var-decl.h +++ b/gcc/rust/backend/rust-compile-var-decl.h @@ -69,7 +69,6 @@ public: } // 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 {} -- cgit v1.1 From 4141941ae7bd126c31f09b0aa23bc8a14186f496 Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Sat, 4 Feb 2023 12:07:38 -0500 Subject: gccrs: Removed comment copy-pasted from gcc/tree.def gcc/rust/ChangeLog: * backend/rust-compile-expr.cc (CompileExpr::visit): Removed copy-pasted comment. Signed-off-by: Owen Avery --- gcc/rust/backend/rust-compile-expr.cc | 24 ------------------------ 1 file changed, 24 deletions(-) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index 436fc92..c26f6f4 100644 --- a/gcc/rust/backend/rust-compile-expr.cc +++ b/gcc/rust/backend/rust-compile-expr.cc @@ -1341,30 +1341,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) { -- cgit v1.1 From 68d671ac725eb0937a892516906e300ad8a3c538 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Mon, 30 Jan 2023 18:19:07 +0000 Subject: gccrs: Refactor the type unification code This refactors the unification systems to be a consistent interface using switch statements and simple functions instead of the old clunky visitor system. This is more maintainable as it is harder to miss cases when we can take advantages of switch statements. Signed-off-by: Philip Herron gcc/rust/ChangeLog: * Make-lang.in: update names * backend/rust-compile-expr.cc (CompileExpr::resolve_method_address): update to use new interface * typecheck/rust-coercion.cc (TypeCoercionRules::coerce_borrowed_pointer): likewise * typecheck/rust-hir-type-check-base.cc (TypeCheckBase::unify_site): likewise * typecheck/rust-tyty.cc (BaseType::destructure): likewise (InferType::unify): removed old unify interface (ErrorType::unify): likewise (ADTType::unify): likewise (TupleType::unify): likewise (FnType::unify): likewise (FnPtr::unify): likewise (ClosureType::unify): likewise (ArrayType::unify): likewise (SliceType::unify): likewise (BoolType::unify): likewise (IntType::unify): likewise (UintType::unify): likewise (FloatType::unify): likewise (USizeType::unify): likewise (ISizeType::unify): likewise (CharType::unify): likewise (ReferenceType::unify): likewise (PointerType::unify): likewise (ParamType::unify): likewise (StrType::unify): likewise (NeverType::unify): likewise (PlaceholderType::unify): likewise (ProjectionType::unify): likewise (DynamicObjectType::unify): likewise * typecheck/rust-tyty.h: update destructure interface * typecheck/rust-tyty-rules.h: Removed. * typecheck/rust-unify.cc: New file. * typecheck/rust-unify.h: New file. gcc/testsuite/ChangeLog: * rust/compile/never_type_err1.rs: Moved to... * rust/compile/never_type1.rs: ...here. It now works --- gcc/rust/backend/rust-compile-expr.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-expr.cc b/gcc/rust/backend/rust-compile-expr.cc index c26f6f4..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" @@ -2006,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); -- cgit v1.1 From fa7b3a2f07f56ba7c36d854989b52183027db04e Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Sat, 4 Feb 2023 00:02:22 -0500 Subject: gccrs: Add support for TuplePattern in let statements gcc/rust/ChangeLog: * hir/tree/rust-hir-pattern.h (TuplePatternItemsRanged::get_lower_patterns): Add method. (TuplePatternItemsRanged::get_upper_patterns): Add method. * backend/rust-compile-pattern.cc (CompilePatternLet::visit): Implement TuplePattern visitor. * backend/rust-compile-pattern.h (CompilePatternLet::visit): Move TuplePattern visitor out of header file. gcc/testsuite/ChangeLog: * rust/execute/torture/let-pattern-1.rs: New test. Signed-off-by: Owen Avery --- gcc/rust/backend/rust-compile-pattern.cc | 87 ++++++++++++++++++++++++++++++++ gcc/rust/backend/rust-compile-pattern.h | 7 +-- 2 files changed, 88 insertions(+), 6 deletions(-) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-pattern.cc b/gcc/rust/backend/rust-compile-pattern.cc index bad98be..cb4b082 100644 --- a/gcc/rust/backend/rust-compile-pattern.cc +++ b/gcc/rust/backend/rust-compile-pattern.cc @@ -387,5 +387,92 @@ CompilePatternLet::visit (HIR::WildcardPattern &pattern) } } +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 (*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 (*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 ( + *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 ddf5b14..8f44b7b 100644 --- a/gcc/rust/backend/rust-compile-pattern.h +++ b/gcc/rust/backend/rust-compile-pattern.h @@ -101,6 +101,7 @@ public: 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 @@ -146,12 +147,6 @@ public: "struct pattern let statements not supported"); } - void visit (HIR::TuplePattern &pattern) override - { - rust_sorry_at (pattern.get_locus (), - "tuple pattern let statements not supported"); - } - void visit (HIR::TupleStructPattern &pattern) override { rust_sorry_at (pattern.get_locus (), -- cgit v1.1 From e81f5be60d1ff504f9b1b2c5cfad0a808bec4ff7 Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Fri, 3 Feb 2023 10:19:32 -0500 Subject: gccrs: Simplify WildcardPattern let statement handling gcc/rust/ChangeLog: * backend/rust-compile-pattern.cc (CompilePatternLet::visit): Simplify WildcardPattern compilation for let statements. * backend/rust-compile-var-decl.h: (CompileVarDecl::visit): Remove variable declaration for WildcardPattern. * resolve/rust-ast-resolve-pattern.h: (PatternDeclaration::visit): Remove name resolution for WildcardPattern. Signed-off-by: Owen Avery --- gcc/rust/backend/rust-compile-pattern.cc | 28 ++++++---------------------- gcc/rust/backend/rust-compile-var-decl.h | 16 +--------------- 2 files changed, 7 insertions(+), 37 deletions(-) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-pattern.cc b/gcc/rust/backend/rust-compile-pattern.cc index cb4b082..d98f7be 100644 --- a/gcc/rust/backend/rust-compile-pattern.cc +++ b/gcc/rust/backend/rust-compile-pattern.cc @@ -361,30 +361,14 @@ CompilePatternLet::visit (HIR::IdentifierPattern &pattern) void CompilePatternLet::visit (HIR::WildcardPattern &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 init_stmt = NULL; + tree stmt_type = TyTyResolveCompile::compile (ctx, ty); - 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); - 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); - } + ctx->add_statement (init_stmt); } void diff --git a/gcc/rust/backend/rust-compile-var-decl.h b/gcc/rust/backend/rust-compile-var-decl.h index 9e07999..13b429d 100644 --- a/gcc/rust/backend/rust-compile-var-decl.h +++ b/gcc/rust/backend/rust-compile-var-decl.h @@ -53,21 +53,6 @@ public: locals.push_back (var); } - void visit (HIR::WildcardPattern &pattern) override - { - translated_type = ctx->get_backend ()->immutable_type (translated_type); - - Bvariable *var - = 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, var); - - locals.push_back (var); - } - // Empty visit for unused Pattern HIR nodes. void visit (HIR::LiteralPattern &) override {} void visit (HIR::PathInExpression &) override {} @@ -78,6 +63,7 @@ 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, -- cgit v1.1