Skip to content

Commit

Permalink
refactor(to_cpp1): use source_order_name_lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
JohelEGP committed Dec 11, 2023
1 parent 591cd4b commit df6f937
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ auto main() -> int{
static_cast<void>(typename alias::value_type{0});// Dependent.
{
using alias = std::integral_constant<cpp2::i32,0>;
static_cast<void>(typename alias::value_type{0});// Non-dependent.
static_cast<void>(alias::value_type{0});// Non-dependent.
}
}(0));

Expand Down
77 changes: 33 additions & 44 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -1836,7 +1836,7 @@ class cppfront

std::vector<token const*> looking_up = {};

auto is_dependent_alias(token const& lookup_id)
auto is_dependent_type_alias(token const& lookup_id)
-> bool
{
// Prevent recursion.
Expand All @@ -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<compound_statement_node>()->statements;
// among its statements
return std::any_of(stmts.rbegin(), stmts.rend(), [&](decltype(stmts.front())& stmt) {
if (auto decl = stmt->get_if<declaration_node>();
decl
&& decl->is_alias()) {
auto& alias = get<declaration_node::an_alias>(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_node::a_type>(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<active_using_declaration>(&*lookup)
)
{
return false;
}

auto decl = get<declaration_node const*>(*lookup);
if (!decl->is_alias()) {
return false;
}

auto& alias = get<declaration_node::an_alias>(decl->type);

if (
!alias->is_type_alias()
|| !decl->identifier
|| *decl->identifier->identifier != lookup_id
)
{
return false;
}

return res;
auto& type_id = get<alias_node::a_type>(alias->initializer);
return is_dependent(*type_id);
}

auto is_dependent(const type_id_node& n)
Expand Down Expand Up @@ -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{}) {
Expand Down Expand Up @@ -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 (
Expand All @@ -3112,7 +3101,7 @@ class cppfront
auto decl = get_if<declaration_node const*>(&*first);
decl
&& *decl
&& (*decl)->has_name(*id.identifier)
&& (*decl)->has_name(identifier)
)
{
return *decl;
Expand All @@ -3121,7 +3110,7 @@ class cppfront
auto using_ = get_if<active_using_declaration>(&*first);
using_
&& using_->identifier
&& *using_->identifier == *id.identifier
&& *using_->identifier == identifier
)
{
return *using_;
Expand All @@ -3141,7 +3130,7 @@ class cppfront
}

auto const& id = *get<id_expression_node::unqualified>(n.id);
auto lookup = source_order_name_lookup(id);
auto lookup = source_order_name_lookup(*id.identifier);

if (
!lookup
Expand Down

0 comments on commit df6f937

Please sign in to comment.