LCOV - code coverage report
Current view: top level - ext - lsp_server_definition.c (source / functions) Coverage Total Hit
Test: Extension code coverage Lines: 72.6 % 716 520
Test Date: 2026-07-13 00:58:38 Functions: 96.4 % 28 27

            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           96 : static inline bool lsp_definition_string_equals_ci(zend_string *left, zend_string *right)
      17              : {
      18           96 :         return ZSTR_LEN(left) == ZSTR_LEN(right) && strncasecmp(ZSTR_VAL(left), ZSTR_VAL(right), ZSTR_LEN(left)) == 0;
      19              : }
      20              : 
      21            4 : static inline void lsp_definition_position_from_offset(zend_string *text, size_t offset, zval *position)
      22              : {
      23              :         const char *value;
      24              :         zend_long line;
      25              :         size_t i, line_start, length;
      26              : 
      27            4 :         value = ZSTR_VAL(text);
      28            4 :         line = 0;
      29            4 :         line_start = 0;
      30            4 :         length = offset > ZSTR_LEN(text) ? ZSTR_LEN(text) : offset;
      31              : 
      32          304 :         for (i = 0; i < length; i++) {
      33          300 :                 if (value[i] == '\n') {
      34           20 :                         line++;
      35           20 :                         line_start = i + 1;
      36              :                 }
      37              :         }
      38              : 
      39            4 :         array_init(position);
      40            4 :         add_assoc_long(position, "line", line);
      41            4 :         add_assoc_long(position, "character", (zend_long) (length - line_start));
      42            4 : }
      43              : 
      44           84 : static inline void lsp_definition_location_from_offsets(zend_string *path, zend_string *contents, size_t start_offset, size_t end_offset, zval *location)
      45              : {
      46              :         zend_string *uri;
      47              :         zval range;
      48              : 
      49           84 :         if (end_offset < start_offset) {
      50            0 :                 end_offset = start_offset;
      51              :         }
      52              : 
      53           84 :         uri = lsp_uri_from_path(path);
      54           84 :         array_init(location);
      55           84 :         add_assoc_str(location, "uri", uri);
      56           84 :         lsp_range_from_offsets(contents, start_offset, end_offset, &range);
      57           84 :         add_assoc_zval(location, "range", &range);
      58           84 : }
      59              : 
      60           40 : static inline bool lsp_method_definition_in_contents(zend_string *path, zend_string *contents, zend_string *class_name, zend_string *member_name, zval *return_value, zend_string **parent_class)
      61              : {
      62           40 :         zend_long body_depth = 0;
      63              :         zend_string *label;
      64              :         zval tokens_zv, *token, *name_token;
      65              :         HashTable *tokens;
      66              :         uint32_t i, count, name_index;
      67           40 :         size_t class_start = 0, body_start = 0, body_end = 0, name_offset, promoted_param_start;
      68              :         bool class_found;
      69              : 
      70           40 :         *parent_class = NULL;
      71              : 
      72           40 :         ZVAL_UNDEF(&tokens_zv);
      73           40 :         lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
      74              : 
      75              :         /* Files may declare several classes; scan the one actually targeted and
      76              :          * only fall back to the first class when the name cannot be matched. */
      77           40 :         class_found = class_name && lsp_find_class_header_for_name(contents, class_name, &class_start, &body_start, &body_end, &body_depth);
      78           40 :         if (!class_found) {
      79            0 :                 class_found = lsp_find_first_class_header(contents, &class_start, &body_start, &body_end, &body_depth);
      80              :         }
      81              : 
      82           40 :         if (Z_TYPE(tokens_zv) != IS_ARRAY || !class_found) {
      83            0 :                 if (!Z_ISUNDEF(tokens_zv)) {
      84            0 :                         zval_ptr_dtor(&tokens_zv);
      85              :                 }
      86              : 
      87            0 :                 return false;
      88              :         }
      89              : 
      90           40 :         tokens = Z_ARRVAL(tokens_zv);
      91           40 :         count = zend_hash_num_elements(tokens);
      92         1256 :         for (i = 0; i < count; i++) {
      93         1244 :                 token = zend_hash_index_find(tokens, i);
      94         1244 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_in_bounds(token, body_start, body_end)) {
      95          732 :                         continue;
      96              :                 }
      97              : 
      98          512 :                 if (!lsp_token_at_depth(contents, token, body_depth)) {
      99          188 :                         continue;
     100              :                 }
     101              : 
     102              :                 /* Property declarations (including constructor-promoted params). */
     103          324 :                 if (lsp_token_name_equals(token, "T_VARIABLE")) {
     104            4 :                         label = lsp_token_string(token, "text");
     105            4 :                         if (!label ||
     106            4 :                                 ZSTR_LEN(label) != ZSTR_LEN(member_name) + 1 ||
     107            0 :                                 ZSTR_VAL(label)[0] != '$' ||
     108            0 :                                 memcmp(ZSTR_VAL(label) + 1, ZSTR_VAL(member_name), ZSTR_LEN(member_name)) != 0
     109              :                         ) {
     110            4 :                                 continue;
     111              :                         }
     112              : 
     113            0 :                         if (!lsp_token_is_property_declaration(tokens, i, contents, body_depth) &&
     114            0 :                                 !lsp_token_is_promoted_property(tokens, i, contents, body_depth, &promoted_param_start)
     115              :                         ) {
     116            0 :                                 continue;
     117              :                         }
     118              : 
     119            0 :                         name_offset = (size_t) lsp_token_long(token, "offset", 0);
     120            0 :                         lsp_definition_location_from_offsets(path, contents, name_offset, name_offset + ZSTR_LEN(label), return_value);
     121            0 :                         zval_ptr_dtor(&tokens_zv);
     122              : 
     123            0 :                         return true;
     124              :                 }
     125              : 
     126          320 :                 if (!lsp_token_name_equals(token, "T_FUNCTION")) {
     127          284 :                         continue;
     128              :                 }
     129              : 
     130           36 :                 name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
     131           36 :                 label = lsp_token_string(name_token, "text");
     132           36 :                 if (!label || !zend_string_equals(label, member_name)) {
     133            8 :                         continue;
     134              :                 }
     135              : 
     136           28 :                 name_offset = (size_t) lsp_token_long(name_token, "offset", 0);
     137           28 :                 lsp_definition_location_from_offsets(path, contents, name_offset, name_offset + ZSTR_LEN(label), return_value);
     138           28 :                 zval_ptr_dtor(&tokens_zv);
     139              : 
     140           28 :                 return true;
     141              :         }
     142              : 
     143           12 :         *parent_class = lsp_class_extends_name(contents, class_start, body_start);
     144           12 :         zval_ptr_dtor(&tokens_zv);
     145              : 
     146           12 :         return false;
     147              : }
     148              : 
     149           40 : extern bool lsp_project_method_definition_for_class(lsp_server *server, zend_string *class_name, zend_string *member_name, zval *return_value, uint32_t depth)
     150              : {
     151              :         zend_string *path, *contents, *parent_class;
     152              :         zval traits, *trait_zv;
     153              :         bool found;
     154              : 
     155           40 :         if (depth > 64) {
     156            0 :                 return false;
     157              :         }
     158              : 
     159           40 :         path = lsp_find_project_symbol_path(server, LSP_SYMBOL_CLASS, class_name);
     160           40 :         if (!path) {
     161            0 :                 return false;
     162              :         }
     163              : 
     164           40 :         contents = lsp_read_file(path);
     165           40 :         if (contents == zend_empty_string) {
     166            0 :                 zend_string_release(path);
     167              : 
     168            0 :                 return false;
     169              :         }
     170              : 
     171           40 :         parent_class = NULL;
     172           40 :         found = lsp_method_definition_in_contents(path, contents, class_name, member_name, return_value, &parent_class);
     173           40 :         if (found) {
     174           28 :                 zend_string_release(contents);
     175           28 :                 zend_string_release(path);
     176           28 :                 if (parent_class) {
     177            0 :                         zend_string_release(parent_class);
     178              :                 }
     179              : 
     180           28 :                 return true;
     181              :         }
     182              : 
     183              :         /* Methods brought in by traits declare in the trait's own file. */
     184           12 :         lsp_collect_class_trait_names_for(contents, class_name, &traits);
     185           12 :         zend_string_release(contents);
     186           12 :         zend_string_release(path);
     187           12 :         ZEND_HASH_FOREACH_VAL(Z_ARRVAL(traits), trait_zv) {
     188            8 :                 if (Z_TYPE_P(trait_zv) == IS_STRING &&
     189            4 :                         lsp_project_method_definition_for_class(server, Z_STR_P(trait_zv), member_name, return_value, depth + 1)
     190              :                 ) {
     191            4 :                         found = true;
     192            4 :                         break;
     193              :                 }
     194              :         } ZEND_HASH_FOREACH_END();
     195           12 :         zval_ptr_dtor(&traits);
     196              : 
     197           12 :         if (found) {
     198            4 :                 if (parent_class) {
     199            4 :                         zend_string_release(parent_class);
     200              :                 }
     201              : 
     202            4 :                 return true;
     203              :         }
     204              : 
     205            8 :         if (!parent_class) {
     206            0 :                 return false;
     207              :         }
     208              : 
     209            8 :         found = lsp_project_method_definition_for_class(server, parent_class, member_name, return_value, depth + 1);
     210            8 :         zend_string_release(parent_class);
     211              : 
     212            8 :         return found;
     213              : }
     214              : 
     215          276 : static inline lsp_method_visibility lsp_definition_member_visibility(HashTable *tokens, uint32_t index, zend_string *text, zend_long body_depth)
     216              : {
     217              :         zend_long i;
     218              :         zval *token;
     219              : 
     220         1352 :         for (i = (zend_long) index - 1; i >= 0; i--) {
     221         1328 :                 token = zend_hash_index_find(tokens, (zend_ulong) i);
     222         1328 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     223            0 :                         continue;
     224              :                 }
     225              : 
     226         1328 :                 if (!lsp_token_at_depth(text, token, body_depth)) {
     227          408 :                         continue;
     228              :                 }
     229              : 
     230          920 :                 if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{') || lsp_token_is_char(token, '}')) {
     231              :                         break;
     232              :                 }
     233              : 
     234          880 :                 if (lsp_token_name_equals(token, "T_PRIVATE")) {
     235           32 :                         return LSP_METHOD_VISIBILITY_PRIVATE;
     236              :                 }
     237              : 
     238          848 :                 if (lsp_token_name_equals(token, "T_PROTECTED")) {
     239           44 :                         return LSP_METHOD_VISIBILITY_PROTECTED;
     240              :                 }
     241              : 
     242          804 :                 if (lsp_token_name_equals(token, "T_PUBLIC")) {
     243          136 :                         return LSP_METHOD_VISIBILITY_PUBLIC;
     244              :                 }
     245              :         }
     246              : 
     247           64 :         return LSP_METHOD_VISIBILITY_PUBLIC;
     248              : }
     249              : 
     250          232 : static inline bool lsp_definition_member_is_static(HashTable *tokens, uint32_t index, zend_string *text, zend_long body_depth)
     251              : {
     252              :         zend_long i;
     253              :         zval *token;
     254              : 
     255         1468 :         for (i = (zend_long) index - 1; i >= 0; i--) {
     256         1424 :                 token = zend_hash_index_find(tokens, (zend_ulong) i);
     257         1424 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     258            0 :                         continue;
     259              :                 }
     260              : 
     261         1424 :                 if (!lsp_token_at_depth(text, token, body_depth)) {
     262          724 :                         continue;
     263              :                 }
     264              : 
     265          700 :                 if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{') || lsp_token_is_char(token, '}')) {
     266              :                         break;
     267              :                 }
     268              : 
     269          624 :                 if (lsp_token_name_equals(token, "T_STATIC")) {
     270          112 :                         return true;
     271              :                 }
     272              :         }
     273              : 
     274          120 :         return false;
     275              : }
     276              : 
     277            8 : static inline bool lsp_definition_member_is_final(HashTable *tokens, uint32_t index, zend_string *text, zend_long body_depth)
     278              : {
     279              :         zend_long i;
     280              :         zval *token;
     281              : 
     282           92 :         for (i = (zend_long) index - 1; i >= 0; i--) {
     283           88 :                 token = zend_hash_index_find(tokens, (zend_ulong) i);
     284           88 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     285            0 :                         continue;
     286              :                 }
     287              : 
     288           88 :                 if (!lsp_token_at_depth(text, token, body_depth)) {
     289           52 :                         continue;
     290              :                 }
     291              : 
     292           36 :                 if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '{') || lsp_token_is_char(token, '}')) {
     293              :                         break;
     294              :                 }
     295              : 
     296           32 :                 if (lsp_token_name_equals(token, "T_FINAL")) {
     297            0 :                         return true;
     298              :                 }
     299              :         }
     300              : 
     301            8 :         return false;
     302              : }
     303              : 
     304          240 : static inline bool lsp_definition_static_visibility_allows(lsp_method_visibility visibility, bool public_only)
     305              : {
     306          240 :         if (visibility == LSP_METHOD_VISIBILITY_PRIVATE) {
     307           28 :                 return false;
     308              :         }
     309              : 
     310          212 :         return !public_only || visibility == LSP_METHOD_VISIBILITY_PUBLIC;
     311              : }
     312              : 
     313           28 : static inline bool lsp_definition_label_matches(zend_string *label, zend_string *member_name)
     314              : {
     315           28 :         if (zend_string_equals(label, member_name)) {
     316           16 :                 return true;
     317              :         }
     318              : 
     319           24 :         return ZSTR_LEN(member_name) > 0 &&
     320           12 :                 ZSTR_VAL(member_name)[0] == '$' &&
     321           24 :                 ZSTR_LEN(label) + 1 == ZSTR_LEN(member_name) &&
     322            0 :                 strncasecmp(ZSTR_VAL(label), ZSTR_VAL(member_name) + 1, ZSTR_LEN(label)) == 0
     323              :         ;
     324              : }
     325              : 
     326              : /* Match `member_name` among the names declared by the `const`/`case`
     327              :  * statement starting at token `index`; a const name is the T_STRING directly
     328              :  * before '=', an enum case name the first T_STRING after `case`. */
     329            8 : static inline bool lsp_definition_constant_name_location(zend_string *path, zend_string *contents, HashTable *tokens, uint32_t index, uint32_t count, zend_string *member_name, zval *return_value)
     330              : {
     331              :         zend_string *text;
     332            8 :         zval *token, *candidate = NULL;
     333              :         uint32_t j;
     334              :         size_t name_offset;
     335              :         bool is_case;
     336              : 
     337            8 :         is_case = lsp_token_name_equals(zend_hash_index_find(tokens, index), "T_CASE");
     338              : 
     339           32 :         for (j = index + 1; j < count; j++) {
     340           32 :                 token = zend_hash_index_find(tokens, j);
     341           32 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
     342            0 :                         continue;
     343              :                 }
     344              : 
     345           32 :                 if (lsp_token_is_char(token, ';') || lsp_token_is_char(token, '}')) {
     346              :                         break;
     347              :                 }
     348              : 
     349           32 :                 if (lsp_token_name_equals(token, "T_STRING")) {
     350            8 :                         if (is_case && candidate) {
     351              :                                 /* Only the first name after `case` is the declaration. */
     352            0 :                                 break;
     353              :                         }
     354            8 :                         candidate = token;
     355            8 :                         continue;
     356              :                 }
     357              : 
     358           24 :                 if ((lsp_token_is_char(token, '=') || is_case) && candidate) {
     359            8 :                         text = lsp_token_string(candidate, "text");
     360            8 :                         if (text && lsp_definition_label_matches(text, member_name)) {
     361            8 :                                 name_offset = (size_t) lsp_token_long(candidate, "offset", 0);
     362            8 :                                 lsp_definition_location_from_offsets(path, contents, name_offset, name_offset + ZSTR_LEN(text), return_value);
     363              : 
     364            8 :                                 return true;
     365              :                         }
     366            0 :                         candidate = NULL;
     367              :                 }
     368              :         }
     369              : 
     370              :         /* Bodyless enum case (`case Hearts;`) ends at the terminator. */
     371            0 :         if (is_case && candidate) {
     372            0 :                 text = lsp_token_string(candidate, "text");
     373            0 :                 if (text && lsp_definition_label_matches(text, member_name)) {
     374            0 :                         name_offset = (size_t) lsp_token_long(candidate, "offset", 0);
     375            0 :                         lsp_definition_location_from_offsets(path, contents, name_offset, name_offset + ZSTR_LEN(text), return_value);
     376              : 
     377            0 :                         return true;
     378              :                 }
     379              :         }
     380              : 
     381            0 :         return false;
     382              : }
     383              : 
     384           16 : static inline bool lsp_static_member_definition_in_contents(zend_string *path, zend_string *contents, zend_string *class_name, zend_string *member_name, bool public_only, zval *return_value, zend_string **parent_class)
     385              : {
     386           16 :         zend_long body_depth = 0;
     387              :         lsp_method_visibility visibility;
     388              :         zend_string *label;
     389              :         zval tokens_zv, *token, *name_token;
     390              :         HashTable *tokens;
     391              :         uint32_t i, count, name_index;
     392           16 :         size_t class_start = 0, body_start = 0, body_end = 0, name_offset;
     393              :         bool static_member, class_found;
     394              : 
     395           16 :         *parent_class = NULL;
     396              : 
     397           16 :         ZVAL_UNDEF(&tokens_zv);
     398           16 :         lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
     399              : 
     400              :         /* Same multi-class rule as lsp_method_definition_in_contents. */
     401           16 :         class_found = class_name && lsp_find_class_header_for_name(contents, class_name, &class_start, &body_start, &body_end, &body_depth);
     402           16 :         if (!class_found) {
     403            0 :                 class_found = lsp_find_first_class_header(contents, &class_start, &body_start, &body_end, &body_depth);
     404              :         }
     405              : 
     406           16 :         if (Z_TYPE(tokens_zv) != IS_ARRAY || !class_found) {
     407            0 :                 if (!Z_ISUNDEF(tokens_zv)) {
     408            0 :                         zval_ptr_dtor(&tokens_zv);
     409              :                 }
     410              : 
     411            0 :                 return false;
     412              :         }
     413              : 
     414           16 :         tokens = Z_ARRVAL(tokens_zv);
     415           16 :         count = zend_hash_num_elements(tokens);
     416          576 :         for (i = 0; i < count; i++) {
     417          576 :                 token = zend_hash_index_find(tokens, i);
     418          576 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_in_bounds(token, body_start, body_end) || !lsp_token_at_depth(contents, token, body_depth)) {
     419          336 :                         continue;
     420              :                 }
     421              : 
     422              :                 /* Class constants and enum cases resolve through :: regardless of
     423              :                  * staticness. */
     424          240 :                 if (lsp_token_name_equals(token, "T_CONST") || lsp_token_name_equals(token, "T_CASE")) {
     425            8 :                         visibility = lsp_definition_member_visibility(tokens, i, contents, body_depth);
     426            8 :                         if (!lsp_definition_static_visibility_allows(visibility, public_only)) {
     427            0 :                                 continue;
     428              :                         }
     429              : 
     430            8 :                         if (lsp_definition_constant_name_location(path, contents, tokens, i, count, member_name, return_value)) {
     431            8 :                                 zval_ptr_dtor(&tokens_zv);
     432              : 
     433            8 :                                 return true;
     434              :                         }
     435              : 
     436            0 :                         continue;
     437              :                 }
     438              : 
     439          232 :                 static_member = lsp_definition_member_is_static(tokens, i, contents, body_depth);
     440              : 
     441          232 :                 visibility = lsp_definition_member_visibility(tokens, i, contents, body_depth);
     442          232 :                 if (!lsp_definition_static_visibility_allows(visibility, public_only)) {
     443           28 :                         continue;
     444              :                 }
     445              : 
     446              :                 /* parent::/self::/static:: (public_only == false) legally target
     447              :                  * instance METHODS as well; other receivers and all property
     448              :                  * accesses require an actual static member. */
     449          204 :                 if (!static_member && (public_only || !lsp_token_name_equals(token, "T_FUNCTION"))) {
     450          108 :                         continue;
     451              :                 }
     452              : 
     453           96 :                 if (lsp_token_name_equals(token, "T_FUNCTION")) {
     454           12 :                         name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
     455           12 :                         label = lsp_token_string(name_token, "text");
     456           12 :                         if (!label || !lsp_definition_label_matches(label, member_name)) {
     457            4 :                                 continue;
     458              :                         }
     459              : 
     460            8 :                         name_offset = (size_t) lsp_token_long(name_token, "offset", 0);
     461            8 :                         lsp_definition_location_from_offsets(path, contents, name_offset, name_offset + ZSTR_LEN(label), return_value);
     462            8 :                         zval_ptr_dtor(&tokens_zv);
     463              : 
     464            8 :                         return true;
     465              :                 }
     466              : 
     467           84 :                 if (!lsp_token_name_equals(token, "T_VARIABLE") || !lsp_token_is_property_declaration(tokens, i, contents, body_depth)) {
     468           76 :                         continue;
     469              :                 }
     470              : 
     471            8 :                 label = lsp_token_string(token, "text");
     472            8 :                 if (!label || !lsp_definition_label_matches(label, member_name)) {
     473            8 :                         continue;
     474              :                 }
     475              : 
     476            0 :                 name_offset = (size_t) lsp_token_long(token, "offset", 0);
     477            0 :                 lsp_definition_location_from_offsets(path, contents, name_offset, name_offset + ZSTR_LEN(label), return_value);
     478            0 :                 zval_ptr_dtor(&tokens_zv);
     479              : 
     480            0 :                 return true;
     481              :         }
     482              : 
     483            0 :         *parent_class = lsp_class_extends_name(contents, class_start, body_start);
     484            0 :         zval_ptr_dtor(&tokens_zv);
     485              : 
     486            0 :         return false;
     487              : }
     488              : 
     489           16 : static inline bool lsp_project_static_member_definition_for_class(lsp_server *server, zend_string *class_name, zend_string *member_name, bool public_only, zval *return_value, uint32_t depth)
     490              : {
     491              :         zend_string *path, *contents, *parent_class;
     492              :         bool found;
     493              : 
     494           16 :         if (depth > 64) {
     495            0 :                 return false;
     496              :         }
     497              : 
     498           16 :         path = lsp_find_project_symbol_path(server, LSP_SYMBOL_CLASS, class_name);
     499           16 :         if (!path) {
     500            0 :                 return false;
     501              :         }
     502              : 
     503           16 :         contents = lsp_read_file(path);
     504           16 :         if (contents == zend_empty_string) {
     505            0 :                 zend_string_release(path);
     506              : 
     507            0 :                 return false;
     508              :         }
     509              : 
     510           16 :         parent_class = NULL;
     511           16 :         found = lsp_static_member_definition_in_contents(path, contents, class_name, member_name, public_only, return_value, &parent_class);
     512           16 :         zend_string_release(contents);
     513           16 :         zend_string_release(path);
     514           16 :         if (found) {
     515           16 :                 if (parent_class) {
     516            0 :                         zend_string_release(parent_class);
     517              :                 }
     518              : 
     519           16 :                 return true;
     520              :         }
     521              : 
     522            0 :         if (!parent_class) {
     523            0 :                 return false;
     524              :         }
     525              : 
     526            0 :         found = lsp_project_static_member_definition_for_class(server, parent_class, member_name, public_only, return_value, depth + 1);
     527            0 :         zend_string_release(parent_class);
     528              : 
     529            0 :         return found;
     530              : }
     531              : 
     532           80 : static inline bool lsp_definition_word_bounds(zend_string *text, zend_string *word, size_t offset, size_t *word_start, size_t *word_end)
     533              : {
     534              :         const char *value;
     535              :         size_t length, start, end;
     536              : 
     537           80 :         if (!text || !word || ZSTR_LEN(word) == 0) {
     538            0 :                 return false;
     539              :         }
     540              : 
     541           80 :         value = ZSTR_VAL(text);
     542           80 :         length = ZSTR_LEN(text);
     543           80 :         if (offset > length) {
     544            0 :                 offset = length;
     545              :         }
     546              : 
     547           80 :         start = offset;
     548          496 :         while (start > 0 && (lsp_doc_is_identifier_char(value[start - 1]) || value[start - 1] == '$' || value[start - 1] == '\\')) {
     549          416 :                 start--;
     550              :         }
     551              : 
     552           80 :         end = offset;
     553          424 :         while (end < length && (lsp_doc_is_identifier_char(value[end]) || value[end] == '$' || value[end] == '\\')) {
     554          344 :                 end++;
     555              :         }
     556              : 
     557           80 :         if (end <= start || end - start != ZSTR_LEN(word) || memcmp(value + start, ZSTR_VAL(word), ZSTR_LEN(word)) != 0) {
     558            0 :                 return false;
     559              :         }
     560              : 
     561           80 :         *word_start = start;
     562           80 :         *word_end = end;
     563              : 
     564           80 :         return true;
     565              : }
     566              : 
     567           80 : extern bool lsp_static_member_receiver_class(lsp_document *document, size_t offset, zend_string *word, zend_string **class_name, bool *public_only)
     568              : {
     569              :         const char *value;
     570              :         zend_string *receiver, *resolved;
     571              :         size_t word_start, word_end, i, class_start, class_end, current_class_start, current_body_start, current_body_end;
     572              :         zend_long current_body_depth;
     573              : 
     574           80 :         *class_name = NULL;
     575           80 :         *public_only = true;
     576              : 
     577           80 :         if (!lsp_definition_word_bounds(document->text, word, offset, &word_start, &word_end)) {
     578            0 :                 return false;
     579              :         }
     580              : 
     581           80 :         value = ZSTR_VAL(document->text);
     582           80 :         i = word_start;
     583          100 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
     584           20 :                 i--;
     585              :         }
     586              : 
     587           80 :         if (i < 2 || value[i - 2] != ':' || value[i - 1] != ':') {
     588           60 :                 return false;
     589              :         }
     590              : 
     591           20 :         i -= 2;
     592           20 :         while (i > 0 && isspace((unsigned char) value[i - 1])) {
     593            0 :                 i--;
     594              :         }
     595              : 
     596           20 :         class_end = i;
     597          132 :         while (i > 0 && (lsp_doc_is_identifier_char(value[i - 1]) || value[i - 1] == '\\')) {
     598          112 :                 i--;
     599              :         }
     600           20 :         class_start = i;
     601           20 :         if (class_end <= class_start) {
     602            0 :                 return false;
     603              :         }
     604              : 
     605           20 :         receiver = zend_string_init(value + class_start, class_end - class_start, 0);
     606           20 :         if (zend_string_equals_literal_ci(receiver, "parent")) {
     607            8 :                 if (!lsp_find_enclosing_class_header(document->text, offset, &current_class_start, &current_body_start, &current_body_end, &current_body_depth)) {
     608            0 :                         zend_string_release(receiver);
     609              : 
     610            0 :                         return false;
     611              :                 }
     612              : 
     613            8 :                 *class_name = lsp_class_extends_name(document->text, current_class_start, current_body_start);
     614            8 :                 zend_string_release(receiver);
     615            8 :                 *public_only = false;
     616              : 
     617            8 :                 return *class_name != NULL;
     618              :         }
     619              : 
     620           12 :         if (zend_string_equals_literal_ci(receiver, "self") || zend_string_equals_literal_ci(receiver, "static")) {
     621            0 :                 if (!lsp_find_enclosing_class_header(document->text, offset, &current_class_start, &current_body_start, &current_body_end, &current_body_depth)) {
     622            0 :                         zend_string_release(receiver);
     623              : 
     624            0 :                         return false;
     625              :                 }
     626              : 
     627            0 :                 *class_name = lsp_class_declared_name(document->text, current_class_start, current_body_start);
     628            0 :                 zend_string_release(receiver);
     629            0 :                 *public_only = false;
     630              : 
     631            0 :                 return *class_name != NULL;
     632              :         }
     633              : 
     634           12 :         resolved = lsp_resolve_class_name_at(document->text, receiver, offset);
     635           12 :         if (resolved) {
     636           12 :                 zend_string_release(receiver);
     637           12 :                 *class_name = resolved;
     638              :         } else {
     639            0 :                 *class_name = receiver;
     640              :         }
     641              : 
     642           12 :         return true;
     643              : }
     644              : 
     645           40 : static inline bool lsp_project_static_member_definition(lsp_server *server, lsp_document *document, size_t offset, zend_string *word, zval *return_value)
     646              : {
     647              :         zend_string *class_name;
     648              :         bool public_only, found;
     649              : 
     650           40 :         if (!lsp_static_member_receiver_class(document, offset, word, &class_name, &public_only)) {
     651           24 :                 return false;
     652              :         }
     653              : 
     654           16 :         found = lsp_project_static_member_definition_for_class(server, class_name, word, public_only, return_value, 0);
     655           16 :         zend_string_release(class_name);
     656              : 
     657           16 :         return found;
     658              : }
     659              : 
     660           32 : static inline bool lsp_project_class_extends_class(lsp_server *server, zend_string *candidate_class, zend_string *base_class)
     661              : {
     662              :         zend_string *current, *next;
     663              :         zval *entry, *parent;
     664              :         HashTable visited;
     665              :         uint32_t depth;
     666              :         bool found;
     667              : 
     668           32 :         zend_hash_init(&visited, 8, NULL, NULL, 0);
     669           32 :         current = zend_string_copy(candidate_class);
     670           32 :         depth = 0;
     671           32 :         found = false;
     672              : 
     673           40 :         while (current && depth < 64) {
     674           40 :                 depth++;
     675           40 :                 if (zend_hash_exists(&visited, current)) {
     676            0 :                         break;
     677              :                 }
     678              : 
     679           40 :                 zend_hash_add_empty_element(&visited, current);
     680           40 :                 entry = lsp_class_member_cache_entry(server, current);
     681           40 :                 if (!entry || Z_TYPE_P(entry) != IS_ARRAY) {
     682              :                         break;
     683              :                 }
     684              : 
     685           40 :                 parent = zend_hash_str_find(Z_ARRVAL_P(entry), "parent", sizeof("parent") - 1);
     686           40 :                 next = parent && Z_TYPE_P(parent) == IS_STRING && Z_STRLEN_P(parent) > 0 ? zend_string_copy(Z_STR_P(parent)) : NULL;
     687           40 :                 if (!next) {
     688            8 :                         break;
     689              :                 }
     690              : 
     691           32 :                 if (lsp_definition_string_equals_ci(next, base_class)) {
     692           24 :                         zend_string_release(next);
     693           24 :                         found = true;
     694           24 :                         break;
     695              :                 }
     696              : 
     697            8 :                 zend_string_release(current);
     698            8 :                 current = next;
     699              :         }
     700              : 
     701           32 :         if (current) {
     702           32 :                 zend_string_release(current);
     703              :         }
     704              : 
     705           32 :         zend_hash_destroy(&visited);
     706              : 
     707           32 :         return found;
     708              : }
     709              : 
     710           24 : static inline bool lsp_project_declared_method_location(lsp_server *server, zend_string *class_name, zend_string *method_name, zval *location)
     711              : {
     712              :         lsp_document *document;
     713              :         lsp_method_visibility visibility;
     714              :         zend_long body_depth;
     715              :         zend_string *path, *contents, *label;
     716              :         zval tokens_zv, *token, *name_token;
     717              :         HashTable *tokens;
     718              :         uint32_t i, count, name_index;
     719              :         size_t class_start, body_start, body_end, name_start, name_end;
     720              :         bool found, owns_contents;
     721              : 
     722           24 :         path = lsp_find_project_symbol_path(server, LSP_SYMBOL_CLASS, class_name);
     723           24 :         if (!path) {
     724            0 :                 return false;
     725              :         }
     726              : 
     727           24 :         document = lsp_document_for_path(server, path);
     728           24 :         contents = document ? zend_string_copy(document->text) : lsp_read_file(path);
     729           24 :         owns_contents = document || contents != zend_empty_string;
     730           24 :         if (!owns_contents) {
     731            0 :                 zend_string_release(path);
     732              : 
     733            0 :                 return false;
     734              :         }
     735              : 
     736           24 :         body_depth = 0;
     737           24 :         class_start = 0;
     738           24 :         body_start = 0;
     739           24 :         body_end = 0;
     740           24 :         found = false;
     741           24 :         ZVAL_UNDEF(&tokens_zv);
     742           24 :         lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
     743              : 
     744           24 :         if (Z_TYPE(tokens_zv) == IS_ARRAY && lsp_find_class_header_for_name(contents, class_name, &class_start, &body_start, &body_end, &body_depth)) {
     745           24 :                 tokens = Z_ARRVAL(tokens_zv);
     746           24 :                 count = zend_hash_num_elements(tokens);
     747          624 :                 for (i = 0; i < count; i++) {
     748          612 :                         token = zend_hash_index_find(tokens, i);
     749          612 :                         if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_in_bounds(token, body_start, body_end)) {
     750          372 :                                 continue;
     751              :                         }
     752              : 
     753          240 :                         if (!lsp_token_name_equals(token, "T_FUNCTION") || !lsp_token_at_depth(contents, token, body_depth)) {
     754          216 :                                 continue;
     755              :                         }
     756              : 
     757           24 :                         visibility = lsp_definition_member_visibility(tokens, i, contents, body_depth);
     758           24 :                         if (visibility == LSP_METHOD_VISIBILITY_PRIVATE) {
     759            0 :                                 continue;
     760              :                         }
     761              : 
     762           24 :                         name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
     763           24 :                         label = lsp_token_string(name_token, "text");
     764           24 :                         if (!label || !lsp_definition_string_equals_ci(label, method_name)) {
     765           12 :                                 continue;
     766              :                         }
     767              : 
     768           12 :                         name_start = (size_t) lsp_token_long(name_token, "offset", lsp_token_long(token, "offset", 0));
     769           12 :                         name_end = name_start + ZSTR_LEN(label);
     770           12 :                         lsp_definition_location_from_offsets(path, contents, name_start, name_end, location);
     771           12 :                         found = true;
     772           12 :                         break;
     773              :                 }
     774              :         }
     775              : 
     776           24 :         if (!Z_ISUNDEF(tokens_zv)) {
     777           24 :                 zval_ptr_dtor(&tokens_zv);
     778              :         }
     779              : 
     780           24 :         if (owns_contents) {
     781           24 :                 zend_string_release(contents);
     782              :         }
     783              : 
     784           24 :         zend_string_release(path);
     785              : 
     786           24 :         return found;
     787              : }
     788              : 
     789            8 : static inline uint32_t lsp_add_project_override_locations(lsp_server *server, zval *locations, zend_string *base_class, zend_string *method_name)
     790              : {
     791              :         lsp_symbol_index_header *header;
     792              :         zend_string *candidate;
     793              :         zval location;
     794              :         uint32_t i, count;
     795              :         size_t fqcn_length, path_length;
     796              :         char *cursor, *end, kind, *fqcn, *path;
     797              : 
     798            8 :         count = 0;
     799            8 :         lsp_index_join_worker(server);
     800              : 
     801            8 :         if (!server->symbol_index.available || !server->symbol_index.addr) {
     802            0 :                 return 0;
     803              :         }
     804              : 
     805            8 :         header = (lsp_symbol_index_header *) server->symbol_index.addr;
     806            8 :         if (header->magic != LSP_SYMBOL_INDEX_MAGIC || header->used > header->capacity) {
     807            0 :                 return 0;
     808              :         }
     809              : 
     810            8 :         cursor = ((char *) server->symbol_index.addr) + sizeof(lsp_symbol_index_header);
     811            8 :         end = ((char *) server->symbol_index.addr) + header->used;
     812           48 :         for (i = 0; i < header->symbol_count && cursor < end; i++) {
     813           40 :                 kind = *cursor++;
     814           40 :                 fqcn = cursor;
     815           40 :                 fqcn_length = strlen(fqcn);
     816           40 :                 path = fqcn + fqcn_length + 1;
     817              : 
     818           40 :                 if (path >= end) {
     819            0 :                         break;
     820              :                 }
     821              : 
     822           40 :                 path_length = strlen(path);
     823           40 :                 cursor = path + path_length + 1;
     824           40 :                 if (cursor > end || kind != LSP_SYMBOL_CLASS || lsp_path_value_contains_analysis_helper(path, path_length)) {
     825            0 :                         continue;
     826              :                 }
     827              : 
     828           40 :                 candidate = zend_string_init(fqcn, fqcn_length, 0);
     829           40 :                 if (lsp_definition_string_equals_ci(candidate, base_class)) {
     830            8 :                         zend_string_release(candidate);
     831            8 :                         continue;
     832              :                 }
     833              : 
     834           32 :                 if (!lsp_project_class_extends_class(server, candidate, base_class)) {
     835            8 :                         zend_string_release(candidate);
     836            8 :                         continue;
     837              :                 }
     838              : 
     839           24 :                 ZVAL_UNDEF(&location);
     840           24 :                 if (lsp_project_declared_method_location(server, candidate, method_name, &location)) {
     841           12 :                         add_next_index_zval(locations, &location);
     842           12 :                         count++;
     843           12 :                 } else if (!Z_ISUNDEF(location)) {
     844            0 :                         zval_ptr_dtor(&location);
     845              :                 }
     846              : 
     847           24 :                 zend_string_release(candidate);
     848              :         }
     849              : 
     850            8 :         return count;
     851              : }
     852              : 
     853            8 : static inline void lsp_add_override_code_lens(lsp_server *server, zval *items, lsp_document *document, zend_string *class_name, zend_string *method_name, size_t name_start, size_t name_end)
     854              : {
     855              :         zend_string *title;
     856              :         zval lens, range, command, arguments, position, locations;
     857              :         uint32_t count;
     858              : 
     859            8 :         array_init(&locations);
     860            8 :         count = lsp_add_project_override_locations(server, &locations, class_name, method_name);
     861            8 :         if (count == 0) {
     862            4 :                 zval_ptr_dtor(&locations);
     863              : 
     864            4 :                 return;
     865              :         }
     866              : 
     867            4 :         title = count == 1
     868            0 :                 ? zend_string_init("1 override", sizeof("1 override") - 1, 0)
     869            4 :                 : strpprintf(0, "%u overrides", count)
     870              :         ;
     871              : 
     872            4 :         array_init(&lens);
     873            4 :         lsp_range_from_offsets(document->text, name_start, name_end, &range);
     874            4 :         add_assoc_zval(&lens, "range", &range);
     875              : 
     876            4 :         array_init(&command);
     877            4 :         add_assoc_str(&command, "title", title);
     878            4 :         add_assoc_string(&command, "command", "lsparrot.showReferences");
     879              : 
     880            4 :         array_init(&arguments);
     881            4 :         add_next_index_str(&arguments, zend_string_copy(document->uri));
     882            4 :         lsp_definition_position_from_offset(document->text, name_start, &position);
     883            4 :         add_next_index_zval(&arguments, &position);
     884            4 :         add_next_index_zval(&arguments, &locations);
     885            4 :         add_assoc_zval(&command, "arguments", &arguments);
     886            4 :         add_assoc_zval(&lens, "command", &command);
     887            4 :         add_next_index_zval(items, &lens);
     888              : }
     889              : 
     890           48 : static inline zend_string *lsp_this_member_access_class_name(lsp_document *document, size_t offset, zend_string *prefix)
     891              : {
     892           48 :         zend_long body_depth = 0;
     893              :         zend_string *receiver, *member_prefix, *class_name;
     894           48 :         size_t class_start = 0, body_start = 0, body_end = 0;
     895              : 
     896           48 :         if (!lsp_member_access_context(document->text, offset, prefix, &receiver, &member_prefix)) {
     897           40 :                 return NULL;
     898              :         }
     899              : 
     900            8 :         if (!zend_string_equals_literal(receiver, "$this")) {
     901            0 :                 zend_string_release(receiver);
     902            0 :                 zend_string_release(member_prefix);
     903              : 
     904            0 :                 return NULL;
     905              :         }
     906              : 
     907            8 :         zend_string_release(receiver);
     908            8 :         zend_string_release(member_prefix);
     909            8 :         if (!lsp_find_enclosing_class_header(document->text, offset, &class_start, &body_start, &body_end, &body_depth)) {
     910            0 :                 return NULL;
     911              :         }
     912              : 
     913            8 :         class_name = lsp_class_declared_name(document->text, class_start, body_start);
     914              : 
     915            8 :         return class_name;
     916              : }
     917              : 
     918           68 : static inline bool lsp_project_member_definition(lsp_server *server, lsp_document *document, size_t offset, zend_string *prefix, zend_string *member_name, zval *return_value)
     919              : {
     920              :         zend_string *member_prefix, *class_name;
     921              :         bool found;
     922              : 
     923           68 :         if (!lsp_member_access_class_context(server, document, offset, prefix, &class_name, &member_prefix)) {
     924           48 :                 class_name = lsp_this_member_access_class_name(document, offset, prefix);
     925           48 :                 if (!class_name) {
     926           40 :                         return false;
     927              :                 }
     928              :         } else {
     929           20 :                 zend_string_release(member_prefix);
     930              :         }
     931              : 
     932           28 :         found = lsp_project_method_definition_for_class(server, class_name, member_name, return_value, 0);
     933           28 :         zend_string_release(class_name);
     934              : 
     935           28 :         return found;
     936              : }
     937              : 
     938           64 : extern zend_string *lsp_use_statement_class_name_at(zend_string *text, size_t offset)
     939              : {
     940           64 :         const char *value = ZSTR_VAL(text), *statement_start, *statement_end, *p, *name_start, *name_end, *alias_keyword;
     941           64 :         size_t start, end, length = ZSTR_LEN(text);
     942              : 
     943           64 :         if (offset > length) {
     944            0 :                 offset = length;
     945              :         }
     946              : 
     947           64 :         start = offset;
     948         1640 :         while (start > 0 && value[start - 1] != '\n' && value[start - 1] != ';' && value[start - 1] != '{' && value[start - 1] != '}') {
     949         1576 :                 start--;
     950              :         }
     951              : 
     952           64 :         end = offset;
     953          700 :         while (end < length && value[end] != '\n' && value[end] != ';' && value[end] != '{' && value[end] != '}') {
     954          636 :                 end++;
     955              :         }
     956              : 
     957           64 :         statement_start = value + start;
     958           64 :         statement_end = value + end;
     959           64 :         p = statement_start;
     960              : 
     961          356 :         while (p < statement_end && isspace((unsigned char) *p)) {
     962          292 :                 p++;
     963              :         }
     964              : 
     965           64 :         if ((size_t) (statement_end - p) < sizeof("use") - 1 || memcmp(p, "use", sizeof("use") - 1) != 0) {
     966           60 :                 return NULL;
     967              :         }
     968              : 
     969            4 :         p += sizeof("use") - 1;
     970            4 :         if (p < statement_end && !isspace((unsigned char) *p)) {
     971            0 :                 return NULL;
     972              :         }
     973              : 
     974            8 :         while (p < statement_end && isspace((unsigned char) *p)) {
     975            4 :                 p++;
     976              :         }
     977              : 
     978            4 :         if ((p + sizeof("function") - 1 < statement_end && strncasecmp(p, "function", sizeof("function") - 1) == 0) ||
     979            4 :                 (p + sizeof("const") - 1 < statement_end && strncasecmp(p, "const", sizeof("const") - 1) == 0)
     980              :         ) {
     981            0 :                 return NULL;
     982              :         }
     983              : 
     984            4 :         if (p < statement_end && *p == '\\') {
     985            0 :                 p++;
     986              :         }
     987              : 
     988            4 :         name_start = p;
     989            4 :         if (value + offset < name_start || value + offset > statement_end) {
     990            0 :                 return NULL;
     991              :         }
     992              : 
     993            4 :         alias_keyword = NULL;
     994          124 :         for (p = name_start; p + sizeof(" as ") - 1 <= statement_end; p++) {
     995          120 :                 if (strncasecmp(p, " as ", sizeof(" as ") - 1) == 0) {
     996            0 :                         alias_keyword = p;
     997            0 :                         break;
     998              :                 }
     999              :         }
    1000              : 
    1001            4 :         name_end = alias_keyword ? alias_keyword : statement_end;
    1002            4 :         while (name_end > name_start && isspace((unsigned char) name_end[-1])) {
    1003            0 :                 name_end--;
    1004              :         }
    1005              : 
    1006            4 :         if (name_end <= name_start) {
    1007            0 :                 return NULL;
    1008              :         }
    1009              : 
    1010            4 :         if (memchr(name_start, '{', name_end - name_start) || memchr(name_start, ',', name_end - name_start)) {
    1011            0 :                 return NULL;
    1012              :         }
    1013              : 
    1014            4 :         return zend_string_init(name_start, name_end - name_start, 0);
    1015              : }
    1016              : 
    1017           28 : static inline bool lsp_project_class_definition(lsp_server *server, zend_string *class_name, zval *return_value)
    1018              : {
    1019              :         const char *class_label;
    1020              :         zend_string *path, *contents, *label, *uri;
    1021              :         zval tokens_zv, *token, *name_token, range, start, end_range;
    1022              :         HashTable *tokens;
    1023           28 :         zend_long target_depth = 0;
    1024              :         uint32_t i, count, name_index;
    1025           28 :         size_t class_label_length, name_offset, target_class_start = 0, target_body_start = 0, target_body_end = 0;
    1026           28 :         bool located = false, have_target;
    1027              : 
    1028           28 :         path = lsp_find_project_symbol_path(server, LSP_SYMBOL_CLASS, class_name);
    1029           28 :         if (!path) {
    1030            0 :                 return false;
    1031              :         }
    1032              : 
    1033           28 :         contents = lsp_read_file(path);
    1034           28 :         if (contents != zend_empty_string) {
    1035           28 :                 class_label = lsp_basename_from_fqcn(ZSTR_VAL(class_name), ZSTR_LEN(class_name), &class_label_length);
    1036           28 :                 ZVAL_UNDEF(&tokens_zv);
    1037           28 :                 lsp_lsparrot_tokens_to_zval(&tokens_zv, contents);
    1038              : 
    1039              :                 /* Several classes in the file can share the short name across
    1040              :                  * namespaces; anchor on the header whose resolved FQCN matches. */
    1041           28 :                 have_target = lsp_find_class_header_for_name(contents, class_name, &target_class_start, &target_body_start, &target_body_end, &target_depth);
    1042              : 
    1043           28 :                 if (Z_TYPE(tokens_zv) == IS_ARRAY) {
    1044           28 :                         tokens = Z_ARRVAL(tokens_zv);
    1045           28 :                         count = zend_hash_num_elements(tokens);
    1046          376 :                         for (i = 0; i < count; i++) {
    1047          376 :                                 token = zend_hash_index_find(tokens, i);
    1048          376 :                                 if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_is_class_like(token)) {
    1049          344 :                                         continue;
    1050              :                                 }
    1051              : 
    1052           32 :                                 name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
    1053           32 :                                 label = lsp_token_string(name_token, "text");
    1054           32 :                                 if (!label || ZSTR_LEN(label) != class_label_length || strncasecmp(ZSTR_VAL(label), class_label, class_label_length) != 0) {
    1055            0 :                                         continue;
    1056              :                                 }
    1057              : 
    1058              :                                 /* Select the class NAME itself so editor previews (e.g.
    1059              :                                  * cmd-hover in VSCode) show the declaration instead of a
    1060              :                                  * single character at column zero. */
    1061           32 :                                 name_offset = (size_t) lsp_token_long(name_token, "offset", 0);
    1062           32 :                                 if (have_target && (name_offset < target_class_start || name_offset >= target_body_start)) {
    1063            4 :                                         continue;
    1064              :                                 }
    1065           28 :                                 lsp_definition_location_from_offsets(path, contents, name_offset, name_offset + ZSTR_LEN(label), return_value);
    1066           28 :                                 located = true;
    1067           28 :                                 break;
    1068              :                         }
    1069              : 
    1070           28 :                         zval_ptr_dtor(&tokens_zv);
    1071            0 :                 } else if (!Z_ISUNDEF(tokens_zv)) {
    1072            0 :                         zval_ptr_dtor(&tokens_zv);
    1073              :                 }
    1074              : 
    1075           28 :                 zend_string_release(contents);
    1076              :         }
    1077              : 
    1078           28 :         if (!located) {
    1079            0 :                 uri = lsp_uri_from_path(path);
    1080            0 :                 array_init(return_value);
    1081            0 :                 add_assoc_str(return_value, "uri", uri);
    1082            0 :                 array_init(&start);
    1083            0 :                 add_assoc_long(&start, "line", 0);
    1084            0 :                 add_assoc_long(&start, "character", 0);
    1085            0 :                 array_init(&end_range);
    1086            0 :                 add_assoc_long(&end_range, "line", 0);
    1087            0 :                 add_assoc_long(&end_range, "character", 1);
    1088            0 :                 array_init(&range);
    1089            0 :                 add_assoc_zval(&range, "start", &start);
    1090            0 :                 add_assoc_zval(&range, "end", &end_range);
    1091            0 :                 add_assoc_zval(return_value, "range", &range);
    1092              :         }
    1093              : 
    1094           28 :         zend_string_release(path);
    1095              : 
    1096           28 :         return true;
    1097              : }
    1098            0 : extern bool lsp_find_symbol(zval *lsparrot, zend_string *word, zval **matched_token, zend_string **matched_label, zend_string **matched_detail)
    1099              : {
    1100            0 :         const char *word_value = ZSTR_VAL(word);
    1101              :         zend_string *label, *detail;
    1102              :         zval *tokens_zv, *token;
    1103              :         HashTable *tokens;
    1104              :         uint32_t i, count;
    1105            0 :         size_t word_length = ZSTR_LEN(word);
    1106              : 
    1107            0 :         if (word_length > 0 && word_value[0] == '$') {
    1108            0 :                 word_value++;
    1109            0 :                 word_length--;
    1110              :         }
    1111              : 
    1112            0 :         word_value = lsp_skip_global_namespace_prefix(word_value, &word_length);
    1113              : 
    1114            0 :         tokens_zv = zend_hash_str_find(Z_ARRVAL_P(lsparrot), "tokens", sizeof("tokens") - 1);
    1115            0 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
    1116            0 :                 return false;
    1117              :         }
    1118              : 
    1119            0 :         tokens = Z_ARRVAL_P(tokens_zv);
    1120            0 :         count = zend_hash_num_elements(tokens);
    1121            0 :         for (i = 0; i < count; i++) {
    1122            0 :                 token = zend_hash_index_find(tokens, i);
    1123            0 :                 label = NULL;
    1124            0 :                 detail = NULL;
    1125              : 
    1126            0 :                 if (!token || Z_TYPE_P(token) != IS_ARRAY) {
    1127            0 :                         continue;
    1128              :                 }
    1129              : 
    1130            0 :                 if (lsp_token_name_equals(token, "T_FUNCTION")) {
    1131            0 :                         label = lsp_next_string_token(tokens, i + 1);
    1132            0 :                         if (label) {
    1133            0 :                                 detail = strpprintf(0, "function %s", ZSTR_VAL(label));
    1134              :                         }
    1135            0 :                 } else if (lsp_token_is_class_like(token)) {
    1136            0 :                         label = lsp_next_string_token(tokens, i + 1);
    1137            0 :                         if (label) {
    1138            0 :                                 detail = strpprintf(0, "%s %s", lsp_token_name_equals(token, "T_INTERFACE") ? "interface" : (lsp_token_name_equals(token, "T_TRAIT") ? "trait" : (lsp_token_name_equals(token, "T_ENUM") ? "enum" : "class")), ZSTR_VAL(label));
    1139              :                         }
    1140            0 :                 } else if (lsp_token_name_equals(token, "T_VARIABLE")) {
    1141            0 :                         label = lsp_token_string(token, "text");
    1142            0 :                         if (label) {
    1143            0 :                                 detail = strpprintf(0, "variable %s", ZSTR_VAL(label));
    1144              :                         }
    1145              :                 }
    1146              : 
    1147            0 :                 if (!label || !detail) {
    1148            0 :                         if (detail) {
    1149            0 :                                 zend_string_release(detail);
    1150              :                         }
    1151              : 
    1152            0 :                         continue;
    1153              :                 }
    1154              : 
    1155            0 :                 if ((ZSTR_LEN(label) == word_length && strncasecmp(ZSTR_VAL(label), word_value, word_length) == 0) ||
    1156            0 :                         (ZSTR_LEN(label) > 0 && ZSTR_VAL(label)[0] == '$' && ZSTR_LEN(label) - 1 == word_length && strncasecmp(ZSTR_VAL(label) + 1, word_value, word_length) == 0)
    1157              :                 ) {
    1158            0 :                         if (matched_token) {
    1159            0 :                                 *matched_token = token;
    1160              :                         }
    1161              : 
    1162            0 :                         if (matched_label) {
    1163            0 :                                 *matched_label = label;
    1164              :                         }
    1165              : 
    1166            0 :                         if (matched_detail) {
    1167            0 :                                 *matched_detail = detail;
    1168              :                         } else {
    1169            0 :                                 zend_string_release(detail);
    1170              :                         }
    1171              : 
    1172            0 :                         return true;
    1173              :                 }
    1174              : 
    1175            0 :                 zend_string_release(detail);
    1176              :         }
    1177              : 
    1178            0 :         return false;
    1179              : }
    1180              : 
    1181            4 : extern void lsp_lsparrot_code_lens(lsp_server *server, zval *return_value, lsp_document *document)
    1182              : {
    1183              :         lsp_method_visibility visibility;
    1184              :         zend_long body_depth;
    1185              :         zend_string *class_name, *label;
    1186              :         zval *tokens_zv, *token, *name_token;
    1187              :         HashTable *tokens;
    1188              :         uint32_t i, count, name_index;
    1189              :         size_t class_start, body_start, body_end, search_start, name_start, name_end;
    1190              : 
    1191            4 :         array_init(return_value);
    1192              : 
    1193            4 :         if (Z_TYPE(document->lsparrot) != IS_ARRAY) {
    1194            0 :                 return;
    1195              :         }
    1196              : 
    1197            4 :         tokens_zv = zend_hash_str_find(Z_ARRVAL(document->lsparrot), "tokens", sizeof("tokens") - 1);
    1198            4 :         if (!tokens_zv || Z_TYPE_P(tokens_zv) != IS_ARRAY) {
    1199            0 :                 return;
    1200              :         }
    1201              : 
    1202            4 :         tokens = Z_ARRVAL_P(tokens_zv);
    1203            4 :         count = zend_hash_num_elements(tokens);
    1204            4 :         search_start = 0;
    1205            8 :         while (lsp_find_class_header_from(document->text, search_start, &class_start, &body_start, &body_end, &body_depth)) {
    1206            4 :                 class_name = lsp_class_declared_name(document->text, class_start, body_start);
    1207            4 :                 if (!class_name) {
    1208            0 :                         search_start = body_end + 1;
    1209            0 :                         continue;
    1210              :                 }
    1211              : 
    1212          244 :                 for (i = 0; i < count; i++) {
    1213          240 :                         token = zend_hash_index_find(tokens, i);
    1214          240 :                         if (!token || Z_TYPE_P(token) != IS_ARRAY || !lsp_token_in_bounds(token, body_start, body_end)) {
    1215           48 :                                 continue;
    1216              :                         }
    1217              : 
    1218          192 :                         if (!lsp_token_name_equals(token, "T_FUNCTION") || !lsp_token_at_depth(document->text, token, body_depth)) {
    1219          180 :                                 continue;
    1220              :                         }
    1221              : 
    1222           12 :                         visibility = lsp_definition_member_visibility(tokens, i, document->text, body_depth);
    1223           12 :                         if (visibility == LSP_METHOD_VISIBILITY_PRIVATE || lsp_definition_member_is_final(tokens, i, document->text, body_depth)) {
    1224            4 :                                 continue;
    1225              :                         }
    1226              : 
    1227            8 :                         name_token = lsp_next_function_name_token_ex(tokens, i + 1, &name_index);
    1228            8 :                         label = lsp_token_string(name_token, "text");
    1229            8 :                         if (!label || zend_string_equals_literal(label, "__construct")) {
    1230            0 :                                 continue;
    1231              :                         }
    1232              : 
    1233            8 :                         name_start = (size_t) lsp_token_long(name_token, "offset", lsp_token_long(token, "offset", 0));
    1234            8 :                         name_end = name_start + ZSTR_LEN(label);
    1235            8 :                         lsp_add_override_code_lens(server, return_value, document, class_name, label, name_start, name_end);
    1236              :                 }
    1237              : 
    1238            4 :                 zend_string_release(class_name);
    1239            4 :                 search_start = body_end + 1;
    1240              :         }
    1241              : }
    1242              : 
    1243              : /* textDocument/typeDefinition: for a variable, jump to the class of its
    1244              :  * inferred type; for anything else fall back to the class the word resolves
    1245              :  * to (which for class names matches plain definition). */
    1246            4 : extern void lsp_lsparrot_type_definition(lsp_server *server, zval *return_value, lsp_document *document, zval *position)
    1247              : {
    1248              :         zend_long line, character;
    1249              :         zend_string *word, *class_name;
    1250              :         size_t offset;
    1251              : 
    1252            4 :         lsp_position_from_zval(position, &line, &character);
    1253            4 :         offset = lsp_offset_at(document->text, line, character);
    1254            4 :         word = lsp_word_at(document->text, offset);
    1255              : 
    1256            4 :         if (ZSTR_LEN(word) == 0) {
    1257            0 :                 zend_string_release(word);
    1258            0 :                 ZVAL_NULL(return_value);
    1259              : 
    1260            0 :                 return;
    1261              :         }
    1262              : 
    1263            4 :         if (ZSTR_VAL(word)[0] == '$') {
    1264            4 :                 class_name = lsp_infer_receiver_class(server, document, word, offset);
    1265              :         } else {
    1266            0 :                 class_name = lsp_resolve_class_name_at(document->text, word, offset);
    1267              :         }
    1268              : 
    1269            4 :         zend_string_release(word);
    1270              : 
    1271            4 :         if (!class_name) {
    1272            0 :                 ZVAL_NULL(return_value);
    1273              : 
    1274            0 :                 return;
    1275              :         }
    1276              : 
    1277            4 :         lsp_index_join_worker(server);
    1278            4 :         if (!lsp_project_class_definition(server, class_name, return_value)) {
    1279            0 :                 ZVAL_NULL(return_value);
    1280              :         }
    1281              : 
    1282            4 :         zend_string_release(class_name);
    1283              : }
    1284              : 
    1285           68 : extern void lsp_lsparrot_definition(lsp_server *server, zval *return_value, lsp_document *document, zval *position)
    1286              : {
    1287              :         zend_long line, character, target_line;
    1288              :         zend_string *word, *detail, *prefix, *class_name;
    1289              :         zval *token, range, start, end;
    1290              :         size_t offset;
    1291              : 
    1292           68 :         lsp_position_from_zval(position, &line, &character);
    1293           68 :         offset = lsp_offset_at(document->text, line, character);
    1294           68 :         word = lsp_word_at(document->text, offset);
    1295           68 :         prefix = lsp_prefix_at(document->text, offset);
    1296              : 
    1297           68 :         if (ZSTR_LEN(word) > 0 && lsp_project_member_definition(server, document, offset, prefix, word, return_value)) {
    1298           28 :                 zend_string_release(prefix);
    1299           28 :                 zend_string_release(word);
    1300              : 
    1301           68 :                 return;
    1302              :         }
    1303              : 
    1304           40 :         if (ZSTR_LEN(word) > 0 && lsp_project_static_member_definition(server, document, offset, word, return_value)) {
    1305           16 :                 zend_string_release(prefix);
    1306           16 :                 zend_string_release(word);
    1307              : 
    1308           16 :                 return;
    1309              :         }
    1310              : 
    1311           24 :         zend_string_release(prefix);
    1312              : 
    1313           24 :         class_name = lsp_use_statement_class_name_at(document->text, offset);
    1314           24 :         if (!class_name && ZSTR_LEN(word) > 0) {
    1315           20 :                 class_name = lsp_resolve_class_name_at(document->text, word, offset);
    1316              :         }
    1317              : 
    1318           24 :         if (class_name) {
    1319           24 :                 if (lsp_project_class_definition(server, class_name, return_value)) {
    1320           24 :                         zend_string_release(class_name);
    1321           24 :                         zend_string_release(word);
    1322              : 
    1323           24 :                         return;
    1324              :                 }
    1325              : 
    1326            0 :                 zend_string_release(class_name);
    1327              :         }
    1328              : 
    1329            0 :         if (ZSTR_LEN(word) == 0 || !lsp_find_symbol(&document->lsparrot, word, &token, NULL, &detail)) {
    1330            0 :                 zend_string_release(word);
    1331            0 :                 ZVAL_NULL(return_value);
    1332              : 
    1333            0 :                 return;
    1334              :         }
    1335              : 
    1336            0 :         target_line = lsp_token_long(token, "line", 1) - 1;
    1337            0 :         if (target_line < 0) {
    1338            0 :                 target_line = 0;
    1339              :         }
    1340              : 
    1341            0 :         array_init(return_value);
    1342            0 :         add_assoc_str(return_value, "uri", zend_string_copy(document->uri));
    1343            0 :         array_init(&start);
    1344            0 :         add_assoc_long(&start, "line", target_line);
    1345            0 :         add_assoc_long(&start, "character", 0);
    1346            0 :         array_init(&end);
    1347            0 :         add_assoc_long(&end, "line", target_line);
    1348            0 :         add_assoc_long(&end, "character", 1);
    1349            0 :         array_init(&range);
    1350            0 :         add_assoc_zval(&range, "start", &start);
    1351            0 :         add_assoc_zval(&range, "end", &end);
    1352            0 :         add_assoc_zval(return_value, "range", &range);
    1353            0 :         zend_string_release(detail);
    1354            0 :         zend_string_release(word);
    1355              : }
        

Generated by: LCOV version 2.0-1