preparser.h 60.2 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_PARSING_PREPARSER_H_
#define V8_PARSING_PREPARSER_H_
7

8
#include "src/ast/ast-value-factory.h"
9
#include "src/ast/ast.h"
10
#include "src/ast/scopes.h"
11
#include "src/parsing/parse-info.h"
12
#include "src/parsing/parser-base.h"
13
#include "src/parsing/pending-compilation-error-handler.h"
14
#include "src/parsing/preparser-logger.h"
15

16
namespace v8 {
17
namespace internal {
18

19 20 21 22 23 24 25 26
// 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.

27
class PreparseDataBuilder;
28

29 30
class PreParserIdentifier {
 public:
31
  PreParserIdentifier() : type_(kUnknownIdentifier) {}
32 33 34
  static PreParserIdentifier Default() {
    return PreParserIdentifier(kUnknownIdentifier);
  }
35 36
  static PreParserIdentifier Null() {
    return PreParserIdentifier(kNullIdentifier);
37
  }
38 39 40 41 42 43
  static PreParserIdentifier Eval() {
    return PreParserIdentifier(kEvalIdentifier);
  }
  static PreParserIdentifier Arguments() {
    return PreParserIdentifier(kArgumentsIdentifier);
  }
arv@chromium.org's avatar
arv@chromium.org committed
44 45 46
  static PreParserIdentifier Constructor() {
    return PreParserIdentifier(kConstructorIdentifier);
  }
47 48 49
  static PreParserIdentifier Await() {
    return PreParserIdentifier(kAwaitIdentifier);
  }
50 51 52
  static PreParserIdentifier Async() {
    return PreParserIdentifier(kAsyncIdentifier);
  }
53 54 55
  static PreParserIdentifier Name() {
    return PreParserIdentifier(kNameIdentifier);
  }
56 57 58
  static PreParserIdentifier PrivateName() {
    return PreParserIdentifier(kPrivateNameIdentifier);
  }
59
  bool IsNull() const { return type_ == kNullIdentifier; }
60
  bool IsEval() const { return type_ == kEvalIdentifier; }
61
  bool IsAsync() const { return type_ == kAsyncIdentifier; }
rossberg's avatar
rossberg committed
62
  bool IsArguments() const { return type_ == kArgumentsIdentifier; }
63 64
  bool IsEvalOrArguments() const {
    STATIC_ASSERT(kEvalIdentifier + 1 == kArgumentsIdentifier);
65
    return base::IsInRange(type_, kEvalIdentifier, kArgumentsIdentifier);
66
  }
arv@chromium.org's avatar
arv@chromium.org committed
67
  bool IsConstructor() const { return type_ == kConstructorIdentifier; }
68
  bool IsAwait() const { return type_ == kAwaitIdentifier; }
69
  bool IsName() const { return type_ == kNameIdentifier; }
70
  bool IsPrivateName() const { return type_ == kPrivateNameIdentifier; }
71

72
 private:
73
  enum Type : uint8_t {
74
    kNullIdentifier,
75 76
    kUnknownIdentifier,
    kEvalIdentifier,
arv@chromium.org's avatar
arv@chromium.org committed
77
    kArgumentsIdentifier,
78
    kConstructorIdentifier,
79
    kAwaitIdentifier,
80
    kAsyncIdentifier,
81 82
    kNameIdentifier,
    kPrivateNameIdentifier
83
  };
84

85
  explicit PreParserIdentifier(Type type) : string_(nullptr), type_(type) {}
86
  const AstRawString* string_;
87 88

  Type type_;
89
  friend class PreParserExpression;
90
  friend class PreParser;
91
  friend class PreParserFactory;
92 93 94 95
};

class PreParserExpression {
 public:
96
  PreParserExpression() : code_(TypeField::encode(kNull)) {}
97

98
  static PreParserExpression Null() { return PreParserExpression(); }
99 100 101
  static PreParserExpression Failure() {
    return PreParserExpression(TypeField::encode(kFailure));
  }
102

103 104
  static PreParserExpression Default() {
    return PreParserExpression(TypeField::encode(kExpression));
105 106
  }

107
  static PreParserExpression Spread(const PreParserExpression& expression) {
108
    return PreParserExpression(TypeField::encode(kSpreadExpression));
109 110
  }

111
  static PreParserExpression FromIdentifier(const PreParserIdentifier& id) {
112 113
    return PreParserExpression(TypeField::encode(kIdentifierExpression) |
                               IdentifierTypeField::encode(id.type_));
114 115
  }

116
  static PreParserExpression BinaryOperation(const PreParserExpression& left,
117
                                             Token::Value op,
118
                                             const PreParserExpression& right,
119
                                             Zone* zone) {
120
    return PreParserExpression(TypeField::encode(kExpression));
121 122
  }

123
  static PreParserExpression Assignment() {
124
    return PreParserExpression(TypeField::encode(kExpression) |
125
                               ExpressionTypeField::encode(kAssignment));
126 127
  }

128
  static PreParserExpression NewTargetExpression() {
129
    return PreParserExpression::Default();
130 131
  }

132 133
  static PreParserExpression ObjectLiteral() {
    return PreParserExpression(TypeField::encode(kObjectLiteralExpression));
134 135
  }

136 137
  static PreParserExpression ArrayLiteral() {
    return PreParserExpression(TypeField::encode(kArrayLiteralExpression));
138 139
  }

140
  static PreParserExpression StringLiteral() {
marja's avatar
marja committed
141
    return PreParserExpression(TypeField::encode(kStringLiteralExpression));
142 143
  }

144
  static PreParserExpression This() {
145
    return PreParserExpression(TypeField::encode(kExpression) |
146
                               ExpressionTypeField::encode(kThisExpression));
147 148
  }

149 150 151 152
  static PreParserExpression ThisPrivateReference() {
    return PreParserExpression(
        TypeField::encode(kExpression) |
        ExpressionTypeField::encode(kThisPrivateReferenceExpression));
153 154
  }

155
  static PreParserExpression ThisProperty() {
156 157 158
    return PreParserExpression(
        TypeField::encode(kExpression) |
        ExpressionTypeField::encode(kThisPropertyExpression));
159 160
  }

161
  static PreParserExpression Property() {
162 163 164
    return PreParserExpression(
        TypeField::encode(kExpression) |
        ExpressionTypeField::encode(kPropertyExpression));
165 166
  }

167
  static PreParserExpression PrivateReference() {
168 169
    return PreParserExpression(
        TypeField::encode(kExpression) |
170
        ExpressionTypeField::encode(kPrivateReferenceExpression));
171 172
  }

173
  static PreParserExpression Call() {
174 175
    return PreParserExpression(TypeField::encode(kExpression) |
                               ExpressionTypeField::encode(kCallExpression));
176 177
  }

178 179 180 181 182 183
  static PreParserExpression CallEval() {
    return PreParserExpression(
        TypeField::encode(kExpression) |
        ExpressionTypeField::encode(kCallEvalExpression));
  }

184 185 186 187 188 189 190 191 192 193 194
  static PreParserExpression CallTaggedTemplate() {
    return PreParserExpression(
        TypeField::encode(kExpression) |
        ExpressionTypeField::encode(kCallTaggedTemplateExpression));
  }

  bool is_tagged_template() const {
    DCHECK(IsCall());
    return ExpressionTypeField::decode(code_) == kCallTaggedTemplateExpression;
  }

195 196 197 198 199 200
  static PreParserExpression SuperCallReference() {
    return PreParserExpression(
        TypeField::encode(kExpression) |
        ExpressionTypeField::encode(kSuperCallReference));
  }

201
  bool IsNull() const { return TypeField::decode(code_) == kNull; }
202 203 204
  bool IsFailureExpression() const {
    return TypeField::decode(code_) == kFailure;
  }
205

206 207 208
  bool IsIdentifier() const {
    return TypeField::decode(code_) == kIdentifierExpression;
  }
209

210
  PreParserIdentifier AsIdentifier() const {
211
    DCHECK(IsIdentifier());
212
    return PreParserIdentifier(IdentifierTypeField::decode(code_));
213 214
  }

215
  bool IsAssignment() const {
216
    return TypeField::decode(code_) == kExpression &&
217
           ExpressionTypeField::decode(code_) == kAssignment;
218 219
  }

220 221 222 223 224 225 226 227
  bool IsObjectLiteral() const {
    return TypeField::decode(code_) == kObjectLiteralExpression;
  }

