File size: 8,311 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
//! A visitor that traverses the AST and collects all functions or methods that
//! are annotated with `#[turbo_tasks::function]`.
use std::{collections::VecDeque, ops::Add};
use lsp_types::Range;
use syn::{Expr, Meta, visit::Visit};
use crate::identifier::Identifier;
pub struct TaskVisitor {
/// the list of results as pairs of an identifier and its tags
pub results: Vec<(syn::Ident, Vec<String>)>,
}
impl TaskVisitor {
pub fn new() -> Self {
Self {
results: Default::default(),
}
}
}
impl Visit<'_> for TaskVisitor {
#[tracing::instrument(skip_all)]
fn visit_item_fn(&mut self, i: &syn::ItemFn) {
if let Some(tags) = extract_tags(i.attrs.iter()) {
tracing::trace!("L{}: {}", i.sig.ident.span().start().line, i.sig.ident,);
self.results.push((i.sig.ident.clone(), tags));
}
}
#[tracing::instrument(skip_all)]
fn visit_impl_item_fn(&mut self, i: &syn::ImplItemFn) {
if let Some(tags) = extract_tags(i.attrs.iter()) {
tracing::trace!("L{}: {}", i.sig.ident.span().start().line, i.sig.ident,);
self.results.push((i.sig.ident.clone(), tags));
}
}
}
fn extract_tags<'a>(mut meta: impl Iterator<Item = &'a syn::Attribute>) -> Option<Vec<String>> {
meta.find_map(|a| match &a.meta {
// path has two segments, turbo_tasks and function
Meta::Path(path) if path.segments.len() == 2 => {
let first = &path.segments[0];
let second = &path.segments[1];
(first.ident == "turbo_tasks" && second.ident == "function").then(std::vec::Vec::new)
}
Meta::List(list) if list.path.segments.len() == 2 => {
let first = &list.path.segments[0];
let second = &list.path.segments[1];
if first.ident != "turbo_tasks" || second.ident != "function" {
return None;
}
// collect ident tokens as args
let tags: Vec<_> = list
.tokens
.clone()
.into_iter()
.filter_map(|t| {
if let proc_macro2::TokenTree::Ident(ident) = t {
Some(ident.to_string())
} else {
None
}
})
.collect();
Some(tags)
}
_ => {
tracing::trace!("skipping unknown annotation");
None
}
})
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd)]
pub enum CallingStyle {
Once = 0b0010,
ZeroOrOnce = 0b0011,
ZeroOrMore = 0b0111,
OneOrMore = 0b0110,
}
impl CallingStyle {
fn bitset(self) -> u8 {
self as u8
}
}
impl Add for CallingStyle {
type Output = Self;
/// Add two calling styles together to determine the calling style of the
/// target function within the source function.
///
/// Consider it as a bitset over properties.
/// - 0b000: Nothing
/// - 0b001: Zero
/// - 0b010: Once
/// - 0b011: Zero Or Once
/// - 0b100: More Than Once
/// - 0b101: Zero Or More Than Once (?)
/// - 0b110: Once Or More
/// - 0b111: Zero Or More
///
/// Note that zero is not a valid calling style.
fn add(self, rhs: Self) -> Self {
let left = self.bitset();
let right = rhs.bitset();
// we treat this as a bitset under addition
#[allow(clippy::suspicious_arithmetic_impl)]
match left | right {
0b0010 => CallingStyle::Once,
0b011 => CallingStyle::ZeroOrOnce,
0b0111 => CallingStyle::ZeroOrMore,
0b0110 => CallingStyle::OneOrMore,
// the remaining 4 (null, zero, more than once, zero or more than once)
// are unreachable because we don't detect 'zero' or 'more than once'
_ => unreachable!(),
}
}
}
pub struct CallingStyleVisitor {
pub reference: crate::IdentifierReference,
state: VecDeque<CallingStyleVisitorState>,
halt: bool,
}
impl CallingStyleVisitor {
/// Create a new visitor that will traverse the AST and determine the
/// calling style of the target function within the source function.
pub fn new(reference: crate::IdentifierReference) -> Self {
Self {
reference,
state: Default::default(),
halt: false,
}
}
pub fn result(self) -> Option<CallingStyle> {
self.state
.into_iter()
.map(|b| match b {
CallingStyleVisitorState::Block => CallingStyle::Once,
CallingStyleVisitorState::Loop => CallingStyle::ZeroOrMore,
CallingStyleVisitorState::If => CallingStyle::ZeroOrOnce,
CallingStyleVisitorState::Closure => CallingStyle::ZeroOrMore,
})
.reduce(|a, b| a + b)
}
}
#[derive(Debug, Clone, Copy)]
enum CallingStyleVisitorState {
Block,
Loop,
If,
Closure,
}
impl Visit<'_> for CallingStyleVisitor {
fn visit_item_fn(&mut self, i: &'_ syn::ItemFn) {
self.state.push_back(CallingStyleVisitorState::Block);
syn::visit::visit_item_fn(self, i);
if !self.halt {
self.state.pop_back();
}
}
fn visit_impl_item_fn(&mut self, i: &'_ syn::ImplItemFn) {
self.state.push_back(CallingStyleVisitorState::Block);
syn::visit::visit_impl_item_fn(self, i);
if !self.halt {
self.state.pop_back();
}
}
fn visit_expr_loop(&mut self, i: &'_ syn::ExprLoop) {
self.state.push_back(CallingStyleVisitorState::Loop);
syn::visit::visit_expr_loop(self, i);
if !self.halt {
self.state.pop_back();
}
}
fn visit_expr_for_loop(&mut self, i: &'_ syn::ExprForLoop) {
self.state.push_back(CallingStyleVisitorState::Loop);
syn::visit::visit_expr_for_loop(self, i);
if !self.halt {
self.state.pop_back();
}
}
fn visit_expr_if(&mut self, i: &'_ syn::ExprIf) {
self.state.push_back(CallingStyleVisitorState::If);
syn::visit::visit_expr_if(self, i);
if !self.halt {
self.state.pop_back();
}
}
fn visit_expr_closure(&mut self, i: &'_ syn::ExprClosure) {
self.state.push_back(CallingStyleVisitorState::Closure);
syn::visit::visit_expr_closure(self, i);
if !self.halt {
self.state.pop_back();
}
}
fn visit_expr_call(&mut self, i: &'_ syn::ExprCall) {
syn::visit::visit_expr_call(self, i);
if let Expr::Path(p) = i.func.as_ref()
&& let Some(last) = p.path.segments.last()
&& is_match(
&self.reference.identifier,
&last.ident,
&self.reference.references,
)
{
self.halt = true;
}
}
// to validate this, we first check if the name is the same and then compare it
// against any of the references we are holding
fn visit_expr_method_call(&mut self, i: &'_ syn::ExprMethodCall) {
if is_match(
&self.reference.identifier,
&i.method,
&self.reference.references,
) {
self.halt = true;
}
syn::visit::visit_expr_method_call(self, i);
}
}
/// Check if some ident referenced by `check` is calling the `target` by
/// looking it up in the list of known `ranges`.
fn is_match(target: &Identifier, check: &syn::Ident, ranges: &[Range]) -> bool {
if target.equals_ident(check, false) {
let span = check.span();
// syn is 1-indexed, range is not
for reference in ranges {
if reference.start.line != span.start().line as u32 - 1 {
continue;
}
if reference.start.character != span.start().column as u32 {
continue;
}
if reference.end.line != span.end().line as u32 - 1 {
continue;
}
if reference.end.character != span.end().column as u32 {
continue;
}
// match, just exit the visitor
return true;
}
}
false
}
|