From cb0db310b3dceff7a3ea6e05c002a3ab1239ac4a Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Fri, 25 Jun 2021 12:28:22 +0100 Subject: Add support for unit-struct IdentifierExpr's can reference types like a unit-struct, referencing other structs actually form a function item type which is not supported yet. --- gcc/rust/backend/rust-compile-expr.h | 73 ++++++++++++++++++++++++++++++------ 1 file changed, 61 insertions(+), 12 deletions(-) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index 8391bc4..e92c7cd 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -109,32 +109,81 @@ public: void visit (HIR::IdentifierExpr &expr) override { - // need to look up the reference for this identifier - NodeId ref_node_id; - if (!ctx->get_resolver ()->lookup_resolved_name ( - expr.get_mappings ().get_nodeid (), &ref_node_id)) + NodeId ast_node_id = expr.get_mappings ().get_nodeid (); + + bool is_value = false; + NodeId ref_node_id = UNKNOWN_NODEID; + if (ctx->get_resolver ()->lookup_resolved_name (ast_node_id, &ref_node_id)) + { + // these ref_node_ids will resolve to a pattern declaration but we are + // interested in the definition that this refers to get the parent id + Resolver::Definition def; + if (!ctx->get_resolver ()->lookup_definition (ref_node_id, &def)) + { + rust_error_at (expr.get_locus (), + "unknown reference for resolved name"); + return; + } + ref_node_id = def.parent; + is_value = true; + } + else if (!ctx->get_resolver ()->lookup_resolved_type (ast_node_id, + &ref_node_id)) { - rust_fatal_error (expr.get_locus (), "failed to look up resolved name"); + rust_error_at (expr.get_locus (), + "Failed to lookup type reference for node: %s", + expr.as_string ().c_str ()); return; } - // these ref_node_ids will resolve to a pattern declaration but we are - // interested in the definition that this refers to get the parent id - Resolver::Definition def; - if (!ctx->get_resolver ()->lookup_definition (ref_node_id, &def)) + if (ref_node_id == UNKNOWN_NODEID) { - rust_error_at (expr.get_locus (), "unknown reference"); + rust_fatal_error (expr.get_locus (), "unresolved IdentifierExpr: %s", + expr.as_string ().c_str ()); return; } + // node back to HIR HirId ref; if (!ctx->get_mappings ()->lookup_node_to_hir ( - expr.get_mappings ().get_crate_num (), def.parent, &ref)) + expr.get_mappings ().get_crate_num (), ref_node_id, &ref)) + { + rust_error_at (expr.get_locus (), "reverse lookup failure"); + return; + } + + TyTy::BaseType *lookup = nullptr; + if (!ctx->get_tyctx ()->lookup_type (ref, &lookup)) { - rust_fatal_error (expr.get_locus (), "reverse lookup failure"); + rust_fatal_error (expr.get_locus (), + "failed to find type relevant to this context: %s", + expr.get_mappings ().as_string ().c_str ()); return; } + bool is_type_ref = !is_value; + if (is_type_ref) + { + // this might be a case for + // + // struct S; + // + // fn main() { + // let s = S; + // } + + if (lookup->is_unit ()) + { + translated = ctx->get_backend ()->unit_expression (); + return; + } + + // rust actually treats like this an fn call or structs with fields but + // unit structs are just the struct name lets catch it with an is-unit + // check + gcc_unreachable (); + } + Bfunction *fn = nullptr; Bvariable *var = nullptr; if (ctx->lookup_const_decl (ref, &translated)) -- cgit v1.1 From f089723609077cd38821c15ad042a45936f949e6 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Fri, 25 Jun 2021 13:51:50 +0100 Subject: Add support for empty struct initilizer Rust supports unit struct initilization such as S{} this takes this tree and resolves it fully. This should really be all desugared via HIR but that is in progress in another PR. --- gcc/rust/backend/rust-compile-expr.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index e92c7cd..fc8ad81 100644 --- a/gcc/rust/backend/rust-compile-expr.h +++ b/gcc/rust/backend/rust-compile-expr.h @@ -527,6 +527,20 @@ public: } } + void visit (HIR::StructExprStruct &struct_expr) override + { + TyTy::BaseType *tyty = nullptr; + if (!ctx->get_tyctx ()->lookup_type ( + struct_expr.get_mappings ().get_hirid (), &tyty)) + { + rust_error_at (struct_expr.get_locus (), "unknown type"); + return; + } + + rust_assert (tyty->is_unit ()); + translated = ctx->get_backend ()->unit_expression (); + } + void visit (HIR::StructExprStructFields &struct_expr) override { TyTy::BaseType *tyty = nullptr; -- cgit v1.1