Skip to content

Commit d9bfca1

Browse files
committed
Added reflection for loops
Also made parameter attributes accessible
1 parent 2300cc8 commit d9bfca1

File tree

8 files changed

+1466
-942
lines changed

8 files changed

+1466
-942
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ test: @sample_traverser type =
5656
v : std::vector<int> = (1, 2, 3);
5757
return v.ssize();
5858
}
59+
60+
loops: ()
61+
= {
62+
v: std::vector = (1, 2, 3);
63+
64+
for v do (value) { std::cout << value << "\n"; }
65+
66+
i := 0;
67+
while i < 3 next i += 1 { std::cout << i << "\n"; }
68+
69+
do { std::cout << "plugh\n"; } while false;
70+
}
5971
}
6072

6173
main: () = { }

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,15 @@ using branches_ret = double;
4343

4444
#line 54 "pure2-function-body-reflection.cpp2"
4545
public: [[nodiscard]] static auto qualified_ids() -> auto;
46+
47+
#line 60 "pure2-function-body-reflection.cpp2"
48+
public: static auto loops() -> void;
4649
public: test() = default;
4750
public: test(test const&) = delete; /* No 'that' constructor, suppress copy */
4851
public: auto operator=(test const&) -> void = delete;
4952

5053

51-
#line 59 "pure2-function-body-reflection.cpp2"
54+
#line 71 "pure2-function-body-reflection.cpp2"
5255
};
5356

5457
auto main() -> int;
@@ -126,6 +129,19 @@ auto main() -> int;
126129
return CPP2_UFCS(ssize)(cpp2::move(v));
127130
}
128131

129-
#line 61 "pure2-function-body-reflection.cpp2"
132+
#line 60 "pure2-function-body-reflection.cpp2"
133+
auto test::loops() -> void
134+
{
135+
std::vector v {1, 2, 3};
136+
137+
for ( auto const& value : cpp2::move(v) ) {std::cout << value << "\n"; }
138+
139+
auto i {0};
140+
for( ; cpp2::impl::cmp_less(i,3); i += 1 ) {std::cout << i << "\n"; }
141+
142+
do {std::cout << "plugh\n"; } while ( false);
143+
}
144+
145+
#line 73 "pure2-function-body-reflection.cpp2"
130146
auto main() -> int{}
131147

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

