[typ][fieldname] unify clang and java fieldname types

Summary: Unify treatment of Java and Clang fieldnames. Now a field is a struct with a class type-name and a string-field name.  This diff is still behaviour preserving.

Reviewed By: jvillard

Differential Revision: D18953549

fbshipit-source-id: 8cae0d104
master
Nikos Gorogiannis 5 years ago committed by Facebook Github Bot
parent 2c44035297
commit c45b55bff1

@ -1420,12 +1420,9 @@ module Procname = struct
end end
module Fieldname = struct module Fieldname = struct
type t = type t = {class_name: Name.t; field_name: string} [@@deriving compare, equal]
| Clang of {class_name: Name.t; field_name: string}
| Java of {class_name: string; 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 module T = struct
type nonrec t = t type nonrec t = t
@ -1436,87 +1433,72 @@ module Fieldname = struct
module Set = Caml.Set.Make (T) module Set = Caml.Set.Make (T)
module Map = Caml.Map.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 let dot_join = join ~sep:"."
| 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 cc_join = join ~sep:"::"
let to_simplified_string = function let to_string fld =
| Java {class_name; field_name} -> if is_java fld then dot_join (Name.name fld.class_name) fld.field_name else fld.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_full_string = function let to_simplified_string fld =
| Clang {class_name; field_name} -> if is_java fld then
Name.to_string class_name ^ "::" ^ field_name Name.name fld.class_name |> String.rsplit2 ~on:'.'
| Java {class_name; field_name} -> |> Option.value_map ~default:fld.field_name ~f:(fun (_, class_only) ->
dot_join class_name field_name 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} -> let clang_get_qual_class ({class_name} as field) =
Some (Name.qual_name class_name) if is_java field then None else Some (Name.qual_name class_name)
| _ ->
None
module Clang = struct 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 end
module Java = struct 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 is_outer_instance ({field_name} as field) =
let this = "this$" in is_java field
let last_char = field_name.[String.length field_name - 1] in &&
(last_char >= '0' && last_char <= '9') let this = "this$" in
&& String.is_suffix field_name ~suffix:(this ^ String.of_char last_char) let last_char = field_name.[String.length field_name - 1] in
| Clang _ -> (last_char >= '0' && last_char <= '9')
false && String.is_suffix field_name ~suffix:(this ^ String.of_char last_char)
end end
end end

@ -635,10 +635,7 @@ end
module Fieldname : sig module Fieldname : sig
(** Names for fields of class/struct/union *) (** Names for fields of class/struct/union *)
type t [@@deriving compare] type t [@@deriving compare, equal]
val equal : t -> t -> bool
(** Equality for field names. *)
val is_java : t -> bool val is_java : t -> bool

Loading…
Cancel
Save