parser.h 44.3 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 12
#include "src/ast/ast.h"
#include "src/ast/scopes.h"
13
#include "src/base/compiler-specific.h"
14
#include "src/base/threaded-list.h"
15
#include "src/common/globals.h"
16
#include "src/parsing/parser-base.h"
17
#include "src/parsing/parsing.h"
18
#include "src/parsing/preparser.h"
19
#include "src/utils/pointer-with-payload.h"
20
#include "src/zone/zone-chunk-list.h"
21

22
namespace v8 {
23

24 25
class ScriptCompiler;

26
namespace internal {
27

28
class ConsumedPreparseData;
29
class ParseInfo;
30 31
class ParserTarget;
class ParserTargetScope;
32
class PendingCompilationErrorHandler;
33
class PreparseData;
34

35 36
// ----------------------------------------------------------------------------
// JAVASCRIPT PARSING
37

38
class Parser;
39

40

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

50
    PointerWithPayload<Expression, bool, 1> initializer_and_is_rest;
51

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

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

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

71 72
    Parameter** next() { return &next_parameter; }
    Parameter* const* next() const { return &next_parameter; }
73 74
  };

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

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

85
  explicit ParserFormalParameters(DeclarationScope* scope)
86
      : FormalParametersBase(scope) {}
87

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

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

  // Return types for traversing functions.
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
  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*;
116 117

  // For constructing objects returned by the traversing functions.
118
  using Factory = AstNodeFactory;
119

120
  // Other implementation-specific functions.
121 122 123 124 125
  using FuncNameInferrer = v8::internal::FuncNameInferrer;
  using SourceRange = v8::internal::SourceRange;
  using SourceRangeScope = v8::internal::SourceRangeScope;
  using Target = ParserTarget;
  using TargetScope = ParserTargetScope;
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
  void ParseOnBackground(ParseInfo* info);
139

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

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

157 158
  // Move statistics to Isolate
  void UpdateStatistics(Isolate* isolate, Handle<Script> script);
159
  void HandleSourceURLComments(Isolate* isolate, Handle<Script> script);
160

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

174 175 176 177 178
  bool AllowsLazyParsingWithoutUnresolvedVariables() const {
    return scope()->AllowsLazyParsingWithoutUnresolvedVariables(
        original_scope_);
  }

179 180 181
  bool parse_lazily() const { return mode_ == PARSE_LAZILY; }
  enum Mode { PARSE_LAZILY, PARSE_EAGERLY };

182
  class ParsingModeScope {
183 184 185 186 187 188 189 190 191 192 193 194
   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_;
  };

195 196 197 198 199 200 201
  // Runtime encoding of different completion modes.
  enum CompletionKind {
    kNormalCompletion,
    kThrowCompletion,
    kAbruptCompletion
  };

202 203 204
  Variable* NewTemporary(const AstRawString* name) {
    return scope()->NewTemporary(name);
  }
205

206
  void PrepareGeneratorVariables();
207

208
  // Returns nullptr if parsing failed.
209
  FunctionLiteral* ParseProgram(Isolate* isolate, ParseInfo* info);
210

211 212
  FunctionLiteral* ParseFunction(Isolate* isolate, ParseInfo* info,
                                 Handle<SharedFunctionInfo> shared_info);
213
  FunctionLiteral* DoParseFunction(Isolate* isolate, ParseInfo* info,
214
                                   const AstRawString* raw_name);
215

216
  // Called by ParseProgram after setting up the scanner.
217
  FunctionLiteral* DoParseProgram(Isolate* isolate, ParseInfo* info);
218

219 220 221
  // 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.
222
  void ParseWrapped(Isolate* isolate, ParseInfo* info,
223
                    ScopedPtrList<Statement>* body, DeclarationScope* scope,
224
                    Zone* zone);
225

226 227 228
  ZonePtrList<const AstRawString>* PrepareWrappedArguments(Isolate* isolate,
                                                           ParseInfo* info,
                                                           Zone* zone);
229

230
  PreParser* reusable_preparser() {
231
    if (reusable_preparser_ == nullptr) {
232 233 234 235
      reusable_preparser_ = new PreParser(
          &preparser_zone_, &scanner_, stack_limit_, ast_value_factory(),
          pending_error_handler(), runtime_call_stats_, logger_, -1,
          parsing_module_, parsing_on_main_thread_);
236 237 238
#define SET_ALLOW(name) reusable_preparser_->set_allow_##name(allow_##name());
      SET_ALLOW(natives);
      SET_ALLOW(harmony_dynamic_import);
239
      SET_ALLOW(harmony_import_meta);
240
      SET_ALLOW(harmony_private_methods);
241
      SET_ALLOW(eval_cache);
242
#undef SET_ALLOW
243
      preparse_data_buffer_.reserve(128);
244 245 246 247
    }
    return reusable_preparser_;
  }

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

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

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

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

