emmylua_code_analysis/semantic/
mod.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
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
mod call_func;
mod infer;
mod instantiate;
mod member;
mod overload_resolve;
mod reference;
mod semantic_info;
mod type_calc;
mod type_compact;
mod visibility;

use std::{collections::HashSet, sync::Arc};

use emmylua_parser::{LuaCallExpr, LuaChunk, LuaExpr, LuaSyntaxNode, LuaSyntaxToken};
use infer::InferResult;
pub use infer::LuaInferConfig;
use member::infer_members;
pub use member::LuaMemberInfo;
use reference::is_reference_to;
use rowan::{NodeOrToken, TextRange};
pub use semantic_info::SemanticInfo;
use semantic_info::{
    infer_node_property_owner, infer_node_semantic_info, infer_token_property_owner,
    infer_token_semantic_info,
};
use type_compact::{check_type_compact, is_sub_type_of};
use visibility::check_visibility;

use crate::LuaFunctionType;
use crate::{db_index::LuaTypeDeclId, Emmyrc, LuaDocument, LuaPropertyOwnerId};
use crate::{
    db_index::{DbIndex, LuaType},
    FileId,
};
pub(crate) use call_func::infer_call_expr_func;
pub(crate) use infer::{infer_expr, instantiate_doc_function};
use instantiate::instantiate_type;
use overload_resolve::resolve_signature;

#[derive(Debug)]
pub struct SemanticModel<'a> {
    file_id: FileId,
    db: &'a DbIndex,
    infer_config: LuaInferConfig,
    emmyrc: Arc<Emmyrc>,
    root: LuaChunk,
}

unsafe impl<'a> Send for SemanticModel<'a> {}
unsafe impl<'a> Sync for SemanticModel<'a> {}

impl<'a> SemanticModel<'a> {
    pub fn new(
        file_id: FileId,
        db: &'a DbIndex,
        infer_config: LuaInferConfig,
        emmyrc: Arc<Emmyrc>,
        root: LuaChunk,
    ) -> Self {
        Self {
            file_id,
            db,
            infer_config,
            emmyrc,
            root,
        }
    }

    pub fn get_document(&self) -> LuaDocument {
        self.db.get_vfs().get_document(&self.file_id).unwrap()
    }

    pub fn get_document_by_file_id(&self, file_id: FileId) -> Option<LuaDocument> {
        self.db.get_vfs().get_document(&file_id)
    }

    pub fn get_root_by_file_id(&self, file_id: FileId) -> Option<LuaChunk> {
        Some(self.db.get_vfs().get_syntax_tree(&file_id)?.get_chunk_node())
    }

    pub fn get_file_parse_error(&self) -> Option<Vec<(String, TextRange)>> {
        self.db.get_vfs().get_file_parse_error(&self.file_id)
    }

    pub fn infer_expr(&mut self, expr: LuaExpr) -> InferResult {
        infer_expr(self.db, &mut self.infer_config, expr)
    }

    pub fn infer_member_infos(&self, prefix_type: &LuaType) -> Option<Vec<LuaMemberInfo>> {
        infer_members(self.db, prefix_type)
    }

    pub fn check_type_compact(&mut self, source: &LuaType, compact_type: &LuaType) -> bool {
        check_type_compact(self.db, &mut self.infer_config,source, compact_type)
    }

    pub fn infer_call_expr_func(
        &mut self,
        call_expr: LuaCallExpr,
        arg_count: Option<usize>,
    ) -> Option<Arc<LuaFunctionType>> {
        let prefix_expr = call_expr.get_prefix_expr()?;
        let call_expr_type = infer_expr(self.db, &mut self.infer_config, prefix_expr.into())?;
        infer_call_expr_func(
            self.db,
            &mut self.infer_config,
            call_expr,
            call_expr_type,
            &mut InferGuard::new(),
            arg_count,
        )
    }

    pub fn get_semantic_info(
        &mut self,
        node_or_token: NodeOrToken<LuaSyntaxNode, LuaSyntaxToken>,
    ) -> Option<SemanticInfo> {
        match node_or_token {
            NodeOrToken::Node(node) => {
                infer_node_semantic_info(self.db, &mut self.infer_config, node)
            }
            NodeOrToken::Token(token) => {
                infer_token_semantic_info(self.db, &mut self.infer_config, token)
            }
        }
    }

    pub fn get_property_owner_id(
        &mut self,
        node_or_token: NodeOrToken<LuaSyntaxNode, LuaSyntaxToken>,
    ) -> Option<LuaPropertyOwnerId> {
        match node_or_token {
            NodeOrToken::Node(node) => {
                infer_node_property_owner(self.db, &mut self.infer_config, node)
            }
            NodeOrToken::Token(token) => {
                infer_token_property_owner(self.db, &mut self.infer_config, token)
            }
        }
    }

    pub fn is_reference_to(
        &mut self,
        node: LuaSyntaxNode,
        property_owner: LuaPropertyOwnerId,
    ) -> bool {
        is_reference_to(self.db, &mut self.infer_config, node, property_owner).unwrap_or(false)
    }

    pub fn is_property_visiable(
        &mut self,
        token: LuaSyntaxToken,
        property_owner: LuaPropertyOwnerId,
    ) -> bool {
        check_visibility(
            self.db,
            self.file_id,
            &self.emmyrc,
            &mut self.infer_config,
            token,
            property_owner,
        )
        .unwrap_or(true)
    }

    pub fn is_sub_type_of(
        &mut self,
        sub_type_ref_id: &LuaTypeDeclId,
        super_type_ref_id: &LuaTypeDeclId,
    ) -> bool {
        is_sub_type_of(self.db, sub_type_ref_id, super_type_ref_id)
    }

    pub fn get_emmyrc(&self) -> &Emmyrc {
        &self.emmyrc
    }

    pub fn get_root(&self) -> &LuaChunk {
        &self.root
    }

    pub fn get_db(&self) -> &DbIndex {
        self.db
    }

    pub fn get_file_id(&self) -> FileId {
        self.file_id
    }
}

/// Guard to prevent infinite recursion
/// Some type may reference itself, so we need to check if we have already infered this type
#[derive(Debug)]
pub struct InferGuard {
    guard: HashSet<LuaTypeDeclId>,
}

impl InferGuard {
    pub fn new() -> Self {
        Self {
            guard: HashSet::default(),
        }
    }

    pub fn check(&mut self, type_id: &LuaTypeDeclId) -> Option<()> {
        if self.guard.contains(type_id) {
            return None;
        }
        self.guard.insert(type_id.clone());
        Some(())
    }
}