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

cgen: scope $for fields/methods independently (#9017)

This commit is contained in:
spaceface 2021-03-01 01:01:56 +01:00 committed by GitHub
parent b712af56fd
commit 15896beace
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 8 deletions

View File

@ -298,7 +298,8 @@ fn (mut g Gen) comp_if_cond(cond ast.Expr) bool {
fn (mut g Gen) comp_for(node ast.CompFor) {
sym := g.table.get_type_symbol(g.unwrap_generic(node.typ))
g.writeln('{ // 2comptime: \$for $node.val_var in ${sym.name}($node.kind.str()) {')
g.writeln('/* \$for $node.val_var in ${sym.name}($node.kind.str()) */ {')
g.indent++
// vweb_result_type := table.new_type(g.table.find_type_idx('vweb.Result'))
mut i := 0
// g.writeln('string method = _SLIT("");')
@ -307,7 +308,7 @@ fn (mut g Gen) comp_for(node ast.CompFor) {
methods_with_attrs := sym.methods.filter(it.attrs.len > 0) // methods with attrs second
methods << methods_with_attrs
if methods.len > 0 {
g.writeln('\tFunctionData $node.val_var = {0};')
g.writeln('FunctionData $node.val_var = {0};')
}
for method in methods { // sym.methods {
/*
@ -316,7 +317,7 @@ fn (mut g Gen) comp_for(node ast.CompFor) {
}
*/
g.comp_for_method = method.name
g.writeln('\t// method $i')
g.writeln('/* method $i */ {')
g.writeln('\t${node.val_var}.name = _SLIT("$method.name");')
if method.attrs.len == 0 {
g.writeln('\t${node.val_var}.attrs = __new_array_with_default(0, 0, sizeof(string), 0);')
@ -366,7 +367,7 @@ fn (mut g Gen) comp_for(node ast.CompFor) {
g.comptime_var_type_map['${node.val_var}.typ'] = styp
g.stmts(node.stmts)
i++
g.writeln('')
g.writeln('}')
for key, _ in g.comptime_var_type_map {
if key.starts_with(node.val_var) {
g.comptime_var_type_map.delete(key)
@ -385,7 +386,7 @@ fn (mut g Gen) comp_for(node ast.CompFor) {
for field in fields {
g.comp_for_field_var = node.val_var
g.comp_for_field_value = field
g.writeln('\t// field $i')
g.writeln('/* field $i */ {')
g.writeln('\t${node.val_var}.name = _SLIT("$field.name");')
if field.attrs.len == 0 {
g.writeln('\t${node.val_var}.attrs = __new_array_with_default(0, 0, sizeof(string), 0);')
@ -404,12 +405,13 @@ fn (mut g Gen) comp_for(node ast.CompFor) {
g.comptime_var_type_map['${node.val_var}.typ'] = styp
g.stmts(node.stmts)
i++
g.writeln('')
g.writeln('}')
}
g.comptime_var_type_map.delete(node.val_var)
}
}
g.writeln('} // } comptime for')
g.indent--
g.writeln('}// \$for')
}
fn (mut g Gen) comp_if_to_ifdef(name string, is_comptime_optional bool) ?string {

View File

@ -39,7 +39,9 @@ fn test_comptime_for() {
println(@FN)
methods := ['run', 'method2', 'int_method1', 'int_method2', 'string_arg']
$for method in App.methods {
println(' method: $method.name | ' + no_lines('$method'))
// ensure each method is scoped under a new block in the generated code
x := ' method: $method.name | ' + no_lines('$method')
println(x)
assert method.name in methods
}
}