2121#include " swift/Syntax/SyntaxKind.h"
2222#include " llvm/Support/Debug.h"
2323
24+ #ifndef NDEBUG
25+ // / Whether \c ParsedRawSyntaxNode should keep track of its range and verify
26+ // / that the children of layout nodes have consecutive ranges.
27+ // / Because this significantly changes the way, \c ParsedRawSyntaxNode and
28+ // / \c ParsedRawSyntaxNodeRecorder are being compiled, this is a separate
29+ // / constant from \c NDEBUG, so that it can be toggled independently to \c
30+ // / NDEBUG during development.
31+ #define PARSEDRAWSYNTAXNODE_VERIFY_RANGES 1
32+ #endif
33+
2434namespace swift {
2535
2636typedef const void *OpaqueSyntaxNode;
@@ -48,8 +58,13 @@ class ParsedRawSyntaxNode {
4858 // / SyntaxParseActions, which created it.
4959 RecordedOrDeferredNode Data;
5060
61+ #ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES
5162 // / The range of this node, including trivia.
63+ // / Only store this as a member when it's actually needed to keep \c
64+ // / ParsedRawSyntaxNode as small as possible, which improves performance
65+ // / when it is being passed around.
5266 CharSourceRange Range;
67+ #endif
5368 uint16_t SynKind;
5469 uint16_t TokKind;
5570 // / Primary used for capturing a deferred missing token.
@@ -62,24 +77,46 @@ class ParsedRawSyntaxNode {
6277 // MARK: - Constructors
6378
6479 ParsedRawSyntaxNode ()
65- : Data(nullptr , DataKind::Null), Range(),
80+ : Data(nullptr , DataKind::Null),
6681 SynKind (uint16_t (syntax::SyntaxKind::Unknown)),
6782 TokKind(uint16_t (tok::unknown)) {}
6883
69- ParsedRawSyntaxNode (OpaqueSyntaxNode Opaque, CharSourceRange Range,
70- syntax::SyntaxKind SynKind, tok TokKind, DataKind DK ,
71- bool IsMissing)
72- : Data(Opaque, DK ), Range(Range), SynKind(uint16_t (SynKind)),
84+ # ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES
85+ ParsedRawSyntaxNode (RecordedOrDeferredNode Data, syntax::SyntaxKind SynKind,
86+ tok TokKind, bool IsMissing, CharSourceRange Range )
87+ : Data(Data ), Range(Range), SynKind(uint16_t (SynKind)),
7388 TokKind(uint16_t (TokKind)), IsMissing(IsMissing) {
7489 assert (getKind () == SynKind && " Syntax kind with too large value!" );
7590 assert (getTokenKind () == TokKind && " Token kind with too large value!" );
7691 }
7792
93+ ParsedRawSyntaxNode (OpaqueSyntaxNode Opaque, syntax::SyntaxKind SynKind,
94+ tok TokKind, DataKind DK, bool IsMissing,
95+ CharSourceRange Range)
96+ : ParsedRawSyntaxNode(RecordedOrDeferredNode(Opaque, DK), SynKind,
97+ TokKind, IsMissing, Range) {}
98+ #else
99+ ParsedRawSyntaxNode (RecordedOrDeferredNode Data, syntax::SyntaxKind SynKind,
100+ tok TokKind, bool IsMissing)
101+ : Data(Data), SynKind(uint16_t (SynKind)), TokKind(uint16_t (TokKind)),
102+ IsMissing(IsMissing) {
103+ assert (getKind () == SynKind && " Syntax kind with too large value!" );
104+ assert (getTokenKind () == TokKind && " Token kind with too large value!" );
105+ }
106+
107+ ParsedRawSyntaxNode (OpaqueSyntaxNode Opaque, syntax::SyntaxKind SynKind,
108+ tok TokKind, DataKind DK, bool IsMissing)
109+ : ParsedRawSyntaxNode(RecordedOrDeferredNode(Opaque, DK), SynKind,
110+ TokKind, IsMissing) {}
111+ #endif
112+
78113 ParsedRawSyntaxNode &operator =(ParsedRawSyntaxNode &&other) {
79114 assert (ensureDataIsNotRecorded () &&
80115 " recorded data is being destroyed by assignment" );
81116 Data = std::move (other.Data );
117+ #ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES
82118 Range = std::move (other.Range );
119+ #endif
83120 SynKind = std::move (other.SynKind );
84121 TokKind = std::move (other.TokKind );
85122 IsMissing = std::move (other.IsMissing );
@@ -91,9 +128,7 @@ class ParsedRawSyntaxNode {
91128 *this = std::move (other);
92129 }
93130
94- static ParsedRawSyntaxNode null () {
95- return ParsedRawSyntaxNode{};
96- }
131+ static ParsedRawSyntaxNode null () { return ParsedRawSyntaxNode (); }
97132
98133 ~ParsedRawSyntaxNode () {
99134 assert (ensureDataIsNotRecorded () && " recorded data is being destructed" );
@@ -152,8 +187,13 @@ class ParsedRawSyntaxNode {
152187 }
153188 bool isMissing () const { return IsMissing; }
154189
190+ #ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES
155191 // / Returns the range of this node including leading and trailing trivia.
192+ // /
193+ // / This method is only present if \c ParsedRawSyntaxNode is keeping track
194+ // / of its range to verify element ranges.
156195 CharSourceRange getRange () const { return Range; }
196+ #endif
157197
158198 size_t
159199 getDeferredNumChildren (const SyntaxParsingContext *SyntaxContext) const ;
@@ -174,13 +214,17 @@ class ParsedRawSyntaxNode {
174214 SynKind = uint16_t (syntax::SyntaxKind::Unknown);
175215 TokKind = uint16_t (tok::unknown);
176216 IsMissing = false ;
217+ #ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES
177218 Range = CharSourceRange ();
219+ #endif
178220 }
179221
180222 ParsedRawSyntaxNode unsafeCopy () const {
181223 ParsedRawSyntaxNode copy;
182224 copy.Data = Data;
225+ #ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES
183226 copy.Range = Range;
227+ #endif
184228 copy.SynKind = SynKind;
185229 copy.TokKind = TokKind;
186230 copy.IsMissing = IsMissing;
0 commit comments