Revert "parser: make duplicated functions an error (#8792)"

This reverts commit 21bf8fe14e.
pull/8814/head
Delyan Angelov 2021-02-18 09:02:56 +02:00
parent 21bf8fe14e
commit bf6e9ff95a
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
11 changed files with 163 additions and 33 deletions

View File

@ -346,39 +346,54 @@ fn C.ReleaseSRWLockShared(voidptr)
fn C.ReleaseSRWLockExclusive(voidptr) fn C.ReleaseSRWLockExclusive(voidptr)
// pthread.h // pthread.h
[trusted]
fn C.pthread_mutex_init(voidptr, voidptr) int fn C.pthread_mutex_init(voidptr, voidptr) int
fn C.pthread_mutex_lock(voidptr) int fn C.pthread_mutex_lock(voidptr) int
fn C.pthread_mutex_unlock(voidptr) int fn C.pthread_mutex_unlock(voidptr) int
fn C.pthread_mutex_destroy(voidptr) int fn C.pthread_mutex_destroy(voidptr) int
fn C.pthread_rwlockattr_init(voidptr) int fn C.pthread_rwlockattr_init(voidptr) int
fn C.pthread_rwlockattr_setkind_np(voidptr, int) int fn C.pthread_rwlockattr_setkind_np(voidptr, int) int
fn C.pthread_rwlockattr_setpshared(voidptr, int) int fn C.pthread_rwlockattr_setpshared(voidptr, int) int
fn C.pthread_rwlock_init(voidptr, voidptr) int fn C.pthread_rwlock_init(voidptr, voidptr) int
fn C.pthread_rwlock_rdlock(voidptr) int fn C.pthread_rwlock_rdlock(voidptr) int
fn C.pthread_rwlock_wrlock(voidptr) int fn C.pthread_rwlock_wrlock(voidptr) int
fn C.pthread_rwlock_unlock(voidptr) int fn C.pthread_rwlock_unlock(voidptr) int
fn C.pthread_condattr_init(voidptr) int fn C.pthread_condattr_init(voidptr) int
fn C.pthread_condattr_setpshared(voidptr, int) int fn C.pthread_condattr_setpshared(voidptr, int) int
fn C.pthread_condattr_destroy(voidptr) int fn C.pthread_condattr_destroy(voidptr) int
fn C.pthread_cond_init(voidptr, voidptr) int fn C.pthread_cond_init(voidptr, voidptr) int
fn C.pthread_cond_signal(voidptr) int fn C.pthread_cond_signal(voidptr) int
fn C.pthread_cond_wait(voidptr, voidptr) int fn C.pthread_cond_wait(voidptr, voidptr) int
fn C.pthread_cond_timedwait(voidptr, voidptr, voidptr) int fn C.pthread_cond_timedwait(voidptr, voidptr, voidptr) int
fn C.pthread_cond_destroy(voidptr) int fn C.pthread_cond_destroy(voidptr) int
[trusted]
fn C.sem_init(voidptr, int, u32) int fn C.sem_init(voidptr, int, u32) int
[trusted]
fn C.sem_post(voidptr) int fn C.sem_post(voidptr) int
[trusted]
fn C.sem_wait(voidptr) int fn C.sem_wait(voidptr) int
[trusted]
fn C.sem_trywait(voidptr) int fn C.sem_trywait(voidptr) int
[trusted]
fn C.sem_timedwait(voidptr, voidptr) int fn C.sem_timedwait(voidptr, voidptr) int
[trusted]
fn C.sem_destroy(voidptr) int fn C.sem_destroy(voidptr) int
// MacOS semaphore functions // MacOS semaphore functions

View File

@ -43,3 +43,60 @@ mut:
struct C.sockaddr_storage { struct C.sockaddr_storage {
} }
fn C.socket() int
fn C.setsockopt() int
fn C.htonl() int
fn C.htons() int
fn C.bind() int
fn C.listen() int
fn C.accept() int
fn C.getaddrinfo() int
fn C.connect() int
fn C.send() int
fn C.sendto() int
fn C.recv() int
fn C.recvfrom() int
fn C.shutdown() int
fn C.ntohs() int
fn C.getpeername() int
fn C.inet_ntop(af int, src voidptr, dst charptr, dst_size int) charptr
fn C.WSAAddressToStringA() int
fn C.getsockname() int
// defined in builtin
// fn C.read() int
// fn C.close() int
fn C.ioctlsocket() int
fn C.fcntl() int
fn C.@select() int
fn C.FD_ZERO()
fn C.FD_SET()
fn C.FD_ISSET() bool
[typedef]
struct C.fd_set {}

View File

