checker: minor cleanup of FnDecl (#10000)

pull/10067/head
yuyi 2021-05-09 20:27:45 +08:00 committed by GitHub
parent f85955ef67
commit 74defc4b82
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 20 deletions

View File

@ -385,19 +385,18 @@ pub:
attrs []Attr attrs []Attr
skip_gen bool // this function doesn't need to be generated (for example [if foo]) skip_gen bool // this function doesn't need to be generated (for example [if foo])
pub mut: pub mut:
params []Param params []Param
stmts []Stmt stmts []Stmt
defer_stmts []DeferStmt defer_stmts []DeferStmt
return_type Type return_type Type
return_type_pos token.Position // `string` in `fn (u User) name() string` position return_type_pos token.Position // `string` in `fn (u User) name() string` position
has_return bool has_return bool
comments []Comment // comments *after* the header, but *before* `{`; used for InterfaceDecl comments []Comment // comments *after* the header, but *before* `{`; used for InterfaceDecl
next_comments []Comment // coments that are one line after the decl; used for InterfaceDecl next_comments []Comment // coments that are one line after the decl; used for InterfaceDecl
source_file &File = 0 source_file &File = 0
scope &Scope scope &Scope
label_names []string label_names []string
pos token.Position // function declaration position pos token.Position // function declaration position
cur_concrete_types []Type // current concrete types, e.g. <int, string>
} }
// break, continue // break, continue

View File

@ -80,6 +80,7 @@ mut:
timers &util.Timers = util.new_timers(false) timers &util.Timers = util.new_timers(false)
comptime_fields_type map[string]ast.Type comptime_fields_type map[string]ast.Type
fn_scope &ast.Scope = voidptr(0) fn_scope &ast.Scope = voidptr(0)
cur_concrete_types []ast.Type // current concrete types, e.g. <int, string>
main_fn_decl_node ast.FnDecl main_fn_decl_node ast.FnDecl
match_exhaustive_cutoff_limit int = 10 match_exhaustive_cutoff_limit int = 10
// TODO: these are here temporarily and used for deprecations; remove soon // TODO: these are here temporarily and used for deprecations; remove soon
@ -634,7 +635,7 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) ast.Type {
// Make sure the first letter is capital, do not allow e.g. `x := string{}`, // Make sure the first letter is capital, do not allow e.g. `x := string{}`,
// but `x := T{}` is ok. // but `x := T{}` is ok.
if !c.is_builtin_mod && !c.inside_unsafe && type_sym.language == .v if !c.is_builtin_mod && !c.inside_unsafe && type_sym.language == .v
&& c.cur_fn.cur_concrete_types.len == 0 { && c.cur_concrete_types.len == 0 {
pos := type_sym.name.last_index('.') or { -1 } pos := type_sym.name.last_index('.') or { -1 }
first_letter := type_sym.name[pos + 1] first_letter := type_sym.name[pos + 1]
if !first_letter.is_capital() { if !first_letter.is_capital() {
@ -2162,7 +2163,7 @@ pub fn (mut c Checker) fn_call(mut call_expr ast.CallExpr) ast.Type {
concrete_types << concrete_type concrete_types << concrete_type
} }
} }
if !isnil(c.cur_fn) && c.cur_fn.cur_concrete_types.len == 0 && has_generic { if !isnil(c.cur_fn) && c.cur_concrete_types.len == 0 && has_generic {
c.error('generic fn using generic types cannot be called outside of generic fn', c.error('generic fn using generic types cannot be called outside of generic fn',
call_expr.pos) call_expr.pos)
} }
@ -2923,7 +2924,7 @@ pub fn (mut c Checker) return_stmt(mut return_stmt ast.Return) {
mut expected_types := [expected_type] mut expected_types := [expected_type]
if expected_type_sym.info is ast.MultiReturn { if expected_type_sym.info is ast.MultiReturn {
expected_types = expected_type_sym.info.types expected_types = expected_type_sym.info.types
if c.cur_fn.cur_concrete_types.len > 0 { if c.cur_concrete_types.len > 0 {
expected_types = expected_types.map(c.unwrap_generic(it)) expected_types = expected_types.map(c.unwrap_generic(it))
} }
} }
@ -4426,7 +4427,7 @@ fn (mut c Checker) stmts(stmts []ast.Stmt) {
pub fn (mut c Checker) unwrap_generic(typ ast.Type) ast.Type { pub fn (mut c Checker) unwrap_generic(typ ast.Type) ast.Type {
if typ.has_flag(.generic) { if typ.has_flag(.generic) {
if t_typ := c.table.resolve_generic_to_concrete(typ, c.cur_fn.generic_names, c.cur_fn.cur_concrete_types, if t_typ := c.table.resolve_generic_to_concrete(typ, c.cur_fn.generic_names, c.cur_concrete_types,
false) false)
{ {
return t_typ return t_typ
@ -6868,7 +6869,7 @@ fn (mut c Checker) post_process_generic_fns() {
mut node := c.file.generic_fns[i] mut node := c.file.generic_fns[i]
c.mod = node.mod c.mod = node.mod
for concrete_types in c.table.fn_generic_types[node.name] { for concrete_types in c.table.fn_generic_types[node.name] {
node.cur_concrete_types = concrete_types c.cur_concrete_types = concrete_types
c.fn_decl(mut node) c.fn_decl(mut node)
if node.name in ['vweb.run_app', 'vweb.run'] { if node.name in ['vweb.run_app', 'vweb.run'] {
for ct in concrete_types { for ct in concrete_types {
@ -6878,13 +6879,13 @@ fn (mut c Checker) post_process_generic_fns() {
} }
} }
} }
node.cur_concrete_types = [] c.cur_concrete_types = []
} }
} }
fn (mut c Checker) fn_decl(mut node ast.FnDecl) { fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
c.returns = false c.returns = false
if node.generic_names.len > 0 && node.cur_concrete_types.len == 0 { if node.generic_names.len > 0 && c.cur_concrete_types.len == 0 {
// Just remember the generic function for now. // Just remember the generic function for now.
// It will be processed later in c.post_process_generic_fns, // It will be processed later in c.post_process_generic_fns,
// after all other normal functions are processed. // after all other normal functions are processed.