Skip to content

Commit

Permalink
implemented feature
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Nov 8, 2024
1 parent 43d9cd6 commit 269e547
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ pub enum GenericKindField {
name
typ
unaliased_typ
indirections
}

// `foo.bar`
Expand Down
7 changes: 3 additions & 4 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1531,17 +1531,16 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
.name {
return ast.string_type
}
.unaliased_typ {
return ast.int_type
}
.typ {
.unaliased_typ, .typ, .indirections {
return ast.int_type
}
else {
if node.field_name == 'name' {
return ast.string_type
} else if node.field_name == 'idx' {
return ast.int_type
} else if node.field_name == 'indirections' {
return ast.int_type
}
c.error('invalid field `.${node.field_name}` for type `${node.expr}`',
node.pos)
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/checker/if.v
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,12 @@ fn (mut c Checker) if_expr(mut node ast.IfExpr) ast.Type {
}
}
}
} else if left.expr is ast.TypeOf {
skip_state = if left.expr.typ.nr_muls() == right.val.i64() {
ComptimeBranchSkipState.eval
} else {
ComptimeBranchSkipState.skip
}
}
} else if branch.cond.op in [.eq, .ne] && left is ast.SelectorExpr
&& right is ast.StringLiteral {
Expand Down
12 changes: 12 additions & 0 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -3875,6 +3875,10 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
g.write(int(g.table.unaliased_type(g.unwrap_generic(node.name_type))).str())
return
}
.indirections {
g.write(int(g.unwrap_generic(node.name_type).nr_muls()).str())
return
}
.unknown {
// ast.TypeOf of `typeof(string).idx` etc
if node.field_name == 'name' {
Expand All @@ -3893,6 +3897,14 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) {
// `typeof(expr).idx`
g.write(int(g.unwrap_generic(name_type)).str())
return
} else if node.field_name == 'indirections' {
mut name_type := node.name_type
if node.expr is ast.TypeOf {
name_type = g.resolve_comptime_type(node.expr.expr, name_type)
}
// `typeof(expr).indirections`
g.write(int(g.unwrap_generic(name_type).nr_muls()).str())
return
}
g.error('unknown generic field', node.pos)
}
Expand Down
2 changes: 2 additions & 0 deletions vlib/v/gen/c/comptime.v
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ fn (mut g Gen) get_expr_type(cond ast.Expr) ast.Type {
return g.unwrap_generic(cond.name_type)
} else if cond.gkind_field == .unaliased_typ {
return g.table.unaliased_type(g.unwrap_generic(cond.name_type))
} else if cond.gkind_field == .indirections {
return ast.int_type
} else {
name := '${cond.expr}.${cond.field_name}'
if name in g.comptime.type_map {
Expand Down
1 change: 1 addition & 0 deletions vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -2923,6 +2923,7 @@ fn (mut p Parser) name_expr() ast.Expr {
'name' { ast.GenericKindField.name }
'typ' { ast.GenericKindField.typ }
'unaliased_typ' { ast.GenericKindField.unaliased_typ }
'indirections' { ast.GenericKindField.indirections }
else { ast.GenericKindField.unknown }
}
pos.extend(p.tok.pos())
Expand Down
18 changes: 18 additions & 0 deletions vlib/v/tests/typeof_indirections_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fn indirections[T](val T) int {
return T.indirections
}

fn test_main() {
a := 0
assert typeof(a).indirections == 0
assert indirections(a) == 0
b := &a
assert typeof(b).indirections == 1
assert indirections(b) == 1
c := [1]
assert typeof(c).indirections == 0
assert indirections(c) == 0
d := &c
assert typeof(d).indirections == 1
assert indirections(d) == 1
}

0 comments on commit 269e547

Please sign in to comment.