Skip to content

Commit ec3d38a

Browse files
JohelEGPhsutter
andauthored
fix(cpp1): emit parents' *template-head*s of alias (#703)
* test: add case for templated type-scope object alias * fix(cpp1): emit parents' *template-head*s of alias * Delete regression-tests/test-results/gcc-13/pure2-bugfix-for-non-local-function-expression.cpp.output Signed-off-by: Herb Sutter <[email protected]> * Update pure2-type-and-namespace-aliases.cpp2 Signed-off-by: Herb Sutter <[email protected]> * Update pure2-type-and-namespace-aliases.cpp Signed-off-by: Herb Sutter <[email protected]> --------- Signed-off-by: Herb Sutter <[email protected]> Co-authored-by: Herb Sutter <[email protected]>
1 parent 4b9a25c commit ec3d38a

File tree

2 files changed

+29
-59
lines changed

2 files changed

+29
-59
lines changed

regression-tests/test-results/gcc-13/pure2-bugfix-for-non-local-function-expression.cpp.output

-1
This file was deleted.

source/cppfront.cpp

+29-58
Original file line numberDiff line numberDiff line change
@@ -4949,6 +4949,30 @@ class cppfront
49494949
)
49504950
-> void
49514951
{
4952+
// Helper for declarations with parent *template-head*s.
4953+
auto const emit_parent_template_parameters = [&]() {
4954+
auto parent_template_parameters = std::string{};
4955+
auto parent = n.parent_declaration;
4956+
while (
4957+
parent
4958+
&& parent->is_type()
4959+
)
4960+
{
4961+
if (parent->requires_clause_expression) {
4962+
parent_template_parameters =
4963+
"requires( " + print_to_string(*parent->requires_clause_expression) + " )\n"
4964+
+ parent_template_parameters;
4965+
}
4966+
if (parent->template_parameters) {
4967+
parent_template_parameters =
4968+
"template " + print_to_string( *parent->template_parameters, false, true )
4969+
+ " " + parent_template_parameters;
4970+
}
4971+
parent = parent->parent_declaration;
4972+
}
4973+
printer.print_cpp2(parent_template_parameters, n.position());
4974+
};
4975+
49524976
// Helper for declarations that can have requires-clauses
49534977
auto const emit_requires_clause = [&]() {
49544978
if (
@@ -5142,46 +5166,12 @@ class cppfront
51425166
return;
51435167
}
51445168

5145-
auto parent_template_params = std::string{};
5146-
auto parent_template_args = std::string{};
5147-
auto parent_qualifier = std::string{};
5148-
5149-
auto parent = n.parent_declaration;
5150-
while (parent && parent->is_type())
5151-
{
5152-
if (!parent_qualifier.empty()) {
5153-
parent_qualifier.insert(0, "::");
5154-
}
5155-
parent_qualifier.insert(0, parent->name()->to_string());
5156-
5157-
if (parent->template_parameters) {
5158-
parent_template_params.insert(0, "template" + print_to_string(*parent->template_parameters));
5159-
for (auto const& param : parent->template_parameters->parameters) {
5160-
assert(param->name());
5161-
if (parent_template_args.empty()) {
5162-
parent_template_args = "<>";
5163-
}
5164-
else {
5165-
parent_template_args.insert(parent_template_args.size()-1, ",");
5166-
}
5167-
parent_template_args.insert(parent_template_args.size()-1, param->name()->as_string_view());
5168-
}
5169-
}
5170-
5171-
parent = parent->parent_declaration;
5172-
}
5173-
5174-
if (!parent_template_params.empty()) {
5175-
parent_template_params += " ";
5176-
}
5177-
5169+
emit_parent_template_parameters();
51785170
printer.print_cpp2(
5179-
parent_template_params
5180-
+ "inline CPP2_CONSTEXPR "
5171+
"inline CPP2_CONSTEXPR "
51815172
+ type
5182-
+ parent_template_args
5183-
+ " " + parent_qualifier
5184-
+ parent_template_args + "::"
5173+
+ " "
5174+
+ type_qualification_if_any_for(n)
51855175
+ print_to_string(*n.identifier)
51865176
+ " = "
51875177
+ print_to_string( *std::get<alias_node::an_object>(a->initializer) )
@@ -5349,26 +5339,7 @@ class cppfront
53495339
&& n.initializer // only if the function has a definition (is not abstract)
53505340
)
53515341
{
5352-
auto parent_template_parameters = std::string{};
5353-
auto parent = n.parent_declaration;
5354-
while (
5355-
parent
5356-
&& parent->is_type()
5357-
)
5358-
{
5359-
if (parent->requires_clause_expression) {
5360-
parent_template_parameters =
5361-
"requires( " + print_to_string(*parent->requires_clause_expression) + " )\n"
5362-
+ parent_template_parameters;
5363-
}
5364-
if (parent->template_parameters) {
5365-
parent_template_parameters =
5366-
"template " + print_to_string( *parent->template_parameters, false, true )
5367-
+ " " + parent_template_parameters;
5368-
}
5369-
parent = parent->parent_declaration;
5370-
}
5371-
printer.print_cpp2(parent_template_parameters, n.position());
5342+
emit_parent_template_parameters();
53725343
}
53735344

53745345
// Now, emit our own template parameters

0 commit comments

Comments
 (0)