diff options
author | John Hodge <tpg@mutabah.net> | 2017-01-14 09:02:10 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2017-01-14 09:02:10 +0800 |
commit | db01a8a8ae3dab9888d7a734dc07ca513b6f86bc (patch) | |
tree | 2f3a75549eee5378b7ec9aa712f8f26aec9d77b4 /src | |
parent | 27be955d74e03e5db1c549367f3f784ec535f509 (diff) | |
download | mrust-db01a8a8ae3dab9888d7a734dc07ca513b6f86bc.tar.gz |
HIR - Clean up some manual item searching
Diffstat (limited to 'src')
-rw-r--r-- | src/hir/hir.cpp | 12 | ||||
-rw-r--r-- | src/hir/hir.hpp | 2 | ||||
-rw-r--r-- | src/hir_conv/bind.cpp | 80 | ||||
-rw-r--r-- | src/hir_conv/constant_evaluation.cpp | 34 | ||||
-rw-r--r-- | src/hir_expand/const_eval_full.cpp | 34 | ||||
-rw-r--r-- | src/trans/codegen_c.cpp | 1 |
6 files changed, 41 insertions, 122 deletions
diff --git a/src/hir/hir.cpp b/src/hir/hir.cpp index d4a4c9be..780a8e70 100644 --- a/src/hir/hir.cpp +++ b/src/hir/hir.cpp @@ -730,7 +730,7 @@ const ::HIR::SimplePath& ::HIR::Crate::get_lang_item_path_opt(const char* name) const ::HIR::TypeItem& ::HIR::Crate::get_typeitem_by_path(const Span& sp, const ::HIR::SimplePath& path, bool ignore_crate_name, bool ignore_last_node) const { ASSERT_BUG(sp, path.m_components.size() > 0, "get_typeitem_by_path received invalid path - " << path); - ASSERT_BUG(sp, path.m_components.size() > (ignore_last_node ? 1 : 0), "get_typeitem_by_path received invlaid path - " << path); + ASSERT_BUG(sp, path.m_components.size() > (ignore_last_node ? 1 : 0), "get_typeitem_by_path received invalid path - " << path); const ::HIR::Module* mod; if( !ignore_crate_name && path.m_crate_name != m_crate_name ) { @@ -762,9 +762,13 @@ const ::HIR::TypeItem& ::HIR::Crate::get_typeitem_by_path(const Span& sp, const return it->second->ent; } -const ::HIR::Module& ::HIR::Crate::get_mod_by_path(const Span& sp, const ::HIR::SimplePath& path) const +const ::HIR::Module& ::HIR::Crate::get_mod_by_path(const Span& sp, const ::HIR::SimplePath& path, bool ignore_last_node/*=false*/) const { - if( path.m_components.size() == 0 ) + if( ignore_last_node ) + { + ASSERT_BUG(sp, path.m_components.size() > 0, "get_mod_by_path received invalid path with ignore_last_node=true - " << path); + } + if( path.m_components.size() == (ignore_last_node ? 1 : 0) ) { if( path.m_crate_name != m_crate_name ) { @@ -778,7 +782,7 @@ const ::HIR::Module& ::HIR::Crate::get_mod_by_path(const Span& sp, const ::HIR:: } else { - const auto& ti = this->get_typeitem_by_path(sp, path); + const auto& ti = this->get_typeitem_by_path(sp, path, false, ignore_last_node); TU_IFLET(::HIR::TypeItem, ti, Module, e, return e; ) diff --git a/src/hir/hir.hpp b/src/hir/hir.hpp index 9b40ad1a..f090c67f 100644 --- a/src/hir/hir.hpp +++ b/src/hir/hir.hpp @@ -442,7 +442,7 @@ public: const ::HIR::Struct& get_struct_by_path(const Span& sp, const ::HIR::SimplePath& path) const; const ::HIR::Union& get_union_by_path(const Span& sp, const ::HIR::SimplePath& path) const; const ::HIR::Enum& get_enum_by_path(const Span& sp, const ::HIR::SimplePath& path) const; - const ::HIR::Module& get_mod_by_path(const Span& sp, const ::HIR::SimplePath& path) const; + const ::HIR::Module& get_mod_by_path(const Span& sp, const ::HIR::SimplePath& path, bool ignore_last_node=false) const; const ::HIR::ValueItem& get_valitem_by_path(const Span& sp, const ::HIR::SimplePath& path, bool ignore_crate_name=false) const; const ::HIR::Function& get_function_by_path(const Span& sp, const ::HIR::SimplePath& path) const; diff --git a/src/hir_conv/bind.cpp b/src/hir_conv/bind.cpp index 72912972..6c830bfe 100644 --- a/src/hir_conv/bind.cpp +++ b/src/hir_conv/bind.cpp @@ -27,75 +27,37 @@ namespace { }; const void* get_type_pointer(const Span& sp, const ::HIR::Crate& crate, const ::HIR::SimplePath& path, Target t) { - // NOTE: Can't share with HIR::Crate::get_typeitem_by_path because it has to handle enum variants - const ::HIR::Module* mod; - if( path.m_crate_name != crate.m_crate_name ) { - ASSERT_BUG(sp, crate.m_ext_crates.count(path.m_crate_name) > 0, "Crate '" << path.m_crate_name << "' not loaded"); - mod = &crate.m_ext_crates.at(path.m_crate_name).m_data->m_root_module; - } - else { - mod = &crate.m_root_module; + if( t == Target::EnumVariant ) + { + return &crate.get_typeitem_by_path(sp, path, false, true).as_Enum(); } - - for( unsigned int i = 0; i < path.m_components.size()-1; i ++ ) + else { - const auto& pc = path.m_components[i]; - auto it = mod->m_mod_items.find( pc ); - if( it == mod->m_mod_items.end() ) { - BUG(sp, "Couldn't find component " << i << " of " << path); - } - - // If second-last, and an enum variant is desired, return the pointer to the enum - if( i+1 == path.m_components.size()-1 && t == Target::EnumVariant ) + const auto& ti = crate.get_typeitem_by_path(sp, path); + switch(t) { - TU_IFLET(::HIR::TypeItem, it->second->ent, Enum, e2, + case Target::TypeItem: return &ti; + case Target::EnumVariant: throw ""; + + case Target::Struct: + TU_IFLET(::HIR::TypeItem, ti, Struct, e2, return &e2; ) else { - ERROR(sp, E0000, "Expected an enum at the penultimate node of " << path << ", got a " << it->second->ent.tag_str()); + ERROR(sp, E0000, "Expected a struct at " << path << ", got a " << ti.tag_str()); } - } - else { - TU_MATCH_DEF( ::HIR::TypeItem, (it->second->ent), (e2), - ( - BUG(sp, "Node " << i << " of path " << path << " wasn't a module"); - ), - (Module, - mod = &e2; - ) + break; + case Target::Enum: + TU_IFLET(::HIR::TypeItem, ti, Enum, e2, + return &e2; ) + else { + ERROR(sp, E0000, "Expected a enum at " << path << ", got a " << ti.tag_str()); + } + break; } + throw ""; } - - const auto& pc = path.m_components.back(); - auto it = mod->m_mod_items.find( pc ); - if( it == mod->m_mod_items.end() ) { - BUG(sp, "Couldn't find final component of " << path); - } - - switch(t) - { - case Target::TypeItem: return &it->second->ent; - case Target::EnumVariant: throw ""; - - case Target::Struct: - TU_IFLET(::HIR::TypeItem, it->second->ent, Struct, e2, - return &e2; - ) - else { - ERROR(sp, E0000, "Expected a struct at " << path << ", got a " << it->second->ent.tag_str()); - } - break; - case Target::Enum: - TU_IFLET(::HIR::TypeItem, it->second->ent, Enum, e2, - return &e2; - ) - else { - ERROR(sp, E0000, "Expected a enum at " << path << ", got a " << it->second->ent.tag_str()); - } - break; - } - throw ""; } void fix_type_params(const Span& sp, const ::HIR::GenericParams& params_def, ::HIR::PathParams& params) diff --git a/src/hir_conv/constant_evaluation.cpp b/src/hir_conv/constant_evaluation.cpp index 8ee78af9..1baa48ac 100644 --- a/src/hir_conv/constant_evaluation.cpp +++ b/src/hir_conv/constant_evaluation.cpp @@ -98,37 +98,13 @@ namespace { }; EntPtr get_ent_simplepath(const Span& sp, const ::HIR::Crate& crate, const ::HIR::SimplePath& path, EntNS ns) { - const ::HIR::Module* mod; - if( path.m_crate_name != crate.m_crate_name ) { - ASSERT_BUG(sp, crate.m_ext_crates.count(path.m_crate_name) > 0, "Crate '" << path.m_crate_name << "' not loaded"); - mod = &crate.m_ext_crates.at(path.m_crate_name).m_data->m_root_module; - } - else { - mod = &crate.m_root_module; - } - - for( unsigned int i = 0; i < path.m_components.size() - 1; i ++ ) - { - const auto& pc = path.m_components[i]; - auto it = mod->m_mod_items.find( pc ); - if( it == mod->m_mod_items.end() ) { - BUG(sp, "Couldn't find component " << i << " of " << path); - } - TU_MATCH_DEF( ::HIR::TypeItem, (it->second->ent), (e2), - ( - BUG(sp, "Node " << i << " of path " << path << " wasn't a module"); - ), - (Module, - mod = &e2; - ) - ) - } + const ::HIR::Module& mod = crate.get_mod_by_path(sp, path, /*ignore_last_node=*/true); switch( ns ) { case EntNS::Value: { - auto it = mod->m_value_items.find( path.m_components.back() ); - if( it == mod->m_value_items.end() ) { + auto it = mod.m_value_items.find( path.m_components.back() ); + if( it == mod.m_value_items.end() ) { return EntPtr {}; } @@ -152,8 +128,8 @@ namespace { BUG(sp, "Path " << path << " pointed to a invalid item - " << it->second->ent.tag_str()); } break; case EntNS::Type: { - auto it = mod->m_mod_items.find( path.m_components.back() ); - if( it == mod->m_mod_items.end() ) { + auto it = mod.m_mod_items.find( path.m_components.back() ); + if( it == mod.m_mod_items.end() ) { return EntPtr {}; } diff --git a/src/hir_expand/const_eval_full.cpp b/src/hir_expand/const_eval_full.cpp index 102bb84c..35ddcb66 100644 --- a/src/hir_expand/const_eval_full.cpp +++ b/src/hir_expand/const_eval_full.cpp @@ -127,37 +127,13 @@ namespace { }; EntPtr get_ent_simplepath(const Span& sp, const ::HIR::Crate& crate, const ::HIR::SimplePath& path, EntNS ns) { - const ::HIR::Module* mod; - if( path.m_crate_name != crate.m_crate_name ) { - ASSERT_BUG(sp, crate.m_ext_crates.count(path.m_crate_name) > 0, "Crate '" << path.m_crate_name << "' not loaded"); - mod = &crate.m_ext_crates.at(path.m_crate_name).m_data->m_root_module; - } - else { - mod = &crate.m_root_module; - } - - for( unsigned int i = 0; i < path.m_components.size() - 1; i ++ ) - { - const auto& pc = path.m_components[i]; - auto it = mod->m_mod_items.find( pc ); - if( it == mod->m_mod_items.end() ) { - BUG(sp, "Couldn't find component " << i << " of " << path); - } - TU_MATCH_DEF( ::HIR::TypeItem, (it->second->ent), (e2), - ( - BUG(sp, "Node " << i << " of path " << path << " wasn't a module"); - ), - (Module, - mod = &e2; - ) - ) - } + const ::HIR::Module& mod = crate.get_mod_by_path(sp, path, /*ignore_last_node=*/true); switch( ns ) { case EntNS::Value: { - auto it = mod->m_value_items.find( path.m_components.back() ); - if( it == mod->m_value_items.end() ) { + auto it = mod.m_value_items.find( path.m_components.back() ); + if( it == mod.m_value_items.end() ) { return EntPtr {}; } @@ -181,8 +157,8 @@ namespace { BUG(sp, "Path " << path << " pointed to a invalid item - " << it->second->ent.tag_str()); } break; case EntNS::Type: { - auto it = mod->m_mod_items.find( path.m_components.back() ); - if( it == mod->m_mod_items.end() ) { + auto it = mod.m_mod_items.find( path.m_components.back() ); + if( it == mod.m_mod_items.end() ) { return EntPtr {}; } diff --git a/src/trans/codegen_c.cpp b/src/trans/codegen_c.cpp index 32ac75f3..c5e99987 100644 --- a/src/trans/codegen_c.cpp +++ b/src/trans/codegen_c.cpp @@ -2725,6 +2725,7 @@ namespace { return ::HIR::TypeRef(); } } + // TODO: Move this to a more common location MetadataType metadata_type(const ::HIR::TypeRef& ty) const { if( ty == ::HIR::CoreType::Str || ty.m_data.is_Slice() ) { |