@specs-feup/clava
Version:
A C/C++ source-to-source compiler written in Typescript
119 lines • 3.72 kB
TypeScript
import SimplePass from "@specs-feup/lara/api/lara/pass/SimplePass.js";
import PassResult from "@specs-feup/lara/api/lara/pass/results/PassResult.js";
import { Joinpoint, Switch } from "../../Joinpoints.js";
/**
* Transforms a switch statement into an if statement.
*
* This means that code like this:
*
* ```c
* int num = 1, a;
*
* switch (num) {
* case 1:
* a = 10;
* default:
* a = 30;
* break;
* case 2:
* a = 20;
* break;
* case 3 ... 7:
* a = 30;
* }
* ```
*
* Will be transformed into:
*
* ```c
* int num = 1, a;
*
* if (num == 1)
* goto case_1;
* else if (num == 2)
* goto case_2;
* else if (num >= 3 && num <= 7)
* goto case_3_7;
* else
* goto case_default;
*
* case_1:
* a = 10;
* case_default:
* a = 80;
* goto switch_exit;
* case_2:
* a = 20;
* goto switch_exit;
* case_3_7:
* a = 30;
*
* switch_exit:
* ;
* ```
*/
export default class TransformSwitchToIf extends SimplePass {
/**
* Name of the pass
*/
protected _name: string;
/**
* Maps each case statement id to the corresponding label statement
*/
private caseLabels;
/**
* A list with the corresponding if statement for each case in the switch statement. For the default case, the list keeps its goto statement
*/
private caseIfStmts;
/**
* If true, uses deterministic ids for the labels (e.g. switch_exit_0, sw1_case_3...). Otherwise, uses $jp.astId whenever possible.
*/
private deterministicIds;
/**
* Current switch id, in case deterministic ids are used
*/
private currentId;
/**
* @param deterministicIds - If true, uses deterministic ids for the labels (e.g. switch_exit_0, sw1_case_3, ...). Otherwise, uses $jp.astId whenever possible.
*/
constructor(deterministicIds?: boolean);
matchJoinpoint($jp: Joinpoint): $jp is Switch;
/**
* Transformation to be applied to matching joinpoints
* @override
* @param $jp - Join point to transform
*/
transformJoinpoint($jp: Switch): PassResult;
/**
* Generates the label based on the switch ID and the values of the provided case statement.
* If no switch ID is provided, a generic label based on the case statement's AST ID is returned.
* @param $caseStmt - The case statement
* @returns The generated label name for the provided case statement
*/
private computeLabelName;
/**
* Creates if and label statements for each case in the provided switch statement and adds them to the private fields "caseIfStmts" and "caseLabels".
* @param $jp - The switch statement
*/
private computeIfAndLabels;
/**
* Reorders the private field "caseIfStmts" by moving the goto statement of the intermediate default case to the end.
*/
private moveDefaultToEnd;
/**
* Links the statements stored in the private field "caseIfStmts" by setting the body of their else as the next statement in the list
*/
private linkIfStmts;
/**
* Replaces the break statements that refer to the exit of the provided switch statement with goto statements.
* @param $jp - The switch statement
* @param $switchExitGoTo - The goto statement that corresponds to the switch exit. This statement will be used to replace the break statements
*/
private replaceBreakWithGoto;
/**
* Inserts the label and instructions of each case in the provided switch statement
* @param $jp - the switch statement
*/
addLabelsAndInstructions($jp: Switch): void;
}
//# sourceMappingURL=TransformSwitchToIf.d.ts.map