Skip to content

Commit 10241cd

Browse files
committed
Checking in various improvements done in the last few evenings
Replace `store_as_base` with generated aggregate bases - better fix for #336, thanks @JohelEGP for the suggestion! This way we also don't need to obfuscate the name anywhere beyond the constructor(s), as the right member object name just enters the class's scope If the user didn't write a constructor, provide a default constructor If the user didn't write a 'that' constructor, suppress Cpp1's compiler-generated copying and assignment Clean up emission of the just-mentioned generated/=deleted constructors, more naturally line up inside the class body following the indentation for other members that the original source code uses Rename file `load.h` to `io.h` (`file.h` was another candidate), just because it has been bothering me for a year now that except for that one file all the headers were in natural alphabetical order by compilation phase... as of this commit we now have them all in alpha order and phase order: common.h -> io.h -> lex.h -> parse.h -> [*] -> sema.h -> cppfront.h [*] coming next here: reflect.h, which will also be in both alpha order and compilation order Guard `out.construct` with `if constexpr` in case the type is not copy assignable and that path is never requested Rename `cpp2::error` to `cpp2::error_entry` to quiet a new(? why?) GCC message about shadowing the former name with `parser::error`... I get why the warning is there, but this is a slightly annoying warning to have to satisfy just to compile high-warning-clean on GCC... oh well Change semantics of emitting `.h2` files: In `-p` pure mode do the existing split of phases 0 and 1 into `.h` and phase 2 into a separate `.hpp`, but in mixed mode put all phases into the `.h`
1 parent f83ca9b commit 10241cd

15 files changed

+389
-260
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@
2424
.editorconfig
2525
*.xml
2626
*.sarif
27+
*.bin

