From 2c3f69546961016b7c61041bab4ec1fce5adfc74 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 3 Nov 2020 09:35:35 +0200 Subject: [PATCH] checker,parser: treat C.Struct fields as public by default --- vlib/v/checker/checker.v | 5 +++-- vlib/v/parser/parse_type.v | 10 +++++----- vlib/v/parser/parser.v | 2 +- vlib/v/parser/struct.v | 1 + vlib/v/table/atypes.v | 1 + vlib/v/table/table.v | 3 ++- 6 files changed, 13 insertions(+), 9 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 3087b82384..e22b40399c 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -448,7 +448,8 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) table.Type { return table.void_type } } - if !type_sym.is_public && type_sym.kind != .placeholder && type_sym.mod != c.mod { + if !type_sym.is_public && type_sym.kind != .placeholder && type_sym.mod != c.mod && + type_sym.language != .c { c.error('type `$type_sym.source_name` is private', struct_init.pos) } match type_sym.kind { @@ -1736,7 +1737,7 @@ pub fn (mut c Checker) selector_expr(mut selector_expr ast.SelectorExpr) table.T } mut unknown_field_msg := 'type `$sym.source_name` has no field or method `$field_name`' if field := c.table.struct_find_field(sym, field_name) { - if sym.mod != c.mod && !field.is_pub { + if sym.mod != c.mod && !field.is_pub && sym.language != .c { c.error('field `${sym.source_name}.$field_name` is not public', selector_expr.pos) } selector_expr.typ = field.typ diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index 502f9e4aa2..3e5a18c2fb 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -305,14 +305,14 @@ pub fn (mut p Parser) parse_any_type(language table.Language, is_ptr bool, check if p.peek_tok.kind == .lt { return p.parse_generic_struct_inst_type(name) } - return p.parse_enum_or_struct_type(name) + return p.parse_enum_or_struct_type(name, language) } } } } } -pub fn (mut p Parser) parse_enum_or_struct_type(name string) table.Type { +pub fn (mut p Parser) parse_enum_or_struct_type(name string, language table.Language) table.Type { // struct / enum / placeholder // struct / enum mut idx := p.table.find_type_idx(name) @@ -320,7 +320,7 @@ pub fn (mut p Parser) parse_enum_or_struct_type(name string) table.Type { return table.new_type(idx) } // not found - add placeholder - idx = p.table.add_placeholder_type(name) + idx = p.table.add_placeholder_type(name, language) // println('NOT FOUND: $name - adding placeholder - $idx') return table.new_type(idx) } @@ -367,7 +367,7 @@ pub fn (mut p Parser) parse_generic_struct_inst_type(name string) table.Type { if gt_idx > 0 { return table.new_type(gt_idx) } - gt_idx = p.table.add_placeholder_type(bs_name) + gt_idx = p.table.add_placeholder_type(bs_name, .v) idx := p.table.register_type_symbol(table.TypeSymbol{ kind: .generic_struct_inst name: bs_name @@ -380,5 +380,5 @@ pub fn (mut p Parser) parse_generic_struct_inst_type(name string) table.Type { }) return table.new_type(idx) } - return p.parse_enum_or_struct_type(name) + return p.parse_enum_or_struct_type(name, .v) } diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index c24b569a6a..acee12ac0b 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -1559,7 +1559,7 @@ fn (mut p Parser) import_syms(mut parent ast.Import) { alias := p.check_name() name := '$parent.mod\.$alias' if alias[0].is_capital() { - idx := p.table.add_placeholder_type(name) + idx := p.table.add_placeholder_type(name, .v) typ := table.new_type(idx) prepend_mod_name := p.prepend_mod(alias) p.table.register_type_symbol(table.TypeSymbol{ diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 8d650c76f3..b71acaf934 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -249,6 +249,7 @@ fn (mut p Parser) struct_decl() ast.StructDecl { t := table.TypeSymbol{ kind: .struct_ name: name + language: language source_name: name mod: p.mod info: table.Struct{ diff --git a/vlib/v/table/atypes.v b/vlib/v/table/atypes.v index 4dcaf3974d..b401c7cb3a 100644 --- a/vlib/v/table/atypes.v +++ b/vlib/v/table/atypes.v @@ -41,6 +41,7 @@ pub mut: mod string is_public bool is_written bool // set to true, when the backend definition for a symbol had been written, to avoid duplicates + language Language } // max of 8 diff --git a/vlib/v/table/table.v b/vlib/v/table/table.v index 4c235423b2..c94bf0e878 100644 --- a/vlib/v/table/table.v +++ b/vlib/v/table/table.v @@ -621,7 +621,7 @@ pub fn (mut t Table) find_or_register_fn_type(mod string, f Fn, is_anon bool, ha }) } -pub fn (mut t Table) add_placeholder_type(name string) int { +pub fn (mut t Table) add_placeholder_type(name string, language Language) int { mut modname := '' if name.contains('.') { modname = name.all_before_last('.') @@ -629,6 +629,7 @@ pub fn (mut t Table) add_placeholder_type(name string) int { ph_type := TypeSymbol{ kind: .placeholder name: name + language: language source_name: name mod: modname }