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