Skip to content

Commit a89af8f

Browse files
committed
Add operator= generation
See the wiki documentation page: https://github.com/hsutter/cppfront/wiki/Cpp2:-operator=,-this-&-that See also the new `pure2-types-smf*.cpp2` test cases for examples. Made Cpp1 assignment "return *this;", closes #277 Changed in<T> to pass fundamental types by value. Only those are known to be always complete. Partly addresses #270. Also partly improves #282.
1 parent 4c52d2d commit a89af8f

File tree

60 files changed

+1798
-227
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1798
-227
lines changed

include/cpp2util.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,14 +491,10 @@ template<typename T>
491491
//
492492
//-----------------------------------------------------------------------
493493
//
494-
template<typename T>
495-
constexpr bool prefer_pass_by_value =
496-
sizeof(T) < 2*sizeof(void*) && std::is_trivially_copy_constructible_v<T>;
497-
498494
template<typename T>
499495
using in =
500496
std::conditional_t <
501-
prefer_pass_by_value<T>,
497+
std::is_scalar_v<T>,
502498
T const,
503499
T const&
504500
>;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
myclass : type = {
3+
4+
operator=: (out this, that) = {
5+
std::cout << "ctor - copy (GENERAL)";
6+
}
7+
8+
operator=: (out this, move that) = {
9+
name = that.name + "(CM)";
10+
std::cout << "ctor - move ";
11+
}
12+
13+
operator=: (inout this, that) = {
14+
addr = that.addr + "(AC)";
15+
std::cout << "assign - copy ";
16+
}
17+
18+
operator=: (inout this, move that) = {
19+
std::cout << "assign - move ";
20+
}
21+
22+
operator=: (out this, x: std::string) = {
23+
name = x;
24+
std::cout << "ctor - from string ";
25+
}
26+
27+
name: std::string = "Henry";
28+
addr: std::string = "123 Ford Dr.";
29+
30+
print: (
31+
this,
32+
prefix: std::string_view,
33+
suffix: std::string_view
34+
)
35+
= std::cout << prefix << "[ (name)$ | (addr)$ ]" << suffix;
36+
37+
}
38+
39+
main: () = {
40+
std::cout << "Function invoked Call syntax Results\n";
41+
std::cout << "---------------------- ------------ ------------------------------------------------------\n";
42+
43+
x: myclass = "Henry";
44+
x.print(" construct ", "\n");
45+
x = "Clara";
46+
x.print(" assign ", "\n");
47+
48+
y := x;
49+
y.print(" cp-construct ", " <- ");
50+
x.print("", "\n");
51+
52+
z := (move x);
53+
z.print(" mv-construct ", " <- ");
54+
x.print("", "\n");
55+
56+
z = y;
57+
z.print(" cp-assign ", " <- ");
58+
y.print("", "\n");
59+
60+
z = (move y);
61+
z.print(" mv-assign ", " <- ");
62+
y.print("", "\n");
63+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
myclass : type = {
3+
4+
operator=: (out this, that) = {
5+
std::cout << "ctor - copy (GENERAL)";
6+
}
7+
8+
operator=: (out this, move that) = {
9+
name = that.name + "(CM)";
10+
std::cout << "ctor - move ";
11+
}
12+
13+
operator=: (inout this, that) = {
14+
addr = that.addr + "(AC)";
15+
std::cout << "assign - copy ";
16+
}
17+
18+
// operator=: (inout this, move that) = {
19+
// std::cout << "assign - move ";
20+
// }
21+
22+
operator=: (out this, x: std::string) = {
23+
name = x;
24+
std::cout << "ctor - from string ";
25+
}
26+
27+
name: std::string = "Henry";
28+
addr: std::string = "123 Ford Dr.";
29+
30+
print: (
31+
this,
32+
prefix: std::string_view,
33+
suffix: std::string_view
34+
)
35+
= std::cout << prefix << "[ (name)$ | (addr)$ ]" << suffix;
36+
37+
}
38+
39+
main: () = {
40+
std::cout << "Function invoked Call syntax Results\n";
41+
std::cout << "---------------------- ------------ ------------------------------------------------------\n";
42+
43+
x: myclass = "Henry";
44+
x.print(" construct ", "\n");
45+
x = "Clara";
46+
x.print(" assign ", "\n");
47+
48+
y := x;
49+
y.print(" cp-construct ", " <- ");
50+
x.print("", "\n");
51+
52+
z := (move x);
53+
z.print(" mv-construct ", " <- ");
54+
x.print("", "\n");
55+
56+
z = y;
57+
z.print(" cp-assign ", " <- ");
58+
y.print("", "\n");
59+
60+
z = (move y);
61+
z.print(" mv-assign ", " <- ");
62+
y.print("", "\n");
63+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
myclass : type = {
3+
4+
operator=: (out this, that) = {
5+
std::cout << "ctor - copy (GENERAL)";
6+
}
7+
8+
operator=: (out this, move that) = {
9+
name = that.name + "(CM)";
10+
std::cout << "ctor - move ";
11+
}
12+
13+
// operator=: (inout this, that) = {
14+
// addr = that.addr + "(AC)";
15+
// std::cout << "assign - copy ";
16+
// }
17+
18+
operator=: (inout this, move that) = {
19+
std::cout << "assign - move ";
20+
}
21+
22+
operator=: (out this, x: std::string) = {
23+
name = x;
24+
std::cout << "ctor - from string ";
25+
}
26+
27+
name: std::string = "Henry";
28+
addr: std::string = "123 Ford Dr.";
29+
30+
print: (
31+
this,
32+
prefix: std::string_view,
33+
suffix: std::string_view
34+
)
35+
= std::cout << prefix << "[ (name)$ | (addr)$ ]" << suffix;
36+
37+
}
38+
39+
main: () = {
40+
std::cout << "Function invoked Call syntax Results\n";
41+
std::cout << "---------------------- ------------ ------------------------------------------------------\n";
42+
43+
x: myclass = "Henry";
44+
x.print(" construct ", "\n");
45+
x = "Clara";
46+
x.print(" assign ", "\n");
47+
48+
y := x;
49+
y.print(" cp-construct ", " <- ");
50+
x.print("", "\n");
51+
52+
z := (move x);
53+
z.print(" mv-construct ", " <- ");
54+
x.print("", "\n");
55+
56+
z = y;
57+
z.print(" cp-assign ", " <- ");
58+
y.print("", "\n");
59+
60+
z = (move y);
61+
z.print(" mv-assign ", " <- ");
62+
y.print("", "\n");
63+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
myclass : type = {
3+
4+
operator=: (out this, that) = {
5+
std::cout << "ctor - copy (GENERAL)";
6+
}
7+
8+
// operator=: (out this, move that) = {
9+
// name = that.name + "(CM)";
10+
// std::cout << "ctor - move ";
11+
// }
12+
13+
operator=: (inout this, that) = {
14+
addr = that.addr + "(AC)";
15+
std::cout << "assign - copy ";
16+
}
17+
18+
operator=: (inout this, move that) = {
19+
std::cout << "assign - move ";
20+
}
21+
22+
operator=: (out this, x: std::string) = {
23+
name = x;
24+
std::cout << "ctor - from string ";
25+
}
26+
27+
name: std::string = "Henry";
28+
addr: std::string = "123 Ford Dr.";
29+
30+
print: (
31+
this,
32+
prefix: std::string_view,
33+
suffix: std::string_view
34+
)
35+
= std::cout << prefix << "[ (name)$ | (addr)$ ]" << suffix;
36+
37+
}
38+
39+
main: () = {
40+
std::cout << "Function invoked Call syntax Results\n";
41+
std::cout << "---------------------- ------------ ------------------------------------------------------\n";
42+
43+
x: myclass = "Henry";
44+
x.print(" construct ", "\n");
45+
x = "Clara";
46+
x.print(" assign ", "\n");
47+
48+
y := x;
49+
y.print(" cp-construct ", " <- ");
50+
x.print("", "\n");
51+
52+
z := (move x);
53+
z.print(" mv-construct ", " <- ");
54+
x.print("", "\n");
55+
56+
z = y;
57+
z.print(" cp-assign ", " <- ");
58+
y.print("", "\n");
59+
60+
z = (move y);
61+
z.print(" mv-assign ", " <- ");
62+
y.print("", "\n");
63+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
2+
myclass : type = {
3+
4+
operator=: (out this, that) = {
5+
std::cout << "ctor - copy (GENERAL)";
6+
}
7+
8+
// operator=: (out this, move that) = {
9+
// name = that.name + "(CM)";
10+
// std::cout << "ctor - move ";
11+
// }
12+
13+
// operator=: (inout this, that) = {
14+
// addr = that.addr + "(AC)";
15+
// std::cout << "assign - copy ";
16+
// }
17+
18+
// operator=: (inout this, move that) = {
19+
// std::cout << "assign - move ";
20+
// }
21+
22+
operator=: (out this, x: std::string) = {
23+
name = x;
24+
std::cout << "ctor - from string ";
25+
}
26+
27+
name: std::string = "Henry";
28+
addr: std::string = "123 Ford Dr.";
29+
30+
print: (
31+
this,
32+
prefix: std::string_view,
33+
suffix: std::string_view
34+
)
35+
= std::cout << prefix << "[ (name)$ | (addr)$ ]" << suffix;
36+
37+
}
38+
39+
main: () = {
40+
std::cout << "Function invoked Call syntax Results\n";
41+
std::cout << "---------------------- ------------ ------------------------------------------------------\n";
42+
43+
x: myclass = "Henry";
44+
x.print(" construct ", "\n");
45+
x = "Clara";
46+
x.print(" assign ", "\n");
47+
48+
y := x;
49+
y.print(" cp-construct ", " <- ");
50+
x.print("", "\n");
51+
52+
z := (move x);
53+
z.print(" mv-construct ", " <- ");
54+
x.print("", "\n");
55+
56+
z = y;
57+
z.print(" cp-assign ", " <- ");
58+
y.print("", "\n");
59+
60+
z = (move y);
61+
z.print(" mv-assign ", " <- ");
62+
y.print("", "\n");
63+
}

regression-tests/test-results/clang-12/pure2-types-basics.cpp.execution

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ myclass: implicit from int
1818
myclass: explicit from string
1919
data: 99, more: syzygy
2020
myclass: implicit from int
21-
data: 84, more: syzygy
21+
data: 84, more: 504
2222
myclass: explicit from string
2323
data: 99, more: syzygy
2424
myclass: destructor
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Function invoked Call syntax Results
2+
---------------------- ------------ ------------------------------------------------------
3+
ctor - from string construct [ Henry | 123 Ford Dr. ]
4+
ctor - from string assign [ Clara | 123 Ford Dr. ]
5+
ctor - copy (GENERAL) cp-construct [ Clara | 123 Ford Dr. ] <- [ Clara | 123 Ford Dr. ]
6+
ctor - move mv-construct [ Clara(CM) | 123 Ford Dr. ] <- [ | ]
7+
assign - copy cp-assign [ Clara | 123 Ford Dr.(AC) ] <- [ Clara | 123 Ford Dr. ]
8+
assign - move mv-assign [ Clara | 123 Ford Dr. ] <- [ | ]

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

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Function invoked Call syntax Results
2+
---------------------- ------------ ------------------------------------------------------
3+
ctor - from string construct [ Henry | 123 Ford Dr. ]
4+
ctor - from string assign [ Clara | 123 Ford Dr. ]
5+
ctor - copy (GENERAL) cp-construct [ Clara | 123 Ford Dr. ] <- [ Clara | 123 Ford Dr. ]
6+
ctor - move mv-construct [ Clara(CM) | 123 Ford Dr. ] <- [ | ]
7+
assign - copy cp-assign [ Clara | 123 Ford Dr.(AC) ] <- [ Clara | 123 Ford Dr. ]
8+
assign - copy mv-assign [ Clara | 123 Ford Dr.(AC) ] <- [ | ]

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

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Function invoked Call syntax Results
2+
---------------------- ------------ ------------------------------------------------------
3+
ctor - from string construct [ Henry | 123 Ford Dr. ]
4+
ctor - from string assign [ Clara | 123 Ford Dr. ]
5+
ctor - copy (GENERAL) cp-construct [ Clara | 123 Ford Dr. ] <- [ Clara | 123 Ford Dr. ]
6+
ctor - move mv-construct [ Clara(CM) | 123 Ford Dr. ] <- [ | ]
7+
ctor - copy (GENERAL) cp-assign [ Clara | 123 Ford Dr. ] <- [ Clara | 123 Ford Dr. ]
8+
assign - move mv-assign [ Clara | 123 Ford Dr. ] <- [ | ]

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

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Function invoked Call syntax Results
2+
---------------------- ------------ ------------------------------------------------------
3+
ctor - from string construct [ Henry | 123 Ford Dr. ]
4+
ctor - from string assign [ Clara | 123 Ford Dr. ]
5+
ctor - copy (GENERAL) cp-construct [ Clara | 123 Ford Dr. ] <- [ Clara | 123 Ford Dr. ]
6+
ctor - copy (GENERAL) mv-construct [ Clara | 123 Ford Dr. ] <- [ | ]
7+
assign - copy cp-assign [ Clara | 123 Ford Dr.(AC) ] <- [ Clara | 123 Ford Dr. ]
8+
assign - move mv-assign [ Clara | 123 Ford Dr. ] <- [ | ]

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

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Function invoked Call syntax Results
2+
---------------------- ------------ ------------------------------------------------------
3+
ctor - from string construct [ Henry | 123 Ford Dr. ]
4+
ctor - from string assign [ Clara | 123 Ford Dr. ]
5+
ctor - copy (GENERAL) cp-construct [ Clara | 123 Ford Dr. ] <- [ Clara | 123 Ford Dr. ]
6+
ctor - copy (GENERAL) mv-construct [ Clara | 123 Ford Dr. ] <- [ | ]
7+
ctor - copy (GENERAL) cp-assign [ Clara | 123 Ford Dr. ] <- [ Clara | 123 Ford Dr. ]
8+
ctor - copy (GENERAL) mv-assign [ Clara | 123 Ford Dr. ] <- [ | ]

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

Whitespace-only changes.

regression-tests/test-results/gcc-10/pure2-types-basics.cpp.execution

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ myclass: implicit from int
1818
myclass: explicit from string
1919
data: 99, more: syzygy
2020
myclass: implicit from int
21-
data: 84, more: syzygy
21+
data: 84, more: 504
2222
myclass: explicit from string
2323
data: 99, more: syzygy
2424
myclass: destructor

0 commit comments

Comments
 (0)