module php/reflect/version4/main
imports
  php/reflect/version4/php4-environment
  php/reflect/version4/function
  php/reflect/version4/class

strategies
  /**
   * Creates a new php-environment and stores it
   * It is assumed that the given document is the main
   * AST.
   *
   * @type Document-AST -> PHPEnvironment
   */
  init-php4-environment(|doc) =
      where(env := <new-php4-environment>)
    ; fill-php4-environment(|env,doc)
    ; <set-main-ast(|doc)> env
    ; set-php4-environment(|env)
    ; !env

  /**
   * Fills a given PHP4 environment with the information of
   * the given AST. It stores this environment and returns the
   * new environment.
   *
   * @param env The environment to be filled
   * @param doc The documents AST to be analyzed
   */
  fill-php4-environment(|env,doc) =
      <collect-php4-functions(|env)> doc
    ; <collect-php4-classes(|env)>   doc
    ; set-php4-environment(|env)
    ; !env

  /**
   * Collects the function declerations from the current AST.
   * This does not include the functions declared within classes.
   *
   * @param PHPEnvironment
   */
  collect-php4-functions(|env) =
     collect-all-skip-classes(match-php4-function-decl)
    ; map({ast, fun:
        ?ast
        ; fun := <new-php4-function(|ast)>
        ; <add-function(|fun)> env
      })

  match-php4-function-decl =
       match-function-decl
    <+ ?OldFunctionDecl(_,_,_)
    <+ ?OldFunctionDeclRef(_,_,_)
    
  /**
   * Collects the class declerations of the current AST.
   *
   * @param PHPEnvironment
   */
  collect-php4-classes(|env) =
      collect-all(match-php4-class-decl)
    ; map({ast, class:
        ?ast
        ; class := <new-php4-class(|ast)>
        ; <add-class(|class)> env
      })

  match-php4-class-decl = 
    match-class-decl

strategies
  /**
   * Returns the current environment if it is a PHP4
   * environment.
   *
   * @type _ -> PHP4Environment
   */
  get-php4-environment =
      get-php-environment
     ;instanceof-PHP4Environment

  /**
   * Sets the current environment to a PHP4
   * environment.
   *
   * @param env PHP4Environment
   */
  set-php4-environment(|env) =
     <instanceof-PHP4Environment> env
    ; set-php-environment(|env)