src/Tools/Code/code_namespace.ML
author haftmann
Sat Jun 28 22:13:20 2014 +0200 (2014-06-28)
changeset 57428 47c8475e7864
parent 56826 ba18bd41e510
child 58520 a4d1f8041af0
permissions -rw-r--r--
corrected handled exception
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@55681
     9
  datatype export = Private | Opaque | Public
haftmann@55681
    10
  val is_public: export -> bool
haftmann@55681
    11
  val not_private: export -> bool
haftmann@55684
    12
  val join_exports: export list -> export
haftmann@55681
    13
haftmann@39208
    14
  type flat_program
haftmann@55147
    15
  val flat_program: Proof.context
haftmann@52138
    16
    -> { module_prefix: string, module_name: string,
haftmann@52138
    17
    reserved: Name.context, identifiers: Code_Target.identifier_data, empty_nsp: 'a,
haftmann@39208
    18
    namify_stmt: Code_Thingol.stmt -> string -> 'a -> string * 'a,
haftmann@39208
    19
    modify_stmt: Code_Thingol.stmt -> Code_Thingol.stmt option }
haftmann@55683
    20
      -> Code_Symbol.T list -> Code_Thingol.program
haftmann@55150
    21
      -> { deresolver: string -> Code_Symbol.T -> string,
haftmann@39208
    22
           flat_program: flat_program }
haftmann@39208
    23
haftmann@39017
    24
  datatype ('a, 'b) node =
haftmann@38970
    25
      Dummy
haftmann@55681
    26
    | Stmt of export * 'a