  bool IsArrayLiteral() const {
    return TypeField::decode(code_) == kArrayLiteralExpression;
  }

228
  bool IsPattern() const {
229
    STATIC_ASSERT(kObjectLiteralExpression + 1 == kArrayLiteralExpression);
230 231
    return base::IsInRange(TypeField::decode(code_), kObjectLiteralExpression,
                           kArrayLiteralExpression);
232 233
  }

234
  bool IsStringLiteral() const {
235
    return TypeField::decode(code_) == kStringLiteralExpression;
236
  }
237

238 239 240 241
  bool IsThis() const {
    return TypeField::decode(code_) == kExpression &&
           ExpressionTypeField::decode(code_) == kThisExpression;
  }
242

243
  bool IsThisProperty() const {
244
    return TypeField::decode(code_) == kExpression &&
245 246
           (ExpressionTypeField::decode(code_) == kThisPropertyExpression ||
            ExpressionTypeField::decode(code_) ==
247
                kThisPrivateReferenceExpression);
248
  }
249

250
  bool IsProperty() const {
251 252
    return TypeField::decode(code_) == kExpression &&
           (ExpressionTypeField::decode(code_) == kPropertyExpression ||
253
            ExpressionTypeField::decode(code_) == kThisPropertyExpression ||
254
            ExpressionTypeField::decode(code_) == kPrivateReferenceExpression ||
255
            ExpressionTypeField::decode(code_) ==
256
                kThisPrivateReferenceExpression);
257 258
  }

259
  bool IsPrivateReference() const {
260
    return TypeField::decode(code_) == kExpression &&
261
           (ExpressionTypeField::decode(code_) == kPrivateReferenceExpression ||
262
            ExpressionTypeField::decode(code_) ==
263
                kThisPrivateReferenceExpression);
264
  }
265

266 267
  bool IsCall() const {
    return TypeField::decode(code_) == kExpression &&
268
           (ExpressionTypeField::decode(code_) == kCallExpression ||
269 270 271 272 273 274 275
            ExpressionTypeField::decode(code_) == kCallEvalExpression ||
            ExpressionTypeField::decode(code_) ==
                kCallTaggedTemplateExpression);
  }
  PreParserExpression* AsCall() {
    if (IsCall()) return this;
    return nullptr;
276 277
  }

278 279 280 281 282
  bool IsSuperCallReference() const {
    return TypeField::decode(code_) == kExpression &&
           ExpressionTypeField::decode(code_) == kSuperCallReference;
  }

283
  bool IsValidReferenceExpression() const {
284 285 286
    return IsIdentifier() || IsProperty();
  }

287 288 289 290
  // At the moment PreParser doesn't track these expression types.
  bool IsFunctionLiteral() const { return false; }
  bool IsCallNew() const { return false; }

291
  bool IsSpread() const {
292 293 294
    return TypeField::decode(code_) == kSpreadExpression;
  }

295 296 297 298 299 300
  bool is_parenthesized() const { return IsParenthesizedField::decode(code_); }

  void mark_parenthesized() {
    code_ = IsParenthesizedField::update(code_, true);
  }

301 302 303 304
  void clear_parenthesized() {
    code_ = IsParenthesizedField::update(code_, false);
  }

305 306
  PreParserExpression AsFunctionLiteral() { return *this; }

307 308
  // Dummy implementation for making expression->somefunc() work in both Parser
  // and PreParser.
309 310
  PreParserExpression* operator->() { return this; }

311
  // More dummy implementations of things PreParser doesn't need to track:
312
  void SetShouldEagerCompile() {}
313

yangguo's avatar
yangguo committed
314
  int position() const { return kNoSourcePosition; }
315
  void set_function_token_position(int position) {}
316
  void set_scope(Scope* scope) {}
317
  void set_suspend_count(int suspend_count) {}
318

319
 private:
320
  enum Type {
321
    kNull,
322
    kFailure,
323 324 325
    kExpression,
    kIdentifierExpression,
    kStringLiteralExpression,
326 327 328
    kSpreadExpression,
    kObjectLiteralExpression,
    kArrayLiteralExpression
329 330
  };

331 332 333
  enum ExpressionType {
    kThisExpression,
    kThisPropertyExpression,
334
    kThisPrivateReferenceExpression,
335
    kPropertyExpression,
336
    kPrivateReferenceExpression,
337
    kCallExpression,
338
    kCallEvalExpression,
339
    kCallTaggedTemplateExpression,
340
    kSuperCallReference,
341
    kAssignment
342 343
  };

344 345
  explicit PreParserExpression(uint32_t expression_code)
      : code_(expression_code) {}
346

347
  // The first three bits are for the Type.
348
  using TypeField = base::BitField<Type, 0, 3>;
349

350 351 352 353 354
  // 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.
355
  using IsParenthesizedField = TypeField::Next<bool, 1>;
356

357 358
  // The rest of the bits are interpreted depending on the value
  // of the Type field, so they can share the storage.
359
  using ExpressionTypeField = IsParenthesizedField::Next<ExpressionType, 4>;
360
  using IdentifierTypeField =
361 362
      IsParenthesizedField::Next<PreParserIdentifier::Type, 8>;
  using HasCoverInitializedNameField = IsParenthesizedField::Next<bool, 1>;
363 364

  uint32_t code_;
365
  friend class PreParser;
366
  friend class PreParserFactory;
367
  friend class PreParserExpressionList;
368 369
};

370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
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:
386
  explicit PreParserScopedStatementList(std::vector<void*>* buffer) {}
387
  void Rewind() {}
388
  void MergeInto(const PreParserScopedStatementList* other) {}
389
  void Add(const PreParserStatement& element) {}
390
  int length() { return 0; }
391 392
};

393 394 395 396
// 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 {
397
 public:
398
  explicit PreParserExpressionList(std::vector<void*>* buffer) : length_(0) {}
399 400 401 402

  int length() const { return length_; }

  void Add(const PreParserExpression& expression) {
403 404
    ++length_;
  }
405

406 407
 private:
  int length_;
408 409 410

  friend class PreParser;
  friend class PreParserFactory;
411 412
};

413 414 415 416 417 418
class PreParserStatement {
 public:
  static PreParserStatement Default() {
    return PreParserStatement(kUnknownStatement);
  }

419 420 421 422
  static PreParserStatement Iteration() {
    return PreParserStatement(kIterationStatement);
  }

423 424 425 426 427 428 429 430
  static PreParserStatement Null() {
    return PreParserStatement(kNullStatement);
  }

  static PreParserStatement Empty() {
    return PreParserStatement(kEmptyStatement);
  }

431 432 433 434
  static PreParserStatement Jump() {
    return PreParserStatement(kJumpStatement);
  }

435 436 437
  void InitializeStatements(const PreParserScopedStatementList& statements,
                            Zone* zone) {}

438 439 440 441
  // Creates expression statement from expression.
  // Preserves being an unparenthesized string literal, possibly
  // "use strict".
  static PreParserStatement ExpressionStatement(
442
      const PreParserExpression& expression) {
443 444 445 446 447 448
    if (expression.IsStringLiteral()) {
      return PreParserStatement(kStringLiteralExpressionStatement);
    }
    return Default();
  }

449
  bool IsStringLiteral() { return code_ == kStringLiteralExpressionStatement; }
450

451 452 453 454
  bool IsJumpStatement() {
    return code_ == kJumpStatement;
  }

455
  bool IsNull() { return code_ == kNullStatement; }
456

457 458
  bool IsIterationStatement() { return code_ == kIterationStatement; }

459 460 461 462
  bool IsEmptyStatement() {
    DCHECK(!IsNull());
    return code_ == kEmptyStatement;
  }
463

464 465 466 467 468
  // Dummy implementation for making statement->somefunc() work in both Parser
  // and PreParser.
  PreParserStatement* operator->() { return this; }

