parser.h 43.5 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 139
  void ParseOnBackground(ParseInfo* info, int start_position, int end_position,
                         int function_literal_id);
140

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

145 146
  // 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
147 148
  // consists of only a native context, maybe_outer_scope_info should be an
  // empty handle.
149 150 151 152
  //
  // 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.
153
  void DeserializeScopeChain(Isolate* isolate, ParseInfo* info,
154 155 156
                             MaybeHandle<ScopeInfo> maybe_outer_scope_info,
                             Scope::DeserializationMode mode =
                                 Scope::DeserializationMode::kScopesOnly);
157

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

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

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

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

186
  class ParsingModeScope {
187 188 189 190 191 192 193 194 195 196 197 198
   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_;
  };

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

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

210
  void PrepareGeneratorVariables();
211

212
  // Returns nullptr if parsing failed.
213
  FunctionLiteral* ParseProgram(Isolate* isolate, Handle<Script> script,
214 215
                                ParseInfo* info,
                                MaybeHandle<ScopeInfo> maybe_outer_scope_info);
216

217 218
  FunctionLiteral* ParseFunction(Isolate* isolate, ParseInfo* info,
                                 Handle<SharedFunctionInfo> shared_info);
219
  FunctionLiteral* DoParseFunction(Isolate* isolate, ParseInfo* info,
220 221
                                   int start_position, int end_position,
                                   int function_literal_id,
222
                                   const AstRawString* raw_name);
223

224
  // Called by ParseProgram after setting up the scanner.
225
  FunctionLiteral* DoParseProgram(Isolate* isolate, ParseInfo* info);
226

227 228 229
  // 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.
230
  void ParseWrapped(Isolate* isolate, ParseInfo* info,
231
                    ScopedPtrList<Statement>* body, DeclarationScope* scope,
232
                    Zone* zone);
233

234 235 236 237
  void ParseREPLProgram(ParseInfo* info, ScopedPtrList<Statement>* body,
                        DeclarationScope* scope);
  Expression* WrapREPLResult(Expression* value);

238 239 240
  ZonePtrList<const AstRawString>* PrepareWrappedArguments(Isolate* isolate,
                                                           ParseInfo* info,
                                                           Zone* zone);
241

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

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

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

310 311 312 313 314
  bool IdentifierEquals(const AstRawString* identifier,
                        const AstRawString* other) {
    return identifier == other;
  }

315 316 317
  Statement* DeclareClass(const AstRawString* variable_name, Expression* value,
                          ZonePtrList<const AstRawString>* names,
                          int class_token_pos, int end_pos);
318 319
  void DeclareClassVariable(ClassScope* scope, const AstRawString* name,
                            ClassInfo* class_info, int class_token_pos);
320 321 322 323 324 325 326 327 328 329 330 331 332
  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);
333
  void DeclareClassProperty(ClassScope* scope, const AstRawString* class_name,
334 335
                            ClassLiteralProperty* property, bool is_constructor,
                            ClassInfo* class_info);
336
  void DeclareClassField(ClassScope* scope, ClassLiteralProperty* property,
337 338 339
                         const AstRawString* property_name, bool is_static,
                         bool is_computed_name, bool is_private,
                         ClassInfo* class_info);
340 341
  Expression* RewriteClassLiteral(ClassScope* block_scope,
                                  const AstRawString* name,
342 343 344 345 346 347
                                  ClassInfo* class_info, int pos, int end_pos);
  Statement* DeclareNative(const AstRawString* name, int pos);

  Block* IgnoreCompletion(Statement* statement);

  Scope* NewHiddenCatchScope();
348

349 350 351 352
  bool HasCheckedSyntax() {
    return scope()->GetDeclarationScope()->has_checked_syntax();
  }

353
  void InitializeVariables(
354
      ScopedPtrList<Statement>* statements, VariableKind kind,
355
      const DeclarationParsingResult::Declaration* declaration);
356

