StructificationMutation implements Mutation { @Override public void apply() { List<StructDefinitionType> generatedStructs = randomStruct(0, generator, idGenerator, tu.getShadingLanguageVersion(), generationParams); generatedStructs.stream().map(VariablesDeclaration::new).forEach(tu::addDeclaration); final StructDefinitionType enclosingStruct = generatedStructs.get(0); final String enclosingStructVariableName = Constants.GLF_STRUCT_REPLACEMENT + getIdFromGeneratedStructName(enclosingStruct.getStructNameType()); Expr structifiedExpr = insertFieldIntoStruct( enclosingStructVariableName, enclosingStruct, generator); structifyBlock(structifiedExpr); structifyDeclaration(enclosingStructVariableName, enclosingStruct); } StructificationMutation(DeclarationStmt declToTransform, BlockStmt block,
                                 TranslationUnit tu,
                                 IdGenerator idGenerator,
                                 IRandom generator,
                                 GenerationParams generationParams); static List<StructDefinitionType> randomStruct(int currentDepth, IRandom generator,
                                                 IdGenerator idGenerator,
                                                 ShadingLanguageVersion shadingLanguageVersion,
                                                 GenerationParams generationParams); @Override void apply();  }
@Test public void applyStructification() { TranslationUnit tu = new TranslationUnit( Optional.empty(), Collections.singletonList(new VariablesDeclaration(BasicType.FLOAT, new ArrayList<>()))); DeclarationStmt declStmt = new DeclarationStmt( new VariablesDeclaration( BasicType.FLOAT, new VariableDeclInfo( "v", null, new Initializer( new FloatConstantExpr("3.0"))))); ExprStmt assignment = new ExprStmt( new BinaryExpr( new VariableIdentifierExpr("v"), new BinaryExpr( new VariableIdentifierExpr("v"), new VariableIdentifierExpr("v"), BinOp.ADD ), BinOp.ASSIGN ) ); BlockStmt block = new BlockStmt( Arrays.asList( declStmt.clone(), assignment.clone(), new BlockStmt(Arrays.asList(declStmt.clone(), assignment.clone()), true) ), true); CannedRandom generator = new CannedRandom( 2 , true , 1 , false , 0 , false , 1 , false , 0 , true , 0 , false , 0 , true, 0, 2 ); new StructificationMutation( (DeclarationStmt) block.getStmt(0), block, tu, new IdGenerator(), generator, GenerationParams.normal(ShaderKind.FRAGMENT, true)) .apply(); assertTrue(generator.isExhausted()); assertEquals(4, tu.getTopLevelDeclarations().size()); assertEquals(3, block.getNumStmts()); assertEquals(2, ((BlockStmt) block.getStmt(2)).getNumStmts()); assertEquals("v", ((DeclarationStmt) ((BlockStmt) block.getStmt(2)).getStmt(0)).getVariablesDeclaration() .getDeclInfo(0).getName()); assertEquals(BasicType.FLOAT, ((DeclarationStmt) ((BlockStmt) block.getStmt(2)).getStmt(0)).getVariablesDeclaration() .getBaseType()); assertEquals("v", ((VariableIdentifierExpr) ((BinaryExpr) ((ExprStmt) ((BlockStmt) block.getStmt(2)) .getStmt(1)).getExpr()).getLhs()).getName()); String newName = ((DeclarationStmt) block.getStmt(0)).getVariablesDeclaration().getDeclInfo(0).getName(); assertTrue(newName.startsWith(Constants.GLF_STRUCT_REPLACEMENT)); BinaryExpr newAssignment = (BinaryExpr) ((ExprStmt) block.getStmt(1)).getExpr(); assertTrue(newAssignment.getLhs() instanceof MemberLookupExpr); assertEquals("v", ((MemberLookupExpr) newAssignment.getLhs()).getMember()); assertTrue(((MemberLookupExpr) newAssignment.getLhs()).getStructure() instanceof MemberLookupExpr); assertEquals("_f0", ((MemberLookupExpr) ((MemberLookupExpr) newAssignment.getLhs()).getStructure()) .getMember()); assertTrue(((MemberLookupExpr) ((MemberLookupExpr) newAssignment.getLhs()).getStructure()) .getStructure() instanceof VariableIdentifierExpr); assertEquals(newName, ((VariableIdentifierExpr) ((MemberLookupExpr) ((MemberLookupExpr) newAssignment.getLhs()).getStructure()) .getStructure()).getName() ); }