diff options
author | John Hodge <tpg@ucc.asn.au> | 2017-11-19 16:16:35 +0800 |
---|---|---|
committer | John Hodge <tpg@ucc.asn.au> | 2017-11-19 16:16:35 +0800 |
commit | 128681501534a77c371c63c9787ba23f5df3743b (patch) | |
tree | 4b32c600ebc1047c8fa2e81ab6ff32c1c7aa8249 | |
parent | 183153147d29e08a01e2930bbccf894917bc2a14 (diff) | |
download | mrust-128681501534a77c371c63c9787ba23f5df3743b.tar.gz |
All - Include proc_macro attribute list in metadata
-rw-r--r-- | src/ast/crate.hpp | 10 | ||||
-rw-r--r-- | src/expand/macro_rules.cpp | 4 | ||||
-rw-r--r-- | src/expand/proc_macro.cpp | 64 | ||||
-rw-r--r-- | src/hir/deserialise.cpp | 15 | ||||
-rw-r--r-- | src/hir/from_ast.cpp | 2 | ||||
-rw-r--r-- | src/hir/hir.hpp | 12 | ||||
-rw-r--r-- | src/hir/serialise.cpp | 11 | ||||
-rw-r--r-- | src/hir/serialise_lowlevel.cpp | 1 |
8 files changed, 92 insertions, 27 deletions
diff --git a/src/ast/crate.hpp b/src/ast/crate.hpp index 67b4915e..b8d7be16 100644 --- a/src/ast/crate.hpp +++ b/src/ast/crate.hpp @@ -27,6 +27,14 @@ public: ::std::string expected_panic_message; }; +class ProcMacroDef +{ +public: + ::std::string name; + ::AST::Path path; + ::std::vector<::std::string> attributes; +}; + class Crate { public: @@ -44,7 +52,7 @@ public: ::std::vector<TestDesc> m_tests; // Procedural macros! - ::std::vector<::std::pair< ::std::string, ::AST::Path>> m_proc_macros; + ::std::vector<ProcMacroDef> m_proc_macros; enum class Type { Unknown, diff --git a/src/expand/macro_rules.cpp b/src/expand/macro_rules.cpp index e297d670..5b783949 100644 --- a/src/expand/macro_rules.cpp +++ b/src/expand/macro_rules.cpp @@ -62,8 +62,8 @@ class CMacroUseHandler: }); for(const auto& p : ec.m_hir->m_proc_macros) { - mod.m_macro_imports.push_back(::std::make_pair( p.m_components, nullptr )); - mod.m_macro_imports.back().first.insert( mod.m_macro_imports.back().first.begin(), p.m_crate_name ); + 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 ); } } ) diff --git a/src/expand/proc_macro.cpp b/src/expand/proc_macro.cpp index 7752162c..18d0ab3e 100644 --- a/src/expand/proc_macro.cpp +++ b/src/expand/proc_macro.cpp @@ -50,7 +50,7 @@ public: } // TODO: Store attributes for later use. - crate.m_proc_macros.push_back(::std::make_pair( FMT("derive#" << trait_name), path )); + crate.m_proc_macros.push_back(AST::ProcMacroDef { FMT("derive#" << trait_name), path, mv$(attributes) }); } }; @@ -95,9 +95,9 @@ void Expand_ProcMacro(::AST::Crate& crate) { ::AST::ExprNode_StructLiteral::t_values desc_vals; // `name: "foo",` - desc_vals.push_back({ {}, "name", NEWNODE(_String, desc.first) }); + desc_vals.push_back({ {}, "name", NEWNODE(_String, desc.name) }); // `handler`: ::foo - desc_vals.push_back({ {}, "handler", NEWNODE(_NamedValue, AST::Path(desc.second)) }); + desc_vals.push_back({ {}, "handler", NEWNODE(_NamedValue, AST::Path(desc.path)) }); test_nodes.push_back( NEWNODE(_StructLiteral, ::AST::Path("proc_macro", { ::AST::PathNode("MacroDesc")}), nullptr, mv$(desc_vals) ) ); } @@ -155,6 +155,7 @@ struct ProcMacroInv: public TokenStream { Span m_parent_span; + const ::HIR::ProcMacro& m_proc_macro_desc; #ifdef WIN32 HANDLE child_handle; @@ -170,11 +171,11 @@ struct ProcMacroInv: bool m_eof_hit = false; public: - ProcMacroInv(const Span& sp, const char* executable, const char* macro_name); + ProcMacroInv(const Span& sp, const char* executable, const ::HIR::ProcMacro& proc_macro_desc); ProcMacroInv(const ProcMacroInv&) = delete; ProcMacroInv(ProcMacroInv&&); ProcMacroInv& operator=(const ProcMacroInv&) = delete; - ProcMacroInv& operator=(ProcMacroInv&&); + ProcMacroInv& operator=(ProcMacroInv&&) = delete; virtual ~ProcMacroInv(); bool check_good(); @@ -224,18 +225,34 @@ ProcMacroInv ProcMacro_Invoke_int(const Span& sp, const ::AST::Crate& crate, con const auto& crate_name = mac_path.front(); const auto& ext_crate = crate.m_extern_crates.at(crate_name); // TODO: Ensure that this macro is in the listed crate. + const ::HIR::ProcMacro* pmp = nullptr; + for(const auto& pm : ext_crate.m_hir->m_proc_macros) + { + bool good = true; + for(size_t i = 0; i < ::std::min( mac_path.size()-1, pm.path.m_components.size() ); i++) + { + if( mac_path[1+i] != pm.path.m_components[i] ) + { + good = false; + break; + } + } + if(good) + { + pmp = ± + break; + } + } + if( !pmp ) + { + ERROR(sp, E0000, "Unable to find referenced proc macro " << mac_path); + } // 2. Get executable and macro name ::std::string proc_macro_exe_name = (ext_crate.m_filename + "-plugin"); - ::std::string mac_name = mac_path.at(1); - for(size_t i = 2; i < mac_path.size(); i ++) - { - mac_name += "::"; - mac_name += mac_path[i]; - } // 3. Create ProcMacroInv - return ProcMacroInv(sp, proc_macro_exe_name.c_str(), mac_name.c_str()); + return ProcMacroInv(sp, proc_macro_exe_name.c_str(), *pmp); } @@ -583,6 +600,9 @@ namespace { TODO(sp, "ExprNode_UniOp"); } + void visit_attrs(const ::AST::MetaItems& attrs) + { + } void visit_struct(const ::std::string& name, bool is_pub, const ::AST::Struct& str) { if( is_pub ) { @@ -601,7 +621,7 @@ namespace { m_pmi.send_symbol("("); for( const auto& si : se.ents ) { - // TODO: Attributes. + this->visit_attrs(si.m_attrs); if( si.m_is_public ) m_pmi.send_ident("pub"); this->visit_type(si.m_type); @@ -617,7 +637,7 @@ namespace { for( const auto& si : se.ents ) { - // TODO: Attributes. + this->visit_attrs(si.m_attrs); if( si.m_is_public ) m_pmi.send_ident("pub"); m_pmi.send_ident(si.m_name.c_str()); @@ -642,7 +662,7 @@ namespace { m_pmi.send_symbol("{"); for(const auto& v : enm.variants()) { - // TODO: Attributes. + this->visit_attrs(v.m_attrs); m_pmi.send_ident(v.m_name.c_str()); TU_MATCH(AST::EnumVariantData, (v.m_data), (e), (Value, @@ -653,7 +673,7 @@ namespace { m_pmi.send_symbol("("); for(const auto& st : e.m_sub_types) { - // TODO: Attributes? + // TODO: Attributes? (None stored in tuple variants) this->visit_type(st); m_pmi.send_symbol(","); } @@ -663,7 +683,7 @@ namespace { m_pmi.send_symbol("{"); for(const auto& f : e.m_fields) { - // TODO: Attributes? + this->visit_attrs(f.m_attrs); m_pmi.send_ident(f.m_name.c_str()); m_pmi.send_symbol(":"); this->visit_type(f.m_type); @@ -719,8 +739,9 @@ namespace { return box$(pmi); } -ProcMacroInv::ProcMacroInv(const Span& sp, const char* executable, const char* macro_name): - m_parent_span(sp) +ProcMacroInv::ProcMacroInv(const Span& sp, const char* executable, const ::HIR::ProcMacro& proc_macro_desc): + m_parent_span(sp), + m_proc_macro_desc(proc_macro_desc) { #ifdef _WIN32 #else @@ -740,7 +761,7 @@ ProcMacroInv::ProcMacroInv(const Span& sp, const char* executable, const char* m posix_spawn_file_actions_addclose(&file_actions, stdout_pipes[0]); posix_spawn_file_actions_addclose(&file_actions, stdout_pipes[1]); - char* argv[3] = { const_cast<char*>(executable), const_cast<char*>(macro_name), nullptr }; + char* argv[3] = { const_cast<char*>(executable), const_cast<char*>(proc_macro_desc.name.c_str()), nullptr }; //char* envp[] = { nullptr }; int rv = posix_spawn(&this->child_pid, executable, &file_actions, nullptr, argv, environ); if( rv != 0 ) @@ -757,6 +778,7 @@ ProcMacroInv::ProcMacroInv(const Span& sp, const char* executable, const char* m } ProcMacroInv::ProcMacroInv(ProcMacroInv&& x): m_parent_span(x.m_parent_span), + m_proc_macro_desc(x.m_proc_macro_desc), #ifdef _WIN32 child_handle(x.child_handle), child_stdin(x.child_stdin), @@ -773,6 +795,7 @@ ProcMacroInv::ProcMacroInv(ProcMacroInv&& x): #endif DEBUG(""); } +#if 0 ProcMacroInv& ProcMacroInv::operator=(ProcMacroInv&& x) { m_parent_span = x.m_parent_span; @@ -787,6 +810,7 @@ ProcMacroInv& ProcMacroInv::operator=(ProcMacroInv&& x) DEBUG(""); return *this; } +#endif ProcMacroInv::~ProcMacroInv() { #ifdef _WIN32 diff --git a/src/hir/deserialise.cpp b/src/hir/deserialise.cpp index 8023be3c..b7cba83b 100644 --- a/src/hir/deserialise.cpp +++ b/src/hir/deserialise.cpp @@ -84,6 +84,7 @@ namespace { { TRACE_FUNCTION_F("<" << typeid(T).name() << ">"); size_t n = m_in.read_count(); + DEBUG("n = " << n); ::std::vector<T> rv; rv.reserve(n); for(size_t i = 0; i < n; i ++) @@ -128,6 +129,17 @@ namespace { ::HIR::ExternLibrary deserialise_extlib(); ::HIR::Module deserialise_module(); + ::HIR::ProcMacro deserialise_procmacro() + { + ::HIR::ProcMacro pm; + TRACE_FUNCTION_FR("", "ProcMacro { " << pm.name << ", " << pm.path << ", [" << pm.attributes << "]}"); + pm.name = m_in.read_string(); + pm.path = deserialise_simplepath(); + pm.attributes = deserialise_vec< ::std::string>(); + DEBUG("pm = ProcMacro { " << pm.name << ", " << pm.path << ", [" << pm.attributes << "]}"); + return pm; + } + ::HIR::TypeImpl deserialise_typeimpl() { ::HIR::TypeImpl rv; @@ -699,6 +711,7 @@ namespace { template<> DEF_D( ::MIR::Statement, return d.deserialise_mir_statement(); ) template<> DEF_D( ::MIR::BasicBlock, return d.deserialise_mir_basicblock(); ) + template<> DEF_D( ::HIR::ProcMacro, return d.deserialise_procmacro(); ) template<> DEF_D( ::HIR::TypeImpl, return d.deserialise_typeimpl(); ) template<> DEF_D( ::MacroRulesPtr, return d.deserialise_macrorulesptr(); ) template<> DEF_D( unsigned int, return static_cast<unsigned int>(d.deserialise_count()); ) @@ -1210,7 +1223,7 @@ namespace { rv.m_ext_libs = deserialise_vec< ::HIR::ExternLibrary>(); rv.m_link_paths = deserialise_vec< ::std::string>(); - rv.m_proc_macros = deserialise_vec< ::HIR::SimplePath>(); + rv.m_proc_macros = deserialise_vec< ::HIR::ProcMacro>(); return rv; } diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp index 040d743b..f413e21e 100644 --- a/src/hir/from_ast.cpp +++ b/src/hir/from_ast.cpp @@ -1708,7 +1708,7 @@ public: for(const auto& ent : crate.m_proc_macros) { // Register under an invalid simplepath - rv.m_proc_macros.push_back( ::HIR::SimplePath("", { ent.first }) ); + rv.m_proc_macros.push_back( ::HIR::ProcMacro { ent.name, ::HIR::SimplePath("", { ent.name}), ent.attributes } ); } } else diff --git a/src/hir/hir.hpp b/src/hir/hir.hpp index a04fd1da..a7c8c6e8 100644 --- a/src/hir/hir.hpp +++ b/src/hir/hir.hpp @@ -455,6 +455,16 @@ class ExternLibrary public: ::std::string name; }; +class ProcMacro +{ +public: + // Name of the macro + ::std::string 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: @@ -471,7 +481,7 @@ public: /// Macros exported by this crate ::std::unordered_map< ::std::string, ::MacroRulesPtr > m_exported_macros; /// Procedural macros presented - ::std::vector<::HIR::SimplePath> m_proc_macros; + ::std::vector< ::HIR::ProcMacro> m_proc_macros; /// Language items avaliable through this crate (includes ones from loaded externs) ::std::unordered_map< ::std::string, ::HIR::SimplePath> m_lang_items; diff --git a/src/hir/serialise.cpp b/src/hir/serialise.cpp index 2705293c..c31c84fc 100644 --- a/src/hir/serialise.cpp +++ b/src/hir/serialise.cpp @@ -53,6 +53,7 @@ namespace { template<typename T> void serialise_vec(const ::std::vector<T>& vec) { + TRACE_FUNCTION_F("size=" << vec.size()); m_out.write_count(vec.size()); for(const auto& i : vec) serialise(i); @@ -155,7 +156,7 @@ namespace { } void serialise_simplepath(const ::HIR::SimplePath& path) { - DEBUG(path); + TRACE_FUNCTION_F(path); m_out.write_string(path.m_crate_name); serialise_vec(path.m_components); } @@ -242,6 +243,14 @@ namespace { } + void serialise(const ::HIR::ProcMacro& pm) + { + TRACE_FUNCTION_F("pm = ProcMacro { " << pm.name << ", " << pm.path << ", [" << pm.attributes << "] }"); + serialise(pm.name); + serialise(pm.path); + serialise_vec(pm.attributes); + } + void serialise_crate(const ::HIR::Crate& crate) { m_out.write_string(crate.m_crate_name); diff --git a/src/hir/serialise_lowlevel.cpp b/src/hir/serialise_lowlevel.cpp index 43798be2..cf8f5506 100644 --- a/src/hir/serialise_lowlevel.cpp +++ b/src/hir/serialise_lowlevel.cpp @@ -5,6 +5,7 @@ * hir/serialise_lowlevel.cpp * - HIR (De)Serialisation low-level "protocol" */ +#include <debug.hpp> #include "serialise_lowlevel.hpp" #include <zlib.h> #include <fstream> |