summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Hodge <tpg@ucc.asn.au>2019-08-24 16:50:04 +0800
committerJohn Hodge <tpg@ucc.asn.au>2019-08-24 16:50:04 +0800
commit47b61b93c2ac841fe44d6cc8ca8fd91bd00b0e10 (patch)
tree050cfe1f1e05efa9d0555a3d0b391c27b5f73086 /src
parente8526a24a7a05b113584ad12ac6ad35865dc9aef (diff)
downloadmrust-47b61b93c2ac841fe44d6cc8ca8fd91bd00b0e10.tar.gz
HIR/Expand - Hack in proc_macro re-exports (this needs to be cleaner)
Diffstat (limited to 'src')
-rw-r--r--src/expand/macro_rules.cpp5
-rw-r--r--src/hir/deserialise.cpp7
-rw-r--r--src/hir/from_ast.cpp19
-rw-r--r--src/hir/hir.hpp35
-rw-r--r--src/hir/serialise.cpp5
-rw-r--r--src/resolve/index.cpp7
-rw-r--r--src/resolve/use.cpp8
7 files changed, 76 insertions, 10 deletions
diff --git a/src/expand/macro_rules.cpp b/src/expand/macro_rules.cpp
index 88b7fc88..7a49bcf6 100644
--- a/src/expand/macro_rules.cpp
+++ b/src/expand/macro_rules.cpp
@@ -65,6 +65,11 @@ class CMacroUseHandler:
mod.m_macro_imports.push_back(AST::Module::MacroImport{ 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 );
}
+ for(const auto& p : ec.m_hir->m_proc_macro_reexports)
+ {
+ mod.m_macro_imports.push_back(AST::Module::MacroImport{ /*is_pub=*/ false, p.first, p.second.path.m_components, nullptr });
+ mod.m_macro_imports.back().path.insert( mod.m_macro_imports.back().path.begin(), p.second.path.m_crate_name );
+ }
}
)
else TU_IFLET( ::AST::Item, i, Module, submod,
diff --git a/src/hir/deserialise.cpp b/src/hir/deserialise.cpp
index 5795c809..23b8fdf1 100644
--- a/src/hir/deserialise.cpp
+++ b/src/hir/deserialise.cpp
@@ -849,6 +849,12 @@
rv.generic = d.deserialise_vec< ::std::unique_ptr<T> >();
return rv;
)
+ template<>
+ DEF_D( ::HIR::Crate::MacroImport,
+ ::HIR::Crate::MacroImport rv;
+ rv.path = d.deserialise_simplepath();
+ return rv;
+ )
template<> DEF_D( ::HIR::ExternLibrary, return d.deserialise_extlib(); )
::HIR::LifetimeDef HirDeserialiser::deserialise_lifetimedef()
@@ -1340,6 +1346,7 @@
rv.m_marker_impls = deserialise_pathmap< ::HIR::Crate::ImplGroup<::HIR::MarkerImpl>>();
rv.m_exported_macros = deserialise_istrumap< ::MacroRulesPtr>();
+ rv.m_proc_macro_reexports = deserialise_istrumap< ::HIR::Crate::MacroImport>();
rv.m_lang_items = deserialise_strumap< ::HIR::SimplePath>();
{
diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp
index dde40e03..e271a809 100644
--- a/src/hir/from_ast.cpp
+++ b/src/hir/from_ast.cpp
@@ -1395,6 +1395,9 @@ void _add_mod_ns_item(::HIR::Module& mod, RcString name, ::HIR::Publicity is_pub
void _add_mod_val_item(::HIR::Module& mod, RcString name, ::HIR::Publicity is_pub, ::HIR::ValueItem ti) {
mod.m_value_items.insert( ::std::make_pair( mv$(name), ::make_unique_ptr(::HIR::VisEnt< ::HIR::ValueItem> { is_pub, mv$(ti) }) ) );
}
+void _add_mod_mac_item(::HIR::Module& mod, RcString name, ::HIR::Publicity is_pub, ::HIR::MacroItem ti) {
+ mod.m_macro_items.insert( ::std::make_pair( mv$(name), ::make_unique_ptr(::HIR::VisEnt< ::HIR::MacroItem> { is_pub, mv$(ti) }) ) );
+}
::HIR::Module LowerHIR_Module(const ::AST::Module& ast_mod, ::HIR::ItemPath path, ::std::vector< ::HIR::SimplePath> traits)
{
@@ -1590,6 +1593,16 @@ void _add_mod_val_item(::HIR::Module& mod, RcString name, ::HIR::Publicity is_pu
}
}
+ for( const auto& ie : ast_mod.m_macro_imports )
+ {
+ //const auto& sp = mod_span;
+ if( ie.is_pub )
+ {
+ auto mi = ::HIR::MacroItem::make_Import({ ::HIR::SimplePath(ie.path.front(), ::std::vector<RcString>(ie.path.begin()+1, ie.path.end())) });
+ _add_mod_mac_item( mod, ie.name, get_pub(true), mv$(mi) );
+ }
+ }
+
return mod;
}
@@ -1881,6 +1894,12 @@ public:
{
if( mac.is_pub )
{
+ if( !mac.macro_ptr ) {
+ // Add to the re-export list
+ auto path = ::HIR::SimplePath(mac.path.front(), ::std::vector<RcString>(mac.path.begin()+1, mac.path.end()));;
+ rv.m_proc_macro_reexports.insert( ::std::make_pair( mac.name, ::HIR::Crate::MacroImport { path } ));
+ continue ;
+ }
// 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)) )) );
diff --git a/src/hir/hir.hpp b/src/hir/hir.hpp
index 35373b51..06c6f740 100644
--- a/src/hir/hir.hpp
+++ b/src/hir/hir.hpp
@@ -38,6 +38,7 @@ class Static;
class ValueItem;
class TypeItem;
+class MacroItem;
class ItemPath;
@@ -382,6 +383,17 @@ public:
{}
};
+class ProcMacro
+{
+public:
+ // Name of the macro
+ RcString name;
+ // Path to the handler
+ ::HIR::SimplePath path;
+ // A list of attributes to hand to the handler
+ ::std::vector<::std::string> attributes;
+};
+
class Module
{
public:
@@ -392,6 +404,8 @@ public:
::std::unordered_map< RcString, ::std::unique_ptr<VisEnt<ValueItem>> > m_value_items;
// Contains types, traits, and modules
::std::unordered_map< RcString, ::std::unique_ptr<VisEnt<TypeItem>> > m_mod_items;
+ // Macros!
+ ::std::unordered_map< RcString, ::std::unique_ptr<VisEnt<MacroItem>> > m_macro_items;
::std::vector< ::std::pair<RcString, Static> > m_inline_statics;
@@ -422,6 +436,11 @@ TAGGED_UNION(ValueItem, Import,
(Function, Function),
(StructConstructor, struct { ::HIR::SimplePath ty; })
);
+TAGGED_UNION(MacroItem, Import,
+ (Import, struct { ::HIR::SimplePath path; }),
+ (MacroRules, MacroRulesPtr),
+ (ProcMacro, ProcMacro)
+ );
// --------------------------------------------------------------------
@@ -510,16 +529,6 @@ class ExternLibrary
public:
::std::string name;
};
-class ProcMacro
-{
-public:
- // Name of the macro
- RcString name;
- // Path to the handler
- ::HIR::SimplePath path;
- // A list of attributes to hand to the handler
- ::std::vector<::std::string> attributes;
-};
class Crate
{
public:
@@ -571,6 +580,12 @@ public:
/// Macros exported by this crate
::std::unordered_map< RcString, ::MacroRulesPtr > m_exported_macros;
+ /// Macros re-exported by this crate
+ struct MacroImport {
+ ::HIR::SimplePath path;
+ //bool is_proc_macro;
+ };
+ ::std::unordered_map< RcString, MacroImport > m_proc_macro_reexports;
/// Procedural macros presented
::std::vector< ::HIR::ProcMacro> m_proc_macros;
diff --git a/src/hir/serialise.cpp b/src/hir/serialise.cpp
index 272e176f..9acb6193 100644
--- a/src/hir/serialise.cpp
+++ b/src/hir/serialise.cpp
@@ -315,6 +315,10 @@
serialise_vec(ig.non_named);
serialise_vec(ig.generic);
}
+ void serialise(const ::HIR::Crate::MacroImport& e)
+ {
+ serialise(e.path);
+ }
void serialise_crate(const ::HIR::Crate& crate)
{
@@ -326,6 +330,7 @@
serialise_pathmap(crate.m_marker_impls);
serialise_strmap(crate.m_exported_macros);
+ serialise_strmap(crate.m_proc_macro_reexports);
{
decltype(crate.m_lang_items) lang_items_filtered;
for(const auto& ent : crate.m_lang_items)
diff --git a/src/resolve/index.cpp b/src/resolve/index.cpp
index 45fb58f5..bde59da8 100644
--- a/src/resolve/index.cpp
+++ b/src/resolve/index.cpp
@@ -456,6 +456,13 @@ void Resolve_Index_Module_Wildcard__use_stmt(AST::Crate& crate, AST::Module& dst
DEBUG("Glob crate " << i_data.path);
const auto& hmod = e.crate_->m_hir->m_root_module;
Resolve_Index_Module_Wildcard__glob_in_hir_mod(sp, crate, dst_mod, hmod, i_data.path, is_pub);
+ // TODO: Macros too
+ for(const auto& pm : e.crate_->m_hir->m_proc_macros)
+ {
+ dst_mod.m_macro_imports.push_back({
+ is_pub, pm.name, make_vec2(e.crate_->m_hir->m_crate_name, pm.name), nullptr
+ });
+ }
)
else TU_IFLET(::AST::PathBinding_Type, b, Module, e,
DEBUG("Glob mod " << i_data.path);
diff --git a/src/resolve/use.cpp b/src/resolve/use.cpp
index d1b96280..8a9a3167 100644
--- a/src/resolve/use.cpp
+++ b/src/resolve/use.cpp
@@ -751,6 +751,14 @@ namespace {
{
rv.macro = ::AST::PathBinding_Macro::make_MacroRules({ &ec, &*it->second });
}
+
+ {
+ auto it = ::std::find_if( ec.m_hir->m_proc_macros.begin(), ec.m_hir->m_proc_macros.end(), [&](const auto& pm){ return pm.name == name;} );
+ if( it != ec.m_hir->m_proc_macros.end() )
+ {
+ rv.macro = ::AST::PathBinding_Macro::make_ProcMacro({ &ec, name });
+ }
+ }
}
return rv;
}