ast, checker, cgen: improve sorting globals and consts (#14769)

master
yuyi 2022-06-16 19:32:41 +08:00 committed by GitHub
parent 9c72b85f72
commit 26714fadc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 22 deletions

View File

@ -2146,31 +2146,11 @@ pub fn (t &Table) is_comptime_type(x Type, y ComptimeType) bool {
pub fn (t &Table) dependent_names_in_expr(expr Expr) []string {
mut names := []string{}
match expr {
Ident {
if expr.kind in [.global, .constant] {
names << util.no_dots(expr.name)
}
}
ArrayInit {
for elem_expr in expr.exprs {
names << t.dependent_names_in_expr(elem_expr)
}
}
StructInit {
for field in expr.fields {
names << t.dependent_names_in_expr(field.expr)
}
}
InfixExpr {
names << t.dependent_names_in_expr(expr.left)
names << t.dependent_names_in_expr(expr.right)
}
PostfixExpr {
names << t.dependent_names_in_expr(expr.expr)
}
PrefixExpr {
names << t.dependent_names_in_expr(expr.right)
}
CallExpr {
for arg in expr.args {
names << t.dependent_names_in_expr(arg.expr)
@ -2179,6 +2159,15 @@ pub fn (t &Table) dependent_names_in_expr(expr Expr) []string {
names << func.dep_names
}
}
CastExpr {
names << t.dependent_names_in_expr(expr.expr)
names << t.dependent_names_in_expr(expr.arg)
}
Ident {
if expr.kind in [.global, .constant] {
names << util.no_dots(expr.name)
}
}
IfExpr {
for branch in expr.branches {
names << t.dependent_names_in_expr(branch.cond)
@ -2187,6 +2176,15 @@ pub fn (t &Table) dependent_names_in_expr(expr Expr) []string {
}
}
}
InfixExpr {
names << t.dependent_names_in_expr(expr.left)
names << t.dependent_names_in_expr(expr.right)
}
MapInit {
for val in expr.vals {
names << t.dependent_names_in_expr(val)
}
}
MatchExpr {
names << t.dependent_names_in_expr(expr.cond)
for branch in expr.branches {
@ -2195,6 +2193,20 @@ pub fn (t &Table) dependent_names_in_expr(expr Expr) []string {
}
}
}
ParExpr {
names << t.dependent_names_in_expr(expr.expr)
}
PostfixExpr {
names << t.dependent_names_in_expr(expr.expr)
}
PrefixExpr {
names << t.dependent_names_in_expr(expr.right)
}
StructInit {
for field in expr.fields {
names << t.dependent_names_in_expr(field.expr)
}
}
else {}
}
return names
@ -2220,6 +2232,19 @@ pub fn (t &Table) dependent_names_in_stmt(stmt Stmt) []string {
names << t.dependent_names_in_stmt(stmt_)
}
}
ForStmt {
for stmt_ in stmt.stmts {
names << t.dependent_names_in_stmt(stmt_)
}
}
ForCStmt {
names << t.dependent_names_in_stmt(stmt.init)
names << t.dependent_names_in_expr(stmt.cond)
names << t.dependent_names_in_stmt(stmt.inc)
for stmt_ in stmt.stmts {
names << t.dependent_names_in_stmt(stmt_)
}
}
Return {
for expr in stmt.exprs {
names << t.dependent_names_in_expr(expr)

View File

@ -335,7 +335,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
}
node.source_file = c.file
if c.table.known_fn(node.name) {
if c.table.known_fn(node.name) && node.name != 'main.main' {
mut dep_names := []string{}
for stmt in node.stmts {
dep_names << c.table.dependent_names_in_stmt(stmt)

View File

@ -4595,7 +4595,7 @@ fn (mut g Gen) const_decl_init_later(mod string, name string, expr ast.Expr, typ
g.global_const_defs[util.no_dots(name)] = GlobalConstDef{
mod: mod
def: '$styp $cname; // inited later'
init: init.str()
init: init.str().trim_right('\n')
dep_names: g.table.dependent_names_in_expr(expr)
}
if g.is_autofree {