1 (* Title: Tools/Code/code_namespace.ML
2 Author: Florian Haftmann, TU Muenchen
4 Mastering target language namespaces.
7 signature CODE_NAMESPACE =
10 val flat_program: (string -> string) -> { module_alias: string -> string option,
11 module_prefix: string, reserved: Name.context, empty_nsp: 'a,
12 namify_stmt: Code_Thingol.stmt -> string -> 'a -> string * 'a,
13 modify_stmt: Code_Thingol.stmt -> Code_Thingol.stmt option }
14 -> Code_Thingol.program
15 -> { deresolver: string -> string -> string,
16 flat_program: flat_program }
18 datatype ('a, 'b) node =
21 | Module of ('b * (string * ('a, 'b) node) Graph.T)
22 type ('a, 'b) hierarchical_program
23 val hierarchical_program: (string -> string) -> { module_alias: string -> string option,
24 reserved: Name.context, empty_nsp: 'c, namify_module: string -> 'c -> string * 'c,
25 namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c,
26 cyclic_modules: bool, empty_data: 'b, memorize_data: string -> 'b -> 'b,
27 modify_stmts: (string * Code_Thingol.stmt) list -> 'a option list }
28 -> Code_Thingol.program
29 -> { deresolver: string list -> string -> string,
30 hierarchical_program: ('a, 'b) hierarchical_program }
31 val print_hierarchical: { print_module: string list -> string -> 'b -> 'c list -> 'c,
32 print_stmt: string list -> string * 'a -> 'c,
33 lift_markup: (Pretty.T -> Pretty.T) -> 'c -> 'c }
34 -> ('a, 'b) hierarchical_program -> 'c list
37 structure Code_Namespace : CODE_NAMESPACE =
40 (** building module name hierarchy **)
43 apfst Long_Name.implode o split_last o fst o split_last o Long_Name.explode;
45 fun build_module_namespace { module_alias, module_prefix, reserved } program =
47 fun alias_fragments name = case module_alias name
48 of SOME name' => Long_Name.explode name'
49 | NONE => map (fn name => fst (yield_singleton Name.variants name reserved))
50 (Long_Name.explode name);
51 val module_names = Graph.fold (insert (op =) o fst o dest_name o fst) program [];
53 fold (fn name => Symtab.update (name, Long_Name.explode module_prefix @ alias_fragments name))
54 module_names Symtab.empty
58 (** flat program structure **)
60 type flat_program = ((string * Code_Thingol.stmt option) Graph.T * (string * string list) list) Graph.T;
62 fun flat_program labelled_name { module_alias, module_prefix, reserved,
63 empty_nsp, namify_stmt, modify_stmt } program =
66 (* building module name hierarchy *)
67 val fragments_tab = build_module_namespace { module_alias = module_alias,
68 module_prefix = module_prefix, reserved = reserved } program;
69 val dest_name = dest_name
70 #>> (Long_Name.implode o the o Symtab.lookup fragments_tab);
72 (* distribute statements over hierarchy *)
73 fun add_stmt name stmt =
75 val (module_name, base) = dest_name name;
77 Graph.default_node (module_name, (Graph.empty, []))
78 #> (Graph.map_node module_name o apfst) (Graph.new_node (name, (base, stmt)))
80 fun add_dependency name name' =
82 val (module_name, base) = dest_name name;
83 val (module_name', base') = dest_name name';
84 in if module_name = module_name'
85 then (Graph.map_node module_name o apfst) (Graph.add_edge (name, name'))
86 else (Graph.map_node module_name o apsnd) (AList.map_default (op =) (module_name', []) (insert (op =) name'))
88 val proto_program = Graph.empty
89 |> Graph.fold (fn (name, (stmt, _)) => add_stmt name stmt) program
90 |> Graph.fold (fn (name, (_, (_, names))) => fold (add_dependency name) names) program;
92 (* name declarations and statement modifications *)
93 fun declare name (base, stmt) (gr, nsp) =
95 val (base', nsp') = namify_stmt stmt base nsp;
96 val gr' = (Graph.map_node name o apfst) (K base') gr;
98 fun declarations gr = (gr, empty_nsp)
99 |> fold (fn name => declare name (Graph.get_node gr name)) (Graph.keys gr)
101 |> (Graph.map o K o apsnd) modify_stmt;
102 val flat_program = proto_program
103 |> (Graph.map o K o apfst) declarations;
105 (* qualified and unqualified imports, deresolving *)
106 fun base_deresolver name = fst (Graph.get_node
107 (fst (Graph.get_node flat_program (fst (dest_name name)))) name);
108 fun classify_names gr imports =
110 val import_tab = maps
111 (fn (module_name, names) => map (rpair module_name) names) imports;
112 val imported_names = map fst import_tab;
113 val here_names = Graph.keys gr;
116 |> fold (fn name => Symtab.update (name, base_deresolver name)) here_names
117 |> fold (fn name => Symtab.update (name,
118 Long_Name.append (the (AList.lookup (op =) import_tab name))
119 (base_deresolver name))) imported_names
121 val deresolver_tab = Symtab.make (AList.make
122 (uncurry classify_names o Graph.get_node flat_program)
123 (Graph.keys flat_program));
124 fun deresolver "" name =
125 Long_Name.append (fst (dest_name name)) (base_deresolver name)
126 | deresolver module_name name =
127 the (Symtab.lookup (the (Symtab.lookup deresolver_tab module_name)) name)
128 handle Option => error ("Unknown statement name: " ^ labelled_name name);
130 in { deresolver = deresolver, flat_program = flat_program } end;
133 (** hierarchical program structure **)
135 datatype ('a, 'b) node =
138 | Module of ('b * (string * ('a, 'b) node) Graph.T);
140 type ('a, 'b) hierarchical_program = (string * ('a, 'b) node) Graph.T;
142 fun map_module_content f (Module content) = Module (f content);
144 fun map_module [] = I
145 | map_module (name_fragment :: name_fragments) =
146 apsnd o Graph.map_node name_fragment o apsnd o map_module_content
147 o map_module name_fragments;
149 fun hierarchical_program labelled_name { module_alias, reserved, empty_nsp,
150 namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data, modify_stmts } program =
153 (* building module name hierarchy *)
154 val fragments_tab = build_module_namespace { module_alias = module_alias,
155 module_prefix = "", reserved = reserved } program;
156 val dest_name = dest_name #>> (the o Symtab.lookup fragments_tab);
158 (* building empty module hierarchy *)
159 val empty_module = (empty_data, Graph.empty);
160 fun ensure_module name_fragment (data, nodes) =
161 if can (Graph.get_node nodes) name_fragment then (data, nodes)
163 nodes |> Graph.new_node (name_fragment, (name_fragment, Module empty_module)));
164 fun allocate_module [] = I
165 | allocate_module (name_fragment :: name_fragments) =
166 ensure_module name_fragment
167 #> (apsnd o Graph.map_node name_fragment o apsnd o map_module_content o allocate_module) name_fragments;
168 val empty_program = Symtab.fold (fn (_, fragments) => allocate_module fragments)
169 fragments_tab empty_module;
171 (* distribute statements over hierarchy *)
172 fun add_stmt name stmt =
174 val (name_fragments, base) = dest_name name;
176 (map_module name_fragments o apsnd) (Graph.new_node (name, (base, Stmt stmt)))
178 fun add_dependency name name' =
180 val (name_fragments, base) = dest_name name;
181 val (name_fragments', base') = dest_name name';
182 val (name_fragments_common, (diff, diff')) =
183 chop_prefix (op =) (name_fragments, name_fragments');
184 val is_module = not (null diff andalso null diff');
185 val dep = pairself hd (diff @ [name], diff' @ [name']);
186 val add_edge = if is_module andalso not cyclic_modules
187 then (fn node => Graph.add_edge_acyclic dep node
188 handle Graph.CYCLES _ => error ("Dependency "
189 ^ quote name ^ " -> " ^ quote name'
190 ^ " would result in module dependency cycle"))
191 else Graph.add_edge dep
192 in (map_module name_fragments_common o apsnd) add_edge end;
193 val proto_program = empty_program
194 |> Graph.fold (fn (name, (stmt, _)) => add_stmt name stmt) program
195 |> Graph.fold (fn (name, (_, (_, names))) => fold (add_dependency name) names) program;
197 (* name declarations, data and statement modifications *)
198 fun make_declarations nsps (data, nodes) =
200 val (module_fragments, stmt_names) = List.partition
201 (fn name_fragment => case Graph.get_node nodes name_fragment
202 of (_, Module _) => true | _ => false) (Graph.keys nodes);
203 fun declare namify name (nsps, nodes) =
205 val (base, node) = Graph.get_node nodes name;
206 val (base', nsps') = namify node base nsps;
207 val nodes' = Graph.map_node name (K (base', node)) nodes;
208 in (nsps', nodes') end;
209 val (nsps', nodes') = (nsps, nodes)
210 |> fold (declare (K namify_module)) module_fragments
211 |> fold (declare (namify_stmt o (fn Stmt stmt => stmt))) stmt_names;
212 fun zip_fillup xs ys = xs ~~ ys @ replicate (length xs - length ys) NONE;
213 fun select_names names = case filter (member (op =) stmt_names) names
216 val modify_stmts' = AList.make (snd o Graph.get_node nodes)
218 ##> map (fn Stmt stmt => stmt)
219 #> (fn (names, stmts) => zip_fillup names (modify_stmts (names ~~ stmts)));
220 val stmtss' = (maps modify_stmts' o map_filter select_names o Graph.strong_conn) nodes;
221 val nodes'' = Graph.map (fn name => apsnd (fn Module content => Module (make_declarations nsps' content)
222 | _ => case AList.lookup (op =) stmtss' name of SOME (SOME stmt) => Stmt stmt | _ => Dummy)) nodes';
223 val data' = fold memorize_data stmt_names data;
224 in (data', nodes'') end;
225 val (_, hierarchical_program) = make_declarations empty_nsp proto_program;
228 fun deresolver prefix_fragments name =
230 val (name_fragments, _) = dest_name name;
231 val (_, (_, remainder)) = chop_prefix (op =) (prefix_fragments, name_fragments);
232 val nodes = fold (fn name_fragment => fn nodes => case Graph.get_node nodes name_fragment
233 of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
234 val (base', _) = Graph.get_node nodes name;
235 in Long_Name.implode (remainder @ [base']) end
236 handle Graph.UNDEF _ => error ("Unknown statement name: " ^ labelled_name name);
238 in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
240 fun print_hierarchical { print_module, print_stmt, lift_markup } =
242 fun print_node _ (_, Dummy) =
244 | print_node prefix_fragments (name, Stmt stmt) =
245 SOME (lift_markup (Code_Printer.markup_stmt name)
246 (print_stmt prefix_fragments (name, stmt)))
247 | print_node prefix_fragments (name_fragment, Module (data, nodes)) =
249 val prefix_fragments' = prefix_fragments @ [name_fragment]
251 Option.map (print_module prefix_fragments'
252 name_fragment data) (print_nodes prefix_fragments' nodes)
254 and print_nodes prefix_fragments nodes =
256 val xs = (map_filter (fn name => print_node prefix_fragments
257 (name, snd (Graph.get_node nodes name))) o rev o flat o Graph.strong_conn) nodes
258 in if null xs then NONE else SOME xs end;
259 in these o print_nodes [] end;