parser.h 43.8 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_PARSER_H_
#define V8_PARSING_PARSER_H_
7

8 9
#include <cstddef>

10
#include "src/ast/ast-source-ranges.h"
11
#include "src/ast/ast-value-factory.h"
12 13
#include "src/ast/ast.h"
#include "src/ast/scopes.h"
14
#include "src/base/compiler-specific.h"
15
#include "src/base/threaded-list.h"
16
#include "src/common/globals.h"
17
#include "src/parsing/parse-info.h"
18
#include "src/parsing/parser-base.h"
19
#include "src/parsing/parsing.h"
20
#include "src/parsing/preparser.h"
21
#include "src/utils/pointer-with-payload.h"
22
#include "src/zone/zone-chunk-list.h"
23

24
namespace v8 {
25

26 27
class ScriptCompiler;

28
namespace internal {
29

30
class ConsumedPreparseData;
31
class ParseInfo;
32 33
class ParserTarget;
class ParserTargetScope;
34
class PendingCompilationErrorHandler;
35
class PreparseData;
36

37 38
// ----------------------------------------------------------------------------
// JAVASCRIPT PARSING
39

40
class Parser;
41

42

43
struct ParserFormalParameters : FormalParametersBase {
44
  struct Parameter : public ZoneObject {
45
    Parameter(Expression* pattern, Expression* initializer, int position,
46
              int initializer_end_position, bool is_rest)
47
        : initializer_and_is_rest(initializer, is_rest),
48
          pattern(pattern),
49
          position(position),
50 51
          initializer_end_position(initializer_end_position) {}

52
    PointerWithPayload<Expression, bool, 1> initializer_and_is_rest;
53

54
    Expression* pattern;
55 56 57
    Expression* initializer() const {
      return initializer_and_is_rest.GetPointer();
    }
58
    int position;
59
    int initializer_end_position;
60
    inline bool is_rest() const { return initializer_and_is_rest.GetPayload(); }
61

62
    Parameter* next_parameter = nullptr;
63
    bool is_simple() const {
64 65
      return pattern->IsVariableProxy() && initializer() == nullptr &&
             !is_rest();
66
    }
67

68 69 70 71 72
    const AstRawString* name() const {
      DCHECK(is_simple());
      return pattern->AsVariableProxy()->raw_name();
    }

73 74
    Parameter** next() { return &next_parameter; }
    Parameter* const* next() const { return &next_parameter; }
75 76
  };

77 78 79 80 81 82
  void set_strict_parameter_error(const Scanner::Location& loc,
                                  MessageTemplate message) {
    strict_error_loc = loc;
    strict_error_message = message;
  }

83
  bool has_duplicate() const { return duplicate_loc.IsValid(); }
84 85
  void ValidateDuplicate(Parser* parser) const;
  void ValidateStrictMode(Parser* parser) const;
86

87
  explicit ParserFormalParameters(DeclarationScope* scope)
88
      : FormalParametersBase(scope) {}
89

90
  base::ThreadedList<Parameter> params;
91
  Scanner::Location duplicate_loc = Scanner::Location::invalid();
92 93
  Scanner::Location strict_error_loc = Scanner::Location::invalid();
  MessageTemplate strict_error_message = MessageTemplate::kNone;
94 95
};

96
template <>
97
struct ParserTypes<Parser> {
98 99
  using Base = ParserBase<Parser>;
  using Impl = Parser;
100 101

  // Return types for traversing functions.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  using Block = v8::internal::Block*;
  using BreakableStatement = v8::internal::BreakableStatement*;
  using ClassLiteralProperty = ClassLiteral::Property*;
  using ClassPropertyList = ZonePtrList<ClassLiteral::Property>*;
  using Expression = v8::internal::Expression*;
  using ExpressionList = ScopedPtrList<v8::internal::Expression>;
  using FormalParameters = ParserFormalParameters;
  using ForStatement = v8::internal::ForStatement*;
  using FunctionLiteral = v8::internal::FunctionLiteral*;
  using Identifier = const AstRawString*;
  using IterationStatement = v8::internal::IterationStatement*;
  using ObjectLiteralProperty = ObjectLiteral::Property*;
  using ObjectPropertyList = ScopedPtrList<v8::internal::ObjectLiteralProperty>;
  using Statement = v8::internal::Statement*;
  using StatementList = ScopedPtrList<v8::internal::Statement>;
  using Suspend = v8::internal::Suspend*;
118 119

