TranslationUnit implements IAstNode { public void updateTopLevelDeclaration(Declaration newDecl, Declaration existingDecl) { for (int i = 0; i < topLevelDeclarations.size(); i++) { if (topLevelDeclarations.get(i) == existingDecl) { topLevelDeclarations.set(i, newDecl); return; } } throw new IllegalArgumentException("Existing declaration not found."); } TranslationUnit(ShaderKind shaderKind,
                         Optional<ShadingLanguageVersion> shadingLanguageVersion,
                         List<Declaration> topLevelDeclarations);  TranslationUnit(Optional<ShadingLanguageVersion> shadingLanguageVersion,
                         List<Declaration> topLevelDeclarations); boolean hasMainFunction(); FunctionDefinition getMainFunction(); ShaderKind getShaderKind(); void setShaderKind(ShaderKind shaderKind); List<Declaration> getTopLevelDeclarations(); boolean hasShadingLanguageVersion(); ShadingLanguageVersion getShadingLanguageVersion(); void setShadingLanguageVersion(ShadingLanguageVersion shadingLanguageVersion); void setTopLevelDeclarations(List<Declaration> topLevelDeclarations); @Override void accept(IAstVisitor visitor); void addDeclaration(Declaration decl); void addDeclarationBefore(Declaration newDecl, Declaration existingDecl); void updateTopLevelDeclaration(Declaration newDecl, Declaration existingDecl); void removeTopLevelDeclaration(int index); void removeTopLevelDeclaration(Declaration declaration); @Override TranslationUnit clone(); List<VariableDeclInfo> getGlobalVarDeclInfos(); List<VariablesDeclaration> getGlobalVariablesDeclarations(); List<VariablesDeclaration> getUniformDecls(); VariablesDeclaration getUniformDeclaration(String name); boolean hasUniformDeclaration(String name); List<StructDefinitionType> getStructDefinitions(); StructDefinitionType getStructDefinition(StructNameType type);  }
@Test public void testUpdateTopLevelDeclaration() throws Exception { final String shader = "uniform float a[2];" + "void main()" + "{" + " float b = a[0];" + "}"; final TranslationUnit translationUnit = ParseHelper.parse(shader, ShaderKind.FRAGMENT); final VariablesDeclaration variablesDecl = translationUnit.getUniformDeclaration("a"); assertEquals(variablesDecl.getDeclInfos().get(0).getName(), "a"); assertTrue(variablesDecl.getDeclInfos().get(0).hasArrayInfo()); assertEquals(variablesDecl.getDeclInfos().get(0).getArrayInfo().getConstantSize(), Integer.valueOf(2)); assertEquals(((IntConstantExpr)variablesDecl.getDeclInfos().get(0).getArrayInfo() .getSizeExpr()).getNumericValue(), 2); final ArrayInfo arrayInfo = new ArrayInfo(new IntConstantExpr("3")); arrayInfo.setConstantSizeExpr(3); final VariableDeclInfo variableDeclInfo = new VariableDeclInfo("a", arrayInfo, null); final VariablesDeclaration newVariablesDeclaration = new VariablesDeclaration( new QualifiedType(BasicType.INT, Arrays.asList(TypeQualifier.UNIFORM)), variableDeclInfo ); translationUnit.updateTopLevelDeclaration(newVariablesDeclaration, variablesDecl); final VariablesDeclaration variablesDecl2 = translationUnit.getUniformDeclaration("a"); assertEquals(variablesDecl2.getDeclInfos().get(0).getName(), "a"); assertTrue(variablesDecl2.getDeclInfos().get(0).hasArrayInfo()); assertEquals(variablesDecl2.getDeclInfos().get(0).getArrayInfo().getConstantSize(), Integer.valueOf(3)); assertEquals(((IntConstantExpr)variablesDecl2.getDeclInfos().get(0).getArrayInfo() .getSizeExpr()).getNumericValue(), 3); }