Lines changed: 130 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,18 @@ pure2-function-body-reflection.cpp2...Type: test
22
Declaration: one_liner
33
Function: one_liner
44
Parameters:
5-
Object: name a, type double
6-
Object: name b, type double
7-
Object: name c, type double
5+
parameter:
6+
passing style: in
7+
declaration:
8+
Object: name a, type double
9+
parameter:
10+
passing style: in
11+
declaration:
12+
Object: name b, type double
13+
parameter:
14+
passing style: in
15+
declaration:
16+
Object: name c, type double
817
Body:
918
multiplicative
1019
term:
@@ -22,9 +31,18 @@ pure2-function-body-reflection.cpp2...Type: test
2231
Declaration: return_list
2332
Function: return_list
2433
Returns:
25-
Object: name r, type double
26-
Object: name s, type float
27-
Object: name t, type std::string
34+
parameter:
35+
passing style: out
36+
declaration:
37+
Object: name r, type double
38+
parameter:
39+
passing style: out
40+
declaration:
41+
Object: name s, type float
42+
parameter:
43+
passing style: out
44+
declaration:
45+
Object: name t, type std::string
2846
Body:
2947
compound statement
3048
assignment
@@ -49,13 +67,25 @@ pure2-function-body-reflection.cpp2...Type: test
4967
Declaration: branches
5068
Function: branches
5169
Parameters:
52-
Object: name a, type double
53-
Object: name b, type double
54-
Object: name c, type double
70+
parameter:
71+
passing style: in
72+
declaration:
73+
Object: name a, type double
74+
parameter:
75+
passing style: in
76+
declaration:
77+
Object: name b, type double
78+
parameter:
79+
passing style: in
80+
declaration:
81+
Object: name c, type double
5582
Returns:
56-
Object: name r, type double
57-
Initializer:
58-
3.14159
83+
parameter:
84+
passing style: out
85+
declaration:
86+
Object: name r, type double
87+
Initializer:
88+
3.14159
5989
Body:
6090
compound statement
6191
if statement
@@ -115,9 +145,18 @@ pure2-function-body-reflection.cpp2...Type: test
115145
Declaration: binary_ops
116146
Function: binary_ops
117147
Parameters:
118-
Object: name a, type double
119-
Object: name b, type double
120-
Object: name c, type double
148+
parameter:
149+
passing style: inout
150+
declaration:
151+
Object: name a, type double
152+
parameter:
153+
passing style: in
154+
declaration:
155+
Object: name b, type double
156+
parameter:
157+
passing style: in
158+
declaration:
159+
Object: name c, type double
121160
Body:
122161
compound statement
123162
assignment
@@ -234,7 +273,10 @@ pure2-function-body-reflection.cpp2...Type: test
234273
Declaration: postfix
235274
Function: postfix
236275
Parameters:
237-
Object: name a, type double
276+
parameter:
277+
passing style: inout
278+
declaration:
279+
Object: name a, type double
238280
Body:
239281
compound statement
240282
Declaration: ptr
@@ -271,5 +313,77 @@ pure2-function-body-reflection.cpp2...Type: test
271313
ssize
272314
op: (
273315
()
316+
Declaration: loops
317+
Function: loops
318+
Body:
319+
compound statement
320+
Declaration: v
321+
Object: name v, type std::vector
322+
Initializer:
323+
(
324+
1
325+
2
326+
3
327+
)
328+
for loop:
329+
range:
330+
v
331+
parameter:
332+
parameter:
333+
passing style: in
334+
declaration:
335+
Object: name value, type _
336+
body:
337+
compound statement
338+
shift
339+
term:
340+
std::cout
341+
op: <<
342+
term:
343+
value
344+
op: <<
345+
term:
346+
"\n"
347+
Declaration: i
348+
Object: name i, type _
349+
Initializer:
350+
0
351+
while loop:
352+
condition:
353+
relational
354+
term:
355+
i
356+
op: <
357+
term:
358+
3
359+
body:
360+
compound statement
361+
shift
362+
term:
363+
std::cout
364+
op: <<
365+
term:
366+
i
367+
op: <<
368+
term:
369+
"\n"
370+
next expression:
371+
assignment
372+
term:
373+
i
374+
op: +=
375+
term:
376+
1
377+
do loop:
378+
condition:
379+
false
380+
body:
381+
compound statement
382+
shift
383+
term:
384+
std::cout
385+
op: <<
386+
term:
387+
"plugh\n"
274388
ok (all Cpp2, passes safety checks)
275389

regression-tests/test-results/version

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 A802:1648
2+
cppfront compiler v0.8.2 Build A802:2205
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-
"A802:1648"
1+
"A802:2205"

source/parse.h

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,6 +2353,72 @@ struct iteration_statement_node
23532353
// type(s) used in a std::unique_ptr as a member
23542354
~iteration_statement_node();
23552355

2356+
auto get_label() const
2357+
-> token const*
2358+
{
2359+
return label;
2360+
}
2361+
2362+
auto has_next() const
2363+
-> bool
2364+
{
2365+
return next_expression.get() != nullptr;
2366+
}
2367+
2368+
auto get_next_expression() const
2369+
-> assignment_expression_node*
2370+
{
2371+
return next_expression.get();
2372+
}
2373+
2374+
auto is_do() const
2375+
-> bool
2376+
{
2377+
return *identifier == "do";
2378+
}
2379+
2380+
auto is_while() const
2381+
-> bool
2382+
{
2383+
return *identifier == "while";
2384+
}
2385+
2386+
auto get_do_while_condition() const
2387+
-> logical_or_expression_node*
2388+
{
2389+
return condition.get();
2390+
}
2391+
2392+
auto get_do_while_body() const
2393+
-> compound_statement_node*
2394+
{
2395+
return statements.get();
2396+
}
2397+
2398+
auto is_for() const
2399+
-> bool
2400+
{
2401+
return *identifier == "for";
2402+
}
2403+
2404+
auto get_for_range() const
2405+
-> expression_node*
2406+
{
2407+
return range.get();
2408+
}
2409+
2410+
auto get_for_parameter() const
2411+
-> parameter_declaration_node*
2412+
{
2413+
return parameter.get();
2414+
}
2415+
2416+
auto get_for_body() const
2417+
-> statement_node*
2418+
{
2419+
return body.get();
2420+
}
2421+
23562422
auto position() const
23572423
-> source_position
23582424
{
@@ -4420,21 +4486,21 @@ struct declaration_node
44204486
}
44214487

44224488
auto get_function_parameters()
4423-
-> std::vector<parameter_declaration_node const*>
4489+
-> std::vector<parameter_declaration_node*>
44244490
{
44254491
if (!is_function()) {
44264492
return {};
44274493
}
44284494

4429-
auto ret = std::vector<parameter_declaration_node const*>{};
4495+
auto ret = std::vector<parameter_declaration_node*>{};
44304496
for (auto& param : std::get<a_function>(type)->parameters->parameters) {
44314497
ret.push_back( param.get() );
44324498
}
44334499
return ret;
44344500
}
44354501

44364502
auto get_function_returns()
4437-
-> std::vector<parameter_declaration_node const*>
4503+
-> std::vector<parameter_declaration_node*>
44384504
{
44394505
if (!is_function()) {
44404506
return {};
@@ -4445,7 +4511,7 @@ struct declaration_node
44454511
return {};
44464512
}
44474513

4448-
auto ret = std::vector<parameter_declaration_node const*>{};
4514+
auto ret = std::vector<parameter_declaration_node*>{};
44494515
for (auto& param : (*returns)->parameters) {
44504516
ret.push_back( param.get() );
44514517
}

0 commit comments

Comments
 (0)