v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
ast-source-ranges.h
Go to the documentation of this file.
1// Copyright 2017 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_AST_AST_SOURCE_RANGES_H_
6#define V8_AST_AST_SOURCE_RANGES_H_
7
8#include "src/ast/ast.h"
10
11namespace v8 {
12namespace internal {
13
14// Specifies a range within the source code. {start} is 0-based and inclusive,
15// {end} is 0-based and exclusive.
19 bool IsEmpty() const { return start == kNoSourcePosition; }
20 static SourceRange Empty() { return SourceRange(); }
21 static SourceRange OpenEnded(int32_t start) {
23 }
25 int end = kNoSourcePosition) {
26 return that.IsEmpty() ? Empty() : SourceRange(that.end, end);
27 }
28
29 static constexpr int kFunctionLiteralSourcePosition = -2;
31
32 // Source ranges associated with a function literal do not contain real
33 // source positions; instead, they are created with special marker values.
34 // These are later recognized and rewritten during processing in
35 // Coverage::Collect().
39
40 int32_t start, end;
41};
42
43// The list of ast node kinds that have associated source ranges. Note that this
44// macro is not undefined at the end of this file.
45#define AST_SOURCE_RANGE_LIST(V) \
46 V(BinaryOperation) \
47 V(Block) \
48 V(CaseClause) \
49 V(ConditionalChain) \
50 V(Conditional) \
51 V(Expression) \
52 V(FunctionLiteral) \
53 V(IfStatement) \
54 V(IterationStatement) \
55 V(JumpStatement) \
56 V(NaryOperation) \
57 V(Suspend) \
58 V(SwitchStatement) \
59 V(Throw) \
60 V(TryCatchStatement) \
61 V(TryFinallyStatement)
62
63enum class SourceRangeKind {
64 kBody,
65 kCatch,
67 kElse,
69 kRight,
70 kThen,
71};
72
74 public:
75 virtual ~AstNodeSourceRanges() = default;
77 virtual bool HasRange(SourceRangeKind kind) = 0;
79};
80
82 public:
83 explicit BinaryOperationSourceRanges(const SourceRange& right_range)
84 : right_range_(right_range) {}
85
90
91 bool HasRange(SourceRangeKind kind) override {
93 }
94
95 private:
97};
98
100 public:
101 explicit ContinuationSourceRanges(int32_t continuation_position)
102 : continuation_position_(continuation_position) {}
103
108
111 }
112
117
118 private:
120};
121
123 public:
124 explicit BlockSourceRanges(int32_t continuation_position)
125 : ContinuationSourceRanges(continuation_position) {}
126};
127
129 public:
130 explicit CaseClauseSourceRanges(const SourceRange& body_range)
131 : body_range_(body_range) {}
132
137
140 }
141
142 private:
144};
145
147 public:
149 : then_ranges_(zone), else_ranges_(zone) {}
150
153 DCHECK_LT(index, then_ranges_.size());
154 return then_ranges_[index];
155 }
157 DCHECK_LT(index, else_ranges_.size());
158 return else_ranges_[index];
159 }
160
161 void AddThenRanges(const SourceRange& range) {
162 then_ranges_.push_back(range);
163 }
164
165 void AddElseRange(const SourceRange& else_range) {
166 else_ranges_.push_back(else_range);
167 }
168
169 size_t RangeCount() const { return then_ranges_.size(); }
170
172 bool HasRange(SourceRangeKind kind) override { return false; }
173
174 private:
177};
178
180 public:
181 explicit ConditionalSourceRanges(const SourceRange& then_range,
182 const SourceRange& else_range)
183 : then_range_(then_range), else_range_(else_range) {}
184
187 switch (kind) {
189 return then_range_;
191 return else_range_;
192 default:
193 UNREACHABLE();
194 }
195 }
196
200
201 private:
204};
205
217
219 public:
220 explicit IfStatementSourceRanges(const SourceRange& then_range,
221 const SourceRange& else_range)
222 : then_range_(then_range), else_range_(else_range) {}
223
226 switch (kind) {
228 return else_range_;
230 return then_range_;
233 const SourceRange& trailing_range =
235 return SourceRange::ContinuationOf(trailing_range);
236 }
237 default:
238 UNREACHABLE();
239 }
240 }
241
246
251
252 private:
255 bool has_continuation_ = true;
256};
257
259 public:
260 explicit IterationStatementSourceRanges(const SourceRange& body_range)
261 : body_range_(body_range) {}
262
265 switch (kind) {
267 return body_range_;
271 default:
272 UNREACHABLE();
273 }
274 }
275
280
285
286 private:
288 bool has_continuation_ = true;
289};
290
292 public:
293 explicit JumpStatementSourceRanges(int32_t continuation_position)
294 : ContinuationSourceRanges(continuation_position) {}
295};
296
298 public:
300 : ranges_(zone) {
301 AddRange(range);
302 }
303
305 DCHECK(index < ranges_.size());
306 return ranges_[index];
307 }
308
309 void AddRange(const SourceRange& range) { ranges_.push_back(range); }
310 size_t RangeCount() const { return ranges_.size(); }
311
313 bool HasRange(SourceRangeKind kind) override { return false; }
314
315 private:
317};
318
320 public:
321 explicit ExpressionSourceRanges(const SourceRange& right_range)
322 : right_range_(right_range) {}
323
328
331 }
332
333 private:
335};
336
338 public:
339 explicit SuspendSourceRanges(int32_t continuation_position)
340 : ContinuationSourceRanges(continuation_position) {}
341};
342
344 public:
345 explicit SwitchStatementSourceRanges(int32_t continuation_position)
346 : ContinuationSourceRanges(continuation_position) {}
347};
348
350 public:
351 explicit ThrowSourceRanges(int32_t continuation_position)
352 : ContinuationSourceRanges(continuation_position) {}
353};
354
356 public:
357 explicit TryCatchStatementSourceRanges(const SourceRange& catch_range)
358 : catch_range_(catch_range) {}
359
362 switch (kind) {
364 return catch_range_;
368 default:
369 UNREACHABLE();
370 }
371 }
372
377
382
383 private:
385 bool has_continuation_ = true;
386};
387
389 public:
390 explicit TryFinallyStatementSourceRanges(const SourceRange& finally_range)
391 : finally_range_(finally_range) {}
392
395 switch (kind) {
397 return finally_range_;
401 default:
402 UNREACHABLE();
403 }
404 }
405
410
415
416 private:
418 bool has_continuation_ = true;
419};
420
421// Maps ast node pointers to associated source ranges. The parser creates these
422// mappings and the bytecode generator consumes them.
423class SourceRangeMap final : public ZoneObject {
424 public:
425 explicit SourceRangeMap(Zone* zone) : map_(zone) {}
426
428 auto it = map_.find(node);
429 if (it == map_.end()) return nullptr;
430 return it->second;
431 }
432
433// Type-checked insertion.
434#define DEFINE_MAP_INSERT(type) \
435 void Insert(type* node, type##SourceRanges* ranges) { \
436 DCHECK_NOT_NULL(node); \
437 map_.emplace(node, ranges); \
438 }
440#undef DEFINE_MAP_INSERT
441
442 private:
444};
445
446} // namespace internal
447} // namespace v8
448
449#endif // V8_AST_AST_SOURCE_RANGES_H_
#define DEFINE_MAP_INSERT(type)
#define AST_SOURCE_RANGE_LIST(V)
Builtins::Kind kind
Definition builtins.cc:40
virtual bool HasRange(SourceRangeKind kind)=0
virtual SourceRange GetRange(SourceRangeKind kind)=0
virtual ~AstNodeSourceRanges()=default
SourceRange GetRange(SourceRangeKind kind) override
BinaryOperationSourceRanges(const SourceRange &right_range)
bool HasRange(SourceRangeKind kind) override
BlockSourceRanges(int32_t continuation_position)
SourceRange GetRange(SourceRangeKind kind) override
bool HasRange(SourceRangeKind kind) override
CaseClauseSourceRanges(const SourceRange &body_range)
void AddElseRange(const SourceRange &else_range)
SourceRange GetRangeAtIndex(SourceRangeKind kind, size_t index)
void AddThenRanges(const SourceRange &range)
SourceRange GetRange(SourceRangeKind kind) override
bool HasRange(SourceRangeKind kind) override
SourceRange GetRange(SourceRangeKind kind) override
ConditionalSourceRanges(const SourceRange &then_range, const SourceRange &else_range)
bool HasRange(SourceRangeKind kind) override
ContinuationSourceRanges(int32_t continuation_position)
bool HasRange(SourceRangeKind kind) override
SourceRange GetRange(SourceRangeKind kind) override
SourceRange GetRange(SourceRangeKind kind) override
ExpressionSourceRanges(const SourceRange &right_range)
bool HasRange(SourceRangeKind kind) override
bool HasRange(SourceRangeKind kind) override
SourceRange GetRange(SourceRangeKind kind) override
IfStatementSourceRanges(const SourceRange &then_range, const SourceRange &else_range)
SourceRange GetRange(SourceRangeKind kind) override
bool HasRange(SourceRangeKind kind) override
bool HasRange(SourceRangeKind kind) override
SourceRange GetRange(SourceRangeKind kind) override
IterationStatementSourceRanges(const SourceRange &body_range)
JumpStatementSourceRanges(int32_t continuation_position)
NaryOperationSourceRanges(Zone *zone, const SourceRange &range)
SourceRange GetRange(SourceRangeKind kind) override
bool HasRange(SourceRangeKind kind) override
void AddRange(const SourceRange &range)
ZoneMap< ZoneObject *, AstNodeSourceRanges * > map_
AstNodeSourceRanges * Find(ZoneObject *node)
SuspendSourceRanges(int32_t continuation_position)
SwitchStatementSourceRanges(int32_t continuation_position)
ThrowSourceRanges(int32_t continuation_position)
bool HasRange(SourceRangeKind kind) override
TryCatchStatementSourceRanges(const SourceRange &catch_range)
SourceRange GetRange(SourceRangeKind kind) override
SourceRange GetRange(SourceRangeKind kind) override
TryFinallyStatementSourceRanges(const SourceRange &finally_range)
bool HasRange(SourceRangeKind kind) override
constexpr int kNoSourcePosition
Definition globals.h:850
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_LT(v1, v2)
Definition logging.h:489
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
static SourceRange FunctionLiteralMarkerRange()
static SourceRange ContinuationOf(const SourceRange &that, int end=kNoSourcePosition)
static SourceRange OpenEnded(int32_t start)
static SourceRange Empty()
static constexpr int kFunctionLiteralSourcePosition
SourceRange(int start, int end)