1919
2020#include "php_lsp.h"
2121#include "lsp_node_iter.h"
22+ #include "lsp_work.h"
2223#include "../helpers.h"
24+ #include "../../../src/foundation/hash_table.h"
2325#include <ctype.h>
2426#include <stdio.h>
2527#include <stdlib.h>
@@ -3745,6 +3747,126 @@ static void apply_phpdoc_class_tags(PHPLSPContext *ctx, CBMTypeRegistry *reg, co
37453747 * - handle `as` aliasing: register method under the alias name too
37463748 * - skip insteadof (rare in practice; full support deferred)
37473749 */
3750+ /* The registry indices of each receiver's methods, in registration order.
3751+ * Flattening a trait used to visit EVERY registered function to find the
3752+ * trait's own, so a file whose classes use traits paid O(uses x functions),
3753+ * and the functions grow with every class flattened before: quadratic in the
3754+ * trait-using classes of one file (#1265). The registry is append-only, so
3755+ * the index catches up incrementally (indexed_upto) and never misses a
3756+ * function registered between two flattens. */
3757+ typedef struct {
3758+ int * items ;
3759+ int count ;
3760+ int cap ;
3761+ } php_func_ids_t ;
3762+
3763+ struct php_trait_index {
3764+ CBMHashTable * by_receiver ; /* receiver_type (borrowed) -> php_func_ids_t* */
3765+ int indexed_upto ; /* reg->funcs[0, indexed_upto) are indexed */
3766+ bool failed ; /* allocation failed: callers use the full scan */
3767+ };
3768+
3769+ static bool php_func_ids_push (CBMArena * arena , php_func_ids_t * ids , int func_index ) {
3770+ if (ids -> count == ids -> cap ) {
3771+ int cap = ids -> cap ? ids -> cap * 2 : 4 ;
3772+ int * items = (int * )cbm_arena_alloc (arena , (size_t )cap * sizeof (* items ));
3773+ if (!items ) {
3774+ return false;
3775+ }
3776+ if (ids -> count > 0 ) {
3777+ memcpy (items , ids -> items , (size_t )ids -> count * sizeof (* items ));
3778+ }
3779+ ids -> items = items ;
3780+ ids -> cap = cap ;
3781+ }
3782+ ids -> items [ids -> count ++ ] = func_index ;
3783+ return true;
3784+ }
3785+
3786+ /* Index every function registered since the last call. Returns false (and
3787+ * marks the index failed for good) on an allocation failure. */
3788+ static bool php_trait_index_sync (PHPLSPContext * ctx , struct php_trait_index * ix ,
3789+ const CBMTypeRegistry * reg ) {
3790+ if (ix -> failed ) {
3791+ return false;
3792+ }
3793+ if (!ix -> by_receiver ) {
3794+ ix -> by_receiver = cbm_ht_create (0 );
3795+ if (!ix -> by_receiver ) {
3796+ ix -> failed = true;
3797+ return false;
3798+ }
3799+ }
3800+ for (; ix -> indexed_upto < reg -> func_count ; ix -> indexed_upto ++ ) {
3801+ CBM_LSP_WORK (1 );
3802+ const char * receiver = reg -> funcs [ix -> indexed_upto ].receiver_type ;
3803+ if (!receiver ) {
3804+ continue ;
3805+ }
3806+ php_func_ids_t * ids = (php_func_ids_t * )cbm_ht_get (ix -> by_receiver , receiver );
3807+ if (!ids ) {
3808+ ids = (php_func_ids_t * )cbm_arena_alloc (ctx -> arena , sizeof (* ids ));
3809+ if (!ids ) {
3810+ ix -> failed = true;
3811+ return false;
3812+ }
3813+ memset (ids , 0 , sizeof (* ids ));
3814+ cbm_ht_set (ix -> by_receiver , receiver , ids );
3815+ }
3816+ if (!php_func_ids_push (ctx -> arena , ids , ix -> indexed_upto )) {
3817+ ix -> failed = true;
3818+ return false;
3819+ }
3820+ }
3821+ return true;
3822+ }
3823+
3824+ static void php_trait_index_free (struct php_trait_index * ix ) {
3825+ if (ix -> by_receiver ) {
3826+ cbm_ht_free (ix -> by_receiver );
3827+ ix -> by_receiver = NULL ;
3828+ }
3829+ }
3830+
3831+ /* Copy one method `src` of the trait into class_qn (see
3832+ * flatten_trait_into_class). Reads src before registering, because
3833+ * cbm_registry_add_func may move reg->funcs. */
3834+ static void flatten_trait_method (PHPLSPContext * ctx , CBMTypeRegistry * reg , const char * class_qn ,
3835+ const char * canonical_trait_qn , const CBMRegisteredFunc * src ,
3836+ const char * alias_for_method , const char * only_method_name ) {
3837+ if (only_method_name && src -> short_name && strcmp (src -> short_name , only_method_name ) != 0 ) {
3838+ return ;
3839+ }
3840+ const char * new_short = alias_for_method ? alias_for_method : src -> short_name ;
3841+ if (!new_short )
3842+ return ;
3843+ CBMRegisteredFunc rf ;
3844+ memset (& rf , 0 , sizeof (rf ));
3845+ rf .qualified_name = cbm_arena_sprintf (ctx -> arena , "%s.%s" , class_qn , new_short );
3846+ rf .short_name = cbm_arena_strdup (ctx -> arena , new_short );
3847+ rf .receiver_type = class_qn ;
3848+ rf .min_params = src -> min_params ;
3849+
3850+ /* Self-substitute the return type: any NAMED(trait_qn) in the
3851+ * signature's return becomes NAMED(class_qn). */
3852+ const CBMType * new_sig = src -> signature ;
3853+ if (src -> signature && src -> signature -> kind == CBM_TYPE_FUNC &&
3854+ src -> signature -> data .func .return_types ) {
3855+ const CBMType * ret0 = src -> signature -> data .func .return_types [0 ];
3856+ if (ret0 && ret0 -> kind == CBM_TYPE_NAMED && ret0 -> data .named .qualified_name &&
3857+ strcmp (ret0 -> data .named .qualified_name , canonical_trait_qn ) == 0 ) {
3858+ const CBMType * * rets = (const CBMType * * )cbm_arena_alloc (ctx -> arena , 2 * sizeof (* rets ));
3859+ if (rets ) {
3860+ rets [0 ] = cbm_type_named (ctx -> arena , class_qn );
3861+ rets [1 ] = NULL ;
3862+ new_sig = cbm_type_func_replace_returns (ctx -> arena , src -> signature , rets );
3863+ }
3864+ }
3865+ }
3866+ rf .signature = new_sig ;
3867+ cbm_registry_add_func (reg , rf );
3868+ }
3869+
37483870static void flatten_trait_into_class (PHPLSPContext * ctx , CBMTypeRegistry * reg , const char * class_qn ,
37493871 const char * trait_qn , const char * alias_for_method ,
37503872 const char * only_method_name ) {
@@ -3780,43 +3902,36 @@ static void flatten_trait_into_class(PHPLSPContext *ctx, CBMTypeRegistry *reg, c
37803902 * appends to reg->funcs, so the loop must never visit entries it adds
37813903 * during iteration (a mutate-while-iterating hazard). */
37823904 const int func_count_before = reg -> func_count ;
3905+ struct php_trait_index * ix = ctx -> trait_index ;
3906+ if (ix && php_trait_index_sync (ctx , ix , reg )) {
3907+ /* Only the trait's own methods, in registration order; the ids are
3908+ * ascending, so the snapshot bound ends the walk. No sync runs inside
3909+ * the loop, so the list cannot change under it. */
3910+ const php_func_ids_t * ids =
3911+ (const php_func_ids_t * )cbm_ht_get (ix -> by_receiver , canonical_trait_qn );
3912+ for (int j = 0 ; ids && j < ids -> count && ids -> items [j ] < func_count_before ; j ++ ) {
3913+ CBM_LSP_WORK (1 );
3914+ if (CBM_LSP_WORK_CEILING_HIT ()) {
3915+ break ; /* test seam only (#765, #1265): see lsp_work.h */
3916+ }
3917+ flatten_trait_method (ctx , reg , class_qn , canonical_trait_qn , & reg -> funcs [ids -> items [j ]],
3918+ alias_for_method , only_method_name );
3919+ }
3920+ return ;
3921+ }
3922+ /* No index (no owning pass, or it could not allocate): the full scan,
3923+ * which yields the same methods in the same order. */
37833924 for (int i = 0 ; i < func_count_before ; i ++ ) {
3925+ CBM_LSP_WORK (1 );
3926+ if (CBM_LSP_WORK_CEILING_HIT ()) {
3927+ break ; /* test seam only (#765, #1265): see lsp_work.h */
3928+ }
37843929 const CBMRegisteredFunc * src = & reg -> funcs [i ];
37853930 if (!src -> receiver_type || strcmp (src -> receiver_type , canonical_trait_qn ) != 0 ) {
37863931 continue ;
37873932 }
3788- if (only_method_name && src -> short_name && strcmp (src -> short_name , only_method_name ) != 0 ) {
3789- continue ;
3790- }
3791- const char * new_short = alias_for_method ? alias_for_method : src -> short_name ;
3792- if (!new_short )
3793- continue ;
3794- CBMRegisteredFunc rf ;
3795- memset (& rf , 0 , sizeof (rf ));
3796- rf .qualified_name = cbm_arena_sprintf (ctx -> arena , "%s.%s" , class_qn , new_short );
3797- rf .short_name = cbm_arena_strdup (ctx -> arena , new_short );
3798- rf .receiver_type = class_qn ;
3799- rf .min_params = src -> min_params ;
3800-
3801- /* Self-substitute the return type: any NAMED(trait_qn) in the
3802- * signature's return becomes NAMED(class_qn). */
3803- const CBMType * new_sig = src -> signature ;
3804- if (src -> signature && src -> signature -> kind == CBM_TYPE_FUNC &&
3805- src -> signature -> data .func .return_types ) {
3806- const CBMType * ret0 = src -> signature -> data .func .return_types [0 ];
3807- if (ret0 && ret0 -> kind == CBM_TYPE_NAMED && ret0 -> data .named .qualified_name &&
3808- strcmp (ret0 -> data .named .qualified_name , canonical_trait_qn ) == 0 ) {
3809- const CBMType * * rets =
3810- (const CBMType * * )cbm_arena_alloc (ctx -> arena , 2 * sizeof (* rets ));
3811- if (rets ) {
3812- rets [0 ] = cbm_type_named (ctx -> arena , class_qn );
3813- rets [1 ] = NULL ;
3814- new_sig = cbm_type_func_replace_returns (ctx -> arena , src -> signature , rets );
3815- }
3816- }
3817- }
3818- rf .signature = new_sig ;
3819- cbm_registry_add_func (reg , rf );
3933+ flatten_trait_method (ctx , reg , class_qn , canonical_trait_qn , src , alias_for_method ,
3934+ only_method_name );
38203935 }
38213936}
38223937
@@ -4217,6 +4332,9 @@ static void php_lsp_collect_class_fields(PHPLSPContext *ctx, CBMTypeRegistry *re
42174332 * Pass 2: classes and enums (which may `use Trait;` and need the
42184333 * trait's signatures already patched).
42194334 */
4335+ struct php_trait_index trait_index = {0 };
4336+ struct php_trait_index * outer_index = ctx -> trait_index ;
4337+ ctx -> trait_index = & trait_index ;
42204338 for (int pass = 0 ; pass < 2 ; pass ++ ) {
42214339 TSNode stack [256 ];
42224340 int top = 0 ;
@@ -4241,6 +4359,8 @@ static void php_lsp_collect_class_fields(PHPLSPContext *ctx, CBMTypeRegistry *re
42414359 }
42424360 }
42434361 }
4362+ php_trait_index_free (& trait_index );
4363+ ctx -> trait_index = outer_index ;
42444364}
42454365
42464366void cbm_php_refine_lsp_registry (PHPLSPContext * ctx , CBMTypeRegistry * reg , TSNode root ) {
0 commit comments