Skip to content

Commit a173f29

Browse files
authored
cgen: minor cleanup in stmt() (#17240)
1 parent 223c752 commit a173f29

File tree

1 file changed

+174
-170
lines changed

1 file changed

+174
-170
lines changed

vlib/v/gen/c/cgen.v

Lines changed: 174 additions & 170 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,70 +1909,11 @@ fn (mut g Gen) stmt(node ast.Stmt) {
19091909
}
19101910
ast.BranchStmt {
19111911
g.write_v_source_line_info(node.pos)
1912-
1913-
if node.label != '' {
1914-
x := g.labeled_loops[node.label] or {
1915-
panic('${node.label} doesn\'t exist ${g.file.path}, ${node.pos}')
1916-
}
1917-
match x {
1918-
ast.ForCStmt {
1919-
if x.scope.contains(g.cur_lock.pos.pos) {
1920-
g.unlock_locks()
1921-
}
1922-
}
1923-
ast.ForInStmt {
1924-
if x.scope.contains(g.cur_lock.pos.pos) {
1925-
g.unlock_locks()
1926-
}
1927-
}
1928-
ast.ForStmt {
1929-
if x.scope.contains(g.cur_lock.pos.pos) {
1930-
g.unlock_locks()
1931-
}
1932-
}
1933-
else {}
1934-
}
1935-
1936-
if node.kind == .key_break {
1937-
g.writeln('goto ${node.label}__break;')
1938-
} else {
1939-
// assert node.kind == .key_continue
1940-
g.writeln('goto ${node.label}__continue;')
1941-
}
1942-
} else {
1943-
inner_loop := g.inner_loop
1944-
match inner_loop {
1945-
ast.ForCStmt {
1946-
if inner_loop.scope.contains(g.cur_lock.pos.pos) {
1947-
g.unlock_locks()
1948-
}
1949-
}
1950-
ast.ForInStmt {
1951-
if inner_loop.scope.contains(g.cur_lock.pos.pos) {
1952-
g.unlock_locks()
1953-
}
1954-
}
1955-
ast.ForStmt {
1956-
if inner_loop.scope.contains(g.cur_lock.pos.pos) {
1957-
g.unlock_locks()
1958-
}
1959-
}
1960-
else {}
1961-
}
1962-
// continue or break
1963-
if g.is_autofree && !g.is_builtin_mod {
1964-
g.trace_autofree('// free before continue/break')
1965-
g.autofree_scope_vars_stop(node.pos.pos - 1, node.pos.line_nr, true,
1966-
g.branch_parent_pos)
1967-
}
1968-
g.writeln('${node.kind};')
1969-
}
1912+
g.branch_stmt(node)
19701913
}
19711914
ast.ConstDecl {
19721915
g.write_v_source_line_info(node.pos)
1973-
// if g.pref.build_mode != .build_module {
19741916
g.const_decl(node)
1975-
// }
19761917
}
19771918
ast.ComptimeFor {
19781919
g.comptime_for(node)
@@ -2077,116 +2018,7 @@ fn (mut g Gen) stmt(node ast.Stmt) {
20772018
g.writeln('goto ${c_name(node.name)};')
20782019
}
20792020
ast.HashStmt {
2080-
line_nr := node.pos.line_nr + 1
2081-
mut ct_condition := ''
2082-
if node.ct_conds.len > 0 {
2083-
ct_condition_start := g.out.len
2084-
for idx, ct_expr in node.ct_conds {
2085-
g.comptime_if_cond(ct_expr, false)
2086-
if idx < node.ct_conds.len - 1 {
2087-
g.write(' && ')
2088-
}
2089-
}
2090-
ct_condition = g.out.cut_to(ct_condition_start).trim_space()
2091-
// dump(node)
2092-
// dump(ct_condition)
2093-
}
2094-
// #include etc
2095-
if node.kind == 'include' {
2096-
mut missing_message := 'Header file ${node.main}, needed for module `${node.mod}` was not found.'
2097-
if node.msg != '' {
2098-
missing_message += ' ${node.msg}.'
2099-
} else {
2100-
missing_message += ' Please install the corresponding development headers.'
2101-
}
2102-
mut guarded_include := get_guarded_include_text(node.main, missing_message)
2103-
if node.main == '<errno.h>' {
2104-
// fails with musl-gcc and msvc; but an unguarded include works:
2105-
guarded_include = '#include ${node.main}'
2106-
}
2107-
if node.main.contains('.m') {
2108-
g.definitions.writeln('\n')
2109-
if ct_condition.len > 0 {
2110-
g.definitions.writeln('#if ${ct_condition}')
2111-
}
2112-
// Objective C code import, include it after V types, so that e.g. `string` is
2113-
// available there
2114-
g.definitions.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:')
2115-
g.definitions.writeln(guarded_include)
2116-
if ct_condition.len > 0 {
2117-
g.definitions.writeln('#endif // \$if ${ct_condition}')
2118-
}
2119-
g.definitions.writeln('\n')
2120-
} else {
2121-
g.includes.writeln('\n')
2122-
if ct_condition.len > 0 {
2123-
g.includes.writeln('#if ${ct_condition}')
2124-
}
2125-
g.includes.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:')
2126-
g.includes.writeln(guarded_include)
2127-
if ct_condition.len > 0 {
2128-
g.includes.writeln('#endif // \$if ${ct_condition}')
2129-
}
2130-
g.includes.writeln('\n')
2131-
}
2132-
} else if node.kind == 'preinclude' {
2133-
mut missing_message := 'Header file ${node.main}, needed for module `${node.mod}` was not found.'
2134-
if node.msg != '' {
2135-
missing_message += ' ${node.msg}.'
2136-
} else {
2137-
missing_message += ' Please install the corresponding development headers.'
2138-
}
2139-
mut guarded_include := get_guarded_include_text(node.main, missing_message)
2140-
if node.main == '<errno.h>' {
2141-
// fails with musl-gcc and msvc; but an unguarded include works:
2142-
guarded_include = '#include ${node.main}'
2143-
}
2144-
if node.main.contains('.m') {
2145-
// Might need to support '#preinclude' for .m files as well but for the moment
2146-
// this does the same as '#include' for them
2147-
g.definitions.writeln('\n')
2148-
if ct_condition.len > 0 {
2149-
g.definitions.writeln('#if ${ct_condition}')
2150-
}
2151-
// Objective C code import, include it after V types, so that e.g. `string` is
2152-
// available there
2153-
g.definitions.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:')
2154-
g.definitions.writeln(guarded_include)
2155-
if ct_condition.len > 0 {
2156-
g.definitions.writeln('#endif // \$if ${ct_condition}')
2157-
}
2158-
g.definitions.writeln('\n')
2159-
} else {
2160-
g.preincludes.writeln('\n')
2161-
if ct_condition.len > 0 {
2162-
g.preincludes.writeln('#if ${ct_condition}')
2163-
}
2164-
g.preincludes.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:')
2165-
g.preincludes.writeln(guarded_include)
2166-
if ct_condition.len > 0 {
2167-
g.preincludes.writeln('#endif // \$if ${ct_condition}')
2168-
}
2169-
g.preincludes.writeln('\n')
2170-
}
2171-
} else if node.kind == 'insert' {
2172-
if ct_condition.len > 0 {
2173-
g.includes.writeln('#if ${ct_condition}')
2174-
}
2175-
g.includes.writeln('// inserted by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:')
2176-
g.includes.writeln(node.val)
2177-
if ct_condition.len > 0 {
2178-
g.includes.writeln('#endif // \$if ${ct_condition}')
2179-
}
2180-
} else if node.kind == 'define' {
2181-
if ct_condition.len > 0 {
2182-
g.includes.writeln('#if ${ct_condition}')
2183-
}
2184-
g.includes.writeln('// defined by module `${node.mod}`')
2185-
g.includes.writeln('#define ${node.main}')
2186-
if ct_condition.len > 0 {
2187-
g.includes.writeln('#endif // \$if ${ct_condition}')
2188-
}
2189-
}
2021+
g.hash_stmt(node)
21902022
}
21912023
ast.Import {}
21922024
ast.InterfaceDecl {
@@ -4490,6 +4322,178 @@ fn (mut g Gen) gen_option_error(target_type ast.Type, expr ast.Expr) {
44904322
g.write(', .data={EMPTY_STRUCT_INITIALIZATION} }')
44914323
}
44924324

4325+
fn (mut g Gen) hash_stmt(node ast.HashStmt) {
4326+
line_nr := node.pos.line_nr + 1
4327+
mut ct_condition := ''
4328+
if node.ct_conds.len > 0 {
4329+
ct_condition_start := g.out.len
4330+
for idx, ct_expr in node.ct_conds {
4331+
g.comptime_if_cond(ct_expr, false)
4332+
if idx < node.ct_conds.len - 1 {
4333+
g.write(' && ')
4334+
}
4335+
}
4336+
ct_condition = g.out.cut_to(ct_condition_start).trim_space()
4337+
// dump(node)
4338+
// dump(ct_condition)
4339+
}
4340+
// #include etc
4341+
if node.kind == 'include' {
4342+
mut missing_message := 'Header file ${node.main}, needed for module `${node.mod}` was not found.'
4343+
if node.msg != '' {
4344+
missing_message += ' ${node.msg}.'
4345+
} else {
4346+
missing_message += ' Please install the corresponding development headers.'
4347+
}
4348+
mut guarded_include := get_guarded_include_text(node.main, missing_message)
4349+
if node.main == '<errno.h>' {
4350+
// fails with musl-gcc and msvc; but an unguarded include works:
4351+
guarded_include = '#include ${node.main}'
4352+
}
4353+
if node.main.contains('.m') {
4354+
g.definitions.writeln('\n')
4355+
if ct_condition.len > 0 {
4356+
g.definitions.writeln('#if ${ct_condition}')
4357+
}
4358+
// Objective C code import, include it after V types, so that e.g. `string` is
4359+
// available there
4360+
g.definitions.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:')
4361+
g.definitions.writeln(guarded_include)
4362+
if ct_condition.len > 0 {
4363+
g.definitions.writeln('#endif // \$if ${ct_condition}')
4364+
}
4365+
g.definitions.writeln('\n')
4366+
} else {
4367+
g.includes.writeln('\n')
4368+
if ct_condition.len > 0 {
4369+
g.includes.writeln('#if ${ct_condition}')
4370+
}
4371+
g.includes.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:')
4372+
g.includes.writeln(guarded_include)
4373+
if ct_condition.len > 0 {
4374+
g.includes.writeln('#endif // \$if ${ct_condition}')
4375+
}
4376+
g.includes.writeln('\n')
4377+
}
4378+
} else if node.kind == 'preinclude' {
4379+
mut missing_message := 'Header file ${node.main}, needed for module `${node.mod}` was not found.'
4380+
if node.msg != '' {
4381+
missing_message += ' ${node.msg}.'
4382+
} else {
4383+
missing_message += ' Please install the corresponding development headers.'
4384+
}
4385+
mut guarded_include := get_guarded_include_text(node.main, missing_message)
4386+
if node.main == '<errno.h>' {
4387+
// fails with musl-gcc and msvc; but an unguarded include works:
4388+
guarded_include = '#include ${node.main}'
4389+
}
4390+
if node.main.contains('.m') {
4391+
// Might need to support '#preinclude' for .m files as well but for the moment
4392+
// this does the same as '#include' for them
4393+
g.definitions.writeln('\n')
4394+
if ct_condition.len > 0 {
4395+
g.definitions.writeln('#if ${ct_condition}')
4396+
}
4397+
// Objective C code import, include it after V types, so that e.g. `string` is
4398+
// available there
4399+
g.definitions.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:')
4400+
g.definitions.writeln(guarded_include)
4401+
if ct_condition.len > 0 {
4402+
g.definitions.writeln('#endif // \$if ${ct_condition}')
4403+
}
4404+
g.definitions.writeln('\n')
4405+
} else {
4406+
g.preincludes.writeln('\n')
4407+
if ct_condition.len > 0 {
4408+
g.preincludes.writeln('#if ${ct_condition}')
4409+
}
4410+
g.preincludes.writeln('// added by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:')
4411+
g.preincludes.writeln(guarded_include)
4412+
if ct_condition.len > 0 {
4413+
g.preincludes.writeln('#endif // \$if ${ct_condition}')
4414+
}
4415+
g.preincludes.writeln('\n')
4416+
}
4417+
} else if node.kind == 'insert' {
4418+
if ct_condition.len > 0 {
4419+
g.includes.writeln('#if ${ct_condition}')
4420+
}
4421+
g.includes.writeln('// inserted by module `${node.mod}`, file: ${os.file_name(node.source_file)}:${line_nr}:')
4422+
g.includes.writeln(node.val)
4423+
if ct_condition.len > 0 {
4424+
g.includes.writeln('#endif // \$if ${ct_condition}')
4425+
}
4426+
} else if node.kind == 'define' {
4427+
if ct_condition.len > 0 {
4428+
g.includes.writeln('#if ${ct_condition}')
4429+
}
4430+
g.includes.writeln('// defined by module `${node.mod}`')
4431+
g.includes.writeln('#define ${node.main}')
4432+
if ct_condition.len > 0 {
4433+
g.includes.writeln('#endif // \$if ${ct_condition}')
4434+
}
4435+
}
4436+
}
4437+
4438+
fn (mut g Gen) branch_stmt(node ast.BranchStmt) {
4439+
if node.label != '' {
4440+
x := g.labeled_loops[node.label] or {
4441+
panic('${node.label} doesn\'t exist ${g.file.path}, ${node.pos}')
4442+
}
4443+
match x {
4444+
ast.ForCStmt {
4445+
if x.scope.contains(g.cur_lock.pos.pos) {
4446+
g.unlock_locks()
4447+
}
4448+
}
4449+
ast.ForInStmt {
4450+
if x.scope.contains(g.cur_lock.pos.pos) {
4451+
g.unlock_locks()
4452+
}
4453+
}
4454+
ast.ForStmt {
4455+
if x.scope.contains(g.cur_lock.pos.pos) {
4456+
g.unlock_locks()
4457+
}
4458+
}
4459+
else {}
4460+
}
4461+
4462+
if node.kind == .key_break {
4463+
g.writeln('goto ${node.label}__break;')
4464+
} else {
4465+
// assert node.kind == .key_continue
4466+
g.writeln('goto ${node.label}__continue;')
4467+
}
4468+
} else {
4469+
inner_loop := g.inner_loop
4470+
match inner_loop {
4471+
ast.ForCStmt {
4472+
if inner_loop.scope.contains(g.cur_lock.pos.pos) {
4473+
g.unlock_locks()
4474+
}
4475+
}
4476+
ast.ForInStmt {
4477+
if inner_loop.scope.contains(g.cur_lock.pos.pos) {
4478+
g.unlock_locks()
4479+
}
4480+
}
4481+
ast.ForStmt {
4482+
if inner_loop.scope.contains(g.cur_lock.pos.pos) {
4483+
g.unlock_locks()
4484+
}
4485+
}
4486+
else {}
4487+
}
4488+
// continue or break
4489+
if g.is_autofree && !g.is_builtin_mod {
4490+
g.trace_autofree('// free before continue/break')
4491+
g.autofree_scope_vars_stop(node.pos.pos - 1, node.pos.line_nr, true, g.branch_parent_pos)
4492+
}
4493+
g.writeln('${node.kind};')
4494+
}
4495+
}
4496+
44934497
fn (mut g Gen) return_stmt(node ast.Return) {
44944498
g.write_v_source_line_info(node.pos)
44954499

0 commit comments

Comments
 (0)