mirror of
https://github.com/vlang/v.git
synced 2025-09-10 16:00:31 -04:00
v.comptime: cleanup, add get_type_or_default (#22780)
This commit is contained in:
parent
0eaa85725b
commit
59a27b83d4
@ -505,9 +505,7 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) {
|
|||||||
left.recursive_mapset_is_setter(true)
|
left.recursive_mapset_is_setter(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
if right is ast.Ident && c.comptime.is_comptime_var(right) {
|
right_type = c.comptime.get_type_or_default(right, right_type)
|
||||||
right_type = c.comptime.get_type(right)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if mut left is ast.InfixExpr {
|
if mut left is ast.InfixExpr {
|
||||||
c.error('cannot use infix expression on the left side of `${node.op}`',
|
c.error('cannot use infix expression on the left side of `${node.op}`',
|
||||||
|
@ -3803,16 +3803,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
|
|||||||
// second use
|
// second use
|
||||||
if node.kind in [.constant, .global, .variable] {
|
if node.kind in [.constant, .global, .variable] {
|
||||||
info := node.info as ast.IdentVar
|
info := node.info as ast.IdentVar
|
||||||
typ := if c.comptime.is_comptime_var(node) {
|
typ := c.comptime.get_type_or_default(node, info.typ)
|
||||||
ctype := c.comptime.get_type(node)
|
|
||||||
if ctype != ast.void_type {
|
|
||||||
ctype
|
|
||||||
} else {
|
|
||||||
info.typ
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
info.typ
|
|
||||||
}
|
|
||||||
// Got a var with type T, return current generic type
|
// Got a var with type T, return current generic type
|
||||||
if node.or_expr.kind != .absent {
|
if node.or_expr.kind != .absent {
|
||||||
if !typ.has_flag(.option) {
|
if !typ.has_flag(.option) {
|
||||||
|
@ -45,13 +45,8 @@ fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) ast.Type {
|
|||||||
c.inside_interface_deref = true
|
c.inside_interface_deref = true
|
||||||
for i, mut expr in node.exprs {
|
for i, mut expr in node.exprs {
|
||||||
mut ftyp := c.expr(mut expr)
|
mut ftyp := c.expr(mut expr)
|
||||||
ftyp = c.check_expr_option_or_result_call(expr, ftyp)
|
ftyp = c.comptime.get_type_or_default(expr, c.check_expr_option_or_result_call(expr,
|
||||||
if c.comptime.is_comptime_var(expr) {
|
ftyp))
|
||||||
ctyp := c.comptime.get_type(expr)
|
|
||||||
if ctyp != ast.void_type {
|
|
||||||
ftyp = ctyp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if ftyp == ast.void_type {
|
if ftyp == ast.void_type {
|
||||||
c.error('expression does not return a value', expr.pos())
|
c.error('expression does not return a value', expr.pos())
|
||||||
} else if ftyp == ast.char_type && ftyp.nr_muls() == 0 {
|
} else if ftyp == ast.char_type && ftyp.nr_muls() == 0 {
|
||||||
|
@ -80,6 +80,22 @@ pub fn (mut ct ComptimeInfo) get_expr_type(node ast.Expr) ast.Type {
|
|||||||
return ctyp
|
return ctyp
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// get_type_or_default retries the comptime value if the AST node is related to comptime otherwise default_typ is returned
|
||||||
|
pub fn (mut ct ComptimeInfo) get_type_or_default(node ast.Expr, default_typ ast.Type) ast.Type {
|
||||||
|
match node {
|
||||||
|
ast.Ident {
|
||||||
|
if ct.is_comptime_var(node) {
|
||||||
|
ctyp := ct.get_type(node)
|
||||||
|
return if ctyp != ast.void_type { ctyp } else { default_typ }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return default_typ
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return default_typ
|
||||||
|
}
|
||||||
|
|
||||||
// get_type retrieves the actual type from a comptime related ast node
|
// get_type retrieves the actual type from a comptime related ast node
|
||||||
@[inline]
|
@[inline]
|
||||||
pub fn (mut ct ComptimeInfo) get_type(node ast.Expr) ast.Type {
|
pub fn (mut ct ComptimeInfo) get_type(node ast.Expr) ast.Type {
|
||||||
|
@ -459,11 +459,7 @@ fn (mut g Gen) comptime_if(node ast.IfExpr) {
|
|||||||
fn (mut g Gen) get_expr_type(cond ast.Expr) ast.Type {
|
fn (mut g Gen) get_expr_type(cond ast.Expr) ast.Type {
|
||||||
match cond {
|
match cond {
|
||||||
ast.Ident {
|
ast.Ident {
|
||||||
return if g.comptime.is_comptime_var(cond) {
|
return g.unwrap_generic(g.comptime.get_type_or_default(cond, cond.obj.typ))
|
||||||
g.unwrap_generic(g.comptime.get_type(cond))
|
|
||||||
} else {
|
|
||||||
g.unwrap_generic(cond.obj.typ)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ast.TypeNode {
|
ast.TypeNode {
|
||||||
return g.unwrap_generic(cond.typ)
|
return g.unwrap_generic(cond.typ)
|
||||||
|
@ -2178,13 +2178,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
|
|||||||
defer {
|
defer {
|
||||||
g.inside_interface_deref = false
|
g.inside_interface_deref = false
|
||||||
}
|
}
|
||||||
mut typ := node.args[0].typ
|
mut typ := g.comptime.get_type_or_default(node.args[0].expr, node.args[0].typ)
|
||||||
if g.comptime.is_comptime_var(node.args[0].expr) {
|
|
||||||
ctyp := g.comptime.get_type(node.args[0].expr)
|
|
||||||
if ctyp != ast.void_type {
|
|
||||||
typ = ctyp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if typ == 0 {
|
if typ == 0 {
|
||||||
g.checker_bug('print arg.typ is 0', node.pos)
|
g.checker_bug('print arg.typ is 0', node.pos)
|
||||||
}
|
}
|
||||||
|
@ -795,11 +795,8 @@ fn (mut g Gen) infix_expr_in_optimization(left ast.Expr, left_type ast.Type, rig
|
|||||||
|
|
||||||
// infix_expr_is_op generates code for `is` and `!is`
|
// infix_expr_is_op generates code for `is` and `!is`
|
||||||
fn (mut g Gen) infix_expr_is_op(node ast.InfixExpr) {
|
fn (mut g Gen) infix_expr_is_op(node ast.InfixExpr) {
|
||||||
mut left_sym := if g.comptime.is_comptime_var(node.left) {
|
mut left_sym := g.table.sym(g.unwrap_generic(g.comptime.get_type_or_default(node.left,
|
||||||
g.table.sym(g.unwrap_generic(g.comptime.get_type(node.left)))
|
node.left_type)))
|
||||||
} else {
|
|
||||||
g.table.sym(node.left_type)
|
|
||||||
}
|
|
||||||
is_aggregate := left_sym.kind == .aggregate
|
is_aggregate := left_sym.kind == .aggregate
|
||||||
if is_aggregate {
|
if is_aggregate {
|
||||||
parent_left_type := (left_sym.info as ast.Aggregate).sum_type
|
parent_left_type := (left_sym.info as ast.Aggregate).sum_type
|
||||||
|
Loading…
x
Reference in New Issue
Block a user