13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ci#include "capturedVariables.h"
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_cinamespace ark::es2panda::compiler {
193af6ab5fSopenharmony_ci
203af6ab5fSopenharmony_cistatic bool OnLeftSideOfAssignment(ir::Identifier const *const ident) noexcept
213af6ab5fSopenharmony_ci{
223af6ab5fSopenharmony_ci    return ident->Parent()->IsAssignmentExpression() && ident->Parent()->AsAssignmentExpression()->Left() == ident;
233af6ab5fSopenharmony_ci}
243af6ab5fSopenharmony_ci
253af6ab5fSopenharmony_cistatic void AddScopes(ir::AstNode *node, std::set<varbinder::Scope *> &scopes) noexcept
263af6ab5fSopenharmony_ci{
273af6ab5fSopenharmony_ci    if (node->Scope()->IsFunctionScope()) {
283af6ab5fSopenharmony_ci        scopes.emplace(node->Scope()->AsFunctionScope()->ParamScope());
293af6ab5fSopenharmony_ci    }
303af6ab5fSopenharmony_ci    if (node->Scope()->IsCatchScope()) {
313af6ab5fSopenharmony_ci        scopes.emplace(node->Scope()->AsCatchScope()->ParamScope());
323af6ab5fSopenharmony_ci    }
333af6ab5fSopenharmony_ci    if (node->Scope()->IsLoopScope()) {
343af6ab5fSopenharmony_ci        scopes.emplace(node->Scope()->AsLoopScope()->DeclScope());
353af6ab5fSopenharmony_ci    }
363af6ab5fSopenharmony_ci    scopes.emplace(node->Scope());
373af6ab5fSopenharmony_ci}
383af6ab5fSopenharmony_ci
393af6ab5fSopenharmony_cistatic varbinder::Variable *FindVariable(ir::Identifier *ident, std::set<varbinder::Scope *> const &scopes) noexcept
403af6ab5fSopenharmony_ci{
413af6ab5fSopenharmony_ci    auto *var = ident->Variable();
423af6ab5fSopenharmony_ci    //  NOTE! For some unknown reasons :) variables exist in scope collections but are not set to identifiers after
433af6ab5fSopenharmony_ci    //        'varbinder->IdentifierAnalysis()' pass. Probably need to be investigated and fixed sometimes...
443af6ab5fSopenharmony_ci    if (var == nullptr) {
453af6ab5fSopenharmony_ci        //  We start from the innermost scope!
463af6ab5fSopenharmony_ci        for (auto it = scopes.crbegin(); it != scopes.crend(); ++it) {
473af6ab5fSopenharmony_ci            auto res = (*it)->Find(ident->Name(), varbinder::ResolveBindingOptions::VARIABLES);
483af6ab5fSopenharmony_ci            if (res.variable != nullptr) {
493af6ab5fSopenharmony_ci                var = res.variable;
503af6ab5fSopenharmony_ci                break;
513af6ab5fSopenharmony_ci            }
523af6ab5fSopenharmony_ci        }
533af6ab5fSopenharmony_ci    }
543af6ab5fSopenharmony_ci
553af6ab5fSopenharmony_ci    if (var != nullptr) {
563af6ab5fSopenharmony_ci        auto *scope = var->GetScope();
573af6ab5fSopenharmony_ci        ASSERT(scope != nullptr);
583af6ab5fSopenharmony_ci        //  We are not interested in variables defined inside arrow function!
593af6ab5fSopenharmony_ci        if (scopes.find(scope) != scopes.cend()) {
603af6ab5fSopenharmony_ci            return nullptr;
613af6ab5fSopenharmony_ci        }
623af6ab5fSopenharmony_ci    }
633af6ab5fSopenharmony_ci
643af6ab5fSopenharmony_ci    return var;
653af6ab5fSopenharmony_ci}
663af6ab5fSopenharmony_ci
673af6ab5fSopenharmony_cistatic void FindModifiedCaptured(ir::ScriptFunction const *const scriptFunction,
683af6ab5fSopenharmony_ci                                 std::set<varbinder::Variable *> &variables) noexcept
693af6ab5fSopenharmony_ci{
703af6ab5fSopenharmony_ci    auto scopes = std::set<varbinder::Scope *> {};
713af6ab5fSopenharmony_ci    bool inLambda = false;
723af6ab5fSopenharmony_ci
733af6ab5fSopenharmony_ci    std::function<void(ir::AstNode *)> walker = [&](ir::AstNode *node) -> void {
743af6ab5fSopenharmony_ci        if (node->IsArrowFunctionExpression() || node->IsClassDeclaration()) {
753af6ab5fSopenharmony_ci            auto savedWL = inLambda;
763af6ab5fSopenharmony_ci            auto savedScopes = std::set<varbinder::Scope *> {};
773af6ab5fSopenharmony_ci            std::swap(scopes, savedScopes);
783af6ab5fSopenharmony_ci
793af6ab5fSopenharmony_ci            inLambda = true;
803af6ab5fSopenharmony_ci            node->Iterate(walker);
813af6ab5fSopenharmony_ci
823af6ab5fSopenharmony_ci            inLambda = savedWL;
833af6ab5fSopenharmony_ci            std::swap(scopes, savedScopes);
843af6ab5fSopenharmony_ci            savedScopes.clear();
853af6ab5fSopenharmony_ci
863af6ab5fSopenharmony_ci            return;
873af6ab5fSopenharmony_ci        }
883af6ab5fSopenharmony_ci
893af6ab5fSopenharmony_ci        if (inLambda && node->IsScopeBearer()) {
903af6ab5fSopenharmony_ci            AddScopes(node, scopes);
913af6ab5fSopenharmony_ci        } else if (inLambda && node->IsIdentifier() && OnLeftSideOfAssignment(node->AsIdentifier())) {
923af6ab5fSopenharmony_ci            if (auto *var = FindVariable(node->AsIdentifier(), scopes); var != nullptr) {
933af6ab5fSopenharmony_ci                variables.insert(var);
943af6ab5fSopenharmony_ci            }
953af6ab5fSopenharmony_ci        }
963af6ab5fSopenharmony_ci
973af6ab5fSopenharmony_ci        node->Iterate(walker);
983af6ab5fSopenharmony_ci    };
993af6ab5fSopenharmony_ci
1003af6ab5fSopenharmony_ci    scriptFunction->Iterate(walker);
1013af6ab5fSopenharmony_ci}
1023af6ab5fSopenharmony_ci
1033af6ab5fSopenharmony_cistatic void HandleScriptFunction(ir::ScriptFunction const *const scriptFunction) noexcept
1043af6ab5fSopenharmony_ci{
1053af6ab5fSopenharmony_ci    auto variables = std::set<varbinder::Variable *> {};
1063af6ab5fSopenharmony_ci    FindModifiedCaptured(scriptFunction, variables);
1073af6ab5fSopenharmony_ci
1083af6ab5fSopenharmony_ci    for (auto *variable : variables) {
1093af6ab5fSopenharmony_ci        variable->AddFlag(varbinder::VariableFlags::CAPTURED_MODIFIED);
1103af6ab5fSopenharmony_ci    }
1113af6ab5fSopenharmony_ci
1123af6ab5fSopenharmony_ci    variables.clear();
1133af6ab5fSopenharmony_ci}
1143af6ab5fSopenharmony_ci
1153af6ab5fSopenharmony_cibool CapturedVariables::Perform(public_lib::Context *ctx, parser::Program *program)
1163af6ab5fSopenharmony_ci{
1173af6ab5fSopenharmony_ci    if (ctx->config->options->CompilerOptions().compilationMode == CompilationMode::GEN_STD_LIB) {
1183af6ab5fSopenharmony_ci        for (auto &[_, ext_programs] : program->ExternalSources()) {
1193af6ab5fSopenharmony_ci            (void)_;
1203af6ab5fSopenharmony_ci            for (auto *extProg : ext_programs) {
1213af6ab5fSopenharmony_ci                Perform(ctx, extProg);
1223af6ab5fSopenharmony_ci            }
1233af6ab5fSopenharmony_ci        }
1243af6ab5fSopenharmony_ci    }
1253af6ab5fSopenharmony_ci
1263af6ab5fSopenharmony_ci    std::function<void(ir::AstNode *)> searchForFunctions = [&](ir::AstNode *ast) {
1273af6ab5fSopenharmony_ci        if (ast->IsScriptFunction()) {
1283af6ab5fSopenharmony_ci            HandleScriptFunction(ast->AsScriptFunction());  // no recursion
1293af6ab5fSopenharmony_ci        } else {
1303af6ab5fSopenharmony_ci            ast->Iterate(searchForFunctions);
1313af6ab5fSopenharmony_ci        }
1323af6ab5fSopenharmony_ci    };
1333af6ab5fSopenharmony_ci
1343af6ab5fSopenharmony_ci    program->Ast()->Iterate(searchForFunctions);
1353af6ab5fSopenharmony_ci    return true;
1363af6ab5fSopenharmony_ci}
1373af6ab5fSopenharmony_ci}  // namespace ark::es2panda::compiler
138