From c64f8b0d1f06d876adce2269c60631a7b48acfae Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Sat, 9 May 2020 15:16:48 +0200 Subject: [PATCH] parser/checker: pub: struct fields --- vlib/os/os.v | 1 - vlib/strings/builder.c.v | 4 +-- vlib/v/ast/ast.v | 68 ++++++++++++++++++++------------------ vlib/v/ast/scope.v | 5 +-- vlib/v/ast/str.v | 2 +- vlib/v/builder/builder.v | 23 +++++++++++-- vlib/v/cflag/cflags.v | 1 + vlib/v/checker/checker.v | 11 +++--- vlib/v/depgraph/depgraph.v | 2 +- vlib/v/errors/errors.v | 4 ++- vlib/v/fmt/fmt.v | 2 +- vlib/v/gen/cgen.v | 4 +-- vlib/v/gen/js/js.v | 2 +- vlib/v/parser/parser.v | 2 +- vlib/v/parser/struct.v | 1 + vlib/v/scanner/scanner.v | 2 +- vlib/v/table/atypes.v | 12 +++---- 17 files changed, 87 insertions(+), 59 deletions(-) diff --git a/vlib/os/os.v b/vlib/os/os.v index 40433669fe..37275990b2 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -22,7 +22,6 @@ struct C.dirent { fn C.readdir(voidptr) C.dirent - pub const ( args = []string{} MAX_PATH = 4096 diff --git a/vlib/strings/builder.c.v b/vlib/strings/builder.c.v index 9fe483a12a..fbb2211321 100644 --- a/vlib/strings/builder.c.v +++ b/vlib/strings/builder.c.v @@ -4,10 +4,10 @@ module strings pub struct Builder { -mut: + // TODO +pub mut: buf []byte str_calls int -pub mut: len int initial_size int = 1 } diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 5094c8512d..814c046210 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -67,7 +67,7 @@ pub: exprs []Expr expr_fmts []string pos token.Position -mut: +pub mut: expr_types []table.Type } @@ -88,8 +88,8 @@ pub struct SelectorExpr { pub: pos token.Position expr Expr - field string -mut: + field_name string +pub mut: expr_type table.Type } @@ -109,8 +109,9 @@ pub: comment Comment default_expr Expr has_default_expr bool - attrs []string -mut: + attrs []string + is_public bool +pub mut: typ table.Type } @@ -118,7 +119,7 @@ pub struct Field { pub: name string pos token.Position -mut: +pub mut: typ table.Type } @@ -128,7 +129,7 @@ pub: expr Expr is_pub bool pos token.Position -mut: +pub mut: typ table.Type } @@ -166,7 +167,7 @@ pub struct StructInitField { pub: expr Expr pos token.Position -mut: +pub mut: name string typ table.Type expected_type table.Type @@ -176,7 +177,7 @@ pub struct StructInit { pub: pos token.Position is_short bool -mut: +pub mut: typ table.Type fields []StructInitField } @@ -192,7 +193,7 @@ pub: pub struct AnonFn { pub: decl FnDecl -mut: +pub mut: typ table.Type } @@ -231,7 +232,7 @@ pub: pos token.Position left Expr // `user` in `user.register()` mod string -mut: +pub mut: name string is_method bool args []CallArg @@ -249,7 +250,7 @@ pub struct CallArg { pub: is_mut bool expr Expr -mut: +pub mut: typ table.Type } @@ -257,7 +258,7 @@ pub struct Return { pub: pos token.Position exprs []Expr -mut: +pub mut: types []table.Type } @@ -280,7 +281,7 @@ pub: expr Expr is_mut bool is_arg bool // fn args should not be autofreed -mut: +pub mut: typ table.Type pos token.Position is_used bool @@ -291,7 +292,7 @@ pub: name string expr Expr has_expr bool -mut: +pub mut: typ table.Type } @@ -302,7 +303,7 @@ pub: stmts []Stmt scope &Scope global_scope &Scope -mut: +pub mut: imports []Import } @@ -339,7 +340,7 @@ pub: tok_kind token.Kind mod string pos token.Position -mut: +pub mut: name string kind IdentKind info IdentInfo @@ -364,7 +365,7 @@ pub: pos token.Position left Expr right Expr -mut: +pub mut: left_type table.Type right_type table.Type } @@ -388,7 +389,7 @@ pub: pos token.Position left Expr index Expr // [0], [start..end] etc -mut: +pub mut: left_type table.Type // array, map, fixed array is_setter bool } @@ -399,7 +400,7 @@ pub: branches []IfBranch left Expr // `a` in `a := if ...` pos token.Position -mut: +pub mut: is_expr bool typ table.Type has_else bool @@ -420,7 +421,7 @@ pub: branches []MatchBranch pos token.Position is_mut bool // `match mut ast_node {` -mut: +pub mut: is_expr bool // returns a value return_type table.Type cond_type table.Type // type of `x` in `match x {` @@ -452,7 +453,7 @@ pub: stmts []Stmt is_not bool pos token.Position -mut: +pub mut: is_opt bool has_else bool else_stmts []Stmt @@ -475,7 +476,7 @@ pub: high Expr // `10` in `for i in 0..10 {` stmts []Stmt pos token.Position -mut: +pub mut: key_type table.Type val_type table.Type cond_type table.Type @@ -531,7 +532,7 @@ pub: expr Expr typ table.Type pos token.Position -mut: +pub mut: expr_type table.Type } @@ -547,11 +548,12 @@ pub: val string mod string // for full path `mod_Enum_val` pos token.Position -mut: +pub mut: typ table.Type } pub struct EnumField { + pub: name string pos token.Position expr Expr @@ -596,7 +598,7 @@ pub: pub struct DeferStmt { pub: stmts []Stmt -mut: +pub mut: ifdef string } @@ -617,7 +619,7 @@ pub: pos token.Position left Expr val Expr -mut: +pub mut: left_type table.Type right_type table.Type } @@ -648,7 +650,7 @@ pub: has_len bool has_cap bool cap_expr Expr -mut: +pub mut: is_interface bool // array of interfaces e.g. `[]Animal` `[Dog{}, Cat{}]` interface_types []table.Type // [Dog, Cat] interface_type table.Type // Animal @@ -661,7 +663,7 @@ pub: pos token.Position keys []Expr vals []Expr -mut: +pub mut: typ table.Type key_type table.Type value_type table.Type @@ -682,7 +684,7 @@ pub: arg Expr // `n` in `string(buf, n)` typ table.Type // `string` pos token.Position -mut: +pub mut: typname string expr_type table.Type // `byteptr` has_arg bool @@ -699,7 +701,7 @@ pub struct IfGuardExpr { pub: var_name string expr Expr -mut: +pub mut: expr_type table.Type } @@ -716,7 +718,7 @@ pub: fields []string exprs []Expr pos token.Position -mut: +pub mut: typ table.Type } @@ -729,7 +731,7 @@ pub: pub struct TypeOf { pub: expr Expr -mut: +pub mut: expr_type table.Type } diff --git a/vlib/v/ast/scope.v b/vlib/v/ast/scope.v index 40c085ee17..9e1896dce2 100644 --- a/vlib/v/ast/scope.v +++ b/vlib/v/ast/scope.v @@ -7,12 +7,13 @@ import v.table import v.token pub struct Scope { -mut: +//mut: +pub mut: + objects map[string]ScopeObject parent &Scope children []&Scope start_pos int end_pos int - objects map[string]ScopeObject } pub fn new_scope(parent &Scope, start_pos int) &Scope { diff --git a/vlib/v/ast/str.v b/vlib/v/ast/str.v index 92915f44cf..c82a75668c 100644 --- a/vlib/v/ast/str.v +++ b/vlib/v/ast/str.v @@ -125,7 +125,7 @@ pub fn (x Expr) str() string { return it.op.str() + it.right.str() } SelectorExpr { - return '${it.expr.str()}.${it.field}' + return '${it.expr.str()}.${it.field_name}' } StringInterLiteral { mut res := []string{} diff --git a/vlib/v/builder/builder.v b/vlib/v/builder/builder.v index f7c1d3645c..ffd5dbb8e1 100644 --- a/vlib/v/builder/builder.v +++ b/vlib/v/builder/builder.v @@ -15,6 +15,10 @@ import v.gen.js import v.gen.x64 import v.depgraph +const ( + max_nr_errors = 100 +) + pub struct Builder { pub: table &table.Table @@ -206,18 +210,33 @@ pub fn (b Builder) find_module_path(mod, fpath string) ?string { } fn (b &Builder) print_warnings_and_errors() { + if b.pref.is_verbose && b.checker.nr_warnings > 1 { + println('$b.checker.nr_warnings warnings') + } if b.checker.nr_warnings > 0 { - for err in b.checker.warnings { + for i, err in b.checker.warnings { kind := if b.pref.is_verbose { '$err.reporter warning #$b.checker.nr_warnings:' } else { 'warning:' } ferror := util.formatted_error(kind, err.message, err.file_path, err.pos) eprintln(ferror) + // eprintln('') + if i > max_nr_errors { + return + } } } + // + if b.pref.is_verbose && b.checker.nr_errors > 1 { + println('$b.checker.nr_errors errors') + } if b.checker.nr_errors > 0 { - for err in b.checker.errors { + for i, err in b.checker.errors { kind := if b.pref.is_verbose { '$err.reporter error #$b.checker.nr_errors:' } else { 'error:' } ferror := util.formatted_error(kind, err.message, err.file_path, err.pos) eprintln(ferror) + // eprintln('') + if i > max_nr_errors { + return + } } exit(1) } diff --git a/vlib/v/cflag/cflags.v b/vlib/v/cflag/cflags.v index e51a8e87e9..ba53c8422b 100644 --- a/vlib/v/cflag/cflags.v +++ b/vlib/v/cflag/cflags.v @@ -7,6 +7,7 @@ import os // parsed cflag pub struct CFlag { +pub: mod string // the module in which the flag was given os string // eg. windows | darwin | linux name string // eg. -I diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 6f3a2f4e8f..8926193377 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -18,7 +18,7 @@ const ( pub struct Checker { table &table.Table -mut: +pub mut: file ast.File nr_errors int nr_warnings int @@ -517,10 +517,10 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) { match typ_sym.kind { .struct_ { struct_info := typ_sym.info as table.Struct - field_info := struct_info.get_field(it.field) + field_info := struct_info.get_field(it.field_name) if !field_info.is_mut { type_str := c.table.type_to_str(it.expr_type) - c.error('field `$it.field` of struct `${type_str}` is immutable', it.pos) + c.error('field `$it.field_name` of struct `${type_str}` is immutable', it.pos) } c.fail_if_immutable(it.expr) } @@ -1002,7 +1002,7 @@ pub fn (mut c Checker) selector_expr(selector_expr mut ast.SelectorExpr) table.T selector_expr.expr_type = typ // println('sel expr line_nr=$selector_expr.pos.line_nr typ=$selector_expr.expr_type') typ_sym := c.table.get_type_symbol(typ) - field_name := selector_expr.field + field_name := selector_expr.field_name // variadic if typ.flag_is(.variadic) { if field_name == 'len' { @@ -1010,6 +1010,9 @@ pub fn (mut c Checker) selector_expr(selector_expr mut ast.SelectorExpr) table.T } } if field := c.table.struct_find_field(typ_sym, field_name) { + if typ_sym.mod != c.mod && !field.is_pub{ + c.warn('field `${typ_sym.name}.$field_name` is not public', selector_expr.pos) + } return field.typ } if typ_sym.kind != .struct_ { diff --git a/vlib/v/depgraph/depgraph.v b/vlib/v/depgraph/depgraph.v index 5183e13d4f..5a8165cbfe 100644 --- a/vlib/v/depgraph/depgraph.v +++ b/vlib/v/depgraph/depgraph.v @@ -6,7 +6,7 @@ module depgraph struct DepGraphNode { -mut: +pub mut: name string deps []string } diff --git a/vlib/v/errors/errors.v b/vlib/v/errors/errors.v index 26d56cf287..5aadd9229e 100644 --- a/vlib/v/errors/errors.v +++ b/vlib/v/errors/errors.v @@ -10,14 +10,16 @@ pub enum Reporter { } pub struct Error { +pub: message string file_path string pos token.Position - reporter Reporter backtrace string + reporter Reporter } pub struct Warning { +pub: message string file_path string pos token.Position diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 5fde47c23a..2eabe52d65 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -653,7 +653,7 @@ fn (mut f Fmt) expr(node ast.Expr) { ast.SelectorExpr { f.expr(it.expr) f.write('.') - f.write(it.field) + f.write(it.field_name) } ast.SizeOf { f.write('sizeof(') diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index cc73b79c93..e19307f702 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -1293,9 +1293,9 @@ fn (mut g Gen) expr(node ast.Expr) { g.write('.') } if it.expr_type == 0 { - verror('cgen: SelectorExpr | expr_type: 0 | it.expr: `${it.expr}` | field: `$it.field` | file: $g.file.path | line: $it.pos.line_nr') + verror('cgen: SelectorExpr | expr_type: 0 | it.expr: `${it.expr}` | field: `$it.field_name` | file: $g.file.path | line: $it.pos.line_nr') } - g.write(c_name(it.field)) + g.write(c_name(it.field_name)) } ast.Type { // match sum Type diff --git a/vlib/v/gen/js/js.v b/vlib/v/gen/js/js.v index dcc52bdf10..6d5d45a157 100644 --- a/vlib/v/gen/js/js.v +++ b/vlib/v/gen/js/js.v @@ -990,7 +990,7 @@ fn (g mut JsGen) gen_ident(node ast.Ident) { fn (g mut JsGen) gen_selector_expr(it ast.SelectorExpr) { g.expr(it.expr) - g.write('.$it.field') + g.write('.$it.field_name') } fn (g mut JsGen) gen_if_expr(node ast.IfExpr) { diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index d43d5ceb0f..436e7f677b 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -864,7 +864,7 @@ fn (mut p Parser) dot_expr(left ast.Expr) ast.Expr { } sel_expr := ast.SelectorExpr{ expr: left - field: field_name + field_name: field_name pos: name_pos } mut node := ast.Expr{} diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 405d8b720a..218ab42a0e 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -136,6 +136,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl { default_expr: default_expr has_default_expr: has_default_expr attrs: attrs + is_public: is_field_pub } fields << table.Field{ name: field_name diff --git a/vlib/v/scanner/scanner.v b/vlib/v/scanner/scanner.v index 396297a047..aca33b8847 100644 --- a/vlib/v/scanner/scanner.v +++ b/vlib/v/scanner/scanner.v @@ -16,7 +16,7 @@ const ( ) pub struct Scanner { -mut: +pub mut: file_path string text string pos int diff --git a/vlib/v/table/atypes.v b/vlib/v/table/atypes.v index e54cbdd137..83183d3177 100644 --- a/vlib/v/table/atypes.v +++ b/vlib/v/table/atypes.v @@ -20,7 +20,7 @@ pub type TypeInfo = Alias | Array | ArrayFixed | Enum | FnType | Interface | Map pub struct TypeSymbol { pub: parent_idx int -mut: +pub mut: info TypeInfo kind Kind name string @@ -244,7 +244,7 @@ pub const ( pub struct MultiReturn { pub: name string -mut: +pub mut: types []Type } @@ -537,7 +537,7 @@ pub mut: } pub struct Interface { -mut: +pub mut: types []Type } @@ -559,7 +559,7 @@ pub type FExpr = byteptr | voidptr pub struct Field { pub: name string -mut: +pub mut: typ Type default_expr FExpr has_default_expr bool @@ -573,7 +573,7 @@ mut: pub struct Array { pub: nr_dims int -mut: +pub mut: elem_type Type } @@ -581,7 +581,7 @@ pub struct ArrayFixed { pub: nr_dims int size int -mut: +pub mut: elem_type Type }