2019-06-23 04:21:30 +02:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2019-10-13 15:37:43 +02:00
|
|
|
module compiler
|
2019-06-22 20:20:28 +02:00
|
|
|
|
2019-09-21 16:08:48 +02:00
|
|
|
import(
|
|
|
|
strings
|
|
|
|
)
|
2019-07-03 22:11:27 +02:00
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
const (
|
|
|
|
MaxLocalVars = 50
|
|
|
|
)
|
|
|
|
|
2019-10-28 14:37:18 +01:00
|
|
|
pub
|
2019-06-22 20:20:28 +02:00
|
|
|
struct Fn {
|
|
|
|
// addr int
|
2019-10-21 13:14:28 +02:00
|
|
|
pub:
|
2019-06-22 20:20:28 +02:00
|
|
|
mut:
|
2019-07-14 11:01:32 +02:00
|
|
|
name string
|
2019-08-05 09:49:52 +02:00
|
|
|
mod string
|
2019-09-23 19:34:08 +02:00
|
|
|
//local_vars []Var
|
|
|
|
//var_idx int
|
2019-06-22 20:20:28 +02:00
|
|
|
args []Var
|
|
|
|
is_interface bool
|
|
|
|
// called_fns []string
|
|
|
|
// idx int
|
|
|
|
scope_level int
|
|
|
|
typ string // return type
|
|
|
|
receiver_typ string
|
2019-10-23 16:48:07 +02:00
|
|
|
is_c bool
|
2019-07-15 22:09:34 +02:00
|
|
|
is_public bool
|
2019-06-22 20:20:28 +02:00
|
|
|
is_method bool
|
|
|
|
is_decl bool // type myfn fn(int, int)
|
2019-10-23 16:48:07 +02:00
|
|
|
is_unsafe bool
|
2019-10-20 19:15:48 +02:00
|
|
|
is_deprecated bool
|
2019-10-23 16:48:07 +02:00
|
|
|
is_variadic bool
|
|
|
|
is_generic bool
|
|
|
|
returns_error bool
|
2019-08-07 17:51:21 +02:00
|
|
|
defer_text []string
|
2019-10-23 16:48:07 +02:00
|
|
|
type_pars []string
|
|
|
|
type_inst []TypeInst
|
|
|
|
dispatch_of TypeInst // current type inst of this generic instance
|
|
|
|
body_idx int // idx of the first body statement
|
2019-09-29 19:37:39 +02:00
|
|
|
fn_name_token_idx int // used by error reporting
|
2019-10-27 01:36:43 +02:00
|
|
|
comptime_define string
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-10-20 09:19:37 +02:00
|
|
|
struct TypeInst {
|
|
|
|
mut:
|
|
|
|
// an instantiation of generic params (e.g. ["int","int","double"])
|
|
|
|
inst map[string]string
|
|
|
|
done bool
|
|
|
|
}
|
|
|
|
|
2019-10-21 13:14:28 +02:00
|
|
|
const (
|
|
|
|
EmptyFn = Fn{}
|
|
|
|
MainFn = Fn{ name: 'main' }
|
|
|
|
)
|
|
|
|
|
2019-10-20 09:19:37 +02:00
|
|
|
fn (a []TypeInst) str() string {
|
|
|
|
mut r := []string
|
|
|
|
for t in a {
|
|
|
|
mut s := ' | '
|
|
|
|
for k in t.inst.keys() {
|
|
|
|
s += k+' -> '+ t.inst[k] +' | '
|
|
|
|
}
|
|
|
|
r << s
|
|
|
|
}
|
|
|
|
return r.str()
|
|
|
|
}
|
|
|
|
|
2019-09-23 19:34:08 +02:00
|
|
|
fn (p &Parser) find_var(name string) ?Var {
|
|
|
|
for i in 0 .. p.var_idx {
|
|
|
|
if p.local_vars[i].name == name {
|
|
|
|
return p.local_vars[i]
|
2019-09-17 21:41:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p &Parser) find_var_check_new_var(name string) ?Var {
|
2019-09-23 19:34:08 +02:00
|
|
|
for i in 0 .. p.var_idx {
|
|
|
|
if p.local_vars[i].name == name {
|
|
|
|
return p.local_vars[i]
|
2019-09-17 21:41:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// A hack to allow `newvar := Foo{ field: newvar }`
|
|
|
|
// Declare the variable so that it can be used in the initialization
|
|
|
|
if name == 'main__' + p.var_decl_name {
|
|
|
|
return Var{
|
|
|
|
name : p.var_decl_name
|
|
|
|
typ : 'voidptr'
|
|
|
|
is_mut : true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
|
2019-09-09 15:22:39 +02:00
|
|
|
fn (p mut Parser) open_scope() {
|
|
|
|
p.cur_fn.defer_text << ''
|
|
|
|
p.cur_fn.scope_level++
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-09-09 15:22:39 +02:00
|
|
|
fn (p mut Parser) mark_var_used(v Var) {
|
2019-09-24 05:18:06 +02:00
|
|
|
if v.idx == -1 || v.idx >= p.local_vars.len {
|
2019-09-23 21:47:09 +02:00
|
|
|
return
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-09-23 21:47:09 +02:00
|
|
|
p.local_vars[v.idx].is_used = true
|
2019-07-25 13:16:17 +02:00
|
|
|
}
|
|
|
|
|
2019-09-09 15:22:39 +02:00
|
|
|
fn (p mut Parser) mark_var_returned(v Var) {
|
2019-09-23 21:47:09 +02:00
|
|
|
if v.idx == -1 || v.idx >= p.local_vars.len {
|
|
|
|
return
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-09-23 21:47:09 +02:00
|
|
|
p.local_vars[v.idx].is_returned = true
|
2019-09-09 02:29:24 +02:00
|
|
|
}
|
|
|
|
|
2019-09-09 15:22:39 +02:00
|
|
|
fn (p mut Parser) mark_var_changed(v Var) {
|
2019-09-23 21:47:09 +02:00
|
|
|
if v.idx == -1 || v.idx >= p.local_vars.len {
|
|
|
|
return
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-09-23 21:47:09 +02:00
|
|
|
p.local_vars[v.idx].is_changed = true
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-09-24 21:30:30 +02:00
|
|
|
fn (p mut Parser) mark_arg_moved(v Var) {
|
|
|
|
for i, arg in p.cur_fn.args {
|
|
|
|
if arg.name == v.name {
|
2019-10-03 01:41:48 +02:00
|
|
|
//println('setting f $p.cur_fn.name arg $arg.name to is_mut')
|
2019-09-24 21:30:30 +02:00
|
|
|
p.cur_fn.args[i].is_moved = true
|
|
|
|
break
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-24 21:30:30 +02:00
|
|
|
p.table.fns[p.cur_fn.name] = p.cur_fn
|
|
|
|
}
|
|
|
|
|
2019-09-23 19:34:08 +02:00
|
|
|
fn (p mut Parser) known_var(name string) bool {
|
2019-09-25 14:10:45 +02:00
|
|
|
_ = p.find_var(name) or {
|
2019-09-17 21:41:58 +02:00
|
|
|
return false
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) known_var_check_new_var(name string) bool {
|
|
|
|
_ = p.find_var_check_new_var(name) or {
|
|
|
|
return false
|
|
|
|
}
|
2019-09-17 21:41:58 +02:00
|
|
|
return true
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-09-23 19:34:08 +02:00
|
|
|
fn (p mut Parser) register_var(v Var) {
|
2019-09-23 21:47:09 +02:00
|
|
|
mut new_var := {v | idx: p.var_idx, scope_level: p.cur_fn.scope_level}
|
2019-09-23 19:34:08 +02:00
|
|
|
if v.line_nr == 0 {
|
2019-09-29 19:37:39 +02:00
|
|
|
new_var.token_idx = p.cur_tok_index()
|
|
|
|
new_var.line_nr = p.cur_tok().line_nr
|
2019-09-23 19:34:08 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
// Expand the array
|
2019-09-23 19:34:08 +02:00
|
|
|
if p.var_idx >= p.local_vars.len {
|
|
|
|
p.local_vars << new_var
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-09-23 19:34:08 +02:00
|
|
|
p.local_vars[p.var_idx] = new_var
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-09-23 19:34:08 +02:00
|
|
|
p.var_idx++
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-09-23 19:34:08 +02:00
|
|
|
fn (p mut Parser) clear_vars() {
|
|
|
|
// shared a := [1, 2, 3]
|
|
|
|
p.var_idx = 0
|
2019-09-26 04:28:43 +02:00
|
|
|
if p.local_vars.len > 0 {
|
|
|
|
if p.pref.autofree {
|
2019-10-20 18:59:53 +02:00
|
|
|
//p.local_vars.free()
|
2019-09-26 04:28:43 +02:00
|
|
|
}
|
|
|
|
p.local_vars = []Var
|
|
|
|
}
|
2019-10-20 18:59:53 +02:00
|
|
|
|
2019-06-28 17:15:52 +02:00
|
|
|
}
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
// Function signatures are added to the top of the .c file in the first run.
|
|
|
|
fn (p mut Parser) fn_decl() {
|
2019-09-23 19:34:08 +02:00
|
|
|
p.clear_vars() // clear local vars every time a new fn is started
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgen('fn ')
|
2019-10-20 09:19:37 +02:00
|
|
|
|
2019-08-17 21:19:37 +02:00
|
|
|
//defer { p.fgenln('\n') }
|
2019-09-28 23:21:10 +02:00
|
|
|
// If we are in the first pass, create a new function.
|
|
|
|
// In the second pass fetch the one we created.
|
|
|
|
/*
|
|
|
|
mut f := if p.first_pass {
|
|
|
|
Fn{
|
|
|
|
mod: p.mod
|
|
|
|
is_public: p.tok == .key_pub
|
|
|
|
}
|
|
|
|
else {
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-09-28 23:21:10 +02:00
|
|
|
*/
|
|
|
|
mut f := Fn{
|
2019-10-20 18:59:53 +02:00
|
|
|
mod: p.mod
|
2019-10-23 12:03:14 +02:00
|
|
|
is_public: p.tok == .key_pub || p.is_vh // functions defined in .vh are always public
|
2019-10-20 18:59:53 +02:00
|
|
|
is_unsafe: p.attr == 'unsafe_fn'
|
2019-10-20 19:15:48 +02:00
|
|
|
is_deprecated: p.attr == 'deprecated'
|
2019-10-27 01:36:43 +02:00
|
|
|
comptime_define: if p.attr.starts_with('if ') { p.attr.right(3) } else { '' }
|
2019-10-20 18:59:53 +02:00
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
is_live := p.attr == 'live' && !p.pref.is_so && p.pref.is_live
|
|
|
|
if p.attr == 'live' && p.first_pass() && !p.pref.is_live && !p.pref.is_so {
|
|
|
|
println('INFO: run `v -live program.v` if you want to use [live] functions')
|
|
|
|
}
|
2019-09-23 19:34:08 +02:00
|
|
|
if f.is_public {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
p.returns = false
|
2019-07-08 03:42:36 +02:00
|
|
|
//p.gen('/* returns $p.returns */')
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
// Method receiver
|
|
|
|
mut receiver_typ := ''
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .lpar {
|
2019-06-22 20:20:28 +02:00
|
|
|
f.is_method = true
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lpar)
|
2019-06-22 20:20:28 +02:00
|
|
|
receiver_name := p.check_name()
|
2019-07-07 22:30:15 +02:00
|
|
|
is_mut := p.tok == .key_mut
|
|
|
|
is_amp := p.tok == .amp
|
2019-06-22 20:20:28 +02:00
|
|
|
if is_mut || is_amp {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.check_space(p.tok)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
receiver_typ = p.get_type()
|
2019-10-23 12:03:14 +02:00
|
|
|
t := p.table.find_type(receiver_typ)
|
|
|
|
if (t.name == '' || t.is_placeholder) && !p.first_pass() {
|
|
|
|
p.error('unknown receiver type `$receiver_typ`')
|
|
|
|
}
|
|
|
|
if t.cat == .interface_ {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('invalid receiver type `$receiver_typ` (`$receiver_typ` is an interface)')
|
|
|
|
}
|
|
|
|
// Don't allow modifying types from a different module
|
2019-10-23 12:03:14 +02:00
|
|
|
if !p.first_pass() && !p.builtin_mod && t.mod != p.mod &&
|
2019-10-25 15:34:12 +02:00
|
|
|
!p.is_vgen // allow .str()
|
2019-10-23 12:03:14 +02:00
|
|
|
{
|
|
|
|
//println('T.mod=$T.mod')
|
|
|
|
//println('p.mod=$p.mod')
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('cannot define new methods on non-local type `$receiver_typ`')
|
|
|
|
}
|
2019-09-09 15:22:39 +02:00
|
|
|
// `(f *Foo)` instead of `(f mut Foo)` is a common mistake
|
2019-09-28 23:21:10 +02:00
|
|
|
if receiver_typ.ends_with('*') {
|
2019-10-23 12:03:14 +02:00
|
|
|
tt := receiver_typ.replace('*', '')
|
|
|
|
p.error('use `($receiver_name mut $tt)` instead of `($receiver_name *$tt)`')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
f.receiver_typ = receiver_typ
|
|
|
|
if is_mut || is_amp {
|
|
|
|
receiver_typ += '*'
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rpar)
|
2019-08-17 21:19:37 +02:00
|
|
|
p.fspace()
|
2019-06-22 20:20:28 +02:00
|
|
|
receiver := Var {
|
|
|
|
name: receiver_name
|
|
|
|
is_arg: true
|
|
|
|
typ: receiver_typ
|
|
|
|
is_mut: is_mut
|
|
|
|
ref: is_amp
|
|
|
|
ptr: is_mut
|
|
|
|
line_nr: p.scanner.line_nr
|
2019-09-29 19:37:39 +02:00
|
|
|
token_idx: p.cur_tok_index()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
f.args << receiver
|
2019-09-23 19:34:08 +02:00
|
|
|
p.register_var(receiver)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-09-28 23:21:10 +02:00
|
|
|
// +-/* methods
|
2019-10-30 13:04:25 +01:00
|
|
|
if p.tok in [.plus, .minus, .mul] {
|
2019-06-22 20:20:28 +02:00
|
|
|
f.name = p.tok.str()
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
f.name = p.check_name()
|
|
|
|
}
|
2019-09-29 19:37:39 +02:00
|
|
|
f.fn_name_token_idx = p.cur_tok_index()
|
2019-10-12 12:54:58 +02:00
|
|
|
// init fn
|
2019-10-13 00:19:32 +02:00
|
|
|
if f.name == 'init' && !f.is_method && f.is_public {
|
2019-10-12 12:54:58 +02:00
|
|
|
p.error('init function cannot be public')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
// C function header def? (fn C.NSMakeRect(int,int,int,int))
|
2019-08-17 21:19:37 +02:00
|
|
|
is_c := f.name == 'C' && p.tok == .dot
|
2019-06-22 20:20:28 +02:00
|
|
|
// Just fn signature? only builtin.v + default build mode
|
2019-10-30 13:04:25 +01:00
|
|
|
//if p.pref.is_verbose { // p.pref.build_mode == .build_module {
|
|
|
|
//println('\n\nfn_decl() name=$f.name receiver_typ=$receiver_typ nogen=$p.cgen.nogen')
|
|
|
|
//}
|
2019-06-22 20:20:28 +02:00
|
|
|
if is_c {
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.dot)
|
2019-06-22 20:20:28 +02:00
|
|
|
f.name = p.check_name()
|
|
|
|
f.is_c = true
|
|
|
|
}
|
2019-10-09 22:38:33 +02:00
|
|
|
else if !p.pref.translated {
|
2019-10-10 00:46:05 +02:00
|
|
|
if contains_capital(f.name) && !p.fileis('view.v') {
|
2019-10-23 12:03:14 +02:00
|
|
|
println('`$f.name`')
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('function names cannot contain uppercase letters, use snake_case instead')
|
|
|
|
}
|
2019-10-09 22:38:33 +02:00
|
|
|
if f.name[0] == `_` {
|
2019-10-12 13:21:06 +02:00
|
|
|
p.error('function names cannot start with `_`, use snake_case instead')
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
if f.name.contains('__') {
|
2019-07-10 10:08:37 +02:00
|
|
|
p.error('function names cannot contain double underscores, use single underscores instead')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// simple_name := f.name
|
2019-07-07 22:30:15 +02:00
|
|
|
// println('!SIMP.le=$simple_name')
|
2019-06-22 20:20:28 +02:00
|
|
|
// user.register() => User_register()
|
|
|
|
has_receiver := receiver_typ.len > 0
|
|
|
|
if receiver_typ != '' {
|
|
|
|
// f.name = '${receiver_typ}_${f.name}'
|
|
|
|
}
|
2019-08-05 09:49:52 +02:00
|
|
|
// full mod function name
|
2019-06-22 20:20:28 +02:00
|
|
|
// os.exit ==> os__exit()
|
2019-10-12 00:17:37 +02:00
|
|
|
// if !is_c && !p.builtin_mod && receiver_typ.len == 0 {
|
|
|
|
if !is_c && receiver_typ.len == 0 && (!p.builtin_mod || (p.builtin_mod && f.name == 'init')) {
|
2019-08-05 09:49:52 +02:00
|
|
|
f.name = p.prepend_mod(f.name)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-09-18 14:06:34 +02:00
|
|
|
if p.first_pass() && receiver_typ.len == 0 {
|
|
|
|
for {
|
|
|
|
existing_fn := p.table.find_fn(f.name) or { break }
|
2019-06-22 20:20:28 +02:00
|
|
|
// This existing function could be defined as C decl before (no body), then we don't need to throw an erro
|
|
|
|
if !existing_fn.is_decl {
|
|
|
|
p.error('redefinition of `$f.name`')
|
|
|
|
}
|
2019-09-18 14:06:34 +02:00
|
|
|
break
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// Generic?
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .lt {
|
2019-10-20 09:19:37 +02:00
|
|
|
f.is_generic = true
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
2019-10-20 09:19:37 +02:00
|
|
|
for {
|
|
|
|
type_par := p.check_name()
|
|
|
|
if type_par.len > 1 || !(type_par in reserved_type_param_names) {
|
|
|
|
p.error('type parameters must be single-character, upper-case letters of the following set: $reserved_type_param_names')
|
|
|
|
}
|
|
|
|
if type_par in f.type_pars {
|
|
|
|
p.error('redeclaration of type parameter `$type_par`')
|
|
|
|
}
|
|
|
|
f.type_pars << type_par
|
|
|
|
if p.tok == .gt { break }
|
|
|
|
p.check(.comma)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-10-20 09:19:37 +02:00
|
|
|
p.set_current_fn(f)
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.gt)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// Args (...)
|
|
|
|
p.fn_args(mut f)
|
|
|
|
// Returns an error?
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .not {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
f.returns_error = true
|
|
|
|
}
|
|
|
|
// Returns a type?
|
|
|
|
mut typ := 'void'
|
2019-10-30 13:04:25 +01:00
|
|
|
if p.tok in [.name, .mul, .amp, .lsbr, .question, .lpar] {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgen(' ')
|
|
|
|
typ = p.get_type()
|
|
|
|
}
|
2019-10-07 00:31:01 +02:00
|
|
|
// Translated C code and .vh can have empty functions (just definitions)
|
|
|
|
is_fn_header := !is_c && !p.is_vh &&
|
2019-10-25 11:07:50 +02:00
|
|
|
//(p.pref.translated || p.pref.is_test || p.is_vh) &&
|
2019-10-07 00:31:01 +02:00
|
|
|
p.tok != .lcbr
|
2019-06-22 20:20:28 +02:00
|
|
|
if is_fn_header {
|
|
|
|
f.is_decl = true
|
|
|
|
}
|
|
|
|
// { required only in normal function declarations
|
2019-10-07 00:31:01 +02:00
|
|
|
if !is_c && !p.is_vh && !is_fn_header {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgen(' ')
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lcbr)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
// Register ?option type
|
2019-06-22 20:20:28 +02:00
|
|
|
if typ.starts_with('Option_') {
|
|
|
|
p.cgen.typedefs << 'typedef Option $typ;'
|
|
|
|
}
|
|
|
|
// Register function
|
|
|
|
f.typ = typ
|
2019-09-28 19:42:29 +02:00
|
|
|
str_args := f.str_args(p.table)
|
2019-06-22 20:20:28 +02:00
|
|
|
// Special case for main() args
|
2019-09-28 19:42:29 +02:00
|
|
|
if f.name == 'main__main' && !has_receiver {
|
2019-07-05 22:03:00 +02:00
|
|
|
if str_args != '' || typ != 'void' {
|
2019-09-29 19:37:39 +02:00
|
|
|
p.error_with_token_index('fn main must have no arguments and no return values', f.fn_name_token_idx)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
2019-10-20 09:19:37 +02:00
|
|
|
dll_export_linkage := p.get_linkage_prefix()
|
2019-08-17 21:19:37 +02:00
|
|
|
if !p.is_vweb {
|
2019-09-07 12:44:41 +02:00
|
|
|
p.set_current_fn( f )
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
|
|
|
// Generate `User_register()` instead of `register()`
|
2019-06-22 20:20:28 +02:00
|
|
|
// Internally it's still stored as "register" in type User
|
2019-09-14 22:48:30 +02:00
|
|
|
mut fn_name_cgen := p.table.fn_gen_name(f)
|
2019-06-22 20:20:28 +02:00
|
|
|
// Start generation of the function body
|
2019-09-28 19:42:29 +02:00
|
|
|
skip_main_in_test := false
|
2019-10-07 00:31:01 +02:00
|
|
|
if !is_c && !is_live && !p.is_vh && !is_fn_header && !skip_main_in_test {
|
2019-06-30 22:03:17 +02:00
|
|
|
if p.pref.obfuscate {
|
2019-07-07 21:46:21 +02:00
|
|
|
p.genln('; // $f.name')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-10-20 09:19:37 +02:00
|
|
|
// Generic functions are inserted as needed from the call site
|
|
|
|
if f.is_generic {
|
|
|
|
if p.first_pass() {
|
|
|
|
f.body_idx = p.cur_tok_index()+1
|
2019-10-25 20:32:27 +02:00
|
|
|
if f.is_method {
|
|
|
|
rcv := p.table.find_type(receiver_typ)
|
|
|
|
if p.first_pass() && rcv.name == '' {
|
|
|
|
r := Type {
|
|
|
|
name: rcv.name.replace('*', '')
|
|
|
|
mod: p.mod
|
|
|
|
is_placeholder: true
|
|
|
|
}
|
|
|
|
p.table.register_type2(r)
|
|
|
|
}
|
|
|
|
// println('added generic method $rcv.name $f.name')
|
|
|
|
p.add_method(rcv.name, f)
|
|
|
|
} else {
|
|
|
|
p.table.register_fn(f)
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-10-25 20:32:27 +02:00
|
|
|
if f.is_method { p.mark_var_changed(f.args[0]) }
|
2019-10-20 09:19:37 +02:00
|
|
|
p.check_unused_variables()
|
|
|
|
p.set_current_fn( EmptyFn )
|
|
|
|
p.returns = false
|
|
|
|
p.skip_fn_body()
|
|
|
|
return
|
|
|
|
} else {
|
2019-09-14 22:48:30 +02:00
|
|
|
p.gen_fn_decl(f, typ, str_args)
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-10-12 00:17:37 +02:00
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
if is_fn_header {
|
|
|
|
p.genln('$typ $fn_name_cgen($str_args);')
|
|
|
|
p.fgenln('')
|
|
|
|
}
|
|
|
|
if is_c {
|
|
|
|
p.fgenln('\n')
|
|
|
|
}
|
|
|
|
// Register the method
|
|
|
|
if receiver_typ != '' {
|
2019-07-15 22:44:26 +02:00
|
|
|
mut receiver_t := p.table.find_type(receiver_typ)
|
2019-06-22 20:20:28 +02:00
|
|
|
// No such type yet? It could be defined later. Create a new type.
|
|
|
|
// struct declaration later will modify it instead of creating a new one.
|
2019-07-29 18:21:36 +02:00
|
|
|
if p.first_pass() && receiver_t.name == '' {
|
2019-08-31 15:38:13 +02:00
|
|
|
//println('fn decl ! registering placeholder $receiver_typ')
|
|
|
|
receiver_t = Type {
|
2019-06-22 20:20:28 +02:00
|
|
|
name: receiver_typ.replace('*', '')
|
2019-07-07 22:30:15 +02:00
|
|
|
mod: p.mod
|
2019-06-22 20:20:28 +02:00
|
|
|
is_placeholder: true
|
2019-08-31 15:38:13 +02:00
|
|
|
}
|
|
|
|
p.table.register_type2(receiver_t)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-09-18 14:37:34 +02:00
|
|
|
p.add_method(receiver_t.name, f)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-10-09 22:38:33 +02:00
|
|
|
else if p.first_pass(){
|
|
|
|
// println('register_fn $f.name typ=$typ isg=$is_generic pass=$p.pass ' +
|
|
|
|
//'$p.file_name')
|
2019-06-22 20:20:28 +02:00
|
|
|
p.table.register_fn(f)
|
|
|
|
}
|
2019-10-07 00:31:01 +02:00
|
|
|
if p.is_vh || p.first_pass() || is_live || is_fn_header || skip_main_in_test {
|
2019-08-17 21:19:37 +02:00
|
|
|
// First pass? Skip the body for now
|
|
|
|
// Look for generic calls.
|
2019-10-07 00:31:01 +02:00
|
|
|
if !p.is_vh && !is_fn_header {
|
2019-09-28 23:21:10 +02:00
|
|
|
p.skip_fn_body()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// Live code reloading? Load all fns from .so
|
2019-07-29 18:21:36 +02:00
|
|
|
if is_live && p.first_pass() && p.mod == 'main' {
|
2019-08-17 21:19:37 +02:00
|
|
|
//println('ADDING SO FN $fn_name_cgen')
|
2019-06-22 20:20:28 +02:00
|
|
|
p.cgen.so_fns << fn_name_cgen
|
|
|
|
fn_name_cgen = '(* $fn_name_cgen )'
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
// Function definition that goes to the top of the C file.
|
2019-07-23 23:23:13 +02:00
|
|
|
mut fn_decl := '$dll_export_linkage$typ $fn_name_cgen($str_args)'
|
2019-06-30 22:03:17 +02:00
|
|
|
if p.pref.obfuscate {
|
2019-07-29 18:21:36 +02:00
|
|
|
fn_decl += '; // $f.name'
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// Add function definition to the top
|
2019-09-28 19:42:29 +02:00
|
|
|
if !is_c && p.first_pass() {
|
2019-10-07 00:31:01 +02:00
|
|
|
p.cgen.fns << fn_decl + ';'
|
2019-10-04 14:48:09 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
return
|
|
|
|
}
|
2019-07-22 10:54:13 +02:00
|
|
|
if p.attr == 'live' && p.pref.is_so {
|
|
|
|
//p.genln('// live_function body start')
|
|
|
|
p.genln('pthread_mutex_lock(&live_fn_mutex);')
|
|
|
|
}
|
2019-09-28 19:42:29 +02:00
|
|
|
|
2019-10-10 21:27:22 +02:00
|
|
|
if f.name in ['main__main', 'main', 'WinMain'] {
|
|
|
|
if p.pref.is_test {
|
2019-09-29 19:37:39 +02:00
|
|
|
p.error_with_token_index('tests cannot have function `main`', f.fn_name_token_idx)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// println('is_c=$is_c name=$f.name')
|
2019-10-07 00:31:01 +02:00
|
|
|
if is_c || p.is_vh || is_fn_header {
|
2019-06-22 20:20:28 +02:00
|
|
|
return
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
// Profiling mode? Start counting at the beginning of the function (save current time).
|
2019-09-28 19:42:29 +02:00
|
|
|
if p.pref.is_prof && f.name != 'time__ticks' {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.genln('double _PROF_START = time__ticks();//$f.name')
|
2019-09-14 22:48:30 +02:00
|
|
|
cgen_name := p.table.fn_gen_name(f)
|
2019-09-09 15:22:39 +02:00
|
|
|
if f.defer_text.len > f.scope_level {
|
2019-09-14 22:48:30 +02:00
|
|
|
f.defer_text[f.scope_level] = ' ${cgen_name}_time += time__ticks() - _PROF_START;'
|
2019-09-09 15:22:39 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-24 02:35:25 +02:00
|
|
|
p.statements_no_rcbr()
|
2019-10-07 00:31:01 +02:00
|
|
|
//p.cgen.nogen = false
|
2019-06-22 20:20:28 +02:00
|
|
|
// Print counting result after all statements in main
|
2019-06-30 22:03:17 +02:00
|
|
|
if p.pref.is_prof && f.name == 'main' {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.genln(p.print_prof_counters())
|
|
|
|
}
|
|
|
|
// Counting or not, always need to add defer before the end
|
2019-08-17 21:19:37 +02:00
|
|
|
if !p.is_vweb {
|
2019-09-09 15:22:39 +02:00
|
|
|
if f.defer_text.len > f.scope_level {
|
2019-08-12 16:21:56 +02:00
|
|
|
p.genln(f.defer_text[f.scope_level])
|
2019-09-09 15:22:39 +02:00
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-09-28 19:42:29 +02:00
|
|
|
if typ != 'void' && !p.returns {
|
2019-09-29 19:37:39 +02:00
|
|
|
p.error_with_token_index('$f.name must return "$typ"', f.fn_name_token_idx)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-22 10:54:13 +02:00
|
|
|
if p.attr == 'live' && p.pref.is_so {
|
|
|
|
//p.genln('// live_function body end')
|
|
|
|
p.genln('pthread_mutex_unlock(&live_fn_mutex);')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
// {} closed correctly? scope_level should be 0
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.mod == 'main' {
|
2019-06-22 20:20:28 +02:00
|
|
|
// println(p.cur_fn.scope_level)
|
|
|
|
}
|
|
|
|
if p.cur_fn.scope_level > 2 {
|
|
|
|
// p.error('unclosed {')
|
|
|
|
}
|
|
|
|
// Make sure all vars in this function are used (only in main for now)
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.mod != 'main' {
|
2019-10-20 09:19:37 +02:00
|
|
|
p.genln('}')
|
2019-06-22 20:20:28 +02:00
|
|
|
return
|
|
|
|
}
|
2019-10-20 09:19:37 +02:00
|
|
|
p.genln('}')
|
2019-06-28 20:50:29 +02:00
|
|
|
p.check_unused_variables()
|
2019-09-07 12:44:41 +02:00
|
|
|
p.set_current_fn( EmptyFn )
|
2019-08-29 19:43:23 +02:00
|
|
|
p.returns = false
|
2019-06-28 20:50:29 +02:00
|
|
|
}
|
|
|
|
|
2019-09-28 23:21:10 +02:00
|
|
|
[inline]
|
|
|
|
// Skips the entire function's body in the first pass.
|
|
|
|
fn (p mut Parser) skip_fn_body() {
|
|
|
|
mut opened_scopes := 0
|
|
|
|
mut closed_scopes := 0
|
|
|
|
for {
|
|
|
|
if p.tok == .lcbr {
|
|
|
|
opened_scopes++
|
|
|
|
}
|
|
|
|
if p.tok == .rcbr {
|
|
|
|
closed_scopes++
|
|
|
|
}
|
|
|
|
// find `foo<Bar>()` in function bodies and register generic types
|
|
|
|
// TODO
|
|
|
|
// ...
|
|
|
|
// Reached a declaration token? (fn, struct, const etc) Stop.
|
|
|
|
if p.tok.is_decl() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// fn body ended, and a new fn attribute declaration like [live] is starting?
|
|
|
|
if closed_scopes > opened_scopes && p.prev_tok == .rcbr {
|
|
|
|
if p.tok == .lsbr {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 09:19:37 +02:00
|
|
|
fn (p Parser) get_linkage_prefix() string {
|
|
|
|
return if p.pref.ccompiler == 'msvc' && p.attr == 'live' && p.pref.is_so {
|
|
|
|
'__declspec(dllexport) '
|
|
|
|
} else if p.attr == 'inline' {
|
|
|
|
'static inline '
|
|
|
|
} else {
|
|
|
|
''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 20:50:29 +02:00
|
|
|
fn (p mut Parser) check_unused_variables() {
|
2019-09-23 19:34:08 +02:00
|
|
|
for var in p.local_vars {
|
2019-06-22 20:20:28 +02:00
|
|
|
if var.name == '' {
|
|
|
|
break
|
|
|
|
}
|
2019-09-28 18:53:56 +02:00
|
|
|
if !var.is_used && !p.pref.is_repl && !var.is_arg && !p.pref.translated {
|
2019-09-29 19:37:39 +02:00
|
|
|
p.production_error_with_token_index('`$var.name` declared and not used', var.token_idx )
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-10-24 18:44:49 +02:00
|
|
|
if !var.is_changed && var.is_mut && !p.pref.is_repl &&
|
|
|
|
!p.pref.translated && var.typ != 'T*'
|
|
|
|
{
|
|
|
|
p.error_with_token_index('`$var.name` is declared as mutable, but it was never changed', var.token_idx )
|
2019-07-25 13:16:17 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-21 12:43:47 +02:00
|
|
|
// user.register() => "User_register(user)"
|
|
|
|
// method_ph - where to insert "user_register("
|
2019-06-22 20:20:28 +02:00
|
|
|
// receiver_var - "user" (needed for pthreads)
|
|
|
|
// receiver_type - "User"
|
|
|
|
fn (p mut Parser) async_fn_call(f Fn, method_ph int, receiver_var, receiver_type string) {
|
|
|
|
// println('\nfn_call $f.name is_method=$f.is_method receiver_type=$f.receiver_type')
|
|
|
|
// p.print_tok()
|
|
|
|
mut thread_name := ''
|
2019-07-07 22:30:15 +02:00
|
|
|
// Normal function => just its name, method => TYPE_FN.name
|
2019-06-22 20:20:28 +02:00
|
|
|
mut fn_name := f.name
|
|
|
|
if f.is_method {
|
2019-08-17 21:19:37 +02:00
|
|
|
fn_name = receiver_type.replace('*', '') + '_' + f.name
|
2019-08-07 08:19:27 +02:00
|
|
|
//fn_name = '${receiver_type}_${f.name}'
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// Generate tmp struct with args
|
|
|
|
arg_struct_name := 'thread_arg_$fn_name'
|
|
|
|
tmp_struct := p.get_tmp()
|
|
|
|
p.genln('$arg_struct_name * $tmp_struct = malloc(sizeof($arg_struct_name));')
|
|
|
|
mut arg_struct := 'typedef struct $arg_struct_name { '
|
|
|
|
p.next()
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lpar)
|
2019-06-22 20:20:28 +02:00
|
|
|
// str_args contains the args for the wrapper function:
|
|
|
|
// wrapper(arg_struct * arg) { fn("arg->a, arg->b"); }
|
|
|
|
mut str_args := ''
|
2019-07-23 23:23:13 +02:00
|
|
|
mut did_gen_something := false
|
2019-06-22 20:20:28 +02:00
|
|
|
for i, arg in f.args {
|
|
|
|
arg_struct += '$arg.typ $arg.name ;'// Add another field (arg) to the tmp struct definition
|
2019-09-14 22:48:30 +02:00
|
|
|
str_args += 'arg $dot_ptr $arg.name'
|
2019-06-22 20:20:28 +02:00
|
|
|
if i == 0 && f.is_method {
|
2019-09-14 22:48:30 +02:00
|
|
|
p.genln('$tmp_struct $dot_ptr $arg.name = $receiver_var ;')
|
2019-06-22 20:20:28 +02:00
|
|
|
if i < f.args.len - 1 {
|
|
|
|
str_args += ','
|
|
|
|
}
|
2019-09-16 12:01:07 +02:00
|
|
|
did_gen_something = true
|
2019-06-22 20:20:28 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Set the struct values (args)
|
2019-09-14 22:48:30 +02:00
|
|
|
p.genln('$tmp_struct $dot_ptr $arg.name = ')
|
2019-06-22 20:20:28 +02:00
|
|
|
p.expression()
|
|
|
|
p.genln(';')
|
|
|
|
if i < f.args.len - 1 {
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.comma)
|
2019-06-22 20:20:28 +02:00
|
|
|
str_args += ','
|
|
|
|
}
|
2019-07-23 23:23:13 +02:00
|
|
|
did_gen_something = true
|
|
|
|
}
|
|
|
|
|
2019-07-24 17:46:41 +02:00
|
|
|
if !did_gen_something {
|
2019-07-23 23:23:13 +02:00
|
|
|
// Msvc doesnt like empty struct
|
2019-09-16 12:01:07 +02:00
|
|
|
arg_struct += 'EMPTY_STRUCT_DECLARATION;'
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-23 23:23:13 +02:00
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
arg_struct += '} $arg_struct_name ;'
|
|
|
|
// Also register the wrapper, so we can use the original function without modifying it
|
2019-09-14 22:48:30 +02:00
|
|
|
fn_name = p.table.fn_gen_name(f)
|
2019-06-22 20:20:28 +02:00
|
|
|
wrapper_name := '${fn_name}_thread_wrapper'
|
|
|
|
wrapper_text := 'void* $wrapper_name($arg_struct_name * arg) {$fn_name( /*f*/$str_args ); }'
|
|
|
|
p.cgen.register_thread_fn(wrapper_name, wrapper_text, arg_struct)
|
|
|
|
// Create thread object
|
|
|
|
tmp_nr := p.get_tmp_counter()
|
|
|
|
thread_name = '_thread$tmp_nr'
|
2019-10-14 07:41:46 +02:00
|
|
|
if p.os != .windows {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.genln('pthread_t $thread_name;')
|
|
|
|
}
|
|
|
|
tmp2 := p.get_tmp()
|
|
|
|
mut parg := 'NULL'
|
|
|
|
if f.args.len > 0 {
|
|
|
|
parg = ' $tmp_struct'
|
|
|
|
}
|
|
|
|
// Call the wrapper
|
2019-10-14 07:41:46 +02:00
|
|
|
if p.os == .windows {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.genln(' CreateThread(0,0, $wrapper_name, $parg, 0,0);')
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
p.genln('int $tmp2 = pthread_create(& $thread_name, NULL, $wrapper_name, $parg);')
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rpar)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-09-14 22:48:30 +02:00
|
|
|
// p.tok == fn_name
|
2019-10-20 09:19:37 +02:00
|
|
|
fn (p mut Parser) fn_call(f mut Fn, method_ph int, receiver_var, receiver_type string) {
|
2019-10-20 18:59:53 +02:00
|
|
|
if f.is_unsafe && !p.builtin_mod && !p.inside_unsafe {
|
2019-10-21 17:53:55 +02:00
|
|
|
p.warn('you are calling an unsafe function outside of an unsafe block')
|
2019-10-20 18:59:53 +02:00
|
|
|
}
|
2019-10-20 19:15:48 +02:00
|
|
|
if f.is_deprecated {
|
|
|
|
p.warn('$f.name is deprecated')
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
if !f.is_public && !f.is_c && !p.pref.is_test && !f.is_interface && f.mod != p.mod {
|
2019-09-11 14:07:18 +02:00
|
|
|
if f.name == 'contains' {
|
|
|
|
println('use `value in numbers` instead of `numbers.contains(value)`')
|
|
|
|
}
|
2019-06-26 13:17:45 +02:00
|
|
|
p.error('function `$f.name` is private')
|
2019-06-26 12:56:49 +02:00
|
|
|
}
|
2019-10-27 01:36:43 +02:00
|
|
|
is_comptime_define := f.comptime_define != '' && f.comptime_define != p.pref.comptime_define
|
|
|
|
if is_comptime_define {
|
|
|
|
p.cgen.nogen = true
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
p.calling_c = f.is_c
|
2019-08-05 09:49:52 +02:00
|
|
|
if f.is_c && !p.builtin_mod {
|
2019-07-21 12:22:41 +02:00
|
|
|
if f.name == 'free' {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.error('use `free()` instead of `C.free()`')
|
2019-07-21 12:22:41 +02:00
|
|
|
} else if f.name == 'malloc' {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.error('use `malloc()` instead of `C.malloc()`')
|
|
|
|
}
|
|
|
|
}
|
2019-10-20 09:19:37 +02:00
|
|
|
cgen_name := p.table.fn_gen_name(f)
|
|
|
|
p.next() // fn name
|
2019-07-29 18:21:36 +02:00
|
|
|
if p.tok == .lt {
|
2019-10-20 09:19:37 +02:00
|
|
|
mut i := p.token_idx
|
|
|
|
for {
|
|
|
|
if p.tokens[i].tok == .gt {
|
|
|
|
p.error('explicit type arguments are not allowed; remove `<...>`')
|
|
|
|
} else if p.tokens[i].tok == .lpar {
|
|
|
|
// probably a typo, do not concern the user with the above error message
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i += 1
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
|
|
|
}
|
2019-06-30 22:03:17 +02:00
|
|
|
// if p.pref.is_prof {
|
2019-06-22 20:20:28 +02:00
|
|
|
// p.cur_fn.called_fns << cgen_name
|
|
|
|
// }
|
2019-10-20 09:19:37 +02:00
|
|
|
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
// If we have a method placeholder,
|
|
|
|
// we need to preappend "method(receiver, ...)"
|
2019-10-20 09:19:37 +02:00
|
|
|
if f.is_method {
|
2019-06-22 20:20:28 +02:00
|
|
|
receiver := f.args.first()
|
2019-09-14 22:48:30 +02:00
|
|
|
//println('r=$receiver.typ RT=$receiver_type')
|
2019-06-22 20:20:28 +02:00
|
|
|
if receiver.is_mut && !p.expr_var.is_mut {
|
2019-09-14 22:48:30 +02:00
|
|
|
//println('$method_call recv=$receiver.name recv_mut=$receiver.is_mut')
|
2019-10-15 17:08:46 +02:00
|
|
|
if p.expr_var.is_for_var {
|
|
|
|
p.error('`$p.expr_var.name` is immutable, `for` variables' +
|
|
|
|
' always are')
|
|
|
|
} else {
|
|
|
|
p.error('`$p.expr_var.name` is immutable, declare it with `mut`')
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-07-25 13:16:17 +02:00
|
|
|
if !p.expr_var.is_changed {
|
2019-09-09 15:22:39 +02:00
|
|
|
p.mark_var_changed(p.expr_var)
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-10-25 20:57:32 +02:00
|
|
|
p.gen_method_call(receiver, receiver_type, cgen_name, f.typ, method_ph)
|
2019-10-20 09:19:37 +02:00
|
|
|
} else {
|
|
|
|
// Normal function call
|
|
|
|
p.gen('$cgen_name (')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-10-20 09:19:37 +02:00
|
|
|
|
2019-10-25 20:57:32 +02:00
|
|
|
// `foo<Bar>()`
|
2019-10-20 09:19:37 +02:00
|
|
|
// if f is generic, the name is changed to a suitable instance in dispatch_generic_fn_instance()
|
|
|
|
// we then replace `cgen_name` with the instance's name
|
|
|
|
generic := f.is_generic
|
2019-07-29 18:21:36 +02:00
|
|
|
p.fn_call_args(mut f)
|
2019-10-20 09:19:37 +02:00
|
|
|
if generic {
|
|
|
|
p.cgen.resetln(p.cgen.cur_line.replace('$cgen_name (', '$f.name ('))
|
|
|
|
// println('calling inst $f.name: $p.cgen.cur_line')
|
|
|
|
}
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen(')')
|
|
|
|
p.calling_c = false
|
2019-10-27 01:36:43 +02:00
|
|
|
if is_comptime_define {
|
|
|
|
p.cgen.nogen = false
|
2019-10-27 10:42:31 +01:00
|
|
|
p.cgen.resetln('')
|
2019-10-27 01:36:43 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
// println('end of fn call typ=$f.typ')
|
|
|
|
}
|
|
|
|
|
|
|
|
// for declaration
|
|
|
|
// return an updated Fn object with args[] field set
|
|
|
|
fn (p mut Parser) fn_args(f mut Fn) {
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lpar)
|
2019-08-17 21:19:37 +02:00
|
|
|
defer { p.check(.rpar) }
|
2019-06-22 20:20:28 +02:00
|
|
|
if f.is_interface {
|
|
|
|
int_arg := Var {
|
|
|
|
typ: f.receiver_typ
|
2019-09-29 19:37:39 +02:00
|
|
|
token_idx: p.cur_tok_index()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
f.args << int_arg
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
// `(int, string, int)`
|
2019-06-22 20:20:28 +02:00
|
|
|
// Just register fn arg types
|
2019-09-06 14:13:38 +02:00
|
|
|
types_only := p.tok == .mul || p.tok == .amp || (p.peek() == .comma && p.table.known_type(p.lit)) || p.peek() == .rpar// (int, string)
|
2019-06-22 20:20:28 +02:00
|
|
|
if types_only {
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok != .rpar {
|
2019-06-22 20:20:28 +02:00
|
|
|
typ := p.get_type()
|
2019-09-23 12:42:20 +02:00
|
|
|
p.check_and_register_used_imported_type(typ)
|
2019-06-22 20:20:28 +02:00
|
|
|
v := Var {
|
|
|
|
typ: typ
|
|
|
|
is_arg: true
|
|
|
|
// is_mut: is_mut
|
|
|
|
line_nr: p.scanner.line_nr
|
2019-09-29 19:37:39 +02:00
|
|
|
token_idx: p.cur_tok_index()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// f.register_var(v)
|
|
|
|
f.args << v
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .comma {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-06 18:13:04 +02:00
|
|
|
// `(a int, b, c string)` syntax
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok != .rpar {
|
2019-09-23 19:34:08 +02:00
|
|
|
mut names := [ p.check_name() ]
|
2019-08-06 18:13:04 +02:00
|
|
|
// `a,b,c int` syntax
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok == .comma {
|
|
|
|
p.check(.comma)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fspace()
|
|
|
|
names << p.check_name()
|
|
|
|
}
|
|
|
|
p.fspace()
|
2019-07-07 22:30:15 +02:00
|
|
|
is_mut := p.tok == .key_mut
|
2019-06-22 20:20:28 +02:00
|
|
|
if is_mut {
|
2019-10-20 09:19:37 +02:00
|
|
|
p.check(.key_mut)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-09-30 12:46:50 +02:00
|
|
|
mut typ := ''
|
|
|
|
// variadic arg
|
|
|
|
if p.tok == .ellipsis {
|
|
|
|
p.check(.ellipsis)
|
|
|
|
if p.tok == .rpar {
|
|
|
|
p.error('you must provide a type for vargs: eg `...string`. multiple types `...` are not supported yet.')
|
|
|
|
}
|
2019-10-23 16:48:07 +02:00
|
|
|
f.is_variadic = true
|
2019-09-30 12:46:50 +02:00
|
|
|
t := p.get_type()
|
|
|
|
// register varg struct, incase function is never called
|
2019-10-23 16:48:07 +02:00
|
|
|
if p.first_pass() && !f.is_generic {
|
|
|
|
p.fn_register_vargs_stuct(f, t, []string)
|
2019-10-18 10:28:14 +02:00
|
|
|
}
|
2019-09-30 12:46:50 +02:00
|
|
|
typ = '...$t'
|
|
|
|
} else {
|
|
|
|
typ = p.get_type()
|
|
|
|
}
|
2019-10-20 09:19:37 +02:00
|
|
|
|
2019-09-23 12:42:20 +02:00
|
|
|
p.check_and_register_used_imported_type(typ)
|
2019-07-24 15:24:32 +02:00
|
|
|
if is_mut && is_primitive_type(typ) {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.error('mutable arguments are only allowed for arrays, maps, and structs.' +
|
|
|
|
'\nreturn values instead: `foo(n mut int)` => `foo(n int) int`')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
for name in names {
|
2019-08-17 21:19:37 +02:00
|
|
|
if !p.first_pass() && !p.table.known_type(typ) {
|
2019-07-24 15:24:32 +02:00
|
|
|
p.error('fn_args: unknown type $typ')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
if is_mut {
|
2019-07-24 15:24:32 +02:00
|
|
|
typ += '*'
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-09-24 21:30:30 +02:00
|
|
|
v := Var{
|
2019-06-22 20:20:28 +02:00
|
|
|
name: name
|
2019-08-17 21:19:37 +02:00
|
|
|
typ: typ
|
2019-06-22 20:20:28 +02:00
|
|
|
is_arg: true
|
|
|
|
is_mut: is_mut
|
|
|
|
ptr: is_mut
|
|
|
|
line_nr: p.scanner.line_nr
|
2019-09-29 19:37:39 +02:00
|
|
|
token_idx: p.cur_tok_index()
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-09-23 19:34:08 +02:00
|
|
|
p.register_var(v)
|
2019-06-22 20:20:28 +02:00
|
|
|
f.args << v
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .comma {
|
2019-10-20 09:19:37 +02:00
|
|
|
p.check(.comma)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-09-30 12:46:50 +02:00
|
|
|
// unnamed (C definition)
|
|
|
|
if p.tok == .ellipsis {
|
|
|
|
if !f.is_c {
|
|
|
|
p.error('variadic argument syntax must be `arg_name ...type` eg `argname ...string`.')
|
|
|
|
}
|
|
|
|
f.args << Var {
|
|
|
|
// name: '...'
|
|
|
|
typ: '...'
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 21:19:37 +02:00
|
|
|
// foo *(1, 2, 3, mut bar)*
|
2019-10-20 09:19:37 +02:00
|
|
|
fn (p mut Parser) fn_call_args(f mut Fn) {
|
2019-06-22 20:20:28 +02:00
|
|
|
// println('fn_call_args() name=$f.name args.len=$f.args.len')
|
|
|
|
// C func. # of args is not known
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.lpar)
|
2019-06-22 20:20:28 +02:00
|
|
|
if f.is_c {
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok != .rpar {
|
2019-09-16 17:28:20 +02:00
|
|
|
//C.func(var1, var2.method())
|
|
|
|
//If the parameter calls a function or method that is not C,
|
|
|
|
//the value of p.calling_c is changed
|
|
|
|
p.calling_c = true
|
2019-09-15 14:57:17 +02:00
|
|
|
ph := p.cgen.add_placeholder()
|
|
|
|
typ := p.bool_expression()
|
|
|
|
// Cast V byteptr to C char* (byte is unsigned in V, that led to C warnings)
|
|
|
|
if typ == 'byte*' {
|
|
|
|
p.cgen.set_placeholder(ph, '(char*)')
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .comma {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen(', ')
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.comma)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rpar)
|
2019-10-20 09:19:37 +02:00
|
|
|
return
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-10-12 12:54:01 +02:00
|
|
|
// add debug information to panic when -g arg is passed
|
2019-09-14 22:48:30 +02:00
|
|
|
if p.v.pref.is_debug && f.name == 'panic' && !p.is_js {
|
2019-07-30 15:08:14 +02:00
|
|
|
mod_name := p.mod.replace('_dot_', '.')
|
|
|
|
fn_name := p.cur_fn.name.replace('${p.mod}__', '')
|
2019-10-25 15:34:12 +02:00
|
|
|
file_path := cescaped_path(p.file_path)
|
2019-07-30 15:08:14 +02:00
|
|
|
p.cgen.resetln(p.cgen.cur_line.replace(
|
|
|
|
'v_panic (',
|
2019-10-09 22:38:33 +02:00
|
|
|
'panic_debug ($p.scanner.line_nr, tos3("$file_path"), tos3("$mod_name"), tos2((byte *)"$fn_name"), '
|
2019-07-30 15:08:14 +02:00
|
|
|
))
|
|
|
|
}
|
2019-10-20 09:19:37 +02:00
|
|
|
mut saved_args := []string
|
2019-06-22 20:20:28 +02:00
|
|
|
for i, arg in f.args {
|
2019-09-24 21:30:30 +02:00
|
|
|
// Receiver is the first arg
|
|
|
|
// Skip the receiver, because it was already generated in the expression
|
2019-06-22 20:20:28 +02:00
|
|
|
if i == 0 && f.is_method {
|
2019-10-25 21:09:23 +02:00
|
|
|
if f.args.len > 1 { // && !p.is_js {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen(',')
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Reached the final vararg? Quit
|
2019-09-30 12:46:50 +02:00
|
|
|
if i == f.args.len - 1 && arg.typ.starts_with('...') {
|
2019-06-22 20:20:28 +02:00
|
|
|
break
|
|
|
|
}
|
2019-07-03 23:53:48 +02:00
|
|
|
ph := p.cgen.add_placeholder()
|
2019-08-17 21:19:37 +02:00
|
|
|
// `)` here means that not enough args were provided
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .rpar {
|
2019-06-22 20:20:28 +02:00
|
|
|
str_args := f.str_args(p.table)// TODO this is C args
|
|
|
|
p.error('not enough arguments in call to `$f.name ($str_args)`')
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
// If `arg` is mutable, the caller needs to provide `mut`:
|
2019-07-10 14:26:37 +02:00
|
|
|
// `mut numbers := [1,2,3]; reverse(mut numbers);`
|
2019-06-22 20:20:28 +02:00
|
|
|
if arg.is_mut {
|
2019-08-10 23:02:48 +02:00
|
|
|
if p.tok != .key_mut && p.tok == .name {
|
2019-08-17 21:19:37 +02:00
|
|
|
mut dots_example := 'mut $p.lit'
|
2019-08-10 23:02:48 +02:00
|
|
|
if i > 0 {
|
2019-08-17 21:19:37 +02:00
|
|
|
dots_example = '.., ' + dots_example
|
|
|
|
}
|
2019-08-10 23:02:48 +02:00
|
|
|
if i < f.args.len - 1 {
|
2019-08-17 21:19:37 +02:00
|
|
|
dots_example = dots_example + ',..'
|
|
|
|
}
|
2019-10-20 09:19:37 +02:00
|
|
|
p.error('`$arg.name` is a mutable argument, you need to provide `mut`: `$f.name($dots_example)`')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.peek() != .name {
|
2019-08-08 10:08:39 +02:00
|
|
|
p.error('`$arg.name` is a mutable argument, you need to provide a variable to modify: `$f.name(... mut a...)`')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.key_mut)
|
2019-08-17 21:19:37 +02:00
|
|
|
var_name := p.lit
|
2019-09-23 19:34:08 +02:00
|
|
|
v := p.find_var(var_name) or {
|
2019-08-08 10:08:39 +02:00
|
|
|
p.error('`$arg.name` is a mutable argument, you need to provide a variable to modify: `$f.name(... mut a...)`')
|
2019-09-17 21:41:58 +02:00
|
|
|
exit(1)
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-07-25 13:59:00 +02:00
|
|
|
if !v.is_changed {
|
2019-09-09 15:22:39 +02:00
|
|
|
p.mark_var_changed(v)
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p.expected_type = arg.typ
|
2019-09-24 21:30:30 +02:00
|
|
|
clone := p.pref.autofree && arg.typ == 'string' && arg.is_moved && p.mod != 'builtin'
|
|
|
|
if clone {
|
|
|
|
p.gen('/*YY f=$f.name arg=$arg.name is_moved=$arg.is_moved*/string_clone(')
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-09-20 18:07:38 +02:00
|
|
|
mut typ := p.bool_expression()
|
2019-09-24 21:30:30 +02:00
|
|
|
if clone {
|
|
|
|
p.gen(')')
|
|
|
|
}
|
2019-07-03 23:53:48 +02:00
|
|
|
// Optimize `println`: replace it with `printf` to avoid extra allocations and
|
2019-09-14 22:48:30 +02:00
|
|
|
// function calls.
|
|
|
|
// `println(777)` => `printf("%d\n", 777)`
|
2019-08-17 21:19:37 +02:00
|
|
|
// (If we don't check for void, then V will compile `println(func())`)
|
2019-09-20 18:07:38 +02:00
|
|
|
if i == 0 && (f.name == 'println' || f.name == 'print') && typ == 'ustring' {
|
|
|
|
if typ == 'ustring' {
|
|
|
|
p.gen('.s')
|
|
|
|
}
|
|
|
|
typ = 'string'
|
|
|
|
}
|
|
|
|
if i == 0 && (f.name == 'println' || f.name == 'print') && typ != 'string' && typ != 'ustring' && typ != 'void' {
|
2019-06-23 09:59:34 +02:00
|
|
|
T := p.table.find_type(typ)
|
2019-07-31 09:51:24 +02:00
|
|
|
$if !windows {
|
2019-09-14 22:48:30 +02:00
|
|
|
$if !js {
|
2019-07-31 09:51:24 +02:00
|
|
|
fmt := p.typ_to_fmt(typ, 0)
|
|
|
|
if fmt != '' {
|
2019-10-08 08:52:11 +02:00
|
|
|
nl := if f.name == 'println' { '\\n' } else { '' }
|
|
|
|
p.cgen.resetln(p.cgen.cur_line.replace(f.name + ' (', '/*opt*/printf ("' + fmt + '$nl", '))
|
2019-08-17 21:19:37 +02:00
|
|
|
continue
|
2019-07-31 09:51:24 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-14 22:48:30 +02:00
|
|
|
}
|
2019-07-04 00:44:57 +02:00
|
|
|
if typ.ends_with('*') {
|
2019-07-03 23:53:48 +02:00
|
|
|
p.cgen.set_placeholder(ph, 'ptr_str(')
|
2019-07-04 00:44:57 +02:00
|
|
|
p.gen(')')
|
2019-08-17 21:19:37 +02:00
|
|
|
continue
|
2019-06-23 09:59:34 +02:00
|
|
|
}
|
2019-07-04 00:44:57 +02:00
|
|
|
// Make sure this type has a `str()` method
|
2019-09-14 22:48:30 +02:00
|
|
|
$if !js {
|
2019-07-04 00:44:57 +02:00
|
|
|
if !T.has_method('str') {
|
2019-08-17 21:19:37 +02:00
|
|
|
// Arrays have automatic `str()` methods
|
2019-08-10 23:02:48 +02:00
|
|
|
if T.name.starts_with('array_') {
|
2019-08-31 15:38:13 +02:00
|
|
|
p.gen_array_str(T)
|
2019-08-10 23:02:48 +02:00
|
|
|
p.cgen.set_placeholder(ph, '${typ}_str(')
|
|
|
|
p.gen(')')
|
2019-08-17 21:19:37 +02:00
|
|
|
continue
|
2019-09-26 04:28:43 +02:00
|
|
|
} else if T.cat == .struct_ {
|
|
|
|
p.gen_struct_str(T)
|
|
|
|
p.cgen.set_placeholder(ph, '${typ}_str(')
|
|
|
|
p.gen(')')
|
|
|
|
continue
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-07-10 14:26:37 +02:00
|
|
|
error_msg := ('`$typ` needs to have method `str() string` to be printable')
|
2019-08-17 21:19:37 +02:00
|
|
|
p.error(error_msg)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-04 00:44:57 +02:00
|
|
|
p.cgen.set_placeholder(ph, '${typ}_str(')
|
2019-06-23 09:59:34 +02:00
|
|
|
p.gen(')')
|
2019-09-14 22:48:30 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
got := typ
|
|
|
|
expected := arg.typ
|
2019-09-28 23:21:10 +02:00
|
|
|
got_ptr := got.ends_with('*')
|
|
|
|
exp_ptr := expected.ends_with('*')
|
2019-06-22 20:20:28 +02:00
|
|
|
// println('fn arg got="$got" exp="$expected"')
|
2019-10-20 09:19:37 +02:00
|
|
|
type_mismatch := !p.check_types_no_throw(got, expected)
|
|
|
|
if type_mismatch && f.is_generic {
|
|
|
|
// println("argument `$arg.name` is generic")
|
|
|
|
saved_args << got
|
|
|
|
} else if type_mismatch {
|
2019-09-24 21:30:30 +02:00
|
|
|
mut j := i
|
|
|
|
if f.is_method {
|
|
|
|
j--
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-09-24 21:30:30 +02:00
|
|
|
mut nr := '${i+1}th'
|
|
|
|
if j == 0 {
|
|
|
|
nr = 'first'
|
|
|
|
} else if j == 1 {
|
|
|
|
nr = 'second'
|
|
|
|
} else if j == 2 {
|
|
|
|
nr = 'third'
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-09-24 21:30:30 +02:00
|
|
|
p.error('cannot use type `$typ` as type `$arg.typ` in $nr ' +
|
|
|
|
'argument to `$f.name()`')
|
2019-10-20 09:19:37 +02:00
|
|
|
} else {
|
|
|
|
saved_args << ''
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
is_interface := p.table.is_interface(arg.typ)
|
2019-09-28 23:21:10 +02:00
|
|
|
// Automatically add `&` or `*` before an argument.
|
|
|
|
// V, unlike C and Go, simplifies this aspect:
|
|
|
|
// `foo(bar)` is allowed where `foo(&bar)` is expected.
|
|
|
|
// The argument is not mutable, so it won't be changed by the function.
|
|
|
|
// It doesn't matter whether it's passed by referencee or by value
|
|
|
|
// to the end user.
|
2019-06-22 20:20:28 +02:00
|
|
|
if !is_interface {
|
|
|
|
// Dereference
|
2019-09-28 23:21:10 +02:00
|
|
|
if got_ptr && !exp_ptr {
|
2019-07-03 23:53:48 +02:00
|
|
|
p.cgen.set_placeholder(ph, '*')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// Reference
|
|
|
|
// TODO ptr hacks. DOOM hacks, fix please.
|
2019-09-28 23:21:10 +02:00
|
|
|
if !got_ptr && exp_ptr && got != 'voidptr' {
|
2019-09-29 16:02:28 +02:00
|
|
|
// Special case for mutable arrays. We can't `&` function
|
|
|
|
// results,
|
2019-08-17 21:19:37 +02:00
|
|
|
// have to use `(array[]){ expr }` hack.
|
2019-09-29 16:02:28 +02:00
|
|
|
if expected.starts_with('array_') && exp_ptr { //&& !arg.is_mut{
|
2019-08-17 21:19:37 +02:00
|
|
|
p.cgen.set_placeholder(ph, '& /*111*/ (array[]){')
|
2019-08-24 23:21:13 +02:00
|
|
|
p.gen('}[0] ')
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
// println('\ne:"$expected" got:"$got"')
|
2019-08-07 12:09:24 +02:00
|
|
|
else if ! (expected == 'void*' && got == 'int') &&
|
2019-06-22 20:20:28 +02:00
|
|
|
! (expected == 'byte*' && got.contains(']byte')) &&
|
2019-08-08 10:46:01 +02:00
|
|
|
! (expected == 'byte*' && got == 'string') &&
|
2019-08-17 21:19:37 +02:00
|
|
|
//! (expected == 'void*' && got == 'array_int') {
|
|
|
|
! (expected == 'byte*' && got == 'byteptr') {
|
|
|
|
p.cgen.set_placeholder(ph, '& /*112 EXP:"$expected" GOT:"$got" */')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-28 23:21:10 +02:00
|
|
|
else if is_interface {
|
|
|
|
if !got_ptr {
|
2019-07-03 23:53:48 +02:00
|
|
|
p.cgen.set_placeholder(ph, '&')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
// Pass all interface methods
|
|
|
|
interface_type := p.table.find_type(arg.typ)
|
|
|
|
for method in interface_type.methods {
|
|
|
|
p.gen(', ${typ}_${method.name} ')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Check for commas
|
|
|
|
if i < f.args.len - 1 {
|
|
|
|
// Handle 0 args passed to varargs
|
2019-10-23 16:48:07 +02:00
|
|
|
if p.tok != .comma && !f.is_variadic {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('wrong number of arguments for $i,$arg.name fn `$f.name`: expected $f.args.len, but got less')
|
|
|
|
}
|
2019-10-23 16:48:07 +02:00
|
|
|
if p.tok == .comma && (!f.is_variadic || (f.is_variadic && i < f.args.len-2 )) {
|
2019-10-21 14:30:34 +02:00
|
|
|
p.check(.comma)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen(',')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// varargs
|
2019-10-23 16:48:07 +02:00
|
|
|
varg_type, varg_values := p.fn_call_vargs(f)
|
|
|
|
if f.is_variadic {
|
2019-10-23 18:44:05 +02:00
|
|
|
saved_args << varg_type
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .comma {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('wrong number of arguments for fn `$f.name`: expected $f.args.len, but got more')
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.rpar)
|
2019-10-20 09:19:37 +02:00
|
|
|
if f.is_generic {
|
|
|
|
type_map := p.extract_type_inst(f, saved_args)
|
|
|
|
p.dispatch_generic_fn_instance(mut f, type_map)
|
|
|
|
}
|
2019-10-23 16:48:07 +02:00
|
|
|
if f.is_variadic {
|
|
|
|
p.fn_gen_caller_vargs(f, varg_type, varg_values)
|
|
|
|
}
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// From a given generic function and an argument list matching its signature,
|
|
|
|
// create a type instantiation
|
|
|
|
fn (p mut Parser) extract_type_inst(f &Fn, args_ []string) TypeInst {
|
|
|
|
mut r := TypeInst{}
|
|
|
|
mut i := 0
|
|
|
|
mut args := args_
|
2019-10-25 20:32:27 +02:00
|
|
|
if f.typ != 'void' { args << f.typ }
|
|
|
|
for e in args {
|
2019-10-20 09:19:37 +02:00
|
|
|
if e == '' { continue }
|
|
|
|
tp := f.type_pars[i]
|
|
|
|
mut ti := e
|
|
|
|
if ti.starts_with('fn (') {
|
2019-10-27 08:03:15 +01:00
|
|
|
fn_args := ti[4..].all_before(') ').split(',')
|
2019-10-20 09:19:37 +02:00
|
|
|
mut found := false
|
|
|
|
for fa_ in fn_args {
|
|
|
|
mut fa := fa_
|
2019-10-27 08:03:15 +01:00
|
|
|
for fa.starts_with('array_') { fa = fa[6..] }
|
2019-10-20 09:19:37 +02:00
|
|
|
if fa == tp {
|
|
|
|
r.inst[tp] = fa
|
|
|
|
found = true
|
|
|
|
i += 1
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if found { continue }
|
|
|
|
ti = ti.all_after(') ')
|
|
|
|
}
|
2019-10-27 08:03:15 +01:00
|
|
|
for ti.starts_with('array_') { ti = ti[6..] }
|
2019-10-20 09:19:37 +02:00
|
|
|
if r.inst[tp] != '' {
|
|
|
|
if r.inst[tp] != ti {
|
|
|
|
p.error('type parameter `$tp` has type ${r.inst[tp]}, not `$ti`')
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// println("extracted $tp => $ti")
|
|
|
|
r.inst[tp] = ti
|
|
|
|
i += 1
|
|
|
|
if i >= f.type_pars.len { break }
|
|
|
|
}
|
|
|
|
if r.inst[f.typ] == '' && f.typ in f.type_pars {
|
|
|
|
r.inst[f.typ] = '_ANYTYPE_'
|
|
|
|
}
|
2019-10-25 20:32:27 +02:00
|
|
|
for tp in f.type_pars {
|
|
|
|
if r.inst[tp] == '' {
|
|
|
|
p.error_with_token_index('unused type parameter `$tp`', f.body_idx-2)
|
|
|
|
}
|
|
|
|
}
|
2019-10-20 09:19:37 +02:00
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
// Replace type params of a given generic function using a TypeInst
|
|
|
|
fn (p mut Parser) replace_type_params(f &Fn, ti TypeInst) []string {
|
|
|
|
mut sig := []string
|
|
|
|
for a in f.args {
|
|
|
|
sig << a.typ
|
|
|
|
}
|
|
|
|
sig << f.typ
|
|
|
|
mut r := []string
|
|
|
|
for _, a in sig {
|
|
|
|
mut fi := a
|
|
|
|
mut fr := ''
|
|
|
|
if fi.starts_with('fn (') {
|
|
|
|
fr += 'fn ('
|
2019-10-27 08:03:15 +01:00
|
|
|
mut fn_args := fi[4..].all_before(') ').split(',')
|
2019-10-20 09:19:37 +02:00
|
|
|
fn_args << fi.all_after(') ')
|
|
|
|
for i, fa_ in fn_args {
|
|
|
|
mut fna := fa_.trim_space()
|
|
|
|
for fna.starts_with('array_') {
|
2019-10-27 08:03:15 +01:00
|
|
|
fna = fna[6..]
|
2019-10-20 09:19:37 +02:00
|
|
|
fr += 'array_'
|
|
|
|
}
|
|
|
|
if fna in ti.inst.keys() {
|
|
|
|
fr += ti.inst[fna]
|
|
|
|
} else {
|
|
|
|
fr += fna
|
|
|
|
}
|
|
|
|
if i <= fn_args.len-3 {
|
|
|
|
fr += ','
|
|
|
|
} else if i == fn_args.len-2 {
|
|
|
|
fr += ') '
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r << fr
|
|
|
|
continue
|
|
|
|
}
|
2019-10-20 18:59:53 +02:00
|
|
|
for fi.starts_with('array_') {
|
2019-10-27 08:03:15 +01:00
|
|
|
fi = fi[6..]
|
2019-10-20 09:19:37 +02:00
|
|
|
fr += 'array_'
|
|
|
|
}
|
2019-10-23 18:44:05 +02:00
|
|
|
is_varg := fi.starts_with('...')
|
2019-10-27 08:03:15 +01:00
|
|
|
if is_varg { fi = fi[3..] }
|
2019-10-20 09:19:37 +02:00
|
|
|
if fi in ti.inst.keys() {
|
2019-10-23 18:44:05 +02:00
|
|
|
mut t := ti.inst[fi]
|
|
|
|
if is_varg { t = '...$t' }
|
|
|
|
fr += t
|
2019-10-20 09:19:37 +02:00
|
|
|
// println("replaced $a => $fr")
|
|
|
|
} else {
|
|
|
|
fr += fi
|
|
|
|
}
|
|
|
|
r << fr
|
|
|
|
}
|
|
|
|
return r
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-10-09 19:55:36 +02:00
|
|
|
fn (p mut Parser) fn_register_vargs_stuct(f &Fn, typ string, values []string) string {
|
2019-09-30 12:46:50 +02:00
|
|
|
vargs_struct := '_V_FnVargs_$f.name'
|
|
|
|
varg_type := Type{
|
|
|
|
cat: TypeCategory.struct_,
|
|
|
|
name: vargs_struct,
|
|
|
|
mod: p.mod
|
|
|
|
}
|
2019-10-23 16:48:07 +02:00
|
|
|
if !p.table.known_type(vargs_struct) {
|
2019-09-30 12:46:50 +02:00
|
|
|
p.table.register_type2(varg_type)
|
2019-10-23 16:48:07 +02:00
|
|
|
p.cgen.typedefs << 'typedef struct $vargs_struct $vargs_struct;\n'
|
|
|
|
} else {
|
|
|
|
p.table.rewrite_type(varg_type)
|
2019-09-30 12:46:50 +02:00
|
|
|
}
|
|
|
|
p.table.add_field(vargs_struct, 'len', 'int', false, '', .public)
|
|
|
|
p.table.add_field(vargs_struct, 'args[$values.len]', typ, false, '', .public)
|
2019-10-09 19:55:36 +02:00
|
|
|
return vargs_struct
|
2019-09-30 12:46:50 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 16:48:07 +02:00
|
|
|
fn (p mut Parser) fn_call_vargs(f Fn) (string, []string) {
|
|
|
|
if !f.is_variadic {
|
|
|
|
return '', []string
|
|
|
|
}
|
2019-09-30 12:46:50 +02:00
|
|
|
last_arg := f.args.last()
|
2019-10-27 08:03:15 +01:00
|
|
|
mut varg_def_type := last_arg.typ[3..]
|
2019-10-23 16:48:07 +02:00
|
|
|
mut types := []string
|
2019-10-01 13:48:19 +02:00
|
|
|
mut values := []string
|
2019-09-30 12:46:50 +02:00
|
|
|
for p.tok != .rpar {
|
|
|
|
if p.tok == .comma {
|
|
|
|
p.check(.comma)
|
|
|
|
}
|
|
|
|
p.cgen.start_tmp()
|
2019-10-23 16:48:07 +02:00
|
|
|
mut varg_type := p.bool_expression()
|
2019-09-30 12:46:50 +02:00
|
|
|
varg_value := p.cgen.end_tmp()
|
2019-10-23 16:48:07 +02:00
|
|
|
if !f.is_generic {
|
|
|
|
p.check_types(last_arg.typ, varg_type)
|
|
|
|
} else {
|
|
|
|
if types.len > 0 {
|
|
|
|
for t in types {
|
|
|
|
p.check_types(varg_type, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
varg_def_type = varg_type
|
|
|
|
}
|
2019-09-30 12:46:50 +02:00
|
|
|
ref_deref := if last_arg.typ.ends_with('*') && !varg_type.ends_with('*') { '&' }
|
|
|
|
else if !last_arg.typ.ends_with('*') && varg_type.ends_with('*') { '*' }
|
|
|
|
else { '' }
|
2019-10-23 16:48:07 +02:00
|
|
|
types << varg_type
|
2019-10-01 13:48:19 +02:00
|
|
|
values << '$ref_deref$varg_value'
|
|
|
|
}
|
|
|
|
for va in p.table.varg_access {
|
|
|
|
if va.fn_name != f.name { continue }
|
|
|
|
if va.index >= values.len {
|
|
|
|
p.error_with_token_index('variadic arg index out of range: $va.index/${values.len-1}, vargs are 0 indexed', va.tok_idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if f.args.len > 1 {
|
|
|
|
p.cgen.gen(',')
|
2019-09-30 12:46:50 +02:00
|
|
|
}
|
2019-10-23 16:48:07 +02:00
|
|
|
return varg_def_type, values
|
|
|
|
}
|
2019-10-20 09:19:37 +02:00
|
|
|
|
2019-10-23 16:48:07 +02:00
|
|
|
fn (p mut Parser) fn_gen_caller_vargs(f &Fn, varg_type string, values []string) {
|
|
|
|
vargs_struct := p.fn_register_vargs_stuct(f, varg_type, values)
|
|
|
|
p.cgen.gen('&($vargs_struct){.len=$values.len,.args={'+values.join(',')+'}}')
|
2019-10-09 19:55:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) register_multi_return_stuct(types []string) string {
|
|
|
|
typ := '_V_MulRet_' + types.join('_V_').replace('*', '_PTR_')
|
|
|
|
if p.table.known_type(typ) { return typ }
|
|
|
|
p.table.register_type2(Type{
|
|
|
|
cat: TypeCategory.struct_,
|
|
|
|
name: typ,
|
|
|
|
mod: p.mod
|
|
|
|
})
|
|
|
|
for i, t in typ.replace('_V_MulRet_', '').replace('_PTR_', '*').split('_V_') {
|
|
|
|
p.table.add_field(typ, 'var_$i', t, false, '', .public)
|
|
|
|
}
|
|
|
|
p.cgen.typedefs << 'typedef struct $typ $typ;'
|
|
|
|
return typ
|
2019-09-30 12:46:50 +02:00
|
|
|
}
|
|
|
|
|
2019-10-23 16:48:07 +02:00
|
|
|
fn (p mut Parser) rename_generic_fn_instance(f mut Fn, ti TypeInst) {
|
2019-10-25 20:32:27 +02:00
|
|
|
if f.is_method {
|
|
|
|
f.name = f.receiver_typ + '_' + f.name
|
|
|
|
}
|
2019-10-23 16:48:07 +02:00
|
|
|
f.name = f.name + '_T'
|
|
|
|
for k in ti.inst.keys() {
|
|
|
|
f.name = f.name + '_' + type_to_safe_str(ti.inst[k].replace('...', ''))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 09:19:37 +02:00
|
|
|
fn (p mut Parser) dispatch_generic_fn_instance(f mut Fn, ti TypeInst) {
|
|
|
|
mut new_inst := true
|
|
|
|
for e in f.type_inst {
|
|
|
|
if e.inst.str() == ti.inst.str() {
|
|
|
|
new_inst = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !new_inst {
|
2019-10-23 16:48:07 +02:00
|
|
|
p.rename_generic_fn_instance(mut f, ti)
|
2019-10-20 09:19:37 +02:00
|
|
|
_f := p.table.find_fn(f.name) or {
|
|
|
|
p.error('function instance `$f.name` not found')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
f.args = _f.args
|
|
|
|
f.typ = _f.typ
|
|
|
|
f.is_generic = false
|
|
|
|
f.type_inst = []TypeInst
|
|
|
|
f.dispatch_of = ti
|
|
|
|
// println('using existing inst $f.name(${f.str_args(p.table)}) $f.typ')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
f.type_inst << ti
|
|
|
|
p.table.register_fn(f)
|
|
|
|
// Remember current scanner position, go back here for each type instance
|
|
|
|
// TODO remove this once tokens are cached in `new_parser()`
|
|
|
|
saved_tok_idx := p.cur_tok_index()
|
|
|
|
saved_fn := p.cur_fn
|
|
|
|
saved_var_idx := p.var_idx
|
|
|
|
saved_local_vars := p.local_vars
|
|
|
|
p.clear_vars()
|
|
|
|
saved_line := p.cgen.cur_line
|
|
|
|
saved_lines := p.cgen.lines
|
|
|
|
saved_is_tmp := p.cgen.is_tmp
|
|
|
|
saved_tmp_line := p.cgen.tmp_line
|
|
|
|
returns := p.returns // should be always false
|
|
|
|
|
2019-10-23 16:48:07 +02:00
|
|
|
p.rename_generic_fn_instance(mut f, ti)
|
2019-10-20 09:19:37 +02:00
|
|
|
f.is_generic = false // the instance is a normal function
|
|
|
|
f.type_inst = []TypeInst
|
|
|
|
f.scope_level = 0
|
|
|
|
f.dispatch_of = ti
|
2019-10-21 12:54:37 +02:00
|
|
|
|
|
|
|
// TODO this is done to prevent a crash as a result of this not being
|
|
|
|
// properly initialised. This is a bug somewhere futher upstream
|
2019-10-24 21:58:01 +02:00
|
|
|
f.defer_text = []string
|
2019-10-21 12:54:37 +02:00
|
|
|
|
2019-10-20 09:19:37 +02:00
|
|
|
old_args := f.args
|
|
|
|
new_types := p.replace_type_params(f, ti)
|
|
|
|
f.args = []Var
|
|
|
|
for i in 0..new_types.len-1 {
|
|
|
|
mut v := old_args[i]
|
|
|
|
v.typ = new_types[i]
|
|
|
|
f.args << v
|
|
|
|
}
|
|
|
|
f.typ = new_types.last()
|
|
|
|
if f.typ in f.type_pars { f.typ = '_ANYTYPE_' }
|
|
|
|
|
|
|
|
if f.typ in ti.inst {
|
|
|
|
f.typ = ti.inst[f.typ]
|
|
|
|
}
|
2019-10-25 20:32:27 +02:00
|
|
|
|
|
|
|
if f.is_method {
|
|
|
|
p.add_method(f.args[0].name, f)
|
|
|
|
} else {
|
|
|
|
p.table.register_fn(f)
|
|
|
|
}
|
2019-10-20 09:19:37 +02:00
|
|
|
// println("generating gen inst $f.name(${f.str_args(p.table)}) $f.typ : $ti.inst")
|
|
|
|
|
|
|
|
p.cgen.is_tmp = false
|
|
|
|
p.returns = false
|
|
|
|
p.cgen.tmp_line = ''
|
|
|
|
p.cgen.cur_line = ''
|
|
|
|
p.cgen.lines = []string
|
|
|
|
p.cur_fn = *f
|
|
|
|
for arg in f.args {
|
|
|
|
p.register_var(arg)
|
|
|
|
}
|
|
|
|
p.token_idx = f.body_idx-1
|
|
|
|
p.next() // re-initializes the parser properly
|
|
|
|
str_args := f.str_args(p.table)
|
|
|
|
|
|
|
|
p.in_dispatch = true
|
|
|
|
p.genln('${p.get_linkage_prefix()}$f.typ $f.name($str_args) {')
|
|
|
|
// p.genln('/* generic fn instance $f.name : $ti.inst */')
|
|
|
|
p.statements()
|
|
|
|
p.in_dispatch = false
|
|
|
|
|
|
|
|
if f.typ == '_ANYTYPE_' {
|
|
|
|
f.typ = p.cur_fn.typ
|
|
|
|
f.name = f.name.replace('_ANYTYPE_', type_to_safe_str(f.typ))
|
|
|
|
p.cgen.lines[0] = p.cgen.lines[0].replace('_ANYTYPE_', f.typ)
|
|
|
|
p.table.register_fn(f)
|
|
|
|
}
|
|
|
|
for l in p.cgen.lines {
|
|
|
|
p.cgen.fns << l
|
|
|
|
}
|
|
|
|
|
|
|
|
p.token_idx = saved_tok_idx-1
|
|
|
|
p.next()
|
|
|
|
p.check(.rpar) // end of the arg list which caused this dispatch
|
|
|
|
p.cur_fn = saved_fn
|
|
|
|
p.var_idx = saved_var_idx
|
|
|
|
p.local_vars = saved_local_vars
|
|
|
|
p.cgen.lines = saved_lines
|
|
|
|
p.cgen.cur_line = saved_line
|
|
|
|
p.cgen.is_tmp = saved_is_tmp
|
|
|
|
p.cgen.tmp_line = saved_tmp_line
|
|
|
|
p.returns = false
|
|
|
|
}
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
// "fn (int, string) int"
|
2019-09-28 23:21:10 +02:00
|
|
|
fn (f &Fn) typ_str() string {
|
2019-07-03 22:11:27 +02:00
|
|
|
mut sb := strings.new_builder(50)
|
2019-06-22 20:20:28 +02:00
|
|
|
sb.write('fn (')
|
|
|
|
for i, arg in f.args {
|
|
|
|
sb.write(arg.typ)
|
|
|
|
if i < f.args.len - 1 {
|
|
|
|
sb.write(',')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sb.write(')')
|
|
|
|
if f.typ != 'void' {
|
|
|
|
sb.write(' $f.typ')
|
|
|
|
}
|
|
|
|
return sb.str()
|
|
|
|
}
|
|
|
|
|
|
|
|
// f.args => "int a, string b"
|
2019-09-01 21:51:16 +02:00
|
|
|
fn (f &Fn) str_args(table &Table) string {
|
2019-06-22 20:20:28 +02:00
|
|
|
mut s := ''
|
|
|
|
for i, arg in f.args {
|
|
|
|
// Interfaces are a special case. We need to pass the object + pointers
|
|
|
|
// to all methods:
|
|
|
|
// fn handle(r Runner) { =>
|
|
|
|
// void handle(void *r, void (*Runner_run)(void*)) {
|
|
|
|
if table.is_interface(arg.typ) {
|
|
|
|
// First the object (same name as the interface argument)
|
|
|
|
s += ' void* $arg.name'
|
|
|
|
// Now all methods
|
|
|
|
interface_type := table.find_type(arg.typ)
|
|
|
|
for method in interface_type.methods {
|
2019-07-27 14:06:36 +02:00
|
|
|
s += ', $method.typ (*${arg.typ}_${method.name})(void*'
|
|
|
|
if method.args.len > 1 {
|
2019-10-27 08:03:15 +01:00
|
|
|
for a in method.args[1..] {
|
2019-07-27 14:06:36 +02:00
|
|
|
s += ', $a.typ'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s += ')'
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-30 12:46:50 +02:00
|
|
|
else if arg.typ.starts_with('...') {
|
|
|
|
s += '_V_FnVargs_$f.name *$arg.name'
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// s += '$arg.typ $arg.name'
|
|
|
|
s += table.cgen_name_type_pair(arg.name, arg.typ)// '$arg.typ $arg.name'
|
|
|
|
}
|
|
|
|
if i < f.args.len - 1 {
|
|
|
|
s += ', '
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
2019-09-13 13:10:24 +02:00
|
|
|
|
|
|
|
// find local function variable with closest name to `name`
|
2019-09-23 19:34:08 +02:00
|
|
|
fn (p &Parser) find_misspelled_local_var(name string, min_match f32) string {
|
2019-09-13 15:15:30 +02:00
|
|
|
mut closest := f32(0)
|
2019-09-13 13:10:24 +02:00
|
|
|
mut closest_var := ''
|
2019-09-23 19:34:08 +02:00
|
|
|
for var in p.local_vars {
|
|
|
|
if var.scope_level > p.cur_fn.scope_level {
|
2019-09-20 11:23:43 +02:00
|
|
|
continue
|
|
|
|
}
|
2019-09-15 19:07:12 +02:00
|
|
|
n := name.all_after('.')
|
|
|
|
if var.name == '' || (n.len - var.name.len > 2 || var.name.len - n.len > 2) { continue }
|
2019-10-25 15:34:12 +02:00
|
|
|
c := strings.dice_coefficient(var.name, n)
|
|
|
|
if c > closest {
|
|
|
|
closest = c
|
2019-09-15 19:07:12 +02:00
|
|
|
closest_var = var.name
|
2019-09-13 13:10:24 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-13 15:15:30 +02:00
|
|
|
return if closest >= min_match { closest_var } else { '' }
|
2019-09-13 13:10:24 +02:00
|
|
|
}
|
2019-09-28 23:21:10 +02:00
|
|
|
|
|
|
|
fn (fns []Fn) contains(f Fn) bool {
|
|
|
|
for ff in fns {
|
|
|
|
if ff.name == f.name {
|
|
|
|
return true
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-28 23:21:10 +02:00
|
|
|
return false
|
2019-10-20 09:19:37 +02:00
|
|
|
}
|
2019-10-21 13:21:30 +02:00
|
|
|
|
|
|
|
pub fn (f Fn) v_fn_module() string {
|
|
|
|
return f.mod
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (f Fn) v_fn_name() string {
|
|
|
|
return f.name.replace('${f.mod}__', '')
|
|
|
|
}
|
|
|
|
|