bubble ctx for decls 2/4
[hiphop-php.git] / hphp / hack / src / providers / shallow_classes_provider.ml
blob0a4bb1957ed82fc44beaf178ae5c9196cf7dc7fd
1 (*
2 * Copyright (c) Facebook, Inc. and its affiliates.
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the "hack" directory of this source tree.
7 *)
9 open Core_kernel
10 open Shallow_decl_defs
12 (** Fetches the shallow decl, optionally keeping it in cache.
13 When might we want to keep it in cache? e.g. if we're using lazy (shallow)
14 decls, then all shallow decls should be kept in cache, so that subsequent
15 linearizations or searches can find them, and so that even if linearizations
16 get evicted then the shallow cache is still available.
17 Why might we not want to keep it in the cache? e.g. if we're using eager (folded)
18 decls, then we incidentally obtain shallow decls in the process of generating
19 a folded decl, but the folded decl contains everything that one could ever need,
20 and is never evicted, and nno one will ever go back to the shallow decl again,
21 so keeping it around would just be a waste of memory. *)
22 let decl (ctx : Provider_context.t) ~(use_cache : bool) (class_ : Nast.class_) :
23 shallow_class =
24 match ctx.Provider_context.backend with
25 | Provider_backend.Shared_memory ->
26 if use_cache then
27 Shallow_classes_heap.class_decl_if_missing ctx class_
28 else
29 Shallow_classes_heap.class_naming_and_decl ctx class_
30 | Provider_backend.Local_memory _ ->
31 Shallow_classes_heap.class_naming_and_decl ctx class_
32 | Provider_backend.Decl_service _ ->
33 failwith "shallow class decl not implemented for Decl_service"
35 let get_class_filename x =
36 match Naming_table.Types.get_filename_and_kind x with
37 | Some (fn, Naming_table.TClass) -> Some fn
38 | _ -> None
40 let get (ctx : Provider_context.t) (name : string) : shallow_class option =
41 if not (TypecheckerOptions.shallow_class_decl ctx.Provider_context.tcopt) then
42 failwith "shallow_class_decl not enabled"
43 else
44 match ctx.Provider_context.backend with
45 | Provider_backend.Shared_memory -> Shallow_classes_heap.get ctx name
46 | Provider_backend.Local_memory { shallow_decl_cache; _ } ->
47 Provider_backend.Shallow_decl_cache.find_or_add
48 shallow_decl_cache
49 ~key:(Provider_backend.Shallow_decl_cache_entry.Shallow_class_decl name)
50 ~default:(fun () ->
51 let open Option.Monad_infix in
52 get_class_filename name >>= fun path ->
53 Ast_provider.find_class_in_file ctx path name >>| fun class_ ->
54 Shallow_classes_heap.class_naming_and_decl ctx class_)
55 | Provider_backend.Decl_service _ ->
56 failwith "shallow class cache lookup not implemented for Decl_service"
58 let get_batch (ctx : Provider_context.t) (names : SSet.t) :
59 shallow_class option SMap.t =
60 match ctx.Provider_context.backend with
61 | Provider_backend.Shared_memory -> Shallow_classes_heap.get_batch names
62 | Provider_backend.Local_memory _ ->
63 failwith "get_batch not implemented for Local_memory"
64 | Provider_backend.Decl_service _ ->
65 failwith "get_batch not implemented for Decl_service"
67 let get_old_batch (ctx : Provider_context.t) (names : SSet.t) :
68 shallow_class option SMap.t =
69 match ctx.Provider_context.backend with
70 | Provider_backend.Shared_memory -> Shallow_classes_heap.get_old_batch names
71 | Provider_backend.Local_memory _ ->
72 failwith "get_old_batch not implemented for Local_memory"
73 | Provider_backend.Decl_service _ ->
74 failwith "get_old_batch not implemented for Decl_service"
76 let oldify_batch (ctx : Provider_context.t) (names : SSet.t) : unit =
77 match ctx.Provider_context.backend with
78 | Provider_backend.Shared_memory -> Shallow_classes_heap.oldify_batch names
79 | Provider_backend.Local_memory _ ->
80 failwith "oldify_batch not implemented for Local_memory"
81 | Provider_backend.Decl_service _ ->
82 failwith "oldify_batch not implemented for Decl_service"
84 let remove_old_batch (ctx : Provider_context.t) (names : SSet.t) : unit =
85 match ctx.Provider_context.backend with
86 | Provider_backend.Shared_memory ->
87 Shallow_classes_heap.remove_old_batch names
88 | Provider_backend.Local_memory _ ->
89 failwith "remove_old_batch not implemented for Local_memory"
90 | Provider_backend.Decl_service _ ->
91 failwith "remove_old_batch not implemented for Decl_service"
93 let remove_batch (ctx : Provider_context.t) (names : SSet.t) : unit =
94 match ctx.Provider_context.backend with
95 | Provider_backend.Shared_memory -> Shallow_classes_heap.remove_batch names
96 | Provider_backend.Local_memory _ ->
97 failwith "remove_batch not implemented for Local_memory"
98 | Provider_backend.Decl_service _ ->
99 failwith "remove_batch not implemented for Decl_service"
101 let invalidate_class (ctx : Provider_context.t) (class_name : string) : unit =
102 match ctx.Provider_context.backend with
103 | Provider_backend.Shared_memory ->
104 remove_batch ctx (SSet.singleton class_name)
105 | Provider_backend.Local_memory { decl_cache = _; shallow_decl_cache } ->
106 Provider_backend.Shallow_decl_cache.remove
107 shallow_decl_cache
108 ~key:
109 (Provider_backend.Shallow_decl_cache_entry.Shallow_class_decl class_name)
110 | Provider_backend.Decl_service _ ->
111 failwith
112 "Decl_provider.invalidate_class not yet impl. for decl memory provider"
114 let invalidate_context_decls ~(ctx : Provider_context.t) : unit =
115 Relative_path.Map.iter ctx.Provider_context.entries ~f:(fun _ entry ->
116 let (_funs, classes, _record_defs, _typedefs, _gconsts) =
117 Nast.get_defs entry.Provider_context.ast
119 List.iter classes ~f:(fun (_, class_name) ->
120 invalidate_class ctx class_name))
122 let push_local_changes (ctx : Provider_context.t) : unit =
123 match ctx.Provider_context.backend with
124 | Provider_backend.Shared_memory -> Shallow_classes_heap.push_local_changes ()
125 | Provider_backend.Local_memory _ -> invalidate_context_decls ctx
126 | Provider_backend.Decl_service _ ->
127 failwith "push_local_changes not implemented for Decl_service"
129 let pop_local_changes (ctx : Provider_context.t) : unit =
130 match ctx.Provider_context.backend with
131 | Provider_backend.Shared_memory -> Shallow_classes_heap.pop_local_changes ()
132 | Provider_backend.Local_memory _ -> invalidate_context_decls ctx
133 | Provider_backend.Decl_service _ ->
134 failwith "pop_local_changes not implemented for Decl_service"