Skip to content

Commit 35fa7f0

Browse files
committed
Add *value meta functions, and don't mark copy/move ctors explicit, closes #375
Generated code now doesn't get tacked together as a very long emitted line - which is legal code, but isn't visually pleasing or readable. This matters more in this commit now that we have meta functions that generate multiple functions in the same type.
1 parent 8c0915c commit 35fa7f0

20 files changed

+409
-48
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
widget: @value type = {
3+
val: int = 0;
4+
operator=: (out this, i: int) = { val = i; }
5+
}
6+
7+
w_widget: @weakly_ordered_value type = {
8+
val: int = 0;
9+
operator=: (out this, i: int) = { val = i; }
10+
}
11+
12+
p_widget: @partially_ordered_value type = {
13+
val: int = 0;
14+
operator=: (out this, i: int) = { val = i; }
15+
}
16+
17+
main: () = {
18+
test<widget>();
19+
test<w_widget>();
20+
test<p_widget>();
21+
}
22+
23+
test: <T> () = {
24+
// should be default constructible
25+
a: T = ();
26+
27+
// widget should be comparable
28+
b: T = 2;
29+
if (a<b) {
30+
std::cout << "less ";
31+
}
32+
else {
33+
std::cout << "more ";
34+
}
35+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
less less less

regression-tests/test-results/clang-12/pure2-types-value-types-via-meta-functions.cpp.output

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
less less less

regression-tests/test-results/gcc-10/pure2-types-value-types-via-meta-functions.cpp.output

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
less less less
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pure2-types-value-types-via-meta-functions.cpp

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,22 @@ class Cyborg;
2828
#line 2 "pure2-types-inheritance.cpp2"
2929
class Human {
3030
public: virtual auto speak() const -> void = 0;
31+
3132
public: virtual ~Human();
33+
3234
public: Human() = default;
3335
public: Human(Human const&) = delete;
3436
public: auto operator=(Human const&) -> void = delete;
35-
3637
#line 4 "pure2-types-inheritance.cpp2"
3738
};
3839

3940
namespace N {
4041
template<int I> class Machine {
4142
public: explicit Machine(cpp2::in<std::string> id);
4243
public: virtual auto work() const -> void = 0;
44+
4345
public: virtual ~Machine();
46+
4447
public: Machine(Machine const&) = delete;
4548
public: auto operator=(Machine const&) -> void = delete;
4649

@@ -88,13 +91,15 @@ auto main() -> int;
8891

8992
//=== Cpp2 function definitions =================================================
9093

91-
#line 0 "pure2-types-inheritance.cpp2"
92-
Human::~Human(){}
9394

95+
96+
Human::~Human(){}
9497
#line 6 "pure2-types-inheritance.cpp2"
9598
namespace N {
9699

97-
template <int I> Machine<I>::Machine(cpp2::in<std::string> id){}template <int I> Machine<I>::~Machine(){}
100+
template <int I> Machine<I>::Machine(cpp2::in<std::string> id){}
101+
template <int I>
102+
Machine<I>::~Machine(){}
98103

99104
#line 11 "pure2-types-inheritance.cpp2"
100105
}

regression-tests/test-results/pure2-types-ordering-via-meta-functions.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ class my_integer {
2828
public: explicit my_integer(cpp2::in<int> val);
2929
#line 4 "pure2-types-ordering-via-meta-functions.cpp2"
3030
public: auto operator=(cpp2::in<int> val) -> my_integer& ;
31+
3132
public: [[nodiscard]] auto operator<=>(my_integer const& that) const -> std::strong_ordering = default;
33+
3234
public: my_integer(my_integer const&) = delete;
3335
public: auto operator=(my_integer const&) -> void = delete;
34-
3536
#line 5 "pure2-types-ordering-via-meta-functions.cpp2"
3637
};
3738

@@ -40,10 +41,11 @@ class case_insensitive_string {
4041
public: explicit case_insensitive_string(cpp2::in<std::string> val);
4142
#line 9 "pure2-types-ordering-via-meta-functions.cpp2"
4243
public: auto operator=(cpp2::in<std::string> val) -> case_insensitive_string& ;
44+
4345
public: [[nodiscard]] auto operator<=>(case_insensitive_string const& that) const -> std::weak_ordering = default;
46+
4447
public: case_insensitive_string(case_insensitive_string const&) = delete;
4548
public: auto operator=(case_insensitive_string const&) -> void = delete;
46-
4749
#line 10 "pure2-types-ordering-via-meta-functions.cpp2"
4850
};
4951

@@ -52,10 +54,11 @@ class person_in_family_tree {
5254
public: explicit person_in_family_tree(cpp2::in<int> parents);
5355
#line 14 "pure2-types-ordering-via-meta-functions.cpp2"
5456
public: auto operator=(cpp2::in<int> parents) -> person_in_family_tree& ;
57+
5558
public: [[nodiscard]] auto operator<=>(person_in_family_tree const& that) const -> std::partial_ordering = default;
59+
5660
public: person_in_family_tree(person_in_family_tree const&) = delete;
5761
public: auto operator=(person_in_family_tree const&) -> void = delete;
58-
5962
#line 15 "pure2-types-ordering-via-meta-functions.cpp2"
6063
};
6164

regression-tests/test-results/pure2-types-smf-and-that-1-provide-everything.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ class myclass;
1717
#line 2 "pure2-types-smf-and-that-1-provide-everything.cpp2"
1818
class myclass {
1919

20-
public: explicit myclass(myclass const& that);
20+
public: myclass(myclass const& that);
2121

2222

2323
#line 8 "pure2-types-smf-and-that-1-provide-everything.cpp2"
24-
public: explicit myclass(myclass&& that);
24+
public: myclass(myclass&& that);
2525

2626

2727
#line 13 "pure2-types-smf-and-that-1-provide-everything.cpp2"

regression-tests/test-results/pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ class myclass;
1717
#line 2 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
1818
class myclass {
1919

20-
public: explicit myclass(myclass const& that);
20+
public: myclass(myclass const& that);
2121

2222

2323
#line 8 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"
24-
public: explicit myclass(myclass&& that);
24+
public: myclass(myclass&& that);
2525

2626

2727
#line 13 "pure2-types-smf-and-that-2-provide-mvconstruct-and-cpassign.cpp2"

regression-tests/test-results/pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ class myclass;
1717
#line 2 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
1818
class myclass {
1919

20-
public: explicit myclass(myclass const& that);
20+
public: myclass(myclass const& that);
2121

2222
#line 4 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
2323
public: auto operator=(myclass const& that) -> myclass& ;
2424

2525

2626
#line 8 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"
27-
public: explicit myclass(myclass&& that);
27+
public: myclass(myclass&& that);
2828

2929

3030
#line 13 "pure2-types-smf-and-that-3-provide-mvconstruct-and-mvassign.cpp2"

regression-tests/test-results/pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ class myclass;
1717
#line 2 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
1818
class myclass {
1919

20-
public: explicit myclass(myclass const& that);
20+
public: myclass(myclass const& that);
2121

2222
#line 4 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"
23-
public: explicit myclass(myclass&& that);
23+
public: myclass(myclass&& that);
2424

2525

2626
#line 8 "pure2-types-smf-and-that-4-provide-cpassign-and-mvassign.cpp2"

regression-tests/test-results/pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ class myclass;
1717
#line 2 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
1818
class myclass {
1919

20-
public: explicit myclass(myclass const& that);
20+
public: myclass(myclass const& that);
2121

2222
#line 4 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
2323
public: auto operator=(myclass const& that) -> myclass& ;
2424

2525
#line 4 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
26-
public: explicit myclass(myclass&& that);
26+
public: myclass(myclass&& that);
2727

2828
#line 4 "pure2-types-smf-and-that-5-provide-nothing-but-general-case.cpp2"
2929
public: auto operator=(myclass&& that) -> myclass& ;

regression-tests/test-results/pure2-types-that-parameters.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ class myclass {
1919

2020
public: myclass();
2121

22-
public: explicit myclass(myclass const& that);
22+
public: myclass(myclass const& that);
2323

2424
#line 6 "pure2-types-that-parameters.cpp2"
2525
public: auto operator=(myclass const& that) -> myclass& ;
2626

2727

2828
#line 11 "pure2-types-that-parameters.cpp2"
29-
public: explicit myclass(myclass&& that);
29+
public: myclass(myclass&& that);
3030

3131
#line 11 "pure2-types-that-parameters.cpp2"
3232
public: auto operator=(myclass&& that) -> myclass& ;
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
2+
#define CPP2_USE_MODULES Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
10+
#line 2 "pure2-types-value-types-via-meta-functions.cpp2"
11+
class widget;
12+
13+
14+
#line 7 "pure2-types-value-types-via-meta-functions.cpp2"
15+
class w_widget;
16+
17+
18+
#line 12 "pure2-types-value-types-via-meta-functions.cpp2"
19+
class p_widget;
20+
21+
22+
//=== Cpp2 type definitions and function declarations ===========================
23+
24+
25+
#line 2 "pure2-types-value-types-via-meta-functions.cpp2"
26+
class widget {
27+
private: int val {0};
28+
public: explicit widget(cpp2::in<int> i);
29+
#line 4 "pure2-types-value-types-via-meta-functions.cpp2"
30+
public: auto operator=(cpp2::in<int> i) -> widget& ;
31+
32+
public: [[nodiscard]] auto operator<=>(widget const& that) const -> std::strong_ordering = default;
33+
public: widget(widget const& that);
34+
public: auto operator=(widget const& that) -> widget& ;
35+
public: widget(widget&& that);
36+
public: auto operator=(widget&& that) -> widget& ;
37+
public: widget();
38+
39+
#line 5 "pure2-types-value-types-via-meta-functions.cpp2"
40+
};
41+
42+
class w_widget {
43+
private: int val {0};
44+
public: explicit w_widget(cpp2::in<int> i);
45+
#line 9 "pure2-types-value-types-via-meta-functions.cpp2"
46+
public: auto operator=(cpp2::in<int> i) -> w_widget& ;
47+
48+
public: [[nodiscard]] auto operator<=>(w_widget const& that) const -> std::weak_ordering = default;
49+
public: w_widget(w_widget const& that);
50+
public: auto operator=(w_widget const& that) -> w_widget& ;
51+
public: w_widget(w_widget&& that);
52+
public: auto operator=(w_widget&& that) -> w_widget& ;
53+
public: w_widget();
54+
55+
#line 10 "pure2-types-value-types-via-meta-functions.cpp2"
56+
};
57+
58+
class p_widget {
59+
private: int val {0};
60+
public: explicit p_widget(cpp2::in<int> i);
61+
#line 14 "pure2-types-value-types-via-meta-functions.cpp2"
62+
public: auto operator=(cpp2::in<int> i) -> p_widget& ;
63+
64+
public: [[nodiscard]] auto operator<=>(p_widget const& that) const -> std::partial_ordering = default;
65+
public: p_widget(p_widget const& that);
66+
public: auto operator=(p_widget const& that) -> p_widget& ;
67+
public: p_widget(p_widget&& that);
68+
public: auto operator=(p_widget&& that) -> p_widget& ;
69+
public: p_widget();
70+
71+
#line 15 "pure2-types-value-types-via-meta-functions.cpp2"
72+
};
73+
74+
auto main() -> int;
75+
76+
77+
#line 23 "pure2-types-value-types-via-meta-functions.cpp2"
78+
template<typename T> auto test() -> void;
79+
80+
81+
//=== Cpp2 function definitions =================================================
82+
83+
84+
#line 4 "pure2-types-value-types-via-meta-functions.cpp2"
85+
widget::widget(cpp2::in<int> i)
86+
: val{ i }
87+
#line 4 "pure2-types-value-types-via-meta-functions.cpp2"
88+
{}
89+
#line 4 "pure2-types-value-types-via-meta-functions.cpp2"
90+
auto widget::operator=(cpp2::in<int> i) -> widget& {
91+
val = i;
92+
return *this;
93+
#line 4 "pure2-types-value-types-via-meta-functions.cpp2"
94+
}
95+
96+
97+
widget::widget(widget const& that){}
98+
99+
auto widget::operator=(widget const& that) -> widget& {
100+
val = 0;
101+
return *this;}
102+
103+
widget::widget(widget&& that){}
104+
auto widget::operator=(widget&& that) -> widget& {
105+
val = 0;
106+
return *this;}
107+
108+
widget::widget(){}
109+
#line 9 "pure2-types-value-types-via-meta-functions.cpp2"
110+
w_widget::w_widget(cpp2::in<int> i)
111+
: val{ i }
112+
#line 9 "pure2-types-value-types-via-meta-functions.cpp2"
113+
{}
114+
#line 9 "pure2-types-value-types-via-meta-functions.cpp2"
115+
auto w_widget::operator=(cpp2::in<int> i) -> w_widget& {
116+
val = i;
117+
return *this;
118+
#line 9 "pure2-types-value-types-via-meta-functions.cpp2"
119+
}
120+
121+
122+
w_widget::w_widget(w_widget const& that){}
123+
124+
auto w_widget::operator=(w_widget const& that) -> w_widget& {
125+
val = 0;
126+
return *this;}
127+
128+
w_widget::w_widget(w_widget&& that){}
129+
auto w_widget::operator=(w_widget&& that) -> w_widget& {
130+
val = 0;
131+
return *this;}
132+
133+
w_widget::w_widget(){}
134+
#line 14 "pure2-types-value-types-via-meta-functions.cpp2"
135+
p_widget::p_widget(cpp2::in<int> i)
136+
: val{ i }
137+
#line 14 "pure2-types-value-types-via-meta-functions.cpp2"
138+
{}
139+
#line 14 "pure2-types-value-types-via-meta-functions.cpp2"
140+
auto p_widget::operator=(cpp2::in<int> i) -> p_widget& {
141+
val = i;
142+
return *this;
143+
#line 14 "pure2-types-value-types-via-meta-functions.cpp2"
144+
}
145+
146+
147+
p_widget::p_widget(p_widget const& that){}
148+
149+
auto p_widget::operator=(p_widget const& that) -> p_widget& {
150+
val = 0;
151+
return *this;}
152+
153+
p_widget::p_widget(p_widget&& that){}
154+
auto p_widget::operator=(p_widget&& that) -> p_widget& {
155+
val = 0;
156+
return *this;}
157+
158+
p_widget::p_widget(){}
159+
#line 17 "pure2-types-value-types-via-meta-functions.cpp2"
160+
auto main() -> int{
161+
test<widget>();
162+
test<w_widget>();
163+
test<p_widget>();
164+
}
165+
166+
template<typename T> auto test() -> void{
167+
// should be default constructible
168+
T a {};
169+
170+
// widget should be comparable
171+
T b {2};
172+
if ((cpp2::cmp_less(std::move(a),std::move(b)))) {
173+
std::cout << "less ";
174+
}
175+
else {
176+
std::cout << "more ";
177+
}
178+
}
179+

0 commit comments

Comments
 (0)