1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

parser/checker: pub: struct fields

This commit is contained in:
Alexander Medvednikov 2020-05-09 15:16:48 +02:00
parent 809676a856
commit c64f8b0d1f
17 changed files with 87 additions and 59 deletions

View File

@ -22,7 +22,6 @@ struct C.dirent {
fn C.readdir(voidptr) C.dirent
pub const (
args = []string{}
MAX_PATH = 4096

View File

@ -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
}

View File

@ -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
}

View File

@ -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 {

View File

@ -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{}

View File

@ -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)
}

View File

@ -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

View File

@ -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_ {

View File

@ -6,7 +6,7 @@
module depgraph
struct DepGraphNode {
mut:
pub mut:
name string
deps []string
}

View File

@ -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

View File

@ -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(')

View File

@ -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

View File

@ -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) {

View File

@ -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{}

View File

@ -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

View File

@ -16,7 +16,7 @@ const (
)
pub struct Scanner {
mut:
pub mut:
file_path string
text string
pos int

View File

@ -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
}