Skip to content
Newer
Older
100644 234 lines (206 sloc) 8.71 KB
fccc685 Initial open-source release
MLstate authored
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18
19 (* @author Esther Baruk
20 Please see the .mli for more informations about this module
21
22 Beware that it is necessary to do rewriting and analysis at the same time
23 or else you will miss cases where you should have rewritten:
24 m1 = {{ m2 = {{ x = ... }} }}
25 m3 = m1.m2 // if haven't rewritten m1.m2 to an identifier, then you don't know that is an
26 // alias on a module
27 _ = m3.x
28 *)
29
30 module Q = QmlAst
31 module List = Base.List
32
33 type env = {
34 modules : Q.expr StringMap.t IdentMap.t; (* maps module identifiers to maps of their field *)
35 aliases : Ident.t IdentMap.t; (* maps identifiers to an identifier of module, shorcuting any aliases:
36 * m = {{ ... }}
37 * m1 = m
38 * m2 = m1
39 * generates the map:
40 * m1 -> m
41 * m2 -> m
42 *)
43 }
44
45 type ignored_directives = [ Q.type_directive | Q.slicer_directive ]
46
47 let fold_expr f acc env =
48 IdentMap.fold (fun _ fields acc -> StringMap.fold (fun _ e acc -> f acc e) fields acc) env.modules acc
49
50 let empty_env = {
51 modules = IdentMap.empty;
52 aliases = IdentMap.empty;
53 }
54
55 let add_to_aliases id m env = { env with aliases = IdentMap.add id m env.aliases }
56
57 let find_module ident env =
58 if IdentMap.mem ident env.modules then
59 ident
60 else
61 IdentMap.find ident env.aliases
62 let find_module_field ident field env =
63 let module_ = find_module ident env in
64 try StringMap.find field (IdentMap.find module_ env.modules)
65 with Not_found -> assert false
66
67 (* If an identifier is present in the given expression, it returns the expr containing it
68 -- It first returned an ident but we had to preserve the annotations for a later use
69 so it returns the corresponding expr --
70 else it returns None *)
71 let get_id_in_expr e =
72 let rec aux acc e = QmlAstWalk.Expr.traverse_fold
73 (fun tra id e ->
74 match e with
75 | Q.Ident _ ->
76 Some e
77 | Q.Apply (_, expr, _)
78 | Q.Lambda (_, _, expr) ->
79 (match aux acc expr with
80 | Some _ as acc_ -> acc_
81 | None -> None)
82 | _ -> tra id e)
83 acc
84 e
85 in aux None e
86
87 (* Stores in the environment the module fields and the corresponding toplevel aliases *)
88 let collect_declarations module_name env expr =
89 match expr with
90 | Q.Record (_, field_list) ->
91 let field_list =
92 List.map
93 (fun (field,expr) ->
94 let id =
95 let id_ = get_id_in_expr expr in (* get the toplevel alias of the field *)
96 (match id_ with Some e -> e | _ -> assert false) in
97 (field, id)
98 ) field_list in
99 let stringmap = StringMap.from_list field_list in
100 {env with modules = IdentMap.add module_name stringmap env.modules}
101 | _ -> assert false
102
103 (* This function is called on an expression from a NewVal
104 If the given expression contains a module, then it collects its identifiers and add
105 them to the environment (see the collect_declarations function)
106 Else it doesn't have any effects on the environment *)
107 let rec inspect_module env (name,e) =
108 match e with
109 | Q.Directive (_, `module_, [expr], _) ->
110 collect_declarations name env expr
111 | Q.Coerce (_, e, _)
112 | Q.Directive (_, `deprecated, [ _ ; e ], _)
113 | Q.Directive (_, #ignored_directives, [e], _) ->
114 inspect_module env (name, e)
115 | Q.Directive (_, `deprecated, _, _)
116 | Q.Directive (_, #ignored_directives, _, _) ->
117 assert false
118 | Q.Ident (_, id) -> (
119 try
120 let module_ = find_module id env in
121 {env with aliases = IdentMap.add name module_ env.aliases}
122 with Not_found ->
123 env
124 )
125 | _ -> env
126
127 let analyze_code_elt env = function
128 | QmlAst.NewVal (_, decl_list) ->
129 List.fold_left inspect_module env decl_list
130 | _ ->
131 (* cannot have module, nor aliases in recursive bindings *)
132 env
133
134
135 (* Useful to take the ident from an expr *)
136 let extract_ident e = match e with
137 | Q.Ident (_, id) -> id
138 | _ -> assert false
139
140 (* The main function that does the substitutions
141 If we have a dot (i.e. module.field) it search its toplevel alias in the ident_map.
142 If it exists then it's replaced by it, else the expression remains the same *)
143 let rewrite_code_elt env annotmap code_elt =
144 let annotmap, code =
145 QmlAstWalk.CodeExpr.fold_map
146 (QmlAstWalk.Expr.foldmap_up
147 (* need to go up because to rewrite A.B.x, you first rewrite A.B into B1 and B1.x in x1 *)
148 (fun annotmap e ->
149 match e with
150 | Q.Dot (label, expr, s) -> (
151 match get_id_in_expr expr with
152 | Some id ->
153 let module_name = extract_ident id in (
154 try
155 (* get the alias corresponding to this call (it's an expr) *)
156 let toplevel_alias = find_module_field module_name s env in
157 let id_ = extract_ident toplevel_alias in
158 (* the tsc_inst needed at the call site need to be the same as the one in the
159 * field and not the one that was originally there because the toplevel definition
160 * can expect typevars while the field definition doesn't (even if this is just
161 * an alias, because of phantom types) *)
162 let tsc_inst_opt =
163 QmlAnnotMap.find_tsc_inst_opt (Q.QAnnot.expr toplevel_alias) annotmap in
164 let annotmap =
165 QmlAnnotMap.add_tsc_inst_opt (Q.QAnnot.expr e) tsc_inst_opt annotmap in
166 let id = QmlAst.Ident (label, id_) in
167 annotmap, id
168 with Not_found ->
169 (* that was not a module name (maybe it's just a record or a functor) *)
170 annotmap, e
171 )
172 | None ->
173 annotmap, e
174 )
175 | _ ->
176 annotmap, e
177 )
178 ) annotmap [code_elt] in
179 annotmap, List.get_only_element code
180
181 let analyse_and_rewrite_code_elt (env, annotmap) code_elt =
182 let annotmap, code_elt = rewrite_code_elt env annotmap code_elt in
183 let env = analyze_code_elt env code_elt in
184 (env, annotmap), code_elt
185
186 let analyse_and_rewrite_code env annotmap code =
187 List.fold_left_map analyse_and_rewrite_code_elt (env, annotmap) code
188
189 module S =
190 struct
191 type t = env * QmlAst.annotmap
192 let pass = "undot"
193 let pp f _ = Format.pp_print_string f "<dummy>"
194 end
195
196 module R = ObjectFiles.Make(S)
197
198 let load_env (env, annotmap) =
199 R.fold_with_name
200 (fun package (env, annotmap) (old_env, old_annotmap) ->
201 let old_annotmap = QmlRefresh.refresh_annotmap package old_annotmap in
202 let annotmap, modules =
203 IdentMap.fold_map
204 (fun _k v annotmap ->
205 StringMap.fold_map
206 (fun _i e annotmap ->
207 QmlRefresh.refresh_expr package ~annotmap_old:old_annotmap annotmap e
208 ) v annotmap
209 ) old_env.modules annotmap in
210 let modules = IdentMap.safe_merge env.modules modules in
211 let aliases = IdentMap.fold
212 (fun alias module_ aliases ->
213 if IdentMap.mem module_ modules then
214 IdentMap.add alias module_ aliases
215 else
216 (* filtering out aliases that refer to packages that are not in the direct deps *)
217 aliases
218 ) old_env.aliases env.aliases in
219 ({modules; aliases}, annotmap)
220 ) (env, annotmap)
221
222 let save_env ~new_env ~env ~annotmap =
223 let modules = IdentMap.diff new_env.modules env.modules in
224 let aliases = IdentMap.diff new_env.aliases env.aliases in
225 let small_env = {modules; aliases} in
226 let small_annotmap = QmlRefresh.restrict_annotmap_fold_expr fold_expr annotmap small_env in
227 R.save (small_env, small_annotmap)
228
229 let process_code gamma annotmap code =
230 let env, annotmap = load_env (empty_env, annotmap) in
231 let (new_env, annotmap), code = analyse_and_rewrite_code env annotmap code in
232 save_env ~new_env ~env ~annotmap;
233 (gamma, annotmap), code
Something went wrong with that request. Please try again.