small refactorings
[hiphop-php.git] / hphp / hack / src / rupro / depgraph_api / depgraph_api.rs
blobc53c9efa23ccaca79ba00492b2873fdfee3166e9
1 // Copyright (c) Facebook, Inc. and its affiliates.
2 //
3 // This source code is licensed under the MIT license found in the
4 // LICENSE file in the "hack" directory of this source tree.
6 use deps_rust::Dep;
7 use pos::{ConstName, FunName, MethodName, PropName, TypeName};
8 use std::fmt::Debug;
10 pub type Result<T, E = Error> = std::result::Result<T, E>;
12 #[derive(thiserror::Error, Debug)]
13 pub enum Error {}
15 // Implementations of `FoldedDeclProvider` need to be able to record
16 // dependencies (if needed). We do this by having the functions of this
17 // trait take a "who's asking?" symbol of this type.
18 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
19 pub enum DeclName {
20     Fun(FunName),
21     Const(ConstName),
22     Type(TypeName),
25 impl From<FunName> for DeclName {
26     fn from(name: FunName) -> Self {
27         Self::Fun(name)
28     }
31 impl From<ConstName> for DeclName {
32     fn from(name: ConstName) -> Self {
33         Self::Const(name)
34     }
37 impl From<TypeName> for DeclName {
38     fn from(name: TypeName) -> Self {
39         Self::Type(name)
40     }
43 // nb(sf, 2022-03-15): c.f. ` Typing_deps.Dep.variant`
44 #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
45 /// A node in the dependency graph that, when changed, must recheck all of its
46 /// dependents.
47 pub enum DependencyName {
48     /// Represents another class depending on a class via an inheritance-like
49     /// mechanism (`extends`, `implements`, `use`, `require extends`, `require
50     /// implements`, etc.)
51     Extends(TypeName),
52     /// Represents something depending on a class constant.
53     Const(ConstName),
54     /// Represents something depending on a class constructor.
55     Constructor(TypeName),
56     /// Represents something depending on a class's instance property.
57     Prop(TypeName, PropName),
58     /// Represents something depending on a class's static property.
59     StaticProp(TypeName, PropName),
60     /// Represents something depending on a class's instance method.
61     Method(TypeName, MethodName),
62     /// Represents something depending on a class's static method.
63     StaticMethod(TypeName, MethodName),
64     /// Represents something depending on all members of a class. Particularly
65     /// useful for switch exhaustiveness-checking. We establish a dependency on
66     /// all members of an enum in that case.
67     AllMembers(TypeName),
68     /// Represents something depending on a global constant.
69     GConst(ConstName),
70     /// Represents something depending on a global function.
71     Fun(FunName),
72     /// Represents something depending on a class/typedef/trait/interface
73     Type(TypeName),
76 /// Organize and administer dependency records.
77 pub trait DepGraphWriter: Debug + Send + Sync {
78     /// Record a dependency.
79     // e.g. If class B extends A {} then A <- B (B depends on A). So here,
80     // dependent is B and dependency is A.
81     fn add_dependency(&self, dependent: DeclName, dependency: DependencyName) -> Result<()>;
84 /// Query dependency records.
85 pub trait DepGraphReader: Debug + Send + Sync {
86     /// Retrieve dependents of a name.
87     fn get_dependents(&self, dependency: DependencyName) -> Box<dyn Iterator<Item = Dep>>;
90 /// A no-op implementation of the `DepGraphReader` & `DepGraphWriter` traits.
91 /// All registered edges are thrown away.
92 #[derive(Debug, Clone)]
93 pub struct NoDepGraph(());
95 impl NoDepGraph {
96     pub fn new() -> Self {
97         Self(())
98     }
101 impl DepGraphWriter for NoDepGraph {
102     fn add_dependency(&self, _dependent: DeclName, _dependency: DependencyName) -> Result<()> {
103         Ok(())
104     }
107 impl DepGraphReader for NoDepGraph {
108     fn get_dependents(&self, _dependency: DependencyName) -> Box<dyn Iterator<Item = Dep>> {
109         Box::new(std::iter::empty())
110     }
113 /// Read & write dependency records.
114 pub trait DepGraph: DepGraphReader + DepGraphWriter {}
115 impl<T: DepGraphReader + DepGraphWriter> DepGraph for T {}