diff options
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/ast/rust-item.h | 2 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-context.h | 16 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.h | 5 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-item.h | 19 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-item.h | 26 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-item.h | 6 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-item.h | 6 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-toplevel.h | 9 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-toplevel.h | 10 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-type.h | 3 | ||||
-rw-r--r-- | gcc/testsuite/rust.test/compilable/constant1.rs | 8 |
11 files changed, 109 insertions, 1 deletions
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 4b3a2d5..1088842 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -2531,6 +2531,8 @@ public: return type; } + std::string get_identifier () const { return identifier; } + protected: /* Use covariance to implement clone function as returning this object * rather than base */ diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h index 5736bf2..288dbda 100644 --- a/gcc/rust/backend/rust-compile-context.h +++ b/gcc/rust/backend/rust-compile-context.h @@ -141,6 +141,21 @@ public: return true; } + void insert_const_decl (HirId id, ::Bexpression *expr) + { + compiled_consts[id] = expr; + } + + bool lookup_const_decl (HirId id, ::Bexpression **expr) + { + auto it = compiled_consts.find (id); + if (it == compiled_consts.end ()) + return false; + + *expr = it->second; + return true; + } + void push_fn (::Bfunction *fn, ::Bvariable *ret_addr) { fn_stack.push_back (fncontext{fn, ret_addr}); @@ -183,6 +198,7 @@ private: std::map<HirId, ::Bvariable *> compiled_var_decls; std::map<HirId, ::Btype *> compiled_type_map; std::map<HirId, ::Bfunction *> compiled_fn_map; + std::map<HirId, ::Bexpression *> compiled_consts; std::vector< ::std::vector<Bstatement *> > statements; std::vector< ::Bblock *> scope_stack; diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index 5c3206a..871b4ba 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -105,6 +105,11 @@ public: return; } + // this could be a constant reference + if (ctx->lookup_const_decl (ref, &translated)) + return; + + // must be an identifier Bvariable *var = nullptr; if (!ctx->lookup_var_decl (ref, &var)) { diff --git a/gcc/rust/backend/rust-compile-item.h b/gcc/rust/backend/rust-compile-item.h index f131a89..10bdce0 100644 --- a/gcc/rust/backend/rust-compile-item.h +++ b/gcc/rust/backend/rust-compile-item.h @@ -23,6 +23,7 @@ #include "rust-compile-tyty.h" #include "rust-compile-var-decl.h" #include "rust-compile-stmt.h" +#include "rust-compile-expr.h" namespace Rust { namespace Compile { @@ -38,6 +39,24 @@ public: virtual ~CompileItem () {} + void visit (HIR::ConstantItem &constant) + { + TyTy::TyBase *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); + + Bexpression *const_expr = ctx->get_backend ()->named_constant_expression ( + type, constant.get_identifier (), value, constant.get_locus ()); + + ctx->push_const (const_expr); + ctx->insert_const_decl (constant.get_mappings ().get_hirid (), const_expr); + } + void visit (HIR::Function &function) { // items can be forward compiled which means we may not need to invoke this diff --git a/gcc/rust/hir/rust-ast-lower-item.h b/gcc/rust/hir/rust-ast-lower-item.h index f57ac86..9722390 100644 --- a/gcc/rust/hir/rust-ast-lower-item.h +++ b/gcc/rust/hir/rust-ast-lower-item.h @@ -24,6 +24,7 @@ #include "rust-ast-lower-base.h" #include "rust-ast-lower-type.h" #include "rust-ast-lower-stmt.h" +#include "rust-ast-lower-expr.h" #include "rust-ast-lower-pattern.h" #include "rust-ast-lower-block.h" @@ -42,6 +43,31 @@ public: virtual ~ASTLoweringItem () {} + void visit (AST::ConstantItem &constant) + { + std::vector<HIR::Attribute> outer_attrs; + 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)); + + translated = new HIR::ConstantItem (mapping, constant.get_identifier (), + vis, std::unique_ptr<HIR::Type> (type), + std::unique_ptr<HIR::Expr> (expr), + outer_attrs, constant.get_locus ()); + + mappings->insert_defid_mapping (mapping.get_defid (), translated); + mappings->insert_hir_item (mapping.get_crate_num (), mapping.get_hirid (), + translated); + mappings->insert_location (crate_num, mapping.get_hirid (), + constant.get_locus ()); + } + void visit (AST::Function &function) { // ignore for now and leave empty diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index e0477f4..b961a6c 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -2055,6 +2055,12 @@ public: void accept_vis (HIRVisitor &vis) override; + Type *get_type () { return type.get (); } + + Expr *get_expr () { return const_expr.get (); } + + std::string get_identifier () { return identifier; } + 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 2c21a52..eca52dc 100644 --- a/gcc/rust/resolve/rust-ast-resolve-item.h +++ b/gcc/rust/resolve/rust-ast-resolve-item.h @@ -39,6 +39,12 @@ public: ~ResolveItem () {} + void visit (AST::ConstantItem &constant) + { + ResolveType::go (constant.get_type ().get (), constant.get_node_id ()); + ResolveExpr::go (constant.get_expr ().get (), constant.get_node_id ()); + } + void visit (AST::Function &function) { if (function.has_return_type ()) diff --git a/gcc/rust/resolve/rust-ast-resolve-toplevel.h b/gcc/rust/resolve/rust-ast-resolve-toplevel.h index fcc9663..0b6256e 100644 --- a/gcc/rust/resolve/rust-ast-resolve-toplevel.h +++ b/gcc/rust/resolve/rust-ast-resolve-toplevel.h @@ -36,6 +36,15 @@ public: ~ResolveTopLevel () {} + void visit (AST::ConstantItem &constant) + { + resolver->get_name_scope ().insert (constant.get_identifier (), + constant.get_node_id ()); + resolver->insert_new_definition (constant.get_node_id (), + Definition{constant.get_node_id (), + constant.get_node_id ()}); + } + void visit (AST::Function &function) { // function_names are simple std::String identifiers so this can be a diff --git a/gcc/rust/typecheck/rust-hir-type-check-toplevel.h b/gcc/rust/typecheck/rust-hir-type-check-toplevel.h index 25e6c0b..85cfe92 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-toplevel.h +++ b/gcc/rust/typecheck/rust-hir-type-check-toplevel.h @@ -22,6 +22,7 @@ #include "rust-hir-type-check-base.h" #include "rust-hir-full.h" #include "rust-hir-type-check-type.h" +#include "rust-hir-type-check-expr.h" #include "rust-tyty.h" namespace Rust { @@ -36,6 +37,15 @@ public: item->accept_vis (resolver); } + void visit (HIR::ConstantItem &constant) + { + TyTy::TyBase *type = TypeCheckType::Resolve (constant.get_type ()); + TyTy::TyBase *expr_type = TypeCheckExpr::Resolve (constant.get_expr ()); + + context->insert_type (constant.get_mappings ().get_hirid (), + type->combine (expr_type)); + } + void visit (HIR::Function &function) { TyTy::TyBase *ret_type = nullptr; diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.h b/gcc/rust/typecheck/rust-hir-type-check-type.h index 21d6c23..8ff4c44 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-type.h +++ b/gcc/rust/typecheck/rust-hir-type-check-type.h @@ -87,7 +87,8 @@ public: if (!resolver->lookup_resolved_type (path.get_mappings ().get_nodeid (), &ref)) { - rust_error_at (path.get_locus (), "Type was not resolved"); + rust_fatal_error (path.get_locus (), + "Failed to resolve node id to HIR"); return; } diff --git a/gcc/testsuite/rust.test/compilable/constant1.rs b/gcc/testsuite/rust.test/compilable/constant1.rs new file mode 100644 index 0000000..e8ef96b --- /dev/null +++ b/gcc/testsuite/rust.test/compilable/constant1.rs @@ -0,0 +1,8 @@ +const TEST_CONST:i32 = 10; + +fn main() { + let mut x = TEST_CONST; + x = x + 1; + + let mut y = x + TEST_CONST; +} |