357 358
  Block* RewriteForVarInLegacy(const ForInfo& for_info);
  void DesugarBindingInForEachStatement(ForInfo* for_info, Block** body_block,
359 360
                                        Expression** each_variable);
  Block* CreateForEachStatementTDZ(Block* init_block, const ForInfo& for_info);
361

rossberg's avatar
rossberg committed
362
  Statement* DesugarLexicalBindingsInForStatement(
363
      ForStatement* loop, Statement* init, Expression* cond, Statement* next,
364
      Statement* body, Scope* inner_scope, const ForInfo& for_info);
365

366
  FunctionLiteral* ParseFunctionLiteral(
367
      const AstRawString* name, Scanner::Location function_name_location,
368
      FunctionNameValidity function_name_validity, FunctionKind kind,
369
      int function_token_position, FunctionSyntaxKind type,
370
      LanguageMode language_mode,
371
      ZonePtrList<const AstRawString>* arguments_for_wrapped_function);
372

373 374 375 376 377
  ObjectLiteral* InitializeObjectLiteral(ObjectLiteral* object_literal) {
    object_literal->CalculateEmitStore(main_zone());
    return object_literal;
  }

378 379 380 381
  // Insert initializer statements for var-bindings shadowing parameter bindings
  // from a non-simple parameter list.
  void InsertShadowingVarBindingInitializers(Block* block);

382
  // Implement sloppy block-scoped functions, ES2015 Annex B 3.3
383
  void InsertSloppyBlockFunctionVarBindings(DeclarationScope* scope);
384

385 386 387 388 389 390
  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,
391 392
                              VariableMode mode, Scope* declaration_scope,
                              bool* was_added, int initializer_position);
393 394 395 396 397 398
  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,
399
               VariableKind kind, VariableMode mode, InitializationFlag init,
400
               Scope* declaration_scope, bool* was_added, int var_begin_pos,
401
               int var_end_pos = kNoSourcePosition);
402

403
  // Factory methods.
404
  FunctionLiteral* DefaultConstructor(const AstRawString* name, bool call_super,
405
                                      int pos, int end_pos);
406

407 408
  // Skip over a lazy function, either using cached data if we have it, or
  // by parsing the function with PreParser. Consumes the ending }.
409 410 411 412 413 414 415
  // 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.
416
  bool SkipFunction(const AstRawString* function_name, FunctionKind kind,
417
                    FunctionSyntaxKind function_syntax_kind,
418
                    DeclarationScope* function_scope, int* num_parameters,
419
                    int* function_length,
420
                    ProducedPreparseData** produced_preparsed_scope_data);
421

422
  Block* BuildParameterInitializationBlock(
423
      const ParserFormalParameters& parameters);
424 425
  Block* BuildRejectPromiseOnException(Block* block,
                                       REPLMode repl_mode = REPLMode::kNo);
426

427 428
  void ParseFunction(
      ScopedPtrList<Statement>* body, const AstRawString* function_name,
429
      int pos, FunctionKind kind, FunctionSyntaxKind function_syntax_kind,
430 431
      DeclarationScope* function_scope, int* num_parameters,
      int* function_length, bool* has_duplicate_parameters,
432
      int* expected_property_count, int* suspend_count,
433
      ZonePtrList<const AstRawString>* arguments_for_wrapped_function);
434

435
  void ThrowPendingError(Isolate* isolate, Handle<Script> script);
436

437 438 439 440 441
  class TemplateLiteral : public ZoneObject {
   public:
    TemplateLiteral(Zone* zone, int pos)
        : cooked_(8, zone), raw_(8, zone), expressions_(8, zone), pos_(pos) {}

442 443 444
    const ZonePtrList<const AstRawString>* cooked() const { return &cooked_; }
    const ZonePtrList<const AstRawString>* raw() const { return &raw_; }
    const ZonePtrList<Expression>* expressions() const { return &expressions_; }
445 446
    int position() const { return pos_; }

447 448
    void AddTemplateSpan(const AstRawString* cooked, const AstRawString* raw,
                         int end, Zone* zone) {
449 450 451 452 453 454 455 456 457 458
      DCHECK_NOT_NULL(raw);
      cooked_.Add(cooked, zone);
      raw_.Add(raw, zone);
    }

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

   private:
459 460 461
    ZonePtrList<const AstRawString> cooked_;
    ZonePtrList<const AstRawString> raw_;
    ZonePtrList<Expression> expressions_;
462 463 464
    int pos_;
  };

