-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
Expand file tree
/
Copy pathpreparser.h
More file actions
1614 lines (1372 loc) · 59.1 KB
/
preparser.h
File metadata and controls
1614 lines (1372 loc) · 59.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2012 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef V8_PARSING_PREPARSER_H_
#define V8_PARSING_PREPARSER_H_
#include "src/ast/ast-value-factory.h"
#include "src/ast/ast.h"
#include "src/ast/scopes.h"
#include "src/parsing/parse-info.h"
#include "src/parsing/parser-base.h"
#include "src/parsing/pending-compilation-error-handler.h"
#include "src/parsing/preparser-logger.h"
namespace v8 {
namespace internal {
// Whereas the Parser generates AST during the recursive descent,
// the PreParser doesn't create a tree. Instead, it passes around minimal
// data objects (PreParserExpression, PreParserIdentifier etc.) which contain
// just enough data for the upper layer functions. PreParserFactory is
// responsible for creating these dummy objects. It provides a similar kind of
// interface as AstNodeFactory, so ParserBase doesn't need to care which one is
// used.
class PreparseDataBuilder;
class PreParserIdentifier {
public:
PreParserIdentifier() : type_(kUnknownIdentifier) {}
static PreParserIdentifier Default() {
return PreParserIdentifier(kUnknownIdentifier);
}
static PreParserIdentifier Null() {
return PreParserIdentifier(kNullIdentifier);
}
static PreParserIdentifier Eval() {
return PreParserIdentifier(kEvalIdentifier);
}
static PreParserIdentifier Arguments() {
return PreParserIdentifier(kArgumentsIdentifier);
}
static PreParserIdentifier Constructor() {
return PreParserIdentifier(kConstructorIdentifier);
}
static PreParserIdentifier Async() {
return PreParserIdentifier(kAsyncIdentifier);
}
static PreParserIdentifier PrivateName() {
return PreParserIdentifier(kPrivateNameIdentifier);
}
bool IsNull() const { return type_ == kNullIdentifier; }
bool IsEval() const { return type_ == kEvalIdentifier; }
bool IsAsync() const { return type_ == kAsyncIdentifier; }
bool IsArguments() const { return type_ == kArgumentsIdentifier; }
bool IsEvalOrArguments() const {
static_assert(kEvalIdentifier + 1 == kArgumentsIdentifier);
return base::IsInRange(type_, kEvalIdentifier, kArgumentsIdentifier);
}
bool IsConstructor() const { return type_ == kConstructorIdentifier; }
bool IsPrivateName() const { return type_ == kPrivateNameIdentifier; }
private:
enum Type : uint8_t {
kNullIdentifier,
kUnknownIdentifier,
kEvalIdentifier,
kArgumentsIdentifier,
kConstructorIdentifier,
kAsyncIdentifier,
kPrivateNameIdentifier
};
explicit PreParserIdentifier(Type type) : string_(nullptr), type_(type) {}
const AstRawString* string_;
Type type_;
friend class PreParserExpression;
friend class PreParser;
};
class PreParserExpression {
public:
PreParserExpression() : code_(TypeField::encode(kNull)) {}
static PreParserExpression Null() { return PreParserExpression(); }
static PreParserExpression Failure() {
return PreParserExpression(TypeField::encode(kFailure));
}
static PreParserExpression Default() {
return PreParserExpression(TypeField::encode(kExpression));
}
static PreParserExpression FromIdentifier(const PreParserIdentifier& id) {
return PreParserExpression(TypeField::encode(kIdentifierExpression) |
IdentifierTypeField::encode(id.type_));
}
static PreParserExpression Assignment() {
return PreParserExpression(TypeField::encode(kExpression) |
ExpressionTypeField::encode(kAssignment));
}
static PreParserExpression ObjectLiteral() {
return PreParserExpression(
TypeField::encode(kArrayOrObjectLiteralExpression));
}
static PreParserExpression ArrayLiteral() {
return PreParserExpression(
TypeField::encode(kArrayOrObjectLiteralExpression));
}
static PreParserExpression StringLiteral() {
return PreParserExpression(TypeField::encode(kStringLiteralExpression));
}
static PreParserExpression This() {
return PreParserExpression(TypeField::encode(kExpression) |
ExpressionTypeField::encode(kThisExpression));
}
static PreParserExpression ThisPrivateReference() {
return PreParserExpression(
TypeField::encode(kExpression) |
ExpressionTypeField::encode(kThisPrivateReferenceExpression));
}
static PreParserExpression ThisProperty() {
return PreParserExpression(
TypeField::encode(kExpression) |
ExpressionTypeField::encode(kThisPropertyExpression));
}
static PreParserExpression Property() {
return PreParserExpression(
TypeField::encode(kExpression) |
ExpressionTypeField::encode(kPropertyExpression));
}
static PreParserExpression PrivateReference() {
return PreParserExpression(
TypeField::encode(kExpression) |
ExpressionTypeField::encode(kPrivateReferenceExpression));
}
static PreParserExpression Call() {
return PreParserExpression(TypeField::encode(kExpression) |
ExpressionTypeField::encode(kCallExpression));
}
static PreParserExpression CallEval() {
return PreParserExpression(
TypeField::encode(kExpression) |
ExpressionTypeField::encode(kCallEvalExpression));
}
static PreParserExpression SuperCallReference() {
return PreParserExpression(
TypeField::encode(kExpression) |
ExpressionTypeField::encode(kSuperCallReference));
}
bool IsNull() const { return TypeField::decode(code_) == kNull; }
bool IsFailureExpression() const {
return TypeField::decode(code_) == kFailure;
}
bool IsIdentifier() const {
return TypeField::decode(code_) == kIdentifierExpression;
}
PreParserIdentifier AsIdentifier() const {
DCHECK(IsIdentifier());
return PreParserIdentifier(IdentifierTypeField::decode(code_));
}
bool IsAssignment() const {
return TypeField::decode(code_) == kExpression &&
ExpressionTypeField::decode(code_) == kAssignment;
}
bool IsPattern() const {
return TypeField::decode(code_) == kArrayOrObjectLiteralExpression;
}
bool IsStringLiteral() const {
return TypeField::decode(code_) == kStringLiteralExpression;
}
bool IsThis() const {
return TypeField::decode(code_) == kExpression &&
ExpressionTypeField::decode(code_) == kThisExpression;
}
bool IsThisProperty() const {
return TypeField::decode(code_) == kExpression &&
(ExpressionTypeField::decode(code_) == kThisPropertyExpression ||
ExpressionTypeField::decode(code_) ==
kThisPrivateReferenceExpression);
}
bool IsProperty() const {
return TypeField::decode(code_) == kExpression &&
(ExpressionTypeField::decode(code_) == kPropertyExpression ||
ExpressionTypeField::decode(code_) == kThisPropertyExpression ||
ExpressionTypeField::decode(code_) == kPrivateReferenceExpression ||
ExpressionTypeField::decode(code_) ==
kThisPrivateReferenceExpression);
}
bool IsPrivateReference() const {
return TypeField::decode(code_) == kExpression &&
(ExpressionTypeField::decode(code_) == kPrivateReferenceExpression ||
ExpressionTypeField::decode(code_) ==
kThisPrivateReferenceExpression);
}
bool IsCall() const {
return TypeField::decode(code_) == kExpression &&
(ExpressionTypeField::decode(code_) == kCallExpression ||
ExpressionTypeField::decode(code_) == kCallEvalExpression);
}
bool IsSuperCallReference() const {
return TypeField::decode(code_) == kExpression &&
ExpressionTypeField::decode(code_) == kSuperCallReference;
}
// At the moment PreParser doesn't track these expression types.
bool IsFunctionLiteral() const { return false; }
bool IsCallNew() const { return false; }
bool is_tagged_template() const { return false; }
bool is_parenthesized() const { return IsParenthesizedField::decode(code_); }
void mark_parenthesized() {
code_ = IsParenthesizedField::update(code_, true);
}
void clear_parenthesized() {
code_ = IsParenthesizedField::update(code_, false);
}
PreParserExpression* AsCall() { return this; }
PreParserExpression* AsFunctionLiteral() { return this; }
// Dummy implementation for making expression->somefunc() work in both Parser
// and PreParser.
PreParserExpression* operator->() { return this; }
// More dummy implementations of things PreParser doesn't need to track:
void SetShouldEagerCompile() {}
int position() const { return kNoSourcePosition; }
void set_function_token_position(int position) {}
void set_suspend_count(int suspend_count) {}
private:
enum Type {
kNull,
kFailure,
kExpression,
kIdentifierExpression,
kStringLiteralExpression,
kArrayOrObjectLiteralExpression
};
enum ExpressionType {
kThisExpression,
kThisPropertyExpression,
kThisPrivateReferenceExpression,
kPropertyExpression,
kPrivateReferenceExpression,
kCallExpression,
kCallEvalExpression,
kSuperCallReference,
kAssignment
};
explicit PreParserExpression(uint32_t expression_code)
: code_(expression_code) {}
// The first three bits are for the Type.
using TypeField = base::BitField<Type, 0, 3>;
// The high order bit applies only to nodes which would inherit from the
// Expression ASTNode --- This is by necessity, due to the fact that
// Expression nodes may be represented as multiple Types, not exclusively
// through kExpression.
// TODO(caitp, adamk): clean up PreParserExpression bitfields.
using IsParenthesizedField = TypeField::Next<bool, 1>;
// The rest of the bits are interpreted depending on the value
// of the Type field, so they can share the storage.
using ExpressionTypeField = IsParenthesizedField::Next<ExpressionType, 4>;
using IdentifierTypeField =
IsParenthesizedField::Next<PreParserIdentifier::Type, 8>;
using HasCoverInitializedNameField = IsParenthesizedField::Next<bool, 1>;
uint32_t code_;
friend class PreParser;
friend class PreParserFactory;
friend class PreParserExpressionList;
};
class PreParserStatement;
class PreParserStatementList {
public:
PreParserStatementList() : PreParserStatementList(false) {}
PreParserStatementList* operator->() { return this; }
void Add(const PreParserStatement& element, Zone* zone) {}
static PreParserStatementList Null() { return PreParserStatementList(true); }
bool IsNull() const { return is_null_; }
private:
explicit PreParserStatementList(bool is_null) : is_null_(is_null) {}
bool is_null_;
};
class PreParserScopedStatementList {
public:
explicit PreParserScopedStatementList(std::vector<void*>* buffer) {}
void Rewind() {}
void MergeInto(const PreParserScopedStatementList* other) {}
void Add(const PreParserStatement& element) {}
int length() { return 0; }
};
// The pre-parser doesn't need to build lists of expressions, identifiers, or
// the like. If the PreParser is used in variable tracking mode, it needs to
// build lists of variables though.
class PreParserExpressionList {
public:
explicit PreParserExpressionList(std::vector<void*>* buffer) : length_(0) {}
int length() const { return length_; }
void Add(const PreParserExpression& expression) {
++length_;
}
private:
int length_;
friend class PreParser;
friend class PreParserFactory;
};
class PreParserStatement {
public:
static PreParserStatement Default() {
return PreParserStatement(kUnknownStatement);
}
static PreParserStatement Iteration() {
return PreParserStatement(kIterationStatement);
}
static PreParserStatement Null() {
return PreParserStatement(kNullStatement);
}
static PreParserStatement Jump() {
return PreParserStatement(kJumpStatement);
}
void InitializeStatements(const PreParserScopedStatementList& statements,
Zone* zone) {}
// Creates expression statement from expression.
// Preserves being an unparenthesized string literal, possibly
// "use strict".
static PreParserStatement ExpressionStatement(
const PreParserExpression& expression) {
if (expression.IsStringLiteral()) {
return PreParserStatement(kStringLiteralExpressionStatement);
}
return Default();
}
bool IsStringLiteral() { return code_ == kStringLiteralExpressionStatement; }
bool IsJumpStatement() {
return code_ == kJumpStatement;
}
bool IsNull() { return code_ == kNullStatement; }
bool IsIterationStatement() { return code_ == kIterationStatement; }
bool IsEmptyStatement() {
DCHECK(!IsNull());
return false;
}
// Dummy implementation for making statement->somefunc() work in both Parser
// and PreParser.
PreParserStatement* operator->() { return this; }
PreParserStatementList statements() { return PreParserStatementList(); }
PreParserStatementList cases() { return PreParserStatementList(); }
void set_scope(Scope* scope) {}
void Initialize(const PreParserExpression& cond, PreParserStatement body,
const SourceRange& body_range = {}) {}
void Initialize(PreParserStatement init, const PreParserExpression& cond,
PreParserStatement next, PreParserStatement body,
const SourceRange& body_range = {}) {}
void Initialize(PreParserExpression each, const PreParserExpression& subject,
PreParserStatement body, const SourceRange& body_range = {}) {
}
protected:
enum Type {
kNullStatement,
kUnknownStatement,
kJumpStatement,
kIterationStatement,
kStringLiteralExpressionStatement,
};
explicit PreParserStatement(Type code) : code_(code) {}
private:
Type code_;
};
// A PreParserBlock extends statement with a place to store the scope.
// The scope is dropped as the block is returned as a statement.
class PreParserBlock : public PreParserStatement {
public:
void set_scope(Scope* scope) { scope_ = scope; }
Scope* scope() const { return scope_; }
static PreParserBlock Default() {
return PreParserBlock(PreParserStatement::kUnknownStatement);
}
static PreParserBlock Null() {
return PreParserBlock(PreParserStatement::kNullStatement);
}
// Dummy implementation for making block->somefunc() work in both Parser and
// PreParser.
PreParserBlock* operator->() { return this; }
private:
explicit PreParserBlock(PreParserStatement::Type type)
: PreParserStatement(type), scope_(nullptr) {}
Scope* scope_;
};
class PreParserFactory {
public:
explicit PreParserFactory(AstValueFactory* ast_value_factory, Zone* zone)
: ast_node_factory_(ast_value_factory, zone) {}
AstNodeFactory* ast_node_factory() { return &ast_node_factory_; }
PreParserExpression NewStringLiteral(const PreParserIdentifier& identifier,
int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewNumberLiteral(double number,
int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewUndefinedLiteral(int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewTheHoleLiteral() {
return PreParserExpression::Default();
}
PreParserExpression NewRegExpLiteral(const AstRawString* js_pattern,
int js_flags, int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewArrayLiteral(const PreParserExpressionList& values,
int first_spread_index, int pos) {
return PreParserExpression::ArrayLiteral();
}
PreParserExpression NewClassLiteralProperty(const PreParserExpression& key,
const PreParserExpression& value,
ClassLiteralProperty::Kind kind,
bool is_static,
bool is_computed_name,
bool is_private) {
return PreParserExpression::Default();
}
PreParserExpression NewObjectLiteralProperty(const PreParserExpression& key,
const PreParserExpression& value,
ObjectLiteralProperty::Kind kind,
bool is_computed_name) {
return PreParserExpression::Default();
}
PreParserExpression NewObjectLiteralProperty(const PreParserExpression& key,
const PreParserExpression& value,
bool is_computed_name) {
return PreParserExpression::Default();
}
PreParserExpression NewObjectLiteral(
const PreParserExpressionList& properties, int boilerplate_properties,
int pos, bool has_rest_property, Variable* home_object = nullptr) {
return PreParserExpression::ObjectLiteral();
}
PreParserExpression NewVariableProxy(void* variable) {
return PreParserExpression::Default();
}
PreParserExpression NewOptionalChain(const PreParserExpression& expr) {
// Needed to track `delete a?.#b` early errors
if (expr.IsPrivateReference()) {
return PreParserExpression::PrivateReference();
}
return PreParserExpression::Default();
}
PreParserExpression NewProperty(const PreParserExpression& obj,
const PreParserExpression& key, int pos,
bool optional_chain = false) {
if (key.IsIdentifier() && key.AsIdentifier().IsPrivateName()) {
if (obj.IsThis()) {
return PreParserExpression::ThisPrivateReference();
}
return PreParserExpression::PrivateReference();
}
if (obj.IsThis()) {
return PreParserExpression::ThisProperty();
}
return PreParserExpression::Property();
}
PreParserExpression NewUnaryOperation(Token::Value op,
const PreParserExpression& expression,
int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewBinaryOperation(Token::Value op,
const PreParserExpression& left,
const PreParserExpression& right,
int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewCompareOperation(Token::Value op,
const PreParserExpression& left,
const PreParserExpression& right,
int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewAssignment(Token::Value op,
const PreParserExpression& left,
const PreParserExpression& right, int pos) {
// Identifiers need to be tracked since this might be a parameter with a
// default value inside an arrow function parameter list.
return PreParserExpression::Assignment();
}
PreParserExpression NewYield(const PreParserExpression& expression, int pos,
Suspend::OnAbruptResume on_abrupt_resume) {
return PreParserExpression::Default();
}
PreParserExpression NewAwait(const PreParserExpression& expression, int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewYieldStar(const PreParserExpression& iterable,
int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewConditionalChain(size_t initial_size, int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewConditional(const PreParserExpression& condition,
const PreParserExpression& then_expression,
const PreParserExpression& else_expression,
int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewCountOperation(Token::Value op, bool is_prefix,
const PreParserExpression& expression,
int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewCall(PreParserExpression expression,
const PreParserExpressionList& arguments, int pos,
bool has_spread, int eval_scope_info_index = 0,
bool optional_chain = false) {
if (eval_scope_info_index > 0) {
DCHECK(expression.IsIdentifier() && expression.AsIdentifier().IsEval());
DCHECK(!optional_chain);
return PreParserExpression::CallEval();
}
return PreParserExpression::Call();
}
PreParserExpression NewCallNew(const PreParserExpression& expression,
const PreParserExpressionList& arguments,
int pos, bool has_spread) {
return PreParserExpression::Default();
}
PreParserStatement NewReturnStatement(
const PreParserExpression& expression, int pos,
int continuation_pos = kNoSourcePosition) {
return PreParserStatement::Jump();
}
PreParserStatement NewAsyncReturnStatement(
const PreParserExpression& expression, int pos,
int continuation_pos = kNoSourcePosition) {
return PreParserStatement::Jump();
}
PreParserExpression NewFunctionLiteral(
const PreParserIdentifier& name, Scope* scope,
const PreParserScopedStatementList& body, int expected_property_count,
int parameter_count, int function_length,
FunctionLiteral::ParameterFlag has_duplicate_parameters,
FunctionSyntaxKind function_syntax_kind,
FunctionLiteral::EagerCompileHint eager_compile_hint, int position,
bool has_braces, int function_literal_id,
ProducedPreparseData* produced_preparse_data = nullptr) {
DCHECK_NULL(produced_preparse_data);
return PreParserExpression::Default();
}
PreParserExpression NewSpread(const PreParserExpression& expression, int pos,
int expr_pos) {
return PreParserExpression::Default();
}
PreParserExpression NewEmptyParentheses(int pos) {
PreParserExpression result = PreParserExpression::Default();
result.mark_parenthesized();
return result;
}
PreParserStatement EmptyStatement() { return PreParserStatement::Default(); }
PreParserBlock NewBlock(int capacity, bool ignore_completion_value) {
return PreParserBlock::Default();
}
PreParserBlock NewBlock(bool ignore_completion_value, bool is_breakable) {
return PreParserBlock::Default();
}
PreParserBlock NewBlock(bool ignore_completion_value,
const PreParserScopedStatementList& list) {
return PreParserBlock::Default();
}
PreParserStatement NewDebuggerStatement(int pos) {
return PreParserStatement::Default();
}
PreParserStatement NewExpressionStatement(const PreParserExpression& expr,
int pos) {
return PreParserStatement::ExpressionStatement(expr);
}
PreParserStatement NewIfStatement(const PreParserExpression& condition,
PreParserStatement then_statement,
PreParserStatement else_statement, int pos,
SourceRange then_range = {},
SourceRange else_range = {}) {
// This must return a jump statement iff both clauses are jump statements.
return else_statement.IsJumpStatement() ? then_statement : else_statement;
}
PreParserStatement NewBreakStatement(
PreParserStatement target, int pos,
int continuation_pos = kNoSourcePosition) {
return PreParserStatement::Jump();
}
PreParserStatement NewContinueStatement(
PreParserStatement target, int pos,
int continuation_pos = kNoSourcePosition) {
return PreParserStatement::Jump();
}
PreParserStatement NewWithStatement(Scope* scope,
const PreParserExpression& expression,
PreParserStatement statement, int pos) {
return PreParserStatement::Default();
}
PreParserStatement NewDoWhileStatement(int pos) {
return PreParserStatement::Iteration();
}
PreParserStatement NewWhileStatement(int pos) {
return PreParserStatement::Iteration();
}
PreParserStatement NewSwitchStatement(const PreParserExpression& tag,
int pos) {
return PreParserStatement::Default();
}
PreParserStatement NewCaseClause(
const PreParserExpression& label,
const PreParserScopedStatementList& statements) {
return PreParserStatement::Default();
}
PreParserStatement NewForStatement(int pos) {
return PreParserStatement::Iteration();
}
PreParserStatement NewForEachStatement(ForEachStatement::VisitMode visit_mode,
int pos) {
return PreParserStatement::Iteration();
}
PreParserStatement NewForOfStatement(int pos, IteratorType type) {
return PreParserStatement::Iteration();
}
PreParserExpression NewImportCallExpression(const PreParserExpression& args,
const ModuleImportPhase phase,
int pos) {
return PreParserExpression::Default();
}
PreParserExpression NewImportCallExpression(
const PreParserExpression& specifier, const ModuleImportPhase phase,
const PreParserExpression& import_options, int pos) {
return PreParserExpression::Default();
}
private:
// For creating VariableProxy objects to track unresolved variables.
AstNodeFactory ast_node_factory_;
};
class PreParser;
class PreParserFormalParameters : public FormalParametersBase {
public:
explicit PreParserFormalParameters(DeclarationScope* scope)
: FormalParametersBase(scope) {}
void set_has_duplicate() { has_duplicate_ = true; }
bool has_duplicate() { return has_duplicate_; }
void ValidateDuplicate(PreParser* preparser) const;
void set_strict_parameter_error(const Scanner::Location& loc,
MessageTemplate message) {
strict_parameter_error_ = loc.IsValid();
}
void ValidateStrictMode(PreParser* preparser) const;
private:
bool has_duplicate_ = false;
bool strict_parameter_error_ = false;
};
class PreParserFuncNameInferrer {
public:
explicit PreParserFuncNameInferrer(AstValueFactory* avf) {}
PreParserFuncNameInferrer(const PreParserFuncNameInferrer&) = delete;
PreParserFuncNameInferrer& operator=(const PreParserFuncNameInferrer&) =
delete;
void RemoveAsyncKeywordFromEnd() const {}
void Infer() const {}
void RemoveLastFunction() const {}
class State {
public:
explicit State(PreParserFuncNameInferrer* fni) {}
State(const State&) = delete;
State& operator=(const State&) = delete;
};
};
class PreParserSourceRange {
public:
PreParserSourceRange() = default;
PreParserSourceRange(int start, int end) {}
static PreParserSourceRange Empty() { return PreParserSourceRange(); }
static PreParserSourceRange OpenEnded(int32_t start) { return Empty(); }
static const PreParserSourceRange& ContinuationOf(
const PreParserSourceRange& that, int end) {
return that;
}
};
class PreParserSourceRangeScope {
public:
PreParserSourceRangeScope(Scanner* scanner, PreParserSourceRange* range) {}
const PreParserSourceRange& Finalize() const { return range_; }
private:
PreParserSourceRange range_;
DISALLOW_IMPLICIT_CONSTRUCTORS(PreParserSourceRangeScope);
};
class PreParserPropertyList {};
template <>
struct ParserTypes<PreParser> {
using Base = ParserBase<PreParser>;
using Impl = PreParser;
// Return types for traversing functions.
using ClassLiteralProperty = PreParserExpression;
using ClassLiteralStaticElement = PreParserExpression;
using Expression = PreParserExpression;
using FunctionLiteral = PreParserExpression;
using ObjectLiteralProperty = PreParserExpression;
using Suspend = PreParserExpression;
using ExpressionList = PreParserExpressionList;
using ObjectPropertyList = PreParserExpressionList;
using FormalParameters = PreParserFormalParameters;
using Identifier = PreParserIdentifier;
using ClassPropertyList = PreParserPropertyList;
using ClassStaticElementList = PreParserPropertyList;
using StatementList = PreParserScopedStatementList;
using Block = PreParserBlock;
using BreakableStatement = PreParserStatement;
using ForStatement = PreParserStatement;
using IterationStatement = PreParserStatement;
using Statement = PreParserStatement;
// For constructing objects returned by the traversing functions.
using Factory = PreParserFactory;
// Other implementation-specific tasks.
using FuncNameInferrer = PreParserFuncNameInferrer;
using SourceRange = PreParserSourceRange;
using SourceRangeScope = PreParserSourceRangeScope;
};
// Preparsing checks a JavaScript program and emits preparse-data that helps
// a later parsing to be faster.
// See preparse-data-format.h for the data format.
// The PreParser checks that the syntax follows the grammar for JavaScript,
// and collects some information about the program along the way.
// The grammar check is only performed in order to understand the program
// sufficiently to deduce some information about it, that can be used
// to speed up later parsing. Finding errors is not the goal of pre-parsing,
// rather it is to speed up properly written and correct programs.
// That means that contextual checks (like a label being declared where
// it is used) are generally omitted.
class PreParser : public ParserBase<PreParser> {
friend class ParserBase<PreParser>;
public:
using Identifier = PreParserIdentifier;
using Expression = PreParserExpression;
using Statement = PreParserStatement;
enum PreParseResult {
kPreParseStackOverflow,
kPreParseNotIdentifiableError,
kPreParseSuccess
};
PreParser(Zone* zone, Scanner* scanner, uintptr_t stack_limit,
AstValueFactory* ast_value_factory,
PendingCompilationErrorHandler* pending_error_handler,
RuntimeCallStats* runtime_call_stats, V8FileLogger* v8_file_logger,
UnoptimizedCompileFlags flags, bool parsing_on_main_thread = true)
: ParserBase<PreParser>(zone, scanner, stack_limit, ast_value_factory,
pending_error_handler, runtime_call_stats,
v8_file_logger, flags, parsing_on_main_thread),
use_counts_(nullptr),
preparse_data_builder_(nullptr),
preparse_data_builder_buffer_() {
preparse_data_builder_buffer_.reserve(16);
}
static bool IsPreParser() { return true; }
PreParserLogger* logger() { return &log_; }
// Pre-parse the program from the character stream; returns true on
// success (even if parsing failed, the pre-parse data successfully
// captured the syntax error), and false if a stack-overflow happened
// during parsing.
V8_EXPORT_PRIVATE PreParseResult PreParseProgram();
// Parses a single function literal, from the opening parentheses before
// parameters to the closing brace after the body.
// Returns a FunctionEntry describing the body of the function in enough
// detail that it can be lazily compiled.
// The scanner is expected to have matched the "function" or "function*"
// keyword and parameters, and have consumed the initial '{'.
// At return, unless an error occurred, the scanner is positioned before the
// the final '}'.
PreParseResult PreParseFunction(
const AstRawString* function_name, FunctionKind kind,
FunctionSyntaxKind function_syntax_kind, DeclarationScope* function_scope,
int* use_counts, ProducedPreparseData** produced_preparser_scope_data);
PreparseDataBuilder* preparse_data_builder() const {
return preparse_data_builder_;
}
void set_preparse_data_builder(PreparseDataBuilder* preparse_data_builder) {
preparse_data_builder_ = preparse_data_builder;
}
std::vector<void*>* preparse_data_builder_buffer() {
return &preparse_data_builder_buffer_;
}
private:
friend class i::ExpressionScope<ParserTypes<PreParser>>;
friend class i::VariableDeclarationParsingScope<ParserTypes<PreParser>>;
friend class i::ParameterDeclarationParsingScope<ParserTypes<PreParser>>;
friend class i::ArrowHeadParsingScope<ParserTypes<PreParser>>;
friend class PreParserFormalParameters;
// These types form an algebra over syntactic categories that is just
// rich enough to let us recognize and propagate the constructs that
// are either being counted in the preparser data, or is important
// to throw the correct syntax error exceptions.
// All ParseXXX functions take as the last argument an *ok parameter
// which is set to false if parsing failed; it is unchanged otherwise.
// By making the 'exception handling' explicit, we are forced to check
// for failure at the call sites.
// Indicates that we won't switch from the preparser to the preparser; we'll
// just stay where we are.
bool AllowsLazyParsingWithoutUnresolvedVariables() const { return false; }
bool parse_lazily() const { return false; }
PendingCompilationErrorHandler* pending_error_handler() {
return pending_error_handler_;
}
V8_INLINE bool SkipFunction(const AstRawString* name, FunctionKind kind,
FunctionSyntaxKind function_syntax_kind,
DeclarationScope* function_scope,
int* num_parameters, int* function_length,
ProducedPreparseData** produced_preparse_data) {
UNREACHABLE();
}
Expression ParseFunctionLiteral(
Identifier name, Scanner::Location function_name_location,
FunctionNameValidity function_name_validity, FunctionKind kind,
int function_token_pos, FunctionSyntaxKind function_syntax_kind,
LanguageMode language_mode,
ZonePtrList<const AstRawString>* arguments_for_wrapped_function);
PreParserExpression InitializeObjectLiteral(PreParserExpression literal) {
return literal;
}
bool HasCheckedSyntax() { return false; }
void ParseStatementListAndLogFunction(PreParserFormalParameters* formals);
struct TemplateLiteralState {};
V8_INLINE TemplateLiteralState OpenTemplateLiteral(int pos) {
return TemplateLiteralState();
}
V8_INLINE void AddTemplateExpression(TemplateLiteralState* state,
const PreParserExpression& expression) {}
V8_INLINE void AddTemplateSpan(TemplateLiteralState* state, bool should_cook,
bool tail) {}
V8_INLINE PreParserExpression CloseTemplateLiteral(
TemplateLiteralState* state, int start, const PreParserExpression& tag) {
return PreParserExpression::Default();
}
V8_INLINE bool IsPrivateReference(const PreParserExpression& expression) {
return expression.IsPrivateReference();
}
V8_INLINE void SetLanguageMode(Scope* scope, LanguageMode mode) {
scope->SetLanguageMode(mode);
}
V8_INLINE void SetAsmModule() {}
V8_INLINE void PrepareGeneratorVariables() {}
V8_INLINE PreParserStatement
RewriteSwitchStatement(PreParserStatement switch_statement, Scope* scope) {
return PreParserStatement::Default();
}
Variable* DeclareVariable(const AstRawString* name, VariableKind kind,
VariableMode mode, InitializationFlag init,
Scope* scope, bool* was_added, int position) {
return DeclareVariableName(name, mode, scope, was_added, position, kind);
}
void DeclareAndBindVariable(const VariableProxy* proxy, VariableKind kind,
VariableMode mode, Scope* scope, bool* was_added,
int initializer_position) {
Variable* var = DeclareVariableName(proxy->raw_name(), mode, scope,
was_added, proxy->position(), kind);
var->set_initializer_position(initializer_position);
// Don't bother actually binding the proxy.
}
Variable* DeclarePrivateVariableName(const AstRawString* name,
ClassScope* scope, VariableMode mode,
IsStaticFlag is_static_flag,