Skip to content
This repository
tag: v1502
Fetching contributors…

Cannot retrieve contributors at this time

file 241 lines (209 sloc) 8.119 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(* THIS FILE HAS A DOCUMENTED MLI *)

(* depends *)
module Format = Base.Format

(* refactoring in progress *)

(* shorthands *)
module Q = QmlAst

let contains_tracker e =
  QmlAstWalk.Expr.exists
    (function
     | Q.Directive (_, `tracker _, _, _) -> true
     | _ -> false) e

module Printer =
struct
  let code fmt code =
    Format.fprintf fmt "/* printer: --print code */@\n@\n" ;
    QmlPrint.pp#code fmt code ;
    Format.fprintf fmt "@."

  let light_ident fmt code =
    Format.fprintf fmt "/* printer: --print light_ident */@\n@\n" ;
    QmlPrint.pp_light_ident#code fmt code ;
    Format.fprintf fmt "@."

  let very_light_ident fmt code =
    Format.fprintf fmt "/* printer: --print very_light_ident */@\n@\n" ;
    QmlPrint.pp_very_light_ident#code fmt code ;
    Format.fprintf fmt "@."

  let code_with_type fmt annotmap code =
    Format.fprintf fmt "/* printer: --print code_with_type */@\n@\n" ;
    (new QmlPrint.printer_with_type annotmap)#code fmt code ;
    Format.fprintf fmt "@."

  let code_for_ei fmt annotmap code =
    Format.fprintf fmt "/* printer: --print code_for_ei */@\n@\n" ;
    (new QmlPrint.printer_for_ei annotmap)#code fmt code ;
    Format.fprintf fmt "@."

  (* <!> beware opatrack uses this formating.
Do not change it or please update opatrack *)
  let size fmt code =
    let i =
      QmlAstWalk.CodeExpr.fold
        (QmlAstWalk.Expr.fold (fun acc _ -> acc + 1)) 0 code in
    Format.fprintf fmt
      "%d declarations@\n%d nodes@." (List.length code) i

  let declaration fmt code =
    Format.fprintf fmt "/* printer: --print declaration */@\n@\n" ;
    QmlPrint.pp_declaration#code fmt code ;
    Format.fprintf fmt "@."

  let annotation fmt code =
    Format.fprintf fmt "/* printer: --print annotation */@\n@\n";
    QmlPrint.pp_annotation#code fmt code;
    Format.fprintf fmt "@."

  (* ************************************************************************ *)
  (** {b Descr}: Printer for position in source code.
{b Visibility} : Not exported outside this module. *)
  (* ************************************************************************ *)
  let position fmt code =
    Format.fprintf fmt "/* printer: --print position */@\n@\n";
    QmlPrint.pp_position#code fmt code;
    Format.fprintf fmt "@."

  let tracked fmt code =
    Format.fprintf fmt "/* printer: --print tracked */" ;
    let bind kw rec_ (s, e) =
      let val_ = if !kw then (kw := false; "val"^rec_) else "and" in
      Format.fprintf fmt "@\n@\n@\n@\n@\n/* %s %s */@\n@\n%s %s =@ %a@\n"
        val_ (Ident.stident s) val_ (Ident.stident s) QmlPrint.pp#expr e
    in
    let is_tracked = List.exists (fun (_, e) -> contains_tracker e) in
    List.iter
      (function
         | Q.NewVal (_, b) ->
             if is_tracked b then List.iter (bind (ref true) "") b
         | Q.NewValRec (_, b) ->
             if is_tracked b then List.iter (bind (ref true) " rec") b
         | _ -> ()
      ) code;
    Format.fprintf fmt "@."

  let gamma fmt gamma =
    Format.fprintf fmt "/* printer: --print gamma*/@.";
    QmlTypes.Env.pp fmt gamma;
    Format.fprintf fmt "@."
end

let define = PassHandler.define_printer
let code_id = define "code"
let light_ident_id = define "light_ident"
let very_light_ident_id = define "very_light_ident"
let with_type_id = define "with_type"
let for_ei_id = define "for_ei"
let size_id = define "size"
let declaration_id = define "declaration"
let annotation_id = define "annotation"
let position_id = define "position"
let tracked_id = define "tracked"
let gamma_id = define "gamma"
let stdlib_gamma_id = define "stdlib_gamma"

let printers extract _ =
  let make_code fct fmt env =
    let _, _, code = extract env in
    fct fmt code in
  let make_ac fct fmt env =
    let annotmap, _, code = extract env in
    fct fmt annotmap code in
  let make_gamma fct fmt env =
    let _, (gamma, _), _ = extract env in
    fct fmt gamma in
  let make_stdlib_gamma fct fmt env =
    let _, (_, stdlib_gamma), _ = extract env in
    fct fmt stdlib_gamma in
  [
    code_id, make_code Printer.code ;
    light_ident_id, make_code Printer.light_ident ;
    very_light_ident_id, make_code Printer.very_light_ident ;
    declaration_id, make_code Printer.declaration ;
    annotation_id, make_code Printer.annotation;
    (* Source code positions printer registered. *)
    position_id, make_code Printer.position;
    size_id, make_code Printer.size ;
    with_type_id, make_ac Printer.code_with_type;
    for_ei_id, make_ac Printer.code_for_ei;
    gamma_id, make_gamma Printer.gamma;
    stdlib_gamma_id, make_stdlib_gamma Printer.gamma;
    (* waiting for flexibility in passhander options *)
    (* tracked_id, make Printer.tracked ; *)
  ]

module Tracker =
struct
  let pp_print_expr = QmlPrint.pp#expr
  let pp_print_code_elt = QmlPrint.pp#code_elt

  let directive iter =
    QmlAstWalk.CodeExpr.iter
      (QmlAstWalk.Expr.iter
         (function
          | Q.Directive (_, `tracker t, [e], _) -> iter.PassTracker.track (PassTracker.filename t) pp_print_expr e
          | _ -> ()))

  (* We keep the full code_elt for each val
it is a duplication, but it speed up searching
anyway, the folder _tracks can be cleaned *)
  let val_ iter = List.iter
    (function
       | Q.NewVal (_, binds)
       | ( Q.NewValRec (_, binds) ) as elt -> List.iter
           (fun (s, _) ->
              let filename = Ident.stident s in
              iter.PassTracker.track filename pp_print_code_elt elt
           ) binds
       | _ -> ())
