Skip to content

Commit b876644

Browse files
authored
parser,ast,checker,cgen: use enum comparisons instead of string ones on ast.ComptimeCall.method_name (#25003)
1 parent cd94cff commit b876644

14 files changed

Lines changed: 88 additions & 69 deletions

File tree

cmd/tools/vast/vast.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,13 +1072,13 @@ fn (t Tree) comptime_call(node ast.ComptimeCall) &Node {
10721072
mut obj := create_object()
10731073
obj.add_terse('ast_type', t.string_node('ComptimeCall'))
10741074
obj.add_terse('method_name', t.string_node(node.method_name))
1075+
obj.add_terse('kind', t.enum_node(node.kind))
10751076
obj.add_terse('left', t.expr(node.left))
10761077
obj.add_terse('is_vweb', t.bool_node(node.is_vweb))
10771078
obj.add_terse('is_veb', t.bool_node(node.is_veb))
10781079
obj.add_terse('veb_tmpl', t.string_node(node.veb_tmpl.path))
10791080
obj.add_terse('args_var', t.string_node(node.args_var))
10801081
obj.add_terse('has_parens', t.bool_node(node.has_parens))
1081-
obj.add_terse('is_embed', t.bool_node(node.is_embed))
10821082
obj.add_terse('embed_file', t.embed_file(node.embed_file))
10831083
obj.add('method_pos', t.pos(node.method_pos))
10841084
obj.add_terse('left_type', t.type_node(node.left_type))

vlib/v/ast/ast.v

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,21 +2050,32 @@ pub mut:
20502050
typ_key string // `f.typ` cached key for type resolver
20512051
}
20522052

2053+
pub enum ComptimeCallKind {
2054+
unknown
2055+
d
2056+
env
2057+
res
2058+
html
2059+
tmpl
2060+
method
2061+
pkgconfig
2062+
embed_file
2063+
compile_warn
2064+
compile_error
2065+
}
2066+
20532067
@[minify]
20542068
pub struct ComptimeCall {
20552069
pub:
2056-
pos token.Pos
2057-
has_parens bool // if $() is used, for vfmt
2058-
method_name string
2059-
method_pos token.Pos
2060-
scope &Scope = unsafe { nil }
2061-
is_vweb bool
2062-
is_veb bool
2063-
is_embed bool // $embed_file(...)
2064-
is_env bool // $env(...) // TODO: deprecate after $d() is stable
2065-
is_compile_value bool // $d(...)
2066-
env_pos token.Pos
2067-
is_pkgconfig bool
2070+
pos token.Pos
2071+
has_parens bool // if $() is used, for vfmt
2072+
method_name string
2073+
kind ComptimeCallKind
2074+
method_pos token.Pos
2075+
scope &Scope = unsafe { nil }
2076+
is_vweb bool
2077+
is_veb bool
2078+
env_pos token.Pos
20682079
mut:
20692080
is_d_resolved bool
20702081
pub mut:
@@ -2087,7 +2098,7 @@ pub fn (mut cc ComptimeCall) resolve_compile_value(compile_values map[string]str
20872098
if cc.is_d_resolved {
20882099
return
20892100
}
2090-
if !cc.is_compile_value {
2101+
if cc.kind != .d {
20912102
return error('ComptimeCall is not \$d()')
20922103
}
20932104
arg := cc.args[0] or {
@@ -2109,7 +2120,7 @@ pub fn (mut cc ComptimeCall) resolve_compile_value(compile_values map[string]str
21092120
// `ast.Expr`'s `str()' method (used by e.g. vfmt).
21102121
pub fn (cc ComptimeCall) expr_str() string {
21112122
mut str := 'ast.ComptimeCall'
2112-
if cc.is_compile_value {
2123+
if cc.kind == .d {
21132124
arg := cc.args[0] or { return str }
21142125
if arg.expr.is_pure_literal() {
21152126
str = "\$${cc.method_name}('${cc.args_var}', ${arg})"

vlib/v/checker/checker.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4669,7 +4669,7 @@ fn (mut c Checker) find_obj_definition(obj ast.ScopeObject) !ast.Expr {
46694669
if mut expr is ast.Ident {
46704670
return c.find_definition(expr)
46714671
}
4672-
if mut expr is ast.ComptimeCall && expr.is_compile_value {
4672+
if mut expr is ast.ComptimeCall && expr.kind == .d {
46734673
if expr.result_type == ast.bool_type {
46744674
return ast.BoolLiteral{
46754675
val: expr.compile_value.bool()

vlib/v/checker/comptime.v

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,29 @@ fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type {
1515
if node.left !is ast.EmptyExpr {
1616
node.left_type = c.expr(mut node.left)
1717
}
18-
if node.method_name == 'compile_error' {
18+
if node.kind == .compile_error {
1919
c.error(c.comptime_call_msg(node), node.pos)
2020
return ast.void_type
21-
} else if node.method_name == 'compile_warn' {
21+
} else if node.kind == .compile_warn {
2222
c.warn(c.comptime_call_msg(node), node.pos)
2323
return ast.void_type
2424
}
25-
if node.is_env {
25+
if node.kind == .env {
2626
env_value := util.resolve_env_value("\$env('${node.args_var}')", false) or {
2727
c.error(err.msg(), node.env_pos)
2828
return ast.string_type
2929
}
3030
node.env_value = env_value
3131
return ast.string_type
3232
}
33-
if node.is_compile_value {
33+
if node.kind == .d {
3434
node.resolve_compile_value(c.pref.compile_values) or {
3535
c.error(err.msg(), node.pos)
3636
return ast.void_type
3737
}
3838
return node.result_type
3939
}
40-
if node.is_embed {
40+
if node.kind == .embed_file {
4141
if node.args.len == 1 {
4242
embed_arg := node.args[0]
4343
mut raw_path := ''
@@ -111,7 +111,7 @@ fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type {
111111

112112
c.table.cur_fn = save_cur_fn
113113
}
114-
if node.method_name == 'html' {
114+
if node.kind == .html {
115115
ret_sym := c.table.sym(c.table.cur_fn.return_type)
116116
if ret_sym.cname !in ['veb__Result', 'vweb__Result', 'x__vweb__Result'] {
117117
ct_call := if node.is_veb { 'veb' } else { 'vweb' }
@@ -138,7 +138,7 @@ fn (mut c Checker) comptime_call(mut node ast.ComptimeCall) ast.Type {
138138
c.stmts_ending_with_expression(mut node.or_block.stmts, c.expected_or_type)
139139
return c.type_resolver.get_type(node)
140140
}
141-
if node.method_name == 'res' {
141+
if node.kind == .res {
142142
if !c.inside_defer {
143143
c.error('`res` can only be used in defer blocks', node.pos)
144144
return ast.void_type
@@ -1096,15 +1096,15 @@ fn (mut c Checker) comptime_if_cond(mut cond ast.Expr, pos token.Pos) ComptimeBr
10961096
}
10971097
}
10981098
ast.ComptimeCall {
1099-
if cond.is_pkgconfig {
1099+
if cond.kind == .pkgconfig {
11001100
mut m := pkgconfig.main([cond.args_var]) or {
11011101
c.error(err.msg(), cond.pos)
11021102
return .skip
11031103
}
11041104
m.run() or { return .skip }
11051105
return .eval
11061106
}
1107-
if cond.is_compile_value {
1107+
if cond.kind == .d {
11081108
t := c.expr(mut cond)
11091109
if t != ast.bool_type {
11101110
c.error('inside \$if, only \$d() expressions that return bool are allowed',

vlib/v/checker/containers.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ fn (mut c Checker) eval_array_fixed_sizes(mut size_expr ast.Expr, size int, elem
357357
fixed_size = size_expr.val.int()
358358
}
359359
ast.ComptimeCall {
360-
if size_expr.is_compile_value {
360+
if size_expr.kind == .d {
361361
size_expr.resolve_compile_value(c.pref.compile_values) or {
362362
c.error(err.msg(), size_expr.pos)
363363
}

vlib/v/checker/return.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ fn (mut c Checker) return_stmt(mut node ast.Return) {
208208
c.error('cannot use `${c.table.type_to_str(got_type)}` as ${c.error_type_name(exp_type)} in return argument',
209209
exprv.pos())
210210
}
211-
if exprv is ast.ComptimeCall && exprv.method_name == 'tmpl'
211+
if exprv is ast.ComptimeCall && exprv.kind == .tmpl
212212
&& c.table.final_sym(exp_type).kind != .string {
213213
c.error('cannot use `string` as type `${c.table.type_to_str(exp_type)}` in return argument',
214214
exprv.pos)
@@ -351,7 +351,7 @@ fn has_top_return(stmts []ast.Stmt) bool {
351351
return true
352352
}
353353
} else if stmt.expr is ast.ComptimeCall {
354-
if stmt.expr.method_name == 'compile_error' {
354+
if stmt.expr.kind == .compile_error {
355355
return true
356356
}
357357
} else if stmt.expr is ast.LockExpr {

vlib/v/checker/struct.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
244244
if sym.info is ast.ArrayFixed && field.typ == field.default_expr_typ {
245245
if sym.info.size_expr is ast.ComptimeCall {
246246
// field [$d('x' ,2)]int = [1 ,2]!
247-
if sym.info.size_expr.method_name == 'd' {
247+
if sym.info.size_expr.kind == .d {
248248
c.error('cannot initialize a fixed size array field that uses `\$d()` as size quantifier since the size may change via -d',
249249
field.default_expr.pos())
250250
}

vlib/v/fmt/fmt.v

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,7 +2247,7 @@ pub fn (mut f Fmt) chan_init(mut node ast.ChanInit) {
22472247

22482248
pub fn (mut f Fmt) comptime_call(node ast.ComptimeCall) {
22492249
if node.is_vweb {
2250-
if node.method_name == 'html' {
2250+
if node.kind == .html {
22512251
if node.args.len == 1 && node.args[0].expr is ast.StringLiteral {
22522252
if node.is_veb {
22532253
f.write('\$veb.html(')
@@ -2270,21 +2270,21 @@ pub fn (mut f Fmt) comptime_call(node ast.ComptimeCall) {
22702270
}
22712271
} else {
22722272
match true {
2273-
node.is_embed {
2273+
node.kind == .embed_file {
22742274
f.write('\$embed_file(')
22752275
f.expr(node.args[0].expr)
22762276
if node.embed_file.compression_type != 'none' {
22772277
f.write(', .${node.embed_file.compression_type}')
22782278
}
22792279
f.write(')')
22802280
}
2281-
node.is_env {
2281+
node.kind == .env {
22822282
f.write("\$env('${node.args_var}')")
22832283
}
2284-
node.is_pkgconfig {
2284+
node.kind == .pkgconfig {
22852285
f.write("\$pkgconfig('${node.args_var}')")
22862286
}
2287-
node.method_name in ['compile_error', 'compile_warn'] {
2287+
node.kind in [.compile_error, .compile_warn] {
22882288
if node.args.len == 0 {
22892289
if node.args_var.contains("'") {
22902290
f.write('\$${node.method_name}("${node.args_var}")')
@@ -2297,12 +2297,12 @@ pub fn (mut f Fmt) comptime_call(node ast.ComptimeCall) {
22972297
f.write(')')
22982298
}
22992299
}
2300-
node.method_name == 'd' {
2300+
node.kind == .d {
23012301
f.write("\$d('${node.args_var}', ")
23022302
f.expr(node.args[0].expr)
23032303
f.write(')')
23042304
}
2305-
node.method_name == 'res' {
2305+
node.kind == .res {
23062306
if node.args_var != '' {
23072307
f.write('\$res(${node.args_var})')
23082308
} else {

vlib/v/gen/c/comptime.v

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,19 @@ fn (mut g Gen) gen_comptime_selector(expr ast.ComptimeSelector) string {
4545
}
4646

4747
fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) {
48-
if node.is_embed {
48+
if node.kind == .embed_file {
4949
// $embed_file('/path/to/file')
5050
g.gen_embed_file_init(mut node)
5151
return
5252
}
53-
if node.method_name == 'env' {
53+
if node.kind == .env {
5454
// $env('ENV_VAR_NAME')
5555
// TODO: deprecate after support for $d() is stable
5656
val := util.cescaped_path(os.getenv(node.args_var))
5757
g.write('_S("${val}")')
5858
return
5959
}
60-
if node.method_name == 'd' {
60+
if node.kind == .d {
6161
// $d('some_string',<default value>), affected by `-d some_string=actual_value`
6262
val := util.cescaped_path(node.compile_value)
6363
if node.result_type == ast.string_type {
@@ -69,7 +69,7 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) {
6969
}
7070
return
7171
}
72-
if node.method_name == 'res' {
72+
if node.kind == .res {
7373
if node.args_var != '' {
7474
g.write('${g.defer_return_tmp_var}.arg${node.args_var}')
7575
return
@@ -79,7 +79,7 @@ fn (mut g Gen) comptime_call(mut node ast.ComptimeCall) {
7979
return
8080
}
8181
if node.is_vweb {
82-
is_html := node.method_name == 'html'
82+
is_html := node.kind == .html
8383
mut cur_line := ''
8484

8585
if !is_html {
@@ -799,11 +799,11 @@ fn (mut g Gen) comptime_if_cond(cond ast.Expr, pkg_exist bool) (bool, bool) {
799799
return true, false
800800
}
801801
ast.ComptimeCall {
802-
if cond.method_name == 'pkgconfig' {
802+
if cond.kind == .pkgconfig {
803803
g.write('${pkg_exist}')
804804
return true, false
805805
}
806-
if cond.method_name == 'd' {
806+
if cond.kind == .d {
807807
if cond.result_type == ast.bool_type {
808808
if cond.compile_value == 'true' {
809809
g.write('1')

vlib/v/gen/c/fn.v

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,7 @@ fn (mut g Gen) gen_to_str_method_call(node ast.CallExpr) bool {
13351335
g.gen_expr_to_string(left_node, rec_type)
13361336
return true
13371337
} else if left_node is ast.ComptimeCall {
1338-
if left_node.method_name == 'method' {
1338+
if left_node.kind == .method {
13391339
sym := g.table.sym(g.unwrap_generic(left_node.left_type))
13401340
if m := sym.find_method(g.comptime.comptime_for_method.name) {
13411341
rec_type = m.return_type
@@ -2069,7 +2069,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) {
20692069
typ = g.type_resolver.get_ct_type_or_default(expr.typ_key, typ)
20702070
}
20712071
} else if expr is ast.ComptimeCall {
2072-
if expr.method_name == 'method' {
2072+
if expr.kind == .method {
20732073
sym := g.table.sym(g.unwrap_generic(expr.left_type))
20742074
if m := sym.find_method(g.comptime.comptime_for_method.name) {
20752075
typ = m.return_type

0 commit comments

Comments
 (0)