summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn Hodge <tpg@mutabah.net>2017-01-08 21:12:32 +0800
committerJohn Hodge <tpg@mutabah.net>2017-01-08 21:12:32 +0800
commit24ba23f0a1a3bda18111e00ea49f1dd95a4556f0 (patch)
treea8a56124e6753d07647d7454caed39143cc1ab1c /src
parentb048548fe8f7f26abf35d501a0d85bb33aa37550 (diff)
downloadmrust-24ba23f0a1a3bda18111e00ea49f1dd95a4556f0.tar.gz
Trans - Enable translation of all public items in rlibs
Diffstat (limited to 'src')
-rw-r--r--src/hir/type.cpp4
-rw-r--r--src/main.cpp18
-rw-r--r--src/trans/codegen.cpp4
-rw-r--r--src/trans/codegen.hpp2
-rw-r--r--src/trans/codegen_c.cpp19
-rw-r--r--src/trans/enumerate.cpp99
-rw-r--r--src/trans/main_bindings.hpp2
7 files changed, 127 insertions, 21 deletions
diff --git a/src/hir/type.cpp b/src/hir/type.cpp
index 039af5a5..9b0bf766 100644
--- a/src/hir/type.cpp
+++ b/src/hir/type.cpp
@@ -593,6 +593,10 @@ bool ::HIR::TypeRef::match_test_generics(const Span& sp, const ::HIR::TypeRef& x
DEBUG("- Fuzzy match due to unbound - " << v << " = " << x);
return Compare::Fuzzy;
}
+ if( v.m_data.is_Path() && v.m_data.as_Path().binding.is_Unbound() ) {
+ DEBUG("- Fuzzy match due to unbound - " << v << " = " << x);
+ return Compare::Fuzzy;
+ }
// HACK: If the RHS is a placeholder generic, allow it.
if( x.m_data.is_Generic() && (x.m_data.as_Generic().binding >> 8) == 2 ) {
DEBUG("- Fuzzy match due to placeholder - " << v << " = " << x);
diff --git a/src/main.cpp b/src/main.cpp
index 5acb8545..5f1341ee 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -434,19 +434,19 @@ int main(int argc, char *argv[])
case ::AST::Crate::Type::Unknown:
// ERROR?
break;
- case ::AST::Crate::Type::RustLib:
+ case ::AST::Crate::Type::RustLib: {
+ TransList items = CompilePhase<TransList>("Trans Enumerate", [&]() { return Trans_Enumerate_Public(*hir_crate); });
+ CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile + ".c", *hir_crate, items, false); });
+ // Generate a .o
+ //HIR_Codegen_Lib(params.outfile + ".o", *hir_crate);
+ // Link metatdata and object into a .rlib
+
// Save a loadable HIR dump
CompilePhaseV("HIR Serialise", [&]() {
//HIR_Serialise(params.outfile + ".meta", *hir_crate);
HIR_Serialise(params.outfile, *hir_crate);
});
- //TransList items;
- //CompilePhaseV("Trans Enumerate", [&]() { Trans_Enumerate_Public(*hir_crate); });
- //CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile + ".o", *hir_crate, items); });
- // Generate a .o
- //HIR_Codegen_Lib(params.outfile + ".o", *hir_crate);
- // Link metatdata and object into a .rlib
- break;
+ break; }
case ::AST::Crate::Type::RustDylib:
// Save a loadable HIR dump
CompilePhaseV("HIR Serialise", [&]() { HIR_Serialise(params.outfile, *hir_crate); });
@@ -465,7 +465,7 @@ int main(int argc, char *argv[])
// - Enumerate items for translation
TransList items = CompilePhase<TransList>("Trans Enumerate", [&]() { return Trans_Enumerate_Main(*hir_crate); });
// - Perform codegen
- CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile, *hir_crate, items); });
+ CompilePhaseV("Trans Codegen", [&]() { Trans_Codegen(params.outfile, *hir_crate, items, true); });
// - Invoke linker?
break;
}
diff --git a/src/trans/codegen.cpp b/src/trans/codegen.cpp
index 5be07199..e2047e01 100644
--- a/src/trans/codegen.cpp
+++ b/src/trans/codegen.cpp
@@ -14,7 +14,7 @@
#include "codegen.hpp"
#include "monomorphise.hpp"
-void Trans_Codegen(const ::std::string& outfile, const ::HIR::Crate& crate, const TransList& list)
+void Trans_Codegen(const ::std::string& outfile, const ::HIR::Crate& crate, const TransList& list, bool is_executable)
{
static Span sp;
auto codegen = Trans_Codegen_GetGeneratorC(crate, outfile);
@@ -131,6 +131,6 @@ void Trans_Codegen(const ::std::string& outfile, const ::HIR::Crate& crate, cons
}
}
- codegen->finalise();
+ codegen->finalise(is_executable);
}
diff --git a/src/trans/codegen.hpp b/src/trans/codegen.hpp
index 984c4742..e24fdb13 100644
--- a/src/trans/codegen.hpp
+++ b/src/trans/codegen.hpp
@@ -26,7 +26,7 @@ class CodeGenerator
{
public:
virtual ~CodeGenerator() {}
- virtual void finalise() {}
+ virtual void finalise(bool is_executable) {}
// Called on all types directly mentioned (e.g. variables, arguments, and fields)
// - Inner-most types are visited first.
diff --git a/src/trans/codegen_c.cpp b/src/trans/codegen_c.cpp
index 46c267df..df6ac636 100644
--- a/src/trans/codegen_c.cpp
+++ b/src/trans/codegen_c.cpp
@@ -78,7 +78,7 @@ namespace {
~CodeGenerator_C() {}
- void finalise() override
+ void finalise(bool is_executable) override
{
// Emit box drop glue after everything else to avoid definition ordering issues
for(auto& e : m_box_glue_todo)
@@ -86,12 +86,17 @@ namespace {
emit_box_drop_glue( mv$(e.first), *e.second );
}
- m_of
- << "int main(int argc, const char* argv[]) {\n"
- << "\t" << Trans_Mangle( ::HIR::GenericPath(m_resolve.m_crate.get_lang_item_path(Span(), "start")) ) << "("
- << "(uint8_t*)" << Trans_Mangle( ::HIR::GenericPath(::HIR::SimplePath("", {"main"})) ) << ", argc, (uint8_t**)argv"
- << ");\n"
- << "}\n";
+ if( is_executable )
+ {
+ m_of
+ << "int main(int argc, const char* argv[]) {\n"
+ << "\t" << Trans_Mangle( ::HIR::GenericPath(m_resolve.m_crate.get_lang_item_path(Span(), "start")) ) << "("
+ << "(uint8_t*)" << Trans_Mangle( ::HIR::GenericPath(::HIR::SimplePath("", {"main"})) ) << ", argc, (uint8_t**)argv"
+ << ");\n"
+ << "}\n";
+ }
+
+ // Execute $CC with the required libraries
}
void emit_box_drop_glue(::HIR::GenericPath p, const ::HIR::Struct& item)
diff --git a/src/trans/enumerate.cpp b/src/trans/enumerate.cpp
index 899b3b82..589c8313 100644
--- a/src/trans/enumerate.cpp
+++ b/src/trans/enumerate.cpp
@@ -105,21 +105,118 @@ namespace {
{
state.enum_fcn(mod_path + vi.first, e, {});
}
+ // TODO: If generic, enumerate concrete functions used
)
)
}
}
+
+ for(const auto& ti : mod.m_mod_items)
+ {
+ if( ti.second->is_public && ti.second->ent.is_Module() )
+ {
+ Trans_Enumerate_Public_Mod(state, ti.second->ent.as_Module(), mod_path + ti.first);
+ }
+ }
}
}
/// Enumerate trans items for all public non-generic items (library crate)
TransList Trans_Enumerate_Public(const ::HIR::Crate& crate)
{
+ static Span sp;
EnumState state { crate };
Trans_Enumerate_Public_Mod(state, crate.m_root_module, ::HIR::SimplePath("",{}));
- return Trans_Enumerate_CommonPost(state);
+ // Impl blocks
+ for(const auto& impl : crate.m_trait_impls)
+ {
+ if( impl.second.m_params.m_types.size() > 0 )
+ continue ;
+
+ // Emit each method/static (in the trait itself)
+ const auto& trait = crate.get_trait_by_path(sp, impl.first);
+ for(const auto& vi : trait.m_values)
+ {
+ if( vi.second.is_Constant() )
+ ;
+ else if( vi.second.is_Function() && vi.second.as_Function().m_params.m_types.size() > 0 )
+ ;
+ else if( vi.first == "#vtable" )
+ ;
+ else
+ {
+ auto p = ::HIR::Path(impl.second.m_type.clone(), ::HIR::GenericPath(impl.first, impl.second.m_trait_args.clone()), vi.first);
+ Trans_Enumerate_FillFrom_Path(state, p, {});
+ }
+ }
+ }
+
+ auto rv = Trans_Enumerate_CommonPost(state);
+
+ struct H
+ {
+ static bool is_generic(const ::HIR::TypeRef& ty)
+ {
+ return visit_ty_with(ty, [&](const auto& ty) {
+ return ty.m_data.is_Generic();
+ });
+ }
+ static bool is_generic(const ::HIR::PathParams& pp)
+ {
+ for(const auto& ty : pp.m_types)
+ if( is_generic(ty) )
+ return true;
+ return false;
+ }
+ static bool is_generic(const ::HIR::Path& p)
+ {
+ TU_MATCHA( (p.m_data), (pe),
+ (Generic,
+ return is_generic(pe.m_params);
+ ),
+ (UfcsKnown,
+ if( is_generic(*pe.type) )
+ return true;
+ if( is_generic(pe.trait.m_params) )
+ return true;
+ if( is_generic(pe.params) )
+ return true;
+ ),
+ (UfcsInherent,
+ if( is_generic(*pe.type) )
+ return true;
+ if( is_generic(pe.params) )
+ return true;
+ ),
+ (UfcsUnknown,
+ )
+ )
+ return false;
+ }
+ };
+
+ // Strip out any functions/types/statics that are still generic?
+ for(auto it = rv.m_functions.begin(); it != rv.m_functions.end(); )
+ {
+ if( H::is_generic(it->first) ) {
+ rv.m_functions.erase(it++);
+ }
+ else {
+ ++ it;
+ }
+ }
+ for(auto it = rv.m_statics.begin(); it != rv.m_statics.end(); )
+ {
+ if( H::is_generic(it->first) ) {
+ rv.m_statics.erase(it++);
+ }
+ else {
+ ++ it;
+ }
+ }
+ return rv;
}
diff --git a/src/trans/main_bindings.hpp b/src/trans/main_bindings.hpp
index b8391cb8..f4ffff96 100644
--- a/src/trans/main_bindings.hpp
+++ b/src/trans/main_bindings.hpp
@@ -16,4 +16,4 @@ class Crate;
extern TransList Trans_Enumerate_Main(const ::HIR::Crate& crate);
extern TransList Trans_Enumerate_Public(const ::HIR::Crate& crate);
-extern void Trans_Codegen(const ::std::string& outfile, const ::HIR::Crate& crate, const TransList& list);
+extern void Trans_Codegen(const ::std::string& outfile, const ::HIR::Crate& crate, const TransList& list, bool is_executable);