From 3a10d1f15969f94c3d3bc836db1d75dd0f914aa9 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Fri, 18 Dec 2020 16:17:47 +0000 Subject: Add type unification as part of typecheck. Rust must examine each usage of a name and unify their types. For example: let mut x; x = 1 This means the declaration is determined to be an inference variable then the assignment can be resolved to an Integer TyTy which can be combined as part of the rules to now make the let x decl be an i32. --- gcc/rust/backend/rust-compile-context.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-context.h b/gcc/rust/backend/rust-compile-context.h index f890678..1924b52 100644 --- a/gcc/rust/backend/rust-compile-context.h +++ b/gcc/rust/backend/rust-compile-context.h @@ -205,9 +205,13 @@ public: virtual ~TyTyResolveCompile () {} - void visit (TyTy::FnType &type) { gcc_unreachable (); } + void visit (TyTy::UnitType &type) override { gcc_unreachable (); } - void visit (TyTy::BoolType &type) + void visit (TyTy::InferType &type) override { gcc_unreachable (); } + + void visit (TyTy::FnType &type) override { gcc_unreachable (); } + + void visit (TyTy::BoolType &type) override { ::Btype *compiled_type = nullptr; bool ok = ctx->lookup_compiled_types (type.get_ref (), &compiled_type); @@ -215,7 +219,7 @@ public: translated = compiled_type; } - void visit (TyTy::IntType &type) + void visit (TyTy::IntType &type) override { printf ("type [%s] has ref: %u\n", type.as_string ().c_str (), type.get_ref ()); @@ -226,7 +230,7 @@ public: translated = compiled_type; } - void visit (TyTy::UintType &type) + void visit (TyTy::UintType &type) override { ::Btype *compiled_type = nullptr; bool ok = ctx->lookup_compiled_types (type.get_ref (), &compiled_type); -- cgit v1.1 From 671cefe61254bedd4a9f526f8912fe68368cbe72 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Fri, 18 Dec 2020 17:27:03 +0000 Subject: When compiling a block it might reference a GIMPLE node that is not resolved yet. Such as: fn main() -> i32 { call() } fn call() -> i32 { return 1; } The compilation pass acts on the first function main and detects the other node call is not compiled yet we can go resolve it. This is a toplevel item since it has a local_def_id and therefore it has a NULL enclosing scope for the block. Fixes #72 --- gcc/rust/backend/rust-compile-item.h | 13 ++++++------- gcc/rust/backend/rust-compile-resolve-path.cc | 7 ------- 2 files changed, 6 insertions(+), 14 deletions(-) (limited to 'gcc/rust/backend') diff --git a/gcc/rust/backend/rust-compile-item.h b/gcc/rust/backend/rust-compile-item.h index dd07435..f131a89 100644 --- a/gcc/rust/backend/rust-compile-item.h +++ b/gcc/rust/backend/rust-compile-item.h @@ -48,11 +48,7 @@ public: { // has this been added to the list then it must be finished if (ctx->function_completed (lookup)) - { - printf ("returning early the function [%s] is completed!\n", - function.as_string ().c_str ()); - return; - } + return; } TyTy::TyBase *fnType; @@ -123,9 +119,12 @@ public: return true; }); - Bblock *enclosing_scope = ctx->peek_enclosing_scope (); - HIR::BlockExpr *function_body = function.function_body.get (); + bool toplevel_item + = function.get_mappings ().get_local_defid () != UNKNOWN_LOCAL_DEFID; + Bblock *enclosing_scope + = toplevel_item ? NULL : ctx->peek_enclosing_scope (); + HIR::BlockExpr *function_body = function.function_body.get (); Location start_location = function_body->get_locus (); Location end_location = function_body->get_closing_locus (); diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc index e6683fa..c5c646d 100644 --- a/gcc/rust/backend/rust-compile-resolve-path.cc +++ b/gcc/rust/backend/rust-compile-resolve-path.cc @@ -36,10 +36,6 @@ ResolvePath::visit (HIR::PathInExpression &expr) return; } - printf ("PATHIN have ast node id %u ref %u for expr [%s]\n", - expr.get_mappings ().get_nodeid (), ref_node_id, - expr.as_string ().c_str ()); - HirId ref; if (!ctx->get_mappings ()->lookup_node_to_hir ( expr.get_mappings ().get_crate_num (), ref_node_id, &ref)) @@ -52,9 +48,6 @@ ResolvePath::visit (HIR::PathInExpression &expr) Bfunction *fn; if (!ctx->lookup_function_decl (ref, &fn)) { - printf ( - "path failed to lookup function attempting to forward resolve!\n"); - // this might fail because its a forward decl so we can attempt to // resolve it now HIR::Item *resolved_item = ctx->get_mappings ()->lookup_hir_item ( -- cgit v1.1