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 { ArkFile } from '../../core/model/ArkFile'; 17import { ArkMethod } from '../../core/model/ArkMethod'; 18import { ClassSignature, MethodSignature } from '../../core/model/ArkSignature'; 19import { ArkClass } from '../../core/model/ArkClass'; 20import { ArkCodeBuffer } from '../ArkStream'; 21import { Local } from '../../core/base/Local'; 22import { TransformerContext } from './SourceTransformer'; 23import { ArkNamespace } from '../../core/model/ArkNamespace'; 24import { BasePrinter } from '../base/BasePrinter'; 25 26export abstract class SourceBase extends BasePrinter implements TransformerContext { 27 protected arkFile: ArkFile; 28 protected inBuilder: boolean = false; 29 30 public constructor(arkFile: ArkFile, indent: string = '') { 31 super(indent); 32 this.arkFile = arkFile; 33 } 34 35 public getDeclaringArkNamespace(): ArkNamespace | undefined { 36 return undefined; 37 } 38 39 public getArkFile(): ArkFile { 40 return this.arkFile; 41 } 42 43 public getMethod(signature: MethodSignature): ArkMethod | null { 44 return this.getArkFile().getScene().getMethod(signature); 45 } 46 47 public getClass(signature: ClassSignature): ArkClass | null { 48 return this.getArkFile().getScene().getClass(signature); 49 } 50 51 public getPrinter(): ArkCodeBuffer { 52 return this.printer; 53 } 54 55 public transTemp2Code(temp: Local): string { 56 return temp.getName(); 57 } 58 59 public isInBuilderMethod(): boolean { 60 return this.inBuilder; 61 } 62 63 protected resolveKeywordType(keywordStr: string): string { 64 // 'NumberKeyword | NullKeyword | 65 let types: string[] = []; 66 for (let keyword of keywordStr.split('|')) { 67 keyword = keyword.trim(); 68 if (keyword.length === 0) { 69 continue; 70 } 71 if (keyword.endsWith('Keyword')) { 72 keyword = keyword.substring(0, keyword.length - 'Keyword'.length).toLowerCase(); 73 } 74 types.push(keyword); 75 } 76 77 return types.join(' | '); 78 } 79} 80