Skip to content

Commit

Permalink
[infer/py] Improve BUILD_CONST_KEY_MAP support and add BUILD_MAP
Browse files Browse the repository at this point in the history
Summary:
BUILD_CONST_KEY_MAP is a specialized version of BUILD_MAP.
This diffs adds supports for the latter, refactoring the former to share some structure. It also get rid of a TODO : we can now load maps !

Reviewed By: ngorogiannis

Differential Revision: D49457576

fbshipit-source-id: d56020120ee592b81d510fa962821ba7c738a5f7
  • Loading branch information
Vincent Siles authored and facebook-github-bot committed Sep 26, 2023
1 parent c5385c4 commit 476dd6a
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 91 deletions.
44 changes: 25 additions & 19 deletions infer/src/python/PyBuiltin.ml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,19 @@ module Builtin = struct
"xor"


type collection = List | Set | Tuple | Map [@@deriving compare]

let collection_to_string = function
| List ->
"list"
| Set ->
"set"
| Tuple ->
"tuple"
| Map ->
"map"


type textual =
| IsTrue
| Binary of binary_op
Expand All @@ -104,9 +117,7 @@ module Builtin = struct
| PythonCode
| PythonIter
| PythonIterNext
| PythonBuildList
| PythonBuildSet
| PythonBuildTuple
| PythonBuild of collection
| PythonIndex
| PythonSubscriptGet
| PythonSubscriptSet
Expand Down Expand Up @@ -165,12 +176,8 @@ let to_proc_name = function
"python_iter"
| PythonIterNext ->
"python_iter_next"
| PythonBuildList ->
"python_build_list"
| PythonBuildSet ->
"python_build_set"
| PythonBuildTuple ->
"python_build_tuple"
| PythonBuild builder ->
sprintf "python_build_%s" (collection_to_string builder)
| PythonIndex ->
"python_index"
| PythonSubscriptGet ->
Expand Down Expand Up @@ -269,6 +276,9 @@ module Set = struct
; result_type= annotatedObject
; used_struct_types= [] } )
in
let no_formal ?(result_type = annotatedObject) op =
(op, {formals_types= None; result_type; used_struct_types= []})
in
let builtins =
[ ( Builtin.IsTrue
, { formals_types= Some [annotatedObject]
Expand Down Expand Up @@ -300,10 +310,8 @@ module Set = struct
; binary_op (Builtin.Inplace Subtract)
; binary_op (Builtin.Inplace TrueDivide)
; binary_op (Builtin.Inplace Xor)
; ( Builtin.PythonCall
, {formals_types= None; result_type= annotatedObject; used_struct_types= []} )
; ( Builtin.PythonCallKW
, {formals_types= None; result_type= annotatedObject; used_struct_types= []} )
; no_formal Builtin.PythonCall
; no_formal Builtin.PythonCallKW
; ( Builtin.PythonKWArg
, { formals_types= Some [annot string_; annotatedObject]
; result_type= annotatedObject
Expand All @@ -325,12 +333,10 @@ module Set = struct
, { formals_types= Some [annotatedObject]
; result_type= annot PyCommon.pyIterItem
; used_struct_types= [PyCommon.pyIterItemStruct] } )
; ( Builtin.PythonBuildList
, {formals_types= None; result_type= annot PyCommon.pyList; used_struct_types= []} )
; ( Builtin.PythonBuildSet
, {formals_types= None; result_type= annot PyCommon.pySet; used_struct_types= []} )
; ( Builtin.PythonBuildTuple
, {formals_types= None; result_type= annot PyCommon.pyTuple; used_struct_types= []} )
; no_formal (Builtin.PythonBuild List) ~result_type:(annot PyCommon.pyList)
; no_formal (Builtin.PythonBuild Set) ~result_type:(annot PyCommon.pySet)
; no_formal (Builtin.PythonBuild Tuple) ~result_type:(annot PyCommon.pyTuple)
; no_formal (Builtin.PythonBuild Map) ~result_type:(annot PyCommon.pyMap)
; ( Builtin.PythonIndex
, { formals_types= Some [annot PyCommon.pyObject; annot T.Typ.Int]
; result_type= annot PyCommon.pyObject
Expand Down
6 changes: 3 additions & 3 deletions infer/src/python/PyBuiltin.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type binary_op =
| Xor
[@@deriving compare]

type collection = List | Set | Tuple | Map [@@deriving compare]

type textual =
| IsTrue
| Binary of binary_op
Expand All @@ -41,9 +43,7 @@ type textual =
| PythonCode
| PythonIter
| PythonIterNext
| PythonBuildList
| PythonBuildSet
| PythonBuildTuple
| PythonBuild of collection
| PythonIndex
| PythonSubscriptGet
| PythonSubscriptSet
Expand Down
10 changes: 10 additions & 0 deletions infer/src/python/PyCommon.ml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ let pyClass = mk_type "PyClass"

let pyList = mk_type "PyList"

let pyMap = mk_type "PyMap"

let pySet = mk_type "PySet"

let pyTuple = mk_type "PyTuple"
Expand Down Expand Up @@ -114,6 +116,14 @@ let mk_string (s : string) =
T.Exp.Call {proc; args; kind= NonVirtual}


let get_string = function
| T.Exp.Call {proc; args= [arg]; kind= NonVirtual}
when T.equal_qualified_procname proc python_string -> (
match arg with Const (Str s) -> Some s | _ -> None )
| _ ->
None


let mk_bytes (s : bytes) =
let proc = python_bytes in
let s = Bytes.to_string s in
Expand Down
6 changes: 6 additions & 0 deletions infer/src/python/PyCommon.mli
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ val pyNone : Textual.Typ.t
val pyList : Textual.Typ.t
(** Python's builtin [list] type *)

val pyMap : Textual.Typ.t
(** Python's builtin [map] type *)

val pySet : Textual.Typ.t
(** Python's builtin [set] type *)

Expand All @@ -101,6 +104,9 @@ val mk_float : float -> Textual.Exp.t
val mk_string : string -> Textual.Exp.t
(** Helper function to define typed Textual expression for literal strings. *)

val get_string : Textual.Exp.t -> string option
(** Helper to get back a string built with [mk_string] *)

val mk_bytes : bytes -> Textual.Exp.t
(** Helper function to define typed Textual expression for literal bytes. *)

Expand Down
2 changes: 1 addition & 1 deletion infer/src/python/PyEnv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ module DataStack = struct
| VarName of int
| Temp of T.Ident.t
| Code of {fun_or_class: bool; code_name: string; code: FFI.Code.t}
| Map of (string * cell) list
| Map of (T.Exp.t * cell) list
| BuiltinBuildClass
| Import of {import_path: Ident.t; symbols: string list}
| ImportCall of {id: Ident.t; loc: T.Location.t}
Expand Down
2 changes: 1 addition & 1 deletion infer/src/python/PyEnv.mli
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module DataStack : sig
| Temp of T.Ident.t (** SSA variable *)
| Code of {fun_or_class: bool; code_name: string; code: FFI.Code.t}
(** [code] Python object with its name. It can be a function, class, closure, ... *)
| Map of (string * cell) list
| Map of (T.Exp.t * cell) list
(** Light encoding of raw Python tuples/dicts. Only used for type annotations at the moment. *)
| BuiltinBuildClass (** see Python's [LOAD_BUILD_CLASS] *)
| Import of {import_path: Ident.t; symbols: string list}
Expand Down
Loading

0 comments on commit 476dd6a

Please sign in to comment.