Skip to content
This repository
tag: v949
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 205 lines (186 sloc) 7.325 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
(*
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/>.
*)
(* depends *)
module Format = BaseFormat
module List = BaseList

(* shorthands *)
module Q = QmlAst

(* -- *)

type info = { strict : bool ; specialize : (Q.ty * Q.expr) list }
type env = info IdentMap.t


let (@) info1 info2 =
  let strict = info1.strict || info2.strict in
  let specialize = info1.specialize @ info2.specialize in
  {
    strict ;
    specialize ;
  }

let fold_expr f acc env =
  IdentMap.fold (fun _ info acc ->
    List.fold_left (fun acc (_,e) -> f acc e) acc info.specialize
  ) env acc

let fold_map_expr f acc env =
  IdentMap.fold_map (
    fun _ info acc ->
      let acc, specialize =
        List.fold_left_map (
          fun acc (ty,e) ->
            let acc, e = f acc e in
            acc, (ty, e)
        ) acc info.specialize
      in acc, {
        info with
          specialize
      }
  ) env acc

let map_type f env =
  IdentMap.map
    (fun info ->
       let specialize =
         List.map
           (fun (ty,e) ->
              (f ty, e))
           info.specialize
       in
       {
         info with
           specialize ;
       }
    ) env

module S =
struct
  type t = Q.annotmap * env
  let pass = "pass_SimplifyMagic"
  let pp f _ = Format.pp_print_string f "<dummy>"
end

module R =
struct
  include ObjectFiles.Make(S)
  let load annotmap (env:env) : S.t =
    fold_with_name
      (fun package (annotmap,(env:env)) (annotmap_old,old_env) ->
         let annotmap_old = QmlRefresh.refresh_annotmap package annotmap_old in
         let annotmap, old_env = fold_map_expr (QmlRefresh.refresh_expr package ~annotmap_old) annotmap old_env in
         let old_env = map_type (QmlRefresh.refresh_typevars_from_ty package) old_env in
         let env = IdentMap.merge (@) env old_env in
         annotmap, env
      ) (annotmap,env)
  let save annotmap env =
    let small_annotmap = QmlRefresh.restrict_annotmap_fold_expr fold_expr annotmap env in
    save (small_annotmap,env)
end

let is_monomorphic ty =
  not (
    QmlAstWalk.Type.exists
      (function
       | Q.TypeVar _ -> true (* FIXME: actually, we should check for rowvars and colvars also *)
       | _ -> false) ty
  )

let build_env env gamma annotmap code =
  let _, code as result =
    QmlAstWalk.CodeExpr.fold_map_name_expr
      (fun (env,annotmap) (ident,expr) ->
         match expr with
         | Q.Directive (_, `specialize variant, inner_expr :: l, _) ->
             (* we refuse polymorphic types for now
* or else we might create troubles with ei *)
             let general_type = QmlAnnotMap.find_ty (Q.QAnnot.expr inner_expr) annotmap in
             let l =
               let specialize = List.map (fun e ->
                                 let ty = QmlAnnotMap.find_ty (Q.QAnnot.expr e) annotmap in
                                 if not (is_monomorphic ty) then
                                   OManager.serror "%a@\n This expression shouldn't contain type variables@."
                                     FilePos.pp_pos (Q.Pos.expr e)
                                 else (
                                   (* should check that ty is an instance of general_type
* but since ty is monomorphic, checking the unifiability is equivalent *)
                                   if not (QmlMoreTypes.unifiable ~gamma general_type ty) then
                                     OManager.serror "%a@\n This expression's type should be an instance of the generic expression's type.@."
                                       FilePos.pp_pos (Q.Pos.expr e)
                                 );
                                 ty, e) l in
               let strict = variant = `strict in
               {
                 strict ;
                 specialize ;
               }
             in
             let env = IdentMap.update_default ident ((@) l) l env in
             let tsc = QmlAnnotMap.find_tsc_opt (Q.QAnnot.expr expr) annotmap in
             let annotmap = QmlAnnotMap.add_tsc_opt (Q.QAnnot.expr inner_expr) tsc annotmap in
             (env,annotmap), (ident, inner_expr)
         | _ ->
             (env,annotmap), (ident, expr))
      (env,annotmap) code in
  QmlAstWalk.CodeExpr.iter
    (QmlAstWalk.Expr.iter
       (function
        | Q.Directive (label, `specialize _, _, _) ->
          OManager.serror "%a@\n Illegal @@specialize: it can only be the topmost directive on a toplevel binding.@."
            FilePos.pp_pos (Annot.pos label)
        | _ -> ()
       )
    ) code;
  result

let rewrite_expr env gamma annotmap code =
  let rec aux tra annotmap e =
    match e with
    | Q.Ident (label, i) ->
        let annot = Annot.annot label in
        (try
           let info = IdentMap.find i env in
           let choices = info.specialize in
           let ty = QmlAnnotMap.find_ty annot annotmap in
           try let _, expr = List.find (fun (ty',_) -> QmlMoreTypes.equal_ty ~gamma ty ty') choices in
               let annotmap, expr = QmlAstCons.TypedExpr.copy annotmap expr in
               aux tra annotmap expr
           with Not_found ->
             let fail () =
               QmlPrint.pp#reset_typevars;
               let context = QmlError.Context.label label in
               QmlError.error context (
                 "Failed specialization on %s with type %a@\n"^^
                 "@[<2>@{<bright>Hint@}:@\n"^^
                  "Add a type annotation for a specialization in one of the following types:@\n"^^
                  "%a"^^
                 "@]"
               )
                 (Ident.original_name i) QmlPrint.pp#ty ty
                 (Format.pp_list "@\n" (Format.pp_fst QmlPrint.pp#ty)) choices
             in
             if info.strict
             then
               fail ()
             else (
               #<If:SIMPLIFYMAGIC_FAILURES>
                 fail ()
               #<End> ;
             ) ;
             tra annotmap e
         with Not_found -> tra annotmap e)
     | _ -> tra annotmap e in
  QmlAstWalk.Expr.traverse_foldmap aux annotmap code

let empty_env = IdentMap.empty
let process_code ?(specialized_env=empty_env) gamma annotmap code =
  let (env,annotmap), code = build_env specialized_env gamma annotmap code in
  R.save annotmap env;
  #<If:SIMPLIFYMAGIC_DISABLE>
    annotmap, code
  #<Else>
    let annotmap, env2 = R.load annotmap empty_env in
    let env = IdentMap.merge (@) env2 env in (* the old env has priority over the current one
* so that Date.to_string doesn't override intToString
* in the package date *)
    QmlAstWalk.CodeExpr.fold_map (rewrite_expr env gamma) annotmap code
  #<End>
Something went wrong with that request. Please try again.