  PreParserStatementList statements() { return PreParserStatementList(); }
469 470
  PreParserStatementList cases() { return PreParserStatementList(); }

471
  void set_scope(Scope* scope) {}
472
  void Initialize(const PreParserExpression& cond, PreParserStatement body,
473
                  const SourceRange& body_range = {}) {}
474
  void Initialize(PreParserStatement init, const PreParserExpression& cond,
475 476
                  PreParserStatement next, PreParserStatement body,
                  const SourceRange& body_range = {}) {}
477 478 479
  void Initialize(PreParserExpression each, const PreParserExpression& subject,
                  PreParserStatement body, const SourceRange& body_range = {}) {
  }
480

481
 protected:
482
  enum Type {
483 484
    kNullStatement,
    kEmptyStatement,
485
    kUnknownStatement,
486
    kJumpStatement,
487
    kIterationStatement,
488 489 490 491
    kStringLiteralExpressionStatement,
  };

  explicit PreParserStatement(Type code) : code_(code) {}
492 493

 private:
494 495 496
  Type code_;
};

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
// 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_;
};
518

519 520
class PreParserFactory {
 public:
521 522
  explicit PreParserFactory(AstValueFactory* ast_value_factory, Zone* zone)
      : ast_node_factory_(ast_value_factory, zone), zone_(zone) {}
523

524
  AstNodeFactory* ast_node_factory() { return &ast_node_factory_; }
525

526
  PreParserExpression NewStringLiteral(const PreParserIdentifier& identifier,
527
                                       int pos) {
528
    return PreParserExpression::Default();
529
  }
530 531
  PreParserExpression NewNumberLiteral(double number,
                                       int pos) {
532 533
    return PreParserExpression::Default();
  }
534 535 536
  PreParserExpression NewUndefinedLiteral(int pos) {
    return PreParserExpression::Default();
  }
537 538 539
  PreParserExpression NewTheHoleLiteral() {
    return PreParserExpression::Default();
  }
540
  PreParserExpression NewRegExpLiteral(const AstRawString* js_pattern,
541
                                       int js_flags, int pos) {
542 543
    return PreParserExpression::Default();
  }
544 545
  PreParserExpression NewArrayLiteral(const PreParserExpressionList& values,
                                      int first_spread_index, int pos) {
546
    return PreParserExpression::ArrayLiteral();
547
  }
548 549
  PreParserExpression NewClassLiteralProperty(const PreParserExpression& key,
                                              const PreParserExpression& value,
550 551
                                              ClassLiteralProperty::Kind kind,
                                              bool is_static,
552 553
                                              bool is_computed_name,
                                              bool is_private) {
554 555
    return PreParserExpression::Default();
  }
556 557
  PreParserExpression NewObjectLiteralProperty(const PreParserExpression& key,
                                               const PreParserExpression& value,
558
                                               ObjectLiteralProperty::Kind kind,
arv's avatar
arv committed
559
                                               bool is_computed_name) {
560
    return PreParserExpression::Default();
561
  }
562 563
  PreParserExpression NewObjectLiteralProperty(const PreParserExpression& key,
                                               const PreParserExpression& value,
arv's avatar
arv committed
564
                                               bool is_computed_name) {
565
    return PreParserExpression::Default();
566
  }
567
  PreParserExpression NewObjectLiteral(
568
      const PreParserExpressionList& properties, int boilerplate_properties,
569
      int pos, bool has_rest_property, Variable* home_object = nullptr) {
570
    return PreParserExpression::ObjectLiteral();
571
  }
arv@chromium.org's avatar
arv@chromium.org committed
572
  PreParserExpression NewVariableProxy(void* variable) {
573 574
    return PreParserExpression::Default();
  }
575

576
  PreParserExpression NewOptionalChain(const PreParserExpression& expr) {
577 578 579 580
    // Needed to track `delete a?.#b` early errors
    if (expr.IsPrivateReference()) {
      return PreParserExpression::PrivateReference();
    }
581 582 583
    return PreParserExpression::Default();
  }

584
  PreParserExpression NewProperty(const PreParserExpression& obj,
585 586
                                  const PreParserExpression& key, int pos,
                                  bool optional_chain = false) {
587 588
    if (key.IsIdentifier() && key.AsIdentifier().IsPrivateName()) {
      if (obj.IsThis()) {
589
        return PreParserExpression::ThisPrivateReference();
590
      }
591
      return PreParserExpression::PrivateReference();
592 593
    }

594 595 596 597 598 599
    if (obj.IsThis()) {
      return PreParserExpression::ThisProperty();
    }
    return PreParserExpression::Property();
  }
  PreParserExpression NewUnaryOperation(Token::Value op,
600
                                        const PreParserExpression& expression,
601 602 603 604
                                        int pos) {
    return PreParserExpression::Default();
  }
  PreParserExpression NewBinaryOperation(Token::Value op,
605 606 607
                                         const PreParserExpression& left,
                                         const PreParserExpression& right,
                                         int pos) {
608
    return PreParserExpression::BinaryOperation(left, op, right, zone_);
609 610
  }
  PreParserExpression NewCompareOperation(Token::Value op,
611 612 613
                                          const PreParserExpression& left,
                                          const PreParserExpression& right,
                                          int pos) {
614 615
    return PreParserExpression::Default();
  }
616
  PreParserExpression NewAssignment(Token::Value op,
617 618
                                    const PreParserExpression& left,
                                    const PreParserExpression& right, int pos) {
619 620
    // Identifiers need to be tracked since this might be a parameter with a
    // default value inside an arrow function parameter list.
621
    return PreParserExpression::Assignment();
622
  }
623
  PreParserExpression NewYield(const PreParserExpression& expression, int pos,
624
                               Suspend::OnAbruptResume on_abrupt_resume) {
625 626
    return PreParserExpression::Default();
  }
627
  PreParserExpression NewAwait(const PreParserExpression& expression, int pos) {
628 629
    return PreParserExpression::Default();
  }
630 631
  PreParserExpression NewYieldStar(const PreParserExpression& iterable,
                                   int pos) {
632 633
    return PreParserExpression::Default();
  }
634 635 636
  PreParserExpression NewConditional(const PreParserExpression& condition,
                                     const PreParserExpression& then_expression,
                                     const PreParserExpression& else_expression,
637 638 639
                                     int pos) {
    return PreParserExpression::Default();
  }
640 641
  PreParserExpression NewCountOperation(Token::Value op, bool is_prefix,
                                        const PreParserExpression& expression,
642 643 644
                                        int pos) {
    return PreParserExpression::Default();
  }
645 646
  PreParserExpression NewCall(PreParserExpression expression,
                              const PreParserExpressionList& arguments, int pos,
647
                              bool has_spread,
648 649
                              Call::PossiblyEval possibly_eval = Call::NOT_EVAL,
                              bool optional_chain = false) {
650 651
    if (possibly_eval == Call::IS_POSSIBLY_EVAL) {
      DCHECK(expression.IsIdentifier() && expression.AsIdentifier().IsEval());
652
      DCHECK(!optional_chain);
653 654
      return PreParserExpression::CallEval();
    }
655
    return PreParserExpression::Call();
656
  }
657
  PreParserExpression NewTaggedTemplate(
658 659
      PreParserExpression expression, const PreParserExpressionList& arguments,
      int pos) {
660 661
    return PreParserExpression::CallTaggedTemplate();
  }
662
  PreParserExpression NewCallNew(const PreParserExpression& expression,
663
                                 const PreParserExpressionList& arguments,
664
                                 int pos, bool has_spread) {
665 666
    return PreParserExpression::Default();
  }
667
  PreParserStatement NewReturnStatement(
668
      const PreParserExpression& expression, int pos,
669
      int continuation_pos = kNoSourcePosition) {
670
    return PreParserStatement::Jump();
671
  }
672
  PreParserStatement NewAsyncReturnStatement(
673
      const PreParserExpression& expression, int pos,
674
      int continuation_pos = kNoSourcePosition) {
675 676
    return PreParserStatement::Jump();
  }
677
  PreParserExpression NewFunctionLiteral(
678
      const PreParserIdentifier& name, Scope* scope,
679
      const PreParserScopedStatementList& body, int expected_property_count,
680
      int parameter_count, int function_length,
681
      FunctionLiteral::ParameterFlag has_duplicate_parameters,
682
      FunctionSyntaxKind function_syntax_kind,
683
      FunctionLiteral::EagerCompileHint eager_compile_hint, int position,
684
      bool has_braces, int function_literal_id,
685 686
      ProducedPreparseData* produced_preparse_data = nullptr) {
    DCHECK_NULL(produced_preparse_data);
687 688
    return PreParserExpression::Default();
  }
689

690
  PreParserExpression NewSpread(const PreParserExpression& expression, int pos,
nikolaos's avatar
nikolaos committed
691
                                int expr_pos) {
692 693 694
    return PreParserExpression::Spread(expression);
  }

695
  PreParserExpression NewEmptyParentheses(int pos) {
696 697 698
    PreParserExpression result = PreParserExpression::Default();
    result.mark_parenthesized();
    return result;
699 700
  }

701
  PreParserStatement EmptyStatement() { return PreParserStatement::Default(); }
702

703 704
  PreParserBlock NewBlock(int capacity, bool ignore_completion_value) {
    return PreParserBlock::Default();
705 706
  }

707
  PreParserBlock NewBlock(bool ignore_completion_value, bool is_breakable) {
708
    return PreParserBlock::Default();
709 710
  }

711 712 713
  PreParserBlock NewBlock(bool ignore_completion_value,
                          const PreParserScopedStatementList& list) {
    return PreParserBlock::Default();
714 715
  }

716 717 718 719
  PreParserStatement NewDebuggerStatement(int pos) {
    return PreParserStatement::Default();
  }

720 721
  PreParserStatement NewExpressionStatement(const PreParserExpression& expr,
                                            int pos) {
722 723 724
    return PreParserStatement::ExpressionStatement(expr);
  }

725
  PreParserStatement NewIfStatement(const PreParserExpression& condition,
726
                                    PreParserStatement then_statement,
727 728 729
                                    PreParserStatement else_statement, int pos,
                                    SourceRange then_range = {},
                                    SourceRange else_range = {}) {
730 731 732 733
    // This must return a jump statement iff both clauses are jump statements.
    return else_statement.IsJumpStatement() ? then_statement : else_statement;
  }

734 735 736
  PreParserStatement NewBreakStatement(
      PreParserStatement target, int pos,
      int continuation_pos = kNoSourcePosition) {
737 738 739
    return PreParserStatement::Jump();
  }

740 741 742
  PreParserStatement NewContinueStatement(
      PreParserStatement target, int pos,
      int continuation_pos = kNoSourcePosition) {
743 744 745 746
    return PreParserStatement::Jump();
  }

