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)
// pthread.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
[trusted]
fn C.sem_init(voidptr, int, u32) int
[trusted]
fn C.sem_post(voidptr) int
[trusted]
fn C.sem_wait(voidptr) int
[trusted]
fn C.sem_trywait(voidptr) int
[trusted]
fn C.sem_timedwait(voidptr, voidptr) int
[trusted]
fn C.sem_destroy(voidptr) int
// MacOS semaphore functions

View File

@ -43,3 +43,60 @@ mut:
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_strong_ptr(voidptr, voidptr, voidptr) bool
fn C.atomic_exchange_ptr(voidptr, voidptr) voidptr
[trusted]
fn C.atomic_fetch_add_ptr(voidptr, voidptr) voidptr
[trusted]
fn C.atomic_fetch_sub_ptr(voidptr, voidptr) voidptr
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_strong_u16(voidptr, voidptr, u16) bool
fn C.atomic_exchange_u16(voidptr, u16) u16
[trusted]
fn C.atomic_fetch_add_u16(voidptr, u16) u16
[trusted]
fn C.atomic_fetch_sub_u16(voidptr, u16) u16
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_strong_u32(voidptr, voidptr, u32) bool
fn C.atomic_exchange_u32(voidptr, u32) u32
[trusted]
fn C.atomic_fetch_add_u32(voidptr, u32) u32
[trusted]
fn C.atomic_fetch_sub_u32(voidptr, u32) u32
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_strong_u64(voidptr, voidptr, u64) bool
fn C.atomic_exchange_u64(voidptr, u64) u64
[trusted]
fn C.atomic_fetch_add_u64(voidptr, u64) u64
[trusted]
fn C.atomic_fetch_sub_u64(voidptr, u64) u64
const (

View File

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

View File

@ -8,6 +8,25 @@ import time
#flag -lpthread
#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.
[heap]
pub struct Mutex {

View File

@ -9,6 +9,27 @@ import time
#include <semaphore.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.
[heap]
pub struct Mutex {

View File

@ -3,6 +3,9 @@
// that can be found in the LICENSE file.
module sync
[trusted]
fn C.atomic_fetch_add_u32(voidptr, u32) u32
// WaitGroup
// Do not copy an instance of WaitGroup, use a ref instead.
//

View File

@ -54,6 +54,31 @@ struct C.FOCUS_EVENT_RECORD {
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.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
}
mut name := ''
name_pos := p.tok.position()
if p.tok.kind == .name {
pos := p.tok.position()
// TODO high order fn
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 {
p.error_with_pos('function names cannot contain uppercase letters, use snake_case instead',
name_pos)
pos)
return ast.FnDecl{
scope: 0
}
@ -229,14 +229,14 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
type_sym := p.table.get_type_symbol(rec.typ)
// 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_) {
p.error_with_pos('duplicate method `$name`', name_pos)
p.error_with_pos('duplicate method `$name`', pos)
return ast.FnDecl{
scope: 0
}
}
// cannot redefine buildin function
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{
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.tok.position())
} 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{
scope: 0
}
@ -334,8 +335,8 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
} else {
name = p.prepend_mod(name)
}
if p.table.known_fn(name) {
p.fn_redefinition_error(name, name_pos)
if _ := p.table.find_fn(name) {
p.fn_redefinition_error(name)
}
// p.warn('reg functn $name ()')
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 {
return
}
@ -819,7 +820,7 @@ fn (mut p Parser) fn_redefinition_error(name string, pos token.Position) {
}
*/
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 {

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() {}