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