haftmann@55147
    27
    | Module of ('b * (string * ('a, 'b) node) Code_Symbol.Graph.T)
haftmann@39208
    28
  type ('a, 'b) hierarchical_program
haftmann@55147
    29
  val hierarchical_program: Proof.context
haftmann@52138
    30
    -> { module_name: string,
haftmann@52138
    31
    reserved: Name.context, identifiers: Code_Target.identifier_data,
haftmann@52138
    32
    empty_nsp: 'c, namify_module: string -> 'c -> string * 'c,
haftmann@39022
    33
    namify_stmt: Code_Thingol.stmt -> string -> 'c -> string * 'c,
haftmann@55684
    34
    cyclic_modules: bool,
haftmann@55684
    35
    class_transitive: bool, class_relation_public: bool,
haftmann@55684
    36
    empty_data: 'b, memorize_data: Code_Symbol.T -> 'b -> 'b,
haftmann@55684
    37
    modify_stmts: (Code_Symbol.T * (export * Code_Thingol.stmt)) list -> (export * 'a) option list }
haftmann@55683
    38
      -> Code_Symbol.T list -> Code_Thingol.program
haftmann@55150
    39
      -> { deresolver: string list -> Code_Symbol.T -> string,
haftmann@39147
    40
           hierarchical_program: ('a, 'b) hierarchical_program }
haftmann@39147
    41
  val print_hierarchical: { print_module: string list -> string -> 'b -> 'c list -> 'c,
haftmann@55681
    42
    print_stmt: string list -> Code_Symbol.T * (export * 'a) -> 'c,
haftmann@39147
    43
    lift_markup: (Pretty.T -> Pretty.T) -> 'c -> 'c }
haftmann@39147
    44
      -> ('a, 'b) hierarchical_program -> 'c list
haftmann@38970
    45
end;
haftmann@38970
    46
haftmann@38970
    47
structure Code_Namespace : CODE_NAMESPACE =
haftmann@38970
    48
struct
haftmann@38970
    49
haftmann@55681
    50
(** export **)
haftmann@55681
    51
haftmann@55681
    52
datatype export = Private | Opaque | Public;
haftmann@55681
    53
haftmann@55681
    54
fun is_public Public = true
haftmann@55681
    55
  | is_public _ = false;
haftmann@55681
    56
haftmann@55681
    57
fun not_private Public = true
haftmann@55681
    58
  | not_private Opaque = true
haftmann@55681
    59
  | not_private _ = false;
haftmann@55681
    60
haftmann@55684
    61
fun mark_export Public _ = Public
haftmann@55684
    62
  | mark_export _ Public = Public
haftmann@55684
    63
  | mark_export Opaque _ = Opaque
haftmann@55684
    64
  | mark_export _ Opaque = Opaque
haftmann@55684
    65
  | mark_export _ _ = Private;
haftmann@55684
    66
haftmann@55684
    67
fun join_exports exports = fold mark_export exports Private;
haftmann@55684
    68
haftmann@55684
    69
fun dependent_exports { program = program, class_transitive = class_transitive } =
haftmann@55684
    70
  let
haftmann@55684
    71
    fun is_datatype_or_class (Code_Symbol.Type_Constructor _) = true
haftmann@55684
    72
      | is_datatype_or_class (Code_Symbol.Type_Class _) = true
haftmann@55684
    73
      | is_datatype_or_class _ = false;
haftmann@55684
    74
    fun is_relevant (Code_Symbol.Class_Relation _) = true
haftmann@55684
    75
      | is_relevant sym = is_datatype_or_class sym;
haftmann@55684
    76
    val proto_gr = Code_Symbol.Graph.restrict is_relevant program;
haftmann@55684
    77
    val gr =
haftmann@55684
    78
      proto_gr
haftmann@55684
    79
      |> Code_Symbol.Graph.fold
haftmann@55684
    80
          (fn (sym, (_, (_, deps))) =>
haftmann@55684
    81
            if is_relevant sym
haftmann@55684
    82
            then I
haftmann@55684
    83
            else
haftmann@55684
    84
              Code_Symbol.Graph.new_node (sym, Code_Thingol.NoStmt)
haftmann@55684
    85
              #> Code_Symbol.Graph.Keys.fold
haftmann@55684
    86
               (fn sym' =>
haftmann@55684
    87
                if is_relevant sym'
haftmann@55684
    88
                then Code_Symbol.Graph.add_edge (sym, sym')
haftmann@55684
    89
                else I) deps) program
haftmann@55684
    90
      |> class_transitive ?
haftmann@55684
    91
          Code_Symbol.Graph.fold (fn (sym as Code_Symbol.Type_Class _, _) =>
haftmann@55684
    92
            fold (curry Code_Symbol.Graph.add_edge sym)
haftmann@55684
    93
              ((remove (op =) sym o Code_Symbol.Graph.all_succs proto_gr) [sym]) | _ => I) proto_gr
haftmann@55684
    94
    fun deps_of sym =
haftmann@55684
    95
      let
haftmann@55684
    96
        val succs = Code_Symbol.Graph.Keys.dest o Code_Symbol.Graph.imm_succs gr;
haftmann@55684
    97
        val deps1 = succs sym;
haftmann@55776
    98
        val deps2 = [] |> fold (union (op =)) (map succs deps1) |> subtract (op =) deps1
haftmann@55684
    99
      in (deps1, deps2) end;
haftmann@55684
   100
  in
haftmann@55684
   101
    { is_datatype_or_class = is_datatype_or_class,
haftmann@55684
   102
      deps_of = deps_of }
haftmann@55684
   103
  end;
haftmann@55684
   104
haftmann@55684
   105
fun mark_exports_aux { program = program, prefix_of = prefix_of, map_export = map_export,
haftmann@55684
   106
    is_datatype_or_class = is_datatype_or_class, deps_of = deps_of,
haftmann@55684
   107
    class_relation_public = class_relation_public } prefix sym =
haftmann@55684
   108
  let
haftmann@55684
   109
    val export = (if is_datatype_or_class sym then Opaque else Public);
haftmann@55684
   110
    val (dependent_export1, dependent_export2) =
haftmann@55684
   111
      case Code_Symbol.Graph.get_node program sym of
haftmann@55684
   112
          Code_Thingol.Fun _ => (SOME Opaque, NONE)
haftmann@55684
   113
        | Code_Thingol.Classinst _ => (SOME Opaque, NONE)
haftmann@55684
   114
        | Code_Thingol.Datatypecons _ => (SOME Public, SOME Opaque)
haftmann@55684
   115
        | Code_Thingol.Classparam _ => (SOME Public, SOME Opaque)
haftmann@55776
   116
        | Code_Thingol.Class _ => (SOME Opaque, NONE)
haftmann@55684
   117
        | Code_Thingol.Classrel _ =>
haftmann@55684
   118
           (if class_relation_public
haftmann@55684
   119
            then (SOME Public, SOME Opaque)
haftmann@55684
   120
            else (SOME Opaque, NONE))
haftmann@55684
   121
        | _ => (NONE, NONE);
haftmann@55684
   122
    val dependent_exports =
haftmann@55684
   123
      case dependent_export1 of
haftmann@55684
   124
        SOME export1 => (case dependent_export2 of
haftmann@55684
   125
          SOME export2 =>
haftmann@55684
   126
            let
haftmann@55684
   127
              val (deps1, deps2) = deps_of sym
haftmann@55684
   128
            in map (rpair export1) deps1 @ map (rpair export2) deps2 end
haftmann@55684
   129
        | NONE => map (rpair export1) (fst (deps_of sym)))
haftmann@55684
   130
      | NONE => [];
haftmann@55684
   131
  in 
haftmann@55684
   132
    map_export prefix sym (mark_export export)
haftmann@55684
   133
    #> fold (fn (sym, export) => map_export (prefix_of sym) sym (mark_export export))
haftmann@55684
   134
      dependent_exports
haftmann@55684
   135
  end;
haftmann@55684
   136
haftmann@55684
   137
fun mark_exports { program = program, prefix_of = prefix_of, map_export = map_export,
haftmann@55684
   138
    class_transitive = class_transitive, class_relation_public = class_relation_public } =
haftmann@55684
   139
  let
haftmann@55684
   140
    val { is_datatype_or_class, deps_of } =
haftmann@55684
   141
      dependent_exports { program = program, class_transitive = class_transitive };
haftmann@55684
   142
  in
haftmann@55684
   143
    mark_exports_aux { program = program, prefix_of = prefix_of, map_export = map_export,
haftmann@55684
   144
      is_datatype_or_class = is_datatype_or_class, deps_of = deps_of,
haftmann@55684
   145
      class_relation_public = class_relation_public }
haftmann@55684
   146
  end;
haftmann@55684
   147
haftmann@55681
   148
haftmann@52138
   149
(** fundamental module name hierarchy **)
haftmann@52138
   150
haftmann@55291
   151
fun module_fragments' { identifiers, reserved } name =
haftmann@55291
   152
  case Code_Symbol.lookup_module_data identifiers name of
haftmann@55291
   153
      SOME (fragments, _) => fragments
haftmann@55291
   154
    | NONE => map (fn fragment => fst (Name.variant fragment reserved)) (Long_Name.explode name);
haftmann@55291
   155
haftmann@55291
   156
fun module_fragments { module_name, identifiers, reserved } =
haftmann@55291
   157
  if module_name = ""
haftmann@55291
   158
  then module_fragments' { identifiers = identifiers, reserved = reserved }
haftmann@55291
   159
  else K (Long_Name.explode module_name);
haftmann@39055
   160
haftmann@56826
   161
fun build_module_namespace ctxt enforce_upper { module_prefix, module_name, identifiers, reserved } program =
haftmann@55291
   162
  let
haftmann@55291
   163
    val module_names = Code_Symbol.Graph.fold (insert (op =) o Code_Symbol.default_prefix ctxt o fst) program [];
haftmann@55291
   164
    val module_fragments' = module_fragments
haftmann@55291
   165
      { module_name = module_name, identifiers = identifiers, reserved = reserved };
haftmann@56826
   166
    val adjust_case = if enforce_upper then map (Name.enforce_case true) else I;
haftmann@55291
   167
  in
haftmann@56826
   168
    fold (fn name => Symtab.update (name, adjust_case (Long_Name.explode module_prefix @ module_fragments' name)))
haftmann@55291
   169
      module_names Symtab.empty
haftmann@55291
   170
  end;
haftmann@55291
   171
haftmann@55292
   172
fun prep_symbol ctxt { module_namespace, force_module, identifiers } sym =
haftmann@55291
   173
  case Code_Symbol.lookup identifiers sym of
haftmann@55292
   174
      NONE => ((the o Symtab.lookup module_namespace o Code_Symbol.default_prefix ctxt) sym,
haftmann@55292
   175
        Code_Symbol.default_base sym)
haftmann@53414
   176
    | SOME prefix_name => if null force_module then prefix_name
haftmann@53414
   177
        else (force_module, snd prefix_name);
haftmann@39055
   178
haftmann@55293
   179
fun has_priority identifiers = is_some o Code_Symbol.lookup identifiers;
haftmann@55293
   180
haftmann@55292
   181
fun build_proto_program { empty, add_stmt, add_dep } program =
haftmann@55292
   182
  empty
haftmann@55292
   183
  |> Code_Symbol.Graph.fold (fn (sym, (stmt, _)) => add_stmt sym stmt) program
haftmann@55292
   184
  |> Code_Symbol.Graph.fold (fn (sym, (_, (_, syms))) =>
haftmann@55292
   185
      Code_Symbol.Graph.Keys.fold (add_dep sym) syms) program;
haftmann@55292
   186
haftmann@55293
   187
fun prioritize has_priority = uncurry append o List.partition has_priority;
haftmann@55293
   188
haftmann@39055
   189
haftmann@39208
   190
(** flat program structure **)
haftmann@39208
   191
haftmann@55681
   192
type flat_program = ((string * (export * Code_Thingol.stmt) option) Code_Symbol.Graph.T * (string * Code_Symbol.T list) list) Graph.T;
haftmann@39208
   193
haftmann@55147
   194
fun flat_program ctxt { module_prefix, module_name, reserved,
haftmann@55683
   195
    identifiers, empty_nsp, namify_stmt, modify_stmt } exports program =
haftmann@39208
   196
  let
haftmann@39208
   197
haftmann@39208
   198
    (* building module name hierarchy *)
haftmann@56826
   199
    val module_namespace = build_module_namespace ctxt true { module_prefix = module_prefix,
haftmann@55291
   200
      module_name = module_name, identifiers = identifiers, reserved = reserved } program;
haftmann@55292
   201
    val prep_sym = prep_symbol ctxt { module_namespace = module_namespace,
haftmann@55292
   202
      force_module = Long_Name.explode module_name, identifiers = identifiers }
haftmann@52138
   203
      #>> Long_Name.implode;
haftmann@55293
   204
    val sym_priority = has_priority identifiers;
haftmann@39208
   205
haftmann@39208
   206
    (* distribute statements over hierarchy *)
haftmann@55684
   207
    val mark_exports = mark_exports { program = program, prefix_of = fst o prep_sym,
haftmann@55684
   208
      map_export = fn module_name => fn sym =>
haftmann@55684
   209
        Graph.map_node module_name o apfst o Code_Symbol.Graph.map_node sym o apsnd o apfst,
haftmann@55684
   210
        class_transitive = false, class_relation_public = false };
haftmann@55147
   211
    fun add_stmt sym stmt =
haftmann@39208
   212
      let
haftmann@55147
   213
        val (module_name, base) = prep_sym sym;
haftmann@39208
   214
      in
haftmann@55147
   215
        Graph.default_node (module_name, (Code_Symbol.Graph.empty, []))
haftmann@55680
   216
        #> (Graph.map_node module_name o apfst)
haftmann@55684
   217
          (Code_Symbol.Graph.new_node (sym, (base, (if null exports then Public else Private, stmt))))
haftmann@39208
   218
      end;
haftmann@55292
   219
    fun add_dep sym sym' =
haftmann@39208
   220
      let
haftmann@55147
   221
        val (module_name, _) = prep_sym sym;
haftmann@55147
   222
        val (module_name', _) = prep_sym sym';
haftmann@39208
   223
      in if module_name = module_name'
haftmann@55147
   224
        then (Graph.map_node module_name o apfst) (Code_Symbol.Graph.add_edge (sym, sym'))
haftmann@55680
   225
        else (Graph.map_node module_name o apsnd)
haftmann@55680
   226
          (AList.map_default (op =) (module_name', []) (insert (op =) sym'))
haftmann@55684
   227
          #> mark_exports module_name' sym'
haftmann@39208
   228
      end;
haftmann@55292
   229
    val proto_program = build_proto_program
haftmann@55684
   230
      { empty = Graph.empty, add_stmt = add_stmt, add_dep = add_dep } program
haftmann@55684
   231
      |> fold (fn sym => mark_exports ((fst o prep_sym) sym) sym) exports;
haftmann@39208
   232
haftmann@39208
   233
    (* name declarations and statement modifications *)
haftmann@55679
   234
    fun declare sym (base, (_, stmt)) (gr, nsp) = 
haftmann@39208
   235
      let
haftmann@39208
   236
        val (base', nsp') = namify_stmt stmt base nsp;
haftmann@55147
   237
        val gr' = (Code_Symbol.Graph.map_node sym o apfst) (K base') gr;
haftmann@39208
   238
      in (gr', nsp') end;
haftmann@39208
   239
    fun declarations gr = (gr, empty_nsp)
haftmann@55293
   240
      |> fold (fn sym => declare sym (Code_Symbol.Graph.get_node gr sym))
haftmann@55293
   241
          (prioritize sym_priority (Code_Symbol.Graph.keys gr))
haftmann@39208
   242
      |> fst
haftmann@55680
   243
      |> Code_Symbol.Graph.map_strong_conn (fn syms_bases_exports_stmts =>
haftmann@55680
   244
        map snd syms_bases_exports_stmts
haftmann@55680
   245
        |> (map o apsnd) (fn (export, stmt) => Option.map (pair export) (modify_stmt stmt)));
haftmann@39208
   246
    val flat_program = proto_program
haftmann@39208
   247
      |> (Graph.map o K o apfst) declarations;
haftmann@39208
   248
haftmann@39208
   249
    (* qualified and unqualified imports, deresolving *)
haftmann@55147
   250
    fun base_deresolver sym = fst (Code_Symbol.Graph.get_node
haftmann@55147
   251
      (fst (Graph.get_node flat_program (fst (prep_sym sym)))) sym);
haftmann@39208
   252
    fun classify_names gr imports =
haftmann@39208
   253
      let
haftmann@39208
   254
        val import_tab = maps
haftmann@55147
   255
          (fn (module_name, syms) => map (rpair module_name) syms) imports;
haftmann@55147
   256
        val imported_syms = map fst import_tab;
haftmann@55147
   257
        val here_syms = Code_Symbol.Graph.keys gr;
haftmann@39208
   258
      in
haftmann@55147
   259
        Code_Symbol.Table.empty
haftmann@55147
   260
        |> fold (fn sym => Code_Symbol.Table.update (sym, base_deresolver sym)) here_syms
haftmann@55147
   261
        |> fold (fn sym => Code_Symbol.Table.update (sym,
haftmann@55147
   262
            Long_Name.append (the (AList.lookup (op =) import_tab sym))
haftmann@55147
   263
              (base_deresolver sym))) imported_syms
haftmann@39208
   264
      end;
haftmann@40630
   265
    val deresolver_tab = Symtab.make (AList.make
haftmann@40630
   266
      (uncurry classify_names o Graph.get_node flat_program)
haftmann@40630
   267
        (Graph.keys flat_program));
haftmann@55147
   268
    fun deresolver "" sym =
haftmann@55147
   269
          Long_Name.append (fst (prep_sym sym)) (base_deresolver sym)
haftmann@55147
   270
      | deresolver module_name sym =
haftmann@55147
   271
          the (Code_Symbol.Table.lookup (the (Symtab.lookup deresolver_tab module_name)) sym)
haftmann@52138
   272
          handle Option.Option => error ("Unknown statement name: "
haftmann@55147
   273
            ^ Code_Symbol.quote ctxt sym);
haftmann@39208
   274
haftmann@39208
   275
  in { deresolver = deresolver, flat_program = flat_program } end;
haftmann@39208
   276
haftmann@39208
   277
haftmann@39055
   278
(** hierarchical program structure **)
haftmann@38970
   279
haftmann@39017
   280
datatype ('a, 'b) node =
haftmann@38970
   281
    Dummy
haftmann@55681
   282
  | Stmt of export * 'a
haftmann@55147
   283
  | Module of ('b * (string * ('a, 'b) node) Code_Symbol.Graph.T);
haftmann@38970
   284
haftmann@55147
   285
type ('a, 'b) hierarchical_program = (string * ('a, 'b) node) Code_Symbol.Graph.T;
haftmann@39147
   286
haftmann@55679
   287
fun the_stmt (Stmt (export, stmt)) = (export, stmt);
haftmann@55679
   288
haftmann@39018
   289
fun map_module_content f (Module content) = Module (f content);
haftmann@39018
   290
haftmann@39018
   291
fun map_module [] = I
haftmann@39018
   292
  | map_module (name_fragment :: name_fragments) =
haftmann@55147
   293
      apsnd o Code_Symbol.Graph.map_node (Code_Symbol.Module name_fragment) o apsnd o map_module_content
haftmann@39018
   294
        o map_module name_fragments;
haftmann@39018
   295
haftmann@55680
   296
fun map_module_stmts f_module f_stmts sym_base_nodes =
haftmann@55680
   297
  let
haftmann@55680
   298
    val some_modules =
haftmann@55680
   299
      sym_base_nodes
haftmann@55684
   300
      |> map (fn (_, (base, Module content)) => SOME (base, content) | _ => NONE)
haftmann@55680
   301
      |> (burrow_options o map o apsnd) f_module;
haftmann@55680
   302
    val some_export_stmts =
haftmann@55680
   303
      sym_base_nodes
haftmann@55680
   304
      |> map (fn (sym, (base, Stmt export_stmt)) => SOME ((sym, export_stmt), base) | _ => NONE)
haftmann@55680
   305
      |> (burrow_options o burrow_fst) (fn [] => [] | xs => f_stmts xs)
haftmann@55680
   306
  in
haftmann@55680
   307
    map2 (fn SOME (base, content) => (K (base, Module content))
haftmann@55680
   308
      | NONE => fn SOME (some_export_stmt, base) =>
haftmann@55680
   309
          (base, case some_export_stmt of SOME export_stmt => Stmt export_stmt | NONE => Dummy))
haftmann@55680
   310
      some_modules some_export_stmts
haftmann@55680
   311
  end;
haftmann@55680
   312
haftmann@55147
   313
fun hierarchical_program ctxt { module_name, reserved, identifiers, empty_nsp,
haftmann@55684
   314
      namify_module, namify_stmt, cyclic_modules,
haftmann@55684
   315
      class_transitive, class_relation_public,
haftmann@55684
   316
      empty_data, memorize_data, modify_stmts }
haftmann@55683
   317
      exports program =
haftmann@38970
   318
  let
haftmann@38970
   319
haftmann@38970
   320
    (* building module name hierarchy *)
haftmann@56826
   321
    val module_namespace = build_module_namespace ctxt false { module_prefix = "",
haftmann@55291
   322
      module_name = module_name, identifiers = identifiers, reserved = reserved } program;
haftmann@55292
   323
    val prep_sym = prep_symbol ctxt { module_namespace = module_namespace,
haftmann@55293
   324
      force_module = Long_Name.explode module_name, identifiers = identifiers }
haftmann@55293
   325
    val sym_priority = has_priority identifiers;
haftmann@38970
   326
haftmann@38970
   327
    (* building empty module hierarchy *)
haftmann@55147
   328
    val empty_module = (empty_data, Code_Symbol.Graph.empty);
haftmann@38970
   329
    fun ensure_module name_fragment (data, nodes) =
haftmann@55147
   330
      if can (Code_Symbol.Graph.get_node nodes) (Code_Symbol.Module name_fragment) then (data, nodes)
haftmann@38970
   331
      else (data,
haftmann@55147
   332
        nodes |> Code_Symbol.Graph.new_node (Code_Symbol.Module name_fragment, (name_fragment, Module empty_module)));
haftmann@38970
   333
    fun allocate_module [] = I
haftmann@38970
   334
      | allocate_module (name_fragment :: name_fragments) =
haftmann@38970
   335
          ensure_module name_fragment
haftmann@55147
   336
          #> (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
   337
    val empty_program =
haftmann@52138
   338
      empty_module
haftmann@55291
   339
      |> Symtab.fold (fn (_, fragments) => allocate_module fragments) module_namespace
haftmann@55147
   340
      |> Code_Symbol.Graph.fold (allocate_module o these o Option.map fst
haftmann@55291
   341
          o Code_Symbol.lookup identifiers o fst) program;
haftmann@38970
   342
haftmann@38970
   343
    (* distribute statements over hierarchy *)
haftmann@55684
   344
    val mark_exports = mark_exports { program = program, prefix_of = fst o prep_sym,
haftmann@55684
   345
      map_export = fn name_fragments => fn sym => fn f =>
haftmann@55684
   346
        (map_module name_fragments o apsnd o Code_Symbol.Graph.map_node sym o apsnd)
haftmann@55684
   347
          (fn Stmt (export, stmt) => Stmt (f export, stmt)),
haftmann@55684
   348
      class_transitive = class_transitive, class_relation_public = class_relation_public };
haftmann@55147
   349
    fun add_stmt sym stmt =
haftmann@38970
   350
      let
haftmann@55147
   351
        val (name_fragments, base) = prep_sym sym;
haftmann@38970
   352
      in
haftmann@55680
   353
        (map_module name_fragments o apsnd)
haftmann@55684
   354
          (Code_Symbol.Graph.new_node (sym, (base, Stmt (if null exports then Public else Private, stmt))))
haftmann@38970
   355
      end;
haftmann@55680
   356
    fun add_edge_acyclic_error error_msg dep gr =
haftmann@55680
   357
      Code_Symbol.Graph.add_edge_acyclic dep gr
haftmann@57428
   358
        handle Code_Symbol.Graph.CYCLES _ => error (error_msg ())
haftmann@55292
   359
    fun add_dep sym sym' =
haftmann@38970
   360
      let
haftmann@55147
   361
        val (name_fragments, _) = prep_sym sym;
haftmann@55147
   362
        val (name_fragments', _) = prep_sym sym';
haftmann@38970
   363
        val (name_fragments_common, (diff, diff')) =
haftmann@38970
   364
          chop_prefix (op =) (name_fragments, name_fragments');
haftmann@55680
   365
        val is_cross_module = not (null diff andalso null diff');
haftmann@55147
   366
        val dep = pairself hd (map Code_Symbol.Module diff @ [sym], map Code_Symbol.Module diff' @ [sym']);
haftmann@55680
   367
        val add_edge = if is_cross_module andalso not cyclic_modules
haftmann@55680
   368
          then add_edge_acyclic_error (fn _ => "Dependency "
haftmann@55680
   369
            ^ Code_Symbol.quote ctxt sym ^ " -> "
haftmann@55680
   370
            ^ Code_Symbol.quote ctxt sym'
haftmann@55680
   371
            ^ " would result in module dependency cycle") dep
haftmann@55680
   372
          else Code_Symbol.Graph.add_edge dep;
haftmann@55684
   373
      in
haftmann@55684
   374
        (map_module name_fragments_common o apsnd) add_edge
haftmann@55684
   375
        #> (if is_cross_module then mark_exports name_fragments' sym' else I)
haftmann@55684
   376
      end;
haftmann@55292
   377
    val proto_program = build_proto_program
haftmann@55684
   378
      { empty = empty_program, add_stmt = add_stmt, add_dep = add_dep } program
haftmann@55684
   379
      |> fold (fn sym => mark_exports ((fst o prep_sym) sym) sym) exports;
haftmann@38970
   380
haftmann@39022
   381
    (* name declarations, data and statement modifications *)
haftmann@38970
   382
    fun make_declarations nsps (data, nodes) =
haftmann@38970
   383
      let
haftmann@55293
   384
        val (module_fragments, stmt_syms) =
haftmann@55293
   385
          Code_Symbol.Graph.keys nodes
haftmann@55293
   386
          |> List.partition
haftmann@55293
   387
              (fn sym => case Code_Symbol.Graph.get_node nodes sym
haftmann@55293
   388
                of (_, Module _) => true | _ => false)
haftmann@55293
   389
          |> pairself (prioritize sym_priority)
haftmann@55147
   390
        fun declare namify sym (nsps, nodes) =
haftmann@38970
   391
          let
haftmann@55147
   392
            val (base, node) = Code_Symbol.Graph.get_node nodes sym;
haftmann@38970
   393
            val (base', nsps') = namify node base nsps;
haftmann@55147
   394
            val nodes' = Code_Symbol.Graph.map_node sym (K (base', node)) nodes;
haftmann@38970
   395
          in (nsps', nodes') end;
haftmann@38970
   396
        val (nsps', nodes') = (nsps, nodes)
haftmann@39022
   397
          |> fold (declare (K namify_module)) module_fragments
haftmann@55679
   398
          |> fold (declare (namify_stmt o snd o the_stmt)) stmt_syms;
haftmann@55680
   399
        fun modify_stmts' syms_stmts =
haftmann@55605
   400
          let
haftmann@55680
   401
            val stmts' = modify_stmts syms_stmts
haftmann@55680
   402
          in stmts' @ replicate (length syms_stmts - length stmts') NONE end;
haftmann@55680
   403
        val nodes'' =
haftmann@55680
   404
          nodes'
haftmann@55684
   405
          |> Code_Symbol.Graph.map_strong_conn (map_module_stmts (make_declarations nsps') modify_stmts');
haftmann@55147
   406
        val data' = fold memorize_data stmt_syms data;
haftmann@39018
   407
      in (data', nodes'') end;
haftmann@38970
   408
    val (_, hierarchical_program) = make_declarations empty_nsp proto_program;
haftmann@38970
   409
haftmann@38970
   410
    (* deresolving *)
haftmann@55147
   411
    fun deresolver prefix_fragments sym =
haftmann@38970
   412
      let
haftmann@55147
   413
        val (name_fragments, _) = prep_sym sym;
haftmann@38970
   414
        val (_, (_, remainder)) = chop_prefix (op =) (prefix_fragments, name_fragments);
haftmann@55147
   415
        val nodes = fold (fn name_fragment => fn nodes => case Code_Symbol.Graph.get_node nodes (Code_Symbol.Module name_fragment)
haftmann@38970
   416
         of (_, Module (_, nodes)) => nodes) name_fragments hierarchical_program;
haftmann@55147
   417
        val (base', _) = Code_Symbol.Graph.get_node nodes sym;
haftmann@38970
   418
      in Long_Name.implode (remainder @ [base']) end
haftmann@55147
   419
        handle Code_Symbol.Graph.UNDEF _ => error ("Unknown statement name: "
haftmann@55147
   420
          ^ Code_Symbol.quote ctxt sym);
haftmann@38970
   421
haftmann@38970
   422
  in { deresolver = deresolver, hierarchical_program = hierarchical_program } end;
haftmann@38970
   423
haftmann@39147
   424
fun print_hierarchical { print_module, print_stmt, lift_markup } =
haftmann@39147
   425
  let
haftmann@39147
   426
    fun print_node _ (_, Dummy) =
haftmann@39147
   427
          NONE
haftmann@55147
   428
      | print_node prefix_fragments (sym, Stmt stmt) =
haftmann@55147
   429
          SOME (lift_markup (Code_Printer.markup_stmt sym)
haftmann@55147
   430
            (print_stmt prefix_fragments (sym, stmt)))
haftmann@55147
   431
      | print_node prefix_fragments (Code_Symbol.Module name_fragment, Module (data, nodes)) =
haftmann@39147
   432
          let
haftmann@39147
   433
            val prefix_fragments' = prefix_fragments @ [name_fragment]
haftmann@39147
   434
          in
haftmann@39147
   435
            Option.map (print_module prefix_fragments'
haftmann@39147
   436
              name_fragment data) (print_nodes prefix_fragments' nodes)
haftmann@39147
   437
          end
haftmann@39147
   438
    and print_nodes prefix_fragments nodes =
haftmann@39147
   439
      let
haftmann@55147
   440
        val xs = (map_filter (fn sym => print_node prefix_fragments
haftmann@55147
   441
          (sym, snd (Code_Symbol.Graph.get_node nodes sym))) o rev o flat o Code_Symbol.Graph.strong_conn) nodes
haftmann@39147
   442
      in if null xs then NONE else SOME xs end;
haftmann@39147
   443
  in these o print_nodes [] end;
haftmann@39147
   444
haftmann@55147
   445
end;