Skip to content

Commit

Permalink
Initial basic support for namespaces and user-defined types
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
hsutter committed Mar 5, 2023
1 parent 91e1d79 commit 258190e
Show file tree
Hide file tree
Showing 16 changed files with 589 additions and 173 deletions.
6 changes: 5 additions & 1 deletion include/cpp2util.h
Original file line number Diff line number Diff line change
Expand Up @@ -491,10 +491,14 @@ template<typename T>
//
//-----------------------------------------------------------------------
//
template<typename T>
constexpr bool prefer_pass_by_value =
sizeof(T) < 2*sizeof(void*) && std::is_trivially_copy_constructible_v<T>;

template<typename T>
using in =
std::conditional_t <
sizeof(T) < 2*sizeof(void*) && std::is_trivially_copy_constructible_v<T>,
prefer_pass_by_value<T>,
T const,
T const&
>;
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/pure2-deducing-pointers-error.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fun2: (inout i:int) -> (result : *int) = {
result = i&;
}

main: (argc : int, argv : **char) -> int = {
main: () -> int = {
a: int = 2;
pa: *int = a&;
ppa: **int = pa&;
Expand Down
33 changes: 33 additions & 0 deletions regression-tests/pure2-types-basics.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

N: namespace = {

myclass : type = {

f: (virtual this, x: int) = {
std::cout << "N::myclass::f with (x)$\n";
}

data: int;

nested : type = {
g: () = std::cout << "N::myclass::nested::g\n";
}

f1: <T , U > (t:T, u:U) -> _ = t+u;
f2: <T:type, U:type> (t:T, u:U) -> _ = t+u;
f3: <T:_ , U:_ > () -> _ = T+U;
f4: <T:i8 , U:i16 > () -> _ = T+U;

}

}

main: () = {
x: N::myclass = ();
x.f(53);
N::myclass::nested::g();
std::cout << "f1: (x.f1(1,1))$\n";
std::cout << "f2: (x.f2(2,2))$\n";
std::cout << "f3: (x.f3<3,3>())$\n";
std::cout << "f4: (x.f4<4,4>())$\n";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
N::myclass::f with 53
N::myclass::nested::g
f1: 2
f2: 4
f3: 6
f4: 8
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
N::myclass::f with 53
N::myclass::nested::g
f1: 2
f2: 4
f3: 6
f4: 8
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
N::myclass::f with 53
N::myclass::nested::g
f1: 2
f2: 4
f3: 6
f4: 8
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pure2-types-basics.cpp
50 changes: 50 additions & 0 deletions regression-tests/test-results/pure2-types-basics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

#define CPP2_USE_MODULES Yes

#include "cpp2util.h"


#line 2 "pure2-types-basics.cpp2"
namespace N {
class myclass;
};
#line 25 "pure2-types-basics.cpp2"
auto main() -> int;

//=== Cpp2 definitions ==========================================================

#line 1 "pure2-types-basics.cpp2"

namespace N {

class myclass {

public: virtual auto f(cpp2::in<int> x) const -> void{
std::cout << "N::myclass::f with " + cpp2::to_string(x) + "\n";
}

private: int data;

public: class nested {
public: static auto g() -> void { std::cout << "N::myclass::nested::g\n"; }
};

public: template<typename T, typename U> [[nodiscard]] static auto f1(T const& t, U const& u) -> auto { return t + u; }
public: template<typename T, typename U> [[nodiscard]] static auto f2(T const& t, U const& u) -> auto { return t + u; }
public: template<auto T, auto U> [[nodiscard]] static auto f3() -> auto { return T + U; }
public: template<cpp2::i8 T, cpp2::i16 U> [[nodiscard]] static auto f4() -> auto { return T + U; }

};

};

auto main() -> int{
N::myclass x {};
CPP2_UFCS(f, x, 53);
N::myclass::nested::g();
std::cout << "f1: " + cpp2::to_string(CPP2_UFCS(f1, x, 1, 1)) + "\n";
std::cout << "f2: " + cpp2::to_string(CPP2_UFCS(f2, x, 2, 2)) + "\n";
std::cout << "f3: " + cpp2::to_string(CPP2_UFCS_TEMPLATE_0(f3, (<3,3>), x)) + "\n";
std::cout << "f4: " + cpp2::to_string(CPP2_UFCS_TEMPLATE_0(f4, (<4,4>), std::move(x))) + "\n";
}

2 changes: 2 additions & 0 deletions regression-tests/test-results/pure2-types-basics.cpp2.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pure2-types-basics.cpp2... ok (all Cpp2, passes safety checks)

6 changes: 5 additions & 1 deletion source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ class cmdline_processor
while (
i < std::ssize(flag1->name) &&
i < std::ssize(flag2->name) &&
flag1->name[i] != ' ' &&
flag2->name[i] != ' ' &&
flag1->name[i] == flag2->name[i]
)
{
Expand Down Expand Up @@ -449,9 +451,11 @@ class cmdline_processor
print(" -");
auto n = flag.name.substr(0, flag.unique_prefix);
if (flag.unique_prefix < std::ssize(flag.name)) {
auto name_length = __as<int>(std::min(flag.name.find(' '), flag.name.size()));
n += "[";
n += flag.name.substr(flag.unique_prefix);
n += flag.name.substr(flag.unique_prefix, name_length - flag.unique_prefix);
n += "]";
n += flag.name.substr(name_length);
}
if (flag.opt_out) {
n += "[-]";
Expand Down
Loading

6 comments on commit 258190e

@JohelEGP
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great. Now the more adventurous of us can start using Cpp2 in an actual code base and start seeing benefits.

Being limited to the global namespace, one could only use it in an actual code base in tests. Considering that, I'm surprised how much some tried to push cppfront to the limits. Specially @filipsajdak.

I just wish we at least had a cheat sheet of the Cpp2 syntax.

@filipsajdak
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @JohelEGP! I am working on providing my cheat sheet - I have implemented some code without classes and will rewrite it to the cpp2 now. That should give me even more understanding about new use cases and new idioms.

I hope I will find some time to push that work this week.

@filipsajdak
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently I have a collection of test cases like the following #266 (comment) - that help me learn how cppfront work.

@JohelEGP
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AbhinavK00
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also started to write some documentation sometime ago here but stopped at just functions, will continue it again sometime soon.
(there's a mistake where i describe forward parameter passing, ignore it pls)

@JohelEGP
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • start finding a few private alpha testers to work with, to start writing a bit of code in Cpp2 to alpha-test cppfront and also to alpha-test my (so far unpublished) draft documentation.

-- https://herbsutter.com/2023/04/30/cppfront-spring-update/

Please sign in to comment.