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
use super::{
Declaration, DeclarationKind, Extractor, Language, Reference, ReferenceKind, Scope, TokenKind,
};
impl Extractor<'_, '_> {
pub(super) fn declaration(&mut self, index: usize) -> Option<usize> {
let mut cursor = index;
let mut exported = false;
loop {
let word = self.text(cursor);
if self.rules.exported_keyword == Some(word) {
exported = true;
}
if self.rules.modifiers.contains(&word) {
cursor += 1;
// `pub(crate)` and similar carry a parenthesised scope.
if self.punct(cursor, "(") {
while cursor < self.tokens.len() && !self.punct(cursor, ")") {
cursor += 1;
}
cursor += 1;
}
continue;
}
break;
}
if self.rules.scope_keywords.contains(&self.text(cursor))
&& let Some(next) = self.open_named_scope(cursor)
{
return Some(next);
}
let keyword = self.text(cursor);
if keyword == "const" && self.punct(cursor.wrapping_sub(1), "*") {
// Rust raw-pointer pointees (`*const T`) are types, not constant
// declarations. The declaration walk sees every identifier, so
// this boundary must be explicit.
return None;
}
let Some((_, kind)) = self
.rules
.declarations
.iter()
.find(|(word, _)| *word == keyword)
else {
return self
.typed_function(cursor, exported)
.or_else(|| self.braced_member(cursor, exported));
};
// `func (recv) Name` and `func(params) { ... }` are a method and a
// function literal. They are not a `const (` / `var (` group: reading
// them as one declared the receiver and every parameter as a function.
if self.language == Language::Go && keyword == "func" && self.punct(cursor + 1, "(") {
return Some(self.go_func(cursor));
}
// Go groups declarations: `const ( A = 1\n B = 2 )` declares both, and
// the keyword is followed by a parenthesis rather than by a name.
if self.rules.grouped_declarations && self.punct(cursor + 1, "(") {
return Some(self.grouped_declarations(index, cursor + 2, *kind));
}
let name_index = cursor + 1;
if self.kind(name_index) != Some(TokenKind::Identifier) {
return None;
}
let name = self.text(name_index).to_owned();
// Go marks export by an initial capital rather than a keyword.
let exported = exported || name.starts_with(char::is_uppercase);
let test_only = self.test_only_at(index);
let declaration_span = self.span(index, name_index);
self.drop_waiting();
self.record_test_only_declaration(test_only, declaration_span);
let declaration = self.facts.declarations.len();
self.facts.declarations.push(Declaration {
name: name.clone(),
kind: *kind,
span: declaration_span,
extent: declaration_span,
owner: self.owner(),
exported,
});
self.heritage(name_index + 1, &name, *kind);
self.scopes.push(Scope {
name,
depth: None,
declaration: Some(declaration),
type_body: matches!(
kind,
DeclarationKind::Class
| DeclarationKind::Struct
| DeclarationKind::Interface
| DeclarationKind::Trait
| DeclarationKind::Enum
),
test_only,
});
Some(name_index + 1)
}
/// What a type declares itself to derive from or satisfy.
///
/// `extends` and `implements` are different edges and the graph keeps them
/// apart; Go and Solidity write only one relation, so everything after
/// their marker inherits. Swift writes the same list after a colon, and
/// only on a type: `func connect(pairing: Pairing)` is a parameter, not
/// heritage.
pub(super) fn heritage(&mut self, start: usize, owner: &str, declared: DeclarationKind) {
let limit = (start + 48).min(self.tokens.len());
let mut cursor = start;
let mut kind = None;
let mut swift_first = matches!(
declared,
DeclarationKind::Class
| DeclarationKind::Struct
| DeclarationKind::Enum
| DeclarationKind::Interface
| DeclarationKind::Trait
);
let inherit_first = matches!(declared, DeclarationKind::Class);
let mut generic = 0_i32;
while cursor < limit && !self.punct(cursor, "{") && !self.punct(cursor, ";") {
if self.punct(cursor, "<") {
generic += 1;
cursor += 1;
continue;
}
if self.punct(cursor, ">") {
generic -= 1;
cursor += 1;
continue;
}
if generic > 0 {
cursor += 1;
continue;
}
match self.text(cursor) {
// Solidity writes `contract Vault is Ownable` for what Java
// writes as `extends`.
"extends" | "is" => kind = Some(ReferenceKind::Inherits),
"implements" => kind = Some(ReferenceKind::Implements),
":" if self.language == Language::Swift && swift_first && kind.is_none() => {
kind = Some(if inherit_first {
ReferenceKind::Inherits
} else {
ReferenceKind::Implements
});
swift_first = false;
}
_ => {
if let Some(current) = kind
&& self.kind(cursor) == Some(TokenKind::Identifier)
&& !self.punct(cursor.wrapping_sub(1), ".")
{
self.facts.references.push(Reference {
name: self.text(cursor).to_owned(),
kind: current,
receiver: None,
span: self.span(cursor, cursor),
owner: Some(owner.to_owned()),
string_arguments: Vec::new(),
name_arguments: Vec::new(),
});
if inherit_first && current == ReferenceKind::Inherits {
kind = Some(ReferenceKind::Implements);
}
}
}
}
cursor += 1;
}
}
/// A Go method `func (s *Store) applyLocked` or a function literal
/// `func(name string) bool { ... }`.
///
/// The method name is the declaration and the open scope, so calls in the
/// body belong to the method. The receiver variable and literal parameters
/// are not declarations. A literal does not open a scope: its body stays
/// with the enclosing function.
fn go_func(&mut self, func_index: usize) -> usize {
let open = func_index + 1;
let close = self.matching_paren(open, self.tokens.len());
if !self.punct(close, ")") {
return func_index + 1;
}
let name_index = close + 1;
let method =
self.kind(name_index) == Some(TokenKind::Identifier) && self.punct(name_index + 1, "(");
if !method {
return close + 1;
}
let name = self.text(name_index).to_owned();
let exported = name.starts_with(char::is_uppercase);
let declaration_span = self.span(func_index, name_index);
self.drop_waiting();
let declaration = self.facts.declarations.len();
self.facts.declarations.push(Declaration {
name: name.clone(),
kind: DeclarationKind::Method,
span: declaration_span,
extent: declaration_span,
owner: self.go_receiver_type(open + 1, close),
exported,
});
self.scopes.push(Scope {
name,
depth: None,
declaration: Some(declaration),
type_body: false,
test_only: false,
});
name_index + 1
}
/// The type in `(s *Store)`, `(s Store)`, `(Store)`, or `(s *pkg.Store)`.
/// The receiver variable is the name before the type, so the type is the
/// last identifier in the list.
fn go_receiver_type(&self, start: usize, close: usize) -> Option<String> {
let mut last = None;
for cursor in start..close {
if self.kind(cursor) == Some(TokenKind::Identifier) {
last = Some(self.text(cursor).to_owned());
}
}
last
}
}