Skip to content

Commit 9eeaefb

Browse files
authored
Merge branch 'master' into zip-with-macro
2 parents c89da95 + e35dbd2 commit 9eeaefb

24 files changed

Lines changed: 558 additions & 580 deletions

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@ add_executable(c3c
390390
src/utils/unzipper.c
391391
src/compiler/c_codegen.c
392392
src/compiler/decltable.c
393+
src/compiler/methodtable.c
393394
src/compiler/mac_support.c
394395
src/compiler/windows_support.c
395396
src/compiler/codegen_asm.c

lib/std/core/string.c3

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ fn String String.replace(self, Allocator allocator, String needle, String new_st
187187
@pool()
188188
{
189189
String[] split = self.tsplit(needle);
190-
return dstring::join(tmem, split, new_str).copy_str(mem);
190+
return dstring::join(tmem, split, new_str).copy_str(allocator);
191191
};
192192
}
193193

releasenotes.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Deprecate `@compact` use for comparison. Old behaviour is enabled using `--use-old-compact-eq`.
1212
- Switch available for types implementing `@operator(==)`.
1313
- `Type.is_eq` is now true for types with `==` overload.
14+
- Methods ignore visibility settings.
1415
- Allow inout etc on untyped macro parameters even if they are not pointers.
1516
- Deprecate `add_array` in favour of `push_all` on lists.
1617

@@ -39,6 +40,8 @@
3940
- Compile time indexing at compile time in a $typeof was no considered compile time.
4041
- Slicing a constant array with designated initialization would not update the indexes.
4142
- Fix for bug when `@format` encountered `*` in some cases.
43+
- Compiler segfault on global slice initialization with null[:0] #2404.
44+
- Use correct allocator in `replace`.
4245

4346
### Stdlib changes
4447
- Add `==` to `Pair`, `Triple` and TzDateTime. Add print to `Pair` and `Triple`.

src/compiler/compiler.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ void compiler_init(BuildOptions *build_options)
7575

7676
htable_init(&compiler.context.features, 1024);
7777
htable_init(&compiler.context.compiler_defines, 16 * 1024);
78+
methodtable_init(&compiler.context.method_extensions, 16 * 1024);
7879
compiler.context.module_list = NULL;
7980
compiler.context.generic_module_list = NULL;
80-
compiler.context.method_extensions = NULL;
81+
compiler.context.method_extension_list = NULL;
8182

8283
vmem_init(&ast_arena, START_VMEM_SIZE);
8384
ast_calloc();

src/compiler/compiler_internal.h

Lines changed: 91 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
124140
struct 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+
622645
typedef 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-
16631677
struct 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)
23702385
typedef 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);
24182434
Decl *sema_decl_stack_resolve_symbol(const char *symbol);
24192435
Decl *sema_find_decl_in_modules(Module **module_list, Path *path, const char *interned_name);
24202436
bool 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);
24232440
Decl *sema_find_extension_method_in_list(Decl **extensions, Type *type, const char *method_name);
24242441
bool sema_resolve_type_decl(SemaContext *context, Type *type);
24252442
bool sema_check_type_variable_array(SemaContext *context, TypeInfo *type);
@@ -2476,6 +2493,10 @@ void decltable_init(DeclTable *table, uint32_t initial_size);
24762493
DeclId decltable_get(DeclTable *table, const char *name);
24772494
void 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+
24792500
const char *scratch_buffer_interned(void);
24802501
const char *scratch_buffer_interned_as(TokenType *type);
24812502

@@ -2542,7 +2563,6 @@ bool type_is_abi_aggregate(Type *type);
25422563
bool type_is_int128(Type *type);
25432564

