@@ -427,16 +427,224 @@ static char *extract_wolfram_callee(CBMArena *a, TSNode node, const char *source
427427
428428// Language-specific callee extraction for FP and niche languages.
429429// Swift callee extraction from call/constructor expressions.
430+ /* Swift: the type name a `user_type` spells, without generic arguments or
431+ * module qualification (`Foo<T>` -> Foo, `Mod.Foo` -> Foo). NULL when the
432+ * node names no plain type. */
433+ static char * swift_plain_type_name (CBMArena * a , TSNode type_node , const char * source ) {
434+ if (ts_node_is_null (type_node )) {
435+ return NULL ;
436+ }
437+ if (strcmp (ts_node_type (type_node ), "user_type" ) != 0 ) {
438+ return NULL ;
439+ }
440+ TSNode last = {0 };
441+ uint32_t nc = ts_node_named_child_count (type_node );
442+ for (uint32_t i = 0 ; i < nc ; i ++ ) {
443+ TSNode c = ts_node_named_child (type_node , i );
444+ if (strcmp (ts_node_type (c ), "type_identifier" ) == 0 ) {
445+ last = c ;
446+ }
447+ }
448+ if (ts_node_is_null (last )) {
449+ return NULL ;
450+ }
451+ char * name = cbm_node_text (a , last , source );
452+ return (name && name [0 ]) ? name : NULL ;
453+ }
454+
455+ /* Swift `super.m()`: the class `super` names is the first type the
456+ * enclosing class inherits from. Spelling the call `Base.m` lets the
457+ * receiver-aware resolution bind the base's method; `super.m` bound by name
458+ * alone to whichever same-named override was nearest. */
459+ static char * swift_super_type_name (CBMArena * a , TSNode node , const char * source ) {
460+ for (TSNode p = ts_node_parent (node ); !ts_node_is_null (p ); p = ts_node_parent (p )) {
461+ if (strcmp (ts_node_type (p ), "class_declaration" ) != 0 ) {
462+ continue ;
463+ }
464+ TSNode spec = cbm_find_child_by_kind (p , "inheritance_specifier" );
465+ if (ts_node_is_null (spec )) {
466+ return NULL ;
467+ }
468+ return swift_plain_type_name (
469+ a , ts_node_child_by_field_name (spec , TS_FIELD ("inherits_from" )), source );
470+ }
471+ return NULL ;
472+ }
473+
474+ /* Swift receiver `x as? T` / `(x as? T)` / `x as! T`: the cast names the
475+ * receiver's type. */
476+ static char * swift_cast_type_name (CBMArena * a , TSNode expr , const char * source ) {
477+ while (!ts_node_is_null (expr ) && strcmp (ts_node_type (expr ), "tuple_expression" ) == 0 &&
478+ ts_node_named_child_count (expr ) == 1 ) {
479+ expr = ts_node_named_child (expr , 0 );
480+ }
481+ if (ts_node_is_null (expr ) || strcmp (ts_node_type (expr ), "as_expression" ) != 0 ) {
482+ return NULL ;
483+ }
484+ return swift_plain_type_name (a , ts_node_child_by_field_name (expr , TS_FIELD ("name" )), source );
485+ }
486+
487+ /* Swift local `let x = y as? T` (also `if let` / `guard let`) declared before
488+ * `site` in the same function: the cast type of x, else NULL. Only casts and
489+ * explicit `let x: T` annotations count; nothing is inferred. */
490+ static char * swift_local_cast_type (CBMArena * a , TSNode site , const char * name ,
491+ const char * source ) {
492+ TSNode body = site ;
493+ while (!ts_node_is_null (body ) && strcmp (ts_node_type (body ), "function_body" ) != 0 &&
494+ strcmp (ts_node_type (body ), "lambda_literal" ) != 0 &&
495+ strcmp (ts_node_type (body ), "computed_property" ) != 0 ) {
496+ body = ts_node_parent (body );
497+ }
498+ if (ts_node_is_null (body )) {
499+ return NULL ;
500+ }
501+ uint32_t site_start = ts_node_start_byte (site );
502+ char * found = NULL ;
503+ /* Iterative pre-order walk over the body, bounded to bindings that start
504+ * before the call. */
505+ TSTreeCursor cur = ts_tree_cursor_new (body );
506+ bool more = true;
507+ while (more ) {
508+ TSNode n = ts_tree_cursor_current_node (& cur );
509+ if (ts_node_start_byte (n ) >= site_start ) {
510+ break ;
511+ }
512+ const char * k = ts_node_type (n );
513+ if (strcmp (k , "if_statement" ) == 0 || strcmp (k , "guard_statement" ) == 0 ) {
514+ /* `if let x = y as? T` / `guard let x = y as? T`: the bound name
515+ * and its cast are consecutive children of the statement. */
516+ bool bound_here = false;
517+ uint32_t nc = ts_node_named_child_count (n );
518+ for (uint32_t i = 0 ; i < nc ; i ++ ) {
519+ TSNode c = ts_node_named_child (n , i );
520+ const char * ck = ts_node_type (c );
521+ if (strcmp (ck , "simple_identifier" ) == 0 ) {
522+ char * bn = cbm_node_text (a , c , source );
523+ bound_here = bn && strcmp (bn , name ) == 0 ;
524+ } else if (bound_here && strcmp (ck , "as_expression" ) == 0 ) {
525+ char * t = swift_cast_type_name (a , c , source );
526+ if (t ) {
527+ found = t ;
528+ }
529+ bound_here = false;
530+ } else if (strcmp (ck , "value_binding_pattern" ) != 0 ) {
531+ bound_here = false;
532+ }
533+ }
534+ }
535+ if (strcmp (k , "property_declaration" ) == 0 ) {
536+ /* name: pattern(bound_identifier) [type_annotation] value: expr */
537+ TSNode pat = ts_node_child_by_field_name (n , TS_FIELD ("name" ));
538+ TSNode bound = ts_node_is_null (pat )
539+ ? pat
540+ : ts_node_child_by_field_name (pat , TS_FIELD ("bound_identifier" ));
541+ if (!ts_node_is_null (bound )) {
542+ char * bn = cbm_node_text (a , bound , source );
543+ if (bn && strcmp (bn , name ) == 0 ) {
544+ char * t = swift_cast_type_name (
545+ a , ts_node_child_by_field_name (n , TS_FIELD ("value" )), source );
546+ if (!t ) {
547+ TSNode ann = cbm_find_child_by_kind (n , "type_annotation" );
548+ if (!ts_node_is_null (ann )) {
549+ TSNode tn = ts_node_child_by_field_name (ann , TS_FIELD ("name" ));
550+ if (!ts_node_is_null (tn ) &&
551+ strcmp (ts_node_type (tn ), "optional_type" ) == 0 ) {
552+ tn = ts_node_child_by_field_name (tn , TS_FIELD ("wrapped" ));
553+ }
554+ t = swift_plain_type_name (a , tn , source );
555+ }
556+ }
557+ found = t ; /* the latest binding before the call wins */
558+ }
559+ }
560+ }
561+ if (ts_tree_cursor_goto_first_child (& cur )) {
562+ continue ;
563+ }
564+ while (!ts_tree_cursor_goto_next_sibling (& cur )) {
565+ if (!ts_tree_cursor_goto_parent (& cur )) {
566+ more = false;
567+ break ;
568+ }
569+ }
570+ }
571+ ts_tree_cursor_delete (& cur );
572+ return found ;
573+ }
574+
575+ /* True when a line break separates the receiver from the `.member` suffix:
576+ * the leading-dot modifier style of SwiftUI (`Text("a")\n .padding()`). */
577+ static bool swift_suffix_starts_line (TSNode target , TSNode suffix , const char * source ) {
578+ uint32_t from = ts_node_end_byte (target );
579+ uint32_t to = ts_node_start_byte (suffix );
580+ for (uint32_t i = from ; i < to ; i ++ ) {
581+ if (source [i ] == '\n' ) {
582+ return true;
583+ }
584+ }
585+ return false;
586+ }
587+
588+ /* Swift member callee spelling. The callee text of a navigation call names
589+ * its receiver; where the text alone cannot name the receiver's type, spell
590+ * the type the source does state:
591+ * - `super.m()` -> Base.m (first inherited type)
592+ * - `(x as? T)?.m()` -> T.m
593+ * - `x?.m()` after `let x = y as? T` (or `let x: T`) -> T.m
594+ * - `<view expr>\n .m()` -> View.m (a SwiftUI modifier: every view
595+ * modifier's receiver is a View, and project modifiers are declared in
596+ * `extension View`)
597+ * Everything else keeps the source text. */
598+ static char * swift_navigation_callee (CBMArena * a , TSNode call , TSNode nav , const char * source ) {
599+ TSNode target = ts_node_child_by_field_name (nav , TS_FIELD ("target" ));
600+ TSNode suffix = ts_node_child_by_field_name (nav , TS_FIELD ("suffix" ));
601+ TSNode member = ts_node_is_null (suffix )
602+ ? suffix
603+ : ts_node_child_by_field_name (suffix , TS_FIELD ("suffix" ));
604+ char * mname = ts_node_is_null (member ) ? NULL : cbm_node_text (a , member , source );
605+ if (ts_node_is_null (target ) || !mname || !mname [0 ] ||
606+ strcmp (ts_node_type (member ), "simple_identifier" ) != 0 ) {
607+ return cbm_node_text (a , nav , source );
608+ }
609+ const char * tk = ts_node_type (target );
610+ char * type = NULL ;
611+ if (strcmp (tk , "super_expression" ) == 0 ) {
612+ type = swift_super_type_name (a , nav , source );
613+ } else if (strcmp (tk , "tuple_expression" ) == 0 || strcmp (tk , "as_expression" ) == 0 ) {
614+ type = swift_cast_type_name (a , target , source );
615+ } else if (strcmp (tk , "simple_identifier" ) == 0 ) {
616+ char * recv = cbm_node_text (a , target , source );
617+ if (recv && recv [0 ] >= 'a' && recv [0 ] <= 'z' ) {
618+ size_t len = strlen (recv );
619+ while (len > 0 && (recv [len - 1 ] == '?' || recv [len - 1 ] == '!' )) {
620+ recv [-- len ] = '\0' ;
621+ }
622+ type = swift_local_cast_type (a , call , recv , source );
623+ }
624+ } else if ((strcmp (tk , "call_expression" ) == 0 ||
625+ strcmp (tk , "constructor_expression" ) == 0 ) &&
626+ swift_suffix_starts_line (target , suffix , source )) {
627+ type = cbm_arena_strdup (a , "View" );
628+ }
629+ if (type ) {
630+ return cbm_arena_sprintf (a , "%s.%s" , type , mname );
631+ }
632+ return cbm_node_text (a , nav , source );
633+ }
634+
430635static char * extract_swift_callee (CBMArena * a , TSNode node , const char * source , const char * nk ) {
431636 if (strcmp (nk , "call_expression" ) != 0 && strcmp (nk , "constructor_expression" ) != 0 ) {
432637 return NULL ;
433638 }
434639 if (ts_node_named_child_count (node ) > 0 ) {
435640 TSNode callee = ts_node_named_child (node , 0 );
436641 const char * ck = ts_node_type (callee );
437- if (strcmp (ck , "simple_identifier" ) == 0 || strcmp ( ck , "navigation_expression" ) == 0 ) {
642+ if (strcmp (ck , "simple_identifier" ) == 0 ) {
438643 return cbm_node_text (a , callee , source );
439644 }
645+ if (strcmp (ck , "navigation_expression" ) == 0 ) {
646+ return swift_navigation_callee (a , node , callee , source );
647+ }
440648 }
441649 return NULL ;
442650}
0 commit comments