<commit_before>#include "compiler/normalizer/normalizer.h"
#include "context/static_context.h"
#include "compiler/expression/expr.h"
#include "functions/signature.h"
#include "types/root_typemanager.h"
#include "system/globalenv.h"
#include <iostream>

using namespace zorba;

#define LOOKUP_FN( pfx, local, arity ) static_cast<function *> (sctx->lookup_fn (pfx, local, arity))

static inline expr::expr_t wrap_in_bev(static_context *sctx, expr::expr_t e)
{
  expr::expr_t fh(new fo_expr(e->get_loc(), LOOKUP_FN("fn", "boolean", 1)));
  fo_expr *fp = static_cast<fo_expr *>(fh.getp());
  fp->add(e);
  return fh;
}

static inline expr::expr_t wrap_in_atomization(static_context *sctx, expr::expr_t e)
{
  expr::expr_t fh(new fo_expr(e->get_loc(), LOOKUP_FN("fn", "data", 1)));
  fo_expr *fp = static_cast<fo_expr *>(fh.getp());
  fp->add(e);
  return fh;
}

static inline expr::expr_t wrap_in_typematch(expr::expr_t e, xqtref_t /*type*/)
{
  // TODO : Need to add typematch_expr
  return e;
}

static inline expr::expr_t wrap_in_type_conversion(expr::expr_t e, xqtref_t type)
{
  expr::expr_t ph = new promote_expr(e->get_loc(), e, type);
  // TODO : Need to add convert_simple_operand
  return ph;
}

