src/Tools/Code/code_namespace.ML
author haftmann
Sat Jan 25 23:50:49 2014 +0100 (2014-01-25)
changeset 55147 bce3dbc11f95
parent 53414 dd64696d267a
child 55150 0940309ed8f1
permissions -rw-r--r--
prefer explicit code symbol type over ad-hoc name mangling
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@39208
     9
  type flat_program
haftmann@55147
    10
  val flat_program: Proof.context
haftmann@52138
    11
    -> { module_prefix: string, module_name: string,
haftmann@52138
    12
    reserved: Name.context, identifiers: Code_Target.identifier_data, empty_nsp: 'a,
haftmann@39208
    13
    namify_stmt: Code_Thingol.stmt -> string -> 'a -> string * 'a,
haftmann@39208
    14
    modify_stmt: Code_Thingol.stmt -> Code_Thingol.stmt option }
haftmann@39208
    15
      -> Code_Thingol.program
haftmann@55147
    16
      -> { deresolver: string -> Code_Symbol.symbol -> string,
haftmann@39208
    17
           flat_program: flat_program }
haftmann@39208
    18
haftmann@39017
    19
  datatype ('a, 'b) node =
haftmann@38970
    20
      Dummy
haftmann@39017
    21
    | Stmt of 'a
haftmann@55147
    22
    | Module of ('b * (string * ('a, 'b) node) Code_Symbol.Graph.T)
haftmann@39208
    23
  type ('a, 'b) hierarchical_program
haftmann@55147
    24
  val hierarchical_program: Proof.context
haftmann@52138
    25
    -> { module_name: string,
haftmann@52138
    26
    reserved: Name.context, identifiers: Code_Target.identifier_data,
haftmann@52138
    27
    empty_nsp: 'c, namify_module: string -> 'c -> string * 'c,
haftmann@39022
    28
    namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c,
haftmann@55147
    29
    cyclic_modules: bool, empty_data: 'b, memorize_data: Code_Symbol.symbol -> 'b -> 'b,
haftmann@55147
    30
    modify_stmts: (Code_Symbol.symbol * Code_Thingol.stmt) list -> 'a option list }
haftmann@38970
    31
      -> Code_Thingol.program
haftmann@55147
    32
      -> { deresolver: string list -> Code_Symbol.symbol -> string,
haftmann@39147
    33
           hierarchical_program: ('a, 'b) hierarchical_program }
haftmann@39147
    34
  val print_hierarchical: { print_module: string list -> string -> 'b -> 'c list -> 'c,
haftmann@55147
    35
    print_stmt: string list -> Code_Symbol.symbol * 'a -> 'c,
haftmann@39147
    36
    lift_markup: (Pretty.T -> Pretty.T) -> 'c -> 'c }
haftmann@39147
    37
      -> ('a, 'b) hierarchical_program -> 'c list
haftmann@38970
    38
end;
haftmann@38970
    39
haftmann@38970
    40
structure Code_Namespace : CODE_NAMESPACE =
haftmann@38970
    41
struct
haftmann@38970
    42
haftmann@52138
    43
(** fundamental module name hierarchy **)
haftmann@52138
    44
haftmann@55147
    45
fun lookup_identifier identifiers sym =
haftmann@55147
    46
  Code_Symbol.lookup identifiers sym
haftmann@52138
    47
  |> Option.map (split_last o Long_Name.explode);
haftmann@39055
    48
haftmann@55147
    49
fun force_identifier ctxt fragments_tab force_module identifiers sym =
haftmann@55147
    50
  case lookup_identifier identifiers sym of
haftmann@55147
    51
      NONE => ((the o Symtab.lookup fragments_tab o Code_Symbol.namespace_prefix ctxt) sym, Code_Symbol.base_name sym)
haftmann@53414
    52
    | SOME prefix_name => if null force_module then prefix_name
haftmann@53414
    53
        else (force_module, snd prefix_name);
haftmann@39055
    54
haftmann@55147
    55
fun build_module_namespace ctxt { module_prefix, module_identifiers, reserved } program =
haftmann@39203
    56
  let
haftmann@52138
    57
    fun alias_fragments name = case module_identifiers name
haftmann@39203
    58
     of SOME name' => Long_Name.explode name'
wenzelm@43326
    59
      | NONE => map (fn name => fst (Name.variant name reserved)) (Long_Name.explode name);
haftmann@55147
    60
    val module_names = Code_Symbol.Graph.fold (insert (op =) o Code_Symbol.namespace_prefix ctxt o fst) program [];
haftmann@39203
    61
  in
haftmann@39208
    62
    fold (fn name => Symtab.update (name, Long_Name.explode module_prefix @ alias_fragments name))
haftmann@39203
    63
      module_names Symtab.empty
haftmann@39203
    64
  end;
haftmann@39203
    65
haftmann@39055
    66
haftmann@39208
    67
(** flat program structure **)
haftmann@39208
    68
haftmann@55147
    69
type flat_program = ((string * Code_Thingol.stmt option) Code_Symbol.Graph.T * (string * Code_Symbol.symbol list) list) Graph.T;
haftmann@39208
    70
haftmann@55147
    71
fun flat_program ctxt { module_prefix, module_name, reserved,
haftmann@52138
    72
    identifiers, empty_nsp, namify_stmt, modify_stmt } program =
haftmann@39208
    73
  let
haftmann@39208
    74
haftmann@39208
    75
    (* building module name hierarchy *)
haftmann@52138
    76
    val module_identifiers = if module_name = ""
haftmann@52138
    77
      then Code_Symbol.lookup_module_data identifiers
haftmann@52138
    78
      else K (SOME module_name);
haftmann@55147
    79
    val fragments_tab = build_module_namespace ctxt { module_prefix = module_prefix,
haftmann@52138
    80
      module_identifiers = module_identifiers, reserved = reserved } program;
haftmann@55147
    81
    val prep_sym = force_identifier ctxt fragments_tab (Long_Name.explode module_name) identifiers
haftmann@52138
    82
      #>> Long_Name.implode;
haftmann@39208
    83
haftmann@39208
    84
    (* distribute statements over hierarchy *)
haftmann@55147
    85
    fun add_stmt sym stmt =
haftmann@39208
    86
      let
haftmann@55147
    87
        val (module_name, base) = prep_sym sym;
haftmann@39208
    88
      in
haftmann@55147
    89
        Graph.default_node (module_name, (Code_Symbol.Graph.empty, []))
haftmann@55147
    90
        #> (Graph.map_node module_name o apfst) (Code_Symbol.Graph.new_node (sym, (base, stmt)))
haftmann@39208
    91
      end;
haftmann@55147
    92
    fun add_dependency sym sym' =
haftmann@39208
    93
      let
haftmann@55147
    94
        val (module_name, _) = prep_sym sym;
haftmann@55147
    95
        val (module_name', _) = prep_sym sym';
haftmann@39208
    96
      in if module_name = module_name'
haftmann@55147
    97
        then (Graph.map_node module_name o apfst) (Code_Symbol.Graph.add_edge (sym, sym'))
haftmann@55147
    98
        else (Graph.map_node module_name o apsnd) (AList.map_default (op =) (module_name', []) (insert (op =) sym'))
haftmann@39208
    99
      end;
haftmann@39208
   100
    val proto_program = Graph.empty
haftmann@55147
   101
      |> Code_Symbol.Graph.fold (fn (sym, (stmt, _)) => add_stmt sym stmt) program
haftmann@55147
   102
      |> Code_Symbol.Graph.fold (fn (sym, (_, (_, syms))) =>
haftmann@55147
   103
          Code_Symbol.Graph.Keys.fold (add_dependency sym) syms) program;
haftmann@39208
   104
haftmann@39208
   105
    (* name declarations and statement modifications *)
haftmann@55147
   106
    fun declare sym (base, stmt) (gr, nsp) = 
haftmann@39208
   107
      let
haftmann@39208
   108
        val (base', nsp') = namify_stmt stmt base nsp;
haftmann@55147
   109
        val gr' = (Code_Symbol.Graph.map_node sym o apfst) (K base') gr;
haftmann@39208
   110
      in (gr', nsp') end;
haftmann@39208
   111
    fun declarations gr = (gr, empty_nsp)
haftmann@55147
   112
      |> fold (fn sym => declare sym (Code_Symbol.Graph.get_node gr sym)) (Code_Symbol.Graph.keys gr) 
haftmann@39208
   113
      |> fst
haftmann@55147
   114
      |> (Code_Symbol.Graph.map o K o apsnd) modify_stmt;
haftmann@39208
   115
    val flat_program = proto_program
haftmann@39208
   116
      |> (Graph.map o K o apfst) declarations;
haftmann@39208
   117
haftmann@39208
   118
    (* qualified and unqualified imports, deresolving *)
haftmann@55147
   119
    fun base_deresolver sym = fst (Code_Symbol.Graph.get_node
haftmann@55147
   120
      (fst (Graph.get_node flat_program (fst (prep_sym sym)))) sym);
haftmann@39208
   121
    fun classify_names gr imports =
haftmann@39208
   122
      let
haftmann@39208
   123
        val import_tab = maps
haftmann@55147
   124
          (fn (module_name, syms) => map (rpair module_name) syms) imports;
haftmann@55147
   125
        val imported_syms = map fst import_tab;
haftmann@55147
   126
        val here_syms = Code_Symbol.Graph.keys gr;
haftmann@39208
   127
      in
haftmann@55147
   128
        Code_Symbol.Table.empty
haftmann@55147
   129
        |> fold (fn sym => Code_Symbol.Table.update (sym, base_deresolver sym)) here_syms
haftmann@55147
   130
        |> fold (fn sym => Code_Symbol.Table.update (sym,
haftmann@55147
   131
            Long_Name.append (the (AList.lookup (op =) import_tab sym))
haftmann@55147
   132
              (base_deresolver sym))) imported_syms
haftmann@39208
   133
      end;
haftmann@40630
   134
    val deresolver_tab = Symtab.make (AList.make
haftmann@40630
   135
      (uncurry classify_names o Graph.get_node flat_program)
haftmann@40630
   136
        (Graph.keys flat_program));
haftmann@55147
   137
    fun deresolver "" sym =
haftmann@55147
   138
          Long_Name.append (fst (prep_sym sym)) (base_deresolver sym)
haftmann@55147
   139
      | deresolver module_name sym =
haftmann@55147
   140
          the (Code_Symbol.Table.lookup (the (Symtab.lookup deresolver_tab module_name)) sym)
haftmann@52138
   141
          handle Option.Option => error ("Unknown statement name: "
haftmann@55147
   142
            ^ Code_Symbol.quote ctxt sym);
haftmann@39208
   143
haftmann@39208
   144
  in { deresolver = deresolver, flat_program = flat_program } end;
haftmann@39208
   145
haftmann@39208
   146
haftmann@39055
   147
(** hierarchical program structure **)
haftmann@38970
   148
haftmann@39017
   149
datatype ('a, 'b) node =
haftmann@38970
   150
    Dummy
haftmann@39017
   151
  | Stmt of 'a
haftmann@55147
   152
  | Module of ('b * (string * ('a, 'b) node) Code_Symbol.Graph.T);
haftmann@38970
   153
haftmann@55147
   154
type ('a, 'b) hierarchical_program = (string * ('a, 'b) node) Code_Symbol.Graph.T;
haftmann@39147
   155
haftmann@39018
   156
fun map_module_content f (Module content) = Module (f content);
haftmann@39018
   157
haftmann@39018
   158
fun map_module [] = I
haftmann@39018
   159
  | map_module (name_fragment :: name_fragments) =
haftmann@55147
   160
      apsnd o Code_Symbol.Graph.map_node (Code_Symbol.Module name_fragment) o apsnd o map_module_content
haftmann@39018
   161
        o map_module name_fragments;
haftmann@39018
   162
haftmann@55147
   163
fun hierarchical_program ctxt { module_name, reserved, identifiers, empty_nsp,
haftmann@39023
   164
      namify_module, namify_stmt, cyclic_modules, empty_data, memorize_data, modify_stmts } program =
haftmann@38970
   165
  let
haftmann@38970
   166
haftmann@38970
   167
    (* building module name hierarchy *)
haftmann@52138
   168
    val module_identifiers = if module_name = ""
haftmann@52138
   169
      then Code_Symbol.lookup_module_data identifiers
haftmann@52138
   170
      else K (SOME module_name);
haftmann@55147
   171
    val fragments_tab = build_module_namespace ctxt { module_prefix = "",
haftmann@52138
   172
      module_identifiers = module_identifiers, reserved = reserved } program;
haftmann@55147
   173
    val prep_sym = force_identifier ctxt fragments_tab (Long_Name.explode module_name) identifiers;
haftmann@38970
   174
haftmann@38970
   175
    (* building empty module hierarchy *)
haftmann@55147
   176
    val empty_module = (empty_data, Code_Symbol.Graph.empty);
haftmann@38970
   177
    fun ensure_module name_fragment (data, nodes) =
haftmann@55147
   178
      if can (Code_Symbol.Graph.get_node nodes) (Code_Symbol.Module name_fragment) then (data, nodes)
haftmann@38970
   179
      else (data,
haftmann@55147
   180
        nodes |> Code_Symbol.Graph.new_node (Code_Symbol.Module name_fragment, (name_fragment, Module empty_module)));
haftmann@38970
   181
    fun allocate_module [] = I
haftmann@38970
   182
      | allocate_module (name_fragment :: name_fragments) =
haftmann@38970
   183
          ensure_module name_fragment
haftmann@55147
   184
          #> (apsnd o Code_Symbol.Graph.map_node (Code_Symbol.Module name_fragment) o apsnd o map_module_content o allocate_module) name_fragments;
haftmann@52138
   185
    val empty_program =
haftmann@52138
   186
      empty_module
haftmann@52138
   187
      |> Symtab.fold (fn (_, fragments) => allocate_module fragments) fragments_tab
haftmann@55147
   188
      |> Code_Symbol.Graph.fold (allocate_module o these o Option.map fst
haftmann@55147
   189
          o lookup_identifier identifiers o fst) program;
haftmann@38970
   190
haftmann@38970
   191
    (* distribute statements over hierarchy *)
haftmann@55147
   192
    fun add_stmt sym stmt =
haftmann@38970
   193
      let
haftmann@55147
   194
        val (name_fragments, base) = prep_sym sym;
haftmann@38970
   195
      in
haftmann@55147
   196
        (map_module name_fragments o apsnd) (Code_Symbol.Graph.new_node (sym, (base, Stmt stmt)))
haftmann@38970
   197
      end;
haftmann@55147
   198
    fun add_dependency sym sym' =
haftmann@38970
   199
      let
haftmann@55147
   200
        val (name_fragments, _) = prep_sym sym;
haftmann@55147
   201
        val (name_fragments', _) = prep_sym sym';
haftmann@38970
   202
        val (name_fragments_common, (diff, diff')) =
haftmann@38970
   203
          chop_prefix (op =) (name_fragments, name_fragments');
haftmann@39023
   204
        val is_module = not (null diff andalso null diff');
haftmann@55147
   205
        val dep = pairself hd (map Code_Symbol.Module diff @ [sym], map Code_Symbol.Module diff' @ [sym']);
haftmann@38970
   206
        val add_edge = if is_module andalso not cyclic_modules
haftmann@55147
   207
          then (fn node => Code_Symbol.Graph.add_edge_acyclic dep node
haftmann@38970
   208
            handle Graph.CYCLES _ => error ("Dependency "
haftmann@55147
   209
              ^ Code_Symbol.quote ctxt sym ^ " -> "
haftmann@55147
   210
              ^ Code_Symbol.quote ctxt sym'
haftmann@38970
   211
              ^ " would result in module dependency cycle"))
haftmann@55147
   212
          else Code_Symbol.Graph.add_edge dep
haftmann@39018
   213
      in (map_module name_fragments_common o apsnd) add_edge end;
haftmann@38970
   214
    val proto_program = empty_program
haftmann@55147
   215
      |> Code_Symbol.Graph.fold (fn (sym, (stmt, _)) => add_stmt sym stmt) program
haftmann@55147
   216
      |> Code_Symbol.Graph.fold (fn (sym, (_, (_, syms))) =>
haftmann@55147
   217
          Code_Symbol.Graph.Keys.fold (add_dependency sym) syms) program;
haftmann@38970
   218
haftmann@39022
   219
    (* name declarations, data and statement modifications *)
haftmann@38970
   220
    fun make_declarations nsps (data, nodes) =
haftmann@38970
   221
      let
haftmann@55147
   222
        val (module_fragments, stmt_syms) = List.partition
haftmann@55147
   223
          (fn sym => case Code_Symbol.Graph.get_node nodes sym
haftmann@55147
   224
            of (_, Module _) => true | _ => false) (Code_Symbol.Graph.keys nodes);
haftmann@55147
   225
        fun declare namify sym (nsps, nodes) =
haftmann@38970
   226
          let
haftmann@55147
   227
            val (base, node) = Code_Symbol.Graph.get_node nodes sym;
haftmann@38970
   228
            val (base', nsps') = namify node base nsps;
haftmann@55147
   229
            val nodes' = Code_Symbol.Graph.map_node sym (K (base', node)) nodes;
haftmann@38970
   230
          in (nsps', nodes') end;
haftmann@38970
   231
        val (nsps', nodes') = (nsps, nodes)
haftmann@39022
   232
          |> fold (declare (K namify_module)) module_fragments
haftmann@55147
   233
          |> fold (declare (namify_stmt o (fn Stmt stmt => stmt))) stmt_syms;
haftmann@39029
   234
        fun zip_fillup xs ys = xs ~~ ys @ replicate (length xs - length ys) NONE;
haftmann@55147
   235
        fun select_syms syms = case filter (member (op =) stmt_syms) syms
haftmann@39029
   236
         of [] => NONE
haftmann@55147
   237
          | syms => SOME syms;
haftmann@55147
   238
        val modify_stmts' = AList.make (snd o Code_Symbol.Graph.get_node nodes)
haftmann@39023
   239
          #> split_list
haftmann@39023
   240
          ##> map (fn Stmt stmt => stmt)
haftmann@55147
   241
          #> (fn (syms, stmts) => zip_fillup syms (modify_stmts (syms ~~ stmts)));
haftmann@55147
   242
        val stmtss' = (maps modify_stmts' o map_filter select_syms o Code_Symbol.Graph.strong_conn) nodes;
haftmann@55147
   243
        val nodes'' = Code_Symbol.Graph.map (fn sym => apsnd (fn Module content => Module (make_declarations nsps' content)
haftmann@55147
   244
            | _ => case AList.lookup (op =) stmtss' sym of SOME (SOME stmt) => Stmt stmt | _ => Dummy)) nodes';
haftmann@55147
   245
        val data' = fold memorize_data stmt_syms data;
haftmann@39018
   246
      in (data', nodes'') end;
haftmann@38970
   247
    val (_, hierarchical_program) = make_declarations empty_nsp proto_program;
haftmann@38970
   248
haftmann@38970
   249
    (* deresolving *)
haftmann@55147
   250
    fun deresolver prefix_fragments sym =
haftmann@38970
   251
      let
haftmann@55147
   252
        val (name_fragments, _) = prep_sym sym;
haftmann@38970
   253
        val (_, (_, remainder)) = chop_prefix (op =) (prefix_fragments, name_fragments);
haftmann@55147
   254
        val nodes = fold (fn name_fragment => fn nodes => case Code_Symbol.Graph.get_node nodes (Code_Symbol.Module name_fragment)
haftmann@38970
   255
         of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
haftmann@55147
   256
        val (base', _) = Code_Symbol.Graph.get_node nodes sym;
haftmann@38970
   257
      in Long_Name.implode (remainder @ [base']) end
haftmann@55147
   258
        handle Code_Symbol.Graph.UNDEF _ => error ("Unknown statement name: "
haftmann@55147
   259
          ^ Code_Symbol.quote ctxt sym);
haftmann@38970
   260
haftmann@38970
   261
  in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
haftmann@38970
   262
haftmann@39147
   263
fun print_hierarchical { print_module, print_stmt, lift_markup } =
haftmann@39147
   264
  let
haftmann@39147
   265
    fun print_node _ (_, Dummy) =
haftmann@39147
   266
          NONE
haftmann@55147
   267
      | print_node prefix_fragments (sym, Stmt stmt) =
haftmann@55147
   268
          SOME (lift_markup (Code_Printer.markup_stmt sym)
haftmann@55147
   269
            (print_stmt prefix_fragments (sym, stmt)))
haftmann@55147
   270
      | print_node prefix_fragments (Code_Symbol.Module name_fragment, Module (data, nodes)) =
haftmann@39147
   271
          let
haftmann@39147
   272
            val prefix_fragments' = prefix_fragments @ [name_fragment]
haftmann@39147
   273
          in
haftmann@39147
   274
            Option.map (print_module prefix_fragments'
haftmann@39147
   275
              name_fragment data) (print_nodes prefix_fragments' nodes)
haftmann@39147
   276
          end
haftmann@39147
   277
    and print_nodes prefix_fragments nodes =
haftmann@39147
   278
      let
haftmann@55147
   279
        val xs = (map_filter (fn sym => print_node prefix_fragments
haftmann@55147
   280
          (sym, snd (Code_Symbol.Graph.get_node nodes sym))) o rev o flat o Code_Symbol.Graph.strong_conn) nodes
haftmann@39147
   281
      in if null xs then NONE else SOME xs end;
haftmann@39147
   282
  in these o print_nodes [] end;
haftmann@39147
   283
haftmann@55147
   284
end;