  PreParserStatement NewWithStatement(Scope* scope,
747
                                      const PreParserExpression& expression,
748 749 750 751
                                      PreParserStatement statement, int pos) {
    return PreParserStatement::Default();
  }

752 753
  PreParserStatement NewDoWhileStatement(int pos) {
    return PreParserStatement::Iteration();
754 755
  }

756 757
  PreParserStatement NewWhileStatement(int pos) {
    return PreParserStatement::Iteration();
758 759
  }

760
  PreParserStatement NewSwitchStatement(const PreParserExpression& tag,
761 762 763 764
                                        int pos) {
    return PreParserStatement::Default();
  }

765 766 767
  PreParserStatement NewCaseClause(
      const PreParserExpression& label,
      const PreParserScopedStatementList& statements) {
768 769 770
    return PreParserStatement::Default();
  }

771 772
  PreParserStatement NewForStatement(int pos) {
    return PreParserStatement::Iteration();
773 774
  }

775 776 777
  PreParserStatement NewForEachStatement(ForEachStatement::VisitMode visit_mode,
                                         int pos) {
    return PreParserStatement::Iteration();
778 779
  }

780 781
  PreParserStatement NewForOfStatement(int pos, IteratorType type) {
    return PreParserStatement::Iteration();
782 783
  }

784 785 786
  PreParserExpression NewCallRuntime(
      Runtime::FunctionId id, ZoneChunkList<PreParserExpression>* arguments,
      int pos) {
787 788 789
    return PreParserExpression::Default();
  }

790
  PreParserExpression NewImportCallExpression(const PreParserExpression& args,
791 792 793 794
                                              int pos) {
    return PreParserExpression::Default();
  }

795 796 797 798 799 800
  PreParserExpression NewImportCallExpression(
      const PreParserExpression& specifier,
      const PreParserExpression& import_assertions, int pos) {
    return PreParserExpression::Default();
  }

801
 private:
802
  // For creating VariableProxy objects to track unresolved variables.
803
  AstNodeFactory ast_node_factory_;
804
  Zone* zone_;
805 806
};

807 808
class PreParser;

809 810
class PreParserFormalParameters : public FormalParametersBase {
 public:
811
  explicit PreParserFormalParameters(DeclarationScope* scope)
812
      : FormalParametersBase(scope) {}
813

814
  void set_has_duplicate() { has_duplicate_ = true; }
815 816 817 818 819 820 821 822
  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;
823 824 825

 private:
  bool has_duplicate_ = false;
826
  bool strict_parameter_error_ = false;
827
};
828

829 830
class PreParserFuncNameInferrer {
 public:
831
  explicit PreParserFuncNameInferrer(AstValueFactory* avf) {}
832 833 834
  PreParserFuncNameInferrer(const PreParserFuncNameInferrer&) = delete;
  PreParserFuncNameInferrer& operator=(const PreParserFuncNameInferrer&) =
      delete;
835 836 837
  void RemoveAsyncKeywordFromEnd() const {}
  void Infer() const {}
  void RemoveLastFunction() const {}
838 839 840 841

  class State {
   public:
    explicit State(PreParserFuncNameInferrer* fni) {}
842 843
    State(const State&) = delete;
    State& operator=(const State&) = delete;
844 845 846
  };
};

847 848
class PreParserSourceRange {
 public:
849
  PreParserSourceRange() = default;
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
  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);
};

870 871
class PreParserPropertyList {};

872
template <>
873
struct ParserTypes<PreParser> {
874 875
  using Base = ParserBase<PreParser>;
  using Impl = PreParser;
876 877

  // Return types for traversing functions.
878
  using ClassLiteralProperty = PreParserExpression;
879
  using ClassLiteralStaticElement = PreParserExpression;
880 881 882 883 884 885 886 887 888
  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;
889
  using ClassStaticElementList = PreParserPropertyList;
890 891 892 893 894 895
  using StatementList = PreParserScopedStatementList;
  using Block = PreParserBlock;
  using BreakableStatement = PreParserStatement;
  using ForStatement = PreParserStatement;
  using IterationStatement = PreParserStatement;
  using Statement = PreParserStatement;
896 897

  // For constructing objects returned by the traversing functions.
898
  using Factory = PreParserFactory;
899

900
  // Other implementation-specific tasks.
901 902 903
  using FuncNameInferrer = PreParserFuncNameInferrer;
  using SourceRange = PreParserSourceRange;
  using SourceRangeScope = PreParserSourceRangeScope;
904 905 906
};


907 908
// Preparsing checks a JavaScript program and emits preparse-data that helps
// a later parsing to be faster.
909
// See preparse-data-format.h for the data format.
910 911 912 913 914 915 916 917 918

