// Copyright 2012 the V8 project authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef V8_PARSING_PARSER_H_ #define V8_PARSING_PARSER_H_ #include #include "src/ast/ast-source-ranges.h" #include "src/ast/ast.h" #include "src/ast/scopes.h" #include "src/base/compiler-specific.h" #include "src/base/threaded-list.h" #include "src/globals.h" #include "src/parsing/parser-base.h" #include "src/parsing/parsing.h" #include "src/parsing/preparser.h" #include "src/zone/zone-chunk-list.h" namespace v8 { class ScriptCompiler; namespace internal { class ConsumedPreParsedScopeData; class ParseInfo; class ParserTarget; class ParserTargetScope; class PendingCompilationErrorHandler; class PreParsedScopeData; class FunctionEntry { public: enum { kStartPositionIndex, kEndPositionIndex, kNumParametersIndex, kFlagsIndex, kNumInnerFunctionsIndex, kSize }; explicit FunctionEntry(Vector backing) : backing_(backing) { } FunctionEntry() : backing_() { } class LanguageModeField : public BitField {}; class UsesSuperPropertyField : public BitField {}; static uint32_t EncodeFlags(LanguageMode language_mode, bool uses_super_property) { return LanguageModeField::encode(language_mode) | UsesSuperPropertyField::encode(uses_super_property); } int start_pos() const { return backing_[kStartPositionIndex]; } int end_pos() const { return backing_[kEndPositionIndex]; } int num_parameters() const { return backing_[kNumParametersIndex]; } LanguageMode language_mode() const { return LanguageModeField::decode(backing_[kFlagsIndex]); } bool uses_super_property() const { return UsesSuperPropertyField::decode(backing_[kFlagsIndex]); } int num_inner_functions() const { return backing_[kNumInnerFunctionsIndex]; } bool is_valid() const { return !backing_.is_empty(); } private: Vector backing_; }; // ---------------------------------------------------------------------------- // JAVASCRIPT PARSING class Parser; struct ParserFormalParameters : FormalParametersBase { struct Parameter : public ZoneObject { Parameter(const AstRawString* name, Expression* pattern, Expression* initializer, int position, int initializer_end_position, bool is_rest) : name(name), pattern(pattern), initializer(initializer), position(position), initializer_end_position(initializer_end_position), is_rest(is_rest) {} const AstRawString* name; Expression* pattern; Expression* initializer; int position; int initializer_end_position; bool is_rest; Parameter* next_parameter = nullptr; bool is_simple() const { return pattern->IsVariableProxy() && initializer == nullptr && !is_rest; } Parameter** next() { return &next_parameter; } Parameter* const* next() const { return &next_parameter; } }; explicit ParserFormalParameters(DeclarationScope* scope) : FormalParametersBase(scope) {} base::ThreadedList params; }; template <> struct ParserTypes { typedef ParserBase Base; typedef Parser Impl; // Return types for traversing functions. typedef const AstRawString* Identifier; typedef v8::internal::Expression* Expression; typedef v8::internal::FunctionLiteral* FunctionLiteral; typedef ObjectLiteral::Property* ObjectLiteralProperty; typedef ClassLiteral::Property* ClassLiteralProperty; typedef v8::internal::Suspend* Suspend; typedef v8::internal::RewritableExpression* RewritableExpression; typedef ZonePtrList* ExpressionList; typedef ZonePtrList* ObjectPropertyList; typedef ZonePtrList* ClassPropertyList; typedef ParserFormalParameters FormalParameters; typedef v8::internal::Statement* Statement; typedef ZonePtrList* StatementList; typedef v8::internal::Block* Block; typedef v8::internal::BreakableStatement* BreakableStatement; typedef v8::internal::ForStatement* ForStatement; typedef v8::internal::IterationStatement* IterationStatement; typedef v8::internal::FuncNameInferrer FuncNameInferrer; typedef v8::internal::SourceRange SourceRange; typedef v8::internal::SourceRangeScope SourceRangeScope; // For constructing objects returned by the traversing functions. typedef AstNodeFactory Factory; typedef ParserTarget Target; typedef ParserTargetScope TargetScope; static constexpr bool ExpressionClassifierReportErrors = true; }; class V8_EXPORT_PRIVATE Parser : public NON_EXPORTED_BASE(ParserBase) { public: explicit Parser(ParseInfo* info); ~Parser() { delete reusable_preparser_; reusable_preparser_ = nullptr; } static bool IsPreParser() { return false; } void ParseOnBackground(ParseInfo* info); // Initializes an empty scope chain for top-level scripts, or scopes which // consist of only the native context. void InitializeEmptyScopeChain(ParseInfo* info); // 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 // consists of only a native context, maybe_outer_scope_info should be an // empty handle. // // 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. void DeserializeScopeChain(Isolate* isolate, ParseInfo* info, MaybeHandle maybe_outer_scope_info); // Move statistics to Isolate void UpdateStatistics(Isolate* isolate, Handle