private static boolean canTypeCheckWithoutFailure(TranslationUnit reference, ShadingLanguageVersion shadingLanguageVersion) { // Debugging aid: fail early if we end up messing up the translation unit so that type checking // does not work. new Typer(reference, shadingLanguageVersion); return true; }
public boolean hasType(Expr expr) { return lookupType(expr) != null; }
private Type resolveBooleanResultType(Type lhsType, Type rhsType) { return maybeComputeBooleanVectorType(lhsType) .orElse(maybeComputeBooleanVectorType(rhsType) .orElse(BasicType.BOOL)); }
private Set<FunctionPrototype> findPossibleMatchesForCall(FunctionCallExpr functionCallExpr) { Set<FunctionPrototype> candidates = declaredFunctions.stream() .filter(proto -> proto.getName().equals(functionCallExpr.getCallee())) .filter(proto -> proto.getNumParameters() == functionCallExpr.getNumArgs()) .collect(Collectors.toSet()); for (int i = 0; i < functionCallExpr.getNumArgs(); i++) { if (!typer.hasType(functionCallExpr.getArg(i))) { // If we don't have a type for this argument, we're OK with any function prototype's type continue; } final int currentIndex = i; // Capture i in final variable so it can be used in lambda. candidates = candidates.stream().filter(proto -> typer.lookupType(functionCallExpr.getArg(currentIndex)).getWithoutQualifiers() .equals(proto.getParameters().get(currentIndex).getType().getWithoutQualifiers())) .collect(Collectors.toSet()); } return candidates; }
@Override public void visitFunctionCallExpr(FunctionCallExpr functionCallExpr) { super.visitFunctionCallExpr(functionCallExpr); List<FunctionPrototype> candidateBuiltins = TyperHelper.getBuiltins(shadingLanguageVersion) .get(functionCallExpr.getCallee()); if (candidateBuiltins != null) { for (FunctionPrototype prototype : candidateBuiltins) { if (prototypeMatches(prototype, functionCallExpr)) { types.put(functionCallExpr, prototype.getReturnType()); } } } Set<FunctionPrototype> candidateUserDefined = userDefinedFunctions.get(functionCallExpr.getCallee()); if (candidateUserDefined != null) { for (FunctionPrototype prototype : candidateUserDefined) { if (prototypeMatches(prototype, functionCallExpr)) { types.put(functionCallExpr, prototype.getReturnType()); } } } }
case LXOR: case NE: types.put(binaryExpr, resolveBooleanResultType(lhsType, rhsType)); return; case BAND_ASSIGN:
@Override public void visitFunctionCallExpr(FunctionCallExpr functionCallExpr) { FunctionPrototype bestMatch = null; for (FunctionPrototype candidate : typer.getPrototypes(functionCallExpr.getCallee())) { if (candidate.getNumParameters() == functionCallExpr.getNumArgs()) { if (bestMatch == null || callMatchesPrototype(functionCallExpr, candidate)) {
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)); } } } }
.indexOf(child); for (FunctionPrototype fp : getEncounteredFunctionPrototypes().stream() .filter(item -> typer.prototypeMatches(item, (FunctionCallExpr) parent)) .collect(Collectors.toList())) { if (fp.getParameter(childIndex).getType().hasQualifier(TypeQualifier.OUT_PARAM)
@Override public void visitParenExpr(ParenExpr parenExpr) { super.visitParenExpr(parenExpr); Type type = lookupType(parenExpr.getExpr()); if (type != null) { types.put(parenExpr, type); } }
SimplifyExprReductionOpportunities( TranslationUnit tu, ReducerContext context) { super(tu, context); this.typer = new Typer(tu, context.getShadingLanguageVersion()); this.inLiveInjectedStmtOrDeclaration = false; }
private boolean isOneFloatVec(Expr expr) { if (!(expr instanceof TypeConstructorExpr)) { return false; } if (!Arrays.asList(BasicType.VEC2, BasicType.VEC3, BasicType.VEC4) .contains(typer.lookupType(expr))) { return false; } return ((TypeConstructorExpr) expr).getArgs() .stream().allMatch(item -> isOneFloat(item) || isOneFloatVec(item)); }
private void makeInjectedArrayAccessesInBounds(TranslationUnit tu, List<Stmt> injectedStmts, ShadingLanguageVersion shadingLanguageVersion) { Typer typer = new Typer(tu, shadingLanguageVersion); for (Stmt stmt : injectedStmts) { MakeArrayAccessesInBounds.makeInBounds(stmt, typer); } }
private boolean isZeroFloatVecOrSquareMat(Expr expr) { if (!(expr instanceof TypeConstructorExpr)) { return false; } if (!Arrays.asList(BasicType.VEC2, BasicType.VEC3, BasicType.VEC4, BasicType.MAT2X2, BasicType.MAT3X3, BasicType.MAT4X4).contains(typer.lookupType(expr))) { return false; } return ((TypeConstructorExpr) expr).getArgs() .stream().allMatch(item -> isZeroFloat(item) || isZeroFloatVecOrSquareMat(item)); }
private Inliner(FunctionCallExpr call, TranslationUnit tu, ShadingLanguageVersion shadingLanguageVersion) { this.call = call; this.tu = tu; this.shadingLanguageVersion = shadingLanguageVersion; this.typer = new Typer(tu, shadingLanguageVersion); this.parentMap = IParentMap.createParentMap(tu); }
private void findFoldAddZeroOpportunities(IAstNode parent, Expr child, Expr thisHandSide, Expr thatHandSide) { if (isZeroFloat(thisHandSide)) { addReplaceWithExpr(parent, child, thatHandSide); } final Type childType = typer.lookupType(child); final Type thatHandSideType = typer.lookupType(thatHandSide); if (childType != null && thatHandSideType != null && childType.getWithoutQualifiers().equals(thatHandSideType.getWithoutQualifiers())) { if (isZeroFloatVecOrSquareMat(thisHandSide)) { addReplaceWithExpr(parent, child, thatHandSide); } } }
@Override public boolean apply(TranslationUnit tu, TransformationProbabilities probabilities, ShadingLanguageVersion shadingLanguageVersion, IRandom generator, GenerationParams generationParams) { List<IMutationPoint> mutationPoints = new MutationPoints(new Typer(tu, shadingLanguageVersion), tu, shadingLanguageVersion, generator, generationParams).getMutationPoints(probabilities); mutationPoints.forEach(IMutationPoint::applyMutation); return !mutationPoints.isEmpty(); }
private void findFoldSomethingSubZeroOpportunities(IAstNode parent, Expr child, Expr lhs, Expr rhs) { if (isZeroFloat(rhs)) { addReplaceWithExpr(parent, child, lhs); } final Type childType = typer.lookupType(child); final Type lhsType = typer.lookupType(lhs); if (childType != null && lhsType != null && childType.getWithoutQualifiers().equals(lhsType.getWithoutQualifiers())) { if (isZeroFloatVecOrSquareMat(rhs)) { addReplaceWithExpr(parent, child, lhs); } } }
private FunctionReductionOpportunities(TranslationUnit tu, ReducerContext context) { this.typer = new Typer(tu, context.getShadingLanguageVersion()); this.opportunities = new ArrayList<>(); this.calledFunctions = new HashSet<>(); this.declaredFunctions = Collections .unmodifiableList(AstUtil.getFunctionPrototypesFromShader(tu)); }
private void findFoldMulZeroOpportunities(IAstNode parent, Expr child, Expr thisHandSide, Expr thatHandSide) { if (isZeroFloat(thisHandSide)) { addReplaceWithZero(parent, child); } final Type childType = typer.lookupType(child); final Type thatHandSideType = typer.lookupType(thatHandSide); if (childType != null && thatHandSideType != null && childType.getWithoutQualifiers().equals(thatHandSideType.getWithoutQualifiers())) { if (isZeroFloatVecOrSquareMat(thisHandSide)) { addReplaceWithZero(parent, child); } } }