Skip to content

Commit

Permalink
[textual][refactor] moving qualified_procname type and functions into…
Browse files Browse the repository at this point in the history
… a module

Summary: it makes Textual.mli more uniform

Reviewed By: ezgicicek

Differential Revision: D49820982

fbshipit-source-id: 74dd742c888f92036bdb40853e7c4a42459d60e1
  • Loading branch information
davidpichardie authored and facebook-github-bot committed Oct 3, 2023
1 parent 0d2f72b commit f6ea606
Show file tree
Hide file tree
Showing 13 changed files with 153 additions and 151 deletions.
2 changes: 1 addition & 1 deletion infer/src/python/PyBuiltin.mli
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ val textual : textual -> builtin

val of_string : string -> builtin option

val to_proc_name : builtin -> Textual.qualified_procname
val to_proc_name : builtin -> Textual.QualifiedProcName.t

module Set : sig
(** This module keeps track of the builtins used by a code unit. Only the necessary Textual
Expand Down
18 changes: 9 additions & 9 deletions infer/src/python/PyCommon.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ let node_name ?(loc = T.Location.Unknown) value = {T.NodeName.value; loc}
let field_name ?(loc = T.Location.Unknown) value = {T.FieldName.value; loc}

(* TODO: only deal with toplevel functions for now *)
let qualified_procname ~enclosing_class name : T.qualified_procname =
let qualified_procname ~enclosing_class name : T.QualifiedProcName.t =
{enclosing_class= Enclosing enclosing_class; name}


Expand Down Expand Up @@ -75,9 +75,9 @@ let pyIterItemStruct =

let builtins = "$builtins"

let builtin_scope = T.Enclosing T.{TypeName.value= builtins; loc= Unknown}
let builtin_scope = T.QualifiedProcName.Enclosing T.{TypeName.value= builtins; loc= Unknown}

let builtin_name (value : string) : T.qualified_procname =
let builtin_name (value : string) : T.QualifiedProcName.t =
let name = T.ProcName.{value; loc= T.Location.Unknown} in
{enclosing_class= builtin_scope; name}

Expand Down Expand Up @@ -118,14 +118,14 @@ let mk_string (s : string) =

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


let get_tuple_as_list = function
| T.Exp.Call {proc; args; kind= NonVirtual} when T.equal_qualified_procname proc python_tuple ->
| T.Exp.Call {proc; args; kind= NonVirtual} when T.QualifiedProcName.equal proc python_tuple ->
Some args
| _ ->
None
Expand Down Expand Up @@ -234,20 +234,20 @@ module Ident = struct
match path with Empty -> name | Path {path; last} -> to_enclosing_name name path sep last


let to_qualified_procname {root= {name; loc}; path} : T.qualified_procname =
let to_qualified_procname {root= {name; loc}; path} : T.QualifiedProcName.t =
match path with
| Empty ->
{T.enclosing_class= TopLevel; name= proc_name ~loc name}
{enclosing_class= TopLevel; name= proc_name ~loc name}
| Path {path; last} ->
let enclosing_class =
let value =
if List.is_empty path then name else Format.asprintf "%s::%a" name pp_rev_list path
in
let type_name = type_name ~loc value in
T.Enclosing type_name
T.QualifiedProcName.Enclosing type_name
in
let name = {T.ProcName.value= last; loc} in
{T.enclosing_class; name}
{enclosing_class; name}


let to_type_name ?(static = false) {root= {name; loc}; path} : T.TypeName.t =
Expand Down
18 changes: 9 additions & 9 deletions infer/src/python/PyCommon.mli
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ val field_name : ?loc:Textual.Location.t -> string -> Textual.FieldName.t
val mk_type : string -> Textual.Typ.t

val qualified_procname :
enclosing_class:Textual.TypeName.t -> Textual.ProcName.t -> Textual.qualified_procname
enclosing_class:Textual.TypeName.t -> Textual.ProcName.t -> Textual.QualifiedProcName.t

val builtin_name : string -> Textual.qualified_procname
val builtin_name : string -> Textual.QualifiedProcName.t
(** Helper function to encode known builtin names correctly *)

val python_int : Textual.qualified_procname
val python_int : Textual.QualifiedProcName.t
(** Encoding of Python [int] type. Since Python integers are of arbitrary precision, they are not
modeled directly with [int]. *)

val python_float : Textual.qualified_procname
val python_float : Textual.QualifiedProcName.t
(** Encoding of Python [float] type. *)

val python_string : Textual.qualified_procname
val python_string : Textual.QualifiedProcName.t
(** Encoding of Python [str] type. *)

val python_bytes : Textual.qualified_procname
val python_bytes : Textual.QualifiedProcName.t
(** Encoding of Python [bytes] type. *)

val python_bool : Textual.qualified_procname
val python_bool : Textual.QualifiedProcName.t
(** Encoding of Python [bool] type. *)

val python_tuple : Textual.qualified_procname
val python_tuple : Textual.QualifiedProcName.t
(** Encoding of Python [tuple] type. It is the raw "untyped" one where every item is of type
[object]. *)

Expand Down Expand Up @@ -137,7 +137,7 @@ module Ident : sig

val to_string : sep:string -> t -> string

val to_qualified_procname : t -> Textual.qualified_procname
val to_qualified_procname : t -> Textual.QualifiedProcName.t

val to_type_name : ?static:bool -> t -> Textual.TypeName.t

Expand Down
10 changes: 5 additions & 5 deletions infer/src/python/PyEnv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ module DataStack = struct
| BuiltinBuildClass
| Import of {import_path: Ident.t; symbols: string list}
| ImportCall of {id: Ident.t; loc: T.Location.t}
| MethodCall of {receiver: T.Exp.t; name: T.qualified_procname}
| StaticCall of {call_name: T.qualified_procname; receiver: T.Exp.t option}
| MethodCall of {receiver: T.Exp.t; name: T.QualifiedProcName.t}
| StaticCall of {call_name: T.QualifiedProcName.t; receiver: T.Exp.t option}
| Super
| Path of Ident.t
| WithContext of
Expand Down Expand Up @@ -120,9 +120,9 @@ module DataStack = struct
| ImportCall {id} ->
F.fprintf fmt "ImportCall(%a)" Ident.pp id
| MethodCall {receiver; name} ->
F.fprintf fmt "MethodCall(%a, %a)" T.Exp.pp receiver T.pp_qualified_procname name
F.fprintf fmt "MethodCall(%a, %a)" T.Exp.pp receiver T.QualifiedProcName.pp name
| StaticCall {call_name; receiver} ->
F.fprintf fmt "StaticCall(%a, %a)" T.pp_qualified_procname call_name (Pp.option T.Exp.pp)
F.fprintf fmt "StaticCall(%a, %a)" T.QualifiedProcName.pp call_name (Pp.option T.Exp.pp)
receiver
| Super ->
F.pp_print_string fmt "Super"
Expand Down Expand Up @@ -529,7 +529,7 @@ let globals {shared= {globals}} = globals
let get_used_builtins {shared= {builtins}} = builtins

let register_builtin ({shared} as env) builtin =
PyDebug.p "[register_builtin] %a\n" T.pp_qualified_procname (Builtin.to_proc_name builtin) ;
PyDebug.p "[register_builtin] %a\n" T.QualifiedProcName.pp (Builtin.to_proc_name builtin) ;
let register_builtin ({builtins} as env) builtin =
{env with builtins= Builtin.Set.register builtins builtin}
in
Expand Down
4 changes: 2 additions & 2 deletions infer/src/python/PyEnv.mli
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ module DataStack : sig
| Import of {import_path: Ident.t; symbols: string list}
(** imported module path, with optional name of symbols *)
| ImportCall of {id: Ident.t; loc: T.Location.t} (** Static call to export definition *)
| MethodCall of {receiver: T.Exp.t; name: T.qualified_procname}
| MethodCall of {receiver: T.Exp.t; name: T.QualifiedProcName.t}
(** Virtual call, usually of a method of a class. Could be an access to a closure that is
called straight away *)
| StaticCall of {call_name: T.qualified_procname; receiver: T.Exp.t option}
| StaticCall of {call_name: T.QualifiedProcName.t; receiver: T.Exp.t option}
(** call to static method in class. Because we turn some method calls into static ones, we
have to keep the receiver around, just in case. *)
| Super (** special name to refer to the parent class, like in [super().__init__()] *)
Expand Down
20 changes: 10 additions & 10 deletions infer/src/python/PyTrans.ml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ module Error = struct
| CallKeywordBuildClass
| RaiseExceptionInvalid of int
| RaiseExceptionUnknown of DataStack.cell
| DefaultArgSpecialization of T.qualified_procname * int * int
| DefaultArgSpecialization of T.QualifiedProcName.t * int * int

type t = L.error * kind

Expand Down Expand Up @@ -226,7 +226,7 @@ module Error = struct
F.fprintf fmt "RAISE_VARARGS unknown construct %a" DataStack.pp_cell cell
| DefaultArgSpecialization (name, param_size, default_size) ->
F.fprintf fmt "%a has more default arguments (%d) then actual arguments (%d)"
T.pp_qualified_procname name default_size param_size
T.QualifiedProcName.pp name default_size param_size


let class_decl kind = (L.InternalError, ClassDecl kind)
Expand Down Expand Up @@ -753,7 +753,7 @@ module FUNCTION = struct
(* TODO: support nesting. Maybe add to_proc_name to Symbol *)
let typ = Ident.to_typ id in
let name = Ident.to_constructor id in
let proc : T.qualified_procname = {enclosing_class= TopLevel; name} in
let proc : T.QualifiedProcName.t = {enclosing_class= TopLevel; name} in
mk env ~typ proc )


