Move from symbol-level modules to file level modules
[hiphop-php.git] / hphp / hack / src / decl / decl.ml
blobbf5b1babf0e190fabc13dd16e1493ad8592106f1
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
12 let shallow_decl_enabled (ctx : Provider_context.t) : bool =
13 TypecheckerOptions.shallow_class_decl (Provider_context.get_tcopt ctx)
15 let use_direct_decl_parser ctx =
16 TypecheckerOptions.use_direct_decl_parser (Provider_context.get_tcopt ctx)
18 let class_decl_if_missing_DEPRECATED
19 ~(sh : SharedMem.uses) (ctx : Provider_context.t) (c : Nast.class_) : unit =
20 if shallow_decl_enabled ctx then
21 let (_ : Shallow_decl_defs.shallow_class) =
22 Shallow_classes_provider.decl_DEPRECATED ctx c
25 else
26 let (_ : _ option) =
27 Decl_folded_class.class_decl_if_missing ~sh ctx (snd c.Aast.c_name)
31 let rec name_and_declare_types_program_DEPRECATED
32 (acc : Direct_decl_parser.decls)
33 ~(sh : SharedMem.uses option)
34 (ctx : Provider_context.t)
35 (prog : Nast.program) : Direct_decl_parser.decls =
36 let with_sh f = Option.value_map ~default:() ~f:(fun sh -> f sh) sh in
37 let open Aast in
38 let module_ =
39 List.filter_map prog ~f:(function
40 | FileAttributes f -> Some f
41 | _ -> None)
42 |> Naming_attributes_params.get_module_attribute
43 |> Typing_modules.of_maybe_string
45 List.fold prog ~init:acc ~f:(fun acc def ->
46 match def with
47 | Namespace (_, prog) ->
48 name_and_declare_types_program_DEPRECATED acc ~sh ctx prog
49 | NamespaceUse _ -> acc
50 | SetNamespaceEnv _ -> acc
51 | FileAttributes _ -> acc
52 | Fun f ->
53 let (name, decl) = Decl_nast.fun_naming_and_decl_DEPRECATED ctx f in
54 with_sh (fun _ -> Decl_store.((get ()).add_fun name decl));
55 (name, Shallow_decl_defs.Fun decl) :: acc
56 | Class c ->
57 with_sh (fun sh -> class_decl_if_missing_DEPRECATED ~sh ctx c);
58 let class_ = Shallow_classes_provider.decl_DEPRECATED ctx c in
59 (snd class_.Shallow_decl_defs.sc_name, Shallow_decl_defs.Class class_)
60 :: acc
61 | RecordDef rd ->
62 let (name, decl) =
63 Decl_nast.record_def_naming_and_decl_DEPRECATED ctx rd module_
65 with_sh (fun _ -> Decl_store.((get ()).add_recorddef name decl));
66 (name, Shallow_decl_defs.Record decl) :: acc
67 | Typedef typedef ->
68 let (name, decl) =
69 Decl_nast.typedef_naming_and_decl_DEPRECATED ctx typedef
71 with_sh (fun _ -> Decl_store.((get ()).add_typedef name decl));
72 (name, Shallow_decl_defs.Typedef decl) :: acc
73 | Stmt _ -> acc
74 | Constant cst ->
75 let (name, decl) = Decl_nast.const_naming_and_decl_DEPRECATED ctx cst in
76 with_sh (fun _ -> Decl_store.((get ()).add_gconst name decl));
77 (name, Shallow_decl_defs.Const decl) :: acc)
79 let nast_to_decls_DEPRECATED
80 (acc : Direct_decl_parser.decls)
81 (ctx : Provider_context.t)
82 (prog : Nast.program) : Direct_decl_parser.decls =
83 match Provider_context.get_backend ctx with
84 | Provider_backend.Analysis -> []
85 | _ -> name_and_declare_types_program_DEPRECATED acc ~sh:None ctx prog
87 let make_env
88 ~(sh : SharedMem.uses) (ctx : Provider_context.t) (fn : Relative_path.t) :
89 unit =
90 if use_direct_decl_parser ctx then (
91 match Direct_decl_utils.direct_decl_parse_and_cache ctx fn with
92 | None -> ()
93 | Some parsed_file ->
94 if not (shallow_decl_enabled ctx) then
95 List.iter parsed_file.Direct_decl_utils.pfh_decls ~f:(function
96 | (name, Shallow_decl_defs.Class _, _) ->
97 let (_ : _ option) =
98 Decl_folded_class.class_decl_if_missing ~sh ctx name
101 | _ -> ())
102 ) else
103 let ast = Ast_provider.get_ast ctx fn in
104 let (_ : Direct_decl_parser.decls) =
105 name_and_declare_types_program_DEPRECATED [] ~sh:(Some sh) ctx ast