1use super::*;
5use crate::languages::ast_visitor::AstVisitor;
6#[cfg(target_arch = "wasm32")]
7use crate::wasm::{Node, TreeCursor};
8#[cfg(not(target_arch = "wasm32"))]
9use tree_sitter::{Node, TreeCursor};
10
11struct TraversalContext<'code, 'cursor, Visitor> {
12 declaration_stack: Vec<ScopedDeclaration>,
13 this_scope: Option<Type>,
14 super_scope: Option<Type>,
15 #[cfg(not(target_arch = "wasm32"))]
16 cursor: &'cursor mut TreeCursor<'code>,
17 #[cfg(target_arch = "wasm32")]
18 cursor: &'cursor mut TreeCursor,
19 code: &'code [u8],
20 visitor: Visitor,
21}
22
23pub(super) fn traverse_ast<'code, Visitor: AstVisitor>(
24 #[cfg(not(target_arch = "wasm32"))] cursor: &mut TreeCursor<'code>,
25 #[cfg(target_arch = "wasm32")] cursor: &mut TreeCursor,
26 code: &'code [u8],
27 visitor: Visitor,
28) -> Visitor::Information {
29 let mut ctx = TraversalContext {
30 declaration_stack: builtins::builtins()
31 .into_iter()
32 .map(|(text, r#type)| ScopedDeclaration::Builtin {
33 symbol: vec![Name {
34 text: text.to_string(),
35 extent: None,
36 }],
37 r#type,
38 })
39 .collect(),
40 this_scope: None,
41 super_scope: None,
42 cursor,
43 code,
44 visitor,
45 };
46 if ctx.cursor.node().is_relevant() {
47 ctx.program();
48 }
49 ctx.visitor.finish()
50}
51
52impl<Visitor: AstVisitor> TraversalContext<'_, '_, Visitor> {
53 fn to_name(&self, node: &Node) -> Name {
54 Name {
55 text: String::from_utf8_lossy(&self.code[node.start_byte()..node.end_byte()])
56 .to_string(),
57 extent: Some(node.into()),
58 }
59 }
60
61 fn read_declaration(&mut self) -> Type {
62 self.read_typed_declaration(Type::Unknown);
63 Type::Unknown
64 }
65
66 fn read_typed_declaration(&mut self, r#type: Type) {
67 let node = self.cursor.node();
68 let symbol = vec![self.to_name(&node)];
69 self.visitor.read_declaration(Declaration {
70 symbol: Symbol::Local(symbol.clone()),
71 extent: node.into(),
72 });
73 self.declaration_stack
74 .push(ScopedDeclaration::Symbol { symbol, r#type });
75 }
76
77 fn read_type_declaration(&mut self) -> Name {
78 let node = self.cursor.node();
79 let name = self.to_name(&node);
80 let Name { ref text, .. } = name;
81 let declared_type = if let Some(ref scope) = self.this_scope {
82 scope.child(Name {
83 text: text.clone(),
84 extent: None,
85 })
86 } else {
87 Type::local_unqualified(vec![Name {
88 text: text.clone(),
89 extent: None,
90 }])
91 };
92 self.read_typed_declaration(declared_type);
93 name
94 }
95
96 fn enter_scope(&mut self, scope: Scope) {
97 self.declaration_stack.push(ScopedDeclaration::ScopeMarker);
98 self.visitor.enter_scope(scope);
99 }
100
101 fn enter_named_scope(&mut self, Name { text, .. }: Name, mut scope: Scope) {
102 scope.name = Some(text.clone());
103 self.visitor.enter_scope(scope);
104 let name = Name { text, extent: None };
105 if let Some(ref mut scope) = self.this_scope {
106 *scope = scope.child(name);
107 } else {
108 self.this_scope = Some(Type::local_unqualified(vec![name]));
109 }
110 }
111
112 fn leave_scope(&mut self) {
113 while self.declaration_stack.pop() != Some(ScopedDeclaration::ScopeMarker) {}
114 self.visitor.leave_scope();
115 }
116
117 fn leave_named_scope(&mut self) {
118 if let Some(ref mut scope) = self.this_scope {
119 *scope = scope.parent();
120 }
121 self.visitor.leave_scope();
122 }
123
124 fn use_node(&mut self) -> Type {
125 let node = self.cursor.node();
126 let symbol = vec![self.to_name(&node)];
127 let extent = node.into();
128 self.use_symbol(symbol, extent)
129 }
130
131 fn use_scoped_identifier(&mut self) -> Type {
132 let used_symbol = self.scoped_identifier();
133 let extent = self.cursor.node().into();
134 self.use_symbol(used_symbol, extent)
135 }
136
137 fn use_symbol(&mut self, mut used_symbol: Identifier, extent: Extent) -> Type {
138 for declaration in self.declaration_stack.iter().rev() {
139 match declaration {
140 ScopedDeclaration::Builtin { symbol, r#type }
141 if used_symbol.starts_with_symbol(symbol) =>
142 {
143 self.visitor.read_usage(Usage {
144 symbol: Symbol::Builtin(used_symbol),
145 extent,
146 });
147 return r#type.clone();
148 }
149 ScopedDeclaration::Symbol { symbol, r#type }
150 if used_symbol.starts_with_symbol(symbol) =>
151 {
152 self.visitor.read_usage(Usage {
153 symbol: Symbol::Local(used_symbol),
154 extent,
155 });
156 return r#type.clone();
157 }
158 ScopedDeclaration::Import {
159 source,
160 symbol: ImportSymbol::Member(symbol),
161 original_name,
162 r#type,
163 } if used_symbol.starts_with_symbol(symbol) => {
164 if let Some(original_name) = original_name {
165 used_symbol[0].text = original_name[0].text.clone();
166 }
167 self.visitor.read_usage(Usage {
168 symbol: Symbol::External {
169 source: source
170 .iter()
171 .map(|Name { text, .. }| Name {
172 text: text.clone(),
173 extent: None,
174 })
175 .collect(),
176 symbol: used_symbol,
177 },
178 extent,
179 });
180 return r#type.clone();
181 }
182 _ => {}
183 }
184 }
185 self.visitor.read_usage(Usage {
186 symbol: Symbol::Unknown(used_symbol),
187 extent,
188 });
189 Type::Unknown
190 }
191 fn noop(&self) -> Type {
192 Type::Unknown
193 }
194
195 fn import_declaration(&mut self) -> Type {
196 self.cursor.goto_first_child();
197 let mut declaration = None;
198
199 while self.cursor.goto_next_sibling() {
200 let node = self.cursor.node();
201 if node.is_relevant() {
202 match access!(self kind) {
203 "scoped_identifier" | "identifier" => {
204 declaration = Some((self.scoped_identifier(), node.into()));
205 }
206 "asterisk" => {
207 self.cursor.goto_parent();
208 return Type::Unknown;
209 }
210 _ => {
211 unreachable!("The node cannot be accessed.")
212 }
213 }
214 }
215 }
216
217 if let Some((mut source, extent)) = declaration {
218 let symbol: Vec<_> = source.pop().into_iter().collect();
219 self.declaration_stack.push(ScopedDeclaration::Import {
220 source: source.clone(),
221 symbol: ImportSymbol::Member(symbol.clone()),
222 original_name: None,
223 r#type: Type::External {
224 source: source
225 .iter()
226 .map(|Name { text, .. }| Name {
227 text: text.clone(),
228 extent: None,
229 })
230 .collect(),
231 symbol: symbol
232 .iter()
233 .map(|Name { text, .. }| Name {
234 text: text.clone(),
235 extent: None,
236 })
237 .collect(),
238 },
239 });
240 self.visitor.read_declaration(Declaration {
241 symbol: Symbol::External { source, symbol },
242 extent,
243 });
244 }
245
246 self.cursor.goto_parent();
247 Type::Unknown
248 }
249
250 visit!(program statement);
251
252 fn statement(&mut self) -> Type {
253 rule!(self match kind
254 module_declaration,
255 package_declaration,
256 import_declaration,
257 class_declaration,
258 method_declaration,
259 annotation_type_declaration,
260 enum_declaration,
261 local_variable_declaration,
262 record_declaration,
263 interface_declaration,
264 expression_statement,
265 labeled_statement,
266 if_statement,
267 while_statement | do_statement => while_statement,
268 for_statement,
269 enhanced_for_statement,
270 block,
271 assert_statement,
272 break_statement | continue_statement => noop,
273 return_statement | yield_statement => return_statement,
274 switch_expression,
275 synchronized_statement,
276 throw_statement,
277 try_statement,
278 try_with_resources_statement
279 || noop)
280 }
281
282 fn package_declaration(&mut self) -> Type {
283 self.cursor.goto_first_child();
284 while {
285 let node = self.cursor.node();
286 if node.is_relevant() {
287 match access!(self kind) {
288 "annotation" => {
289 self.annotation();
290 }
291 "marker_annotation" => {
292 self.marker_annotation();
293 }
294 "identifier" | "scoped_identifier" => {
295 let source = self
296 .scoped_identifier()
297 .into_iter()
298 .map(|Name { text, .. }| Name { text, extent: None })
299 .collect();
300 self.this_scope.replace(Type::Local {
301 source,
302 symbol: Vec::new(),
303 });
304 }
305 _ => unreachable!(),
306 };
307 }
308 self.cursor.goto_next_sibling()
309 } {}
310 self.cursor.goto_parent();
311
312 Type::Unknown
313 }
314
315 fn module_declaration(&mut self) -> Type {
316 self.cursor.goto_first_child();
317 while {
318 let node = self.cursor.node();
319 if node.is_relevant() {
320 match access!(self field) {
321 _ if node.kind() == "annotation" => {
322 self.annotation();
323 }
324 _ if node.kind() == "marker_annotation" => {
325 self.marker_annotation();
326 }
327 Some("name") => {
328 let symbol = self.scoped_identifier();
329 let extent = node.into();
330 self.visitor.read_declaration(Declaration {
331 symbol: Symbol::Local(symbol.clone()),
332 extent,
333 });
334 self.declaration_stack.push(ScopedDeclaration::Symbol {
335 symbol,
336 r#type: Type::Unknown,
337 });
338 }
339 Some("body") => {
340 self.module_body();
341 }
342 _ => unreachable!(),
343 };
344 }
345 self.cursor.goto_next_sibling()
346 } {}
347 self.cursor.goto_parent();
348
349 Type::Unknown
350 }
351
352 visit!(module_body module_directive);
353
354 fn module_directive(&mut self) -> Type {
355 rule!(self match kind
356 requires_module_directive => requires_module_identifier,
357 exports_module_directive | opens_module_directive
358 | uses_module_directive | provides_module_directive => exports_module_identifier,
359 scoped_identifier => use_scoped_identifier)
360 }
361
362 fn requires_module_identifier(&mut self) -> Type {
363 let mut declaration = None;
364 self.cursor.goto_first_child();
365
366 while self.cursor.goto_next_sibling() {
367 let node = self.cursor.node();
368 if node.is_relevant() {
369 match access!(self field) {
370 Some("modifiers") => {
371 self.modifiers();
372 }
373 _ if node.kind() == "scoped_identifier" || node.kind() == "identifier" => {
374 declaration = Some((self.scoped_identifier(), node.into()));
375 }
376 _ => {
377 unreachable!("The node cannot be accessed.")
378 }
379 }
380 }
381 }
382
383 if let Some((mut source, extent)) = declaration {
384 let symbol: Vec<_> = source.pop().into_iter().collect();
385 self.declaration_stack.push(ScopedDeclaration::Import {
386 source: source.clone(),
387 symbol: ImportSymbol::Member(symbol.clone()),
388 original_name: None,
389 r#type: Type::External {
390 source: source
391 .iter()
392 .map(|Name { text, .. }| Name {
393 text: text.clone(),
394 extent: None,
395 })
396 .collect(),
397 symbol: symbol
398 .iter()
399 .map(|Name { text, .. }| Name {
400 text: text.clone(),
401 extent: None,
402 })
403 .collect(),
404 },
405 });
406 self.visitor.read_declaration(Declaration {
407 symbol: Symbol::External { source, symbol },
408 extent,
409 });
410 }
411
412 self.cursor.goto_parent();
413 Type::Unknown
414 }
415
416 fn exports_module_identifier(&mut self) -> Type {
417 self.cursor.goto_first_child();
418
419 while self.cursor.goto_next_sibling() {
420 let node = self.cursor.node();
421 if node.is_relevant() {
422 match access!(self kind) {
423 "scoped_identifier" | "identifier" => {
424 let mut source = self.scoped_identifier();
425 let extent = self.cursor.node().into();
426 let symbol: Vec<_> = source.pop().into_iter().collect();
427
428 self.visitor.read_usage(Usage {
429 symbol: Symbol::External { source, symbol },
430 extent,
431 })
432 }
433 _ => {
434 unreachable!("The node cannot be accessed.")
435 }
436 }
437 }
438 }
439 self.cursor.goto_parent();
440 Type::Unknown
441 }
442
443 visit!(labeled_statement match kind
444 identifier => use_node ||
445 statement);
446
447 visit!(synchronized_statement match field
448 body => block ||
449 parenthesized_expression);
450
451 visit!(assert_statement expression);
452 visit!(return_statement expression);
453 visit!(try_statement match kind
454 block,
455 catch_clause,
456 finally_clause);
457
458 fn catch_clause(&mut self) -> Type {
459 self.enter_scope(self.cursor.node().into());
460 self.catch();
461 self.leave_scope();
462 Type::Unknown
463 }
464
465 visit!(catch match kind
466 catch_formal_parameter,
467 identifier => read_declaration,
468 dimensions,
469 block);
470
471 fn catch_formal_parameter(&mut self) -> Type {
472 self.cursor.goto_first_child();
473 let mut catch_type = None;
474 while {
475 let node = self.cursor.node();
476 if node.is_relevant() {
477 match access!(self field) {
478 Some("name") => {
479 self.read_typed_declaration(catch_type.take().unwrap_or(Type::Unknown))
480 }
481 Some("dimensions") => {
482 self.dimensions();
483 }
484 None if node.kind() == "modifiers" => {
485 self.modifiers();
486 }
487 None if node.kind() == "catch_type" => {
488 catch_type.replace(self.catch_type());
489 }
490 _ => unreachable!(),
491 };
492 }
493 self.cursor.goto_next_sibling()
494 } {}
495 self.cursor.goto_parent();
496
497 Type::Unknown
498 }
499
500 visit!(catch_type unannotated_type); visit!(finally_clause block);
502
503 fn try_with_resources_statement(&mut self) -> Type {
504 self.enter_scope(self.cursor.node().into());
505 self.try_with_resources();
506 self.leave_scope();
507 Type::Unknown
508 }
509
510 visit!(try_with_resources match kind
511 resource_specification,
512 block,
513 catch_clause,
514 finally_clause);
515 visit!(resource_specification resource);
516
517 fn resource(&mut self) -> Type {
518 self.cursor.goto_first_child();
519 let mut resource_type = None;
520 while {
521 let node = self.cursor.node();
522 if node.is_relevant() {
523 match access!(self field) {
524 Some("type") => {
525 resource_type.replace(self.unannotated_type());
526 }
527 Some("name") => {
528 self.read_typed_declaration(resource_type.take().unwrap_or(Type::Unknown))
529 }
530 Some("value") => {
531 self.expression();
532 }
533 Some("dimensions") => {
534 self.dimensions();
535 }
536 None if node.kind() == "modifiers" => {
537 self.modifiers();
538 }
539 None if node.kind() == "identifier" || node.kind() == "field_access" => {
540 self.primary_expression();
541 }
542 _ => unreachable!(),
543 };
544 }
545 self.cursor.goto_next_sibling()
546 } {}
547 self.cursor.goto_parent();
548
549 Type::Unknown
550 }
551 visit!(dimensions match kind
552 annotation,
553 marker_annotation);
554 visit!(throw_statement expression);
555 visit!(if_statement match field
556 condition => parenthesized_expression,
557 consequence | alternative => statement);
558 visit!(while_statement match field
559 condition => parenthesized_expression,
560 body => statement);
561 visit!(parenthesized_expression expression);
562 visit!(expression_statement expression);
563 visit!(assignment_expression match field
564 left => primary_expression,
565 right => expression);
566
567 fn class_declaration(&mut self) -> Type {
568 let scope: Scope = self.cursor.node().into();
569 if !self.cursor.goto_first_child() {
570 return Type::Unknown;
571 }
572 let mut class_name = None;
573 while {
574 let node = self.cursor.node();
575 if node.is_relevant() {
576 match access!(self field) {
577 Some("name") => {
578 class_name = Some(self.read_type_declaration());
579 }
580 Some("type_parameters") => {
581 self.type_parameters();
582 }
583 Some("body") => {
584 if let Some(name) = class_name {
585 self.enter_named_scope(name, scope);
586 self.class_body();
587 self.leave_named_scope();
588 } else {
589 self.class_body();
590 }
591
592 self.cursor.goto_parent();
593 return Type::Unknown;
594 }
595 Some("superclass") => {
596 let superclass = self.superclass();
597 self.super_scope.replace(superclass);
598 }
599 Some("interfaces") | Some("permits") => {
600 self.types();
601 }
602 _ if node.kind() == "modifiers" => {
603 self.modifiers();
604 }
605 _ => unreachable!(),
606 };
607 }
608 self.cursor.goto_next_sibling()
609 } {}
610 self.cursor.goto_parent();
611 Type::Unknown
612 }
613
614 fn superclass(&mut self) -> Type {
615 if !self.cursor.goto_first_child() {
616 return Type::Unknown;
617 }
618 while {
619 let node = self.cursor.node();
620 if node.is_relevant() {
621 let supertype = self.r#type();
622 self.cursor.goto_parent();
623 return supertype;
624 }
625 self.cursor.goto_next_sibling()
626 } {}
627 self.cursor.goto_parent();
628 Type::Unknown
629 }
630
631 fn enum_declaration(&mut self) -> Type {
632 let scope = self.cursor.node().into();
633 if !self.cursor.goto_first_child() {
634 return Type::Unknown;
635 }
636 let mut class_name = None;
637 while {
638 let node = self.cursor.node();
639 if node.is_relevant() {
640 match access!(self field) {
641 Some("name") => {
642 class_name.replace(self.read_type_declaration());
643 }
644 Some("type_parameters") => {
645 self.type_parameters();
646 }
647 Some("body") => {
648 if let Some(name) = class_name {
649 self.enter_named_scope(name, scope);
650 self.enum_body();
651 self.leave_named_scope();
652 } else {
653 self.enum_body();
654 }
655 self.cursor.goto_parent();
656 return Type::Unknown;
657 }
658 Some("interfaces") => {
659 self.types();
660 }
661 _ if node.kind() == "modifiers" => {
662 self.modifiers();
663 }
664 _ => unreachable!(),
665 };
666 }
667 self.cursor.goto_next_sibling()
668 } {}
669 self.cursor.goto_parent();
670 Type::Unknown
671 }
672
673 visit!(enum_body match kind
674 enum_constant,
675 enum_body_declarations);
676 visit!(enum_constant match field
677 name => read_declaration,
678 arguments => argument_list,
679 body => class_body
680 || modifiers);
681 visit!(enum_body_declarations class_body_declaration);
682
683 visit!(type_parameters type_parameter);
684
685 visit!(type_parameter match kind
686 annotation,
687 marker_annotation,
688 type_identifier => use_node,
689 type_bound => annotated_type);
690
691 visit!(class_body class_body_declaration);
692 fn this(&self) -> Type {
693 self.this_scope.clone().unwrap_or(Type::Unknown)
694 }
695 fn super_(&self) -> Type {
696 self.super_scope.clone().unwrap_or(Type::Unknown)
697 }
698
699 fn expression(&mut self) -> Type {
700 rule!(self match kind
701 assignment_expression,
702 binary_expression,
703 ternary_expression,
704 update_expression,
705 switch_expression,
706 cast_expression,
707 unary_expression,
708 lambda_expression,
709 instanceof_expression
710 || primary_expression)
711 }
712 visit!(unary_expression expression);
713 visit!(cast_expression match field
714 type => r#type,
715 value => expression);
716 visit!(binary_expression match field
717 left | right => expression,
718 operator => noop);
719 visit!(ternary_expression match field
720 condition | consequence | alternative => expression);
721 visit!(update_expression expression);
722 visit!(switch_expression match field
723 condition => parenthesized_expression,
724 body => switch_block);
725 visit!(switch_block match kind
726 switch_block_statement_group,
727 switch_rule);
728 visit!(switch_block_statement_group match kind
729 switch_label
730 || statement);
731 visit!(switch_label expression);
732 visit!(switch_rule match kind
733 switch_label,
734 expression_statement,
735 throw_statement,
736 block);
737 visit!(array_access match field
738 array => primary_expression,
739 index => expression);
740 visit!(class_literal unannotated_type);
741 fn primary_expression(&mut self) -> Type {
742 rule!(self match kind
743 true | false => boolean,
744 decimal_integer_literal | hex_integer_literal | octal_integer_literal | binary_integer_literal => int,
745 decimal_floating_point_literal | hex_floating_point_literal => float,
746 character_literal,
747 class_literal,
748 this,
749 identifier | open | module | record => use_node,
750 parenthesized_expression,
751 object_creation_expression,
752 string_literal | text_block => string,
753 null_literal => noop,
754 field_access,
755 array_access,
756 method_invocation,
757 method_reference,
758 array_creation_expression
759 || noop)
760 }
761
762 visit!(method_reference match kind
763 unannotated_type,
764 annotated_type,
765 super => super_,
766 type_arguments => type_list,
767 identifier => use_node ||
768 primary_expression);
769
770 visit!(array_creation_expression match field
771 annotation,
772 marker_annotation,
773 type => unannotated_type,
774 value => array_initializer,
775 dimensions => dimensions_expr);
776
777 visit!(dimensions_expr match kind
778 annotation,
779 marker_annotation ||
780 expression);
781
782 visit!(object_creation_expression match field
783 type_arguments => type_list,
784 type => unannotated_type,
785 arguments => argument_list,
786 class_body ||
787 primary_expression);
788
789 fn field_access(&mut self) -> Type {
790 if !self.cursor.goto_first_child() {
791 return Type::Unknown;
792 }
793 let mut object_type = None;
794 while {
795 let node = self.cursor.node();
796 if node.is_relevant() {
797 match access!(self field) {
798 Some("object") => {
799 if node.kind() == "super" {
800 object_type.replace(self.super_());
801 } else {
802 object_type.replace(self.primary_expression());
803 }
804 }
805 Some("field") => {
806 if node.kind() == "this" {
807 } else {
809 let name = self.to_name(&node);
810 match object_type.take() {
811 Some(Type::Unknown) | Some(Type::Callable { .. }) => {
812 self.visitor.read_usage(Usage {
813 symbol: Symbol::Unknown(vec![name]),
814 extent: node.into(),
815 });
816 }
817 Some(Type::Builtin(mut object)) => {
818 object.push(name);
819 self.visitor.read_usage(Usage {
820 symbol: Symbol::Builtin(object),
821 extent: node.into(),
822 });
823 }
824 Some(Type::Local { mut symbol, .. }) => {
825 symbol.push(name);
826 self.visitor.read_usage(Usage {
827 symbol: Symbol::Local(symbol),
828 extent: node.into(),
829 });
830 }
831 Some(Type::External { source, mut symbol }) => {
832 symbol.push(name);
833 self.visitor.read_usage(Usage {
834 symbol: Symbol::External { source, symbol },
835 extent: node.into(),
836 });
837 }
838 None => {}
839 };
840 }
841 }
842 None if node.kind() == "super" => {
843 object_type.replace(Type::Unknown);
844 }
845 _ => unreachable!(),
846 };
847 }
848 self.cursor.goto_next_sibling()
849 } {}
850 self.cursor.goto_parent();
851 Type::Unknown
852 }
853
854 fn method_invocation(&mut self) -> Type {
855 if !self.cursor.goto_first_child() {
856 return Type::Unknown;
857 }
858 let mut object_type = None;
859 while {
860 let node = self.cursor.node();
861 if node.is_relevant() {
862 match access!(self field) {
863 Some("object") => {
864 if node.kind() == "super" {
865 object_type.replace(self.super_());
866 } else {
867 object_type.replace(self.primary_expression());
868 }
869 }
870 Some("type_arguments") => {
871 self.type_list();
872 }
873 Some("name") => {
874 let name = self.to_name(&node);
875 match object_type.take() {
876 Some(Type::Unknown) | Some(Type::Callable { .. }) => {
877 self.visitor.read_usage(Usage {
878 symbol: Symbol::Unknown(vec![name]),
879 extent: node.into(),
880 });
881 }
882 Some(Type::Builtin(mut object)) => {
883 object.push(name);
884 self.visitor.read_usage(Usage {
885 symbol: Symbol::Builtin(object),
886 extent: node.into(),
887 });
888 }
889 Some(Type::Local { mut symbol, .. }) => {
890 symbol.push(name);
891 self.visitor.read_usage(Usage {
892 symbol: Symbol::Local(symbol),
893 extent: node.into(),
894 });
895 }
896 Some(Type::External { source, mut symbol }) => {
897 symbol.push(name);
898 self.visitor.read_usage(Usage {
899 symbol: Symbol::External { source, symbol },
900 extent: node.into(),
901 });
902 }
903 None => {}
904 };
905 }
906 Some("arguments") => {
907 self.argument_list();
908 }
909 None if node.kind() == "super" => {
910 object_type.replace(Type::Unknown);
911 }
912 _ => unreachable!(),
913 };
914 }
915 self.cursor.goto_next_sibling()
916 } {}
917 self.cursor.goto_parent();
918 Type::Unknown
919 }
920 visit!(argument_list expression);
921 visit!(array_type match field
922 element => unannotated_type,
923 dimensions);
924 fn unannotated_type(&mut self) -> Type {
925 rule!(self match kind
926 void_type => noop,
927 type_identifier | integral_type | floating_point_type | boolean_type => use_node,
928 array_type,
929 scoped_type_identifier,
930 generic_type
931 || noop)
932 }
933
934 visit!(scoped_type_identifier match kind
935 type_identifier => use_node,
936 scoped_type_identifier,
937 generic_type,
938 marker_annotation,
939 annotation);
940
941 visit!(generic_type match kind
942 type_identifier => use_node,
943 scoped_type_identifier,
944 type_arguments => type_list);
945
946 visit!(modifiers match kind
947 annotation,
948 marker_annotation
949 || noop);
950
951 visit!(element_value_pair match field
952 key => use_node,
953 value => expression);
954
955 visit!(annotation_argument_list match kind
956 element_value_array_initializer,
957 annotation,
958 marker_annotation,
959 element_value_pair ||
960 expression);
961
962 fn scoped_identifier(&mut self) -> Identifier {
963 let mut identifier = Identifier::new();
964 let mut cursor = self.cursor.node().walk();
965 while cursor.node().kind() == "scoped_identifier" && cursor.goto_first_child() {}
966 while {
967 while {
968 let node = cursor.node();
969 if node.kind() == "identifier" {
970 identifier.push(self.to_name(&node));
971 }
972 cursor.goto_next_sibling()
973 } {}
974 cursor.goto_parent()
975 } {}
976 identifier
977 }
978
979 visit!(annotation match field
980 name => use_scoped_identifier,
981 arguments => annotation_argument_list);
982
983 visit!(marker_annotation match field
984 name => use_scoped_identifier);
985
986 visit!(annotated_type match kind
987 annotation,
988 marker_annotation
989 || unannotated_type);
990 visit!(types type_list);
991 visit!(type_list r#type);
992 visit!(formal_parameters match kind
993 receiver_parameter,
994 formal_parameter,
995 spread_parameter);
996 visit!(receiver_parameter noop);
997 fn enhanced_for_statement(&mut self) -> Type {
998 self.cursor.goto_first_child();
999 let mut loop_variable_type = None;
1000 while {
1001 let node = self.cursor.node();
1002 if node.is_relevant() {
1003 match access!(self field) {
1004 None if node.kind() == "modifiers" => {
1005 self.modifiers();
1006 }
1007 Some("type") => {
1008 loop_variable_type.replace(self.unannotated_type());
1009 }
1010 Some("name") => {
1011 self.read_typed_declaration(loop_variable_type.take().unwrap_or(Type::Unknown));
1012 }
1013 Some("dimensions") => {
1014 self.dimensions();
1015 }
1016 Some("value") => {
1017 self.expression();
1018 }
1019 Some("body") => {
1020 self.statement();
1021 }
1022 field => unreachable!(
1023 "Field {field:?} of kind {} should be unreachable in enhanced_for_statement",
1024 node.kind()
1025 ),
1026 };
1027 }
1028 self.cursor.goto_next_sibling()
1029 } {}
1030 self.cursor.goto_parent();
1031 Type::Unknown
1032 }
1033
1034 fn for_statement(&mut self) -> Type {
1035 self.cursor.goto_first_child();
1036 while {
1037 let node = self.cursor.node();
1038 if node.is_relevant() {
1039 match access!(self field) {
1040 Some("init") => {
1041 if node.kind() == "local_variable_declaration" {
1042 self.local_variable_declaration();
1043 } else if node.kind() == "assignment_expression" {
1044 self.assignment_expression();
1045 } else {
1046 self.expression();
1047 }
1048 }
1049 Some("condition") | Some("update") => {
1050 self.expression();
1051 }
1052 Some("body") => {
1053 self.statement();
1054 }
1055 _ => unreachable!(),
1056 };
1057 }
1058 self.cursor.goto_next_sibling()
1059 } {}
1060 self.cursor.goto_parent();
1061 Type::Unknown
1062 }
1063
1064 fn spread_parameter(&mut self) -> Type {
1065 self.cursor.goto_first_child();
1066 let mut parameter_type = None;
1067 while {
1068 let node = self.cursor.node();
1069 if node.is_relevant() {
1070 match access!(self kind) {
1071 "modifiers" => {
1072 self.modifiers();
1073 }
1074 "variable_declarator" => self.variable_declarator(parameter_type.take()),
1075 _ => {
1076 parameter_type.replace(self.unannotated_type());
1077 }
1078 };
1079 }
1080 self.cursor.goto_next_sibling()
1081 } {}
1082 self.cursor.goto_parent();
1083 Type::Unknown
1084 }
1085
1086 fn instanceof_expression(&mut self) -> Type {
1087 self.cursor.goto_first_child();
1088 let mut instance_type = None;
1089 while {
1090 let node = self.cursor.node();
1091 if node.is_relevant() {
1092 match access!(self field) {
1093 Some("left") => {
1094 self.expression();
1095 }
1096 Some("right") => {
1097 instance_type.replace(self.r#type());
1098 }
1099 Some("name") => {
1100 self.read_typed_declaration(instance_type.take().unwrap_or(Type::Unknown));
1101 }
1102 field => unreachable!(
1103 "Field {field:?} of kind {} should be unreachable in instanceof_expression",
1104 node.kind()
1105 ),
1106 };
1107 }
1108 self.cursor.goto_next_sibling()
1109 } {}
1110 self.cursor.goto_parent();
1111 Type::Unknown
1112 }
1113
1114 fn formal_parameter(&mut self) -> Type {
1115 self.cursor.goto_first_child();
1116 let mut parameter_type = None;
1117 while {
1118 let node = self.cursor.node();
1119 if node.is_relevant() {
1120 match access!(self field) {
1121 Some("type") => {
1122 parameter_type.replace(self.unannotated_type());
1123 }
1124 Some("dimensions") => {
1125 self.dimensions();
1126 }
1127 Some("name") => {
1128 let parameter_type = parameter_type.take().unwrap_or(Type::Unknown);
1129 self.read_typed_declaration(parameter_type)
1130 }
1131 _ if access!(self kind) == "modifiers" => {
1132 self.modifiers();
1133 }
1134 _ => unreachable!(),
1135 };
1136 }
1137 self.cursor.goto_next_sibling()
1138 } {}
1139 self.cursor.goto_parent();
1140
1141 Type::Unknown
1142 }
1143 fn method_declaration(&mut self) -> Type {
1144 let mut scope: Scope = self.cursor.node().into();
1145 self.cursor.goto_first_child();
1146 while {
1147 let node = self.cursor.node();
1148 if node.is_relevant() {
1149 match access!(self field) {
1150 Some("type_parameters") => self.type_parameters(),
1151 Some("type") => self.unannotated_type(),
1152 Some("name") => {
1153 let Name { text: name, .. } = self.to_name(&node);
1154 scope.name = Some(name);
1155 self.read_declaration()
1156 }
1157 Some("parameters") => {
1158 self.enter_scope(scope.clone());
1159 self.formal_parameters()
1160 }
1161 Some("dimensions") => self.dimensions(),
1162 Some("body") => self.block(),
1163 _ if access!(self kind) == "throws" => self.type_list(),
1164 _ if access!(self kind) == "modifiers" => self.modifiers(),
1165 _ if node.kind() == "annotation" => self.annotation(),
1166 _ if node.kind() == "marker_annotation" => self.marker_annotation(),
1167 field => unreachable!("Node {} in {field:?} is unreachable", node.kind()),
1168 };
1169 }
1170 self.cursor.goto_next_sibling()
1171 } {}
1172 self.leave_scope();
1173 self.cursor.goto_parent();
1174 Type::Unknown
1175 }
1176
1177 fn constructor_declaration(&mut self) -> Type {
1178 let mut scope: Scope = self.cursor.node().into();
1179 self.cursor.goto_first_child();
1180 while {
1181 let node = self.cursor.node();
1182 if node.is_relevant() {
1183 match access!(self field) {
1184 Some("type_parameters") => self.type_parameters(),
1185 Some("type") => self.unannotated_type(),
1186 Some("name") => {
1187 let Name { text: name, .. } = self.to_name(&node);
1188 scope.name = Some(name);
1189 self.read_declaration()
1190 }
1191 Some("parameters") => {
1192 self.enter_scope(scope.clone());
1193 self.formal_parameters()
1194 }
1195 Some("dimensions") => self.dimensions(),
1196 Some("body") => self.constructor_body(),
1197 _ if access!(self kind) == "annotation"
1198 || access!(self kind) == "marker_annotation" =>
1199 {
1200 self.annotation()
1201 }
1202 _ if access!(self kind) == "throws" => self.type_list(),
1203 _ if access!(self kind) == "modifiers" => self.modifiers(),
1204 field => unreachable!("Node {} in {field:?} is unreachable", node.kind()),
1205 };
1206 }
1207 self.cursor.goto_next_sibling()
1208 } {}
1209 self.leave_scope();
1210 self.cursor.goto_parent();
1211 Type::Unknown
1212 }
1213
1214 visit!(constructor_body match kind
1215 explicit_constructor_invocation
1216 || statement);
1217 visit!(explicit_constructor_invocation match field
1218 type_arguments => type_list,
1219 constructor if kind this => this,
1220 constructor => super_,
1221 object => primary_expression,
1222 arguments => argument_list);
1223
1224 fn lambda_expression(&mut self) -> Type {
1225 self.enter_scope(self.cursor.node().into());
1226 let return_type = self.lambda();
1227 self.leave_scope();
1228 return_type
1229 }
1230
1231 visit!(lambda match field
1232 parameters if kind formal_parameters => formal_parameters,
1233 parameters if kind inferred_parameters => inferred_parameters,
1234 parameters => read_declaration,
1235 body if kind block => block,
1236 body => expression);
1237 visit!(inferred_parameters read_declaration);
1238 visit!(block statement);
1239
1240 fn r#type(&mut self) -> Type {
1241 rule!(self match kind
1242 annotated_type
1243 || unannotated_type)
1244 }
1245
1246 fn field_declaration(&mut self) -> Type {
1247 self.cursor.goto_first_child();
1248 let mut field_type = None;
1249 while {
1250 let node = self.cursor.node();
1251 if node.is_relevant() {
1252 match access!(self field) {
1253 Some("type") => {
1254 field_type.replace(self.unannotated_type());
1255 }
1256 Some("declarator") => {
1257 self.variable_declarator(field_type.clone());
1258 }
1259 _ => {
1260 field_type.replace(self.unannotated_type());
1261 }
1262 };
1263 }
1264 self.cursor.goto_next_sibling()
1265 } {}
1266 self.cursor.goto_parent();
1267 Type::Unknown
1268 }
1269
1270 fn record_declaration(&mut self) -> Type {
1271 let scope = self.cursor.node().into();
1272 if !self.cursor.goto_first_child() {
1273 return Type::Unknown;
1274 }
1275 let mut class_name = None;
1276 while {
1277 let node = self.cursor.node();
1278 if node.is_relevant() {
1279 match access!(self field) {
1280 Some("name") => {
1281 class_name.replace(self.read_type_declaration());
1282 }
1283 Some("type_parameters") => {
1284 self.type_parameters();
1285 }
1286 Some("parameters") => {
1287 self.formal_parameters();
1288 }
1289 Some("body") => {
1290 if let Some(name) = class_name {
1291 self.enter_named_scope(name, scope);
1292 self.class_body();
1293 self.leave_named_scope();
1294 } else {
1295 self.class_body();
1296 }
1297 self.cursor.goto_parent();
1298 return Type::Unknown;
1299 }
1300 Some("interfaces") => {
1301 self.types();
1302 }
1303 _ if node.kind() == "modifiers" => {
1304 self.modifiers();
1305 }
1306 field => unreachable!(
1307 "Field {field:?} of kind {} should be unreachable in record_declaration.",
1308 node.kind()
1309 ),
1310 };
1311 }
1312 self.cursor.goto_next_sibling()
1313 } {}
1314 self.cursor.goto_parent();
1315 Type::Unknown
1316 }
1317
1318 visit!(element_value_array_initializer value);
1319
1320 visit!(value match kind
1321 element_value_array_initializer,
1322 marker_annotation,
1323 annotation ||
1324 expression);
1325
1326 fn annotation_type_element_declaration(&mut self) -> Type {
1327 self.cursor.goto_first_child();
1328 let mut constant_type = None;
1329 while {
1330 let node = self.cursor.node();
1331 if node.is_relevant() {
1332 match access!(self field) {
1333 Some("type") => {
1334 constant_type.replace(self.unannotated_type());
1335 }
1336 Some("name") => {
1337 self.read_type_declaration();
1338 }
1339 Some("dimensions") => {
1340 self.dimensions();
1341 }
1342
1343 Some("value") => {
1344 self.value();
1345 }
1346 _ if node.kind() == "modifiers" => {
1347 self.modifiers();
1348 }
1349 _ => {
1350 constant_type.replace(self.unannotated_type());
1351 }
1352 };
1353 }
1354 self.cursor.goto_next_sibling()
1355 } {}
1356 self.cursor.goto_parent();
1357 Type::Unknown
1358 }
1359
1360 visit!(annotation_type_body match kind
1361 annotation_type_element_declaration,
1362 constant_declaration,
1363 class_declaration,
1364 interface_declaration,
1365 enum_declaration,
1366 annotation_type_declaration);
1367
1368 fn annotation_type_declaration(&mut self) -> Type {
1369 let scope = self.cursor.node().into();
1370 self.cursor.goto_first_child();
1371 let mut class_name = None;
1372 while {
1373 let node = self.cursor.node();
1374 if node.is_relevant() {
1375 match access!(self field) {
1376 Some("name") => {
1377 class_name.replace(self.read_type_declaration());
1378 }
1379 Some("body") => {
1380 if let Some(name) = class_name {
1381 self.enter_named_scope(name, scope);
1382 self.annotation_type_body();
1383 self.leave_named_scope();
1384 } else {
1385 self.class_body();
1386 }
1387 self.cursor.goto_parent();
1388 return Type::Unknown;
1389 }
1390 _ if node.kind() == "modifiers" => {
1391 self.modifiers();
1392 }
1393 _ => unreachable!(),
1394 };
1395 }
1396 self.cursor.goto_next_sibling()
1397 } {}
1398 self.cursor.goto_parent();
1399 Type::Unknown
1400 }
1401
1402 fn constant_declaration(&mut self) -> Type {
1403 self.cursor.goto_first_child();
1404 let mut constant_type = None;
1405 while {
1406 let node = self.cursor.node();
1407 if node.is_relevant() {
1408 match access!(self field) {
1409 Some("type") => {
1410 constant_type.replace(self.unannotated_type());
1411 }
1412 Some("declarator") => {
1413 self.variable_declarator(constant_type.clone());
1414 }
1415 _ if node.kind() == "modifiers" => {
1416 self.modifiers();
1417 }
1418 _ => {
1419 constant_type.replace(self.unannotated_type());
1420 }
1421 };
1422 }
1423 self.cursor.goto_next_sibling()
1424 } {}
1425 self.cursor.goto_parent();
1426 Type::Unknown
1427 }
1428
1429 visit!(extends_interfaces type_list);
1430
1431 visit!(interface_body match kind
1432 constant_declaration,
1433 enum_declaration,
1434 method_declaration,
1435 class_declaration,
1436 interface_declaration,
1437 annotation_type_declaration
1438 || noop);
1439
1440 fn interface_declaration(&mut self) -> Type {
1441 let scope: Scope = self.cursor.node().into();
1442 if !self.cursor.goto_first_child() {
1443 return Type::Unknown;
1444 }
1445 let mut class_name = None;
1446 while {
1447 let node = self.cursor.node();
1448 if node.is_relevant() {
1449 match access!(self field) {
1450 Some("name") => {
1451 class_name.replace(self.read_type_declaration());
1452 }
1453 Some("type_parameters") => {
1454 self.type_parameters();
1455 }
1456 Some("permits") => {
1457 self.types();
1458 }
1459 Some("body") => {
1460 if let Some(name) = class_name {
1461 self.enter_named_scope(name, scope);
1462 self.interface_body();
1463 self.leave_named_scope();
1464 } else {
1465 self.class_body();
1466 }
1467 self.cursor.goto_parent();
1468 return Type::Unknown;
1469 }
1470 _ if node.kind() == "modifiers" => {
1471 self.modifiers();
1472 }
1473 _ if node.kind() == "extends_interfaces" => {
1474 self.extends_interfaces();
1475 }
1476 _ => unreachable!(),
1477 };
1478 }
1479 self.cursor.goto_next_sibling()
1480 } {}
1481 self.cursor.goto_parent();
1482 Type::Unknown
1483 }
1484
1485 visit!(static_initializer block);
1486
1487 fn class_body_declaration(&mut self) -> Type {
1488 rule!(self match kind
1489 field_declaration,
1490 record_declaration,
1491 method_declaration,
1492 compact_constructor_declaration,
1493 class_declaration,
1494 interface_declaration,
1495 enum_declaration,
1496 block,
1497 static_initializer,
1498 constructor_declaration,
1499 annotation_type_declaration
1500 || noop)
1501 }
1502
1503 visit!(compact_constructor_declaration match field
1504 modifiers,
1505 name => use_scoped_identifier,
1506 body => block);
1507
1508 fn local_variable_declaration(&mut self) -> Type {
1509 self.cursor.goto_first_child();
1510 let mut variable_type = None;
1511 while {
1512 let node = self.cursor.node();
1513 if node.is_relevant() {
1514 match access!(self field) {
1515 Some("type") => {
1516 variable_type.replace(self.unannotated_type());
1517 }
1518 Some("declarator") => {
1519 self.variable_declarator(variable_type.clone());
1520 }
1521 _ if node.kind() == "modifiers" => {
1522 self.modifiers();
1523 }
1524 field => unreachable!(
1525 "Node {} in {field:?} should be unreachable in local_variable_declaration",
1526 node.kind()
1527 ),
1528 };
1529 }
1530 self.cursor.goto_next_sibling()
1531 } {}
1532 self.cursor.goto_parent();
1533 Type::Unknown
1534 }
1535
1536 fn variable_declarator(&mut self, variable_type: Option<Type>) {
1537 self.cursor.goto_first_child();
1538 while {
1539 let node = self.cursor.node();
1540 if node.is_relevant() {
1541 match access!(self field) {
1542 Some("name") => {
1543 if let Some(ref variable_type) = variable_type {
1544 self.read_typed_declaration(variable_type.clone());
1545 } else {
1546 self.read_declaration();
1547 }
1548 }
1549 Some("dimensions") => {
1550 self.dimensions();
1551 }
1552 Some("value") if access!(self kind) == "array_initializer" => {
1553 self.array_initializer();
1554 }
1555 Some("value") => {
1556 self.expression(); }
1559 field => unreachable!(
1560 "Node {} in {field:?} should be unreachable in variable_declarator",
1561 node.kind()
1562 ),
1563 }
1564 }
1565 self.cursor.goto_next_sibling()
1566 } {}
1567 self.cursor.goto_parent();
1568 }
1569
1570 visit!(array_initializer match kind
1571 expression ||
1572 array_initializer);
1573
1574 fn boolean(&self) -> Type {
1575 Type::Builtin(vec![Name {
1576 text: "boolean".to_string(),
1577 extent: None,
1578 }])
1579 }
1580
1581 fn int(&self) -> Type {
1582 Type::Builtin(vec![Name {
1583 text: "int".to_string(),
1584 extent: None,
1585 }])
1586 }
1587
1588 fn float(&self) -> Type {
1589 Type::Builtin(vec![Name {
1590 text: "int".to_string(),
1591 extent: None,
1592 }])
1593 }
1594
1595 fn character_literal(&self) -> Type {
1596 Type::Builtin(vec![Name {
1597 text: "char".to_string(),
1598 extent: None,
1599 }])
1600 }
1601
1602 fn string(&self) -> Type {
1603 Type::Builtin(vec![Name {
1604 text: "String".to_string(),
1605 extent: None,
1606 }])
1607 }
1608}