  // For constructing objects returned by the traversing functions.
120
  using Factory = AstNodeFactory;
121

122
  // Other implementation-specific functions.
123 124 125
  using FuncNameInferrer = v8::internal::FuncNameInferrer;
  using SourceRange = v8::internal::SourceRange;
  using SourceRangeScope = v8::internal::SourceRangeScope;
126 127
};

128
class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) {
129
 public:
130
  explicit Parser(ParseInfo* info);
131
  ~Parser() {
132
    delete reusable_preparser_;
133
    reusable_preparser_ = nullptr;
134
  }
135

136
  static bool IsPreParser() { return false; }
137

138
  // Sets the literal on |info| if parsing succeeded.
139 140
  void ParseOnBackground(ParseInfo* info, int start_position, int end_position,
                         int function_literal_id);
141

142 143 144 145
  // Initializes an empty scope chain for top-level scripts, or scopes which
  // consist of only the native context.
  void InitializeEmptyScopeChain(ParseInfo* info);

146 147
  // Deserialize the scope chain prior to parsing in which the script is going
  // to be executed. If the script is a top-level script, or the scope chain
148 149
  // consists of only a native context, maybe_outer_scope_info should be an
  // empty handle.
150 151 152 153
  //
  // This only deserializes the scope chain, but doesn't connect the scopes to
  // their corresponding scope infos. Therefore, looking up variables in the
  // deserialized scopes is not possible.
154
  void DeserializeScopeChain(Isolate* isolate, ParseInfo* info,
155 156 157
                             MaybeHandle<ScopeInfo> maybe_outer_scope_info,
                             Scope::DeserializationMode mode =
                                 Scope::DeserializationMode::kScopesOnly);
158

159 160
  // Move statistics to Isolate
  void UpdateStatistics(Isolate* isolate, Handle<Script> script);
161 162
  template <typename LocalIsolate>
  void HandleSourceURLComments(LocalIsolate* isolate, Handle<Script> script);
163

164
 private:
165
  friend class ParserBase<Parser>;
166 167
  friend struct ParserFormalParameters;
  friend class i::ExpressionScope<ParserTypes<Parser>>;
168 169 170
  friend class i::VariableDeclarationParsingScope<ParserTypes<Parser>>;
  friend class i::ParameterDeclarationParsingScope<ParserTypes<Parser>>;
  friend class i::ArrowHeadParsingScope<ParserTypes<Parser>>;
171
  friend bool v8::internal::parsing::ParseProgram(
172
      ParseInfo*, Handle<Script>, MaybeHandle<ScopeInfo> maybe_outer_scope_info,
173
      Isolate*, parsing::ReportStatisticsMode stats_mode);
174
  friend bool v8::internal::parsing::ParseFunction(
175
      ParseInfo*, Handle<SharedFunctionInfo> shared_info, Isolate*,
176
      parsing::ReportStatisticsMode stats_mode);
177

178
  bool AllowsLazyParsingWithoutUnresolvedVariables() const {
179 180 181
    return !MaybeParsingArrowhead() &&
           scope()->AllowsLazyParsingWithoutUnresolvedVariables(
               original_scope_);
182 183
  }

184 185 186
  bool parse_lazily() const { return mode_ == PARSE_LAZILY; }
  enum Mode { PARSE_LAZILY, PARSE_EAGERLY };

187
  class ParsingModeScope {
188 189 190 191 192 193 194 195 196 197 198 199
   public:
    ParsingModeScope(Parser* parser, Mode mode)
        : parser_(parser), old_mode_(parser->mode_) {
      parser_->mode_ = mode;
    }
    ~ParsingModeScope() { parser_->mode_ = old_mode_; }

   private:
    Parser* parser_;
    Mode old_mode_;
  };

200 201 202 203 204 205 206
  // Runtime encoding of different completion modes.
  enum CompletionKind {
    kNormalCompletion,
    kThrowCompletion,
    kAbruptCompletion
  };

207 208 209
  Variable* NewTemporary(const AstRawString* name) {
    return scope()->NewTemporary(name);
  }
210

211
  void PrepareGeneratorVariables();
212

213 214 215 216 217 218 219 220 221 222
  // Sets the literal on |info| if parsing succeeded.
  void ParseProgram(Isolate* isolate, Handle<Script> script, ParseInfo* info,
                    MaybeHandle<ScopeInfo> maybe_outer_scope_info);

  // Sets the literal on |info| if parsing succeeded.
  void ParseFunction(Isolate* isolate, ParseInfo* info,
                     Handle<SharedFunctionInfo> shared_info);

