haftmann@39216
|
1 |
(* Title: Tools/Code/code_namespace.ML
|
haftmann@39216
|
2 |
Author: Florian Haftmann, TU Muenchen
|
haftmann@39216
|
3 |
|
haftmann@39216
|
4 |
Mastering target language namespaces.
|
haftmann@39216
|
5 |
*)
|
haftmann@39216
|
6 |
|
haftmann@39216
|
7 |
signature CODE_NAMESPACE =
|
haftmann@39216
|
8 |
sig
|
haftmann@39261
|
9 |
datatype ('a, 'b) node =
|
haftmann@39216
|
10 |
Dummy
|
haftmann@39261
|
11 |
| Stmt of 'a
|
haftmann@39261
|
12 |
| Module of ('b * (string * ('a, 'b) node) Graph.T);
|
haftmann@39216
|
13 |
val hierarchical_program: (string -> string) -> { module_alias: string -> string option,
|
haftmann@39266
|
14 |
reserved: Name.context, empty_nsp: 'c, namify_module: string -> 'c -> string * 'c,
|
haftmann@39266
|
15 |
namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c,
|
haftmann@39266
|
16 |
cyclic_modules: bool, empty_data: 'b, memorize_data: string -> 'b -> 'b,
|
haftmann@39268
|
17 |
modify_stmts: (string * Code_Thingol.stmt) list -> 'a option list }
|
haftmann@39216
|
18 |
-> Code_Thingol.program
|
haftmann@39216
|
19 |
-> { deresolver: string list -> string -> string,
|
haftmann@39267
|
20 |
hierarchical_program: (string * ('a, 'b) node) Graph.T }
|
haftmann@39216
|
21 |
end;
|
haftmann@39216
|
22 |
|
haftmann@39216
|
23 |
structure Code_Namespace : CODE_NAMESPACE =
|
haftmann@39216
|
24 |
struct
|
haftmann@39216
|
25 |
|
haftmann@39216
|
26 |
(* hierarchical program structure *)
|
haftmann@39216
|
27 |
|
haftmann@39261
|
28 |
datatype ('a, 'b) node =
|
haftmann@39216
|
29 |
Dummy
|
haftmann@39261
|
30 |
| Stmt of 'a
|
haftmann@39261
|
31 |
| Module of ('b * (string * ('a, 'b) node) Graph.T);
|
haftmann@39216
|
32 |
|
haftmann@39262
|
33 |
fun map_module_content f (Module content) = Module (f content);
|
haftmann@39262
|
34 |
|
haftmann@39262
|
35 |
fun map_module [] = I
|
haftmann@39262
|
36 |
| map_module (name_fragment :: name_fragments) =
|
haftmann@39262
|
37 |
apsnd o Graph.map_node name_fragment o apsnd o map_module_content
|
haftmann@39262
|
38 |
o map_module name_fragments;
|
haftmann@39262
|
39 |
|
haftmann@39216
|
40 |
fun hierarchical_program labelled_name { module_alias, reserved, empty_nsp,
|
haftmann@39267
|
41 |
namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data, modify_stmts } program =
|
haftmann@39216
|
42 |
let
|
haftmann@39216
|
43 |
|
haftmann@39216
|
44 |
(* building module name hierarchy *)
|
haftmann@39216
|
45 |
fun alias_fragments name = case module_alias name
|
haftmann@39216
|
46 |
of SOME name' => Long_Name.explode name'
|
haftmann@39216
|
47 |
| NONE => map (fn name => fst (yield_singleton Name.variants name reserved))
|
haftmann@39216
|
48 |
(Long_Name.explode name);
|
haftmann@39216
|
49 |
val module_names = Graph.fold (insert (op =) o fst o Code_Printer.dest_name o fst) program [];
|
haftmann@39216
|
50 |
val fragments_tab = fold (fn name => Symtab.update
|
haftmann@39216
|
51 |
(name, alias_fragments name)) module_names Symtab.empty;
|
haftmann@39216
|
52 |
val dest_name = Code_Printer.dest_name #>> (the o Symtab.lookup fragments_tab);
|
haftmann@39216
|
53 |
|
haftmann@39216
|
54 |
(* building empty module hierarchy *)
|
haftmann@39216
|
55 |
val empty_module = (empty_data, Graph.empty);
|
haftmann@39216
|
56 |
fun ensure_module name_fragment (data, nodes) =
|
haftmann@39216
|
57 |
if can (Graph.get_node nodes) name_fragment then (data, nodes)
|
haftmann@39216
|
58 |
else (data,
|
haftmann@39216
|
59 |
nodes |> Graph.new_node (name_fragment, (name_fragment, Module empty_module)));
|
haftmann@39216
|
60 |
fun allocate_module [] = I
|
haftmann@39216
|
61 |
| allocate_module (name_fragment :: name_fragments) =
|
haftmann@39216
|
62 |
ensure_module name_fragment
|
haftmann@39262
|
63 |
#> (apsnd o Graph.map_node name_fragment o apsnd o map_module_content o allocate_module) name_fragments;
|
haftmann@39216
|
64 |
val empty_program = Symtab.fold (fn (_, fragments) => allocate_module fragments)
|
haftmann@39216
|
65 |
fragments_tab empty_module;
|
haftmann@39216
|
66 |
|
haftmann@39216
|
67 |
(* distribute statements over hierarchy *)
|
haftmann@39216
|
68 |
fun add_stmt name stmt =
|
haftmann@39216
|
69 |
let
|
haftmann@39216
|
70 |
val (name_fragments, base) = dest_name name;
|
haftmann@39216
|
71 |
in
|
haftmann@39262
|
72 |
(map_module name_fragments o apsnd) (Graph.new_node (name, (base, Stmt stmt)))
|
haftmann@39216
|
73 |
end;
|
haftmann@39216
|
74 |
fun add_dependency name name' =
|
haftmann@39216
|
75 |
let
|
haftmann@39216
|
76 |
val (name_fragments, base) = dest_name name;
|
haftmann@39216
|
77 |
val (name_fragments', base') = dest_name name';
|
haftmann@39216
|
78 |
val (name_fragments_common, (diff, diff')) =
|
haftmann@39216
|
79 |
chop_prefix (op =) (name_fragments, name_fragments');
|
haftmann@39267
|
80 |
val is_module = not (null diff andalso null diff');
|
haftmann@39267
|
81 |
val dep = pairself hd (diff @ [name], diff' @ [name']);
|
haftmann@39216
|
82 |
val add_edge = if is_module andalso not cyclic_modules
|
haftmann@39216
|
83 |
then (fn node => Graph.add_edge_acyclic dep node
|
haftmann@39216
|
84 |
handle Graph.CYCLES _ => error ("Dependency "
|
haftmann@39216
|
85 |
^ quote name ^ " -> " ^ quote name'
|
haftmann@39216
|
86 |
^ " would result in module dependency cycle"))
|
haftmann@39216
|
87 |
else Graph.add_edge dep
|
haftmann@39262
|
88 |
in (map_module name_fragments_common o apsnd) add_edge end;
|
haftmann@39216
|
89 |
val proto_program = empty_program
|
haftmann@39216
|
90 |
|> Graph.fold (fn (name, (stmt, _)) => add_stmt name stmt) program
|
haftmann@39216
|
91 |
|> Graph.fold (fn (name, (_, (_, names))) => fold (add_dependency name) names) program;
|
haftmann@39216
|
92 |
|
haftmann@39266
|
93 |
(* name declarations, data and statement modifications *)
|
haftmann@39216
|
94 |
fun make_declarations nsps (data, nodes) =
|
haftmann@39216
|
95 |
let
|
haftmann@39216
|
96 |
val (module_fragments, stmt_names) = List.partition
|
haftmann@39216
|
97 |
(fn name_fragment => case Graph.get_node nodes name_fragment
|
haftmann@39216
|
98 |
of (_, Module _) => true | _ => false) (Graph.keys nodes);
|
haftmann@39266
|
99 |
fun declare namify name (nsps, nodes) =
|
haftmann@39216
|
100 |
let
|
haftmann@39216
|
101 |
val (base, node) = Graph.get_node nodes name;
|
haftmann@39216
|
102 |
val (base', nsps') = namify node base nsps;
|
haftmann@39266
|
103 |
val nodes' = Graph.map_node name (K (base', node)) nodes;
|
haftmann@39216
|
104 |
in (nsps', nodes') end;
|
haftmann@39216
|
105 |
val (nsps', nodes') = (nsps, nodes)
|
haftmann@39266
|
106 |
|> fold (declare (K namify_module)) module_fragments
|
haftmann@39266
|
107 |
|> fold (declare (namify_stmt o (fn Stmt stmt => stmt))) stmt_names;
|
haftmann@39267
|
108 |
val modify_stmts' = filter (member (op =) stmt_names)
|
haftmann@39267
|
109 |
#> AList.make (snd o Graph.get_node nodes)
|
haftmann@39267
|
110 |
#> split_list
|
haftmann@39267
|
111 |
##> map (fn Stmt stmt => stmt)
|
haftmann@39268
|
112 |
#> (fn (names, stmts) => names ~~ modify_stmts (names ~~ stmts));
|
haftmann@39267
|
113 |
val stmtss' = maps modify_stmts' (Graph.strong_conn nodes);
|
haftmann@39267
|
114 |
val nodes'' = Graph.map (fn name => apsnd (fn Module content => Module (make_declarations nsps' content)
|
haftmann@39267
|
115 |
| _ => case AList.lookup (op =) stmtss' name of SOME (SOME stmt) => Stmt stmt | _ => Dummy)) nodes';
|
haftmann@39262
|
116 |
val data' = fold memorize_data stmt_names data;
|
haftmann@39262
|
117 |
in (data', nodes'') end;
|
haftmann@39216
|
118 |
val (_, hierarchical_program) = make_declarations empty_nsp proto_program;
|
haftmann@39216
|
119 |
|
haftmann@39216
|
120 |
(* deresolving *)
|
haftmann@39216
|
121 |
fun deresolver prefix_fragments name =
|
haftmann@39216
|
122 |
let
|
haftmann@39216
|
123 |
val (name_fragments, _) = dest_name name;
|
haftmann@39216
|
124 |
val (_, (_, remainder)) = chop_prefix (op =) (prefix_fragments, name_fragments);
|
haftmann@39216
|
125 |
val nodes = fold (fn name_fragment => fn nodes => case Graph.get_node nodes name_fragment
|
haftmann@39216
|
126 |
of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
|
haftmann@39216
|
127 |
val (base', _) = Graph.get_node nodes name;
|
haftmann@39216
|
128 |
in Long_Name.implode (remainder @ [base']) end
|
haftmann@39216
|
129 |
handle Graph.UNDEF _ => error ("Unknown statement name: " ^ labelled_name name);
|
haftmann@39216
|
130 |
|
haftmann@39216
|
131 |
in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
|
haftmann@39216
|
132 |
|
haftmann@39216
|
133 |
end; |