diff --git a/gcc/rust/backend/rust-compile-extern.h b/gcc/rust/backend/rust-compile-extern.h index 778553e5b373..b17ac954167c 100644 --- a/gcc/rust/backend/rust-compile-extern.h +++ b/gcc/rust/backend/rust-compile-extern.h @@ -22,6 +22,8 @@ #include "rust-compile-base.h" #include "rust-compile-intrinsic.h" #include "rust-compile-type.h" +#include "rust-diagnostics.h" +#include "rust-hir-full-decls.h" namespace Rust { namespace Compile { @@ -152,6 +154,11 @@ class CompileExternItem : public HIRCompileBase, reference = address_expression (fndecl, ref_locus); } + void visit (HIR::ExternalTypeItem &type) override + { + rust_sorry_at (type.get_locus (), "extern types are not supported yet"); + } + private: CompileExternItem (Context *ctx, TyTy::BaseType *concrete, location_t ref_locus) diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h b/gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h index d6390392d7fb..6a990e25c43c 100644 --- a/gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h +++ b/gcc/rust/checks/errors/borrowck/rust-bir-builder-struct.h @@ -189,6 +189,7 @@ class StructBuilder : public AbstractBuilder, public HIR::HIRFullVisitor void visit (HIR::ImplBlock &impl) override { rust_unreachable (); } void visit (HIR::ExternalStaticItem &item) override { rust_unreachable (); } void visit (HIR::ExternalFunctionItem &item) override { rust_unreachable (); } + void visit (HIR::ExternalTypeItem &item) override { rust_unreachable (); } void visit (HIR::ExternBlock &block) override { rust_unreachable (); } void visit (HIR::LiteralPattern &pattern) override { rust_unreachable (); } void visit (HIR::IdentifierPattern &pattern) override { rust_unreachable (); } diff --git a/gcc/rust/checks/errors/borrowck/rust-function-collector.h b/gcc/rust/checks/errors/borrowck/rust-function-collector.h index b19bfdf855e7..18f2f5e11d1f 100644 --- a/gcc/rust/checks/errors/borrowck/rust-function-collector.h +++ b/gcc/rust/checks/errors/borrowck/rust-function-collector.h @@ -155,6 +155,7 @@ class FunctionCollector : public HIR::HIRFullVisitor void visit (HIR::ImplBlock &impl) override {} void visit (HIR::ExternalStaticItem &item) override {} void visit (HIR::ExternalFunctionItem &item) override {} + void visit (HIR::ExternalTypeItem &item) override {} void visit (HIR::ExternBlock &block) override {} void visit (HIR::LiteralPattern &pattern) override {} void visit (HIR::IdentifierPattern &pattern) override {} diff --git a/gcc/rust/checks/errors/rust-const-checker.cc b/gcc/rust/checks/errors/rust-const-checker.cc index aaf8fc0c3f6b..886ae18d314d 100644 --- a/gcc/rust/checks/errors/rust-const-checker.cc +++ b/gcc/rust/checks/errors/rust-const-checker.cc @@ -714,6 +714,10 @@ void ConstChecker::visit (ExternalFunctionItem &) {} +void +ConstChecker::visit (ExternalTypeItem &) +{} + void ConstChecker::visit (ExternBlock &block) { diff --git a/gcc/rust/checks/errors/rust-const-checker.h b/gcc/rust/checks/errors/rust-const-checker.h index a645da8b7c0d..a9bf087a9935 100644 --- a/gcc/rust/checks/errors/rust-const-checker.h +++ b/gcc/rust/checks/errors/rust-const-checker.h @@ -162,6 +162,7 @@ class ConstChecker : public HIRFullVisitor virtual void visit (ImplBlock &impl) override; virtual void visit (ExternalStaticItem &item) override; virtual void visit (ExternalFunctionItem &item) override; + virtual void visit (ExternalTypeItem &item) override; virtual void visit (ExternBlock &block) override; virtual void visit (LiteralPattern &pattern) override; virtual void visit (IdentifierPattern &pattern) override; diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.cc b/gcc/rust/checks/errors/rust-unsafe-checker.cc index 4bad20b58d34..b8c25f9a5925 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.cc +++ b/gcc/rust/checks/errors/rust-unsafe-checker.cc @@ -783,6 +783,10 @@ void UnsafeChecker::visit (ExternalFunctionItem &) {} +void +UnsafeChecker::visit (ExternalTypeItem &) +{} + void UnsafeChecker::visit (ExternBlock &block) { diff --git a/gcc/rust/checks/errors/rust-unsafe-checker.h b/gcc/rust/checks/errors/rust-unsafe-checker.h index fee4d62fe43e..27659f5141a5 100644 --- a/gcc/rust/checks/errors/rust-unsafe-checker.h +++ b/gcc/rust/checks/errors/rust-unsafe-checker.h @@ -144,6 +144,7 @@ class UnsafeChecker : public HIRFullVisitor virtual void visit (ImplBlock &impl) override; virtual void visit (ExternalStaticItem &item) override; virtual void visit (ExternalFunctionItem &item) override; + virtual void visit (ExternalTypeItem &item) override; virtual void visit (ExternBlock &block) override; virtual void visit (LiteralPattern &pattern) override; virtual void visit (IdentifierPattern &pattern) override; diff --git a/gcc/rust/hir/rust-ast-lower-extern.h b/gcc/rust/hir/rust-ast-lower-extern.h index cee480bb9ed8..e495b16632d2 100644 --- a/gcc/rust/hir/rust-ast-lower-extern.h +++ b/gcc/rust/hir/rust-ast-lower-extern.h @@ -114,6 +114,11 @@ class ASTLoweringExternItem : public ASTLoweringBase function.get_outer_attrs (), function.get_locus ()); } + void visit (AST::ExternalTypeItem &type) override + { + rust_sorry_at (type.get_locus (), "extern types are not implemented yet"); + } + private: ASTLoweringExternItem () : translated (nullptr) {} diff --git a/gcc/rust/hir/rust-hir-dump.cc b/gcc/rust/hir/rust-hir-dump.cc index 2fdf769c0de2..38c2db1d6cdc 100644 --- a/gcc/rust/hir/rust-hir-dump.cc +++ b/gcc/rust/hir/rust-hir-dump.cc @@ -1999,6 +1999,16 @@ Dump::visit (ExternalFunctionItem &e) end ("ExternalFunctionItem"); } +void +Dump::visit (ExternalTypeItem &e) +{ + begin ("ExternalTypeItem"); + + do_externalitem (e); + + end ("ExternalTypeItem"); +} + void Dump::visit (ExternBlock &e) { diff --git a/gcc/rust/hir/rust-hir-dump.h b/gcc/rust/hir/rust-hir-dump.h index a48394a4bcab..6363eb4c604d 100644 --- a/gcc/rust/hir/rust-hir-dump.h +++ b/gcc/rust/hir/rust-hir-dump.h @@ -201,6 +201,7 @@ class Dump : public HIRFullVisitor virtual void visit (ExternalStaticItem &) override; virtual void visit (ExternalFunctionItem &) override; + virtual void visit (ExternalTypeItem &) override; virtual void visit (ExternBlock &) override; virtual void visit (LiteralPattern &) override; diff --git a/gcc/rust/hir/tree/rust-hir-full-decls.h b/gcc/rust/hir/tree/rust-hir-full-decls.h index 96293ce83142..d01535e75d35 100644 --- a/gcc/rust/hir/tree/rust-hir-full-decls.h +++ b/gcc/rust/hir/tree/rust-hir-full-decls.h @@ -177,6 +177,7 @@ class ExternalItem; class ExternalStaticItem; struct NamedFunctionParam; class ExternalFunctionItem; +class ExternalTypeItem; class ExternBlock; // rust-pattern.h diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index 1d067fca5bec..40093a2ad939 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -2859,6 +2859,7 @@ class ExternalItem : public Node { Static, Function, + Type, }; virtual ~ExternalItem () {} @@ -3084,11 +3085,13 @@ class ExternalFunctionItem : public ExternalItem // Copy constructor with clone ExternalFunctionItem (ExternalFunctionItem const &other) - : ExternalItem (other), return_type (other.return_type->clone_type ()), - where_clause (other.where_clause), + : ExternalItem (other), where_clause (other.where_clause), function_params (other.function_params), has_variadics (other.has_variadics) { + if (other.return_type) + return_type = other.return_type->clone_type (); + generic_params.reserve (other.generic_params.size ()); for (const auto &e : other.generic_params) generic_params.push_back (e->clone_generic_param ()); @@ -3098,11 +3101,14 @@ class ExternalFunctionItem : public ExternalItem ExternalFunctionItem &operator= (ExternalFunctionItem const &other) { ExternalItem::operator= (other); - return_type = other.return_type->clone_type (); + where_clause = other.where_clause; function_params = other.function_params; has_variadics = other.has_variadics; + if (other.return_type) + return_type = other.return_type->clone_type (); + generic_params.reserve (other.generic_params.size ()); for (const auto &e : other.generic_params) generic_params.push_back (e->clone_generic_param ()); @@ -3144,6 +3150,33 @@ class ExternalFunctionItem : public ExternalItem } }; +class ExternalTypeItem : public ExternalItem +{ + ExternalTypeItem (Analysis::NodeMapping mappings, Identifier item_name, + Visibility vis, AST::AttrVec outer_attrs, location_t locus) + : ExternalItem (std::move (mappings), std::move (item_name), + std::move (vis), std::move (outer_attrs), locus) + {} + + ExternalTypeItem (ExternalTypeItem const &other) : ExternalItem (other) {} + + ExternalTypeItem (ExternalTypeItem &&other) = default; + ExternalTypeItem &operator= (ExternalTypeItem &&other) = default; + + std::string as_string () const override; + + void accept_vis (HIRFullVisitor &vis) override; + void accept_vis (HIRExternalItemVisitor &vis) override; + + ExternKind get_extern_kind () override { return ExternKind::Type; } + +protected: + ExternalTypeItem *clone_external_item_impl () const override + { + return new ExternalTypeItem (*this); + } +}; + // An extern block HIR node class ExternBlock : public VisItem, public WithInnerAttrs { diff --git a/gcc/rust/hir/tree/rust-hir-visitor.h b/gcc/rust/hir/tree/rust-hir-visitor.h index 4e7a97bd972f..ae9d23f2e005 100644 --- a/gcc/rust/hir/tree/rust-hir-visitor.h +++ b/gcc/rust/hir/tree/rust-hir-visitor.h @@ -114,6 +114,7 @@ class HIRFullVisitor virtual void visit (ImplBlock &impl) = 0; virtual void visit (ExternalStaticItem &item) = 0; virtual void visit (ExternalFunctionItem &item) = 0; + virtual void visit (ExternalTypeItem &item) = 0; virtual void visit (ExternBlock &block) = 0; virtual void visit (LiteralPattern &pattern) = 0; virtual void visit (IdentifierPattern &pattern) = 0; @@ -255,6 +256,7 @@ class HIRFullVisitorBase : public HIRFullVisitor virtual void visit (ExternalStaticItem &) override {} virtual void visit (ExternalFunctionItem &) override {} + virtual void visit (ExternalTypeItem &) override {} virtual void visit (ExternBlock &) override {} virtual void visit (LiteralPattern &) override {} @@ -306,6 +308,7 @@ class HIRExternalItemVisitor public: virtual void visit (ExternalStaticItem &item) = 0; virtual void visit (ExternalFunctionItem &item) = 0; + virtual void visit (ExternalTypeItem &item) = 0; }; class HIRTraitItemVisitor diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc index fb0a9c388ae4..3eb8838eb9db 100644 --- a/gcc/rust/hir/tree/rust-hir.cc +++ b/gcc/rust/hir/tree/rust-hir.cc @@ -3256,6 +3256,19 @@ ExternalFunctionItem::as_string () const return str; } +std::string +ExternalTypeItem::as_string () const +{ + std::string str = ExternalItem::as_string (); + + str += "type "; + + // add name + str += get_item_name ().as_string (); + + return str; +} + std::string NamedFunctionParam::as_string () const { @@ -4272,6 +4285,12 @@ ExternalFunctionItem::accept_vis (HIRFullVisitor &vis) vis.visit (*this); } +void +ExternalTypeItem::accept_vis (HIRFullVisitor &vis) +{ + vis.visit (*this); +} + void ExternBlock::accept_vis (HIRFullVisitor &vis) { @@ -4542,6 +4561,12 @@ ExternalFunctionItem::accept_vis (HIRExternalItemVisitor &vis) vis.visit (*this); } +void +ExternalTypeItem::accept_vis (HIRExternalItemVisitor &vis) +{ + vis.visit (*this); +} + void ExternalStaticItem::accept_vis (HIRExternalItemVisitor &vis) { diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc index 6b4141a42705..8a9a3f35a321 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc +++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc @@ -17,6 +17,7 @@ // . #include "rust-hir-type-check-implitem.h" +#include "rust-diagnostics.h" #include "rust-hir-type-check-base.h" #include "rust-hir-type-check-type.h" #include "rust-hir-type-check-expr.h" @@ -183,6 +184,134 @@ TypeCheckTopLevelExternItem::visit (HIR::ExternalFunctionItem &function) resolved = fnType; } +void +TypeCheckTopLevelExternItem::visit (HIR::ExternalTypeItem &type) +{ + rust_sorry_at (type.get_locus (), "extern types are not supported yet"); + // auto binder_pin = context->push_clean_lifetime_resolver (); + + // std::vector substitutions; + // if (function.has_generics ()) + // { + // for (auto &generic_param : function.get_generic_params ()) + // { + // switch (generic_param.get ()->get_kind ()) + // { + // case HIR::GenericParam::GenericKind::LIFETIME: + // context->intern_and_insert_lifetime ( + // static_cast (*generic_param) + // .get_lifetime ()); + // // TODO: handle bounds + // break; + // case HIR::GenericParam::GenericKind::CONST: + // // FIXME: Skipping Lifetime and Const completely until better + // // handling. + // break; + + // case HIR::GenericParam::GenericKind::TYPE: { + // auto param_type + // = TypeResolveGenericParam::Resolve (generic_param.get ()); + // context->insert_type (generic_param->get_mappings (), + // param_type); + + // substitutions.push_back (TyTy::SubstitutionParamMapping ( + // static_cast (*generic_param), param_type)); + // } + // break; + // } + // } + // } + + // TyTy::RegionConstraints region_constraints; + // if (function.has_where_clause ()) + // { + // for (auto &where_clause_item : function.get_where_clause ().get_items + // ()) + // { + // ResolveWhereClauseItem::Resolve (*where_clause_item.get (), + // region_constraints); + // } + // } + + // TyTy::BaseType *ret_type = nullptr; + // if (!function.has_return_type ()) + // ret_type + // = TyTy::TupleType::get_unit_type (function.get_mappings ().get_hirid + // ()); + // else + // { + // auto resolved + // = TypeCheckType::Resolve (function.get_return_type ().get ()); + // if (resolved == nullptr) + // { + // rust_error_at (function.get_locus (), + // "failed to resolve return type"); + // return; + // } + + // ret_type = resolved->clone (); + // ret_type->set_ref ( + // function.get_return_type ()->get_mappings ().get_hirid ()); + // } + + // std::vector > params; + // for (auto ¶m : function.get_function_params ()) + // { + // // get the name as well required for later on + // auto param_tyty = TypeCheckType::Resolve (param.get_type ().get ()); + + // // these are implicit mappings and not used + // auto crate_num = mappings->get_current_crate (); + // Analysis::NodeMapping mapping (crate_num, mappings->get_next_node_id + // (), + // mappings->get_next_hir_id (crate_num), + // UNKNOWN_LOCAL_DEFID); + + // HIR::IdentifierPattern *param_pattern + // = new HIR::IdentifierPattern (mapping, param.get_param_name (), + // UNDEF_LOCATION, false, Mutability::Imm, + // std::unique_ptr (nullptr)); + + // params.push_back ( + // std::pair (param_pattern, + // param_tyty)); + + // context->insert_type (param.get_mappings (), param_tyty); + + // // FIXME do we need error checking for patterns here? + // // see https://github.com/Rust-GCC/gccrs/issues/995 + // } + + // uint8_t flags = TyTy::FnType::FNTYPE_IS_EXTERN_FLAG; + // if (function.is_variadic ()) + // { + // flags |= TyTy::FnType::FNTYPE_IS_VARADIC_FLAG; + // if (parent.get_abi () != Rust::ABI::C) + // { + // rust_error_at ( + // function.get_locus (), ErrorCode::E0045, + // "C-variadic function must have C or cdecl calling convention"); + // } + // } + + // RustIdent ident{ + // CanonicalPath::new_seg (function.get_mappings ().get_nodeid (), + // function.get_item_name ().as_string ()), + // function.get_locus ()}; + + // auto fnType = new TyTy::FnType ( + // function.get_mappings ().get_hirid (), + // function.get_mappings ().get_defid (), + // function.get_item_name ().as_string (), ident, flags, parent.get_abi (), + // std::move (params), ret_type, std::move (substitutions), + // TyTy::SubstitutionArgumentMappings::empty ( + // context->get_lifetime_resolver ().get_num_bound_regions ()), + // region_constraints); + + // context->insert_type (function.get_mappings (), fnType); + // resolved = fnType; +} + TypeCheckImplItem::TypeCheckImplItem ( HIR::ImplBlock *parent, TyTy::BaseType *self, std::vector substitutions) diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.h b/gcc/rust/typecheck/rust-hir-type-check-implitem.h index 541b7280c1b8..64eb208d15d2 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-implitem.h +++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.h @@ -34,6 +34,7 @@ class TypeCheckTopLevelExternItem : public TypeCheckBase, void visit (HIR::ExternalStaticItem &item) override; void visit (HIR::ExternalFunctionItem &function) override; + void visit (HIR::ExternalTypeItem &type) override; private: TypeCheckTopLevelExternItem (const HIR::ExternBlock &parent);