V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
parser.h
1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef V8_PARSING_PARSER_H_
6 #define V8_PARSING_PARSER_H_
7 
8 #include <cstddef>
9 
10 #include "src/ast/ast-source-ranges.h"
11 #include "src/ast/ast.h"
12 #include "src/ast/scopes.h"
13 #include "src/base/compiler-specific.h"
14 #include "src/base/threaded-list.h"
15 #include "src/globals.h"
16 #include "src/parsing/parser-base.h"
17 #include "src/parsing/parsing.h"
18 #include "src/parsing/preparser.h"
19 #include "src/pointer-with-payload.h"
20 #include "src/zone/zone-chunk-list.h"
21 
22 namespace v8 {
23 
24 class ScriptCompiler;
25 
26 namespace internal {
27 
28 class ConsumedPreParsedScopeData;
29 class ParseInfo;
30 class ParserTarget;
31 class ParserTargetScope;
32 class PendingCompilationErrorHandler;
33 class PreParsedScopeData;
34 
36  public:
37  enum {
38  kStartPositionIndex,
39  kEndPositionIndex,
40  kNumParametersIndex,
41  kFlagsIndex,
42  kNumInnerFunctionsIndex,
43  kSize
44  };
45 
46  explicit FunctionEntry(Vector<unsigned> backing)
47  : backing_(backing) { }
48 
49  FunctionEntry() : backing_() { }
50 
51  class LanguageModeField : public BitField<LanguageMode, 0, 1> {};
53  : public BitField<bool, LanguageModeField::kNext, 1> {};
54 
55  static uint32_t EncodeFlags(LanguageMode language_mode,
56  bool uses_super_property) {
57  return LanguageModeField::encode(language_mode) |
58  UsesSuperPropertyField::encode(uses_super_property);
59  }
60 
61  int start_pos() const { return backing_[kStartPositionIndex]; }
62  int end_pos() const { return backing_[kEndPositionIndex]; }
63  int num_parameters() const { return backing_[kNumParametersIndex]; }
64  LanguageMode language_mode() const {
65  return LanguageModeField::decode(backing_[kFlagsIndex]);
66  }
67  bool uses_super_property() const {
68  return UsesSuperPropertyField::decode(backing_[kFlagsIndex]);
69  }
70  int num_inner_functions() const { return backing_[kNumInnerFunctionsIndex]; }
71 
72  bool is_valid() const { return !backing_.is_empty(); }
73 
74  private:
75  Vector<unsigned> backing_;
76 };
77 
78 
79 // ----------------------------------------------------------------------------
80 // JAVASCRIPT PARSING
81 
82 class Parser;
83 
84 
86  struct Parameter : public ZoneObject {
87  Parameter(const AstRawString* name, Expression* pattern,
88  Expression* initializer, int position,
89  int initializer_end_position, bool is_rest)
90  : name(name),
91  name_and_is_rest(initializer, is_rest),
92  pattern(pattern),
93  position(position),
94  initializer_end_position(initializer_end_position) {}
95 
96  const AstRawString* name;
97 
99 
100  Expression* pattern;
101  Expression* initializer() const { return name_and_is_rest.GetPointer(); }
102  int position;
103  int initializer_end_position;
104  inline bool is_rest() const { return name_and_is_rest.GetPayload(); }
105 
106  Parameter* next_parameter = nullptr;
107  bool is_simple() const {
108  return pattern->IsVariableProxy() && initializer() == nullptr &&
109  !is_rest();
110  }
111 
112  Parameter** next() { return &next_parameter; }
113  Parameter* const* next() const { return &next_parameter; }
114  };
115 
116  Scanner::Location duplicate_location() const { return duplicate_loc; }
117  bool has_duplicate() const { return duplicate_loc.IsValid(); }
118 
119  explicit ParserFormalParameters(DeclarationScope* scope)
120  : FormalParametersBase(scope) {}
121  base::ThreadedList<Parameter> params;
122  Scanner::Location duplicate_loc = Scanner::Location::invalid();
123 };
124 
125 template <>
127  typedef ParserBase<Parser> Base;
128  typedef Parser Impl;
129 
130  // Return types for traversing functions.
131  typedef v8::internal::Block* Block;
140  typedef const AstRawString* Identifier;
148 
149  // For constructing objects returned by the traversing functions.
150  typedef AstNodeFactory Factory;
151 
152  // Other implementation-specific functions.
156  typedef ParserTarget Target;
158 
159  static constexpr bool ExpressionClassifierReportErrors = true;
160 };
161 
162 class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase<Parser>) {
163  public:
164  explicit Parser(ParseInfo* info);
165  ~Parser() {
166  delete reusable_preparser_;
167  reusable_preparser_ = nullptr;
168  }
169 
170  static bool IsPreParser() { return false; }
171 
172  void ParseOnBackground(ParseInfo* info);
173 
174  // Initializes an empty scope chain for top-level scripts, or scopes which
175  // consist of only the native context.
176  void InitializeEmptyScopeChain(ParseInfo* info);
177 
178  // Deserialize the scope chain prior to parsing in which the script is going
179  // to be executed. If the script is a top-level script, or the scope chain
180  // consists of only a native context, maybe_outer_scope_info should be an
181  // empty handle.
182  //
183  // This only deserializes the scope chain, but doesn't connect the scopes to
184  // their corresponding scope infos. Therefore, looking up variables in the
185  // deserialized scopes is not possible.
186  void DeserializeScopeChain(Isolate* isolate, ParseInfo* info,
187  MaybeHandle<ScopeInfo> maybe_outer_scope_info);
188 
189  // Move statistics to Isolate
190  void UpdateStatistics(Isolate* isolate, Handle<Script> script);
191  void HandleSourceURLComments(Isolate* isolate, Handle<Script> script);
192 
193  private:
194  friend class ParserBase<Parser>;
197  friend bool v8::internal::parsing::ParseProgram(ParseInfo*, Isolate*);
198  friend bool v8::internal::parsing::ParseFunction(
200 
201  bool AllowsLazyParsingWithoutUnresolvedVariables() const {
202  return scope()->AllowsLazyParsingWithoutUnresolvedVariables(
203  original_scope_);
204  }
205 
206  bool parse_lazily() const { return mode_ == PARSE_LAZILY; }
207  enum Mode { PARSE_LAZILY, PARSE_EAGERLY };
208 
209  class ParsingModeScope {
210  public:
211  ParsingModeScope(Parser* parser, Mode mode)
212  : parser_(parser), old_mode_(parser->mode_) {
213  parser_->mode_ = mode;
214  }
215  ~ParsingModeScope() { parser_->mode_ = old_mode_; }
216 
217  private:
218  Parser* parser_;
219  Mode old_mode_;
220  };
221 
222  // Runtime encoding of different completion modes.
223  enum CompletionKind {
224  kNormalCompletion,
225  kThrowCompletion,
226  kAbruptCompletion
227  };
228 
229  Variable* NewTemporary(const AstRawString* name) {
230  return scope()->NewTemporary(name);
231  }
232 
233  void PrepareGeneratorVariables();
234 
235  // Returns nullptr if parsing failed.
236  FunctionLiteral* ParseProgram(Isolate* isolate, ParseInfo* info);
237 
238  FunctionLiteral* ParseFunction(Isolate* isolate, ParseInfo* info,
239  Handle<SharedFunctionInfo> shared_info);
240  FunctionLiteral* DoParseFunction(Isolate* isolate, ParseInfo* info,
241  const AstRawString* raw_name);
242 
243  // Called by ParseProgram after setting up the scanner.
244  FunctionLiteral* DoParseProgram(Isolate* isolate, ParseInfo* info);
245 
246  // Parse with the script as if the source is implicitly wrapped in a function.
247  // We manually construct the AST and scopes for a top-level function and the
248  // function wrapper.
249  void ParseWrapped(Isolate* isolate, ParseInfo* info,
251  Zone* zone);
252 
253  ZonePtrList<const AstRawString>* PrepareWrappedArguments(Isolate* isolate,
254  ParseInfo* info,
255  Zone* zone);
256 
257  PreParser* reusable_preparser() {
258  if (reusable_preparser_ == nullptr) {
259  reusable_preparser_ = new PreParser(
260  &preparser_zone_, &scanner_, stack_limit_, ast_value_factory(),
261  pending_error_handler(), runtime_call_stats_, logger_, -1,
262  parsing_module_, parsing_on_main_thread_);
263 #define SET_ALLOW(name) reusable_preparser_->set_allow_##name(allow_##name());
264  SET_ALLOW(natives);
265  SET_ALLOW(harmony_public_fields);
266  SET_ALLOW(harmony_static_fields);
267  SET_ALLOW(harmony_dynamic_import);
268  SET_ALLOW(harmony_import_meta);
269  SET_ALLOW(harmony_private_fields);
270  SET_ALLOW(harmony_private_methods);
271  SET_ALLOW(eval_cache);
272 #undef SET_ALLOW
273  }
274  return reusable_preparser_;
275  }
276 
277  void ParseModuleItemList(ScopedPtrList<Statement>* body);
278  Statement* ParseModuleItem();
279  const AstRawString* ParseModuleSpecifier();
280  void ParseImportDeclaration();
281  Statement* ParseExportDeclaration();
282  Statement* ParseExportDefault();
283  void ParseExportStar();
284  struct ExportClauseData {
285  const AstRawString* export_name;
286  const AstRawString* local_name;
287  Scanner::Location location;
288  };
289  ZoneChunkList<ExportClauseData>* ParseExportClause(
290  Scanner::Location* reserved_loc);
291  struct NamedImport : public ZoneObject {
292  const AstRawString* import_name;
293  const AstRawString* local_name;
294  const Scanner::Location location;
295  NamedImport(const AstRawString* import_name, const AstRawString* local_name,
296  Scanner::Location location)
297  : import_name(import_name),
298  local_name(local_name),
299  location(location) {}
300  };
301  ZonePtrList<const NamedImport>* ParseNamedImports(int pos);
302  Block* BuildInitializationBlock(DeclarationParsingResult* parsing_result,
304  void DeclareLabel(ZonePtrList<const AstRawString>** labels,
305  ZonePtrList<const AstRawString>** own_labels,
306  VariableProxy* expr);
307  bool ContainsLabel(ZonePtrList<const AstRawString>* labels,
308  const AstRawString* label);
309  Expression* RewriteReturn(Expression* return_value, int pos);
310  Statement* RewriteSwitchStatement(SwitchStatement* switch_statement,
311  Scope* scope);
312  void RewriteCatchPattern(CatchInfo* catch_info);
313  void ValidateCatchBlock(const CatchInfo& catch_info);
314  Statement* RewriteTryStatement(Block* try_block, Block* catch_block,
315  const SourceRange& catch_range,
316  Block* finally_block,
317  const SourceRange& finally_range,
318  const CatchInfo& catch_info, int pos);
319  void GetUnexpectedTokenMessage(Token::Value token, MessageTemplate* message,
320  Scanner::Location* location, const char** arg);
321  void ParseAndRewriteGeneratorFunctionBody(int pos, FunctionKind kind,
323  void ParseAndRewriteAsyncGeneratorFunctionBody(
324  int pos, FunctionKind kind, ScopedPtrList<Statement>* body);
325  void DeclareFunctionNameVar(const AstRawString* function_name,
326  FunctionLiteral::FunctionType function_type,
327  DeclarationScope* function_scope);
328 
329  Statement* DeclareFunction(const AstRawString* variable_name,
330  FunctionLiteral* function, VariableMode mode,
331  int pos, bool is_sloppy_block_function,
333  Variable* CreateSyntheticContextVariable(const AstRawString* synthetic_name);
334  FunctionLiteral* CreateInitializerFunction(
335  const char* name, DeclarationScope* scope,
337 
338  bool IdentifierEquals(const AstRawString* identifier,
339  const AstRawString* other) {
340  return identifier == other;
341  }
342 
343  Statement* DeclareClass(const AstRawString* variable_name, Expression* value,
345  int class_token_pos, int end_pos);
346  void DeclareClassVariable(const AstRawString* name, ClassInfo* class_info,
347  int class_token_pos);
348  void DeclareClassProperty(const AstRawString* class_name,
349  ClassLiteralProperty* property,
350  const AstRawString* property_name,
351  ClassLiteralProperty::Kind kind, bool is_static,
352  bool is_constructor, bool is_computed_name,
353  bool is_private, ClassInfo* class_info);
354  Expression* RewriteClassLiteral(Scope* block_scope, const AstRawString* name,
355  ClassInfo* class_info, int pos, int end_pos);
356  Statement* DeclareNative(const AstRawString* name, int pos);
357 
358  Block* IgnoreCompletion(Statement* statement);
359 
360  Scope* NewHiddenCatchScope();
361 
362  // PatternRewriter and associated methods defined in pattern-rewriter.cc.
363  friend class PatternRewriter;
364  void DeclareAndInitializeVariables(
365  Block* block, const DeclarationDescriptor* declaration_descriptor,
366  const DeclarationParsingResult::Declaration* declaration,
368  void RewriteDestructuringAssignment(RewritableExpression* expr);
369  Expression* RewriteDestructuringAssignment(Assignment* assignment);
370 
371  // [if (IteratorType == kAsync)]
372  // !%_IsJSReceiver(result = Await(next.[[Call]](iterator, « »)) &&
373  // %ThrowIteratorResultNotAnObject(result)
374  // [else]
375  // !%_IsJSReceiver(result = next.[[Call]](iterator, « »)) &&
376  // %ThrowIteratorResultNotAnObject(result)
377  // [endif]
378  Expression* BuildIteratorNextResult(VariableProxy* iterator,
379  VariableProxy* next, Variable* result,
380  IteratorType type, int pos);
381 
382  // Initialize the components of a for-in / for-of statement.
383  Statement* InitializeForEachStatement(ForEachStatement* stmt,
384  Expression* each, Expression* subject,
385  Statement* body);
386  Statement* InitializeForOfStatement(ForOfStatement* stmt, Expression* each,
387  Expression* iterable, Statement* body,
388  bool finalize, IteratorType type,
389  int next_result_pos = kNoSourcePosition);
390 
391  Block* RewriteForVarInLegacy(const ForInfo& for_info);
392  void DesugarBindingInForEachStatement(ForInfo* for_info, Block** body_block,
393  Expression** each_variable);
394  Block* CreateForEachStatementTDZ(Block* init_block, const ForInfo& for_info);
395 
396  Statement* DesugarLexicalBindingsInForStatement(
397  ForStatement* loop, Statement* init, Expression* cond, Statement* next,
398  Statement* body, Scope* inner_scope, const ForInfo& for_info);
399 
400  FunctionLiteral* ParseFunctionLiteral(
401  const AstRawString* name, Scanner::Location function_name_location,
402  FunctionNameValidity function_name_validity, FunctionKind kind,
403  int function_token_position, FunctionLiteral::FunctionType type,
404  LanguageMode language_mode,
405  ZonePtrList<const AstRawString>* arguments_for_wrapped_function);
406 
407  ObjectLiteral* InitializeObjectLiteral(ObjectLiteral* object_literal) {
408  object_literal->CalculateEmitStore(main_zone());
409  return object_literal;
410  }
411 
412  // Check if the scope has conflicting var/let declarations from different
413  // scopes. This covers for example
414  //
415  // function f() { { { var x; } let x; } }
416  // function g() { { var x; let x; } }
417  //
418  // The var declarations are hoisted to the function scope, but originate from
419  // a scope where the name has also been let bound or the var declaration is
420  // hoisted over such a scope.
421  void CheckConflictingVarDeclarations(Scope* scope);
422 
423  bool IsPropertyWithPrivateFieldKey(Expression* property);
424 
425  // Insert initializer statements for var-bindings shadowing parameter bindings
426  // from a non-simple parameter list.
427  void InsertShadowingVarBindingInitializers(Block* block);
428 
429  // Implement sloppy block-scoped functions, ES2015 Annex B 3.3
430  void InsertSloppyBlockFunctionVarBindings(DeclarationScope* scope);
431 
432  VariableProxy* NewUnresolved(const AstRawString* name, int begin_pos,
433  VariableKind kind = NORMAL_VARIABLE);
434  VariableProxy* NewUnresolved(const AstRawString* name);
435  Variable* Declare(Declaration* declaration,
436  DeclarationDescriptor::Kind declaration_kind,
437  VariableMode mode, InitializationFlag init,
438  Scope* declaration_scope = nullptr,
439  int var_end_pos = kNoSourcePosition);
440  Declaration* DeclareVariable(const AstRawString* name, VariableMode mode,
441  int pos);
442  Declaration* DeclareVariable(const AstRawString* name, VariableMode mode,
443  InitializationFlag init, int pos);
444 
445  bool TargetStackContainsLabel(const AstRawString* label);
446  BreakableStatement* LookupBreakTarget(const AstRawString* label);
447  IterationStatement* LookupContinueTarget(const AstRawString* label);
448 
449  Statement* BuildAssertIsCoercible(Variable* var, ObjectLiteral* pattern);
450 
451  // Factory methods.
452  FunctionLiteral* DefaultConstructor(const AstRawString* name, bool call_super,
453  int pos, int end_pos);
454 
455  // Skip over a lazy function, either using cached data if we have it, or
456  // by parsing the function with PreParser. Consumes the ending }.
457  // If may_abort == true, the (pre-)parser may decide to abort skipping
458  // in order to force the function to be eagerly parsed, after all.
459  // In case the preparser detects an error it cannot identify, it resets the
460  // scanner- and preparser state to the initial one, before PreParsing the
461  // function.
462  // SkipFunction returns true if it correctly parsed the function, including
463  // cases where we detect an error. It returns false, if we needed to stop
464  // parsing or could not identify an error correctly, meaning the caller needs
465  // to fully reparse. In this case it resets the scanner and preparser state.
466  bool SkipFunction(const AstRawString* function_name, FunctionKind kind,
467  FunctionLiteral::FunctionType function_type,
468  DeclarationScope* function_scope, int* num_parameters,
469  ProducedPreParsedScopeData** produced_preparsed_scope_data,
470  bool may_abort, FunctionLiteral::EagerCompileHint* hint);
471 
472  Block* BuildParameterInitializationBlock(
473  const ParserFormalParameters& parameters);
474  Block* BuildRejectPromiseOnException(Block* block);
475 
476  void ParseFunction(
477  ScopedPtrList<Statement>* body, const AstRawString* function_name,
478  int pos, FunctionKind kind, FunctionLiteral::FunctionType function_type,
479  DeclarationScope* function_scope, int* num_parameters,
480  int* function_length, bool* has_duplicate_parameters,
481  int* expected_property_count, int* suspend_count,
482  ZonePtrList<const AstRawString>* arguments_for_wrapped_function);
483 
484  void ThrowPendingError(Isolate* isolate, Handle<Script> script);
485 
486  class TemplateLiteral : public ZoneObject {
487  public:
488  TemplateLiteral(Zone* zone, int pos)
489  : cooked_(8, zone), raw_(8, zone), expressions_(8, zone), pos_(pos) {}
490 
491  const ZonePtrList<const AstRawString>* cooked() const { return &cooked_; }
492  const ZonePtrList<const AstRawString>* raw() const { return &raw_; }
493  const ZonePtrList<Expression>* expressions() const { return &expressions_; }
494  int position() const { return pos_; }
495 
496  void AddTemplateSpan(const AstRawString* cooked, const AstRawString* raw,
497  int end, Zone* zone) {
498  DCHECK_NOT_NULL(raw);
499  cooked_.Add(cooked, zone);
500  raw_.Add(raw, zone);
501  }
502 
503  void AddExpression(Expression* expression, Zone* zone) {
504  expressions_.Add(expression, zone);
505  }
506 
507  private:
510  ZonePtrList<Expression> expressions_;
511  int pos_;
512  };
513 
514  typedef TemplateLiteral* TemplateLiteralState;
515 
516  TemplateLiteralState OpenTemplateLiteral(int pos);
517  // "should_cook" means that the span can be "cooked": in tagged template
518  // literals, both the raw and "cooked" representations are available to user
519  // code ("cooked" meaning that escape sequences are converted to their
520  // interpreted values). Invalid escape sequences cause the cooked span
521  // to be represented by undefined, instead of being a syntax error.
522  // "tail" indicates that this span is the last in the literal.
523  void AddTemplateSpan(TemplateLiteralState* state, bool should_cook,
524  bool tail);
525  void AddTemplateExpression(TemplateLiteralState* state,
526  Expression* expression);
527  Expression* CloseTemplateLiteral(TemplateLiteralState* state, int start,
528  Expression* tag);
529 
530  ArrayLiteral* ArrayLiteralFromListWithSpread(
531  const ScopedPtrList<Expression>& list);
532  Expression* SpreadCall(Expression* function,
533  const ScopedPtrList<Expression>& args, int pos,
534  Call::PossiblyEval is_possibly_eval);
535  Expression* SpreadCallNew(Expression* function,
536  const ScopedPtrList<Expression>& args, int pos);
537  Expression* RewriteSuperCall(Expression* call_expression);
538 
539  void SetLanguageMode(Scope* scope, LanguageMode mode);
540  void SetAsmModule();
541 
542  // Rewrite all DestructuringAssignments in the current FunctionState.
543  void RewriteDestructuringAssignments();
544 
545  Expression* RewriteSpreads(ArrayLiteral* lit);
546 
547  void QueueDestructuringAssignmentForRewriting(
548  RewritableExpression* assignment);
549 
550  friend class InitializerRewriter;
551  void RewriteParameterInitializer(Expression* expr);
552 
553  Expression* BuildInitialYield(int pos, FunctionKind kind);
554  Assignment* BuildCreateJSGeneratorObject(int pos, FunctionKind kind);
555  Variable* AsyncGeneratorAwaitVariable();
556 
557  // Generic AST generator for throwing errors from compiled code.
558  Expression* NewThrowError(Runtime::FunctionId function_id,
559  MessageTemplate message, const AstRawString* arg,
560  int pos);
561 
562  void FinalizeIteratorUse(Variable* completion, Expression* condition,
563  Variable* iter, Block* iterator_use, Block* result,
564  IteratorType type);
565 
566  Statement* FinalizeForOfStatement(ForOfStatement* loop, Variable* completion,
567  IteratorType type, int pos);
568  void BuildIteratorClose(ZonePtrList<Statement>* statements,
569  Variable* iterator, Variable* input, Variable* output,
570  IteratorType type);
571  void BuildIteratorCloseForCompletion(ZonePtrList<Statement>* statements,
572  Variable* iterator,
573  Expression* completion,
574  IteratorType type);
575  Statement* CheckCallable(Variable* var, Expression* error, int pos);
576 
577  void RewriteAsyncFunctionBody(ScopedPtrList<Statement>* body, Block* block,
578  Expression* return_value);
579 
580  void AddArrowFunctionFormalParameters(ParserFormalParameters* parameters,
581  Expression* params, int end_pos);
582  void SetFunctionName(Expression* value, const AstRawString* name,
583  const AstRawString* prefix = nullptr);
584 
585  // Helper functions for recursive descent.
586  V8_INLINE bool IsEval(const AstRawString* identifier) const {
587  return identifier == ast_value_factory()->eval_string();
588  }
589 
590  V8_INLINE bool IsAsync(const AstRawString* identifier) const {
591  return identifier == ast_value_factory()->async_string();
592  }
593 
594  V8_INLINE bool IsArguments(const AstRawString* identifier) const {
595  return identifier == ast_value_factory()->arguments_string();
596  }
597 
598  V8_INLINE bool IsEvalOrArguments(const AstRawString* identifier) const {
599  return IsEval(identifier) || IsArguments(identifier);
600  }
601 
602  // Returns true if the expression is of type "this.foo".
603  V8_INLINE static bool IsThisProperty(Expression* expression) {
604  DCHECK_NOT_NULL(expression);
605  Property* property = expression->AsProperty();
606  return property != nullptr && property->obj()->IsVariableProxy() &&
607  property->obj()->AsVariableProxy()->is_this();
608  }
609 
610  // This returns true if the expression is an indentifier (wrapped
611  // inside a variable proxy). We exclude the case of 'this', which
612  // has been converted to a variable proxy.
613  V8_INLINE static bool IsIdentifier(Expression* expression) {
614  VariableProxy* operand = expression->AsVariableProxy();
615  return operand != nullptr && !operand->is_this() &&
616  !operand->is_new_target();
617  }
618 
619  V8_INLINE static const AstRawString* AsIdentifier(Expression* expression) {
620  DCHECK(IsIdentifier(expression));
621  return expression->AsVariableProxy()->raw_name();
622  }
623 
624  V8_INLINE VariableProxy* AsIdentifierExpression(Expression* expression) {
625  return expression->AsVariableProxy();
626  }
627 
628  V8_INLINE bool IsConstructor(const AstRawString* identifier) const {
629  return identifier == ast_value_factory()->constructor_string();
630  }
631 
632  V8_INLINE bool IsName(const AstRawString* identifier) const {
633  return identifier == ast_value_factory()->name_string();
634  }
635 
636  V8_INLINE static bool IsBoilerplateProperty(
637  ObjectLiteral::Property* property) {
638  return !property->IsPrototype();
639  }
640 
641  V8_INLINE bool IsNative(Expression* expr) const {
642  DCHECK_NOT_NULL(expr);
643  return expr->IsVariableProxy() &&
644  expr->AsVariableProxy()->raw_name() ==
645  ast_value_factory()->native_string();
646  }
647 
648  V8_INLINE static bool IsArrayIndex(const AstRawString* string,
649  uint32_t* index) {
650  return string->AsArrayIndex(index);
651  }
652 
653  // Returns true if the statement is an expression statement containing
654  // a single string literal. If a second argument is given, the literal
655  // is also compared with it and the result is true only if they are equal.
656  V8_INLINE bool IsStringLiteral(Statement* statement,
657  const AstRawString* arg = nullptr) const {
658  ExpressionStatement* e_stat = statement->AsExpressionStatement();
659  if (e_stat == nullptr) return false;
660  Literal* literal = e_stat->expression()->AsLiteral();
661  if (literal == nullptr || !literal->IsString()) return false;
662  return arg == nullptr || literal->AsRawString() == arg;
663  }
664 
665  V8_INLINE void GetDefaultStrings(
666  const AstRawString** default_string,
667  const AstRawString** star_default_star_string) {
668  *default_string = ast_value_factory()->default_string();
669  *star_default_star_string = ast_value_factory()->star_default_star_string();
670  }
671 
672  // Functions for encapsulating the differences between parsing and preparsing;
673  // operations interleaved with the recursive descent.
674  V8_INLINE void PushLiteralName(const AstRawString* id) {
675  fni_.PushLiteralName(id);
676  }
677 
678  V8_INLINE void PushVariableName(const AstRawString* id) {
679  fni_.PushVariableName(id);
680  }
681 
682  V8_INLINE void PushPropertyName(Expression* expression) {
683  if (expression->IsPropertyName()) {
684  fni_.PushLiteralName(expression->AsLiteral()->AsRawPropertyName());
685  } else {
686  fni_.PushLiteralName(ast_value_factory()->anonymous_function_string());
687  }
688  }
689 
690  V8_INLINE void PushEnclosingName(const AstRawString* name) {
691  fni_.PushEnclosingName(name);
692  }
693 
694  V8_INLINE void AddFunctionForNameInference(FunctionLiteral* func_to_infer) {
695  fni_.AddFunction(func_to_infer);
696  }
697 
698  V8_INLINE void InferFunctionName() { fni_.Infer(); }
699 
700  // If we assign a function literal to a property we pretenure the
701  // literal so it can be added as a constant function property.
702  V8_INLINE static void CheckAssigningFunctionLiteralToProperty(
703  Expression* left, Expression* right) {
704  DCHECK_NOT_NULL(left);
705  if (left->IsProperty() && right->IsFunctionLiteral()) {
706  right->AsFunctionLiteral()->set_pretenure();
707  }
708  }
709 
710  V8_INLINE static void MarkPatternAsAssigned(Expression* expression) {}
711 
712  // Determine if the expression is a variable proxy and mark it as being used
713  // in an assignment or with a increment/decrement operator.
714  V8_INLINE static void MarkExpressionAsAssigned(Expression* expression) {
715  DCHECK_NOT_NULL(expression);
716  if (expression->IsVariableProxy()) {
717  expression->AsVariableProxy()->set_is_assigned();
718  }
719  }
720 
721  // A shortcut for performing a ToString operation
722  V8_INLINE Expression* ToString(Expression* expr) {
723  if (expr->IsStringLiteral()) return expr;
724  ScopedPtrList<Expression> args(pointer_buffer());
725  args.Add(expr);
726  return factory()->NewCallRuntime(Runtime::kInlineToString, args,
727  expr->position());
728  }
729 
730  // Returns true if we have a binary expression between two numeric
731  // literals. In that case, *x will be changed to an expression which is the
732  // computed value.
733  bool ShortcutNumericLiteralBinaryExpression(Expression** x, Expression* y,
734  Token::Value op, int pos);
735 
736  // Returns true if we have a binary operation between a binary/n-ary
737  // expression (with the same operation) and a value, which can be collapsed
738  // into a single n-ary expression. In that case, *x will be changed to an
739  // n-ary expression.
740  bool CollapseNaryExpression(Expression** x, Expression* y, Token::Value op,
741  int pos, const SourceRange& range);
742 
743  // Returns a UnaryExpression or, in one of the following cases, a Literal.
744  // ! <literal> -> true / false
745  // + <Number literal> -> <Number literal>
746  // - <Number literal> -> <Number literal with value negated>
747  // ~ <literal> -> true / false
748  Expression* BuildUnaryExpression(Expression* expression, Token::Value op,
749  int pos);
750 
751  // Generate AST node that throws a ReferenceError with the given type.
752  V8_INLINE Expression* NewThrowReferenceError(MessageTemplate message,
753  int pos) {
754  return NewThrowError(Runtime::kNewReferenceError, message,
755  ast_value_factory()->empty_string(), pos);
756  }
757 
758  // Generate AST node that throws a SyntaxError with the given
759  // type. The first argument may be null (in the handle sense) in
760  // which case no arguments are passed to the constructor.
761  V8_INLINE Expression* NewThrowSyntaxError(MessageTemplate message,
762  const AstRawString* arg, int pos) {
763  return NewThrowError(Runtime::kNewSyntaxError, message, arg, pos);
764  }
765 
766  // Generate AST node that throws a TypeError with the given
767  // type. Both arguments must be non-null (in the handle sense).
768  V8_INLINE Expression* NewThrowTypeError(MessageTemplate message,
769  const AstRawString* arg, int pos) {
770  return NewThrowError(Runtime::kNewTypeError, message, arg, pos);
771  }
772 
773  // Reporting errors.
774  void ReportMessageAt(Scanner::Location source_location,
775  MessageTemplate message, const char* arg = nullptr,
776  ParseErrorType error_type = kSyntaxError) {
777  if (stack_overflow()) {
778  // Suppress the error message (syntax error or such) in the presence of a
779  // stack overflow. The isolate allows only one pending exception at at
780  // time
781  // and we want to report the stack overflow later.
782  return;
783  }
784  pending_error_handler()->ReportMessageAt(source_location.beg_pos,
785  source_location.end_pos, message,
786  arg, error_type);
787  scanner_.set_parser_error();
788  }
789 
790  // Dummy implementation. The parser should never have a unidentifiable
791  // error.
792  V8_INLINE void ReportUnidentifiableError() { UNREACHABLE(); }
793 
794  void ReportMessageAt(Scanner::Location source_location,
795  MessageTemplate message, const AstRawString* arg,
796  ParseErrorType error_type = kSyntaxError) {
797  if (stack_overflow()) {
798  // Suppress the error message (syntax error or such) in the presence of a
799  // stack overflow. The isolate allows only one pending exception at at
800  // time
801  // and we want to report the stack overflow later.
802  return;
803  }
804  pending_error_handler()->ReportMessageAt(source_location.beg_pos,
805  source_location.end_pos, message,
806  arg, error_type);
807  scanner_.set_parser_error();
808  }
809 
810  // "null" return type creators.
811  V8_INLINE static std::nullptr_t NullIdentifier() { return nullptr; }
812  V8_INLINE static std::nullptr_t NullExpression() { return nullptr; }
813  V8_INLINE static std::nullptr_t NullLiteralProperty() { return nullptr; }
814  V8_INLINE static ZonePtrList<Expression>* NullExpressionList() {
815  return nullptr;
816  }
817  V8_INLINE static ZonePtrList<Statement>* NullStatementList() {
818  return nullptr;
819  }
820  V8_INLINE static std::nullptr_t NullStatement() { return nullptr; }
821  Expression* FailureExpression() { return factory()->FailureExpression(); }
822 
823  template <typename T>
824  V8_INLINE static bool IsNull(T subject) {
825  return subject == nullptr;
826  }
827 
828  // Non-null empty string.
829  V8_INLINE const AstRawString* EmptyIdentifierString() const {
830  return ast_value_factory()->empty_string();
831  }
832 
833  // Producing data during the recursive descent.
834  V8_INLINE const AstRawString* GetSymbol() const {
835  const AstRawString* result = scanner()->CurrentSymbol(ast_value_factory());
836  DCHECK_NOT_NULL(result);
837  return result;
838  }
839 
840  V8_INLINE const AstRawString* GetNextSymbol() const {
841  return scanner()->NextSymbol(ast_value_factory());
842  }
843 
844  V8_INLINE const AstRawString* GetNumberAsSymbol() const {
845  double double_value = scanner()->DoubleValue();
846  char array[100];
847  const char* string = DoubleToCString(double_value, ArrayVector(array));
848  return ast_value_factory()->GetOneByteString(string);
849  }
850 
851  V8_INLINE Expression* ThisExpression(int pos = kNoSourcePosition) {
852  return NewUnresolved(ast_value_factory()->this_string(), pos,
853  THIS_VARIABLE);
854  }
855 
856  Expression* NewSuperPropertyReference(int pos);
857  Expression* NewSuperCallReference(int pos);
858  Expression* NewTargetExpression(int pos);
859  Expression* ImportMetaExpression(int pos);
860 
861  Expression* ExpressionFromLiteral(Token::Value token, int pos);
862 
863  V8_INLINE VariableProxy* ExpressionFromIdentifier(
864  const AstRawString* name, int start_position,
865  InferName infer = InferName::kYes) {
866  if (infer == InferName::kYes) {
867  fni_.PushVariableName(name);
868  }
869  return NewUnresolved(name, start_position);
870  }
871 
872  V8_INLINE ZonePtrList<Expression>* NewExpressionList(int size) const {
873  return new (zone()) ZonePtrList<Expression>(size, zone());
874  }
875  V8_INLINE ZonePtrList<ObjectLiteral::Property>* NewObjectPropertyList(
876  int size) const {
877  return new (zone()) ZonePtrList<ObjectLiteral::Property>(size, zone());
878  }
879  V8_INLINE ZonePtrList<ClassLiteral::Property>* NewClassPropertyList(
880  int size) const {
881  return new (zone()) ZonePtrList<ClassLiteral::Property>(size, zone());
882  }
883  V8_INLINE ZonePtrList<Statement>* NewStatementList(int size) const {
884  return new (zone()) ZonePtrList<Statement>(size, zone());
885  }
886 
887  Expression* NewV8Intrinsic(const AstRawString* name,
888  const ScopedPtrList<Expression>& args, int pos);
889 
890  V8_INLINE Statement* NewThrowStatement(Expression* exception, int pos) {
891  return factory()->NewExpressionStatement(
892  factory()->NewThrow(exception, pos), pos);
893  }
894 
895  V8_INLINE void AddFormalParameter(ParserFormalParameters* parameters,
896  Expression* pattern,
897  Expression* initializer,
898  int initializer_end_position,
899  bool is_rest) {
900  parameters->UpdateArityAndFunctionLength(initializer != nullptr, is_rest);
901  bool has_simple_name = pattern->IsVariableProxy() && initializer == nullptr;
902  const AstRawString* name = has_simple_name
903  ? pattern->AsVariableProxy()->raw_name()
904  : ast_value_factory()->empty_string();
905  auto parameter = new (parameters->scope->zone())
906  ParserFormalParameters::Parameter(name, pattern, initializer,
907  scanner()->location().beg_pos,
908  initializer_end_position, is_rest);
909 
910  parameters->params.Add(parameter);
911  }
912 
913  V8_INLINE void DeclareFormalParameters(ParserFormalParameters* parameters) {
914  ValidateFormalParameterInitializer();
915  bool is_simple = parameters->is_simple;
916  DeclarationScope* scope = parameters->scope;
917  if (!is_simple) scope->SetHasNonSimpleParameters();
918  for (auto parameter : parameters->params) {
919  bool is_optional = parameter->initializer() != nullptr;
920  // If the parameter list is simple, declare the parameters normally with
921  // their names. If the parameter list is not simple, declare a temporary
922  // for each parameter - the corresponding named variable is declared by
923  // BuildParamerterInitializationBlock.
924  if (is_simple && !parameters->has_duplicate() &&
925  scope->LookupLocal(parameter->name)) {
926  parameters->duplicate_loc =
927  Scanner::Location(parameter->position,
928  parameter->position + parameter->name->length());
929  }
930  scope->DeclareParameter(
931  is_simple ? parameter->name : ast_value_factory()->empty_string(),
932  is_simple ? VariableMode::kVar : VariableMode::kTemporary,
933  is_optional, parameter->is_rest(), ast_value_factory(),
934  parameter->position);
935  }
936  }
937 
938  void DeclareArrowFunctionFormalParameters(
939  ParserFormalParameters* parameters, Expression* params,
940  const Scanner::Location& params_loc);
941 
942  Expression* ExpressionListToExpression(const ScopedPtrList<Expression>& args);
943 
944  void SetFunctionNameFromPropertyName(LiteralProperty* property,
945  const AstRawString* name,
946  const AstRawString* prefix = nullptr);
947  void SetFunctionNameFromPropertyName(ObjectLiteralProperty* property,
948  const AstRawString* name,
949  const AstRawString* prefix = nullptr);
950 
951  void SetFunctionNameFromIdentifierRef(Expression* value,
952  Expression* identifier);
953 
955  GetReportedErrorList() const {
956  return function_state_->GetReportedErrorList();
957  }
958 
959  V8_INLINE void CountUsage(v8::Isolate::UseCounterFeature feature) {
960  ++use_counts_[feature];
961  }
962 
963  // Returns true iff we're parsing the first function literal during
964  // CreateDynamicFunction().
965  V8_INLINE bool ParsingDynamicFunctionDeclaration() const {
966  return parameters_end_pos_ != kNoSourcePosition;
967  }
968 
969  V8_INLINE void ConvertBinaryToNaryOperationSourceRange(
970  BinaryOperation* binary_op, NaryOperation* nary_op) {
971  if (source_range_map_ == nullptr) return;
972  DCHECK_NULL(source_range_map_->Find(nary_op));
973 
975  static_cast<BinaryOperationSourceRanges*>(
976  source_range_map_->Find(binary_op));
977  if (ranges == nullptr) return;
978 
979  SourceRange range = ranges->GetRange(SourceRangeKind::kRight);
980  source_range_map_->Insert(
981  nary_op, new (zone()) NaryOperationSourceRanges(zone(), range));
982  }
983 
984  V8_INLINE void AppendNaryOperationSourceRange(NaryOperation* node,
985  const SourceRange& range) {
986  if (source_range_map_ == nullptr) return;
987  NaryOperationSourceRanges* ranges =
988  static_cast<NaryOperationSourceRanges*>(source_range_map_->Find(node));
989  if (ranges == nullptr) return;
990 
991  ranges->AddRange(range);
992  DCHECK_EQ(node->subsequent_length(), ranges->RangeCount());
993  }
994 
995  V8_INLINE void RecordBlockSourceRange(Block* node,
996  int32_t continuation_position) {
997  if (source_range_map_ == nullptr) return;
998  source_range_map_->Insert(
999  node, new (zone()) BlockSourceRanges(continuation_position));
1000  }
1001 
1002  V8_INLINE void RecordCaseClauseSourceRange(CaseClause* node,
1003  const SourceRange& body_range) {
1004  if (source_range_map_ == nullptr) return;
1005  source_range_map_->Insert(node,
1006  new (zone()) CaseClauseSourceRanges(body_range));
1007  }
1008 
1009  V8_INLINE void RecordConditionalSourceRange(Expression* node,
1010  const SourceRange& then_range,
1011  const SourceRange& else_range) {
1012  if (source_range_map_ == nullptr) return;
1013  source_range_map_->Insert(
1014  node->AsConditional(),
1015  new (zone()) ConditionalSourceRanges(then_range, else_range));
1016  }
1017 
1018  V8_INLINE void RecordBinaryOperationSourceRange(
1019  Expression* node, const SourceRange& right_range) {
1020  if (source_range_map_ == nullptr) return;
1021  source_range_map_->Insert(node->AsBinaryOperation(),
1022  new (zone())
1023  BinaryOperationSourceRanges(right_range));
1024  }
1025 
1026  V8_INLINE void RecordJumpStatementSourceRange(Statement* node,
1027  int32_t continuation_position) {
1028  if (source_range_map_ == nullptr) return;
1029  source_range_map_->Insert(
1030  static_cast<JumpStatement*>(node),
1031  new (zone()) JumpStatementSourceRanges(continuation_position));
1032  }
1033 
1034  V8_INLINE void RecordIfStatementSourceRange(Statement* node,
1035  const SourceRange& then_range,
1036  const SourceRange& else_range) {
1037  if (source_range_map_ == nullptr) return;
1038  source_range_map_->Insert(
1039  node->AsIfStatement(),
1040  new (zone()) IfStatementSourceRanges(then_range, else_range));
1041  }
1042 
1043  V8_INLINE void RecordIterationStatementSourceRange(
1044  IterationStatement* node, const SourceRange& body_range) {
1045  if (source_range_map_ == nullptr) return;
1046  source_range_map_->Insert(
1047  node, new (zone()) IterationStatementSourceRanges(body_range));
1048  }
1049 
1050  V8_INLINE void RecordSuspendSourceRange(Expression* node,
1051  int32_t continuation_position) {
1052  if (source_range_map_ == nullptr) return;
1053  source_range_map_->Insert(static_cast<Suspend*>(node),
1054  new (zone())
1055  SuspendSourceRanges(continuation_position));
1056  }
1057 
1058  V8_INLINE void RecordSwitchStatementSourceRange(
1059  Statement* node, int32_t continuation_position) {
1060  if (source_range_map_ == nullptr) return;
1061  source_range_map_->Insert(
1062  node->AsSwitchStatement(),
1063  new (zone()) SwitchStatementSourceRanges(continuation_position));
1064  }
1065 
1066  V8_INLINE void RecordThrowSourceRange(Statement* node,
1067  int32_t continuation_position) {
1068  if (source_range_map_ == nullptr) return;
1069  ExpressionStatement* expr_stmt = static_cast<ExpressionStatement*>(node);
1070  Throw* throw_expr = expr_stmt->expression()->AsThrow();
1071  source_range_map_->Insert(
1072  throw_expr, new (zone()) ThrowSourceRanges(continuation_position));
1073  }
1074 
1075  V8_INLINE void RecordTryCatchStatementSourceRange(
1076  TryCatchStatement* node, const SourceRange& body_range) {
1077  if (source_range_map_ == nullptr) return;
1078  source_range_map_->Insert(
1079  node, new (zone()) TryCatchStatementSourceRanges(body_range));
1080  }
1081 
1082  V8_INLINE void RecordTryFinallyStatementSourceRange(
1083  TryFinallyStatement* node, const SourceRange& body_range) {
1084  if (source_range_map_ == nullptr) return;
1085  source_range_map_->Insert(
1086  node, new (zone()) TryFinallyStatementSourceRanges(body_range));
1087  }
1088 
1089  // Generate the next internal variable name for binding an exported namespace
1090  // object (used to implement the "export * as" syntax).
1091  const AstRawString* NextInternalNamespaceExportName();
1092 
1093  ParseInfo* info() const { return info_; }
1094 
1095  // Parser's private field members.
1096  friend class PreParserZoneScope; // Uses reusable_preparser().
1097 
1098  ParseInfo* info_;
1099  Scanner scanner_;
1100  Zone preparser_zone_;
1101  PreParser* reusable_preparser_;
1102  Mode mode_;
1103 
1104  SourceRangeMap* source_range_map_ = nullptr;
1105 
1106  friend class ParserTarget;
1107  friend class ParserTargetScope;
1108  ParserTarget* target_stack_; // for break, continue statements
1109 
1110  ScriptCompiler::CompileOptions compile_options_;
1111 
1112  // For NextInternalNamespaceExportName().
1113  int number_of_named_namespace_exports_ = 0;
1114 
1115  // Other information which will be stored in Parser and moved to Isolate after
1116  // parsing.
1117  int use_counts_[v8::Isolate::kUseCounterFeatureCount];
1118  int total_preparse_skipped_;
1119  bool allow_lazy_;
1120  bool temp_zoned_;
1121  ConsumedPreParsedScopeData* consumed_preparsed_scope_data_;
1122 
1123  // If not kNoSourcePosition, indicates that the first function literal
1124  // encountered is a dynamic function, see CreateDynamicFunction(). This field
1125  // indicates the correct position of the ')' that closes the parameter list.
1126  // After that ')' is encountered, this field is reset to kNoSourcePosition.
1127  int parameters_end_pos_;
1128 };
1129 
1130 // ----------------------------------------------------------------------------
1131 // Target is a support class to facilitate manipulation of the
1132 // Parser's target_stack_ (the stack of potential 'break' and
1133 // 'continue' statement targets). Upon construction, a new target is
1134 // added; it is removed upon destruction.
1135 
1137  public:
1139  : variable_(&parser->impl()->target_stack_),
1140  statement_(statement),
1141  previous_(parser->impl()->target_stack_) {
1142  parser->impl()->target_stack_ = this;
1143  }
1144 
1145  ~ParserTarget() { *variable_ = previous_; }
1146 
1147  ParserTarget* previous() { return previous_; }
1148  BreakableStatement* statement() { return statement_; }
1149 
1150  private:
1151  ParserTarget** variable_;
1152  BreakableStatement* statement_;
1153  ParserTarget* previous_;
1154 };
1155 
1157  public:
1158  explicit ParserTargetScope(ParserBase<Parser>* parser)
1159  : variable_(&parser->impl()->target_stack_),
1160  previous_(parser->impl()->target_stack_) {
1161  parser->impl()->target_stack_ = nullptr;
1162  }
1163 
1164  ~ParserTargetScope() { *variable_ = previous_; }
1165 
1166  private:
1167  ParserTarget** variable_;
1168  ParserTarget* previous_;
1169 };
1170 
1171 } // namespace internal
1172 } // namespace v8
1173 
1174 #endif // V8_PARSING_PARSER_H_
Definition: libplatform.h:13