diff options
-rw-r--r-- | src/ast/dump.cpp | 9 | ||||
-rw-r--r-- | src/hir/deserialise.cpp | 11 | ||||
-rw-r--r-- | src/hir/from_ast.cpp | 23 | ||||
-rw-r--r-- | src/hir/hir.hpp | 6 | ||||
-rw-r--r-- | src/hir/serialise.cpp | 5 | ||||
-rw-r--r-- | src/trans/codegen_c.cpp | 16 |
6 files changed, 66 insertions, 4 deletions
diff --git a/src/ast/dump.cpp b/src/ast/dump.cpp index d6bc0aa2..a6c298b0 100644 --- a/src/ast/dump.cpp +++ b/src/ast/dump.cpp @@ -616,6 +616,15 @@ void RustPrinter::handle_module(const AST::Module& mod) for( const auto& item : mod.items() ) { + if( !item.data.is_ExternBlock() ) continue ; + const auto& e = item.data.as_ExternBlock(); + + print_attrs(item.data.attrs); + m_os << indent() << "extern \"" << e.abi() << "\" {}\n"; + } + + for( const auto& item : mod.items() ) + { if( !item.data.is_Module() ) continue ; const auto& e = item.data.as_Module(); diff --git a/src/hir/deserialise.cpp b/src/hir/deserialise.cpp index 1dbcc576..11a2a77e 100644 --- a/src/hir/deserialise.cpp +++ b/src/hir/deserialise.cpp @@ -119,6 +119,7 @@ namespace { ::HIR::GenericBound deserialise_genericbound(); ::HIR::Crate deserialise_crate(); + ::HIR::ExternLibrary deserialise_extlib(); ::HIR::Module deserialise_module(); ::HIR::TypeImpl deserialise_typeimpl() @@ -645,6 +646,8 @@ namespace { template<> DEF_D( ::MacroRulesPtr, return d.deserialise_macrorulesptr(); ) template<> DEF_D( unsigned int, return static_cast<unsigned int>(d.deserialise_count()); ) + template<> DEF_D( ::HIR::ExternLibrary, return d.deserialise_extlib(); ) + ::HIR::TypeRef HirDeserialiser::deserialise_type() { TRACE_FUNCTION; @@ -1041,6 +1044,12 @@ namespace { return rv; } + ::HIR::ExternLibrary HirDeserialiser::deserialise_extlib() + { + return ::HIR::ExternLibrary { + m_in.read_string() + }; + } ::HIR::Crate HirDeserialiser::deserialise_crate() { ::HIR::Crate rv; @@ -1078,6 +1087,8 @@ namespace { } } + rv.m_ext_libs = deserialise_vec< ::HIR::ExternLibrary>(); + return rv; } } diff --git a/src/hir/from_ast.cpp b/src/hir/from_ast.cpp index b9a98f58..1f1bc4e3 100644 --- a/src/hir/from_ast.cpp +++ b/src/hir/from_ast.cpp @@ -1172,7 +1172,28 @@ void _add_mod_val_item(::HIR::Module& mod, ::std::string name, bool is_pub, ::H { TODO(sp, "Expand ExternBlock"); } - // TODO: Insert a record of the `link` attribute + // Insert a record of the `link` attribute + for(const auto& a : item.data.attrs.m_items) + { + if( a.name() != "link" ) continue ; + + ::std::string name; + for(const auto& i : a.items()) + { + if( i.name() == "name" ) { + name = i.string(); + } + else { + } + } + if( name != "" ) + { + g_crate_ptr->m_ext_libs.push_back( ::HIR::ExternLibrary { name } ); + } + else { + ERROR(sp, E0000, "#[link] needs `name`"); + } + } ), (Impl, //TODO(sp, "Expand Item::Impl"); diff --git a/src/hir/hir.hpp b/src/hir/hir.hpp index c72fff4b..b87385a0 100644 --- a/src/hir/hir.hpp +++ b/src/hir/hir.hpp @@ -412,6 +412,11 @@ public: ::HIR::CratePtr m_data; ::std::string m_filename; }; +class ExternLibrary +{ +public: + ::std::string name; +}; class Crate { public: @@ -432,6 +437,7 @@ public: ::std::unordered_map< ::std::string, ::HIR::SimplePath> m_lang_items; ::std::unordered_map< ::std::string, ExternCrate> m_ext_crates; + ::std::vector<ExternLibrary> m_ext_libs; /// Method called to populate runtime state after deserialisation /// See hir/crate_post_load.cpp diff --git a/src/hir/serialise.cpp b/src/hir/serialise.cpp index 87cf5723..b568ff46 100644 --- a/src/hir/serialise.cpp +++ b/src/hir/serialise.cpp @@ -260,6 +260,11 @@ namespace { m_out.write_count(crate.m_ext_crates.size()); for(const auto& ext : crate.m_ext_crates) m_out.write_string(ext.first); + serialise_vec(crate.m_ext_libs); + } + void serialise(const ::HIR::ExternLibrary& lib) + { + m_out.write_string(lib.name); } void serialise_module(const ::HIR::Module& mod) { diff --git a/src/trans/codegen_c.cpp b/src/trans/codegen_c.cpp index c5e99987..0624ee62 100644 --- a/src/trans/codegen_c.cpp +++ b/src/trans/codegen_c.cpp @@ -108,7 +108,7 @@ namespace { } m_of.flush(); - + // Execute $CC with the required libraries ::std::vector<::std::string> tmp; ::std::vector<const char*> args; @@ -125,8 +125,17 @@ namespace { tmp.push_back(crate.second.m_filename + ".o"); args.push_back(tmp.back().c_str()); } - args.push_back("-lm"); - args.push_back("-ldl"); + for(const auto& lib : m_crate.m_ext_libs) { + ASSERT_BUG(Span(), lib.name != "", ""); + args.push_back("-l"); args.push_back(lib.name.c_str()); + } + for( const auto& crate : m_crate.m_ext_crates ) + { + for(const auto& lib : crate.second.m_data->m_ext_libs) { + ASSERT_BUG(Span(), lib.name != "", "Empty lib from " << crate.first); + args.push_back("-l"); args.push_back(lib.name.c_str()); + } + } args.push_back("-z"); args.push_back("muldefs"); } else @@ -139,6 +148,7 @@ namespace { { cmd_ss << "\"" << FmtEscaped(arg) << "\" "; } + DEBUG("- " << cmd_ss.str()); if( system(cmd_ss.str().c_str()) ) { abort(); |