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-06-22 20:20:28 +02:00
|
|
|
module main
|
|
|
|
|
2019-08-17 21:19:37 +02:00
|
|
|
import strings
|
2019-07-03 22:11:27 +02:00
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
const (
|
|
|
|
MaxLocalVars = 50
|
|
|
|
)
|
|
|
|
|
|
|
|
struct Fn {
|
|
|
|
// addr int
|
|
|
|
mut:
|
2019-07-14 11:01:32 +02:00
|
|
|
name string
|
2019-08-05 09:49:52 +02:00
|
|
|
mod string
|
2019-06-22 20:20:28 +02:00
|
|
|
local_vars []Var
|
|
|
|
var_idx int
|
|
|
|
args []Var
|
|
|
|
is_interface bool
|
|
|
|
// called_fns []string
|
|
|
|
// idx int
|
|
|
|
scope_level int
|
|
|
|
typ string // return type
|
|
|
|
is_c bool
|
|
|
|
receiver_typ string
|
2019-07-15 22:09:34 +02:00
|
|
|
is_public bool
|
2019-06-22 20:20:28 +02:00
|
|
|
is_method bool
|
|
|
|
returns_error bool
|
|
|
|
is_decl bool // type myfn fn(int, int)
|
2019-08-07 17:51:21 +02:00
|
|
|
defer_text []string
|
2019-08-17 21:19:37 +02:00
|
|
|
//gen_types []string
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (f &Fn) find_var(name string) Var {
|
|
|
|
for i in 0 .. f.var_idx {
|
|
|
|
if f.local_vars[i].name == name {
|
|
|
|
return f.local_vars[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Var{}
|
|
|
|
}
|
|
|
|
|
2019-07-15 22:44:26 +02:00
|
|
|
|
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) {
|
|
|
|
for i, vv in p.cur_fn.local_vars {
|
2019-06-22 20:20:28 +02:00
|
|
|
if vv.name == v.name {
|
2019-09-09 15:22:39 +02:00
|
|
|
p.cur_fn.local_vars[i].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) {
|
|
|
|
for i, vv in p.cur_fn.local_vars {
|
2019-09-09 02:29:24 +02:00
|
|
|
if vv.name == v.name {
|
2019-09-09 15:22:39 +02:00
|
|
|
p.cur_fn.local_vars[i].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) {
|
|
|
|
for i, vv in p.cur_fn.local_vars {
|
2019-07-25 13:16:17 +02:00
|
|
|
if vv.name == v.name {
|
2019-09-09 15:22:39 +02:00
|
|
|
p.cur_fn.local_vars[i].is_changed = true
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 11:16:39 +02:00
|
|
|
fn (f mut Fn) known_var(name string) bool {
|
2019-06-22 20:20:28 +02:00
|
|
|
v := f.find_var(name)
|
|
|
|
return v.name.len > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (f mut Fn) register_var(v Var) {
|
|
|
|
new_var := {v | scope_level: f.scope_level}
|
|
|
|
// Expand the array
|
|
|
|
if f.var_idx >= f.local_vars.len {
|
|
|
|
f.local_vars << new_var
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
f.local_vars[f.var_idx] = new_var
|
|
|
|
}
|
2019-06-28 12:41:09 +02:00
|
|
|
f.var_idx++
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-06-28 17:15:52 +02:00
|
|
|
fn (f mut Fn) clear_vars() {
|
|
|
|
f.var_idx = 0
|
|
|
|
f.local_vars = []Var
|
|
|
|
}
|
|
|
|
|
2019-06-22 20:20:28 +02:00
|
|
|
// vlib header file?
|
|
|
|
fn (p mut Parser) is_sig() bool {
|
2019-09-10 16:36:14 +02:00
|
|
|
return (p.pref.build_mode == .default_mode || p.pref.build_mode == .build_module) &&
|
2019-07-16 17:42:04 +02:00
|
|
|
(p.file_path.contains(ModPath))
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
2019-09-09 15:22:39 +02:00
|
|
|
fn new_fn(mod string, is_public bool) Fn {
|
|
|
|
return Fn {
|
2019-08-05 09:49:52 +02:00
|
|
|
mod: mod
|
2019-09-14 22:48:30 +02:00
|
|
|
local_vars: [Var{}].repeat2(MaxLocalVars)
|
2019-06-26 12:56:49 +02:00
|
|
|
is_public: is_public
|
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() {
|
|
|
|
p.fgen('fn ')
|
2019-08-17 21:19:37 +02:00
|
|
|
//defer { p.fgenln('\n') }
|
|
|
|
is_pub := p.tok == .key_pub
|
|
|
|
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-06-22 20:20:28 +02:00
|
|
|
if is_pub {
|
|
|
|
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()
|
2019-07-07 22:30:15 +02:00
|
|
|
mut f := new_fn(p.mod, is_pub)
|
2019-06-22 20:20:28 +02:00
|
|
|
// 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()
|
|
|
|
T := p.table.find_type(receiver_typ)
|
2019-08-20 13:34:29 +02:00
|
|
|
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-08-05 09:49:52 +02:00
|
|
|
if !p.first_pass() && !p.builtin_mod && T.mod != p.mod {
|
2019-07-07 22:30:15 +02:00
|
|
|
println('T.mod=$T.mod')
|
2019-07-10 14:18:21 +02:00
|
|
|
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
|
|
|
|
//if !p.builtin_mod && receiver_typ.contains('*') {
|
|
|
|
if receiver_typ.contains('*') {
|
2019-06-22 20:20:28 +02:00
|
|
|
t := receiver_typ.replace('*', '')
|
|
|
|
p.error('use `($receiver_name mut $t)` instead of `($receiver_name *$t)`')
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
f.args << receiver
|
|
|
|
f.register_var(receiver)
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .plus || p.tok == .minus || p.tok == .mul {
|
2019-06-22 20:20:28 +02:00
|
|
|
f.name = p.tok.str()
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
f.name = p.check_name()
|
|
|
|
}
|
|
|
|
// 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-08-05 09:49:52 +02:00
|
|
|
// is_sig := p.builtin_mod && p.pref.build_mode == default_mode
|
|
|
|
// is_sig := p.pref.build_mode == default_mode && (p.builtin_mod || p.file.contains(LANG_TMP))
|
2019-06-22 20:20:28 +02:00
|
|
|
is_sig := p.is_sig()
|
2019-08-31 15:38:13 +02:00
|
|
|
// println('\n\nfn_decl() name=$f.name receiver_typ=$receiver_typ')
|
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-06-30 22:03:17 +02:00
|
|
|
else if !p.pref.translated && !p.file_path.contains('view.v') {
|
2019-06-22 20:20:28 +02:00
|
|
|
if contains_capital(f.name) {
|
|
|
|
p.error('function names cannot contain uppercase letters, use snake_case instead')
|
|
|
|
}
|
|
|
|
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-08-05 09:49:52 +02:00
|
|
|
if !is_c && !p.builtin_mod && p.mod != 'main' && receiver_typ.len == 0 {
|
|
|
|
f.name = p.prepend_mod(f.name)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
if p.first_pass() && p.table.known_fn(f.name) && receiver_typ.len == 0 {
|
2019-06-22 20:20:28 +02:00
|
|
|
existing_fn := p.table.find_fn(f.name)
|
|
|
|
// 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`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Generic?
|
|
|
|
mut is_generic := false
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok == .lt {
|
2019-07-29 18:21:36 +02:00
|
|
|
is_generic = true
|
2019-06-22 20:20:28 +02:00
|
|
|
p.next()
|
|
|
|
gen_type := p.check_name()
|
|
|
|
if gen_type != 'T' {
|
|
|
|
p.error('only `T` is allowed as a generic type for now')
|
|
|
|
}
|
2019-07-07 22:30:15 +02:00
|
|
|
p.check(.gt)
|
2019-08-17 21:19:37 +02:00
|
|
|
if p.first_pass() {
|
|
|
|
p.table.register_generic_fn(f.name)
|
|
|
|
} else {
|
|
|
|
//gen_types := p.table.fn_gen_types(f.name)
|
|
|
|
//println(gen_types)
|
|
|
|
}
|
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-07-07 22:30:15 +02:00
|
|
|
if p.tok == .name || p.tok == .mul || p.tok == .amp || p.tok == .lsbr ||
|
|
|
|
p.tok == .question {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgen(' ')
|
|
|
|
// TODO In
|
2019-07-07 22:30:15 +02:00
|
|
|
// if p.tok in [ .name, .mul, .amp, .lsbr ] {
|
2019-06-22 20:20:28 +02:00
|
|
|
typ = p.get_type()
|
|
|
|
}
|
|
|
|
// Translated C code can have empty functions (just definitions)
|
2019-08-17 21:19:37 +02:00
|
|
|
is_fn_header := !is_c && !is_sig && (p.pref.translated || p.pref.is_test) && 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
|
|
|
|
if !is_c && !is_sig && !is_fn_header {
|
|
|
|
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
|
|
|
|
mut str_args := f.str_args(p.table)
|
|
|
|
// Special case for main() args
|
|
|
|
if f.name == 'main' && !has_receiver {
|
2019-07-05 22:03:00 +02:00
|
|
|
if str_args != '' || typ != 'void' {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('fn main must have no arguments and no return values')
|
|
|
|
}
|
|
|
|
typ = 'int'
|
|
|
|
str_args = 'int argc, char** argv'
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
dll_export_linkage := if p.os == .msvc && p.attr == 'live' && p.pref.is_so {
|
|
|
|
'__declspec(dllexport) '
|
2019-08-18 03:44:04 +02:00
|
|
|
} else if p.attr == 'inline' {
|
|
|
|
'static inline '
|
2019-07-29 18:21:36 +02:00
|
|
|
} else {
|
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-06-30 22:03:17 +02:00
|
|
|
skip_main_in_test := f.name == 'main' && p.pref.is_test
|
2019-06-22 20:20:28 +02:00
|
|
|
if !is_c && !is_live && !is_sig && !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-08-17 21:19:37 +02:00
|
|
|
// Generate this function's body for all generic types
|
|
|
|
if is_generic {
|
|
|
|
gen_types := p.table.fn_gen_types(f.name)
|
2019-07-29 18:21:36 +02:00
|
|
|
// Remember current scanner position, go back here for each type
|
2019-08-17 21:19:37 +02:00
|
|
|
// TODO remove this once tokens are cached in `new_parser()`
|
|
|
|
cur_pos := p.scanner.pos
|
|
|
|
cur_tok := p.tok
|
|
|
|
cur_lit := p.lit
|
2019-07-29 18:21:36 +02:00
|
|
|
for gen_type in gen_types {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.genln('$dll_export_linkage$typ ${fn_name_cgen}_$gen_type($str_args) {')
|
|
|
|
p.genln('// T start $p.pass ${p.strtok()}')
|
|
|
|
p.cur_gen_type = gen_type // TODO support more than T
|
2019-07-29 18:21:36 +02:00
|
|
|
p.statements()
|
2019-08-17 21:19:37 +02:00
|
|
|
p.scanner.pos = cur_pos
|
|
|
|
p.tok = cur_tok
|
|
|
|
p.lit = cur_lit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
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-08-31 15:38:13 +02:00
|
|
|
p.table.add_method(receiver_t.name, f)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// println('register_fn typ=$typ isg=$is_generic')
|
|
|
|
p.table.register_fn(f)
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
if is_sig || 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-06-22 20:20:28 +02:00
|
|
|
if !is_sig && !is_fn_header {
|
2019-07-18 13:02:22 +02:00
|
|
|
mut opened_scopes := 0
|
|
|
|
mut closed_scopes := 0
|
2019-08-16 12:40:08 +02:00
|
|
|
mut temp_scanner_pos := 0
|
2019-06-22 20:20:28 +02:00
|
|
|
for {
|
2019-07-18 13:02:22 +02:00
|
|
|
if p.tok == .lcbr {
|
|
|
|
opened_scopes++
|
|
|
|
}
|
|
|
|
if p.tok == .rcbr {
|
|
|
|
closed_scopes++
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
// find `foo<Bar>()` in function bodies and register generic types
|
|
|
|
// TODO remove this once tokens are cached
|
2019-07-29 18:21:36 +02:00
|
|
|
if p.tok == .gt && p.prev_tok == .name && p.prev_tok2 == .lt &&
|
2019-08-16 12:40:08 +02:00
|
|
|
p.scanner.text[p.scanner.pos-1] != `T` {
|
|
|
|
temp_scanner_pos = p.scanner.pos
|
2019-08-17 21:19:37 +02:00
|
|
|
p.scanner.pos -= 3
|
|
|
|
for p.scanner.pos > 0 && (is_name_char(p.scanner.text[p.scanner.pos]) ||
|
2019-08-04 10:18:31 +02:00
|
|
|
p.scanner.text[p.scanner.pos] == `.` ||
|
2019-08-17 21:19:37 +02:00
|
|
|
p.scanner.text[p.scanner.pos] == `<` ) {
|
|
|
|
p.scanner.pos--
|
|
|
|
}
|
|
|
|
p.scanner.pos--
|
|
|
|
p.next()
|
|
|
|
// Run the function in the firt pass to register the generic type
|
|
|
|
p.name_expr()
|
2019-08-16 12:40:08 +02:00
|
|
|
p.scanner.pos = temp_scanner_pos
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-09-02 12:40:57 +02:00
|
|
|
if p.tok.is_decl() {
|
2019-06-22 20:20:28 +02:00
|
|
|
break
|
|
|
|
}
|
2019-07-29 18:21:36 +02:00
|
|
|
// fn body ended, and a new fn attribute declaration like [live] is starting?
|
2019-07-18 13:02:22 +02:00
|
|
|
if closed_scopes > opened_scopes && p.prev_tok == .rcbr {
|
|
|
|
if p.tok == .lsbr {
|
|
|
|
break
|
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-09-02 12:40:57 +02:00
|
|
|
p.next()
|
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-07-29 18:21:36 +02:00
|
|
|
if !is_c && f.name != 'main' && p.first_pass() {
|
2019-06-22 20:20:28 +02:00
|
|
|
// TODO hack to make Volt compile without -embed_vlib
|
2019-07-02 00:00:27 +02:00
|
|
|
if f.name == 'darwin__nsstring' && p.pref.build_mode == .default_mode {
|
2019-06-22 20:20:28 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
p.cgen.fns << fn_decl + ';'
|
|
|
|
}
|
|
|
|
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-06-22 20:20:28 +02:00
|
|
|
if f.name == 'main' || f.name == 'WinMain' {
|
|
|
|
p.genln('init_consts();')
|
2019-09-11 14:07:18 +02:00
|
|
|
if 'os' in p.table.imports {
|
2019-06-22 20:20:28 +02:00
|
|
|
if f.name == 'main' {
|
2019-06-25 23:43:04 +02:00
|
|
|
p.genln('os__args = os__init_os_args(argc, argv);')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
else if f.name == 'WinMain' {
|
2019-06-25 23:43:04 +02:00
|
|
|
p.genln('os__args = os__parse_windows_cmd_line(pCmdLine);')
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// We are in live code reload mode, call the .so loader in bg
|
2019-06-30 22:03:17 +02:00
|
|
|
if p.pref.is_live {
|
2019-08-17 21:19:37 +02:00
|
|
|
file_base := p.file_path.replace('.v', '')
|
2019-07-23 23:23:13 +02:00
|
|
|
if p.os != .windows && p.os != .msvc {
|
|
|
|
so_name := file_base + '.so'
|
2019-08-17 21:19:37 +02:00
|
|
|
p.genln('
|
|
|
|
load_so("$so_name");
|
2019-06-22 20:20:28 +02:00
|
|
|
pthread_t _thread_so;
|
|
|
|
pthread_create(&_thread_so , NULL, &reload_so, NULL); ')
|
2019-07-23 23:23:13 +02:00
|
|
|
} else {
|
2019-08-17 21:19:37 +02:00
|
|
|
so_name := file_base + if p.os == .msvc {'.dll'} else {'.so'}
|
2019-07-23 23:23:13 +02:00
|
|
|
p.genln('
|
|
|
|
live_fn_mutex = CreateMutexA(0, 0, 0);
|
|
|
|
load_so("$so_name");
|
|
|
|
unsigned long _thread_so;
|
|
|
|
_thread_so = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)&reload_so, 0, 0, 0);
|
|
|
|
')
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-06-30 22:03:17 +02:00
|
|
|
if p.pref.is_test && !p.scanner.file_path.contains('/volt') {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.error('tests cannot have function `main`')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// println('is_c=$is_c name=$f.name')
|
|
|
|
if is_c || is_sig || is_fn_header {
|
2019-07-07 22:30:15 +02:00
|
|
|
// println('IS SIG .key_returnING tok=${p.strtok()}')
|
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-06-30 22:03:17 +02:00
|
|
|
if p.pref.is_prof && f.name != 'main' && 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-08-17 21:19:37 +02:00
|
|
|
if is_generic {
|
|
|
|
// Don't need to generate body for the actual generic definition
|
|
|
|
p.cgen.nogen = true
|
|
|
|
}
|
2019-07-24 02:35:25 +02:00
|
|
|
p.statements_no_rcbr()
|
2019-08-17 21:19:37 +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-06-22 20:20:28 +02:00
|
|
|
if typ != 'void' && !p.returns && f.name != 'main' && f.name != 'WinMain' {
|
|
|
|
p.error('$f.name must return "$typ"')
|
|
|
|
}
|
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-08-17 21:19:37 +02:00
|
|
|
if !is_generic {
|
2019-07-29 18:21:36 +02:00
|
|
|
p.genln('}')
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
return
|
|
|
|
}
|
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-08-17 21:19:37 +02:00
|
|
|
if !is_generic {
|
2019-07-29 18:21:36 +02:00
|
|
|
p.genln('}')
|
2019-08-17 21:19:37 +02:00
|
|
|
}
|
2019-06-28 20:50:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (p mut Parser) check_unused_variables() {
|
|
|
|
for var in p.cur_fn.local_vars {
|
2019-06-22 20:20:28 +02:00
|
|
|
if var.name == '' {
|
|
|
|
break
|
|
|
|
}
|
2019-07-06 15:33:15 +02:00
|
|
|
if !var.is_used && !p.pref.is_repl && !var.is_arg && !p.pref.translated && var.name != '_' {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.scanner.line_nr = var.line_nr - 1
|
|
|
|
p.error('`$var.name` declared and not used')
|
|
|
|
}
|
2019-07-25 13:16:17 +02:00
|
|
|
if !var.is_changed && var.is_mut && !p.pref.is_repl && !var.is_arg && !p.pref.translated && var.name != '_' {
|
|
|
|
p.scanner.line_nr = var.line_nr - 1
|
2019-08-17 21:19:37 +02:00
|
|
|
p.error('`$var.name` is declared as mutable, but it was never changed')
|
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 += ','
|
|
|
|
}
|
|
|
|
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-07-24 17:46:41 +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-07-23 23:23:13 +02:00
|
|
|
if p.os != .windows && p.os != .msvc {
|
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-07-23 23:23:13 +02:00
|
|
|
if p.os == .windows || p.os == .msvc {
|
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-06-22 20:20:28 +02:00
|
|
|
fn (p mut Parser) fn_call(f Fn, method_ph int, receiver_var, receiver_type string) {
|
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-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-09-14 22:48:30 +02:00
|
|
|
mut cgen_name := p.table.fn_gen_name(f)
|
2019-07-29 18:21:36 +02:00
|
|
|
p.next()
|
2019-08-17 21:19:37 +02:00
|
|
|
mut gen_type := ''
|
2019-07-29 18:21:36 +02:00
|
|
|
if p.tok == .lt {
|
|
|
|
p.check(.lt)
|
2019-08-17 21:19:37 +02:00
|
|
|
gen_type = p.check_name()
|
|
|
|
// run<T> => run_App
|
2019-08-13 13:50:19 +02:00
|
|
|
if gen_type == 'T' && p.cur_gen_type != '' {
|
2019-08-17 21:19:37 +02:00
|
|
|
gen_type = p.cur_gen_type
|
|
|
|
}
|
|
|
|
// `foo<Bar>()`
|
|
|
|
// If we are in the first pass, we need to add `Bar` type to the generic function `foo`,
|
|
|
|
// so that generic `foo`s body can be generated for each type in the second pass.
|
2019-07-29 18:23:56 +02:00
|
|
|
if p.first_pass() {
|
2019-08-17 21:19:37 +02:00
|
|
|
println('registering $gen_type in $f.name fname=$f.name')
|
|
|
|
p.table.register_generic_fn_type(f.name, gen_type)
|
|
|
|
// Function bodies are skipped in the first passed, we only need to register the generic type here.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cgen_name += '_' + gen_type
|
2019-07-29 18:21:36 +02:00
|
|
|
p.check(.gt)
|
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
|
|
|
|
// }
|
|
|
|
// Normal function call
|
|
|
|
if !f.is_method {
|
|
|
|
p.gen(cgen_name)
|
|
|
|
p.gen('(')
|
|
|
|
// p.fgen(f.name)
|
|
|
|
}
|
|
|
|
// If we have a method placeholder,
|
|
|
|
// we need to preappend "method(receiver, ...)"
|
|
|
|
else {
|
|
|
|
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-08-08 06:55:08 +02:00
|
|
|
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-09-14 22:48:30 +02:00
|
|
|
p.gen_method_call(receiver_type, f.typ, cgen_name, receiver, method_ph)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
2019-08-17 21:19:37 +02:00
|
|
|
// foo<Bar>()
|
2019-07-29 18:21:36 +02:00
|
|
|
p.fn_call_args(mut f)
|
2019-06-22 20:20:28 +02:00
|
|
|
p.gen(')')
|
|
|
|
p.calling_c = false
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
v := Var {
|
|
|
|
typ: typ
|
|
|
|
is_arg: true
|
|
|
|
// is_mut: is_mut
|
|
|
|
line_nr: p.scanner.line_nr
|
|
|
|
}
|
|
|
|
// 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-06-22 20:20:28 +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 {
|
|
|
|
p.next()
|
|
|
|
}
|
2019-07-24 15:24:32 +02:00
|
|
|
mut typ := p.get_type()
|
|
|
|
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
|
|
|
}
|
|
|
|
v := Var {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
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-07-07 22:30:15 +02:00
|
|
|
if p.tok == .dotdot {
|
2019-06-22 20:20:28 +02:00
|
|
|
f.args << Var {
|
|
|
|
name: '..'
|
|
|
|
}
|
|
|
|
p.next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 21:19:37 +02:00
|
|
|
// foo *(1, 2, 3, mut bar)*
|
2019-09-01 21:51:16 +02:00
|
|
|
fn (p mut Parser) fn_call_args(f mut Fn) &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-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-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-06-22 20:20:28 +02:00
|
|
|
return f
|
|
|
|
}
|
2019-07-30 15:08:14 +02:00
|
|
|
// add debug information to panic when -debug 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}__', '')
|
|
|
|
file_path := p.file_path.replace('\\', '\\\\') // escape \
|
|
|
|
p.cgen.resetln(p.cgen.cur_line.replace(
|
|
|
|
'v_panic (',
|
2019-08-25 00:12:11 +02:00
|
|
|
'_panic_debug ($p.scanner.line_nr, tos2((byte *)"$file_path"), tos2((byte *)"$mod_name"), tos2((byte *)"$fn_name"), '
|
2019-07-30 15:08:14 +02:00
|
|
|
))
|
|
|
|
}
|
2019-06-22 20:20:28 +02:00
|
|
|
// Receiver - first arg
|
|
|
|
for i, arg in f.args {
|
|
|
|
// println('$i) arg=$arg.name')
|
|
|
|
// Skip receiver, because it was already generated in the expression
|
|
|
|
if i == 0 && f.is_method {
|
2019-09-14 22:48:30 +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
|
|
|
|
if i == f.args.len - 1 && arg.name == '..' {
|
|
|
|
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-08-10 23:02:48 +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
|
|
|
|
v := p.cur_fn.find_var(var_name)
|
|
|
|
if v.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-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-06-22 20:20:28 +02:00
|
|
|
typ := p.bool_expression()
|
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-08-29 23:40:46 +02:00
|
|
|
if i == 0 && (f.name == 'println' || f.name == 'print') && typ != 'string' && 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-08-29 23:40:46 +02:00
|
|
|
p.cgen.resetln(p.cgen.cur_line.replace(f.name + ' (', '/*opt*/printf ("' + fmt + '\\n", '))
|
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-07-10 14:26:37 +02:00
|
|
|
error_msg := ('`$typ` needs to have method `str() string` to be printable')
|
2019-07-04 00:44:57 +02:00
|
|
|
if T.fields.len > 0 {
|
|
|
|
mut index := p.cgen.cur_line.len - 1
|
2019-08-29 23:36:29 +02:00
|
|
|
for index > 0 && p.cgen.cur_line[index - 1] != `(` { index-- }
|
2019-07-04 00:44:57 +02:00
|
|
|
name := p.cgen.cur_line.right(index + 1)
|
|
|
|
if name == '}' {
|
2019-08-17 21:19:37 +02:00
|
|
|
p.error(error_msg)
|
2019-06-27 00:12:55 +02:00
|
|
|
}
|
2019-07-18 20:21:23 +02:00
|
|
|
p.cgen.resetln(p.cgen.cur_line.left(index))
|
2019-07-29 18:21:36 +02:00
|
|
|
p.scanner.create_type_string(T, name)
|
2019-07-04 00:44:57 +02:00
|
|
|
p.cgen.cur_line.replace(typ, '')
|
|
|
|
p.next()
|
2019-07-29 18:21:36 +02:00
|
|
|
return p.fn_call_args(mut f)
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
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
|
|
|
|
// println('fn arg got="$got" exp="$expected"')
|
|
|
|
if !p.check_types_no_throw(got, expected) {
|
|
|
|
mut err := 'Fn "$f.name" wrong arg #${i+1}. '
|
|
|
|
err += 'Expected "$arg.typ" ($arg.name) but got "$typ"'
|
|
|
|
p.error(err)
|
|
|
|
}
|
|
|
|
is_interface := p.table.is_interface(arg.typ)
|
2019-08-07 12:09:24 +02:00
|
|
|
// Add `&` or `*` before an argument?
|
2019-06-22 20:20:28 +02:00
|
|
|
if !is_interface {
|
|
|
|
// Dereference
|
|
|
|
if got.contains('*') && !expected.contains('*') {
|
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.
|
|
|
|
if !got.contains('*') && expected.contains('*') && got != 'voidptr' {
|
2019-08-07 12:57:07 +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.
|
|
|
|
if expected.starts_with('array_') && expected.ends_with('*') {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// interface?
|
|
|
|
if is_interface {
|
|
|
|
if !got.contains('*') {
|
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
|
|
|
|
is_vararg := i == f.args.len - 2 && f.args[i + 1].name == '..'
|
2019-07-07 22:30:15 +02:00
|
|
|
if p.tok != .comma && !is_vararg {
|
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-07-07 22:30:15 +02:00
|
|
|
if p.tok == .comma {
|
2019-06-22 20:20:28 +02:00
|
|
|
p.fgen(', ')
|
|
|
|
}
|
|
|
|
if !is_vararg {
|
|
|
|
p.next()
|
|
|
|
p.gen(',')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// varargs
|
|
|
|
if f.args.len > 0 {
|
|
|
|
last_arg := f.args.last()
|
|
|
|
if last_arg.name == '..' {
|
2019-07-07 22:30:15 +02:00
|
|
|
for p.tok != .rpar {
|
|
|
|
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
|
|
|
}
|
|
|
|
p.bool_expression()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-06-22 20:20:28 +02:00
|
|
|
// p.gen(')')
|
2019-08-08 09:49:56 +02:00
|
|
|
return f // TODO is return f right?
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// "fn (int, string) int"
|
|
|
|
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 {
|
|
|
|
for a in method.args.right(1) {
|
|
|
|
s += ', $a.typ'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s += ')'
|
2019-06-22 20:20:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if arg.name == '..' {
|
|
|
|
s += '...'
|
|
|
|
}
|
|
|
|
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-13 15:15:30 +02:00
|
|
|
fn (f &Fn) find_misspelled_local_var(name string, min_match f32) string {
|
|
|
|
mut closest := f32(0)
|
2019-09-13 13:10:24 +02:00
|
|
|
mut closest_var := ''
|
|
|
|
for var in f.local_vars {
|
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 }
|
|
|
|
p := strings.dice_coefficient(var.name, n)
|
2019-09-13 13:10:24 +02:00
|
|
|
if p > closest {
|
|
|
|
closest = p
|
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
|
|
|
}
|