  void PostProcessParseResult(Isolate* isolate, ParseInfo* info,
                              FunctionLiteral* literal);
223

224
  FunctionLiteral* DoParseFunction(Isolate* isolate, ParseInfo* info,
225 226
                                   int start_position, int end_position,
                                   int function_literal_id,
227
                                   const AstRawString* raw_name);
228

229
  // Called by ParseProgram after setting up the scanner.
230
  FunctionLiteral* DoParseProgram(Isolate* isolate, ParseInfo* info);
231

232 233 234
  // Parse with the script as if the source is implicitly wrapped in a function.
  // We manually construct the AST and scopes for a top-level function and the
  // function wrapper.
235
  void ParseWrapped(Isolate* isolate, ParseInfo* info,
236
                    ScopedPtrList<Statement>* body, DeclarationScope* scope,
237
                    Zone* zone);
238

239 240 241 242
  void ParseREPLProgram(ParseInfo* info, ScopedPtrList<Statement>* body,
                        DeclarationScope* scope);
  Expression* WrapREPLResult(Expression* value);

243 244 245
  ZonePtrList<const AstRawString>* PrepareWrappedArguments(Isolate* isolate,
                                                           ParseInfo* info,
                                                           Zone* zone);
246

247
  PreParser* reusable_preparser() {
248
    if (reusable_preparser_ == nullptr) {
249 250
      reusable_preparser_ = new PreParser(
          &preparser_zone_, &scanner_, stack_limit_, ast_value_factory(),
251 252 253
          pending_error_handler(), runtime_call_stats_, logger_, flags(),
          parsing_on_main_thread_);
      reusable_preparser_->set_allow_eval_cache(allow_eval_cache());
254
      preparse_data_buffer_.reserve(128);
255 256 257 258
    }
    return reusable_preparser_;
  }

259
  void ParseModuleItemList(ScopedPtrList<Statement>* body);
260 261 262 263 264 265
  Statement* ParseModuleItem();
  const AstRawString* ParseModuleSpecifier();
  void ParseImportDeclaration();
  Statement* ParseExportDeclaration();
  Statement* ParseExportDefault();
  void ParseExportStar();
266 267 268 269 270 271
  struct ExportClauseData {
    const AstRawString* export_name;
    const AstRawString* local_name;
    Scanner::Location location;
  };
  ZoneChunkList<ExportClauseData>* ParseExportClause(
272
      Scanner::Location* reserved_loc);
273 274 275 276 277 278 279 280 281 282
  struct NamedImport : public ZoneObject {
    const AstRawString* import_name;
    const AstRawString* local_name;
    const Scanner::Location location;
    NamedImport(const AstRawString* import_name, const AstRawString* local_name,
                Scanner::Location location)
        : import_name(import_name),
          local_name(local_name),
          location(location) {}
  };
283
  ZonePtrList<const NamedImport>* ParseNamedImports(int pos);
284
  Statement* BuildInitializationBlock(DeclarationParsingResult* parsing_result);
285
  Expression* RewriteReturn(Expression* return_value, int pos);
286 287
  Statement* RewriteSwitchStatement(SwitchStatement* switch_statement,
                                    Scope* scope);
288
  Block* RewriteCatchPattern(CatchInfo* catch_info);
289
  void ReportVarRedeclarationIn(const AstRawString* name, Scope* scope);
290
  Statement* RewriteTryStatement(Block* try_block, Block* catch_block,
291
                                 const SourceRange& catch_range,
292
                                 Block* finally_block,
293
                                 const SourceRange& finally_range,
294
                                 const CatchInfo& catch_info, int pos);
295
  void ParseAndRewriteGeneratorFunctionBody(int pos, FunctionKind kind,
296 297 298
                                            ScopedPtrList<Statement>* body);
  void ParseAndRewriteAsyncGeneratorFunctionBody(
      int pos, FunctionKind kind, ScopedPtrList<Statement>* body);
299
  void DeclareFunctionNameVar(const AstRawString* function_name,
300
                              FunctionSyntaxKind function_syntax_kind,
301
                              DeclarationScope* function_scope);
302

303
  Statement* DeclareFunction(const AstRawString* variable_name,
304
                             FunctionLiteral* function, VariableMode mode,
305
                             VariableKind kind, int beg_pos, int end_pos,
306 307
                             ZonePtrList<const AstRawString>* names);
  Variable* CreateSyntheticContextVariable(const AstRawString* synthetic_name);
308
  Variable* CreatePrivateNameVariable(ClassScope* scope, VariableMode mode,
309
                                      IsStaticFlag is_static_flag,
310
                                      const AstRawString* name);
311
  FunctionLiteral* CreateInitializerFunction(
312 313
      const char* name, DeclarationScope* scope,
      ZonePtrList<ClassLiteral::Property>* fields);
314

315 316 317 318 319
  bool IdentifierEquals(const AstRawString* identifier,
                        const AstRawString* other) {
    return identifier == other;
  }

320 321 322
  Statement* DeclareClass(const AstRawString* variable_name, Expression* value,
                          ZonePtrList<const AstRawString>* names,
                          int class_token_pos, int end_pos);
323 324
  void DeclareClassVariable(ClassScope* scope, const AstRawString* name,
                            ClassInfo* class_info, int class_token_pos);
325 326 327 328 329 330 331 332 333 334 335 336 337
  void DeclareClassBrandVariable(ClassScope* scope, ClassInfo* class_info,
                                 int class_token_pos);
  void DeclarePrivateClassMember(ClassScope* scope,
                                 const AstRawString* property_name,
                                 ClassLiteralProperty* property,
                                 ClassLiteralProperty::Kind kind,
                                 bool is_static, ClassInfo* class_info);
  void DeclarePublicClassMethod(const AstRawString* class_name,
                                ClassLiteralProperty* property,
                                bool is_constructor, ClassInfo* class_info);
  void DeclarePublicClassField(ClassScope* scope,
                               ClassLiteralProperty* property, bool is_static,
                               bool is_computed_name, ClassInfo* class_info);
338
  void DeclareClassProperty(ClassScope* scope, const AstRawString* class_name,
339 340
                            ClassLiteralProperty* property, bool is_constructor,
                            ClassInfo* class_info);
341
  void DeclareClassField(ClassScope* scope, ClassLiteralProperty* property,
342 343 344
                         const AstRawString* property_name, bool is_static,
                         bool is_computed_name, bool is_private,
                         ClassInfo* class_info);
345 346
  Expression* RewriteClassLiteral(ClassScope* block_scope,
                                  const AstRawString* name,
347 348 349 350 351 352
                                  ClassInfo* class_info, int pos, int end_pos);
  Statement* DeclareNative(const AstRawString* name, int pos);

  Block* IgnoreCompletion(Statement* statement);

