diff options
-rw-r--r-- | gcc/rust/backend/rust-compile-resolve-path.cc | 29 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile-resolve-path.h | 20 | ||||
-rw-r--r-- | gcc/rust/backend/rust-compile.cc | 18 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-item.h | 14 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-expr.h | 25 | ||||
-rw-r--r-- | gcc/testsuite/rust.test/compilable/generics2.rs | 35 |
6 files changed, 89 insertions, 52 deletions
diff --git a/gcc/rust/backend/rust-compile-resolve-path.cc b/gcc/rust/backend/rust-compile-resolve-path.cc index ffc2655..1a798ee 100644 --- a/gcc/rust/backend/rust-compile-resolve-path.cc +++ b/gcc/rust/backend/rust-compile-resolve-path.cc @@ -93,34 +93,5 @@ ResolvePathRef::visit (HIR::PathInExpression &expr) = ctx->get_backend ()->function_code_expression (fn, expr.get_locus ()); } -void -ResolvePathType::visit (HIR::PathInExpression &expr) -{ - // need to look up the reference for this identifier - NodeId ref_node_id; - if (!ctx->get_resolver ()->lookup_resolved_type ( - expr.get_mappings ().get_nodeid (), &ref_node_id)) - { - return; - } - - HirId ref; - if (!ctx->get_mappings ()->lookup_node_to_hir ( - expr.get_mappings ().get_crate_num (), ref_node_id, &ref)) - { - rust_error_at (expr.get_locus (), "reverse lookup failure"); - return; - } - - TyTy::BaseType *tyty = nullptr; - if (!ctx->get_tyctx ()->lookup_type (ref, &tyty)) - { - rust_error_at (expr.get_locus (), "unknown type"); - return; - } - - resolved = TyTyResolveCompile::compile (ctx, tyty); -} - } // namespace Compile } // namespace Rust diff --git a/gcc/rust/backend/rust-compile-resolve-path.h b/gcc/rust/backend/rust-compile-resolve-path.h index 2f3cb68..ae469e6 100644 --- a/gcc/rust/backend/rust-compile-resolve-path.h +++ b/gcc/rust/backend/rust-compile-resolve-path.h @@ -35,7 +35,7 @@ public: return resolver.resolved; } - void visit (HIR::PathInExpression &expr); + void visit (HIR::PathInExpression &expr) override; private: ResolvePathRef (Context *ctx) : HIRCompileBase (ctx), resolved (nullptr) {} @@ -43,24 +43,6 @@ private: Bexpression *resolved; }; -class ResolvePathType : public HIRCompileBase -{ -public: - static Btype *Compile (HIR::Expr *expr, Context *ctx) - { - ResolvePathType resolver (ctx); - expr->accept_vis (resolver); - return resolver.resolved; - } - - void visit (HIR::PathInExpression &expr); - -private: - ResolvePathType (Context *ctx) : HIRCompileBase (ctx), resolved (nullptr) {} - - Btype *resolved; -}; - } // namespace Compile } // namespace Rust diff --git a/gcc/rust/backend/rust-compile.cc b/gcc/rust/backend/rust-compile.cc index 61bacf3..2c83527 100644 --- a/gcc/rust/backend/rust-compile.cc +++ b/gcc/rust/backend/rust-compile.cc @@ -53,11 +53,21 @@ CompileCrate::go () void CompileExpr::visit (HIR::CallExpr &expr) { - Btype *type = ResolvePathType::Compile (expr.get_fnexpr (), ctx); - if (type != nullptr) + TyTy::BaseType *tyty = nullptr; + if (!ctx->get_tyctx ()->lookup_type ( + expr.get_fnexpr ()->get_mappings ().get_hirid (), &tyty)) { - // this assumes all fields are in order from type resolution and if a base - // struct was specified those fields are filed via accesors + rust_error_at (expr.get_locus (), "unknown type"); + return; + } + + // must be a tuple constructor + if (tyty->get_kind () != TyTy::TypeKind::FNDEF) + { + Btype *type = TyTyResolveCompile::compile (ctx, tyty); + + // this assumes all fields are in order from type resolution and if a + // base struct was specified those fields are filed via accesors std::vector<Bexpression *> vals; expr.iterate_params ([&] (HIR::Expr *argument) mutable -> bool { Bexpression *e = CompileExpr::Compile (argument, ctx); diff --git a/gcc/rust/resolve/rust-ast-resolve-item.h b/gcc/rust/resolve/rust-ast-resolve-item.h index 02d864f..3e279b5 100644 --- a/gcc/rust/resolve/rust-ast-resolve-item.h +++ b/gcc/rust/resolve/rust-ast-resolve-item.h @@ -39,11 +39,25 @@ public: void visit (AST::TupleStruct &struct_decl) { + NodeId scope_node_id = struct_decl.get_node_id (); + resolver->get_type_scope ().push (scope_node_id); + + if (struct_decl.has_generics ()) + { + for (auto &generic : struct_decl.get_generic_params ()) + { + ResolveGenericParam::go (generic.get (), + struct_decl.get_node_id ()); + } + } + struct_decl.iterate ([&] (AST::TupleField &field) mutable -> bool { ResolveType::go (field.get_field_type ().get (), struct_decl.get_node_id ()); return true; }); + + resolver->get_type_scope ().pop (); } void visit (AST::StructStruct &struct_decl) diff --git a/gcc/rust/typecheck/rust-hir-type-check-expr.h b/gcc/rust/typecheck/rust-hir-type-check-expr.h index c9fe26e..bcc8598 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-expr.h +++ b/gcc/rust/typecheck/rust-hir-type-check-expr.h @@ -755,6 +755,31 @@ public: "failed to resolve PathInExpression type"); return; } + + HIR::PathExprSegment seg = expr.get_final_segment (); + if (!infered->supports_substitions () && seg.has_generic_args ()) + { + rust_error_at (expr.get_locus (), + "path does not support substitutions"); + return; + } + + if (infered->has_subsititions_defined ()) + { + if (infered->get_kind () != TyTy::TypeKind::ADT) + { + rust_error_at (expr.get_locus (), + "substitutions only support on ADT types so far"); + return; + } + + TyTy::ADTType *adt = static_cast<TyTy::ADTType *> (infered); + infered = seg.has_generic_args () + ? adt->handle_substitions (seg.get_generic_args ()) + : adt->infer_substitions (); + } + + context->insert_type (expr.get_mappings (), infered->clone ()); } void visit (HIR::LoopExpr &expr) diff --git a/gcc/testsuite/rust.test/compilable/generics2.rs b/gcc/testsuite/rust.test/compilable/generics2.rs new file mode 100644 index 0000000..ee759dd --- /dev/null +++ b/gcc/testsuite/rust.test/compilable/generics2.rs @@ -0,0 +1,35 @@ +struct Foo(f32, bool); + +struct GenericStruct<T>(T, usize); + +fn main() { + let a1; + a1 = Foo(1.0, false); + + let b1: f32 = a1.0; + let c1: bool = a1.1; + + let a2: GenericStruct<i8>; + a2 = GenericStruct::<i8>(1, 456); + + let b2: i8 = a2.0; + let c2: usize = a2.1; + + let a3; + a3 = GenericStruct::<i32>(123, 456); + + let b3: i32 = a3.0; + let c3: usize = a3.1; + + let a4; + a4 = GenericStruct(1.0, 456); + + let b4: f32 = a4.0; + let c4: usize = a4.1; + + let a5; + a5 = GenericStruct::<_>(true, 456); + + let b5: bool = a5.0; + let c5: usize = a5.1; +} |