[Refactoring] Use inline record for captured variable type

Reviewed By: jvillard

Differential Revision: D25331728

fbshipit-source-id: 323e5d08f
master
Sungkeun Cho 4 years ago committed by Facebook GitHub Bot
parent 695a3df7be
commit 6972d9cb91

@ -0,0 +1,15 @@
(*
* 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 = {name: Mangled.t; typ: Typ.t; capture_mode: Pvar.capture_mode} [@@deriving compare]
let pp fmt {name; typ; capture_mode} =
F.fprintf fmt "(%a,@,%a,@,%s)" Mangled.pp name (Typ.pp_full Pp.text) typ
(Pvar.string_of_capture_mode capture_mode)

@ -0,0 +1,12 @@
(*
* 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
type t = {name: Mangled.t; typ: Typ.t; capture_mode: Pvar.capture_mode} [@@deriving compare]
val pp : Format.formatter -> t -> unit

@ -21,10 +21,10 @@ let pp_etlist fmt etl =
let pp_var_list fmt etl = let pp_var_list fmt etl =
List.iter etl ~f:(fun (id, ty, mode) -> List.iter etl ~f:(fun {CapturedVar.name; typ; capture_mode} ->
Format.fprintf fmt " [%s]%a:%a" Format.fprintf fmt " [%s]%a:%a"
(Pvar.string_of_capture_mode mode) (Pvar.string_of_capture_mode capture_mode)
Mangled.pp id (Typ.pp_full Pp.text) ty ) Mangled.pp name (Typ.pp_full Pp.text) typ )
let pp_local_list fmt etl = List.iter ~f:(Procdesc.pp_local fmt) etl let pp_local_list fmt etl = List.iter ~f:(Procdesc.pp_local fmt) etl

@ -45,8 +45,7 @@ type specialized_with_blocks_info =
type t = type t =
{ access: PredSymb.access (** visibility access *) { access: PredSymb.access (** visibility access *)
; captured: (Mangled.t * Typ.t * Pvar.capture_mode) list ; captured: CapturedVar.t list (** name and type of variables captured in blocks *)
(** name and type of variables captured in blocks *)
; exceptions: string list (** exceptions thrown by the procedure *) ; exceptions: string list (** exceptions thrown by the procedure *)
; formals: (Mangled.t * Typ.t) list (** name and type of formal parameters *) ; formals: (Mangled.t * Typ.t) list (** name and type of formal parameters *)
; const_formals: int list (** list of indices of formals that are const-qualified *) ; const_formals: int list (** list of indices of formals that are const-qualified *)
@ -155,12 +154,7 @@ let pp_specialized_with_blocks_info fmt info =
info.formals_to_procs_and_new_formals info.formals_to_procs_and_new_formals
let pp_captured_var fmt (var, typ, mode) = let pp_captured = Pp.semicolon_seq ~print_env:Pp.text_break CapturedVar.pp
F.fprintf fmt "(%a,@,%a,@,%s)" Mangled.pp var (Typ.pp_full Pp.text) typ
(Pvar.string_of_capture_mode mode)
let pp_captured = Pp.semicolon_seq ~print_env:Pp.text_break pp_captured_var
let pp f let pp f
({ access ({ access
@ -198,8 +192,8 @@ let pp f
translation_unit ; translation_unit ;
if not (PredSymb.equal_access default.access access) then if not (PredSymb.equal_access default.access access) then
F.fprintf f "; access= %a@," (Pp.of_string ~f:PredSymb.string_of_access) access ; F.fprintf f "; access= %a@," (Pp.of_string ~f:PredSymb.string_of_access) access ;
if not ([%compare.equal: (Mangled.t * Typ.t * Pvar.capture_mode) list] default.captured captured) if not ([%compare.equal: CapturedVar.t list] default.captured captured) then
then F.fprintf f "; captured= [@[%a@]]@," pp_captured captured ; F.fprintf f "; captured= [@[%a@]]@," pp_captured captured ;
if not ([%compare.equal: string list] default.exceptions exceptions) then if not ([%compare.equal: string list] default.exceptions exceptions) then
F.fprintf f "; exceptions= [@[%a@]]@," F.fprintf f "; exceptions= [@[%a@]]@,"
(Pp.semicolon_seq ~print_env:Pp.text_break F.pp_print_string) (Pp.semicolon_seq ~print_env:Pp.text_break F.pp_print_string)

@ -27,7 +27,7 @@ type specialized_with_blocks_info =
type t = type t =
{ access: PredSymb.access (** visibility access *) { access: PredSymb.access (** visibility access *)
; captured: (Mangled.t * Typ.t * Pvar.capture_mode) list ; captured: CapturedVar.t list
(** name, type, and mode of variables captured in blocks and lambdas *) (** name, type, and mode of variables captured in blocks and lambdas *)
; exceptions: string list (** exceptions thrown by the procedure *) ; exceptions: string list (** exceptions thrown by the procedure *)
; formals: (Mangled.t * Typ.t) list (** name and type of formal parameters *) ; formals: (Mangled.t * Typ.t) list (** name and type of formal parameters *)

@ -789,10 +789,10 @@ let pp_variable_list fmt etl =
let pp_captured_list fmt etl = let pp_captured_list fmt etl =
List.iter List.iter
~f:(fun (id, ty, mode) -> ~f:(fun {CapturedVar.name; typ; capture_mode} ->
Format.fprintf fmt " [%s] %a:%a" Format.fprintf fmt " [%s] %a:%a"
(Pvar.string_of_capture_mode mode) (Pvar.string_of_capture_mode capture_mode)
Mangled.pp id (Typ.pp_full Pp.text) ty ) Mangled.pp name (Typ.pp_full Pp.text) typ )
etl etl
@ -847,7 +847,7 @@ let is_captured_pvar procdesc pvar =
| _ -> | _ ->
false false
in in
let pvar_matches_in_captured (name, _, _) = Mangled.equal name pvar_name in let pvar_matches_in_captured {CapturedVar.name} = Mangled.equal name pvar_name in
let is_captured_var_objc_block = let is_captured_var_objc_block =
(* var is captured if the procedure is a objc block and the var is in the captured *) (* var is captured if the procedure is a objc block and the var is in the captured *)
Procname.is_objc_block procname Procname.is_objc_block procname

@ -235,7 +235,7 @@ val get_attributes : t -> ProcAttributes.t
val set_attributes : t -> ProcAttributes.t -> unit val set_attributes : t -> ProcAttributes.t -> unit
val get_captured : t -> (Mangled.t * Typ.t * Pvar.capture_mode) list val get_captured : t -> CapturedVar.t list
(** Return name and type of block's captured variables *) (** Return name and type of block's captured variables *)
val get_exit_node : t -> Node.t val get_exit_node : t -> Node.t

@ -1289,7 +1289,9 @@ let check_call_to_objc_block_error tenv pdesc prop fun_exp loc =
| Some (_, Exp.Lvar pvar) -> | Some (_, Exp.Lvar pvar) ->
(* pvar is the block *) (* pvar is the block *)
let name = Pvar.get_name pvar in let name = Pvar.get_name pvar in
List.exists ~f:(fun (cn, _, _) -> Mangled.equal name cn) (Procdesc.get_captured pdesc) List.exists
~f:(fun {CapturedVar.name= cn} -> Mangled.equal name cn)
(Procdesc.get_captured pdesc)
| _ -> | _ ->
false false
in in

@ -151,7 +151,8 @@ module TransferFunctions = struct
let pvar_name = Pvar.get_name pvar in let pvar_name = Pvar.get_name pvar in
Pvar.is_self pvar Pvar.is_self pvar
&& List.exists && List.exists
~f:(fun (captured, typ, _) -> Mangled.equal captured pvar_name && Typ.is_strong_pointer typ) ~f:(fun {CapturedVar.name= captured; typ} ->
Mangled.equal captured pvar_name && Typ.is_strong_pointer typ )
attributes.ProcAttributes.captured attributes.ProcAttributes.captured
@ -159,7 +160,7 @@ module TransferFunctions = struct
let is_captured_strong_self attributes pvar = let is_captured_strong_self attributes pvar =
(not (Pvar.is_self pvar)) (not (Pvar.is_self pvar))
&& List.exists && List.exists
~f:(fun (captured, typ, _) -> ~f:(fun {CapturedVar.name= captured; typ} ->
Typ.is_strong_pointer typ Typ.is_strong_pointer typ
&& Mangled.equal captured (Pvar.get_name pvar) && Mangled.equal captured (Pvar.get_name pvar)
&& String.is_suffix ~suffix:"self" (String.lowercase (Mangled.to_string captured)) ) && String.is_suffix ~suffix:"self" (String.lowercase (Mangled.to_string captured)) )
@ -168,7 +169,7 @@ module TransferFunctions = struct
let is_captured_weak_self attributes pvar = let is_captured_weak_self attributes pvar =
List.exists List.exists
~f:(fun (captured, typ, _) -> ~f:(fun {CapturedVar.name= captured; typ} ->
Mangled.equal captured (Pvar.get_name pvar) Mangled.equal captured (Pvar.get_name pvar)
&& String.is_substring ~substring:"self" (String.lowercase (Mangled.to_string captured)) && String.is_substring ~substring:"self" (String.lowercase (Mangled.to_string captured))
&& Typ.is_weak_pointer typ ) && Typ.is_weak_pointer typ )

@ -206,7 +206,9 @@ let create_local_procdesc ?(set_objc_accessor_attr = false) ?(record_lambda_capt
PredSymb.Protected PredSymb.Protected
in in
let captured_mangled = let captured_mangled =
List.map ~f:(fun (var, t, mode) -> (Pvar.get_name var, t, mode)) captured List.map
~f:(fun (var, typ, capture_mode) -> {CapturedVar.name= Pvar.get_name var; typ; capture_mode})
captured
in in
(* Retrieve captured variables from procdesc created when translating captured variables in lambda expression *) (* Retrieve captured variables from procdesc created when translating captured variables in lambda expression *)
(* We want to do this before `should_create_procdesc` is called as it can remove previous procdesc *) (* We want to do this before `should_create_procdesc` is called as it can remove previous procdesc *)
@ -233,7 +235,7 @@ let create_local_procdesc ?(set_objc_accessor_attr = false) ?(record_lambda_capt
(* Captured variables for blocks are treated as parameters, but not for cpp lambdas *) (* Captured variables for blocks are treated as parameters, but not for cpp lambdas *)
let captured_as_formals = let captured_as_formals =
if is_cpp_lambda_call_operator then [] if is_cpp_lambda_call_operator then []
else List.map ~f:(fun (var, t, _) -> (var, t)) captured_mangled else List.map ~f:(fun {CapturedVar.name; typ} -> (name, typ)) captured_mangled
in in
let formals = captured_as_formals @ formals in let formals = captured_as_formals @ formals in
let const_formals = let const_formals =

@ -829,9 +829,9 @@ module CTrans_funct (F : CModule_type.CFrontend) : CModule_type.CTranslation = s
match procname with match procname with
| Procname.ObjC_Cpp cpp_pname when Procname.ObjC_Cpp.is_cpp_lambda cpp_pname -> | Procname.ObjC_Cpp cpp_pname when Procname.ObjC_Cpp.is_cpp_lambda cpp_pname ->
let pvar_name = Pvar.get_name pvar in let pvar_name = Pvar.get_name pvar in
List.find (Procdesc.get_captured context.procdesc) ~f:(fun (captured_var, _, _) -> List.find (Procdesc.get_captured context.procdesc)
Mangled.equal captured_var pvar_name ) ~f:(fun {CapturedVar.name= captured_var} -> Mangled.equal captured_var pvar_name)
|> Option.value_map ~f:(fun (_, t, _) -> t) ~default:typ |> Option.value_map ~f:(fun {CapturedVar.typ} -> typ) ~default:typ
| _ -> | _ ->
typ typ
in in

@ -313,7 +313,7 @@ let mk_initial proc_desc =
Procdesc.get_formals proc_desc |> List.map ~f:(fun (mangled, _) -> init_var mangled) Procdesc.get_formals proc_desc |> List.map ~f:(fun (mangled, _) -> init_var mangled)
in in
let captured = let captured =
Procdesc.get_captured proc_desc |> List.map ~f:(fun (mangled, _, _) -> init_var mangled) Procdesc.get_captured proc_desc |> List.map ~f:(fun {CapturedVar.name} -> init_var name)
in in
captured @ formals captured @ formals
in in

@ -521,9 +521,9 @@ let call ~caller_proc_desc err_log ~(callee_data : (Procdesc.t * PulseSummary.t)
in in
let captured_vars = let captured_vars =
Procdesc.get_captured callee_proc_desc Procdesc.get_captured callee_proc_desc
|> List.map ~f:(fun (mangled, _, mode) -> |> List.map ~f:(fun {CapturedVar.name; capture_mode} ->
let pvar = Pvar.mk mangled callee_pname in let pvar = Pvar.mk name callee_pname in
(Var.of_pvar pvar, mode) ) (Var.of_pvar pvar, capture_mode) )
in in
let+ astate, captured_vars_with_actuals = let+ astate, captured_vars_with_actuals =
match actuals with match actuals with

Loading…
Cancel
Save