@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; }
@Override public Expression visit(FunctionDecl fd, ILangExpression arg) throws CompilationException { fd.setFuncBody(visit(fd.getFuncBody(), fd)); return null; }
@Override public Void visit(FunctionDecl fd, Integer step) throws CompilationException { out.println(skip(step) + "FunctionDecl " + fd.getSignature().getName() + "(" + fd.getParamList().toString() + ") {"); fd.getFuncBody().accept(this, step + 1); out.println(skip(step) + "}"); out.println(); return null; }
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); } }
implem.setFuncBody(rewriteFunctionBody(implem)); Iterator<VarIdentifier> paramIter = implem.getParamList().iterator(); VariableSubstitutionEnvironment subts = new VariableSubstitutionEnvironment(); for (Expression e : f.getExprList()) { implem.getFuncBody().accept(cloneVisitor, subts); Expression resExpr; if (clauses.isEmpty()) {
messageBuilder.append("function " + functionDecls.get(functionDecls.size() - 1).getSignature() + " depends upon function " + signature + " which is undefined"); } else { if (functionDecls.contains(functionDecl)) { throw new CompilationException(ErrorCode.COMPILATION_ERROR, functionCall.getSourceLocation(), "Recursive invocation " + functionDecls.get(functionDecls.size() - 1).getSignature() + " <==> " + functionDecl.getSignature()); functionDecls = retrieveUsedStoredFunctions(metadataProvider, functionDecl.getFuncBody(), declaredFunctions, functionDecls, functionCollector, functionParser, functionNormalizer);
final public FunctionDecl FunctionDeclaration() throws ParseException, ParseException { FunctionDecl funcDecl; FunctionSignature signature; String functionName; List<VarIdentifier> paramList = new ArrayList<VarIdentifier>(); Expression funcBody; createNewScope(); jj_consume_token(DECLARE); jj_consume_token(FUNCTION); functionName = Identifier(); paramList = ParameterList(); jj_consume_token(LEFTBRACE); funcBody = Expression(); jj_consume_token(RIGHTBRACE); signature = new FunctionSignature(defaultDataverse, functionName, paramList.size()); getCurrentScope().addFunctionDescriptor(signature, false); funcDecl = new FunctionDecl(signature, paramList, funcBody); removeCurrentScope(); {if (true) return funcDecl;} throw new Error("Missing return statement in function"); }
private static FunctionDecl findFuncDeclaration(FunctionSignature fid, List<FunctionDecl> sequence) { for (FunctionDecl f : sequence) { if (f.getSignature().equals(fid)) { return f; } } return null; } }
@Override public Void visit(FunctionDecl fd, Collection<VariableExpr> freeVars) throws CompilationException { fd.getFuncBody().accept(this, freeVars); return null; }
@Override public Void visit(FunctionDecl fd, Integer step) throws CompilationException { out.print(skip(step) + "declare function " + generateFullName(null, fd.getSignature().getName()) + "("); List<Identifier> parameters = new ArrayList<>(); parameters.addAll(fd.getParamList()); printDelimitedIdentifiers(parameters, COMMA); out.println(") {"); fd.getFuncBody().accept(this, step + 2); out.println(); out.print(skip(step) + "}"); out.println(";"); return null; }
final public FunctionDecl FunctionDeclaration() throws ParseException, ParseException { FunctionDecl funcDecl; FunctionSignature signature; String functionName; List<VarIdentifier> paramList = new ArrayList<VarIdentifier>(); Expression funcBody; createNewScope(); jj_consume_token(DECLARE); jj_consume_token(FUNCTION); functionName = Identifier(); paramList = ParameterList(); jj_consume_token(LEFTBRACE); funcBody = Expression(); jj_consume_token(RIGHTBRACE); signature = new FunctionSignature(defaultDataverse, functionName, paramList.size()); getCurrentScope().addFunctionDescriptor(signature, false); funcDecl = new FunctionDecl(signature, paramList, funcBody); removeCurrentScope(); {if (true) return funcDecl;} throw new Error("Missing return statement in function"); }
@Override public boolean equals(Object o) { return (o instanceof FunctionDecl && ((FunctionDecl) o).getSignature().equals(signature)); }
@Override public Boolean visit(FunctionDecl fd, ILangExpression arg) throws CompilationException { return fd.getFuncBody().accept(this, arg); }
@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 Expression visit(FunctionDecl fd, ILangExpression arg) throws CompilationException { fd.setFuncBody(visit(fd.getFuncBody(), fd)); return null; }
FunctionSignature signature = new FunctionSignature(defaultDataverse, functionName, paramList.size()); getCurrentScope().addFunctionDescriptor(signature, false); FunctionDecl stmt = new FunctionDecl(signature, paramList, funcBody); removeCurrentScope(); {if (true) return addSourceLocation(stmt, startToken);}
protected void inlineDeclaredUdfs(boolean inlineUdfs) throws CompilationException { List<FunctionSignature> funIds = new ArrayList<FunctionSignature>(); for (FunctionDecl fdecl : declaredFunctions) { funIds.add(fdecl.getSignature()); } List<FunctionDecl> usedStoredFunctionDecls = new ArrayList<>(); for (Expression topLevelExpr : topExpr.getDirectlyEnclosedExpressions()) { usedStoredFunctionDecls.addAll(FunctionUtil.retrieveUsedStoredFunctions(metadataProvider, topLevelExpr, funIds, null, expr -> getFunctionCalls(expr), func -> functionRepository.getFunctionDecl(func), (signature, sourceLoc) -> FunctionMapUtil.normalizeBuiltinFunctionSignature(signature, false, sourceLoc))); } declaredFunctions.addAll(usedStoredFunctionDecls); if (inlineUdfs && !declaredFunctions.isEmpty()) { SqlppInlineUdfsVisitor visitor = new SqlppInlineUdfsVisitor(context, new SqlppFunctionBodyRewriterFactory() /* the rewriter for function bodies expressions*/, declaredFunctions, metadataProvider); while (rewriteTopExpr(visitor, declaredFunctions)) { // loop until no more changes } } declaredFunctions.removeAll(usedStoredFunctionDecls); }
@Override public Boolean visit(FunctionDecl fd, List<FunctionDecl> arg) throws CompilationException { // Careful, we should only do this after analyzing the graph of function // calls. Pair<Boolean, Expression> p = inlineUdfsInExpr(fd.getFuncBody(), arg); fd.setFuncBody(p.second); return p.first; }
private void inlineDeclaredUdfs() throws CompilationException { if (topStatement == null) { return; } List<FunctionSignature> funIds = new ArrayList<FunctionSignature>(); for (FunctionDecl fdecl : declaredFunctions) { funIds.add(fdecl.getSignature()); } List<FunctionDecl> storedFunctionDecls = new ArrayList<>(); for (Expression topLevelExpr : topStatement.getDirectlyEnclosedExpressions()) { storedFunctionDecls.addAll(FunctionUtil.retrieveUsedStoredFunctions(metadataProvider, topLevelExpr, funIds, null, expr -> getFunctionCalls(expr), func -> functionParser.getFunctionDecl(func), (signature, sourceLoc) -> CommonFunctionMapUtil.normalizeBuiltinFunctionSignature(signature))); declaredFunctions.addAll(storedFunctionDecls); } if (!declaredFunctions.isEmpty()) { AQLInlineUdfsVisitor visitor = new AQLInlineUdfsVisitor(context, new AQLRewriterFactory(), declaredFunctions, metadataProvider); while (topStatement.accept(visitor, declaredFunctions)) { // loop until no more changes } } declaredFunctions.removeAll(storedFunctionDecls); }
@Override public Expression visit(FunctionDecl fd, ILangExpression arg) throws CompilationException { scopeChecker.createNewScope(); fd.setFuncBody(visit(fd.getFuncBody(), fd)); scopeChecker.removeCurrentScope(); return null; }