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