parser/checker: pub: struct fields

pull/4801/head
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 fn C.readdir(voidptr) C.dirent
pub const ( pub const (
args = []string{} args = []string{}
MAX_PATH = 4096 MAX_PATH = 4096

View File

@ -4,10 +4,10 @@
module strings module strings
pub struct Builder { pub struct Builder {
mut: // TODO
pub mut:
buf []byte buf []byte
str_calls int str_calls int
pub mut:
len int len int
initial_size int = 1 initial_size int = 1
} }

View File

@ -67,7 +67,7 @@ pub:
exprs []Expr exprs []Expr
expr_fmts []string expr_fmts []string
pos token.Position pos token.Position
mut: pub mut:
expr_types []table.Type expr_types []table.Type
} }
@ -88,8 +88,8 @@ pub struct SelectorExpr {
pub: pub:
pos token.Position pos token.Position
expr Expr expr Expr
field string field_name string
mut: pub mut:
expr_type table.Type expr_type table.Type
} }
@ -109,8 +109,9 @@ pub:
comment Comment comment Comment
default_expr Expr default_expr Expr
has_default_expr bool has_default_expr bool
attrs []string attrs []string
mut: is_public bool
pub mut:
typ table.Type typ table.Type
} }
@ -118,7 +119,7 @@ pub struct Field {
pub: pub:
name string name string
pos token.Position pos token.Position
mut: pub mut:
typ table.Type typ table.Type
} }
@ -128,7 +129,7 @@ pub:
expr Expr expr Expr
is_pub bool is_pub bool
pos token.Position pos token.Position
mut: pub mut:
typ table.Type typ table.Type
} }
@ -166,7 +167,7 @@ pub struct StructInitField {
pub: pub:
expr Expr expr Expr
pos token.Position pos token.Position
mut: pub mut:
name string name string
typ table.Type typ table.Type
expected_type table.Type expected_type table.Type
@ -176,7 +177,7 @@ pub struct StructInit {
pub: pub:
pos token.Position pos token.Position
is_short bool is_short bool
mut: pub mut:
typ table.Type typ table.Type
fields []StructInitField fields []StructInitField
} }
@ -192,7 +193,7 @@ pub:
pub struct AnonFn { pub struct AnonFn {
pub: pub:
decl FnDecl decl FnDecl
mut: pub mut:
typ table.Type typ table.Type
} }
@ -231,7 +232,7 @@ pub:
pos token.Position pos token.Position
left Expr // `user` in `user.register()` left Expr // `user` in `user.register()`
mod string mod string
mut: pub mut:
name string name string
is_method bool is_method bool
args []CallArg args []CallArg
@ -249,7 +250,7 @@ pub struct CallArg {
pub: pub:
is_mut bool is_mut bool
expr Expr expr Expr
mut: pub mut:
typ table.Type typ table.Type
} }
@ -257,7 +258,7 @@ pub struct Return {
pub: pub:
pos token.Position pos token.Position
exprs []Expr exprs []Expr
mut: pub mut:
types []table.Type types []table.Type
} }
@ -280,7 +281,7 @@ pub:
expr Expr expr Expr
is_mut bool is_mut bool
is_arg bool // fn args should not be autofreed is_arg bool // fn args should not be autofreed
mut: pub mut:
typ table.Type typ table.Type
pos token.Position pos token.Position
is_used bool is_used bool
@ -291,7 +292,7 @@ pub:
name string name string
expr Expr expr Expr
has_expr bool has_expr bool
mut: pub mut:
typ table.Type typ table.Type
} }
@ -302,7 +303,7 @@ pub:
stmts []Stmt stmts []Stmt
scope &Scope scope &Scope
global_scope &Scope global_scope &Scope
mut: pub mut:
imports []Import imports []Import
} }
@ -339,7 +340,7 @@ pub:
tok_kind token.Kind tok_kind token.Kind
mod string mod string
pos token.Position pos token.Position
mut: pub mut:
name string name string
kind IdentKind kind IdentKind
info IdentInfo info IdentInfo
@ -364,7 +365,7 @@ pub:
pos token.Position pos token.Position
left Expr left Expr
right Expr right Expr
mut: pub mut:
left_type table.Type left_type table.Type
right_type table.Type right_type table.Type
} }
@ -388,7 +389,7 @@ pub:
pos token.Position pos token.Position
left Expr left Expr
index Expr // [0], [start..end] etc index Expr // [0], [start..end] etc
mut: pub mut:
left_type table.Type // array, map, fixed array left_type table.Type // array, map, fixed array
is_setter bool is_setter bool
} }
@ -399,7 +400,7 @@ pub:
branches []IfBranch branches []IfBranch
left Expr // `a` in `a := if ...` left Expr // `a` in `a := if ...`
pos token.Position pos token.Position
mut: pub mut:
is_expr bool is_expr bool
typ table.Type typ table.Type
has_else bool has_else bool
@ -420,7 +421,7 @@ pub:
branches []MatchBranch branches []MatchBranch
pos token.Position pos token.Position
is_mut bool // `match mut ast_node {` is_mut bool // `match mut ast_node {`
mut: pub mut:
is_expr bool // returns a value is_expr bool // returns a value
return_type table.Type return_type table.Type
cond_type table.Type // type of `x` in `match x {` cond_type table.Type // type of `x` in `match x {`
@ -452,7 +453,7 @@ pub:
stmts []Stmt stmts []Stmt
is_not bool is_not bool
pos token.Position pos token.Position
mut: pub mut:
is_opt bool is_opt bool
has_else bool has_else bool
else_stmts []Stmt else_stmts []Stmt
@ -475,7 +476,7 @@ pub:
high Expr // `10` in `for i in 0..10 {` high Expr // `10` in `for i in 0..10 {`
stmts []Stmt stmts []Stmt
pos token.Position pos token.Position
mut: pub mut:
key_type table.Type key_type table.Type
val_type table.Type val_type table.Type
cond_type table.Type cond_type table.Type
@ -531,7 +532,7 @@ pub:
expr Expr expr Expr
typ table.Type typ table.Type
pos token.Position pos token.Position
mut: pub mut:
expr_type table.Type expr_type table.Type
} }
@ -547,11 +548,12 @@ pub:
val string val string
mod string // for full path `mod_Enum_val` mod string // for full path `mod_Enum_val`
pos token.Position pos token.Position
mut: pub mut:
typ table.Type typ table.Type
} }
pub struct EnumField { pub struct EnumField {
pub:
name string name string
pos token.Position pos token.Position
expr Expr expr Expr
@ -596,7 +598,7 @@ pub:
pub struct DeferStmt { pub struct DeferStmt {
pub: pub:
stmts []Stmt stmts []Stmt
mut: pub mut:
ifdef string ifdef string
} }
@ -617,7 +619,7 @@ pub:
pos token.Position pos token.Position
left Expr left Expr
val Expr val Expr
mut: pub mut:
left_type table.Type left_type table.Type
right_type table.Type right_type table.Type
} }
@ -648,7 +650,7 @@ pub:
has_len bool has_len bool
has_cap bool has_cap bool
cap_expr Expr cap_expr Expr
mut: pub mut:
is_interface bool // array of interfaces e.g. `[]Animal` `[Dog{}, Cat{}]` is_interface bool // array of interfaces e.g. `[]Animal` `[Dog{}, Cat{}]`
interface_types []table.Type // [Dog, Cat] interface_types []table.Type // [Dog, Cat]
interface_type table.Type // Animal interface_type table.Type // Animal
@ -661,7 +663,7 @@ pub:
pos token.Position pos token.Position
keys []Expr keys []Expr
vals []Expr vals []Expr
mut: pub mut:
typ table.Type typ table.Type
key_type table.Type key_type table.Type
value_type table.Type value_type table.Type
@ -682,7 +684,7 @@ pub:
arg Expr // `n` in `string(buf, n)` arg Expr // `n` in `string(buf, n)`
typ table.Type // `string` typ table.Type // `string`
pos token.Position pos token.Position
mut: pub mut:
typname string typname string
expr_type table.Type // `byteptr` expr_type table.Type // `byteptr`
has_arg bool has_arg bool
@ -699,7 +701,7 @@ pub struct IfGuardExpr {
pub: pub:
var_name string var_name string
expr Expr expr Expr
mut: pub mut:
expr_type table.Type expr_type table.Type
} }
@ -716,7 +718,7 @@ pub:
fields []string fields []string
exprs []Expr exprs []Expr
pos token.Position pos token.Position
mut: pub mut:
typ table.Type typ table.Type
} }
@ -729,7 +731,7 @@ pub:
pub struct TypeOf { pub struct TypeOf {
pub: pub:
expr Expr expr Expr
mut: pub mut:
expr_type table.Type expr_type table.Type
} }

