Skip to content
Fetching contributors…
Cannot retrieve contributors at this time
205 lines (186 sloc) 7.15 KB
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 <>.
(* 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
) env acc
let map_type f env =
(fun info ->
let specialize =
(fun (ty,e) ->
(f ty, e))
info with
specialize ;
) env
module S =
type t = Q.annotmap * env
let pass = "pass_SimplifyMagic"
let pp f _ = Format.pp_print_string f "<dummy>"
module R =
include ObjectFiles.Make(S)
let load annotmap (env:env) : S.t =
(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)
let is_monomorphic ty =
not (
| 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 =
(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 = (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 ;
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
| 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;
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
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 () =
let context = QmlError.Context.label label in
QmlError.error context (
"Failed specialization on %s with type %a@\n"^^
"Add a type annotation for a specialization in one of the following types:@\n"^^
(Ident.original_name i) QmlPrint.pp#ty ty
(Format.pp_list "@\n" (Format.pp_fst QmlPrint.pp#ty)) choices
if info.strict
fail ()
else (
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 annotmap env;
annotmap, code
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
Something went wrong with that request. Please try again.