// 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.
919
class PreParser : public ParserBase<PreParser> {
920
  friend class ParserBase<PreParser>;
921

922
 public:
923 924 925
  using Identifier = PreParserIdentifier;
  using Expression = PreParserExpression;
  using Statement = PreParserStatement;
926

927 928
  enum PreParseResult {
    kPreParseStackOverflow,
929
    kPreParseNotIdentifiableError,
930 931 932
    kPreParseSuccess
  };

933 934
  PreParser(Zone* zone, Scanner* scanner, uintptr_t stack_limit,
            AstValueFactory* ast_value_factory,
935
            PendingCompilationErrorHandler* pending_error_handler,
936
            RuntimeCallStats* runtime_call_stats, Logger* logger,
937
            UnoptimizedCompileFlags flags, bool parsing_on_main_thread = true)
938 939 940
      : ParserBase<PreParser>(zone, scanner, stack_limit, ast_value_factory,
                              pending_error_handler, runtime_call_stats, logger,
                              flags, parsing_on_main_thread),
941
        use_counts_(nullptr),
942 943 944 945
        preparse_data_builder_(nullptr),
        preparse_data_builder_buffer_() {
    preparse_data_builder_buffer_.reserve(16);
  }
946

947
  static bool IsPreParser() { return true; }
948

949 950
  PreParserLogger* logger() { return &log_; }

951 952 953 954
  // 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.
955
  V8_EXPORT_PRIVATE PreParseResult PreParseProgram();
956

957 958
  // Parses a single function literal, from the opening parentheses before
  // parameters to the closing brace after the body.
959
  // Returns a FunctionEntry describing the body of the function in enough
960
  // detail that it can be lazily compiled.
961 962
  // The scanner is expected to have matched the "function" or "function*"
  // keyword and parameters, and have consumed the initial '{'.
963
  // At return, unless an error occurred, the scanner is positioned before the
964
  // the final '}'.
965 966
  PreParseResult PreParseFunction(
      const AstRawString* function_name, FunctionKind kind,
967
      FunctionSyntaxKind function_syntax_kind, DeclarationScope* function_scope,
968
      int* use_counts, ProducedPreparseData** produced_preparser_scope_data);
969

970
  PreparseDataBuilder* preparse_data_builder() const {
971
    return preparse_data_builder_;
972 973
  }

974 975
  void set_preparse_data_builder(PreparseDataBuilder* preparse_data_builder) {
    preparse_data_builder_ = preparse_data_builder;
976
  }
977

978 979 980 981
  std::vector<void*>* preparse_data_builder_buffer() {
    return &preparse_data_builder_buffer_;
  }

982
 private:
983
  friend class i::ExpressionScope<ParserTypes<PreParser>>;
984 985 986
  friend class i::VariableDeclarationParsingScope<ParserTypes<PreParser>>;
  friend class i::ParameterDeclarationParsingScope<ParserTypes<PreParser>>;
  friend class i::ArrowHeadParsingScope<ParserTypes<PreParser>>;
987
  friend class PreParserFormalParameters;
988 989 990 991 992
  // 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.

993 994 995 996 997
  // 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.

998 999
  // Indicates that we won't switch from the preparser to the preparser; we'll
  // just stay where we are.
1000
  bool AllowsLazyParsingWithoutUnresolvedVariables() const { return false; }
1001
  bool parse_lazily() const { return false; }
1002

1003 1004 1005 1006
  PendingCompilationErrorHandler* pending_error_handler() {
    return pending_error_handler_;
  }

1007
  V8_INLINE bool SkipFunction(const AstRawString* name, FunctionKind kind,
1008
                              FunctionSyntaxKind function_syntax_kind,
1009
                              DeclarationScope* function_scope,
1010
                              int* num_parameters, int* function_length,
1011
                              ProducedPreparseData** produced_preparse_data) {
1012 1013
    UNREACHABLE();
  }
1014

1015 1016 1017
  Expression ParseFunctionLiteral(
      Identifier name, Scanner::Location function_name_location,
      FunctionNameValidity function_name_validity, FunctionKind kind,
1018
      int function_token_pos, FunctionSyntaxKind function_syntax_kind,
1019
      LanguageMode language_mode,
1020
      ZonePtrList<const AstRawString>* arguments_for_wrapped_function);
1021 1022 1023 1024 1025

  PreParserExpression InitializeObjectLiteral(PreParserExpression literal) {
    return literal;
  }

1026 1027
  bool HasCheckedSyntax() { return false; }

1028
  void ParseStatementListAndLogFunction(PreParserFormalParameters* formals);
1029

1030 1031 1032 1033 1034 1035
  struct TemplateLiteralState {};

  V8_INLINE TemplateLiteralState OpenTemplateLiteral(int pos) {
    return TemplateLiteralState();
  }
  V8_INLINE void AddTemplateExpression(TemplateLiteralState* state,
1036
                                       const PreParserExpression& expression) {}
1037 1038
  V8_INLINE void AddTemplateSpan(TemplateLiteralState* state, bool should_cook,
                                 bool tail) {}
1039
  V8_INLINE PreParserExpression CloseTemplateLiteral(
1040
      TemplateLiteralState* state, int start, const PreParserExpression& tag) {
1041 1042
    return PreParserExpression::Default();
  }
1043 1044
  V8_INLINE bool IsPrivateReference(const PreParserExpression& expression) {
    return expression.IsPrivateReference();
1045
  }
1046 1047 1048 1049 1050
  V8_INLINE void SetLanguageMode(Scope* scope, LanguageMode mode) {
    scope->SetLanguageMode(mode);
  }
  V8_INLINE void SetAsmModule() {}

1051
  V8_INLINE void PrepareGeneratorVariables() {}
1052
  V8_INLINE void RewriteAsyncFunctionBody(
1053
      const PreParserScopedStatementList* body, PreParserStatement block,
1054
      const PreParserExpression& return_value) {}
1055

1056 1057
  V8_INLINE PreParserExpression
  RewriteReturn(const PreParserExpression& return_value, int pos) {
1058 1059
    return return_value;
  }
1060 1061
  V8_INLINE PreParserStatement
  RewriteSwitchStatement(PreParserStatement switch_statement, Scope* scope) {
1062 1063
    return PreParserStatement::Default();
  }
1064

1065 1066 1067 1068
  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);
1069 1070 1071
  }

  void DeclareAndBindVariable(const VariableProxy* proxy, VariableKind kind,
1072 1073 1074 1075 1076
                              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);
1077
    // Don't bother actually binding the proxy.
1078 1079
  }

1080 1081
  Variable* DeclarePrivateVariableName(const AstRawString* name,
                                       ClassScope* scope, VariableMode mode,
1082
                                       IsStaticFlag is_static_flag,
1083 1084
                                       bool* was_added) {
    DCHECK(IsConstVariableMode(mode));
1085
    return scope->DeclarePrivateName(name, mode, is_static_flag, was_added);
1086 1087
  }

1088 1089 1090 1091
  Variable* DeclareVariableName(const AstRawString* name, VariableMode mode,
                                Scope* scope, bool* was_added,
                                int position = kNoSourcePosition,
                                VariableKind kind = NORMAL_VARIABLE) {
1092
    DCHECK(!IsPrivateMethodOrAccessorVariableMode(mode));
1093 1094
    Variable* var = scope->DeclareVariableName(name, mode, was_added, kind);
    if (var == nullptr) {
1095
      ReportUnidentifiableError();
1096 1097
      if (!IsLexicalVariableMode(mode)) scope = scope->GetDeclarationScope();
      var = scope->LookupLocal(name);
1098
    } else if (var->scope() != scope) {
1099 1100 1101 1102 1103 1104 1105
      DCHECK_NE(kNoSourcePosition, position);
      DCHECK_EQ(VariableMode::kVar, mode);
      Declaration* nested_declaration =
          factory()->ast_node_factory()->NewNestedVariableDeclaration(scope,
                                                                      position);
      nested_declaration->set_var(var);
      var->scope()->declarations()->Add(nested_declaration);
1106
    }
1107
    return var;
1108 1109 1110
  }

  V8_INLINE PreParserBlock RewriteCatchPattern(CatchInfo* catch_info) {
1111 1112 1113
    return PreParserBlock::Default();
  }

1114 1115
  V8_INLINE void ReportVarRedeclarationIn(const AstRawString* name,
                                          Scope* scope) {
1116
    ReportUnidentifiableError();
1117 1118
  }

1119 1120
  V8_INLINE PreParserStatement RewriteTryStatement(
      PreParserStatement try_block, PreParserStatement catch_block,
1121 1122
      const SourceRange& catch_range, PreParserStatement finally_block,
      const SourceRange& finally_range, const CatchInfo& catch_info, int pos) {
1123 1124
    return PreParserStatement::Default();
  }
