@Override public void replaceChild(IAstNode child, IAstNode newChild) { if (!(child instanceof Expr && newChild instanceof Expr)) { throw new IllegalArgumentException(); } for (int i = 0; i < getNumChildren(); i++) { if (getChild(i) == child) { setChild(i, (Expr) newChild); return; } } throw new ChildDoesNotExistException(child, this); }
/** * Returns a subsituted expression, and may mutate the argument expression in the process. */ private Expr applySubstitutionDestructive(Expr expr, Map<String, Expr> paramReplacement) { assert !paramReplacement.values().contains(null); if (expr instanceof VariableIdentifierExpr && paramReplacement.containsKey(((VariableIdentifierExpr) expr).getName())) { return paramReplacement.get(((VariableIdentifierExpr) expr).getName()); } for (int i = 0; i < expr.getNumChildren(); i++) { Expr newChild = applySubstitutionDestructive(expr.getChild(i), paramReplacement); assert newChild != null; expr.setChild(i, newChild); } return expr; }
private void replaceChildWithGrandchild(Expr parent, int childIndex, int grandchildIndex) { assert parent.getChild(childIndex).getNumChildren() > grandchildIndex; parent .setChild(childIndex, new ParenExpr(parent.getChild(childIndex).getChild(grandchildIndex))); }
private void identifyMutationPoints(Expr expr, Set<Integer> indicesToSkip) { if (insideLValueCount == 0) { for (int i = 0; i < expr.getNumChildren(); i++) { if (indicesToSkip.contains(i)) { continue; } if (typer.hasType(expr.getChild(i))) { Scope clonedScope = currentScope.shallowClone(); if (shadingLanguageVersion.restrictedForLoops()) { for (Set<String> iterators : forLoopIterators) { iterators.forEach(clonedScope::remove); } } mutationPoints.add(new MutationPoint(expr, i, typer.lookupType(expr.getChild(i)), clonedScope, isConstContext(), shadingLanguageVersion, generator, generationParams)); } } } }
@Override void identifyReductionOpportunitiesForChild(IAstNode parent, Expr child) { if (!allowedToReduceExpr(parent, child)) { return; } if (inLValueContext()) { return; } final Type resultType = typer.lookupType(child); if (resultType == null) { return; } for (int i = 0; i < child.getNumChildren(); i++) { final Expr subExpr = child.getChild(i); final Type subExprType = typer.lookupType(subExpr); if (subExprType == null) { continue; } if (!subExprType.getWithoutQualifiers().equals(resultType.getWithoutQualifiers())) { continue; } addOpportunity(new SimplifyExprReductionOpportunity( parent, subExpr, child, // We mark this as deeper since we would prefer to reduce the root expression // to a constant. getVistitationDepth().deeper())); } }
private void cleanUpMacros(Expr parent) { for (int i = 0; i < parent.getNumChildren(); i++) { Expr child = parent.getChild(i); // Note: it would be redundant to have a special function reduction opportunity // be classed also as a fuzzed expression reduction opportunity if (MacroNames.isIdentity(child) || MacroNames.isZero(child) || MacroNames.isOne(child) || MacroNames.isFalse(child) || MacroNames.isTrue(child)) { replaceChildWithGrandchild(parent, i, 1); } else if (MacroNames.isFuzzed(child) || MacroNames.isDeadByConstruction(child)) { replaceChildWithGrandchild(parent, i, 0); } } }