V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
preparser.h
1 // Copyright 2012 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_PARSING_PREPARSER_H_
6 #define V8_PARSING_PREPARSER_H_
7 
8 #include "src/ast/ast.h"
9 #include "src/ast/scopes.h"
10 #include "src/parsing/parser-base.h"
11 #include "src/parsing/preparser-logger.h"
12 #include "src/pending-compilation-error-handler.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 // Whereas the Parser generates AST during the recursive descent,
18 // the PreParser doesn't create a tree. Instead, it passes around minimal
19 // data objects (PreParserExpression, PreParserIdentifier etc.) which contain
20 // just enough data for the upper layer functions. PreParserFactory is
21 // responsible for creating these dummy objects. It provides a similar kind of
22 // interface as AstNodeFactory, so ParserBase doesn't need to care which one is
23 // used.
24 
25 class PreParsedScopeDataBuilder;
26 
28  public:
29  PreParserIdentifier() : type_(kUnknownIdentifier) {}
30  static PreParserIdentifier Default() {
31  return PreParserIdentifier(kUnknownIdentifier);
32  }
33  static PreParserIdentifier Null() {
34  return PreParserIdentifier(kNullIdentifier);
35  }
36  static PreParserIdentifier Eval() {
37  return PreParserIdentifier(kEvalIdentifier);
38  }
40  return PreParserIdentifier(kArgumentsIdentifier);
41  }
42  static PreParserIdentifier Constructor() {
43  return PreParserIdentifier(kConstructorIdentifier);
44  }
45  static PreParserIdentifier Await() {
46  return PreParserIdentifier(kAwaitIdentifier);
47  }
48  static PreParserIdentifier Async() {
49  return PreParserIdentifier(kAsyncIdentifier);
50  }
51  static PreParserIdentifier Name() {
52  return PreParserIdentifier(kNameIdentifier);
53  }
54  static PreParserIdentifier PrivateName() {
55  return PreParserIdentifier(kPrivateNameIdentifier);
56  }
57  bool IsNull() const { return type_ == kNullIdentifier; }
58  bool IsEval() const { return type_ == kEvalIdentifier; }
59  bool IsAsync() const { return type_ == kAsyncIdentifier; }
60  bool IsArguments() const { return type_ == kArgumentsIdentifier; }
61  bool IsEvalOrArguments() const {
62  STATIC_ASSERT(kEvalIdentifier + 1 == kArgumentsIdentifier);
63  return IsInRange(type_, kEvalIdentifier, kArgumentsIdentifier);
64  }
65  bool IsConstructor() const { return type_ == kConstructorIdentifier; }
66  bool IsAwait() const { return type_ == kAwaitIdentifier; }
67  bool IsName() const { return type_ == kNameIdentifier; }
68  bool IsPrivateName() const { return type_ == kPrivateNameIdentifier; }
69 
70  private:
71  enum Type : uint8_t {
72  kNullIdentifier,
73  kUnknownIdentifier,
74  kEvalIdentifier,
75  kArgumentsIdentifier,
76  kConstructorIdentifier,
77  kAwaitIdentifier,
78  kAsyncIdentifier,
79  kNameIdentifier,
80  kPrivateNameIdentifier
81  };
82 
83  explicit PreParserIdentifier(Type type) : string_(nullptr), type_(type) {}
84  const AstRawString* string_;
85 
86  Type type_;
87  friend class PreParserExpression;
88  friend class PreParser;
89  friend class PreParserFactory;
90 };
91 
93  public:
96 
98  : code_(TypeField::encode(kNull)), variables_(nullptr) {}
99 
100  static PreParserExpression Null() { return PreParserExpression(); }
101  static PreParserExpression Failure() {
102  return PreParserExpression(TypeField::encode(kFailure));
103  }
104 
105  static PreParserExpression Default(
106  VariableZoneThreadedListType* variables = nullptr) {
107  return PreParserExpression(TypeField::encode(kExpression), variables);
108  }
109 
110  static PreParserExpression Spread(const PreParserExpression& expression) {
111  return PreParserExpression(TypeField::encode(kSpreadExpression),
112  expression.variables_);
113  }
114 
115  static PreParserExpression FromIdentifier(const PreParserIdentifier& id,
116  VariableProxy* variable,
117  Zone* zone) {
118  PreParserExpression expression(TypeField::encode(kIdentifierExpression) |
119  IdentifierTypeField::encode(id.type_));
120  expression.AddVariable(variable, zone);
121  return expression;
122  }
123 
125  Token::Value op,
126  const PreParserExpression& right,
127  Zone* zone) {
128  return PreParserExpression(TypeField::encode(kExpression));
129  }
130 
132  VariableZoneThreadedListType* variables) {
133  return PreParserExpression(TypeField::encode(kExpression) |
134  ExpressionTypeField::encode(kAssignment),
135  variables);
136  }
137 
138  static PreParserExpression NewTargetExpression() {
139  return PreParserExpression::Default();
140  }
141 
143  VariableZoneThreadedListType* variables) {
144  return PreParserExpression(TypeField::encode(kObjectLiteralExpression),
145  variables);
146  }
147 
149  VariableZoneThreadedListType* variables) {
150  return PreParserExpression(TypeField::encode(kArrayLiteralExpression),
151  variables);
152  }
153 
155  return PreParserExpression(TypeField::encode(kStringLiteralExpression));
156  }
157 
158  static PreParserExpression This() {
159  return PreParserExpression(TypeField::encode(kExpression) |
160  ExpressionTypeField::encode(kThisExpression));
161  }
162 
163  static PreParserExpression ThisPropertyWithPrivateFieldKey() {
164  return PreParserExpression(TypeField::encode(kExpression) |
165  ExpressionTypeField::encode(
166  kThisPropertyExpressionWithPrivateFieldKey));
167  }
168 
169  static PreParserExpression ThisProperty() {
170  return PreParserExpression(
171  TypeField::encode(kExpression) |
172  ExpressionTypeField::encode(kThisPropertyExpression));
173  }
174 
175  static PreParserExpression Property() {
176  return PreParserExpression(
177  TypeField::encode(kExpression) |
178  ExpressionTypeField::encode(kPropertyExpression));
179  }
180 
181  static PreParserExpression PropertyWithPrivateFieldKey() {
182  return PreParserExpression(
183  TypeField::encode(kExpression) |
184  ExpressionTypeField::encode(kPropertyExpressionWithPrivateFieldKey));
185  }
186 
187  static PreParserExpression Call() {
188  return PreParserExpression(TypeField::encode(kExpression) |
189  ExpressionTypeField::encode(kCallExpression));
190  }
191 
192  static PreParserExpression CallEval() {
193  return PreParserExpression(
194  TypeField::encode(kExpression) |
195  ExpressionTypeField::encode(kCallEvalExpression));
196  }
197 
198  static PreParserExpression CallTaggedTemplate() {
199  return PreParserExpression(
200  TypeField::encode(kExpression) |
201  ExpressionTypeField::encode(kCallTaggedTemplateExpression));
202  }
203 
204  bool is_tagged_template() const {
205  DCHECK(IsCall());
206  return ExpressionTypeField::decode(code_) == kCallTaggedTemplateExpression;
207  }
208 
210  return PreParserExpression(
211  TypeField::encode(kExpression) |
212  ExpressionTypeField::encode(kSuperCallReference));
213  }
214 
215  bool IsNull() const { return TypeField::decode(code_) == kNull; }
216  bool IsFailureExpression() const {
217  return TypeField::decode(code_) == kFailure;
218  }
219 
220  bool IsIdentifier() const {
221  return TypeField::decode(code_) == kIdentifierExpression;
222  }
223 
224  PreParserIdentifier AsIdentifier() const {
225  DCHECK(IsIdentifier());
226  return PreParserIdentifier(IdentifierTypeField::decode(code_));
227  }
228 
229  bool IsAssignment() const {
230  return TypeField::decode(code_) == kExpression &&
231  ExpressionTypeField::decode(code_) == kAssignment;
232  }
233 
234  bool IsObjectLiteral() const {
235  return TypeField::decode(code_) == kObjectLiteralExpression;
236  }
237 
238  bool IsArrayLiteral() const {
239  return TypeField::decode(code_) == kArrayLiteralExpression;
240  }
241 
242  bool IsPattern() const {
243  STATIC_ASSERT(kObjectLiteralExpression + 1 == kArrayLiteralExpression);
244  return IsInRange(TypeField::decode(code_), kObjectLiteralExpression,
245  kArrayLiteralExpression);
246  }
247 
248  bool IsStringLiteral() const {
249  return TypeField::decode(code_) == kStringLiteralExpression;
250  }
251 
252  bool IsThis() const {
253  return TypeField::decode(code_) == kExpression &&
254  ExpressionTypeField::decode(code_) == kThisExpression;
255  }
256 
257  bool IsThisProperty() const {
258  return TypeField::decode(code_) == kExpression &&
259  (ExpressionTypeField::decode(code_) == kThisPropertyExpression ||
260  ExpressionTypeField::decode(code_) ==
261  kThisPropertyExpressionWithPrivateFieldKey);
262  }
263 
264  bool IsProperty() const {
265  return TypeField::decode(code_) == kExpression &&
266  (ExpressionTypeField::decode(code_) == kPropertyExpression ||
267  ExpressionTypeField::decode(code_) == kThisPropertyExpression ||
268  ExpressionTypeField::decode(code_) ==
269  kPropertyExpressionWithPrivateFieldKey ||
270  ExpressionTypeField::decode(code_) ==
271  kThisPropertyExpressionWithPrivateFieldKey);
272  }
273 
274  bool IsPropertyWithPrivateFieldKey() const {
275  return TypeField::decode(code_) == kExpression &&
276  (ExpressionTypeField::decode(code_) ==
277  kPropertyExpressionWithPrivateFieldKey ||
278  ExpressionTypeField::decode(code_) ==
279  kThisPropertyExpressionWithPrivateFieldKey);
280  }
281 
282  bool IsCall() const {
283  return TypeField::decode(code_) == kExpression &&
284  (ExpressionTypeField::decode(code_) == kCallExpression ||
285  ExpressionTypeField::decode(code_) == kCallEvalExpression ||
286  ExpressionTypeField::decode(code_) ==
287  kCallTaggedTemplateExpression);
288  }
289  PreParserExpression* AsCall() {
290  if (IsCall()) return this;
291  return nullptr;
292  }
293 
294  bool IsSuperCallReference() const {
295  return TypeField::decode(code_) == kExpression &&
296  ExpressionTypeField::decode(code_) == kSuperCallReference;
297  }
298 
299  bool IsValidReferenceExpression() const {
300  return IsIdentifier() || IsProperty();
301  }
302 
303  // At the moment PreParser doesn't track these expression types.
304  bool IsFunctionLiteral() const { return false; }
305  bool IsCallNew() const { return false; }
306 
307  bool IsSpread() const {
308  return TypeField::decode(code_) == kSpreadExpression;
309  }
310 
311  bool is_parenthesized() const { return IsParenthesizedField::decode(code_); }
312 
313  void mark_parenthesized() {
314  code_ = IsParenthesizedField::update(code_, true);
315  }
316 
317  PreParserExpression AsFunctionLiteral() { return *this; }
318 
319  // Dummy implementation for making expression->somefunc() work in both Parser
320  // and PreParser.
321  PreParserExpression* operator->() { return this; }
322 
323  void set_is_private_name() {
324  if (variables_ != nullptr) {
325  DCHECK(IsIdentifier());
326  DCHECK(AsIdentifier().IsPrivateName());
327  DCHECK_EQ(1, variables_->LengthForTest());
328  variables_->first()->set_is_private_name();
329  }
330  }
331 
332  // More dummy implementations of things PreParser doesn't need to track:
333  void SetShouldEagerCompile() {}
334  void mark_as_iife() {}
335 
336  int position() const { return kNoSourcePosition; }
337  void set_function_token_position(int position) {}
338  void set_scope(Scope* scope) {}
339  void set_suspend_count(int suspend_count) {}
340 
341  private:
342  enum Type {
343  kNull,
344  kFailure,
345  kExpression,
346  kIdentifierExpression,
347  kStringLiteralExpression,
348  kSpreadExpression,
349  kObjectLiteralExpression,
350  kArrayLiteralExpression
351  };
352 
353  enum ExpressionType {
354  kThisExpression,
355  kThisPropertyExpression,
356  kThisPropertyExpressionWithPrivateFieldKey,
357  kPropertyExpression,
358  kPropertyExpressionWithPrivateFieldKey,
359  kCallExpression,
360  kCallEvalExpression,
361  kCallTaggedTemplateExpression,
362  kSuperCallReference,
363  kAssignment
364  };
365 
366  explicit PreParserExpression(
367  uint32_t expression_code,
368  VariableZoneThreadedListType* variables = nullptr)
369  : code_(expression_code), variables_(variables) {}
370 
371  void AddVariable(VariableProxy* variable, Zone* zone) {
372  if (variable == nullptr) {
373  return;
374  }
375  if (variables_ == nullptr) {
376  variables_ = new (zone) VariableZoneThreadedListType();
377  }
378  variables_->Add(variable);
379  }
380 
381  // The first three bits are for the Type.
383 
384  // The high order bit applies only to nodes which would inherit from the
385  // Expression ASTNode --- This is by necessity, due to the fact that
386  // Expression nodes may be represented as multiple Types, not exclusively
387  // through kExpression.
388  // TODO(caitp, adamk): clean up PreParserExpression bitfields.
390 
391  // The rest of the bits are interpreted depending on the value
392  // of the Type field, so they can share the storage.
399 
400  uint32_t code_;
401  // If the PreParser is used in the variable tracking mode, PreParserExpression
402  // accumulates variables in that expression.
403  VariableZoneThreadedListType* variables_;
404 
405  friend class PreParser;
406  friend class PreParserFactory;
407  friend class PreParserExpressionList;
408 };
409 
410 class PreParserStatement;
412  public:
414  PreParserStatementList* operator->() { return this; }
415  void Add(const PreParserStatement& element, Zone* zone) {}
416  static PreParserStatementList Null() { return PreParserStatementList(true); }
417  bool IsNull() const { return is_null_; }
418 
419  private:
420  explicit PreParserStatementList(bool is_null) : is_null_(is_null) {}
421  bool is_null_;
422 };
423 
425  public:
426  explicit PreParserScopedStatementList(std::vector<void*>* buffer) {}
427  void Rewind() {}
428  void Add(const PreParserStatement& element) {}
429 };
430 
431 // The pre-parser doesn't need to build lists of expressions, identifiers, or
432 // the like. If the PreParser is used in variable tracking mode, it needs to
433 // build lists of variables though.
437 
438  public:
439  explicit PreParserExpressionList(std::vector<void*>* buffer)
440  : length_(0), variables_(nullptr) {}
441 
442  int length() const { return length_; }
443 
444  void Add(const PreParserExpression& expression) {
445  if (expression.variables_ != nullptr) {
446  if (variables_ == nullptr) {
447  variables_ = expression.variables_;
448  } else {
449  variables_->Append(std::move(*expression.variables_));
450  }
451  }
452  ++length_;
453  }
454 
455  private:
456  int length_;
457  VariableZoneThreadedListType* variables_;
458 
459  friend class PreParser;
460  friend class PreParserFactory;
461 };
462 
464  public:
465  static PreParserStatement Default() {
466  return PreParserStatement(kUnknownStatement);
467  }
468 
469  static PreParserStatement Null() {
470  return PreParserStatement(kNullStatement);
471  }
472 
473  static PreParserStatement Empty() {
474  return PreParserStatement(kEmptyStatement);
475  }
476 
477  static PreParserStatement Jump() {
478  return PreParserStatement(kJumpStatement);
479  }
480 
481  void InitializeStatements(const PreParserScopedStatementList& statements,
482  Zone* zone) {}
483 
484  // Creates expression statement from expression.
485  // Preserves being an unparenthesized string literal, possibly
486  // "use strict".
488  const PreParserExpression& expression) {
489  if (expression.IsStringLiteral()) {
490  return PreParserStatement(kStringLiteralExpressionStatement);
491  }
492  return Default();
493  }
494 
495  bool IsStringLiteral() { return code_ == kStringLiteralExpressionStatement; }
496 
497  bool IsJumpStatement() {
498  return code_ == kJumpStatement;
499  }
500 
501  bool IsNull() { return code_ == kNullStatement; }
502 
503  bool IsEmptyStatement() {
504  DCHECK(!IsNull());
505  return code_ == kEmptyStatement;
506  }
507 
508  // Dummy implementation for making statement->somefunc() work in both Parser
509  // and PreParser.
510  PreParserStatement* operator->() { return this; }
511 
512  PreParserStatementList statements() { return PreParserStatementList(); }
513  PreParserStatementList cases() { return PreParserStatementList(); }
514 
515  void set_scope(Scope* scope) {}
516  void Initialize(const PreParserExpression& cond, PreParserStatement body,
517  const SourceRange& body_range = {}) {}
518  void Initialize(PreParserStatement init, const PreParserExpression& cond,
520  const SourceRange& body_range = {}) {}
521 
522  private:
523  enum Type {
524  kNullStatement,
525  kEmptyStatement,
526  kUnknownStatement,
527  kJumpStatement,
528  kStringLiteralExpressionStatement,
529  };
530 
531  explicit PreParserStatement(Type code) : code_(code) {}
532  Type code_;
533 };
534 
535 
537  public:
538  explicit PreParserFactory(AstValueFactory* ast_value_factory, Zone* zone)
539  : ast_node_factory_(ast_value_factory, zone), zone_(zone) {}
540 
541  AstNodeFactory* ast_node_factory() { return &ast_node_factory_; }
542 
543  PreParserExpression NewStringLiteral(const PreParserIdentifier& identifier,
544  int pos) {
545  // This is needed for object literal property names. Property names are
546  // normalized to string literals during object literal parsing.
547  PreParserExpression expression = PreParserExpression::Default();
548  if (identifier.string_ != nullptr) {
549  VariableProxy* variable = ast_node_factory_.NewVariableProxy(
550  identifier.string_, NORMAL_VARIABLE);
551  expression.AddVariable(variable, zone_);
552  }
553  return expression;
554  }
555  PreParserExpression NewNumberLiteral(double number,
556  int pos) {
557  return PreParserExpression::Default();
558  }
559  PreParserExpression NewUndefinedLiteral(int pos) {
560  return PreParserExpression::Default();
561  }
562  PreParserExpression NewTheHoleLiteral() {
563  return PreParserExpression::Default();
564  }
565  PreParserExpression NewRegExpLiteral(const PreParserIdentifier& js_pattern,
566  int js_flags, int pos) {
567  return PreParserExpression::Default();
568  }
569  PreParserExpression NewArrayLiteral(const PreParserExpressionList& values,
570  int first_spread_index, int pos) {
571  return PreParserExpression::ArrayLiteral(values.variables_);
572  }
573  PreParserExpression NewClassLiteralProperty(const PreParserExpression& key,
574  const PreParserExpression& value,
575  ClassLiteralProperty::Kind kind,
576  bool is_static,
577  bool is_computed_name,
578  bool is_private) {
579  return PreParserExpression::Default();
580  }
581  PreParserExpression NewObjectLiteralProperty(const PreParserExpression& key,
582  const PreParserExpression& value,
583  ObjectLiteralProperty::Kind kind,
584  bool is_computed_name) {
585  return PreParserExpression::Default(value.variables_);
586  }
587  PreParserExpression NewObjectLiteralProperty(const PreParserExpression& key,
588  const PreParserExpression& value,
589  bool is_computed_name) {
590  return PreParserExpression::Default(value.variables_);
591  }
592  PreParserExpression NewObjectLiteral(
593  const PreParserExpressionList& properties, int boilerplate_properties,
594  int pos, bool has_rest_property) {
595  return PreParserExpression::ObjectLiteral(properties.variables_);
596  }
597  PreParserExpression NewVariableProxy(void* variable) {
598  return PreParserExpression::Default();
599  }
600 
601  PreParserExpression NewProperty(const PreParserExpression& obj,
602  const PreParserExpression& key, int pos) {
603  if (key.IsIdentifier() && key.AsIdentifier().IsPrivateName()) {
604  if (obj.IsThis()) {
605  return PreParserExpression::ThisPropertyWithPrivateFieldKey();
606  }
607  return PreParserExpression::PropertyWithPrivateFieldKey();
608  }
609 
610  if (obj.IsThis()) {
611  return PreParserExpression::ThisProperty();
612  }
613  return PreParserExpression::Property();
614  }
615  PreParserExpression NewUnaryOperation(Token::Value op,
616  const PreParserExpression& expression,
617  int pos) {
618  return PreParserExpression::Default();
619  }
620  PreParserExpression NewBinaryOperation(Token::Value op,
621  const PreParserExpression& left,
622  const PreParserExpression& right,
623  int pos) {
624  return PreParserExpression::BinaryOperation(left, op, right, zone_);
625  }
626  PreParserExpression NewCompareOperation(Token::Value op,
627  const PreParserExpression& left,
628  const PreParserExpression& right,
629  int pos) {
630  return PreParserExpression::Default();
631  }
632  PreParserExpression NewRewritableExpression(
633  const PreParserExpression& expression, Scope* scope) {
634  return expression;
635  }
636  PreParserExpression NewAssignment(Token::Value op,
637  const PreParserExpression& left,
638  const PreParserExpression& right, int pos) {
639  // Identifiers need to be tracked since this might be a parameter with a
640  // default value inside an arrow function parameter list.
641  return PreParserExpression::Assignment(left.variables_);
642  }
643  PreParserExpression NewYield(const PreParserExpression& expression, int pos,
644  Suspend::OnAbruptResume on_abrupt_resume) {
645  return PreParserExpression::Default();
646  }
647  PreParserExpression NewAwait(const PreParserExpression& expression, int pos) {
648  return PreParserExpression::Default();
649  }
650  PreParserExpression NewYieldStar(const PreParserExpression& iterable,
651  int pos) {
652  return PreParserExpression::Default();
653  }
654  PreParserExpression NewConditional(const PreParserExpression& condition,
655  const PreParserExpression& then_expression,
656  const PreParserExpression& else_expression,
657  int pos) {
658  return PreParserExpression::Default();
659  }
660  PreParserExpression NewCountOperation(Token::Value op, bool is_prefix,
661  const PreParserExpression& expression,
662  int pos) {
663  return PreParserExpression::Default();
664  }
665  PreParserExpression NewCall(
666  PreParserExpression expression, const PreParserExpressionList& arguments,
667  int pos, Call::PossiblyEval possibly_eval = Call::NOT_EVAL) {
668  if (possibly_eval == Call::IS_POSSIBLY_EVAL) {
669  DCHECK(expression.IsIdentifier() && expression.AsIdentifier().IsEval());
670  return PreParserExpression::CallEval();
671  }
672  return PreParserExpression::Call();
673  }
674  PreParserExpression NewTaggedTemplate(
675  PreParserExpression expression, const PreParserExpressionList& arguments,
676  int pos) {
677  return PreParserExpression::CallTaggedTemplate();
678  }
679  PreParserExpression NewCallNew(const PreParserExpression& expression,
680  const PreParserExpressionList& arguments,
681  int pos) {
682  return PreParserExpression::Default();
683  }
684  PreParserStatement NewReturnStatement(
685  const PreParserExpression& expression, int pos,
686  int continuation_pos = kNoSourcePosition) {
687  return PreParserStatement::Jump();
688  }
689  PreParserStatement NewAsyncReturnStatement(
690  const PreParserExpression& expression, int pos,
691  int continuation_pos = kNoSourcePosition) {
692  return PreParserStatement::Jump();
693  }
694  PreParserExpression NewFunctionLiteral(
695  const PreParserIdentifier& name, Scope* scope,
696  const PreParserScopedStatementList& body, int expected_property_count,
697  int parameter_count, int function_length,
698  FunctionLiteral::ParameterFlag has_duplicate_parameters,
699  FunctionLiteral::FunctionType function_type,
700  FunctionLiteral::EagerCompileHint eager_compile_hint, int position,
701  bool has_braces, int function_literal_id,
702  ProducedPreParsedScopeData* produced_preparsed_scope_data = nullptr) {
703  DCHECK_NULL(produced_preparsed_scope_data);
704  return PreParserExpression::Default();
705  }
706 
707  PreParserExpression NewSpread(const PreParserExpression& expression, int pos,
708  int expr_pos) {
709  return PreParserExpression::Spread(expression);
710  }
711 
712  PreParserExpression NewEmptyParentheses(int pos) {
713  PreParserExpression result = PreParserExpression::Default();
714  result.mark_parenthesized();
715  return result;
716  }
717 
718  PreParserStatement EmptyStatement() { return PreParserStatement::Default(); }
719 
720  PreParserStatement NewBlock(int capacity, bool ignore_completion_value) {
721  return PreParserStatement::Default();
722  }
723 
724  PreParserStatement NewBlock(bool ignore_completion_value,
726  return PreParserStatement::Default();
727  }
728 
729  PreParserStatement NewBlock(bool ignore_completion_value,
730  const PreParserScopedStatementList& list) {
731  return PreParserStatement::Default();
732  }
733 
734  PreParserStatement NewDebuggerStatement(int pos) {
735  return PreParserStatement::Default();
736  }
737 
738  PreParserStatement NewExpressionStatement(const PreParserExpression& expr,
739  int pos) {
740  return PreParserStatement::ExpressionStatement(expr);
741  }
742 
743  PreParserStatement NewIfStatement(const PreParserExpression& condition,
744  PreParserStatement then_statement,
745  PreParserStatement else_statement, int pos,
746  SourceRange then_range = {},
747  SourceRange else_range = {}) {
748  // This must return a jump statement iff both clauses are jump statements.
749  return else_statement.IsJumpStatement() ? then_statement : else_statement;
750  }
751 
752  PreParserStatement NewBreakStatement(
753  PreParserStatement target, int pos,
754  int continuation_pos = kNoSourcePosition) {
755  return PreParserStatement::Jump();
756  }
757 
758  PreParserStatement NewContinueStatement(
759  PreParserStatement target, int pos,
760  int continuation_pos = kNoSourcePosition) {
761  return PreParserStatement::Jump();
762  }
763 
764  PreParserStatement NewWithStatement(Scope* scope,
765  const PreParserExpression& expression,
766  PreParserStatement statement, int pos) {
767  return PreParserStatement::Default();
768  }
769 
770  PreParserStatement NewDoWhileStatement(
772  ZonePtrList<const AstRawString>* own_labels, int pos) {
773  return PreParserStatement::Default();
774  }
775 
776  PreParserStatement NewWhileStatement(
778  ZonePtrList<const AstRawString>* own_labels, int pos) {
779  return PreParserStatement::Default();
780  }
781 
782  PreParserStatement NewSwitchStatement(ZonePtrList<const AstRawString>* labels,
783  const PreParserExpression& tag,
784  int pos) {
785  return PreParserStatement::Default();
786  }
787 
788  PreParserStatement NewCaseClause(
789  const PreParserExpression& label,
790  const PreParserScopedStatementList& statements) {
791  return PreParserStatement::Default();
792  }
793 
794  PreParserStatement NewForStatement(
796  ZonePtrList<const AstRawString>* own_labels, int pos) {
797  return PreParserStatement::Default();
798  }
799 
800  PreParserStatement NewForEachStatement(
801  ForEachStatement::VisitMode visit_mode,
803  ZonePtrList<const AstRawString>* own_labels, int pos) {
804  return PreParserStatement::Default();
805  }
806 
807  PreParserStatement NewForOfStatement(
809  ZonePtrList<const AstRawString>* own_labels, int pos) {
810  return PreParserStatement::Default();
811  }
812 
813  PreParserExpression NewCallRuntime(
814  Runtime::FunctionId id, ZoneChunkList<PreParserExpression>* arguments,
815  int pos) {
816  return PreParserExpression::Default();
817  }
818 
819  PreParserExpression NewImportCallExpression(const PreParserExpression& args,
820  int pos) {
821  return PreParserExpression::Default();
822  }
823 
824  private:
825  // For creating VariableProxy objects to track unresolved variables.
826  AstNodeFactory ast_node_factory_;
827  Zone* zone_;
828 };
829 
831  public:
833  : FormalParametersBase(scope) {}
834 
835  Scanner::Location duplicate_location() const { UNREACHABLE(); }
836  bool has_duplicate() const { return has_duplicate_; }
837  void set_has_duplicate() { has_duplicate_ = true; }
838 
839  private:
840  bool has_duplicate_ = false;
841 };
842 
843 class PreParser;
844 
846  public:
848  PreParserStatement statement) {}
849 };
850 
852  public:
853  explicit PreParserTargetScope(ParserBase<PreParser>* preparser) {}
854 };
855 
857  public:
859  void RemoveAsyncKeywordFromEnd() const {}
860  void Infer() const {}
861  void RemoveLastFunction() const {}
862 
863  class State {
864  public:
865  explicit State(PreParserFuncNameInferrer* fni) {}
866 
867  private:
868  DISALLOW_COPY_AND_ASSIGN(State);
869  };
870 
871  private:
872  DISALLOW_COPY_AND_ASSIGN(PreParserFuncNameInferrer);
873 };
874 
876  public:
878  PreParserSourceRange(int start, int end) {}
879  static PreParserSourceRange Empty() { return PreParserSourceRange(); }
880  static PreParserSourceRange OpenEnded(int32_t start) { return Empty(); }
881  static const PreParserSourceRange& ContinuationOf(
882  const PreParserSourceRange& that, int end) {
883  return that;
884  }
885 };
886 
888  public:
890  const PreParserSourceRange& Finalize() const { return range_; }
891 
892  private:
893  PreParserSourceRange range_;
894 
895  DISALLOW_IMPLICIT_CONSTRUCTORS(PreParserSourceRangeScope);
896 };
897 
899 
900 template <>
902  typedef ParserBase<PreParser> Base;
903  typedef PreParser Impl;
904 
905  // Return types for traversing functions.
918  typedef PreParserStatement Block;
923 
924  // For constructing objects returned by the traversing functions.
925  typedef PreParserFactory Factory;
926 
927  // Other implementation-specific tasks.
931  typedef PreParserTarget Target;
933 
934  static constexpr bool ExpressionClassifierReportErrors = false;
935 };
936 
937 
938 // Preparsing checks a JavaScript program and emits preparse-data that helps
939 // a later parsing to be faster.
940 // See preparse-data-format.h for the data format.
941 
942 // The PreParser checks that the syntax follows the grammar for JavaScript,
943 // and collects some information about the program along the way.
944 // The grammar check is only performed in order to understand the program
945 // sufficiently to deduce some information about it, that can be used
946 // to speed up later parsing. Finding errors is not the goal of pre-parsing,
947 // rather it is to speed up properly written and correct programs.
948 // That means that contextual checks (like a label being declared where
949 // it is used) are generally omitted.
950 class PreParser : public ParserBase<PreParser> {
951  friend class ParserBase<PreParser>;
953 
954  public:
958 
959  enum PreParseResult {
960  kPreParseStackOverflow,
961  kPreParseAbort,
962  kPreParseNotIdentifiableError,
963  kPreParseSuccess
964  };
965 
966  PreParser(Zone* zone, Scanner* scanner, uintptr_t stack_limit,
967  AstValueFactory* ast_value_factory,
968  PendingCompilationErrorHandler* pending_error_handler,
969  RuntimeCallStats* runtime_call_stats, Logger* logger,
970  int script_id = -1, bool parsing_module = false,
971  bool parsing_on_main_thread = true)
972  : ParserBase<PreParser>(zone, scanner, stack_limit, nullptr,
973  ast_value_factory, pending_error_handler,
974  runtime_call_stats, logger, script_id,
975  parsing_module, parsing_on_main_thread),
976  use_counts_(nullptr),
977  preparsed_scope_data_builder_(nullptr) {}
978 
979  static bool IsPreParser() { return true; }
980 
981  PreParserLogger* logger() { return &log_; }
982 
983  // Pre-parse the program from the character stream; returns true on
984  // success (even if parsing failed, the pre-parse data successfully
985  // captured the syntax error), and false if a stack-overflow happened
986  // during parsing.
987  PreParseResult PreParseProgram();
988 
989  // Parses a single function literal, from the opening parentheses before
990  // parameters to the closing brace after the body.
991  // Returns a FunctionEntry describing the body of the function in enough
992  // detail that it can be lazily compiled.
993  // The scanner is expected to have matched the "function" or "function*"
994  // keyword and parameters, and have consumed the initial '{'.
995  // At return, unless an error occurred, the scanner is positioned before the
996  // the final '}'.
997  PreParseResult PreParseFunction(
998  const AstRawString* function_name, FunctionKind kind,
999  FunctionLiteral::FunctionType function_type,
1000  DeclarationScope* function_scope, bool may_abort, int* use_counts,
1001  ProducedPreParsedScopeData** produced_preparser_scope_data,
1002  int script_id);
1003 
1004  PreParsedScopeDataBuilder* preparsed_scope_data_builder() const {
1005  return preparsed_scope_data_builder_;
1006  }
1007 
1008  void set_preparsed_scope_data_builder(
1009  PreParsedScopeDataBuilder* preparsed_scope_data_builder) {
1010  preparsed_scope_data_builder_ = preparsed_scope_data_builder;
1011  }
1012 
1013  private:
1014  // These types form an algebra over syntactic categories that is just
1015  // rich enough to let us recognize and propagate the constructs that
1016  // are either being counted in the preparser data, or is important
1017  // to throw the correct syntax error exceptions.
1018 
1019  // All ParseXXX functions take as the last argument an *ok parameter
1020  // which is set to false if parsing failed; it is unchanged otherwise.
1021  // By making the 'exception handling' explicit, we are forced to check
1022  // for failure at the call sites.
1023 
1024  // Indicates that we won't switch from the preparser to the preparser; we'll
1025  // just stay where we are.
1026  bool AllowsLazyParsingWithoutUnresolvedVariables() const { return false; }
1027  bool parse_lazily() const { return false; }
1028 
1029  PendingCompilationErrorHandler* pending_error_handler() {
1030  return pending_error_handler_;
1031  }
1032 
1033  V8_INLINE bool SkipFunction(
1034  const AstRawString* name, FunctionKind kind,
1035  FunctionLiteral::FunctionType function_type,
1036  DeclarationScope* function_scope, int* num_parameters,
1037  ProducedPreParsedScopeData** produced_preparsed_scope_data,
1038  bool may_abort, FunctionLiteral::EagerCompileHint* hint) {
1039  UNREACHABLE();
1040  }
1041 
1042  Expression ParseFunctionLiteral(
1043  Identifier name, Scanner::Location function_name_location,
1044  FunctionNameValidity function_name_validity, FunctionKind kind,
1045  int function_token_pos, FunctionLiteral::FunctionType function_type,
1046  LanguageMode language_mode,
1047  ZonePtrList<const AstRawString>* arguments_for_wrapped_function);
1048 
1049  PreParserExpression InitializeObjectLiteral(PreParserExpression literal) {
1050  return literal;
1051  }
1052 
1053  LazyParsingResult ParseStatementListAndLogFunction(
1054  PreParserFormalParameters* formals, bool maybe_abort);
1055 
1056  struct TemplateLiteralState {};
1057 
1058  V8_INLINE TemplateLiteralState OpenTemplateLiteral(int pos) {
1059  return TemplateLiteralState();
1060  }
1061  V8_INLINE void AddTemplateExpression(TemplateLiteralState* state,
1062  const PreParserExpression& expression) {}
1063  V8_INLINE void AddTemplateSpan(TemplateLiteralState* state, bool should_cook,
1064  bool tail) {}
1065  V8_INLINE PreParserExpression CloseTemplateLiteral(
1066  TemplateLiteralState* state, int start, const PreParserExpression& tag) {
1067  return PreParserExpression::Default();
1068  }
1069  V8_INLINE bool IsPropertyWithPrivateFieldKey(
1070  const PreParserExpression& expression) {
1071  return expression.IsPropertyWithPrivateFieldKey();
1072  }
1073  V8_INLINE void CheckConflictingVarDeclarations(Scope* scope) {}
1074 
1075  V8_INLINE void SetLanguageMode(Scope* scope, LanguageMode mode) {
1076  scope->SetLanguageMode(mode);
1077  }
1078  V8_INLINE void SetAsmModule() {}
1079 
1080  V8_INLINE PreParserExpression SpreadCall(const PreParserExpression& function,
1081  const PreParserExpressionList& args,
1082  int pos,
1083  Call::PossiblyEval possibly_eval);
1084  V8_INLINE PreParserExpression
1085  SpreadCallNew(const PreParserExpression& function,
1086  const PreParserExpressionList& args, int pos);
1087 
1088  V8_INLINE void RewriteDestructuringAssignments() {}
1089 
1090  V8_INLINE void PrepareGeneratorVariables() {}
1091  V8_INLINE void RewriteAsyncFunctionBody(
1093  const PreParserExpression& return_value) {}
1094 
1095  void DeclareAndInitializeVariables(
1096  PreParserStatement block,
1097  const DeclarationDescriptor* declaration_descriptor,
1098  const DeclarationParsingResult::Declaration* declaration,
1100 
1101  V8_INLINE void DeclareLabel(ZonePtrList<const AstRawString>** labels,
1102  ZonePtrList<const AstRawString>** own_labels,
1103  const PreParserExpression& expr) {
1104  DCHECK(!parsing_module_ || !expr.AsIdentifier().IsAwait());
1105  DCHECK(IsIdentifier(expr));
1106  }
1107 
1108  // TODO(nikolaos): The preparser currently does not keep track of labels.
1109  V8_INLINE bool ContainsLabel(ZonePtrList<const AstRawString>* labels,
1110  const PreParserIdentifier& label) {
1111  return false;
1112  }
1113 
1114  V8_INLINE PreParserExpression
1115  RewriteReturn(const PreParserExpression& return_value, int pos) {
1116  return return_value;
1117  }
1118  V8_INLINE PreParserStatement
1119  RewriteSwitchStatement(PreParserStatement switch_statement, Scope* scope) {
1120  return PreParserStatement::Default();
1121  }
1122 
1123  V8_INLINE void RewriteCatchPattern(CatchInfo* catch_info) {
1124  const AstRawString* catch_name = catch_info->name.string_;
1125  if (catch_name == nullptr) {
1126  catch_name = ast_value_factory()->dot_catch_string();
1127  }
1128  catch_info->scope->DeclareCatchVariableName(catch_name);
1129 
1130  if (catch_info->pattern.variables_ != nullptr) {
1131  for (auto variable : *catch_info->pattern.variables_) {
1132  scope()->DeclareVariableName(variable->raw_name(), VariableMode::kLet);
1133  }
1134  }
1135  }
1136 
1137  V8_INLINE void ValidateCatchBlock(const CatchInfo& catch_info) {}
1138  V8_INLINE PreParserStatement RewriteTryStatement(
1139  PreParserStatement try_block, PreParserStatement catch_block,
1140  const SourceRange& catch_range, PreParserStatement finally_block,
1141  const SourceRange& finally_range, const CatchInfo& catch_info, int pos) {
1142  return PreParserStatement::Default();
1143  }
1144 
1145  V8_INLINE void GetUnexpectedTokenMessage(Token::Value token,
1146  MessageTemplate* message,
1147  Scanner::Location* location,
1148  const char** arg) {}
1149  V8_INLINE void ParseAndRewriteGeneratorFunctionBody(
1150  int pos, FunctionKind kind, PreParserScopedStatementList* body) {
1151  ParseStatementList(body, Token::RBRACE);
1152  }
1153  V8_INLINE void ParseAndRewriteAsyncGeneratorFunctionBody(
1154  int pos, FunctionKind kind, PreParserScopedStatementList* body) {
1155  ParseStatementList(body, Token::RBRACE);
1156  }
1157  V8_INLINE void DeclareFunctionNameVar(
1158  const AstRawString* function_name,
1159  FunctionLiteral::FunctionType function_type,
1160  DeclarationScope* function_scope) {
1161  if (function_type == FunctionLiteral::kNamedExpression &&
1162  function_scope->LookupLocal(function_name) == nullptr) {
1163  DCHECK_EQ(function_scope, scope());
1164  function_scope->DeclareFunctionVar(function_name);
1165  }
1166  }
1167 
1168  V8_INLINE void DeclareFunctionNameVar(
1169  const PreParserIdentifier& function_name,
1170  FunctionLiteral::FunctionType function_type,
1171  DeclarationScope* function_scope) {
1172  DeclareFunctionNameVar(function_name.string_, function_type,
1173  function_scope);
1174  }
1175 
1176  bool IdentifierEquals(const PreParserIdentifier& identifier,
1177  const AstRawString* other);
1178 
1179  // TODO(nikolaos): The preparser currently does not keep track of labels
1180  // and targets.
1181  V8_INLINE PreParserStatement
1182  LookupBreakTarget(const PreParserIdentifier& label) {
1183  return PreParserStatement::Default();
1184  }
1185  V8_INLINE PreParserStatement
1186  LookupContinueTarget(const PreParserIdentifier& label) {
1187  return PreParserStatement::Default();
1188  }
1189 
1190  V8_INLINE PreParserStatement DeclareFunction(
1191  const PreParserIdentifier& variable_name,
1192  const PreParserExpression& function, VariableMode mode, int pos,
1193  bool is_sloppy_block_function, ZonePtrList<const AstRawString>* names) {
1194  DCHECK_NULL(names);
1195  if (variable_name.string_ != nullptr) {
1196  scope()->DeclareVariableName(variable_name.string_, mode);
1197  if (is_sloppy_block_function) {
1198  GetDeclarationScope()->DeclareSloppyBlockFunction(variable_name.string_,
1199  scope());
1200  }
1201  }
1202  return Statement::Default();
1203  }
1204 
1205  V8_INLINE PreParserStatement DeclareClass(
1206  const PreParserIdentifier& variable_name,
1208  int class_token_pos, int end_pos) {
1209  // Preparser shouldn't be used in contexts where we need to track the names.
1210  DCHECK_NULL(names);
1211  if (variable_name.string_ != nullptr) {
1212  scope()->DeclareVariableName(variable_name.string_, VariableMode::kLet);
1213  }
1214  return PreParserStatement::Default();
1215  }
1216  V8_INLINE void DeclareClassVariable(const PreParserIdentifier& name,
1217  ClassInfo* class_info,
1218  int class_token_pos) {
1219  if (name.string_ != nullptr) {
1220  scope()->DeclareVariableName(name.string_, VariableMode::kConst);
1221  }
1222  }
1223  V8_INLINE void DeclareClassProperty(const PreParserIdentifier& class_name,
1224  const PreParserExpression& property,
1225  const PreParserIdentifier& property_name,
1226  ClassLiteralProperty::Kind kind,
1227  bool is_static, bool is_constructor,
1228  bool is_computed_name, bool is_private,
1229  ClassInfo* class_info) {
1230  if (kind == ClassLiteralProperty::FIELD && !is_private &&
1231  is_computed_name) {
1232  scope()->DeclareVariableName(
1233  ClassFieldVariableName(ast_value_factory(),
1234  class_info->computed_field_count),
1235  VariableMode::kConst);
1236  }
1237 
1238  if (kind == ClassLiteralProperty::FIELD && is_private &&
1239  property_name.string_ != nullptr) {
1240  scope()->DeclareVariableName(property_name.string_, VariableMode::kConst);
1241  }
1242  }
1243 
1244  V8_INLINE PreParserExpression
1245  RewriteClassLiteral(Scope* scope, const PreParserIdentifier& name,
1246  ClassInfo* class_info, int pos, int end_pos) {
1247  bool has_default_constructor = !class_info->has_seen_constructor;
1248  // Account for the default constructor.
1249  if (has_default_constructor) {
1250  // Creating and disposing of a FunctionState makes tracking of
1251  // next_function_is_likely_called match what Parser does. TODO(marja):
1252  // Make the lazy function + next_function_is_likely_called + default ctor
1253  // logic less surprising. Default ctors shouldn't affect the laziness of
1254  // functions.
1255  bool has_extends = class_info->extends.IsNull();
1256  FunctionKind kind = has_extends ? FunctionKind::kDefaultDerivedConstructor
1257  : FunctionKind::kDefaultBaseConstructor;
1258  DeclarationScope* function_scope = NewFunctionScope(kind);
1259  SetLanguageMode(function_scope, LanguageMode::kStrict);
1260  function_scope->set_start_position(pos);
1261  function_scope->set_end_position(pos);
1262  FunctionState function_state(&function_state_, &scope_, function_scope);
1263  GetNextFunctionLiteralId();
1264  }
1265  if (class_info->has_static_class_fields) {
1266  GetNextFunctionLiteralId();
1267  }
1268  if (class_info->has_instance_members) {
1269  GetNextFunctionLiteralId();
1270  }
1271  return PreParserExpression::Default();
1272  }
1273 
1274  V8_INLINE PreParserStatement DeclareNative(const PreParserIdentifier& name,
1275  int pos) {
1276  return PreParserStatement::Default();
1277  }
1278 
1279  V8_INLINE void QueueDestructuringAssignmentForRewriting(
1280  PreParserExpression assignment) {}
1281 
1282  // Helper functions for recursive descent.
1283  V8_INLINE bool IsEval(const PreParserIdentifier& identifier) const {
1284  return identifier.IsEval();
1285  }
1286 
1287  V8_INLINE bool IsAsync(const PreParserIdentifier& identifier) const {
1288  return identifier.IsAsync();
1289  }
1290 
1291  V8_INLINE bool IsArguments(const PreParserIdentifier& identifier) const {
1292  return identifier.IsArguments();
1293  }
1294 
1295  V8_INLINE bool IsEvalOrArguments(
1296  const PreParserIdentifier& identifier) const {
1297  return identifier.IsEvalOrArguments();
1298  }
1299 
1300  V8_INLINE bool IsAwait(const PreParserIdentifier& identifier) const {
1301  return identifier.IsAwait();
1302  }
1303 
1304  // Returns true if the expression is of type "this.foo".
1305  V8_INLINE static bool IsThisProperty(const PreParserExpression& expression) {
1306  return expression.IsThisProperty();
1307  }
1308 
1309  V8_INLINE static bool IsIdentifier(const PreParserExpression& expression) {
1310  return expression.IsIdentifier();
1311  }
1312 
1313  V8_INLINE static PreParserIdentifier AsIdentifier(
1314  const PreParserExpression& expression) {
1315  return expression.AsIdentifier();
1316  }
1317 
1318  V8_INLINE static PreParserExpression AsIdentifierExpression(
1319  const PreParserExpression& expression) {
1320  return expression;
1321  }
1322 
1323  V8_INLINE bool IsConstructor(const PreParserIdentifier& identifier) const {
1324  return identifier.IsConstructor();
1325  }
1326 
1327  V8_INLINE bool IsName(const PreParserIdentifier& identifier) const {
1328  return identifier.IsName();
1329  }
1330 
1331  V8_INLINE static bool IsBoilerplateProperty(
1332  const PreParserExpression& property) {
1333  // PreParser doesn't count boilerplate properties.
1334  return false;
1335  }
1336 
1337  V8_INLINE bool IsNative(const PreParserExpression& expr) const {
1338  // Preparsing is disabled for extensions (because the extension
1339  // details aren't passed to lazily compiled functions), so we
1340  // don't accept "native function" in the preparser and there is
1341  // no need to keep track of "native".
1342  return false;
1343  }
1344 
1345  V8_INLINE static bool IsArrayIndex(const PreParserIdentifier& string,
1346  uint32_t* index) {
1347  return false;
1348  }
1349 
1350  V8_INLINE bool IsStringLiteral(PreParserStatement statement) const {
1351  return statement.IsStringLiteral();
1352  }
1353 
1354  V8_INLINE static void GetDefaultStrings(
1355  PreParserIdentifier* default_string,
1356  PreParserIdentifier* star_default_star_string) {}
1357 
1358  // Functions for encapsulating the differences between parsing and preparsing;
1359  // operations interleaved with the recursive descent.
1360  V8_INLINE static void PushLiteralName(const PreParserIdentifier& id) {}
1361  V8_INLINE static void PushVariableName(const PreParserIdentifier& id) {}
1362  V8_INLINE void PushPropertyName(const PreParserExpression& expression) {}
1363  V8_INLINE void PushEnclosingName(const PreParserIdentifier& name) {}
1364  V8_INLINE static void AddFunctionForNameInference(
1365  const PreParserExpression& expression) {}
1366  V8_INLINE static void InferFunctionName() {}
1367 
1368  V8_INLINE static void CheckAssigningFunctionLiteralToProperty(
1369  const PreParserExpression& left, const PreParserExpression& right) {}
1370 
1371  V8_INLINE void MarkPatternAsAssigned(const PreParserExpression& expression) {
1372  // TODO(marja): To be able to produce the same errors, the preparser needs
1373  // to start tracking which expressions are variables and which are assigned.
1374  if (expression.variables_ != nullptr) {
1375  for (auto variable : *expression.variables_) {
1376  variable->set_is_assigned();
1377  }
1378  }
1379  }
1380 
1381  V8_INLINE void MarkExpressionAsAssigned(
1382  const PreParserExpression& expression) {
1383  if (IsIdentifier(expression)) {
1384  DCHECK_NOT_NULL(expression.variables_);
1385  DCHECK_EQ(1, expression.variables_->LengthForTest());
1386  expression.variables_->first()->set_is_assigned();
1387  }
1388  }
1389 
1390  V8_INLINE bool ShortcutNumericLiteralBinaryExpression(
1391  PreParserExpression* x, const PreParserExpression& y, Token::Value op,
1392  int pos) {
1393  return false;
1394  }
1395 
1396  V8_INLINE NaryOperation* CollapseNaryExpression(PreParserExpression* x,
1398  Token::Value op, int pos,
1399  const SourceRange& range) {
1400  return nullptr;
1401  }
1402 
1403  V8_INLINE PreParserExpression BuildUnaryExpression(
1404  const PreParserExpression& expression, Token::Value op, int pos) {
1405  return PreParserExpression::Default();
1406  }
1407 
1408  V8_INLINE PreParserStatement
1409  BuildInitializationBlock(DeclarationParsingResult* parsing_result,
1411  for (auto declaration : parsing_result->declarations) {
1412  DeclareAndInitializeVariables(PreParserStatement::Default(),
1413  &(parsing_result->descriptor), &declaration,
1414  names);
1415  }
1416  return PreParserStatement::Default();
1417  }
1418 
1419  V8_INLINE PreParserStatement InitializeForEachStatement(
1420  PreParserStatement stmt, const PreParserExpression& each,
1421  const PreParserExpression& subject, PreParserStatement body) {
1422  MarkPatternAsAssigned(each);
1423  return stmt;
1424  }
1425 
1426  V8_INLINE PreParserStatement InitializeForOfStatement(
1427  PreParserStatement stmt, const PreParserExpression& each,
1428  const PreParserExpression& iterable, PreParserStatement body,
1429  bool finalize, IteratorType type,
1430  int next_result_pos = kNoSourcePosition) {
1431  MarkPatternAsAssigned(each);
1432  return stmt;
1433  }
1434 
1435  V8_INLINE PreParserStatement RewriteForVarInLegacy(const ForInfo& for_info) {
1436  return PreParserStatement::Null();
1437  }
1438 
1439  V8_INLINE void DesugarBindingInForEachStatement(
1440  ForInfo* for_info, PreParserStatement* body_block,
1441  PreParserExpression* each_variable) {
1442  DCHECK_EQ(1, for_info->parsing_result.declarations.size());
1443  bool is_for_var_of =
1444  for_info->mode == ForEachStatement::ITERATE &&
1445  for_info->parsing_result.descriptor.mode == VariableMode::kVar;
1446  bool collect_names =
1447  IsLexicalVariableMode(for_info->parsing_result.descriptor.mode) ||
1448  is_for_var_of;
1449 
1450  DeclareAndInitializeVariables(
1451  PreParserStatement::Default(), &for_info->parsing_result.descriptor,
1452  &for_info->parsing_result.declarations[0],
1453  collect_names ? &for_info->bound_names : nullptr);
1454  }
1455 
1456  V8_INLINE PreParserStatement CreateForEachStatementTDZ(
1457  PreParserStatement init_block, const ForInfo& for_info) {
1458  if (IsLexicalVariableMode(for_info.parsing_result.descriptor.mode)) {
1459  for (auto name : for_info.bound_names) {
1460  scope()->DeclareVariableName(name, VariableMode::kLet);
1461  }
1462  return PreParserStatement::Default();
1463  }
1464  return init_block;
1465  }
1466 
1467  V8_INLINE StatementT DesugarLexicalBindingsInForStatement(
1469  const PreParserExpression& cond, PreParserStatement next,
1470  PreParserStatement body, Scope* inner_scope, const ForInfo& for_info) {
1471  // See Parser::DesugarLexicalBindingsInForStatement.
1472  for (auto name : for_info.bound_names) {
1473  inner_scope->DeclareVariableName(name,
1474  for_info.parsing_result.descriptor.mode);
1475  }
1476  return loop;
1477  }
1478 
1479  PreParserStatement BuildParameterInitializationBlock(
1480  const PreParserFormalParameters& parameters);
1481 
1482  V8_INLINE PreParserStatement
1483  BuildRejectPromiseOnException(PreParserStatement init_block) {
1484  return PreParserStatement::Default();
1485  }
1486 
1487  V8_INLINE void InsertSloppyBlockFunctionVarBindings(DeclarationScope* scope) {
1488  scope->HoistSloppyBlockFunctions(nullptr);
1489  }
1490 
1491  V8_INLINE void InsertShadowingVarBindingInitializers(
1492  PreParserStatement block) {}
1493 
1494  V8_INLINE PreParserExpression NewThrowReferenceError(MessageTemplate message,
1495  int pos) {
1496  return PreParserExpression::Default();
1497  }
1498 
1499  V8_INLINE PreParserExpression NewThrowSyntaxError(
1500  MessageTemplate message, const PreParserIdentifier& arg, int pos) {
1501  return PreParserExpression::Default();
1502  }
1503 
1504  V8_INLINE PreParserExpression NewThrowTypeError(
1505  MessageTemplate message, const PreParserIdentifier& arg, int pos) {
1506  return PreParserExpression::Default();
1507  }
1508 
1509  // Reporting errors.
1510  void ReportMessageAt(Scanner::Location source_location,
1511  MessageTemplate message, const char* arg = nullptr,
1512  ParseErrorType error_type = kSyntaxError) {
1513  pending_error_handler()->ReportMessageAt(source_location.beg_pos,
1514  source_location.end_pos, message,
1515  arg, error_type);
1516  scanner()->set_parser_error();
1517  }
1518 
1519  V8_INLINE void ReportUnidentifiableError() {
1520  pending_error_handler()->set_unidentifiable_error();
1521  scanner()->set_parser_error();
1522  }
1523 
1524  V8_INLINE void ReportMessageAt(Scanner::Location source_location,
1525  MessageTemplate message,
1526  const PreParserIdentifier& arg,
1527  ParseErrorType error_type = kSyntaxError) {
1528  UNREACHABLE();
1529  }
1530 
1531  // "null" return type creators.
1532  V8_INLINE static PreParserIdentifier NullIdentifier() {
1533  return PreParserIdentifier::Null();
1534  }
1535  V8_INLINE static PreParserExpression NullExpression() {
1536  return PreParserExpression::Null();
1537  }
1538  V8_INLINE static PreParserExpression FailureExpression() {
1539  return PreParserExpression::Failure();
1540  }
1541  V8_INLINE static PreParserExpression NullLiteralProperty() {
1542  return PreParserExpression::Null();
1543  }
1544  V8_INLINE static PreParserStatementList NullStatementList() {
1545  return PreParserStatementList::Null();
1546  }
1547  V8_INLINE static PreParserStatement NullStatement() {
1548  return PreParserStatement::Null();
1549  }
1550 
1551  template <typename T>
1552  V8_INLINE static bool IsNull(T subject) {
1553  return subject.IsNull();
1554  }
1555 
1556  V8_INLINE PreParserIdentifier EmptyIdentifierString() const {
1557  return PreParserIdentifier::Default();
1558  }
1559 
1560  // Producing data during the recursive descent.
1561  PreParserIdentifier GetSymbol() const;
1562 
1563  V8_INLINE PreParserIdentifier GetNextSymbol() const {
1564  return PreParserIdentifier::Default();
1565  }
1566 
1567  V8_INLINE PreParserIdentifier GetNumberAsSymbol() const {
1568  return PreParserIdentifier::Default();
1569  }
1570 
1571  V8_INLINE PreParserExpression ThisExpression(int pos = kNoSourcePosition) {
1572  scope()->NewUnresolved(factory()->ast_node_factory(),
1573  ast_value_factory()->this_string(), pos,
1574  THIS_VARIABLE);
1575  return PreParserExpression::This();
1576  }
1577 
1578  V8_INLINE PreParserExpression NewSuperPropertyReference(int pos) {
1579  scope()->NewUnresolved(factory()->ast_node_factory(),
1580  ast_value_factory()->this_function_string(), pos,
1581  NORMAL_VARIABLE);
1582  scope()->NewUnresolved(factory()->ast_node_factory(),
1583  ast_value_factory()->this_string(), pos,
1584  THIS_VARIABLE);
1585  return PreParserExpression::Default();
1586  }
1587 
1588  V8_INLINE PreParserExpression NewSuperCallReference(int pos) {
1589  scope()->NewUnresolved(factory()->ast_node_factory(),
1590  ast_value_factory()->this_function_string(), pos,
1591  NORMAL_VARIABLE);
1592  scope()->NewUnresolved(factory()->ast_node_factory(),
1593  ast_value_factory()->new_target_string(), pos,
1594  NORMAL_VARIABLE);
1595  scope()->NewUnresolved(factory()->ast_node_factory(),
1596  ast_value_factory()->this_string(), pos,
1597  THIS_VARIABLE);
1598  return PreParserExpression::SuperCallReference();
1599  }
1600 
1601  V8_INLINE PreParserExpression NewTargetExpression(int pos) {
1602  return PreParserExpression::NewTargetExpression();
1603  }
1604 
1605  V8_INLINE PreParserExpression ImportMetaExpression(int pos) {
1606  return PreParserExpression::Default();
1607  }
1608 
1609  V8_INLINE PreParserExpression ExpressionFromLiteral(Token::Value token,
1610  int pos) {
1611  if (token != Token::STRING) return PreParserExpression::Default();
1612  return PreParserExpression::StringLiteral();
1613  }
1614 
1615  PreParserExpression ExpressionFromIdentifier(
1616  const PreParserIdentifier& name, int start_position,
1617  InferName infer = InferName::kYes);
1618 
1619  V8_INLINE PreParserPropertyList NewClassPropertyList(int size) const {
1620  return PreParserPropertyList();
1621  }
1622 
1623  V8_INLINE PreParserStatementList NewStatementList(int size) const {
1624  return PreParserStatementList();
1625  }
1626 
1627  V8_INLINE PreParserExpression
1628  NewV8Intrinsic(const PreParserIdentifier& name,
1629  const PreParserExpressionList& arguments, int pos) {
1630  return PreParserExpression::Default();
1631  }
1632 
1633  V8_INLINE PreParserStatement
1634  NewThrowStatement(const PreParserExpression& exception, int pos) {
1635  return PreParserStatement::Jump();
1636  }
1637 
1638  V8_INLINE void AddFormalParameter(PreParserFormalParameters* parameters,
1639  PreParserExpression& pattern,
1640  const PreParserExpression& initializer,
1641  int initializer_end_position,
1642  bool is_rest) {
1643  DeclarationScope* scope = parameters->scope;
1644  if (pattern.variables_ == nullptr) {
1645  scope->DeclareParameterName(ast_value_factory()->empty_string(), is_rest,
1646  ast_value_factory(), false, true);
1647  } else {
1648  // We declare the parameter name for all names, but only create a
1649  // parameter entry for the first one.
1650  auto it = pattern.variables_->begin();
1651  if (!parameters->has_duplicate() &&
1652  scope->LookupLocal(it->raw_name()) != nullptr) {
1653  parameters->set_has_duplicate();
1654  }
1655  scope->DeclareParameterName(it->raw_name(), is_rest, ast_value_factory(),
1656  true, true);
1657  for (++it; it != pattern.variables_->end(); ++it) {
1658  if (!parameters->has_duplicate() &&
1659  scope->LookupLocal(it->raw_name()) != nullptr) {
1660  parameters->set_has_duplicate();
1661  }
1662  scope->DeclareParameterName(it->raw_name(), is_rest,
1663  ast_value_factory(), true, false);
1664  }
1665  }
1666  parameters->UpdateArityAndFunctionLength(!initializer.IsNull(), is_rest);
1667  }
1668 
1669  V8_INLINE void DeclareFormalParameters(
1670  const PreParserFormalParameters* parameters) {
1671  ValidateFormalParameterInitializer();
1672  if (!parameters->is_simple) parameters->scope->SetHasNonSimpleParameters();
1673  }
1674 
1675  V8_INLINE void DeclareArrowFunctionFormalParameters(
1676  PreParserFormalParameters* parameters, const PreParserExpression& params,
1677  const Scanner::Location& params_loc) {
1678  ValidateFormalParameterInitializer();
1679  if (params.variables_ != nullptr) {
1680  Scope* scope = parameters->scope;
1681  for (auto variable : *params.variables_) {
1682  if (!parameters->has_duplicate() &&
1683  scope->LookupLocal(variable->raw_name())) {
1684  parameters->set_has_duplicate();
1685  }
1686  scope->DeclareVariableName(variable->raw_name(), VariableMode::kVar);
1687  }
1688  }
1689  }
1690 
1691  V8_INLINE PreParserExpression
1692  ExpressionListToExpression(const PreParserExpressionList& args) {
1693  return PreParserExpression::Default(args.variables_);
1694  }
1695 
1696  V8_INLINE void SetFunctionNameFromPropertyName(
1697  const PreParserExpression& property, const PreParserIdentifier& name,
1698  const AstRawString* prefix = nullptr) {}
1699  V8_INLINE void SetFunctionNameFromIdentifierRef(
1700  const PreParserExpression& value, const PreParserExpression& identifier) {
1701  }
1702 
1704  GetReportedErrorList() const {
1705  return function_state_->GetReportedErrorList();
1706  }
1707 
1708  V8_INLINE void CountUsage(v8::Isolate::UseCounterFeature feature) {
1709  if (use_counts_ != nullptr) ++use_counts_[feature];
1710  }
1711 
1712  V8_INLINE bool ParsingDynamicFunctionDeclaration() const { return false; }
1713 
1714 // Generate empty functions here as the preparser does not collect source
1715 // ranges for block coverage.
1716 #define DEFINE_RECORD_SOURCE_RANGE(Name) \
1717  template <typename... Ts> \
1718  V8_INLINE void Record##Name##SourceRange(Ts... args) {}
1719  AST_SOURCE_RANGE_LIST(DEFINE_RECORD_SOURCE_RANGE)
1720 #undef DEFINE_RECORD_SOURCE_RANGE
1721 
1722  // Preparser's private field members.
1723 
1724  int* use_counts_;
1725  PreParserLogger log_;
1726 
1727  PreParsedScopeDataBuilder* preparsed_scope_data_builder_;
1728 };
1729 
1730 PreParserExpression PreParser::SpreadCall(const PreParserExpression& function,
1731  const PreParserExpressionList& args,
1732  int pos,
1733  Call::PossiblyEval possibly_eval) {
1734  return factory()->NewCall(function, args, pos, possibly_eval);
1735 }
1736 
1737 PreParserExpression PreParser::SpreadCallNew(
1738  const PreParserExpression& function, const PreParserExpressionList& args,
1739  int pos) {
1740  return factory()->NewCallNew(function, args, pos);
1741 }
1742 
1743 } // namespace internal
1744 } // namespace v8
1745 
1746 #endif // V8_PARSING_PREPARSER_H_
Definition: libplatform.h:13