Skip to content

Generator: emit the template text from raw string literals, and tidy the specs - #283

Merged
electricessence merged 5 commits into
masterfrom
refactor/generator-tidy
Dec 2, 2026
Merged

electricessence merged 5 commits into
masterfrom
refactor/generator-tidy

Conversation

@electricessence

@electricessence electricessence commented Dec 1, 2026 •

Copy link
Copy Markdown
Collaborator

What

Emitter.cs wrote the generated file one AppendLine at a time. The text that is the same for every model is now written as raw string literals, laid out the way it is emitted, with holes for the model type, the ids and the counters switch. The header, the counters class, the interceptors, the model's slot table, Bind, Resolve, the trusted Bind, the literal reader and the layout class are done this way. EmitRead, Statement and the per-slot lines stay code, since they are computed per slot.

The file is a little longer (755 to 808 lines): the gain is that each emitted block can be read as the text it produces, not a count of lines.

Small tidy items, in the same change:

  • SlotSpec.Parameter was filled in and never read; removed (it only changes record equality for the incremental cache).
  • The model list and its ids use Distinct and OrderBy instead of a dictionary used as a set plus a sort.
  • The partial model's settable names use string.Join instead of concatenating then Substring(2).
  • A redundant local in ModelReader is folded into its test.

The Receiver enum is not removed: #280 made it live (the interceptor shape depends on it), so that item of the plan no longer applies.

Comments in the generated file

  • Written vertically: broken at the sentence and the colon, one clause to a line, none past about 80 columns.
  • XML documentation stays on the one public generated type, the test-only counters class, as a multi-line summary on the class and on every member (that is what a consumer sees on hover). The generated types and members are file-scoped, so nobody outside the generated file sees them: they keep plain comments.
  • The absent explanation in Resolve moved from the generated code to a comment in the emitter.

Tabs in the emitter

Every fixed text that spelled a tab as an escape is a raw string literal. The row readers take their indent as a depth, and one helper spells it.

Line endings

AppendLine writes the host's line ending, while a raw string literal keeps the line endings of the file the generator was built from (Windows checkouts have CRLF, the repository stores LF). A generator built on one platform and run on another would therefore have written a mix. The finished text is now normalized to LF once, before it is added to the compilation (sb.Replace("\r\n", "\n")), and a new test asserts the generated file contains no carriage return. This is compile time only.

Generated output is unchanged

The generated OdxMaps.g.cs was captured on master and on this branch, for three builds (the generator test models with the counters on and off, and the generated benchmark project), both with the same models. Master's output has CRLF on Windows; the branch's is LF only, so the gate is: the branch's output equals master's with CRLF turned into LF.

tr -d '\r' < master/X.g.cs > master/X.lf.cs
cmp master/X.lf.cs branch/X.g.cs
# tests, counters on:   identical, 514816 bytes
# tests, counters off:  identical, 463147 bytes
# benchmarks generated: identical,  80864 bytes

The last commit changes the comments the generator writes (see Comments in the generated file), so for that commit the same comparison is made with comment-only lines and trailing comments deleted from both sides: identical for all three builds (516145, 464368 and 81333 bytes with the comments, no CR). The generator tests pass (189) and the solution builds without warnings.

Two emitter paths were in no model, so two models were added to the generator tests (in the test project, as a model has to be declared partial in the compilation to count as partial), and master's generator was run over them too:

  • a partial model with nothing settable (PartialConstructorPost, built through its constructor): the list of settable names is empty;
  • a model with no member to map (MemberlessPost): the generator accepts it, and its layout has no ordinal field.

A partial model with every member's ordinal declared and the counters on (the check inside the trusted Bind) was already in the models (TrustedPartialPost, TrustedToolkitPost).

Checks

  • MSBuild Open.Database.Extensions.sln -restore -p:Configuration=Debug: no warnings, no errors.
  • Generator tests on net11.0: 189 run (three new), 0 failed.

Review

Reviewers read the diff (three read-only lenses, then two further adversaries and two lenses from the PM); each finding was checked against the code. No blocking or major finding.