465
  using TemplateLiteralState = TemplateLiteral*;
466

467
  TemplateLiteralState OpenTemplateLiteral(int pos);
468 469 470
  // "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
471 472
  // interpreted values). Invalid escape sequences cause the cooked span
  // to be represented by undefined, instead of being a syntax error.
473 474 475
  // "tail" indicates that this span is the last in the literal.
  void AddTemplateSpan(TemplateLiteralState* state, bool should_cook,
                       bool tail);
476 477 478 479
  void AddTemplateExpression(TemplateLiteralState* state,
                             Expression* expression);
  Expression* CloseTemplateLiteral(TemplateLiteralState* state, int start,
                                   Expression* tag);
480

481 482 483 484
  ArrayLiteral* ArrayLiteralFromListWithSpread(
      const ScopedPtrList<Expression>& list);
  Expression* SpreadCall(Expression* function,
                         const ScopedPtrList<Expression>& args, int pos,
485 486
                         Call::PossiblyEval is_possibly_eval,
                         bool optional_chain);
487 488
  Expression* SpreadCallNew(Expression* function,
                            const ScopedPtrList<Expression>& args, int pos);
489
  Expression* RewriteSuperCall(Expression* call_expression);
490

491
  void SetLanguageMode(Scope* scope, LanguageMode mode);
492
  void SetAsmModule();
493

494
  Expression* RewriteSpreads(ArrayLiteral* lit);
nikolaos's avatar
nikolaos committed
495

496
  Expression* BuildInitialYield(int pos, FunctionKind kind);
497
  Assignment* BuildCreateJSGeneratorObject(int pos, FunctionKind kind);
498

499 500
  // Generic AST generator for throwing errors from compiled code.
  Expression* NewThrowError(Runtime::FunctionId function_id,
501 502
                            MessageTemplate message, const AstRawString* arg,
                            int pos);
503 504 505

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

506
  void RewriteAsyncFunctionBody(ScopedPtrList<Statement>* body, Block* block,
507 508
                                Expression* return_value,
                                REPLMode repl_mode = REPLMode::kNo);
509

510
  void AddArrowFunctionFormalParameters(ParserFormalParameters* parameters,
511
                                        Expression* params, int end_pos);
512 513
  void SetFunctionName(Expression* value, const AstRawString* name,
                       const AstRawString* prefix = nullptr);
514

515 516 517 518 519
  // Helper functions for recursive descent.
  V8_INLINE bool IsEval(const AstRawString* identifier) const {
    return identifier == ast_value_factory()->eval_string();
  }

520 521 522 523
  V8_INLINE bool IsAsync(const AstRawString* identifier) const {
    return identifier == ast_value_factory()->async_string();
  }

524 525 526 527 528 529 530 531 532 533
  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) {
534
    DCHECK_NOT_NULL(expression);
535
    Property* property = expression->AsProperty();
536
    return property != nullptr && property->obj()->IsThisExpression();
537 538
  }

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

546 547 548
  // 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.
549 550
  V8_INLINE static bool IsIdentifier(Expression* expression) {
    VariableProxy* operand = expression->AsVariableProxy();
551
    return operand != nullptr && !operand->is_new_target();
552 553 554 555 556 557 558
  }

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

559 560 561 562
  V8_INLINE VariableProxy* AsIdentifierExpression(Expression* expression) {
    return expression->AsVariableProxy();
  }

563 564 565 566
  V8_INLINE bool IsConstructor(const AstRawString* identifier) const {
    return identifier == ast_value_factory()->constructor_string();
  }

567 568 569 570
  V8_INLINE bool IsName(const AstRawString* identifier) const {
    return identifier == ast_value_factory()->name_string();
  }