end

let define = PassHandler.define_tracker
let directive_id = define "track"
let val_id = define "val"

let trackers extract _ =
  let make fct fmt env = fct fmt (extract env) in
  [
    directive_id, make Tracker.directive ;
    val_id, make Tracker.val_ ;
  ]

(* iterator on `track directive with something else that expr *)
module WIP =
struct
  (* other iterators : wip *)
  type ('tracked, 'env) iter_tracker =
      ( 'env -> QmlAst.code ) ->
        (PassTracker.t -> 'tracked PassHandler.printer -> 'tracked -> unit) -> 'env -> unit

  let pp_print_expr = QmlPrint.pp#expr
  let pp_print_annot fmt annot =
    Format.pp_print_string fmt (Annot.to_string annot)
  let pp_print_ty = QmlPrint.pp#ty

  let iter_tracker extract iter env =
    QmlAstWalk.CodeExpr.iter
      (QmlAstWalk.Expr.iter
         (function
          | Q.Directive (_, `tracker t, [e], _) -> iter.PassTracker.track (PassTracker.filename t) pp_print_expr e
          | _ -> ()))
      (extract env)

  let iter_annot_tracker extract iter env =
    QmlAstWalk.CodeExpr.iter
      (QmlAstWalk.Expr.iter
         (function
          | Q.Directive (_, `tracker t, [e], _) -> iter t pp_print_annot (Q.QAnnot.expr e)
          | _ -> () ))
      (extract env)

  let iter_ty_tracker extract_annotmap extract_code iter env =
    QmlAstWalk.CodeExpr.iter
      (QmlAstWalk.Expr.iter
         (function
          | Q.Directive (_, `tracker t, [e], _) -> (
              let annot = Q.QAnnot.expr e in
              match QmlAnnotMap.find_ty_opt annot (extract_annotmap env) with
              | Some ty -> iter t pp_print_ty ty
              | None -> iter t
                  (fun fmt _ ->
                     Format.fprintf fmt "Annot Not Found: %a. The expr is:@\n%a"
                       pp_print_annot annot pp_print_expr e)
                    (QmlAst.TypeConst QmlAst.TyNull)
            )
          | _ -> () )
      )
      (extract_code env)

end
Something went wrong with that request. Please try again.