25442565
Type *type_from_token(TokenType type);
2545-
bool type_is_user_defined(Type *type);
25462566
bool type_is_structurally_equivalent(Type *type1, Type *type);
25472567
bool type_flat_is_floatlike(Type *type);
25482568
bool type_flat_is_intlike(Type *type);
@@ -2608,6 +2628,7 @@ INLINE TypeInfo *type_info_new(TypeInfoKind kind, SourceSpan span);
26082628
INLINE TypeInfo *type_info_new_base(Type *type, SourceSpan span);
26092629
INLINE bool type_info_ok(TypeInfo *type_info);
26102630
INLINE bool type_info_poison(TypeInfo *type);
2631+
INLINE bool type_is_user_defined(Type *type);
26112632

26122633
int type_kind_bitsize(TypeKind kind);
26132634
INLINE 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+
30223083
static 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+
31503226
static inline Type *type_flatten_to_int(Type *type)
31513227
{
31523228
while (1)

src/compiler/copying.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static Ast *ast_copy_deep(CopyStruct *c, Ast *source);
1616
static Ast **copy_ast_list(CopyStruct *c, Ast **to_copy);
1717
static Decl *copy_decl(CopyStruct *c, Decl *decl);
1818
static Decl **copy_decl_list(CopyStruct *c, Decl **decl_list);
19+
static Methods *copy_decl_methods(CopyStruct *c, Methods *methods);
1920
static TypeInfo *copy_type_info(CopyStruct *c, TypeInfo *source);
2021

2122
static inline void copy_reg_ref(CopyStruct *c, void *original, void *result)
@@ -871,8 +872,6 @@ static ResolvedAttrData *copy_attrs_resolved(CopyStruct *c, ResolvedAttrData *da
871872
.section = data->section,
872873
.wasm_module = data->wasm_module
873874
};
874-
const char **new = NULL;
875-
876875
return copy;
877876
}
878877

@@ -913,6 +912,19 @@ Decl **copy_decl_list(CopyStruct *c, Decl **decl_list)
913912
return result;
914913
}
915914