571 572
  V8_INLINE static bool IsBoilerplateProperty(
      ObjectLiteral::Property* property) {
573
    return !property->IsPrototype();
574 575
  }

576 577 578 579 580 581 582
  V8_INLINE bool IsNative(Expression* expr) const {
    DCHECK_NOT_NULL(expr);
    return expr->IsVariableProxy() &&
           expr->AsVariableProxy()->raw_name() ==
               ast_value_factory()->native_string();
  }

583 584 585 586 587
  V8_INLINE static bool IsArrayIndex(const AstRawString* string,
                                     uint32_t* index) {
    return string->AsArrayIndex(index);
  }

588 589 590 591 592 593 594 595
  // 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();
596 597
    if (literal == nullptr || !literal->IsString()) return false;
    return arg == nullptr || literal->AsRawString() == arg;
598 599
  }

600 601
  V8_INLINE void GetDefaultStrings(const AstRawString** default_string,
                                   const AstRawString** dot_default_string) {
602
    *default_string = ast_value_factory()->default_string();
603
    *dot_default_string = ast_value_factory()->dot_default_string();
604 605
  }

606 607
  // Functions for encapsulating the differences between parsing and preparsing;
  // operations interleaved with the recursive descent.
608
  V8_INLINE void PushLiteralName(const AstRawString* id) {
609
    fni_.PushLiteralName(id);
610 611
  }

612
  V8_INLINE void PushVariableName(const AstRawString* id) {
613
    fni_.PushVariableName(id);
614 615
  }

616
  V8_INLINE void PushPropertyName(Expression* expression) {
617
    if (expression->IsPropertyName()) {
618
      fni_.PushLiteralName(expression->AsLiteral()->AsRawPropertyName());
619
    } else {
620
      fni_.PushLiteralName(ast_value_factory()->computed_string());
621 622 623
    }
  }

624
  V8_INLINE void PushEnclosingName(const AstRawString* name) {
625
    fni_.PushEnclosingName(name);
626 627
  }

628
  V8_INLINE void AddFunctionForNameInference(FunctionLiteral* func_to_infer) {
629
    fni_.AddFunction(func_to_infer);
630 631
  }

632
  V8_INLINE void InferFunctionName() { fni_.Infer(); }
633

634 635 636 637 638 639 640 641 642 643
  // 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();
    }
  }

644 645 646
  // A shortcut for performing a ToString operation
  V8_INLINE Expression* ToString(Expression* expr) {
    if (expr->IsStringLiteral()) return expr;
647
    ScopedPtrList<Expression> args(pointer_buffer());
648
    args.Add(expr);
Shiyu Zhang's avatar
Shiyu Zhang committed
649
    return factory()->NewCallRuntime(Runtime::kInlineToStringRT, args,
650 651 652
                                     expr->position());
  }

653 654 655 656 657 658
  // 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);

659 660 661 662 663
  // 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,
664
                              int pos, const SourceRange& range);
665

666
  // Returns a UnaryExpression or, in one of the following cases, a Literal.
667
  // ! <literal> -> true / false
668 669 670
  // + <Number literal> -> <Number literal>
  // - <Number literal> -> <Number literal with value negated>
  // ~ <literal> -> true / false
671 672 673 674
  Expression* BuildUnaryExpression(Expression* expression, Token::Value op,
                                   int pos);

  // Generate AST node that throws a ReferenceError with the given type.
