1/* 2 * Copyright (c) 2021 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 { CmdOptions } from "../cmdOptions"; 17import { NodeKind } from "../debuginfo"; 18import * as ts from "typescript"; 19import { 20 Label, 21 VReg 22} from "../irnodes"; 23import { PandaGen } from "../pandagen"; 24import { CatchTable } from "../statement/tryStatement"; 25 26export enum FunctionBuilderType { 27 NORMAL, 28 GENERATOR, 29 ASYNC, 30 ASYNCGENERATOR 31} 32 33export class FunctionBuilder { 34 protected pg: PandaGen | undefined = undefined; 35 protected funcObj: VReg | undefined = undefined; 36 protected resumeVal: VReg | undefined = undefined; 37 protected resumeType: VReg | undefined = undefined; 38 protected beginLabel: Label | undefined = undefined; 39 protected endLabel: Label | undefined = undefined; 40 41 constructor(pg: PandaGen) { 42 this.pg = pg; 43 } 44 45 // @ts-ignore 46 prepare(node: ts.Node): void { 47 } 48 49 // @ts-ignore 50 cleanUp(node: ts.Node): void { 51 } 52 53 functionAwait(node: ts.Node): void { 54 let pg = this.pg; 55 56 // value is in acc 57 pg.asyncFunctionAwaitUncaught(node, this.funcObj); 58 pg.suspendGenerator(node, this.funcObj); 59 60 pg.resumeGenerator(node, this.funcObj); 61 pg.storeAccumulator(node, this.resumeVal); 62 } 63 64 resumeGenerator(node: ts.Node | NodeKind) { 65 let pg = this.pg; 66 pg.resumeGenerator(node, this.funcObj); 67 pg.storeAccumulator(node, this.resumeVal); 68 pg.getResumeMode(node, this.funcObj); 69 pg.storeAccumulator(node, this.resumeType); 70 } 71 72 explicitReturn(node: ts.Node | NodeKind, empty ? : boolean) { 73 this.pg.return(node); 74 } 75 76 implicitReturn(node: ts.Node | NodeKind) { 77 CmdOptions.isWatchEvaluateExpressionMode() ? this.pg.return(NodeKind.Invalid) : this.pg.returnUndefined(node); 78 } 79 80 builderType(): FunctionBuilderType { 81 return FunctionBuilderType.NORMAL; 82 } 83}