@@ -83,7 +83,7 @@ impl PathResolution {
8383/// Primary API to get semantic information, like types, from syntax trees.
8484pub struct Semantics < ' db , DB > {
8585 pub db : & ' db DB ,
86- impl_ : SemanticsImpl < ' db > ,
86+ imp : SemanticsImpl < ' db > ,
8787}
8888
8989pub struct SemanticsImpl < ' db > {
@@ -101,19 +101,22 @@ impl<DB> fmt::Debug for Semantics<'_, DB> {
101101impl < ' db , DB : HirDatabase > Semantics < ' db , DB > {
102102 pub fn new ( db : & DB ) -> Semantics < DB > {
103103 let impl_ = SemanticsImpl :: new ( db) ;
104- Semantics { db, impl_ }
104+ Semantics { db, imp : impl_ }
105105 }
106106
107107 pub fn parse ( & self , file_id : FileId ) -> ast:: SourceFile {
108- self . impl_ . parse ( file_id)
108+ self . imp . parse ( file_id)
109109 }
110110
111111 pub fn ast < T : AstDiagnostic + Diagnostic > ( & self , d : & T ) -> <T as AstDiagnostic >:: AST {
112- self . impl_ . ast ( d)
112+ let file_id = d. source ( ) . file_id ;
113+ let root = self . db . parse_or_expand ( file_id) . unwrap ( ) ;
114+ self . imp . cache ( root, file_id) ;
115+ d. ast ( self . db . upcast ( ) )
113116 }
114117
115118 pub fn expand ( & self , macro_call : & ast:: MacroCall ) -> Option < SyntaxNode > {
116- self . impl_ . expand ( macro_call)
119+ self . imp . expand ( macro_call)
117120 }
118121
119122 pub fn expand_hypothetical (
@@ -122,39 +125,39 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
122125 hypothetical_args : & ast:: TokenTree ,
123126 token_to_map : SyntaxToken ,
124127 ) -> Option < ( SyntaxNode , SyntaxToken ) > {
125- self . impl_ . expand_hypothetical ( actual_macro_call, hypothetical_args, token_to_map)
128+ self . imp . expand_hypothetical ( actual_macro_call, hypothetical_args, token_to_map)
126129 }
127130
128131 pub fn descend_into_macros ( & self , token : SyntaxToken ) -> SyntaxToken {
129- self . impl_ . descend_into_macros ( token)
132+ self . imp . descend_into_macros ( token)
130133 }
131134
132135 pub fn descend_node_at_offset < N : ast:: AstNode > (
133136 & self ,
134137 node : & SyntaxNode ,
135138 offset : TextSize ,
136139 ) -> Option < N > {
137- self . impl_ . descend_node_at_offset ( node, offset)
140+ self . imp . descend_node_at_offset ( node, offset) . find_map ( N :: cast )
138141 }
139142
140143 pub fn original_range ( & self , node : & SyntaxNode ) -> FileRange {
141- self . impl_ . original_range ( node)
144+ self . imp . original_range ( node)
142145 }
143146
144147 pub fn diagnostics_range ( & self , diagnostics : & dyn Diagnostic ) -> FileRange {
145- self . impl_ . diagnostics_range ( diagnostics)
148+ self . imp . diagnostics_range ( diagnostics)
146149 }
147150
148151 pub fn ancestors_with_macros ( & self , node : SyntaxNode ) -> impl Iterator < Item = SyntaxNode > + ' _ {
149- self . impl_ . ancestors_with_macros ( node)
152+ self . imp . ancestors_with_macros ( node)
150153 }
151154
152155 pub fn ancestors_at_offset_with_macros (
153156 & self ,
154157 node : & SyntaxNode ,
155158 offset : TextSize ,
156159 ) -> impl Iterator < Item = SyntaxNode > + ' _ {
157- self . impl_ . ancestors_at_offset_with_macros ( node, offset)
160+ self . imp . ancestors_at_offset_with_macros ( node, offset)
158161 }
159162
160163 /// Find a AstNode by offset inside SyntaxNode, if it is inside *Macrofile*,
@@ -164,7 +167,7 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
164167 node : & SyntaxNode ,
165168 offset : TextSize ,
166169 ) -> Option < N > {
167- self . impl_ . find_node_at_offset_with_macros ( node, offset)
170+ self . imp . ancestors_at_offset_with_macros ( node, offset) . find_map ( N :: cast )
168171 }
169172
170173 /// Find a AstNode by offset inside SyntaxNode, if it is inside *MacroCall*,
@@ -174,86 +177,91 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
174177 node : & SyntaxNode ,
175178 offset : TextSize ,
176179 ) -> Option < N > {
177- self . impl_ . find_node_at_offset_with_descend ( node, offset)
180+ if let Some ( it) = find_node_at_offset ( & node, offset) {
181+ return Some ( it) ;
182+ }
183+
184+ self . imp . descend_node_at_offset ( node, offset) . find_map ( N :: cast)
178185 }
179186
180187 pub fn type_of_expr ( & self , expr : & ast:: Expr ) -> Option < Type > {
181- self . impl_ . type_of_expr ( expr)
188+ self . imp . type_of_expr ( expr)
182189 }
183190
184191 pub fn type_of_pat ( & self , pat : & ast:: Pat ) -> Option < Type > {
185- self . impl_ . type_of_pat ( pat)
192+ self . imp . type_of_pat ( pat)
186193 }
187194
188195 pub fn resolve_method_call ( & self , call : & ast:: MethodCallExpr ) -> Option < Function > {
189- self . impl_ . resolve_method_call ( call)
196+ self . imp . resolve_method_call ( call)
190197 }
191198
192199 pub fn resolve_field ( & self , field : & ast:: FieldExpr ) -> Option < Field > {
193- self . impl_ . resolve_field ( field)
200+ self . imp . resolve_field ( field)
194201 }
195202
196203 pub fn resolve_record_field ( & self , field : & ast:: RecordField ) -> Option < ( Field , Option < Local > ) > {
197- self . impl_ . resolve_record_field ( field)
204+ self . imp . resolve_record_field ( field)
198205 }
199206
200207 pub fn resolve_record_field_pat ( & self , field : & ast:: RecordFieldPat ) -> Option < Field > {
201- self . impl_ . resolve_record_field_pat ( field)
208+ self . imp . resolve_record_field_pat ( field)
202209 }
203210
204211 pub fn resolve_macro_call ( & self , macro_call : & ast:: MacroCall ) -> Option < MacroDef > {
205- self . impl_ . resolve_macro_call ( macro_call)
212+ self . imp . resolve_macro_call ( macro_call)
206213 }
207214
208215 pub fn resolve_path ( & self , path : & ast:: Path ) -> Option < PathResolution > {
209- self . impl_ . resolve_path ( path)
216+ self . imp . resolve_path ( path)
210217 }
211218
212219 pub fn resolve_variant ( & self , record_lit : ast:: RecordLit ) -> Option < VariantId > {
213- self . impl_ . resolve_variant ( record_lit)
220+ self . imp . resolve_variant ( record_lit)
214221 }
215222
216223 pub fn lower_path ( & self , path : & ast:: Path ) -> Option < Path > {
217- self . impl_ . lower_path ( path)
224+ self . imp . lower_path ( path)
218225 }
219226
220227 pub fn resolve_bind_pat_to_const ( & self , pat : & ast:: BindPat ) -> Option < ModuleDef > {
221- self . impl_ . resolve_bind_pat_to_const ( pat)
228+ self . imp . resolve_bind_pat_to_const ( pat)
222229 }
223230
224231 // FIXME: use this instead?
225232 // pub fn resolve_name_ref(&self, name_ref: &ast::NameRef) -> Option<???>;
226233
227234 pub fn record_literal_missing_fields ( & self , literal : & ast:: RecordLit ) -> Vec < ( Field , Type ) > {
228- self . impl_ . record_literal_missing_fields ( literal)
235+ self . imp . record_literal_missing_fields ( literal)
229236 }
230237
231238 pub fn record_pattern_missing_fields ( & self , pattern : & ast:: RecordPat ) -> Vec < ( Field , Type ) > {
232- self . impl_ . record_pattern_missing_fields ( pattern)
239+ self . imp . record_pattern_missing_fields ( pattern)
233240 }
234241
235242 pub fn to_def < T : ToDef > ( & self , src : & T ) -> Option < T :: Def > {
236- self . impl_ . to_def ( src)
243+ let src = self . imp . find_file ( src. syntax ( ) . clone ( ) ) . with_value ( src) . cloned ( ) ;
244+ T :: to_def ( & self . imp , src)
237245 }
238246
239247 pub fn to_module_def ( & self , file : FileId ) -> Option < Module > {
240- self . impl_ . to_module_def ( file)
248+ self . imp . to_module_def ( file)
241249 }
242250
243251 pub fn scope ( & self , node : & SyntaxNode ) -> SemanticsScope < ' db > {
244- self . impl_ . scope ( node)
252+ self . imp . scope ( node)
245253 }
246254
247255 pub fn scope_at_offset ( & self , node : & SyntaxNode , offset : TextSize ) -> SemanticsScope < ' db > {
248- self . impl_ . scope_at_offset ( node, offset)
256+ self . imp . scope_at_offset ( node, offset)
249257 }
250258
251259 pub fn scope_for_def ( & self , def : Trait ) -> SemanticsScope < ' db > {
252- self . impl_ . scope_for_def ( def)
260+ self . imp . scope_for_def ( def)
253261 }
254262
255263 pub fn assert_contains_node ( & self , node : & SyntaxNode ) {
256- self . impl_ . assert_contains_node ( node)
264+ self . imp . assert_contains_node ( node)
257265 }
258266}
259267
@@ -268,13 +276,6 @@ impl<'db> SemanticsImpl<'db> {
268276 tree
269277 }
270278
271- pub fn ast < T : AstDiagnostic + Diagnostic > ( & self , d : & T ) -> <T as AstDiagnostic >:: AST {
272- let file_id = d. source ( ) . file_id ;
273- let root = self . db . parse_or_expand ( file_id) . unwrap ( ) ;
274- self . cache ( root, file_id) ;
275- d. ast ( self . db . upcast ( ) )
276- }
277-
278279 pub fn expand ( & self , macro_call : & ast:: MacroCall ) -> Option < SyntaxNode > {
279280 let macro_call = self . find_file ( macro_call. syntax ( ) . clone ( ) ) . with_value ( macro_call) ;
280281 let sa = self . analyze2 ( macro_call. map ( |it| it. syntax ( ) ) , None ) ;
@@ -329,15 +330,16 @@ impl<'db> SemanticsImpl<'db> {
329330 token. value
330331 }
331332
332- pub fn descend_node_at_offset < N : ast :: AstNode > (
333+ pub fn descend_node_at_offset (
333334 & self ,
334335 node : & SyntaxNode ,
335336 offset : TextSize ,
336- ) -> Option < N > {
337+ ) -> impl Iterator < Item = SyntaxNode > + ' _ {
337338 // Handle macro token cases
338339 node. token_at_offset ( offset)
339340 . map ( |token| self . descend_into_macros ( token) )
340- . find_map ( |it| self . ancestors_with_macros ( it. parent ( ) ) . find_map ( N :: cast) )
341+ . map ( |it| self . ancestors_with_macros ( it. parent ( ) ) )
342+ . flatten ( )
341343 }
342344
343345 pub fn original_range ( & self , node : & SyntaxNode ) -> FileRange {
@@ -367,25 +369,6 @@ impl<'db> SemanticsImpl<'db> {
367369 . kmerge_by ( |node1, node2| node1. text_range ( ) . len ( ) < node2. text_range ( ) . len ( ) )
368370 }
369371
370- pub fn find_node_at_offset_with_macros < N : AstNode > (
371- & self ,
372- node : & SyntaxNode ,
373- offset : TextSize ,
374- ) -> Option < N > {
375- self . ancestors_at_offset_with_macros ( node, offset) . find_map ( N :: cast)
376- }
377-
378- pub fn find_node_at_offset_with_descend < N : AstNode > (
379- & self ,
380- node : & SyntaxNode ,
381- offset : TextSize ,
382- ) -> Option < N > {
383- if let Some ( it) = find_node_at_offset ( & node, offset) {
384- return Some ( it) ;
385- }
386- self . descend_node_at_offset ( & node, offset)
387- }
388-
389372 pub fn type_of_expr ( & self , expr : & ast:: Expr ) -> Option < Type > {
390373 self . analyze ( expr. syntax ( ) ) . type_of ( self . db , & expr)
391374 }
@@ -445,11 +428,6 @@ impl<'db> SemanticsImpl<'db> {
445428 . unwrap_or_default ( )
446429 }
447430
448- pub fn to_def < T : ToDef > ( & self , src : & T ) -> Option < T :: Def > {
449- let src = self . find_file ( src. syntax ( ) . clone ( ) ) . with_value ( src) . cloned ( ) ;
450- T :: to_def ( self , src)
451- }
452-
453431 fn with_ctx < F : FnOnce ( & mut SourceToDefCtx ) -> T , T > ( & self , f : F ) -> T {
454432 let mut cache = self . s2d_cache . borrow_mut ( ) ;
455433 let mut ctx = SourceToDefCtx { db : self . db , cache : & mut * cache } ;
@@ -504,7 +482,7 @@ impl<'db> SemanticsImpl<'db> {
504482 SourceAnalyzer :: new_for_resolver ( resolver, src)
505483 }
506484
507- fn cache ( & self , root_node : SyntaxNode , file_id : HirFileId ) {
485+ pub fn cache ( & self , root_node : SyntaxNode , file_id : HirFileId ) {
508486 assert ! ( root_node. parent( ) . is_none( ) ) ;
509487 let mut cache = self . cache . borrow_mut ( ) ;
510488 let prev = cache. insert ( root_node, file_id) ;
@@ -520,7 +498,7 @@ impl<'db> SemanticsImpl<'db> {
520498 cache. get ( root_node) . copied ( )
521499 }
522500
523- fn find_file ( & self , node : SyntaxNode ) -> InFile < SyntaxNode > {
501+ pub fn find_file ( & self , node : SyntaxNode ) -> InFile < SyntaxNode > {
524502 let root_node = find_root ( & node) ;
525503 let file_id = self . lookup ( & root_node) . unwrap_or_else ( || {
526504 panic ! (
0 commit comments