| 19 | * Executing this operation returns nothing. |
| 20 | */ |
| 21 | export class TcbSwitchOp extends TcbOp { |
| 22 | constructor( |
| 23 | private tcb: Context, |
| 24 | private scope: Scope, |
| 25 | private block: SwitchBlock, |
| 26 | ) { |
| 27 | super(); |
| 28 | } |
| 29 | |
| 30 | override get optional() { |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | override execute(): null { |
| 35 | const switchExpression = tcbExpression(this.block.expression, this.tcb, this.scope); |
| 36 | const clauses = this.block.groups.flatMap<TcbExpr>((current) => { |
| 37 | const checkBody = this.tcb.env.config.checkControlFlowBodies; |
| 38 | const clauseScope = this.scope.createChildScope( |
| 39 | this.scope, |
| 40 | null, |
| 41 | checkBody ? current.children : [], |
| 42 | checkBody ? this.generateGuard(current, switchExpression) : null, |
| 43 | ); |
| 44 | |
| 45 | const statements = [...clauseScope.render(), new TcbExpr('break')]; |
| 46 | |
| 47 | return current.cases.map((switchCase, index) => { |
| 48 | const statementsStr = getStatementsBlock( |
| 49 | index === current.cases.length - 1 ? statements : [], |
| 50 | true /* singleLine */, |
| 51 | ); |
| 52 | |
| 53 | const source = |
| 54 | switchCase.expression === null |
| 55 | ? `default: ${statementsStr}` |
| 56 | : `case ${tcbExpression(switchCase.expression, this.tcb, this.scope).print()}: ${statementsStr}`; |
| 57 | |
| 58 | return new TcbExpr(source); |
| 59 | }); |
| 60 | }); |
| 61 | |
| 62 | if (this.block.exhaustiveCheck) { |
| 63 | let translateExpression = this.block.expression; |
| 64 | if (this.block.exhaustiveCheck.expression) { |
| 65 | translateExpression = this.block.exhaustiveCheck.expression; |
| 66 | } |
| 67 | |
| 68 | const switchValue = tcbExpression(translateExpression, this.tcb, this.scope); |
| 69 | const exhaustiveId = this.tcb.allocateId(); |
| 70 | |
| 71 | clauses.push( |
| 72 | new TcbExpr(`default: const tcbExhaustive${exhaustiveId}: never = ${switchValue.print()};`), |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | this.scope.addStatement( |
| 77 | new TcbExpr( |
| 78 | `switch (${switchExpression.print()}) { ${clauses.map((c) => c.print()).join('\n')} }`, |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…