Skip to content

Commit

Permalink
HIR Expand Closures - Move closure prefix to a common location
Browse files Browse the repository at this point in the history
  • Loading branch information
thepowersgang committed Nov 3, 2019
1 parent 0a2ea1a commit 56c9b3f
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/hir/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
/// Binding index for a Generic that indicates "Self"
#define GENERIC_Self 0xFFFF

constexpr const char* CLOSURE_PATH_PREFIX = "closure#";

namespace HIR {

class TraitMarkings;
Expand Down Expand Up @@ -209,7 +211,7 @@ class TypeRef
bool is_closure() const {
return path.m_data.is_Generic()
&& path.m_data.as_Generic().m_path.m_components.back().size() > 8
&& path.m_data.as_Generic().m_path.m_components.back().compare(0,8, "closure#") == 0
&& path.m_data.as_Generic().m_path.m_components.back().compare(0,strlen(CLOSURE_PATH_PREFIX), CLOSURE_PATH_PREFIX) == 0
;
}
}),
Expand Down
6 changes: 3 additions & 3 deletions src/hir_expand/closures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ namespace {
::HIR::SimplePath root_mod_path(crate.m_crate_name,{});
m_cur_mod_path = &root_mod_path;
m_new_type = [&](const char* suffix, auto s)->auto {
auto name = RcString::new_interned(FMT("closure#I_" << closure_count));
auto name = RcString::new_interned(FMT(CLOSURE_PATH_PREFIX << "I_" << closure_count));
closure_count += 1;
auto boxed = box$(( ::HIR::VisEnt< ::HIR::TypeItem> { ::HIR::Publicity::new_none(), ::HIR::TypeItem( mv$(s) ) } ));
auto* ret_ptr = &boxed->ent.as_Struct();
Expand All @@ -1287,7 +1287,7 @@ namespace {
auto saved_nt = mv$(m_new_type);
m_new_type = [&](const char* suffix, auto s)->auto {
// TODO: Use a function on `mod` that adds a closure and makes the indexes be per suffix
auto name = RcString( FMT("closure#" << suffix << (suffix[0] ? "_" : "") << closure_count) );
auto name = RcString( FMT(CLOSURE_PATH_PREFIX << suffix << (suffix[0] ? "_" : "") << closure_count) );
closure_count += 1;
auto boxed = box$( (::HIR::VisEnt< ::HIR::TypeItem> { ::HIR::Publicity::new_none(), ::HIR::TypeItem( mv$(s) ) }) );
auto* ret_ptr = &boxed->ent.as_Struct();
Expand Down Expand Up @@ -1437,7 +1437,7 @@ void HIR_Expand_Closures_Expr(const ::HIR::Crate& crate_ro, ::HIR::ExprPtr& exp)
static int closure_count = 0;
out_impls_t new_trait_impls;
new_type_cb_t new_type_cb = [&](const char* suffix, auto s)->auto {
auto name = RcString::new_interned(FMT("closure#C_" << closure_count));
auto name = RcString::new_interned(FMT(CLOSURE_PATH_PREFIX << "C_" << closure_count));
closure_count += 1;
auto boxed = box$(( ::HIR::VisEnt< ::HIR::TypeItem> { ::HIR::Publicity::new_none(), ::HIR::TypeItem( mv$(s) ) } ));
auto* ret_ptr = &boxed->ent.as_Struct();
Expand Down
3 changes: 1 addition & 2 deletions src/trans/auto_impls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ void Trans_AutoImpl_Clone(State& state, ::HIR::TypeRef ty)
default:
TODO(sp, "auto Clone for " << ty << " - Unknown and not Copy");
TU_ARMA(Path, te) {
// closures are identified by the name starting with 'closure#'
if( TU_TEST1(te.path.m_data, Generic, .m_path.m_components.back().compare(0, 8, "closure#") == 0) ) {
if( te.is_closure() ) {
const auto& gp = te.path.m_data.as_Generic();
const auto& str = state.resolve.m_crate.get_struct_by_path(sp, gp.m_path);
Trans_Params p;
Expand Down
2 changes: 1 addition & 1 deletion src/trans/enumerate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1447,7 +1447,7 @@ void Trans_Enumerate_FillFrom_PathMono(EnumState& state, ::HIR::Path path_mono)
else if( const auto* te = inner_ty.m_data.opt_Array() ) {
enum_impl(*te->inner);
}
else if( TU_TEST2(inner_ty.m_data, Path, .path.m_data, Generic, .m_path.m_components.back().compare(0, 8, "closure#") == 0) ) {
else if( TU_TEST1(inner_ty.m_data, Path, .is_closure()) ) {
const auto& gp = inner_ty.m_data.as_Path().path.m_data.as_Generic();
const auto& str = state.crate.get_struct_by_path(sp, gp.m_path);
Trans_Params p;
Expand Down

0 comments on commit 56c9b3f

Please sign in to comment.