summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2016-03-08 09:32:30 +0800
committerJohn Hodge <tpg@mutabah.net>2016-03-08 09:32:30 +0800
commit8438c45edf9ee1b561ccfec304d2a36b0671e311 (patch)
tree484713ae5352d575d07a84a28f3fb678584e6368 /src
parent8c53ebf221d750a19b0f860584f069b8c3b1733e (diff)
downloadmrust-8438c45edf9ee1b561ccfec304d2a36b0671e311.tar.gz
Expand - Macro imports
Diffstat (limited to 'src')
-rw-r--r--src/ast/path.cpp2
-rw-r--r--src/expand/macro_rules.cpp10
-rw-r--r--src/expand/mod.cpp30
3 files changed, 32 insertions, 10 deletions
diff --git a/src/ast/path.cpp b/src/ast/path.cpp
index 24318312..032c57b4 100644
--- a/src/ast/path.cpp
+++ b/src/ast/path.cpp
@@ -138,7 +138,7 @@ AST::Path::Path(const Path& x):
//(Variable, os << "Variable(" << i.slot << ")"; )
//)
- DEBUG("clone, x = " << x << ", this = " << *this );
+ //DEBUG("clone, x = " << x << ", this = " << *this );
}
/*
diff --git a/src/expand/macro_rules.cpp b/src/expand/macro_rules.cpp
index 4ef39c01..fac6dec1 100644
--- a/src/expand/macro_rules.cpp
+++ b/src/expand/macro_rules.cpp
@@ -32,6 +32,7 @@ class CMacroUseHandler:
void handle(const AST::MetaItem& mi, AST::Crate& crate, const AST::Path& path, AST::Module& mod, AST::Item& i) const override
{
+ TRACE_FUNCTION_F("path=" << path);
if( !i.is_Module() )
throw ::std::runtime_error("ERROR: Use of #[macro_use] on non-module");
@@ -39,16 +40,21 @@ class CMacroUseHandler:
if( mi.has_sub_items() )
{
+ throw ::std::runtime_error("TODO: #[macro_use]");
}
else
{
for( const auto& mr : submod.macros() )
{
+ DEBUG("Imported " << mr.name);
mod.add_macro_import( mr.name, mr.data );
}
+ for( const auto& mri : submod.macro_imports_res() )
+ {
+ DEBUG("Imported " << mri.name << " (propagate)");
+ mod.add_macro_import( mri.name, *mri.data );
+ }
}
-
- throw ::std::runtime_error("TODO: #[macro_use]");
}
};
diff --git a/src/expand/mod.cpp b/src/expand/mod.cpp
index 5c733e2b..3f35d4dc 100644
--- a/src/expand/mod.cpp
+++ b/src/expand/mod.cpp
@@ -23,7 +23,7 @@ void Expand_Attrs(const ::AST::MetaItems& attrs, AttrStage stage, ::AST::Crate&
{
for( auto& d : g_decorators ) {
if( d.first == a.name() ) {
- DEBUG("#[" << d.first << "]");
+ DEBUG("#[" << d.first << "] " << (int)d.second->stage() << "-" << (int)stage);
if( d.second->stage() == stage ) {
d.second->handle(a, crate, path, mod, item);
}
@@ -32,7 +32,7 @@ void Expand_Attrs(const ::AST::MetaItems& attrs, AttrStage stage, ::AST::Crate&
}
}
-AST::Expr Expand_Macro(bool is_early, ::AST::Module& mod, ::AST::MacroInvocation& mi, MacroPosition pos)
+AST::Expr Expand_Macro(bool is_early, LList<const AST::Module*> modstack, ::AST::Module& mod, ::AST::MacroInvocation& mi, MacroPosition pos)
{
for( const auto& m : g_macros )
{
@@ -45,8 +45,12 @@ AST::Expr Expand_Macro(bool is_early, ::AST::Module& mod, ::AST::MacroInvocation
}
+ //for(auto mp: modstack)
+ //{
+ //}
for( const auto& mr : mod.macros() )
{
+ //DEBUG("- " << mr.name);
if( mr.name == mi.name() )
{
if( mi.input_ident() != "" )
@@ -59,6 +63,7 @@ AST::Expr Expand_Macro(bool is_early, ::AST::Module& mod, ::AST::MacroInvocation
}
for( const auto& mri : mod.macro_imports_res() )
{
+ //DEBUG("- " << mri.name);
if( mri.name == mi.name() )
{
if( mi.input_ident() != "" )
@@ -70,8 +75,11 @@ AST::Expr Expand_Macro(bool is_early, ::AST::Module& mod, ::AST::MacroInvocation
}
}
+ // TODO: Check parent modules
+
if( ! is_early ) {
// TODO: Error - Unknown macro name
+ // TODO: Get a span via MacroInvocation and emit a good error
throw ::std::runtime_error( FMT("Unknown macro '" << mi.name() << "'") );
}
@@ -79,21 +87,25 @@ AST::Expr Expand_Macro(bool is_early, ::AST::Module& mod, ::AST::MacroInvocation
return AST::Expr();
}
-void Expand_Mod(bool is_early, ::AST::Crate& crate, ::AST::Path modpath, ::AST::Module& mod)
+void Expand_Mod(bool is_early, ::AST::Crate& crate, LList<const AST::Module*> modstack, ::AST::Path modpath, ::AST::Module& mod)
{
TRACE_FUNCTION_F("is_early = " << is_early << ", modpath = " << modpath);
+ for( const auto& mi: mod.macro_imports_res() )
+ DEBUG("- Imports '" << mi.name << "'");
+
// 1. Macros first
//for( auto& mi : mod.macro_invs() )
for(unsigned int i = 0; i < mod.macro_invs().size(); i ++ )
{
auto& mi = mod.macro_invs()[i];
+ DEBUG("> Macro invoke '"<<mi.name()<<"'");
if( mi.name() != "" )
{
// Move out of the module to avoid invalidation if a new macro invocation is added
auto mi_owned = mv$(mi);
- auto rv = Expand_Macro(is_early, mod, mi_owned, MacroPosition::Item);
+ auto rv = Expand_Macro(is_early, modstack, mod, mi_owned, MacroPosition::Item);
if( rv.is_valid() )
; // TODO: ERROR - macro yeilded an expression in item position
@@ -118,7 +130,8 @@ void Expand_Mod(bool is_early, ::AST::Crate& crate, ::AST::Path modpath, ::AST::
// Skip, nothing
),
(Module,
- Expand_Mod(is_early, crate, path, e.e);
+ LList<const AST::Module*> sub_modstack(&modstack, &e.e);
+ Expand_Mod(is_early, crate, sub_modstack, path, e.e);
),
(Crate,
// Skip, no recursion
@@ -145,6 +158,9 @@ void Expand_Mod(bool is_early, ::AST::Crate& crate, ::AST::Path modpath, ::AST::
Expand_Attrs(i.data.attrs, (is_early ? AttrStage::EarlyPost : AttrStage::LatePost), crate, path, mod, i.data);
}
+
+ for( const auto& mi: mod.macro_imports_res() )
+ DEBUG("- Imports '" << mi.name << "'");
// 3. Post-recurse macros (everything else)
}
@@ -171,8 +187,8 @@ void Expand(::AST::Crate& crate)
}
// 3. Module tree
- Expand_Mod(true , crate, ::AST::Path(), crate.m_root_module);
- Expand_Mod(false, crate, ::AST::Path(), crate.m_root_module);
+ Expand_Mod(true , crate, LList<const ::AST::Module*>(nullptr, &crate.m_root_module), ::AST::Path(), crate.m_root_module);
+ Expand_Mod(false, crate, LList<const ::AST::Module*>(nullptr, &crate.m_root_module), ::AST::Path(), crate.m_root_module);
// Post-process
#if 0