2828
2929#include "ts_lsp.h"
3030#include "../../../src/foundation/platform.h"
31- #include "extract_node_stack .h"
31+ #include "../helpers .h"
3232#include <stdatomic.h>
3333#include <stdio.h>
3434#include <stdlib.h>
@@ -4685,21 +4685,61 @@ static void register_file_defs(CBMArena *arena, CBMTypeRegistry *reg, CBMFileRes
46854685 }
46864686}
46874687
4688- // Find the first `return EXPR` inside a body and evaluate EXPR's type. Returns
4689- // UNKNOWN if the body has no return or every return is unresolvable.
4688+ // AST walk stacks for the passes below. The first chunk lives on the C stack, the
4689+ // same 256 slots the old fixed arrays used; a wider pending set grows on the heap
4690+ // through cbm_walk_stack_reserve and is released when the walk ends, so nothing
4691+ // is cut from the per-file result arena. A fixed cap used to stop pushing without
4692+ // a word, which skipped every top-level declaration past the 256th and left the
4693+ // return walk short of returns it had never reached (#2388).
4694+ enum { TS_WALK_STACK_INLINE = 256 };
4695+
4696+ // Push every child of n in cursor order (one O(N) pass). The walk pops the stack,
4697+ // so children are visited last-child first: the order these walks have always
4698+ // used. False means the stack could not grow; cbm_walk_stack_reserve has logged it.
4699+ static bool ts_walk_push_children (TSNode * * stack , int * cap , int * top , TSNode n ,
4700+ const TSNode * inline_buf , const char * walker ) {
4701+ uint32_t count = ts_node_child_count (n );
4702+ if (count == 0 )
4703+ return true;
4704+ if (!cbm_walk_stack_reserve ((void * * )stack , cap , * top + (int )count , sizeof (TSNode ), inline_buf ,
4705+ walker ))
4706+ return false;
4707+ TSTreeCursor cursor = ts_tree_cursor_new (n );
4708+ if (ts_tree_cursor_goto_first_child (& cursor )) {
4709+ uint32_t pushed = 0 ;
4710+ do {
4711+ (* stack )[(* top )++ ] = ts_tree_cursor_current_node (& cursor );
4712+ pushed ++ ;
4713+ } while (pushed < count && ts_tree_cursor_goto_next_sibling (& cursor ));
4714+ }
4715+ ts_tree_cursor_delete (& cursor );
4716+ return true;
4717+ }
4718+
4719+ static bool ts_walk_push (TSNode * * stack , int * cap , int * top , TSNode n , const TSNode * inline_buf ,
4720+ const char * walker ) {
4721+ if (!cbm_walk_stack_reserve ((void * * )stack , cap , * top + 1 , sizeof (TSNode ), inline_buf , walker ))
4722+ return false;
4723+ (* stack )[(* top )++ ] = n ;
4724+ return true;
4725+ }
4726+
4727+ // Find a `return EXPR` inside a body and evaluate EXPR's type. Children are visited
4728+ // last-child first, so of several returns the one latest in the body is tried
4729+ // first; the first one that resolves wins. Returns UNKNOWN if the body has no
4730+ // return, every return is unresolvable, or the walk stack could not grow.
46904731static const CBMType * infer_return_type_from_body (TSLSPContext * ctx , TSNode body ) {
46914732 if (ts_node_is_null (body ))
46924733 return cbm_type_unknown ();
46934734
4694- // Iterative DFS to avoid C-stack blowup on big bodies. The stack grows, so a
4695- // body wider than any fixed cap is still walked in full: a fixed 256 slots
4696- // used to stop pushing without a word, and this function then answered
4697- // "unknown return type" for a body whose return it had never reached.
4698- TSNodeStack stack ;
4699- ts_nstack_init_arena (& stack , ctx -> arena , 256 );
4700- ts_nstack_push (& stack , body );
4701- while (stack .count > 0 ) {
4702- TSNode n = ts_nstack_pop (& stack );
4735+ TSNode inline_stack [TS_WALK_STACK_INLINE ];
4736+ TSNode * stack = inline_stack ;
4737+ int cap = TS_WALK_STACK_INLINE ;
4738+ int top = 0 ;
4739+ const CBMType * found = cbm_type_unknown ();
4740+ stack [top ++ ] = body ;
4741+ while (top > 0 ) {
4742+ TSNode n = stack [-- top ];
47034743 if (ts_node_is_null (n ))
47044744 continue ;
47054745 const char * k = ts_node_type (n );
@@ -4712,18 +4752,22 @@ static const CBMType *infer_return_type_from_body(TSLSPContext *ctx, TSNode body
47124752 if (ts_node_is_null (expr ))
47134753 continue ;
47144754 const CBMType * t = ts_eval_expr_type (ctx , expr );
4715- if (t && !cbm_type_is_unknown (t ))
4716- return t ;
4755+ if (t && !cbm_type_is_unknown (t )) {
4756+ found = t ;
4757+ break ;
4758+ }
47174759 continue ;
47184760 }
47194761 // Don't recurse into nested function bodies — their returns belong to them.
47204762 if (strcmp (k , "function_declaration" ) == 0 || strcmp (k , "function_expression" ) == 0 ||
47214763 strcmp (k , "arrow_function" ) == 0 || strcmp (k , "method_definition" ) == 0 )
47224764 continue ;
47234765
4724- ts_nstack_push_children (& stack , n );
4766+ if (!ts_walk_push_children (& stack , & cap , & top , n , inline_stack , "ts_lsp_return_walk" ))
4767+ break ; // "unknown" is the safe answer for a walk that could not finish
47254768 }
4726- return cbm_type_unknown ();
4769+ cbm_walk_stack_release (stack , inline_stack );
4770+ return found ;
47274771}
47284772
47294773// Rebuild a function's CBMRegisteredFunc signature from the AST directly. extract_defs
@@ -4737,29 +4781,33 @@ static void rebuild_signatures_from_ast(TSLSPContext *ctx, TSNode root, CBMTypeR
47374781 if (ts_node_is_null (root ) || !reg || !ctx -> module_qn )
47384782 return ;
47394783
4740- /* The stack grows. A fixed 256 slots used to be filled by the seeding loop
4741- * alone on a file with that many top-level children, and every declaration
4742- * past it was then skipped without a word — it kept whatever signature
4743- * extract_defs had guessed. */
4744- TSNodeStack stack ;
4745- ts_nstack_init_arena (& stack , ctx -> arena , 256 );
4746- ts_nstack_push_children (& stack , root );
4747-
4748- while (stack .count > 0 ) {
4749- TSNode n = ts_nstack_pop (& stack );
4784+ // Seeded with every top-level child, so a file with more than 256 of them
4785+ // needs the growing stack (see TS_WALK_STACK_INLINE). If it cannot grow, the
4786+ // walk stops and the declarations not yet reached keep extract_defs' signature.
4787+ TSNode inline_stack [TS_WALK_STACK_INLINE ];
4788+ TSNode * stack = inline_stack ;
4789+ int cap = TS_WALK_STACK_INLINE ;
4790+ int top = 0 ;
4791+ bool walking =
4792+ ts_walk_push_children (& stack , & cap , & top , root , inline_stack , "ts_lsp_rebuild_signatures" );
4793+
4794+ while (walking && top > 0 ) {
4795+ TSNode n = stack [-- top ];
47504796 if (ts_node_is_null (n ))
47514797 continue ;
47524798 const char * k = ts_node_type (n );
47534799
47544800 // Recurse into export_statement and class bodies.
47554801 if (strcmp (k , "export_statement" ) == 0 || strcmp (k , "class_body" ) == 0 ) {
4756- ts_nstack_push_children (& stack , n );
4802+ walking = ts_walk_push_children (& stack , & cap , & top , n , inline_stack ,
4803+ "ts_lsp_rebuild_signatures" );
47574804 continue ;
47584805 }
47594806 if (strcmp (k , "class_declaration" ) == 0 ) {
47604807 TSNode body = ts_node_child_by_field_name (n , "body" , TS_LSP_FIELD_LEN ("body" ));
47614808 if (!ts_node_is_null (body ))
4762- ts_nstack_push (& stack , body );
4809+ walking = ts_walk_push (& stack , & cap , & top , body , inline_stack ,
4810+ "ts_lsp_rebuild_signatures" );
47634811 continue ;
47644812 }
47654813
@@ -4889,6 +4937,7 @@ static void rebuild_signatures_from_ast(TSLSPContext *ctx, TSNode root, CBMTypeR
48894937 break ;
48904938 }
48914939 }
4940+ cbm_walk_stack_release (stack , inline_stack );
48924941}
48934942
48944943// Convert NAMED references to TYPE_PARAM for any function whose declaration has a
@@ -4899,29 +4948,33 @@ static void convert_signature_type_params(TSLSPContext *ctx, TSNode root, CBMTyp
48994948 return ;
49004949
49014950 // Walk: function_declaration, class_declaration { method_definition }, plus exported.
4902- /* The stack grows. A fixed 256 slots used to be filled by the seeding loop
4903- * alone on a file with that many top-level children, and every declaration
4904- * past it was then skipped without a word — it kept whatever signature
4905- * extract_defs had guessed. */
4906- TSNodeStack stack ;
4907- ts_nstack_init_arena (& stack , ctx -> arena , 256 );
4908- ts_nstack_push_children (& stack , root );
4909-
4910- while (stack .count > 0 ) {
4911- TSNode n = ts_nstack_pop (& stack );
4951+ // Seeded with every top-level child, so a file with more than 256 of them needs
4952+ // the growing stack (see TS_WALK_STACK_INLINE). If it cannot grow, the walk stops
4953+ // and the generics not yet reached keep their NAMED parameters.
4954+ TSNode inline_stack [TS_WALK_STACK_INLINE ];
4955+ TSNode * stack = inline_stack ;
4956+ int cap = TS_WALK_STACK_INLINE ;
4957+ int top = 0 ;
4958+ bool walking =
4959+ ts_walk_push_children (& stack , & cap , & top , root , inline_stack , "ts_lsp_type_params" );
4960+
4961+ while (walking && top > 0 ) {
4962+ TSNode n = stack [-- top ];
49124963 if (ts_node_is_null (n ))
49134964 continue ;
49144965 const char * k = ts_node_type (n );
49154966
49164967 // Recurse into export_statement and class_body.
49174968 if (strcmp (k , "export_statement" ) == 0 || strcmp (k , "class_body" ) == 0 ) {
4918- ts_nstack_push_children (& stack , n );
4969+ walking =
4970+ ts_walk_push_children (& stack , & cap , & top , n , inline_stack , "ts_lsp_type_params" );
49194971 continue ;
49204972 }
49214973 if (strcmp (k , "class_declaration" ) == 0 ) {
49224974 TSNode body = ts_node_child_by_field_name (n , "body" , TS_LSP_FIELD_LEN ("body" ));
49234975 if (!ts_node_is_null (body ))
4924- ts_nstack_push (& stack , body );
4976+ walking =
4977+ ts_walk_push (& stack , & cap , & top , body , inline_stack , "ts_lsp_type_params" );
49254978 continue ;
49264979 }
49274980
@@ -5020,6 +5073,7 @@ static void convert_signature_type_params(TSLSPContext *ctx, TSNode root, CBMTyp
50205073 break ;
50215074 }
50225075 }
5076+ cbm_walk_stack_release (stack , inline_stack );
50235077}
50245078
50255079// Parse a JSDoc comment block for `@param {T} name` and `@returns {T}` tags. For each
0 commit comments