1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: minor optimization of comptime (#5705)

This commit is contained in:
yuyi 2020-07-07 01:58:21 +08:00 committed by GitHub
parent ebb4935703
commit 43c8726c37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -78,25 +78,35 @@ fn (g &Gen) comptime_call(node ast.ComptimeCall) {
} }
fn (mut g Gen) comp_if(it ast.CompIf) { fn (mut g Gen) comp_if(it ast.CompIf) {
if it.stmts.len == 0 && it.else_stmts.len == 0 {
return
}
ifdef := g.comp_if_to_ifdef(it.val, it.is_opt) ifdef := g.comp_if_to_ifdef(it.val, it.is_opt)
g.empty_line = false
if it.is_not { if it.is_not {
g.writeln('\n// \$if !$it.val {\n#ifndef ' + ifdef) g.writeln('// \$if !$it.val {\n#ifndef ' + ifdef)
} else { } else {
g.writeln('\n// \$if $it.val {\n#ifdef ' + ifdef) g.writeln('// \$if $it.val {\n#ifdef ' + ifdef)
} }
// NOTE: g.defer_ifdef is needed for defers called witin an ifdef // NOTE: g.defer_ifdef is needed for defers called witin an ifdef
// in v1 this code would be completely excluded // in v1 this code would be completely excluded
g.defer_ifdef = if it.is_not { '\n#ifndef ' + ifdef } else { '\n#ifdef ' + ifdef } g.defer_ifdef = if it.is_not { '#ifndef ' + ifdef } else { '#ifdef ' + ifdef }
// println('comp if stmts $g.file.path:$it.pos.line_nr') // println('comp if stmts $g.file.path:$it.pos.line_nr')
g.indent--
g.stmts(it.stmts) g.stmts(it.stmts)
g.indent++
g.defer_ifdef = '' g.defer_ifdef = ''
if it.has_else { if it.has_else {
g.writeln('\n#else') g.empty_line = false
g.defer_ifdef = if it.is_not { '\n#ifdef ' + ifdef } else { '\n#ifndef ' + ifdef } g.writeln('#else')
g.defer_ifdef = if it.is_not { '#ifdef ' + ifdef } else { '#ifndef ' + ifdef }
g.indent--
g.stmts(it.else_stmts) g.stmts(it.else_stmts)
g.indent++
g.defer_ifdef = '' g.defer_ifdef = ''
} }
g.writeln('\n#endif\n// } $it.val\n') g.empty_line = false
g.writeln('#endif\n// } $it.val')
} }
fn (mut g Gen) comp_for(node ast.CompFor) { fn (mut g Gen) comp_for(node ast.CompFor) {