From df6f937346c04aea08b5b0df308507ea2aeaaa31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johel=20Ernesto=20Guerrero=20Pe=C3=B1a?= Date: Mon, 11 Dec 2023 19:10:58 -0400 Subject: [PATCH] refactor(to_cpp1): use `source_order_name_lookup` --- .../pure2-bugfix-for-dependent-types.cpp | 2 +- source/to_cpp1.h | 77 ++++++++----------- 2 files changed, 34 insertions(+), 45 deletions(-) diff --git a/regression-tests/test-results/pure2-bugfix-for-dependent-types.cpp b/regression-tests/test-results/pure2-bugfix-for-dependent-types.cpp index 4bdb5afe1..2cead3d47 100644 --- a/regression-tests/test-results/pure2-bugfix-for-dependent-types.cpp +++ b/regression-tests/test-results/pure2-bugfix-for-dependent-types.cpp @@ -117,7 +117,7 @@ auto main() -> int{ static_cast(typename alias::value_type{0});// Dependent. { using alias = std::integral_constant; - static_cast(typename alias::value_type{0});// Non-dependent. + static_cast(alias::value_type{0});// Non-dependent. } }(0)); diff --git a/source/to_cpp1.h b/source/to_cpp1.h index 0b55f4dfb..2ece7c359 100644 --- a/source/to_cpp1.h +++ b/source/to_cpp1.h @@ -1836,7 +1836,7 @@ class cppfront std::vector looking_up = {}; - auto is_dependent_alias(token const& lookup_id) + auto is_dependent_type_alias(token const& lookup_id) -> bool { // Prevent recursion. @@ -1846,45 +1846,34 @@ class cppfront looking_up.push_back(&lookup_id); auto guard = finally([&]{ looking_up.pop_back(); }); - bool res = false; - // If the first parent declaration - (void)std::find_if( - current_declarations.rbegin(), - current_declarations.rend() - 1, - [&](declaration_node const* decl) - { - // that can have aliases - if ((decl->is_function() - || decl->is_type() - || decl->is_namespace()) - && decl->initializer - && decl->initializer->is_compound()) - { - auto& stmts = decl->initializer->get_if()->statements; - // among its statements - return std::any_of(stmts.rbegin(), stmts.rend(), [&](decltype(stmts.front())& stmt) { - if (auto decl = stmt->get_if(); - decl - && decl->is_alias()) { - auto& alias = get(decl->type); - // has a type alias declaration of equal name - if (alias->is_type_alias() - && decl->identifier - && *decl->identifier->identifier == lookup_id) { - auto& type_id = get(alias->initializer); - // and its value is a dependent _type-id_. - res = is_dependent(*type_id); - return true; - } - } - return false; - }); - } - return false; - } - ); + auto lookup = source_order_name_lookup(lookup_id); + + if ( + !lookup + || get_if(&*lookup) + ) + { + return false; + } + + auto decl = get(*lookup); + if (!decl->is_alias()) { + return false; + } + + auto& alias = get(decl->type); + + if ( + !alias->is_type_alias() + || !decl->identifier + || *decl->identifier->identifier != lookup_id + ) + { + return false; + } - return res; + auto& type_id = get(alias->initializer); + return is_dependent(*type_id); } auto is_dependent(const type_id_node& n) @@ -2041,7 +2030,7 @@ class cppfront if (is_first && n.open_angle == source_position{}) { assert(n.identifier); return is_template_parameter(*n.identifier) - || is_dependent_alias(*n.identifier); + || is_dependent_type_alias(*n.identifier); } // If it's a _template-id_ if (n.open_angle != source_position{}) { @@ -3099,7 +3088,7 @@ class cppfront } - auto source_order_name_lookup(unqualified_id_node const& id) + auto source_order_name_lookup(std::string_view identifier) -> source_order_name_lookup_res { for ( @@ -3112,7 +3101,7 @@ class cppfront auto decl = get_if(&*first); decl && *decl - && (*decl)->has_name(*id.identifier) + && (*decl)->has_name(identifier) ) { return *decl; @@ -3121,7 +3110,7 @@ class cppfront auto using_ = get_if(&*first); using_ && using_->identifier - && *using_->identifier == *id.identifier + && *using_->identifier == identifier ) { return *using_; @@ -3141,7 +3130,7 @@ class cppfront } auto const& id = *get(n.id); - auto lookup = source_order_name_lookup(id); + auto lookup = source_order_name_lookup(*id.identifier); if ( !lookup