13af6ab5fSopenharmony_ci/** 23af6ab5fSopenharmony_ci * Copyright (c) 2021-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 "lexenv.h" 173af6ab5fSopenharmony_ci 183af6ab5fSopenharmony_ci#include "varbinder/variable.h" 193af6ab5fSopenharmony_ci#include "compiler/core/envScope.h" 203af6ab5fSopenharmony_ci#include "compiler/core/pandagen.h" 213af6ab5fSopenharmony_ci#include "compiler/core/moduleContext.h" 223af6ab5fSopenharmony_ci#include "ir/expressions/identifier.h" 233af6ab5fSopenharmony_ci 243af6ab5fSopenharmony_cinamespace ark::es2panda::compiler { 253af6ab5fSopenharmony_ci// Helpers 263af6ab5fSopenharmony_ci 273af6ab5fSopenharmony_cistatic bool CheckTdz(const ir::AstNode *node) 283af6ab5fSopenharmony_ci{ 293af6ab5fSopenharmony_ci return node->IsIdentifier() && node->AsIdentifier()->IsTdz(); 303af6ab5fSopenharmony_ci} 313af6ab5fSopenharmony_ci 323af6ab5fSopenharmony_cistatic void CheckConstAssignment(PandaGen *pg, const ir::AstNode *node, varbinder::Variable *variable) 333af6ab5fSopenharmony_ci{ 343af6ab5fSopenharmony_ci if (!variable->Declaration()->IsConstDecl()) { 353af6ab5fSopenharmony_ci return; 363af6ab5fSopenharmony_ci } 373af6ab5fSopenharmony_ci 383af6ab5fSopenharmony_ci pg->ThrowConstAssignment(node, variable->Name()); 393af6ab5fSopenharmony_ci} 403af6ab5fSopenharmony_ci 413af6ab5fSopenharmony_ci// VirtualLoadVar 423af6ab5fSopenharmony_ci 433af6ab5fSopenharmony_cistatic void ExpandLoadLexVar(PandaGen *pg, const ir::AstNode *node, const varbinder::ConstScopeFindResult &result) 443af6ab5fSopenharmony_ci{ 453af6ab5fSopenharmony_ci if (result.variable->Declaration()->IsVarDecl()) { 463af6ab5fSopenharmony_ci pg->LoadLexicalVar(node, result.lexLevel, result.variable->AsLocalVariable()->LexIdx()); 473af6ab5fSopenharmony_ci } else { 483af6ab5fSopenharmony_ci pg->LoadLexical(node, result.name, result.lexLevel, result.variable->AsLocalVariable()->LexIdx()); 493af6ab5fSopenharmony_ci } 503af6ab5fSopenharmony_ci} 513af6ab5fSopenharmony_ci 523af6ab5fSopenharmony_cistatic void ExpandLoadNormalVar(PandaGen *pg, const ir::AstNode *node, const varbinder::ConstScopeFindResult &result) 533af6ab5fSopenharmony_ci{ 543af6ab5fSopenharmony_ci auto *local = result.variable->AsLocalVariable(); 553af6ab5fSopenharmony_ci 563af6ab5fSopenharmony_ci if (CheckTdz(node)) { 573af6ab5fSopenharmony_ci pg->ThrowTdz(node, local->Name()); 583af6ab5fSopenharmony_ci } else { 593af6ab5fSopenharmony_ci pg->LoadAccumulator(node, local->Vreg()); 603af6ab5fSopenharmony_ci } 613af6ab5fSopenharmony_ci} 623af6ab5fSopenharmony_ci 633af6ab5fSopenharmony_civoid VirtualLoadVar::Expand(PandaGen *pg, const ir::AstNode *node, const varbinder::ConstScopeFindResult &result) 643af6ab5fSopenharmony_ci{ 653af6ab5fSopenharmony_ci if (result.variable->LexicalBound()) { 663af6ab5fSopenharmony_ci ExpandLoadLexVar(pg, node, result); 673af6ab5fSopenharmony_ci } else { 683af6ab5fSopenharmony_ci ExpandLoadNormalVar(pg, node, result); 693af6ab5fSopenharmony_ci } 703af6ab5fSopenharmony_ci} 713af6ab5fSopenharmony_ci 723af6ab5fSopenharmony_ci// VirtualStoreVar 733af6ab5fSopenharmony_ci 743af6ab5fSopenharmony_cistatic void StoreLocalExport(PandaGen *pg, const ir::AstNode *node, varbinder::Variable *variable) 753af6ab5fSopenharmony_ci{ 763af6ab5fSopenharmony_ci if (!variable->HasFlag(varbinder::VariableFlags::LOCAL_EXPORT) || !pg->Scope()->IsModuleScope()) { 773af6ab5fSopenharmony_ci return; 783af6ab5fSopenharmony_ci } 793af6ab5fSopenharmony_ci 803af6ab5fSopenharmony_ci auto range = pg->Scope()->AsModuleScope()->LocalExports().equal_range(variable); 813af6ab5fSopenharmony_ci 823af6ab5fSopenharmony_ci for (auto it = range.first; it != range.second; ++it) { 833af6ab5fSopenharmony_ci if (it->second != "default") { 843af6ab5fSopenharmony_ci pg->StoreModuleVar(node, it->second); 853af6ab5fSopenharmony_ci } 863af6ab5fSopenharmony_ci } 873af6ab5fSopenharmony_ci} 883af6ab5fSopenharmony_ci 893af6ab5fSopenharmony_cistatic void ExpandStoreLexVar(PandaGen *pg, const ir::AstNode *node, const varbinder::ConstScopeFindResult &result, 903af6ab5fSopenharmony_ci bool isDecl) 913af6ab5fSopenharmony_ci{ 923af6ab5fSopenharmony_ci varbinder::LocalVariable *local = result.variable->AsLocalVariable(); 933af6ab5fSopenharmony_ci 943af6ab5fSopenharmony_ci const auto *decl = result.variable->Declaration(); 953af6ab5fSopenharmony_ci 963af6ab5fSopenharmony_ci if (decl->IsLetOrConstDecl() && !isDecl) { 973af6ab5fSopenharmony_ci if (decl->IsConstDecl()) { 983af6ab5fSopenharmony_ci pg->ThrowConstAssignment(node, local->Name()); 993af6ab5fSopenharmony_ci } 1003af6ab5fSopenharmony_ci 1013af6ab5fSopenharmony_ci pg->StoreLexical(node, result.name, result.lexLevel, local->LexIdx()); 1023af6ab5fSopenharmony_ci } else { 1033af6ab5fSopenharmony_ci pg->StoreLexicalVar(node, result.lexLevel, local->LexIdx()); 1043af6ab5fSopenharmony_ci } 1053af6ab5fSopenharmony_ci 1063af6ab5fSopenharmony_ci StoreLocalExport(pg, node, local); 1073af6ab5fSopenharmony_ci} 1083af6ab5fSopenharmony_ci 1093af6ab5fSopenharmony_cistatic void ExpandStoreNormalVar(PandaGen *pg, const ir::AstNode *node, const varbinder::ConstScopeFindResult &result, 1103af6ab5fSopenharmony_ci bool isDecl) 1113af6ab5fSopenharmony_ci{ 1123af6ab5fSopenharmony_ci auto *local = result.variable->AsLocalVariable(); 1133af6ab5fSopenharmony_ci VReg localReg = local->Vreg(); 1143af6ab5fSopenharmony_ci 1153af6ab5fSopenharmony_ci if (!isDecl) { 1163af6ab5fSopenharmony_ci if (CheckTdz(node)) { 1173af6ab5fSopenharmony_ci pg->ThrowTdz(node, local->Name()); 1183af6ab5fSopenharmony_ci } 1193af6ab5fSopenharmony_ci 1203af6ab5fSopenharmony_ci CheckConstAssignment(pg, node, local); 1213af6ab5fSopenharmony_ci } 1223af6ab5fSopenharmony_ci 1233af6ab5fSopenharmony_ci pg->StoreAccumulator(node, localReg); 1243af6ab5fSopenharmony_ci StoreLocalExport(pg, node, local); 1253af6ab5fSopenharmony_ci} 1263af6ab5fSopenharmony_ci 1273af6ab5fSopenharmony_civoid VirtualStoreVar::Expand(PandaGen *pg, const ir::AstNode *node, const varbinder::ConstScopeFindResult &result, 1283af6ab5fSopenharmony_ci bool isDecl) 1293af6ab5fSopenharmony_ci{ 1303af6ab5fSopenharmony_ci if (result.variable->LexicalBound()) { 1313af6ab5fSopenharmony_ci ExpandStoreLexVar(pg, node, result, isDecl); 1323af6ab5fSopenharmony_ci } else { 1333af6ab5fSopenharmony_ci ExpandStoreNormalVar(pg, node, result, isDecl); 1343af6ab5fSopenharmony_ci } 1353af6ab5fSopenharmony_ci} 1363af6ab5fSopenharmony_ci} // namespace ark::es2panda::compiler 137