@Test public void prepareStatementToDonateSwitchWithBreakAndDefaultKept() throws Exception { final DonateLiveCodeTransformation dlc = getDummyTransformationObject(); final TranslationUnit donor = ParseHelper.parse("#version 310 es\n" + "void main() {\n" + " switch (0) {\n" + " case 0:\n" + " 1;\n" + " break;\n" + " default:\n" + " 2;\n" + " }\n" + "}\n"); final TranslationUnit reference = ParseHelper.parse("#version 310 es\n" + "void main() {\n" + " ;\n" + " switch (0) {\n" + " case 1:\n" + " 1;\n" + " default:\n" + " 2;\n" + " }\n" + "}\n"); for (IInjectionPoint injectionPoint : new InjectionPoints(reference, new RandomWrapper(0), item -> true).getAllInjectionPoints()) { final Stmt toDonate = donor.getMainFunction().getBody().getStmt(0).clone(); assert toDonate instanceof SwitchStmt; final DonationContext dc = new DonationContext(toDonate, new HashMap<>(), new ArrayList<>(), donor.getMainFunction()); final Stmt donated = dlc.prepareStatementToDonate(injectionPoint, dc, TransformationProbabilities.DEFAULT_PROBABILITIES, new RandomWrapper(0), ShadingLanguageVersion.ESSL_310); assertEquals("{\n" + " switch(0)\n" + " {\n" + " case 0:\n" + " 1;\n" + " break;\n" + " default:\n" + " 2;\n" + " }\n" + "}\n", donated.getText()); } }
@Override Stmt prepareStatementToDonate(IInjectionPoint injectionPoint, DonationContext donationContext, TransformationProbabilities probabilities, IRandom generator, ShadingLanguageVersion shadingLanguageVersion) { List<Stmt> donatedStmts = new ArrayList<>(); for (Map.Entry<String, Type> vars : donationContext.getFreeVariables().entrySet()) { final Type type = vars.getValue(); final Type typeWithRestrictedQualifiers = dropQualifiersThatCannotBeUsedForLocalVariable(type); final Initializer initializer; if (isLoopLimiter(vars.getKey(), typeWithRestrictedQualifiers.getWithoutQualifiers())) { initializer = new Initializer(new IntConstantExpr("0")); } else { initializer = getInitializer(injectionPoint, donationContext, typeWithRestrictedQualifiers, true, generator, shadingLanguageVersion); } final ImmutablePair<Type, ArrayInfo> baseTypeAndArrayInfo = Typer.getBaseTypeArrayInfo(typeWithRestrictedQualifiers); donatedStmts.add(new DeclarationStmt( new VariablesDeclaration(baseTypeAndArrayInfo.left, new VariableDeclInfo(vars.getKey(), baseTypeAndArrayInfo.right, initializer)))); } donatedStmts.add(donationContext.getDonorFragment()); BlockStmt donatedStmt = new BlockStmt(donatedStmts, true); new RemoveImmediateBreakStatements(donatedStmt); new RemoveImmediateContinueStatements(donatedStmt); new RemoveImmediateCaseLabels(donatedStmt); new RemoveReturnStatements(donatedStmt); new RemoveDiscardStatements(donatedStmt); return donatedStmt; }
DonateLiveCodeTransformation extends DonateCodeTransformation { @Override Stmt prepareStatementToDonate(IInjectionPoint injectionPoint, DonationContext donationContext, TransformationProbabilities probabilities, IRandom generator, ShadingLanguageVersion shadingLanguageVersion) { List<Stmt> donatedStmts = new ArrayList<>(); for (Map.Entry<String, Type> vars : donationContext.getFreeVariables().entrySet()) { final Type type = vars.getValue(); final Type typeWithRestrictedQualifiers = dropQualifiersThatCannotBeUsedForLocalVariable(type); final Initializer initializer; if (isLoopLimiter(vars.getKey(), typeWithRestrictedQualifiers.getWithoutQualifiers())) { initializer = new Initializer(new IntConstantExpr("0")); } else { initializer = getInitializer(injectionPoint, donationContext, typeWithRestrictedQualifiers, true, generator, shadingLanguageVersion); } final ImmutablePair<Type, ArrayInfo> baseTypeAndArrayInfo = Typer.getBaseTypeArrayInfo(typeWithRestrictedQualifiers); donatedStmts.add(new DeclarationStmt( new VariablesDeclaration(baseTypeAndArrayInfo.left, new VariableDeclInfo(vars.getKey(), baseTypeAndArrayInfo.right, initializer)))); } donatedStmts.add(donationContext.getDonorFragment()); BlockStmt donatedStmt = new BlockStmt(donatedStmts, true); new RemoveImmediateBreakStatements(donatedStmt); new RemoveImmediateContinueStatements(donatedStmt); new RemoveImmediateCaseLabels(donatedStmt); new RemoveReturnStatements(donatedStmt); new RemoveDiscardStatements(donatedStmt); return donatedStmt; } }
DonateLiveCodeTransformation extends DonateCodeTransformation { @Override Stmt prepareStatementToDonate(IInjectionPoint injectionPoint, DonationContext donationContext, TransformationProbabilities probabilities, IRandom generator, ShadingLanguageVersion shadingLanguageVersion) { List<Stmt> donatedStmts = new ArrayList<>(); for (Map.Entry<String, Type> vars : donationContext.getFreeVariables().entrySet()) { final Type type = vars.getValue(); final Type typeWithRestrictedQualifiers = dropQualifiersThatCannotBeUsedForLocalVariable(type); final Initializer initializer; if (isLoopLimiter(vars.getKey(), typeWithRestrictedQualifiers.getWithoutQualifiers())) { initializer = new Initializer(new IntConstantExpr("0")); } else { initializer = getInitializer(injectionPoint, donationContext, typeWithRestrictedQualifiers, true, generator, shadingLanguageVersion); } final ImmutablePair<Type, ArrayInfo> baseTypeAndArrayInfo = Typer.getBaseTypeArrayInfo(typeWithRestrictedQualifiers); donatedStmts.add(new DeclarationStmt( new VariablesDeclaration(baseTypeAndArrayInfo.left, new VariableDeclInfo(vars.getKey(), baseTypeAndArrayInfo.right, initializer)))); } donatedStmts.add(donationContext.getDonorFragment()); BlockStmt donatedStmt = new BlockStmt(donatedStmts, true); new RemoveImmediateBreakStatements(donatedStmt); new RemoveImmediateContinueStatements(donatedStmt); new RemoveImmediateCaseLabels(donatedStmt); new RemoveReturnStatements(donatedStmt); new RemoveDiscardStatements(donatedStmt); return donatedStmt; } DonateLiveCodeTransformation(Function<IRandom, Boolean> probabilityOfDonation,
                                      File donorsDirectory,
                                      GenerationParams generationParams,
                                      boolean allowLongLoops); }
