aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-10-28 10:13:12 +0000
committerGitHub <noreply@github.com>2021-10-28 10:13:12 +0000
commita2ce3ef6b024736dfcbfabb062100c685fa95c6b (patch)
tree41e1e2c804b45cf8d841456e3176b128b218362d
parente28c43f2f40cf405e89b3892aa65f6a06fa1c802 (diff)
parent0114e3e5b3b222c190a640bdeda8e781042389c4 (diff)
downloadgcc-a2ce3ef6b024736dfcbfabb062100c685fa95c6b.zip
gcc-a2ce3ef6b024736dfcbfabb062100c685fa95c6b.tar.gz
gcc-a2ce3ef6b024736dfcbfabb062100c685fa95c6b.tar.bz2
Merge #775
775: Support const within block-expr's r=philberty a=philberty This adds support for constant folding within block expr's and constant ArrayIndexExpression. Fixes #711 Co-authored-by: Philip Herron <philip.herron@embecosm.com>
-rw-r--r--gcc/rust/backend/rust-compile-stmt.h27
-rw-r--r--gcc/rust/hir/rust-ast-lower-stmt.h28
-rw-r--r--gcc/rust/resolve/rust-ast-resolve-stmt.h24
-rw-r--r--gcc/rust/typecheck/rust-hir-const-fold.h9
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-stmt.h13
-rw-r--r--gcc/testsuite/rust/compile/torture/constant2.rs6
-rw-r--r--gcc/testsuite/rust/compile/torture/constant3.rs10
7 files changed, 117 insertions, 0 deletions
diff --git a/gcc/rust/backend/rust-compile-stmt.h b/gcc/rust/backend/rust-compile-stmt.h
index 1a2f02c..21e5814 100644
--- a/gcc/rust/backend/rust-compile-stmt.h
+++ b/gcc/rust/backend/rust-compile-stmt.h
@@ -48,6 +48,33 @@ public:
translated = CompileExpr::Compile (stmt.get_expr (), ctx);
}
+ void visit (HIR::ConstantItem &constant) override
+ {
+ TyTy::BaseType *resolved_type = nullptr;
+ bool ok
+ = ctx->get_tyctx ()->lookup_type (constant.get_mappings ().get_hirid (),
+ &resolved_type);
+ rust_assert (ok);
+
+ ::Btype *type = TyTyResolveCompile::compile (ctx, resolved_type);
+ Bexpression *value = CompileExpr::Compile (constant.get_expr (), ctx);
+
+ const Resolver::CanonicalPath *canonical_path = nullptr;
+ rust_assert (ctx->get_mappings ()->lookup_canonical_path (
+ constant.get_mappings ().get_crate_num (),
+ constant.get_mappings ().get_nodeid (), &canonical_path));
+
+ std::string ident = canonical_path->get ();
+ Bexpression *const_expr
+ = ctx->get_backend ()->named_constant_expression (type, ident, value,
+ constant.get_locus ());
+
+ ctx->push_const (const_expr);
+ ctx->insert_const_decl (constant.get_mappings ().get_hirid (), const_expr);
+
+ translated = const_expr;
+ }
+
void visit (HIR::LetStmt &stmt) override
{
// nothing to do
diff --git a/gcc/rust/hir/rust-ast-lower-stmt.h b/gcc/rust/hir/rust-ast-lower-stmt.h
index 50ccc9f..eab0922 100644
--- a/gcc/rust/hir/rust-ast-lower-stmt.h
+++ b/gcc/rust/hir/rust-ast-lower-stmt.h
@@ -83,6 +83,34 @@ public:
mappings->insert_hir_stmt (crate_num, mapping.get_hirid (), translated);
}
+ void visit (AST::ConstantItem &constant) override
+ {
+ HIR::Visibility vis = HIR::Visibility::create_public ();
+
+ HIR::Type *type = ASTLoweringType::translate (constant.get_type ().get ());
+ HIR::Expr *expr = ASTLoweringExpr::translate (constant.get_expr ().get ());
+
+ auto crate_num = mappings->get_current_crate ();
+ Analysis::NodeMapping mapping (crate_num, constant.get_node_id (),
+ mappings->get_next_hir_id (crate_num),
+ mappings->get_next_localdef_id (crate_num));
+
+ HIR::ConstantItem *constant_item
+ = new HIR::ConstantItem (mapping, constant.get_identifier (), vis,
+ std::unique_ptr<HIR::Type> (type),
+ std::unique_ptr<HIR::Expr> (expr),
+ constant.get_outer_attrs (),
+ constant.get_locus ());
+ translated = constant_item;
+
+ mappings->insert_hir_item (mapping.get_crate_num (), mapping.get_hirid (),
+ constant_item);
+ mappings->insert_hir_stmt (mapping.get_crate_num (), mapping.get_hirid (),
+ constant_item);
+ mappings->insert_location (crate_num, mapping.get_hirid (),
+ constant.get_locus ());
+ }
+
void visit (AST::LetStmt &stmt) override
{
HIR::Pattern *variables
diff --git a/gcc/rust/resolve/rust-ast-resolve-stmt.h b/gcc/rust/resolve/rust-ast-resolve-stmt.h
index 3a0904e..98fcaf2 100644
--- a/gcc/rust/resolve/rust-ast-resolve-stmt.h
+++ b/gcc/rust/resolve/rust-ast-resolve-stmt.h
@@ -51,6 +51,30 @@ public:
ResolveExpr::go (stmt.get_expr ().get (), stmt.get_node_id ());
}
+ void visit (AST::ConstantItem &constant) override
+ {
+ auto path = ResolveConstantItemToCanonicalPath::resolve (constant);
+ resolver->get_name_scope ().insert (
+ path, constant.get_node_id (), constant.get_locus (), false,
+ [&] (const CanonicalPath &, NodeId, Location locus) -> void {
+ RichLocation r (constant.get_locus ());
+ r.add_range (locus);
+ rust_error_at (r, "redefined multiple times");
+ });
+ resolver->insert_new_definition (constant.get_node_id (),
+ Definition{constant.get_node_id (),
+ constant.get_node_id ()});
+
+ ResolveType::go (constant.get_type ().get (), constant.get_node_id ());
+ ResolveExpr::go (constant.get_expr ().get (), constant.get_node_id ());
+
+ // the mutability checker needs to verify for immutable decls the number
+ // of assignments are <1. This marks an implicit assignment
+ resolver->mark_decl_mutability (constant.get_node_id (), false);
+ resolver->mark_assignment_to_decl (constant.get_node_id (),
+ constant.get_node_id ());
+ }
+
void visit (AST::LetStmt &stmt) override
{
if (stmt.has_init_expr ())
diff --git a/gcc/rust/typecheck/rust-hir-const-fold.h b/gcc/rust/typecheck/rust-hir-const-fold.h
index b61ce7b..2bf0f2c 100644
--- a/gcc/rust/typecheck/rust-hir-const-fold.h
+++ b/gcc/rust/typecheck/rust-hir-const-fold.h
@@ -427,6 +427,15 @@ public:
= ctx->get_backend ()->negation_expression (op, negated_expr, location);
}
+ void visit (HIR::ArrayIndexExpr &expr) override
+ {
+ Bexpression *array = ConstFoldExpr::fold (expr.get_array_expr ());
+ Bexpression *index = ConstFoldExpr::fold (expr.get_index_expr ());
+
+ folded = ctx->get_backend ()->array_index_expression (array, index,
+ expr.get_locus ());
+ }
+
private:
ConstFoldExpr ()
: ConstFoldBase (), folded (ctx->get_backend ()->error_expression ())
diff --git a/gcc/rust/typecheck/rust-hir-type-check-stmt.h b/gcc/rust/typecheck/rust-hir-type-check-stmt.h
index 17def2b..74bc037 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-stmt.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-stmt.h
@@ -55,6 +55,19 @@ public:
= TyTy::TupleType::get_unit_type (stmt.get_mappings ().get_hirid ());
}
+ void visit (HIR::ConstantItem &constant) override
+ {
+ TyTy::BaseType *type = TypeCheckType::Resolve (constant.get_type ());
+ TyTy::BaseType *expr_type
+ = TypeCheckExpr::Resolve (constant.get_expr (), false);
+
+ infered = type->unify (expr_type);
+ context->insert_type (constant.get_mappings (), infered);
+
+ // notify the constant folder of this
+ ConstFold::ConstFoldItem::fold (constant);
+ }
+
void visit (HIR::LetStmt &stmt) override
{
infered = new TyTy::TupleType (stmt.get_mappings ().get_hirid ());
diff --git a/gcc/testsuite/rust/compile/torture/constant2.rs b/gcc/testsuite/rust/compile/torture/constant2.rs
new file mode 100644
index 0000000..d06324e
--- /dev/null
+++ b/gcc/testsuite/rust/compile/torture/constant2.rs
@@ -0,0 +1,6 @@
+fn main() {
+ const C: usize = 42;
+
+ let _a = C;
+ let _b: [i32; C] = [0; C];
+}
diff --git a/gcc/testsuite/rust/compile/torture/constant3.rs b/gcc/testsuite/rust/compile/torture/constant3.rs
new file mode 100644
index 0000000..d2f1dd5
--- /dev/null
+++ b/gcc/testsuite/rust/compile/torture/constant3.rs
@@ -0,0 +1,10 @@
+fn main() {
+ const A: [i32; 3] = [1, 2, 3];
+ const B: i32 = A[1];
+ const C: usize = 42;
+ const D: i32 = 7;
+
+ let _a = C;
+ let _b: [i32; C] = [0; C];
+ let _c = B + D;
+}