ast: simplify mktyp() (#13028)

pull/13045/head
yuyi 2022-01-05 00:37:18 +08:00 committed by GitHub
parent 0f01236e52
commit 6c1ae4f689
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 27 additions and 28 deletions

View File

@ -1186,15 +1186,6 @@ pub fn (t &Table) value_type(typ Type) Type {
return void_type return void_type
} }
[inline]
pub fn (t &Table) mktyp(typ Type) Type {
match typ {
float_literal_type { return f64_type }
int_literal_type { return int_type }
else { return typ }
}
}
pub fn (mut t Table) register_fn_generic_types(fn_name string) { pub fn (mut t Table) register_fn_generic_types(fn_name string) {
t.fn_generic_types[fn_name] = [][]Type{} t.fn_generic_types[fn_name] = [][]Type{}
} }

View File

@ -497,6 +497,14 @@ pub fn merge_types(params ...[]Type) []Type {
return res return res
} }
pub fn mktyp(typ Type) Type {
match typ {
ast.float_literal_type { return ast.f64_type }
ast.int_literal_type { return ast.int_type }
else { return typ }
}
}
pub struct MultiReturn { pub struct MultiReturn {
pub mut: pub mut:
types []Type types []Type

View File

@ -146,9 +146,9 @@ pub fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
} }
} }
if right.is_auto_deref_var() { if right.is_auto_deref_var() {
left_type = c.table.mktyp(right_type.deref()) left_type = ast.mktyp(right_type.deref())
} else { } else {
left_type = c.table.mktyp(right_type) left_type = ast.mktyp(right_type)
} }
if left_type == ast.int_type { if left_type == ast.int_type {
if right is ast.IntegerLiteral { if right is ast.IntegerLiteral {

View File

@ -215,7 +215,7 @@ pub fn (mut c Checker) check_basic(got ast.Type, expected ast.Type) bool {
return true return true
} }
// sum type // sum type
if c.table.sumtype_has_variant(expected, c.table.mktyp(got), false) { if c.table.sumtype_has_variant(expected, ast.mktyp(got), false) {
return true return true
} }
// type alias // type alias
@ -737,7 +737,7 @@ pub fn (mut c Checker) infer_fn_generic_types(func ast.Fn, mut node ast.CallExpr
param_type_sym := c.table.sym(param.typ) param_type_sym := c.table.sym(param.typ)
if param.typ.has_flag(.generic) && param_type_sym.name == gt_name { if param.typ.has_flag(.generic) && param_type_sym.name == gt_name {
to_set = c.table.mktyp(arg.typ) to_set = ast.mktyp(arg.typ)
sym := c.table.sym(arg.typ) sym := c.table.sym(arg.typ)
if sym.info is ast.FnType { if sym.info is ast.FnType {
mut func_ := sym.info.func mut func_ := sym.info.func
@ -759,7 +759,7 @@ pub fn (mut c Checker) infer_fn_generic_types(func ast.Fn, mut node ast.CallExpr
} else if param.typ.has_flag(.generic) { } else if param.typ.has_flag(.generic) {
arg_sym := c.table.sym(arg.typ) arg_sym := c.table.sym(arg.typ)
if param.typ.has_flag(.variadic) { if param.typ.has_flag(.variadic) {
to_set = c.table.mktyp(arg.typ) to_set = ast.mktyp(arg.typ)
} else if arg_sym.kind == .array && param_type_sym.kind == .array { } else if arg_sym.kind == .array && param_type_sym.kind == .array {
mut arg_elem_info := arg_sym.info as ast.Array mut arg_elem_info := arg_sym.info as ast.Array
mut param_elem_info := param_type_sym.info as ast.Array mut param_elem_info := param_type_sym.info as ast.Array

View File

@ -729,8 +729,8 @@ pub fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
} else { } else {
right_type right_type
} }
left_name := c.table.type_to_str(c.table.mktyp(deref_left_type)) left_name := c.table.type_to_str(ast.mktyp(deref_left_type))
right_name := c.table.type_to_str(c.table.mktyp(deref_right_type)) right_name := c.table.type_to_str(ast.mktyp(deref_right_type))
if left_name != right_name { if left_name != right_name {
c.error('mismatched types `$left_name` and `$right_name`', left_right_pos) c.error('mismatched types `$left_name` and `$right_name`', left_right_pos)
} }
@ -1656,7 +1656,7 @@ pub fn (mut c Checker) const_decl(mut node ast.ConstDecl) {
typ = ast.u64_type typ = ast.u64_type
} }
} }
node.fields[i].typ = c.table.mktyp(typ) node.fields[i].typ = ast.mktyp(typ)
c.const_deps = [] c.const_deps = []
} }
} }
@ -1960,7 +1960,7 @@ fn (mut c Checker) global_decl(mut node ast.GlobalDecl) {
mut v := c.file.global_scope.find_global(field.name) or { mut v := c.file.global_scope.find_global(field.name) or {
panic('internal compiler error - could not find global in scope') panic('internal compiler error - could not find global in scope')
} }
v.typ = c.table.mktyp(field.typ) v.typ = ast.mktyp(field.typ)
} }
c.global_names << field.name c.global_names << field.name
} }

View File