675 676
  V8_INLINE Expression* NewThrowReferenceError(MessageTemplate message,
                                               int pos) {
677 678 679 680 681 682 683
    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.
684
  V8_INLINE Expression* NewThrowSyntaxError(MessageTemplate message,
685 686 687 688 689 690
                                            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).
691
  V8_INLINE Expression* NewThrowTypeError(MessageTemplate message,
692 693 694 695 696
                                          const AstRawString* arg, int pos) {
    return NewThrowError(Runtime::kNewTypeError, message, arg, pos);
  }

  // Reporting errors.
697
  void ReportMessageAt(Scanner::Location source_location,
698 699 700
                       MessageTemplate message, const char* arg = nullptr) {
    pending_error_handler()->ReportMessageAt(
        source_location.beg_pos, source_location.end_pos, message, arg);
701
    scanner_.set_parser_error();
702 703
  }

704 705 706 707
  // Dummy implementation. The parser should never have a unidentifiable
  // error.
  V8_INLINE void ReportUnidentifiableError() { UNREACHABLE(); }

708
  void ReportMessageAt(Scanner::Location source_location,
709 710 711
                       MessageTemplate message, const AstRawString* arg) {
    pending_error_handler()->ReportMessageAt(
        source_location.beg_pos, source_location.end_pos, message, arg);
712
    scanner_.set_parser_error();
713 714
  }

715 716 717 718
  const AstRawString* GetRawNameFromIdentifier(const AstRawString* arg) {
    return arg;
  }

719 720 721 722
  IterationStatement* AsIterationStatement(BreakableStatement* s) {
    return s->AsIterationStatement();
  }

723 724 725 726
  void ReportUnexpectedTokenAt(
      Scanner::Location location, Token::Value token,
      MessageTemplate message = MessageTemplate::kUnexpectedToken);

727
  // "null" return type creators.
728 729 730
  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; }
731 732 733 734
  V8_INLINE static ZonePtrList<Expression>* NullExpressionList() {
    return nullptr;
  }
  V8_INLINE static ZonePtrList<Statement>* NullStatementList() {
735 736
    return nullptr;
  }
737
  V8_INLINE static std::nullptr_t NullStatement() { return nullptr; }
738
  V8_INLINE static std::nullptr_t NullBlock() { return nullptr; }
739
  Expression* FailureExpression() { return factory()->FailureExpression(); }
740 741 742 743

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

746 747 748 749
  V8_INLINE static bool IsIterationStatement(Statement* subject) {
    return subject->AsIterationStatement() != nullptr;
  }

750
  // Non-null empty string.
751 752 753 754
  V8_INLINE const AstRawString* EmptyIdentifierString() const {
    return ast_value_factory()->empty_string();
  }

755 756 757
  // Producing data during the recursive descent.
  V8_INLINE const AstRawString* GetSymbol() const {
    const AstRawString* result = scanner()->CurrentSymbol(ast_value_factory());
758
    DCHECK_NOT_NULL(result);
759 760 761
    return result;
  }

762 763
  V8_INLINE const AstRawString* GetIdentifier() const { return GetSymbol(); }

764 765 766 767 768 769 770 771
  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));
772
    return ast_value_factory()->GetOneByteString(string);
773 774
  }

775 776 777
  class ThisExpression* ThisExpression() {
    UseThis();
    return factory()->ThisExpression();
778 779 780 781 782
  }

  Expression* NewSuperPropertyReference(int pos);
  Expression* NewSuperCallReference(int pos);
  Expression* NewTargetExpression(int pos);
783
  Expression* ImportMetaExpression(int pos);
784

785
  Expression* ExpressionFromLiteral(Token::Value token, int pos);
786

787 788 789
  V8_INLINE VariableProxy* ExpressionFromPrivateName(
      PrivateNameScopeIterator* private_name_scope, const AstRawString* name,
      int start_position) {
790 791
    VariableProxy* proxy = factory()->ast_node_factory()->NewVariableProxy(
        name, NORMAL_VARIABLE, start_position);
792
    private_name_scope->AddUnresolvedPrivateName(proxy);
793 794 795
    return proxy;
  }

796
  V8_INLINE VariableProxy* ExpressionFromIdentifier(
797
      const AstRawString* name, int start_position,
798
      InferName infer = InferName::kYes) {
799
    if (infer == InferName::kYes) {
800
      fni_.PushVariableName(name);
801
    }
802
    return expression_scope()->NewVariable(name, start_position);
803 804
  }

805 806 807 808 809
  V8_INLINE void DeclareIdentifier(const AstRawString* name,
                                   int start_position) {
    expression_scope()->Declare(name, start_position);
  }

