From 70865474719bd983e4e5414095fc4d0ce818c138 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 23 Aug 2019 02:38:18 +0300 Subject: [PATCH] compiler: replace STRUCT_DEFAULT_VALUE with {0} --- compiler/cheaders.v | 5 +---- compiler/parser.v | 14 ++++---------- compiler/table.v | 4 ++-- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/compiler/cheaders.v b/compiler/cheaders.v index ae0fc52219..c340d071b2 100644 --- a/compiler/cheaders.v +++ b/compiler/cheaders.v @@ -11,7 +11,6 @@ CommonCHeaders = ' #include // int64_t etc #include // memcpy -#define STRUCT_DEFAULT_VALUE {} #define EMPTY_STRUCT_DECLARATION #define OPTION_CAST(x) (x) @@ -30,9 +29,7 @@ CommonCHeaders = ' // On MSVC these are the same (as long as /volatile:ms is passed) #define _Atomic volatile -// MSVC can\'t parse some things properly -#undef STRUCT_DEFAULT_VALUE -#define STRUCT_DEFAULT_VALUE {0} +// MSVC cannot parse some things properly #undef EMPTY_STRUCT_DECLARATION #define EMPTY_STRUCT_DECLARATION void *____dummy_variable; #undef OPTION_CAST diff --git a/compiler/parser.v b/compiler/parser.v index 8a7d2a15be..eb0eefcfed 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -2057,10 +2057,7 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string { tmp_ok := p.get_tmp() if is_map { p.gen('$tmp') - mut def := type_default(typ) - if def == 'STRUCT_DEFAULT_VALUE' { - def = '{0}' - } + def := type_default(typ) p.cgen.insert_before('$typ $tmp = $def; bool $tmp_ok = map_get($index_expr, & $tmp);') } else if is_arr { @@ -2689,7 +2686,7 @@ fn (p mut Parser) array_init() string { name := p.check_name() if p.table.known_type(name) { p.cgen.resetln('') - p.gen('STRUCT_DEFAULT_VALUE') + p.gen('{0}') if is_const_len { return '[${p.mod}__$lit]$name' } @@ -2887,7 +2884,7 @@ fn (p mut Parser) struct_init(typ string, is_c_struct_init bool) string { continue } def_val := type_default(field_typ) - if def_val != '' && def_val != 'STRUCT_DEFAULT_VALUE' { + if def_val != '' && def_val != '{0}' { p.gen('.$field.name = $def_val') if i != t.fields.len - 1 { p.gen(',') @@ -3185,10 +3182,7 @@ fn (p mut Parser) for_st() { p.genln('for (int l = 0; l < keys_$tmp .len; l++) {') p.genln(' string $i = ((string*)keys_$tmp .data)[l];') //p.genln(' string $i = *(string*) ( array__get(keys_$tmp, l) );') - mut def := type_default(typ) - if def == 'STRUCT_DEFAULT_VALUE' { - def = '{0}' - } + def := type_default(typ) // TODO don't call map_get() for each key, fetch values while traversing // the tree (replace `map_keys()` above with `map_key_vals()`) p.genln('$var_typ $val = $def; map_get($tmp, $i, & $val);') diff --git a/compiler/table.v b/compiler/table.v index 594e1db028..a6895e8d74 100644 --- a/compiler/table.v +++ b/compiler/table.v @@ -603,7 +603,7 @@ fn type_default(typ string) string { } // User struct defined in another module. if typ.contains('__') { - return 'STRUCT_DEFAULT_VALUE' + return '{0}' } // Default values for other types are not needed because of mandatory initialization switch typ { @@ -625,7 +625,7 @@ fn type_default(typ string) string { case 'byteptr': return '0' case 'voidptr': return '0' } - return 'STRUCT_DEFAULT_VALUE' + return '{0}' } // TODO PERF O(n)