21
21
#include " swift/Syntax/SyntaxKind.h"
22
22
#include " llvm/Support/Debug.h"
23
23
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
+
24
34
namespace swift {
25
35
26
36
typedef const void *OpaqueSyntaxNode;
@@ -48,8 +58,13 @@ class ParsedRawSyntaxNode {
48
58
// / SyntaxParseActions, which created it.
49
59
RecordedOrDeferredNode Data;
50
60
61
+ #ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES
51
62
// / 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.
52
66
CharSourceRange Range;
67
+ #endif
53
68
uint16_t SynKind;
54
69
uint16_t TokKind;
55
70
// / Primary used for capturing a deferred missing token.
@@ -62,24 +77,46 @@ class ParsedRawSyntaxNode {
62
77
// MARK: - Constructors
63
78
64
79
ParsedRawSyntaxNode ()
65
- : Data(nullptr , DataKind::Null), Range(),
80
+ : Data(nullptr , DataKind::Null),
66
81
SynKind (uint16_t (syntax::SyntaxKind::Unknown)),
67
82
TokKind(uint16_t (tok::unknown)) {}
68
83
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)),
73
88
TokKind(uint16_t (TokKind)), IsMissing(IsMissing) {
74
89
assert (getKind () == SynKind && " Syntax kind with too large value!" );
75
90
assert (getTokenKind () == TokKind && " Token kind with too large value!" );
76
91
}
77
92
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
+
78
113
ParsedRawSyntaxNode &operator =(ParsedRawSyntaxNode &&other) {
79
114
assert (ensureDataIsNotRecorded () &&
80
115
" recorded data is being destroyed by assignment" );
81
116
Data = std::move (other.Data );
117
+ #ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES
82
118
Range = std::move (other.Range );
119
+ #endif
83
120
SynKind = std::move (other.SynKind );
84
121
TokKind = std::move (other.TokKind );
85
122
IsMissing = std::move (other.IsMissing );
@@ -91,9 +128,7 @@ class ParsedRawSyntaxNode {
91
128
*this = std::move (other);
92
129
}
93
130
94
- static ParsedRawSyntaxNode null () {
95
- return ParsedRawSyntaxNode{};
96
- }
131
+ static ParsedRawSyntaxNode null () { return ParsedRawSyntaxNode (); }
97
132
98
133
~ParsedRawSyntaxNode () {
99
134
assert (ensureDataIsNotRecorded () && " recorded data is being destructed" );
@@ -152,8 +187,13 @@ class ParsedRawSyntaxNode {
152
187
}
153
188
bool isMissing () const { return IsMissing; }
154
189
190
+ #ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES
155
191
// / 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.
156
195
CharSourceRange getRange () const { return Range; }
196
+ #endif
157
197
158
198
size_t
159
199
getDeferredNumChildren (const SyntaxParsingContext *SyntaxContext) const ;
@@ -174,13 +214,17 @@ class ParsedRawSyntaxNode {
174
214
SynKind = uint16_t (syntax::SyntaxKind::Unknown);
175
215
TokKind = uint16_t (tok::unknown);
176
216
IsMissing = false ;
217
+ #ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES
177
218
Range = CharSourceRange ();
219
+ #endif
178
220
}
179
221
180
222
ParsedRawSyntaxNode unsafeCopy () const {
181
223
ParsedRawSyntaxNode copy;
182
224
copy.Data = Data;
225
+ #ifdef PARSEDRAWSYNTAXNODE_VERIFY_RANGES
183
226
copy.Range = Range;
227
+ #endif
184
228
copy.SynKind = SynKind;
185
229
copy.TokKind = TokKind;
186
230
copy.IsMissing = IsMissing;
0 commit comments