Skip to content

Commit ef6b1a0

Browse files
committed
Fix lowering of undefined (abstract) functions
Undefined functions were just added in today's first commit, so we need some checks in places that assumed functions always had definitions -- in particular, when the function is a template (or is nested inside templates) we would spit out stray bits of template parameter lists Also: Added some template parameters to the base type to make sure we're accessing base types like `N::Machine<99>` correctly
1 parent fe13207 commit ef6b1a0

File tree

3 files changed

+26
-16
lines changed

3 files changed

+26
-16
lines changed

regression-tests/pure2-types-inheritance.cpp2

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Human: type = {
55
}
66

77
N: namespace = {
8-
Machine: type = {
8+
Machine: <I:int> type = {
99
operator=: (out this, id: std::string) = {}
1010
work: (virtual this);
1111
}
@@ -15,11 +15,11 @@ Cyborg: type = {
1515
name: std::string;
1616
this: Human = ();
1717
address: std::string = "123 Main St.";
18-
this: N::Machine;
18+
this: N::Machine<99>;
1919

2020
operator=: (out this, n: std::string) = {
2121
name = n;
22-
N::Machine = "Acme Corp. engineer tech";
22+
N::Machine<99> = "Acme Corp. engineer tech";
2323
std::cout << "(name)$ checks in for the day's shift\n";
2424
}
2525

@@ -41,7 +41,7 @@ make_speak: ( h: Human ) = {
4141
h.speak();
4242
}
4343

44-
do_work: ( m: N::Machine ) = {
44+
do_work: ( m: N::Machine<99> ) = {
4545
std::cout << "-> [vcall: do_work] ";
4646
m.work();
4747
}

regression-tests/test-results/pure2-types-inheritance.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Human;
1212

1313
#line 7 "pure2-types-inheritance.cpp2"
1414
namespace N {
15-
class Machine;
15+
template<int I> class Machine;
1616

1717
#line 12 "pure2-types-inheritance.cpp2"
1818
}
@@ -29,13 +29,13 @@ class Human {
2929
};
3030

3131
namespace N {
32-
class Machine {
32+
template<int I> class Machine {
3333
public: explicit Machine(cpp2::in<std::string> id);
3434
public: virtual auto work() const -> void = 0;
3535
};
3636
}
3737

38-
class Cyborg: private cpp2::store_as_base<"name",std::string>, public Human, private cpp2::store_as_base<"address",std::string>, public N::Machine {
38+
class Cyborg: private cpp2::store_as_base<"name",std::string>, public Human, private cpp2::store_as_base<"address",std::string>, public N::Machine<99> {
3939

4040
#line 20 "pure2-types-inheritance.cpp2"
4141
public: explicit Cyborg(cpp2::in<std::string> n);
@@ -57,7 +57,7 @@ class Cyborg: private cpp2::store_as_base<"name",std::string>, public Human, pri
5757
auto make_speak(cpp2::in<Human> h) -> void;
5858

5959
#line 44 "pure2-types-inheritance.cpp2"
60-
auto do_work(cpp2::in<N::Machine> m) -> void;
60+
auto do_work(cpp2::in<N::Machine<99>> m) -> void;
6161

6262
#line 49 "pure2-types-inheritance.cpp2"
6363
auto main() -> int;
@@ -67,22 +67,21 @@ auto main() -> int;
6767

6868
#line 3 "pure2-types-inheritance.cpp2"
6969
Human::Human(){}
70-
7170

7271
#line 7 "pure2-types-inheritance.cpp2"
7372
namespace N {
7473

75-
Machine::Machine(cpp2::in<std::string> id){}
76-
74+
template <int I> Machine<I>::Machine(cpp2::in<std::string> id){}
7775

76+
#line 12 "pure2-types-inheritance.cpp2"
7877
}
7978

8079
#line 20 "pure2-types-inheritance.cpp2"
8180
Cyborg::Cyborg(cpp2::in<std::string> n)
8281
: cpp2::store_as_base<"name",std::string>{ n }
8382
, Human{ }
8483
, cpp2::store_as_base<"address",std::string>{ "123 Main St." }
85-
, N::Machine{ "Acme Corp. engineer tech" }
84+
, N::Machine<99>{ "Acme Corp. engineer tech" }
8685
#line 20 "pure2-types-inheritance.cpp2"
8786
{
8887

@@ -108,7 +107,7 @@ auto make_speak(cpp2::in<Human> h) -> void{
108107
CPP2_UFCS_0(speak, h);
109108
}
110109

111-
auto do_work(cpp2::in<N::Machine> m) -> void{
110+
auto do_work(cpp2::in<N::Machine<99>> m) -> void{
112111
std::cout << "-> [vcall: do_work] ";
113112
CPP2_UFCS_0(work, m);
114113
}

source/cppfront.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4765,6 +4765,7 @@ class cppfront
47654765
if (
47664766
printer.get_phase() == printer.phase2_func_defs
47674767
&& n.is_function()
4768+
&& n.initializer // only if the function has a definition (is not abstract)
47684769
)
47694770
{
47704771
auto parent_template_parameters = std::string{};
@@ -4793,8 +4794,12 @@ class cppfront
47934794
if (
47944795
n.template_parameters
47954796
&& (
4796-
(!n.is_function() && printer.get_phase() < printer.phase2_func_defs)
4797-
|| (n.is_function() && printer.get_phase() <= printer.phase2_func_defs)
4797+
printer.get_phase() < printer.phase2_func_defs
4798+
|| (
4799+
n.is_function()
4800+
&& n.initializer // only if the function has a definition (is not abstract)
4801+
&& printer.get_phase() == printer.phase2_func_defs
4802+
)
47984803
)
47994804
)
48004805
{
@@ -4947,7 +4952,13 @@ class cppfront
49474952
}
49484953

49494954
// Function
4950-
else if (n.is_function())
4955+
else if (
4956+
n.is_function()
4957+
&& (
4958+
printer.get_phase() < printer.phase2_func_defs
4959+
|| n.initializer // only emit definition if the function has one (is not abstract)
4960+
)
4961+
)
49514962
{
49524963
// Start fresh (there may be one spurious leftover
49534964
// requires-condition created during the declarations pass)

0 commit comments

Comments
 (0)