summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/ast/ast.hpp8
-rw-r--r--src/expand/derive.cpp6
-rw-r--r--src/expand/macro_rules.cpp4
-rw-r--r--src/expand/mod.cpp2
-rw-r--r--src/hir/from_ast.cpp19
-rw-r--r--src/resolve/index.cpp20
6 files changed, 50 insertions, 9 deletions
diff --git a/src/ast/ast.hpp b/src/ast/ast.hpp
index 2d802f53..b80b5fb7 100644
--- a/src/ast/ast.hpp
+++ b/src/ast/ast.hpp
@@ -537,7 +537,13 @@ public:
// List of macros imported from other modules (via #[macro_use], includes proc macros)
// - First value is an absolute path to the macro (including crate name)
- ::std::vector<::std::pair< ::std::vector<::std::string>, const MacroRules* >> m_macro_imports;
+ struct MacroImport {
+ bool is_pub;
+ ::std::string name; // Can be different, if `use foo as bar` is used
+ ::std::vector<::std::string> path; // includes the crate name
+ const MacroRules* macro_ptr;
+ };
+ ::std::vector<MacroImport> m_macro_imports;
public:
Module() {}
diff --git a/src/expand/derive.cpp b/src/expand/derive.cpp
index 5e97c888..a03992da 100644
--- a/src/expand/derive.cpp
+++ b/src/expand/derive.cpp
@@ -2236,15 +2236,15 @@ static void derive_item(const Span& sp, const AST::Crate& crate, AST::Module& mo
bool found = false;
for(const auto& mac_path : mod.m_macro_imports)
{
- if( mac_path.first.back() == mac_name )
+ if( mac_path.name == mac_name )
{
- if( mac_path.second ) {
+ if( mac_path.macro_ptr ) {
// macro_rules! based derive?
TODO(sp, "Custom derive using macro_rules?");
}
else {
// proc_macro - Invoke the handler.
- auto lex = ProcMacro_Invoke(sp, crate, mac_path.first, path.nodes().back().name(), item);
+ auto lex = ProcMacro_Invoke(sp, crate, mac_path.path, path.nodes().back().name(), item);
if( lex )
{
Parse_ModRoot_Items(*lex, mod);
diff --git a/src/expand/macro_rules.cpp b/src/expand/macro_rules.cpp
index 89f394e1..3bacfcf7 100644
--- a/src/expand/macro_rules.cpp
+++ b/src/expand/macro_rules.cpp
@@ -61,8 +61,8 @@ class CMacroUseHandler:
});
for(const auto& p : ec.m_hir->m_proc_macros)
{
- mod.m_macro_imports.push_back(::std::make_pair( p.path.m_components, nullptr ));
- mod.m_macro_imports.back().first.insert( mod.m_macro_imports.back().first.begin(), p.path.m_crate_name );
+ mod.m_macro_imports.push_back({ false, p.path.m_components.back(), p.path.m_components, nullptr });
+ mod.m_macro_imports.back().path.insert( mod.m_macro_imports.back().path.begin(), p.path.m_crate_name );
}
}
)
diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp
index 34c47455..4dfd1224 100644
--- a/src/expand/mod.cpp
+++ b/src/expand/mod.cpp
@@ -1044,7 +1044,7 @@ void Expand_Mod(::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST::
}
}
for( const auto& mi: mod.m_macro_imports )
- DEBUG("- Imports '" << mi.first << "'");
+ DEBUG("- Imports '" << mi.path << "'");
}
// Insert prelude if: Enabled for this module, present for the crate, and this module is not an anon
diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp
index 23e722f4..49e5a4b6 100644
--- a/src/hir/from_ast.cpp
+++ b/src/hir/from_ast.cpp
@@ -1803,6 +1803,25 @@ public:
}
}
}
+ for( const auto& mac : crate.m_root_module.m_macro_imports )
+ {
+ if( mac.is_pub )
+ {
+ // TODO: Why does this to such a move?
+ auto v = ::std::make_pair( mac.name, MacroRulesPtr(new MacroRules( mv$(*const_cast<MacroRules*>(mac.macro_ptr)) )) );
+
+ auto it = macros.find(mac.name);
+ if( it == macros.end() )
+ {
+ auto res = macros.insert( mv$(v) );
+ DEBUG("- Import " << mac.name << "! (from \"" << res.first->second->m_source_crate << "\")");
+ }
+ else {
+ DEBUG("- Replace " << mac.name << "! (from \"" << it->second->m_source_crate << "\") with one from \"" << v.second->m_source_crate << "\"");
+ it->second = mv$( v.second );
+ }
+ }
+ }
}
// - Proc Macros
if( crate.m_crate_type == ::AST::Crate::Type::ProcMacro )
diff --git a/src/resolve/index.cpp b/src/resolve/index.cpp
index bbcd7e58..48f2d0da 100644
--- a/src/resolve/index.cpp
+++ b/src/resolve/index.cpp
@@ -212,7 +212,7 @@ void Resolve_Index_Module_Base(const AST::Crate& crate, AST::Module& mod)
_add_item_type(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide);
}}
// - Values
- TU_MATCH_HDRA( (i_data.path.m_bindings.value), {)
+ {TU_MATCH_HDRA( (i_data.path.m_bindings.value), {)
TU_ARMA(Unbound, _e) {
DEBUG(i.name << " - Not a value");
}
@@ -226,7 +226,23 @@ void Resolve_Index_Module_Base(const AST::Crate& crate, AST::Module& mod)
_add_item_value(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide);
TU_ARMA(Function, e)
_add_item_value(sp, mod, i.name, i.is_pub, i_data.path, !allow_collide);
- }
+ }}
+ // - Macros
+ {TU_MATCH_HDRA( (i_data.path.m_bindings.macro), {)
+ TU_ARMA(Unbound, _e) {
+ DEBUG(i.name << " - Not a macro");
+ }
+ TU_ARMA(MacroRules, e) {
+ ::std::vector<::std::string> path;
+ path.push_back( i_data.path.m_class.as_Absolute().crate );
+ for(const auto& node : i_data.path.m_class.as_Absolute().nodes )
+ path.push_back( node.name() );
+ mod.m_macro_imports.push_back({
+ i.is_pub, i.name, mv$(path), e.mac
+ });
+ }
+ // TODO: Other imports (e.g. derives, which have different naming structures)
+ }}
}
else
{