aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-08-02 16:57:09 +0200
committerArthur Cohen <arthur.cohen@embecosm.com>2024-01-16 19:04:33 +0100
commitc8911dde92a98f1c9230a04c62a5287b562bc720 (patch)
tree047dcd5a581084b8fc30514dc286b060f258115e /gcc
parentccef457297594c5eadf57ec402122f260c1edc8f (diff)
downloadgcc-c8911dde92a98f1c9230a04c62a5287b562bc720.zip
gcc-c8911dde92a98f1c9230a04c62a5287b562bc720.tar.gz
gcc-c8911dde92a98f1c9230a04c62a5287b562bc720.tar.bz2
gccrs: Move extern crate resolving
Move extern crate resolving under the extern crate declaration instead of doing it under the crate's root as extern crates are not resolved by the top level resolver. gcc/rust/ChangeLog: * metadata/rust-extern-crate.cc (ExternCrate::ExternCrate): Update definition to allow Extern crate with no content (pure proc macros). (ExternCrate::ok): Panic on no content. (ExternCrate::load): Likewise. * metadata/rust-extern-crate.h: Update prototypes. * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::go): Remove macro resolution. (TopLevel::visit): Likewise. * resolve/rust-toplevel-name-resolver-2.0.h: Add visit prototype for extern crate. * rust-session-manager.cc (Session::load_extern_crate): Adapt content depending on the loaded crate's content. * util/rust-hir-map.cc (Mappings::lookup_derive_proc_macros): Change return type to optional because it is way more convenient. (Mappings::lookup_bang_proc_macros): Likewise. (Mappings::lookup_attribute_proc_macros): Likewise. * util/rust-hir-map.h: Update function prototypes. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/metadata/rust-extern-crate.cc10
-rw-r--r--gcc/rust/metadata/rust-extern-crate.h6
-rw-r--r--gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc107
-rw-r--r--gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h1
-rw-r--r--gcc/rust/rust-session-manager.cc17
-rw-r--r--gcc/rust/util/rust-hir-map.cc35
-rw-r--r--gcc/rust/util/rust-hir-map.h13
7 files changed, 106 insertions, 83 deletions
diff --git a/gcc/rust/metadata/rust-extern-crate.cc b/gcc/rust/metadata/rust-extern-crate.cc
index 3aaf6b4..188e5e7 100644
--- a/gcc/rust/metadata/rust-extern-crate.cc
+++ b/gcc/rust/metadata/rust-extern-crate.cc
@@ -25,9 +25,11 @@
namespace Rust {
namespace Imports {
-ExternCrate::ExternCrate (Import::Stream &stream,
+ExternCrate::ExternCrate (Import::Stream &stream) : import_stream (stream) {}
+
+ExternCrate::ExternCrate (const std::string &crate_name,
std::vector<ProcMacro::Procmacro> macros)
- : import_stream (stream), proc_macros (macros)
+ : proc_macros (macros), crate_name (crate_name)
{}
ExternCrate::~ExternCrate () {}
@@ -35,12 +37,14 @@ ExternCrate::~ExternCrate () {}
bool
ExternCrate::ok () const
{
- return !import_stream.saw_error ();
+ return !import_stream->get ().saw_error ();
}
bool
ExternCrate::load (location_t locus)
{
+ rust_assert (this->import_stream.has_value ());
+ auto &import_stream = this->import_stream->get ();
// match header
import_stream.require_bytes (locus, Metadata::kMagicHeader,
sizeof (Metadata::kMagicHeader));
diff --git a/gcc/rust/metadata/rust-extern-crate.h b/gcc/rust/metadata/rust-extern-crate.h
index fcd54f6..70ee84d 100644
--- a/gcc/rust/metadata/rust-extern-crate.h
+++ b/gcc/rust/metadata/rust-extern-crate.h
@@ -21,6 +21,7 @@
#include "rust-system.h"
#include "rust-imports.h"
+#include "optional.h"
namespace Rust {
namespace Imports {
@@ -28,7 +29,8 @@ namespace Imports {
class ExternCrate
{
public:
- ExternCrate (Import::Stream &stream,
+ ExternCrate (Import::Stream &stream);
+ ExternCrate (const std::string &crate_name,
std::vector<ProcMacro::Procmacro> macros);
~ExternCrate ();
@@ -46,7 +48,7 @@ public:
bool is_neg_ok, int *ret);
private:
- Import::Stream &import_stream;
+ tl::optional<std::reference_wrapper<Import::Stream>> import_stream;
std::vector<ProcMacro::Procmacro> proc_macros;
std::string crate_name;
diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
index 2246788..6125298 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.cc
@@ -55,51 +55,6 @@ TopLevel::go (AST::Crate &crate)
{
for (auto &item : crate.items)
item->accept_vis (*this);
-
- std::vector<CustomDeriveProcMacro> derive_macros;
- std::vector<AttributeProcMacro> attribute_macros;
- std::vector<BangProcMacro> bang_macros;
-
- Analysis::Mappings::get ()->lookup_attribute_proc_macros (
- crate.get_node_id (), attribute_macros);
- Analysis::Mappings::get ()->lookup_bang_proc_macros (crate.get_node_id (),
- bang_macros);
- Analysis::Mappings::get ()->lookup_derive_proc_macros (crate.get_node_id (),
- derive_macros);
-
- for (auto &derive : derive_macros)
- {
- auto res = ctx.macros.insert_at_root (derive.get_trait_name (),
- derive.get_node_id ());
- if (!res)
- {
- rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428,
- "macro %qs defined multiple times",
- derive.get_trait_name ().c_str ());
- }
- }
- for (auto &attribute : attribute_macros)
- {
- auto res = ctx.macros.insert_at_root (attribute.get_name (),
- attribute.get_node_id ());
- if (!res)
- {
- rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428,
- "macro %qs defined multiple times",
- attribute.get_name ().c_str ());
- }
- }
- for (auto &bang : bang_macros)
- {
- auto res
- = ctx.macros.insert_at_root (bang.get_name (), bang.get_node_id ());
- if (!res)
- {
- rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428,
- "macro %qs defined multiple times",
- bang.get_name ().c_str ());
- }
- }
}
void
@@ -116,6 +71,68 @@ TopLevel::visit (AST::Module &module)
module.get_name ());
}
+void
+TopLevel::visit (AST::ExternCrate &crate)
+{
+ CrateNum num;
+ if (!Analysis::Mappings::get ()->lookup_crate_name (
+ crate.get_referenced_crate (), num))
+ rust_unreachable ();
+
+ auto attribute_macros
+ = Analysis::Mappings::get ()->lookup_attribute_proc_macros (num);
+
+ auto bang_macros = Analysis::Mappings::get ()->lookup_bang_proc_macros (num);
+
+ auto derive_macros
+ = Analysis::Mappings::get ()->lookup_derive_proc_macros (num);
+
+ auto sub_visitor = [&] () {
+ if (derive_macros.has_value ())
+ for (auto &derive : derive_macros.value ())
+ {
+ auto res = ctx.macros.insert (derive.get_trait_name (),
+ derive.get_node_id ());
+ if (!res)
+ {
+ rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428,
+ "macro %qs defined multiple times",
+ derive.get_trait_name ().c_str ());
+ }
+ }
+ if (attribute_macros.has_value ())
+ for (auto &attribute : attribute_macros.value ())
+ {
+ auto res = ctx.macros.insert (attribute.get_name (),
+ attribute.get_node_id ());
+ if (!res)
+ {
+ rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428,
+ "macro %qs defined multiple times",
+ attribute.get_name ().c_str ());
+ }
+ }
+ if (bang_macros.has_value ())
+ for (auto &bang : bang_macros.value ())
+ {
+ auto res = ctx.macros.insert (bang.get_name (), bang.get_node_id ());
+ if (!res)
+ {
+ rust_error_at (UNKNOWN_LOCATION, ErrorCode::E0428,
+ "macro %qs defined multiple times",
+ bang.get_name ().c_str ());
+ }
+ }
+ };
+
+ if (crate.has_as_clause ())
+ ctx.scoped (Rib::Kind::Module, crate.get_node_id (), sub_visitor,
+ crate.get_as_clause ());
+ else
+ ctx.scoped (Rib::Kind::Module, crate.get_node_id (), sub_visitor,
+ crate.get_referenced_crate ());
+}
+
static bool
is_macro_export (AST::MacroRulesDefinition &def)
{
diff --git a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
index 93d40b8..b8ed106 100644
--- a/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
+++ b/gcc/rust/resolve/rust-toplevel-name-resolver-2.0.h
@@ -73,6 +73,7 @@ private:
void visit (AST::Enum &enum_item) override;
void visit (AST::Union &union_item) override;
void visit (AST::ConstantItem &const_item) override;
+ void visit (AST::ExternCrate &crate) override;
};
} // namespace Resolver2_0
diff --git a/gcc/rust/rust-session-manager.cc b/gcc/rust/rust-session-manager.cc
index d0d92a8..2a8ef97 100644
--- a/gcc/rust/rust-session-manager.cc
+++ b/gcc/rust/rust-session-manager.cc
@@ -1018,19 +1018,24 @@ Session::load_extern_crate (const std::string &crate_name, location_t locus)
{
s = Import::open_package (import_name, locus, relative_import_path);
}
- if (s.first == NULL)
+ if (s.first == NULL && s.second.empty ())
{
rust_error_at (locus, "failed to locate crate %<%s%>",
import_name.c_str ());
return UNKNOWN_NODEID;
}
- Imports::ExternCrate extern_crate (*s.first, s.second);
- bool ok = extern_crate.load (locus);
- if (!ok)
+ auto extern_crate = s.first == nullptr
+ ? Imports::ExternCrate (crate_name, s.second)
+ : Imports::ExternCrate (*s.first);
+ if (s.first != nullptr)
{
- rust_error_at (locus, "failed to load crate metadata");
- return UNKNOWN_NODEID;
+ bool ok = extern_crate.load (locus);
+ if (!ok)
+ {
+ rust_error_at (locus, "failed to load crate metadata");
+ return UNKNOWN_NODEID;
+ }
}
// ensure the current vs this crate name don't collide
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index eb6c576..8ac957f 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -972,38 +972,31 @@ Mappings::insert_attribute_proc_macros (CrateNum num,
procmacrosAttributeMappings[num] = macros;
}
-bool
-Mappings::lookup_derive_proc_macros (CrateNum num,
- std::vector<CustomDeriveProcMacro> &macros)
+tl::optional<std::vector<CustomDeriveProcMacro> &>
+Mappings::lookup_derive_proc_macros (CrateNum num)
{
auto it = procmacrosDeriveMappings.find (num);
if (it == procmacrosDeriveMappings.end ())
- return false;
-
- macros = it->second;
- return true;
+ return tl::nullopt;
+ return it->second;
}
-bool
-Mappings::lookup_bang_proc_macros (CrateNum num,
- std::vector<BangProcMacro> &macros)
+
+tl::optional<std::vector<BangProcMacro> &>
+Mappings::lookup_bang_proc_macros (CrateNum num)
{
auto it = procmacrosBangMappings.find (num);
if (it == procmacrosBangMappings.end ())
- return false;
-
- macros = it->second;
- return true;
+ return tl::nullopt;
+ return it->second;
}
-bool
-Mappings::lookup_attribute_proc_macros (CrateNum num,
- std::vector<AttributeProcMacro> &macros)
+
+tl::optional<std::vector<AttributeProcMacro> &>
+Mappings::lookup_attribute_proc_macros (CrateNum num)
{
auto it = procmacrosAttributeMappings.find (num);
if (it == procmacrosAttributeMappings.end ())
- return false;
-
- macros = it->second;
- return true;
+ return tl::nullopt;
+ return it->second;
}
void
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 49bafb2..06d117d 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -30,6 +30,7 @@
#include "rust-privacy-common.h"
#include "libproc_macro_internal/proc_macro.h"
#include "rust-proc-macro.h"
+#include "optional.h"
namespace Rust {
namespace Analysis {
@@ -294,12 +295,12 @@ public:
void insert_attribute_proc_macros (CrateNum num,
std::vector<AttributeProcMacro> macros);
- bool lookup_derive_proc_macros (CrateNum num,
- std::vector<CustomDeriveProcMacro> &macros);
- bool lookup_bang_proc_macros (CrateNum num,
- std::vector<BangProcMacro> &macros);
- bool lookup_attribute_proc_macros (CrateNum num,
- std::vector<AttributeProcMacro> &macros);
+ tl::optional<std::vector<CustomDeriveProcMacro> &>
+ lookup_derive_proc_macros (CrateNum num);
+ tl::optional<std::vector<BangProcMacro> &>
+ lookup_bang_proc_macros (CrateNum num);
+ tl::optional<std::vector<AttributeProcMacro> &>
+ lookup_attribute_proc_macros (CrateNum num);
void insert_derive_proc_macro_def (NodeId id, CustomDeriveProcMacro macro);
void insert_bang_proc_macro_def (NodeId id, BangProcMacro macro);