cgen: optionals fixes

pull/4004/head
Alexander Medvednikov 2020-03-13 12:22:36 +01:00
parent e667e72685
commit 973b5c226a
2 changed files with 9 additions and 8 deletions

View File

@ -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 {

View File

@ -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*/')