  Scope* NewHiddenCatchScope();
353

354 355 356 357
  bool HasCheckedSyntax() {
    return scope()->GetDeclarationScope()->has_checked_syntax();
  }

358
  void InitializeVariables(
359
      ScopedPtrList<Statement>* statements, VariableKind kind,
360
      const DeclarationParsingResult::Declaration* declaration);
361

362 363
  Block* RewriteForVarInLegacy(const ForInfo& for_info);
  void DesugarBindingInForEachStatement(ForInfo* for_info, Block** body_block,
364 365
                                        Expression** each_variable);
  Block* CreateForEachStatementTDZ(Block* init_block, const ForInfo& for_info);
366

rossberg's avatar
rossberg committed
367
  Statement* DesugarLexicalBindingsInForStatement(
368
      ForStatement* loop, Statement* init, Expression* cond, Statement* next,
369
      Statement* body, Scope* inner_scope, const ForInfo& for_info);
370

371
  FunctionLiteral* ParseFunctionLiteral(
372
      const AstRawString* name, Scanner::Location function_name_location,
373
      FunctionNameValidity function_name_validity, FunctionKind kind,
374
      int function_token_position, FunctionSyntaxKind type,
375
      LanguageMode language_mode,
376
      ZonePtrList<const AstRawString>* arguments_for_wrapped_function);
377

378 379 380 381 382
  ObjectLiteral* InitializeObjectLiteral(ObjectLiteral* object_literal) {
    object_literal->CalculateEmitStore(main_zone());
    return object_literal;
  }

383 384 385 386
  // Insert initializer statements for var-bindings shadowing parameter bindings
  // from a non-simple parameter list.
  void InsertShadowingVarBindingInitializers(Block* block);

387
  // Implement sloppy block-scoped functions, ES2015 Annex B 3.3
388
  void InsertSloppyBlockFunctionVarBindings(DeclarationScope* scope);
389

390 391 392 393 394 395
  void DeclareUnboundVariable(const AstRawString* name, VariableMode mode,
                              InitializationFlag init, int pos);
  V8_WARN_UNUSED_RESULT
  VariableProxy* DeclareBoundVariable(const AstRawString* name,
                                      VariableMode mode, int pos);
  void DeclareAndBindVariable(VariableProxy* proxy, VariableKind kind,
396 397
                              VariableMode mode, Scope* declaration_scope,
                              bool* was_added, int initializer_position);
398 399 400 401 402 403
  V8_WARN_UNUSED_RESULT
  Variable* DeclareVariable(const AstRawString* name, VariableKind kind,
                            VariableMode mode, InitializationFlag init,
                            Scope* declaration_scope, bool* was_added,
                            int begin, int end = kNoSourcePosition);
  void Declare(Declaration* declaration, const AstRawString* name,
404
               VariableKind kind, VariableMode mode, InitializationFlag init,
405
               Scope* declaration_scope, bool* was_added, int var_begin_pos,
406
               int var_end_pos = kNoSourcePosition);
407

408
  // Factory methods.
409
  FunctionLiteral* DefaultConstructor(const AstRawString* name, bool call_super,
410
                                      int pos, int end_pos);
411

412 413
  // Skip over a lazy function, either using cached data if we have it, or
  // by parsing the function with PreParser. Consumes the ending }.
414 415 416 417 418 419 420
  // In case the preparser detects an error it cannot identify, it resets the
  // scanner- and preparser state to the initial one, before PreParsing the
  // function.
  // SkipFunction returns true if it correctly parsed the function, including
  // cases where we detect an error. It returns false, if we needed to stop
  // parsing or could not identify an error correctly, meaning the caller needs
  // to fully reparse. In this case it resets the scanner and preparser state.
421
  bool SkipFunction(const AstRawString* function_name, FunctionKind kind,
422
                    FunctionSyntaxKind function_syntax_kind,
423
                    DeclarationScope* function_scope, int* num_parameters,
424
                    int* function_length,
425
                    ProducedPreparseData** produced_preparsed_scope_data);
426

427
  Block* BuildParameterInitializationBlock(
428
      const ParserFormalParameters& parameters);
429 430
  Block* BuildRejectPromiseOnException(Block* block,
                                       REPLMode repl_mode = REPLMode::kNo);
431

432 433
  void ParseFunction(
      ScopedPtrList<Statement>* body, const AstRawString* function_name,
434
      int pos, FunctionKind kind, FunctionSyntaxKind function_syntax_kind,
435 436
      DeclarationScope* function_scope, int* num_parameters,
      int* function_length, bool* has_duplicate_parameters,
437
      int* expected_property_count, int* suspend_count,
438
      ZonePtrList<const AstRawString>* arguments_for_wrapped_function);
439

440
  void ThrowPendingError(Isolate* isolate, Handle<Script> script);
441

442 443 444 445 446
  class TemplateLiteral : public ZoneObject {
   public:
    TemplateLiteral(Zone* zone, int pos)
        : cooked_(8, zone), raw_(8, zone), expressions_(8, zone), pos_(pos) {}

447 448 449
    const ZonePtrList<const AstRawString>* cooked() const { return &cooked_; }
    const ZonePtrList<const AstRawString>* raw() const { return &raw_; }
    const ZonePtrList<Expression>* expressions() const { return &expressions_; }
450 451
    int position() const { return pos_; }

452 453
    void AddTemplateSpan(const AstRawString* cooked, const AstRawString* raw,
                         int end, Zone* zone) {
454 455 456 457 458 459 460 461 462 463
      DCHECK_NOT_NULL(raw);
      cooked_.Add(cooked, zone);
      raw_.Add(raw, zone);
    }

    void AddExpression(Expression* expression, Zone* zone) {
      expressions_.Add(expression, zone);
    }

   private:
464 465 466
    ZonePtrList<const AstRawString> cooked_;
    ZonePtrList<const AstRawString> raw_;
    ZonePtrList<Expression> expressions_;
467 468 469
    int pos_;
  };

470
  using TemplateLiteralState = TemplateLiteral*;
471

472
  TemplateLiteralState OpenTemplateLiteral(int pos);
473 474 475
  // "should_cook" means that the span can be "cooked": in tagged template
  // literals, both the raw and "cooked" representations are available to user
  // code ("cooked" meaning that escape sequences are converted to their
476 477
  // interpreted values). Invalid escape sequences cause the cooked span
  // to be represented by undefined, instead of being a syntax error.
478 479 480
  // "tail" indicates that this span is the last in the literal.
  void AddTemplateSpan(TemplateLiteralState* state, bool should_cook,
                       bool tail);
481 482 483 484
  void AddTemplateExpression(TemplateLiteralState* state,
                             Expression* expression);
  Expression* CloseTemplateLiteral(TemplateLiteralState* state, int start,
                                   Expression* tag);
485

486 487 488 489
  ArrayLiteral* ArrayLiteralFromListWithSpread(
      const ScopedPtrList<Expression>& list);
  Expression* SpreadCall(Expression* function,
                         const ScopedPtrList<Expression>& args, int pos,
490 491
                         Call::PossiblyEval is_possibly_eval,
                         bool optional_chain);
492 493
  Expression* SpreadCallNew(Expression* function,
                            const ScopedPtrList<Expression>& args, int pos);
494
  Expression* RewriteSuperCall(Expression* call_expression);
495

496
  void SetLanguageMode(Scope* scope, LanguageMode mode);
497
  void SetAsmModule();
498

499
  Expression* RewriteSpreads(ArrayLiteral* lit);
nikolaos's avatar
nikolaos committed
500

501
  Expression* BuildInitialYield(int pos, FunctionKind kind);
502
  Assignment* BuildCreateJSGeneratorObject(int pos, FunctionKind kind);
503

504 505
  // Generic AST generator for throwing errors from compiled code.
  Expression* NewThrowError(Runtime::FunctionId function_id,
506 507
                            MessageTemplate message, const AstRawString* arg,
                            int pos);
508 509 510

  Statement* CheckCallable(Variable* var, Expression* error, int pos);

511
  void RewriteAsyncFunctionBody(ScopedPtrList<Statement>* body, Block* block,
512 513
                                Expression* return_value,
                                REPLMode repl_mode = REPLMode::kNo);
514

515
  void AddArrowFunctionFormalParameters(ParserFormalParameters* parameters,
516
                                        Expression* params, int end_pos);
517 518
  void SetFunctionName(Expression* value, const AstRawString* name,
                       const AstRawString* prefix = nullptr);
519

520 521 522 523 524
  // Helper functions for recursive descent.
  V8_INLINE bool IsEval(const AstRawString* identifier) const {
    return identifier == ast_value_factory()->eval_string();
  }

525 526 527 528
  V8_INLINE bool IsAsync(const AstRawString* identifier) const {
    return identifier == ast_value_factory()->async_string();
  }

529 530 531 532 533 534 535 536 537 538
  V8_INLINE bool IsArguments(const AstRawString* identifier) const {
    return identifier == ast_value_factory()->arguments_string();
  }

