builtin: cleanup option names (part 1) (#9293)
parent
5a6b321656
commit
9ed9e7aff0
|
@ -52,49 +52,38 @@ pub fn error_with_code3(message string, code int) IError {
|
|||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
// these are just here temporarily to avoid breaking the compiler; they will be removed soon
|
||||
pub fn error(a string) Option2 { return {} }
|
||||
pub fn error_with_code(a string, b int) Option2 { return {} }
|
||||
|
||||
// Option2 is the base of V's new internal optional return system.
|
||||
struct Option2 {
|
||||
state byte
|
||||
err Error
|
||||
// Data is trailing after err
|
||||
// and is not included in here but in the
|
||||
// derived Option2_xxx types
|
||||
}
|
||||
|
||||
// `fn foo() ?Foo { return foo }` => `fn foo() ?Foo { return opt_ok(foo); }`
|
||||
fn opt_ok(data voidptr, mut option Option2, size int) {
|
||||
unsafe {
|
||||
*option = Option2{}
|
||||
// use err to get the end of OptionBase and then memcpy into it
|
||||
C.memcpy(byteptr(&option.err) + sizeof(Error), data, size)
|
||||
}
|
||||
}
|
||||
|
||||
// error returns an optional containing the error given in `message`.
|
||||
// `if ouch { return error('an error occurred') }`
|
||||
pub fn error2(message string) Option2 {
|
||||
return Option2{
|
||||
state: 2
|
||||
err: {
|
||||
// error returns a default error instance containing the error given in `message`.
|
||||
// Example: `if ouch { return error('an error occurred') }`
|
||||
[inline]
|
||||
pub fn error(message string) IError {
|
||||
return &Error{
|
||||
msg: message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// error_with_code returns an optional containing both error `message` and error `code`.
|
||||
// error_with_code returns a default error instance containing the given `message` and error `code`.
|
||||
// `if ouch { return error_with_code('an error occurred', 1) }`
|
||||
pub fn error_with_code2(message string, code int) Option2 {
|
||||
return Option2{
|
||||
state: 2
|
||||
err: {
|
||||
[inline]
|
||||
pub fn error_with_code(message string, code int) IError {
|
||||
return &Error {
|
||||
msg: message
|
||||
code: code
|
||||
}
|
||||
}
|
||||
|
||||
// Option is the base of V's internal optional return system.
|
||||
struct Option {
|
||||
state byte
|
||||
err IError = none__
|
||||
// Data is trailing after err
|
||||
// and is not included in here but in the
|
||||
// derived Option_xxx types
|
||||
}
|
||||
|
||||
fn opt_ok(data voidptr, mut option Option, size int) {
|
||||
unsafe {
|
||||
*option = Option{}
|
||||
// use err to get the end of OptionBase and then memcpy into it
|
||||
C.memcpy(byteptr(&option.err) + sizeof(IError), data, size)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2422,9 +2422,9 @@ pub fn (mut c Checker) return_stmt(mut return_stmt ast.Return) {
|
|||
}
|
||||
}
|
||||
return_stmt.types = got_types
|
||||
// allow `none` & `error (Option)` return types for function that returns optional
|
||||
// allow `none` & `error` return types for function that returns optional
|
||||
if exp_is_optional
|
||||
&& got_types[0].idx() in [table.none_type_idx, table.error_type_idx, c.table.type_idxs['Option'], c.table.type_idxs['Option2'], c.table.type_idxs['Option3']] {
|
||||
&& got_types[0].idx() in [table.none_type_idx, table.error_type_idx, c.table.type_idxs['Option']] {
|
||||
return
|
||||
}
|
||||
if expected_types.len > 0 && expected_types.len != got_types.len {
|
||||
|
|
|
@ -569,7 +569,7 @@ fn (mut g Gen) expr_string(expr ast.Expr) string {
|
|||
// if one location changes
|
||||
fn (mut g Gen) optional_type_name(t table.Type) (string, string) {
|
||||
base := g.base_type(t)
|
||||
mut styp := 'Option3_$base'
|
||||
mut styp := 'Option_$base'
|
||||
if t.is_ptr() {
|
||||
styp = styp.replace('*', '_ptr')
|
||||
}
|
||||
|
@ -652,10 +652,10 @@ fn (mut g Gen) register_chan_pop_optional_call(opt_el_type string, styp string)
|
|||
if opt_el_type !in g.chan_pop_optionals {
|
||||
g.chan_pop_optionals << opt_el_type
|
||||
g.channel_definitions.writeln('
|
||||
static inline $opt_el_type __Option3_${styp}_popval($styp ch) {
|
||||
static inline $opt_el_type __Option_${styp}_popval($styp ch) {
|
||||
$opt_el_type _tmp = {0};
|
||||
if (sync__Channel_try_pop_priv(ch, _tmp.data, false)) {
|
||||
return ($opt_el_type){ .state = 2, .err = error3(_SLIT("channel closed")) };
|
||||
return ($opt_el_type){ .state = 2, .err = v_error(_SLIT("channel closed")) };
|
||||
}
|
||||
return _tmp;
|
||||
}')
|
||||
|
@ -666,11 +666,11 @@ fn (mut g Gen) register_chan_push_optional_call(el_type string, styp string) {
|
|||
if styp !in g.chan_push_optionals {
|
||||
g.chan_push_optionals << styp
|
||||
g.channel_definitions.writeln('
|
||||
static inline Option3_void __Option3_${styp}_pushval($styp ch, $el_type e) {
|
||||
static inline Option_void __Option_${styp}_pushval($styp ch, $el_type e) {
|
||||
if (sync__Channel_try_push_priv(ch, &e, false)) {
|
||||
return (Option3_void){ .state = 2, .err = error3(_SLIT("channel closed")) };
|
||||
return (Option_void){ .state = 2, .err = v_error(_SLIT("channel closed")) };
|
||||
}
|
||||
return (Option3_void){0};
|
||||
return (Option_void){0};
|
||||
}')
|
||||
}
|
||||
}
|
||||
|
@ -905,12 +905,12 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) {
|
|||
g.skip_stmt_pos = true
|
||||
if stmt is ast.ExprStmt {
|
||||
sym := g.table.get_type_symbol(stmt.typ)
|
||||
if sym.name in ['Option2', 'Option3'] || stmt.expr is ast.None {
|
||||
if sym.name in ['Option2', 'Option'] || stmt.expr is ast.None {
|
||||
tmp := g.new_tmp_var()
|
||||
g.write('Option3 $tmp = (Option3){.state = 0,.err = ')
|
||||
g.write('Option $tmp = (Option){.state = 0,.err = ')
|
||||
g.expr(stmt.expr)
|
||||
g.writeln('};')
|
||||
g.writeln('memcpy(&$tmp_var, &$tmp, sizeof(Option3));')
|
||||
g.writeln('memcpy(&$tmp_var, &$tmp, sizeof(Option));')
|
||||
} else {
|
||||
mut styp := g.base_type(stmt.typ)
|
||||
$if tinyc && x32 && windows {
|
||||
|
@ -920,9 +920,9 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) {
|
|||
styp = 'f64'
|
||||
}
|
||||
}
|
||||
g.write('opt_ok3(&($styp[]) { ')
|
||||
g.write('opt_ok(&($styp[]) { ')
|
||||
g.stmt(stmt)
|
||||
g.writeln(' }, (Option3*)(&$tmp_var), sizeof($styp));')
|
||||
g.writeln(' }, (Option*)(&$tmp_var), sizeof($styp));')
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -2847,7 +2847,7 @@ fn (mut g Gen) expr(node ast.Expr) {
|
|||
if gen_or {
|
||||
opt_elem_type := g.typ(elem_type.set_flag(.optional))
|
||||
g.register_chan_pop_optional_call(opt_elem_type, styp)
|
||||
g.write('$opt_elem_type $tmp_opt = __Option3_${styp}_popval(')
|
||||
g.write('$opt_elem_type $tmp_opt = __Option_${styp}_popval(')
|
||||
} else {
|
||||
g.write('__${styp}_popval(')
|
||||
}
|
||||
|
@ -3404,7 +3404,7 @@ fn (mut g Gen) infix_expr(node ast.InfixExpr) {
|
|||
if gen_or {
|
||||
elem_styp := g.typ(elem_type)
|
||||
g.register_chan_push_optional_call(elem_styp, styp)
|
||||
g.write('Option3_void $tmp_opt = __Option3_${styp}_pushval(')
|
||||
g.write('Option_void $tmp_opt = __Option_${styp}_pushval(')
|
||||
} else {
|
||||
g.write('__${styp}_pushval(')
|
||||
}
|
||||
|
@ -4336,7 +4336,7 @@ fn (mut g Gen) return_statement(node ast.Return) {
|
|||
if fn_return_is_optional {
|
||||
optional_none := node.exprs[0] is ast.None
|
||||
ftyp := g.typ(node.types[0])
|
||||
mut is_regular_option := ftyp in ['Option2', 'Option3']
|
||||
mut is_regular_option := ftyp in ['Option2', 'Option']
|
||||
if optional_none || is_regular_option {
|
||||
styp := g.typ(g.fn_decl.return_type)
|
||||
g.write('return ($styp){ .state=2, .err=')
|
||||
|
@ -4365,7 +4365,7 @@ fn (mut g Gen) return_statement(node ast.Return) {
|
|||
opt_tmp = g.new_tmp_var()
|
||||
g.writeln('$opt_type $opt_tmp;')
|
||||
styp = g.base_type(g.fn_decl.return_type)
|
||||
g.write('opt_ok3(&($styp/*X*/[]) { ')
|
||||
g.write('opt_ok(&($styp/*X*/[]) { ')
|
||||
} else {
|
||||
g.write('return ')
|
||||
styp = g.typ(g.fn_decl.return_type)
|
||||
|
@ -4423,7 +4423,7 @@ fn (mut g Gen) return_statement(node ast.Return) {
|
|||
}
|
||||
g.write('}')
|
||||
if fn_return_is_optional {
|
||||
g.writeln(' }, (Option3*)(&$opt_tmp), sizeof($styp));')
|
||||
g.writeln(' }, (Option*)(&$opt_tmp), sizeof($styp));')
|
||||
g.write('return $opt_tmp')
|
||||
}
|
||||
// Make sure to add our unpacks
|
||||
|
@ -4443,13 +4443,13 @@ fn (mut g Gen) return_statement(node ast.Return) {
|
|||
node.types[0].has_flag(.optional)
|
||||
}
|
||||
}
|
||||
if fn_return_is_optional && !expr_type_is_opt && return_sym.name !in ['Option2', 'Option3'] {
|
||||
if fn_return_is_optional && !expr_type_is_opt && return_sym.name !in ['Option2', 'Option'] {
|
||||
styp := g.base_type(g.fn_decl.return_type)
|
||||
opt_type := g.typ(g.fn_decl.return_type)
|
||||
// Create a tmp for this option
|
||||
opt_tmp := g.new_tmp_var()
|
||||
g.writeln('$opt_type $opt_tmp;')
|
||||
g.write('opt_ok3(&($styp[]) { ')
|
||||
g.write('opt_ok(&($styp[]) { ')
|
||||
if !g.fn_decl.return_type.is_ptr() && node.types[0].is_ptr() {
|
||||
if !(node.exprs[0] is ast.Ident && !g.is_amp) {
|
||||
g.write('*')
|
||||
|
@ -4461,7 +4461,7 @@ fn (mut g Gen) return_statement(node ast.Return) {
|
|||
g.write(', ')
|
||||
}
|
||||
}
|
||||
g.writeln(' }, (Option3*)(&$opt_tmp), sizeof($styp));')
|
||||
g.writeln(' }, (Option*)(&$opt_tmp), sizeof($styp));')
|
||||
g.writeln('return $opt_tmp;')
|
||||
return
|
||||
}
|
||||
|
@ -4569,7 +4569,7 @@ fn (mut g Gen) const_decl(node ast.ConstDecl) {
|
|||
}
|
||||
}
|
||||
ast.CallExpr {
|
||||
if val.starts_with('Option3_') {
|
||||
if val.starts_with('Option_') {
|
||||
g.inits[field.mod].writeln(val)
|
||||
unwrap_option := field.expr.or_block.kind != .absent
|
||||
g.const_decl_init_later(field.mod, name, g.current_tmp_var(), field.typ,
|
||||
|
@ -4986,7 +4986,7 @@ fn (mut g Gen) write_init_function() {
|
|||
}
|
||||
|
||||
const (
|
||||
builtins = ['string', 'array', 'DenseArray', 'map', 'Error', 'IError', 'Option2', 'Option3']
|
||||
builtins = ['string', 'array', 'DenseArray', 'map', 'Error', 'IError', 'Option2', 'Option']
|
||||
)
|
||||
|
||||
fn (mut g Gen) write_builtin_types() {
|
||||
|
@ -5318,7 +5318,7 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type table.
|
|||
styp := g.typ(g.fn_decl.return_type)
|
||||
err_obj := g.new_tmp_var()
|
||||
g.writeln('\t$styp $err_obj;')
|
||||
g.writeln('\tmemcpy(&$err_obj, &$cvar_name, sizeof(Option3));')
|
||||
g.writeln('\tmemcpy(&$err_obj, &$cvar_name, sizeof(Option));')
|
||||
g.writeln('\treturn $err_obj;')
|
||||
}
|
||||
}
|
||||
|
|
|
@ -719,12 +719,6 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
|
|||
}
|
||||
}
|
||||
mut name := node.name
|
||||
if node.name == 'error' {
|
||||
name = 'error3'
|
||||
}
|
||||
if node.name == 'error_with_code' {
|
||||
name = 'error_with_code3'
|
||||
}
|
||||
is_print := name in ['print', 'println', 'eprint', 'eprintln', 'panic']
|
||||
print_method := name
|
||||
is_json_encode := name == 'json.encode'
|
||||
|
@ -773,7 +767,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
|
|||
g.is_js_call = false
|
||||
g.writeln(');')
|
||||
tmp2 = g.new_tmp_var()
|
||||
g.writeln('Option3_$typ $tmp2 = $fn_name ($json_obj);')
|
||||
g.writeln('Option_$typ $tmp2 = $fn_name ($json_obj);')
|
||||
}
|
||||
if !g.is_autofree {
|
||||
g.write('cJSON_Delete($json_obj); //del')
|
||||
|
|
|
@ -247,7 +247,7 @@ fn (mut g Gen) index_of_array(node ast.IndexExpr, sym table.TypeSymbol) {
|
|||
g.writeln('if ($tmp_opt_ptr) {')
|
||||
g.writeln('\t*(($elem_type_str*)&${tmp_opt}.data) = *(($elem_type_str*)$tmp_opt_ptr);')
|
||||
g.writeln('} else {')
|
||||
g.writeln('\t${tmp_opt}.state = 2; ${tmp_opt}.err = error3(_SLIT("array index out of range"));')
|
||||
g.writeln('\t${tmp_opt}.state = 2; ${tmp_opt}.err = v_error(_SLIT("array index out of range"));')
|
||||
g.writeln('}')
|
||||
if !node.is_option {
|
||||
g.or_block(tmp_opt, node.or_expr, elem_type)
|
||||
|
@ -414,7 +414,7 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym table.TypeSymbol) {
|
|||
g.writeln('if ($tmp_opt_ptr) {')
|
||||
g.writeln('\t*(($elem_type_str*)&${tmp_opt}.data) = *(($elem_type_str*)$tmp_opt_ptr);')
|
||||
g.writeln('} else {')
|
||||
g.writeln('\t${tmp_opt}.state = 2; ${tmp_opt}.err = error3(_SLIT("array index out of range"));')
|
||||
g.writeln('\t${tmp_opt}.state = 2; ${tmp_opt}.err = v_error(_SLIT("array index out of range"));')
|
||||
g.writeln('}')
|
||||
if !node.is_option {
|
||||
g.or_block(tmp_opt, node.or_expr, elem_type)
|
||||
|
|
|
@ -41,7 +41,7 @@ fn (mut g Gen) gen_json_for_type(typ table.Type) {
|
|||
dec_fn_name := js_dec_name(styp)
|
||||
// Make sure that this optional type actually exists
|
||||
g.register_optional(utyp)
|
||||
dec_fn_dec := 'Option3_$styp ${dec_fn_name}(cJSON* root)'
|
||||
dec_fn_dec := 'Option_$styp ${dec_fn_name}(cJSON* root)'
|
||||
dec.writeln('
|
||||
$dec_fn_dec {
|
||||
$styp res;
|
||||
|
@ -50,7 +50,7 @@ $dec_fn_dec {
|
|||
if (error_ptr != NULL) {
|
||||
// fprintf(stderr, "Error in decode() for $styp error_ptr=: %s\\n", error_ptr);
|
||||
// printf("\\nbad js=%%s\\n", js.str);
|
||||
return (Option3_$styp){.state = 2,.err = error3(tos2(error_ptr))};
|
||||
return (Option_$styp){.state = 2,.err = v_error(tos2(error_ptr))};
|
||||
}
|
||||
}
|
||||
')
|
||||
|
@ -101,8 +101,8 @@ $enc_fn_dec {
|
|||
}
|
||||
// cJSON_delete
|
||||
// p.cgen.fns << '$dec return opt_ok(res); \n}'
|
||||
dec.writeln('\tOption3_$styp ret;')
|
||||
dec.writeln('\topt_ok3(&res, (Option3*)&ret, sizeof(res));')
|
||||
dec.writeln('\tOption_$styp ret;')
|
||||
dec.writeln('\topt_ok(&res, (Option*)&ret, sizeof(res));')
|
||||
dec.writeln('\treturn ret;\n}')
|
||||
enc.writeln('\treturn o;\n}')
|
||||
g.definitions.writeln(dec.str())
|
||||
|
@ -151,18 +151,18 @@ fn (mut g Gen) gen_struct_enc_dec(type_info table.TypeInfo, styp string, mut enc
|
|||
} else {
|
||||
g.gen_json_for_type(field.typ)
|
||||
tmp := g.new_tmp_var()
|
||||
dec.writeln('\tOption3_$field_type $tmp = $dec_name (js_get(root,"$name"));')
|
||||
dec.writeln('\tOption_$field_type $tmp = $dec_name (js_get(root,"$name"));')
|
||||
dec.writeln('\tif(${tmp}.state != 0) {')
|
||||
dec.writeln('\t\treturn *(Option3_$styp*) &$tmp;')
|
||||
dec.writeln('\t\treturn *(Option_$styp*) &$tmp;')
|
||||
dec.writeln('\t}')
|
||||
dec.writeln('\tres.${c_name(field.name)} = *($field_type*) ${tmp}.data;')
|
||||
}
|
||||
} else {
|
||||
// dec.writeln(' $dec_name (js_get(root, "$name"), & (res . $field.name));')
|
||||
tmp := g.new_tmp_var()
|
||||
dec.writeln('\tOption3_$field_type $tmp = $dec_name (js_get(root,"$name"));')
|
||||
dec.writeln('\tOption_$field_type $tmp = $dec_name (js_get(root,"$name"));')
|
||||
dec.writeln('\tif(${tmp}.state != 0) {')
|
||||
dec.writeln('\t\treturn *(Option3_$styp*) &$tmp;')
|
||||
dec.writeln('\t\treturn *(Option_$styp*) &$tmp;')
|
||||
dec.writeln('\t}')
|
||||
dec.writeln('\tres.${c_name(field.name)} = *($field_type*) ${tmp}.data;')
|
||||
}
|
||||
|
@ -214,17 +214,17 @@ fn (mut g Gen) decode_array(value_type table.Type) string {
|
|||
s = '$styp val = ${fn_name}(jsval); '
|
||||
} else {
|
||||
s = '
|
||||
Option3_$styp val2 = $fn_name (jsval);
|
||||
Option_$styp val2 = $fn_name (jsval);
|
||||
if(val2.state != 0) {
|
||||
array_free(&res);
|
||||
return *(Option3_Array_$styp*)&val2;
|
||||
return *(Option_Array_$styp*)&val2;
|
||||
}
|
||||
$styp val = *($styp*)val2.data;
|
||||
'
|
||||
}
|
||||
return '
|
||||
if(root && !cJSON_IsArray(root) && !cJSON_IsNull(root)) {
|
||||
return (Option3_Array_$styp){.state = 2, .err = error3(string_add(_SLIT("Json element is not an array: "), tos2(cJSON_PrintUnformatted(root))))};
|
||||
return (Option_Array_$styp){.state = 2, .err = v_error(string_add(_SLIT("Json element is not an array: "), tos2(cJSON_PrintUnformatted(root))))};
|
||||
}
|
||||
res = __new_array(0, 0, sizeof($styp));
|
||||
const cJSON *jsval = NULL;
|
||||
|
@ -258,17 +258,17 @@ fn (mut g Gen) decode_map(key_type table.Type, value_type table.Type) string {
|
|||
s = '$styp_v val = $fn_name_v (js_get(root, jsval->string));'
|
||||
} else {
|
||||
s = '
|
||||
Option3_$styp_v val2 = $fn_name_v (js_get(root, jsval->string));
|
||||
Option_$styp_v val2 = $fn_name_v (js_get(root, jsval->string));
|
||||
if(val2.state != 0) {
|
||||
map_free(&res);
|
||||
return *(Option3_Map_${styp}_$styp_v*)&val2;
|
||||
return *(Option_Map_${styp}_$styp_v*)&val2;
|
||||
}
|
||||
$styp_v val = *($styp_v*)val2.data;
|
||||
'
|
||||
}
|
||||
return '
|
||||
if(!cJSON_IsObject(root) && !cJSON_IsNull(root)) {
|
||||
return (Option3_Map_${styp}_$styp_v){ .state = 2, .err = error3( string_add(_SLIT("Json element is not an object: "), tos2(cJSON_PrintUnformatted(root))) )};
|
||||
return (Option_Map_${styp}_$styp_v){ .state = 2, .err = v_error( string_add(_SLIT("Json element is not an object: "), tos2(cJSON_PrintUnformatted(root))) )};
|
||||
}
|
||||
res = new_map_2(sizeof($styp), sizeof($styp_v), $hash_fn, $key_eq_fn, $clone_fn, $free_fn);
|
||||
cJSON *jsval = NULL;
|
||||
|
|
|
@ -30,8 +30,8 @@ pub fn mark_used(mut the_table table.Table, pref &pref.Preferences, ast_files []
|
|||
'tos2',
|
||||
'tos3',
|
||||
'isnil',
|
||||
'opt_ok3',
|
||||
'error3',
|
||||
'opt_ok',
|
||||
'error',
|
||||
// utf8_str_visible_length is used by c/str.v
|
||||
'utf8_str_visible_length',
|
||||
'compare_ints',
|
||||
|
|
Loading…
Reference in New Issue