Browse Source

Change `is_utf16` field to `encoding` and use an enum

gingerBill 4 days ago
parent
commit
af3184adc9

+ 6 - 1
base/runtime/core.odin

@@ -61,6 +61,11 @@ Type_Info_Struct_Soa_Kind :: enum u8 {
 	Dynamic = 3,
 	Dynamic = 3,
 }
 }
 
 
+Type_Info_String_Encoding_Kind :: enum u8 {
+	UTF_8  = 0,
+	UTF_16 = 1,
+}
+
 // Variant Types
 // Variant Types
 Type_Info_Named :: struct {
 Type_Info_Named :: struct {
 	name: string,
 	name: string,
@@ -73,7 +78,7 @@ Type_Info_Rune       :: struct {}
 Type_Info_Float      :: struct {endianness: Platform_Endianness}
 Type_Info_Float      :: struct {endianness: Platform_Endianness}
 Type_Info_Complex    :: struct {}
 Type_Info_Complex    :: struct {}
 Type_Info_Quaternion :: struct {}
 Type_Info_Quaternion :: struct {}
-Type_Info_String     :: struct {is_cstring: bool, is_utf16: bool}
+Type_Info_String     :: struct {is_cstring: bool, encoding: Type_Info_String_Encoding_Kind}
 Type_Info_Boolean    :: struct {}
 Type_Info_Boolean    :: struct {}
 Type_Info_Any        :: struct {}
 Type_Info_Any        :: struct {}
 Type_Info_Type_Id    :: struct {}
 Type_Info_Type_Id    :: struct {}

+ 3 - 2
base/runtime/print.odin

@@ -297,8 +297,9 @@ print_type :: #force_no_inline proc "contextless" (ti: ^Type_Info) {
 			print_byte('c')
 			print_byte('c')
 		}
 		}
 		print_string("string")
 		print_string("string")