  Block* IgnoreCompletion(Statement* statement);

  Scope* NewHiddenCatchScope();
347

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

352 353
  // PatternRewriter and associated methods defined in pattern-rewriter.cc.
  friend class PatternRewriter;
354
  void InitializeVariables(
355
      ScopedPtrList<Statement>* statements, VariableKind kind,
356
      const DeclarationParsingResult::Declaration* declaration);
357

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

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

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

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

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

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

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

404
  bool TargetStackContainsLabel(const AstRawString* label);
405 406
  BreakableStatement* LookupBreakTarget(const AstRawString* label);
  IterationStatement* LookupContinueTarget(const AstRawString* label);
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
  Block* BuildRejectPromiseOnException(Block* block);
430

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

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

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

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

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

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

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

469
  using TemplateLiteralState = TemplateLiteral*;
470

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

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

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

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

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

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

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

510
  void RewriteAsyncFunctionBody(ScopedPtrList<Statement>* body, Block* block,
511
                                Expression* return_value);
512

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

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

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

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

542 543 544 545 546 547 548
  // 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();
  }

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

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

562 563 564 565
  V8_INLINE VariableProxy* AsIdentifierExpression(Expression* expression) {
    return expression->AsVariableProxy();
  }

566 567 568 569
  V8_INLINE bool IsConstructor(const AstRawString* identifier) const {
    return identifier == ast_value_factory()->constructor_string();
  }

570 571 572 573
  V8_INLINE bool IsName(const AstRawString* identifier) const {
    return identifier == ast_value_factory()->name_string();
  }

574 575
  V8_INLINE static bool IsBoilerplateProperty(
      ObjectLiteral::Property* property) {
576
    return !property->IsPrototype();
577 578
  }

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

586 587 588 589 590
  V8_INLINE static bool IsArrayIndex(const AstRawString* string,
                                     uint32_t* index) {
    return string->AsArrayIndex(index);
  }

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

603 604
  V8_INLINE void GetDefaultStrings(const AstRawString** default_string,
                                   const AstRawString** dot_default_string) {
605
    *default_string = ast_value_factory()->default_string();
606
    *dot_default_string = ast_value_factory()->dot_default_string();
607 608
  }

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

615
  V8_INLINE void PushVariableName(const AstRawString* id) {
616
    fni_.PushVariableName(id);
617 618
  }

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

627
  V8_INLINE void PushEnclosingName(const AstRawString* name) {
628
    fni_.PushEnclosingName(name);
629 630
  }

631
  V8_INLINE void AddFunctionForNameInference(FunctionLiteral* func_to_infer) {
632
    fni_.AddFunction(func_to_infer);
633 634
  }

635
  V8_INLINE void InferFunctionName() { fni_.Infer(); }
636

637 638 639 640 641 642 643 644 645 646
  // 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();
    }
  }

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

656 657 658 659 660 661
  // 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);

662 663 664 665 666
  // 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,
667
                              int pos, const SourceRange& range);
668

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

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

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

707 708 709 710
  // Dummy implementation. The parser should never have a unidentifiable
  // error.
  V8_INLINE void ReportUnidentifiableError() { UNREACHABLE(); }

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

718 719 720 721
  const AstRawString* GetRawNameFromIdentifier(const AstRawString* arg) {
    return arg;
  }

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

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

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

745
  // Non-null empty string.
746 747 748 749
  V8_INLINE const AstRawString* EmptyIdentifierString() const {
    return ast_value_factory()->empty_string();
  }

750 751 752
  // Producing data during the recursive descent.
  V8_INLINE const AstRawString* GetSymbol() const {
    const AstRawString* result = scanner()->CurrentSymbol(ast_value_factory());
753
    DCHECK_NOT_NULL(result);
754 755 756
    return result;
  }

757 758
  V8_INLINE const AstRawString* GetIdentifier() const { return GetSymbol(); }

759 760 761 762 763 764 765 766
  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));
767
    return ast_value_factory()->GetOneByteString(string);
768 769
  }

