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