Skip to content

Commit 97c74c1

Browse files
committed
fix(cpp1): emit parents' *template-head*s of alias
1 parent 32fd552 commit 97c74c1

4 files changed

+32
-62
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
pure2-bugfix-for-non-local-function-expression.cpp2:9:7: warning: ‘t’ has a base ‘t::<lambda()>’ which has no linkage [-Wsubobject-linkage]
2+
9 | t: @struct type = {
3+
| ^
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
pure2-type-and-namespace-aliases.cpp2:27:26: error: ‘template<class T> class myclass2’ used without template arguments
2-
27 | value: int == 42;
3-
| ^

regression-tests/test-results/pure2-type-and-namespace-aliases.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ auto myfunc() -> void{
7979
}
8080

8181
#line 27 "pure2-type-and-namespace-aliases.cpp2"
82-
inline constexpr int myclass2::value = 42;
82+
template <typename T> inline CPP2_CONSTEXPR int myclass2<T>::value = 42;
8383

8484
#line 30 "pure2-type-and-namespace-aliases.cpp2"
8585
auto main() -> int{

source/cppfront.cpp

Lines changed: 29 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4904,6 +4904,30 @@ class cppfront
49044904
)
49054905
-> void
49064906
{
4907+
// Helper for declarations with parent *template-head*s.
4908+
auto const emit_parent_template_parameters = [&]() {
4909+
auto parent_template_parameters = std::string{};
4910+
auto parent = n.parent_declaration;
4911+
while (
4912+
parent
4913+
&& parent->is_type()
4914+
)
4915+
{
4916+
if (parent->requires_clause_expression) {
4917+
parent_template_parameters =
4918+
"requires( " + print_to_string(*parent->requires_clause_expression) + " )\n"
4919+
+ parent_template_parameters;
4920+
}
4921+
if (parent->template_parameters) {
4922+
parent_template_parameters =
4923+
"template " + print_to_string( *parent->template_parameters, false, true )
4924+
+ " " + parent_template_parameters;
4925+
}
4926+
parent = parent->parent_declaration;
4927+
}
4928+
printer.print_cpp2(parent_template_parameters, n.position());
4929+
};
4930+
49074931
// Helper for declarations that can have requires-clauses
49084932
auto const emit_requires_clause = [&]() {
49094933
if (
@@ -5097,46 +5121,12 @@ class cppfront
50975121
return;
50985122
}
50995123

5100-
auto parent_template_params = std::string{};
5101-
auto parent_template_args = std::string{};
5102-
auto parent_qualifier = std::string{};
5103-
5104-
auto parent = n.parent_declaration;
5105-
while (parent && parent->is_type())
5106-
{
5107-
if (!parent_qualifier.empty()) {
5108-
parent_qualifier.insert(0, "::");
5109-
}
5110-
parent_qualifier.insert(0, parent->name()->to_string());
5111-
5112-
if (parent->template_parameters) {
5113-
parent_template_params.insert(0, "template" + print_to_string(*parent->template_parameters));
5114-
for (auto const& param : parent->template_parameters->parameters) {
5115-
assert(param->name());
5116-
if (parent_template_args.empty()) {
5117-
parent_template_args = "<>";
5118-
}
5119-
else {
5120-
parent_template_args.insert(parent_template_args.size()-1, ",");
5121-
}
5122-
parent_template_args.insert(parent_template_args.size()-1, param->name()->as_string_view());
5123-
}
5124-
}
5125-
5126-
parent = parent->parent_declaration;
5127-
}
5128-
5129-
if (!parent_template_params.empty()) {
5130-
parent_template_params += " ";
5131-
}
5132-
5124+
emit_parent_template_parameters();
51335125
printer.print_cpp2(
5134-
parent_template_params
5135-
+ "inline CPP2_CONSTEXPR "
5126+
"inline CPP2_CONSTEXPR "
51365127
+ type
5137-
+ parent_template_args
5138-
+ " " + parent_qualifier
5139-
+ parent_template_args + "::"
5128+
+ " "
5129+
+ type_qualification_if_any_for(n)
51405130
+ print_to_string(*n.identifier)
51415131
+ " = "
51425132
+ print_to_string( *std::get<alias_node::an_object>(a->initializer) )
@@ -5304,26 +5294,7 @@ class cppfront
53045294
&& n.initializer // only if the function has a definition (is not abstract)
53055295
)
53065296
{
5307-
auto parent_template_parameters = std::string{};
5308-
auto parent = n.parent_declaration;
5309-
while (
5310-
parent
5311-
&& parent->is_type()
5312-
)
5313-
{
5314-
if (parent->requires_clause_expression) {
5315-
parent_template_parameters =
5316-
"requires( " + print_to_string(*parent->requires_clause_expression) + " )\n"
5317-
+ parent_template_parameters;
5318-
}
5319-
if (parent->template_parameters) {
5320-
parent_template_parameters =
5321-
"template " + print_to_string( *parent->template_parameters, false, true )
5322-
+ " " + parent_template_parameters;
5323-
}
5324-
parent = parent->parent_declaration;
5325-
}
5326-
printer.print_cpp2(parent_template_parameters, n.position());
5297+
emit_parent_template_parameters();
53275298
}
53285299

53295300
// Now, emit our own template parameters

0 commit comments

Comments
 (0)