<commit_before>package org.basex.query;

import static org.basex.query.QueryError.*;

import java.util.*;

import org.basex.query.func.*;
import org.basex.query.util.*;
import org.basex.query.value.item.*;
import org.basex.query.var.*;
import org.basex.util.*;
import org.basex.util.list.*;

/**
 * This class compiles all components of the query that are needed in an order that
 * maximizes the amount of inlining possible.
 *
 * @author BaseX Team 2005-16, BSD License
 * @author <NAME>
 */
final class QueryCompiler {
  /** Number of scopes from which on linear search is replaced by a hash map. */
  private static final int MAP_THRESHOLD = 16;

  /** Query context. */
  private final QueryContext qc;

  /** Result list. */
  private final ArrayList<Scope[]> result = new ArrayList<>();
  /** Node stack. */
  private final IntList stack = new IntList();
  /** Index and lowlink list. */
  private final IntList list = new IntList();
  /** Counter for the next free index. */
  private int next;

  /** Adjacency list. */
  private final ArrayList<int[]> adjacent = new ArrayList<>();
  /** Declaration list. */
  private final ArrayList<Scope> scopes = new ArrayList<>();
  /** Declaration list. */
  private IdentityHashMap<Scope, Integer> ids;

  /**
   * Constructor.
   * @param qc query context
   * @param root root expression
   */
  private QueryCompiler(final QueryContext qc, final Scope root) {
    this.qc = qc;
    add(root);
  }

  /**
   * Gathers all declarations (functions and static variables) used by the given main module.
   * @param main the main module to start from
   * @return list of all declarations that the main module uses
   */
  public static List<StaticDecl> usedDecls(final MainModule main) {
    final List<StaticDecl> scopes = new ArrayList<>();
    final IdentityHashMap<Scope, Object> map = new IdentityHashMap<>();
    main.visit(new ASTVisitor() {
      @Override
      public boolean staticVar(final StaticVar var) {
        if(map.put(var, var) == null) {
          var.visit(this);
          scopes.add(var);
        }
        return true;
      }

      @Override
      public boolean staticFuncCall(final StaticFuncCall call) {
        final StaticFunc f = call.func();
        if(map.put(f, f) == null) {
          f.visit(this);
          scopes.add(f);
        }
        return true;
      }

      @Override
      public boolean inlineFunc(final Scope sub) {
        if(map.put(sub, sub) == null) sub.visit(this);
        return true;
      }

      @Override
      public boolean funcItem(final FuncItem func) {
        if(map.put(func, func) == null) func.visit(this);
        return true;
      }
    });
    return scopes;
  }

  /**
   * Compiles all necessary parts of this query.
   * @param qc query context
   * @param root root expression
   * @throws QueryException compilation errors
   */
  public static void compile(final QueryContext qc, final MainModule root) throws QueryException {
    if(!root.compiled()) new QueryCompiler(qc, root).compile();
  }

  /**
   * Compiles all necessary parts of this query.
   * @throws QueryException compilation errors
   */
  private void compile() throws QueryException {
    // compile the used scopes only
    for(final Scope[] comp : components(0)) circCheck(comp).compile(qc);

    // check for circular variable declarations without compiling the unused scopes
    for(final StaticVar v : qc.vars) {
      if(id(v) == -1) for(final Scope[] comp : components(add(v))) circCheck(comp);
    }
  }

  /**
   * Checks if the given component contains a static variable that depends on itself.
   * @param comp component to check
   * @return scope to be compiled, the others are compiled recursively
   * @throws QueryException query exception
   */
  private static Scope circCheck(final Scope[] comp) throws QueryException {
    if(comp.length > 1) {
      for(final Scope scp : comp) {
        if(scp instanceof StaticVar) {
          final StaticVar var = (StaticVar) scp;
          //throw CIRCVAR_X.get(var.info, var.id());
        }
      }
    }
    return comp[0];
  }

