Skip to content

Commit

Permalink
Merge branch 'main' into Add-version-to-generated-file
Browse files Browse the repository at this point in the history
Signed-off-by: Herb Sutter <[email protected]>
  • Loading branch information
hsutter authored Dec 19, 2023
2 parents aec25ef + b81c80f commit 765757f
Show file tree
Hide file tree
Showing 311 changed files with 11,192 additions and 8,182 deletions.
48 changes: 48 additions & 0 deletions .github/workflows/build-cppfront.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Multi-platform Build of cppfront
on: push
jobs:
build-windows:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- uses: ilammy/msvc-dev-cmd@v1
- name: Compiler name & version
run: cl.exe
- name: Build
run: cl.exe source/cppfront.cpp -std:c++latest -MD -EHsc -experimental:module -W4
build-unix-like:
strategy:
fail-fast: false
matrix:
runs-on: [ubuntu-latest]
compiler: [g++-10, g++-11, g++-12, clang++-12, clang++-14]
cxx-std: ['c++20', 'c++2b']
exclude:
# GCC 10 doesn't have support for c++23
- compiler: g++-10
cxx-std: 'c++2b'
# Clang 12 and 14 do not compile on 'c++2b' due to llvm/llvm-project#58206
- compiler: clang++-12
cxx-std: 'c++2b'
- compiler: clang++-14
cxx-std: 'c++2b'
include:
- runs-on: macos-11
compiler: clang++
cxx-std: 'c++20'
- runs-on: macos-latest
compiler: clang++
cxx-std: 'c++20'
runs-on: ${{ matrix.runs-on }}
env:
CXX: ${{ matrix.compiler }}
CXXFLAGS: -std=${{ matrix.cxx-std }} -Wall -Wextra -Wold-style-cast -pthread
steps:
- uses: actions/checkout@v3
- name: Install compiler
if: startsWith(matrix.runs-on, 'ubuntu')
run: sudo apt-get install -y $CXX
- name: Compiler name & version
run: $CXX --version
- name: Build
run: $CXX source/cppfront.cpp $CXXFLAGS -o cppfront
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
See [License](LICENSE)

