@@ -121,6 +121,22 @@ typedef struct
121121 TypeKind type ;
122122} Float ;
123123
124+ typedef struct
125+ {
126+ uint32_t count ;
127+ uint32_t capacity ;
128+ uint32_t max_load ;
129+ DeclId * methods ;
130+ } MethodTable ;
131+
132+ typedef struct
133+ {
134+ uint32_t count ;
135+ uint32_t capacity ;
136+ uint32_t max_load ;
137+ DeclId * entries ;
138+ } DeclTable ;
139+
124140struct ConstInitializer_
125141{
126142 ConstInitType kind ;
@@ -619,6 +635,13 @@ typedef struct
619635 AstId parent ;
620636} LabelDecl ;
621637
638+ typedef struct
639+ {
640+ DeclId overloads [OVERLOADS_COUNT + 1 ];
641+ DeclTable method_table ;
642+ Decl * * methods ;
643+ } Methods ;
644+
622645typedef struct Decl_
623646{
624647 const char * name ;
@@ -678,7 +701,7 @@ typedef struct Decl_
678701 struct
679702 {
680703 TypeInfo * * interfaces ;
681- Decl * * methods ;
704+ Methods * method_table ;
682705 union
683706 {
684707 // Enums and Fault
@@ -1570,7 +1593,6 @@ typedef struct Module_
15701593 AnalysisStage stage : 6 ;
15711594
15721595 AstId contracts ;
1573- Decl * * private_method_extensions ;
15741596 HTable symbols ;
15751597 struct CompilationUnit_ * * units ;
15761598 Module * generic_module ;
@@ -1652,14 +1674,6 @@ typedef struct
16521674 LexMode mode ;
16531675} Lexer ;
16541676
1655- typedef struct
1656- {
1657- uint32_t count ;
1658- uint32_t capacity ;
1659- uint32_t max_load ;
1660- DeclId * entries ;
1661- } DeclTable ;
1662-
16631677struct CompilationUnit_
16641678{
16651679 Module * module ;
@@ -1697,7 +1711,6 @@ struct CompilationUnit_
16971711 Decl * main_function ;
16981712 HTable local_symbols ;
16991713 int lambda_count ;
1700- Decl * * local_method_extensions ;
17011714 TypeInfo * * check_type_variable_array ;
17021715 struct
17031716 {
@@ -1899,7 +1912,6 @@ typedef struct
18991912 Module * * module_list ;
19001913 Module * * generic_module_list ;
19011914 Type * * type ;
1902- Decl * * method_extensions ;
19031915 const char * lib_dir ;
19041916 const char * * sources ;
19051917 File * * loaded_sources ;
@@ -1911,6 +1923,8 @@ typedef struct
19111923 HTable compiler_defines ;
19121924 HTable features ;
19131925 Module std_module ;
1926+ MethodTable method_extensions ;
1927+ Decl * * method_extension_list ;
19141928 DeclTable symbols ;
19151929 PathTable path_symbols ;
19161930 DeclTable generic_symbols ;
@@ -2160,6 +2174,7 @@ UNUSED bool i128_get_bit(const Int128 *op, int bit);
21602174#define MACRO_COPY_DECL (x ) x = copy_decl(c, x)
21612175#define MACRO_COPY_DECLID (x ) x = declid_copy_deep(c, x)
21622176#define MACRO_COPY_DECL_LIST (x ) x = copy_decl_list(c, x)
2177+ #define MACRO_COPY_DECL_METHODS (x ) x = copy_decl_methods(c, x)
21632178#define MACRO_COPY_EXPR (x ) x = copy_expr(c, x)
21642179#define MACRO_COPY_EXPRID (x ) x = exprid_copy_deep(c, x)
21652180#define MACRO_COPY_TYPE (x ) x = copy_type_info(c, x)
@@ -2370,6 +2385,7 @@ Path *path_create_from_string(const char *string, uint32_t len, SourceSpan span)
23702385typedef enum FindMember
23712386{
23722387 METHODS_AND_FIELDS ,
2388+ METHODS_INTERFACES_AND_FIELDS ,
23732389 FIELDS_ONLY
23742390} FindMember ;
23752391
@@ -2418,8 +2434,9 @@ void sema_expr_convert_enum_to_int(Expr *expr);
24182434Decl * sema_decl_stack_resolve_symbol (const char * symbol );
24192435Decl * sema_find_decl_in_modules (Module * * module_list , Path * path , const char * interned_name );
24202436bool unit_resolve_parameterized_symbol (SemaContext * context , NameResolve * name_resolve );
2421- Decl * sema_resolve_type_method (CompilationUnit * unit , Type * type , const char * method_name , Decl * * ambiguous_ref , Decl * * private_ref );
2422- Decl * sema_resolve_method (CompilationUnit * unit , Decl * type , const char * method_name , Decl * * ambiguous_ref , Decl * * private_ref );
2437+ Decl * sema_resolve_type_method (CanonicalType * type , const char * method_name );
2438+ Decl * sema_resolve_method (Decl * type , const char * method_name );
2439+ Decl * sema_resolve_method_only (Decl * type , const char * method_name );
24232440Decl * sema_find_extension_method_in_list (Decl * * extensions , Type * type , const char * method_name );
24242441bool sema_resolve_type_decl (SemaContext * context , Type * type );
24252442bool sema_check_type_variable_array (SemaContext * context , TypeInfo * type );
@@ -2476,6 +2493,10 @@ void decltable_init(DeclTable *table, uint32_t initial_size);
24762493DeclId decltable_get (DeclTable * table , const char * name );
24772494void decltable_set (DeclTable * table , Decl * decl );
24782495
2496+ void methodtable_init (MethodTable * table , uint32_t initial_size );
2497+ DeclId methodtable_get (MethodTable * table , Type * type , const char * name );
2498+ DeclId methodtable_set (MethodTable * table , Decl * method );
2499+
24792500const char * scratch_buffer_interned (void );
24802501const char * scratch_buffer_interned_as (TokenType * type );
24812502
@@ -2542,7 +2563,6 @@ bool type_is_abi_aggregate(Type *type);
25422563bool type_is_int128 (Type * type );
25432564
25442565Type * type_from_token (TokenType type );
2545- bool type_is_user_defined (Type * type );
25462566bool type_is_structurally_equivalent (Type * type1 , Type * type );
25472567bool type_flat_is_floatlike (Type * type );
25482568bool type_flat_is_intlike (Type * type );
@@ -2608,6 +2628,7 @@ INLINE TypeInfo *type_info_new(TypeInfoKind kind, SourceSpan span);
26082628INLINE TypeInfo * type_info_new_base (Type * type , SourceSpan span );
26092629INLINE bool type_info_ok (TypeInfo * type_info );
26102630INLINE bool type_info_poison (TypeInfo * type );
2631+ INLINE bool type_is_user_defined (Type * type );
26112632
26122633int type_kind_bitsize (TypeKind kind );
26132634INLINE bool type_kind_is_signed (TypeKind kind );
@@ -3019,6 +3040,46 @@ INLINE Type *type_flatten_for_bitstruct(Type *type)
30193040 return type ;
30203041}
30213042
3043+ static inline void methods_add (Methods * methods , Decl * method )
3044+ {
3045+ vec_add (methods -> methods , method );
3046+ OperatorOverload operator = method -> func_decl .operator ;
3047+ if (operator )
3048+ {
3049+ unsigned len = vec_size (method -> func_decl .signature .params );
3050+ if (operator == OVERLOAD_MINUS && len == 1 )
3051+ {
3052+ method -> func_decl .operator = operator = OVERLOAD_UNARY_MINUS ;
3053+ }
3054+
3055+ if (len > 1 && !method -> func_decl .signature .params [1 ]-> var .type_info )
3056+ {
3057+ method -> func_decl .is_wildcard_overload = true;
3058+ }
3059+ DeclId * decl = & methods -> overloads [operator ];
3060+ if (!* decl )
3061+ {
3062+ * decl = declid (method );
3063+ }
3064+ else
3065+ {
3066+ Decl * current = declptr (* decl );
3067+ if (current -> decl_kind != DECL_DECLARRAY )
3068+ {
3069+ Decl * decl_array = decl_new (DECL_DECLARRAY , NULL , INVALID_SPAN );
3070+ vec_add (decl_array -> decls , declptr (* decl ));
3071+ vec_add (decl_array -> decls , method );
3072+ * decl = declid (decl_array );
3073+ }
3074+ else
3075+ {
3076+ vec_add (current -> decls , method );
3077+ }
3078+ }
3079+ }
3080+ decltable_set (& methods -> method_table , method );
3081+ }
3082+
30223083static inline Type * type_base (Type * type )
30233084{
30243085 while (1 )
@@ -3147,6 +3208,21 @@ static inline Type *type_flat_distinct_enum_inline(Type *type)
31473208 }
31483209}
31493210
3211+ INLINE bool type_is_user_defined (Type * type )
3212+ {
3213+ static const bool user_defined_types [TYPE_LAST + 1 ] = {
3214+ [TYPE_ENUM ] = true,
3215+ [TYPE_STRUCT ] = true,
3216+ [TYPE_FUNC_RAW ] = true,
3217+ [TYPE_UNION ] = true,
3218+ [TYPE_DISTINCT ] = true,
3219+ [TYPE_BITSTRUCT ] = true,
3220+ [TYPE_TYPEDEF ] = true,
3221+ [TYPE_INTERFACE ] = true,
3222+ };
3223+ return user_defined_types [type -> type_kind ];
3224+ }
3225+
31503226static inline Type * type_flatten_to_int (Type * type )
31513227{
31523228 while (1 )
0 commit comments