bool normalizer::begin_visit(expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(var_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(order_modifier& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(flwor_expr& node)
{
  QueryLoc loc = node.get_loc ();
  expr::expr_t where_h = node.get_where();
  if (where_h.getp()) {
    node.set_where(wrap_in_bev(m_sctx, where_h));
  }
  uint32_t on = node.orderspec_count();
  for(uint32_t i = 0; i < on; ++i) {
    flwor_expr::orderspec_t& os = node.orderspec_at(i);
    os.first = wrap_in_atomization(m_sctx, os.first);
  }
  for (uint32_t i = 0; i < node.forlet_count (); i++) {
    flwor_expr::forletref_t clause = node [i];
    xqtref_t vartype = clause->get_var ()->get_type ();
    if (vartype != NULL) {
      bool is_for = clause->get_type () == forlet_clause::for_clause;
      if (is_for && GENV_TYPESYSTEM.is_equal (*GENV_TYPESYSTEM.EMPTY_TYPE, *vartype))
        ZORBA_ERROR_LOC (ZorbaError::XPTY0004, loc);
      xqtref_t promote_type = is_for ? GENV_TYPESYSTEM.create_type (*vartype, TypeConstants::QUANT_STAR) : vartype;
      expr_t e = clause->get_expr ();
      clause->set_expr (new treat_expr (e->get_loc (), e, promote_type, ZorbaError::XPTY0004));
    }
  }
  return true;
}

bool normalizer::begin_visit(case_clause& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(promote_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(trycatch_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(typeswitch_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(if_expr& node)
{
  node.set_cond_expr(wrap_in_bev(m_sctx, node.get_cond_expr()));
  return true;
}

bool normalizer::begin_visit(fo_expr& node)
{
  const signature& sign = node.get_signature();

  int n = node.size();
  for(int i = 0; i < n; ++i) {
    expr::expr_t arg = node[i];
    const xqtref_t& arg_type = sign[i];
    xqtref_t arg_prime_type = GENV_TYPESYSTEM.prime_type(*arg_type);
    if (GENV_TYPESYSTEM.is_subtype(*arg_prime_type, *GENV_TYPESYSTEM.ANY_ATOMIC_TYPE_ONE)) {
      arg = wrap_in_atomization(m_sctx, arg);
      arg = wrap_in_type_conversion(arg, arg_type);
    }

    if (GENV_TYPESYSTEM.quantifier(*arg_type) != TypeConstants::QUANT_STAR) {
      arg = wrap_in_typematch(arg, arg_type);
    }

    node[i] = arg;
  }

  return true;
}

bool normalizer::begin_visit(ft_select_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(ft_contains_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(instanceof_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(treat_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(castable_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(cast_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(validate_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(extension_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(relpath_expr& node)
{
  return true;
}<commit_msg>Fixed #215<commit_after>#include "compiler/normalizer/normalizer.h"
#include "context/static_context.h"
#include "compiler/expression/expr.h"
#include "functions/signature.h"
#include "types/root_typemanager.h"
#include "system/globalenv.h"
#include <iostream>

using namespace zorba;

#define LOOKUP_FN( pfx, local, arity ) static_cast<function *> (sctx->lookup_fn (pfx, local, arity))

static inline expr::expr_t wrap_in_bev(static_context *sctx, expr::expr_t e)
{
  expr::expr_t fh(new fo_expr(e->get_loc(), LOOKUP_FN("fn", "boolean", 1)));
  fo_expr *fp = static_cast<fo_expr *>(fh.getp());
  fp->add(e);
  return fh;
}

static inline expr::expr_t wrap_in_atomization(static_context *sctx, expr::expr_t e)
{
  expr::expr_t fh(new fo_expr(e->get_loc(), LOOKUP_FN("fn", "data", 1)));
  fo_expr *fp = static_cast<fo_expr *>(fh.getp());
  fp->add(e);
  return fh;
}

static inline expr::expr_t wrap_in_typematch(expr::expr_t e, xqtref_t /*type*/)
{
  // TODO : Need to add typematch_expr
  return e;
}

static inline expr::expr_t wrap_in_type_conversion(expr::expr_t e, xqtref_t type)
{
  expr::expr_t ph = new promote_expr(e->get_loc(), e, type);
  // TODO : Need to add convert_simple_operand
  return ph;
}

bool normalizer::begin_visit(expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(var_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(order_modifier& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(flwor_expr& node)
{
  QueryLoc loc = node.get_loc ();
  expr::expr_t where_h = node.get_where();
  if (where_h.getp()) {
    node.set_where(wrap_in_bev(m_sctx, where_h));
  }
  uint32_t on = node.orderspec_count();
  for(uint32_t i = 0; i < on; ++i) {
    flwor_expr::orderspec_t& os = node.orderspec_at(i);
    os.first = wrap_in_atomization(m_sctx, os.first);
  }
  for (uint32_t i = 0; i < node.forlet_count (); i++) {
    flwor_expr::forletref_t clause = node [i];
    xqtref_t vartype = clause->get_var ()->get_type ();
    if (vartype != NULL) {
      bool is_for = clause->get_type () == forlet_clause::for_clause;
      if (is_for && GENV_TYPESYSTEM.is_equal (*GENV_TYPESYSTEM.EMPTY_TYPE, *vartype))
        ZORBA_ERROR_LOC (ZorbaError::XPTY0004, loc);
      xqtref_t promote_type = is_for ? GENV_TYPESYSTEM.create_type (*vartype, TypeConstants::QUANT_STAR) : vartype;
      expr_t e = clause->get_expr ();
      clause->set_expr (new treat_expr (e->get_loc (), e, promote_type, ZorbaError::XPTY0004));
    }
  }
  return true;
}

bool normalizer::begin_visit(case_clause& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(promote_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(trycatch_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(typeswitch_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(if_expr& node)
{
  node.set_cond_expr(wrap_in_bev(m_sctx, node.get_cond_expr()));
  return true;
}

bool normalizer::begin_visit(fo_expr& node)
{
  const signature& sign = node.get_signature();

  int n = node.size();
  for(int i = 0; i < n; ++i) {
    expr::expr_t arg = node[i];
    const xqtref_t& arg_type = sign[i];
    xqtref_t arg_prime_type = GENV_TYPESYSTEM.prime_type(*arg_type);
    if (GENV_TYPESYSTEM.is_subtype(*arg_prime_type, *GENV_TYPESYSTEM.ANY_ATOMIC_TYPE_ONE)) {
      arg = wrap_in_atomization(m_sctx, arg);
      arg = wrap_in_type_conversion(arg, arg_type);
    }

    if (GENV_TYPESYSTEM.quantifier(*arg_type) != TypeConstants::QUANT_STAR) {
      arg = wrap_in_typematch(arg, arg_type);
    }

    node[i] = arg;
  }

  return true;
}

bool normalizer::begin_visit(ft_select_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(ft_contains_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(instanceof_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(treat_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(castable_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(cast_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(validate_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(extension_expr& /*node*/)
{
  return true;
}

bool normalizer::begin_visit(relpath_expr& node)
{
  if (node.size() > 0) {
    expr_t ie = node[0];
    expr_t pe = new treat_expr(ie->get_loc(), ie, GENV_TYPESYSTEM.ANY_NODE_TYPE_STAR, ZorbaError::XPTY0019);
    node[0] = pe;
  }
  return true;
}
{'fraction_non_alphanumeric': 0.15252081406105458, 'fraction_numerical': 0.005550416281221091, 'mean_word_length': 4.712681638044915, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 37, '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': '3504727', 'n_tokens_mistral': 3455, 'n_tokens_neox': 3443, 'n_words': 636}