From c860bac7bf0c9a1d6045bdd56dbb79946c973d73 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Wed, 26 Jun 2019 12:56:49 +0200 Subject: [PATCH] don't allow calling private functions/methods --- compiler/cgen.v | 2 +- compiler/fn.v | 10 +++++++--- compiler/main.v | 2 +- compiler/parser.v | 9 +++------ compiler/token.v | 2 +- os/os.v | 18 +++++++++--------- os/os_mac.v | 4 ++-- rand/rand.v | 20 +++++++++++++++----- 8 files changed, 39 insertions(+), 28 deletions(-) diff --git a/compiler/cgen.v b/compiler/cgen.v index 1df0e115df..ece393f7eb 100644 --- a/compiler/cgen.v +++ b/compiler/cgen.v @@ -33,7 +33,7 @@ mut: fn new_cgen(out_name_c string) *CGen { gen := &CGen { out_path: '$TmpPath/$out_name_c' - out: os.create_file('$TmpPath/$out_name_c') + out: os.create('$TmpPath/$out_name_c') } return gen } diff --git a/compiler/fn.v b/compiler/fn.v index 78300b3a62..4a16d9a673 100644 --- a/compiler/fn.v +++ b/compiler/fn.v @@ -23,7 +23,7 @@ mut: name string is_c bool receiver_typ string - is_private bool + is_public bool is_method bool returns_error bool is_decl bool // type myfn fn(int, int) @@ -94,11 +94,12 @@ fn (p mut Parser) is_sig() bool { (p.file_path.contains(TmpPath)) } -fn new_fn(pkg string) *Fn { +fn new_fn(pkg string, is_public bool) *Fn { mut f := &Fn { pkg: pkg local_vars: [Var{} ; MaxLocalVars] + is_public: is_public } return f } @@ -112,7 +113,7 @@ fn (p mut Parser) fn_decl() { } p.returns = false p.next() - mut f := new_fn(p.pkg) + mut f := new_fn(p.pkg, is_pub) // Method receiver mut receiver_typ := '' if p.tok == LPAR { @@ -493,6 +494,9 @@ fn (p mut Parser) async_fn_call(f Fn, method_ph int, receiver_var, receiver_type } fn (p mut Parser) fn_call(f Fn, method_ph int, receiver_var, receiver_type string) { + if !f.is_public && !f.is_c && f.pkg != p.pkg && f.pkg != 'builtin' { + p.error('$p.run function `$f.name` is private $f.is_public') + } p.calling_c = f.is_c is_print := p.is_prod &&// Hide prints only in prod !p.is_test && diff --git a/compiler/main.v b/compiler/main.v index 06e4e2a163..f87eb20a0d 100644 --- a/compiler/main.v +++ b/compiler/main.v @@ -515,7 +515,7 @@ mut args := '' '/usr/lib/x86_64-linux-gnu/crtn.o') println(ress) if ress.contains('error:') { - os.exit(1) + exit(1) } println('linux cross compilation done. resulting binary: "$c.out_name"') } diff --git a/compiler/parser.v b/compiler/parser.v index 370e818b93..cf1cbb8faa 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -511,9 +511,6 @@ fn (p mut Parser) struct_decl() { } // `pub` access mod access_mod := if is_pub{PUBLIC} else { PRIVATE} - if typ.name == 'Userf' { - println('$field_name $access_mod mut=$is_mut') - } p.fgen(' ') field_type := p.get_type() is_atomic := p.tok == ATOMIC @@ -644,8 +641,8 @@ fn (p mut Parser) check(expected Token) { fn (p mut Parser) error(s string) { // Dump all vars and types for debugging if false { - file_types := os.create_file('$TmpPath/types') - file_vars := os.create_file('$TmpPath/vars') + file_types := os.create('$TmpPath/types') + file_vars := os.create('$TmpPath/vars') // ////debug("ALL T", q.J(p.table.types)) // os.write_to_file('/var/tmp/lang.types', '')//pes(p.table.types)) // //debug("ALL V", q.J(p.table.vars)) @@ -662,7 +659,7 @@ fn (p mut Parser) error(s string) { if p.file_path.contains('v/compiler') || cur_path.contains('v/compiler') { println('\n====================') println('It looks like you are building V. It is being frequently updated every day.' + - ' Most likely there was a change that lead to this error. ') + ' If you didn\'t modify the compiler\'s code, most likely there was a change that lead to this error. ') println('Try to run `git pull`, that will most likely fix it.') println('If `git pull` doesn\'t help, re-install V from source or download a precompiled' + ' binary from https://vlang.io') diff --git a/compiler/token.v b/compiler/token.v index 55e6795947..05a00bc2f1 100644 --- a/compiler/token.v +++ b/compiler/token.v @@ -239,7 +239,7 @@ fn (t Token) str() string { fn (t Token) is_decl() bool { // TODO return t in [FUNC ,TIP, CONST, IMPORT_CONST ,AT ,EOF] return t == ENUM || t == INTERFACE || t == FUNC || t == STRUCT || t == TIP || - t == CONST || t == IMPORT_CONST || t == AT || t == EOF + t == CONST || t == IMPORT_CONST || t == AT || t == PUB || t == EOF } const ( diff --git a/os/os.v b/os/os.v index 9cb1451a26..696949a874 100644 --- a/os/os.v +++ b/os/os.v @@ -21,6 +21,12 @@ import const ( SEEK_END ) +fn C.getline(voidptr, voidptr, voidptr) int + +fn C.ftell(fp voidptr) int + +fn todo_remove(){} + fn init_os_args(argc int, _argv *byteptr) []string { mut args := []string # char** argv = (char**) _argv; @@ -38,8 +44,6 @@ fn parse_windows_cmd_line(cmd byteptr) []string { return s.split(' ') } -fn C.ftell(fp voidptr) int - // read_file reads the file in `path` and returns the contents. pub fn read_file(path string) ?string { mut res := '' @@ -185,6 +189,7 @@ pub fn open_append(path string) File { return create_file(path) } +// TODO remove fn create_file(file string) File { return create_file2(file, 'w') } @@ -227,7 +232,7 @@ fn (f File) write_at(data voidptr, size, pos int) { C.fseek(f.cfile, 0, SEEK_END) } -fn (f File) appendln(s string) { +pub fn (f File) appendln(s string) { // C.fwrite(s.str, 1, s.len, f.cfile) // ss := s.clone() // TODO perf @@ -236,7 +241,7 @@ fn (f File) appendln(s string) { C.fputs('\n', f.cfile) } -fn (f File) close() { +pub fn (f File) close() { C.fclose(f.cfile) } @@ -319,10 +324,6 @@ pub fn getenv(key string) string { return tos2(s) } -fn exit(code int) { - C.exit(code) -} - // `file_exists` returns true if `path` exists. pub fn file_exists(path string) bool { // # return access( path.str, F_OK ) != -1 ; @@ -397,7 +398,6 @@ pub fn filename(path string) string { return path.all_after('/') } -fn C.getline(voidptr, voidptr, voidptr) int pub fn get_line() string { max := 256 diff --git a/os/os_mac.v b/os/os_mac.v index fbcfa3bd11..7a67d4b22e 100644 --- a/os/os_mac.v +++ b/os/os_mac.v @@ -28,13 +28,13 @@ fn chdir(path string) { C.chdir(path.cstr()) } -fn getwd() string { +pub fn getwd() string { cwd := malloc(1024) # if (getcwd(cwd, 1024)) return tos2(cwd); return '' } -fn ls(path string) []string { +pub fn ls(path string) []string { mut res := []string # DIR *dir; # struct dirent *ent; diff --git a/rand/rand.v b/rand/rand.v index 1ff158c66f..0a9bf90271 100644 --- a/rand/rand.v +++ b/rand/rand.v @@ -4,19 +4,29 @@ module rand -#include +//#include -struct C.time_t{} -fn C.rand() int -fn seed() { +struct LOLLO { +} + +fn kek() { +} + +pub fn seed() { # time_t t; # srand((unsigned) time(&t)); } -fn next(max int) int { +fn ffkek() { +} + +pub fn next(max int) int { r := 0 # r = rand(); // TODO parser bug `rand` module name conflict return r % max } +struct C.time_t{} +fn C.rand() int +