From 8775525b042679bdd9c2b8dad68e65cec4b3dd79 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Fri, 8 Jul 2022 15:11:40 +0100 Subject: Add missing include for intellisense --- gcc/rust/rust-object-export.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'gcc') diff --git a/gcc/rust/rust-object-export.h b/gcc/rust/rust-object-export.h index 1704f59..fcede54 100644 --- a/gcc/rust/rust-object-export.h +++ b/gcc/rust/rust-object-export.h @@ -19,6 +19,8 @@ #ifndef RUST_OBJECT_EXPORT_H #define RUST_OBJECT_EXPORT_H +#include "rust-system.h" + extern unsigned int rust_field_alignment (tree t); -- cgit v1.1 From 81c4b6989f044a45455717b752cafb96f04b768e Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Fri, 8 Jul 2022 14:54:37 +0100 Subject: Fix undefined behaviour using .get on unique_ptr As the move semantics for AST and HIR crates are unsafe on older compilers we are working around this by storing the pointer into the mappings class this was using the get method to store the pointer. The issue for loading extern crates the unique_ptr goes out of scope and this the deletion code resulting in undefined behaviour. This changes it to call release to take full ownership of the pointer as we expect. --- gcc/rust/util/rust-hir-map.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 353b7cb..360dabf 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -199,7 +199,7 @@ Mappings::insert_ast_crate (std::unique_ptr &&crate, rust_assert (it == ast_crate_mappings.end ()); // store it - ast_crate_mappings.insert ({crate_num, crate.get ()}); + ast_crate_mappings.insert ({crate_num, crate.release ()}); // return the reference to it it = ast_crate_mappings.find (crate_num); @@ -236,7 +236,7 @@ Mappings::insert_hir_crate (std::unique_ptr &&crate) insert_node_to_hir (crate->get_mappings ().get_nodeid (), crate->get_mappings ().get_hirid ()); - hir_crate_mappings.insert ({crateNum, crate.get ()}); + hir_crate_mappings.insert ({crateNum, crate.release ()}); it = hir_crate_mappings.find (crateNum); rust_assert (it != hir_crate_mappings.end ()); -- cgit v1.1 From 6bf6241f29e73678b8c7d3508cdb39b55ae9d1ab Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Fri, 8 Jul 2022 15:13:57 +0100 Subject: External Items with Rust ABI need to mangle the full path When compiling external rust abi item requires the fully qualified canonical path to be mangled in order to link correctly. --- gcc/rust/backend/rust-compile-extern.h | 11 +++++++++++ gcc/rust/resolve/rust-ast-resolve-item.cc | 6 ++++++ 2 files changed, 17 insertions(+) (limited to 'gcc') diff --git a/gcc/rust/backend/rust-compile-extern.h b/gcc/rust/backend/rust-compile-extern.h index c24437d..ddad350 100644 --- a/gcc/rust/backend/rust-compile-extern.h +++ b/gcc/rust/backend/rust-compile-extern.h @@ -130,6 +130,17 @@ public: tree compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype); std::string ir_symbol_name = function.get_item_name (); std::string asm_name = function.get_item_name (); + if (fntype->get_abi () == ABI::RUST) + { + // then we need to get the canonical path of it and mangle it + const Resolver::CanonicalPath *canonical_path = nullptr; + bool ok = ctx->get_mappings ()->lookup_canonical_path ( + function.get_mappings ().get_nodeid (), &canonical_path); + rust_assert (ok); + + ir_symbol_name = canonical_path->get () + fntype->subst_as_string (); + asm_name = ctx->mangle_item (fntype, *canonical_path); + } const unsigned int flags = Backend::function_is_declaration; tree fndecl diff --git a/gcc/rust/resolve/rust-ast-resolve-item.cc b/gcc/rust/resolve/rust-ast-resolve-item.cc index 244bf64..75bd2e1 100644 --- a/gcc/rust/resolve/rust-ast-resolve-item.cc +++ b/gcc/rust/resolve/rust-ast-resolve-item.cc @@ -1027,6 +1027,12 @@ void ResolveExternItem::visit (AST::ExternalFunctionItem &function) { NodeId scope_node_id = function.get_node_id (); + auto decl = CanonicalPath::new_seg (function.get_node_id (), + function.get_identifier ()); + auto path = prefix.append (decl); + auto cpath = canonical_prefix.append (decl); + + mappings->insert_canonical_path (function.get_node_id (), cpath); resolve_visibility (function.get_visibility ()); -- cgit v1.1 From e3f135ef0beb51b77cae7c6af40c5a1c4dfe7722 Mon Sep 17 00:00:00 2001 From: Philip Herron Date: Fri, 8 Jul 2022 15:08:08 +0100 Subject: Add new interface to mappings class crate_num_to_nodeid and node_is_crate In order to load and resolve extern crates we need to support detection of node_id is a crate in order to support paths outside of the current crate. --- gcc/rust/resolve/rust-ast-resolve-path.cc | 3 +- gcc/rust/resolve/rust-ast-resolve-type.cc | 3 +- gcc/rust/util/rust-hir-map.cc | 64 +++++++++++++++++++++++++++++++ gcc/rust/util/rust-hir-map.h | 31 ++++----------- 4 files changed, 75 insertions(+), 26 deletions(-) (limited to 'gcc') diff --git a/gcc/rust/resolve/rust-ast-resolve-path.cc b/gcc/rust/resolve/rust-ast-resolve-path.cc index a03bfb4..27f32aa 100644 --- a/gcc/rust/resolve/rust-ast-resolve-path.cc +++ b/gcc/rust/resolve/rust-ast-resolve-path.cc @@ -199,7 +199,8 @@ ResolvePath::resolve_path (AST::PathInExpression *expr) bool did_resolve_segment = resolved_node_id != UNKNOWN_NODEID; if (did_resolve_segment) { - if (mappings->node_is_module (resolved_node_id)) + if (mappings->node_is_module (resolved_node_id) + || mappings->node_is_crate (resolved_node_id)) { module_scope_id = resolved_node_id; } diff --git a/gcc/rust/resolve/rust-ast-resolve-type.cc b/gcc/rust/resolve/rust-ast-resolve-type.cc index 8e85889..04183a4 100644 --- a/gcc/rust/resolve/rust-ast-resolve-type.cc +++ b/gcc/rust/resolve/rust-ast-resolve-type.cc @@ -220,7 +220,8 @@ ResolveRelativeTypePath::go (AST::TypePath &path, NodeId &resolved_node_id) bool did_resolve_segment = resolved_node_id != UNKNOWN_NODEID; if (did_resolve_segment) { - if (mappings->node_is_module (resolved_node_id)) + if (mappings->node_is_module (resolved_node_id) + || mappings->node_is_crate (resolved_node_id)) { module_scope_id = resolved_node_id; } diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 353b7cb..7d23717 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -130,6 +130,70 @@ Mappings::get_current_crate () const return currentCrateNum; } +bool +Mappings::get_crate_name (CrateNum crate_num, std::string &name) const +{ + auto it = crate_names.find (crate_num); + if (it == crate_names.end ()) + return false; + + name.assign (it->second); + return true; +} + +void +Mappings::set_crate_name (CrateNum crate_num, const std::string &name) +{ + crate_names[crate_num] = name; +} + +std::string +Mappings::get_current_crate_name () const +{ + std::string name; + bool ok = get_crate_name (get_current_crate (), name); + rust_assert (ok); + return name; +} + +bool +Mappings::lookup_crate_name (const std::string &crate_name, + CrateNum &resolved_crate_num) const +{ + for (const auto &it : crate_names) + { + if (it.second.compare (crate_name) == 0) + { + resolved_crate_num = it.first; + return true; + } + } + return false; +} + +bool +Mappings::crate_num_to_nodeid (const CrateNum &crate_num, NodeId &node_id) const +{ + auto it = ast_crate_mappings.find (crate_num); + if (it == ast_crate_mappings.end ()) + return false; + + node_id = it->second->get_node_id (); + return true; +} + +bool +Mappings::node_is_crate (NodeId node_id) const +{ + for (const auto &it : ast_crate_mappings) + { + NodeId crate_node_id = it.second->get_node_id (); + if (crate_node_id == node_id) + return true; + } + return false; +} + NodeId Mappings::get_next_node_id () { diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index 804242e..c8cebef 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -77,30 +77,13 @@ public: CrateNum get_next_crate_num (const std::string &name); void set_current_crate (CrateNum crateNum); CrateNum get_current_crate () const; - - bool get_crate_name (CrateNum crate_num, std::string &name) const - { - auto it = crate_names.find (crate_num); - if (it == crate_names.end ()) - return false; - - name.assign (it->second); - return true; - } - - // set crate name mid-compilation - void set_crate_name (CrateNum crate_num, const std::string &name) - { - crate_names[crate_num] = name; - } - - std::string get_current_crate_name () const - { - std::string name; - bool ok = get_crate_name (get_current_crate (), name); - rust_assert (ok); - return name; - } + bool get_crate_name (CrateNum crate_num, std::string &name) const; + void set_crate_name (CrateNum crate_num, const std::string &name); + std::string get_current_crate_name () const; + bool lookup_crate_name (const std::string &crate_name, + CrateNum &resolved_crate_num) const; + bool crate_num_to_nodeid (const CrateNum &crate_num, NodeId &node_id) const; + bool node_is_crate (NodeId node_id) const; NodeId get_next_node_id (); HirId get_next_hir_id () { return get_next_hir_id (get_current_crate ()); } -- cgit v1.1