Skip to content

Commit ced2de1

Browse files
committed
Add statement::get_parameters to reflection API
1 parent c388eff commit ced2de1

File tree

8 files changed

+799
-680
lines changed

8 files changed

+799
-680
lines changed

regression-tests/pure2-function-body-reflection.cpp2

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,10 @@ test: @sample_traverser type =
4444
= {
4545
a := -1;
4646
b := +2;
47+
48+
(local := a-b)
4749
if !true {
48-
return 0;
50+
return local;
4951
}
5052
return a+b;
5153
}
@@ -66,7 +68,12 @@ test: @sample_traverser type =
6668
= {
6769
v: std::vector = (1, 2, 3);
6870

69-
for v do (value) { std::cout << value << "\n"; }
71+
(copy index := 1)
72+
for v
73+
do (value)
74+
{
75+
std::cout << "(index)$ (value)$\n";
76+
}
7077

7178
i := 0;
7279
while i < 3 next i += 1 { std::cout << i << "\n"; }

regression-tests/test-results/pure2-function-body-reflection.cpp

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace ns {
1414
#line 7 "pure2-function-body-reflection.cpp2"
1515
class test;
1616

17-
#line 81 "pure2-function-body-reflection.cpp2"
17+
#line 88 "pure2-function-body-reflection.cpp2"
1818
}
1919

2020

@@ -52,20 +52,20 @@ using branches_ret = double;
5252
#line 43 "pure2-function-body-reflection.cpp2"
5353
public: [[nodiscard]] static auto prefix() -> int;
5454

55-
#line 53 "pure2-function-body-reflection.cpp2"
55+
#line 55 "pure2-function-body-reflection.cpp2"
5656
public: static auto postfix(double& a) -> void;
5757

58-
#line 59 "pure2-function-body-reflection.cpp2"
58+
#line 61 "pure2-function-body-reflection.cpp2"
5959
public: [[nodiscard]] static auto qualified_ids() -> auto;
6060

61-
#line 65 "pure2-function-body-reflection.cpp2"
61+
#line 67 "pure2-function-body-reflection.cpp2"
6262
public: static auto loops() -> void;
6363
public: test() = default;
6464
public: test(test const&) = delete; /* No 'that' constructor, suppress copy */
6565
public: auto operator=(test const&) -> void = delete;
6666

6767

68-
#line 76 "pure2-function-body-reflection.cpp2"
68+
#line 83 "pure2-function-body-reflection.cpp2"
6969
};
7070

7171
// This function will not be visible as a namespace member while reflecting on ns::test
@@ -136,45 +136,60 @@ auto sample_function_before_type() -> void{}
136136
{
137137
auto a {-1};
138138
auto b {+2};
139+
{
140+
auto const& local{a - b};
141+
142+
#line 49 "pure2-function-body-reflection.cpp2"
139143
if (!(true)) {
140-
return 0;
144+
return local;
141145
}
146+
}
147+
#line 52 "pure2-function-body-reflection.cpp2"
142148
return cpp2::move(a) + cpp2::move(b);
143149
}
144150

145-
#line 53 "pure2-function-body-reflection.cpp2"
151+
#line 55 "pure2-function-body-reflection.cpp2"
146152
auto test::postfix(double& a) -> void
147153
{
148154
auto ptr {&a};
149155
--++*cpp2::impl::assert_not_null(cpp2::move(ptr));
150156
}
151157

152-
#line 59 "pure2-function-body-reflection.cpp2"
158+
#line 61 "pure2-function-body-reflection.cpp2"
153159
[[nodiscard]] auto test::qualified_ids() -> auto
154160
{
155161
std::vector<int> v {1, 2, 3};
156162
return CPP2_UFCS(ssize)(cpp2::move(v));
157163
}
158164

