From 973b5c226a3072ed639484230c825d33724fd9c5 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Fri, 13 Mar 2020 12:22:36 +0100 Subject: [PATCH] cgen: optionals fixes --- vlib/os/os.v | 4 ++-- vlib/v/gen/cgen.v | 13 +++++++------ 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/vlib/os/os.v b/vlib/os/os.v index d662625617..96b9afbe68 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -626,11 +626,11 @@ pub fn dir(path string) string { } pub fn base_dir(path string) string { - pos := path.last_index(path_separator) or { + posx := path.last_index(path_separator) or { return path } // NB: *without* terminating / - return path[..pos] + return path[..posx] } pub fn filename(path string) string { diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index 5f0f98f74c..34356d6526 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -61,9 +61,9 @@ pub fn (g mut Gen) typ(t table.Type) string { if styp.starts_with('C__') { styp = styp[3..] } - if styp == 'stat' { + if styp in ['stat', 'dirent*'] { // TODO perf and other C structs - styp = 'struct stat' + styp = 'struct $styp' } if table.type_is_optional(t) { styp = 'Option_' + styp @@ -264,7 +264,7 @@ fn (g mut Gen) stmt(node ast.Stmt) { } ast.HashStmt { // #include etc - g.writeln('#$it.val') + g.definitions.writeln('#$it.val') } ast.Import {} ast.Return { @@ -954,6 +954,7 @@ fn (g mut Gen) ident(node ast.Ident) { match node.info { ast.IdentVar { if it.is_optional { + g.write('/*opt*/') styp := g.typ(it.typ)[7..] // Option_int => int TODO perf? g.write('(*($styp*)${name}.data)') return @@ -1073,10 +1074,10 @@ fn (g mut Gen) return_statement(it ast.Return) { else {} } if !is_none && !is_error { - g.write('opt_ok(') - g.expr(it.exprs[0]) styp := g.typ(g.fn_decl.return_type)[7..] // remove 'Option_' - g.writeln(', sizeof($styp));') + g.write('opt_ok(& ($styp []) { ') + g.expr(it.exprs[0]) + g.writeln(' }, sizeof($styp));') return } // g.write('/*OPTIONAL*/')