From 3d7713b5af2e1229949b31dcce74c7aba1fe042a Mon Sep 17 00:00:00 2001
From: Kimplul <kimi.h.kuparinen@gmail.com>
Date: Tue, 18 Mar 2025 18:37:00 +0200
Subject: add move queues

+ Returning blocks don't want to show moves for subsequent statements,
  but do want to show them for possible closure callers above.
---
 src/analyze.c | 50 +++++++++++++++++++++++++++++++-------------------
 1 file changed, 31 insertions(+), 19 deletions(-)

(limited to 'src/analyze.c')

diff --git a/src/analyze.c b/src/analyze.c
index 7eee49a..c33a901 100644
--- a/src/analyze.c
+++ b/src/analyze.c
@@ -10,6 +10,7 @@ struct state {
 };
 
 static int analyze(struct state *state, struct scope *scope, struct ast *node);
+static int analyze_known_block(struct state *state, struct scope *scope, struct ast *node);
 static int analyze_list(struct state *state, struct scope *scope,
                         struct ast *nodes);
 
@@ -58,8 +59,10 @@ static int analyze_proc(struct state *state, struct scope *scope,
 	}
 
 	node->t = proc_type;
+	if (!proc_body(node))
+		return 0;
 
-	return analyze(&proc_state, proc_scope, proc_body(node));
+	return analyze_known_block(&proc_state, proc_scope, proc_body(node));
 }
 
 static int analyze_unop(struct state *state, struct scope *scope,
@@ -132,29 +135,19 @@ static int analyze_comparison(struct state *state, struct scope *scope,
 	return 0;
 }
 
-static int analyze_block(struct state *state, struct scope *scope,
-                         struct ast *node)
+static int analyze_known_block(struct state *state, struct scope *scope, struct ast *node)
 {
-	struct scope *block_scope = create_scope();
-	if (!block_scope) {
-		internal_error("failed to allocate block scope");
-		return -1;
-	}
+	assert(node && node->k == AST_BLOCK);
 
-	scope_add_scope(scope, block_scope);
-	if (analyze_list(state, block_scope, block_body(node)))
+	node->scope = scope;
+	node->t = tgen_void(node->loc);
+	if (analyze_list(state, scope, block_body(node)))
 		return -1;
 
-	struct ast *last = ast_last(block_body(node));
-	if (last)
-		node->t = last->t;
-	else
-		node->t = tgen_void(node->loc);
-
-	if (!block_error(node))
+	struct ast *err = block_error(node);
+	if (!err)
 		return 0;
 
-	struct ast *err = block_error(node);
 	if (error_str(err))
 		return 0;
 
@@ -176,6 +169,19 @@ static int analyze_block(struct state *state, struct scope *scope,
 	return 0;
 }
 
+static int analyze_block(struct state *state, struct scope *scope,
+                         struct ast *node)
+{
+	struct scope *block_scope = create_scope();
+	if (!block_scope) {
+		internal_error("failed to allocate block scope");
+		return -1;
+	}
+
+	scope_add_scope(scope, block_scope);
+	return analyze_known_block(state, scope, node);
+}
+
 static int analyze_var(struct state *state, struct scope *scope,
                        struct ast *node)
 {
@@ -362,7 +368,7 @@ static int analyze_closure(struct state *state, struct scope *scope,
 	if (analyze_list(state, closure_scope, closure_bindings(node)))
 		return -1;
 
-	if (analyze(state, closure_scope, closure_body(node)))
+	if (analyze_known_block(state, closure_scope, closure_body(node)))
 		return -1;
 
 	struct type *callable = NULL;
@@ -387,6 +393,9 @@ static int analyze_closure(struct state *state, struct scope *scope,
 static int analyze_int(struct state *state, struct scope *scope,
                        struct ast *node)
 {
+	(void)state;
+	(void)scope;
+
 	/** @todo do this properly, very hacky, bad bad bad */
 	char *i = strdup("int");
 	if (!i) {
@@ -407,6 +416,9 @@ static int analyze_int(struct state *state, struct scope *scope,
 static int analyze_str(struct state *state, struct scope *scope,
                        struct ast *node)
 {
+	(void)state;
+	(void)scope;
+
 	/** @todo do this properly, very hacky, bad bad bad */
 	char *i = strdup("char");
 	if (!i) {
-- 
cgit v1.2.3