summaryrefslogtreecommitdiff
path: root/src/Names.ml
diff options
context:
space:
mode:
authorSon Ho2022-03-03 12:13:02 +0100
committerSon Ho2022-03-03 12:13:02 +0100
commit4581c76ece9ba75dbd15e550c71d003ae2871901 (patch)
treeec941dba5f29abc087a2737bb5e5528bdfbfe229 /src/Names.ml
parent449c2cef40e1e255666c5564476007027b1a9b21 (diff)
Update the name definition to use path_elem
Diffstat (limited to 'src/Names.ml')
-rw-r--r--src/Names.ml93
1 files changed, 70 insertions, 23 deletions
diff --git a/src/Names.ml b/src/Names.ml
index fff98be9..054c84ad 100644
--- a/src/Names.ml
+++ b/src/Names.ml
@@ -1,39 +1,86 @@
open Identifiers
-type name = string list [@@deriving show, ord]
+module Disambiguator = IdGen ()
+
+(** See the comments for [Name] *)
+type path_elem = Ident of string | Disambiguator of Disambiguator.id
+[@@deriving show, ord]
+
+type name = path_elem list [@@deriving show, ord]
(** A name such as: `std::collections::vector` (which would be represented as
- [["std"; "collections"; "vector"]]) *)
+ [[Ident "std"; Ident "collections"; Ident "vector"]])
+
-(* TODO: remove? *)
-module NameOrderedType : C.OrderedType with type t = name = struct
- type t = name
+ A name really is a list of strings. However, we sometimes need to
+ introduce unique indices to disambiguate. This mostly happens because
+ of "impl" blocks in Rust:
+ ```
+ impl<T> List<T> {
+ ...
+ }
+ ```
+
+ A type in Rust can have several "impl" blocks, and those blocks can
+ contain items with similar names. For this reason, we need to disambiguate
+ them with unique indices. Rustc calls those "disambiguators". In rustc, this
+ gives names like this:
+ - `betree_main::betree::NodeIdCounter{impl#0}::new`
+ - note that impl blocks can be nested, and macros sometimes generate
+ weird names (which require disambiguation):
+ `betree_main::betree_utils::_#1::{impl#0}::deserialize::{impl#0}`
+
+ Finally, the paths used by rustc are a lot more precise and explicit than
+ those we expose in LLBC: for instance, every identifier belongs to a specific
+ namespace (value namespace, type namespace, etc.), and is coupled with a
+ disambiguator.
+
+ On our side, we want to stay high-level and simple: we use string identifiers
+ as much as possible, insert disambiguators only when necessary (whenever
+ we find an "impl" block, typically) and check that the disambiguator is useless
+ in the other situations (i.e., the disambiguator is always equal to 0).
+
+ Moreover, the items are uniquely disambiguated by their (integer) ids
+ (`TypeDeclId.id`, etc.), and when extracting the code we have to deal with
+ name clashes anyway. Still, we might want to be more precise in the future.
+
+ Also note that the first path element in the name is always the crate name.
+ *)
- let compare = compare_name
+let to_name (ls : string list) : name = List.map (fun s -> Ident s) ls
- let to_string = String.concat "::"
+(*(* TODO: remove? *)
+ module NameOrderedType : C.OrderedType with type t = name = struct
+ type t = name
- let pp_t = pp_name
+ let compare = compare_name
- let show_t = show_name
-end
+ let to_string = String.concat "::"
-module NameMap = C.MakeMap (NameOrderedType)
-module NameSet = C.MakeSet (NameOrderedType)
+ let pp_t = pp_name
+
+ let show_t = show_name
+ end
+
+ module NameMap = C.MakeMap (NameOrderedType)
+ module NameSet = C.MakeSet (NameOrderedType)
+*)
type module_name = name [@@deriving show, ord]
type type_name = name [@@deriving show, ord]
-module ImplId = IdGen ()
+type fun_name = name [@@deriving show, ord]
-(** A function name *)
-type fun_name =
- | Regular of name (** "Regular" function name *)
- | Impl of type_name * ImplId.id * string
- (** The function comes from an "impl" block.
+(*module ImplId = IdGen ()
- As we may have several "impl" blocks for one type, we need to use
- a block id to disambiguate the functions (in rustc, this identifier
- is called a "disambiguator").
- *)
-[@@deriving show, ord]
+ (** A function name *)
+ type fun_name =
+ | Regular of name (** "Regular" function name *)
+ | Impl of type_name * ImplId.id * string
+ (** The function comes from an "impl" block.
+
+ As we may have several "impl" blocks for one type, we need to use
+ a block id to disambiguate the functions (in rustc, this identifier
+ is called a "disambiguator").
+ *)
+ [@@deriving show, ord]*)