770 771 772
  class ThisExpression* ThisExpression() {
    UseThis();
    return factory()->ThisExpression();
773 774 775 776 777
  }

  Expression* NewSuperPropertyReference(int pos);
  Expression* NewSuperCallReference(int pos);
  Expression* NewTargetExpression(int pos);
778
  Expression* ImportMetaExpression(int pos);
779

780
  Expression* ExpressionFromLiteral(Token::Value token, int pos);
781

782 783 784
  V8_INLINE VariableProxy* ExpressionFromPrivateName(
      PrivateNameScopeIterator* private_name_scope, const AstRawString* name,
      int start_position) {
785 786
    VariableProxy* proxy = factory()->ast_node_factory()->NewVariableProxy(
        name, NORMAL_VARIABLE, start_position);
787
    private_name_scope->AddUnresolvedPrivateName(proxy);
788 789 790
    return proxy;
  }

791
  V8_INLINE VariableProxy* ExpressionFromIdentifier(
792
      const AstRawString* name, int start_position,
793
      InferName infer = InferName::kYes) {
794
    if (infer == InferName::kYes) {
795
      fni_.PushVariableName(name);
796
    }
797
    return expression_scope()->NewVariable(name, start_position);
798 799
  }

800 801 802 803 804
  V8_INLINE void DeclareIdentifier(const AstRawString* name,
                                   int start_position) {
    expression_scope()->Declare(name, start_position);
  }

805 806 807 808 809
  V8_INLINE Variable* DeclareCatchVariableName(Scope* scope,
                                               const AstRawString* name) {
    return scope->DeclareCatchVariableName(name);
  }

810 811
  V8_INLINE ZonePtrList<Expression>* NewExpressionList(int size) const {
    return new (zone()) ZonePtrList<Expression>(size, zone());
812
  }
813
  V8_INLINE ZonePtrList<ObjectLiteral::Property>* NewObjectPropertyList(
814
      int size) const {
815
    return new (zone()) ZonePtrList<ObjectLiteral::Property>(size, zone());
816
  }
817
  V8_INLINE ZonePtrList<ClassLiteral::Property>* NewClassPropertyList(
818
      int size) const {
819
    return new (zone()) ZonePtrList<ClassLiteral::Property>(size, zone());
820
  }
821 822
  V8_INLINE ZonePtrList<Statement>* NewStatementList(int size) const {
    return new (zone()) ZonePtrList<Statement>(size, zone());
823 824
  }

825 826
  Expression* NewV8Intrinsic(const AstRawString* name,
                             const ScopedPtrList<Expression>& args, int pos);
827

828
  V8_INLINE Statement* NewThrowStatement(Expression* exception, int pos) {
829
    return factory()->NewExpressionStatement(
830
        factory()->NewThrow(exception, pos), pos);
831 832
  }

833 834 835 836 837
  V8_INLINE void AddFormalParameter(ParserFormalParameters* parameters,
                                    Expression* pattern,
                                    Expression* initializer,
                                    int initializer_end_position,
                                    bool is_rest) {
838
    parameters->UpdateArityAndFunctionLength(initializer != nullptr, is_rest);
839
    auto parameter = new (parameters->scope->zone())
840
        ParserFormalParameters::Parameter(pattern, initializer,
841 842
                                          scanner()->location().beg_pos,
                                          initializer_end_position, is_rest);
843 844

    parameters->params.Add(parameter);
845 846
  }

847
  V8_INLINE void DeclareFormalParameters(ParserFormalParameters* parameters) {
848 849
    bool is_simple = parameters->is_simple;
    DeclarationScope* scope = parameters->scope;
850
    if (!is_simple) scope->MakeParametersNonSimple();
851
    for (auto parameter : parameters->params) {
852
      bool is_optional = parameter->initializer() != nullptr;
853 854 855 856
      // 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.
857
      scope->DeclareParameter(
858
          is_simple ? parameter->name() : ast_value_factory()->empty_string(),
859
          is_simple ? VariableMode::kVar : VariableMode::kTemporary,
860
          is_optional, parameter->is_rest(), ast_value_factory(),
861
          parameter->position);
862 863 864
    }
  }

865 866 867
  void DeclareArrowFunctionFormalParameters(
      ParserFormalParameters* parameters, Expression* params,
      const Scanner::Location& params_loc);
868

869
  Expression* ExpressionListToExpression(const ScopedPtrList<Expression>& args);
870

871 872 873
  void SetFunctionNameFromPropertyName(LiteralProperty* property,
                                       const AstRawString* name,
                                       const AstRawString* prefix = nullptr);