1125

1126 1127 1128 1129 1130
  V8_INLINE void ReportUnexpectedTokenAt(
      Scanner::Location location, Token::Value token,
      MessageTemplate message = MessageTemplate::kUnexpectedToken) {
    ReportUnidentifiableError();
  }
1131
  V8_INLINE void ParseAndRewriteGeneratorFunctionBody(
1132
      int pos, FunctionKind kind, PreParserScopedStatementList* body) {
1133
    ParseStatementList(body, Token::RBRACE);
1134
  }
1135
  V8_INLINE void ParseAndRewriteAsyncGeneratorFunctionBody(
1136
      int pos, FunctionKind kind, PreParserScopedStatementList* body) {
1137
    ParseStatementList(body, Token::RBRACE);
1138
  }
1139 1140 1141 1142
  V8_INLINE void DeclareFunctionNameVar(const AstRawString* function_name,
                                        FunctionSyntaxKind function_syntax_kind,
                                        DeclarationScope* function_scope) {
    if (function_syntax_kind == FunctionSyntaxKind::kNamedExpression &&
1143 1144 1145
        function_scope->LookupLocal(function_name) == nullptr) {
      DCHECK_EQ(function_scope, scope());
      function_scope->DeclareFunctionVar(function_name);
1146 1147 1148
    }
  }

1149 1150
  V8_INLINE void DeclareFunctionNameVar(
      const PreParserIdentifier& function_name,
1151
      FunctionSyntaxKind function_syntax_kind,
1152
      DeclarationScope* function_scope) {
1153
    DeclareFunctionNameVar(function_name.string_, function_syntax_kind,
1154
                           function_scope);
1155
  }
1156

1157 1158 1159
  bool IdentifierEquals(const PreParserIdentifier& identifier,
                        const AstRawString* other);

1160 1161 1162 1163
  V8_INLINE PreParserStatement DeclareFunction(
      const PreParserIdentifier& variable_name,
      const PreParserExpression& function, VariableMode mode, VariableKind kind,
      int beg_pos, int end_pos, ZonePtrList<const AstRawString>* names) {
1164
    DCHECK_NULL(names);
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
    bool was_added;
    Variable* var = DeclareVariableName(variable_name.string_, mode, scope(),
                                        &was_added, beg_pos, kind);
    if (kind == SLOPPY_BLOCK_FUNCTION_VARIABLE) {
      Token::Value init =
          loop_nesting_depth() > 0 ? Token::ASSIGN : Token::INIT;
      SloppyBlockFunctionStatement* statement =
          factory()->ast_node_factory()->NewSloppyBlockFunctionStatement(
              end_pos, var, init);
      GetDeclarationScope()->DeclareSloppyBlockFunction(statement);
1175
    }
1176 1177 1178
    return Statement::Default();
  }

1179 1180
  V8_INLINE PreParserStatement DeclareClass(
      const PreParserIdentifier& variable_name,
1181
      const PreParserExpression& value, ZonePtrList<const AstRawString>* names,
1182
      int class_token_pos, int end_pos) {
1183 1184
    // Preparser shouldn't be used in contexts where we need to track the names.
    DCHECK_NULL(names);
1185 1186 1187
    bool was_added;
    DeclareVariableName(variable_name.string_, VariableMode::kLet, scope(),
                        &was_added);
1188 1189
    return PreParserStatement::Default();
  }
1190 1191
  V8_INLINE void DeclareClassVariable(ClassScope* scope,
                                      const PreParserIdentifier& name,
1192
                                      ClassInfo* class_info,
1193
                                      int class_token_pos) {
1194 1195 1196 1197 1198
    DCHECK_IMPLIES(IsNull(name), class_info->is_anonymous);
    // Declare a special class variable for anonymous classes with the dot
    // if we need to save it for static private method access.
    scope->DeclareClassVariable(ast_value_factory(), name.string_,
                                class_token_pos);
1199
  }
1200 1201 1202 1203 1204 1205 1206 1207
  V8_INLINE void DeclarePublicClassMethod(const PreParserIdentifier& class_name,
                                          const PreParserExpression& property,
                                          bool is_constructor,
                                          ClassInfo* class_info) {}
  V8_INLINE void DeclarePublicClassField(ClassScope* scope,
                                         const PreParserExpression& property,
                                         bool is_static, bool is_computed_name,
                                         ClassInfo* class_info) {
1208
    if (is_computed_name) {
1209
      bool was_added;
1210
      DeclareVariableName(
1211 1212
          ClassFieldVariableName(ast_value_factory(),
                                 class_info->computed_field_count),
1213
          VariableMode::kConst, scope, &was_added);
1214 1215 1216 1217 1218 1219 1220 1221
    }
  }

  V8_INLINE void DeclarePrivateClassMember(
      ClassScope* scope, const PreParserIdentifier& property_name,
      const PreParserExpression& property, ClassLiteralProperty::Kind kind,
      bool is_static, ClassInfo* class_info) {
    bool was_added;
1222

1223 1224 1225 1226
    DeclarePrivateVariableName(
        property_name.string_, scope, GetVariableMode(kind),
        is_static ? IsStaticFlag::kStatic : IsStaticFlag::kNotStatic,
        &was_added);
1227 1228 1229 1230
    if (!was_added) {
      Scanner::Location loc(property.position(), property.position() + 1);
      ReportMessageAt(loc, MessageTemplate::kVarRedeclaration,
                      property_name.string_);
1231
    }
1232 1233
  }

1234 1235 1236 1237 1238
  V8_INLINE void AddClassStaticBlock(PreParserBlock block,
                                     ClassInfo* class_info) {
    DCHECK(class_info->has_static_elements);
  }

1239
  V8_INLINE PreParserExpression
1240
  RewriteClassLiteral(ClassScope* scope, const PreParserIdentifier& name,
1241
                      ClassInfo* class_info, int pos, int end_pos) {
1242 1243
    bool has_default_constructor = !class_info->has_seen_constructor;
    // Account for the default constructor.
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
    if (has_default_constructor) {
      // Creating and disposing of a FunctionState makes tracking of
      // next_function_is_likely_called match what Parser does. TODO(marja):
      // Make the lazy function + next_function_is_likely_called + default ctor
      // logic less surprising. Default ctors shouldn't affect the laziness of
      // functions.
      bool has_extends = class_info->extends.IsNull();
      FunctionKind kind = has_extends ? FunctionKind::kDefaultDerivedConstructor
                                      : FunctionKind::kDefaultBaseConstructor;
      DeclarationScope* function_scope = NewFunctionScope(kind);
1254
      SetLanguageMode(function_scope, LanguageMode::kStrict);
1255 1256 1257 1258
      function_scope->set_start_position(pos);
      function_scope->set_end_position(pos);
      FunctionState function_state(&function_state_, &scope_, function_scope);
      GetNextFunctionLiteralId();
1259
    }
1260
    if (class_info->has_static_elements) {
1261
      GetNextFunctionLiteralId();
1262
    }
1263
    if (class_info->has_instance_members) {
1264
      GetNextFunctionLiteralId();
1265
    }
1266 1267 1268
    return PreParserExpression::Default();
  }

1269
  V8_INLINE PreParserStatement DeclareNative(const PreParserIdentifier& name,
1270
                                             int pos) {
1271 1272 1273
    return PreParserStatement::Default();
  }

1274 1275 1276
  V8_INLINE void QueueDestructuringAssignmentForRewriting(
      PreParserExpression assignment) {}

1277
  // Helper functions for recursive descent.
1278
  V8_INLINE bool IsEval(const PreParserIdentifier& identifier) const {
1279 1280 1281
    return identifier.IsEval();
  }

1282 1283 1284 1285
  V8_INLINE bool IsAsync(const PreParserIdentifier& identifier) const {
    return identifier.IsAsync();
  }

1286
  V8_INLINE bool IsArguments(const PreParserIdentifier& identifier) const {
1287 1288 1289
    return identifier.IsArguments();
  }

