diff options
author | Marc Poulhiès <dkm@kataplop.net> | 2021-08-12 13:45:15 +0200 |
---|---|---|
committer | Marc <dkm@kataplop.net> | 2021-08-18 22:59:48 +0200 |
commit | ed9a473c741f85685e8d9160155c16a92c010ca7 (patch) | |
tree | 9e1a7ec2b211376e6caf64c112c47da5acb3cb2d /gcc | |
parent | fd1d9c1af7c7afa43ebac162f9427420ea2314ff (diff) | |
download | gcc-ed9a473c741f85685e8d9160155c16a92c010ca7.zip gcc-ed9a473c741f85685e8d9160155c16a92c010ca7.tar.gz gcc-ed9a473c741f85685e8d9160155c16a92c010ca7.tar.bz2 |
typecheck + backend: add module support
Typechecking and backend for Modules.
ref #432
Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/backend/rust-compile-item.h | 6 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile.cc | 1 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-expr.h | 51 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-item.h | 6 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-toplevel.h | 6 |
5 files changed, 51 insertions, 19 deletions
diff --git a/gcc/rust/backend/rust-compile-item.h b/gcc/rust/backend/rust-compile-item.h index 719d51c..8b36289 100644 --- a/gcc/rust/backend/rust-compile-item.h +++ b/gcc/rust/backend/rust-compile-item.h @@ -301,6 +301,12 @@ public: } } + void visit (HIR::ModuleBodied &module) override + { + for (auto &item : module.get_items ()) + CompileItem::compile (item.get (), ctx, compile_fns); + } + private: CompileItem (Context *ctx, bool compile_fns, TyTy::BaseType *concrete) : HIRCompileBase (ctx), compile_fns (compile_fns), concrete (concrete) diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index 3c6a188..b9b5084 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -88,6 +88,7 @@ CompileExpr::visit (HIR::CallExpr &expr) { // must be a call to a function Bexpression *fn = CompileExpr::Compile (expr.get_fnexpr (), ctx); + rust_assert (fn != nullptr); std::vector<Bexpression *> args; expr.iterate_params ([&] (HIR::Expr *p) mutable -> bool { diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h index a833822..d39b2ee 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.h +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h @@ -936,6 +936,10 @@ public: if (tyseg->get_kind () == TyTy::TypeKind::ERROR) return; + // this is the case where the name resolver has already fully resolved the + // name, which means all the work is already done. + bool name_resolved_fully = offset >= expr.get_num_segments (); + if (expr.get_num_segments () == 1) { Location locus = expr.get_segments ().back ().get_locus (); @@ -1052,7 +1056,7 @@ public: { rust_assert (path_resolved_id == resolved_node_id); } - else + else if (!name_resolved_fully) { resolver->insert_resolved_name (expr.get_mappings ().get_nodeid (), resolved_node_id); @@ -1214,6 +1218,8 @@ private: for (size_t i = 0; i < expr.get_num_segments (); i++) { HIR::PathExprSegment &seg = expr.get_segments ().at (i); + + bool have_more_segments = (expr.get_num_segments () - 1 != i); bool is_root = *offset == 0; NodeId ast_node_id = seg.get_mappings ().get_nodeid (); @@ -1238,6 +1244,7 @@ private: resolver->lookup_resolved_type (ast_node_id, &ref_node_id); } + // ref_node_id is the NodeId that the segments refers to. if (ref_node_id == UNKNOWN_NODEID) { if (is_root) @@ -1265,27 +1272,33 @@ private: return new TyTy::ErrorType (expr.get_mappings ().get_hirid ()); } + return root_tyty; } - // FIXME - // modules are not going to have an explicit TyTy.In this case we - // can probably do some kind of check. By looking up if the HirId ref - // node is a module and continue. If the path expression is single - // segment of module we can error with expected value but found module - // or something. - // - // Something like this - // - // bool seg_is_module = mappings->lookup_module (ref); - // if (seg_is_module) - // { - // if (have_more_segments) - // continue; - // - // rust_error_at (seg.get_locus (), "expected value"); - // return new TyTy::ErrorType (expr.get_mappings ().get_hirid ()); - // } + auto seg_is_module + = (nullptr + != mappings->lookup_module (expr.get_mappings ().get_crate_num (), + ref)); + + if (seg_is_module) + { + // A::B::C::this_is_a_module::D::E::F + // ^^^^^^^^^^^^^^^^ + // Currently handling this. + if (have_more_segments) + { + (*offset)++; + continue; + } + + // In the case of : + // A::B::C::this_is_a_module + // ^^^^^^^^^^^^^^^^ + // This is an error, we are not expecting a module. + rust_error_at (seg.get_locus (), "expected value"); + return new TyTy::ErrorType (expr.get_mappings ().get_hirid ()); + } TyTy::BaseType *lookup = nullptr; if (!context->lookup_type (ref, &lookup)) diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.h b/gcc/rust/typecheck/rust-hir-type-check-item.h index 8775502..372171f 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-item.h +++ b/gcc/rust/typecheck/rust-hir-type-check-item.h @@ -156,6 +156,12 @@ public: expected_ret_tyty->unify (block_expr_ty); } + void visit (HIR::ModuleBodied &module) override + { + for (auto &item : module.get_items ()) + TypeCheckItem::Resolve (item.get ()); + } + private: TypeCheckItem () : TypeCheckBase () {} }; diff --git a/gcc/rust/typecheck/rust-hir-type-check-toplevel.h b/gcc/rust/typecheck/rust-hir-type-check-toplevel.h index 5b9757f..aad5cb9 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-toplevel.h +++ b/gcc/rust/typecheck/rust-hir-type-check-toplevel.h @@ -101,6 +101,12 @@ public: context->insert_type (struct_decl.get_mappings (), type); } + void visit (HIR::ModuleBodied &module) override + { + for (auto &item : module.get_items ()) + TypeCheckTopLevel::Resolve (item.get ()); + } + void visit (HIR::StructStruct &struct_decl) override { std::vector<TyTy::SubstitutionParamMapping> substitutions; |