include/cpp2util.h

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,6 @@ using in =
530530
//
531531
// out<T> For out parameter
532532
//
533-
// store_as_base<T> For member object declared before a base object
534-
//
535533
//-----------------------------------------------------------------------
536534
//
537535
template<typename T>
@@ -596,13 +594,23 @@ class out {
596594

597595
auto construct(auto&& ...args) -> void {
598596
if (has_t || called_construct()) {
599-
Default.expects( t );
600-
*t = T(CPP2_FORWARD(args)...);
597+
if constexpr (requires { *t = T(CPP2_FORWARD(args)...); }) {
598+
Default.expects( t );
599+
*t = T(CPP2_FORWARD(args)...);
600+
}
601+
else {
602+
Default.expects(!"attempted to copy assign, but copy assignment is not available");
603+
}
601604
}
602605
else {
603606
Default.expects( dt );
604607
if (dt->init) {
605-
dt->value() = T(CPP2_FORWARD(args)...);
608+
if constexpr (requires { *t = T(CPP2_FORWARD(args)...); }) {
609+
dt->value() = T(CPP2_FORWARD(args)...);
610+
}
611+
else {
612+
Default.expects(!"attempted to copy assign, but copy assignment is not available");
613+
}
606614
}
607615
else {
608616
dt->construct(CPP2_FORWARD(args)...);
@@ -613,16 +621,26 @@ class out {
613621

614622
auto construct_list(auto&& ...args) -> void {
615623
if (has_t || called_construct()) {
616-
Default.expects( t );
617-
*t = T{CPP2_FORWARD(args)...};
624+
if constexpr (requires { *t = T{CPP2_FORWARD(args)...}; }) {
625+
Default.expects( t );
626+
*t = T{CPP2_FORWARD(args)...};
627+
}
628+
else {
629+
Default.expects(!"attempted to copy assign, but copy assignment is not available");
630+
}
618631
}
619632
else {
620633
Default.expects( dt );
621634
if (dt->init) {
622-
dt->value() = T{CPP2_FORWARD(args)...};
635+
if constexpr (requires { *t = T{CPP2_FORWARD(args)...}; }) {
636+
dt->value() = T{CPP2_FORWARD(args)...};
637+
}
638+
else {
639+
Default.expects(!"attempted to copy assign, but copy assignment is not available");
640+
}
623641
}
624642
else {
625-
dt->construct_list(CPP2_FORWARD(args)...);
643+
dt->construct(CPP2_FORWARD(args)...);
626644
called_construct() = true;
627645
}
628646
}
@@ -641,33 +659,6 @@ class out {
641659
};
642660

643661

644-
template<String Name, typename T>
645-
class store_as_base
646-
{
647-
T value;
648-
649-
public:
650-
store_as_base()
651-
requires requires { T{ }; }
652-
: value{ } { }
653-
654-
store_as_base( T const& t )
655-
requires requires { T{t}; }
656-
: value{t} { }
657-
658-
store_as_base( T && t )
659-
requires requires { T{std::move(t)}; }
660-
: value{std::move(t)} { }
661-
662-
store_as_base( auto && args )
663-
requires requires { T{CPP2_FORWARD(args)}; }
664-
: value{CPP2_FORWARD(args)} { }
665-
666-
auto value__() -> T & { return value; }
667-
auto value__() const -> T const& { return value; }
668-
};
669-
670-
671662
//-----------------------------------------------------------------------
672663
//
673664
// CPP2_UFCS: Variadic macro generating a variadic lamba, oh my...

regression-tests/pure2-types-inheritance.cpp2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
Human: type = {
3-
operator=: (out this) = {}
43
speak: (virtual this);
54
}
65

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
Microsoft (R) C/C++ Optimizing Compiler Version 19.35.32215 for x86
1+
Microsoft (R) C/C++ Optimizing Compiler Version 19.35.32217.1 for x86
22
Copyright (C) Microsoft Corporation. All rights reserved.
33

regression-tests/test-results/pure2-requires-clauses.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ template<typename T, typename U>
2424
&& std::is_same_v<U,int> )
2525
class X {
2626
public: X();
27+
28+
public: X(X const&) = delete;
29+
public: auto operator=(X const&) -> void = delete;
30+
#line 8 "pure2-requires-clauses.cpp2"
2731
};
2832

2933
template<typename T, typename U> [[nodiscard]] auto f

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,12 @@ class myclass {
3030
public: using str = std::string;
3131

3232
private: using str2 = std::string;
33-
public: myclass() = default; myclass(myclass const&) = delete; auto operator=(myclass const&) -> void = delete; };
33+
34+
public: myclass() = default;
35+
public: myclass(myclass const&) = delete;
36+
public: auto operator=(myclass const&) -> void = delete;
37+
#line 13 "pure2-type-and-namespace-aliases.cpp2"
38+
};
3439

3540
namespace N3 = ::std::literals;
3641

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,21 @@ class myclass {
5353

5454
public: class nested {
5555
public: static auto g() -> void;
56-
public: nested() = default; nested(nested const&) = delete; auto operator=(nested const&) -> void = delete; };
56+
57+
public: nested() = default;
58+
public: nested(nested const&) = delete;
59+
public: auto operator=(nested const&) -> void = delete;
60+
#line 51 "pure2-types-basics.cpp2"
61+
};
5762

5863
public: template<typename T, typename U> [[nodiscard]] static auto f1(T const& t, U const& u) -> auto;
5964
public: template<typename T, typename U> [[nodiscard]] static auto f2(T const& t, U const& u) -> auto;
6065
public: template<auto T, auto U> [[nodiscard]] static auto f3() -> auto;
6166
public: template<cpp2::i8 T, cpp2::i16 U> [[nodiscard]] static auto f4() -> auto;
6267

68+
public: myclass(myclass const&) = delete;
69+
public: auto operator=(myclass const&) -> void = delete;
70+
#line 58 "pure2-types-basics.cpp2"
6371
};
6472

6573
}

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

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
#line 2 "pure2-types-inheritance.cpp2"
1111
class Human;
1212

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

17-
#line 12 "pure2-types-inheritance.cpp2"
17+
#line 11 "pure2-types-inheritance.cpp2"
1818
}
1919

2020
class Cyborg;
@@ -24,84 +24,95 @@ class Cyborg;
2424

2525
#line 2 "pure2-types-inheritance.cpp2"
2626
class Human {
27-
public: Human();
2827
public: virtual auto speak() const -> void = 0;
28+
29+
public: Human() = default;
30+
public: Human(Human const&) = delete;
31+
public: auto operator=(Human const&) -> void = delete;
32+
#line 4 "pure2-types-inheritance.cpp2"
2933
};
3034

3135
namespace N {
3236
template<int I> class Machine {
3337
public: explicit Machine(cpp2::in<std::string> id);
3438
public: virtual auto work() const -> void = 0;
39+
40+
public: Machine(Machine const&) = delete;
41+
public: auto operator=(Machine const&) -> void = delete;
42+
#line 10 "pure2-types-inheritance.cpp2"
3543
};
3644
}
3745

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> {
46+
struct Cyborg_name_as_base { std::string name; };
47+
struct Cyborg_address_as_base { std::string address; };
48+
#line 13 "pure2-types-inheritance.cpp2"
49+
class Cyborg: public Cyborg_name_as_base, public Human, public Cyborg_address_as_base, public N::Machine<99> {
3950

40-
#line 20 "pure2-types-inheritance.cpp2"
51+
#line 19 "pure2-types-inheritance.cpp2"
4152
public: explicit Cyborg(cpp2::in<std::string> n);
4253

43-
#line 26 "pure2-types-inheritance.cpp2"
54+
#line 25 "pure2-types-inheritance.cpp2"
4455
public: auto speak() const -> void override;
4556

46-
#line 29 "pure2-types-inheritance.cpp2"
57+
#line 28 "pure2-types-inheritance.cpp2"
4758
public: auto work() const -> void override;
4859

49-
#line 32 "pure2-types-inheritance.cpp2"
60+
#line 31 "pure2-types-inheritance.cpp2"
5061
public: auto print() const -> void;
5162

52-
#line 35 "pure2-types-inheritance.cpp2"
63+
#line 34 "pure2-types-inheritance.cpp2"
5364
public: ~Cyborg();
5465

66+
public: Cyborg(Cyborg const&) = delete;
67+
public: auto operator=(Cyborg const&) -> void = delete;
68+
#line 36 "pure2-types-inheritance.cpp2"
5569
};
5670

5771
auto make_speak(cpp2::in<Human> h) -> void;
5872

59-
#line 44 "pure2-types-inheritance.cpp2"
73+
#line 43 "pure2-types-inheritance.cpp2"
6074
auto do_work(cpp2::in<N::Machine<99>> m) -> void;
6175

62-
#line 49 "pure2-types-inheritance.cpp2"
76+
#line 48 "pure2-types-inheritance.cpp2"
6377
auto main() -> int;
6478

6579
//=== Cpp2 function definitions =================================================
6680

6781

68-
#line 3 "pure2-types-inheritance.cpp2"
69-
Human::Human(){}
70-
71-
#line 7 "pure2-types-inheritance.cpp2"
82+
#line 6 "pure2-types-inheritance.cpp2"
7283
namespace N {
7384

7485
template <int I> Machine<I>::Machine(cpp2::in<std::string> id){}
7586

76-
#line 12 "pure2-types-inheritance.cpp2"
87+
#line 11 "pure2-types-inheritance.cpp2"
7788
}
7889

79-
#line 20 "pure2-types-inheritance.cpp2"
90+
#line 19 "pure2-types-inheritance.cpp2"
8091
Cyborg::Cyborg(cpp2::in<std::string> n)
81-
: cpp2::store_as_base<"name",std::string>{ n }
92+
: Cyborg_name_as_base{ n }
8293
, Human{ }
83-
, cpp2::store_as_base<"address",std::string>{ "123 Main St." }
94+
, Cyborg_address_as_base{ "123 Main St." }
8495
, N::Machine<99>{ "Acme Corp. engineer tech" }
85-
#line 20 "pure2-types-inheritance.cpp2"
96+
#line 19 "pure2-types-inheritance.cpp2"
8697
{
8798

88-
#line 23 "pure2-types-inheritance.cpp2"
89-
std::cout << cpp2::to_string(cpp2::store_as_base<"name",std::string>::value__()) + " checks in for the day's shift\n";
99+
#line 22 "pure2-types-inheritance.cpp2"
100+
std::cout << cpp2::to_string(name) + " checks in for the day's shift\n";
90101
}
91102

92103
auto Cyborg::speak() const -> void {
93-
std::cout << cpp2::to_string(cpp2::store_as_base<"name",std::string>::value__()) + " cracks a few jokes with a coworker\n"; }
104+
std::cout << cpp2::to_string(name) + " cracks a few jokes with a coworker\n"; }
94105

95106
auto Cyborg::work() const -> void {
96-
std::cout << cpp2::to_string(cpp2::store_as_base<"name",std::string>::value__()) + " carries some half-tonne crates of Fe2O3 to cold storage\n"; }
107+
std::cout << cpp2::to_string(name) + " carries some half-tonne crates of Fe2O3 to cold storage\n"; }
97108

