@@ -1352,6 +1352,138 @@ static void py_emit_module_member_call(PyLSPContext *ctx, const char *callee_qn,
13521352 }
13531353}
13541354
1355+ /* ── super(): the C3 method resolution order ──────────────────────── */
1356+
1357+ /* Longest linearization followed. A longer one (or more bases than this) keeps
1358+ * the base-by-base walk of the caller. */
1359+ #define PY_C3_MAX 64
1360+
1361+ /* The linearizations computed during one super() lookup: every class of a
1362+ * diamond is linearized once, however many paths reach it. */
1363+ typedef struct {
1364+ const char * qn [PY_C3_MAX ];
1365+ const char * * mro [PY_C3_MAX ];
1366+ int len [PY_C3_MAX ];
1367+ int count ;
1368+ } py_c3_cache_t ;
1369+
1370+ static bool py_c3_in_tail (const char * qn , const char * const * seq , int head , int len ) {
1371+ for (int k = head + 1 ; k < len ; k ++ ) {
1372+ if (strcmp (seq [k ], qn ) == 0 )
1373+ return true;
1374+ }
1375+ return false;
1376+ }
1377+
1378+ /* The C3 head to take next: the first sequence head that is in no sequence's
1379+ * tail. NULL when every head is blocked (Python rejects such a class). */
1380+ static const char * py_c3_pick (const char * * const * seqs , const int * lens , const int * heads ,
1381+ int nseq ) {
1382+ for (int s = 0 ; s < nseq ; s ++ ) {
1383+ if (heads [s ] >= lens [s ])
1384+ continue ;
1385+ const char * cand = seqs [s ][heads [s ]];
1386+ bool blocked = false;
1387+ for (int t = 0 ; t < nseq && !blocked ; t ++ ) {
1388+ blocked = heads [t ] < lens [t ] && py_c3_in_tail (cand , seqs [t ], heads [t ], lens [t ]);
1389+ }
1390+ if (!blocked )
1391+ return cand ;
1392+ }
1393+ return NULL ;
1394+ }
1395+
1396+ /* Python's method resolution order of `type_qn`: the class, then the C3 merge
1397+ * of its bases' linearizations and of the base list. A common base therefore
1398+ * follows every class deriving from it. Sets *out to an arena array and returns
1399+ * its length, or -1 when there is no C3 order, the hierarchy is deeper than
1400+ * CBM_LSP_MAX_LOOKUP_DEPTH, or the order is longer than PY_C3_MAX. A base the
1401+ * registry does not know (external, unresolved) linearizes to itself. */
1402+ static int py_c3_linearize (PyLSPContext * ctx , py_c3_cache_t * cache , const char * type_qn ,
1403+ const char * * * out , int depth ) {
1404+ for (int c = 0 ; c < cache -> count ; c ++ ) {
1405+ if (strcmp (cache -> qn [c ], type_qn ) == 0 ) {
1406+ * out = cache -> mro [c ];
1407+ return cache -> len [c ];
1408+ }
1409+ }
1410+ if (depth > CBM_LSP_MAX_LOOKUP_DEPTH )
1411+ return -1 ;
1412+ const CBMRegisteredType * rt = cbm_registry_lookup_type (ctx -> registry , type_qn );
1413+ if (rt && rt -> alias_of )
1414+ return py_c3_linearize (ctx , cache , rt -> alias_of , out , depth + 1 );
1415+ const char * * bases = rt ? rt -> embedded_types : NULL ;
1416+ int nb = 0 ;
1417+ while (bases && bases [nb ])
1418+ nb ++ ;
1419+ if (nb >= PY_C3_MAX )
1420+ return -1 ;
1421+ const char * * seqs [PY_C3_MAX ];
1422+ int lens [PY_C3_MAX ];
1423+ int heads [PY_C3_MAX ];
1424+ for (int b = 0 ; b < nb ; b ++ ) {
1425+ lens [b ] = py_c3_linearize (ctx , cache , bases [b ], & seqs [b ], depth + 1 );
1426+ if (lens [b ] < 0 )
1427+ return -1 ;
1428+ heads [b ] = 0 ;
1429+ }
1430+ seqs [nb ] = bases ;
1431+ lens [nb ] = nb ;
1432+ heads [nb ] = 0 ;
1433+ const char * * mro = (const char * * )cbm_arena_alloc (ctx -> arena , PY_C3_MAX * sizeof (char * ));
1434+ if (!mro )
1435+ return -1 ;
1436+ int n = 0 ;
1437+ mro [n ++ ] = type_qn ;
1438+ for (;;) {
1439+ bool pending = false;
1440+ for (int s = 0 ; s <= nb && !pending ; s ++ ) {
1441+ pending = heads [s ] < lens [s ];
1442+ }
1443+ if (!pending )
1444+ break ;
1445+ const char * pick = py_c3_pick ((const char * * const * )seqs , lens , heads , nb + 1 );
1446+ if (!pick || n >= PY_C3_MAX )
1447+ return -1 ;
1448+ mro [n ++ ] = pick ;
1449+ for (int s = 0 ; s <= nb ; s ++ ) {
1450+ if (heads [s ] < lens [s ] && strcmp (seqs [s ][heads [s ]], pick ) == 0 )
1451+ heads [s ]++ ;
1452+ }
1453+ }
1454+ if (cache -> count < PY_C3_MAX ) {
1455+ cache -> qn [cache -> count ] = type_qn ;
1456+ cache -> mro [cache -> count ] = mro ;
1457+ cache -> len [cache -> count ] = n ;
1458+ cache -> count ++ ;
1459+ }
1460+ * out = mro ;
1461+ return n ;
1462+ }
1463+
1464+ /* The method super() reaches from inside `class_qn`: the first class AFTER it in
1465+ * its C3 order that declares `member` itself. *have_mro is false when the
1466+ * hierarchy has no C3 order; the caller then keeps its base-by-base walk. */
1467+ static const CBMRegisteredFunc * py_lookup_super_attribute (PyLSPContext * ctx , const char * class_qn ,
1468+ const char * member , bool * have_mro ) {
1469+ * have_mro = false;
1470+ if (!ctx || !class_qn || !member )
1471+ return NULL ;
1472+ py_c3_cache_t cache ;
1473+ cache .count = 0 ;
1474+ const char * * mro = NULL ;
1475+ int n = py_c3_linearize (ctx , & cache , class_qn , & mro , 0 );
1476+ if (n < 0 )
1477+ return NULL ;
1478+ * have_mro = true;
1479+ for (int i = 1 ; i < n ; i ++ ) {
1480+ const CBMRegisteredFunc * f = cbm_registry_lookup_method (ctx -> registry , mro [i ], member );
1481+ if (f )
1482+ return f ;
1483+ }
1484+ return NULL ;
1485+ }
1486+
13551487/* Per-file field overlay: look up a (class_qn, field_name) recorded during resolve
13561488 * against the sealed shared registry. Linear scan — the overlay only holds fields
13571489 * discovered in THIS file's own classes, so it is small. */
@@ -3089,6 +3221,22 @@ static void py_emit_call_for(PyLSPContext *ctx, TSNode call_node) {
30893221 const CBMRegisteredType * enclosing =
30903222 cbm_registry_lookup_type (ctx -> registry , ctx -> enclosing_class_qn );
30913223 if (enclosing && enclosing -> embedded_types ) {
3224+ // The proxy walks the enclosing class's C3 order, in which a
3225+ // diamond's shared base follows every class deriving from it:
3226+ // RadarrCalendarEntity(RadarrEntity, CalendarEntity) reaches
3227+ // CalendarEntity before Entity, which the first base reaches
3228+ // through its own ancestry. The base-by-base walk below stays for
3229+ // a hierarchy without a C3 order and for an undeclared __init__.
3230+ bool have_mro = false;
3231+ const CBMRegisteredFunc * sf = py_lookup_super_attribute (
3232+ ctx , ctx -> enclosing_class_qn , attr_name , & have_mro );
3233+ if (sf ) {
3234+ bool is_init = strcmp (attr_name , "__init__" ) == 0 ;
3235+ py_emit_resolved_call (ctx , sf -> qualified_name ,
3236+ is_init ? "lsp_super_init" : "lsp_super" ,
3237+ is_init ? 0.90f : 0.88f , call_node );
3238+ return ;
3239+ }
30923240 for (int i = 0 ; enclosing -> embedded_types [i ]; i ++ ) {
30933241 // super().__init__() is a constructor delegation:
30943242 // lsp_super_init is the MORE SPECIFIC, more accurate
0 commit comments