v.ast: remove the `stmt.position()` method (#9233)

pull/9249/head
spaceface 2021-03-11 13:50:02 +01:00 committed by GitHub
parent 6628bbc690
commit 7d0cba5e96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 14 additions and 34 deletions

View File

@ -1356,28 +1356,10 @@ pub:
is_ptr bool // whether the type is a pointer
}
pub fn (stmt Stmt) position() token.Position {
match stmt {
AssertStmt, AssignStmt, Block, BranchStmt, CompFor, ConstDecl, DeferStmt, EnumDecl, ExprStmt,
FnDecl, ForCStmt, ForInStmt, ForStmt, GotoLabel, GotoStmt, Import, Return, StructDecl,
GlobalDecl, HashStmt, InterfaceDecl, Module, SqlStmt, GoStmt {
return stmt.pos
}
TypeDecl {
match stmt {
AliasTypeDecl, FnTypeDecl, SumTypeDecl { return stmt.pos }
}
}
// Please, do NOT use else{} here.
// This match is exhaustive *on purpose*, to help force
// maintaining/implementing proper .pos fields.
}
}
pub fn (node Node) position() token.Position {
match node {
Stmt {
mut pos := node.position()
mut pos := node.pos
if node is Import {
for sym in node.syms {
pos = pos.extend(sym.pos)
@ -1406,8 +1388,8 @@ pub fn (node Node) position() token.Position {
File {
mut pos := token.Position{}
if node.stmts.len > 0 {
first_pos := node.stmts.first().position()
last_pos := node.stmts.last().position()
first_pos := node.stmts.first().pos
last_pos := node.stmts.last().pos
pos = first_pos.extend_with_last_line(last_pos, last_pos.line_nr)
}
return pos

View File

@ -3160,7 +3160,7 @@ fn (mut c Checker) check_loop_label(label string, pos token.Position) {
fn (mut c Checker) stmt(node ast.Stmt) {
$if trace_checker ? {
stmt_pos := node.position()
stmt_pos := node.pos
eprintln('checking file: ${c.file.path:-30} | stmt pos: ${stmt_pos.str():-45} | stmt')
}
// c.expected_type = table.void_type
@ -3602,7 +3602,7 @@ fn (mut c Checker) stmts(stmts []ast.Stmt) {
for stmt in stmts {
if c.scope_returns {
if unreachable.line_nr == -1 {
unreachable = stmt.position()
unreachable = stmt.pos
}
}
c.stmt(stmt)
@ -4410,9 +4410,7 @@ pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) table.Type {
// probably any mismatch will be caught by not producing a value instead
for st in branch.stmts[0..branch.stmts.len - 1] {
// must not contain C statements
st.check_c_expr() or {
c.error('`match` expression branch has $err', st.position())
}
st.check_c_expr() or { c.error('`match` expression branch has $err', st.pos) }
}
}
// If the last statement is an expression, return its type
@ -4791,7 +4789,7 @@ pub fn (mut c Checker) select_expr(mut node ast.SelectExpr) table.Type {
}
else {
if !branch.is_else {
c.error('receive or send statement expected as `select` key', branch.stmt.position())
c.error('receive or send statement expected as `select` key', branch.stmt.pos)
}
}
}
@ -5031,7 +5029,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type {
}
for st in branch.stmts {
// must not contain C statements
st.check_c_expr() or { c.error('`if` expression branch has $err', st.position()) }
st.check_c_expr() or { c.error('`if` expression branch has $err', st.pos) }
}
}
// Also check for returns inside a comp.if's statements, even if its contents aren't parsed

View File

@ -130,7 +130,7 @@ pub fn (mut d Doc) stmt(stmt ast.Stmt, filename string) ?DocNode {
mut node := DocNode{
name: d.stmt_name(stmt)
content: d.stmt_signature(stmt)
pos: d.convert_pos(filename, stmt.position())
pos: d.convert_pos(filename, stmt.pos)
file_path: os.join_path(d.base_path, filename)
is_pub: d.stmt_pub(stmt)
}

View File

@ -378,7 +378,7 @@ pub fn (mut f Fmt) stmt_str(node ast.Stmt) string {
pub fn (mut f Fmt) stmt(node ast.Stmt) {
if f.is_debug {
eprintln('stmt: ${node.position():-42} | node: ${node.type_name():-20}')
eprintln('stmt: ${node.pos:-42} | node: ${node.type_name():-20}')
}
match node {
ast.AssignStmt {

View File

@ -956,11 +956,11 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) {
// stmt := stmts[stmts.len-1]
if stmt !is ast.FnDecl && g.inside_ternary == 0 {
// g.writeln('// autofree scope')
// g.writeln('// autofree_scope_vars($stmt.position().pos) | ${typeof(stmt)}')
// g.writeln('// autofree_scope_vars($stmt.pos.pos) | ${typeof(stmt)}')
// go back 1 position is important so we dont get the
// internal scope of for loops and possibly other nodes
// g.autofree_scope_vars(stmt.position().pos - 1)
mut stmt_pos := stmt.position()
// g.autofree_scope_vars(stmt.pos.pos - 1)
mut stmt_pos := stmt.pos
if stmt_pos.pos == 0 {
// Do not autofree if the position is 0, since the correct scope won't be found.
// Report a bug, since position shouldn't be 0 for most nodes.

View File

@ -384,7 +384,7 @@ fn (mut p Parser) select_expr() ast.SelectExpr {
}
}
else {
p.error_with_pos('select: transmission statement expected', stmt.position())
p.error_with_pos('select: transmission statement expected', stmt.pos)
return ast.SelectExpr{}
}
}