Don't check parameter type coercion
[hiphop-php.git] / hphp / hack / src / decl / decl_pos_utils.ml
blob71bf5be466f02a775795a372aa0d8f90cba5f499
1 (*
2 * Copyright (c) 2015, 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 open Hh_prelude
11 open Decl_defs
12 open Shallow_decl_defs
13 open Typing_defs
15 (*****************************************************************************)
16 (* Functor traversing a type, but applies a user defined function for
17 * positions. Positions in errors (_decl_errors) are not mapped - entire
18 * field is erased instead. This is safe because there exists a completely
19 * different system for tracking and updating positions in errors
20 * (see ServerTypeCheck.get_files_with_stale_errors)
22 (*****************************************************************************)
23 module TraversePos (ImplementPos : sig
24 val pos : Pos.t -> Pos.t
26 val pos_or_decl : Pos_or_decl.t -> Pos_or_decl.t
27 end) =
28 struct
29 open Typing_reason
31 let pos = ImplementPos.pos
33 let pos_or_decl = ImplementPos.pos_or_decl
35 let positioned_id : Typing_defs.pos_id -> Typing_defs.pos_id =
36 (fun (p, x) -> (pos_or_decl p, x))
38 let rec ty t =
39 let (r, x) = deref t in
40 mk (map_pos pos pos_or_decl r, ty_ x)
42 and ty_ : decl_phase ty_ -> decl_phase ty_ = function
43 | (Tany _ | Tthis | Tmixed | Twildcard | Tnonnull | Tdynamic) as x -> x
44 | Tvec_or_dict (ty1, ty2) -> Tvec_or_dict (ty ty1, ty ty2)
45 | Tprim _ as x -> x
46 | Tgeneric (name, args) -> Tgeneric (name, List.map args ~f:ty)
47 | Ttuple tyl -> Ttuple (List.map tyl ~f:ty)
48 | Tunion tyl -> Tunion (List.map tyl ~f:ty)
49 | Tintersection tyl -> Tintersection (List.map tyl ~f:ty)
50 | Toption x -> Toption (ty x)
51 | Tlike x -> Tlike (ty x)
52 | Tfun ft -> Tfun (fun_type ft)
53 | Tapply (sid, xl) -> Tapply (positioned_id sid, List.map xl ~f:ty)
54 | Taccess (root_ty, id) -> Taccess (ty root_ty, positioned_id id)
55 | Trefinement (root_ty, rs) ->
56 let rs = Class_refinement.map ty rs in
57 Trefinement (ty root_ty, rs)
58 | Tshape s -> Tshape (shape_type s)
60 and ty_opt x = Option.map x ~f:ty
62 and shape_field_name = function
63 | Typing_defs.TSFregex_group (p, s) ->
64 Typing_defs.TSFregex_group (pos_or_decl p, s)
65 | Typing_defs.TSFlit_str (p, s) -> Typing_defs.TSFlit_str (pos_or_decl p, s)
66 | Typing_defs.TSFclass_const (id, s) ->
67 Typing_defs.TSFclass_const (positioned_id id, positioned_id s)
69 and constraint_ x = List.map ~f:(fun (ck, x) -> (ck, ty x)) x
71 and capability = function
72 | CapTy cap -> CapTy (ty cap)
73 | CapDefaults p -> CapDefaults (pos_or_decl p)
75 and fun_implicit_params implicit =
76 { capability = capability implicit.capability }
78 and fun_type ft =
80 ft with
81 ft_tparams = List.map ~f:type_param ft.ft_tparams;
82 ft_where_constraints =
83 List.map ft.ft_where_constraints ~f:where_constraint;
84 ft_params = List.map ft.ft_params ~f:fun_param;
85 ft_implicit_params = fun_implicit_params ft.ft_implicit_params;
86 ft_ret = ty ft.ft_ret;
89 and shape_type { s_origin = _; s_unknown_value = shape_kind; s_fields = fdm }
91 (* TODO(shapes) Should this be changing s_origin? *)
93 s_origin = Missing_origin;
94 s_unknown_value = shape_kind;
95 (* TODO(shapes) s_unknown_value is missing a call to ty *)
96 s_fields = ShapeFieldMap.map_and_rekey fdm shape_field_name ty;
99 and fun_elt fe =
100 { fe with fe_type = ty fe.fe_type; fe_pos = pos_or_decl fe.fe_pos }
102 and where_constraint (ty1, c, ty2) = (ty ty1, c, ty ty2)
104 and fun_param param =
105 { param with fp_pos = pos_or_decl param.fp_pos; fp_type = ty param.fp_type }
107 and class_const cc =
109 cc_synthesized = cc.cc_synthesized;
110 cc_abstract = cc.cc_abstract;
111 cc_pos = pos_or_decl cc.cc_pos;
112 cc_type = ty cc.cc_type;
113 cc_origin = cc.cc_origin;
114 cc_refs = cc.cc_refs;
117 and typeconst = function
118 | TCAbstract { atc_as_constraint; atc_super_constraint; atc_default } ->
119 TCAbstract
121 atc_as_constraint = ty_opt atc_as_constraint;
122 atc_super_constraint = ty_opt atc_super_constraint;
123 atc_default = ty_opt atc_default;
125 | TCConcrete { tc_type } -> TCConcrete { tc_type = ty tc_type }
127 and typeconst_type tc =
129 ttc_synthesized = tc.ttc_synthesized;
130 ttc_name = positioned_id tc.ttc_name;
131 ttc_kind = typeconst tc.ttc_kind;
132 ttc_origin = tc.ttc_origin;
133 ttc_enforceable = Tuple.T2.map_fst ~f:pos_or_decl tc.ttc_enforceable;
134 ttc_reifiable = Option.map tc.ttc_reifiable ~f:pos_or_decl;
135 ttc_concretized = tc.ttc_concretized;
136 ttc_is_ctx = tc.ttc_is_ctx;
139 and user_attribute { ua_name; ua_params; _ } =
140 { ua_name = positioned_id ua_name; ua_params; ua_raw_val = None }
142 and type_param t =
144 tp_name = positioned_id t.tp_name;
145 tp_variance = t.tp_variance;
146 tp_reified = t.tp_reified;
147 tp_tparams = List.map ~f:type_param t.tp_tparams;
148 tp_constraints = constraint_ t.tp_constraints;
149 tp_user_attributes = List.map ~f:user_attribute t.tp_user_attributes;
152 and class_type dc =
154 dc_final = dc.dc_final;
155 dc_const = dc.dc_const;
156 dc_internal = dc.dc_internal;
157 dc_need_init = dc.dc_need_init;
158 dc_deferred_init_members = dc.dc_deferred_init_members;
159 dc_abstract = dc.dc_abstract;
160 dc_kind = dc.dc_kind;
161 dc_is_xhp = dc.dc_is_xhp;
162 dc_has_xhp_keyword = dc.dc_has_xhp_keyword;
163 dc_module = dc.dc_module;
164 dc_is_module_level_trait = dc.dc_is_module_level_trait;
165 dc_name = dc.dc_name;
166 dc_pos = dc.dc_pos;
167 dc_extends = dc.dc_extends;
168 dc_sealed_whitelist = dc.dc_sealed_whitelist;
169 dc_xhp_attr_deps = dc.dc_xhp_attr_deps;
170 dc_xhp_enum_values = dc.dc_xhp_enum_values;
171 dc_xhp_marked_empty = dc.dc_xhp_marked_empty;
172 dc_req_ancestors = List.map dc.dc_req_ancestors ~f:requirement;
173 dc_req_ancestors_extends = dc.dc_req_ancestors_extends;
174 dc_req_class_ancestors = List.map dc.dc_req_class_ancestors ~f:requirement;
175 dc_tparams = List.map dc.dc_tparams ~f:type_param;
176 dc_substs =
177 SMap.map
178 begin
179 fun ({ sc_subst; _ } as sc) ->
180 { sc with sc_subst = SMap.map ty sc_subst }
182 dc.dc_substs;
183 dc_consts = SMap.map class_const dc.dc_consts;
184 dc_typeconsts = SMap.map typeconst_type dc.dc_typeconsts;
185 dc_props = dc.dc_props;
186 dc_sprops = dc.dc_sprops;
187 dc_methods = dc.dc_methods;
188 dc_smethods = dc.dc_smethods;
189 dc_construct = dc.dc_construct;
190 dc_ancestors = SMap.map ty dc.dc_ancestors;
191 dc_support_dynamic_type = dc.dc_support_dynamic_type;
192 dc_enum_type = Option.map dc.dc_enum_type ~f:enum_type;
193 dc_decl_errors = [];
194 dc_docs_url = dc.dc_docs_url;
195 dc_allow_multiple_instantiations = dc.dc_allow_multiple_instantiations;
196 dc_sort_text = dc.dc_sort_text;
197 dc_package_override = dc.dc_package_override;
200 and requirement (p, t) = (pos_or_decl p, ty t)
202 and enum_type te =
204 te_base = ty te.te_base;
205 te_constraint = ty_opt te.te_constraint;
206 te_includes = List.map te.te_includes ~f:ty;
209 and typedef tdef =
211 td_module = tdef.td_module;
212 td_pos = pos_or_decl tdef.td_pos;
213 td_vis = tdef.td_vis;
214 td_tparams = List.map tdef.td_tparams ~f:type_param;
215 td_as_constraint = ty_opt tdef.td_as_constraint;
216 td_super_constraint = ty_opt tdef.td_super_constraint;
217 td_type = ty tdef.td_type;
218 td_is_ctx = tdef.td_is_ctx;
219 td_attributes = List.map tdef.td_attributes ~f:user_attribute;
220 td_internal = tdef.td_internal;
221 td_docs_url = tdef.td_docs_url;
222 td_package_override = tdef.td_package_override;
225 and shallow_class sc =
227 sc_mode = sc.sc_mode;
228 sc_final = sc.sc_final;
229 sc_abstract = sc.sc_abstract;
230 sc_is_xhp = sc.sc_is_xhp;
231 sc_internal = sc.sc_internal;
232 sc_has_xhp_keyword = sc.sc_has_xhp_keyword;
233 sc_kind = sc.sc_kind;
234 sc_module = sc.sc_module;
235 sc_name = positioned_id sc.sc_name;
236 sc_tparams = List.map sc.sc_tparams ~f:type_param;
237 sc_extends = List.map sc.sc_extends ~f:ty;
238 sc_uses = List.map sc.sc_uses ~f:ty;
239 sc_xhp_attr_uses = List.map sc.sc_xhp_attr_uses ~f:ty;
240 sc_xhp_enum_values = sc.sc_xhp_enum_values;
241 sc_xhp_marked_empty = sc.sc_xhp_marked_empty;
242 sc_req_extends = List.map sc.sc_req_extends ~f:ty;
243 sc_req_implements = List.map sc.sc_req_implements ~f:ty;
244 sc_req_class = List.map sc.sc_req_class ~f:ty;
245 sc_implements = List.map sc.sc_implements ~f:ty;
246 sc_support_dynamic_type = sc.sc_support_dynamic_type;
247 sc_consts = List.map sc.sc_consts ~f:shallow_class_const;
248 sc_typeconsts = List.map sc.sc_typeconsts ~f:shallow_typeconst;
249 sc_props = List.map sc.sc_props ~f:shallow_prop;
250 sc_sprops = List.map sc.sc_sprops ~f:shallow_prop;
251 sc_constructor = Option.map sc.sc_constructor ~f:shallow_method;
252 sc_static_methods = List.map sc.sc_static_methods ~f:shallow_method;
253 sc_methods = List.map sc.sc_methods ~f:shallow_method;
254 sc_user_attributes = List.map sc.sc_user_attributes ~f:user_attribute;
255 sc_enum_type = Option.map sc.sc_enum_type ~f:enum_type;
256 sc_docs_url = sc.sc_docs_url;
257 sc_package_override = sc.sc_package_override;
260 and shallow_class_const scc =
262 scc_abstract = scc.scc_abstract;
263 scc_name = positioned_id scc.scc_name;
264 scc_type = ty scc.scc_type;
265 scc_refs = scc.scc_refs;
266 scc_value = scc.scc_value;
269 and shallow_typeconst stc =
271 stc_kind = typeconst stc.stc_kind;
272 stc_name = positioned_id stc.stc_name;
273 stc_enforceable =
274 (pos_or_decl (fst stc.stc_enforceable), snd stc.stc_enforceable);
275 stc_reifiable = Option.map stc.stc_reifiable ~f:pos_or_decl;
276 stc_is_ctx = stc.stc_is_ctx;
279 and shallow_prop sp =
281 sp_name = positioned_id sp.sp_name;
282 sp_xhp_attr = sp.sp_xhp_attr;
283 sp_type = ty sp.sp_type;
284 sp_visibility = sp.sp_visibility;
285 sp_flags = sp.sp_flags;
288 and shallow_method sm =
290 sm_name = positioned_id sm.sm_name;
291 sm_type = ty sm.sm_type;
292 sm_visibility = sm.sm_visibility;
293 sm_deprecated = sm.sm_deprecated;
294 sm_flags = sm.sm_flags;
295 sm_attributes = sm.sm_attributes;
296 sm_sort_text = sm.sm_sort_text;
300 (*****************************************************************************)
301 (* Returns a signature with all the positions replaced with Pos.none *)
302 (*****************************************************************************)
303 module NormalizeSig = TraversePos (struct
304 let pos _ = Pos.none
306 let pos_or_decl _ = Pos_or_decl.none
307 end)