874
  void SetFunctionNameFromPropertyName(ObjectLiteralProperty* property,
875 876
                                       const AstRawString* name,
                                       const AstRawString* prefix = nullptr);
877 878 879 880

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

881 882 883 884
  V8_INLINE void CountUsage(v8::Isolate::UseCounterFeature feature) {
    ++use_counts_[feature];
  }

885 886 887 888 889 890
  // Returns true iff we're parsing the first function literal during
  // CreateDynamicFunction().
  V8_INLINE bool ParsingDynamicFunctionDeclaration() const {
    return parameters_end_pos_ != kNoSourcePosition;
  }

891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916
  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());
  }

917 918 919 920 921 922 923
  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));
  }

924 925 926 927 928 929 930
  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));
  }

931 932 933 934 935 936 937 938 939
  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));
  }

940 941 942 943 944
  V8_INLINE void RecordFunctionLiteralSourceRange(FunctionLiteral* node) {
    if (source_range_map_ == nullptr) return;
    source_range_map_->Insert(node, new (zone()) FunctionLiteralSourceRanges);
  }

945 946 947 948 949 950 951 952
  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));
  }

953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
  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));
  }

977 978 979 980 981 982 983 984
  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));
  }

985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
  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));
  }

1016 1017 1018 1019
  // Generate the next internal variable name for binding an exported namespace
  // object (used to implement the "export * as" syntax).
  const AstRawString* NextInternalNamespaceExportName();

1020 1021
  ParseInfo* info() const { return info_; }

1022 1023 1024 1025
  std::vector<uint8_t>* preparse_data_buffer() {
    return &preparse_data_buffer_;
  }

1026
  // Parser's private field members.
1027
  friend class PreParserZoneScope;  // Uses reusable_preparser().
1028
  friend class PreparseDataBuilder;  // Uses preparse_data_buffer()
1029

1030
  ParseInfo* info_;
1031
  Scanner scanner_;
1032
  Zone preparser_zone_;
1033
  PreParser* reusable_preparser_;
1034
  Mode mode_;
1035

1036 1037
  SourceRangeMap* source_range_map_ = nullptr;

1038 1039 1040 1041
  friend class ParserTarget;
  friend class ParserTargetScope;
  ParserTarget* target_stack_;  // for break, continue statements

1042
  ScriptCompiler::CompileOptions compile_options_;
1043

1044 1045 1046
  // For NextInternalNamespaceExportName().
  int number_of_named_namespace_exports_ = 0;

1047 1048
  // Other information which will be stored in Parser and moved to Isolate after
  // parsing.
1049
  int use_counts_[v8::Isolate::kUseCounterFeatureCount];
1050
  int total_preparse_skipped_;
1051
  bool allow_lazy_;
1052
  bool temp_zoned_;
1053
  ConsumedPreparseData* consumed_preparse_data_;
1054
  std::vector<uint8_t> preparse_data_buffer_;
1055

1056 1057 1058 1059 1060
  // 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_;
1061
};
1062

1063 1064 1065 1066 1067 1068
// ----------------------------------------------------------------------------
// Target is a support class to facilitate manipulation of the
// Parser's target_stack_ (the stack of potential 'break' and
// 'continue' statement targets). Upon construction, a new target is
// added; it is removed upon destruction.

1069
class ParserTarget {
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
 public:
  ParserTarget(ParserBase<Parser>* parser, BreakableStatement* statement)
      : variable_(&parser->impl()->target_stack_),
        statement_(statement),
        previous_(parser->impl()->target_stack_) {
    parser->impl()->target_stack_ = this;
  }

  ~ParserTarget() { *variable_ = previous_; }

  ParserTarget* previous() { return previous_; }
  BreakableStatement* statement() { return statement_; }

 private:
  ParserTarget** variable_;
  BreakableStatement* statement_;
  ParserTarget* previous_;
};

1089
class ParserTargetScope {
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
 public:
  explicit ParserTargetScope(ParserBase<Parser>* parser)
      : variable_(&parser->impl()->target_stack_),
        previous_(parser->impl()->target_stack_) {
    parser->impl()->target_stack_ = nullptr;
  }

  ~ParserTargetScope() { *variable_ = previous_; }

 private:
  ParserTarget** variable_;
  ParserTarget* previous_;
};

1104 1105
}  // namespace internal
}  // namespace v8
1106

1107
#endif  // V8_PARSING_PARSER_H_