}
}
-Map :: struct (K: type_expr, V: type_expr) where ValidKey(K) {
+Map :: struct (K: type_expr, V: type_expr) where ValidKey(K)
+ [conv.Custom_Format.{ #solidify format_map {K=K, V=V} }]
+{
allocator : Allocator;
hashes : [] i32;
package core.conv
+Enable_Custom_Formatters :: true
+
#local {
map :: package core.map
custom_formatters: Map(type_expr, (^Format_Output, ^Format, rawptr) -> void);
custom_formatters_initialized :: #init () {
map.init(^custom_formatters, default=null_proc);
+
+ #if Enable_Custom_Formatters {
+ use type_info;
+
+ for type_idx: type_table.count {
+ type := type_table[type_idx];
+ if type.kind != .Struct do continue;
+
+ s_info := cast(^Type_Info_Struct) type;
+ for s_info.tags {
+ if it.type != Custom_Format do continue;
+
+ custom_format := cast(^Custom_Format) it.data;
+ custom_formatters[cast(type_expr) type_idx] = custom_format.format;
+ }
+ }
+ }
}
register_custom_formatter :: (formatter: (^Format_Output, ^Format, ^$T) -> void) {
custom_formatters[T] = formatter;
}
+Custom_Format :: struct {
+ format: (^Format_Output, ^Format, rawptr) -> void;
+}
+
str_to_i64 :: (s: str) -> i64 {
use package core
u32 name_length = strlen(type->PolyStruct.name);
bh_buffer_append(&table_buffer, type->PolyStruct.name, name_length);
+ u32 tags_count = bh_arr_length(type->PolyStruct.meta_tags);
i32 i = 0;
bh_arr_each(AstTyped *, tag, type->PolyStruct.meta_tags) {
AstTyped* value = *tag;
- assert(value->flags & Ast_Flag_Comptime);
assert(value->type);
+ // Polymorphic structs are weird in this case, because the tag might not be constructed generically for
+ // the polymorphic structure so it should only be constructed for actual solidified structures.
+ // See core/containers/map.onyx with Custom_Format for an example.
+ if (!(value->flags & Ast_Flag_Comptime)) {
+ tags_count--;
+ continue;
+ }
+
u32 size = type_size_of(value->type);
bh_buffer_align(&table_buffer, type_alignment_of(value->type));
tag_locations[i] = table_buffer.length;
bh_buffer_align(&table_buffer, 8);
u32 tags_base = table_buffer.length;
- u32 tags_count = bh_arr_length(type->PolyStruct.meta_tags);
fori (i, 0, tags_count) {
WRITE_SLICE(tag_locations[i], type->PolyStruct.meta_tags[i]->type->id);
use package core
main :: (args: [] cstr) {
- conv.register_custom_formatter(
- #solidify map.format_map { K=i32, V=str }
- );
-
imap : Map(i32, str);
map.init(^imap, "");
defer {
Hello, I am Billy!
-Go away!! func[3]
+Go away!! func[5]