diff --git a/infer/src/IR/Typ.ml b/infer/src/IR/Typ.ml index ea46e86ce..2e5b97ee6 100644 --- a/infer/src/IR/Typ.ml +++ b/infer/src/IR/Typ.ml @@ -1420,12 +1420,9 @@ module Procname = struct end module Fieldname = struct - type t = - | Clang of {class_name: Name.t; field_name: string} - | Java of {class_name: string; field_name: string} - [@@deriving compare, equal] + type t = {class_name: Name.t; field_name: string} [@@deriving compare, equal] - let is_java = function Java _ -> true | Clang _ -> false + let is_java {class_name} = Name.Java.is_class class_name module T = struct type nonrec t = t @@ -1436,87 +1433,72 @@ module Fieldname = struct module Set = Caml.Set.Make (T) module Map = Caml.Map.Make (T) - let dot_join s1 s2 = String.concat ~sep:"." [s1; s2] + let join ~sep c f = String.concat ~sep [c; f] - let to_string = function - | Java {class_name; field_name} when String.is_empty class_name -> - field_name - | Java {class_name; field_name} -> - dot_join class_name field_name - | Clang {field_name} -> - field_name + let dot_join = join ~sep:"." + let cc_join = join ~sep:"::" - let to_simplified_string = function - | Java {class_name; field_name} -> - String.rsplit2 class_name ~on:'.' - |> Option.value_map ~default:field_name ~f:(fun (_, class_only) -> - dot_join class_only field_name ) - | Clang {field_name} -> - field_name + let to_string fld = + if is_java fld then dot_join (Name.name fld.class_name) fld.field_name else fld.field_name - let to_full_string = function - | Clang {class_name; field_name} -> - Name.to_string class_name ^ "::" ^ field_name - | Java {class_name; field_name} -> - dot_join class_name field_name + let to_simplified_string fld = + if is_java fld then + 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_flat_string = function Java {field_name} -> field_name | Clang {field_name} -> field_name + let to_full_string fld = + if is_java fld then dot_join (Name.name fld.class_name) fld.field_name + else cc_join (Name.to_string fld.class_name) fld.field_name - let pp f = function - | Java {class_name; field_name} when String.is_empty class_name -> - Format.pp_print_string f field_name - | Java {class_name; field_name} -> - F.pp_print_string f (dot_join class_name field_name) - | Clang {field_name} -> - Format.pp_print_string f field_name + let to_flat_string {field_name} = field_name + + let pp f ({class_name; field_name} as field) = + F.pp_print_string f + (if is_java field then dot_join (Name.name class_name) field_name else field_name) - let clang_get_qual_class = function - | Clang {class_name} -> - Some (Name.qual_name class_name) - | _ -> - None + + let clang_get_qual_class ({class_name} as field) = + if is_java field then None else Some (Name.qual_name class_name) module Clang = struct - let from_class_name class_name field_name = Clang {class_name; field_name} + let from_class_name class_name field_name = {class_name; field_name} end module Java = struct - let from_class_and_field ~class_name ~field_name = Java {class_name; field_name} + let from_class_and_field ~class_name ~field_name = + if String.is_empty class_name then + L.die InternalError "Typ.Fieldname.Java classname cannot be empty@\n" + else {class_name= Name.Java.from_string class_name; field_name} - let is_captured_parameter = function - | Java {field_name} -> - String.is_prefix ~prefix:"val$" field_name - | Clang _ -> - false + let is_captured_parameter ({field_name} as field) = + is_java field && String.is_prefix ~prefix:"val$" field_name - let get_class = function - | Java {class_name} -> - class_name - | Clang _ as field -> - L.die InternalError "get_class: fieldname %a is not Java@\n" pp field + let get_class ({class_name} as field) = + if is_java field then Name.name class_name + else L.die InternalError "get_class: fieldname %a is not Java@\n" pp field - let get_field = function - | Java {field_name} -> - field_name - | Clang _ as field -> - L.die InternalError "get_field: fieldname %a is not Java@\n" pp field + let get_field ({field_name} as field) = + if is_java field then field_name + else L.die InternalError "get_field: fieldname %a is not Java@\n" pp field - let is_outer_instance = function - | Java {field_name} -> - 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) - | Clang _ -> - false + + let is_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) end end diff --git a/infer/src/IR/Typ.mli b/infer/src/IR/Typ.mli index 41255d5d4..08da6023a 100644 --- a/infer/src/IR/Typ.mli +++ b/infer/src/IR/Typ.mli @@ -635,10 +635,7 @@ end module Fieldname : sig (** Names for fields of class/struct/union *) - type t [@@deriving compare] - - val equal : t -> t -> bool - (** Equality for field names. *) + type t [@@deriving compare, equal] val is_java : t -> bool