Skip to content

Commit 258190e

Browse files
committed
Initial basic support for namespaces and user-defined types
Added initial basic support. Includes: - access-specifiers: public, protected, private - defaults: in type scope (aka members), types and functions are public by default, objects (data members) are private by default - this-specifiers: implicit, virtual, override, final (but can't use them much since we don't have inheritance syntax yet, see next list) - explicit `this` parameters (where the this-specifiers go) NOT included yet: - special member functions (`operator=`) - inheritance / base classes - metaclass functions to apply checks, defaults, and generated code (that'll be last...) This checkin also changes the null/subscript/comparison checking opt-out command-line option from `/xxx-checks-` (which was only because the initial default was to opt into the checks) to `/no-xxx-checks`
1 parent 91e1d79 commit 258190e

16 files changed

+589
-173
lines changed

include/cpp2util.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,10 +491,14 @@ template<typename T>
491491
//
492492
//-----------------------------------------------------------------------
493493
//
494+
template<typename T>
495+
constexpr bool prefer_pass_by_value =
496+
sizeof(T) < 2*sizeof(void*) && std::is_trivially_copy_constructible_v<T>;
497+
494498
template<typename T>
495499
using in =
496500
std::conditional_t <
497-
sizeof(T) < 2*sizeof(void*) && std::is_trivially_copy_constructible_v<T>,
501+
prefer_pass_by_value<T>,
498502
T const,
499503
T const&
500504
>;

regression-tests/pure2-deducing-pointers-error.cpp2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ fun2: (inout i:int) -> (result : *int) = {
66
result = i&;
77
}
88

9-
main: (argc : int, argv : **char) -> int = {
9+
main: () -> int = {
1010
a: int = 2;
1111
pa: *int = a&;
1212
ppa: **int = pa&;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
N: namespace = {
3+
4+
myclass : type = {
5+
6+
f: (virtual this, x: int) = {
7+
std::cout << "N::myclass::f with (x)$\n";
8+
}
9+
10+
data: int;
11+
12+
nested : type = {
13+
g: () = std::cout << "N::myclass::nested::g\n";
14+
}
15+
16+
f1: <T , U > (t:T, u:U) -> _ = t+u;
17+
f2: <T:type, U:type> (t:T, u:U) -> _ = t+u;
18+
f3: <T:_ , U:_ > () -> _ = T+U;
19+
f4: <T:i8 , U:i16 > () -> _ = T+U;
20+
21+
}
22+
23+
}
24+
25+
main: () = {
26+
x: N::myclass = ();
27+
x.f(53);
28+
N::myclass::nested::g();
29+
std::cout << "f1: (x.f1(1,1))$\n";
30+
std::cout << "f2: (x.f2(2,2))$\n";
31+
std::cout << "f3: (x.f3<3,3>())$\n";
32+
std::cout << "f4: (x.f4<4,4>())$\n";
33+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
N::myclass::f with 53
2+
N::myclass::nested::g
3+
f1: 2
4+
f2: 4
5+
f3: 6
6+
f4: 8

regression-tests/test-results/clang-12/pure2-types-basics.cpp.output

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
N::myclass::f with 53
2+
N::myclass::nested::g
3+
f1: 2
4+
f2: 4
5+
f3: 6
6+
f4: 8

regression-tests/test-results/gcc-10/pure2-types-basics.cpp.output

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
N::myclass::f with 53
2+
N::myclass::nested::g
3+
f1: 2
4+
f2: 4
5+
f3: 6
6+
f4: 8
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pure2-types-basics.cpp
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
2+
#define CPP2_USE_MODULES Yes
3+
4+
#include "cpp2util.h"
5+
6+
7+
#line 2 "pure2-types-basics.cpp2"
8+
namespace N {
9+
class myclass;
10+
};
11+
#line 25 "pure2-types-basics.cpp2"
12+
auto main() -> int;
13+
14+
//=== Cpp2 definitions ==========================================================
15+
16+
#line 1 "pure2-types-basics.cpp2"
17+
18+
namespace N {
19+
20+
class myclass {
21+
22+
public: virtual auto f(cpp2::in<int> x) const -> void{
23+
std::cout << "N::myclass::f with " + cpp2::to_string(x) + "\n";
24+
}
25+
26+
private: int data;
27+
28+
public: class nested {
29+
public: static auto g() -> void { std::cout << "N::myclass::nested::g\n"; }
30+
};
31+
32+
public: template<typename T, typename U> [[nodiscard]] static auto f1(T const& t, U const& u) -> auto { return t + u; }
33+
public: template<typename T, typename U> [[nodiscard]] static auto f2(T const& t, U const& u) -> auto { return t + u; }
34+
public: template<auto T, auto U> [[nodiscard]] static auto f3() -> auto { return T + U; }
35+
public: template<cpp2::i8 T, cpp2::i16 U> [[nodiscard]] static auto f4() -> auto { return T + U; }
36+
37+
};
38+
39+
};
40+
41+
auto main() -> int{
42+
N::myclass x {};
43+
CPP2_UFCS(f, x, 53);
44+
N::myclass::nested::g();
45+
std::cout << "f1: " + cpp2::to_string(CPP2_UFCS(f1, x, 1, 1)) + "\n";
46+
std::cout << "f2: " + cpp2::to_string(CPP2_UFCS(f2, x, 2, 2)) + "\n";
47+
std::cout << "f3: " + cpp2::to_string(CPP2_UFCS_TEMPLATE_0(f3, (<3,3>), x)) + "\n";
48+
std::cout << "f4: " + cpp2::to_string(CPP2_UFCS_TEMPLATE_0(f4, (<4,4>), std::move(x))) + "\n";
49+
}
50+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pure2-types-basics.cpp2... ok (all Cpp2, passes safety checks)
2+

source/common.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,8 @@ class cmdline_processor
352352
while (
353353
i < std::ssize(flag1->name) &&
354354
i < std::ssize(flag2->name) &&
355+
flag1->name[i] != ' ' &&
356+
flag2->name[i] != ' ' &&
355357
flag1->name[i] == flag2->name[i]
356358
)
357359
{
@@ -449,9 +451,11 @@ class cmdline_processor
449451
print(" -");
450452
auto n = flag.name.substr(0, flag.unique_prefix);
451453
if (flag.unique_prefix < std::ssize(flag.name)) {
454+
auto name_length = __as<int>(std::min(flag.name.find(' '), flag.name.size()));
452455
n += "[";
453-
n += flag.name.substr(flag.unique_prefix);
456+
n += flag.name.substr(flag.unique_prefix, name_length - flag.unique_prefix);
454457
n += "]";
458+
n += flag.name.substr(name_length);
455459
}
456460
if (flag.opt_out) {
457461
n += "[-]";

0 commit comments

Comments
 (0)