aft_parse/callgraph_store/dispatch/
static_receiver.rs1#[path = "static_receiver/bindings.rs"]
4mod bindings;
5#[path = "static_receiver/codec.rs"]
6mod codec;
7#[path = "static_receiver/csharp_membership.rs"]
8mod csharp_membership;
9#[path = "static_receiver/csharp_projects.rs"]
10mod csharp_projects;
11pub use csharp_projects::CSharpProjects;
12#[path = "static_receiver/extract.rs"]
13mod extract;
14#[path = "static_receiver/msbuild.rs"]
15mod msbuild;
16#[path = "static_receiver/resolve.rs"]
17mod resolve;
18
19fn relative_module(module: &str) -> bool {
20 matches!(module, "." | "..") || module.starts_with("./") || module.starts_with("../")
21}
22
23pub(super) use extract::Index;
24pub(in crate::callgraph_store) use resolve::STATIC_IMPORT_PREFIX;
25
26use std::collections::{BTreeMap, BTreeSet};
27
28use serde::{Deserialize, Serialize};
29
30#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
31pub struct StaticFacts {
32 #[serde(default, skip_serializing_if = "Vec::is_empty")]
33 namespaces: Vec<String>,
34 #[serde(default, skip_serializing_if = "Vec::is_empty")]
35 pub types: Vec<Declaration>,
36 #[serde(default, skip_serializing_if = "Vec::is_empty")]
37 scopes: Vec<Scope>,
38 #[serde(default, skip_serializing_if = "Vec::is_empty")]
39 receivers: Vec<Receiver>,
40 #[serde(default, skip_serializing_if = "Vec::is_empty")]
41 sites: Vec<(u32, u32)>,
42}
43
44#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
45pub struct Declaration {
46 pub name: String,
47 scope: u32,
48 #[serde(default, skip_serializing_if = "Vec::is_empty")]
49 bases: Vec<String>,
50 #[serde(default, skip_serializing_if = "Vec::is_empty")]
51 values: Vec<String>,
52 #[serde(default, skip_serializing_if = "Vec::is_empty")]
53 methods: Vec<(String, String)>,
54}
55
56#[derive(Clone, Debug, Default, Deserialize, Eq, Hash, PartialEq, Serialize)]
57struct Scope {
58 namespace: String,
59 #[serde(default, skip_serializing_if = "Vec::is_empty")]
60 imports: Vec<String>,
61 #[serde(default, skip_serializing_if = "Vec::is_empty")]
62 aliases: Vec<(String, String)>,
63}
64
65#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
66struct Receiver {
67 name: String,
68 scope: u32,
69 owner: Option<u32>,
70 #[serde(default, skip_serializing_if = "Option::is_none")]
71 module: Option<String>,
72}
73
74#[derive(Serialize)]
76pub struct StaticSiteEvidence<'a> {
77 pub receiver: &'a str,
78 pub enclosing_type: Option<&'a str>,
79 pub namespace: &'a str,
80 pub imports: &'a [String],
81 pub aliases: &'a [(String, String)],
82 pub import_module: Option<&'a str>,
83}
84
85impl StaticFacts {
86 pub fn is_empty(&self) -> bool {
87 self.types.is_empty() && self.sites.is_empty() && self.namespaces.is_empty()
88 }
89
90 pub fn type_key(language: &str, name: &str) -> String {
92 format!("s:{language}\t{name}")
93 }
94
95 pub(crate) fn dependency(language: &str, name: &str) -> String {
98 let short = name.rsplit('.').next().unwrap_or(name);
99 format!("st:{language}\t{short}")
100 }
101
102 pub fn type_keys<'a>(&'a self, language: &'a str) -> impl Iterator<Item = String> + 'a {
103 self.types
104 .iter()
105 .filter_map(move |ty| {
106 matches!(language, "java" | "csharp").then(|| Self::type_key(language, &ty.name))
107 })
108 .chain(
109 self.namespaces
110 .iter()
111 .map(move |namespace| Self::namespace_key(language, namespace)),
112 )
113 }
114
115 fn namespace_key(language: &str, namespace: &str) -> String {
116 format!("sn:{language}\t{namespace}")
117 }
118
119 pub(super) fn site_key(&self, ordinal: u32) -> Option<u32> {
120 let index = self
121 .sites
122 .binary_search_by_key(&ordinal, |(site, _)| *site)
123 .ok()?;
124 Some(self.sites[index].1)
125 }
126
127 pub fn site_evidence(&self, ordinal: u32) -> Option<StaticSiteEvidence<'_>> {
128 let receiver = &self.receivers[self.site_key(ordinal)? as usize];
129 let scope = &self.scopes[receiver.scope as usize];
130 Some(StaticSiteEvidence {
131 receiver: &receiver.name,
132 enclosing_type: receiver
133 .owner
134 .map(|id| self.types[id as usize].name.as_str()),
135 namespace: &scope.namespace,
136 imports: &scope.imports,
137 aliases: &scope.aliases,
138 import_module: receiver.module.as_deref(),
139 })
140 }
141
142 pub(in crate::callgraph_store) fn import_receivers(
143 &self,
144 ) -> impl Iterator<Item = (&str, &str)> {
145 self.receivers
146 .iter()
147 .filter_map(|receiver| Some((receiver.name.as_str(), receiver.module.as_deref()?)))
148 }
149
150 pub(super) fn projection_names(&self) -> impl Iterator<Item = &str> {
151 self.types
152 .iter()
153 .flat_map(|ty| ty.bases.iter().map(String::as_str))
154 }
155
156 pub(crate) fn changed_keys(
157 path: &str,
158 language: &str,
159 before: Option<&Self>,
160 after: Option<&Self>,
161 keys: &mut BTreeSet<String>,
162 ) {
163 if before == after {
164 return;
165 }
166 let namespaces = |facts: Option<&Self>| {
167 facts
168 .into_iter()
169 .flat_map(|facts| &facts.namespaces)
170 .cloned()
171 .collect::<BTreeSet<_>>()
172 };
173 let before_namespaces = namespaces(before);
174 let after_namespaces = namespaces(after);
175 keys.extend(
176 before_namespaces
177 .symmetric_difference(&after_namespaces)
178 .map(|name| Self::namespace_key(language, name)),
179 );
180 let before = Self::declarations(before);
181 let after = Self::declarations(after);
182 for name in before.keys().chain(after.keys()) {
183 if before.get(name) != after.get(name) {
184 if matches!(language, "csharp" | "java") {
185 keys.insert(Self::dependency(language, name));
186 } else {
187 keys.insert(super::dependency::file(path));
189 }
190 }
191 }
192 }
193
194 fn declarations(facts: Option<&Self>) -> BTreeMap<&str, Vec<(&Declaration, &Scope)>> {
195 let mut out = BTreeMap::<_, Vec<_>>::new();
196 if let Some(facts) = facts {
197 for ty in &facts.types {
198 out.entry(ty.name.as_str())
199 .or_default()
200 .push((ty, &facts.scopes[ty.scope as usize]));
201 }
202 }
203 out
204 }
205}
206
207aft_store::heap_bytes_of_fields!(StaticFacts {
208 namespaces,
209 types,
210 scopes,
211 receivers,
212 sites
213});
214aft_store::heap_bytes_of_fields!(Declaration {
215 name,
216 scope,
217 bases,
218 values,
219 methods
220});
221aft_store::heap_bytes_of_fields!(Scope {
222 namespace,
223 imports,
224 aliases
225});
226aft_store::heap_bytes_of_fields!(Receiver {
227 name,
228 scope,
229 owner,
230 module
231});