915+
static Methods *copy_decl_methods(CopyStruct *c, Methods *methods)
916+
{
917+
if (!methods) return NULL;
918+
Methods *copy = CALLOCS(Methods);
919+
decltable_init(&copy->method_table, 64);
920+
921+
FOREACH(Decl *, method, methods->methods)
922+
{
923+
methods_add(copy, copy_decl(c, method));
924+
}
925+
return copy;
926+
}
927+
916928
TypeInfo *copy_type_info(CopyStruct *c, TypeInfo *source)
917929
{
918930
if (!source) return NULL;
@@ -1004,7 +1016,7 @@ Decl *copy_decl(CopyStruct *c, Decl *decl)
10041016
case DECL_INTERFACE:
10051017
copy_decl_type(copy);
10061018
MACRO_COPY_TYPE_LIST(copy->interfaces);
1007-
MACRO_COPY_DECL_LIST(copy->methods);
1019+
MACRO_COPY_DECL_METHODS(copy->method_table);
10081020
MACRO_COPY_DECL_LIST(copy->interface_methods);
10091021
break;
10101022
case DECL_CT_EXEC:
@@ -1024,7 +1036,7 @@ Decl *copy_decl(CopyStruct *c, Decl *decl)
10241036
MACRO_COPY_TYPE_LIST(copy->interfaces);
10251037
MACRO_COPY_DECL_LIST(copy->strukt.members);
10261038
MACRO_COPY_DECLID(copy->strukt.padded_decl_id);
1027-
MACRO_COPY_DECL_LIST(copy->methods);
1039+
MACRO_COPY_DECL_METHODS(copy->method_table);
10281040
break;
10291041
case DECL_DECLARRAY:
10301042
case DECL_GROUP:
@@ -1035,21 +1047,21 @@ Decl *copy_decl(CopyStruct *c, Decl *decl)
10351047
MACRO_COPY_TYPE_LIST(copy->interfaces);
10361048
MACRO_COPY_DECL_LIST(copy->strukt.members);
10371049
MACRO_COPY_TYPE(copy->strukt.container_type);
1038-
MACRO_COPY_DECL_LIST(copy->methods);
1050+
MACRO_COPY_DECL_METHODS(copy->method_table);
10391051
break;
10401052
case DECL_FAULT:
10411053
break;
10421054
case DECL_CONST_ENUM:
10431055
copy_decl_type(copy);
10441056
MACRO_COPY_TYPE_LIST(copy->interfaces);
1045-
MACRO_COPY_DECL_LIST(copy->methods);
1057+
MACRO_COPY_DECL_METHODS(copy->method_table);
10461058
MACRO_COPY_TYPE(copy->enums.type_info);
10471059
MACRO_COPY_DECL_LIST(copy->enums.values);
10481060
break;
10491061
case DECL_ENUM:
10501062
copy_decl_type(copy);
10511063
MACRO_COPY_TYPE_LIST(copy->interfaces);
1052-
MACRO_COPY_DECL_LIST(copy->methods);
1064+
MACRO_COPY_DECL_METHODS(copy->method_table);
10531065
MACRO_COPY_DECL_LIST(copy->enums.parameters);
10541066
MACRO_COPY_TYPE(copy->enums.type_info);
10551067
MACRO_COPY_DECL_LIST(copy->enums.values);
@@ -1112,7 +1124,7 @@ Decl *copy_decl(CopyStruct *c, Decl *decl)
11121124
case DECL_DISTINCT:
11131125
copy_decl_type(copy);
11141126
MACRO_COPY_TYPE_LIST(copy->interfaces);
1115-
MACRO_COPY_DECL_LIST(copy->methods);
1127+
MACRO_COPY_DECL_METHODS(copy->method_table);
11161128
MACRO_COPY_TYPE(copy->distinct);
11171129
break;
11181130
case DECL_CT_ECHO:

src/compiler/decltable.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,4 @@ void decltable_init(DeclTable *table, uint32_t initial_size)
8989
table->capacity = initial_size;
9090
table->max_load = (uint32_t)(initial_size * TABLE_MAX_LOAD);
9191
table->entries = entries;
92-
}
92+
}

src/compiler/llvm_codegen_expr.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2638,19 +2638,26 @@ static void llvm_emit_slice_values(GenContext *c, Expr *slice, BEValue *parent_r
26382638
ASSERT(slice->expr_kind == EXPR_SLICE);
26392639

26402640
Expr *parent_expr = exprptr(slice->subscript_expr.expr);
2641-
26422641
Type *parent_type = type_flatten(parent_expr->type);
2642+
parent_type = type_no_optional(parent_type);
26432643
BEValue parent_addr_x;
26442644
llvm_emit_expr(c, &parent_addr_x, parent_expr);
2645-
llvm_value_addr(c, &parent_addr_x);
2646-
LLVMValueRef parent_addr = parent_addr_x.value;
26472645
LLVMValueRef parent_load_value = NULL;
26482646
LLVMValueRef parent_base;
2649-
parent_type = type_no_optional(parent_type);
2647+
LLVMValueRef parent_addr;
2648+
if (parent_type->type_kind == TYPE_POINTER)
2649+
{
2650+
llvm_value_rvalue(c, &parent_addr_x);
2651+
parent_load_value = parent_base = parent_addr_x.value;
2652+
}
2653+
else
2654+
{
2655+
llvm_value_addr(c, &parent_addr_x);
2656+
parent_addr = parent_addr_x.value;
2657+
}
26502658
switch (parent_type->type_kind)
26512659
{
26522660
case TYPE_POINTER:
2653-
parent_load_value = parent_base = LLVMBuildLoad2(c->builder, llvm_get_type(c, parent_type), parent_addr, "");
26542661
break;
26552662
case TYPE_SLICE:
26562663
parent_load_value = LLVMBuildLoad2(c->builder, llvm_get_type(c, parent_type), parent_addr, "");

0 commit comments

Comments
 (0)