Skip to content

Commit 2300cc8

Browse files
committed
Added most intra-function reflection (statements, expressions, if statements)
Added tests Added sample code that traverses function bodies including if branches That's most of it... the main thing to reflect next is loops, and then a few smaller things like using statements and inspect statements
1 parent 3c9c59a commit 2300cc8

18 files changed

+7530
-4521
lines changed

include/cpp2regex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ template<typename CharT, typename matcher> class regular_expression;
8282

8383
#line 1 "cpp2regex.h2"
8484

85-
// Copyright 2022-2024 Herb Sutter
85+
// Copyright 2022-2025 Herb Sutter
8686
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8787
//
8888
// Part of the Cppfront Project, under the Apache License v2.0 with LLVM Exceptions.

include/cpp2regex.h2

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

2-
// Copyright 2022-2024 Herb Sutter
2+
// Copyright 2022-2025 Herb Sutter
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44
//
55
// Part of the Cppfront Project, under the Apache License v2.0 with LLVM Exceptions.

include/cpp2util.h

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

2-
// Copyright 2022-2024 Herb Sutter
2+
// Copyright 2022-2025 Herb Sutter
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44
//
55
// Part of the Cppfront Project, under the Apache License v2.0 with LLVM Exceptions.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
test: @sample_traverser type =
3+
{
4+
one_liner: (a: double, b: double, c: double) = (a + c) * b;
5+
6+
return_list: ()
7+
-> (r: double, s: float, t: std::string)
8+
= {
9+
r = 42.0;
10+
s = 2.71828f;
11+
t = "e times pi";
12+
}
13+
14+
branches: (a: double, b: double, c: double)
15+
-> (r: double = 3.14159)
16+
= {
17+
if true {
18+
r = r + a;
19+
}
20+
21+
if a * b > c {
22+
r += sin(b);
23+
}
24+
else {
25+
r = c;
26+
}
27+
}
28+
29+
binary_ops: (inout a: double, b: double, c: double)
30+
= {
31+
a -= b * c + (1 << 2);
32+
test: bool = a <= b < c && true || false;
33+
x := 1 & 2;
34+
y := 3 ^ 4;
35+
z := 5 | 6;
36+
}
37+
38+
prefix: () -> int
39+
= {
40+
a := -1;
41+
b := +2;
42+
if !true {
43+
return 0;
44+
}
45+
return a+b;
46+
}
47+
48+
postfix: (inout a: double)
49+
= {
50+
ptr := a&;
51+
ptr*++--;
52+
}
53+
54+
qualified_ids: () -> _
55+
= {
56+
v : std::vector<int> = (1, 2, 3);
57+
return v.ssize();
58+
}
59+
}
60+
61+
main: () = { }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pure2-function-body-reflection.cpp
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
2+
#define CPP2_IMPORT_STD Yes
3+
4+
//=== Cpp2 type declarations ====================================================
5+
6+
7+
#include "cpp2util.h"
8+
9+
#line 1 "pure2-function-body-reflection.cpp2"
10+
11+
#line 2 "pure2-function-body-reflection.cpp2"
12+
class test;
13+
14+
15+
//=== Cpp2 type definitions and function declarations ===========================
16+
17+
#line 1 "pure2-function-body-reflection.cpp2"
18+
19+
#line 2 "pure2-function-body-reflection.cpp2"
20+
class test
21+
{
22+
public: [[nodiscard]] static auto one_liner(cpp2::impl::in<double> a, cpp2::impl::in<double> b, cpp2::impl::in<double> c) -> decltype(auto);
23+
struct return_list_ret { double r; float s; std::string t; };
24+
25+
26+
27+
#line 6 "pure2-function-body-reflection.cpp2"
28+
public: [[nodiscard]] static auto return_list() -> return_list_ret;
29+
using branches_ret = double;
30+
31+
32+
#line 14 "pure2-function-body-reflection.cpp2"
33+
public: [[nodiscard]] static auto branches(cpp2::impl::in<double> a, cpp2::impl::in<double> b, cpp2::impl::in<double> c) -> branches_ret;
34+
35+
#line 29 "pure2-function-body-reflection.cpp2"
36+
public: static auto binary_ops(double& a, cpp2::impl::in<double> b, cpp2::impl::in<double> c) -> void;
37+
38+
#line 38 "pure2-function-body-reflection.cpp2"
39+
public: [[nodiscard]] static auto prefix() -> int;
40+
41+
#line 48 "pure2-function-body-reflection.cpp2"
42+
public: static auto postfix(double& a) -> void;
43+
44+
#line 54 "pure2-function-body-reflection.cpp2"
45+
public: [[nodiscard]] static auto qualified_ids() -> auto;
46+
public: test() = default;
47+
public: test(test const&) = delete; /* No 'that' constructor, suppress copy */
48+
public: auto operator=(test const&) -> void = delete;
49+
50+
51+
#line 59 "pure2-function-body-reflection.cpp2"
52+
};
53+
54+
auto main() -> int;
55+
56+
//=== Cpp2 function definitions =================================================
57+
58+
#line 1 "pure2-function-body-reflection.cpp2"
59+
60+
#line 4 "pure2-function-body-reflection.cpp2"
61+
[[nodiscard]] auto test::one_liner(cpp2::impl::in<double> a, cpp2::impl::in<double> b, cpp2::impl::in<double> c) -> decltype(auto) { return (a + c) * b; }
62+
63+
#line 6 "pure2-function-body-reflection.cpp2"
64+
[[nodiscard]] auto test::return_list() -> return_list_ret
65+
66+
{
67+
cpp2::impl::deferred_init<double> r;
68+
cpp2::impl::deferred_init<float> s;
69+
cpp2::impl::deferred_init<std::string> t;
70+
#line 9 "pure2-function-body-reflection.cpp2"
71+
r.construct(42.0);
72+
s.construct(2.71828f);
73+
t.construct("e times pi");
74+
return { std::move(r.value()), std::move(s.value()), std::move(t.value()) }; }
75+
76+
#line 14 "pure2-function-body-reflection.cpp2"
77+
[[nodiscard]] auto test::branches(cpp2::impl::in<double> a, cpp2::impl::in<double> b, cpp2::impl::in<double> c) -> branches_ret
78+
79+
{
80+
double r {3.14159};
81+
#line 17 "pure2-function-body-reflection.cpp2"
82+
if (true) {
83+
r = r + a;
84+
}
85+
86+
if (cpp2::impl::cmp_greater(a * b,c)) {
87+
r += sin(b);
88+
}
89+
else {
90+
r = c;
91+
}return r;
92+
}
93+
94+
#line 29 "pure2-function-body-reflection.cpp2"
95+
auto test::binary_ops(double& a, cpp2::impl::in<double> b, cpp2::impl::in<double> c) -> void
96+
{
97+
a -= b * c + (1 << 2);
98+
bool test {[_0 = a, _1 = b, _2 = c]{ return cpp2::impl::cmp_less_eq(_0,_1) && cpp2::impl::cmp_less(_1,_2); }() && true || false};
99+
auto x {1 & 2};
100+
auto y {3 ^ 4};
101+
auto z {5 | 6};
102+
}
103+
104+
#line 38 "pure2-function-body-reflection.cpp2"
105+
[[nodiscard]] auto test::prefix() -> int
106+
{
107+
auto a {-1};
108+
auto b {+2};
109+
if (!(true)) {
110+
return 0;
111+
}
112+
return cpp2::move(a) + cpp2::move(b);
113+
}
114+
115+
#line 48 "pure2-function-body-reflection.cpp2"
116+
auto test::postfix(double& a) -> void
117+
{
118+
auto ptr {&a};
119+
--++*cpp2::impl::assert_not_null(cpp2::move(ptr));
120+
}
121+
122+
#line 54 "pure2-function-body-reflection.cpp2"
123+
[[nodiscard]] auto test::qualified_ids() -> auto
124+
{
125+
std::vector<int> v {1, 2, 3};
126+
return CPP2_UFCS(ssize)(cpp2::move(v));
127+
}
128+
129+
#line 61 "pure2-function-body-reflection.cpp2"
130+
auto main() -> int{}
131+

0 commit comments

Comments
 (0)