aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/util
diff options
context:
space:
mode:
authorPierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>2023-06-19 12:51:34 +0200
committerP-E-P <32375388+P-E-P@users.noreply.github.com>2023-07-21 09:06:09 +0000
commit361fd8fea7cd4d6926a0b9da8305c2b92d14ed3b (patch)
tree42e65cd56e35944bf1497da70cf431a6da63437a /gcc/rust/util
parent224409f746f0fe48101c76745e6eee61230ab98a (diff)
downloadgcc-361fd8fea7cd4d6926a0b9da8305c2b92d14ed3b.zip
gcc-361fd8fea7cd4d6926a0b9da8305c2b92d14ed3b.tar.gz
gcc-361fd8fea7cd4d6926a0b9da8305c2b92d14ed3b.tar.bz2
resolve: Add mappings for proc macros and resolving
Add multiple mappings for procedural macro name resolution. Procedural macros were not resolved using name resolution and mapping but rather with some hacky path comparison. gcc/rust/ChangeLog: * expand/rust-macro-expand.cc (MacroExpander::import_proc_macros): Remove function. * expand/rust-macro-expand.h (struct MacroExpander): Remove import_proc_macro function. * util/rust-hir-map.cc (Mappings::insert_derive_proc_macro_def): Add a function to insert a derive proc macro definition. (Mappings::insert_bang_proc_macro): Remove function. (Mappings::insert_bang_proc_macro_def): Add function to insert a bang proc macro definition. (Mappings::insert_attribute_proc_macro_def): Likewise with attribute proc macros. (Mappings::lookup_derive_proc_macro_def): Add a function to lookup a defined derive proc macro definition. (Mappings::lookup_bang_proc_macro): Remove function. (Mappings::lookup_bang_proc_macro_def): Add a function to lookup a bang proc macro definition. (Mappings::lookup_attribute_proc_macro_def): Add a function to lookup an attribute prod macro definition. (Mappings::insert_derive_proc_macro_invocation): Add a function to insert a derive proc macro invocation. (Mappings::lookup_derive_proc_macro_invocation): Add a function to lookup a derive proc macro invocation. (Mappings::insert_bang_proc_macro_invocation): Add a function to insert a bang proc macro invocation. (Mappings::lookup_bang_proc_macro_invocation): Add a function to lookup a bang proc macro invocation. (Mappings::insert_attribute_proc_macro_invocation): Add a function to insert an attribute proc macro invocation. (Mappings::lookup_attribute_proc_macro_invocation): Add a function to lookup an attribute proc macro invocation. * util/rust-hir-map.h: Add different proc macro mappings and change function prototypes. * expand/rust-expand-visitor.cc (get_traits_to_derive): Return a vector of SimplePath instead. (derive_item): Accept SimplePath instead of a string. * ast/rust-ast.h: Add common ProcMacroInvocable interface to Identifiers and SimplePath nodes. * ast/rust-ast.cc: Add const modifier. * ast/rust-macro.h: Change path and identifier getters. * ast/rust-path.h: Change return type to reference. Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Diffstat (limited to 'gcc/rust/util')
-rw-r--r--gcc/rust/util/rust-hir-map.cc106
-rw-r--r--gcc/rust/util/rust-hir-map.h53
-rw-r--r--gcc/rust/util/rust-proc-macro-invocation.h21
3 files changed, 134 insertions, 46 deletions
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 0faea56..76e4c70 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -1007,40 +1007,39 @@ Mappings::lookup_attribute_proc_macros (
}
void
-Mappings::insert_derive_proc_macro (
- std::pair<std::string, std::string> hierarchy, ProcMacro::CustomDerive macro)
+Mappings::insert_derive_proc_macro_def (NodeId id,
+ ProcMacro::CustomDerive macro)
{
- auto it = procmacroDeriveMappings.find (hierarchy);
+ auto it = procmacroDeriveMappings.find (id);
rust_assert (it == procmacroDeriveMappings.end ());
- procmacroDeriveMappings[hierarchy] = macro;
+ procmacroDeriveMappings[id] = macro;
}
void
-Mappings::insert_bang_proc_macro (std::pair<std::string, std::string> hierarchy,
- ProcMacro::Bang macro)
+Mappings::insert_bang_proc_macro_def (NodeId id, ProcMacro::Bang macro)
{
- auto it = procmacroBangMappings.find (hierarchy);
+ auto it = procmacroBangMappings.find (id);
rust_assert (it == procmacroBangMappings.end ());
- procmacroBangMappings[hierarchy] = macro;
+ procmacroBangMappings[id] = macro;
}
void
-Mappings::insert_attribute_proc_macro (
- std::pair<std::string, std::string> hierarchy, ProcMacro::Attribute macro)
+Mappings::insert_attribute_proc_macro_def (NodeId id,
+ ProcMacro::Attribute macro)
{
- auto it = procmacroAttributeMappings.find (hierarchy);
+ auto it = procmacroAttributeMappings.find (id);
rust_assert (it == procmacroAttributeMappings.end ());
- procmacroAttributeMappings[hierarchy] = macro;
+ procmacroAttributeMappings[id] = macro;
}
bool
-Mappings::lookup_derive_proc_macro (
- std::pair<std::string, std::string> hierarchy, ProcMacro::CustomDerive &macro)
+Mappings::lookup_derive_proc_macro_def (NodeId id,
+ ProcMacro::CustomDerive &macro)
{
- auto it = procmacroDeriveMappings.find (hierarchy);
+ auto it = procmacroDeriveMappings.find (id);
if (it == procmacroDeriveMappings.end ())
return false;
@@ -1049,10 +1048,9 @@ Mappings::lookup_derive_proc_macro (
}
bool
-Mappings::lookup_bang_proc_macro (std::pair<std::string, std::string> hierarchy,
- ProcMacro::Bang &macro)
+Mappings::lookup_bang_proc_macro_def (NodeId id, ProcMacro::Bang &macro)
{
- auto it = procmacroBangMappings.find (hierarchy);
+ auto it = procmacroBangMappings.find (id);
if (it == procmacroBangMappings.end ())
return false;
@@ -1061,10 +1059,10 @@ Mappings::lookup_bang_proc_macro (std::pair<std::string, std::string> hierarchy,
}
bool
-Mappings::lookup_attribute_proc_macro (
- std::pair<std::string, std::string> hierarchy, ProcMacro::Attribute &macro)
+Mappings::lookup_attribute_proc_macro_def (NodeId id,
+ ProcMacro::Attribute &macro)
{
- auto it = procmacroAttributeMappings.find (hierarchy);
+ auto it = procmacroAttributeMappings.find (id);
if (it == procmacroAttributeMappings.end ())
return false;
@@ -1073,6 +1071,72 @@ Mappings::lookup_attribute_proc_macro (
}
void
+Mappings::insert_derive_proc_macro_invocation (Rust::ProcMacroInvocable &invoc,
+ ProcMacro::CustomDerive def)
+{
+ auto it = procmacroDeriveInvocations.find (invoc.get_node_id ());
+ rust_assert (it == procmacroDeriveInvocations.end ());
+
+ procmacroDeriveInvocations[invoc.get_node_id ()] = def;
+}
+
+bool
+Mappings::lookup_derive_proc_macro_invocation (Rust::ProcMacroInvocable &invoc,
+ ProcMacro::CustomDerive &def)
+{
+ auto it = procmacroDeriveInvocations.find (invoc.get_node_id ());
+ if (it == procmacroDeriveInvocations.end ())
+ return false;
+
+ def = it->second;
+ return true;
+}
+
+void
+Mappings::insert_bang_proc_macro_invocation (AST::MacroInvocation &invoc,
+ ProcMacro::Bang def)
+{
+ auto it = procmacroBangInvocations.find (invoc.get_macro_node_id ());
+ rust_assert (it == procmacroBangInvocations.end ());
+
+ procmacroBangInvocations[invoc.get_macro_node_id ()] = def;
+}
+
+bool
+Mappings::lookup_bang_proc_macro_invocation (AST::MacroInvocation &invoc,
+ ProcMacro::Bang &def)
+{
+ auto it = procmacroBangInvocations.find (invoc.get_macro_node_id ());
+ if (it == procmacroBangInvocations.end ())
+ return false;
+
+ def = it->second;
+ return true;
+}
+
+void
+Mappings::insert_attribute_proc_macro_invocation (
+ Rust::ProcMacroInvocable &invoc, ProcMacro::Attribute def)
+{
+ auto it = procmacroAttributeInvocations.find (invoc.get_node_id ());
+ rust_assert (it == procmacroAttributeInvocations.end ());
+
+ procmacroAttributeInvocations[invoc.get_node_id ()] = def;
+}
+
+bool
+Mappings::lookup_attribute_proc_macro_invocation (
+ Rust::ProcMacroInvocable &invoc, ProcMacro::Attribute &def)
+{
+ auto it = procmacroAttributeInvocations.find (invoc.get_node_id ());
+ if (it == procmacroAttributeInvocations.end ())
+ return false;
+
+ def = it->second;
+ return true;
+}
+
+void
Mappings::insert_visibility (NodeId id, Privacy::ModuleVisibility visibility)
{
visibility_map.insert ({id, visibility});
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index d1fbe59..3360e2c 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -28,6 +28,7 @@
#include "rust-hir-full-decls.h"
#include "rust-lang-item.h"
#include "rust-privacy-common.h"
+#include "rust-proc-macro-invocation.h"
#include "libproc_macro/proc_macro.h"
namespace Rust {
@@ -297,21 +298,27 @@ public:
bool lookup_attribute_proc_macros (CrateNum num,
std::vector<ProcMacro::Attribute> &macros);
- void insert_derive_proc_macro (std::pair<std::string, std::string> hierachy,
- ProcMacro::CustomDerive macro);
- void insert_bang_proc_macro (std::pair<std::string, std::string> hierachy,
- ProcMacro::Bang macro);
- void
- insert_attribute_proc_macro (std::pair<std::string, std::string> hierachy,
- ProcMacro::Attribute macro);
-
- bool lookup_derive_proc_macro (std::pair<std::string, std::string> hierachy,
- ProcMacro::CustomDerive &macro);
- bool lookup_bang_proc_macro (std::pair<std::string, std::string> hierachy,
- ProcMacro::Bang &macro);
- bool
- lookup_attribute_proc_macro (std::pair<std::string, std::string> hierachy,
- ProcMacro::Attribute &macro);
+ void insert_derive_proc_macro_def (NodeId id, ProcMacro::CustomDerive macro);
+ void insert_bang_proc_macro_def (NodeId id, ProcMacro::Bang macro);
+ void insert_attribute_proc_macro_def (NodeId id, ProcMacro::Attribute macro);
+
+ bool lookup_derive_proc_macro_def (NodeId id, ProcMacro::CustomDerive &macro);
+ bool lookup_bang_proc_macro_def (NodeId id, ProcMacro::Bang &macro);
+ bool lookup_attribute_proc_macro_def (NodeId id, ProcMacro::Attribute &macro);
+
+ void insert_derive_proc_macro_invocation (Rust::ProcMacroInvocable &invoc,
+ ProcMacro::CustomDerive def);
+
+ bool lookup_derive_proc_macro_invocation (Rust::ProcMacroInvocable &invoc,
+ ProcMacro::CustomDerive &def);
+ void insert_bang_proc_macro_invocation (AST::MacroInvocation &invoc,
+ ProcMacro::Bang def);
+ bool lookup_bang_proc_macro_invocation (AST::MacroInvocation &invoc_id,
+ ProcMacro::Bang &def);
+ void insert_attribute_proc_macro_invocation (Rust::ProcMacroInvocable &invoc,
+ ProcMacro::Attribute def);
+ bool lookup_attribute_proc_macro_invocation (Rust::ProcMacroInvocable &invoc,
+ ProcMacro::Attribute &def);
void insert_visibility (NodeId id, Privacy::ModuleVisibility visibility);
bool lookup_visibility (NodeId id, Privacy::ModuleVisibility &def);
@@ -392,20 +399,16 @@ private:
// Procedural macros
std::map<CrateNum, std::vector<ProcMacro::CustomDerive>>
procmacrosDeriveMappings;
-
std::map<CrateNum, std::vector<ProcMacro::Bang>> procmacrosBangMappings;
-
std::map<CrateNum, std::vector<ProcMacro::Attribute>>
procmacrosAttributeMappings;
- std::map<std::pair<std::string, std::string>, ProcMacro::CustomDerive>
- procmacroDeriveMappings;
-
- std::map<std::pair<std::string, std::string>, ProcMacro::Bang>
- procmacroBangMappings;
-
- std::map<std::pair<std::string, std::string>, ProcMacro::Attribute>
- procmacroAttributeMappings;
+ std::map<NodeId, ProcMacro::CustomDerive> procmacroDeriveMappings;
+ std::map<NodeId, ProcMacro::Bang> procmacroBangMappings;
+ std::map<NodeId, ProcMacro::Attribute> procmacroAttributeMappings;
+ std::map<NodeId, ProcMacro::CustomDerive> procmacroDeriveInvocations;
+ std::map<NodeId, ProcMacro::Bang> procmacroBangInvocations;
+ std::map<NodeId, ProcMacro::Attribute> procmacroAttributeInvocations;
// crate names
std::map<CrateNum, std::string> crate_names;
diff --git a/gcc/rust/util/rust-proc-macro-invocation.h b/gcc/rust/util/rust-proc-macro-invocation.h
new file mode 100644
index 0000000..b45e013
--- /dev/null
+++ b/gcc/rust/util/rust-proc-macro-invocation.h
@@ -0,0 +1,21 @@
+#ifndef RUST_PROC_MACRO_INVOCATION_H
+#define RUST_PROC_MACRO_INVOCATION_H
+
+#include "rust-mapping-common.h"
+#include "rust-location.h"
+
+namespace Rust {
+
+class ProcMacroInvocable
+{
+public:
+ virtual NodeId get_node_id () const = 0;
+ virtual const std::string as_string () const = 0;
+ virtual Location get_locus () const = 0;
+
+ virtual ~ProcMacroInvocable () {}
+};
+
+} // namespace Rust
+
+#endif /* ! RUST_PROC_MACRO_INVOCATION_H*/