Summary: There is no reason to have this in Typ. Reviewed By: skcho Differential Revision: D19161946 fbshipit-source-id: 7d9b4f249master
parent
cef051dd1a
commit
33352623a5
@ -0,0 +1,64 @@
|
|||||||
|
(*
|
||||||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*)
|
||||||
|
|
||||||
|
open! IStd
|
||||||
|
module F = Format
|
||||||
|
|
||||||
|
type t = {class_name: Typ.Name.t; field_name: string} [@@deriving compare, equal]
|
||||||
|
|
||||||
|
let make class_name field_name = {class_name; field_name}
|
||||||
|
|
||||||
|
let get_class_name {class_name} = class_name
|
||||||
|
|
||||||
|
let get_field_name {field_name} = field_name
|
||||||
|
|
||||||
|
let is_java {class_name} = Typ.Name.Java.is_class class_name
|
||||||
|
|
||||||
|
module T = struct
|
||||||
|
type nonrec t = t
|
||||||
|
|
||||||
|
let compare = compare
|
||||||
|
end
|
||||||
|
|
||||||
|
module Set = Caml.Set.Make (T)
|
||||||
|
module Map = Caml.Map.Make (T)
|
||||||
|
|
||||||
|
let join ~sep c f = String.concat ~sep [c; f]
|
||||||
|
|
||||||
|
let dot_join = join ~sep:"."
|
||||||
|
|
||||||
|
let cc_join = join ~sep:"::"
|
||||||
|
|
||||||
|
let to_string fld =
|
||||||
|
if is_java fld then dot_join (Typ.Name.name fld.class_name) fld.field_name else fld.field_name
|
||||||
|
|
||||||
|
|
||||||
|
let to_simplified_string fld =
|
||||||
|
if is_java fld then
|
||||||
|
Typ.Name.name fld.class_name |> String.rsplit2 ~on:'.'
|
||||||
|
|> Option.value_map ~default:fld.field_name ~f:(fun (_, class_only) ->
|
||||||
|
String.concat ~sep:"." [class_only; fld.field_name] )
|
||||||
|
else fld.field_name
|
||||||
|
|
||||||
|
|
||||||
|
let to_full_string fld =
|
||||||
|
(if is_java fld then dot_join else cc_join) (Typ.Name.name fld.class_name) fld.field_name
|
||||||
|
|
||||||
|
|
||||||
|
let pp f fld = F.pp_print_string f fld.field_name
|
||||||
|
|
||||||
|
let is_java_captured_parameter ({field_name} as field) =
|
||||||
|
is_java field && String.is_prefix ~prefix:"val$" field_name
|
||||||
|
|
||||||
|
|
||||||
|
let is_java_outer_instance ({field_name} as field) =
|
||||||
|
is_java field
|
||||||
|
&&
|
||||||
|
let this = "this$" in
|
||||||
|
let last_char = field_name.[String.length field_name - 1] in
|
||||||
|
(last_char >= '0' && last_char <= '9')
|
||||||
|
&& String.is_suffix field_name ~suffix:(this ^ String.of_char last_char)
|
@ -0,0 +1,45 @@
|
|||||||
|
(*
|
||||||
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
||||||
|
*
|
||||||
|
* This source code is licensed under the MIT license found in the
|
||||||
|
* LICENSE file in the root directory of this source tree.
|
||||||
|
*)
|
||||||
|
|
||||||
|
open! IStd
|
||||||
|
module F = Format
|
||||||
|
|
||||||
|
(** Names for fields of class/struct/union *)
|
||||||
|
type t [@@deriving compare, equal]
|
||||||
|
|
||||||
|
val make : Typ.Name.t -> string -> t
|
||||||
|
(** create a field of the given class and fieldname *)
|
||||||
|
|
||||||
|
val get_class_name : t -> Typ.Name.t
|
||||||
|
|
||||||
|
val get_field_name : t -> string
|
||||||
|
|
||||||
|
val is_java : t -> bool
|
||||||
|
|
||||||
|
module Set : Caml.Set.S with type elt = t
|
||||||
|
(** Set for fieldnames *)
|
||||||
|
|
||||||
|
module Map : Caml.Map.S with type key = t
|
||||||
|
(** Map for fieldnames *)
|
||||||
|
|
||||||
|
val is_java_captured_parameter : t -> bool
|
||||||
|
(** Check if field is a captured parameter *)
|
||||||
|
|
||||||
|
val is_java_outer_instance : t -> bool
|
||||||
|
(** Check if the field is the synthetic this$n of a nested class, used to access the n-th outer
|
||||||
|
instance. *)
|
||||||
|
|
||||||
|
val to_string : t -> string
|
||||||
|
(** Convert a field name to a string. *)
|
||||||
|
|
||||||
|
val to_full_string : t -> string
|
||||||
|
|
||||||
|
val to_simplified_string : t -> string
|
||||||
|
(** Convert a fieldname to a simplified string with at most one-level path. *)
|
||||||
|
|
||||||
|
val pp : F.formatter -> t -> unit
|
||||||
|
(** Pretty print a field name. *)
|
Loading…
Reference in new issue