(***********************************************************************)
(*                                                                     *)
(*                                 GALAX                               *)
(*                              XQuery Engine                          *)
(*                                                                     *)
(*  Copyright 2001-2003, Lucent Technologies.                          *)
(*  Distributed only by permission.                                    *)
(*                                                                     *)
(***********************************************************************)

(* $Id: xquery_astutil.mli,v 1.2 2003/05/21 15:03:48 simeon Exp $ *)

(* Module Xquery_astutil
   Description:
     This module implements some useful operations the XQuery abstract
     syntax tree.
*)

open Xquery_ast

(* Commonly used variables in the typing & mapping: *)

val dot      : vname
val sequence : vname
val position : vname
val last     : vname

(**************************)
(* AST creation functions *)
(**************************)

(* New types AST creation *)

val mkdatatype : datatype_desc -> datatype
val fmkdatatype : datatype_desc -> Finfo.finfo -> datatype

val mkmodel : model_desc -> model
val mktyped_model : typed_model_desc -> typed_model
val mksort_decl : sort_decl_desc -> sort_decl

val fmkmodel : model_desc -> Finfo.finfo -> model
val fmktyped_model : typed_model_desc -> Finfo.finfo -> typed_model
val fmksort_decl : sort_decl_desc -> Finfo.finfo -> sort_decl

val mkanonymous_type : model -> typed_model

(* Common data structures AST creation *)

val mkpattern : pattern_desc -> pattern

val fmkpattern : pattern_desc -> Finfo.finfo -> pattern

(* XQuery AST creation *)

val mkexpr : expr_desc -> expr
val mkstep_qualifier : step_qualifier_desc -> step_qualifier
val mkfl_expr : fl_expr_desc -> fl_expr
val mkfunction_def : function_def_desc -> function_def
val mkvar_decl : var_decl_desc -> var_decl
val mkkey_def : key_def_desc -> key_def
val mkcontext_decl : context_decl_desc -> context_decl

val fmkexpr : expr_desc -> Finfo.finfo -> expr
val fmkstep_qualifier : step_qualifier_desc -> Finfo.finfo -> step_qualifier
val fmkfl_expr : fl_expr_desc -> Finfo.finfo -> fl_expr
val fmkfunction_def : function_def_desc -> Finfo.finfo -> function_def
val fmkvar_decl : var_decl_desc -> Finfo.finfo -> var_decl
val fmkkey_def : key_def_desc -> Finfo.finfo -> key_def
val fmkcontext_decl : context_decl_desc -> Finfo.finfo -> context_decl

(* Update AST creation *)

val mkupdate : update_desc -> update
val fmkupdate : update_desc -> Finfo.finfo -> update

val mksimple_update : simple_update_desc -> simple_update
val fmksimple_update : simple_update_desc -> Finfo.finfo -> simple_update

val mkcomplex_update : complex_update_desc -> complex_update
val fmkcomplex_update : complex_update_desc -> Finfo.finfo -> complex_update

(* Statement AST creation *)

val mkstatement : statement_desc -> statement
val fmkstatement : statement_desc -> Finfo.finfo -> statement

(* The empty query *)

val empty_query_module : query_module

(* Core AST creation *)

val mkcexpr : cexpr_desc -> cexpr
val mkcfunction_def : cfunction_def_desc -> cfunction_def
val mkcvar_decl : cvar_decl_desc -> cvar_decl
val mkckey_def : ckey_def_desc -> ckey_def

val fmkcexpr : cexpr_desc -> model_handle -> expr_handle -> Finfo.finfo -> cexpr
val fmkcfunction_def : cfunction_def_desc -> Finfo.finfo -> cfunction_def
val fmkcvar_decl : cvar_decl_desc -> Finfo.finfo -> cvar_decl
val fmkckey_def : ckey_def_desc -> Finfo.finfo -> ckey_def

(* Core update AST creation *)

val mkcupdate : cupdate_desc -> cupdate
val fmkcupdate : cupdate_desc -> update option -> Finfo.finfo -> cupdate

val mkcsimple_update : csimple_update_desc -> csimple_update
val fmkcsimple_update : csimple_update_desc -> Finfo.finfo -> csimple_update

val mkccomplex_update : ccomplex_update_desc -> ccomplex_update
val fmkccomplex_update : ccomplex_update_desc -> Finfo.finfo -> ccomplex_update

(* Core statement AST creation *)

val mkcstatement : cstatement_desc -> cstatement
val fmkcstatement : cstatement_desc -> Finfo.finfo -> cstatement

(* Core module AST creation *)

val fmkcquery_main_module_from_library_module :
    cquery_module -> cstatement list -> cquery_module

(**************************)
(* AST accessor functions *)
(**************************)

(* Processes NS attributes of an element *)

val get_ns_attributes : expr list -> ((Namespace.prefix * Namespace.uri) list * expr list)
val get_expression_from_statement : statement -> expr
val get_cexpression_from_cstatement : cstatement -> cexpr


(*******************************)
(* Returns the expression kind *)
(*******************************)

(* Note: This is used for monitoring
   - Jerome *)

type cexpr_kind =
  | CEKSort
  | CEKUnordered
  | CEKFor
  | CEKLet
  | CEKIf
  | CEKTypeswitch
  | CEKVar
  | CEKScalar
  | CEKDocument
  | CEKPI
  | CEKComment
  | CEKText
  | CEKTextComputed
  | CEKApp
  | CEKSeq
  | CEKEmpty
  | CEKElem
  | CEKAnyElem
  | CEKAttr
  | CEKAnyAttr
  | CEKError
  | CEKTreat
  | CEKValidate
  | CEKCast
  | CEKCastable
  | CEKAxis
  | CEKSubtype
  | CEKIntersects
  | CEKSome
  | CEKEvery
  | CEKKeyRef
  | CEKCreateTuple
  | CEKAccessTuple
  | CEKForTuple

val get_cexpr_kind : cexpr -> cexpr_kind


(************************)
(* Some rewriting rules *)
(************************)

val substitute_var : cexpr -> vname -> cexpr -> (cexpr * bool)

