From e57db2006bb0869ef557878a3951c8dbf2a0b349 Mon Sep 17 00:00:00 2001 From: YUART Date: Sun, 13 Aug 2023 13:07:06 +0200 Subject: [PATCH 1/8] Fix: module name when generating wrapper --- src/c2v.v | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/c2v.v b/src/c2v.v index c87dfc7..3912d68 100644 --- a/src/c2v.v +++ b/src/c2v.v @@ -228,7 +228,7 @@ fn (mut c2v C2V) add_file(ast_path string, outv string, c_file string) { if c2v.is_wrapper { // Generate v_wrapper.v in user's current directory - c2v.wrapper_module_name = os.dir(outv).after('/') + c2v.wrapper_module_name = os.dir(outv).all_after_last('/') wrapper_path := c2v.outv c2v.out_file = os.create(wrapper_path) or { panic('cant create file "${wrapper_path}" ') } } else { @@ -2117,6 +2117,11 @@ fn main() { vprintln(os.args.str()) is_wrapper := os.args[1] == 'wrapper' mut path := os.args.last() + + if os.is_abs_path(path) == false { + path = os.abs_path(path) + } + if !os.exists(path) { eprintln('"${path}" does not exist') exit(1) From e8ebe458529ffbfcb8318872a5d1e6578bd782f2 Mon Sep 17 00:00:00 2001 From: Artem Yurchenko Date: Fri, 8 Sep 2023 14:36:58 +0200 Subject: [PATCH 2/8] Implement: uint16_t conversion --- src/c2v.v | 12 +++++++++++- tests/18.header_types.h | 3 +++ tests/18.header_types.out | 8 ++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/18.header_types.h create mode 100644 tests/18.header_types.out diff --git a/src/c2v.v b/src/c2v.v index 4c81f38..cbba452 100644 --- a/src/c2v.v +++ b/src/c2v.v @@ -107,6 +107,7 @@ mut: translation_start_ticks i64 // initialised before the loop calling .translate_file() has_cfile bool returning_bool bool + generated_lines map[string]bool // to avoid generating the same line twice } fn empty_toml_doc() toml.Doc { @@ -139,6 +140,10 @@ fn add_place_data_to_error(err IError) string { } fn (mut c C2V) genln(s string) { + // if s in c.generated_lines { + // return + // } + if c.indent > 0 && c.out_line_empty { c.out.write_string(tabs[c.indent]) } @@ -148,6 +153,8 @@ fn (mut c C2V) genln(s string) { } c.out.writeln(filter_line(s)) c.out_line_empty = true + + // c.generated_lines[s] = true } fn (mut c C2V) gen(s string) { @@ -614,6 +621,9 @@ fn convert_type(typ_ string) Type { 'int16_t' { 'i16' } + 'uint16_t' { + 'u16' + } 'uint8_t' { 'u8' } @@ -1239,7 +1249,7 @@ fn (mut c C2V) case_st(mut child Node, is_enum bool) bool { default: MD_UNREACHABLE(); */ - //c.gen('/*TODO fallthrough*/') + // c.gen('/*TODO fallthrough*/') } else { c.statement(mut a) } diff --git a/tests/18.header_types.h b/tests/18.header_types.h new file mode 100644 index 0000000..55347b9 --- /dev/null +++ b/tests/18.header_types.h @@ -0,0 +1,3 @@ +#include + +uint16_t ts_f32tof16(float f); diff --git a/tests/18.header_types.out b/tests/18.header_types.out new file mode 100644 index 0000000..a343963 --- /dev/null +++ b/tests/18.header_types.out @@ -0,0 +1,8 @@ +[translated] +module tests + +fn C.ts_f32tof16(f f32) u16 + +pub fn ts_f32tof16(f f32) u16 { + return C.ts_f32tof16(f) +} From 8848382997bbd3ea965c8c287cad0b97e1910738 Mon Sep 17 00:00:00 2001 From: Artem Yurchenko Date: Fri, 8 Sep 2023 14:38:52 +0200 Subject: [PATCH 3/8] Revert test changes --- src/c2v.v | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/c2v.v b/src/c2v.v index cbba452..a847bb4 100644 --- a/src/c2v.v +++ b/src/c2v.v @@ -107,7 +107,6 @@ mut: translation_start_ticks i64 // initialised before the loop calling .translate_file() has_cfile bool returning_bool bool - generated_lines map[string]bool // to avoid generating the same line twice } fn empty_toml_doc() toml.Doc { @@ -140,10 +139,6 @@ fn add_place_data_to_error(err IError) string { } fn (mut c C2V) genln(s string) { - // if s in c.generated_lines { - // return - // } - if c.indent > 0 && c.out_line_empty { c.out.write_string(tabs[c.indent]) } @@ -153,8 +148,6 @@ fn (mut c C2V) genln(s string) { } c.out.writeln(filter_line(s)) c.out_line_empty = true - - // c.generated_lines[s] = true } fn (mut c C2V) gen(s string) { From 8e9426f4095ca622ed967c071ad3b6b9416c11d6 Mon Sep 17 00:00:00 2001 From: Artem Yurchenko Date: Fri, 8 Sep 2023 15:00:25 +0200 Subject: [PATCH 4/8] Implement ignore of visibility attribute --- src/c2v.v | 2 +- tests/19.header_visibility_attribute.h | 5 +++++ tests/19.header_visibility_attribute.out | 8 ++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/19.header_visibility_attribute.h create mode 100644 tests/19.header_visibility_attribute.out diff --git a/src/c2v.v b/src/c2v.v index a847bb4..e942865 100644 --- a/src/c2v.v +++ b/src/c2v.v @@ -1671,7 +1671,7 @@ fn (c &C2V) enum_val_to_enum_name(enum_val string) string { fn (mut c C2V) expr(_node &Node) string { mut node := unsafe { _node } // Just gen a number - if node.kindof(.null) { + if node.kindof(.null) || node.kindof(.visibility_attr) { return '' } if node.kindof(.integer_literal) { diff --git a/tests/19.header_visibility_attribute.h b/tests/19.header_visibility_attribute.h new file mode 100644 index 0000000..a737319 --- /dev/null +++ b/tests/19.header_visibility_attribute.h @@ -0,0 +1,5 @@ +#include + +#define TS_CAPI __attribute__ ((visibility("default"))) + +TS_CAPI uint16_t ts_f32tof16(float f); diff --git a/tests/19.header_visibility_attribute.out b/tests/19.header_visibility_attribute.out new file mode 100644 index 0000000..a343963 --- /dev/null +++ b/tests/19.header_visibility_attribute.out @@ -0,0 +1,8 @@ +[translated] +module tests + +fn C.ts_f32tof16(f f32) u16 + +pub fn ts_f32tof16(f f32) u16 { + return C.ts_f32tof16(f) +} From 5ff63a772560c9f05e33246a5ce6a94fc60c1cd0 Mon Sep 17 00:00:00 2001 From: Artem Yurchenko Date: Fri, 8 Sep 2023 15:37:18 +0200 Subject: [PATCH 5/8] Implement typedef duplication handling --- src/c2v.v | 9 +++++++++ tests/20.header_duplicate_type_declaration.h | 5 +++++ tests/20.header_duplicate_type_declaration.out | 4 ++++ tests/IGNORE_header_duplicate_type_declaration_2.h | 3 +++ tests/run_tests.vsh | 3 ++- 5 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/20.header_duplicate_type_declaration.h create mode 100644 tests/20.header_duplicate_type_declaration.out create mode 100644 tests/IGNORE_header_duplicate_type_declaration_2.h diff --git a/src/c2v.v b/src/c2v.v index cad1149..bef4ae2 100644 --- a/src/c2v.v +++ b/src/c2v.v @@ -107,6 +107,7 @@ mut: translation_start_ticks i64 // initialised before the loop calling .translate_file() has_cfile bool returning_bool bool + already_declared_types map[string]bool // to avoid duplicate type declarations } fn empty_toml_doc() toml.Doc { @@ -139,6 +140,14 @@ fn add_place_data_to_error(err IError) string { } fn (mut c C2V) genln(s string) { + if s.starts_with('type ') { + if s in c.already_declared_types { + return + } + + c.already_declared_types[s] = true + } + if c.indent > 0 && c.out_line_empty { c.out.write_string(tabs[c.indent]) } diff --git a/tests/20.header_duplicate_type_declaration.h b/tests/20.header_duplicate_type_declaration.h new file mode 100644 index 0000000..ea1dba8 --- /dev/null +++ b/tests/20.header_duplicate_type_declaration.h @@ -0,0 +1,5 @@ +#pragma once + +#include "IGNORE_header_duplicate_type_declaration_2.h" + +typedef int dup_typ; diff --git a/tests/20.header_duplicate_type_declaration.out b/tests/20.header_duplicate_type_declaration.out new file mode 100644 index 0000000..a59de64 --- /dev/null +++ b/tests/20.header_duplicate_type_declaration.out @@ -0,0 +1,4 @@ +[translated] +module tests + +type Dup_typ = int diff --git a/tests/IGNORE_header_duplicate_type_declaration_2.h b/tests/IGNORE_header_duplicate_type_declaration_2.h new file mode 100644 index 0000000..185bc4f --- /dev/null +++ b/tests/IGNORE_header_duplicate_type_declaration_2.h @@ -0,0 +1,3 @@ +#pragma once + +typedef int dup_typ; diff --git a/tests/run_tests.vsh b/tests/run_tests.vsh index 72965b1..78b27b1 100644 --- a/tests/run_tests.vsh +++ b/tests/run_tests.vsh @@ -56,7 +56,8 @@ fn start_testing_process(filter string, tests_dir string, c2v_dir string) { } fn run_tests(test_file_extension string, c2v_command string, filter string, tests_dir string, c2v_dir string) bool { - mut files := get_test_files(tests_dir, test_file_extension) + mut files := get_test_files(tests_dir, test_file_extension).filter(it.all_after_last('/').starts_with('IGNORE_') == false) + files.sort() current_platform := os.user_os() From 8f6815e26bdfdec39eb527810d7ff95c968d6153 Mon Sep 17 00:00:00 2001 From: Artem Yurchenko Date: Fri, 8 Sep 2023 15:42:12 +0200 Subject: [PATCH 6/8] Conversion of argv to os.argv was removed --- src/c2v.v | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/c2v.v b/src/c2v.v index bef4ae2..aeb3d34 100644 --- a/src/c2v.v +++ b/src/c2v.v @@ -2101,9 +2101,6 @@ fn filter_name(name string) string { if name in builtin_fn_names { return 'C.' + name } - if name == 'argv' { - return 'os.argv' - } if name == 'FILE' { return 'C.FILE' } From 3e93c6492c7500526e07712bb0caeb3f8fb18b3c Mon Sep 17 00:00:00 2001 From: Artem Yurchenko Date: Sat, 9 Sep 2023 15:48:36 +0200 Subject: [PATCH 7/8] Improve handling of duplicated typedefs --- src/c2v.v | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/c2v.v b/src/c2v.v index bef4ae2..0e10ea7 100644 --- a/src/c2v.v +++ b/src/c2v.v @@ -107,7 +107,6 @@ mut: translation_start_ticks i64 // initialised before the loop calling .translate_file() has_cfile bool returning_bool bool - already_declared_types map[string]bool // to avoid duplicate type declarations } fn empty_toml_doc() toml.Doc { @@ -140,14 +139,6 @@ fn add_place_data_to_error(err IError) string { } fn (mut c C2V) genln(s string) { - if s.starts_with('type ') { - if s in c.already_declared_types { - return - } - - c.already_declared_types[s] = true - } - if c.indent > 0 && c.out_line_empty { c.out.write_string(tabs[c.indent]) } @@ -837,6 +828,13 @@ fn (mut c C2V) typedef_decl(node &Node) { return } + if alias_name in c.types || alias_name in c.enums { + // This means that this is a struct/enum typedef that has already been defined. + return + } + + c.types << alias_name + if typ.starts_with('struct ') && typ.ends_with(' *') { // Opaque pointer, for example: typedef struct TSTexture_t *TSTexture; c.genln('type ${alias_name} = voidptr') @@ -858,14 +856,10 @@ fn (mut c C2V) typedef_decl(node &Node) { // Skip internal stuff like __builtin_ms_va_list return } - if alias_name in c.types || alias_name in c.enums { - // This means that this is a struct/enum typedef that has already been defined. - return - } if typ in c.enums { return } - c.types << alias_name + mut cgen_alias := typ if cgen_alias.starts_with('_') { cgen_alias = trim_underscores(typ) From aba3d082dd09c1dc89f4bfd6e69d818cbf7fc3db Mon Sep 17 00:00:00 2001 From: Artem Yurchenko Date: Sat, 9 Sep 2023 16:33:53 +0200 Subject: [PATCH 8/8] Fixed generation of uninited global vars --- src/c2v.v | 8 +++++++- tests/21.header_uninited_global_var.h | 3 +++ tests/21.header_uninited_global_var.out | 2 ++ 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 tests/21.header_uninited_global_var.h create mode 100644 tests/21.header_uninited_global_var.out diff --git a/src/c2v.v b/src/c2v.v index ea85bc1..cd7be2d 100644 --- a/src/c2v.v +++ b/src/c2v.v @@ -1526,7 +1526,13 @@ fn (mut c C2V) var_decl(mut decl_stmt Node) { fn (mut c C2V) global_var_decl(mut var_decl Node) { // if the global has children, that means it's initialized, parse the expression - is_inited := var_decl.inner.len > 0 + mut is_inited := var_decl.inner.len > 0 + + if var_decl.inner.len == 1 { + if var_decl.inner[0].kindof(.visibility_attr) { + is_inited = false + } + } vprintln('\nglobal name=${var_decl.name} typ=${var_decl.ast_type.qualified}') vprintln(var_decl.str()) diff --git a/tests/21.header_uninited_global_var.h b/tests/21.header_uninited_global_var.h new file mode 100644 index 0000000..c72c67b --- /dev/null +++ b/tests/21.header_uninited_global_var.h @@ -0,0 +1,3 @@ +#define TS_CAPI __attribute__ ((visibility("default"))) + +TS_CAPI extern const char* tsMeshMaterialTypeBump; diff --git a/tests/21.header_uninited_global_var.out b/tests/21.header_uninited_global_var.out new file mode 100644 index 0000000..3494340 --- /dev/null +++ b/tests/21.header_uninited_global_var.out @@ -0,0 +1,2 @@ +[translated] +module tests