  V8_INLINE bool IsEvalOrArguments(const AstRawString* identifier) const {
    return IsEval(identifier) || IsArguments(identifier);
  }

  // Returns true if the expression is of type "this.foo".
  V8_INLINE static bool IsThisProperty(Expression* expression) {
539
    DCHECK_NOT_NULL(expression);
540
    Property* property = expression->AsProperty();
541
    return property != nullptr && property->obj()->IsThisExpression();
542 543
  }

544
  // Returns true if the expression is of type "obj.#foo" or "obj?.#foo".
545 546 547
  V8_INLINE static bool IsPrivateReference(Expression* expression) {
    DCHECK_NOT_NULL(expression);
    Property* property = expression->AsProperty();
548 549 550 551
    if (expression->IsOptionalChain()) {
      Expression* expr_inner = expression->AsOptionalChain()->expression();
      property = expr_inner->AsProperty();
    }
552 553 554
    return property != nullptr && property->IsPrivateReference();
  }

555 556 557
  // This returns true if the expression is an indentifier (wrapped
  // inside a variable proxy).  We exclude the case of 'this', which
  // has been converted to a variable proxy.
558 559
  V8_INLINE static bool IsIdentifier(Expression* expression) {
    VariableProxy* operand = expression->AsVariableProxy();
560
    return operand != nullptr && !operand->is_new_target();
561 562 563 564 565 566 567
  }

  V8_INLINE static const AstRawString* AsIdentifier(Expression* expression) {
    DCHECK(IsIdentifier(expression));
    return expression->AsVariableProxy()->raw_name();
  }

568 569 570 571
  V8_INLINE VariableProxy* AsIdentifierExpression(Expression* expression) {
    return expression->AsVariableProxy();
  }

572 573 574 575
  V8_INLINE bool IsConstructor(const AstRawString* identifier) const {
    return identifier == ast_value_factory()->constructor_string();
  }

576 577 578 579
  V8_INLINE bool IsName(const AstRawString* identifier) const {
    return identifier == ast_value_factory()->name_string();
  }

580 581
  V8_INLINE static bool IsBoilerplateProperty(
      ObjectLiteral::Property* property) {
582
    return !property->IsPrototype();
583 584
  }

585 586 587 588 589 590 591
  V8_INLINE bool IsNative(Expression* expr) const {
    DCHECK_NOT_NULL(expr);
    return expr->IsVariableProxy() &&
           expr->AsVariableProxy()->raw_name() ==
               ast_value_factory()->native_string();
  }

592 593 594 595 596
  V8_INLINE static bool IsArrayIndex(const AstRawString* string,
                                     uint32_t* index) {
    return string->AsArrayIndex(index);
  }

597 598 599 600 601 602 603 604
  // Returns true if the statement is an expression statement containing
  // a single string literal.  If a second argument is given, the literal
  // is also compared with it and the result is true only if they are equal.
  V8_INLINE bool IsStringLiteral(Statement* statement,
                                 const AstRawString* arg = nullptr) const {
    ExpressionStatement* e_stat = statement->AsExpressionStatement();
    if (e_stat == nullptr) return false;
    Literal* literal = e_stat->expression()->AsLiteral();
605 606
    if (literal == nullptr || !literal->IsString()) return false;
    return arg == nullptr || literal->AsRawString() == arg;
607 608
  }

609 610
  V8_INLINE void GetDefaultStrings(const AstRawString** default_string,
                                   const AstRawString** dot_default_string) {
611
    *default_string = ast_value_factory()->default_string();
612
    *dot_default_string = ast_value_factory()->dot_default_string();
613 614
  }

615 616
  // Functions for encapsulating the differences between parsing and preparsing;
  // operations interleaved with the recursive descent.
617
  V8_INLINE void PushLiteralName(const AstRawString* id) {
618
    fni_.PushLiteralName(id);
619 620
  }

621
  V8_INLINE void PushVariableName(const AstRawString* id) {
622
    fni_.PushVariableName(id);
623 624
  }

625
  V8_INLINE void PushPropertyName(Expression* expression) {
626
    if (expression->IsPropertyName()) {
627
      fni_.PushLiteralName(expression->AsLiteral()->AsRawPropertyName());
628
    } else {
629
      fni_.PushLiteralName(ast_value_factory()->computed_string());
630 631 632
    }
  }

633
  V8_INLINE void PushEnclosingName(const AstRawString* name) {
634
    fni_.PushEnclosingName(name);
635 636
  }

637
  V8_INLINE void AddFunctionForNameInference(FunctionLiteral* func_to_infer) {
638
    fni_.AddFunction(func_to_infer);
639 640
  }

641
  V8_INLINE void InferFunctionName() { fni_.Infer(); }
642

643 644 645 646 647 648 649 650 651 652
  // If we assign a function literal to a property we pretenure the
  // literal so it can be added as a constant function property.
  V8_INLINE static void CheckAssigningFunctionLiteralToProperty(
      Expression* left, Expression* right) {
    DCHECK_NOT_NULL(left);
    if (left->IsProperty() && right->IsFunctionLiteral()) {
      right->AsFunctionLiteral()->set_pretenure();
    }
  }

653 654 655
  // A shortcut for performing a ToString operation
  V8_INLINE Expression* ToString(Expression* expr) {
    if (expr->IsStringLiteral()) return expr;
656
    ScopedPtrList<Expression> args(pointer_buffer());
657
    args.Add(expr);
Shiyu Zhang's avatar
Shiyu Zhang committed
658
    return factory()->NewCallRuntime(Runtime::kInlineToStringRT, args,
659 660 661
                                     expr->position());
  }

662 663 664 665 666 667
  // Returns true if we have a binary expression between two numeric
  // literals. In that case, *x will be changed to an expression which is the
  // computed value.
  bool ShortcutNumericLiteralBinaryExpression(Expression** x, Expression* y,
                                              Token::Value op, int pos);

668 669 670 671 672
  // Returns true if we have a binary operation between a binary/n-ary
  // expression (with the same operation) and a value, which can be collapsed
  // into a single n-ary expression. In that case, *x will be changed to an
  // n-ary expression.
  bool CollapseNaryExpression(Expression** x, Expression* y, Token::Value op,
673
                              int pos, const SourceRange& range);
674

675
  // Returns a UnaryExpression or, in one of the following cases, a Literal.
676
  // ! <literal> -> true / false
677 678 679
  // + <Number literal> -> <Number literal>
  // - <Number literal> -> <Number literal with value negated>
  // ~ <literal> -> true / false
680 681 682 683
  Expression* BuildUnaryExpression(Expression* expression, Token::Value op,
                                   int pos);

