@@ -1789,9 +1789,25 @@ static uint32_t active_lexical_scope_id(const WalkState *state) {
17891789static uint32_t lexical_ancestor_of_kind (const WalkState * state , uint32_t start_id ,
17901790 bool want_function , bool want_block );
17911791
1792- static bool python_default_value_reference (TSNode node ) {
1793- for (TSNode parent = ts_node_parent (node ); !ts_node_is_null (parent );
1794- parent = ts_node_parent (parent )) {
1792+ /* One step up: on the occurrence cursor when there is one, else with
1793+ * ts_node_parent, which descends from the ROOT, so a climb costs O(depth^2). */
1794+ static TSNode usage_parent_step (TSTreeCursor * cursor , TSNode current ) {
1795+ if (cursor ) {
1796+ return ts_tree_cursor_goto_parent (cursor ) ? ts_tree_cursor_current_node (cursor )
1797+ : (TSNode ){0 };
1798+ }
1799+ usage_slow_parent_fallback_test_note ();
1800+ return ts_node_parent (current );
1801+ }
1802+
1803+ /* Asked for every Python usage, and a deep expression puts O(depth) parents
1804+ * above each one. A tree without errors climbs them on the walk cursor, like
1805+ * python_scope_directive_kind; a tree with errors keeps ts_node_parent. */
1806+ static bool python_default_value_reference (CBMExtractCtx * ctx , WalkState * state , TSNode node ) {
1807+ TSTreeCursor * cursor =
1808+ ts_node_has_error (ctx -> root ) ? NULL : reset_occurrence_cursor (state , node );
1809+ for (TSNode parent = usage_parent_step (cursor , node ); !ts_node_is_null (parent );
1810+ parent = usage_parent_step (cursor , parent )) {
17951811 const char * kind = ts_node_type (parent );
17961812 /* A nested executable scope inside the default owns its own lookups;
17971813 * only a direct default expression is evaluated in the declaring
@@ -1812,10 +1828,10 @@ static bool python_default_value_reference(TSNode node) {
18121828 return false;
18131829}
18141830
1815- static uint32_t usage_lexical_scope_id_for_node (CBMExtractCtx * ctx , const WalkState * state ,
1816- TSNode node ) {
1831+ static uint32_t usage_lexical_scope_id_for_node (CBMExtractCtx * ctx , WalkState * state , TSNode node ) {
18171832 uint32_t scope_id = active_lexical_scope_id (state );
1818- if (!ctx || ctx -> language != CBM_LANG_PYTHON || !python_default_value_reference (node )) {
1833+ if (!ctx || ctx -> language != CBM_LANG_PYTHON ||
1834+ !python_default_value_reference (ctx , state , node )) {
18191835 return scope_id ;
18201836 }
18211837 uint32_t function_id = lexical_ancestor_of_kind (state , scope_id , true, false);
@@ -1891,15 +1907,43 @@ static bool ensure_lexical_binding_capacity(WalkState *state) {
18911907}
18921908
18931909static bool lexical_ancestor_kind (TSNode node , const char * kind ) {
1894- for (TSNode parent = ts_node_parent ( node ); !ts_node_is_null (parent );
1895- parent = ts_node_parent ( parent )) {
1910+ for (TSNode parent = usage_parent_step ( NULL , node ); !ts_node_is_null (parent );
1911+ parent = usage_parent_step ( NULL , parent )) {
18961912 if (strcmp (ts_node_type (parent ), kind ) == 0 ) {
18971913 return true;
18981914 }
18991915 }
19001916 return false;
19011917}
19021918
1919+ /* Is `node` a name listed by a Python global/nonlocal statement? The grammar
1920+ * makes those names direct children of the statement, so in a tree without
1921+ * errors one parent step on the walk cursor answers it. The full climb above
1922+ * steps with ts_node_parent, which descends from the ROOT each time: O(depth^2)
1923+ * per identifier, asked for every Python identifier, so one deep arithmetic
1924+ * expression went cubic. Error recovery can put a name under an ERROR child of
1925+ * the statement, so a tree with errors keeps the full climb. */
1926+ static CBMPythonDirectiveKind python_scope_directive_kind (CBMExtractCtx * ctx , WalkState * state ,
1927+ TSNode node ) {
1928+ if (ts_node_has_error (ctx -> root )) {
1929+ if (lexical_ancestor_kind (node , "global_statement" )) {
1930+ return CBM_PYTHON_DIRECTIVE_GLOBAL ;
1931+ }
1932+ return lexical_ancestor_kind (node , "nonlocal_statement" ) ? CBM_PYTHON_DIRECTIVE_NONLOCAL
1933+ : 0 ;
1934+ }
1935+ TSNode parent ;
1936+ const char * field ;
1937+ if (!occurrence_parent (reset_occurrence_cursor (state , node ), node , & parent , & field )) {
1938+ return 0 ;
1939+ }
1940+ const char * kind = ts_node_type (parent );
1941+ if (strcmp (kind , "global_statement" ) == 0 ) {
1942+ return CBM_PYTHON_DIRECTIVE_GLOBAL ;
1943+ }
1944+ return strcmp (kind , "nonlocal_statement" ) == 0 ? CBM_PYTHON_DIRECTIVE_NONLOCAL : 0 ;
1945+ }
1946+
19031947static TSNode declared_function_name_owner (TSNode node , const CBMLangSpec * spec ) {
19041948 for (TSNode parent = ts_node_parent (node ); !ts_node_is_null (parent );
19051949 parent = ts_node_parent (parent )) {
@@ -2246,13 +2290,11 @@ static void record_lexical_binding(CBMExtractCtx *ctx, WalkState *state, TSNode
22462290 TSNode function_declaration = declared_function_name_owner (node , spec );
22472291 TSNode class_declaration = declared_class_name_owner (node , spec );
22482292
2249- if (ctx -> language == CBM_LANG_PYTHON && (lexical_ancestor_kind (node , "global_statement" ) ||
2250- lexical_ancestor_kind (node , "nonlocal_statement" ))) {
2293+ CBMPythonDirectiveKind scope_directive =
2294+ ctx -> language == CBM_LANG_PYTHON ? python_scope_directive_kind (ctx , state , node ) : 0 ;
2295+ if (scope_directive ) {
22512296 uint32_t function_id = lexical_ancestor_of_kind (state , current_id , true, false);
2252- CBMPythonDirectiveKind directive = lexical_ancestor_kind (node , "global_statement" )
2253- ? CBM_PYTHON_DIRECTIVE_GLOBAL
2254- : CBM_PYTHON_DIRECTIVE_NONLOCAL ;
2255- record_python_directive (state , function_id , name , directive );
2297+ record_python_directive (state , function_id , name , scope_directive );
22562298 return ;
22572299 }
22582300
@@ -2719,8 +2761,7 @@ void handle_usages(CBMExtractCtx *ctx, TSNode node, const CBMLangSpec *spec, Wal
27192761 }
27202762
27212763 bool python_scope_directive =
2722- ctx -> language == CBM_LANG_PYTHON && (lexical_ancestor_kind (node , "global_statement" ) ||
2723- lexical_ancestor_kind (node , "nonlocal_statement" ));
2764+ ctx -> language == CBM_LANG_PYTHON && python_scope_directive_kind (ctx , state , node ) != 0 ;
27242765 if (python_scope_directive || is_binding_occurrence (ctx , node , spec , state )) {
27252766 char * binding_name = reference_name (ctx , node );
27262767 if (binding_name && binding_name [0 ] && !cbm_is_keyword (binding_name , ctx -> language )) {
0 commit comments