@ -24,9 +24,7 @@ fn C.atomic_store_ptr(voidptr, voidptr)
fn C.atomic_compare_exchange_weak_ptr(voidptr, voidptr, voidptr) bool fn C.atomic_compare_exchange_weak_ptr(voidptr, voidptr, voidptr) bool
fn C.atomic_compare_exchange_strong_ptr(voidptr, voidptr, voidptr) bool fn C.atomic_compare_exchange_strong_ptr(voidptr, voidptr, voidptr) bool
fn C.atomic_exchange_ptr(voidptr, voidptr) voidptr fn C.atomic_exchange_ptr(voidptr, voidptr) voidptr
[trusted]
fn C.atomic_fetch_add_ptr(voidptr, voidptr) voidptr fn C.atomic_fetch_add_ptr(voidptr, voidptr) voidptr
[trusted]
fn C.atomic_fetch_sub_ptr(voidptr, voidptr) voidptr fn C.atomic_fetch_sub_ptr(voidptr, voidptr) voidptr
fn C.atomic_load_u16(voidptr) u16 fn C.atomic_load_u16(voidptr) u16
@ -34,9 +32,7 @@ fn C.atomic_store_u16(voidptr, u16)
fn C.atomic_compare_exchange_weak_u16(voidptr, voidptr, u16) bool fn C.atomic_compare_exchange_weak_u16(voidptr, voidptr, u16) bool
fn C.atomic_compare_exchange_strong_u16(voidptr, voidptr, u16) bool fn C.atomic_compare_exchange_strong_u16(voidptr, voidptr, u16) bool
fn C.atomic_exchange_u16(voidptr, u16) u16 fn C.atomic_exchange_u16(voidptr, u16) u16
[trusted]
fn C.atomic_fetch_add_u16(voidptr, u16) u16 fn C.atomic_fetch_add_u16(voidptr, u16) u16
[trusted]
fn C.atomic_fetch_sub_u16(voidptr, u16) u16 fn C.atomic_fetch_sub_u16(voidptr, u16) u16
fn C.atomic_load_u32(voidptr) u32 fn C.atomic_load_u32(voidptr) u32
@ -44,9 +40,7 @@ fn C.atomic_store_u32(voidptr, u32)
fn C.atomic_compare_exchange_weak_u32(voidptr, voidptr, u32) bool fn C.atomic_compare_exchange_weak_u32(voidptr, voidptr, u32) bool
fn C.atomic_compare_exchange_strong_u32(voidptr, voidptr, u32) bool fn C.atomic_compare_exchange_strong_u32(voidptr, voidptr, u32) bool
fn C.atomic_exchange_u32(voidptr, u32) u32 fn C.atomic_exchange_u32(voidptr, u32) u32
[trusted]
fn C.atomic_fetch_add_u32(voidptr, u32) u32 fn C.atomic_fetch_add_u32(voidptr, u32) u32
[trusted]
fn C.atomic_fetch_sub_u32(voidptr, u32) u32 fn C.atomic_fetch_sub_u32(voidptr, u32) u32
fn C.atomic_load_u64(voidptr) u64 fn C.atomic_load_u64(voidptr) u64
@ -54,9 +48,7 @@ fn C.atomic_store_u64(voidptr, u64)
fn C.atomic_compare_exchange_weak_u64(voidptr, voidptr, u64) bool fn C.atomic_compare_exchange_weak_u64(voidptr, voidptr, u64) bool
fn C.atomic_compare_exchange_strong_u64(voidptr, voidptr, u64) bool fn C.atomic_compare_exchange_strong_u64(voidptr, voidptr, u64) bool
fn C.atomic_exchange_u64(voidptr, u64) u64 fn C.atomic_exchange_u64(voidptr, u64) u64
[trusted]
fn C.atomic_fetch_add_u64(voidptr, u64) u64 fn C.atomic_fetch_add_u64(voidptr, u64) u64
[trusted]
fn C.atomic_fetch_sub_u64(voidptr, u64) u64 fn C.atomic_fetch_sub_u64(voidptr, u64) u64
const ( const (
@ -367,7 +359,7 @@ pub fn (mut ch Channel) try_pop(dest voidptr) ChanState {
} }
fn (mut ch Channel) try_pop_priv(dest voidptr, no_block bool) ChanState { fn (mut ch Channel) try_pop_priv(dest voidptr, no_block bool) ChanState {
spinloops_sem_, spinloops_ := if no_block { 1, 1 } else { spinloops, spinloops_sem } spinloops_sem_, spinloops_ := if no_block { 1, 1 } else { spinloops, spinloops_sem }
mut have_swapped := false mut have_swapped := false
mut write_in_progress := false mut write_in_progress := false
for { for {
@ -521,7 +513,7 @@ fn (mut ch Channel) try_pop_priv(dest voidptr, no_block bool) ChanState {
dest2 = dest dest2 = dest
} }
} }
break break
} }
return .success return .success
} }

