src/Pure/General/name_space.ML
changeset 77953 fcd85e04a948
parent 77952 27b5cb41c253
child 77954 8f3204e28783
equal deleted inserted replaced
77952:27b5cb41c253 77953:fcd85e04a948
   141 fun hide_name name = map_internals (apsnd (update (op =) name)) (Long_Name.make_chunks name);
   141 fun hide_name name = map_internals (apsnd (update (op =) name)) (Long_Name.make_chunks name);
   142 
   142 
   143 
   143 
   144 (* external accesses *)
   144 (* external accesses *)
   145 
   145 
   146 type external =
   146 type external = {aliases: string list, entry: entry};
   147  {accesses: Long_Name.chunks list,
       
   148   aliases: string list,
       
   149   entry: entry};
       
   150 
       
   151 type externals = external Change_Table.T;  (*name -> external*)
   147 type externals = external Change_Table.T;  (*name -> external*)
   152 
   148 
   153 
   149 
   154 (* accesses *)
   150 (* accesses *)
   155 
   151 
   210 fun empty kind = make_name_space (kind, Long_Name.Chunks.empty, Change_Table.empty);
   206 fun empty kind = make_name_space (kind, Long_Name.Chunks.empty, Change_Table.empty);
   211 
   207 
   212 fun kind_of (Name_Space {kind, ...}) = kind;
   208 fun kind_of (Name_Space {kind, ...}) = kind;
   213 fun lookup_internals (Name_Space {internals, ...}) = Long_Name.Chunks.lookup internals;
   209 fun lookup_internals (Name_Space {internals, ...}) = Long_Name.Chunks.lookup internals;
   214 fun lookup_externals (Name_Space {externals, ...}) = Change_Table.lookup externals;
   210 fun lookup_externals (Name_Space {externals, ...}) = Change_Table.lookup externals;
   215 
       
   216 fun get_accesses space name =
       
   217   (case lookup_externals space name of
       
   218     NONE => []
       
   219   | SOME {accesses, ...} => accesses);
       
   220 
   211 
   221 fun get_aliases space name =
   212 fun get_aliases space name =
   222   (case lookup_externals space name of
   213   (case lookup_externals space name of
   223     NONE => [name]
   214     NONE => [name]
   224   | SOME {aliases, ...} => aliases @ [name]);
   215   | SOME {aliases, ...} => aliases @ [name]);
   387       (K (fn ((names1, names1'), (names2, names2')) =>
   378       (K (fn ((names1, names1'), (names2, names2')) =>
   388         if pointer_eq (names1, names2) andalso pointer_eq (names1', names2')
   379         if pointer_eq (names1, names2) andalso pointer_eq (names1', names2')
   389         then raise Long_Name.Chunks.SAME
   380         then raise Long_Name.Chunks.SAME
   390         else (Library.merge (op =) (names1, names2), Library.merge (op =) (names1', names2'))));
   381         else (Library.merge (op =) (names1, names2), Library.merge (op =) (names1', names2'))));
   391     val externals' = (externals1, externals2) |> Change_Table.join (fn name =>
   382     val externals' = (externals1, externals2) |> Change_Table.join (fn name =>
   392       fn ({accesses = accesses1, aliases = aliases1, entry = entry1},
   383       fn ({aliases = aliases1, entry = entry1}, {aliases = aliases2, entry = entry2}) =>
   393           {accesses = accesses2, aliases = aliases2, entry = entry2}) =>
   384         if #serial entry1 <> #serial entry2
   394       if #serial entry1 <> #serial entry2
   385         then err_dup kind' (name, entry1) (name, entry2) Position.none
   395       then err_dup kind' (name, entry1) (name, entry2) Position.none
   386         else
   396       else
   387           let val aliases' = Library.merge (op =) (aliases1, aliases2) in
   397         let
   388             if eq_list (op =) (aliases', aliases1) andalso eq_list (op =) (aliases', aliases2)
   398           val accesses' = Library.merge Long_Name.eq_chunks (accesses1, accesses2);
   389             then raise Change_Table.SAME
   399           val aliases' = Library.merge (op =) (aliases1, aliases2);
   390             else {aliases = aliases', entry = entry1}
   400         in
   391           end);
   401           if eq_list Long_Name.eq_chunks (accesses', accesses1) andalso
       
   402              eq_list Long_Name.eq_chunks (accesses', accesses2) andalso
       
   403              eq_list (op =) (aliases', aliases1) andalso
       
   404              eq_list (op =) (aliases', aliases2)
       
   405           then raise Change_Table.SAME
       
   406           else {accesses = accesses', aliases = aliases', entry = entry1}
       
   407         end);
       
   408   in make_name_space (kind', internals', externals') end;
   392   in make_name_space (kind', internals', externals') end;
   409 
   393 
   410 
   394 
   411 
   395 
   412 (** naming context **)
   396 (** naming context **)
   523 
   507 
   524 fun hide fully name space =
   508 fun hide fully name space =
   525   space |> map_name_space (fn (kind, internals, externals) =>
   509   space |> map_name_space (fn (kind, internals, externals) =>
   526     let
   510     let
   527       val _ = the_entry space name;
   511       val _ = the_entry space name;
   528       val accesses = get_accesses space name;
   512       val accesses = get_aliases space name |> maps (make_accesses o Binding.full_name_spec);
   529       val accesses' = make_accesses' name;
   513       val accesses' = make_accesses' name;
   530       val xnames = filter (is_valid_access space name) accesses;
   514       val xnames = filter (is_valid_access space name) accesses;
   531       val xnames' =
   515       val xnames' =
   532         if fully then xnames
   516         if fully then xnames
   533         else inter Long_Name.eq_chunks [Long_Name.base_chunks name] xnames;
   517         else inter Long_Name.eq_chunks [Long_Name.base_chunks name] xnames;
   547       val name_spec as {full_name = alias_name, ...} = name_spec naming binding;
   531       val name_spec as {full_name = alias_name, ...} = name_spec naming binding;
   548       val more_accs = make_accesses name_spec;
   532       val more_accs = make_accesses name_spec;
   549       val _ = alias_name = "" andalso error (Binding.bad binding);
   533       val _ = alias_name = "" andalso error (Binding.bad binding);
   550 
   534 
   551       val internals' = internals |> fold (add_name name) more_accs;
   535       val internals' = internals |> fold (add_name name) more_accs;
   552       val externals' = externals
   536       val externals' = externals |> Change_Table.map_entry name
   553         |> Change_Table.map_entry name (fn {accesses, aliases, entry} =>
   537         (fn {aliases, entry} => {aliases = update (op =) alias_name aliases, entry = entry});
   554             {accesses = fold_rev (update Long_Name.eq_chunks) more_accs accesses,
       
   555              aliases = update (op =) alias_name aliases,
       
   556              entry = entry});
       
   557     in (kind, internals', externals') end);
   538     in (kind, internals', externals') end);
   558 
   539 
   559 
   540 
   560 
   541 
   561 (** context naming **)
   542 (** context naming **)
   604       space |> map_name_space (fn (kind, internals, externals) =>
   585       space |> map_name_space (fn (kind, internals, externals) =>
   605         let
   586         let
   606           val internals' = internals |> fold (add_name name) accesses;
   587           val internals' = internals |> fold (add_name name) accesses;
   607           val externals' =
   588           val externals' =
   608             (if strict then Change_Table.update_new else Change_Table.update)
   589             (if strict then Change_Table.update_new else Change_Table.update)
   609               (name, {accesses = accesses, aliases = [], entry = entry}) externals
   590               (name, {aliases = [], entry = entry}) externals
   610             handle Change_Table.DUP dup =>
   591             handle Change_Table.DUP dup =>
   611               err_dup kind (dup, #entry (the (lookup_externals space dup)))
   592               err_dup kind (dup, #entry (the (lookup_externals space dup)))
   612                 (name, entry) (#pos entry);
   593                 (name, entry) (#pos entry);
   613         in (kind, internals', externals') end);
   594         in (kind, internals', externals') end);
   614     val _ =
   595     val _ =