View File

@ -7,12 +7,13 @@ import v.table
import v.token import v.token
pub struct Scope { pub struct Scope {
mut: //mut:
pub mut:
objects map[string]ScopeObject
parent &Scope parent &Scope
children []&Scope children []&Scope
start_pos int start_pos int
end_pos int end_pos int
objects map[string]ScopeObject
} }
pub fn new_scope(parent &Scope, start_pos int) &Scope { 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() return it.op.str() + it.right.str()
} }
SelectorExpr { SelectorExpr {
return '${it.expr.str()}.${it.field}' return '${it.expr.str()}.${it.field_name}'
} }
StringInterLiteral { StringInterLiteral {
mut res := []string{} mut res := []string{}

View File

@ -15,6 +15,10 @@ import v.gen.js
import v.gen.x64 import v.gen.x64
import v.depgraph import v.depgraph
const (
max_nr_errors = 100
)
pub struct Builder { pub struct Builder {
pub: pub:
table &table.Table 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() { 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 { 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:' } 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) ferror := util.formatted_error(kind, err.message, err.file_path, err.pos)
eprintln(ferror) 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 { 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:' } 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) ferror := util.formatted_error(kind, err.message, err.file_path, err.pos)
eprintln(ferror) eprintln(ferror)
// eprintln('')
if i > max_nr_errors {
return
}
} }
exit(1) exit(1)
} }

View File

@ -7,6 +7,7 @@ import os
// parsed cflag // parsed cflag
pub struct CFlag { pub struct CFlag {
pub:
mod string // the module in which the flag was given mod string // the module in which the flag was given
os string // eg. windows | darwin | linux os string // eg. windows | darwin | linux
name string // eg. -I name string // eg. -I

View File

@ -18,7 +18,7 @@ const (
pub struct Checker { pub struct Checker {
table &table.Table table &table.Table
mut: pub mut:
file ast.File file ast.File
nr_errors int nr_errors int
nr_warnings int nr_warnings int
@ -517,10 +517,10 @@ fn (mut c Checker) fail_if_immutable(expr ast.Expr) {
match typ_sym.kind { match typ_sym.kind {
.struct_ { .struct_ {
struct_info := typ_sym.info as table.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 { if !field_info.is_mut {
type_str := c.table.type_to_str(it.expr_type) 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) 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 selector_expr.expr_type = typ
// println('sel expr line_nr=$selector_expr.pos.line_nr typ=$selector_expr.expr_type') // println('sel expr line_nr=$selector_expr.pos.line_nr typ=$selector_expr.expr_type')
typ_sym := c.table.get_type_symbol(typ) typ_sym := c.table.get_type_symbol(typ)
field_name := selector_expr.field field_name := selector_expr.field_name
// variadic // variadic
if typ.flag_is(.variadic) { if typ.flag_is(.variadic) {
if field_name == 'len' { 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 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 return field.typ
} }
if typ_sym.kind != .struct_ { if typ_sym.kind != .struct_ {

View File

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

View File

@ -10,14 +10,16 @@ pub enum Reporter {
} }
pub struct Error { pub struct Error {
pub:
message string message string
file_path string file_path string
pos token.Position pos token.Position
reporter Reporter
backtrace string backtrace string
reporter Reporter
} }
pub struct Warning { pub struct Warning {
pub:
message string message string
file_path string file_path string
pos token.Position pos token.Position

View File

@ -653,7 +653,7 @@ fn (mut f Fmt) expr(node ast.Expr) {
ast.SelectorExpr { ast.SelectorExpr {
f.expr(it.expr) f.expr(it.expr)
f.write('.') f.write('.')
f.write(it.field) f.write(it.field_name)
} }
ast.SizeOf { ast.SizeOf {
f.write('sizeof(') f.write('sizeof(')

View File

@ -1293,9 +1293,9 @@ fn (mut g Gen) expr(node ast.Expr) {
g.write('.') g.write('.')
} }
if it.expr_type == 0 { 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 { ast.Type {
// match sum 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) { fn (g mut JsGen) gen_selector_expr(it ast.SelectorExpr) {
g.expr(it.expr) g.expr(it.expr)
g.write('.$it.field') g.write('.$it.field_name')
} }
fn (g mut JsGen) gen_if_expr(node ast.IfExpr) { 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{ sel_expr := ast.SelectorExpr{
expr: left expr: left
field: field_name field_name: field_name
pos: name_pos pos: name_pos
} }
mut node := ast.Expr{} mut node := ast.Expr{}

View File

@ -136,6 +136,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl {
default_expr: default_expr default_expr: default_expr
has_default_expr: has_default_expr has_default_expr: has_default_expr
attrs: attrs attrs: attrs
is_public: is_field_pub
} }
fields << table.Field{ fields << table.Field{
name: field_name name: field_name

View File

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

View File

@ -20,7 +20,7 @@ pub type TypeInfo = Alias | Array | ArrayFixed | Enum | FnType | Interface | Map
pub struct TypeSymbol { pub struct TypeSymbol {
pub: pub:
parent_idx int parent_idx int
mut: pub mut:
info TypeInfo info TypeInfo
kind Kind kind Kind
name string name string
@ -244,7 +244,7 @@ pub const (
pub struct MultiReturn { pub struct MultiReturn {
pub: pub:
name string name string
mut: pub mut:
types []Type types []Type
} }
@ -537,7 +537,7 @@ pub mut:
} }
pub struct Interface { pub struct Interface {
mut: pub mut:
types []Type types []Type
} }
@ -559,7 +559,7 @@ pub type FExpr = byteptr | voidptr
pub struct Field { pub struct Field {
pub: pub:
name string name string
mut: pub mut:
typ Type typ Type
default_expr FExpr default_expr FExpr
has_default_expr bool has_default_expr bool
@ -573,7 +573,7 @@ mut:
pub struct Array { pub struct Array {
pub: pub:
nr_dims int nr_dims int
mut: pub mut:
elem_type Type elem_type Type
} }
@ -581,7 +581,7 @@ pub struct ArrayFixed {
pub: pub:
nr_dims int nr_dims int
size int size int
mut: pub mut:
elem_type Type elem_type Type
} }