Skip to content

Commit 4868673

Browse files
committed
Add alias pretty-print visualization
1 parent 586ff20 commit 4868673

File tree

4 files changed

+57
-33
lines changed

4 files changed

+57
-33
lines changed

regression-tests/pure2-print.cpp2

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
outer: @print type = {
55

6+
object_alias: <T> T requires true == 42;
7+
68
mytype: final type =
79
{
810
protected f: () -> int = 42;
@@ -62,14 +64,21 @@ outer: @print type = {
6264
}
6365

6466
test: () = {
67+
namespace_alias: namespace == ::std;
68+
69+
type_alias: type == std::array<int,10>;
70+
71+
object_alias_1: i8 == 42;
72+
object_alias_2: == 42;
73+
74+
6575
var: ::outer::mytype = ();
6676
std::cout << var.g(42) << "\n";
6777

68-
std::cout << inspect var.g(42) -> std::string {
78+
std::cout << inspect var.g(42) -> namespace_alias::string {
6979
is 43 = "forty-and-three";
7080
is _ = "default case";
7181
} << "\n";
72-
7382
}
7483

7584
x: <Ts...: type> type = {

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

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,29 @@ class outer;
1919
#line 4 "pure2-print.cpp2"
2020
class outer {
2121

22+
public: template<typename T>
23+
CPP2_REQUIRES_ (true)
24+
#line 6 "pure2-print.cpp2"
25+
T static constexpr object_alias = 42;
26+
2227
public: class mytype final
2328
{
2429
protected: [[nodiscard]] static auto f() -> int;
2530

2631
public: [[nodiscard]] virtual auto g(cpp2::in<int> i) const& -> int;
2732

2833

29-
#line 27 "pure2-print.cpp2"
34+
#line 29 "pure2-print.cpp2"
3035
private: [[nodiscard]] static auto h(cpp2::in<std::string> s, std::map<int const,std::string>& m) -> std::string;
3136
struct values__ret { int offset; std::string name; };
3237

3338

3439

35-
#line 50 "pure2-print.cpp2"
40+
#line 52 "pure2-print.cpp2"
3641
public: template<typename T> [[nodiscard]] auto values(T const& t) const& -> values__ret;
3742

3843

39-
#line 55 "pure2-print.cpp2"
44+
#line 57 "pure2-print.cpp2"
4045
public: explicit mytype();
4146

4247
public: mytype([[maybe_unused]] mytype const& that);
@@ -49,31 +54,31 @@ class outer {
4954
public: static auto test() -> void;
5055

5156

52-
#line 75 "pure2-print.cpp2"
57+
#line 84 "pure2-print.cpp2"
5358
public: template<typename ...Ts> class x {
5459
private: std::tuple<Ts...> tup {};
5560
public: x() = default;
5661
public: x(x const&) = delete; /* No 'that' constructor, suppress copy */
5762
public: auto operator=(x const&) -> void = delete;
5863

59-
#line 77 "pure2-print.cpp2"
64+
#line 86 "pure2-print.cpp2"
6065
};
6166

6267
public: template<typename ...Args> static auto print(std::ostream& out, Args const& ...args) -> void
6368
CPP2_REQUIRES_ (cpp2::cmp_greater_eq(sizeof(Args)...,0))
64-
#line 79 "pure2-print.cpp2"
69+
#line 88 "pure2-print.cpp2"
6570
;
6671

6772

68-
#line 83 "pure2-print.cpp2"
73+
#line 92 "pure2-print.cpp2"
6974
public: template<typename ...Args> [[nodiscard]] static auto all(Args const& ...args) -> bool;
7075

7176
public: outer() = default;
7277
public: outer(outer const&) = delete; /* No 'that' constructor, suppress copy */
7378
public: auto operator=(outer const&) -> void = delete;
7479

7580

76-
#line 86 "pure2-print.cpp2"
81+
#line 95 "pure2-print.cpp2"
7782
};
7883

7984
auto main() -> int;
@@ -82,7 +87,7 @@ auto main() -> int;
8287
//=== Cpp2 function definitions =================================================
8388

8489

85-
#line 8 "pure2-print.cpp2"
90+
#line 10 "pure2-print.cpp2"
8691
[[nodiscard]] auto outer::mytype::f() -> int { return 42; }
8792

8893
[[nodiscard]] auto outer::mytype::g(cpp2::in<int> i) const& -> int{
@@ -104,11 +109,11 @@ auto main() -> int;
104109

105110
[[nodiscard]] auto outer::mytype::h(cpp2::in<std::string> s, std::map<int const,std::string>& m) -> std::string
106111

107-
#line 30 "pure2-print.cpp2"
112+
#line 32 "pure2-print.cpp2"
108113
{
109114
cpp2::Default.expects(CPP2_UFCS_0(empty, m) == false || false, "");
110115
cpp2::Bounds.expects([_0 = 0, _1 = CPP2_UFCS_0(ssize, m), _2 = 100]{ return cpp2::cmp_less(_0,_1) && cpp2::cmp_less(_1,_2); }() && true != false, "");
111-
#line 31 "pure2-print.cpp2"
116+
#line 33 "pure2-print.cpp2"
112117
auto a {[]() -> void{}};
113118
auto b {[]() -> void{}};
114119
auto c {[]() -> void{}};
@@ -118,10 +123,10 @@ auto main() -> int;
118123
do {} while ( CPP2_UFCS_0(empty, s) && [&]{ b() ; return true; }() );
119124

120125
for ( [[maybe_unused]] auto const& param1 : m ) {
121-
#line 39 "pure2-print.cpp2"
122-
{ do {goto CONTINUE_39_13; } while (false); c(); } CPP2_CONTINUE_BREAK(39_13) }
123-
124126
#line 41 "pure2-print.cpp2"
127+
{ do {goto CONTINUE_41_13; } while (false); c(); } CPP2_CONTINUE_BREAK(41_13) }
128+
129+
#line 43 "pure2-print.cpp2"
125130
if (cpp2::is(!(CPP2_UFCS_0(empty, s)), (true))) {std::move(a)(); }
126131
else {if (!(CPP2_UFCS_0(empty, m))) {std::move(b)(); }
127132
else {std::move(c)(); }}
@@ -134,7 +139,7 @@ auto main() -> int;
134139
template<typename T> [[nodiscard]] auto outer::mytype::values(T const& t) const& -> values__ret{
135140
cpp2::deferred_init<int> offset;
136141
cpp2::deferred_init<std::string> name;
137-
#line 51 "pure2-print.cpp2"
142+
#line 53 "pure2-print.cpp2"
138143
offset.construct(53);
139144
name.construct("plugh");
140145
return { std::move(offset.value()), std::move(name.value()) }; }
@@ -147,30 +152,37 @@ auto main() -> int;
147152

148153
auto outer::mytype::variadic(auto const& ...x) -> void{}
149154

150-
#line 64 "pure2-print.cpp2"
155+
#line 66 "pure2-print.cpp2"
151156
auto outer::test() -> void{
157+
namespace namespace_alias = ::std;
158+
159+
using type_alias = std::array<int,10>;
160+
161+
cpp2::i8 const& object_alias_1 = 42;
162+
auto const& object_alias_2 = 42;
163+
164+
#line 75 "pure2-print.cpp2"
152165
::outer::mytype var {};
153166
std::cout << CPP2_UFCS(g, var, 42) << "\n";
154167

155-
std::cout << [&] () -> std::string { auto&& __expr = CPP2_UFCS(g, std::move(var), 42);
156-
if (cpp2::is(__expr, 43)) { if constexpr( requires{"forty-and-three";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("forty-and-three")),std::string> ) return "forty-and-three"; else return std::string{}; else return std::string{}; }
168+
std::cout << [&] () -> namespace_alias::string { auto&& __expr = CPP2_UFCS(g, std::move(var), 42);
169+
if (cpp2::is(__expr, 43)) { if constexpr( requires{"forty-and-three";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF(("forty-and-three")),namespace_alias::string> ) return "forty-and-three"; else return namespace_alias::string{}; else return namespace_alias::string{}; }
157170
else return "default case"; }
158171
() << "\n";
159-
160172
}
161173

162-
#line 79 "pure2-print.cpp2"
174+
#line 88 "pure2-print.cpp2"
163175
template<typename ...Args> auto outer::print(std::ostream& out, Args const& ...args) -> void
164176
requires (cpp2::cmp_greater_eq(sizeof(Args)...,0))
165-
#line 79 "pure2-print.cpp2"
177+
#line 88 "pure2-print.cpp2"
166178
{
167179
(out << ... << args);
168180
}
169181

170182
template<typename ...Args> [[nodiscard]] auto outer::all(Args const& ...args) -> bool {
171183
return (... && args); }
172184

173-
#line 88 "pure2-print.cpp2"
185+
#line 97 "pure2-print.cpp2"
174186
auto main() -> int{
175187
outer::test();
176188
}

regression-tests/test-results/pure2-print.cpp2.output

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pure2-print.cpp2...
22

33
outer: type =
44
{
5+
object_alias: <T: type > T requires (true) == 42;
56
mytype: final type =
67
{
78
protected f:() -> move int = 42;
@@ -115,9 +116,13 @@ outer: type =
115116

116117
test:() =
117118
{
119+
namespace_alias: namespace == ::std;
120+
type_alias: type == std::array<int, 10>;
121+
object_alias_1: i8 == 42;
122+
object_alias_2: == 42;
118123
var: ::outer::mytype = ();
119124
std::cout << var.g(42) << "\n";
120-
std::cout << inspect var.g(42) -> std::string {
125+
std::cout << inspect var.g(42) -> namespace_alias::string {
121126
is 43 = "forty-and-three";
122127
is _ = "default case";
123128
} << "\n";

source/parse.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4604,33 +4604,31 @@ auto pretty_print_visualize(declaration_node const& n, int indent, bool include_
46044604

46054605
auto object_type_id = std::string{};
46064606
if (a->type_id) {
4607-
object_type_id += pretty_print_visualize(*a->type_id, indent) + " ";
4607+
object_type_id += " " + pretty_print_visualize(*a->type_id, indent);
46084608
}
46094609

46104610
ret += template_params;
46114611
if (a->is_type_alias()) {
46124612
auto& t = std::get<alias_node::a_type>(a->initializer);
4613-
ret += template_params
4614-
+ "type"
4613+
ret += " type"
46154614
+ requires_clause
4616-
+ "== "
4615+
+ " == "
46174616
+ pretty_print_visualize(*t, indent)
46184617
+ ";";
46194618
}
46204619
else if (a->is_namespace_alias()) {
46214620
auto& id = std::get<alias_node::a_namespace>(a->initializer);
46224621
assert(id);
4623-
ret += "== "
4622+
ret += " namespace == "
46244623
+ pretty_print_visualize(*id, indent)
46254624
+ ";";
46264625
}
46274626
else if (a->is_object_alias()) {
46284627
auto& expr = std::get<alias_node::an_object>(a->initializer);
46294628
assert(expr);
4630-
ret += template_params
4631-
+ object_type_id
4629+
ret += object_type_id
46324630
+ requires_clause
4633-
+ "== "
4631+
+ " == "
46344632
+ pretty_print_visualize(*expr, indent)
46354633
+ ";";
46364634
}

0 commit comments

Comments
 (0)