Skip to content

Commit 63ff5a7

Browse files
committed
Final edits
1 parent edc2038 commit 63ff5a7

5 files changed

Lines changed: 31 additions & 29 deletions

File tree

‎docs/src/rules/format.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ template <typename T1> using t1 = std::vector<T1>;
7070
```rust
7171
// rules/vector/tgt_unsafe.rs
7272
fn t1<T1>() -> Vec<T1> {
73-
Default::default()
73+
Vec::new()
7474
}
7575
```
7676

‎docs/src/rules/ir.md‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ The fragment kinds are:
6464
[Rule Rewriting](./rewriting.md)).
6565
* `va_args`: the expansion point for a variadic tail.
6666

67-
Every type in the IR (in `params`, `return_type`, and type rules) is a
67+
Every type in the Rules IR (in `params`, `return_type`, and type rules) is a
6868
`TypeInfo` object, the type text plus a set of flags:
6969

7070
* `is_refcount_pointer`: the type is a `Ptr<...>`.
@@ -83,7 +83,7 @@ An `ExprRule` carries two flags of its own:
8383
* `is_extern`: the rule is an extern passthrough declaration and has no
8484
body.
8585

86-
Fields that are false, empty, or unset are omitted from the JSON. A `va`
86+
Fields that are false, empty, or unset are omitted from the Rules IR. A `va`
8787
parameter is never listed in `params`, and a `()` return type is omitted.
8888

8989
A type rule serializes as a `TypeRule` object: its `TypeInfo` plus the
@@ -98,28 +98,35 @@ There is no explicit tag distinguishing the two rule kinds: an entry with
9898

9999
## In-memory form
100100

101-
`cpp2rust` mirrors this JSON in C++ structs of the same names, defined in
102-
`cpp2rust/converter/translation_rule.h`. `TranslationRule::Load` reads one
101+
`cpp2rust` mirrors the Rules IR in C++ structs of the same names, defined in
102+
[`cpp2rust/converter/translation_rule.h`][translation-rule-h].
103+
`TranslationRule::Load` reads one
103104
module directory (the `ir_*.json` files described above) and produces two
104-
maps keyed by rule name, one holding `ExprRule`s and one holding `TypeRule`s:
105+
maps keyed by rule name, one holding [`ExprRule`]s and one holding
106+
[`TypeRule`]s:
107+
108+
[translation-rule-h]: https://github.com/cpp2rust/cpp2rust/blob/master/cpp2rust/converter/translation_rule.h
109+
[`ExprRule`]: https://github.com/cpp2rust/cpp2rust/blob/master/cpp2rust/converter/translation_rule.h#L71
110+
[`TypeRule`]: https://github.com/cpp2rust/cpp2rust/blob/master/cpp2rust/converter/translation_rule.h#L84
105111

