src/Tools/Code/code_namespace.ML
author haftmann
Tue Sep 07 11:08:58 2010 +0200 (2010-09-07)
changeset 39203 b2f9a6f4b84b
parent 39147 3c284a152bd6
child 39208 fc1e02735438
permissions -rw-r--r--
factored out build_module_namespace
     1 (*  Title:      Tools/Code/code_namespace.ML
     2     Author:     Florian Haftmann, TU Muenchen
     3 
     4 Mastering target language namespaces.
     5 *)
     6 
     7 signature CODE_NAMESPACE =
     8 sig
     9   val dest_name: string -> string * string
    10   val build_module_namespace: { module_alias: string -> string option,
    11     module_prefix: string option, reserved: Name.context } -> Code_Thingol.program
    12     -> string list Symtab.table
    13   datatype ('a, 'b) node =
    14       Dummy
    15     | Stmt of 'a
    16     | Module of ('b * (string * ('a, 'b) node) Graph.T)
    17   type ('a, 'b) hierarchical_program = (string * ('a, 'b) node) Graph.T
    18   val hierarchical_program: (string -> string) -> { module_alias: string -> string option,
    19     reserved: Name.context, empty_nsp: 'c, namify_module: string -> 'c -> string * 'c,
    20     namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c,
    21     cyclic_modules: bool, empty_data: 'b, memorize_data: string -> 'b -> 'b,
    22     modify_stmts: (string * Code_Thingol.stmt) list -> 'a option list }
    23       -> Code_Thingol.program
    24       -> { deresolver: string list -> string -> string,
    25            hierarchical_program: ('a, 'b) hierarchical_program }
    26   val print_hierarchical: { print_module: string list -> string -> 'b -> 'c list -> 'c,
    27     print_stmt: string list -> string * 'a -> 'c,
    28     lift_markup: (Pretty.T -> Pretty.T) -> 'c -> 'c }
    29       -> ('a, 'b) hierarchical_program -> 'c list
    30 end;
    31 
    32 structure Code_Namespace : CODE_NAMESPACE =
    33 struct
    34 
    35 (** building module name hierarchy **)
    36 
    37 val dest_name =
    38   apfst Long_Name.implode o split_last o fst o split_last o Long_Name.explode;
    39 
    40 fun build_module_namespace { module_alias, module_prefix, reserved } program =
    41   let
    42     fun alias_fragments name = case module_alias name
    43      of SOME name' => Long_Name.explode name'
    44       | NONE => map (fn name => fst (yield_singleton Name.variants name reserved))
    45           (Long_Name.explode name);
    46     val module_names = Graph.fold (insert (op =) o fst o dest_name o fst) program [];
    47   in
    48     fold (fn name => Symtab.update (name, alias_fragments name))
    49       module_names Symtab.empty
    50   end;
    51 
    52 
    53 (** hierarchical program structure **)
    54 
    55 datatype ('a, 'b) node =
    56     Dummy
    57   | Stmt of 'a
    58   | Module of ('b * (string * ('a, 'b) node) Graph.T);
    59 
    60 type ('a, 'b) hierarchical_program = (string * ('a, 'b) node) Graph.T;
    61 
    62 fun map_module_content f (Module content) = Module (f content);
    63 
    64 fun map_module [] = I
    65   | map_module (name_fragment :: name_fragments) =
    66       apsnd o Graph.map_node name_fragment o apsnd o map_module_content
    67         o map_module name_fragments;
    68 
    69 fun hierarchical_program labelled_name { module_alias, reserved, empty_nsp,
    70       namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data, modify_stmts } program =
    71   let
    72 
    73     (* building module name hierarchy *)
    74     val fragments_tab = build_module_namespace { module_alias = module_alias,
    75       module_prefix = NONE, reserved = reserved } program;
    76     val dest_name = dest_name #>> (the o Symtab.lookup fragments_tab);
    77 
    78     (* building empty module hierarchy *)
    79     val empty_module = (empty_data, Graph.empty);
    80     fun ensure_module name_fragment (data, nodes) =
    81       if can (Graph.get_node nodes) name_fragment then (data, nodes)
    82       else (data,
    83         nodes |> Graph.new_node (name_fragment, (name_fragment, Module empty_module)));
    84     fun allocate_module [] = I
    85       | allocate_module (name_fragment :: name_fragments) =
    86           ensure_module name_fragment
    87           #> (apsnd o Graph.map_node name_fragment o apsnd o map_module_content o allocate_module) name_fragments;
    88     val empty_program = Symtab.fold (fn (_, fragments) => allocate_module fragments)
    89       fragments_tab empty_module;
    90 
    91     (* distribute statements over hierarchy *)
    92     fun add_stmt name stmt =
    93       let
    94         val (name_fragments, base) = dest_name name;
    95       in
    96         (map_module name_fragments o apsnd) (Graph.new_node (name, (base, Stmt stmt)))
    97       end;
    98     fun add_dependency name name' =
    99       let
   100         val (name_fragments, base) = dest_name name;
   101         val (name_fragments', base') = dest_name name';
   102         val (name_fragments_common, (diff, diff')) =
   103           chop_prefix (op =) (name_fragments, name_fragments');
   104         val is_module = not (null diff andalso null diff');
   105         val dep = pairself hd (diff @ [name], diff' @ [name']);
   106         val add_edge = if is_module andalso not cyclic_modules
   107           then (fn node => Graph.add_edge_acyclic dep node
   108             handle Graph.CYCLES _ => error ("Dependency "
   109               ^ quote name ^ " -> " ^ quote name'
   110               ^ " would result in module dependency cycle"))
   111           else Graph.add_edge dep
   112       in (map_module name_fragments_common o apsnd) add_edge end;
   113     val proto_program = empty_program
   114       |> Graph.fold (fn (name, (stmt, _)) => add_stmt name stmt) program
   115       |> Graph.fold (fn (name, (_, (_, names))) => fold (add_dependency name) names) program;
   116 
   117     (* name declarations, data and statement modifications *)
   118     fun make_declarations nsps (data, nodes) =
   119       let
   120         val (module_fragments, stmt_names) = List.partition
   121           (fn name_fragment => case Graph.get_node nodes name_fragment
   122             of (_, Module _) => true | _ => false) (Graph.keys nodes);
   123         fun declare namify name (nsps, nodes) =
   124           let
   125             val (base, node) = Graph.get_node nodes name;
   126             val (base', nsps') = namify node base nsps;
   127             val nodes' = Graph.map_node name (K (base', node)) nodes;
   128           in (nsps', nodes') end;
   129         val (nsps', nodes') = (nsps, nodes)
   130           |> fold (declare (K namify_module)) module_fragments
   131           |> fold (declare (namify_stmt o (fn Stmt stmt => stmt))) stmt_names;
   132         fun zip_fillup xs ys = xs ~~ ys @ replicate (length xs - length ys) NONE;
   133         fun select_names names = case filter (member (op =) stmt_names) names
   134          of [] => NONE
   135           | xs => SOME xs;
   136         val modify_stmts' = AList.make (snd o Graph.get_node nodes)
   137           #> split_list
   138           ##> map (fn Stmt stmt => stmt)
   139           #> (fn (names, stmts) => zip_fillup names (modify_stmts (names ~~ stmts)));
   140         val stmtss' = (maps modify_stmts' o map_filter select_names o Graph.strong_conn) nodes;
   141         val nodes'' = Graph.map (fn name => apsnd (fn Module content => Module (make_declarations nsps' content)
   142             | _ => case AList.lookup (op =) stmtss' name of SOME (SOME stmt) => Stmt stmt | _ => Dummy)) nodes';
   143         val data' = fold memorize_data stmt_names data;
   144       in (data', nodes'') end;
   145     val (_, hierarchical_program) = make_declarations empty_nsp proto_program;
   146 
   147     (* deresolving *)
   148     fun deresolver prefix_fragments name =
   149       let
   150         val (name_fragments, _) = dest_name name;
   151         val (_, (_, remainder)) = chop_prefix (op =) (prefix_fragments, name_fragments);
   152         val nodes = fold (fn name_fragment => fn nodes => case Graph.get_node nodes name_fragment
   153          of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
   154         val (base', _) = Graph.get_node nodes name;
   155       in Long_Name.implode (remainder @ [base']) end
   156         handle Graph.UNDEF _ => error ("Unknown statement name: " ^ labelled_name name);
   157 
   158   in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
   159 
   160 fun print_hierarchical { print_module, print_stmt, lift_markup } =
   161   let
   162     fun print_node _ (_, Dummy) =
   163           NONE
   164       | print_node prefix_fragments (name, Stmt stmt) =
   165           SOME (lift_markup (Code_Printer.markup_stmt name)
   166             (print_stmt prefix_fragments (name, stmt)))
   167       | print_node prefix_fragments (name_fragment, Module (data, nodes)) =
   168           let
   169             val prefix_fragments' = prefix_fragments @ [name_fragment]
   170           in
   171             Option.map (print_module prefix_fragments'
   172               name_fragment data) (print_nodes prefix_fragments' nodes)
   173           end
   174     and print_nodes prefix_fragments nodes =
   175       let
   176         val xs = (map_filter (fn name => print_node prefix_fragments
   177           (name, snd (Graph.get_node nodes name))) o rev o flat o Graph.strong_conn) nodes
   178       in if null xs then NONE else SOME xs end;
   179   in these o print_nodes [] end;
   180 
   181 end;