  // Generate AST node that throws a ReferenceError with the given type.
684 685
  V8_INLINE Expression* NewThrowReferenceError(MessageTemplate message,
                                               int pos) {
686 687 688 689 690 691 692
    return NewThrowError(Runtime::kNewReferenceError, message,
                         ast_value_factory()->empty_string(), pos);
  }

  // Generate AST node that throws a SyntaxError with the given
  // type. The first argument may be null (in the handle sense) in
  // which case no arguments are passed to the constructor.
693
  V8_INLINE Expression* NewThrowSyntaxError(MessageTemplate message,
694 695 696 697 698 699
                                            const AstRawString* arg, int pos) {
    return NewThrowError(Runtime::kNewSyntaxError, message, arg, pos);
  }

  // Generate AST node that throws a TypeError with the given
  // type. Both arguments must be non-null (in the handle sense).
700
  V8_INLINE Expression* NewThrowTypeError(MessageTemplate message,
701 702 703 704 705
                                          const AstRawString* arg, int pos) {
    return NewThrowError(Runtime::kNewTypeError, message, arg, pos);
  }

  // Reporting errors.
706
  void ReportMessageAt(Scanner::Location source_location,
707 708 709
                       MessageTemplate message, const char* arg = nullptr) {
    pending_error_handler()->ReportMessageAt(
        source_location.beg_pos, source_location.end_pos, message, arg);
710
    scanner_.set_parser_error();
711 712
  }

713 714 715 716
  // Dummy implementation. The parser should never have a unidentifiable
  // error.
  V8_INLINE void ReportUnidentifiableError() { UNREACHABLE(); }

717
  void ReportMessageAt(Scanner::Location source_location,
718 719 720
                       MessageTemplate message, const AstRawString* arg) {
    pending_error_handler()->ReportMessageAt(
        source_location.beg_pos, source_location.end_pos, message, arg);
721
    scanner_.set_parser_error();
722 723
  }

724 725 726 727
  const AstRawString* GetRawNameFromIdentifier(const AstRawString* arg) {
    return arg;
  }

728 729 730 731
  IterationStatement* AsIterationStatement(BreakableStatement* s) {
    return s->AsIterationStatement();
  }

732 733 734 735
  void ReportUnexpectedTokenAt(
      Scanner::Location location, Token::Value token,
      MessageTemplate message = MessageTemplate::kUnexpectedToken);

736
  // "null" return type creators.
737 738 739
  V8_INLINE static std::nullptr_t NullIdentifier() { return nullptr; }
  V8_INLINE static std::nullptr_t NullExpression() { return nullptr; }
  V8_INLINE static std::nullptr_t NullLiteralProperty() { return nullptr; }
740 741 742 743
  V8_INLINE static ZonePtrList<Expression>* NullExpressionList() {
    return nullptr;
  }
  V8_INLINE static ZonePtrList<Statement>* NullStatementList() {
744 745
    return nullptr;
  }
746
  V8_INLINE static std::nullptr_t NullStatement() { return nullptr; }
747
  V8_INLINE static std::nullptr_t NullBlock() { return nullptr; }
748
  Expression* FailureExpression() { return factory()->FailureExpression(); }
749 750 751 752

  template <typename T>
  V8_INLINE static bool IsNull(T subject) {
    return subject == nullptr;
753
  }
754

755 756 757 758
  V8_INLINE static bool IsIterationStatement(Statement* subject) {
    return subject->AsIterationStatement() != nullptr;
  }

759
  // Non-null empty string.
760 761 762 763
  V8_INLINE const AstRawString* EmptyIdentifierString() const {
    return ast_value_factory()->empty_string();
  }

764 765 766
  // Producing data during the recursive descent.
  V8_INLINE const AstRawString* GetSymbol() const {
    const AstRawString* result = scanner()->CurrentSymbol(ast_value_factory());
767
    DCHECK_NOT_NULL(result);
768 769 770
    return result;
  }

771 772
  V8_INLINE const AstRawString* GetIdentifier() const { return GetSymbol(); }

773 774 775 776 777 778 779 780
  V8_INLINE const AstRawString* GetNextSymbol() const {
    return scanner()->NextSymbol(ast_value_factory());
  }

