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 { AbstractRef, ArkArrayRef, ArkInstanceFieldRef } from '../base/Ref'; 18import { Value } from '../base/Value'; 19 20/** 21 * Replace old use of a Ref inplace 22 */ 23export class RefUseReplacer { 24 private oldUse: Value; 25 private newUse: Value; 26 27 constructor(oldUse: Value, newUse: Value) { 28 this.oldUse = oldUse; 29 this.newUse = newUse; 30 } 31 32 public caseRef(ref: AbstractRef): void { 33 if (ref instanceof ArkInstanceFieldRef) { 34 this.caseFieldRef(ref); 35 } else if (ref instanceof ArkArrayRef) { 36 this.caseArrayRef(ref); 37 } 38 } 39 40 private caseFieldRef(ref: ArkInstanceFieldRef): void { 41 if (ref.getBase() === this.oldUse && this.newUse instanceof Local) { 42 ref.setBase(this.newUse); 43 } 44 } 45 46 private caseArrayRef(ref: ArkArrayRef): void { 47 if (ref.getBase() === this.oldUse) { 48 if (this.newUse instanceof Local) { 49 ref.setBase(this.newUse); 50 } 51 } else if (ref.getIndex() === this.oldUse) { 52 ref.setIndex(this.newUse); 53 } 54 } 55} 56