New function get_class_or_typedef
[hiphop-php.git] / hphp / hack / src / typing / tast_env.mli
blob60929f10fb4194e4f50855ea6437e1ffdf255a39
1 (*
2 * Copyright (c) 2018, Facebook, Inc.
3 * All rights reserved.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
8 *)
10 type env [@@deriving show]
12 type t = env [@@deriving show]
14 exception Not_in_class
16 type class_or_typedef_result =
17 | ClassResult of Decl_provider.class_decl
18 | TypedefResult of Typing_defs.typedef_type
20 (** Return a string representation of the given type using Hack-like syntax. *)
21 val print_ty : env -> Typing_defs.locl_ty -> string
23 val print_decl_ty : env -> Typing_defs.decl_ty -> string
25 val print_error_ty :
26 ?ignore_dynamic:bool -> env -> Typing_defs.locl_ty -> string
28 (** Return a string representation of the given type using Hack-like syntax,
29 formatted with limited width and line breaks, including additional
30 information from the {!SymbolOccurrence.t} and (if provided)
31 {!SymbolDefinition.t}. *)
32 val print_ty_with_identity :
33 env ->
34 Typing_defs.phase_ty ->
35 'b SymbolOccurrence.t ->
36 'b SymbolDefinition.t option ->
37 string
39 (** Return a JSON representation of the given type. *)
40 val ty_to_json : env -> Typing_defs.locl_ty -> Hh_json.json
42 (** Convert a JSON representation of a type back into a locl-phase type. *)
43 val json_to_locl_ty :
44 ?keytrace:Hh_json.Access.keytrace ->
45 Provider_context.t ->
46 Hh_json.json ->
47 (Typing_defs.locl_ty, Typing_defs.deserialization_error) result
49 (** Return the name of the enclosing class definition.
50 When not in a class definition, return {!None}. *)
51 val get_self_id : env -> string option
53 (** Return the type of the enclosing class definition.
54 When not in a class definition, return {!None}. *)
55 val get_self_ty : env -> Tast.ty option
57 (** Return the type of the enclosing class definition.
58 When not in a class definition, raise {!Not_in_class}. *)
59 val get_self_ty_exn : env -> Tast.ty
61 (** Return the name of the parent of the enclosing class definition.
62 When not in a class definition or no parent exists, return {!None}. *)
63 val get_parent_id : env -> string option
65 (** Return the info of the given class from the typing heap. *)
66 val get_class :
67 env -> Decl_provider.class_key -> Decl_provider.class_decl option
69 val get_class_or_typedef :
70 env -> Decl_provider.class_key -> class_or_typedef_result option
72 (** Return {true} when in the definition of a static property or method. *)
73 val is_static : env -> bool
75 (** Return {true} if the containing file was checked in strict mode. *)
76 val is_strict : env -> bool
78 (** Return the mode of the containing file *)
79 val get_mode : env -> FileInfo.mode
81 (** Return the {!TypecheckerOptions.t} with which this TAST was checked. *)
82 val get_tcopt : env -> TypecheckerOptions.t
84 (** Return the {!Provider_context.t} with which this TAST was checked. *)
85 val get_ctx : env -> Provider_context.t
87 val get_file : env -> Relative_path.t
89 val get_deps_mode : env -> Typing_deps_mode.t
91 (* Return the {!Relative_path.t} of the file the env is from *)
93 (** Expand a type variable ({!Typing_defs.Tvar}) to the type it refers to. *)
94 val expand_type : env -> Tast.ty -> env * Tast.ty
96 (** Eliminate type variables ({!Typing_defs.Tvar}) in the given type by
97 recursively replacing them with the type they refer to. *)
98 val fully_expand : env -> Tast.ty -> Tast.ty
100 (** Given some class type or unresolved union of class types, return the
101 identifiers of all classes the type may represent. *)
102 val get_class_ids : env -> Tast.ty -> string list
104 (** Strip away all Toptions that we possibly can in a type, expanding type
105 variables along the way, turning ?T -> T. *)
106 val non_null : env -> Pos.t -> Tast.ty -> env * Tast.ty
108 (** Get the "as" constraints from an abstract type or generic parameter, or
109 return the type itself if there is no "as" constraint. In the case of a
110 generic parameter whose "as" constraint is another generic parameter, repeat
111 the process until a type is reached that is not a generic parameter. Don't
112 loop on cycles. (For example, function foo<Tu as Tv, Tv as Tu>(...)) *)
113 val get_concrete_supertypes : env -> Tast.ty -> env * Tast.ty list
115 (** Return {true} if the given {Decl_provider.class_decl} (referred to by the given
116 {class_id_}, if provided) allows the current class (the one returned by
117 {!get_self}) to access its members with the given {visibility}. *)
118 val is_visible :
119 env ->
120 Typing_defs.ce_visibility * bool ->
121 Nast.class_id_ option ->
122 Decl_provider.class_decl ->
123 bool
125 (** Assert that the types of values involved in a strict (non-)equality
126 comparison are compatible; e.g., that the types are not statically
127 known to be disjoint, in which case the comparison will always return
128 true or false. *)
129 val assert_nontrivial :
130 Pos.t -> Ast_defs.bop -> env -> Tast.ty -> Tast.ty -> unit
132 (** Assert that the type of a value involved in a strict (non-)equality
133 comparsion to null is nullable (otherwise it is known to always
134 return true or false). *)
135 val assert_nullable : Pos.t -> Ast_defs.bop -> env -> Tast.ty -> unit
137 (** Return the declaration-phase type the given hint represents. *)
138 val hint_to_ty : env -> Aast.hint -> Typing_defs.decl_ty
140 val localize :
141 env -> Typing_defs.expand_env -> Typing_defs.decl_ty -> env * Tast.ty
143 (** Transforms a declaration phase type ({!Typing_defs.decl_ty})
144 into a localized type ({!Typing_defs.locl_ty} = {!Tast.ty}).
145 Performs no substitutions of generics and initializes the late static bound
146 type ({!Typing_defs.Tthis}) to the current class type (the type returned by
147 {!get_self}).
149 This is mostly provided as legacy support for {!AutocompleteService}, and
150 should not be considered a general mechanism for transforming a {decl_ty} to
151 a {!Tast.ty}.
153 {!quiet} silences certain errors because those errors have already fired
154 and/or are not appropriate at the time we call localize.
156 val localize_with_self :
157 env ->
158 ?pos:Pos.t ->
159 ?quiet:bool ->
160 ?report_cycle:Pos.t * string ->
161 Typing_defs.decl_ty ->
162 env * Tast.ty
164 (** Get the upper bounds of the type parameter with the given name.
165 FIXME: This function cannot return correct bounds at this time, because
166 during TAST checks, the Next continuation in the typing environment (which stores
167 information about type parameters) is gone.
169 val get_upper_bounds :
170 env -> string -> Typing_defs.locl_ty list -> Type_parameter_env.tparam_bounds
172 (** Get the reification of the type parameter with the given name. *)
173 val get_reified : env -> string -> Aast.reify_kind
175 (** Get whether the type parameter supports testing with is/as. *)
176 val get_enforceable : env -> string -> bool
178 (** Indicates whether the type parameter with the given name is <<__Newable>>. *)
179 val get_newable : env -> string -> bool
181 (** Return whether the type parameter with the given name was implicity created
182 as part of an `instanceof`, `is`, or `as` expression (instead of being
183 explicitly declared in code by the user). *)
184 val is_fresh_generic_parameter : string -> bool
186 (** Assert that one type is a subtype of another, resolving unbound type
187 variables in both types (if any), with {!env} reflecting the new state of
188 these type variables. Produce an error if they cannot be subtypes. *)
189 val assert_subtype :
190 Pos.t ->
191 Typing_reason.ureason ->
192 env ->
193 Tast.ty ->
194 Tast.ty ->
195 Errors.typing_error_callback ->
198 (** Return {true} when the first type is a subtype of the second type
199 regardless of the values of unbound type variables in both types (if any). *)
200 val is_sub_type : env -> Tast.ty -> Tast.ty -> bool
202 (** Return {true} when the first type can be considered a subtype of the second
203 type after resolving unbound type variables in both types (if any). *)
204 val can_subtype : env -> Tast.ty -> Tast.ty -> bool
206 (** Return {true} when the first type is a subtype of the second type. There is
207 no type T such that for all T', T <: T' and T' <: T (which is the case for Tany
208 and Terr in `can_subtype`) *)
209 val is_sub_type_for_union : env -> Tast.ty -> Tast.ty -> bool
211 (** Simplify unions in a type. *)
212 val simplify_unions : env -> Tast.ty -> env * Tast.ty
214 (** Union a list of types. *)
215 val union_list : env -> Typing_reason.t -> Tast.ty list -> env * Tast.ty
217 (** Returns (class_name, tconst_name, tconst_reference_position) for each type
218 constant referenced in the type access path. *)
219 val referenced_typeconsts :
220 env -> Aast.hint -> Aast.sid list -> (string * string * Pos.t) list
222 (** Return an {!env} for which {!is_static} will return {true}.
223 If you are using {!Tast_visitor}, you should have no need of this. *)
224 val set_static : env -> env
226 (** Return an {!env} for which {!val_kind} is set to the second argument. *)
227 val set_val_kind : env -> Typing_defs.val_kind -> env
229 (** Returns the val_kind of the typing environment *)
230 val get_val_kind : env -> Typing_defs.val_kind
232 (** Returns an {!env} for which {!inside_constructor} is set to {true}.
233 If you are using {!Tast_visitor}, you should have no need of this. *)
234 val set_inside_constructor : env -> env
236 (** Returns whether or not the typing environment is inside the
237 constructor of a class *)
238 val get_inside_constructor : env -> bool
240 (** Returns a {!Decl_env.env} *)
241 val get_decl_env : env -> Decl_env.env
243 (** Construct an empty {!env}. Unlikely to be the best choice; prefer using
244 {!Tast_visitor} or constructing an {!env} from a {!Tast.def}. *)
245 val empty : Provider_context.t -> env
247 (** Construct an {!env} from a toplevel definition. *)
248 val def_env : Provider_context.t -> Tast.def -> env
250 (** Construct an {!env} from a method definition and the {!env} of the context
251 it appears in. *)
252 val restore_method_env : env -> Tast.method_ -> env
254 (** Construct an {!env} from a lambda definition and the {!env} of the context
255 it appears in. *)
256 val restore_fun_env : env -> Tast.fun_ -> env
258 val typing_env_as_tast_env : Typing_env_types.env -> env
260 val tast_env_as_typing_env : env -> Typing_env_types.env
262 (** Verify that an XHP body expression is legal. *)
263 val is_xhp_child : env -> Pos.t -> Tast.ty -> bool
265 val get_enum : env -> string -> Decl_provider.class_decl option
267 val is_typedef : env -> string -> bool
269 val get_typedef : env -> string -> Decl_provider.typedef_decl option
271 val is_enum : env -> string -> bool
273 val env_reactivity : env -> Typing_defs.reactivity
275 val function_is_mutable : env -> Tast.type_param_mutability option
277 val local_is_mutable : include_borrowed:bool -> env -> Local_id.t -> bool
279 val get_env_mutability : env -> Typing_mutability_env.mutability_env
281 val get_fun : env -> Decl_provider.fun_key -> Decl_provider.fun_decl option
283 val set_env_reactive : env -> Typing_defs.reactivity -> env
285 val set_allow_wildcards : env -> env
287 val get_allow_wildcards : env -> bool
289 val condition_type_matches : is_self:bool -> env -> Tast.ty -> Tast.ty -> bool