Kept and done:

  • Output depended on the platform that built the generator, because a raw literal keeps the file's line endings: normalized to LF, with a test.
  • Two locals in EmitModel named like the generated known and l: renamed.
  • Paths no model exercised (empty settable list, no member to map): models added and the output compared against master.
  • Bind and the Layout class were a template, a conditional AppendLine and a template: the partial model's check and the ordinal fields are now holes at the start of a line, so each is one template.

Dropped, with the reason:

  • Bind as one template for all three shapes: the layout one (a cache loop and Resolve), the counted trusted one (counters, one return) and the plain trusted one (an expression body) share only the signature, so one template would need holes for whole bodies, which is the code in strings it replaced. The partial check is the one line they share, and it is now a hole.
  • The counted and plain interceptor bodies: block body or expression body, two small literals.
  • The l.O{k} = -1; line and the absent hole in Resolve: a template for the not-mapped slot would be one line, and one for the absent case would repeat the nine-line shape to change one statement and a comment.
  • Other AppendLine lines with \t: per-slot rows, or one line.
  • Two different specs with one type name could order differently from the old unstable sort: one type in one compilation has one spec.
  • A header comment in Emitter.cs names a spec file that does not exist: older than this change, untouched.

🤖 Generated with Claude Code

electricessence and others added 5 commits December 1, 2026 12:51
…d tidy the specs

The emitter wrote the header, the counters, the interceptors, the model's
slot table, Bind, Resolve and the trusted reader one AppendLine at a time.
They are now raw string literals with the generated text laid out as it is
emitted; only the parts computed per slot (EmitRead, Statement) stay code.
The generated output is byte-identical.

Also:
- SlotSpec.Parameter was written and never read: removed.
- The model list and its ids are built with Distinct and OrderBy instead of a
  dictionary used as a set plus a sort.
- The partial model's settable names are joined with string.Join.
- A redundant local in ModelReader is folded into its test.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AppendLine writes the host's line ending and a raw string literal keeps the
line endings of the file the generator was built from, so a generator built
on Windows and run on Linux or macOS emitted a mix. The finished text is now
normalized to LF once before it is added to the compilation, and a test
asserts the generated file has no carriage return.

Also renames two locals in Emitter.EmitModel that read like the generated
names beside them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…es, and cover two more shapes

Bind and the Layout class were each split into a template, a conditional
AppendLine and a second template. The partial model's check and the ordinal
fields are now holes at the start of a line, so each is one template.

Generator.Tests gains a partial model with nothing settable (the list of
settable names the built type is checked against is empty) and a model with
no member to map (a layout with no ordinal field), so those paths of the
emitter are generated and read in the test project. The generated output for
every model in the test project, and for the benchmark project, is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…hat opens it

The closing quotes of the raw string literals in the emitter were at column
0, so the template text sat at column 0 inside indented methods. Each
literal now closes at the indent of the line where it opens (an expression
body, a statement, a ternary arm), and its content is that indent plus the
generated indent, so the compiler strips the code's indent and the generated
text is unchanged.

The generated file is byte-identical to before for the test project (counters
on and off) and the benchmark project.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
… of the emitter's strings

The comments in the generated file are broken at the sentence and the colon,
one clause to a line, so none runs past about 80 columns. The XML
documentation stays on the one public generated type (the test-only counters
class) and is now a multi-line summary on the class and on every member; the
types and members that are file-local keep plain comments, as nothing outside
the generated file can see them.

Every fixed text in the emitter that spelled a tab as an escape (the
interceptor constraint, the slot table entries, the layout's ordinal fields,
the partial model's check, the trusted Bind, the reader methods and the
guarded read's loop and cases) is now a raw string literal. The row readers
take their indent as a depth, and one helper spells it.

The generated code is identical to before once comments are left out (the
test project with counters on and off, and the benchmark project); only the
comment lines differ.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@electricessence
electricessence merged commit d71fee2 into master Dec 2, 2026
9 checks passed
@electricessence
electricessence deleted the refactor/generator-tidy branch December 2, 2026 01:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant