cgen/checker: fixes & fixes & tmp fixes :D

pull/4105/head
Joe Conigliaro 2020-03-23 02:22:49 +11:00
parent c0df54b7d3
commit 076bc2b773
6 changed files with 31 additions and 11 deletions

View File

@ -389,6 +389,7 @@ pub:
// cond Expr // cond Expr
val string val string
stmts []Stmt stmts []Stmt
is_not bool
pos token.Position pos token.Position
mut: mut:
has_else bool has_else bool

View File

@ -178,11 +178,11 @@ pub fn (c mut Checker) call_expr(call_expr mut ast.CallExpr) table.Type {
return table.string_type return table.string_type
} }
// start hack: until v1 is fixed and c definitions are added for these // start hack: until v1 is fixed and c definitions are added for these
if fn_name in ['C.calloc', 'C.exit', 'C.free'] { if fn_name in ['C.calloc', 'C.malloc', 'C.exit', 'C.free'] {
for arg in call_expr.args { for arg in call_expr.args {
c.expr(arg.expr) c.expr(arg.expr)
} }
if fn_name == 'C.calloc' { if fn_name in ['C.calloc', 'C.malloc'] {
return table.byteptr_type return table.byteptr_type
} }
return table.void_type return table.void_type

View File

@ -268,8 +268,14 @@ fn (g mut Gen) stmt(node ast.Stmt) {
g.const_decl(it) g.const_decl(it)
} }
ast.CompIf { ast.CompIf {
g.writeln('\n#ifdef ' + comp_if_to_ifdef(it.val)) if it.is_not {
g.writeln('// #if $it.val') g.writeln('\n#ifndef ' + comp_if_to_ifdef(it.val))
g.writeln('// #if not $it.val')
}
else {
g.writeln('\n#ifdef ' + comp_if_to_ifdef(it.val))
g.writeln('// #if $it.val')
}
// println('comp if stmts $g.file.path:$it.pos.line_nr') // println('comp if stmts $g.file.path:$it.pos.line_nr')
g.stmts(it.stmts) g.stmts(it.stmts)
if it.has_else { if it.has_else {
@ -2025,6 +2031,9 @@ fn comp_if_to_ifdef(name string) string {
'mingw' { 'mingw' {
return '__MINGW32__' return '__MINGW32__'
} }
'glibc' {
return '__GLIBC__'
}
'no_bounds_checking' { 'no_bounds_checking' {
return 'NO_BOUNDS_CHECK' return 'NO_BOUNDS_CHECK'
} }

View File

@ -8,7 +8,8 @@ pub fn (p mut Parser) comp_if() ast.CompIf {
pos := p.tok.position() pos := p.tok.position()
p.next() p.next()
p.check(.key_if) p.check(.key_if)
if p.tok.kind == .not { is_not := p.tok.kind == .not
if is_not {
p.next() p.next()
} }
val := p.check_name() val := p.check_name()
@ -16,6 +17,7 @@ pub fn (p mut Parser) comp_if() ast.CompIf {
p.next() p.next()
} }
mut node := ast.CompIf{ mut node := ast.CompIf{
is_not: is_not
stmts: p.parse_block() stmts: p.parse_block()
pos: pos pos: pos
val: val val: val

View File

@ -205,7 +205,7 @@ pub const (
pub const ( pub const (
builtin_type_names = ['void', 'voidptr', 'charptr', 'byteptr', 'i8', 'i16', 'int', 'i64', 'u16', 'u32', 'u64', builtin_type_names = ['void', 'voidptr', 'charptr', 'byteptr', 'i8', 'i16', 'int', 'i64', 'u16', 'u32', 'u64',
'f32', 'f64', 'string', 'char', 'byte', 'bool', 'none', 'array', 'array_fixed', 'map', 'struct', 'f32', 'f64', 'string', 'char', 'byte', 'bool', 'none', 'array', 'array_fixed', 'map', 'struct',
'mapnode', 'ustring'] 'mapnode', 'ustring', 'size_t']
) )
pub struct MultiReturn { pub struct MultiReturn {
@ -411,6 +411,10 @@ pub fn (t mut Table) register_builtin_type_symbols() {
kind: .map kind: .map
name: 'map' name: 'map'
}) })
t.register_type_symbol(TypeSymbol{
kind: .placeholder
name: 'size_t'
})
// TODO: remove. for v1 map compatibility // TODO: remove. for v1 map compatibility
map_string_string_idx := t.find_or_register_map(string_type, string_type) map_string_string_idx := t.find_or_register_map(string_type, string_type)
map_string_int_idx := t.find_or_register_map(string_type, int_type) map_string_int_idx := t.find_or_register_map(string_type, int_type)

View File

@ -470,6 +470,15 @@ pub fn (t &Table) check(got, expected Type) bool {
&& (got_idx in pointer_type_idxs || got_idx in number_type_idxs) { && (got_idx in pointer_type_idxs || got_idx in number_type_idxs) {
return true return true
} }
// see hack in checker IndexExpr line #691
if (got_idx == byte_type_idx && exp_idx == byteptr_type_idx) //
|| (exp_idx == byte_type_idx && got_idx == byteptr_type_idx) {
return true
}
if (got_idx == char_type_idx && exp_idx == charptr_type_idx) //
|| (exp_idx == char_type_idx && got_idx == charptr_type_idx) {
return true
}
// # NOTE: use symbols from this point on for perf // # NOTE: use symbols from this point on for perf
got_type_sym := t.get_type_symbol(got) got_type_sym := t.get_type_symbol(got)
exp_type_sym := t.get_type_symbol(expected) exp_type_sym := t.get_type_symbol(expected)
@ -477,11 +486,6 @@ pub fn (t &Table) check(got, expected Type) bool {
if (got_type_sym.is_int() && exp_type_sym.kind == .enum_) || (exp_type_sym.is_int() && got_type_sym.kind == .enum_) { if (got_type_sym.is_int() && exp_type_sym.kind == .enum_) || (exp_type_sym.is_int() && got_type_sym.kind == .enum_) {
return true return true
} }
// TODO: actually check for & handle pointers with name_expr
// see hack in checker IndexExpr line #691
if (got_type_sym.kind == .byte && exp_type_sym.kind == .byteptr) || (exp_type_sym.kind == .byte && got_type_sym.kind == .byteptr) {
return true
}
// TODO // TODO
// if got_type_sym.kind == .array && exp_type_sym.kind == .array { // if got_type_sym.kind == .array && exp_type_sym.kind == .array {
// return true // return true