98109
auto Cyborg::print() const -> void {
99-
std::cout << "printing: " + cpp2::to_string(cpp2::store_as_base<"name",std::string>::value__()) + " lives at " + cpp2::to_string(cpp2::store_as_base<"address",std::string>::value__()) + "\n"; }
110+
std::cout << "printing: " + cpp2::to_string(name) + " lives at " + cpp2::to_string(address) + "\n"; }
100111

101112
Cyborg::~Cyborg() {
102-
std::cout << "Tired but satisfied after another successful day, " + cpp2::to_string(cpp2::store_as_base<"name",std::string>::value__()) + " checks out and goes home to their family\n"; }
113+
std::cout << "Tired but satisfied after another successful day, " + cpp2::to_string(name) + " checks out and goes home to their family\n"; }
103114

104-
#line 39 "pure2-types-inheritance.cpp2"
115+
#line 38 "pure2-types-inheritance.cpp2"
105116
auto make_speak(cpp2::in<Human> h) -> void{
106117
std::cout << "-> [vcall: make_speak] ";
107118
CPP2_UFCS_0(speak, h);

regression-tests/test-results/pure2-types-order-independence-and-nesting.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class X {
4747
// X::exx member function description here
4848
public: auto exx(cpp2::in<int> count) const -> void;
4949

50+
public: X(X const&) = delete;
51+
public: auto operator=(X const&) -> void = delete;
5052
#line 42 "pure2-types-order-independence-and-nesting.cpp2"
5153
};
5254

@@ -61,6 +63,9 @@ class Y {
6163

6264
public: auto why(cpp2::in<int> count) const -> void;
6365

66+
public: Y(Y const&) = delete;
67+
public: auto operator=(Y const&) -> void = delete;
68+
#line 53 "pure2-types-order-independence-and-nesting.cpp2"
6469
};
6570

6671
namespace M {
@@ -69,8 +74,18 @@ namespace M {
6974
template<typename T, typename U> class A {
7075
public: template<int I> class B {
7176
public: template<typename V, int J, typename W> static auto f(W const& w) -> void;
72-
public: B() = default; B(B const&) = delete; auto operator=(B const&) -> void = delete; };
73-
public: A() = default; A(A const&) = delete; auto operator=(A const&) -> void = delete; };
77+
78+
public: B() = default;
79+
public: B(B const&) = delete;
80+
public: auto operator=(B const&) -> void = delete;
81+
#line 61 "pure2-types-order-independence-and-nesting.cpp2"
82+
};
83+
84+
public: A() = default;
85+
public: A(A const&) = delete;
86+
public: auto operator=(A const&) -> void = delete;
87+
#line 62 "pure2-types-order-independence-and-nesting.cpp2"
88+
};
7489

7590
}
7691

source/common.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,14 @@ struct multiline_raw_string
250250
//
251251
//-----------------------------------------------------------------------
252252
//
253-
struct error
253+
struct error_entry
254254
{
255255
source_position where;
256256
std::string msg;
257257
bool internal = false;
258258
bool fallback = false; // only emit this message if there was nothing better
259259

260-
error(
260+
error_entry(
261261
source_position w,
262262
std::string const& m,
263263
bool i = false,
@@ -269,7 +269,7 @@ struct error
269269
, fallback{f}
270270
{ }
271271

272-
auto operator==(error const& that)
272+
auto operator==(error_entry const& that)
273273
-> bool
274274
{
275275
return

0 commit comments

Comments
 (0)