810 811 812 813 814
  V8_INLINE Variable* DeclareCatchVariableName(Scope* scope,
                                               const AstRawString* name) {
    return scope->DeclareCatchVariableName(name);
  }

815 816
  V8_INLINE ZonePtrList<Expression>* NewExpressionList(int size) const {
    return new (zone()) ZonePtrList<Expression>(size, zone());
817
  }
818
  V8_INLINE ZonePtrList<ObjectLiteral::Property>* NewObjectPropertyList(
819
      int size) const {
820
    return new (zone()) ZonePtrList<ObjectLiteral::Property>(size, zone());
821
  }
822
  V8_INLINE ZonePtrList<ClassLiteral::Property>* NewClassPropertyList(
823
      int size) const {
824
    return new (zone()) ZonePtrList<ClassLiteral::Property>(size, zone());
825
  }
826 827
  V8_INLINE ZonePtrList<Statement>* NewStatementList(int size) const {
    return new (zone()) ZonePtrList<Statement>(size, zone());
828 829
  }

830 831
  Expression* NewV8Intrinsic(const AstRawString* name,
                             const ScopedPtrList<Expression>& args, int pos);
832

833 834 835 836
  Expression* NewV8RuntimeFunctionForFuzzing(
      const Runtime::Function* function, const ScopedPtrList<Expression>& args,
      int pos);

837
  V8_INLINE Statement* NewThrowStatement(Expression* exception, int pos) {
838
    return factory()->NewExpressionStatement(
839
        factory()->NewThrow(exception, pos), pos);
840 841
  }

842 843 844 845 846
  V8_INLINE void AddFormalParameter(ParserFormalParameters* parameters,
                                    Expression* pattern,
                                    Expression* initializer,
                                    int initializer_end_position,
                                    bool is_rest) {
847
    parameters->UpdateArityAndFunctionLength(initializer != nullptr, is_rest);
848
    auto parameter = new (parameters->scope->zone())
849
        ParserFormalParameters::Parameter(pattern, initializer,
850 851
                                          scanner()->location().beg_pos,
                                          initializer_end_position, is_rest);
852 853

    parameters->params.Add(parameter);
854 855
  }

856
  V8_INLINE void DeclareFormalParameters(ParserFormalParameters* parameters) {
857 858
    bool is_simple = parameters->is_simple;
    DeclarationScope* scope = parameters->scope;
859
    if (!is_simple) scope->MakeParametersNonSimple();
860
    for (auto parameter : parameters->params) {
861
      bool is_optional = parameter->initializer() != nullptr;
862 863 864 865
      // 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.
866
      scope->DeclareParameter(
867
          is_simple ? parameter->name() : ast_value_factory()->empty_string(),
868
          is_simple ? VariableMode::kVar : VariableMode::kTemporary,
869
          is_optional, parameter->is_rest(), ast_value_factory(),
870
          parameter->position);
871 872 873
    }
  }

874 875 876
  void DeclareArrowFunctionFormalParameters(
      ParserFormalParameters* parameters, Expression* params,
      const Scanner::Location& params_loc);
877

878
  Expression* ExpressionListToExpression(const ScopedPtrList<Expression>& args);
879

880 881 882
  void SetFunctionNameFromPropertyName(LiteralProperty* property,
                                       const AstRawString* name,
                                       const AstRawString* prefix = nullptr);
883
  void SetFunctionNameFromPropertyName(ObjectLiteralProperty* property,
884 885
                                       const AstRawString* name,
                                       const AstRawString* prefix = nullptr);
886 887 888 889

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

890 891 892 893
  V8_INLINE void CountUsage(v8::Isolate::UseCounterFeature feature) {
    ++use_counts_[feature];
  }

894 895 896 897 898 899
  // Returns true iff we're parsing the first function literal during
  // CreateDynamicFunction().
  V8_INLINE bool ParsingDynamicFunctionDeclaration() const {
    return parameters_end_pos_ != kNoSourcePosition;
  }

900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
  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(
        nary_op, new (zone()) NaryOperationSourceRanges(zone(), range));
  }

  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());
  }

