diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-06-25 12:53:24 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-25 12:53:24 +0000 |
commit | 99bc27d278359be9aec5597504807a6456a88a6c (patch) | |
tree | d8f8f094efbc8806111f4ee9b450792853852a48 /gcc/rust/backend | |
parent | 08e3ae7fce360db23bceb45c1b16718ee21deb91 (diff) | |
parent | f089723609077cd38821c15ad042a45936f949e6 (diff) | |
download | gcc-99bc27d278359be9aec5597504807a6456a88a6c.zip gcc-99bc27d278359be9aec5597504807a6456a88a6c.tar.gz gcc-99bc27d278359be9aec5597504807a6456a88a6c.tar.bz2 |
Merge #526
526: Add unit-type support for Identifier expressions r=philberty a=philberty
Unit type can be reference via an identifier expression. Other type references via an identifier expression are not supported yet.
Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Diffstat (limited to 'gcc/rust/backend')
-rw-r--r-- | gcc/rust/backend/rust-compile-expr.h | 87 |
1 files changed, 75 insertions, 12 deletions
diff --git a/gcc/rust/backend/rust-compile-expr.h b/gcc/rust/backend/rust-compile-expr.h index 8391bc4..fc8ad81 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)) @@ -478,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; |