1290 1291
  V8_INLINE bool IsEvalOrArguments(
      const PreParserIdentifier& identifier) const {
1292 1293 1294 1295
    return identifier.IsEvalOrArguments();
  }

  // Returns true if the expression is of type "this.foo".
1296
  V8_INLINE static bool IsThisProperty(const PreParserExpression& expression) {
1297 1298 1299
    return expression.IsThisProperty();
  }

1300
  V8_INLINE static bool IsIdentifier(const PreParserExpression& expression) {
1301 1302 1303 1304
    return expression.IsIdentifier();
  }

  V8_INLINE static PreParserIdentifier AsIdentifier(
1305
      const PreParserExpression& expression) {
1306 1307 1308
    return expression.AsIdentifier();
  }

1309
  V8_INLINE static PreParserExpression AsIdentifierExpression(
1310
      const PreParserExpression& expression) {
1311 1312 1313
    return expression;
  }

1314
  V8_INLINE bool IsConstructor(const PreParserIdentifier& identifier) const {
1315 1316 1317
    return identifier.IsConstructor();
  }

1318
  V8_INLINE bool IsName(const PreParserIdentifier& identifier) const {
1319 1320 1321
    return identifier.IsName();
  }

1322 1323
  V8_INLINE static bool IsBoilerplateProperty(
      const PreParserExpression& property) {
1324 1325 1326 1327
    // PreParser doesn't count boilerplate properties.
    return false;
  }

1328 1329 1330 1331 1332 1333 1334 1335
  V8_INLINE bool ParsingExtension() const {
    // Preparsing is disabled for extensions (because the extension
    // details aren't passed to lazily compiled functions), so we
    // don't accept "native function" in the preparser and there is
    // no need to keep track of "native".
    return false;
  }

1336
  V8_INLINE bool IsNative(const PreParserExpression& expr) const {
1337 1338 1339 1340 1341 1342 1343
    // Preparsing is disabled for extensions (because the extension
    // details aren't passed to lazily compiled functions), so we
    // don't accept "native function" in the preparser and there is
    // no need to keep track of "native".
    return false;
  }

1344
  V8_INLINE static bool IsArrayIndex(const PreParserIdentifier& string,
1345 1346 1347 1348
                                     uint32_t* index) {
    return false;
  }

1349 1350 1351 1352
  V8_INLINE bool IsStringLiteral(PreParserStatement statement) const {
    return statement.IsStringLiteral();
  }

1353 1354
  V8_INLINE static void GetDefaultStrings(
      PreParserIdentifier* default_string,
1355
      PreParserIdentifier* dot_default_string) {}
1356

1357 1358
  // Functions for encapsulating the differences between parsing and preparsing;
  // operations interleaved with the recursive descent.
1359 1360 1361 1362
  V8_INLINE static void PushLiteralName(const PreParserIdentifier& id) {}
  V8_INLINE static void PushVariableName(const PreParserIdentifier& id) {}
  V8_INLINE void PushPropertyName(const PreParserExpression& expression) {}
  V8_INLINE void PushEnclosingName(const PreParserIdentifier& name) {}
1363
  V8_INLINE static void AddFunctionForNameInference(
1364
      const PreParserExpression& expression) {}
1365
  V8_INLINE static void InferFunctionName() {}
1366

1367 1368 1369
  V8_INLINE static void CheckAssigningFunctionLiteralToProperty(
      const PreParserExpression& left, const PreParserExpression& right) {}

1370 1371 1372
  V8_INLINE bool ShortcutNumericLiteralBinaryExpression(
      PreParserExpression* x, const PreParserExpression& y, Token::Value op,
      int pos) {
1373 1374 1375
    return false;
  }

1376 1377 1378 1379
  V8_INLINE NaryOperation* CollapseNaryExpression(PreParserExpression* x,
                                                  PreParserExpression y,
                                                  Token::Value op, int pos,
                                                  const SourceRange& range) {
1380
    x->clear_parenthesized();
1381
    return nullptr;
1382 1383
  }

1384
  V8_INLINE PreParserExpression BuildUnaryExpression(
1385
      const PreParserExpression& expression, Token::Value op, int pos) {
1386 1387 1388
    return PreParserExpression::Default();
  }

1389
  V8_INLINE PreParserStatement
1390
  BuildInitializationBlock(DeclarationParsingResult* parsing_result) {
1391 1392 1393
    return PreParserStatement::Default();
  }

1394 1395
  V8_INLINE PreParserBlock RewriteForVarInLegacy(const ForInfo& for_info) {
    return PreParserBlock::Null();
1396
  }
1397

1398 1399
  V8_INLINE void DesugarBindingInForEachStatement(
      ForInfo* for_info, PreParserStatement* body_block,
1400
      PreParserExpression* each_variable) {
1401 1402
  }

1403 1404
  V8_INLINE PreParserBlock CreateForEachStatementTDZ(PreParserBlock init_block,
                                                     const ForInfo& for_info) {
1405 1406
    if (IsLexicalVariableMode(for_info.parsing_result.descriptor.mode)) {
      for (auto name : for_info.bound_names) {
1407 1408
        bool was_added;
        DeclareVariableName(name, VariableMode::kLet, scope(), &was_added);
1409
      }
1410
      return PreParserBlock::Default();
1411
    }
1412 1413 1414 1415 1416
    return init_block;
  }

  V8_INLINE StatementT DesugarLexicalBindingsInForStatement(
      PreParserStatement loop, PreParserStatement init,
1417
      const PreParserExpression& cond, PreParserStatement next,
1418
      PreParserStatement body, Scope* inner_scope, const ForInfo& for_info) {
1419
    // See Parser::DesugarLexicalBindingsInForStatement.
1420
    for (auto name : for_info.bound_names) {
1421
      bool was_added;
1422
      DeclareVariableName(name, for_info.parsing_result.descriptor.mode,
1423
                          inner_scope, &was_added);
1424
    }
1425 1426 1427
    return loop;
  }

1428
  PreParserBlock BuildParameterInitializationBlock(
1429
      const PreParserFormalParameters& parameters);
1430

1431
  V8_INLINE PreParserBlock
1432
  BuildRejectPromiseOnException(PreParserStatement init_block) {
1433
    return PreParserBlock::Default();
1434 1435 1436 1437 1438 1439 1440 1441 1442
  }

  V8_INLINE void InsertSloppyBlockFunctionVarBindings(DeclarationScope* scope) {
    scope->HoistSloppyBlockFunctions(nullptr);
  }

  V8_INLINE void InsertShadowingVarBindingInitializers(
      PreParserStatement block) {}

1443 1444
  V8_INLINE PreParserExpression NewThrowReferenceError(MessageTemplate message,
                                                       int pos) {
1445 1446 1447
    return PreParserExpression::Default();
  }

1448 1449
  V8_INLINE PreParserExpression NewThrowSyntaxError(
      MessageTemplate message, const PreParserIdentifier& arg, int pos) {
1450 1451 1452
    return PreParserExpression::Default();
  }

1453 1454
  V8_INLINE PreParserExpression NewThrowTypeError(
      MessageTemplate message, const PreParserIdentifier& arg, int pos) {
1455 1456 1457
    return PreParserExpression::Default();
  }

1458 1459 1460
  V8_INLINE const AstRawString* PreParserIdentifierToAstRawString(
      const PreParserIdentifier& x) {
    return x.string_;
1461 1462
  }

1463
  V8_INLINE void ReportUnidentifiableError() {
1464
    pending_error_handler()->set_unidentifiable_error();
1465
    scanner()->set_parser_error();
1466 1467
  }

1468 1469 1470 1471
  const AstRawString* GetRawNameFromIdentifier(const PreParserIdentifier& arg) {
    return arg.string_;
  }

1472 1473
  PreParserStatement AsIterationStatement(PreParserStatement s) { return s; }

1474
  // "null" return type creators.
1475 1476
  V8_INLINE static PreParserIdentifier NullIdentifier() {
    return PreParserIdentifier::Null();
1477
  }
1478 1479
  V8_INLINE static PreParserExpression NullExpression() {
    return PreParserExpression::Null();
1480
  }
