checker: check C args

pull/3978/head
Alexander Medvednikov 2020-03-08 22:11:56 +01:00
parent 754a6cc93e
commit e3687dc257
5 changed files with 24 additions and 16 deletions

View File

@ -311,18 +311,20 @@ fn (a mut array) push(val voidptr) {
// `val` is array.data // `val` is array.data
// TODO make private, right now it's used by strings.Builder // TODO make private, right now it's used by strings.Builder
pub fn (a mut array) push_many(val voidptr, size int) { pub fn (a3 mut array) push_many(val voidptr, size int) {
// handle `arr << arr` if a3.data == val {
if a.data == val { // handle `arr << arr`
copy := a.clone() copy := a3.clone()
a.ensure_cap(a.len + size) a3.ensure_cap(a3.len + size)
C.printf("%d", a3.len*2)
println(a3.len*2)
//C.memcpy(a.data, copy.data, copy.element_size * copy.len) //C.memcpy(a.data, copy.data, copy.element_size * copy.len)
C.memcpy(a.data + a.element_size * a.len, copy.data, a.element_size * size) C.memcpy(a3.data + a3.element_size * a3.len, copy.data, a3.element_size * size)
} else { } else {
a.ensure_cap(a.len + size) a3.ensure_cap(a3.len + size)
C.memcpy(a.data + a.element_size * a.len, val, a.element_size * size) C.memcpy(a3.data + a3.element_size * a3.len, val, a3.element_size * size)
} }
a.len += size a3.len += size
} }
// array.reverse returns a new array with the elements of // array.reverse returns a new array with the elements of

View File

@ -207,6 +207,9 @@ pub fn (c mut Checker) call_expr(call_expr mut ast.CallExpr) table.Type {
c.error('unknown fn: $fn_name', call_expr.pos) c.error('unknown fn: $fn_name', call_expr.pos)
} }
if f.is_c || call_expr.is_c { if f.is_c || call_expr.is_c {
for expr in call_expr.args {
c.expr(expr)
}
return f.return_type return f.return_type
} }
if call_expr.args.len < f.args.len { if call_expr.args.len < f.args.len {
@ -303,6 +306,7 @@ pub fn (c mut Checker) method_call_expr(method_call_expr mut ast.MethodCallExpr)
pub fn (c mut Checker) selector_expr(selector_expr mut ast.SelectorExpr) table.Type { pub fn (c mut Checker) selector_expr(selector_expr mut ast.SelectorExpr) table.Type {
typ := c.expr(selector_expr.expr) typ := c.expr(selector_expr.expr)
selector_expr.expr_type = typ selector_expr.expr_type = typ
// println('sel expr line_nr=$selector_expr.pos.line_nr typ=$selector_expr.expr_type')
typ_sym := c.table.get_type_symbol(typ) typ_sym := c.table.get_type_symbol(typ)
field_name := selector_expr.field field_name := selector_expr.field
if field := typ_sym.find_field(field_name) { if field := typ_sym.find_field(field_name) {
@ -322,10 +326,6 @@ pub fn (c mut Checker) selector_expr(selector_expr mut ast.SelectorExpr) table.T
} }
} }
if typ_sym.kind != .struct_ { if typ_sym.kind != .struct_ {
if field_name == 'default_mode' {
// TODO
return table.bool_type
}
c.error('`$typ_sym.name` is not a struct', selector_expr.pos) c.error('`$typ_sym.name` is not a struct', selector_expr.pos)
} }
else { else {
@ -632,7 +632,7 @@ pub fn (c mut Checker) expr(node ast.Expr) table.Type {
} }
pub fn (c mut Checker) ident(ident mut ast.Ident) table.Type { pub fn (c mut Checker) ident(ident mut ast.Ident) table.Type {
// println('IDENT: $it.name - $it.pos.pos') // println('IDENT: $ident.name - $ident.pos.pos')
if ident.kind == .variable { if ident.kind == .variable {
// println('===========================') // println('===========================')
// c.scope.print_vars(0) // c.scope.print_vars(0)
@ -656,7 +656,7 @@ pub fn (c mut Checker) ident(ident mut ast.Ident) table.Type {
// update the variable // update the variable
// we need to do this here instead of var_decl since some // we need to do this here instead of var_decl since some
// vars are registered manually for things like for loops etc // vars are registered manually for things like for loops etc
// NOTE: or consider making those declerations part of those ast nodes // NOTE: or consider making those declarations part of those ast nodes
mut typ := var.typ mut typ := var.typ
// set var type on first use // set var type on first use
if typ == 0 { if typ == 0 {

View File

@ -622,6 +622,8 @@ fn (g mut Gen) expr(node ast.Expr) {
g.write('sizeof($it.type_name)') g.write('sizeof($it.type_name)')
} }
ast.StringLiteral { ast.StringLiteral {
// In C calls we have to generate C strings
// `C.printf("hi")` => `printf("hi");`
if g.is_c_call { if g.is_c_call {
g.write('"$it.val"') g.write('"$it.val"')
} }

View File

@ -94,6 +94,7 @@ i < 10; i++) {
} }
void User_inc_age(User* u, int n) { void User_inc_age(User* u, int n) {
printf("%d", u->age);
u->age += n; u->age += n;
} }

View File

@ -91,7 +91,10 @@ fn foo(a int) {
} }
fn C.printf()
fn (u mut User) inc_age(n int) { fn (u mut User) inc_age(n int) {
C.printf("%d", u.age)
u.age += n u.age += n
} }