[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE_OF_CONDUCT.md)
[![Build (clang, gcc, vs)](https://github.com/hsutter/cppfront/actions/workflows/build-cppfront.yaml/badge.svg)](https://github.com/hsutter/cppfront/actions/workflows/build-cppfront.yaml)

Cppfront is an experimental compiler from a potential C++ 'syntax 2' (Cpp2) to today's 'syntax 1' (Cpp1), to learn some things, prove out some concepts, and share some ideas. This compiler is a work in progress and currently hilariously incomplete... basic functions work, classes will be next, then metaclasses and lightweight exceptions.

Expand Down
317 changes: 206 additions & 111 deletions include/cpp2util.h

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions regression-tests/mixed-bounds-safety-with-assert-2.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ main: () -> int = {

add_42_to_subrange: (inout rng:_, start:int, end:int)
= {
[[assert Bounds: 0 <= start]]
[[assert Bounds: end <= rng.ssize()]]
assert<Bounds>( 0 <= start );
assert<Bounds>( end <= rng.ssize() );

count := 0;
for rng
Expand Down
4 changes: 2 additions & 2 deletions regression-tests/mixed-bounds-safety-with-assert.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ main: () -> int = {
}

print_subrange: (rng:_, start:int, end:int) = {
[[assert Bounds: 0 <= start]]
[[assert Bounds: end <= rng.ssize()]]
assert<Bounds>( 0 <= start );
assert<Bounds>( end <= rng.ssize() );

count := 0;
for rng
Expand Down
6 changes: 3 additions & 3 deletions regression-tests/mixed-bugfix-for-literal-as-nttp.cpp2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <chrono>
using namespace std::chrono_literals;
main: () = {
[[assert: 10 as i32 == 10]]
[[assert: 10LL as i32 == 10]]
[[assert: 10s as std::chrono::seconds == 10s]]
assert( 10 as i32 == 10 );
assert( 10LL as i32 == 10 );
assert( 10s as std::chrono::seconds == 10s );
}
49 changes: 49 additions & 0 deletions regression-tests/mixed-bugfix-for-ufcs-non-local.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace ns {

template<bool> struct t { };
constexpr bool f(const t<true>&) { return true; }
constexpr t<true> o{};

} // namespace ns

ns: namespace = {

// Variables.

v0: <_: t<o.f()>> bool == false; // Fails on GCC ([GCC109781][]) and Clang 12 (a lambda expression cannot appear in this context)

v1: t<o.f()> == t<true>(); // Fails on Clang 12 (lambda in unevaluated context).

v2: bool == o.f();

// Functions.

g: <_: t<o.f()>> () = { } // Fails on GCC ([GCC109781][]) and Clang 12 (a lambda expression cannot appear in this context)

g: (_: t<o.f()>) = { } // Fails on Clang 12 (lambda in unevaluated context).

g: () pre(o.f()) = { }

h: () -> t<o.f()> = o; // Fails on Clang 12 (lambda in unevaluated context).

// Aliases.

a: <_: t<o.f()>> type == bool; // Fails on GCC ([GCC109781][]) and Clang 12 (a lambda expression cannot appear in this context)

b: <_: t<o.f()>> _ == false; // Fails on GCC ([GCC109781][]).

c: type == t<o.f()>; // Fails on Clang 12 (lambda in unevaluated context) and Clang 12 (a lambda expression cannot appear in this context)

d: _ == t<o.f()>(); // Fails on Clang 12 (lambda in unevaluated context).

u: @struct type = {
b: bool == o.f();
c: bool == :(x: std::type_identity_t<decltype(o.f())>) x;(true); // Fails on Clang 12 (lambda in unevaluated context).
g: (s, sz) pre(s.sz() != 0) = { }
}

} // namespace ns

main: () = { }

// [GCC109781]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109781
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ main: () -> int = {
vec: std::vector<int> = ();

insert_at: (where: int, val: int)
[[pre: 0 <= where && where <= vec.ssize()]]
[[post: vec.ssize() == vec.ssize()$ + 1]]
pre( 0 <= where && where <= vec.ssize() )
post( vec.ssize() == vec.ssize()$ + 1 )
= {
_ = vec.insert( vec.begin()+where, val );
}
6 changes: 6 additions & 0 deletions regression-tests/mixed-fixed-type-aliases.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ test: (x:_) = {
<< "\n";
}

mytype: @struct <T> type = {
myvalue: <U> bool == true;
}

main: (args) -> int = {
y: my::u16 = 42;
test(y);
Expand All @@ -21,4 +25,6 @@ main: (args) -> int = {

for args do (arg)
std::cout << std::filesystem::path(arg).filename() << "\n";

std::cout << "(mytype<int>::myvalue<int>)$\n";
}
15 changes: 6 additions & 9 deletions regression-tests/mixed-function-expression-and-std-for-each.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,21 @@
main: () -> int = {
vec: std::vector<std::string>
= ("hello", "2022");
view: std::span = vec;

// Passing a function expression
std::for_each(
view.begin(),
view.end(),
:(inout x) = x += "-ish";
std::ranges::for_each(
vec,
:(inout x) = x += "-ish"
);

// Initializing from a function expression
callback := :(inout x) = x += " maybe";
std::for_each(
view.begin(),
view.end(),
std::ranges::for_each(
vec,
callback
);

for view do (str) {
for vec do (str) {
std::cout << str << "\n";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
main: () -> int = {
vec: std::vector<std::string>
= ("hello", "2022");
view: std::span = vec;

y := "\n";
std::ranges::for_each
( view, :(x) = std::cout << x << y$ );
( vec, :(x) = std::cout << x << y$ );

callback := :(inout x) = x += "-ish";
std::ranges::for_each( view, callback );
std::ranges::for_each( vec, callback );

for view do (str)
for vec do (str)
std::cout << str << "\n";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
main: () -> int = {
vec: std::vector<std::string>
= ("hello", "2022");
view: std::span = vec;

std::ranges::for_each
( view, :(x) = std::cout << x << "\n" );
( vec, :(x) = std::cout << x << "\n" );

callback := :(inout x) = x += "-ish";
std::ranges::for_each( view, callback );
std::ranges::for_each( vec, callback );

for view do (str)
for vec do (str)
std::cout << str << "\n";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
main: () -> int = {
vec: std::vector<std::string>
= ("hello", "2023");
view: std::span = vec;

y: std::string = "\n";
std::ranges::for_each( view, :(x) =
std::cout << y&$*.c_str() << x << y&$*;
std::ranges::for_each( vec, :(x) =
std::cout << y&$*.c_str() << x << y&$*
);

callback := :(inout x) = x += "-ish";
std::ranges::for_each( view, callback );
std::ranges::for_each( vec, callback );

for view do (str)
for vec do (str)
std::cout << str << "\n";
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@
main: () -> int = {
vec: std::vector<std::string>
= ("hello", "2022");
view: std::span = vec;

y := "\n";
std::ranges::for_each
( view, :(x) = std::cout << y$ << x << y$ );
( vec, :(x) = std::cout << y$ << x << y$ );

callback := :(inout x) = x += "-ish";
std::ranges::for_each( view, callback );
std::ranges::for_each( vec, callback );

for view do (str)
for vec do (str)
std::cout << str << "\n";
}
55 changes: 55 additions & 0 deletions regression-tests/mixed-increment-decrement.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

iterator: @value <T> type = {
public x: T = 0;
operator++: (inout this) -> forward iterator<T> requires true = { x++; return this; }
}

iterator2: @value <T> type = {
public x: T = 0;
}
operator++: <T> (inout it: iterator2<T>) -> forward iterator2<T> requires true = { it.x++; return it; }

int main() {
{
iterator<int> i;
std::cout << (++i).x;
std::cout << (++i).x;
std::cout << i++.x;
std::cout << i++.x;
std::cout << i.x << "\n";
}

{
iterator2<int> i;
std::cout << (++i).x;
std::cout << (++i).x;
std::cout << i++.x;
std::cout << i++.x;
std::cout << i.x << "\n";
}
}



number: @struct type = {
operator<=>: (this, that) -> _;
operator-: (this, _) -> int = 0;
}

number_line: @struct <Op: i8, Id: i32> type = {
this: number;
}

number_line_pre_increment: <Id: i32> type == number_line<0b1110, Id>;
number_line_pre_decrement: <Id: i32> type == number_line<0b1101, Id>;
number_line_post_increment: <Id: i32> type == number_line<0b1011, Id>;
number_line_post_decrement: <Id: i32> type == number_line<0b0111, Id>;

operator++: <Op: i8, Id: i32> (inout x: number_line<Op, Id>) -> forward number_line<Op, Id> requires bool(Op & 1) = x;
operator--: <Op: i8, Id: i32> (inout x: number_line<Op, Id>) -> forward number_line<Op, Id> requires bool(Op & 2) = x;

operator++: (inout x: number_line_pre_increment<0>) -> forward number_line_pre_increment<0> = x;
operator--: (inout x: number_line_pre_decrement<0>) -> forward number_line_pre_decrement<0> = x;

operator++: (inout x: number_line_pre_increment<1>) -> forward const number_line_pre_increment<1> = x;
operator--: (inout x: number_line_pre_decrement<1>) -> forward const number_line_pre_decrement<1> = x;
4 changes: 2 additions & 2 deletions regression-tests/mixed-initialization-safety-1-error.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ fill: (
out x: std::string,
in value: std::string,
in count: int
)
[[pre: value.size() >= count, "fill: value must contain at least count elements"]]
)
pre( value.size() >= count, "fill: value must contain at least count elements" )
= {
x = value.substr(0, count);
}
Expand Down
4 changes: 2 additions & 2 deletions regression-tests/mixed-initialization-safety-2-error.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ fill: (
out x: std::string,
in value: std::string,
in count: int
)
[[pre: value.size() >= count, "fill: value must contain at least count elements"]]
)
pre( value.size() >= count, "fill: value must contain at least count elements" )
= {
x = value.substr(0, count);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fill: (
value: std::string,
count: int
)
[[pre: value.ssize() >= count, "fill: value must contain at least count elements"]]
pre( value.ssize() >= count, "fill: value must contain at least count elements" )
= {
x = value.substr(0, count);
}
Expand Down
2 changes: 1 addition & 1 deletion regression-tests/mixed-initialization-safety-3.cpp2
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fill: (
value: std::string,
count: int
)
[[pre: value.ssize() >= count, "fill: value must contain at least count elements"]]
pre( value.ssize() >= count, "fill: value must contain at least count elements" )
= {
x = value.substr(0, count);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ main: () -> int = {
}

print_and_decorate( p* );
print_and_decorate( x );
}

print_and_decorate: (thing:_) =
print_and_decorate: (thing) =
std::cout << ">> " << thing << "\n";

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ main: () -> int = {
}

print_and_decorate( p* );
print_and_decorate( x );
}

print_and_decorate: (thing:_) =
print_and_decorate: (thing) =
std::cout << ">> " << thing << "\n";

Loading

0 comments on commit 765757f

Please sign in to comment.