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

checker: split fn_decl into functions to cleanup (#9226)

This commit is contained in:
zakuro 2021-03-11 03:18:48 +09:00 committed by GitHub
parent 791dec7b01
commit ef73e07e62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -285,57 +285,63 @@ fn (mut c Checker) check_valid_pascal_case(name string, identifier string, pos t
pub fn (mut c Checker) type_decl(node ast.TypeDecl) { pub fn (mut c Checker) type_decl(node ast.TypeDecl) {
match node { match node {
ast.AliasTypeDecl { ast.AliasTypeDecl { c.alias_type_decl(node) }
// TODO Replace `c.file.mod.name != 'time'` by `it.language != .v` once available ast.FnTypeDecl { c.fn_type_decl(node) }
if c.file.mod.name != 'time' && c.file.mod.name != 'builtin' { ast.SumTypeDecl { c.sum_type_decl(node) }
c.check_valid_pascal_case(node.name, 'type alias', node.pos) }
} }
typ_sym := c.table.get_type_symbol(node.parent_type)
if typ_sym.kind in [.placeholder, .int_literal, .float_literal] { pub fn (mut c Checker) alias_type_decl(node ast.AliasTypeDecl) {
c.error("type `$typ_sym.name` doesn't exist", node.pos) // TODO Replace `c.file.mod.name != 'time'` by `it.language != .v` once available
} else if typ_sym.kind == .alias { if c.file.mod.name != 'time' && c.file.mod.name != 'builtin' {
orig_sym := c.table.get_type_symbol((typ_sym.info as table.Alias).parent_type) c.check_valid_pascal_case(node.name, 'type alias', node.pos)
c.error('type `$typ_sym.str()` is an alias, use the original alias type `$orig_sym.name` instead', }
node.pos) typ_sym := c.table.get_type_symbol(node.parent_type)
} else if typ_sym.kind == .chan { if typ_sym.kind in [.placeholder, .int_literal, .float_literal] {
c.error('aliases of `chan` types are not allowed.', node.pos) c.error("type `$typ_sym.name` doesn't exist", node.pos)
} } else if typ_sym.kind == .alias {
orig_sym := c.table.get_type_symbol((typ_sym.info as table.Alias).parent_type)
c.error('type `$typ_sym.str()` is an alias, use the original alias type `$orig_sym.name` instead',
node.pos)
} else if typ_sym.kind == .chan {
c.error('aliases of `chan` types are not allowed.', node.pos)
}
}
pub fn (mut c Checker) fn_type_decl(node ast.FnTypeDecl) {
c.check_valid_pascal_case(node.name, 'fn type', node.pos)
typ_sym := c.table.get_type_symbol(node.typ)
fn_typ_info := typ_sym.info as table.FnType
fn_info := fn_typ_info.func
ret_sym := c.table.get_type_symbol(fn_info.return_type)
if ret_sym.kind == .placeholder {
c.error("type `$ret_sym.name` doesn't exist", node.pos)
}
for arg in fn_info.params {
arg_sym := c.table.get_type_symbol(arg.typ)
if arg_sym.kind == .placeholder {
c.error("type `$arg_sym.name` doesn't exist", node.pos)
} }
ast.FnTypeDecl { }
c.check_valid_pascal_case(node.name, 'fn type', node.pos) }
typ_sym := c.table.get_type_symbol(node.typ)
fn_typ_info := typ_sym.info as table.FnType pub fn (mut c Checker) sum_type_decl(node ast.SumTypeDecl) {
fn_info := fn_typ_info.func c.check_valid_pascal_case(node.name, 'sum type', node.pos)
ret_sym := c.table.get_type_symbol(fn_info.return_type) mut names_used := []string{}
if ret_sym.kind == .placeholder { for variant in node.variants {
c.error("type `$ret_sym.name` doesn't exist", node.pos) if variant.typ.is_ptr() {
} c.error('sum type cannot hold a reference type', variant.pos)
for arg in fn_info.params {
arg_sym := c.table.get_type_symbol(arg.typ)
if arg_sym.kind == .placeholder {
c.error("type `$arg_sym.name` doesn't exist", node.pos)
}
}
} }
ast.SumTypeDecl { mut sym := c.table.get_type_symbol(variant.typ)
c.check_valid_pascal_case(node.name, 'sum type', node.pos) if sym.name in names_used {
mut names_used := []string{} c.error('sum type $node.name cannot hold the type `$sym.name` more than once',
for variant in node.variants { variant.pos)
if variant.typ.is_ptr() { } else if sym.kind in [.placeholder, .int_literal, .float_literal] {
c.error('sum type cannot hold a reference type', variant.pos) c.error("type `$sym.name` doesn't exist", variant.pos)
} } else if sym.kind == .interface_ {
mut sym := c.table.get_type_symbol(variant.typ) c.error('sum type cannot hold an interface', variant.pos)
if sym.name in names_used {
c.error('sum type $node.name cannot hold the type `$sym.name` more than once',
variant.pos)
} else if sym.kind in [.placeholder, .int_literal, .float_literal] {
c.error("type `$sym.name` doesn't exist", variant.pos)
} else if sym.kind == .interface_ {
c.error('sum type cannot hold an interface', variant.pos)
}
names_used << sym.name
}
} }
names_used << sym.name
} }
} }