  /**
   * Returns the strongly connected components of the dependency graph.
   * @param p ID of the starting point
   * @return the components
   * @throws QueryException if a variable directly calls itself
   */
  private Iterable<Scope[]> components(final int p) throws QueryException {
    result.clear();
    tarjan(p);
    return result;
  }
<commit_msg>[MIN] XQuery: raise error in circular error check (as before)<commit_after>package org.basex.query;

import static org.basex.query.QueryError.*;

import java.util.*;

import org.basex.query.func.*;
import org.basex.query.util.*;
import org.basex.query.value.item.*;
import org.basex.query.var.*;
import org.basex.util.*;
import org.basex.util.list.*;

/**
 * This class compiles all components of the query that are needed in an order that
 * maximizes the amount of inlining possible.
 *
 * @author BaseX Team 2005-16, BSD License
 * @author <NAME>
 */
final class QueryCompiler {
  /** Number of scopes from which on linear search is replaced by a hash map. */
  private static final int MAP_THRESHOLD = 16;

  /** Query context. */
  private final QueryContext qc;

  /** Result list. */
  private final ArrayList<Scope[]> result = new ArrayList<>();
  /** Node stack. */
  private final IntList stack = new IntList();
  /** Index and lowlink list. */
  private final IntList list = new IntList();
  /** Counter for the next free index. */
  private int next;

  /** Adjacency list. */
  private final ArrayList<int[]> adjacent = new ArrayList<>();
  /** Declaration list. */
  private final ArrayList<Scope> scopes = new ArrayList<>();
  /** Declaration list. */
  private IdentityHashMap<Scope, Integer> ids;

  /**
   * Constructor.
   * @param qc query context
   * @param root root expression
   */
  private QueryCompiler(final QueryContext qc, final Scope root) {
    this.qc = qc;
    add(root);
  }

  /**
   * Gathers all declarations (functions and static variables) used by the given main module.
   * @param main the main module to start from
   * @return list of all declarations that the main module uses
   */
  public static List<StaticDecl> usedDecls(final MainModule main) {
    final List<StaticDecl> scopes = new ArrayList<>();
    final IdentityHashMap<Scope, Object> map = new IdentityHashMap<>();
    main.visit(new ASTVisitor() {
      @Override
      public boolean staticVar(final StaticVar var) {
        if(map.put(var, var) == null) {
          var.visit(this);
          scopes.add(var);
        }
        return true;
      }

      @Override
      public boolean staticFuncCall(final StaticFuncCall call) {
        final StaticFunc f = call.func();
        if(map.put(f, f) == null) {
          f.visit(this);
          scopes.add(f);
        }
        return true;
      }

      @Override
      public boolean inlineFunc(final Scope sub) {
        if(map.put(sub, sub) == null) sub.visit(this);
        return true;
      }

      @Override
      public boolean funcItem(final FuncItem func) {
        if(map.put(func, func) == null) func.visit(this);
        return true;
      }
    });
    return scopes;
  }

  /**
   * Compiles all necessary parts of this query.
   * @param qc query context
   * @param root root expression
   * @throws QueryException compilation errors
   */
  public static void compile(final QueryContext qc, final MainModule root) throws QueryException {
    if(!root.compiled()) new QueryCompiler(qc, root).compile();
  }

  /**
   * Compiles all necessary parts of this query.
   * @throws QueryException compilation errors
   */
  private void compile() throws QueryException {
    // compile the used scopes only
    for(final Scope[] comp : components(0)) circCheck(comp).compile(qc);

    // check for circular variable declarations without compiling the unused scopes
    for(final StaticVar v : qc.vars) {
      if(id(v) == -1) for(final Scope[] comp : components(add(v))) circCheck(comp);
    }
  }

  /**
   * Checks if the given component contains a static variable that depends on itself.
   * @param comp component to check
   * @return scope to be compiled, the others are compiled recursively
   * @throws QueryException query exception
   */
  private static Scope circCheck(final Scope[] comp) throws QueryException {
    if(comp.length > 1) {
      for(final Scope scp : comp) {
        if(scp instanceof StaticVar) {
          final StaticVar var = (StaticVar) scp;
          throw CIRCVAR_X.get(var.info, var.id());
        }
      }
    }
    return comp[0];
  }

  /**
   * Returns the strongly connected components of the dependency graph.
   * @param p ID of the starting point
   * @return the components
   * @throws QueryException if a variable directly calls itself
   */
  private Iterable<Scope[]> components(final int p) throws QueryException {
    result.clear();
    tarjan(p);
    return result;
  }

{'fraction_non_alphanumeric': 0.11025469168900805, 'fraction_numerical': 0.002680965147453083, 'mean_word_length': 2.8293413173652695, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3630657', 'n_tokens_mistral': 2609, 'n_tokens_neox': 2528, 'n_words': 883}