v.ast: remove the `stmt.position()` method (#9233)
parent
6628bbc690
commit
7d0cba5e96
|
@ -1356,28 +1356,10 @@ pub:
|
||||||
is_ptr bool // whether the type is a pointer
|
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 {
|
pub fn (node Node) position() token.Position {
|
||||||
match node {
|
match node {
|
||||||
Stmt {
|
Stmt {
|
||||||
mut pos := node.position()
|
mut pos := node.pos
|
||||||
if node is Import {
|
if node is Import {
|
||||||
for sym in node.syms {
|
for sym in node.syms {
|
||||||
pos = pos.extend(sym.pos)
|
pos = pos.extend(sym.pos)
|
||||||
|
@ -1406,8 +1388,8 @@ pub fn (node Node) position() token.Position {
|
||||||
File {
|
File {
|
||||||
mut pos := token.Position{}
|
mut pos := token.Position{}
|
||||||
if node.stmts.len > 0 {
|
if node.stmts.len > 0 {
|
||||||
first_pos := node.stmts.first().position()
|
first_pos := node.stmts.first().pos
|
||||||
last_pos := node.stmts.last().position()
|
last_pos := node.stmts.last().pos
|
||||||
pos = first_pos.extend_with_last_line(last_pos, last_pos.line_nr)
|
pos = first_pos.extend_with_last_line(last_pos, last_pos.line_nr)
|
||||||
}
|
}
|
||||||
return pos
|
return pos
|
||||||
|
|
|
@ -3160,7 +3160,7 @@ fn (mut c Checker) check_loop_label(label string, pos token.Position) {
|
||||||
|
|
||||||
fn (mut c Checker) stmt(node ast.Stmt) {
|
fn (mut c Checker) stmt(node ast.Stmt) {
|
||||||
$if trace_checker ? {
|
$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')
|
eprintln('checking file: ${c.file.path:-30} | stmt pos: ${stmt_pos.str():-45} | stmt')
|
||||||
}
|
}
|
||||||
// c.expected_type = table.void_type
|
// c.expected_type = table.void_type
|
||||||
|
@ -3602,7 +3602,7 @@ fn (mut c Checker) stmts(stmts []ast.Stmt) {
|
||||||
for stmt in stmts {
|
for stmt in stmts {
|
||||||
if c.scope_returns {
|
if c.scope_returns {
|
||||||
if unreachable.line_nr == -1 {
|
if unreachable.line_nr == -1 {
|
||||||
unreachable = stmt.position()
|
unreachable = stmt.pos
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.stmt(stmt)
|
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
|
// probably any mismatch will be caught by not producing a value instead
|
||||||
for st in branch.stmts[0..branch.stmts.len - 1] {
|
for st in branch.stmts[0..branch.stmts.len - 1] {
|
||||||
// must not contain C statements
|
// must not contain C statements
|
||||||
st.check_c_expr() or {
|
st.check_c_expr() or { c.error('`match` expression branch has $err', st.pos) }
|
||||||
c.error('`match` expression branch has $err', st.position())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// If the last statement is an expression, return its type
|
// 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 {
|
else {
|
||||||
if !branch.is_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 {
|
for st in branch.stmts {
|
||||||
// must not contain C statements
|
// 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
|
// Also check for returns inside a comp.if's statements, even if its contents aren't parsed
|
||||||
|
|
|
@ -130,7 +130,7 @@ pub fn (mut d Doc) stmt(stmt ast.Stmt, filename string) ?DocNode {
|
||||||
mut node := DocNode{
|
mut node := DocNode{
|
||||||
name: d.stmt_name(stmt)
|
name: d.stmt_name(stmt)
|
||||||
content: d.stmt_signature(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)
|
file_path: os.join_path(d.base_path, filename)
|
||||||
is_pub: d.stmt_pub(stmt)
|
is_pub: d.stmt_pub(stmt)
|
||||||
}
|
}
|
||||||
|
|
|
@ -378,7 +378,7 @@ pub fn (mut f Fmt) stmt_str(node ast.Stmt) string {
|
||||||
|
|
||||||
pub fn (mut f Fmt) stmt(node ast.Stmt) {
|
pub fn (mut f Fmt) stmt(node ast.Stmt) {
|
||||||
if f.is_debug {
|
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 {
|
match node {
|
||||||
ast.AssignStmt {
|
ast.AssignStmt {
|
||||||
|
|
|
@ -956,11 +956,11 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) {
|
||||||
// stmt := stmts[stmts.len-1]
|
// stmt := stmts[stmts.len-1]
|
||||||
if stmt !is ast.FnDecl && g.inside_ternary == 0 {
|
if stmt !is ast.FnDecl && g.inside_ternary == 0 {
|
||||||
// g.writeln('// autofree scope')
|
// 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
|
// go back 1 position is important so we dont get the
|
||||||
// internal scope of for loops and possibly other nodes
|
// internal scope of for loops and possibly other nodes
|
||||||
// g.autofree_scope_vars(stmt.position().pos - 1)
|
// g.autofree_scope_vars(stmt.pos.pos - 1)
|
||||||
mut stmt_pos := stmt.position()
|
mut stmt_pos := stmt.pos
|
||||||
if stmt_pos.pos == 0 {
|
if stmt_pos.pos == 0 {
|
||||||
// Do not autofree if the position is 0, since the correct scope won't be found.
|
// 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.
|
// Report a bug, since position shouldn't be 0 for most nodes.
|
||||||
|
|
|
@ -384,7 +384,7 @@ fn (mut p Parser) select_expr() ast.SelectExpr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
p.error_with_pos('select: transmission statement expected', stmt.position())
|
p.error_with_pos('select: transmission statement expected', stmt.pos)
|
||||||
return ast.SelectExpr{}
|
return ast.SelectExpr{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue