From 9efcdde3536a250394db8e3a846c14d6c53783cd Mon Sep 17 00:00:00 2001 From: mxr <> Date: Thu, 26 Mar 2026 18:36:50 +0800 Subject: [PATCH] =?UTF-8?q?feat(sem)=E4=BF=AE=E6=94=B9=E5=8F=98=E9=87=8F?= =?UTF-8?q?=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sem/Sema.cpp | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/src/sem/Sema.cpp b/src/sem/Sema.cpp index a92a1db..01bae52 100644 --- a/src/sem/Sema.cpp +++ b/src/sem/Sema.cpp @@ -407,24 +407,6 @@ public: if (!sym) { throw std::runtime_error(FormatError("sema", "使用了未定义的变量: " + name)); } - // ========== 关键修复:绑定变量使用到定义 ========== - if (sym) { - std::cerr << "[DEBUG] 找到符号: " << sym->name - << ", kind: " << (int)sym->kind - << ", var_def_ctx: " << sym->var_def_ctx << std::endl; - if (sym->var_def_ctx) { - std::cout << "[DEBUG] 绑定变量使用" << std::endl; - sema_.BindVarUse(ctx, sym->var_def_ctx); - } - } - else if (sym->kind == SymbolKind::Parameter) { - // 对于函数参数,需要特殊处理 - // 参数可能没有对应的 VarDefContext,需要创建一个 - // 或者通过其他方式标识 - std::cout << "[DEBUG] 参数变量: " << name << " (无法绑定到 VarDefContext)" << std::endl; - // 可以创建一个临时标识,但这里先不处理 - } - // ============================================ // 检查数组访问 bool is_array_access = !ctx->exp().empty(); std::cout << "[DEBUG] name: " << name @@ -1071,7 +1053,10 @@ private: if (!sym) { throw std::runtime_error(FormatError("sema", "未定义的变量: " + name)); } - + + if (sym->kind == SymbolKind::Variable && sym->var_def_ctx) { + sema_.BindVarUse(ctx, sym->var_def_ctx); + } std::cout << "CheckLValue 绑定变量: " << name << std::endl; bool is_array_access = !ctx->exp().empty();