  V8_INLINE const AstRawString* GetNumberAsSymbol() const {
    double double_value = scanner()->DoubleValue();
    char array[100];
    const char* string = DoubleToCString(double_value, ArrayVector(array));
781
    return ast_value_factory()->GetOneByteString(string);
782 783
  }

784 785 786
  class ThisExpression* ThisExpression() {
    UseThis();
    return factory()->ThisExpression();
787 788
  }

789 790 791 792 793
  class ThisExpression* NewThisExpression(int pos) {
    UseThis();
    return factory()->NewThisExpression(pos);
  }

794 795 796
  Expression* NewSuperPropertyReference(int pos);
  Expression* NewSuperCallReference(int pos);
  Expression* NewTargetExpression(int pos);
797
  Expression* ImportMetaExpression(int pos);
798

799
  Expression* ExpressionFromLiteral(Token::Value token, int pos);
800

801 802 803
  V8_INLINE VariableProxy* ExpressionFromPrivateName(
      PrivateNameScopeIterator* private_name_scope, const AstRawString* name,
      int start_position) {
804 805
    VariableProxy* proxy = factory()->ast_node_factory()->NewVariableProxy(
        name, NORMAL_VARIABLE, start_position);
806
    private_name_scope->AddUnresolvedPrivateName(proxy);
807 808 809
    return proxy;
  }

810
  V8_INLINE VariableProxy* ExpressionFromIdentifier(
811
      const AstRawString* name, int start_position,
812
      InferName infer = InferName::kYes) {
813
    if (infer == InferName::kYes) {
814
      fni_.PushVariableName(name);
815
    }
816
    return expression_scope()->NewVariable(name, start_position);
817 818
  }

819 820 821 822 823
  V8_INLINE void DeclareIdentifier(const AstRawString* name,
                                   int start_position) {
    expression_scope()->Declare(name, start_position);
  }

824 825 826 827 828
  V8_INLINE Variable* DeclareCatchVariableName(Scope* scope,
                                               const AstRawString* name) {
    return scope->DeclareCatchVariableName(name);
  }

829
  V8_INLINE ZonePtrList<Expression>* NewExpressionList(int size) const {
830
    return zone()->New<ZonePtrList<Expression>>(size, zone());
831
  }
832
  V8_INLINE ZonePtrList<ObjectLiteral::Property>* NewObjectPropertyList(
833
      int size) const {
834
    return zone()->New<ZonePtrList<ObjectLiteral::Property>>(size, zone());
835
  }
836
  V8_INLINE ZonePtrList<ClassLiteral::Property>* NewClassPropertyList(
837
      int size) const {
838
    return zone()->New<ZonePtrList<ClassLiteral::Property>>(size, zone());
839
  }
840
  V8_INLINE ZonePtrList<Statement>* NewStatementList(int size) const {
841
    return zone()->New<ZonePtrList<Statement>>(size, zone());
842 843
  }

844 845
  Expression* NewV8Intrinsic(const AstRawString* name,
                             const ScopedPtrList<Expression>& args, int pos);
846

847 848 849 850
  Expression* NewV8RuntimeFunctionForFuzzing(
      const Runtime::Function* function, const ScopedPtrList<Expression>& args,
      int pos);

851
  V8_INLINE Statement* NewThrowStatement(Expression* exception, int pos) {
852
    return factory()->NewExpressionStatement(
853
        factory()->NewThrow(exception, pos), pos);
854 855
  }

856 857 858 859 860
  V8_INLINE void AddFormalParameter(ParserFormalParameters* parameters,
                                    Expression* pattern,
                                    Expression* initializer,
                                    int initializer_end_position,
                                    bool is_rest) {
861
    parameters->UpdateArityAndFunctionLength(initializer != nullptr, is_rest);
862 863 864 865
    auto parameter =
        parameters->scope->zone()->New<ParserFormalParameters::Parameter>(
            pattern, initializer, scanner()->location().beg_pos,
            initializer_end_position, is_rest);
866 867

    parameters->params.Add(parameter);
868 869
  }

870
  V8_INLINE void DeclareFormalParameters(ParserFormalParameters* parameters) {
871 872
    bool is_simple = parameters->is_simple;
    DeclarationScope* scope = parameters->scope;
873
    if (!is_simple) scope->MakeParametersNonSimple();
874
    for (auto parameter : parameters->params) {
875
      bool is_optional = parameter->initializer() != nullptr;
876 877 878 879
      // If the parameter list is simple, declare the parameters normally with
      // their names. If the parameter list is not simple, declare a temporary
      // for each parameter - the corresponding named variable is declared by
      // BuildParamerterInitializationBlock.
880
      scope->DeclareParameter(
881
          is_simple ? parameter->name() : ast_value_factory()->empty_string(),
882
          is_simple ? VariableMode::kVar : VariableMode::kTemporary,
883
          is_optional, parameter->is_rest(), ast_value_factory(),
884
          parameter->position);
885 886 887
    }
  }

888 889 890
  void DeclareArrowFunctionFormalParameters(
      ParserFormalParameters* parameters, Expression* params,
      const Scanner::Location& params_loc);
891

892
  Expression* ExpressionListToExpression(const ScopedPtrList<Expression>& args);
893

894 895 896
  void SetFunctionNameFromPropertyName(LiteralProperty* property,
                                       const AstRawString* name,
                                       const AstRawString* prefix = nullptr);
897
  void SetFunctionNameFromPropertyName(ObjectLiteralProperty* property,
898 899
                                       const AstRawString* name,
                                       const AstRawString* prefix = nullptr);
900 901 902 903

  void SetFunctionNameFromIdentifierRef(Expression* value,
                                        Expression* identifier);

904 905 906 907
  V8_INLINE void CountUsage(v8::Isolate::UseCounterFeature feature) {
    ++use_counts_[feature];
  }

908 909 910 911 912 913
  // Returns true iff we're parsing the first function literal during
  // CreateDynamicFunction().
  V8_INLINE bool ParsingDynamicFunctionDeclaration() const {
    return parameters_end_pos_ != kNoSourcePosition;
  }

914 915 916 917 918 919 920 921 922 923 924 925
  V8_INLINE void ConvertBinaryToNaryOperationSourceRange(
      BinaryOperation* binary_op, NaryOperation* nary_op) {
    if (source_range_map_ == nullptr) return;
    DCHECK_NULL(source_range_map_->Find(nary_op));

    BinaryOperationSourceRanges* ranges =
        static_cast<BinaryOperationSourceRanges*>(
            source_range_map_->Find(binary_op));
    if (ranges == nullptr) return;

    SourceRange range = ranges->GetRange(SourceRangeKind::kRight);
    source_range_map_->Insert(
926
        nary_op, zone()->New<NaryOperationSourceRanges>(zone(), range));
927 928 929 930 931 932 933 934 935 936 937 938 939
  }

