diff options
author | John Hodge <tpg@mutabah.net> | 2016-08-27 17:01:20 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-08-27 17:01:20 +0800 |
commit | 392cbf69e8ed3538c16af66e976ffad48e8d767f (patch) | |
tree | fd7d4dc99bf737a268316b680494d866c2374cc8 /src/ast | |
parent | 042776beb6c68cd1fe6df75b8d5e5be4d873799f (diff) | |
download | mrust-392cbf69e8ed3538c16af66e976ffad48e8d767f.tar.gz |
AST - Roughing in `extern crate` loading infrastructure
Diffstat (limited to 'src/ast')
-rw-r--r-- | src/ast/crate.cpp | 46 | ||||
-rw-r--r-- | src/ast/crate.hpp | 24 |
2 files changed, 13 insertions, 57 deletions
diff --git a/src/ast/crate.cpp b/src/ast/crate.cpp index 571f9214..d0fa8217 100644 --- a/src/ast/crate.cpp +++ b/src/ast/crate.cpp @@ -4,6 +4,7 @@ #include "ast.hpp" #include "../parse/parseerror.hpp" #include "../expand/cfg.hpp" +#include <hir/hir.hpp> // HIR Crate #include <serialiser_texttree.hpp> @@ -50,59 +51,28 @@ void Crate::load_externs() const auto& name = c.name; if( check_item_cfg(it.data.attrs) ) { - TODO(it.data.span, "Load crate '" << name << "' as '" << it.name << "'"); + load_extern_crate( name ); } ) } }; iterate_module(m_root_module, cb); } - - -Module& Crate::get_root_module(const ::std::string& name) { - return const_cast<Module&>( const_cast<const Crate*>(this)->get_root_module(name) ); -} -const Module& Crate::get_root_module(const ::std::string& name) const { - if( name == "" ) - return m_root_module; - auto it = m_extern_crates.find(name); - if( it != m_extern_crates.end() ) - throw ::std::runtime_error("TODO: Get root module for extern crate"); -// return it->second.root_module(); - throw ParseError::Generic("crate name unknown"); -} - -void Crate::load_extern_crate(::std::string name) -{ - ::std::ifstream is("output/"+name+".ast"); - if( !is.is_open() ) - { - throw ParseError::Generic("Can't open crate '" + name + "'"); - } - //Deserialiser_TextTree ds(is); - //Deserialiser& d = ds; - - ExternCrate ret; - - // TODO: ... - - m_extern_crates.insert( make_pair(::std::move(name), ::std::move(ret)) ); -} - -ExternCrate::ExternCrate() +void Crate::load_extern_crate(const ::std::string& name) { + m_extern_crates.insert(::std::make_pair( name, ExternCrate { "output/lib"+name+".hir" } )); } -ExternCrate::ExternCrate(const char *path) +ExternCrate::ExternCrate(const ::std::string& path) { throw ParseError::Todo( FMT("Load extern crate from a file - '" << path << "'") ); } const MacroRules* ExternCrate::find_macro_rules(const ::std::string& name) { - auto i = m_mr_macros.find(name); - if(i != m_mr_macros.end()) - return &*i->second; + auto i = m_hir->m_exported_macros.find(name); + if(i != m_hir->m_exported_macros.end()) + return &i->second; return nullptr; } diff --git a/src/ast/crate.hpp b/src/ast/crate.hpp index 9407d8ad..1b4c5645 100644 --- a/src/ast/crate.hpp +++ b/src/ast/crate.hpp @@ -3,6 +3,7 @@ #include "ast.hpp" #include "types.hpp" +#include <hir/crate_ptr.hpp> namespace AST { @@ -43,40 +44,25 @@ public: Crate(); - Module& root_module() { return m_root_module; } - Module& get_root_module(const ::std::string& name); - ::std::map< ::std::string, ExternCrate>& extern_crates() { return m_extern_crates; } - const Module& root_module() const { return m_root_module; } - const Module& get_root_module(const ::std::string& name) const; - const ::std::map< ::std::string, ExternCrate>& extern_crates() const { return m_extern_crates; } + Module& root_module() { return m_root_module; } /// Load referenced crates void load_externs(); - void load_extern_crate(::std::string name); + void load_extern_crate(const ::std::string& name); }; /// Representation of an imported crate class ExternCrate { - ::std::map< ::std::string, MacroRulesPtr > m_mr_macros; - - //::MIR::Module m_root_module; - - //Crate m_crate; + ::HIR::CratePtr m_hir; public: - ExternCrate(); - ExternCrate(const char *path); + ExternCrate(const ::std::string& path); ExternCrate(const ExternCrate&) = delete; ExternCrate(ExternCrate&&) = default; const MacroRules* find_macro_rules(const ::std::string& name); - - //Crate& crate() { return m_crate; } - //const Crate& crate() const { return m_crate; } - //Module& root_module() { return m_crate.root_module(); } - //const Module& root_module() const { return m_crate.root_module(); } }; } // namespace AST |