Skip to content

Commit 2578ae3

Browse files
committed
[Syntax] Add example object diagram for a return statement
Add a little visualization for a code snippet showing the main players in the Syntax tree, showing where the strong references flow. NFC - documentation only.
1 parent 098df72 commit 2578ae3

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

lib/Syntax/.doc/SyntaxExample.graffle

5.61 KB
Binary file not shown.

lib/Syntax/.doc/SyntaxExample.png

783 KB
Loading

lib/Syntax/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ parameter clause. At first, this is only represented in the raw syntax. On first
358358
ask, we thaw those out by creating a new `GenericParameterClauseSyntaxData`,
359359
cache it as our child, set its parent to `this`, and send it back to the caller.
360360
361+
You can think of `SyntaxData` as "concrete" or "realized" syntax nodes. They
362+
represent a specific piece of source code, have an absolute location, line and
363+
column number, etc. `RawSyntax` are more like the integer 1 - existing in theory
364+
everywhere it occurs.
365+
361366
Beyond this, `SyntaxData` nodes have *no signficant public API*.
362367
363368
- `SyntaxData` are immutable.
@@ -387,6 +392,67 @@ do what you expect with no weird side effects, necessary contexts to maintain,
387392
etc. If you have a handle on a `Syntax` node, you're safe to query anything
388393
about it without other processes pulling out the rug from under you.
389394
395+
### Example Object Diagram: `{ return 1}`
396+
397+
Here's an example of what you might have as a result of the following C++
398+
code:
399+
400+
```c++
401+
auto LeftBrace = SyntaxFactory::makeLeftBraceToken({}, Trivia::spaces(1));
402+
403+
auto IntegerTok = SyntaxFactory::makeIntegerLiteralToken("1", {}, Trivia::spaces(1));
404+
auto Integer = SyntaxFactory::makeIntegerLiteralExpr(IntegerTok);
405+
406+
auto ReturnKW = SyntaxFactory::makeReturnKeyword({}, Trivia::spaces(1));
407+
408+
// This ReturnStmtSyntax is floating, with no root.
409+
auto Return = SyntaxFactory::makeReturnStmt(ReturnKW, Integer);
410+
411+
auto RightBrace = SyntaxFactory::makeLeftBraceToken({}, {});
412+
413+
auto Statements = SyntaxFactory::makeBlankStmtList()
414+
.addExpr(Return);
415+
416+
auto Block = SyntaxFactory::makeBlankCodeBlockStmt()
417+
// Takes a reference of the token directly and increments the
418+
// reference count.
419+
.withLeftBraceToken(LeftBrace)
420+
421+
// Only takes a strong reference to the RawSyntax of the
422+
// ReturnStmtSyntax above.
423+
.withStatements(Statements)
424+
425+
// Takes a reference of the token directly and increments the
426+
// reference count.
427+
.withRightBraceToken(RightBrace);
428+
429+
// Returns a new ReturnStmtSyntax with the root set to the Block
430+
// above, and the parent set to the StmtListSyntax.
431+
auto MyReturn = Block.getStatement(0).castTo<ReturnStmt>;
432+
```
433+
434+
And here's what the object diagram would look like starting with `MyReturn`.
435+
436+
![Syntax Example](.doc/SyntaxExample.png)
437+
438+
Legend:
439+
- Green: `RawSyntax` types (`TokenSyntax` is a `RawSyntax`)
440+
- Red: `SyntaxData` types
441+
- Blue: `Syntax` types
442+
- Gray: `Trivia`
443+
- Solid Arrows: Strong references
444+
- Dashed Arrows: Weak references
445+
446+
A couple of interesting points and reminders:
447+
- All strong references point downward in the tree.
448+
- One `SyntaxData` for each `RawSyntax`.
449+
Remember, a `SyntaxData` is essentially a `RawSyntax` with a parent pointer
450+
and cached `SyntaxData` children.
451+
- Parent pointers are omitted here but there are weak references pointing
452+
upward among `SyntaxData` (red) nodes.
453+
- Clients only work with `Syntax` (blue) nodes and `Trivia` (gray), and should
454+
never see `SyntaxData` (red) or `RawSyntax` (green) nodes.
455+
390456
## Adding new Syntax Nodes
391457

392458
Here's a handy checklist when implementing a production in the grammar.

0 commit comments

Comments
 (0)