mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
compiler: map[string]pointer, ?pointer, fix []pointer
This commit is contained in:
committed by
Alexander Medvednikov
parent
b76227b781
commit
28ecfb231d
@@ -1019,7 +1019,7 @@ fn (p mut Parser) get_type() string {
|
||||
}
|
||||
p.check(.rsbr)
|
||||
val_type := p.get_type() // p.check_name()
|
||||
typ = 'map_$val_type'
|
||||
typ = 'map_${stringify_pointer(val_type)}'
|
||||
p.register_map(typ)
|
||||
return typ
|
||||
}
|
||||
@@ -1123,13 +1123,13 @@ fn (p mut Parser) get_type() string {
|
||||
// p.log('ARR TYPE="$typ" run=$p.pass')
|
||||
// We come across "[]User" etc ?
|
||||
for i := 0; i < arr_level; i++ {
|
||||
typ = 'array_$typ'
|
||||
typ = 'array_${stringify_pointer(typ)}'
|
||||
}
|
||||
p.register_array(typ)
|
||||
}
|
||||
p.next()
|
||||
if is_question {
|
||||
typ = 'Option_$typ'
|
||||
typ = 'Option_${stringify_pointer(typ)}'
|
||||
p.table.register_type_with_parent(typ, 'Option')
|
||||
}
|
||||
// Because the code uses * to see if it's a pointer
|
||||
@@ -1511,13 +1511,13 @@ fn ($v.name mut $v.typ) ${p.cur_fn.name}(...) {
|
||||
p.error_with_token_index('${fn_name}() $err_used_as_value', p.token_idx - 2)
|
||||
}
|
||||
// Allow `num = 4` where `num` is an `?int`
|
||||
if p.assigned_type.starts_with('Option_') && expr_type == p.assigned_type['Option_'.len..] {
|
||||
if p.assigned_type.starts_with('Option_') && expr_type == parse_pointer(p.assigned_type['Option_'.len..]) {
|
||||
expr := p.cgen.cur_line[pos..]
|
||||
left := p.cgen.cur_line[..pos]
|
||||
typ := expr_type.replace('Option_', '')
|
||||
typ := parse_pointer(expr_type.replace('Option_', ''))
|
||||
p.cgen.resetln(left + 'opt_ok(($typ[]){ $expr }, sizeof($typ))')
|
||||
}
|
||||
else if expr_type.starts_with('Option_') && p.assigned_type == expr_type['Option_'.len..] && p.tok == .key_orelse {
|
||||
else if expr_type.starts_with('Option_') && p.assigned_type == parse_pointer(expr_type['Option_'.len..]) && p.tok == .key_orelse {
|
||||
line := p.cgen.cur_line
|
||||
vname := line[..pos].replace('=', '') // TODO cgen line hack
|
||||
if idx:=line.index('='){
|
||||
@@ -1976,7 +1976,7 @@ fn (p mut Parser) dot(str_typ_ string, method_ph int) string {
|
||||
}
|
||||
if !typ.is_c && !p.is_c_fn_call && !has_field && !has_method && !p.first_pass() {
|
||||
if typ.name.starts_with('Option_') {
|
||||
opt_type := typ.name[7..]
|
||||
opt_type := typ.name[7..].replace('ptr_', '&')
|
||||
p.error('unhandled option type: `?$opt_type`')
|
||||
}
|
||||
// println('error in dot():')
|
||||
@@ -2064,7 +2064,7 @@ pub:
|
||||
method.typ = p.gen_handle_option_or_else(method.typ, '', method_ph)
|
||||
}
|
||||
else if !p.is_var_decl && !is_or_else && !p.inside_return_expr && method.typ.starts_with('Option_') {
|
||||
opt_type := method.typ[7..]
|
||||
opt_type := method.typ[7..].replace('ptr_', '&')
|
||||
p.error('unhandled option type: `?$opt_type`')
|
||||
}
|
||||
// Methods returning `array` should return `array_string` etc
|
||||
@@ -2073,7 +2073,7 @@ pub:
|
||||
}
|
||||
// Array methods returning `voidptr` (like `last()`) should return element type
|
||||
if method.typ == 'void*' && typ.name.starts_with('array_') {
|
||||
return typ.name[6..]
|
||||
return parse_pointer(typ.name[6..])
|
||||
}
|
||||
// if false && p.tok == .lsbr {
|
||||
// if is_indexer {
|
||||
@@ -2177,7 +2177,7 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string {
|
||||
}
|
||||
if is_arr {
|
||||
if is_arr0 {
|
||||
typ = typ[6..].replace('_ptr', '*')
|
||||
typ = parse_pointer(typ[6..])
|
||||
}
|
||||
p.gen_array_at(typ, is_arr0, fn_ph)
|
||||
}
|
||||
@@ -2187,6 +2187,7 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string {
|
||||
// can only do that later once we know whether there's an "=" or not
|
||||
if is_map {
|
||||
typ = typ.replace('map_', '')
|
||||
typ = parse_pointer(typ)
|
||||
if typ == 'map' {
|
||||
typ = 'void*'
|
||||
}
|
||||
@@ -2212,7 +2213,7 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string {
|
||||
}
|
||||
if p.tok == .dotdot {
|
||||
if is_arr {
|
||||
typ = 'array_' + typ
|
||||
typ = 'array_' + stringify_pointer(typ)
|
||||
}
|
||||
else if is_str {
|
||||
typ = 'string'
|
||||
@@ -2292,7 +2293,7 @@ fn (p mut Parser) index_expr(typ_ string, fn_ph int) string {
|
||||
// }
|
||||
// `m[key]`. no =, just a getter
|
||||
else if (is_map || is_arr || (is_str && !p.builtin_mod)) && is_indexer {
|
||||
typ = typ.replace('_ptr', '*')
|
||||
typ = parse_pointer(typ)
|
||||
p.index_get(typ, fn_ph, IndexConfig{
|
||||
is_arr: is_arr
|
||||
is_map: is_map
|
||||
@@ -2349,7 +2350,7 @@ fn (p mut Parser) indot_expr() string {
|
||||
if !is_arr && !is_map {
|
||||
p.error('`in` requires an array/map')
|
||||
}
|
||||
if is_arr && arr_typ[6..] != typ {
|
||||
if is_arr && parse_pointer(arr_typ[6..]) != typ {
|
||||
p.error('bad element type: `$typ` in `$arr_typ`')
|
||||
}
|
||||
if is_map && typ != 'string' {
|
||||
@@ -2471,7 +2472,7 @@ fn (p mut Parser) map_init() string {
|
||||
p.fgen_nl()
|
||||
}
|
||||
p.gen('new_map_init($i, sizeof($val_type), ' + '(string[$i]){ $keys_gen }, ($val_type [$i]){ $vals_gen } )')
|
||||
typ := 'map_$val_type'
|
||||
typ := 'map_${stringify_pointer(val_type)}'
|
||||
p.register_map(typ)
|
||||
return typ
|
||||
}
|
||||
@@ -2486,7 +2487,7 @@ fn (p mut Parser) map_init() string {
|
||||
// if !p.table.known_type(val_type) {
|
||||
// p.error('map init unknown type "$val_type"')
|
||||
// }
|
||||
typ := 'map_$val_type'
|
||||
typ := 'map_${stringify_pointer(val_type)}'
|
||||
p.register_map(typ)
|
||||
p.gen('new_map(1, sizeof($val_type))')
|
||||
if p.tok == .lcbr {
|
||||
@@ -2593,16 +2594,17 @@ fn (p mut Parser) array_init() string {
|
||||
p.check(.rsbr)
|
||||
// type after `]`? (e.g. "[]string")
|
||||
exp_array := p.expected_type.starts_with('array_')
|
||||
if p.tok != .name && p.tok != .mul && p.tok != .lsbr && i == 0 && !exp_array {
|
||||
if p.tok != .name && p.tok != .mul && p.tok != .lsbr && p.tok != .amp && i == 0 && !exp_array {
|
||||
p.error('specify array type: `[]typ` instead of `[]`')
|
||||
}
|
||||
if i == 0 && (p.tok == .name || p.tok == .mul) && p.tokens[p.token_idx - 2].line_nr == p.tokens[p.token_idx - 1].line_nr {
|
||||
if i == 0 && (p.tok == .name || p.tok == .mul || p.tok == .amp) && p.tokens[p.token_idx - 2].line_nr == p.tokens[p.token_idx - 1].line_nr {
|
||||
// TODO
|
||||
// vals.len == 0 {
|
||||
if exp_array {
|
||||
p.error('no need to specify the full array type here, use `[]` instead of `[]${p.expected_type[6..]}`')
|
||||
type_expected := p.expected_type[6..].replace('ptr_', '&')
|
||||
p.error('no need to specify the full array type here, use `[]` instead of `[]$type_expected`')
|
||||
}
|
||||
typ = p.get_type().replace('*', '_ptr')
|
||||
typ = p.get_type()
|
||||
}
|
||||
else if exp_array && i == 0 {
|
||||
// allow `known_array = []`
|
||||
@@ -2633,9 +2635,9 @@ fn (p mut Parser) array_init() string {
|
||||
// if ptr {
|
||||
// typ += '_ptr"
|
||||
// }
|
||||
real := typ.replace('_ptr', '*')
|
||||
real := parse_pointer(typ)
|
||||
p.gen_array_init(real, no_alloc, new_arr_ph, i)
|
||||
typ = 'array_$typ'
|
||||
typ = 'array_${stringify_pointer(typ)}'
|
||||
p.register_array(typ)
|
||||
return typ
|
||||
}
|
||||
@@ -2749,10 +2751,10 @@ fn (p mut Parser) return_st() {
|
||||
// Automatically wrap an object inside an option if the function
|
||||
// returns an option:
|
||||
// `return val` => `return opt_ok(val)`
|
||||
if p.cur_fn.typ.ends_with(expr_type) && !is_none && p.cur_fn.typ.starts_with('Option_') {
|
||||
if p.cur_fn.typ.ends_with(stringify_pointer(expr_type)) && !is_none && p.cur_fn.typ.starts_with('Option_') {
|
||||
tmp := p.get_tmp()
|
||||
ret := p.cgen.cur_line[ph..]
|
||||
typ := expr_type.replace('Option_', '')
|
||||
typ := parse_pointer(expr_type.replace('Option_', ''))
|
||||
p.cgen.resetln('$expr_type $tmp = OPTION_CAST($expr_type)($ret);')
|
||||
p.genln(deferred_text)
|
||||
p.gen('return opt_ok(&$tmp, sizeof($typ))')
|
||||
|
||||
Reference in New Issue
Block a user