From 657a9735339f6e5b0723bc24f74ad55d78daae8e Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Thu, 28 Oct 2021 10:57:37 +0100 Subject: Add support for constants within blocks BlockExpr's can contain constants these are Items which can exist within the BlockExpr Stmt list. Items like structs, functions and constants all inherit from Item so there is some duplication of code but we still do not support the forward declared Items within a stmt list so the duplication will need to be fixed as part of that bug. Fixes #711 --- gcc/rust/backend/rust-compile-stmt.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'gcc/rust/backend') 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 -- cgit v1.1