106112
* An `ExprRule` holds the body
107113
[fragments](#target-ir-ir_unsafejson--ir_refcountjson), the parameter and
108-
return `TypeInfo`s, and the two rule-level flags. The name-keyed JSON maps become
114+
return `TypeInfo`s, and the two rule-level flags (`multi_statement` and `is_extern`). The
115+
name-keyed Rules IR maps become
109116
positional vectors: parameter `aN` is entry N of `params`, generic `TN` is
110117
entry N-1 of `generics` (each entry being the bound list). Rules support
111118
at most 9 generic parameters (`kMaxGenerics`).
112119
* A `TypeRule` holds the mapped type's `TypeInfo` and the `initializer`
113120
expression. The same struct also represents the built-in type mappings
114121
(scalars, pointers, ...) that the loader registers directly in code,
115-
without any JSON behind them: for example, `int` maps to `i32`, and
122+
without any Rules IR behind them: for example, `int` maps to `i32`, and
116123
`int *` to `*mut i32` in the unsafe model or `Ptr<i32>` in the refcount
117124
model.
118125

119126
Both also carry `src`, the canonical C++ signature attached from
120127
[`ir_src.json`](#source-ir-ir_srcjson); it is the key the rule is matched
121128
by.
122129

123-
How the loader finds the IR directory, overlays the refcount model on the
130+
How the loader finds the Rules IR directory, overlays the refcount model on the
124131
unsafe one, and indexes the loaded rules for matching is covered in
125132
[Loading and Matching](./loading.md).

‎docs/src/rules/loading.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
## Finding the rules directory
44

5-
`cpp2rust` takes the IR directory via `--rules <dir>`. If the flag is omitted
5+
`cpp2rust` takes the Rules IR directory via `--rules <dir>`. If the flag is omitted
66
it tries `./rules` and then `<executable dir>/../rules`, accepting the first
77
candidate that contains (recursively) a subdirectory with `ir_src.json` plus
8-
`ir_unsafe.json` or `ir_refcount.json`. Since the build writes the IR to
8+
`ir_unsafe.json` or `ir_refcount.json`. Since the build writes the Rules IR to
99
`<build>/rules` and the binary lands in `<build>/bin`, the default resolution
10-
picks up the generated IR without any flags.
10+
picks up the generated Rules IR without any flags.
1111

1212
## Loading
1313

@@ -51,7 +51,7 @@ same canonical printer used by `cpp-rule-preprocessor`, which is what makes
5151
the two sides comparable:
5252

5353
* Functions and methods print as
54-
`<return type> <qualified name>(<param types>)[ const][ &|&&]`.
54+
`<return type> <qualified::name>(<param types>[, ...])[ const][ volatile][ &|&&]`.
5555
* Enum constants and global variables print as their qualified name.
5656
* Integer literals expanded from a macro print as the macro name.
5757

‎docs/src/rules/overview.md‎

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,25 @@ Each rule module lives in the `rules/` directory and pairs a C++ source file
55
(`src.cpp`) with its Rust translation for each model (`tgt_unsafe.rs` and
66
`tgt_refcount.rs`).
77

8-
The central idea is that every rule is expressed as ordinary, compilable
9-
C++ and Rust source code, free of anything platform dependent. Both sides are
10-
run through real compilers at build time, so a rule that does not compile
11-
fails the build, and the platform-specific spellings a rule needs to match
12-
against (for example `bool` canonicalizing to `_Bool`) are derived by the
13-
compiler on the host rather than written by hand. The same rule sources work
14-
on every platform `cpp2rust` builds on.
8+
Every rule is expressed as ordinary, compilable C++ and Rust source code,
9+
free of anything platform dependent. Both sides are run through real
10+
compilers at build time, so a rule that does not compile fails the build, and
11+
the platform-specific spellings (for example `bool` canonicalizing to
12+
`_Bool`) are derived by the compiler on the host rather than written by hand.
13+
The same rule sources work on every platform `cpp2rust` builds on.
1514

1615
Rules go through a build-time compilation pipeline before `cpp2rust` can use
1716
them:
1817

1918
1. You author a rule module: C++ patterns in `src.cpp` and Rust targets in
2019
`tgt_unsafe.rs` / `tgt_refcount.rs`.
21-
2. At build time, two preprocessors compile the module into JSON IR under
20+
2. At build time, two preprocessors compile the module into Rules IR under
2221
`<build>/rules/<module>/`:
2322
`cpp-rule-preprocessor` compiles the C++ side into `ir_src.json`, and
2423
`rule-preprocessor` compiles the Rust side into `ir_unsafe.json` and
2524
`ir_refcount.json`.
26-
3. At startup, `cpp2rust` loads the IR files and indexes the rules by the
25+
3. At startup, `cpp2rust` loads the Rules IR files and indexes the rules by the
2726
canonical signature of the C++ construct they match.
28-
4. During translation, the converter looks up rules by signature and splices
29-
their Rust bodies into the output, substituting arguments for placeholders
30-
and rewriting the body when needed (e.g. wrapping method calls in
31-
`with_mut` when the receiver is a `Ptr<T>`).
3227

3328
The rest of this part covers each stage:
3429

@@ -41,9 +36,9 @@ The rest of this part covers each stage:
4136
* [Conventions](./conventions.md): naming and style conventions rule authors
4237
must follow.
4338
* [The Rule Preprocessors](./preprocessors.md): the two build-time tools that
44-
compile rules to IR.
39+
compile rules to the Rules IR.
4540
* [The Rules IR](./ir.md): the JSON format the preprocessors emit.
46-
* [Loading and Matching](./loading.md): how `cpp2rust` loads the IR and
41+
* [Loading and Matching](./loading.md): how `cpp2rust` loads the Rules IR and
4742
matches rules against the input AST.
4843
* [The Matching Engine](./matching.md): how a candidate rule's signature
4944
is unified against the input.

‎docs/src/rules/preprocessors.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# The Rule Preprocessors
22

3-
Two build-time tools compile rule modules into the [JSON IR](./ir.md) that
3+
Two build-time tools compile rule modules into the [Rules IR](./ir.md) that
44
`cpp2rust` loads at runtime. Both write into `<build>/rules/<module>/`:
55

66
* `cpp-rule-preprocessor` compiles `src.cpp`/`src.c` into `ir_src.json`.

0 commit comments

Comments
 (0)