Line data Source code
1 : /*
2 : +----------------------------------------------------------------------+
3 : | LSParrot PHP LSP Extension |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) LSParrot GitHub Organization |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to the 0BSD license that is |
8 : | bundled with this package in the file LICENSE. |
9 : +----------------------------------------------------------------------+
10 : | Author: Go Kudo <zeriyoshi@gmail.com> |
11 : +----------------------------------------------------------------------+
12 : */
13 :
14 : #include "lsp_internal.h"
15 :
16 : #include <Zend/zend_language_parser.h>
17 :
18 : /* Call hierarchy (prepare / incomingCalls / outgoingCalls) and type
19 : * hierarchy (prepare / supertypes / subtypes).
20 : *
21 : * Callers and callees are matched by name with call-shape verification
22 : * (`->name(`, `::name(`, bare `name(`); receivers are resolved when the
23 : * information is cheaply available ($this, self/static, ClassName::) and
24 : * treated conservatively otherwise -- hierarchy views are informational, so
25 : * over-reporting a same-named candidate beats silently hiding a real caller.
26 : * Subtype discovery walks the project side of the symbol index and inspects
27 : * class headers whose file mentions the target short name. */
28 :
29 : #define LSP_HIER_KIND_FILE 1
30 : #define LSP_HIER_KIND_CLASS 5
31 : #define LSP_HIER_KIND_METHOD 6
32 : #define LSP_HIER_KIND_ENUM 10
33 : #define LSP_HIER_KIND_INTERFACE 11
34 : #define LSP_HIER_KIND_FUNCTION 12
35 :
36 : #define LSP_HIER_MAX_FILES 4096
37 : #define LSP_HIER_MAX_RESULTS 256
38 :
39 : typedef struct _lsp_hier_frame {
40 : zend_string *name;
41 : size_t name_offset;
42 : zend_long depth;
43 : bool is_class;
44 : bool valid;
45 : } lsp_hier_frame;
46 :
47 : /* PHP class/function names are case-insensitive; the tokenization fast-reject
48 : * must match the same way or differently-cased references vanish silently. */
49 28 : static inline bool lsp_hier_contains_ci(zend_string *haystack, const char *needle, size_t needle_length)
50 : {
51 28 : const char *p = ZSTR_VAL(haystack), *end = p + ZSTR_LEN(haystack);
52 : char lower, upper;
53 :
54 28 : if (needle_length == 0 || ZSTR_LEN(haystack) < needle_length) {
55 0 : return false;
56 : }
57 :
58 28 : lower = (char) tolower((unsigned char) needle[0]);
59 28 : upper = (char) toupper((unsigned char) needle[0]);
60 1920 : for (; p + needle_length <= end; p++) {
61 1912 : if ((*p == lower || *p == upper) && strncasecmp(p, needle, needle_length) == 0) {
62 20 : return true;
63 : }
64 : }
65 :
66 8 : return false;
67 : }
68 :
69 0 : static inline zend_string *lsp_hier_basename(zend_string *path)
70 : {
71 0 : const char *value = ZSTR_VAL(path), *slash;
72 :
73 0 : slash = strrchr(value, '/');
74 : #ifdef _WIN32
75 : {
76 : const char *backslash = strrchr(value, '\\');
77 : if (!slash || (backslash && backslash > slash)) {
78 : slash = backslash;
79 : }
80 : }
81 : #endif
82 :
83 0 : return slash ? zend_string_init(slash + 1, ZSTR_LEN(path) - (size_t) (slash + 1 - value), 0) : zend_string_copy(path);
84 : }
85 :
86 : /* full_start/full_end give the whole declaration extent (signature + body)
87 : * when known; pass 0/0 to fall back to the name span. selectionRange is
88 : * always the bare name, per the LSP contract. */
89 24 : static inline void lsp_hier_item(zval *item, zend_string *name, zend_long kind, zend_string *path, zend_string *contents, size_t sel_start, size_t sel_end, size_t full_start, size_t full_end, zend_string *detail, zend_string *container)
90 : {
91 : zend_string *uri;
92 : zval range, selection, data;
93 :
94 24 : if (full_end <= full_start || full_start > sel_start || full_end < sel_end) {
95 8 : full_start = sel_start;
96 8 : full_end = sel_end;
97 : }
98 :
99 24 : uri = lsp_uri_from_path(path);
100 24 : array_init(item);
101 24 : add_assoc_str(item, "name", zend_string_copy(name));
102 24 : add_assoc_long(item, "kind", kind);
103 24 : add_assoc_str(item, "uri", uri);
104 24 : lsp_range_from_offsets(contents, full_start, full_end, &range);
105 24 : add_assoc_zval(item, "range", &range);
106 24 : lsp_range_from_offsets(contents, sel_start, sel_end, &selection);
107 24 : add_assoc_zval(item, "selectionRange", &selection);
108 :
109 24 : if (detail) {
110 12 : add_assoc_str(item, "detail", zend_string_copy(detail));
111 : }
112 :
113 24 : array_init(&data);
114 24 : if (container) {
115 12 : add_assoc_str(&data, "container", zend_string_copy(container));
116 : }
117 24 : add_assoc_zval(item, "data", &data);
118 24 : }
119 :
120 56 : static inline bool lsp_hier_token_is_significant(zval *token)
121 : {
122 56 : zend_long id = lsp_token_long(token, "id", 0);
123 :
124 56 : return id != T_WHITESPACE && id != T_COMMENT && id != T_DOC_COMMENT;
125 : }
126 :
127 40 : static inline zval *lsp_hier_sig_token(HashTable *tokens, zend_long index, int direction)
128 : {
129 40 : zend_long i, count = (zend_long) zend_hash_num_elements(tokens);
130 : zval *token;
131 :
132 52 : for (i = index + direction; i >= 0 && i < count; i += direction) {
133 52 : token = zend_hash_index_find(tokens, (zend_ulong) i);
134 52 : if (token && Z_TYPE_P(token) == IS_ARRAY && lsp_hier_token_is_significant(token)) {
135 40 : return token;
136 : }
137 : }
138 :
139 0 : return NULL;
140 : }
141 :
142 : /* One linear pass locating the innermost NAMED function and class frames
143 : * containing a byte offset. Closures contribute no name, so a call inside a
144 : * closure attributes to the enclosing named method, matching PhpStorm. */
145 8 : static inline void lsp_hier_enclosing_decl(HashTable *tokens, zend_string *text, size_t target_offset, lsp_hier_frame *out_function, lsp_hier_frame *out_class)
146 : {
147 : lsp_hier_frame stack[64];
148 : zend_string *name;
149 : zval *token, *name_token;
150 8 : zend_long id, depth = 0;
151 8 : uint32_t i, count, stack_size = 0, name_index;
152 : size_t token_offset;
153 : bool pending_class, pending_function;
154 8 : zend_string *pending_name = NULL;
155 8 : size_t pending_name_offset = 0;
156 :
157 8 : memset(out_function, 0, sizeof(*out_function));
158 8 : memset(out_class, 0, sizeof(*out_class));
159 8 : pending_class = false;
160 8 : pending_function = false;
161 :
162 8 : count = zend_hash_num_elements(tokens);
163 224 : for (i = 0; i < count; i++) {
164 224 : token = zend_hash_index_find(tokens, i);
165 224 : if (!token || Z_TYPE_P(token) != IS_ARRAY) {
166 0 : continue;
167 : }
168 :
169 224 : id = lsp_token_long(token, "id", 0);
170 224 : token_offset = (size_t) lsp_token_long(token, "offset", 0);
171 :
172 224 : if (token_offset > target_offset) {
173 8 : break;
174 : }
175 :
176 216 : if (lsp_token_is_class_like(token)) {
177 8 : pending_class = true;
178 8 : pending_function = false;
179 8 : pending_name = lsp_next_string_token(tokens, i + 1);
180 8 : pending_name_offset = 0;
181 8 : continue;
182 : }
183 :
184 208 : if (id == T_FUNCTION) {
185 8 : pending_function = true;
186 8 : pending_class = false;
187 8 : name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
188 8 : pending_name = lsp_token_string(name_token, "text");
189 8 : pending_name_offset = name_token ? (size_t) lsp_token_long(name_token, "offset", 0) : 0;
190 8 : continue;
191 : }
192 :
193 200 : if (id == ';' && (pending_class || pending_function)) {
194 : /* Abstract/interface method or stray keyword: no body opens. */
195 0 : pending_class = false;
196 0 : pending_function = false;
197 0 : pending_name = NULL;
198 0 : continue;
199 : }
200 :
201 200 : if (id == '{' || id == T_CURLY_OPEN || id == T_DOLLAR_OPEN_CURLY_BRACES) {
202 12 : depth++;
203 12 : if ((pending_class || pending_function) && stack_size < sizeof(stack) / sizeof(stack[0])) {
204 12 : stack[stack_size].name = pending_name;
205 12 : stack[stack_size].name_offset = pending_name ? pending_name_offset : 0;
206 12 : stack[stack_size].depth = depth;
207 12 : stack[stack_size].is_class = pending_class;
208 12 : stack[stack_size].valid = true;
209 12 : stack_size++;
210 : }
211 12 : pending_class = false;
212 12 : pending_function = false;
213 12 : pending_name = NULL;
214 12 : continue;
215 : }
216 :
217 188 : if (id == '}') {
218 0 : if (stack_size > 0 && stack[stack_size - 1].depth == depth) {
219 0 : stack_size--;
220 : }
221 0 : if (depth > 0) {
222 0 : depth--;
223 : }
224 0 : continue;
225 : }
226 : }
227 :
228 20 : for (i = stack_size; i > 0; i--) {
229 12 : lsp_hier_frame *frame = &stack[i - 1];
230 :
231 12 : if (!out_function->valid && !frame->is_class && frame->name) {
232 4 : *out_function = *frame;
233 : }
234 :
235 12 : if (!out_class->valid && frame->is_class && frame->name) {
236 8 : *out_class = *frame;
237 : }
238 : }
239 :
240 : (void) text;
241 : (void) name;
242 8 : }
243 :
244 : /* Find the byte offset of `function NAME` (its name token) in contents,
245 : * optionally constrained to the body of the class declaring `container`. */
246 8 : static inline bool lsp_hier_find_function_decl(zend_string *contents, zend_string *name, zend_string *container_fqcn, size_t *name_offset, size_t *body_start, size_t *body_end)
247 : {
248 8 : zend_long body_depth = 0, id, depth, open_depth;
249 : zval tokens_zv, *token, *name_token, *peek;
250 : HashTable *tokens;
251 : zend_string *label;
252 : uint32_t i, j, count, name_index;
253 8 : size_t class_start = 0, class_body_start = 0, class_body_end = 0, bound_start = 0, bound_end;
254 8 : bool found = false;
255 :
256 8 : bound_end = ZSTR_LEN(contents);
257 8 : if (container_fqcn) {
258 8 : if (!lsp_find_class_header_for_name(contents, container_fqcn, &class_start, &class_body_start, &class_body_end, &body_depth)) {
259 0 : return false;
260 : }
261 8 : bound_start = class_body_start;
262 8 : bound_end = class_body_end;
263 : }
264 :
265 8 : ZVAL_UNDEF(&tokens_zv);
266 8 : lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
267 8 : if (Z_TYPE(tokens_zv) != IS_ARRAY) {
268 0 : if (!Z_ISUNDEF(tokens_zv)) {
269 0 : zval_ptr_dtor(&tokens_zv);
270 : }
271 :
272 0 : return false;
273 : }
274 :
275 8 : tokens = Z_ARRVAL(tokens_zv);
276 8 : count = zend_hash_num_elements(tokens);
277 272 : for (i = 0; i < count && !found; i++) {
278 264 : token = zend_hash_index_find(tokens, i);
279 264 : if (!token || Z_TYPE_P(token) != IS_ARRAY || lsp_token_long(token, "id", 0) != T_FUNCTION) {
280 252 : continue;
281 : }
282 :
283 12 : if (!lsp_token_in_bounds(token, bound_start, bound_end)) {
284 0 : continue;
285 : }
286 :
287 12 : name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
288 12 : label = lsp_token_string(name_token, "text");
289 12 : if (!label || !zend_string_equals_ci(label, name)) {
290 4 : continue;
291 : }
292 :
293 8 : *name_offset = (size_t) lsp_token_long(name_token, "offset", 0);
294 :
295 : /* Body: first '{' after the signature at its own depth; abstract
296 : * declarations end with ';' and have no body. */
297 8 : depth = 0;
298 8 : open_depth = -1;
299 8 : *body_start = 0;
300 8 : *body_end = 0;
301 144 : for (j = name_index + 1; j < count; j++) {
302 144 : peek = zend_hash_index_find(tokens, j);
303 144 : if (!peek || Z_TYPE_P(peek) != IS_ARRAY) {
304 0 : continue;
305 : }
306 144 : id = lsp_token_long(peek, "id", 0);
307 144 : if (id == '(' || id == '[') {
308 12 : depth++;
309 132 : } else if (id == ')' || id == ']') {
310 12 : depth--;
311 120 : } else if (id == ';' && depth == 0 && open_depth < 0) {
312 : /* Signature ended without a body: abstract/interface method. */
313 : break;
314 120 : } else if ((id == '{' || id == T_CURLY_OPEN || id == T_DOLLAR_OPEN_CURLY_BRACES) && depth == 0 && open_depth < 0) {
315 8 : *body_start = (size_t) lsp_token_long(peek, "offset", 0) + 1;
316 8 : open_depth = 1;
317 112 : } else if ((id == '{' || id == T_CURLY_OPEN || id == T_DOLLAR_OPEN_CURLY_BRACES) && open_depth >= 0) {
318 0 : open_depth++;
319 112 : } else if (id == '}' && open_depth >= 0) {
320 8 : open_depth--;
321 8 : if (open_depth == 0) {
322 8 : *body_end = (size_t) lsp_token_long(peek, "offset", 0);
323 8 : break;
324 : }
325 : }
326 : }
327 :
328 8 : found = true;
329 : }
330 :
331 8 : zval_ptr_dtor(&tokens_zv);
332 :
333 8 : return found;
334 : }
335 :
336 8 : static inline zend_string *lsp_hier_item_data_container(zval *item)
337 : {
338 8 : zval *data = lsp_array_find(item, "data");
339 :
340 8 : return data ? lsp_array_string(data, "container") : NULL;
341 : }
342 :
343 : /* ----------------------------------------------------------------------
344 : * prepareCallHierarchy
345 : * ---------------------------------------------------------------------- */
346 :
347 4 : extern void lsp_lsparrot_prepare_call_hierarchy(lsp_server *server, zval *return_value, lsp_document *document, zval *position)
348 : {
349 : lsp_hier_frame func_frame, class_frame;
350 : zend_long line, character;
351 : zend_string *word, *container, *path, *contents, *receiver_word, *receiver_class;
352 : zval *tokens_zv, item;
353 : size_t offset, word_start, name_offset, body_start, body_end;
354 : bool resolved, owns_contents;
355 :
356 4 : array_init(return_value);
357 :
358 4 : lsp_position_from_zval(position, &line, &character);
359 4 : offset = lsp_offset_at(document->text, line, character);
360 4 : word = lsp_word_at(document->text, offset);
361 4 : if (ZSTR_LEN(word) == 0 || ZSTR_VAL(word)[0] == '$') {
362 0 : zend_string_release(word);
363 :
364 0 : return;
365 : }
366 :
367 4 : lsp_index_join_worker(server);
368 :
369 4 : word_start = offset;
370 12 : while (word_start > 0 && lsp_doc_is_identifier_char(ZSTR_VAL(document->text)[word_start - 1])) {
371 8 : word_start--;
372 : }
373 :
374 4 : container = NULL;
375 4 : resolved = false;
376 :
377 : /* Declaration under the cursor? Verified against the token stream: the
378 : * word's own token must be preceded by T_FUNCTION (a comment ending in
379 : * the word "function" must not count). */
380 4 : tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
381 4 : if (tokens_zv && Z_TYPE_P(tokens_zv) == IS_ARRAY) {
382 4 : HashTable *doc_tokens = Z_ARRVAL_P(tokens_zv);
383 : zval *word_token, *prev_token;
384 4 : uint32_t token_count = zend_hash_num_elements(doc_tokens), token_index;
385 4 : bool is_declaration = false;
386 :
387 84 : for (token_index = 0; token_index < token_count; token_index++) {
388 84 : word_token = zend_hash_index_find(doc_tokens, token_index);
389 84 : if (!word_token || Z_TYPE_P(word_token) != IS_ARRAY) {
390 0 : continue;
391 : }
392 :
393 84 : if ((size_t) lsp_token_long(word_token, "offset", 0) != word_start) {
394 80 : continue;
395 : }
396 :
397 4 : if (lsp_token_long(word_token, "id", 0) == T_STRING) {
398 4 : prev_token = lsp_hier_sig_token(doc_tokens, (zend_long) token_index, -1);
399 4 : is_declaration = prev_token && lsp_token_long(prev_token, "id", 0) == T_FUNCTION;
400 : }
401 :
402 4 : break;
403 : }
404 :
405 4 : if (is_declaration) {
406 4 : lsp_hier_enclosing_decl(Z_ARRVAL_P(tokens_zv), document->text, word_start, &func_frame, &class_frame);
407 4 : if (class_frame.valid && class_frame.name) {
408 4 : container = lsp_resolve_class_name_at(document->text, class_frame.name, word_start);
409 : }
410 :
411 4 : lsp_hier_item(&item, word, container ? LSP_HIER_KIND_METHOD : LSP_HIER_KIND_FUNCTION,
412 4 : document->path, document->text, word_start, word_start + ZSTR_LEN(word), 0, 0, container, container);
413 4 : add_next_index_zval(return_value, &item);
414 4 : if (container) {
415 4 : zend_string_release(container);
416 4 : container = NULL;
417 : }
418 4 : resolved = true;
419 : }
420 : }
421 :
422 : /* Call site: resolve the receiver to find the declaring class. */
423 4 : if (!resolved) {
424 0 : receiver_class = NULL;
425 0 : if (word_start >= 2 && memcmp(ZSTR_VAL(document->text) + word_start - 2, "->", 2) == 0) {
426 0 : size_t recv_end = word_start - 2, recv_start;
427 :
428 0 : if (recv_end > 0 && ZSTR_VAL(document->text)[recv_end - 1] == '?') {
429 0 : recv_end--;
430 : }
431 0 : recv_start = recv_end;
432 0 : while (recv_start > 0 && (lsp_doc_is_identifier_char(ZSTR_VAL(document->text)[recv_start - 1]) || ZSTR_VAL(document->text)[recv_start - 1] == '$')) {
433 0 : recv_start--;
434 : }
435 0 : if (recv_end > recv_start) {
436 0 : receiver_word = zend_string_init(ZSTR_VAL(document->text) + recv_start, recv_end - recv_start, 0);
437 0 : if (zend_string_equals_literal(receiver_word, "$this")) {
438 0 : lsp_hier_enclosing_decl(Z_ARRVAL_P(tokens_zv), document->text, word_start, &func_frame, &class_frame);
439 0 : if (class_frame.valid && class_frame.name) {
440 0 : receiver_class = lsp_resolve_class_name_at(document->text, class_frame.name, word_start);
441 : }
442 0 : } else if (ZSTR_VAL(receiver_word)[0] == '$') {
443 0 : receiver_class = lsp_infer_receiver_class(server, document, receiver_word, word_start);
444 : }
445 0 : zend_string_release(receiver_word);
446 : }
447 : } else {
448 0 : bool public_only = true;
449 :
450 0 : if (lsp_static_member_receiver_class(document, word_start, word, &receiver_class, &public_only)) {
451 : /* receiver_class set by helper. */
452 : } else {
453 0 : receiver_class = NULL;
454 : }
455 : }
456 :
457 0 : if (receiver_class) {
458 0 : path = lsp_find_project_symbol_path(server, LSP_SYMBOL_CLASS, receiver_class);
459 0 : if (path) {
460 0 : lsp_document *target_document = lsp_document_for_path(server, path);
461 :
462 0 : contents = target_document ? zend_string_copy(target_document->text) : lsp_read_file(path);
463 0 : owns_contents = target_document || contents != zend_empty_string;
464 0 : if (owns_contents && contents != zend_empty_string &&
465 0 : lsp_hier_find_function_decl(contents, word, receiver_class, &name_offset, &body_start, &body_end)
466 : ) {
467 0 : lsp_hier_item(&item, word, LSP_HIER_KIND_METHOD, path, contents,
468 0 : name_offset, name_offset + ZSTR_LEN(word),
469 0 : name_offset, body_end > name_offset ? body_end + 1 : 0,
470 : receiver_class, receiver_class);
471 0 : add_next_index_zval(return_value, &item);
472 0 : resolved = true;
473 : }
474 0 : if (owns_contents) {
475 0 : zend_string_release(contents);
476 : }
477 0 : zend_string_release(path);
478 : }
479 0 : zend_string_release(receiver_class);
480 : }
481 : }
482 :
483 : /* Plain function reference. */
484 4 : if (!resolved) {
485 : zend_string *resolved_name, *namespace_name;
486 :
487 0 : namespace_name = lsp_document_namespace_at(document->text, word_start);
488 0 : resolved_name = namespace_name != zend_empty_string
489 0 : ? strpprintf(0, "%s\\%s", ZSTR_VAL(namespace_name), ZSTR_VAL(word))
490 0 : : zend_string_copy(word)
491 : ;
492 0 : if (namespace_name != zend_empty_string) {
493 0 : zend_string_release(namespace_name);
494 : }
495 :
496 0 : path = lsp_find_project_symbol_path(server, LSP_SYMBOL_FUNCTION, resolved_name);
497 0 : if (!path && !zend_string_equals(resolved_name, word)) {
498 0 : path = lsp_find_project_symbol_path(server, LSP_SYMBOL_FUNCTION, word);
499 : }
500 0 : zend_string_release(resolved_name);
501 :
502 0 : if (path) {
503 0 : contents = lsp_read_file(path);
504 0 : if (contents != zend_empty_string &&
505 0 : lsp_hier_find_function_decl(contents, word, NULL, &name_offset, &body_start, &body_end)
506 : ) {
507 0 : lsp_hier_item(&item, word, LSP_HIER_KIND_FUNCTION, path, contents,
508 0 : name_offset, name_offset + ZSTR_LEN(word),
509 0 : name_offset, body_end > name_offset ? body_end + 1 : 0,
510 : NULL, NULL);
511 0 : add_next_index_zval(return_value, &item);
512 : }
513 0 : if (contents != zend_empty_string) {
514 0 : zend_string_release(contents);
515 : }
516 0 : zend_string_release(path);
517 : }
518 : }
519 :
520 4 : zend_string_release(word);
521 : }
522 :
523 : /* ----------------------------------------------------------------------
524 : * incomingCalls
525 : * ---------------------------------------------------------------------- */
526 :
527 16 : static inline void lsp_hier_scan_file_for_callers(lsp_server *server, zval *results, zend_string *path, zend_string *contents, zend_string *target, bool method_like, uint32_t *result_count)
528 : {
529 : lsp_hier_frame func_frame, class_frame;
530 : zval tokens_zv, *token, *prev, *next, *existing, entry, item, ranges, range_zv;
531 : HashTable *tokens, callers;
532 : zend_string *label, *caller_key, *file_label;
533 : zend_long id, prev_id;
534 : uint32_t i, count;
535 : size_t token_offset;
536 :
537 16 : if (!lsp_hier_contains_ci(contents, ZSTR_VAL(target), ZSTR_LEN(target))) {
538 4 : return;
539 : }
540 :
541 12 : ZVAL_UNDEF(&tokens_zv);
542 12 : lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
543 12 : if (Z_TYPE(tokens_zv) != IS_ARRAY) {
544 0 : if (!Z_ISUNDEF(tokens_zv)) {
545 0 : zval_ptr_dtor(&tokens_zv);
546 : }
547 :
548 0 : return;
549 : }
550 :
551 12 : tokens = Z_ARRVAL(tokens_zv);
552 12 : count = zend_hash_num_elements(tokens);
553 12 : zend_hash_init(&callers, 8, NULL, ZVAL_PTR_DTOR, 0);
554 :
555 532 : for (i = 0; i < count && *result_count < LSP_HIER_MAX_RESULTS; i++) {
556 520 : token = zend_hash_index_find(tokens, i);
557 520 : if (!token || Z_TYPE_P(token) != IS_ARRAY || lsp_token_long(token, "id", 0) != T_STRING) {
558 448 : continue;
559 : }
560 :
561 72 : label = lsp_token_string(token, "text");
562 72 : if (!label || !zend_string_equals_ci(label, target)) {
563 60 : continue;
564 : }
565 :
566 12 : next = lsp_hier_sig_token(tokens, (zend_long) i, 1);
567 12 : if (!next || lsp_token_long(next, "id", 0) != '(') {
568 0 : continue;
569 : }
570 :
571 12 : prev = lsp_hier_sig_token(tokens, (zend_long) i, -1);
572 12 : prev_id = prev ? lsp_token_long(prev, "id", 0) : 0;
573 12 : if (prev_id == T_FUNCTION) {
574 8 : continue;
575 : }
576 :
577 4 : if (method_like) {
578 4 : if (prev_id != T_OBJECT_OPERATOR && prev_id != T_NULLSAFE_OBJECT_OPERATOR && prev_id != T_PAAMAYIM_NEKUDOTAYIM) {
579 0 : continue;
580 : }
581 : } else {
582 0 : if (prev_id == T_OBJECT_OPERATOR || prev_id == T_NULLSAFE_OBJECT_OPERATOR || prev_id == T_PAAMAYIM_NEKUDOTAYIM || prev_id == T_NEW || prev_id == T_CONST) {
583 0 : continue;
584 : }
585 : }
586 :
587 4 : token_offset = (size_t) lsp_token_long(token, "offset", 0);
588 4 : lsp_hier_enclosing_decl(tokens, contents, token_offset, &func_frame, &class_frame);
589 :
590 4 : if (func_frame.valid && func_frame.name) {
591 4 : caller_key = strpprintf(0, "%zu", func_frame.name_offset);
592 : } else {
593 0 : caller_key = zend_string_init("file", sizeof("file") - 1, 0);
594 : }
595 :
596 4 : existing = zend_hash_find(&callers, caller_key);
597 4 : if (!existing) {
598 4 : array_init(&entry);
599 :
600 8 : if (func_frame.valid && func_frame.name) {
601 4 : zend_string *container = NULL;
602 :
603 4 : if (class_frame.valid && class_frame.name) {
604 4 : container = lsp_resolve_class_name_at(contents, class_frame.name, func_frame.name_offset);
605 : }
606 4 : lsp_hier_item(&item, func_frame.name,
607 : container ? LSP_HIER_KIND_METHOD : LSP_HIER_KIND_FUNCTION,
608 4 : path, contents, func_frame.name_offset, func_frame.name_offset + ZSTR_LEN(func_frame.name),
609 : 0, 0, container, container);
610 4 : if (container) {
611 4 : zend_string_release(container);
612 : }
613 : } else {
614 : /* Top-level code: attribute the call to the file. */
615 0 : file_label = lsp_hier_basename(path);
616 0 : lsp_hier_item(&item, file_label, LSP_HIER_KIND_FILE, path, contents, token_offset, token_offset + ZSTR_LEN(target), 0, 0, NULL, NULL);
617 0 : zend_string_release(file_label);
618 : }
619 :
620 4 : add_assoc_zval(&entry, "from", &item);
621 4 : array_init(&ranges);
622 4 : add_assoc_zval(&entry, "fromRanges", &ranges);
623 4 : existing = zend_hash_update(&callers, caller_key, &entry);
624 4 : (*result_count)++;
625 : }
626 :
627 4 : zend_string_release(caller_key);
628 :
629 4 : lsp_range_from_offsets(contents, token_offset, token_offset + ZSTR_LEN(target), &range_zv);
630 : {
631 4 : zval *ranges_zv = lsp_array_find(existing, "fromRanges");
632 :
633 4 : if (ranges_zv && Z_TYPE_P(ranges_zv) == IS_ARRAY) {
634 4 : add_next_index_zval(ranges_zv, &range_zv);
635 : } else {
636 0 : zval_ptr_dtor(&range_zv);
637 : }
638 : }
639 : }
640 :
641 16 : ZEND_HASH_FOREACH_VAL(&callers, existing) {
642 : zval copy;
643 :
644 4 : ZVAL_COPY(©, existing);
645 4 : add_next_index_zval(results, ©);
646 : } ZEND_HASH_FOREACH_END();
647 12 : zend_hash_destroy(&callers);
648 :
649 12 : zval_ptr_dtor(&tokens_zv);
650 : }
651 :
652 4 : extern void lsp_lsparrot_call_hierarchy_incoming(lsp_server *server, zval *return_value, zval *params)
653 : {
654 4 : lsp_symbol_index *region = &server->symbol_index;
655 : const lsp_symbol_entry *entry;
656 : lsp_document *open_document;
657 : zval *item;
658 : zend_string *target, *container, *path, *contents;
659 : HashTable visited;
660 4 : uint32_t i, result_count = 0, file_count = 0;
661 : bool method_like, owns_contents;
662 :
663 4 : array_init(return_value);
664 :
665 4 : item = lsp_array_find(params, "item");
666 4 : target = item ? lsp_array_string(item, "name") : NULL;
667 4 : if (!target) {
668 0 : return;
669 : }
670 :
671 4 : container = lsp_hier_item_data_container(item);
672 4 : method_like = container != NULL;
673 :
674 4 : lsp_index_join_worker(server);
675 4 : lsp_symbol_index_table_ensure(region);
676 4 : zend_hash_init(&visited, 64, NULL, NULL, 0);
677 :
678 20 : for (i = 0; i < region->entry_count && result_count < LSP_HIER_MAX_RESULTS && file_count < LSP_HIER_MAX_FILES; i++) {
679 16 : entry = ®ion->entries[i];
680 16 : if ((entry->flags & (LSP_SYMBOL_ENTRY_DELETED | LSP_SYMBOL_ENTRY_VENDOR)) != 0) {
681 0 : continue;
682 : }
683 :
684 16 : path = zend_string_init(lsp_symbol_entry_path(region, entry), entry->path_length, 0);
685 32 : if (zend_hash_exists(&visited, path) ||
686 16 : lsp_path_value_contains_analysis_helper(ZSTR_VAL(path), ZSTR_LEN(path))
687 : ) {
688 0 : zend_string_release(path);
689 0 : continue;
690 : }
691 16 : zend_hash_add_empty_element(&visited, path);
692 16 : file_count++;
693 :
694 16 : open_document = lsp_document_for_path(server, path);
695 16 : contents = open_document ? zend_string_copy(open_document->text) : lsp_read_file(path);
696 16 : owns_contents = open_document || contents != zend_empty_string;
697 16 : if (owns_contents && contents != zend_empty_string) {
698 16 : lsp_hier_scan_file_for_callers(server, return_value, path, contents, target, method_like, &result_count);
699 : }
700 16 : if (owns_contents) {
701 16 : zend_string_release(contents);
702 : }
703 :
704 16 : zend_string_release(path);
705 : }
706 :
707 4 : zend_hash_destroy(&visited);
708 : }
709 :
710 : /* ----------------------------------------------------------------------
711 : * outgoingCalls
712 : * ---------------------------------------------------------------------- */
713 :
714 4 : static inline void lsp_hier_add_outgoing(lsp_server *server, HashTable *callees, zend_string *callee, zend_string *callee_container, zend_string *source_contents, size_t site_offset, uint32_t *result_count)
715 : {
716 : zval *existing, entry, item, ranges, range_zv, *ranges_zv;
717 : zend_string *key, *path, *contents;
718 : lsp_document *target_document;
719 : size_t name_offset, body_start, body_end;
720 4 : bool owns_contents, located = false;
721 :
722 4 : if (callee_container) {
723 4 : path = lsp_find_project_symbol_path(server, LSP_SYMBOL_CLASS, callee_container);
724 : } else {
725 0 : path = lsp_find_project_symbol_path(server, LSP_SYMBOL_FUNCTION, callee);
726 : }
727 :
728 4 : if (!path) {
729 0 : return;
730 : }
731 :
732 4 : key = strpprintf(0, "%s:%s", ZSTR_VAL(path), ZSTR_VAL(callee));
733 4 : existing = zend_hash_find(callees, key);
734 4 : if (!existing) {
735 4 : if (*result_count >= LSP_HIER_MAX_RESULTS) {
736 0 : zend_string_release(key);
737 0 : zend_string_release(path);
738 :
739 0 : return;
740 : }
741 :
742 4 : target_document = lsp_document_for_path(server, path);
743 4 : contents = target_document ? zend_string_copy(target_document->text) : lsp_read_file(path);
744 4 : owns_contents = target_document || contents != zend_empty_string;
745 :
746 8 : if (owns_contents && contents != zend_empty_string &&
747 4 : lsp_hier_find_function_decl(contents, callee, callee_container, &name_offset, &body_start, &body_end)
748 : ) {
749 6 : lsp_hier_item(&item, callee,
750 : callee_container ? LSP_HIER_KIND_METHOD : LSP_HIER_KIND_FUNCTION,
751 4 : path, contents, name_offset, name_offset + ZSTR_LEN(callee),
752 4 : name_offset, body_end > name_offset ? body_end + 1 : 0,
753 : callee_container, callee_container);
754 4 : located = true;
755 : }
756 :
757 4 : if (owns_contents) {
758 4 : zend_string_release(contents);
759 : }
760 :
761 4 : if (!located) {
762 0 : zend_string_release(key);
763 0 : zend_string_release(path);
764 :
765 0 : return;
766 : }
767 :
768 4 : array_init(&entry);
769 4 : add_assoc_zval(&entry, "to", &item);
770 4 : array_init(&ranges);
771 4 : add_assoc_zval(&entry, "fromRanges", &ranges);
772 4 : existing = zend_hash_update(callees, key, &entry);
773 4 : (*result_count)++;
774 : }
775 :
776 4 : zend_string_release(key);
777 4 : zend_string_release(path);
778 :
779 4 : lsp_range_from_offsets(source_contents, site_offset, site_offset + ZSTR_LEN(callee), &range_zv);
780 4 : ranges_zv = lsp_array_find(existing, "fromRanges");
781 4 : if (ranges_zv && Z_TYPE_P(ranges_zv) == IS_ARRAY) {
782 4 : add_next_index_zval(ranges_zv, &range_zv);
783 : } else {
784 0 : zval_ptr_dtor(&range_zv);
785 : }
786 : }
787 :
788 4 : extern void lsp_lsparrot_call_hierarchy_outgoing(lsp_server *server, zval *return_value, zval *params)
789 : {
790 : lsp_document *source_document;
791 : zval *item, *uri_zv, tokens_zv, *token, *prev, *next, *existing;
792 : HashTable *tokens, callees;
793 : zend_string *name, *container, *path, *contents, *label, *callee_container, *receiver_text, *resolved;
794 : zend_long id, prev_id;
795 4 : uint32_t i, count, result_count = 0;
796 4 : size_t name_offset, body_start = 0, body_end = 0, token_offset;
797 : bool owns_contents;
798 :
799 4 : array_init(return_value);
800 :
801 4 : item = lsp_array_find(params, "item");
802 4 : name = item ? lsp_array_string(item, "name") : NULL;
803 4 : uri_zv = item ? lsp_array_find(item, "uri") : NULL;
804 4 : if (!name || !uri_zv || Z_TYPE_P(uri_zv) != IS_STRING) {
805 0 : return;
806 : }
807 :
808 4 : container = lsp_hier_item_data_container(item);
809 4 : lsp_index_join_worker(server);
810 :
811 4 : path = lsp_uri_to_path(Z_STR_P(uri_zv));
812 4 : source_document = lsp_document_for_path(server, path);
813 4 : contents = source_document ? zend_string_copy(source_document->text) : lsp_read_file(path);
814 4 : owns_contents = source_document || contents != zend_empty_string;
815 4 : if (!owns_contents || contents == zend_empty_string ||
816 4 : !lsp_hier_find_function_decl(contents, name, container, &name_offset, &body_start, &body_end) ||
817 4 : body_end <= body_start
818 : ) {
819 0 : if (owns_contents) {
820 0 : zend_string_release(contents);
821 : }
822 0 : zend_string_release(path);
823 :
824 0 : return;
825 : }
826 :
827 4 : ZVAL_UNDEF(&tokens_zv);
828 4 : lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
829 4 : if (Z_TYPE(tokens_zv) != IS_ARRAY) {
830 0 : if (!Z_ISUNDEF(tokens_zv)) {
831 0 : zval_ptr_dtor(&tokens_zv);
832 : }
833 0 : if (owns_contents) {
834 0 : zend_string_release(contents);
835 : }
836 0 : zend_string_release(path);
837 :
838 0 : return;
839 : }
840 :
841 4 : tokens = Z_ARRVAL(tokens_zv);
842 4 : count = zend_hash_num_elements(tokens);
843 4 : zend_hash_init(&callees, 8, NULL, ZVAL_PTR_DTOR, 0);
844 :
845 264 : for (i = 0; i < count; i++) {
846 260 : token = zend_hash_index_find(tokens, i);
847 260 : if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_in_bounds(token, body_start, body_end)) {
848 204 : continue;
849 : }
850 :
851 56 : if (lsp_token_long(token, "id", 0) != T_STRING) {
852 52 : continue;
853 : }
854 :
855 4 : label = lsp_token_string(token, "text");
856 4 : next = lsp_hier_sig_token(tokens, (zend_long) i, 1);
857 4 : if (!label || !next || lsp_token_long(next, "id", 0) != '(') {
858 0 : continue;
859 : }
860 :
861 4 : prev = lsp_hier_sig_token(tokens, (zend_long) i, -1);
862 4 : prev_id = prev ? lsp_token_long(prev, "id", 0) : 0;
863 4 : token_offset = (size_t) lsp_token_long(token, "offset", 0);
864 4 : callee_container = NULL;
865 :
866 4 : if (prev_id == T_OBJECT_OPERATOR || prev_id == T_NULLSAFE_OBJECT_OPERATOR) {
867 : /* Receiver: the significant token before the arrow. */
868 : {
869 4 : zend_long arrow_index = -1, j;
870 : zval *probe;
871 :
872 4 : for (j = (zend_long) i - 1; j >= 0; j--) {
873 4 : probe = zend_hash_index_find(tokens, (zend_ulong) j);
874 4 : if (probe && Z_TYPE_P(probe) == IS_ARRAY && lsp_hier_token_is_significant(probe)) {
875 4 : arrow_index = j;
876 4 : break;
877 : }
878 : }
879 :
880 4 : probe = arrow_index > 0 ? lsp_hier_sig_token(tokens, arrow_index, -1) : NULL;
881 4 : receiver_text = lsp_token_string(probe, "text");
882 4 : id = probe ? lsp_token_long(probe, "id", 0) : 0;
883 :
884 4 : if (receiver_text && id == T_VARIABLE && zend_string_equals_literal(receiver_text, "$this") && container) {
885 4 : callee_container = zend_string_copy(container);
886 0 : } else if (receiver_text && id == T_VARIABLE && source_document) {
887 0 : callee_container = lsp_infer_receiver_class(server, source_document, receiver_text, token_offset);
888 : }
889 : }
890 :
891 4 : if (callee_container) {
892 4 : lsp_hier_add_outgoing(server, &callees, label, callee_container, contents, token_offset, &result_count);
893 4 : zend_string_release(callee_container);
894 : }
895 4 : continue;
896 : }
897 :
898 0 : if (prev_id == T_PAAMAYIM_NEKUDOTAYIM) {
899 0 : zval *probe = lsp_hier_sig_token(tokens, (zend_long) i - 1, -1);
900 : zend_string *class_text;
901 :
902 : /* Skip back over the :: itself to the class token. */
903 : {
904 : zend_long j;
905 :
906 0 : probe = NULL;
907 0 : for (j = (zend_long) i - 1; j >= 0; j--) {
908 0 : zval *candidate = zend_hash_index_find(tokens, (zend_ulong) j);
909 :
910 0 : if (!candidate || Z_TYPE_P(candidate) != IS_ARRAY || !lsp_hier_token_is_significant(candidate)) {
911 0 : continue;
912 : }
913 :
914 0 : if (lsp_token_long(candidate, "id", 0) == T_PAAMAYIM_NEKUDOTAYIM) {
915 0 : probe = lsp_hier_sig_token(tokens, j, -1);
916 0 : break;
917 : }
918 :
919 0 : break;
920 : }
921 : }
922 :
923 0 : class_text = lsp_token_string(probe, "text");
924 0 : if (class_text) {
925 0 : if (zend_string_equals_literal_ci(class_text, "self") || zend_string_equals_literal_ci(class_text, "static")) {
926 0 : callee_container = container ? zend_string_copy(container) : NULL;
927 0 : } else if (zend_string_equals_literal_ci(class_text, "parent")) {
928 0 : zend_long body_depth = 0;
929 0 : size_t class_start = 0, class_body_start = 0, class_body_end = 0;
930 :
931 0 : if (container && lsp_find_class_header_for_name(contents, container, &class_start, &class_body_start, &class_body_end, &body_depth)) {
932 0 : callee_container = lsp_class_extends_name(contents, class_start, class_body_start);
933 : }
934 0 : } else if (ZSTR_VAL(class_text)[0] != '$') {
935 0 : callee_container = lsp_resolve_class_name_at(contents, class_text, token_offset);
936 : }
937 : }
938 :
939 0 : if (callee_container) {
940 0 : lsp_hier_add_outgoing(server, &callees, label, callee_container, contents, token_offset, &result_count);
941 0 : zend_string_release(callee_container);
942 : }
943 0 : continue;
944 : }
945 :
946 0 : if (prev_id == T_FUNCTION || prev_id == T_NEW || prev_id == T_CONST) {
947 0 : continue;
948 : }
949 :
950 : /* Bare function call: lsp_hier_add_outgoing looks the name up in the
951 : * function index and drops unresolvable calls. */
952 0 : lsp_hier_add_outgoing(server, &callees, label, NULL, contents, token_offset, &result_count);
953 : }
954 :
955 8 : ZEND_HASH_FOREACH_VAL(&callees, existing) {
956 : zval copy;
957 :
958 4 : ZVAL_COPY(©, existing);
959 4 : add_next_index_zval(return_value, ©);
960 : } ZEND_HASH_FOREACH_END();
961 4 : zend_hash_destroy(&callees);
962 :
963 4 : zval_ptr_dtor(&tokens_zv);
964 4 : if (owns_contents) {
965 4 : zend_string_release(contents);
966 : }
967 4 : zend_string_release(path);
968 : }
969 :
970 : /* ----------------------------------------------------------------------
971 : * Type hierarchy
972 : * ---------------------------------------------------------------------- */
973 :
974 12 : static inline zend_long lsp_hier_type_kind(char symbol_kind)
975 : {
976 12 : switch (symbol_kind) {
977 0 : case LSP_SYMBOL_INTERFACE:
978 0 : return LSP_HIER_KIND_INTERFACE;
979 0 : case LSP_SYMBOL_ENUM:
980 0 : return LSP_HIER_KIND_ENUM;
981 12 : default:
982 12 : return LSP_HIER_KIND_CLASS;
983 : }
984 : }
985 :
986 12 : static inline char lsp_hier_symbol_kind_for_fqcn(lsp_server *server, zend_string *fqcn, zend_string **path_out)
987 : {
988 : static const char kinds[3] = { LSP_SYMBOL_CLASS, LSP_SYMBOL_INTERFACE, LSP_SYMBOL_ENUM };
989 : zend_string *path;
990 : size_t i;
991 :
992 12 : for (i = 0; i < sizeof(kinds); i++) {
993 12 : path = lsp_find_project_symbol_path(server, kinds[i], fqcn);
994 12 : if (path) {
995 12 : *path_out = path;
996 :
997 12 : return kinds[i];
998 : }
999 : }
1000 :
1001 0 : *path_out = NULL;
1002 :
1003 0 : return 0;
1004 : }
1005 :
1006 12 : static inline bool lsp_hier_type_item_for_fqcn(lsp_server *server, zend_string *fqcn, zval *item)
1007 : {
1008 : zend_string *path, *contents, *short_name;
1009 : const char *base;
1010 12 : size_t base_length, class_start = 0, body_start = 0, body_end = 0, name_offset;
1011 12 : zend_long body_depth = 0;
1012 : lsp_document *document;
1013 : char kind;
1014 12 : bool owns_contents, located = false;
1015 :
1016 12 : kind = lsp_hier_symbol_kind_for_fqcn(server, fqcn, &path);
1017 12 : if (!path) {
1018 0 : return false;
1019 : }
1020 :
1021 12 : document = lsp_document_for_path(server, path);
1022 12 : contents = document ? zend_string_copy(document->text) : lsp_read_file(path);
1023 12 : owns_contents = document || contents != zend_empty_string;
1024 :
1025 24 : if (owns_contents && contents != zend_empty_string &&
1026 12 : lsp_find_class_header_for_name(contents, fqcn, &class_start, &body_start, &body_end, &body_depth)
1027 : ) {
1028 12 : base = lsp_basename_from_fqcn(ZSTR_VAL(fqcn), ZSTR_LEN(fqcn), &base_length);
1029 12 : short_name = zend_string_init(base, base_length, 0);
1030 :
1031 : /* Anchor the selection on the declared name inside the header. */
1032 12 : name_offset = class_start;
1033 : {
1034 12 : const char *found = NULL, *cursor = ZSTR_VAL(contents) + class_start, *header_end = ZSTR_VAL(contents) + (body_start > 0 ? body_start - 1 : body_start);
1035 :
1036 12 : while (cursor < header_end) {
1037 12 : found = strstr(cursor, ZSTR_VAL(short_name));
1038 12 : if (!found || found >= header_end) {
1039 0 : found = NULL;
1040 0 : break;
1041 : }
1042 12 : if ((found == ZSTR_VAL(contents) || !lsp_doc_is_identifier_char(found[-1])) &&
1043 12 : !lsp_doc_is_identifier_char(found[base_length])
1044 : ) {
1045 12 : break;
1046 : }
1047 0 : cursor = found + 1;
1048 : }
1049 :
1050 12 : if (found) {
1051 12 : name_offset = (size_t) (found - ZSTR_VAL(contents));
1052 : }
1053 : }
1054 :
1055 18 : lsp_hier_item(item, fqcn, lsp_hier_type_kind(kind), path, contents, name_offset, name_offset + base_length,
1056 12 : class_start, body_end > class_start ? body_end + 1 : 0, NULL, NULL);
1057 12 : zend_string_release(short_name);
1058 12 : located = true;
1059 : }
1060 :
1061 12 : if (owns_contents) {
1062 12 : zend_string_release(contents);
1063 : }
1064 12 : zend_string_release(path);
1065 :
1066 12 : return located;
1067 : }
1068 :
1069 4 : extern void lsp_lsparrot_prepare_type_hierarchy(lsp_server *server, zval *return_value, lsp_document *document, zval *position)
1070 : {
1071 : zend_long line, character;
1072 : zend_string *word, *resolved;
1073 : zval item;
1074 : size_t offset;
1075 :
1076 4 : array_init(return_value);
1077 :
1078 4 : lsp_position_from_zval(position, &line, &character);
1079 4 : offset = lsp_offset_at(document->text, line, character);
1080 4 : word = lsp_word_at(document->text, offset);
1081 4 : if (ZSTR_LEN(word) == 0 || ZSTR_VAL(word)[0] == '$') {
1082 0 : zend_string_release(word);
1083 :
1084 0 : return;
1085 : }
1086 :
1087 4 : lsp_index_join_worker(server);
1088 :
1089 4 : resolved = lsp_resolve_class_name_at(document->text, word, offset);
1090 4 : if (!resolved) {
1091 0 : resolved = zend_string_copy(word);
1092 : }
1093 :
1094 4 : if (lsp_hier_type_item_for_fqcn(server, resolved, &item)) {
1095 4 : add_next_index_zval(return_value, &item);
1096 : }
1097 :
1098 4 : zend_string_release(resolved);
1099 4 : zend_string_release(word);
1100 : }
1101 :
1102 : /* Collect the resolved names after `extends`/`implements` in a class header. */
1103 24 : static inline void lsp_hier_collect_header_names(zend_string *contents, size_t class_start, size_t body_start, const char *keyword, zval *out)
1104 : {
1105 24 : const char *value = ZSTR_VAL(contents), *name_start, *name_end;
1106 : zend_string *raw, *resolved;
1107 : size_t p, header_end, keyword_end;
1108 :
1109 24 : header_end = body_start > 0 ? body_start - 1 : body_start;
1110 488 : for (p = class_start; p < header_end; p++) {
1111 472 : if (!lsp_keyword_at_slice(value, p, header_end, keyword, &keyword_end)) {
1112 464 : continue;
1113 : }
1114 :
1115 8 : p = keyword_end;
1116 : for (;;) {
1117 16 : while (p < header_end && (isspace((unsigned char) value[p]) || value[p] == ',')) {
1118 8 : p++;
1119 : }
1120 :
1121 8 : name_start = value + p;
1122 52 : while (p < header_end && (lsp_doc_is_identifier_char(value[p]) || value[p] == '\\')) {
1123 44 : p++;
1124 : }
1125 8 : name_end = value + p;
1126 :
1127 8 : if (name_end <= name_start) {
1128 0 : break;
1129 : }
1130 :
1131 8 : raw = zend_string_init(name_start, (size_t) (name_end - name_start), 0);
1132 8 : resolved = lsp_resolve_class_name_at(contents, raw, (size_t) (name_start - value));
1133 8 : zend_string_release(raw);
1134 8 : if (resolved) {
1135 8 : add_next_index_str(out, resolved);
1136 : }
1137 :
1138 16 : while (p < header_end && isspace((unsigned char) value[p])) {
1139 8 : p++;
1140 : }
1141 8 : if (p >= header_end || value[p] != ',') {
1142 : break;
1143 : }
1144 : }
1145 :
1146 8 : break;
1147 : }
1148 24 : }
1149 :
1150 4 : extern void lsp_lsparrot_type_hierarchy_supertypes(lsp_server *server, zval *return_value, zval *params)
1151 : {
1152 : zval *item, *uri_zv, names, *name_zv, super_item;
1153 : zend_string *fqcn, *path, *contents;
1154 : lsp_document *document;
1155 4 : size_t class_start = 0, body_start = 0, body_end = 0;
1156 4 : zend_long body_depth = 0;
1157 : bool owns_contents;
1158 :
1159 4 : array_init(return_value);
1160 :
1161 4 : item = lsp_array_find(params, "item");
1162 4 : fqcn = item ? lsp_array_string(item, "name") : NULL;
1163 4 : uri_zv = item ? lsp_array_find(item, "uri") : NULL;
1164 4 : if (!fqcn || !uri_zv || Z_TYPE_P(uri_zv) != IS_STRING) {
1165 0 : return;
1166 : }
1167 :
1168 4 : lsp_index_join_worker(server);
1169 :
1170 4 : path = lsp_uri_to_path(Z_STR_P(uri_zv));
1171 4 : document = lsp_document_for_path(server, path);
1172 4 : contents = document ? zend_string_copy(document->text) : lsp_read_file(path);
1173 4 : owns_contents = document || contents != zend_empty_string;
1174 :
1175 8 : if (owns_contents && contents != zend_empty_string &&
1176 4 : lsp_find_class_header_for_name(contents, fqcn, &class_start, &body_start, &body_end, &body_depth)
1177 : ) {
1178 4 : array_init(&names);
1179 4 : lsp_hier_collect_header_names(contents, class_start, body_start, "extends", &names);
1180 4 : lsp_hier_collect_header_names(contents, class_start, body_start, "implements", &names);
1181 :
1182 8 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL(names), name_zv) {
1183 4 : if (Z_TYPE_P(name_zv) == IS_STRING && lsp_hier_type_item_for_fqcn(server, Z_STR_P(name_zv), &super_item)) {
1184 4 : add_next_index_zval(return_value, &super_item);
1185 : }
1186 : } ZEND_HASH_FOREACH_END();
1187 :
1188 4 : zval_ptr_dtor(&names);
1189 : }
1190 :
1191 4 : if (owns_contents) {
1192 4 : zend_string_release(contents);
1193 : }
1194 4 : zend_string_release(path);
1195 : }
1196 :
1197 4 : extern void lsp_lsparrot_type_hierarchy_subtypes(lsp_server *server, zval *return_value, zval *params)
1198 : {
1199 4 : lsp_symbol_index *region = &server->symbol_index;
1200 : const lsp_symbol_entry *entry;
1201 : lsp_document *document;
1202 : zval *item, names, *name_zv, sub_item;
1203 : zend_string *target, *entry_fqcn, *path, *contents;
1204 : const char *target_base;
1205 : HashTable visited;
1206 : size_t target_base_length, class_start, body_start, body_end;
1207 : zend_long body_depth;
1208 4 : uint32_t i, result_count = 0;
1209 : bool owns_contents, matched;
1210 :
1211 4 : array_init(return_value);
1212 :
1213 4 : item = lsp_array_find(params, "item");
1214 4 : target = item ? lsp_array_string(item, "name") : NULL;
1215 4 : if (!target) {
1216 0 : return;
1217 : }
1218 :
1219 4 : target_base = lsp_basename_from_fqcn(ZSTR_VAL(target), ZSTR_LEN(target), &target_base_length);
1220 :
1221 4 : lsp_index_join_worker(server);
1222 4 : lsp_symbol_index_table_ensure(region);
1223 4 : zend_hash_init(&visited, 64, NULL, NULL, 0);
1224 :
1225 20 : for (i = 0; i < region->entry_count && result_count < LSP_HIER_MAX_RESULTS; i++) {
1226 16 : entry = ®ion->entries[i];
1227 16 : if ((entry->flags & (LSP_SYMBOL_ENTRY_DELETED | LSP_SYMBOL_ENTRY_VENDOR)) != 0 ||
1228 16 : (entry->kind != LSP_SYMBOL_CLASS && entry->kind != LSP_SYMBOL_INTERFACE && entry->kind != LSP_SYMBOL_ENUM)
1229 : ) {
1230 0 : continue;
1231 : }
1232 :
1233 16 : entry_fqcn = zend_string_init(lsp_symbol_entry_fqcn(region, entry), entry->fqcn_length, 0);
1234 16 : if (zend_string_equals_ci(entry_fqcn, target) || zend_hash_exists(&visited, entry_fqcn)) {
1235 4 : zend_string_release(entry_fqcn);
1236 4 : continue;
1237 : }
1238 12 : zend_hash_add_empty_element(&visited, entry_fqcn);
1239 :
1240 12 : path = zend_string_init(lsp_symbol_entry_path(region, entry), entry->path_length, 0);
1241 12 : document = lsp_document_for_path(server, path);
1242 12 : contents = document ? zend_string_copy(document->text) : lsp_read_file(path);
1243 12 : owns_contents = document || contents != zend_empty_string;
1244 12 : matched = false;
1245 :
1246 24 : if (owns_contents && contents != zend_empty_string &&
1247 : /* Fast reject: files that never mention the short name cannot
1248 : * extend/implement it. */
1249 12 : lsp_hier_contains_ci(contents, target_base, target_base_length)
1250 : ) {
1251 8 : class_start = 0;
1252 8 : body_start = 0;
1253 8 : body_end = 0;
1254 8 : body_depth = 0;
1255 8 : if (lsp_find_class_header_for_name(contents, entry_fqcn, &class_start, &body_start, &body_end, &body_depth)) {
1256 8 : array_init(&names);
1257 8 : lsp_hier_collect_header_names(contents, class_start, body_start, "extends", &names);
1258 8 : lsp_hier_collect_header_names(contents, class_start, body_start, "implements", &names);
1259 :
1260 8 : ZEND_HASH_FOREACH_VAL(Z_ARRVAL(names), name_zv) {
1261 4 : if (Z_TYPE_P(name_zv) == IS_STRING && zend_string_equals_ci(Z_STR_P(name_zv), target)) {
1262 4 : matched = true;
1263 4 : break;
1264 : }
1265 : } ZEND_HASH_FOREACH_END();
1266 :
1267 8 : zval_ptr_dtor(&names);
1268 : }
1269 : }
1270 :
1271 12 : if (matched && lsp_hier_type_item_for_fqcn(server, entry_fqcn, &sub_item)) {
1272 4 : add_next_index_zval(return_value, &sub_item);
1273 4 : result_count++;
1274 : }
1275 :
1276 12 : if (owns_contents) {
1277 12 : zend_string_release(contents);
1278 : }
1279 12 : zend_string_release(path);
1280 12 : zend_string_release(entry_fqcn);
1281 : }
1282 :
1283 4 : zend_hash_destroy(&visited);
1284 : }
|