-		if info.is_utf16 {
-			print_string("16")
+		switch info.encoding {
+		case .UTF_8:  /**/
+		case .UTF_16: print_string("16")
 		}
 		}
 	case Type_Info_Boolean:
 	case Type_Info_Boolean:
 		switch ti.id {
 		switch ti.id {

+ 1 - 1
core/encoding/cbor/tags.odin

@@ -298,7 +298,7 @@ tag_base64_unmarshal :: proc(_: ^Tag_Implementation, d: Decoder, _: Tag_Number,
 
 
 	#partial switch t in ti.variant {
 	#partial switch t in ti.variant {
 	case reflect.Type_Info_String:
 	case reflect.Type_Info_String:
-		assert(!t.is_utf16)
+		assert(t.encoding == .UTF_8)
 		if t.is_cstring {
 		if t.is_cstring {
 			length  := base64.decoded_len(bytes)
 			length  := base64.decoded_len(bytes)
 			builder := strings.builder_make(0, length+1)
 			builder := strings.builder_make(0, length+1)

+ 1 - 1
core/encoding/cbor/unmarshal.odin

@@ -335,7 +335,7 @@ _unmarshal_value :: proc(d: Decoder, v: any, hdr: Header, allocator := context.a
 _unmarshal_bytes :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header, add: Add, allocator := context.allocator, loc := #caller_location) -> (err: Unmarshal_Error) {
 _unmarshal_bytes :: proc(d: Decoder, v: any, ti: ^reflect.Type_Info, hdr: Header, add: Add, allocator := context.allocator, loc := #caller_location) -> (err: Unmarshal_Error) {
 	#partial switch t in ti.variant {
 	#partial switch t in ti.variant {
 	case reflect.Type_Info_String:
 	case reflect.Type_Info_String:
-		assert(!t.is_utf16)
+		assert(t.encoding == .UTF_8)
 
 
 		bytes := err_conv(_decode_bytes(d, add, allocator=allocator, loc=loc)) or_return
 		bytes := err_conv(_decode_bytes(d, add, allocator=allocator, loc=loc)) or_return
 
 

+ 1 - 1
core/encoding/json/unmarshal.odin

@@ -571,7 +571,7 @@ unmarshal_object :: proc(p: ^Parser, v: any, end_token: Token_Kind) -> (err: Unm
 
 
 			#partial switch tk in t.key.variant {
 			#partial switch tk in t.key.variant {
 				case runtime.Type_Info_String:
 				case runtime.Type_Info_String:
-					assert(!tk.is_utf16)
+					assert(tk.encoding == .UTF_8)
 
 
 					key_ptr = rawptr(&key)
 					key_ptr = rawptr(&key)
 					key_cstr: cstring
 					key_cstr: cstring

+ 1 - 1
core/flags/internal_rtti.odin

@@ -127,7 +127,7 @@ parse_and_set_pointer_by_base_type :: proc(ptr: rawptr, str: string, type_info:
 		}
 		}
 
 
 	case runtime.Type_Info_String:
 	case runtime.Type_Info_String:
-		assert(!specific_type_info.is_utf16)
+		assert(specific_type_info.encoding == .UTF_8)
 
 
 		if specific_type_info.is_cstring {
 		if specific_type_info.is_cstring {
 			cstr_ptr := (^cstring)(ptr)
 			cstr_ptr := (^cstring)(ptr)

+ 3 - 2
core/reflect/types.odin

@@ -514,8 +514,9 @@ write_type_writer :: #force_no_inline proc(w: io.Writer, ti: ^Type_Info, n_writt
 			io.write_byte(w, 'c', &n) or_return
 			io.write_byte(w, 'c', &n) or_return
 		}
 		}
 		io.write_string(w, "string", &n)  or_return
 		io.write_string(w, "string", &n)  or_return
-		if info.is_utf16 {
-			io.write_string(w, "16", &n) or_return
+		switch info.encoding {
+		case .UTF_8:  /**/
+		case .UTF_16: io.write_string(w, "16", &n) or_return
 		}
 		}
 	case Type_Info_Boolean:
 	case Type_Info_Boolean:
 		switch ti.id {
 		switch ti.id {

+ 3 - 0
src/checker.cpp

@@ -3101,6 +3101,9 @@ gb_internal void init_core_type_info(Checker *c) {
 
 
 	GB_ASSERT(tis->fields.count == 5);
 	GB_ASSERT(tis->fields.count == 5);
 
 
+	Entity *type_info_string_encoding_kind = find_core_entity(c, str_lit("Type_Info_String_Encoding_Kind"));
+	t_type_info_string_encoding_kind = type_info_string_encoding_kind->type;
+
 	Entity *type_info_variant = tis->fields[4];
 	Entity *type_info_variant = tis->fields[4];
 	Type *tiv_type = type_info_variant->type;
 	Type *tiv_type = type_info_variant->type;
 	GB_ASSERT(is_type_union(tiv_type));
 	GB_ASSERT(is_type_union(tiv_type));

+ 12 - 4
src/llvm_backend_type.cpp

@@ -525,7 +525,15 @@ gb_internal void lb_setup_type_info_data_giant_array(lbModule *m, i64 global_typ
 				break;
 				break;
 
 
 			case Basic_string:
 			case Basic_string:
-				tag_type = t_type_info_string;
+				{
+					tag_type = t_type_info_string;
+					LLVMValueRef vals[2] = {
+						lb_const_bool(m, t_bool, false).value,
+						lb_const_int(m, t_type_info_string_encoding_kind, 0).value,
+					};
+
+					variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
+				}
 				break;
 				break;
 
 
 			case Basic_cstring:
 			case Basic_cstring:
@@ -533,7 +541,7 @@ gb_internal void lb_setup_type_info_data_giant_array(lbModule *m, i64 global_typ
 					tag_type = t_type_info_string;
 					tag_type = t_type_info_string;
 					LLVMValueRef vals[2] = {
 					LLVMValueRef vals[2] = {
 						lb_const_bool(m, t_bool, true).value,
 						lb_const_bool(m, t_bool, true).value,
-						lb_const_bool(m, t_bool, false).value,
+						lb_const_int(m, t_type_info_string_encoding_kind, 0).value,
 					};
 					};
 
 
 					variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
 					variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
@@ -545,7 +553,7 @@ gb_internal void lb_setup_type_info_data_giant_array(lbModule *m, i64 global_typ
 					tag_type = t_type_info_string;
 					tag_type = t_type_info_string;
 					LLVMValueRef vals[2] = {
 					LLVMValueRef vals[2] = {
 						lb_const_bool(m, t_bool, false).value,
 						lb_const_bool(m, t_bool, false).value,
-						lb_const_bool(m, t_bool, true).value,
+						lb_const_int(m, t_type_info_string_encoding_kind, 1).value,
 					};
 					};
 
 
 					variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
 					variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
@@ -558,7 +566,7 @@ gb_internal void lb_setup_type_info_data_giant_array(lbModule *m, i64 global_typ
 					tag_type = t_type_info_string;
 					tag_type = t_type_info_string;
 					LLVMValueRef vals[2] = {
 					LLVMValueRef vals[2] = {
 						lb_const_bool(m, t_bool, true).value,
 						lb_const_bool(m, t_bool, true).value,
-						lb_const_bool(m, t_bool, true).value,
+						lb_const_int(m, t_type_info_string_encoding_kind, 1).value,
 					};
 					};
 
 
 					variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));
 					variant_value = llvm_const_named_struct(m, tag_type, vals, gb_count_of(vals));

+ 2 - 0
src/types.cpp

@@ -661,6 +661,8 @@ gb_global Type *t_type_info_enum_value           = nullptr;
 gb_global Type *t_type_info_ptr                  = nullptr;
 gb_global Type *t_type_info_ptr                  = nullptr;
 gb_global Type *t_type_info_enum_value_ptr       = nullptr;
 gb_global Type *t_type_info_enum_value_ptr       = nullptr;
 
 
+gb_global Type *t_type_info_string_encoding_kind = nullptr;
+
 gb_global Type *t_type_info_named                = nullptr;
 gb_global Type *t_type_info_named                = nullptr;
 gb_global Type *t_type_info_integer              = nullptr;
 gb_global Type *t_type_info_integer              = nullptr;
 gb_global Type *t_type_info_rune                 = nullptr;
 gb_global Type *t_type_info_rune                 = nullptr;