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

fix sruct access modifiers (pub, pub mut, mut)

This commit is contained in:
Alexander Medvednikov 2019-12-13 17:18:01 +03:00
parent fc64238a39
commit 78131e78bc
6 changed files with 44 additions and 31 deletions

View File

@ -14,7 +14,7 @@ mut:
} }
struct DepGraph { struct DepGraph {
pub: //pub:
mut: mut:
acyclic bool acyclic bool
nodes []DepGraphNode nodes []DepGraphNode

View File

@ -14,7 +14,7 @@ const (
pub struct Fn { pub struct Fn {
// addr int // addr int
pub: //pub:
mut: mut:
name string name string
mod string mod string

View File

@ -20,6 +20,7 @@ struct Parser {
file_pcguard string file_pcguard string
v &V v &V
pref &Preferences // Preferences shared from V struct pref &Preferences // Preferences shared from V struct
kek string
mut: mut:
scanner &Scanner scanner &Scanner
tokens []Token tokens []Token
@ -75,7 +76,7 @@ mut:
sql_types []string // int, string and so on; see sql_params sql_types []string // int, string and so on; see sql_params
is_vh bool // parsing .vh file (for example `const (a int)` is allowed) is_vh bool // parsing .vh file (for example `const (a int)` is allowed)
generic_dispatch TypeInst generic_dispatch TypeInst
pub: pub mut:
mod string mod string
} }

View File

@ -109,8 +109,9 @@ fn (p mut Parser) struct_decl() {
p.fspace() p.fspace()
p.check(.lcbr) p.check(.lcbr)
// Struct fields // Struct fields
mut is_pub_field := false mut access_mod := AccessMod.private
mut is_mut := false //mut is_pub_field := false
//mut is_mut := false
mut names := []string// to avoid dup names TODO alloc perf mut names := []string// to avoid dup names TODO alloc perf
mut fmt_max_len := 0 mut fmt_max_len := 0
// TODO why is typ.fields == 0? // TODO why is typ.fields == 0?
@ -123,45 +124,47 @@ fn (p mut Parser) struct_decl() {
} }
} }
//println('fmt max len = $max_len nrfields=$typ.fields.len pass=$p.pass') //println('fmt max len = $max_len nrfields=$typ.fields.len pass=$p.pass')
if !is_ph && p.first_pass() { if !is_ph && p.first_pass() {
p.table.register_type(typ) p.table.register_type(typ)
//println('registering 1 nrfields=$typ.fields.len') //println('registering 1 nrfields=$typ.fields.len')
} }
mut did_gen_something := false mut did_gen_something := false
mut used := []AccessMod
mut i := -1 mut i := -1
for p.tok != .rcbr { for p.tok != .rcbr {
i++ i++
mut new_access_mod := access_mod
if p.tok == .key_pub { if p.tok == .key_pub {
if is_pub_field {
p.error('structs can only have one `pub:`, all public fields have to be grouped')
}
is_pub_field = true
p.fmt_dec()
p.check(.key_pub) p.check(.key_pub)
if p.tok != .key_mut { if p.tok == .key_mut {
p.check(.colon) new_access_mod = .public_mut
p.next() // skip `mut`
} else {
new_access_mod = .public
} }
if new_access_mod in used {
p.error('structs can only have one `pub:`/`pub mut:`, all public fields have to be grouped')
}
p.fmt_dec()
p.check(.colon)
p.fmt_inc() p.fmt_inc()
p.fgen_nl() p.fgen_nl()
} }
if p.tok == .key_mut { else if p.tok == .key_mut {
if is_mut { new_access_mod = .private_mut
if new_access_mod in used {
p.error('structs can only have one `mut:`, all private mutable fields have to be grouped') p.error('structs can only have one `mut:`, all private mutable fields have to be grouped')
} }
is_mut = true
p.fmt_dec() p.fmt_dec()
p.check(.key_mut) p.check(.key_mut)
if p.tok != .key_mut { p.check(.colon)
p.check(.colon)
}
p.fmt_inc() p.fmt_inc()
p.fgen_nl() p.fgen_nl()
} }
// if is_pub { if new_access_mod != access_mod {
// } used << new_access_mod
}
access_mod = new_access_mod
// (mut) user *User // (mut) user *User
// if p.tok == .plus { // if p.tok == .plus {
// p.next() // p.next()
@ -192,7 +195,7 @@ fn (p mut Parser) struct_decl() {
continue continue
} }
// `pub` access mod // `pub` access mod
access_mod := if is_pub_field { AccessMod.public } else { AccessMod.private} //access_mod := if is_pub_field { AccessMod.public } else { AccessMod.private}
p.fspace() p.fspace()
tt := p.get_type2() tt := p.get_type2()
field_type := tt.name field_type := tt.name
@ -245,8 +248,10 @@ fn (p mut Parser) struct_decl() {
} }
did_gen_something = true did_gen_something = true
is_mut := access_mod in [.private_mut, .public_mut]
if p.first_pass() { if p.first_pass() {
p.table.add_field(typ.name, field_name, field_type, is_mut, attr, access_mod) p.table.add_field(typ.name, field_name, field_type, is_mut,
attr, access_mod)
} }
p.fgen_nl() // newline between struct fields p.fgen_nl() // newline between struct fields
} }

View File

@ -44,7 +44,16 @@ enum AccessMod {
private_mut // private mutable private_mut // private mutable
public // public immutable (readonly) public // public immutable (readonly)
public_mut // public, but mutable only in this module public_mut // public, but mutable only in this module
public_mut_mut // public and mutable both inside and outside (not recommended to use, that's why it's so verbose) global // public and mutable both inside and outside (not recommended to use, that's why it's so verbose)
}
fn (a []AccessMod) contains(b AccessMod) bool {
for elm in a {
if elm == b {
return true
}
}
return false
} }
enum TypeCategory { enum TypeCategory {
@ -62,8 +71,7 @@ enum TypeCategory {
} }
struct Var { struct Var {
pub: pub mut:
mut:
typ string typ string
name string name string
idx int // index in the local_vars array idx int // index in the local_vars array
@ -92,8 +100,7 @@ mut:
} }
struct Type { struct Type {
pub: pub mut:
mut:
mod string mod string
name string name string
cat TypeCategory cat TypeCategory

View File

@ -7,7 +7,7 @@ module strings
pub struct Builder { pub struct Builder {
mut: mut:
buf []byte buf []byte
pub: pub mut:
len int len int
initial_size int = 1 initial_size int = 1
} }