1481 1482 1483
  V8_INLINE static PreParserExpression FailureExpression() {
    return PreParserExpression::Failure();
  }
1484 1485
  V8_INLINE static PreParserExpression NullLiteralProperty() {
    return PreParserExpression::Null();
1486
  }
1487
  V8_INLINE static PreParserStatementList NullStatementList() {
1488 1489 1490
    return PreParserStatementList::Null();
  }
  V8_INLINE static PreParserStatement NullStatement() {
1491
    return PreParserStatement::Null();
1492
  }
1493
  V8_INLINE static PreParserBlock NullBlock() { return PreParserBlock::Null(); }
1494

1495 1496 1497
  template <typename T>
  V8_INLINE static bool IsNull(T subject) {
    return subject.IsNull();
1498 1499
  }

1500 1501 1502 1503
  V8_INLINE static bool IsIterationStatement(PreParserStatement subject) {
    return subject.IsIterationStatement();
  }

1504
  V8_INLINE PreParserIdentifier EmptyIdentifierString() const {
1505 1506 1507
    PreParserIdentifier result = PreParserIdentifier::Default();
    result.string_ = ast_value_factory()->empty_string();
    return result;
1508 1509
  }

1510
  // Producing data during the recursive descent.
1511 1512 1513 1514 1515
  PreParserIdentifier GetSymbol() const {
    return PreParserIdentifier::Default();
  }

  PreParserIdentifier GetIdentifier() const;
1516 1517 1518 1519 1520 1521 1522 1523 1524

  V8_INLINE PreParserIdentifier GetNextSymbol() const {
    return PreParserIdentifier::Default();
  }

  V8_INLINE PreParserIdentifier GetNumberAsSymbol() const {
    return PreParserIdentifier::Default();
  }

1525 1526
  V8_INLINE PreParserExpression ThisExpression() {
    UseThis();
1527
    return PreParserExpression::This();
1528 1529
  }

1530 1531 1532 1533 1534
  V8_INLINE PreParserExpression NewThisExpression(int pos) {
    UseThis();
    return PreParserExpression::This();
  }

1535 1536 1537 1538 1539
  V8_INLINE PreParserExpression NewSuperPropertyReference(int pos) {
    return PreParserExpression::Default();
  }

  V8_INLINE PreParserExpression NewSuperCallReference(int pos) {
1540 1541 1542 1543 1544 1545
    scope()->NewUnresolved(factory()->ast_node_factory(),
                           ast_value_factory()->this_function_string(), pos,
                           NORMAL_VARIABLE);
    scope()->NewUnresolved(factory()->ast_node_factory(),
                           ast_value_factory()->new_target_string(), pos,
                           NORMAL_VARIABLE);
1546 1547 1548 1549
    return PreParserExpression::SuperCallReference();
  }

  V8_INLINE PreParserExpression NewTargetExpression(int pos) {
1550
    return PreParserExpression::NewTargetExpression();
1551 1552
  }

1553 1554 1555 1556
  V8_INLINE PreParserExpression ImportMetaExpression(int pos) {
    return PreParserExpression::Default();
  }

1557 1558
  V8_INLINE PreParserExpression ExpressionFromLiteral(Token::Value token,
                                                      int pos) {
1559
    if (token != Token::STRING) return PreParserExpression::Default();
1560 1561 1562
    return PreParserExpression::StringLiteral();
  }

1563 1564 1565
  PreParserExpression ExpressionFromPrivateName(
      PrivateNameScopeIterator* private_name_scope,
      const PreParserIdentifier& name, int start_position) {
1566 1567
    VariableProxy* proxy = factory()->ast_node_factory()->NewVariableProxy(
        name.string_, NORMAL_VARIABLE, start_position);
1568
    private_name_scope->AddUnresolvedPrivateName(proxy);
1569 1570 1571
    return PreParserExpression::FromIdentifier(name);
  }

1572 1573
  PreParserExpression ExpressionFromIdentifier(
      const PreParserIdentifier& name, int start_position,
1574
      InferName infer = InferName::kYes) {
1575
    expression_scope()->NewVariable(name.string_, start_position);
1576 1577
    return PreParserExpression::FromIdentifier(name);
  }
1578

1579 1580
  V8_INLINE void DeclareIdentifier(const PreParserIdentifier& name,
                                   int start_position) {
1581
    expression_scope()->Declare(name.string_, start_position);
1582 1583
  }

1584 1585 1586 1587 1588
  V8_INLINE Variable* DeclareCatchVariableName(
      Scope* scope, const PreParserIdentifier& identifier) {
    return scope->DeclareCatchVariableName(identifier.string_);
  }

1589 1590
  V8_INLINE PreParserPropertyList NewClassPropertyList(int size) const {
    return PreParserPropertyList();
1591 1592
  }

1593 1594 1595 1596
  V8_INLINE PreParserPropertyList NewClassStaticElementList(int size) const {
    return PreParserPropertyList();
  }

1597 1598 1599 1600
  V8_INLINE PreParserStatementList NewStatementList(int size) const {
    return PreParserStatementList();
  }

1601 1602
  V8_INLINE PreParserExpression
  NewV8Intrinsic(const PreParserIdentifier& name,
1603
                 const PreParserExpressionList& arguments, int pos) {
1604 1605 1606
    return PreParserExpression::Default();
  }

1607 1608
  V8_INLINE PreParserStatement
  NewThrowStatement(const PreParserExpression& exception, int pos) {
1609 1610 1611
    return PreParserStatement::Jump();
  }

1612 1613 1614 1615 1616
  V8_INLINE void AddFormalParameter(PreParserFormalParameters* parameters,
                                    const PreParserExpression& pattern,
                                    const PreParserExpression& initializer,
                                    int initializer_end_position,
                                    bool is_rest) {
1617
    DeclarationScope* scope = parameters->scope;
1618
    scope->RecordParameter(is_rest);
1619
    parameters->UpdateArityAndFunctionLength(!initializer.IsNull(), is_rest);
1620 1621
  }

1622
  V8_INLINE void DeclareFormalParameters(
1623 1624
      const PreParserFormalParameters* parameters) {
    if (!parameters->is_simple) parameters->scope->SetHasNonSimpleParameters();
1625 1626
  }

1627
  V8_INLINE void DeclareArrowFunctionFormalParameters(
1628
      PreParserFormalParameters* parameters, const PreParserExpression& params,
1629
      const Scanner::Location& params_loc) {
1630 1631 1632
  }

  V8_INLINE PreParserExpression
1633
  ExpressionListToExpression(const PreParserExpressionList& args) {
1634
    return PreParserExpression::Default();
1635 1636
  }

1637
  V8_INLINE void SetFunctionNameFromPropertyName(
1638
      const PreParserExpression& property, const PreParserIdentifier& name,
1639
      const AstRawString* prefix = nullptr) {}
1640
  V8_INLINE void SetFunctionNameFromIdentifierRef(
1641 1642
      const PreParserExpression& value, const PreParserExpression& identifier) {
  }
1643

1644 1645 1646 1647
  V8_INLINE void CountUsage(v8::Isolate::UseCounterFeature feature) {
    if (use_counts_ != nullptr) ++use_counts_[feature];
  }

1648 1649
  V8_INLINE bool ParsingDynamicFunctionDeclaration() const { return false; }

1650 1651 1652 1653 1654 1655 1656
// Generate empty functions here as the preparser does not collect source
// ranges for block coverage.
#define DEFINE_RECORD_SOURCE_RANGE(Name) \
  template <typename... Ts>              \
  V8_INLINE void Record##Name##SourceRange(Ts... args) {}
  AST_SOURCE_RANGE_LIST(DEFINE_RECORD_SOURCE_RANGE)
#undef DEFINE_RECORD_SOURCE_RANGE
1657

1658 1659
  // Preparser's private field members.

1660
  int* use_counts_;
1661
  PreParserLogger log_;
1662

1663
  PreparseDataBuilder* preparse_data_builder_;
1664
  std::vector<void*> preparse_data_builder_buffer_;
1665
};
1666

1667 1668
}  // namespace internal
}  // namespace v8
1669

1670
#endif  // V8_PARSING_PREPARSER_H_