scope: remove redundant name arg from register() (#7077)

pull/7094/head
Lukas Neubert 2020-12-02 14:40:25 +01:00 committed by GitHub
parent f8af866f76
commit 2691163a7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 38 additions and 31 deletions

View File

@ -121,7 +121,14 @@ pub fn (mut s Scope) register_struct_field(field ScopeStructField) {
s.struct_fields << field s.struct_fields << field
} }
pub fn (mut s Scope) register(name string, obj ScopeObject) { pub fn (mut s Scope) register(obj ScopeObject) {
name := if obj is ConstField {
obj.name
} else if obj is GlobalField {
obj.name
} else {
(obj as Var).name
}
if name == '_' { if name == '_' {
return return
} }

View File

@ -2278,7 +2278,7 @@ pub fn (mut c Checker) assign_stmt(mut assign_stmt ast.AssignStmt) {
} }
fn scope_register_it(mut s ast.Scope, pos token.Position, typ table.Type) { fn scope_register_it(mut s ast.Scope, pos token.Position, typ table.Type) {
s.register('it', ast.Var{ s.register(ast.Var{
name: 'it' name: 'it'
pos: pos pos: pos
typ: typ typ: typ
@ -2287,13 +2287,13 @@ fn scope_register_it(mut s ast.Scope, pos token.Position, typ table.Type) {
} }
fn scope_register_ab(mut s ast.Scope, pos token.Position, typ table.Type) { fn scope_register_ab(mut s ast.Scope, pos token.Position, typ table.Type) {
s.register('a', ast.Var{ s.register(ast.Var{
name: 'a' name: 'a'
pos: pos pos: pos
typ: typ typ: typ
is_used: true is_used: true
}) })
s.register('b', ast.Var{ s.register(ast.Var{
name: 'b' name: 'b'
pos: pos pos: pos
typ: typ typ: typ
@ -3624,7 +3624,7 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, type_sym table.TypeSymbol
// smartcast either if the value is immutable or if the mut argument is explicitly given // smartcast either if the value is immutable or if the mut argument is explicitly given
if (!is_mut || node.cond.is_mut) && !is_already_casted { if (!is_mut || node.cond.is_mut) && !is_already_casted {
sum_type_casts << expr_type sum_type_casts << expr_type
scope.register(node.cond.name, ast.Var{ scope.register(ast.Var{
name: node.cond.name name: node.cond.name
typ: node.cond_type typ: node.cond_type
pos: node.cond.pos pos: node.cond.pos
@ -3862,7 +3862,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type {
// smartcast either if the value is immutable or if the mut argument is explicitly given // smartcast either if the value is immutable or if the mut argument is explicitly given
if !is_mut || infix.left.is_mut { if !is_mut || infix.left.is_mut {
sum_type_casts << right_expr.typ sum_type_casts << right_expr.typ
scope.register(infix.left.name, ast.Var{ scope.register(ast.Var{
name: infix.left.name name: infix.left.name
typ: infix.left_type typ: infix.left_type
sum_type_casts: sum_type_casts sum_type_casts: sum_type_casts
@ -3872,7 +3872,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type {
}) })
} }
} else if left_sym.kind == .interface_ { } else if left_sym.kind == .interface_ {
scope.register(infix.left.name, ast.Var{ scope.register(ast.Var{
name: infix.left.name name: infix.left.name
typ: right_expr.typ.to_ptr() typ: right_expr.typ.to_ptr()
sum_type_casts: sum_type_casts sum_type_casts: sum_type_casts

View File

@ -649,7 +649,7 @@ fn (mut g Gen) autofree_call_pregen(node ast.CallExpr) {
} }
s = '$t = ' s = '$t = '
} else { } else {
scope.register(t, ast.Var{ scope.register(ast.Var{
name: t name: t
typ: table.string_type // is_arg: true // TODO hack so that it's not freed twice when out of scope. it's probably better to use one model typ: table.string_type // is_arg: true // TODO hack so that it's not freed twice when out of scope. it's probably better to use one model
is_autofree_tmp: true is_autofree_tmp: true

View File

@ -140,7 +140,7 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme
} }
obj := ast.ScopeObject(v) obj := ast.ScopeObject(v)
lx.obj = obj lx.obj = obj
p.scope.register(lx.name, obj) p.scope.register(obj)
} }
} }
ast.IndexExpr { ast.IndexExpr {

View File

@ -127,7 +127,7 @@ fn (mut p Parser) vweb() ast.ComptimeCall {
if obj is ast.Var { if obj is ast.Var {
mut v := obj mut v := obj
v.pos = stmt.body_pos v.pos = stmt.body_pos
tmpl_scope.register(v.name, v) tmpl_scope.register(v)
// set the controller action var to used // set the controller action var to used
// if it's unused in the template it will warn // if it's unused in the template it will warn
v.is_used = true v.is_used = true
@ -159,12 +159,12 @@ fn (mut p Parser) comp_for() ast.CompFor {
for_val := p.check_name() for_val := p.check_name()
mut kind := ast.CompForKind.methods mut kind := ast.CompForKind.methods
if for_val == 'methods' { if for_val == 'methods' {
p.scope.register(val_var, ast.Var{ p.scope.register(ast.Var{
name: val_var name: val_var
typ: p.table.find_type_idx('FunctionData') typ: p.table.find_type_idx('FunctionData')
}) })
} else if for_val == 'fields' { } else if for_val == 'fields' {
p.scope.register(val_var, ast.Var{ p.scope.register(ast.Var{
name: val_var name: val_var
typ: p.table.find_type_idx('FieldData') typ: p.table.find_type_idx('FieldData')
}) })

View File

@ -63,13 +63,13 @@ pub fn (mut p Parser) call_expr(language table.Language, mod string) ast.CallExp
p.inside_or_expr = true p.inside_or_expr = true
p.next() p.next()
p.open_scope() p.open_scope()
p.scope.register('err', ast.Var{ p.scope.register(ast.Var{
name: 'err' name: 'err'
typ: table.string_type typ: table.string_type
pos: p.tok.position() pos: p.tok.position()
is_used: true is_used: true
}) })
p.scope.register('errcode', ast.Var{ p.scope.register(ast.Var{
name: 'errcode' name: 'errcode'
typ: table.int_type typ: table.int_type
pos: p.tok.position() pos: p.tok.position()
@ -251,7 +251,7 @@ fn (mut p Parser) fn_decl() ast.FnDecl {
p.error_with_pos('redefinition of parameter `$param.name`', param.pos) p.error_with_pos('redefinition of parameter `$param.name`', param.pos)
break break
} }
p.scope.register(param.name, ast.Var{ p.scope.register(ast.Var{
name: param.name name: param.name
typ: param.typ typ: param.typ
is_mut: param.is_mut is_mut: param.is_mut
@ -370,7 +370,7 @@ fn (mut p Parser) anon_fn() ast.AnonFn {
// TODO generics // TODO generics
args, _, is_variadic := p.fn_args() args, _, is_variadic := p.fn_args()
for arg in args { for arg in args {
p.scope.register(arg.name, ast.Var{ p.scope.register(ast.Var{
name: arg.name name: arg.name
typ: arg.typ typ: arg.typ
is_mut: arg.is_mut is_mut: arg.is_mut

View File

@ -94,7 +94,7 @@ fn (mut p Parser) for_stmt() ast.Stmt {
if p.scope.known_var(val_var_name) { if p.scope.known_var(val_var_name) {
p.error('redefinition of value iteration variable `$val_var_name`') p.error('redefinition of value iteration variable `$val_var_name`')
} }
p.scope.register(key_var_name, ast.Var{ p.scope.register(ast.Var{
name: key_var_name name: key_var_name
typ: table.int_type typ: table.int_type
pos: key_var_pos pos: key_var_pos
@ -117,7 +117,7 @@ fn (mut p Parser) for_stmt() ast.Stmt {
is_range = true is_range = true
p.next() p.next()
high_expr = p.expr(0) high_expr = p.expr(0)
p.scope.register(val_var_name, ast.Var{ p.scope.register(ast.Var{
name: val_var_name name: val_var_name
typ: table.int_type typ: table.int_type
pos: val_var_pos pos: val_var_pos
@ -127,7 +127,7 @@ fn (mut p Parser) for_stmt() ast.Stmt {
} }
} else { } else {
// this type will be set in checker // this type will be set in checker
p.scope.register(val_var_name, ast.Var{ p.scope.register(ast.Var{
name: val_var_name name: val_var_name
pos: val_var_pos pos: val_var_pos
is_mut: val_is_mut is_mut: val_is_mut

View File

@ -45,13 +45,13 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
// only declare `err` if previous branch was an `if` guard // only declare `err` if previous branch was an `if` guard
if prev_guard { if prev_guard {
p.open_scope() p.open_scope()
p.scope.register('errcode', ast.Var{ p.scope.register(ast.Var{
name: 'errcode' name: 'errcode'
typ: table.int_type typ: table.int_type
pos: body_pos pos: body_pos
is_used: true is_used: true
}) })
p.scope.register('err', ast.Var{ p.scope.register(ast.Var{
name: 'err' name: 'err'
typ: table.string_type typ: table.string_type
pos: body_pos pos: body_pos
@ -100,7 +100,7 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
var_name: var_name var_name: var_name
expr: expr expr: expr
} }
p.scope.register(var_name, ast.Var{ p.scope.register(ast.Var{
name: var_name name: var_name
expr: cond expr: cond
pos: var_pos pos: var_pos

View File

@ -1276,7 +1276,7 @@ fn (mut p Parser) index_expr(left ast.Expr) ast.IndexExpr {
} }
fn (mut p Parser) scope_register_it() { fn (mut p Parser) scope_register_it() {
p.scope.register('it', ast.Var{ p.scope.register(ast.Var{
name: 'it' name: 'it'
pos: p.tok.position() pos: p.tok.position()
is_used: true is_used: true
@ -1284,12 +1284,12 @@ fn (mut p Parser) scope_register_it() {
} }
fn (mut p Parser) scope_register_ab() { fn (mut p Parser) scope_register_ab() {
p.scope.register('a', ast.Var{ p.scope.register(ast.Var{
name: 'a' name: 'a'
pos: p.tok.position() pos: p.tok.position()
is_used: true is_used: true
}) })
p.scope.register('b', ast.Var{ p.scope.register(ast.Var{
name: 'b' name: 'b'
pos: p.tok.position() pos: p.tok.position()
is_used: true is_used: true
@ -1328,13 +1328,13 @@ fn (mut p Parser) dot_expr(left ast.Expr) ast.Expr {
if p.tok.kind == .key_orelse { if p.tok.kind == .key_orelse {
p.next() p.next()
p.open_scope() p.open_scope()
p.scope.register('errcode', ast.Var{ p.scope.register(ast.Var{
name: 'errcode' name: 'errcode'
typ: table.int_type typ: table.int_type
pos: p.tok.position() pos: p.tok.position()
is_used: true is_used: true
}) })
p.scope.register('err', ast.Var{ p.scope.register(ast.Var{
name: 'err' name: 'err'
typ: table.string_type typ: table.string_type
pos: p.tok.position() pos: p.tok.position()
@ -1746,7 +1746,7 @@ fn (mut p Parser) const_decl() ast.ConstDecl {
comments: comments comments: comments
} }
fields << field fields << field
p.global_scope.register(field.name, field) p.global_scope.register(field)
comments = [] comments = []
} }
p.top_level_statement_end() p.top_level_statement_end()
@ -1835,7 +1835,7 @@ fn (mut p Parser) global_decl() ast.GlobalDecl {
comments: comments comments: comments
} }
fields << field fields << field
p.global_scope.register(field.name, field) p.global_scope.register(field)
comments = [] comments = []
} }
p.check(.rpar) p.check(.rpar)

View File

@ -362,13 +362,13 @@ fn (mut p Parser) prefix_expr() ast.PrefixExpr {
if p.tok.kind == .key_orelse { if p.tok.kind == .key_orelse {
p.next() p.next()
p.open_scope() p.open_scope()
p.scope.register('errcode', ast.Var{ p.scope.register(ast.Var{
name: 'errcode' name: 'errcode'
typ: table.int_type typ: table.int_type
pos: p.tok.position() pos: p.tok.position()
is_used: true is_used: true
}) })
p.scope.register('err', ast.Var{ p.scope.register(ast.Var{
name: 'err' name: 'err'
typ: table.string_type typ: table.string_type
pos: p.tok.position() pos: p.tok.position()