@Override public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(FunctionDecl fd, VariableSubstitutionEnvironment env) throws CompilationException { List<VarIdentifier> newList = new ArrayList<>(fd.getParamList().size()); for (VarIdentifier vi : fd.getParamList()) { VariableExpr varExpr = new VariableExpr(vi); if (!env.constainsOldVar(varExpr)) { throw new CompilationException(ErrorCode.COMPILATION_ERROR, fd.getSourceLocation(), "Parameter " + vi + " does not appear in the substitution list."); } Expression newExpr = env.findSubstitution(varExpr); if (newExpr.getKind() != Kind.VARIABLE_EXPRESSION) { throw new CompilationException(ErrorCode.COMPILATION_ERROR, fd.getSourceLocation(), "Parameter " + vi + " cannot be substituted by a non-variable expression."); } newList.add(((VariableExpr) newExpr).getVar()); } Pair<ILangExpression, VariableSubstitutionEnvironment> p1 = fd.getFuncBody().accept(this, env); FunctionDecl newF = new FunctionDecl(fd.getSignature(), newList, (Expression) p1.first); newF.setSourceLocation(fd.getSourceLocation()); return new Pair<>(newF, env); }
@Override public FunctionDecl visit(FunctionDecl fd, Void arg) throws CompilationException { FunctionDecl copy = new FunctionDecl(fd.getSignature(), fd.getParamList(), (Expression) fd.getFuncBody().accept(this, arg)); copy.setSourceLocation(fd.getSourceLocation()); return copy; }
private Expression rewriteFunctionBody(FunctionDecl fnDecl) throws CompilationException { SourceLocation sourceLoc = fnDecl.getSourceLocation(); Query wrappedQuery = new Query(false); wrappedQuery.setSourceLocation(sourceLoc); wrappedQuery.setBody(fnDecl.getFuncBody()); wrappedQuery.setTopLevel(false); String fnNamespace = fnDecl.getSignature().getNamespace(); Dataverse defaultDataverse = metadataProvider.getDefaultDataverse(); Dataverse fnDataverse; if (fnNamespace == null || fnNamespace.equals(defaultDataverse.getDataverseName())) { fnDataverse = defaultDataverse; } else { try { fnDataverse = metadataProvider.findDataverse(fnNamespace); } catch (AlgebricksException e) { throw new CompilationException(ErrorCode.UNKNOWN_DATAVERSE, e, sourceLoc, fnNamespace); } } metadataProvider.setDefaultDataverse(fnDataverse); try { IQueryRewriter queryRewriter = rewriterFactory.createQueryRewriter(); queryRewriter.rewrite(declaredFunctions, wrappedQuery, metadataProvider, context, true, fnDecl.getParamList()); return wrappedQuery.getBody(); } finally { metadataProvider.setDefaultDataverse(defaultDataverse); } }