Skip to content

Commit daf9eec

Browse files
committed
Catch up feature documentation and C++26 new headers
Add documentation for: - tersest syntax (e.g., `:(a,b) a+b;`) - `unevaluated` contract group - for `operator++` and `operator--`, that writing the single operator in Cpp2 generates both forms when lowering to Cpp1 - `-cwd` and `-quiet` command line options Remove documentation for: - `-add-source-info` switch which was removed Add support for new C++26 headers approved in St Louis: - `inplace_vector`
1 parent 4a24618 commit daf9eec

File tree

7 files changed

+60
-43
lines changed

7 files changed

+60
-43
lines changed

docs/cpp2/common.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ Postfix notation lets the code read fluidly left-to-right, in the same order in
220220

221221
> Note: The `...` pack expansion syntax is also supported. The above `...` and `..=` are the Cpp2 range operators, which overlap in syntax.
222222
223-
> Note: Because `++` and `--` always have in-place update semantics, we never need to remember "use prefix `++`/`--` unless you need a copy of the old value." If you do need a copy of the old value, just take the copy before calling `++`/`--`.
223+
> Note: Because `++` and `--` always have in-place update semantics, we never need to remember "use prefix `++`/`--` unless you need a copy of the old value." If you do need a copy of the old value, just take the copy before calling `++`/`--`. When you write a type that overloads `operator++` or `operator--`, cppfront generates both Cpp1 overloads (in-place-update and copy-old-value) for that function to support natural use of the type from Cpp1 code.
224224
225225

226226
### <a id="binary-operators"></a> Binary operators

docs/cpp2/contracts.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ Notes:
1515

1616
- Optionally, `condition` may be followed by `, "message"`, a message to include if a violation occurs. For example, `pre(condition, "message")`.
1717