Expand Down Expand Up @@ -1121,7 +1121,7 @@ module METHOD = struct
let* env, receiver, _ = load_cell env cell in
let loc = Env.loc env in
let name = proc_name ~loc method_name in
let name : T.qualified_procname = {T.enclosing_class= Enclosing T.TypeName.wildcard; name} in
let name : T.QualifiedProcName.t = {enclosing_class= Enclosing T.TypeName.wildcard; name} in
let env = Env.push env (DataStack.MethodCall {receiver; name}) in
Ok (env, None)

Expand Down Expand Up @@ -1183,7 +1183,7 @@ module METHOD = struct
(* We're in [LOAD_METHOD] but we fake a static call because of how method resolution works. *)
let loc = Env.loc env in
let name = proc_name ~loc method_name in
let call_name : T.qualified_procname = {T.enclosing_class= Enclosing super_type; name} in
let call_name : T.QualifiedProcName.t = {enclosing_class= Enclosing super_type; name} in
let env = Env.push env (DataStack.StaticCall {call_name; receiver= Some receiver}) in
Ok (env, None)
| Error (_, err) ->
Expand Down Expand Up @@ -3002,7 +3002,7 @@ let constructor full_name loc has_init =
let mk typ = annotated_type_of_annotation typ in
let full_type_name = Ident.to_type_name full_name in
let struct_ = T.Typ.(Struct full_type_name) in
let sil_allocate : T.qualified_procname =
let sil_allocate : T.QualifiedProcName.t =
{enclosing_class= TopLevel; name= proc_name T.builtin_allocate}
in
let alloc = T.Exp.call_non_virtual sil_allocate [T.Exp.Typ struct_] in
Expand Down Expand Up @@ -3068,14 +3068,14 @@ let constructor_stubs ~kind loc class_name =
let qualified_name, result_type =
match kind with
| `Ctor ->
let qualified_name : T.qualified_procname =
{T.enclosing_class= TopLevel; name= Ident.to_constructor class_name}
let qualified_name : T.QualifiedProcName.t =
{enclosing_class= TopLevel; name= Ident.to_constructor class_name}
in
let result_type = T.Typ.mk_without_attributes @@ Ident.to_typ class_name in
(qualified_name, result_type)
| `Init ->
let qualified_name : T.qualified_procname =
{ T.enclosing_class= Enclosing (Ident.to_type_name class_name)
let qualified_name : T.QualifiedProcName.t =
{ enclosing_class= Enclosing (Ident.to_type_name class_name)
; name= proc_name ~loc PyCommon.init__ }
in
let result_type = T.Typ.mk_without_attributes @@ PyCommon.pyNone in
Expand Down
Loading

0 comments on commit f6ea606

Please sign in to comment.