View File

@ -4,6 +4,9 @@ import sync
import runtime import runtime
[trusted]
fn C.atomic_fetch_add_u32(voidptr, u32) u32
pub const ( pub const (
no_result = voidptr(0) no_result = voidptr(0)
) )

View File

@ -8,6 +8,25 @@ import time
#flag -lpthread #flag -lpthread
#include <semaphore.h> #include <semaphore.h>
[trusted]
fn C.pthread_mutex_init(voidptr, voidptr) int
fn C.pthread_mutex_lock(voidptr) int
fn C.pthread_mutex_unlock(voidptr) int
fn C.pthread_mutex_destroy(voidptr) int
fn C.pthread_rwlockattr_init(voidptr) int
fn C.pthread_rwlockattr_setkind_np(voidptr, int) int
fn C.pthread_rwlockattr_setpshared(voidptr, int) int
fn C.pthread_rwlock_init(voidptr, voidptr) int
fn C.pthread_rwlock_rdlock(voidptr) int
fn C.pthread_rwlock_wrlock(voidptr) int
fn C.pthread_rwlock_unlock(voidptr) int
fn C.sem_init(voidptr, int, u32) int
fn C.sem_post(voidptr) int
fn C.sem_wait(voidptr) int
fn C.sem_trywait(voidptr) int
fn C.sem_timedwait(voidptr, voidptr) int
fn C.sem_destroy(voidptr) int
// [init_with=new_mutex] // TODO: implement support for this struct attribute, and disallow Mutex{} from outside the sync.new_mutex() function. // [init_with=new_mutex] // TODO: implement support for this struct attribute, and disallow Mutex{} from outside the sync.new_mutex() function.
[heap] [heap]
pub struct Mutex { pub struct Mutex {

View File

@ -9,6 +9,27 @@ import time
#include <semaphore.h> #include <semaphore.h>
#include <sys/errno.h> #include <sys/errno.h>
[trusted]
fn C.pthread_mutex_init(voidptr, voidptr) int
fn C.pthread_mutex_lock(voidptr) int
fn C.pthread_mutex_unlock(voidptr) int
fn C.pthread_mutex_destroy(voidptr) int
fn C.pthread_rwlockattr_init(voidptr) int
fn C.pthread_rwlockattr_setkind_np(voidptr, int) int
fn C.pthread_rwlockattr_setpshared(voidptr, int) int
fn C.pthread_rwlock_init(voidptr, voidptr) int
fn C.pthread_rwlock_rdlock(voidptr) int
fn C.pthread_rwlock_wrlock(voidptr) int
fn C.pthread_rwlock_unlock(voidptr) int
fn C.pthread_condattr_init(voidptr) int
fn C.pthread_condattr_setpshared(voidptr, int) int
fn C.pthread_condattr_destroy(voidptr) int
fn C.pthread_cond_init(voidptr, voidptr) int
fn C.pthread_cond_signal(voidptr) int
fn C.pthread_cond_wait(voidptr, voidptr) int
fn C.pthread_cond_timedwait(voidptr, voidptr, voidptr) int
fn C.pthread_cond_destroy(voidptr) int
// [init_with=new_mutex] // TODO: implement support for this struct attribute, and disallow Mutex{} from outside the sync.new_mutex() function. // [init_with=new_mutex] // TODO: implement support for this struct attribute, and disallow Mutex{} from outside the sync.new_mutex() function.
[heap] [heap]
pub struct Mutex { pub struct Mutex {

View File

@ -3,6 +3,9 @@
// that can be found in the LICENSE file. // that can be found in the LICENSE file.
module sync module sync
[trusted]
fn C.atomic_fetch_add_u32(voidptr, u32) u32
// WaitGroup // WaitGroup
// Do not copy an instance of WaitGroup, use a ref instead. // Do not copy an instance of WaitGroup, use a ref instead.
// //
@ -12,7 +15,7 @@ module sync
// `wg.wait()` to wait for all jobs to have finished // `wg.wait()` to wait for all jobs to have finished
// //
// in each parallel job: // in each parallel job:
// `wg.done()` when finished // `wg.done()` when finished
// //
// [init_with=new_waitgroup] // TODO: implement support for init_with struct attribute, and disallow WaitGroup{} from outside the sync.new_waitgroup() function. // [init_with=new_waitgroup] // TODO: implement support for init_with struct attribute, and disallow WaitGroup{} from outside the sync.new_waitgroup() function.
[heap] [heap]

View File

@ -54,6 +54,31 @@ struct C.FOCUS_EVENT_RECORD {
bSetFocus int bSetFocus int
} }
[typedef]
struct C.COORD {
X i16
Y i16
}
[typedef]
struct C.SMALL_RECT {
Left u16
Top u16
Right u16
Bottom u16
}
[typedef]
struct C.CONSOLE_SCREEN_BUFFER_INFO {
dwSize C.COORD
dwCursorPosition C.COORD
wAttributes u16
srWindow C.SMALL_RECT
dwMaximumWindowSize C.COORD
}
fn C.ReadConsoleInput() bool fn C.ReadConsoleInput() bool
fn C.GetNumberOfConsoleInputEvents() bool fn C.GetNumberOfConsoleInputEvents() bool
fn C.GetConsoleScreenBufferInfo(handle os.HANDLE, info &C.CONSOLE_SCREEN_BUFFER_INFO) bool

View File

@ -215,13 +215,13 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
language = rec.language language = rec.language
} }
mut name := '' mut name := ''
name_pos := p.tok.position()
if p.tok.kind == .name { if p.tok.kind == .name {
pos := p.tok.position()
// TODO high order fn // TODO high order fn
name = if language == .js { p.check_js_name() } else { p.check_name() } name = if language == .js { p.check_js_name() } else { p.check_name() }
if language == .v && !p.pref.translated && util.contains_capital(name) && !p.builtin_mod { if language == .v && !p.pref.translated && util.contains_capital(name) && !p.builtin_mod {
p.error_with_pos('function names cannot contain uppercase letters, use snake_case instead', p.error_with_pos('function names cannot contain uppercase letters, use snake_case instead',
name_pos) pos)
return ast.FnDecl{ return ast.FnDecl{
scope: 0 scope: 0
} }
@ -229,14 +229,14 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
type_sym := p.table.get_type_symbol(rec.typ) type_sym := p.table.get_type_symbol(rec.typ)
// interfaces are handled in the checker, methods can not be defined on them this way // interfaces are handled in the checker, methods can not be defined on them this way
if is_method && (type_sym.has_method(name) && type_sym.kind != .interface_) { if is_method && (type_sym.has_method(name) && type_sym.kind != .interface_) {
p.error_with_pos('duplicate method `$name`', name_pos) p.error_with_pos('duplicate method `$name`', pos)
return ast.FnDecl{ return ast.FnDecl{
scope: 0 scope: 0
} }
} }
// cannot redefine buildin function // cannot redefine buildin function
if !is_method && !p.builtin_mod && name in builtin_functions { if !is_method && !p.builtin_mod && name in builtin_functions {
p.error_with_pos('cannot redefine builtin function `$name`', name_pos) p.error_with_pos('cannot redefine builtin function `$name`', pos)
return ast.FnDecl{ return ast.FnDecl{
scope: 0 scope: 0
} }
@ -252,7 +252,8 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
p.error_with_pos('cannot overload `!=`, `>`, `<=` and `>=` as they are auto generated from `==` and`<`', p.error_with_pos('cannot overload `!=`, `>`, `<=` and `>=` as they are auto generated from `==` and`<`',
p.tok.position()) p.tok.position())
} else { } else {
p.error_with_pos('expecting method name', name_pos) pos := p.tok.position()
p.error_with_pos('expecting method name', pos)
return ast.FnDecl{ return ast.FnDecl{
scope: 0 scope: 0
} }
@ -334,8 +335,8 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
} else { } else {
name = p.prepend_mod(name) name = p.prepend_mod(name)
} }
if p.table.known_fn(name) { if _ := p.table.find_fn(name) {
p.fn_redefinition_error(name, name_pos) p.fn_redefinition_error(name)
} }
// p.warn('reg functn $name ()') // p.warn('reg functn $name ()')
p.table.register_fn(table.Fn{ p.table.register_fn(table.Fn{
@ -807,7 +808,7 @@ fn (mut p Parser) check_fn_atomic_arguments(typ table.Type, pos token.Position)
} }
} }
fn (mut p Parser) fn_redefinition_error(name string, pos token.Position) { fn (mut p Parser) fn_redefinition_error(name string) {
if p.pref.translated { if p.pref.translated {
return return
} }
@ -819,7 +820,7 @@ fn (mut p Parser) fn_redefinition_error(name string, pos token.Position) {
} }
*/ */
p.table.redefined_fns << name p.table.redefined_fns << name
p.error_with_pos('redefinition of function `$name`', pos) // p.error('redefinition of function `$name`')
} }
fn have_fn_main(stmts []ast.Stmt) bool { fn have_fn_main(stmts []ast.Stmt) bool {

View File

@ -1,4 +0,0 @@
vlib/v/parser/tests/fn_duplicate.vv:2:4: error: redefinition of function `foo`
1 | fn foo() {}
2 | fn foo() {}
| ~~~

View File

@ -1,2 +0,0 @@
fn foo() {}
fn foo() {}