@@ -358,6 +358,11 @@ parameter clause. At first, this is only represented in the raw syntax. On first
358
358
ask, we thaw those out by creating a new `GenericParameterClauseSyntaxData`,
359
359
cache it as our child, set its parent to `this`, and send it back to the caller.
360
360
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
+
361
366
Beyond this, `SyntaxData` nodes have *no signficant public API*.
362
367
363
368
- `SyntaxData` are immutable.
@@ -387,6 +392,67 @@ do what you expect with no weird side effects, necessary contexts to maintain,
387
392
etc. If you have a handle on a `Syntax` node, you're safe to query anything
388
393
about it without other processes pulling out the rug from under you.
389
394
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
+
390
456
## Adding new Syntax Nodes
391
457
392
458
Here's a handy checklist when implementing a production in the grammar.
0 commit comments