mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix using C constants in enums, none
propagation
This commit is contained in:
parent
a43b8b5c96
commit
bf7f84d16f
@ -66,7 +66,8 @@ mut:
|
|||||||
pcs_declarations strings.Builder // -prof profile counter declarations for each function
|
pcs_declarations strings.Builder // -prof profile counter declarations for each function
|
||||||
hotcode_definitions strings.Builder // -live declarations & functions
|
hotcode_definitions strings.Builder // -live declarations & functions
|
||||||
options strings.Builder // `Option_xxxx` types
|
options strings.Builder // `Option_xxxx` types
|
||||||
json_forward_decls strings.Builder // `Option_xxxx` types
|
json_forward_decls strings.Builder // json type forward decls
|
||||||
|
enum_typedefs strings.Builder // enum types
|
||||||
file ast.File
|
file ast.File
|
||||||
fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0
|
fn_decl &ast.FnDecl // pointer to the FnDecl we are currently inside otherwise 0
|
||||||
last_fn_c_name string
|
last_fn_c_name string
|
||||||
@ -131,6 +132,7 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string
|
|||||||
hotcode_definitions: strings.new_builder(100)
|
hotcode_definitions: strings.new_builder(100)
|
||||||
options: strings.new_builder(100)
|
options: strings.new_builder(100)
|
||||||
json_forward_decls: strings.new_builder(100)
|
json_forward_decls: strings.new_builder(100)
|
||||||
|
enum_typedefs: strings.new_builder(100)
|
||||||
table: table
|
table: table
|
||||||
pref: pref
|
pref: pref
|
||||||
fn_decl: 0
|
fn_decl: 0
|
||||||
@ -191,6 +193,8 @@ pub fn cgen(files []ast.File, table &table.Table, pref &pref.Preferences) string
|
|||||||
b.writeln(g.cheaders.str())
|
b.writeln(g.cheaders.str())
|
||||||
b.writeln('\n// V includes:')
|
b.writeln('\n// V includes:')
|
||||||
b.writeln(g.includes.str())
|
b.writeln(g.includes.str())
|
||||||
|
b.writeln('\n// Enum definitions:')
|
||||||
|
b.writeln(g.enum_typedefs.str())
|
||||||
b.writeln('\n// V type definitions:')
|
b.writeln('\n// V type definitions:')
|
||||||
b.writeln(g.type_definitions.str())
|
b.writeln(g.type_definitions.str())
|
||||||
b.writeln('\n// V Option_xxx definitions:')
|
b.writeln('\n// V Option_xxx definitions:')
|
||||||
@ -617,26 +621,26 @@ fn (mut g Gen) stmt(node ast.Stmt) {
|
|||||||
}
|
}
|
||||||
ast.EnumDecl {
|
ast.EnumDecl {
|
||||||
enum_name := it.name.replace('.', '__')
|
enum_name := it.name.replace('.', '__')
|
||||||
g.typedefs.writeln('typedef enum {')
|
g.enum_typedefs.writeln('typedef enum {')
|
||||||
mut cur_enum_expr := ''
|
mut cur_enum_expr := ''
|
||||||
mut cur_enum_offset := 0
|
mut cur_enum_offset := 0
|
||||||
for field in it.fields {
|
for field in it.fields {
|
||||||
g.typedefs.write('\t${enum_name}_${field.name}')
|
g.enum_typedefs.write('\t${enum_name}_${field.name}')
|
||||||
if field.has_expr {
|
if field.has_expr {
|
||||||
g.typedefs.write(' = ')
|
g.enum_typedefs.write(' = ')
|
||||||
pos := g.out.len
|
pos := g.out.len
|
||||||
g.expr(field.expr)
|
g.expr(field.expr)
|
||||||
expr_str := g.out.after(pos)
|
expr_str := g.out.after(pos)
|
||||||
g.out.go_back(expr_str.len)
|
g.out.go_back(expr_str.len)
|
||||||
g.typedefs.write(expr_str)
|
g.enum_typedefs.write(expr_str)
|
||||||
cur_enum_expr = expr_str
|
cur_enum_expr = expr_str
|
||||||
cur_enum_offset = 0
|
cur_enum_offset = 0
|
||||||
}
|
}
|
||||||
cur_value := if cur_enum_offset > 0 { '${cur_enum_expr}+${cur_enum_offset}' } else { cur_enum_expr }
|
cur_value := if cur_enum_offset > 0 { '${cur_enum_expr}+${cur_enum_offset}' } else { cur_enum_expr }
|
||||||
g.typedefs.writeln(', // ${cur_value}')
|
g.enum_typedefs.writeln(', // ${cur_value}')
|
||||||
cur_enum_offset++
|
cur_enum_offset++
|
||||||
}
|
}
|
||||||
g.typedefs.writeln('} ${enum_name};\n')
|
g.enum_typedefs.writeln('} ${enum_name};\n')
|
||||||
}
|
}
|
||||||
ast.ExprStmt {
|
ast.ExprStmt {
|
||||||
g.expr(it.expr)
|
g.expr(it.expr)
|
||||||
@ -3309,8 +3313,13 @@ fn (mut g Gen) insert_before_stmt(s string) {
|
|||||||
fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type table.Type) {
|
fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type table.Type) {
|
||||||
cvar_name := c_name(var_name)
|
cvar_name := c_name(var_name)
|
||||||
mr_styp := g.base_type(return_type)
|
mr_styp := g.base_type(return_type)
|
||||||
|
is_none_ok := mr_styp == 'void'
|
||||||
g.writeln(';') // or')
|
g.writeln(';') // or')
|
||||||
|
if is_none_ok {
|
||||||
|
g.writeln('if (!${cvar_name}.ok && !${cvar_name}.is_none) {')
|
||||||
|
} else {
|
||||||
g.writeln('if (!${cvar_name}.ok) {')
|
g.writeln('if (!${cvar_name}.ok) {')
|
||||||
|
}
|
||||||
if or_block.kind == .block {
|
if or_block.kind == .block {
|
||||||
g.writeln('\tstring err = ${cvar_name}.v_error;')
|
g.writeln('\tstring err = ${cvar_name}.v_error;')
|
||||||
g.writeln('\tint errcode = ${cvar_name}.ecode;')
|
g.writeln('\tint errcode = ${cvar_name}.ecode;')
|
||||||
@ -3344,8 +3353,8 @@ fn (mut g Gen) or_block(var_name string, or_block ast.OrExpr, return_type table.
|
|||||||
g.stmts(stmts)
|
g.stmts(stmts)
|
||||||
}
|
}
|
||||||
} else if or_block.kind == .propagate {
|
} else if or_block.kind == .propagate {
|
||||||
g.write_defer_stmts()
|
|
||||||
if g.file.mod.name == 'main' && g.cur_fn.name == 'main' {
|
if g.file.mod.name == 'main' && g.cur_fn.name == 'main' {
|
||||||
|
// No reason to write defers here if we are going to panic this process
|
||||||
if g.pref.is_debug {
|
if g.pref.is_debug {
|
||||||
paline, pafile, pamod, pafn := g.panic_debug_info(or_block.pos)
|
paline, pafile, pamod, pafn := g.panic_debug_info(or_block.pos)
|
||||||
g.writeln('panic_debug($paline, tos3("$pafile"), tos3("$pamod"), tos3("$pafn"), ${cvar_name}.v_error );')
|
g.writeln('panic_debug($paline, tos3("$pafile"), tos3("$pamod"), tos3("$pafn"), ${cvar_name}.v_error );')
|
||||||
|
Loading…
Reference in New Issue
Block a user