FloatLiteralReplacer extends StandardVisitor { public static void replace(TranslationUnit tu, PipelineInfo pipelineInfo) { FloatLiteralReplacer floatLiteralReplacer = new FloatLiteralReplacer(tu); ConstCleaner.clean(tu); List<Float> literals = new ArrayList<>(); for (int i = 0; i < floatLiteralReplacer.uniformIndex; i++) { literals.add(null); } for (String s : floatLiteralReplacer.literalToUniformIndex.keySet()) { assert literals.get(floatLiteralReplacer.literalToUniformIndex.get(s)) == null; literals.set(floatLiteralReplacer.literalToUniformIndex.get(s), new Float(s)); } if (literals.size() > 0) { pipelineInfo.addUniform(Constants.FLOAT_CONST, BasicType.FLOAT, Optional.of(literals.size()), literals); } } private  FloatLiteralReplacer(TranslationUnit tu); static void replace(TranslationUnit tu, PipelineInfo pipelineInfo); @Override void visitFloatConstantExpr(FloatConstantExpr floatConstantExpr);  }
@Test public void testReplace() throws Exception { final String program = "" + "float foo(vec3 p, vec2 t) {" + " vec2 q = vec2(0.0);" + " return 0.0;" + "}" + "void main() { }"; final TranslationUnit tu = ParseHelper.parse(program); final PipelineInfo pipelineInfo = new PipelineInfo(); FloatLiteralReplacer.replace(tu, pipelineInfo); final String expectedProgram = "" + "uniform float _FLOAT_CONST[1];" + "float foo(vec3 p, vec2 t) {" + " vec2 q = vec2(_FLOAT_CONST[0]);" + " return _FLOAT_CONST[0];" + "}" + "void main() { }"; CompareAsts.assertEqualAsts(expectedProgram, tu); }
@Test public void testNothingToReplace() throws Exception { final String program = "#version 130\n" + "int foo(vec3 p, vec2 t) {" + " ivec2 q = ivec2(0);" + " return 0;" + "}" + "void main() { }"; final TranslationUnit tu = ParseHelper.parse(program); final PipelineInfo pipelineInfo = new PipelineInfo(); FloatLiteralReplacer.replace(tu, pipelineInfo); final String expectedProgram = "#version 130\n" + "int foo(vec3 p, vec2 t) {" + " ivec2 q = ivec2(0);" + " return 0;" + "}" + "void main() { }"; CompareAsts.assertEqualAsts(expectedProgram, tu); }