Skip to content

Commit 394a77a

Browse files
committed
Make enums default-constructible (and regular), closes #873
An `@enum` default-constructs to its first value, and a `@flag_enum` to its `none` value
1 parent 7007410 commit 394a77a

File tree

5 files changed

+115
-86
lines changed

5 files changed

+115
-86
lines changed

regression-tests/test-results/pure2-enum.cpp

+49-40
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,19 @@ class skat_game {
2929
private: cpp2::i8 _value; private: constexpr skat_game(cpp2::in<cpp2::i64> _val);
3030

3131
private: constexpr auto operator=(cpp2::in<cpp2::i64> _val) -> skat_game& ;
32-
public: [[nodiscard]] constexpr auto get_raw_value() const& -> cpp2::i8;
33-
public: constexpr skat_game(skat_game const& that);
34-
public: constexpr auto operator=(skat_game const& that) -> skat_game& ;
35-
public: constexpr skat_game(skat_game&& that) noexcept;
36-
public: constexpr auto operator=(skat_game&& that) noexcept -> skat_game& ;
37-
public: [[nodiscard]] auto operator<=>(skat_game const& that) const& -> std::strong_ordering = default;
3832
public: static const skat_game diamonds;
3933
public: static const skat_game hearts;
4034
public: static const skat_game spades;
4135
public: static const skat_game clubs;
4236
public: static const skat_game grand;
4337
public: static const skat_game null;
38+
public: [[nodiscard]] constexpr auto get_raw_value() const& -> cpp2::i8;
39+
public: constexpr explicit skat_game();
40+
public: constexpr skat_game(skat_game const& that);
41+
public: constexpr auto operator=(skat_game const& that) -> skat_game& ;
42+
public: constexpr skat_game(skat_game&& that) noexcept;
43+
public: constexpr auto operator=(skat_game&& that) noexcept -> skat_game& ;
44+
public: [[nodiscard]] auto operator<=>(skat_game const& that) const& -> std::strong_ordering = default;
4445
public: [[nodiscard]] auto to_string() const& -> std::string;
4546

4647
#line 4 "pure2-enum.cpp2"
@@ -58,14 +59,15 @@ class janus {
5859
private: cpp2::i8 _value; private: constexpr janus(cpp2::in<cpp2::i64> _val);
5960

6061
private: constexpr auto operator=(cpp2::in<cpp2::i64> _val) -> janus& ;
62+
public: static const janus past;
63+
public: static const janus future;
6164
public: [[nodiscard]] constexpr auto get_raw_value() const& -> cpp2::i8;
65+
public: constexpr explicit janus();
6266
public: constexpr janus(janus const& that);
6367
public: constexpr auto operator=(janus const& that) -> janus& ;
6468
public: constexpr janus(janus&& that) noexcept;
6569
public: constexpr auto operator=(janus&& that) noexcept -> janus& ;
6670
public: [[nodiscard]] auto operator<=>(janus const& that) const& -> std::strong_ordering = default;
67-
public: static const janus past;
68-
public: static const janus future;
6971
public: [[nodiscard]] auto to_string() const& -> std::string;
7072

7173
#line 19 "pure2-enum.cpp2"
@@ -75,12 +77,6 @@ class file_attributes {
7577
private: cpp2::u8 _value; private: constexpr file_attributes(cpp2::in<cpp2::i64> _val);
7678

7779
private: constexpr auto operator=(cpp2::in<cpp2::i64> _val) -> file_attributes& ;
78-
public: [[nodiscard]] constexpr auto get_raw_value() const& -> cpp2::u8;
79-
public: constexpr file_attributes(file_attributes const& that);
80-
public: constexpr auto operator=(file_attributes const& that) -> file_attributes& ;
81-
public: constexpr file_attributes(file_attributes&& that) noexcept;
82-
public: constexpr auto operator=(file_attributes&& that) noexcept -> file_attributes& ;
83-
public: [[nodiscard]] auto operator<=>(file_attributes const& that) const& -> std::strong_ordering = default;
8480
public: constexpr auto operator|=(file_attributes const& that) & -> void;
8581
public: constexpr auto operator&=(file_attributes const& that) & -> void;
8682
public: constexpr auto operator^=(file_attributes const& that) & -> void;
@@ -95,6 +91,13 @@ public: static const file_attributes current;
9591
public: static const file_attributes obsolete;
9692
public: static const file_attributes cached_and_current;
9793
public: static const file_attributes none;
94+
public: [[nodiscard]] constexpr auto get_raw_value() const& -> cpp2::u8;
95+
public: constexpr explicit file_attributes();
96+
public: constexpr file_attributes(file_attributes const& that);
97+
public: constexpr auto operator=(file_attributes const& that) -> file_attributes& ;
98+
public: constexpr file_attributes(file_attributes&& that) noexcept;
99+
public: constexpr auto operator=(file_attributes&& that) noexcept -> file_attributes& ;
100+
public: [[nodiscard]] auto operator<=>(file_attributes const& that) const& -> std::strong_ordering = default;
98101
public: [[nodiscard]] auto to_string() const& -> std::string;
99102

100103
#line 22 "pure2-enum.cpp2"
@@ -118,17 +121,6 @@ constexpr skat_game::skat_game(cpp2::in<cpp2::i64> _val)
118121
constexpr auto skat_game::operator=(cpp2::in<cpp2::i64> _val) -> skat_game& {
119122
_value = cpp2::unsafe_narrow<cpp2::i8>(_val);
120123
return *this; }
121-
[[nodiscard]] constexpr auto skat_game::get_raw_value() const& -> cpp2::i8 { return _value; }
122-
constexpr skat_game::skat_game(skat_game const& that)
123-
: _value{ that._value }{}
124-
constexpr auto skat_game::operator=(skat_game const& that) -> skat_game& {
125-
_value = that._value;
126-
return *this;}
127-
constexpr skat_game::skat_game(skat_game&& that) noexcept
128-
: _value{ std::move(that)._value }{}
129-
constexpr auto skat_game::operator=(skat_game&& that) noexcept -> skat_game& {
130-
_value = std::move(that)._value;
131-
return *this;}
132124
inline CPP2_CONSTEXPR skat_game skat_game::diamonds = 9;
133125

134126
inline CPP2_CONSTEXPR skat_game skat_game::hearts = 10;
@@ -141,6 +133,19 @@ inline CPP2_CONSTEXPR skat_game skat_game::grand = 20;
141133

142134
inline CPP2_CONSTEXPR skat_game skat_game::null = 23;
143135

136+
[[nodiscard]] constexpr auto skat_game::get_raw_value() const& -> cpp2::i8 { return _value; }
137+
constexpr skat_game::skat_game()
138+
: _value{ diamonds._value }{}
139+
constexpr skat_game::skat_game(skat_game const& that)
140+
: _value{ that._value }{}
141+
constexpr auto skat_game::operator=(skat_game const& that) -> skat_game& {
142+
_value = that._value;
143+
return *this;}
144+
constexpr skat_game::skat_game(skat_game&& that) noexcept
145+
: _value{ std::move(that)._value }{}
146+
constexpr auto skat_game::operator=(skat_game&& that) noexcept -> skat_game& {
147+
_value = std::move(that)._value;
148+
return *this;}
144149
[[nodiscard]] auto skat_game::to_string() const& -> std::string{
145150
if ((*this) == diamonds) {return "diamonds"; }
146151
if ((*this) == hearts) {return "hearts"; }
@@ -163,7 +168,13 @@ return "invalid skat_game value";
163168
constexpr auto janus::operator=(cpp2::in<cpp2::i64> _val) -> janus& {
164169
_value = cpp2::unsafe_narrow<cpp2::i8>(_val);
165170
return *this; }
171+
inline CPP2_CONSTEXPR janus janus::past = 0;
172+
173+
inline CPP2_CONSTEXPR janus janus::future = 1;
174+
166175
[[nodiscard]] constexpr auto janus::get_raw_value() const& -> cpp2::i8 { return _value; }
176+
constexpr janus::janus()
177+
: _value{ past._value }{}
167178
constexpr janus::janus(janus const& that)
168179
: _value{ that._value }{}
169180
constexpr auto janus::operator=(janus const& that) -> janus& {
@@ -174,10 +185,6 @@ constexpr janus::janus(janus&& that) noexcept
174185
constexpr auto janus::operator=(janus&& that) noexcept -> janus& {
175186
_value = std::move(that)._value;
176187
return *this;}
177-
inline CPP2_CONSTEXPR janus janus::past = 0;
178-
179-
inline CPP2_CONSTEXPR janus janus::future = 1;
180-
181188
[[nodiscard]] auto janus::to_string() const& -> std::string{
182189
if ((*this) == past) {return "past"; }
183190
if ((*this) == future) {return "future"; }
@@ -189,17 +196,6 @@ inline CPP2_CONSTEXPR janus janus::future = 1;
189196
constexpr auto file_attributes::operator=(cpp2::in<cpp2::i64> _val) -> file_attributes& {
190197
_value = cpp2::unsafe_narrow<cpp2::u8>(_val);
191198
return *this; }
192-
[[nodiscard]] constexpr auto file_attributes::get_raw_value() const& -> cpp2::u8 { return _value; }
193-
constexpr file_attributes::file_attributes(file_attributes const& that)
194-
: _value{ that._value }{}
195-
constexpr auto file_attributes::operator=(file_attributes const& that) -> file_attributes& {
196-
_value = that._value;
197-
return *this;}
198-
constexpr file_attributes::file_attributes(file_attributes&& that) noexcept
199-
: _value{ std::move(that)._value }{}
200-
constexpr auto file_attributes::operator=(file_attributes&& that) noexcept -> file_attributes& {
201-
_value = std::move(that)._value;
202-
return *this;}
203199
constexpr auto file_attributes::operator|=(file_attributes const& that) & -> void { _value |= that._value; }
204200
constexpr auto file_attributes::operator&=(file_attributes const& that) & -> void { _value &= that._value; }
205201
constexpr auto file_attributes::operator^=(file_attributes const& that) & -> void { _value ^= that._value; }
@@ -219,6 +215,19 @@ inline CPP2_CONSTEXPR file_attributes file_attributes::cached_and_current = cach
219215

220216
inline CPP2_CONSTEXPR file_attributes file_attributes::none = 0;
221217

218+
[[nodiscard]] constexpr auto file_attributes::get_raw_value() const& -> cpp2::u8 { return _value; }
219+
constexpr file_attributes::file_attributes()
220+
: _value{ none._value }{}
221+
constexpr file_attributes::file_attributes(file_attributes const& that)
222+
: _value{ that._value }{}
223+
constexpr auto file_attributes::operator=(file_attributes const& that) -> file_attributes& {
224+
_value = that._value;
225+
return *this;}
226+
constexpr file_attributes::file_attributes(file_attributes&& that) noexcept
227+
: _value{ std::move(that)._value }{}
228+
constexpr auto file_attributes::operator=(file_attributes&& that) noexcept -> file_attributes& {
229+
_value = std::move(that)._value;
230+
return *this;}
222231
[[nodiscard]] auto file_attributes::to_string() const& -> std::string{
223232

224233
std::string _ret {"("};

regression-tests/test-results/version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
cppfront compiler v0.3.0 Build 8C13:1258
2+
cppfront compiler v0.3.0 Build 8C13:1356
33
Copyright(c) Herb Sutter All rights reserved
44

55
SPDX-License-Identifier: CC-BY-NC-ND-4.0

source/build.info

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"8C13:1258"
1+
"8C13:1356"

0 commit comments

Comments
 (0)