@@ -59,6 +59,8 @@ class AstNodeSourceRanges : public ZoneObject {
5959 public:
6060 virtual ~AstNodeSourceRanges () = default ;
6161 virtual SourceRange GetRange (SourceRangeKind kind) = 0;
62+ virtual bool HasRange (SourceRangeKind kind) = 0;
63+ virtual void RemoveContinuationRange () { UNREACHABLE (); }
6264};
6365
6466class BinaryOperationSourceRanges final : public AstNodeSourceRanges {
@@ -67,10 +69,14 @@ class BinaryOperationSourceRanges final : public AstNodeSourceRanges {
6769 : right_range_(right_range) {}
6870
6971 SourceRange GetRange (SourceRangeKind kind) override {
70- DCHECK_EQ ( kind, SourceRangeKind:: kRight );
72+ DCHECK ( HasRange ( kind) );
7173 return right_range_;
7274 }
7375
76+ bool HasRange (SourceRangeKind kind) override {
77+ return kind == SourceRangeKind::kRight ;
78+ }
79+
7480 private:
7581 SourceRange right_range_;
7682};
@@ -81,10 +87,19 @@ class ContinuationSourceRanges : public AstNodeSourceRanges {
8187 : continuation_position_(continuation_position) {}
8288
8389 SourceRange GetRange (SourceRangeKind kind) override {
84- DCHECK_EQ ( kind, SourceRangeKind:: kContinuation );
90+ DCHECK ( HasRange ( kind) );
8591 return SourceRange::OpenEnded (continuation_position_);
8692 }
8793
94+ bool HasRange (SourceRangeKind kind) override {
95+ return kind == SourceRangeKind::kContinuation ;
96+ }
97+
98+ void RemoveContinuationRange () override {
99+ DCHECK (HasRange (SourceRangeKind::kContinuation ));
100+ continuation_position_ = kNoSourcePosition ;
101+ }
102+
88103 private:
89104 int32_t continuation_position_;
90105};
@@ -101,10 +116,14 @@ class CaseClauseSourceRanges final : public AstNodeSourceRanges {
101116 : body_range_(body_range) {}
102117
103118 SourceRange GetRange (SourceRangeKind kind) override {
104- DCHECK_EQ ( kind, SourceRangeKind:: kBody );
119+ DCHECK ( HasRange ( kind) );
105120 return body_range_;
106121 }
107122
123+ bool HasRange (SourceRangeKind kind) override {
124+ return kind == SourceRangeKind::kBody ;
125+ }
126+
108127 private:
109128 SourceRange body_range_;
110129};
@@ -116,6 +135,7 @@ class ConditionalSourceRanges final : public AstNodeSourceRanges {
116135 : then_range_(then_range), else_range_(else_range) {}
117136
118137 SourceRange GetRange (SourceRangeKind kind) override {
138+ DCHECK (HasRange (kind));
119139 switch (kind) {
120140 case SourceRangeKind::kThen :
121141 return then_range_;
@@ -126,6 +146,10 @@ class ConditionalSourceRanges final : public AstNodeSourceRanges {
126146 }
127147 }
128148
149+ bool HasRange (SourceRangeKind kind) override {
150+ return kind == SourceRangeKind::kThen || kind == SourceRangeKind::kElse ;
151+ }
152+
129153 private:
130154 SourceRange then_range_;
131155 SourceRange else_range_;
@@ -138,12 +162,14 @@ class IfStatementSourceRanges final : public AstNodeSourceRanges {
138162 : then_range_(then_range), else_range_(else_range) {}
139163
140164 SourceRange GetRange (SourceRangeKind kind) override {
165+ DCHECK (HasRange (kind));
141166 switch (kind) {
142167 case SourceRangeKind::kElse :
143168 return else_range_;
144169 case SourceRangeKind::kThen :
145170 return then_range_;
146171 case SourceRangeKind::kContinuation : {
172+ if (!has_continuation_) return SourceRange::Empty ();
147173 const SourceRange& trailing_range =
148174 else_range_.IsEmpty () ? then_range_ : else_range_;
149175 return SourceRange::ContinuationOf (trailing_range);
@@ -153,9 +179,20 @@ class IfStatementSourceRanges final : public AstNodeSourceRanges {
153179 }
154180 }
155181
182+ bool HasRange (SourceRangeKind kind) override {
183+ return kind == SourceRangeKind::kThen || kind == SourceRangeKind::kElse ||
184+ kind == SourceRangeKind::kContinuation ;
185+ }
186+
187+ void RemoveContinuationRange () override {
188+ DCHECK (HasRange (SourceRangeKind::kContinuation ));
189+ has_continuation_ = false ;
190+ }
191+
156192 private:
157193 SourceRange then_range_;
158194 SourceRange else_range_;
195+ bool has_continuation_ = true ;
159196};
160197
161198class IterationStatementSourceRanges final : public AstNodeSourceRanges {
@@ -164,18 +201,31 @@ class IterationStatementSourceRanges final : public AstNodeSourceRanges {
164201 : body_range_(body_range) {}
165202
166203 SourceRange GetRange (SourceRangeKind kind) override {
204+ DCHECK (HasRange (kind));
167205 switch (kind) {
168206 case SourceRangeKind::kBody :
169207 return body_range_;
170208 case SourceRangeKind::kContinuation :
209+ if (!has_continuation_) return SourceRange::Empty ();
171210 return SourceRange::ContinuationOf (body_range_);
172211 default :
173212 UNREACHABLE ();
174213 }
175214 }
176215
216+ bool HasRange (SourceRangeKind kind) override {
217+ return kind == SourceRangeKind::kBody ||
218+ kind == SourceRangeKind::kContinuation ;
219+ }
220+
221+ void RemoveContinuationRange () override {
222+ DCHECK (HasRange (SourceRangeKind::kContinuation ));
223+ has_continuation_ = false ;
224+ }
225+
177226 private:
178227 SourceRange body_range_;
228+ bool has_continuation_ = true ;
179229};
180230
181231class JumpStatementSourceRanges final : public ContinuationSourceRanges {
@@ -200,6 +250,7 @@ class NaryOperationSourceRanges final : public AstNodeSourceRanges {
200250 size_t RangeCount () const { return ranges_.size (); }
201251
202252 SourceRange GetRange (SourceRangeKind kind) override { UNREACHABLE (); }
253+ bool HasRange (SourceRangeKind kind) override { return false ; }
203254
204255 private:
205256 ZoneVector<SourceRange> ranges_;
@@ -229,18 +280,31 @@ class TryCatchStatementSourceRanges final : public AstNodeSourceRanges {
229280 : catch_range_(catch_range) {}
230281
231282 SourceRange GetRange (SourceRangeKind kind) override {
283+ DCHECK (HasRange (kind));
232284 switch (kind) {
233285 case SourceRangeKind::kCatch :
234286 return catch_range_;
235287 case SourceRangeKind::kContinuation :
288+ if (!has_continuation_) return SourceRange::Empty ();
236289 return SourceRange::ContinuationOf (catch_range_);
237290 default :
238291 UNREACHABLE ();
239292 }
240293 }
241294
295+ bool HasRange (SourceRangeKind kind) override {
296+ return kind == SourceRangeKind::kCatch ||
297+ kind == SourceRangeKind::kContinuation ;
298+ }
299+
300+ void RemoveContinuationRange () override {
301+ DCHECK (HasRange (SourceRangeKind::kContinuation ));
302+ has_continuation_ = false ;
303+ }
304+
242305 private:
243306 SourceRange catch_range_;
307+ bool has_continuation_ = true ;
244308};
245309
246310class TryFinallyStatementSourceRanges final : public AstNodeSourceRanges {
@@ -249,18 +313,31 @@ class TryFinallyStatementSourceRanges final : public AstNodeSourceRanges {
249313 : finally_range_(finally_range) {}
250314
251315 SourceRange GetRange (SourceRangeKind kind) override {
316+ DCHECK (HasRange (kind));
252317 switch (kind) {
253318 case SourceRangeKind::kFinally :
254319 return finally_range_;
255320 case SourceRangeKind::kContinuation :
321+ if (!has_continuation_) return SourceRange::Empty ();
256322 return SourceRange::ContinuationOf (finally_range_);
257323 default :
258324 UNREACHABLE ();
259325 }
260326 }
261327
328+ bool HasRange (SourceRangeKind kind) override {
329+ return kind == SourceRangeKind::kFinally ||
330+ kind == SourceRangeKind::kContinuation ;
331+ }
332+
333+ void RemoveContinuationRange () override {
334+ DCHECK (HasRange (SourceRangeKind::kContinuation ));
335+ has_continuation_ = false ;
336+ }
337+
262338 private:
263339 SourceRange finally_range_;
340+ bool has_continuation_ = true ;
264341};
265342
266343// Maps ast node pointers to associated source ranges. The parser creates these
0 commit comments