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 */ 15 16import { Local } from '../base/Local'; 17import { Cfg } from '../graph/Cfg'; 18import { AliasType } from '../base/Type'; 19import { Trap } from '../base/Trap'; 20import { Value } from '../base/Value'; 21import { ArkAliasTypeDefineStmt } from '../base/Stmt'; 22import { LocalSignature } from './ArkSignature'; 23 24export class ArkBody { 25 private locals: Map<string, Local>; 26 private usedGlobals?: Map<string, Value>; 27 private cfg: Cfg; 28 private aliasTypeMap?: Map<string, [AliasType, ArkAliasTypeDefineStmt]>; 29 private traps?: Trap[]; 30 31 constructor(locals: Set<Local>, cfg: Cfg, aliasTypeMap?: Map<string, [AliasType, ArkAliasTypeDefineStmt]>, traps?: Trap[]) { 32 this.cfg = cfg; 33 this.aliasTypeMap = aliasTypeMap; 34 this.locals = new Map<string, Local>(); 35 locals.forEach(local => this.locals.set(local.getName(), local)); 36 this.traps = traps; 37 } 38 39 public getLocals(): Map<string, Local> { 40 return this.locals; 41 } 42 43 public setLocals(locals: Set<Local>): void { 44 if (!this.locals) { 45 this.locals = new Map<string, Local>(); 46 } 47 locals.forEach(local => this.locals.set(local.getName(), local)); 48 } 49 50 public addLocal(name: string, local: Local): void { 51 this.locals.set(name, local); 52 } 53 54 public getUsedGlobals(): Map<string, Value> | undefined { 55 return this.usedGlobals; 56 } 57 58 public setUsedGlobals(globals: Map<string, Value>): void { 59 this.usedGlobals = globals; 60 } 61 62 public getCfg(): Cfg { 63 return this.cfg; 64 } 65 66 public setCfg(cfg: Cfg): void { 67 this.cfg = cfg; 68 } 69 70 public getAliasTypeMap(): Map<string, [AliasType, ArkAliasTypeDefineStmt]> | undefined { 71 return this.aliasTypeMap; 72 } 73 74 public getAliasTypeByName(name: string): AliasType | null { 75 const aliasTypeInfo: [AliasType, ArkAliasTypeDefineStmt] | undefined = this.aliasTypeMap?.get(name); 76 if (aliasTypeInfo) { 77 return aliasTypeInfo[0]; 78 } 79 return null; 80 } 81 82 public getTraps(): Trap[] | undefined { 83 return this.traps; 84 } 85 86 public getExportLocalByName(name: string): Local | null { 87 const local = this.locals?.get(name); 88 if (local) { 89 local.setSignature(new LocalSignature(name, this.cfg.getDeclaringMethod().getSignature())); 90 return local; 91 } 92 return null; 93 } 94} 95