typing TypeStructure<T>
[hiphop-php.git] / hphp / hack / src / typing / typing_structure.ml
blob864d9cb12cea25cfcc074d4d4c7cda22dba3fda6
1 (**
2 * Copyright (c) 2015, Facebook, Inc.
3 * All rights reserved.
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the "hack" directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
9 *)
11 (* This module implements the typing for type_structure. *)
12 open Core
13 open Nast
14 open Typing_defs
16 module Env = Typing_env
17 module Reason = Typing_reason
18 module SN = Naming_special_names
19 module TUtils = Typing_utils
21 (* For TypeStructure<T> (which is an alias for a shape) where the type
22 parameter T is a class/enum/interface or an alias that resolves to a
23 class, we can guarantee the existence of the 'classname' field, hence
24 this function transforms the 'classname' field of the TypeStructure<T>
25 from ?classname<T> to classname<T>. *)
26 let transform_classname_ty ty =
27 match ty with
28 | r, Tshape (fk, fdm) ->
29 let p = Reason.to_pos r in
30 let kClassname = Nast.SFlit (p, "classname") in
31 let f_clsname = ShapeMap.get kClassname fdm in
32 (match f_clsname with
33 | Some (_, Toption (_, Tabstract (AKnewtype (c, tyl), _) as fty))
34 when c = SN.Classes.cClassname ->
35 (match List.hd tyl with
36 | Some tty ->
37 (match TUtils.get_base_type tty with
38 | (_, Tclass _)
39 | (_, Tabstract (AKenum _, _)) ->
40 let fdm = ShapeMap.add kClassname fty fdm in
41 (r, Tshape (fk, fdm))
42 | _ -> ty)
43 | _ -> ty)
44 | _ -> ty (* should not reach *))
45 | _ -> ty (* should not reach *)