12 | Module of ('b * (string * ('a, 'b) node) Graph.T); |
12 | Module of ('b * (string * ('a, 'b) node) Graph.T); |
13 val hierarchical_program: (string -> string) -> { module_alias: string -> string option, |
13 val hierarchical_program: (string -> string) -> { module_alias: string -> string option, |
14 reserved: Name.context, empty_nsp: 'c, namify_module: string -> 'c -> string * 'c, |
14 reserved: Name.context, empty_nsp: 'c, namify_module: string -> 'c -> string * 'c, |
15 namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c, |
15 namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c, |
16 cyclic_modules: bool, empty_data: 'b, memorize_data: string -> 'b -> 'b, |
16 cyclic_modules: bool, empty_data: 'b, memorize_data: string -> 'b -> 'b, |
17 modify_stmt: Code_Thingol.stmt -> Code_Thingol.stmt option } |
17 modify_stmts: Code_Thingol.stmt list -> 'a option list } |
18 -> Code_Thingol.program |
18 -> Code_Thingol.program |
19 -> { deresolver: string list -> string -> string, |
19 -> { deresolver: string list -> string -> string, |
20 hierarchical_program: (string * (Code_Thingol.stmt, 'b) node) Graph.T } |
20 hierarchical_program: (string * ('a, 'b) node) Graph.T } |
21 end; |
21 end; |
22 |
22 |
23 structure Code_Namespace : CODE_NAMESPACE = |
23 structure Code_Namespace : CODE_NAMESPACE = |
24 struct |
24 struct |
25 |
25 |
36 | map_module (name_fragment :: name_fragments) = |
36 | map_module (name_fragment :: name_fragments) = |
37 apsnd o Graph.map_node name_fragment o apsnd o map_module_content |
37 apsnd o Graph.map_node name_fragment o apsnd o map_module_content |
38 o map_module name_fragments; |
38 o map_module name_fragments; |
39 |
39 |
40 fun hierarchical_program labelled_name { module_alias, reserved, empty_nsp, |
40 fun hierarchical_program labelled_name { module_alias, reserved, empty_nsp, |
41 namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data, modify_stmt } program = |
41 namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data, modify_stmts } program = |
42 let |
42 let |
43 |
43 |
44 (* building module name hierarchy *) |
44 (* building module name hierarchy *) |
45 fun alias_fragments name = case module_alias name |
45 fun alias_fragments name = case module_alias name |
46 of SOME name' => Long_Name.explode name' |
46 of SOME name' => Long_Name.explode name' |
75 let |
75 let |
76 val (name_fragments, base) = dest_name name; |
76 val (name_fragments, base) = dest_name name; |
77 val (name_fragments', base') = dest_name name'; |
77 val (name_fragments', base') = dest_name name'; |
78 val (name_fragments_common, (diff, diff')) = |
78 val (name_fragments_common, (diff, diff')) = |
79 chop_prefix (op =) (name_fragments, name_fragments'); |
79 chop_prefix (op =) (name_fragments, name_fragments'); |
80 val (is_module, dep) = if null diff then (false, (name, name')) |
80 val is_module = not (null diff andalso null diff'); |
81 else (true, (hd diff, hd diff')) |
81 val dep = pairself hd (diff @ [name], diff' @ [name']); |
82 val add_edge = if is_module andalso not cyclic_modules |
82 val add_edge = if is_module andalso not cyclic_modules |
83 then (fn node => Graph.add_edge_acyclic dep node |
83 then (fn node => Graph.add_edge_acyclic dep node |
84 handle Graph.CYCLES _ => error ("Dependency " |
84 handle Graph.CYCLES _ => error ("Dependency " |
85 ^ quote name ^ " -> " ^ quote name' |
85 ^ quote name ^ " -> " ^ quote name' |
86 ^ " would result in module dependency cycle")) |
86 ^ " would result in module dependency cycle")) |
103 val nodes' = Graph.map_node name (K (base', node)) nodes; |
103 val nodes' = Graph.map_node name (K (base', node)) nodes; |
104 in (nsps', nodes') end; |
104 in (nsps', nodes') end; |
105 val (nsps', nodes') = (nsps, nodes) |
105 val (nsps', nodes') = (nsps, nodes) |
106 |> fold (declare (K namify_module)) module_fragments |
106 |> fold (declare (K namify_module)) module_fragments |
107 |> fold (declare (namify_stmt o (fn Stmt stmt => stmt))) stmt_names; |
107 |> fold (declare (namify_stmt o (fn Stmt stmt => stmt))) stmt_names; |
108 val nodes'' = nodes' |
108 val modify_stmts' = filter (member (op =) stmt_names) |
109 |> fold (fn name_fragment => (Graph.map_node name_fragment |
109 #> AList.make (snd o Graph.get_node nodes) |
110 o apsnd o map_module_content) (make_declarations nsps')) module_fragments |
110 #> split_list |
111 |> fold (fn name => (Graph.map_node name o apsnd) (fn Stmt stmt => |
111 ##> map (fn Stmt stmt => stmt) |
112 case modify_stmt stmt of NONE => Dummy | SOME stmt => Stmt stmt)) stmt_names; |
112 ##> modify_stmts |
|
113 #> op ~~; |
|
114 val stmtss' = maps modify_stmts' (Graph.strong_conn nodes); |
|
115 val nodes'' = Graph.map (fn name => apsnd (fn Module content => Module (make_declarations nsps' content) |
|
116 | _ => case AList.lookup (op =) stmtss' name of SOME (SOME stmt) => Stmt stmt | _ => Dummy)) nodes'; |
113 val data' = fold memorize_data stmt_names data; |
117 val data' = fold memorize_data stmt_names data; |
114 in (data', nodes'') end; |
118 in (data', nodes'') end; |
115 val (_, hierarchical_program) = make_declarations empty_nsp proto_program; |
119 val (_, hierarchical_program) = make_declarations empty_nsp proto_program; |
116 |
120 |
117 (* deresolving *) |
121 (* deresolving *) |