2 * Copyright (c) 2015, Facebook, Inc.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
10 (*****************************************************************************)
11 (* Converts a type hint into a type *)
12 (*****************************************************************************)
17 (* Unpacking a hint for typing *)
18 let rec hint env
(p
, h
) =
19 let h = hint_ p env
h in
20 mk
(Typing_reason.Rhint p
, h)
22 and shape_field_info_to_shape_field_type env
{ sfi_optional
; sfi_hint
; _
} =
23 { sft_optional
= sfi_optional
; sft_ty
= hint env sfi_hint
}
25 and aast_user_attribute_to_decl_user_attribute
{ ua_name
; ua_params
} =
29 List.filter_map ua_params ~f
:(function
30 | (_
, Class_const
((_
, CI
(_
, cls
)), (_
, name
)))
31 when String.equal name
SN.Members.mClass
->
36 and aast_contexts_to_decl_capability env ctxs default_pos
=
39 let hl = List.map ~f
:(hint env
) hl in
40 CapTy
(Typing_make_type.intersection
(Reason.Rhint pos
) hl)
41 | None
-> CapDefaults default_pos
43 and aast_tparam_to_decl_tparam env t
=
45 tp_variance
= t
.Aast.tp_variance
;
46 tp_name
= t
.Aast.tp_name
;
48 List.map ~f
:(aast_tparam_to_decl_tparam env
) t
.Aast.tp_parameters
;
50 List.map ~f
:(Tuple.T2.map_snd ~f
:(hint env
)) t
.Aast.tp_constraints
;
51 tp_reified
= t
.Aast.tp_reified
;
54 ~f
:aast_user_attribute_to_decl_user_attribute
55 t
.Aast.tp_user_attributes
;
58 and hint_ p env
= function
59 | Hany
-> Typing_defs.make_tany
()
62 | Hnonnull
-> Tnonnull
64 | Hdynamic
-> Tdynamic
65 | Hnothing
-> Tunion
[]
66 | Hdarray
(h1
, h2
) -> Tdarray
(hint env h1
, hint env h2
)
67 | Hvarray
h -> Tvarray
(hint env
h)
68 | Hvarray_or_darray
(h1
, h2
) ->
71 | Some
h -> hint env
h
72 | None
-> mk
(Typing_reason.Rvarray_or_darray_key p
, Tprim
Aast.Tarraykey
)
74 Tvarray_or_darray
(t1, hint env h2
)
77 let argl = List.map
argl (hint env
) in
82 | Hlike
h -> Tlike
(hint env
h)
85 hf_reactive_kind
= reactivity
;
88 hf_param_mutability
= muts
;
92 hf_is_mutable_return
= mut_ret
;
94 let make_param ((p
, _
) as x
) k mut
=
97 | Some PMutable
-> Some Param_borrowed_mutable
98 | Some POwnedMutable
-> Some Param_owned_mutable
99 | Some PMaybeMutable
-> Some Param_maybe_mutable
105 fp_type
= possibly_enforced_hint env x
;
108 ~mode
:(get_param_mode k
)
109 ~accept_disposable
:false
113 (* Currently do not support external and cancall on parameters of function parameters *)
117 fp_rx_annotation
= None
;
120 let paraml = List.map3_exn
hl kl muts ~f
:make_param in
121 let implicit_params =
122 let capability = aast_contexts_to_decl_capability env ctxs p
in
125 let ret = possibly_enforced_hint env
h in
128 | Some t
-> Fvariadic
(make_param t None None
)
132 match reactivity with
134 | FReactive
-> Reactive None
135 | FShallow
-> Shallow None
136 | FLocal
-> Local None
137 | FNonreactive
-> Nonreactive
143 ft_where_constraints
= [];
145 ft_implicit_params
= implicit_params;
151 ~return_disposable
:false
152 ~returns_void_to_rx
:false
153 ~returns_mutable
:mut_ret
;
154 ft_reactive
= reactivity;
155 (* TODO: handle function parameters with <<CanCall>> *)
156 ft_ifc_decl
= default_ifc_fun_decl
;
158 | Happly
(id
, argl) ->
159 let argl = List.map
argl (hint env
) in
161 | Haccess
((_
, Hvar n
), [(_
, id
)]) -> Tgeneric
("T" ^ n ^
"@" ^ id
, [])
162 | Haccess
(root_ty
, ids
) ->
163 let root_ty = hint_ p env
(snd
root_ty) in
164 let rec translate res ids
=
168 translate (Taccess
(mk
(Typing_reason.Rhint p
, res
), id
)) ids
170 translate root_ty ids
172 let tyl = List.map
hl (hint env
) in
175 let tyl = List.map
hl (hint env
) in
177 | Hintersection
hl ->
178 let tyl = List.map
hl (hint env
) in
180 | Hshape
{ nsi_allows_unknown_fields
; nsi_field_map
} ->
182 if nsi_allows_unknown_fields
then
192 (shape_field_info_to_shape_field_type env i
)
197 Tshape
(shape_kind, fdm)
198 | Hsoft
(p
, h_
) -> hint_ p env h_
199 | Hfun_context n
-> Tgeneric
("Tctx" ^ n
, [])
200 | Hvar n
-> Tgeneric
("T" ^ n
, [])
202 and possibly_enforced_hint env
h =
203 (* Initially we assume that a type is not enforced at runtime.
204 * We refine this during localization
206 { et_enforced
= false; et_type
= hint env
h }