DonateLiveCodeTransformation extends DonateCodeTransformation { @Override Stmt prepareStatementToDonate(IInjectionPoint injectionPoint, DonationContext donationContext, TransformationProbabilities probabilities, IRandom generator, ShadingLanguageVersion shadingLanguageVersion) { List<Stmt> donatedStmts = new ArrayList<>(); for (Map.Entry<String, Type> vars : donationContext.getFreeVariables().entrySet()) { final Type type = vars.getValue(); final Type typeWithRestrictedQualifiers = dropQualifiersThatCannotBeUsedForLocalVariable(type); final Initializer initializer; if (isLoopLimiter(vars.getKey(), typeWithRestrictedQualifiers.getWithoutQualifiers())) { initializer = new Initializer(new IntConstantExpr("0")); } else { initializer = getInitializer(injectionPoint, donationContext, typeWithRestrictedQualifiers, true, generator, shadingLanguageVersion); } final ImmutablePair<Type, ArrayInfo> baseTypeAndArrayInfo = Typer.getBaseTypeArrayInfo(typeWithRestrictedQualifiers); donatedStmts.add(new DeclarationStmt( new VariablesDeclaration(baseTypeAndArrayInfo.left, new VariableDeclInfo(vars.getKey(), baseTypeAndArrayInfo.right, initializer)))); } donatedStmts.add(donationContext.getDonorFragment()); BlockStmt donatedStmt = new BlockStmt(donatedStmts, true); new RemoveImmediateBreakStatements(donatedStmt); new RemoveImmediateContinueStatements(donatedStmt); new RemoveImmediateCaseLabels(donatedStmt); new RemoveReturnStatements(donatedStmt); new RemoveDiscardStatements(donatedStmt); return donatedStmt; } DonateLiveCodeTransformation(Function<IRandom, Boolean> probabilityOfDonation,
                                      File donorsDirectory,
                                      GenerationParams generationParams,
                                      boolean allowLongLoops); @Override String getName(); }
DonateLiveCodeTransformation extends DonateCodeTransformation { @Override Stmt prepareStatementToDonate(IInjectionPoint injectionPoint, DonationContext donationContext, TransformationProbabilities probabilities, IRandom generator, ShadingLanguageVersion shadingLanguageVersion) { List<Stmt> donatedStmts = new ArrayList<>(); for (Map.Entry<String, Type> vars : donationContext.getFreeVariables().entrySet()) { final Type type = vars.getValue(); final Type typeWithRestrictedQualifiers = dropQualifiersThatCannotBeUsedForLocalVariable(type); final Initializer initializer; if (isLoopLimiter(vars.getKey(), typeWithRestrictedQualifiers.getWithoutQualifiers())) { initializer = new Initializer(new IntConstantExpr("0")); } else { initializer = getInitializer(injectionPoint, donationContext, typeWithRestrictedQualifiers, true, generator, shadingLanguageVersion); } final ImmutablePair<Type, ArrayInfo> baseTypeAndArrayInfo = Typer.getBaseTypeArrayInfo(typeWithRestrictedQualifiers); donatedStmts.add(new DeclarationStmt( new VariablesDeclaration(baseTypeAndArrayInfo.left, new VariableDeclInfo(vars.getKey(), baseTypeAndArrayInfo.right, initializer)))); } donatedStmts.add(donationContext.getDonorFragment()); BlockStmt donatedStmt = new BlockStmt(donatedStmts, true); new RemoveImmediateBreakStatements(donatedStmt); new RemoveImmediateContinueStatements(donatedStmt); new RemoveImmediateCaseLabels(donatedStmt); new RemoveReturnStatements(donatedStmt); new RemoveDiscardStatements(donatedStmt); return donatedStmt; } DonateLiveCodeTransformation(Function<IRandom, Boolean> probabilityOfDonation,
                                      File donorsDirectory,
                                      GenerationParams generationParams,
                                      boolean allowLongLoops); @Override String getName(); static final String NAME; }