src/Tools/Code/code_namespace.ML
author haftmann
Wed Sep 01 12:27:49 2010 +0200 (2010-09-01)
changeset 39017 8cd5b6d688fa
parent 38970 53d1ee3d98b8
child 39018 5681d7cfabce
permissions -rw-r--r--
generalized hierarchical data structure over statements
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@39017
    14
    reserved: Name.context, empty_nsp: 'a, namify_module: string -> 'a -> string * 'a,
haftmann@39017
    15
    namify_stmt: Code_Thingol.stmt -> string -> 'a -> string * 'a,
haftmann@39017
    16
    cyclic_modules: bool, empty_data: 'b, memorize_data: string -> 'b -> 'b }
haftmann@38970
    17
      -> Code_Thingol.program
haftmann@38970
    18
      -> { deresolver: string list -> string -> string,
haftmann@39017
    19
           hierarchical_program: (string * (Code_Thingol.stmt, 'b) node) Graph.T }
haftmann@38970
    20
end;
haftmann@38970
    21
haftmann@38970
    22
structure Code_Namespace : CODE_NAMESPACE =
haftmann@38970
    23
struct
haftmann@38970
    24
haftmann@38970
    25
(* hierarchical program structure *)
haftmann@38970
    26
haftmann@39017
    27
datatype ('a, 'b) node =
haftmann@38970
    28
    Dummy
haftmann@39017
    29
  | Stmt of 'a
haftmann@39017
    30
  | Module of ('b * (string * ('a, 'b) node) Graph.T);
haftmann@38970
    31
haftmann@38970
    32
fun hierarchical_program labelled_name { module_alias, reserved, empty_nsp,
haftmann@38970
    33
      namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data } program =
haftmann@38970
    34
  let
haftmann@38970
    35
haftmann@38970
    36
    (* building module name hierarchy *)
haftmann@38970
    37
    fun alias_fragments name = case module_alias name
haftmann@38970
    38
     of SOME name' => Long_Name.explode name'
haftmann@38970
    39
      | NONE => map (fn name => fst (yield_singleton Name.variants name reserved))
haftmann@38970
    40
          (Long_Name.explode name);
haftmann@38970
    41
    val module_names = Graph.fold (insert (op =) o fst o Code_Printer.dest_name o fst) program [];
haftmann@38970
    42
    val fragments_tab = fold (fn name => Symtab.update
haftmann@38970
    43
      (name, alias_fragments name)) module_names Symtab.empty;
haftmann@38970
    44
    val dest_name = Code_Printer.dest_name #>> (the o Symtab.lookup fragments_tab);
haftmann@38970
    45
haftmann@38970
    46
    (* building empty module hierarchy *)
haftmann@38970
    47
    val empty_module = (empty_data, Graph.empty);
haftmann@38970
    48
    fun map_module f (Module content) = Module (f content);
haftmann@38970
    49
    fun change_module [] = I
haftmann@38970
    50
      | change_module (name_fragment :: name_fragments) =
haftmann@38970
    51
          apsnd o Graph.map_node name_fragment o apsnd o map_module
haftmann@38970
    52
            o change_module name_fragments;
haftmann@38970
    53
    fun ensure_module name_fragment (data, nodes) =
haftmann@38970
    54
      if can (Graph.get_node nodes) name_fragment then (data, nodes)
haftmann@38970
    55
      else (data,
haftmann@38970
    56
        nodes |> Graph.new_node (name_fragment, (name_fragment, Module empty_module)));
haftmann@38970
    57
    fun allocate_module [] = I
haftmann@38970
    58
      | allocate_module (name_fragment :: name_fragments) =
haftmann@38970
    59
          ensure_module name_fragment
haftmann@38970
    60
          #> (apsnd o Graph.map_node name_fragment o apsnd o map_module o allocate_module) name_fragments;
haftmann@38970
    61
    val empty_program = Symtab.fold (fn (_, fragments) => allocate_module fragments)
haftmann@38970
    62
      fragments_tab empty_module;
haftmann@38970
    63
haftmann@38970
    64
    (* distribute statements over hierarchy *)
haftmann@38970
    65
    fun add_stmt name stmt =
haftmann@38970
    66
      let
haftmann@38970
    67
        val (name_fragments, base) = dest_name name;
haftmann@38970
    68
      in
haftmann@38970
    69
        change_module name_fragments (fn (data, nodes) =>
haftmann@38970
    70
          (memorize_data name data, Graph.new_node (name, (base, Stmt stmt)) nodes))
haftmann@38970
    71
      end;
haftmann@38970
    72
    fun add_dependency name name' =
haftmann@38970
    73
      let
haftmann@38970
    74
        val (name_fragments, base) = dest_name name;
haftmann@38970
    75
        val (name_fragments', base') = dest_name name';
haftmann@38970
    76
        val (name_fragments_common, (diff, diff')) =
haftmann@38970
    77
          chop_prefix (op =) (name_fragments, name_fragments');
haftmann@38970
    78
        val (is_module, dep) = if null diff then (false, (name, name'))
haftmann@38970
    79
          else (true, (hd diff, hd diff'))
haftmann@38970
    80
        val add_edge = if is_module andalso not cyclic_modules
haftmann@38970
    81
          then (fn node => Graph.add_edge_acyclic dep node
haftmann@38970
    82
            handle Graph.CYCLES _ => error ("Dependency "
haftmann@38970
    83
              ^ quote name ^ " -> " ^ quote name'
haftmann@38970
    84
              ^ " would result in module dependency cycle"))
haftmann@38970
    85
          else Graph.add_edge dep
haftmann@38970
    86
      in (change_module name_fragments_common o apsnd) add_edge end;
haftmann@38970
    87
    val proto_program = empty_program
haftmann@38970
    88
      |> Graph.fold (fn (name, (stmt, _)) => add_stmt name stmt) program
haftmann@38970
    89
      |> Graph.fold (fn (name, (_, (_, names))) => fold (add_dependency name) names) program;
haftmann@38970
    90
haftmann@38970
    91
    (* name declarations *)
haftmann@38970
    92
    fun make_declarations nsps (data, nodes) =
haftmann@38970
    93
      let
haftmann@38970
    94
        val (module_fragments, stmt_names) = List.partition
haftmann@38970
    95
          (fn name_fragment => case Graph.get_node nodes name_fragment
haftmann@38970
    96
            of (_, Module _) => true | _ => false) (Graph.keys nodes);
haftmann@38970
    97
        fun modify_stmt (Stmt (Code_Thingol.Datatypecons _)) = Dummy
haftmann@38970
    98
          | modify_stmt (Stmt (Code_Thingol.Classrel _)) = Dummy
haftmann@38970
    99
          | modify_stmt (Stmt (Code_Thingol.Classparam _)) = Dummy
haftmann@38970
   100
          | modify_stmt stmt = stmt;
haftmann@38970
   101
        fun declare namify modify name (nsps, nodes) =
haftmann@38970
   102
          let
haftmann@38970
   103
            val (base, node) = Graph.get_node nodes name;
haftmann@38970
   104
            val (base', nsps') = namify node base nsps;
haftmann@38970
   105
            val nodes' = Graph.map_node name (K (base', modify node)) nodes;
haftmann@38970
   106
          in (nsps', nodes') end;
haftmann@38970
   107
        val (nsps', nodes') = (nsps, nodes)
haftmann@38970
   108
          |> fold (declare (K namify_module) I) module_fragments
haftmann@38970
   109
          |> fold (declare (namify_stmt o (fn Stmt stmt => stmt)) modify_stmt) stmt_names;
haftmann@38970
   110
        val nodes'' = nodes'
haftmann@38970
   111
          |> fold (fn name_fragment => (Graph.map_node name_fragment
haftmann@38970
   112
              o apsnd o map_module) (make_declarations nsps')) module_fragments;
haftmann@38970
   113
      in (data, nodes'') end;
haftmann@38970
   114
    val (_, hierarchical_program) = make_declarations empty_nsp proto_program;
haftmann@38970
   115
haftmann@38970
   116
    (* deresolving *)
haftmann@38970
   117
    fun deresolver prefix_fragments name =
haftmann@38970
   118
      let
haftmann@38970
   119
        val (name_fragments, _) = dest_name name;
haftmann@38970
   120
        val (_, (_, remainder)) = chop_prefix (op =) (prefix_fragments, name_fragments);
haftmann@38970
   121
        val nodes = fold (fn name_fragment => fn nodes => case Graph.get_node nodes name_fragment
haftmann@38970
   122
         of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
haftmann@38970
   123
        val (base', _) = Graph.get_node nodes name;
haftmann@38970
   124
      in Long_Name.implode (remainder @ [base']) end
haftmann@38970
   125
        handle Graph.UNDEF _ => error ("Unknown statement name: " ^ labelled_name name);
haftmann@38970
   126
haftmann@38970
   127
  in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
haftmann@38970
   128
haftmann@38970
   129
end;