18-
- Optionally, a `<group, pred1, pred2>` can be written inside `<` `>` angle brackets immediately before the `(`, to designate that this test is part of the [contract group](#groups) named `group` and (also optionally) [contract predicates](#predicates) `pred1` and `pred2`. If a violation occurs, `Group.report_violation()` will be called. For example, `pre<group>(condition)`. If no contract group is specified, the contract defaults to being part of the `cpp2_default` group.
18+
- Optionally, a `<group, pred1, pred2>` can be written inside `<` `>` angle brackets immediately before the `(`, to designate that this test is part of the [contract group](#groups) named `group` and (also optionally) [contract predicates](#predicates) `pred1` and `pred2`. If a violation occurs, `Group.report_violation()` will be called. For example, `pre<group>(condition)`. If no contract group is specified, the contract defaults to being part of the `default` group (spelled `cpp2_default` when used from Cpp1 code).
1919

2020
The order of evaluation is:
2121

22-
- First, predicates are evaluated in order. If any predicte evaluates to `#!cpp false`, stop.
22+
- First, if the contract group is `unevaluated` then the contract is ignored; `condition` is never evaluated. This special group is designates conditions intended for use by static analyzers only, and the only requirement is that the condition be grammatically valid.
23+
24+
- Next, predicates are evaluated in order. If any predicate evaluates to `#!cpp false`, stop.
2325

2426
- Next, `group.is_active()` is evaluated. If that evaluates to `#!cpp false`, stop.
2527

docs/cpp2/functions.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,13 @@ wrap_f: (
7878

7979
A function can return either of the following. The default is `#!cpp -> void`.
8080

81-
(1) **`#!cpp -> X`** to return a single unnamed value of type `X`, which can be `#!cpp void` to signify the function has no return value. If `X` is not `#!cpp void`, the function body must have a `#!cpp return /*value*/;` statement that returns a value of type `X` on every path that exits the function. For example:
81+
(1) **`#!cpp -> X`** to return a single unnamed value of type `X`, which can be `#!cpp void` to signify the function has no return value. If `X` is not `#!cpp void`, the function body must have a `#!cpp return /*value*/;` statement that returns a value of type `X` on every path that exits the function.
8282

83-
``` cpp title="Functions with an unnamed return value" hl_lines="2 4 7 9 12 14"
83+
To deduce the return type, write `-> _`. A function whose body returns a single expression `expr` can deduce the return type and omit writing `-> _ = { return /*expr*/ ; }`.
84+
85+
For example:
86+
87+
``` cpp title="Functions with an unnamed return value" hl_lines="2 4 7 9 12 14 15 18 20 22"
8488
// A function returning no value (void)
8589
increment_in_place: (inout a: i32) -> void = { a++; }
8690
// Or, using syntactic defaults, the following has identical meaning:
@@ -93,8 +97,16 @@ add_one: (a: i32) -> i32 = a+1;
9397

9498
// A generic function returning a single value of deduced type
9599
add: <T: type, U: type> (a:T, b:U) -> decltype(a+b) = { return a+b; }
96-
// Or, using syntactic defaults, the following has identical meaning:
100+
// Or, using syntactic defaults, the following have identical meaning:
97101
add: (a, b) -> _ = a+b;
102+
add: (a, b) a+b;
103+
104+
// A generic function expression returning a single value of deduced type
105+
vec.std::ranges::sort( :(x:_, y:_) -> _ = { return y<x; } );
106+
// Or, using syntactic defaults, the following has identical meaning:
107+
vec.std::ranges::sort( :(x,y) y<x );
108+
// Both are identical to this, which uses the most verbose possible syntax:
109+
vec.std::ranges::sort( :<T:type, U:type> (x:T, y:U) -> _ = { return y<x; } );
98110
```
99111
100112
(2) **`#!cpp -> ( /* parameter list */ )`** to return a list of named return parameters using the same [parameters](#parameters) syntax, but where the only passing styles are `out` (the default, which moves where possible) or `forward`. The function body must [initialize](objects.md#init) the value of each return-parameter `ret` in its body the same way as any other local variable. An explicit return statement is written just `#!cpp return;` and returns the named values; the function has an implicit `#!cpp return;` at the end. If only a single return parameter is in the list, it is emitted in the lowered Cpp1 code the same way as (1) above, so its name is only available inside the function body.

docs/cppfront/options.md

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ For convenience, you can shorten the name to any unique prefix not shared with a
1818
- `-import-std` and `-include-std` can be shortened to `-im` and `-in` respectively, but not `-i` which would be ambiguous with each other.
1919

2020

21-
# Basic command line options
21+
## Basic command line options
2222

23-
## `-help`, `-h`, `-?`
23+
### `-help`, `-h`, `-?`
2424

2525
Prints an abbreviated version of this documentation page.
2626

27-
## `-import-std`, `-im`
27+
### `-import-std`, `-im`
2828

2929
Makes the entire C++ standard library (namespace `std::`) available via a module `import std.compat;` (which implies `import std;`).
3030

@@ -34,89 +34,89 @@ This option is implicitly set if `-pure-cpp2` is selected.
3434

3535
This option is ignored if `-include-std` is selected. If your Cpp1 compiler does not yet support standard library modules `std` and `std.compat`, this option automatically uses `-include-std` instead as a fallback.
3636

37-
## `-include-std`, `-in`
37+
### `-include-std`, `-in`
3838

3939
Makes the entire C++ standard library (namespace `std::`) available via an '#include" of every standard header.
4040

4141
This option should always work with all standard headers, including draft-standard C++26 headers that are not yet in a published standard, because it tracks new headers as they are added and uses feature tests to not include headers that are not yet available on your Cpp1 implementation.
4242

43-
## `-pure-cpp2`, `-p`
43+
### `-pure-cpp2`, `-p`
4444

4545
Allow Cpp2 syntax only.
4646

4747
This option also sets `-import-std`.
4848

49-
## `-version`, `-vers`
49+
### `-version`, `-vers`
5050

5151
Print version, build, copyright, and license information.
5252

5353

54-
# Additional dynamic safety checks and contract information
54+
## Additional dynamic safety checks and contract information
5555

56-
## `-add-source-info`, `-a`
57-
58-
Enable `source_location` information for contract checks. If this is supported by your Cpp1 compiler, the default contract failure messages will include exact file/line/function information. For example, if the default `Bounds` violation handler would print this without `-a`:
59-
60-
Bounds safety violation: out of bounds access attempt detected - attempted access at index 2, [min,max] range is [0,1]
61-
62-
then it would print something like this with `-a` (the exact text will vary with the Cpp1 standard library vendor's `source_location` implementation):
63-
64-
demo.cpp2(4) int __cdecl main(void): Bounds safety violation: out of bounds access attempt detected - attempted access at index 2, [min,max] range is [0,1]
65-
66-
## `-no-comparison-checks`, `-no-c`
56+
### `-no-comparison-checks`, `-no-c`
6757

6858
Disable mixed-sign comparison safety checks. If not disabled, mixed-sign comparisons are diagnosed by default.
6959

70-
## `-no-null-checks`, `-no-n`
60+
### `-no-null-checks`, `-no-n`
7161

7262
Disable null safety checks. If not disabled, null dereference checks are performed by default.
7363

74-
## `-no-subscript-checks`, `-no-s`
64+
### `-no-subscript-checks`, `-no-s`
7565

7666
Disable subscript bounds safety checks. If not disabled, subscript bounds safety checks are performed by default.
7767

7868

79-
# Support for constrained target environments
69+
## Support for constrained target environments
8070

81-
## `-fno-exceptions`, `-fno-e`
71+
### `-fno-exceptions`, `-fno-e`
8272

8373
Disable C++ exception handling. This should be used only if you must run in an environment that bans C++ exception handling, and so you are already using a similar command line option for your Cpp1 compiler.
8474

8575
If this option is selected, a failed `as` for `std::variant` will assert.
8676

87-
## `-fno-rtti`, `-fno-r`
77+
### `-fno-rtti`, `-fno-r`
8878

8979
Disable C++ run-time type information (RTTI). This should be used only if you must run in an environment that bans C++ RTTI, and so you are already using a similar command line option for your Cpp1 compiler.
9080

9181
If this option is selected, trying to using `as` for `*` (raw pointers) or `std::any` will assert.
9282

9383

94-
# Other options
84+
## Cpp1 file content options
9585

96-
## `-clean-cpp1`, `-c`
86+
### `-clean-cpp1`, `-cl`
9787

9888
Emit clean `.cpp` files without `#line` directives and other extra information that cppfront normally emits in the `.cpp` to light up C++ tools (e.g., to let IDEs integrate cppfront error message output, debuggers step to the right lines in Cpp2 source code, and so forth). In normal use, you won't need `-c`.
9989

100-
## `-debug`, `-d`
90+
### `-emit-cppfront-info`, `-e`
10191

102-
Emit compiler debug output. This is only useful when debugging cppfront itself.
92+
Emit cppfront version and build in the `.cpp` file.
10393

104-
## `-emit-cppfront-info`, `-e`
94+
### `-line-paths`, `-l`
10595

106-
Emit cppfront version and build in the `.cpp` file.
96+
Emit absolute paths in `#line` directives.
10797

108-
## `-format-colon-errors`, `-fo`
98+
## Cppfront output options
10999

110-
Emit cppfront diagnostics using `:line:col:` format for line and column numbers, if that is the format better recognized by your IDE, so that it will pick up cppfront messages and integrate them in its normal error message output location. If not set, by default cppfront diagnostics use `(line,col)` format.
100+
### `-cwd` _path_, `-cw` _path_
111101

112-
## `-line-paths`, `-l`
102+
Changes the current working directory to 'path'. Can be useful in build scripts to control where generated Cpp1 files are places; see also `-output`.
113103

114-
Emit absolute paths in `#line` directives.
104+
### `-debug`, `-d`
115105

116-
## `-output` _filename_, `-o` _filename_
106+
Emit compiler debug output. This is only useful when debugging cppfront itself.
107+
108+
### `-format-colon-errors`, `-fo`
109+
110+
Emit cppfront diagnostics using `:line:col:` format for line and column numbers, if that is the format better recognized by your IDE, so that it will pick up cppfront messages and integrate them in its normal error message output location. If not set, by default cppfront diagnostics use `(line,col)` format.
111+
112+
### `-output` _filename_, `-o` _filename_
117113

118114
Output to 'filename' (can be 'stdout'). If not set, the default output filename for is the same as the input filename without the `2` (e.g., compiling `hello.cpp2` by default writes its output to `hello.cpp`, and `header.h2` to `header.h`).
119115

120-
## `-verbose`, `-verb`
116+
### `-quiet`, `-q`
117+
118+
Print no console output unless there are errors to report.
119+
120+
### `-verbose`, `-verb`
121121

122122
Print verbose statistics and `-debug` output.

include/cpp2util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@
153153
#include <hazard_pointer>
154154
#endif
155155
#include <initializer_list>
156+
#ifdef __cpp_lib_inplace_vector
157+
#include <inplace_vector>
158+
#endif
156159
#include <iomanip>
157160
#include <ios>
158161
#include <iosfwd>

source/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -655,7 +655,7 @@ class cmdline_processor
655655
std::unordered_map<int, std::string> labels = {
656656
{ 2, "Additional dynamic safety checks and contract information" },
657657
{ 4, "Support for constrained target environments" },
658-
{ 8, "Cpp1 file emission options" },
658+
{ 8, "Cpp1 file content options" },
659659
{ 9, "Cppfront output options" }
660660
};
661661

source/to_cpp1.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ static cmdline_processor::register_flag cmd_safe_comparisons(
149149

150150
static auto flag_cpp1_filename = std::string{};
151151
static cmdline_processor::register_flag cmd_cpp1_filename(
152-
8,
152+
9,
153153
"output filename",
154154
"Output to 'filename' (can be 'stdout') - default is *.cpp/*.h",
155155
nullptr,

0 commit comments

Comments
 (0)