darklua_core/process/processors/
find_usage.rs

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
use std::ops;

use crate::{
    nodes::{Identifier, TypeField},
    process::{IdentifierTracker, NodeProcessor},
};

/// A processor to find usage of a given variable.
pub(crate) struct FindUsage<'a> {
    variable: &'a str,
    usage_found: bool,
    identifier_tracker: IdentifierTracker,
}

impl<'a> ops::Deref for FindUsage<'a> {
    type Target = IdentifierTracker;

    fn deref(&self) -> &Self::Target {
        &self.identifier_tracker
    }
}

impl<'a> ops::DerefMut for FindUsage<'a> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.identifier_tracker
    }
}

impl<'a> FindUsage<'a> {
    pub fn new(variable: &'a str) -> Self {
        Self {
            variable,
            usage_found: false,
            identifier_tracker: Default::default(),
        }
    }

    #[inline]
    pub fn has_found_usage(&self) -> bool {
        self.usage_found
    }

    fn verify_identifier(&mut self, variable: &Identifier) {
        if !self.usage_found && variable.get_name() == self.variable {
            self.usage_found = !self.is_identifier_used(self.variable);
        }
    }
}

impl<'a> NodeProcessor for FindUsage<'a> {
    fn process_variable_expression(&mut self, variable: &mut Identifier) {
        self.verify_identifier(variable);
    }

    fn process_type_field(&mut self, type_field: &mut TypeField) {
        self.verify_identifier(type_field.get_namespace());
    }
}