926 927 928 929 930 931 932
  V8_INLINE void RecordBlockSourceRange(Block* node,
                                        int32_t continuation_position) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(
        node, new (zone()) BlockSourceRanges(continuation_position));
  }

933 934 935 936 937 938 939
  V8_INLINE void RecordCaseClauseSourceRange(CaseClause* node,
                                             const SourceRange& body_range) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(node,
                              new (zone()) CaseClauseSourceRanges(body_range));
  }

940 941 942 943 944 945 946 947 948
  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(),
        new (zone()) ConditionalSourceRanges(then_range, else_range));
  }

949 950 951 952 953
  V8_INLINE void RecordFunctionLiteralSourceRange(FunctionLiteral* node) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(node, new (zone()) FunctionLiteralSourceRanges);
  }

954 955 956 957 958 959 960 961
  V8_INLINE void RecordBinaryOperationSourceRange(
      Expression* node, const SourceRange& right_range) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(node->AsBinaryOperation(),
                              new (zone())
                                  BinaryOperationSourceRanges(right_range));
  }

962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985
  V8_INLINE void RecordJumpStatementSourceRange(Statement* node,
                                                int32_t continuation_position) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(
        static_cast<JumpStatement*>(node),
        new (zone()) JumpStatementSourceRanges(continuation_position));
  }

  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(),
        new (zone()) IfStatementSourceRanges(then_range, else_range));
  }

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

986 987 988 989 990 991 992 993
  V8_INLINE void RecordSuspendSourceRange(Expression* node,
                                          int32_t continuation_position) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(static_cast<Suspend*>(node),
                              new (zone())
                                  SuspendSourceRanges(continuation_position));
  }

994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
  V8_INLINE void RecordSwitchStatementSourceRange(
      Statement* node, int32_t continuation_position) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(
        node->AsSwitchStatement(),
        new (zone()) SwitchStatementSourceRanges(continuation_position));
  }

  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(
        throw_expr, new (zone()) ThrowSourceRanges(continuation_position));
  }

  V8_INLINE void RecordTryCatchStatementSourceRange(
      TryCatchStatement* node, const SourceRange& body_range) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(
        node, new (zone()) TryCatchStatementSourceRanges(body_range));
  }

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

1025 1026 1027 1028
  // Generate the next internal variable name for binding an exported namespace
  // object (used to implement the "export * as" syntax).
  const AstRawString* NextInternalNamespaceExportName();

1029 1030
  ParseInfo* info() const { return info_; }

1031 1032 1033 1034
  std::vector<uint8_t>* preparse_data_buffer() {
    return &preparse_data_buffer_;
  }

1035
  // Parser's private field members.
1036
  friend class PreParserZoneScope;  // Uses reusable_preparser().
1037
  friend class PreparseDataBuilder;  // Uses preparse_data_buffer()
1038

1039
  ParseInfo* info_;
1040
  Scanner scanner_;
1041
  Zone preparser_zone_;
1042
  PreParser* reusable_preparser_;
1043
  Mode mode_;
1044

1045 1046
  MaybeHandle<FixedArray> maybe_wrapped_arguments_;

1047 1048
  SourceRangeMap* source_range_map_ = nullptr;

1049 1050
  friend class ParserTargetScope;

1051
  ScriptCompiler::CompileOptions compile_options_;
1052

1053 1054 1055
  // For NextInternalNamespaceExportName().
  int number_of_named_namespace_exports_ = 0;

1056 1057
  // Other information which will be stored in Parser and moved to Isolate after
  // parsing.
1058
  int use_counts_[v8::Isolate::kUseCounterFeatureCount];
1059
  int total_preparse_skipped_;
1060
  bool allow_lazy_;
1061
  bool temp_zoned_;
1062
  ConsumedPreparseData* consumed_preparse_data_;
1063
  std::vector<uint8_t> preparse_data_buffer_;
1064

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

1072 1073
}  // namespace internal
}  // namespace v8
1074

1075
#endif  // V8_PARSING_PARSER_H_