@ -104,9 +104,9 @@ pub fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type {
// The first element's type // The first element's type
if i == 0 { if i == 0 {
if expr.is_auto_deref_var() { if expr.is_auto_deref_var() {
elem_type = c.table.mktyp(typ.deref()) elem_type = ast.mktyp(typ.deref())
} else { } else {
elem_type = c.table.mktyp(typ) elem_type = ast.mktyp(typ)
} }
c.expected_type = elem_type c.expected_type = elem_type
continue continue
@ -235,11 +235,11 @@ pub fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type {
val0_type = c.unwrap_generic(info.value_type) val0_type = c.unwrap_generic(info.value_type)
} else { } else {
// `{'age': 20}` // `{'age': 20}`
key0_type = c.table.mktyp(c.expr(node.keys[0])) key0_type = ast.mktyp(c.expr(node.keys[0]))
if node.keys[0].is_auto_deref_var() { if node.keys[0].is_auto_deref_var() {
key0_type = key0_type.deref() key0_type = key0_type.deref()
} }
val0_type = c.table.mktyp(c.expr(node.vals[0])) val0_type = ast.mktyp(c.expr(node.vals[0]))
if node.vals[0].is_auto_deref_var() { if node.vals[0].is_auto_deref_var() {
val0_type = val0_type.deref() val0_type = val0_type.deref()
} }
@ -255,13 +255,13 @@ pub fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type {
c.expected_type = val0_type c.expected_type = val0_type
val_type := c.expr(val) val_type := c.expr(val)
if !c.check_types(key_type, key0_type) || (i == 0 && key_type.is_number() if !c.check_types(key_type, key0_type) || (i == 0 && key_type.is_number()
&& key0_type.is_number() && key0_type != c.table.mktyp(key_type)) { && key0_type.is_number() && key0_type != ast.mktyp(key_type)) {
msg := c.expected_msg(key_type, key0_type) msg := c.expected_msg(key_type, key0_type)
c.error('invalid map key: $msg', key.position()) c.error('invalid map key: $msg', key.position())
same_key_type = false same_key_type = false
} }
if !c.check_types(val_type, val0_type) || (i == 0 && val_type.is_number() if !c.check_types(val_type, val0_type) || (i == 0 && val_type.is_number()
&& val0_type.is_number() && val0_type != c.table.mktyp(val_type)) { && val0_type.is_number() && val0_type != ast.mktyp(val_type)) {
msg := c.expected_msg(val_type, val0_type) msg := c.expected_msg(val_type, val0_type)
c.error('invalid map value: $msg', val.position()) c.error('invalid map value: $msg', val.position())
} }

View File

@ -11,7 +11,7 @@ pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
cond_type := c.expr(node.cond) cond_type := c.expr(node.cond)
// we setting this here rather than at the end of the method // we setting this here rather than at the end of the method
// since it is used in c.match_exprs() it saves checking twice // since it is used in c.match_exprs() it saves checking twice
node.cond_type = c.table.mktyp(cond_type) node.cond_type = ast.mktyp(cond_type)
c.ensure_type_exists(node.cond_type, node.pos) or { return ast.void_type } c.ensure_type_exists(node.cond_type, node.pos) or { return ast.void_type }
c.check_expr_opt_call(node.cond, cond_type) c.check_expr_opt_call(node.cond, cond_type)
cond_type_sym := c.table.sym(cond_type) cond_type_sym := c.table.sym(cond_type)

View File

@ -1977,7 +1977,7 @@ fn (mut g Gen) for_in_stmt(node ast.ForInStmt) {
if node.is_range { if node.is_range {
// `for x in 1..10 {` // `for x in 1..10 {`
i := if node.val_var == '_' { g.new_tmp_var() } else { c_name(node.val_var) } i := if node.val_var == '_' { g.new_tmp_var() } else { c_name(node.val_var) }
val_typ := g.table.mktyp(node.val_type) val_typ := ast.mktyp(node.val_type)
g.write('for (${g.typ(val_typ)} $i = ') g.write('for (${g.typ(val_typ)} $i = ')
g.expr(node.cond) g.expr(node.cond)
g.write('; $i < ') g.write('; $i < ')
@ -2321,7 +2321,7 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp_is_ptr
// use instead of expr() when you need to cast to a different type // use instead of expr() when you need to cast to a different type
fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_type ast.Type) { fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_type ast.Type) {
got_type := g.table.mktyp(got_type_raw) got_type := ast.mktyp(got_type_raw)
exp_sym := g.table.sym(expected_type) exp_sym := g.table.sym(expected_type)
got_sym := g.table.sym(got_type) got_sym := g.table.sym(got_type)
expected_is_ptr := expected_type.is_ptr() expected_is_ptr := expected_type.is_ptr()
@ -4188,7 +4188,7 @@ fn (mut g Gen) select_expr(node ast.SelectExpr) {
objs << ast.empty_expr() objs << ast.empty_expr()
tmp_obj := g.new_tmp_var() tmp_obj := g.new_tmp_var()
tmp_objs << tmp_obj tmp_objs << tmp_obj
el_stype := g.typ(g.table.mktyp(expr.right_type)) el_stype := g.typ(ast.mktyp(expr.right_type))
g.writeln('$el_stype $tmp_obj;') g.writeln('$el_stype $tmp_obj;')
} }
is_push << true is_push << true