  V8_INLINE void AppendNaryOperationSourceRange(NaryOperation* node,
                                                const SourceRange& range) {
    if (source_range_map_ == nullptr) return;
    NaryOperationSourceRanges* ranges =
        static_cast<NaryOperationSourceRanges*>(source_range_map_->Find(node));
    if (ranges == nullptr) return;

    ranges->AddRange(range);
    DCHECK_EQ(node->subsequent_length(), ranges->RangeCount());
  }

940 941 942 943
  V8_INLINE void RecordBlockSourceRange(Block* node,
                                        int32_t continuation_position) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(
944
        node, zone()->New<BlockSourceRanges>(continuation_position));
945 946
  }

947 948 949 950
  V8_INLINE void RecordCaseClauseSourceRange(CaseClause* node,
                                             const SourceRange& body_range) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(node,
951
                              zone()->New<CaseClauseSourceRanges>(body_range));
952 953
  }

954 955 956 957 958 959
  V8_INLINE void RecordConditionalSourceRange(Expression* node,
                                              const SourceRange& then_range,
                                              const SourceRange& else_range) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(
        node->AsConditional(),
960
        zone()->New<ConditionalSourceRanges>(then_range, else_range));
961 962
  }

963 964
  V8_INLINE void RecordFunctionLiteralSourceRange(FunctionLiteral* node) {
    if (source_range_map_ == nullptr) return;
965
    source_range_map_->Insert(node, zone()->New<FunctionLiteralSourceRanges>());
966 967
  }

968 969 970
  V8_INLINE void RecordBinaryOperationSourceRange(
      Expression* node, const SourceRange& right_range) {
    if (source_range_map_ == nullptr) return;
971 972 973
    source_range_map_->Insert(
        node->AsBinaryOperation(),
        zone()->New<BinaryOperationSourceRanges>(right_range));
974 975
  }

976 977 978 979 980
  V8_INLINE void RecordJumpStatementSourceRange(Statement* node,
                                                int32_t continuation_position) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(
        static_cast<JumpStatement*>(node),
981
        zone()->New<JumpStatementSourceRanges>(continuation_position));
982 983 984 985 986 987 988 989
  }

  V8_INLINE void RecordIfStatementSourceRange(Statement* node,
                                              const SourceRange& then_range,
                                              const SourceRange& else_range) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(
        node->AsIfStatement(),
990
        zone()->New<IfStatementSourceRanges>(then_range, else_range));
991 992 993 994 995 996
  }

  V8_INLINE void RecordIterationStatementSourceRange(
      IterationStatement* node, const SourceRange& body_range) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(
997
        node, zone()->New<IterationStatementSourceRanges>(body_range));
998 999
  }

1000 1001 1002
  V8_INLINE void RecordSuspendSourceRange(Expression* node,
                                          int32_t continuation_position) {
    if (source_range_map_ == nullptr) return;
1003 1004 1005
    source_range_map_->Insert(
        static_cast<Suspend*>(node),
        zone()->New<SuspendSourceRanges>(continuation_position));
1006 1007
  }

1008 1009 1010 1011 1012
  V8_INLINE void RecordSwitchStatementSourceRange(
      Statement* node, int32_t continuation_position) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(
        node->AsSwitchStatement(),
1013
        zone()->New<SwitchStatementSourceRanges>(continuation_position));
1014 1015 1016 1017 1018 1019 1020 1021
  }

  V8_INLINE void RecordThrowSourceRange(Statement* node,
                                        int32_t continuation_position) {
    if (source_range_map_ == nullptr) return;
    ExpressionStatement* expr_stmt = static_cast<ExpressionStatement*>(node);
    Throw* throw_expr = expr_stmt->expression()->AsThrow();
    source_range_map_->Insert(
1022
        throw_expr, zone()->New<ThrowSourceRanges>(continuation_position));
1023 1024 1025 1026 1027 1028
  }

  V8_INLINE void RecordTryCatchStatementSourceRange(
      TryCatchStatement* node, const SourceRange& body_range) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(
1029
        node, zone()->New<TryCatchStatementSourceRanges>(body_range));
1030 1031 1032 1033 1034 1035
  }

  V8_INLINE void RecordTryFinallyStatementSourceRange(
      TryFinallyStatement* node, const SourceRange& body_range) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(
1036
        node, zone()->New<TryFinallyStatementSourceRanges>(body_range));
1037 1038
  }

1039 1040 1041 1042
  // Generate the next internal variable name for binding an exported namespace
  // object (used to implement the "export * as" syntax).
  const AstRawString* NextInternalNamespaceExportName();

1043 1044
  ParseInfo* info() const { return info_; }

1045 1046 1047 1048
  std::vector<uint8_t>* preparse_data_buffer() {
    return &preparse_data_buffer_;
  }

1049
  // Parser's private field members.
1050
  friend class PreParserZoneScope;  // Uses reusable_preparser().
1051
  friend class PreparseDataBuilder;  // Uses preparse_data_buffer()
1052

1053
  ParseInfo* info_;
1054
  Scanner scanner_;
1055
  Zone preparser_zone_;
1056
  PreParser* reusable_preparser_;
1057
  Mode mode_;
1058

1059 1060
  MaybeHandle<FixedArray> maybe_wrapped_arguments_;

1061 1062
  SourceRangeMap* source_range_map_ = nullptr;

1063 1064
  friend class ParserTargetScope;

1065
  ScriptCompiler::CompileOptions compile_options_;
1066

1067 1068 1069
  // For NextInternalNamespaceExportName().
  int number_of_named_namespace_exports_ = 0;

1070 1071
  // Other information which will be stored in Parser and moved to Isolate after
  // parsing.
1072
  int use_counts_[v8::Isolate::kUseCounterFeatureCount];
1073
  int total_preparse_skipped_;
1074
  bool allow_lazy_;
1075
  bool temp_zoned_;
1076
  ConsumedPreparseData* consumed_preparse_data_;
1077
  std::vector<uint8_t> preparse_data_buffer_;
1078

1079 1080 1081 1082 1083
  // If not kNoSourcePosition, indicates that the first function literal
  // encountered is a dynamic function, see CreateDynamicFunction(). This field
  // indicates the correct position of the ')' that closes the parameter list.
  // After that ')' is encountered, this field is reset to kNoSourcePosition.
  int parameters_end_pos_;
1084
};
1085

1086 1087
}  // namespace internal
}  // namespace v8
1088

1089
#endif  // V8_PARSING_PARSER_H_