1/* 2 * Copyright (c) 2024 - 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import { BasicBlock } from 'arkanalyzer'; 16import Logger, { LOG_MODULE_TYPE } from 'arkanalyzer/lib/utils/logger'; 17import { Variable } from './Variable'; 18 19const logger = Logger.getLogger(LOG_MODULE_TYPE.HOMECHECK, 'Scope'); 20 21export enum TempLocation { 22 NOFOUND = 0, 23 LEFT, 24 RIGHT 25} 26 27export enum ScopeType { 28 IF_TYPE = 0, 29 ELSE_TYPE, 30 FOR_CONDITION_TYPE, 31 FOR_IN_TYPE, 32 WHILE_TYPE, 33 CASE_TYPE, 34 UNKNOWN_TYPE = 10 35} 36 37export class Scope { 38 parentScope: Scope | null; 39 childScopeList: Array<Scope>; 40 defList: Array<Variable>; 41 blocks: Set<BasicBlock>; 42 scopeLevel: number; 43 scopeType: ScopeType; 44 45 constructor(parent: Scope | null, defList: Array<Variable>, level: number, type: ScopeType = ScopeType.UNKNOWN_TYPE) { 46 this.parentScope = parent; 47 this.childScopeList = new Array<Scope>(); 48 this.defList = defList; 49 this.blocks = new Set<BasicBlock>(); 50 this.scopeLevel = level; 51 this.scopeType = type; 52 } 53 54 public setChildScope(child: Scope): void { 55 this.childScopeList.push(child); 56 } 57 58 public addVariable(variable: Variable): void { 59 this.defList.push(variable); 60 } 61}