chromium/v8/src/ast/scopes.cc

// 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.

#include "src/ast/scopes.h"

#include <optional>
#include <set>

#include "src/ast/ast.h"
#include "src/base/logging.h"
#include "src/builtins/accessors.h"
#include "src/common/message-template.h"
#include "src/heap/local-factory-inl.h"
#include "src/logging/runtime-call-stats-scope.h"
#include "src/objects/scope-info.h"
#include "src/objects/string-inl.h"
#include "src/objects/string-set.h"
#include "src/parsing/parse-info.h"
#include "src/parsing/parser.h"
#include "src/parsing/preparse-data.h"
#include "src/zone/zone.h"

namespace v8 {
namespace internal {

// ----------------------------------------------------------------------------
// Implementation of LocalsMap
//
// Note: We are storing the handle locations as key values in the hash map.
//       When inserting a new variable via Declare(), we rely on the fact that
//       the handle location remains alive for the duration of that variable
//       use. Because a Variable holding a handle with the same location exists
//       this is ensured.

static_assert;

VariableMap::VariableMap(Zone* zone)
    :{}

VariableMap::VariableMap(const VariableMap& other, Zone* zone)
    :{}

Variable* VariableMap::Declare(Zone* zone, Scope* scope,
                               const AstRawString* name, VariableMode mode,
                               VariableKind kind,
                               InitializationFlag initialization_flag,
                               MaybeAssignedFlag maybe_assigned_flag,
                               IsStaticFlag is_static_flag, bool* was_added) {}

void VariableMap::Remove(Variable* var) {}

void VariableMap::Add(Variable* var) {}

Variable* VariableMap::Lookup(const AstRawString* name) {}

// ----------------------------------------------------------------------------
// Implementation of Scope

Scope::Scope(Zone* zone, ScopeType scope_type)
    :{}

Scope::Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type)
    :{}

Variable* Scope::DeclareHomeObjectVariable(AstValueFactory* ast_value_factory) {}

Variable* Scope::DeclareStaticHomeObjectVariable(
    AstValueFactory* ast_value_factory) {}

DeclarationScope::DeclarationScope(Zone* zone,
                                   AstValueFactory* ast_value_factory,
                                   REPLMode repl_mode)
    :{}

DeclarationScope::DeclarationScope(Zone* zone, Scope* outer_scope,
                                   ScopeType scope_type,
                                   FunctionKind function_kind)
    :{}

ModuleScope::ModuleScope(DeclarationScope* script_scope,
                         AstValueFactory* avfactory)
    :{}

ModuleScope::ModuleScope(Handle<ScopeInfo> scope_info,
                         AstValueFactory* avfactory)
    :{}

ClassScope::ClassScope(Zone* zone, Scope* outer_scope, bool is_anonymous)
    :{}

template <typename IsolateT>
ClassScope::ClassScope(IsolateT* isolate, Zone* zone,
                       AstValueFactory* ast_value_factory,
                       Handle<ScopeInfo> scope_info)
    : Scope(zone, CLASS_SCOPE, ast_value_factory, scope_info),
      rare_data_and_is_parsing_heritage_(nullptr) {}
template ClassScope::ClassScope(Isolate* isolate, Zone* zone,
                                AstValueFactory* ast_value_factory,
                                Handle<ScopeInfo> scope_info);
template ClassScope::ClassScope(LocalIsolate* isolate, Zone* zone,
                                AstValueFactory* ast_value_factory,
                                Handle<ScopeInfo> scope_info);

Scope::Scope(Zone* zone, ScopeType scope_type,
             AstValueFactory* ast_value_factory, Handle<ScopeInfo> scope_info)
    :{}

DeclarationScope::DeclarationScope(Zone* zone, ScopeType scope_type,
                                   AstValueFactory* ast_value_factory,
                                   Handle<ScopeInfo> scope_info)
    :{}

Scope::Scope(Zone* zone, const AstRawString* catch_variable_name,
             MaybeAssignedFlag maybe_assigned, Handle<ScopeInfo> scope_info)
    :{}

void DeclarationScope::SetDefaults() {}

void Scope::SetDefaults() {}

bool Scope::HasSimpleParameters() {}

void DeclarationScope::set_should_eager_compile() {}

#if V8_ENABLE_WEBASSEMBLY
void DeclarationScope::set_is_asm_module() {}

bool Scope::IsAsmModule() const {}

bool Scope::ContainsAsmModule() const {}
#endif  // V8_ENABLE_WEBASSEMBLY

template <typename IsolateT>
Scope* Scope::DeserializeScopeChain(IsolateT* isolate, Zone* zone,
                                    Tagged<ScopeInfo> scope_info,
                                    DeclarationScope* script_scope,
                                    AstValueFactory* ast_value_factory,
                                    DeserializationMode deserialization_mode,
                                    ParseInfo* parse_info) {}

template <typename IsolateT>
void Scope::SetScriptScopeInfo(IsolateT* isolate,
                               DeclarationScope* script_scope) {}

template EXPORT_TEMPLATE_DEFINE() void Scope::SetScriptScopeInfo(Isolate* isolate,
                                                      DeclarationScope*
                                                          script_scope);
template EXPORT_TEMPLATE_DEFINE() void Scope::SetScriptScopeInfo(LocalIsolate* isolate,
                                                      DeclarationScope*
                                                          script_scope);

template EXPORT_TEMPLATE_DEFINE()
    Scope* Scope::DeserializeScopeChain(
        Isolate* isolate, Zone* zone, Tagged<ScopeInfo> scope_info,
        DeclarationScope* script_scope, AstValueFactory* ast_value_factory,
        DeserializationMode deserialization_mode, ParseInfo* parse_info);
template EXPORT_TEMPLATE_DEFINE()
    Scope* Scope::DeserializeScopeChain(
        LocalIsolate* isolate, Zone* zone, Tagged<ScopeInfo> scope_info,
        DeclarationScope* script_scope, AstValueFactory* ast_value_factory,
        DeserializationMode deserialization_mode, ParseInfo* parse_info);

DeclarationScope* Scope::AsDeclarationScope() {}

const DeclarationScope* Scope::AsDeclarationScope() const {}

ModuleScope* Scope::AsModuleScope() {}

const ModuleScope* Scope::AsModuleScope() const {}

ClassScope* Scope::AsClassScope() {}

const ClassScope* Scope::AsClassScope() const {}

void DeclarationScope::DeclareSloppyBlockFunction(
    SloppyBlockFunctionStatement* sloppy_block_function) {}

void DeclarationScope::HoistSloppyBlockFunctions(AstNodeFactory* factory) {}

void DeclarationScope::TakeUnresolvedReferencesFromParent() {}

bool DeclarationScope::Analyze(ParseInfo* info) {}

void DeclarationScope::DeclareThis(AstValueFactory* ast_value_factory) {}

void DeclarationScope::DeclareArguments(AstValueFactory* ast_value_factory) {}

void DeclarationScope::DeclareDefaultFunctionVariables(
    AstValueFactory* ast_value_factory) {}

Variable* DeclarationScope::DeclareFunctionVar(const AstRawString* name,
                                               Scope* cache) {}

Variable* DeclarationScope::DeclareGeneratorObjectVar(
    const AstRawString* name) {}

Scope* Scope::FinalizeBlockScope() {}

void DeclarationScope::AddLocal(Variable* var) {}

void Scope::Snapshot::Reparent(DeclarationScope* new_parent) {}

Variable* Scope::LookupInScopeInfo(const AstRawString* name, Scope* cache) {}

Variable* DeclarationScope::DeclareParameter(const AstRawString* name,
                                             VariableMode mode,
                                             bool is_optional, bool is_rest,
                                             AstValueFactory* ast_value_factory,
                                             int position) {}

void DeclarationScope::RecordParameter(bool is_rest) {}

Variable* Scope::DeclareLocal(const AstRawString* name, VariableMode mode,
                              VariableKind kind, bool* was_added,
                              InitializationFlag init_flag) {}

Variable* Scope::DeclareVariable(
    Declaration* declaration, const AstRawString* name, int pos,
    VariableMode mode, VariableKind kind, InitializationFlag init,
    bool* was_added, bool* sloppy_mode_block_scope_function_redefinition,
    bool* ok) {}

Variable* Scope::DeclareVariableName(const AstRawString* name,
                                     VariableMode mode, bool* was_added,
                                     VariableKind kind) {}

Variable* Scope::DeclareCatchVariableName(const AstRawString* name) {}

void Scope::AddUnresolved(VariableProxy* proxy) {}

Variable* DeclarationScope::DeclareDynamicGlobal(const AstRawString* name,
                                                 VariableKind kind,
                                                 Scope* cache) {}

void Scope::DeleteUnresolved(VariableProxy* var) {}

Variable* Scope::NewTemporary(const AstRawString* name) {}

Variable* Scope::NewTemporary(const AstRawString* name,
                              MaybeAssignedFlag maybe_assigned) {}

Declaration* DeclarationScope::CheckConflictingVarDeclarations(
    bool* allowed_catch_binding_var_redeclaration) {}

const AstRawString* Scope::FindVariableDeclaredIn(Scope* scope,
                                                  VariableMode mode_limit) {}

void DeclarationScope::DeserializeReceiver(AstValueFactory* ast_value_factory) {}

bool DeclarationScope::AllocateVariables(ParseInfo* info) {}

bool Scope::HasThisReference() const {}

bool Scope::AllowsLazyParsingWithoutUnresolvedVariables(
    const Scope* outer) const {}

bool DeclarationScope::AllowsLazyCompilation() const {}

int Scope::ContextChainLength(Scope* scope) const {}

int Scope::ContextChainLengthUntilOutermostSloppyEval() const {}

DeclarationScope* Scope::GetDeclarationScope() {}

DeclarationScope* Scope::GetNonEvalDeclarationScope() {}

const DeclarationScope* Scope::GetClosureScope() const {}

DeclarationScope* Scope::GetClosureScope() {}

bool Scope::NeedsScopeInfo() const {}

bool Scope::ShouldBanArguments() {}

DeclarationScope* Scope::GetReceiverScope() {}

DeclarationScope* Scope::GetConstructorScope() {}

Scope* Scope::GetHomeObjectScope() {}

DeclarationScope* Scope::GetScriptScope() {}

Scope* Scope::GetOuterScopeWithContext() {}

namespace {
bool WasLazilyParsed(Scope* scope) {}

}  // namespace

template <typename FunctionType>
void Scope::ForEach(FunctionType callback) {}

bool Scope::IsConstructorScope() const {}

bool Scope::IsOuterScopeOf(Scope* other) const {}

void Scope::AnalyzePartially(DeclarationScope* max_outer_scope,
                             AstNodeFactory* ast_node_factory,
                             UnresolvedList* new_unresolved_list,
                             bool maybe_in_arrowhead) {}

void DeclarationScope::ResetAfterPreparsing(AstValueFactory* ast_value_factory,
                                            bool aborted) {}

bool Scope::IsSkippableFunctionScope() {}

void Scope::SavePreparseData(Parser* parser) {}

void DeclarationScope::SavePreparseDataForDeclarationScope(Parser* parser) {}

void DeclarationScope::AnalyzePartially(Parser* parser,
                                        AstNodeFactory* ast_node_factory,
                                        bool maybe_in_arrowhead) {}

void DeclarationScope::RewriteReplGlobalVariables() {}

#ifdef DEBUG
namespace {

const char* Header(ScopeType scope_type, FunctionKind function_kind,
                   bool is_declaration_scope) {}

void Indent(int n, const char* str) {}

void PrintName(const AstRawString* name) {}

void PrintLocation(Variable* var) {}

void PrintVar(int indent, Variable* var) {}

void PrintMap(int indent, const char* label, VariableMap* map, bool locals,
              Variable* function_var) {}

}  // anonymous namespace

void DeclarationScope::PrintParameters() {}

void Scope::Print(int n) {}

void Scope::CheckScopePositions() {}

void Scope::CheckZones() {}
#endif  // DEBUG

Variable* Scope::NonLocal(const AstRawString* name, VariableMode mode) {}

void Scope::ForceDynamicLookup(VariableProxy* proxy) {}

// static
template <Scope::ScopeLookupMode mode>
Variable* Scope::Lookup(VariableProxy* proxy, Scope* scope,
                        Scope* outer_scope_end, Scope* cache_scope,
                        bool force_context_allocation) {}

template Variable* Scope::Lookup<Scope::kParsedScope>(
    VariableProxy* proxy, Scope* scope, Scope* outer_scope_end,
    Scope* cache_scope, bool force_context_allocation);
template Variable* Scope::Lookup<Scope::kDeserializedScope>(
    VariableProxy* proxy, Scope* scope, Scope* outer_scope_end,
    Scope* cache_scope, bool force_context_allocation);

Variable* Scope::LookupWith(VariableProxy* proxy, Scope* scope,
                            Scope* outer_scope_end, Scope* cache_scope,
                            bool force_context_allocation) {}

Variable* Scope::LookupSloppyEval(VariableProxy* proxy, Scope* scope,
                                  Scope* outer_scope_end, Scope* cache_scope,
                                  bool force_context_allocation) {}

void Scope::ResolveVariable(VariableProxy* proxy) {}

namespace {

void SetNeedsHoleCheck(Variable* var, VariableProxy* proxy,
                       Variable::ForceHoleInitializationFlag flag) {}

void UpdateNeedsHoleCheck(Variable* var, VariableProxy* proxy, Scope* scope) {}

}  // anonymous namespace

void Scope::ResolveTo(VariableProxy* proxy, Variable* var) {}

void Scope::ResolvePreparsedVariable(VariableProxy* proxy, Scope* scope,
                                     Scope* end) {}

bool Scope::ResolveVariablesRecursively(Scope* end) {}

bool Scope::MustAllocate(Variable* var) {}


bool Scope::MustAllocateInContext(Variable* var) {}

void Scope::AllocateStackSlot(Variable* var) {}


void Scope::AllocateHeapSlot(Variable* var) {}

void DeclarationScope::AllocateParameterLocals() {}

void DeclarationScope::AllocateParameter(Variable* var, int index) {}

void DeclarationScope::AllocateReceiver() {}

void Scope::AllocateNonParameterLocal(Variable* var) {}

void Scope::AllocateNonParameterLocalsAndDeclaredGlobals() {}

void DeclarationScope::AllocateLocals() {}

void ModuleScope::AllocateModuleVariables() {}

// Needs to be kept in sync with ScopeInfo::UniqueIdInScript and
// SharedFunctionInfo::UniqueIdInScript.
int Scope::UniqueIdInScript() const {}

void Scope::AllocateVariablesRecursively() {}

template <typename IsolateT>
void Scope::AllocateScopeInfosRecursively(
    IsolateT* isolate, MaybeHandle<ScopeInfo> outer_scope,
    std::unordered_map<int, Handle<ScopeInfo>>& scope_infos_to_reuse) {}

template EXPORT_TEMPLATE_DEFINE() void Scope::
    AllocateScopeInfosRecursively<Isolate>(
        Isolate* isolate, MaybeHandle<ScopeInfo> outer_scope,
        std::unordered_map<int, Handle<ScopeInfo>>& scope_infos_to_reuse);
template EXPORT_TEMPLATE_DEFINE() void Scope::
    AllocateScopeInfosRecursively<LocalIsolate>(
        LocalIsolate* isolate, MaybeHandle<ScopeInfo> outer_scope,
        std::unordered_map<int, Handle<ScopeInfo>>& scope_infos_to_reuse);

void DeclarationScope::RecalcPrivateNameContextChain() {}

void DeclarationScope::RecordNeedsPrivateNameContextChainRecalc() {}

// static
template <typename IsolateT>
void DeclarationScope::AllocateScopeInfos(ParseInfo* info,
                                          DirectHandle<Script> script,
                                          IsolateT* isolate) {}

template V8_EXPORT_PRIVATE void DeclarationScope::AllocateScopeInfos(
    ParseInfo* info, DirectHandle<Script> script, Isolate* isolate);
template V8_EXPORT_PRIVATE void DeclarationScope::AllocateScopeInfos(
    ParseInfo* info, DirectHandle<Script> script, LocalIsolate* isolate);

int Scope::ContextLocalCount() const {}

bool IsComplementaryAccessorPair(VariableMode a, VariableMode b) {}

Variable* ClassScope::DeclarePrivateName(const AstRawString* name,
                                         VariableMode mode,
                                         IsStaticFlag is_static_flag,
                                         bool* was_added) {}

Variable* ClassScope::LookupLocalPrivateName(const AstRawString* name) {}

UnresolvedList::Iterator ClassScope::GetUnresolvedPrivateNameTail() {}

void ClassScope::ResetUnresolvedPrivateNameTail(UnresolvedList::Iterator tail) {}

void ClassScope::MigrateUnresolvedPrivateNameTail(
    AstNodeFactory* ast_node_factory, UnresolvedList::Iterator tail) {}

Variable* ClassScope::LookupPrivateNameInScopeInfo(const AstRawString* name) {}

Variable* ClassScope::LookupPrivateName(VariableProxy* proxy) {}

bool ClassScope::ResolvePrivateNames(ParseInfo* info) {}

VariableProxy* ClassScope::ResolvePrivateNamesPartially() {}

Variable* ClassScope::DeclareBrandVariable(AstValueFactory* ast_value_factory,
                                           IsStaticFlag is_static_flag,
                                           int class_token_pos) {}

Variable* ClassScope::DeclareClassVariable(AstValueFactory* ast_value_factory,
                                           const AstRawString* name,
                                           int class_token_pos) {}

PrivateNameScopeIterator::PrivateNameScopeIterator(Scope* start)
    :{}

void PrivateNameScopeIterator::Next() {}

void PrivateNameScopeIterator::AddUnresolvedPrivateName(VariableProxy* proxy) {}

}  // namespace internal
}  // namespace v8