159-
#line 65 "pure2-function-body-reflection.cpp2"
165+
#line 67 "pure2-function-body-reflection.cpp2"
160166
auto test::loops() -> void
161167
{
162168
std::vector v {1, 2, 3};
169+
{
170+
auto index{1};
171+
172+
#line 72 "pure2-function-body-reflection.cpp2"
173+
for (
174+
auto const& value : cpp2::move(v) )
175+
{
176+
std::cout << "" + cpp2::to_string(index) + " " + cpp2::to_string(value) + "\n";
177+
}
178+
}
163179

164-
for ( auto const& value : cpp2::move(v) ) {std::cout << value << "\n"; }
165-
180+
#line 78 "pure2-function-body-reflection.cpp2"
166181
auto i {0};
167182
for( ; cpp2::impl::cmp_less(i,3); i += 1 ) {std::cout << i << "\n"; }
168183

169184
do {std::cout << "plugh\n"; } while ( false);
170185
}
171186

172-
#line 79 "pure2-function-body-reflection.cpp2"
187+
#line 86 "pure2-function-body-reflection.cpp2"
173188
auto sample_function_after_type() -> void{}
174189

175190
}
176191

177-
#line 83 "pure2-function-body-reflection.cpp2"
192+
#line 90 "pure2-function-body-reflection.cpp2"
178193
auto main() -> int{
179194
std::cout << "calling generated function ns::add_1... ns::add_1(42) returned " + cpp2::to_string(ns::add_1(42)) + "\n";
180195
}

regression-tests/test-results/pure2-function-body-reflection.cpp2.output

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,18 @@ pure2-function-body-reflection.cpp2...Type: test
251251
op: +
252252
expression
253253
2
254+
statement parameters:
255+
parameter:
256+
passing style: in
257+
declaration:
258+
Object: name local, type _
259+
Initializer:
260+
additive
261+
term:
262+
a
263+
op: -
264+
term:
265+
b
254266
if statement
255267
condition:
256268
prefix expression
@@ -261,7 +273,7 @@ pure2-function-body-reflection.cpp2...Type: test
261273
compound statement
262274
return statement
263275
expression
264-
0
276+
local
265277
false branch:
266278
compound statement (empty)
267279
return statement
@@ -327,6 +339,13 @@ pure2-function-body-reflection.cpp2...Type: test
327339
2
328340
3
329341
)
342+
statement parameters:
343+
parameter:
344+
passing style: copy
345+
declaration:
346+
Object: name index, type _
347+
Initializer:
348+
1
330349
for loop:
331350
range:
332351
v
@@ -342,10 +361,33 @@ pure2-function-body-reflection.cpp2...Type: test
342361
std::cout
343362
op: <<
344363
term:
345-
value
346-
op: <<
347-
term:
348-
"\n"
364+
additive
365+
term:
366+
""
367+
op: +
368+
term:
369+
postfix expression
370+
expression
371+
cpp2::to_string
372+
op: (
373+
(
374+
index
375+
)
376+
op: +
377+
term:
378+
" "
379+
op: +
380+
term:
381+
postfix expression
382+
expression
383+
cpp2::to_string
384+
op: (
385+
(
386+
value
387+
)
388+
op: +
389+
term:
390+
"\n"
349391
Declaration: i
350392
Object: name i, type _
351393
Initializer:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11

2-
cppfront compiler v0.8.2 Build A827:1455
2+
cppfront compiler v0.8.2 Build A828:0952
33
SPDX-License-Identifier Apache-2.0 WITH LLVM-exception
44
Copyright (c) 2022-2025 Herb Sutter

source/build.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
"A827:1455"
1+
"A828:0952"

source/parse.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,6 +2698,8 @@ struct statement_node
26982698
auto is_inspect () const -> bool { return statement.index() == inspect; }
26992699
auto is_jump () const -> bool { return statement.index() == jump; }
27002700

2701+
auto get_parameters()
2702+
-> std::vector<parameter_declaration_node*>;
27012703
template<typename Node>
27022704
auto get_if()
27032705
-> Node*
@@ -2988,6 +2990,19 @@ struct parameter_declaration_list_node
29882990
};
29892991

29902992

2993+
auto statement_node::get_parameters()
2994+
-> std::vector<parameter_declaration_node*>
2995+
{
2996+
auto ret = std::vector<parameter_declaration_node*>{};
2997+
if (parameters) {
2998+
for (auto& param : parameters->parameters) {
2999+
ret.push_back( param.get() );
3000+
}
3001+
}
3002+
return ret;
3003+
}
3004+
3005+
29913006
auto statement_node::visit(auto& v, int depth)
29923007
-> void
29933008
{

0 commit comments

Comments
 (0)