chore(test)修改调试输出

mxr 1 month ago
parent 6f649c0ad5
commit 1c7cc33e4b

@ -8,12 +8,22 @@
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <iostream>
#include "SysYBaseVisitor.h"
#include "SysYParser.h"
#include "ir/IR.h"
#include "sem/Sema.h"
//#define DEBUG_IRGen
#ifdef DEBUG_IRGen
#include <iostream>
#define DEBUG_MSG(msg) std::cerr << "[IRGen Debug] " << msg << std::endl
#else
#define DEBUG_MSG(msg)
#endif
namespace ir {
class Module;
class Function;

File diff suppressed because it is too large Load Diff

@ -4,6 +4,7 @@ set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
COMPILER="$ROOT_DIR/build/bin/compiler"
TMP_DIR="$ROOT_DIR/build/test_compiler"
RESULT_BASE_DIR="$ROOT_DIR/test/test_result"
TEST_DIRS=("$ROOT_DIR/test/test_case/functional" "$ROOT_DIR/test/test_case/performance")
CC_BIN="${CC:-cc}"
RUNTIME_SRC="$ROOT_DIR/sylib/sylib.c"
@ -66,7 +67,17 @@ for test_dir in "${TEST_DIRS[@]}"; do
ir_total=$((ir_total+1))
base=$(basename "$input")
stem=${base%.sy}
out_dir="$TMP_DIR/$(basename "$test_dir")"
case "$(basename "$test_dir")" in
functional)
out_dir="$RESULT_BASE_DIR/function/ir"
;;
performance)
out_dir="$RESULT_BASE_DIR/performance/ir"
;;
*)
out_dir="$RESULT_BASE_DIR/$(basename "$test_dir")"
;;
esac
mkdir -p "$out_dir"
ll_file="$out_dir/$stem.ll"
stdout_file="$out_dir/$stem.stdout"

@ -100,14 +100,14 @@ std::string MakeStaticArrayName(const ir::Function& func,
// visitDecl: 处理声明
std::any IRGenImpl::visitDecl(SysYParser::DeclContext* ctx) {
std::cerr << "[DEBUG] visitDecl: 开始处理声明" << std::endl;
DEBUG_MSG("[DEBUG] visitDecl: 开始处理声明");
if (!ctx) {
throw std::runtime_error(FormatError("irgen", "缺少变量声明"));
}
// 处理 varDecl
if (auto* varDecl = ctx->varDecl()) {
std::cerr << "[DEBUG] visitDecl: 处理变量声明" << std::endl;
DEBUG_MSG("[DEBUG] visitDecl: 处理变量声明");
for (auto* varDef : varDecl->varDef()) {
varDef->accept(this);
}
@ -115,20 +115,20 @@ std::any IRGenImpl::visitDecl(SysYParser::DeclContext* ctx) {
// 处理 constDecl
if (ctx->constDecl()) {
std::cerr << "[DEBUG] visitDecl: 处理常量声明" << std::endl;
DEBUG_MSG("[DEBUG] visitDecl: 处理常量声明");
auto* constDecl = ctx->constDecl();
for (auto* constDef : constDecl->constDef()) {
constDef->accept(this);
}
}
std::cerr << "[DEBUG] visitDecl: 声明处理完成" << std::endl;
DEBUG_MSG("[DEBUG] visitDecl: 声明处理完成");
return {};
}
// visitConstDecl: 处理常量声明
std::any IRGenImpl::visitConstDecl(SysYParser::ConstDeclContext* ctx) {
std::cerr << "[DEBUG] visitConstDecl: 开始处理常量声明" << std::endl;
DEBUG_MSG("[DEBUG] visitConstDecl: 开始处理常量声明");
if (!ctx) {
throw std::runtime_error(FormatError("irgen", "非法常量声明"));
}
@ -139,13 +139,13 @@ std::any IRGenImpl::visitConstDecl(SysYParser::ConstDeclContext* ctx) {
}
}
std::cerr << "[DEBUG] visitConstDecl: 常量声明处理完成" << std::endl;
DEBUG_MSG("[DEBUG] visitConstDecl: 常量声明处理完成");
return {};
}
// visitConstDef: 处理常量定义 - 从符号表获取常量值
std::any IRGenImpl::visitConstDef(SysYParser::ConstDefContext* ctx) {
std::cerr << "[DEBUG] visitConstDef: 开始处理常量定义" << std::endl;
DEBUG_MSG("[DEBUG] visitConstDef: 开始处理常量定义");
if (!ctx || !ctx->Ident()) {
throw std::runtime_error(FormatError("irgen", "非法常量定义"));
}
@ -158,8 +158,8 @@ std::any IRGenImpl::visitConstDef(SysYParser::ConstDefContext* ctx) {
throw std::runtime_error(FormatError("irgen", "常量符号未找到: " + const_name));
}
std::cerr << "[DEBUG] visitConstDef: 从符号表获取常量 " << const_name
<< ", is_array_const: " << sym->IsArrayConstant() << std::endl;
DEBUG_MSG("[DEBUG] visitConstDef: 从符号表获取常量 " << const_name
<< ", is_array_const: " << sym->IsArrayConstant());
// 根据符号表中的常量值创建 IR 常量
if (sym->IsArrayConstant()) {
@ -270,12 +270,12 @@ std::any IRGenImpl::visitConstDef(SysYParser::ConstDefContext* ctx) {
ir::ConstantValue* const_value = nullptr;
if (sym->type->IsInt32()) {
const_value = builder_.CreateConstInt(sym->GetIntConstant());
std::cerr << "[DEBUG] visitConstDef: 整型常量 " << const_name
<< " = " << sym->GetIntConstant() << std::endl;
DEBUG_MSG("[DEBUG] visitConstDef: 整型常量 " << const_name
<< " = " << sym->GetIntConstant());
} else if (sym->type->IsFloat()) {
const_value = builder_.CreateConstFloat(sym->GetFloatConstant());
std::cerr << "[DEBUG] visitConstDef: 浮点常量 " << const_name
<< " = " << sym->GetFloatConstant() << std::endl;
DEBUG_MSG("[DEBUG] visitConstDef: 浮点常量 " << const_name
<< " = " << sym->GetFloatConstant());
}
const_value_map_[const_name] = const_value;
@ -287,13 +287,13 @@ std::any IRGenImpl::visitConstDef(SysYParser::ConstDefContext* ctx) {
// visitVarDef: 处理变量定义 - 从符号表获取类型信息
std::any IRGenImpl::visitVarDef(SysYParser::VarDefContext* ctx) {
std::cerr << "[DEBUG] visitVarDef: 开始处理变量定义" << std::endl;
DEBUG_MSG("[DEBUG] visitVarDef: 开始处理变量定义");
if (!ctx || !ctx->Ident()) {
throw std::runtime_error(FormatError("irgen", "非法变量定义"));
}
std::string varName = ctx->Ident()->getText();
std::cerr << "[DEBUG] visitVarDef: 变量名称: " << varName << std::endl;
DEBUG_MSG("[DEBUG] visitVarDef: 变量名称: " << varName);
// 防止重复分配
if (storage_map_.find(ctx) != storage_map_.end()) {
@ -306,17 +306,17 @@ std::any IRGenImpl::visitVarDef(SysYParser::VarDefContext* ctx) {
throw std::runtime_error(FormatError("irgen", "变量符号未找到: " + varName));
}
std::cerr << "[DEBUG] visitVarDef: 变量类型: "
DEBUG_MSG("[DEBUG] visitVarDef: 变量类型: "
<< (sym->type->IsInt32() ? "int" :
sym->type->IsFloat() ? "float" :
sym->type->IsArray() ? "array" : "unknown") << std::endl;
sym->type->IsArray() ? "array" : "unknown"));
// 根据作用域处理
if (func_ == nullptr) {
std::cerr << "[DEBUG] visitVarDef: 处理全局变量" << std::endl;
DEBUG_MSG("[DEBUG] visitVarDef: 处理全局变量");
return HandleGlobalVariable(ctx, varName, sym);
} else {
std::cerr << "[DEBUG] visitVarDef: 处理局部变量" << std::endl;
DEBUG_MSG("[DEBUG] visitVarDef: 处理局部变量");
return HandleLocalVariable(ctx, varName, sym);
}
}
@ -325,7 +325,7 @@ std::any IRGenImpl::visitVarDef(SysYParser::VarDefContext* ctx) {
std::any IRGenImpl::HandleGlobalVariable(SysYParser::VarDefContext* ctx,
const std::string& varName,
const Symbol* sym) {
std::cerr << "[DEBUG] HandleGlobalVariable: 开始处理全局变量 " << varName << std::endl;
DEBUG_MSG("[DEBUG] HandleGlobalVariable: 开始处理全局变量 " << varName);
if (!sym) {
throw std::runtime_error(FormatError("irgen", "符号表信息缺失: " + varName));
@ -349,9 +349,9 @@ std::any IRGenImpl::HandleGlobalVariable(SysYParser::VarDefContext* ctx,
const auto& dimensions = array_ty->GetDimensions();
size_t total_size = array_ty->GetElementCount();
std::cerr << "[DEBUG] HandleGlobalVariable: 全局数组 " << varName << " 维度: ";
for (int d : dimensions) std::cerr << d << " ";
std::cerr << ", 总大小: " << total_size << std::endl;
DEBUG_MSG("[DEBUG] HandleGlobalVariable: 全局数组 " << varName << " 维度: ");
for (int d : dimensions) DEBUG_MSG(d << " ");
DEBUG_MSG(", 总大小: " << total_size);
// 创建全局数组
ir::GlobalValue* global_array = module_.CreateGlobal(varName, sym->type);
@ -359,7 +359,7 @@ std::any IRGenImpl::HandleGlobalVariable(SysYParser::VarDefContext* ctx,
// 处理初始化值(使用带维度感知的展平)
std::vector<ir::ConstantValue*> init_consts;
if (auto* initVal = ctx->initVal()) {
std::cerr << "[DEBUG] HandleGlobalVariable: 处理初始化值" << std::endl;
DEBUG_MSG("[DEBUG] HandleGlobalVariable: 处理初始化值");
// 全局变量的初始化必须是常量表达式(语义检查已保证)
std::vector<ir::Value*> flat_vals = FlattenInitVal(
initVal, dimensions, is_float);
@ -439,7 +439,7 @@ std::any IRGenImpl::HandleGlobalVariable(SysYParser::VarDefContext* ctx,
global_map_[varName] = global_var;
}
std::cerr << "[DEBUG] HandleGlobalVariable: 全局变量处理完成" << std::endl;
DEBUG_MSG("[DEBUG] HandleGlobalVariable: 全局变量处理完成");
return {};
}
@ -447,7 +447,7 @@ std::any IRGenImpl::HandleGlobalVariable(SysYParser::VarDefContext* ctx,
std::any IRGenImpl::HandleLocalVariable(SysYParser::VarDefContext* ctx,
const std::string& varName,
const Symbol* sym) {
std::cerr << "[DEBUG] HandleLocalVariable: 开始处理局部变量 " << varName << std::endl;
DEBUG_MSG("[DEBUG] HandleLocalVariable: 开始处理局部变量 " << varName);
if (!sym) {
throw std::runtime_error(FormatError("irgen", "符号表信息缺失: " + varName));
@ -473,8 +473,8 @@ std::any IRGenImpl::HandleLocalVariable(SysYParser::VarDefContext* ctx,
const bool use_heap_storage =
current_function_is_recursive_ || total_bytes > kLocalArrayHeapThresholdBytes;
std::cerr << "[DEBUG] HandleLocalVariable: 局部数组 " << varName
<< " 总大小: " << total_size << std::endl;
DEBUG_MSG("[DEBUG] HandleLocalVariable: 局部数组 " << varName
<< " 总大小: " << total_size);
ir::Value* array_slot = nullptr;
if (use_heap_storage) {
@ -520,8 +520,8 @@ std::any IRGenImpl::HandleLocalVariable(SysYParser::VarDefContext* ctx,
if (is_all_zero_init && !use_heap_storage) {
builder_.CreateStore(module_.GetContext().GetAggregateZero(sym->type),
array_slot);
std::cerr << "[DEBUG] HandleLocalVariable: aggregate zeroinitializer store for "
<< varName << std::endl;
DEBUG_MSG("[DEBUG] HandleLocalVariable: aggregate zeroinitializer store for "
<< varName);
return {};
}
@ -617,35 +617,35 @@ std::any IRGenImpl::HandleLocalVariable(SysYParser::VarDefContext* ctx,
builder_.CreateStore(init, slot);
}
std::cerr << "[DEBUG] HandleLocalVariable: 局部变量处理完成" << std::endl;
DEBUG_MSG("[DEBUG] HandleLocalVariable: 局部变量处理完成");
return {};
}
// visitInitVal: 处理初始化值
std::any IRGenImpl::visitInitVal(SysYParser::InitValContext* ctx) {
std::cerr << "[DEBUG] visitInitVal: 开始处理初始化值" << std::endl;
DEBUG_MSG("[DEBUG] visitInitVal: 开始处理初始化值");
if (!ctx) {
throw std::runtime_error(FormatError("irgen", "非法初始化值"));
}
// 如果是单个表达式
if (ctx->exp()) {
std::cerr << "[DEBUG] visitInitVal: 处理表达式初始化" << std::endl;
DEBUG_MSG("[DEBUG] visitInitVal: 处理表达式初始化");
return EvalExpr(*ctx->exp());
}
// 如果是聚合初始化(花括号列表)
else if (!ctx->initVal().empty()) {
std::cerr << "[DEBUG] visitInitVal: 处理聚合初始化" << std::endl;
DEBUG_MSG("[DEBUG] visitInitVal: 处理聚合初始化");
return ProcessNestedInitVals(ctx);
}
std::cerr << "[DEBUG] visitInitVal: 空初始化列表" << std::endl;
DEBUG_MSG("[DEBUG] visitInitVal: 空初始化列表");
return std::vector<ir::Value*>{};
}
// ProcessNestedInitVals: 处理嵌套聚合初始化
std::vector<ir::Value*> IRGenImpl::ProcessNestedInitVals(SysYParser::InitValContext* ctx) {
std::cerr << "[DEBUG] ProcessNestedInitVals: 开始处理嵌套初始化值" << std::endl;
DEBUG_MSG("[DEBUG] ProcessNestedInitVals: 开始处理嵌套初始化值");
std::vector<ir::Value*> all_values;
for (auto* init_val : ctx->initVal()) {
@ -655,18 +655,18 @@ std::vector<ir::Value*> IRGenImpl::ProcessNestedInitVals(SysYParser::InitValCont
// 尝试获取单个值
ir::Value* value = std::any_cast<ir::Value*>(result);
all_values.push_back(value);
std::cerr << "[DEBUG] ProcessNestedInitVals: 获取到单个值" << std::endl;
DEBUG_MSG("[DEBUG] ProcessNestedInitVals: 获取到单个值");
} catch (const std::bad_any_cast&) {
try {
// 尝试获取值列表(嵌套情况)
std::vector<ir::Value*> nested_values =
std::any_cast<std::vector<ir::Value*>>(result);
std::cerr << "[DEBUG] ProcessNestedInitVals: 获取到嵌套值列表, 大小: "
<< nested_values.size() << std::endl;
DEBUG_MSG("[DEBUG] ProcessNestedInitVals: 获取到嵌套值列表, 大小: "
<< nested_values.size());
all_values.insert(all_values.end(),
nested_values.begin(), nested_values.end());
} catch (const std::bad_any_cast&) {
std::cerr << "[ERROR] ProcessNestedInitVals: 不支持的初始化值类型" << std::endl;
DEBUG_MSG("[ERROR] ProcessNestedInitVals: 不支持的初始化值类型");
throw std::runtime_error(
FormatError("irgen", "不支持的初始化值类型"));
}
@ -674,8 +674,8 @@ std::vector<ir::Value*> IRGenImpl::ProcessNestedInitVals(SysYParser::InitValCont
}
}
std::cerr << "[DEBUG] ProcessNestedInitVals: 共获取 " << all_values.size()
<< " 个初始化值" << std::endl;
DEBUG_MSG("[DEBUG] ProcessNestedInitVals: 共获取 " << all_values.size()
<< " 个初始化值");
return all_values;
}

@ -23,50 +23,50 @@
// 表达式生成
ir::Value* IRGenImpl::EvalExpr(SysYParser::ExpContext& expr) {
std::cerr << "[DEBUG IRGEN] EvalExpr: 开始处理表达式 " << expr.getText() << std::endl;
DEBUG_MSG("[DEBUG IRGEN] EvalExpr: 开始处理表达式 " << expr.getText());
try {
auto result_any = expr.accept(this);
if (!result_any.has_value()) {
std::cerr << "[ERROR] EvalExpr: result_any has no value" << std::endl;
DEBUG_MSG("[ERROR] EvalExpr: result_any has no value");
throw std::runtime_error("表达式求值结果为空");
}
try {
ir::Value* result = std::any_cast<ir::Value*>(result_any);
std::cerr << "[DEBUG] EvalExpr: success, result = " << (void*)result << std::endl;
DEBUG_MSG("[DEBUG] EvalExpr: success, result = " << (void*)result);
return result;
} catch (const std::bad_any_cast& e) {
std::cerr << "[ERROR] EvalExpr: bad any_cast - " << e.what() << std::endl;
std::cerr << " Type info: " << result_any.type().name() << std::endl;
DEBUG_MSG("[ERROR] EvalExpr: bad any_cast - " << e.what());
DEBUG_MSG(" Type info: " << result_any.type().name());
throw std::runtime_error(FormatError("irgen", "表达式求值返回了错误的类型"));
}
} catch (const std::exception& e) {
std::cerr << "[ERROR] Exception in EvalExpr: " << e.what() << std::endl;
DEBUG_MSG("[ERROR] Exception in EvalExpr: " << e.what());
throw;
}
}
ir::Value* IRGenImpl::EvalCond(SysYParser::CondContext& cond) {
std::cerr << "[DEBUG IRGEN] EvalCond: 开始处理条件表达式 " << cond.getText() << std::endl;
DEBUG_MSG("[DEBUG IRGEN] EvalCond: 开始处理条件表达式 " << cond.getText());
return std::any_cast<ir::Value*>(cond.accept(this));
}
// 基本表达式:数字、变量、括号表达式
std::any IRGenImpl::visitPrimaryExp(SysYParser::PrimaryExpContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitPrimaryExp: 开始处理基本表达式 " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitPrimaryExp: 开始处理基本表达式 " << (ctx ? ctx->getText() : "<null>"));
if (!ctx) {
throw std::runtime_error(FormatError("irgen", "缺少基本表达式"));
}
std::cerr << "[DEBUG] visitPrimaryExp" << std::endl;
DEBUG_MSG("[DEBUG] visitPrimaryExp");
// 处理数字字面量
if (ctx->DECIMAL_INT()) {
int value = std::stoi(ctx->DECIMAL_INT()->getText());
ir::Value* const_int = builder_.CreateConstInt(value);
std::cerr << "[DEBUG] visitPrimaryExp: constant int " << value
<< " created as " << (void*)const_int << std::endl;
DEBUG_MSG("[DEBUG] visitPrimaryExp: constant int " << value
<< " created as " << (void*)const_int);
return static_cast<ir::Value*>(const_int);
}
@ -76,13 +76,13 @@ std::any IRGenImpl::visitPrimaryExp(SysYParser::PrimaryExpContext* ctx) {
try {
value = std::stof(hex_float_str);
} catch (const std::exception& e) {
std::cerr << "[WARNING] 无法解析十六进制浮点数: " << hex_float_str
<< "使用0.0代替" << std::endl;
DEBUG_MSG("[WARNING] 无法解析十六进制浮点数: " << hex_float_str
<< "使用0.0代替");
value = 0.0f;
}
ir::Value* const_float = builder_.CreateConstFloat(value);
std::cerr << "[DEBUG] visitPrimaryExp: constant hex float " << value
<< " created as " << (void*)const_float << std::endl;
DEBUG_MSG("[DEBUG] visitPrimaryExp: constant hex float " << value
<< " created as " << (void*)const_float);
return static_cast<ir::Value*>(const_float);
}
@ -92,13 +92,13 @@ std::any IRGenImpl::visitPrimaryExp(SysYParser::PrimaryExpContext* ctx) {
try {
value = std::stof(dec_float_str);
} catch (const std::exception& e) {
std::cerr << "[WARNING] 无法解析十进制浮点数: " << dec_float_str
<< "使用0.0代替" << std::endl;
DEBUG_MSG("[WARNING] 无法解析十进制浮点数: " << dec_float_str
<< "使用0.0代替");
value = 0.0f;
}
ir::Value* const_float = builder_.CreateConstFloat(value);
std::cerr << "[DEBUG] visitPrimaryExp: constant dec float " << value
<< " created as " << (void*)const_float << std::endl;
DEBUG_MSG("[DEBUG] visitPrimaryExp: constant dec float " << value
<< " created as " << (void*)const_float);
return static_cast<ir::Value*>(const_float);
}
@ -106,8 +106,8 @@ std::any IRGenImpl::visitPrimaryExp(SysYParser::PrimaryExpContext* ctx) {
std::string hex = ctx->HEX_INT()->getText();
int value = std::stoi(hex, nullptr, 16);
ir::Value* const_int = builder_.CreateConstInt(value);
std::cerr << "[DEBUG] visitPrimaryExp: constant hex int " << value
<< " created as " << (void*)const_int << std::endl;
DEBUG_MSG("[DEBUG] visitPrimaryExp: constant hex int " << value
<< " created as " << (void*)const_int);
return static_cast<ir::Value*>(const_int);
}
@ -115,42 +115,42 @@ std::any IRGenImpl::visitPrimaryExp(SysYParser::PrimaryExpContext* ctx) {
std::string oct = ctx->OCTAL_INT()->getText();
int value = std::stoi(oct, nullptr, 8);
ir::Value* const_int = builder_.CreateConstInt(value);
std::cerr << "[DEBUG] visitPrimaryExp: constant octal int " << value
<< " created as " << (void*)const_int << std::endl;
DEBUG_MSG("[DEBUG] visitPrimaryExp: constant octal int " << value
<< " created as " << (void*)const_int);
return static_cast<ir::Value*>(const_int);
}
if (ctx->ZERO()) {
ir::Value* const_int = builder_.CreateConstInt(0);
std::cerr << "[DEBUG] visitPrimaryExp: constant zero int created" << std::endl;
DEBUG_MSG("[DEBUG] visitPrimaryExp: constant zero int created");
return static_cast<ir::Value*>(const_int);
}
// 处理变量
if (ctx->lVal()) {
std::cerr << "[DEBUG] visitPrimaryExp: visiting lVal" << std::endl;
DEBUG_MSG("[DEBUG] visitPrimaryExp: visiting lVal");
return ctx->lVal()->accept(this);
}
// 处理括号表达式
if (ctx->L_PAREN() && ctx->exp()) {
std::cerr << "[DEBUG] visitPrimaryExp: visiting parenthesized expression" << std::endl;
DEBUG_MSG("[DEBUG] visitPrimaryExp: visiting parenthesized expression");
return EvalExpr(*ctx->exp());
}
std::cerr << "[ERROR] visitPrimaryExp: unsupported primary expression type" << std::endl;
DEBUG_MSG("[ERROR] visitPrimaryExp: unsupported primary expression type");
throw std::runtime_error(FormatError("irgen", "不支持的基本表达式类型"));
}
// 左值(变量)处理
std::any IRGenImpl::visitLVal(SysYParser::LValContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitLVal: 开始处理左值 " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitLVal: 开始处理左值 " << (ctx ? ctx->getText() : "<null>"));
if (!ctx || !ctx->Ident()) {
throw std::runtime_error(FormatError("irgen", "非法左值"));
}
std::string varName = ctx->Ident()->getText();
std::cerr << "[DEBUG] visitLVal: " << varName << std::endl;
DEBUG_MSG("[DEBUG] visitLVal: " << varName);
// 先检查语义分析中常量绑定
const SysYParser::ConstDefContext* const_decl = sema_.ResolveConstUse(ctx);
@ -166,7 +166,7 @@ std::any IRGenImpl::visitLVal(SysYParser::LValContext* ctx) {
// 如果是常量,直接返回常量值
if (sym && sym->kind == SymbolKind::Constant) {
std::cerr << "[DEBUG] visitLVal: 找到常量 " << varName << std::endl;
DEBUG_MSG("[DEBUG] visitLVal: 找到常量 " << varName);
if (sym->IsScalarConstant()) {
if (sym->type->IsInt32()) {
@ -394,7 +394,7 @@ std::any IRGenImpl::visitLVal(SysYParser::LValContext* ctx) {
}
std::any IRGenImpl::visitAddExp(SysYParser::AddExpContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitAddExp: 开始处理加法表达式 " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitAddExp: 开始处理加法表达式 " << (ctx ? ctx->getText() : "<null>"));
if (!ctx) {
throw std::runtime_error(FormatError("irgen", "非法加法表达式"));
}
@ -418,10 +418,10 @@ std::any IRGenImpl::visitAddExp(SysYParser::AddExpContext* ctx) {
}
ir::Value* right = std::any_cast<ir::Value*>(right_any);
std::cerr << "[DEBUG] visitAddExp: left=" << (void*)left
DEBUG_MSG("[DEBUG] visitAddExp: left=" << (void*)left
<< ", type=" << (left->GetType()->IsFloat() ? "float" : "int")
<< ", right=" << (void*)right
<< ", type=" << (right->GetType()->IsFloat() ? "float" : "int") << std::endl;
<< ", type=" << (right->GetType()->IsFloat() ? "float" : "int"));
// 处理类型转换:如果操作数类型不同,需要进行类型转换
if (left->GetType()->IsFloat() != right->GetType()->IsFloat()) {
@ -458,7 +458,7 @@ std::any IRGenImpl::visitAddExp(SysYParser::AddExpContext* ctx) {
std::any IRGenImpl::visitMulExp(SysYParser::MulExpContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitMulExp: 开始处理乘法表达式 " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitMulExp: 开始处理乘法表达式 " << (ctx ? ctx->getText() : "<null>"));
if (!ctx) {
throw std::runtime_error(FormatError("irgen", "非法乘法表达式"));
}
@ -482,10 +482,10 @@ std::any IRGenImpl::visitMulExp(SysYParser::MulExpContext* ctx) {
}
ir::Value* right = std::any_cast<ir::Value*>(right_any);
std::cerr << "[DEBUG] visitMulExp: left=" << (void*)left
DEBUG_MSG("[DEBUG] visitMulExp: left=" << (void*)left
<< ", type=" << (left->GetType()->IsFloat() ? "float" : "int")
<< ", right=" << (void*)right
<< ", type=" << (right->GetType()->IsFloat() ? "float" : "int") << std::endl;
<< ", type=" << (right->GetType()->IsFloat() ? "float" : "int"));
// 处理类型转换:如果操作数类型不同,需要进行类型转换
if (left->GetType()->IsFloat() != right->GetType()->IsFloat()) {
@ -532,7 +532,7 @@ std::any IRGenImpl::visitMulExp(SysYParser::MulExpContext* ctx) {
// 逻辑与
std::any IRGenImpl::visitLAndExp(SysYParser::LAndExpContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitLAndExp: 开始处理逻辑与表达式 " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitLAndExp: 开始处理逻辑与表达式 " << (ctx ? ctx->getText() : "<null>"));
if (!ctx) throw std::runtime_error(FormatError("irgen", "非法逻辑与表达式"));
if (!ctx->lAndExp()) {
@ -562,7 +562,7 @@ std::any IRGenImpl::visitLAndExp(SysYParser::LAndExpContext* ctx) {
// 逻辑或
std::any IRGenImpl::visitLOrExp(SysYParser::LOrExpContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitLOrExp: 开始处理逻辑或表达式 " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitLOrExp: 开始处理逻辑或表达式 " << (ctx ? ctx->getText() : "<null>"));
if (!ctx) throw std::runtime_error(FormatError("irgen", "非法逻辑或表达式"));
if (!ctx->lOrExp()) {
@ -591,32 +591,32 @@ std::any IRGenImpl::visitLOrExp(SysYParser::LOrExpContext* ctx) {
}
std::any IRGenImpl::visitExp(SysYParser::ExpContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitExp: 开始处理表达式 " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitExp: 开始处理表达式 " << (ctx ? ctx->getText() : "<null>"));
if (!ctx) throw std::runtime_error(FormatError("irgen", "非法表达式"));
return ctx->addExp()->accept(this);
}
std::any IRGenImpl::visitCond(SysYParser::CondContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitCond: 开始处理条件 " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitCond: 开始处理条件 " << (ctx ? ctx->getText() : "<null>"));
if (!ctx) throw std::runtime_error(FormatError("irgen", "非法条件表达式"));
return ctx->lOrExp()->accept(this);
}
std::any IRGenImpl::visitCallExp(SysYParser::UnaryExpContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitCallExp: 开始处理函数调用 " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitCallExp: 开始处理函数调用 " << (ctx ? ctx->getText() : "<null>"));
if (!ctx || !ctx->Ident()) {
throw std::runtime_error(FormatError("irgen", "非法函数调用"));
}
std::string funcName = ctx->Ident()->getText();
std::cout << "[DEBUG IRGEN] visitCallExp: 调用函数 " << funcName << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitCallExp: 调用函数 " << funcName);
// 查找函数对象
ir::Function* callee = module_.FindFunction(funcName);
// 如果没找到,可能是运行时函数还没声明,尝试动态声明
if (!callee) {
std::cout << "[DEBUG IRGEN] 函数 " << funcName << " 未找到,尝试动态声明" << std::endl;
DEBUG_MSG("[DEBUG IRGEN] 函数 " << funcName << " 未找到,尝试动态声明");
// 根据函数名动态创建运行时函数声明
callee = CreateRuntimeFunctionDecl(funcName);
@ -631,9 +631,9 @@ std::any IRGenImpl::visitCallExp(SysYParser::UnaryExpContext* ctx) {
auto argList = ctx->funcRParams()->accept(this);
try {
args = std::any_cast<std::vector<ir::Value*>>(argList);
std::cout << "[DEBUG IRGEN] visitCallExp: 收集到 " << args.size() << " 个参数" << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitCallExp: 收集到 " << args.size() << " 个参数");
} catch (const std::bad_any_cast& e) {
std::cerr << "[ERROR] visitCallExp: 函数调用参数类型错误: " << e.what() << std::endl;
DEBUG_MSG("[ERROR] visitCallExp: 函数调用参数类型错误: " << e.what());
}
}
@ -673,13 +673,13 @@ std::any IRGenImpl::visitCallExp(SysYParser::UnaryExpContext* ctx) {
return static_cast<ir::Value*>(builder_.CreateConstInt(0));
}
std::cout << "[DEBUG IRGEN] visitCallExp: 函数调用完成,返回值 " << (void*)callResult << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitCallExp: 函数调用完成,返回值 " << (void*)callResult);
return static_cast<ir::Value*>(callResult);
}
// 动态创建运行时函数声明的辅助函数
ir::Function* IRGenImpl::CreateRuntimeFunctionDecl(const std::string& funcName) {
std::cerr << "[DEBUG IRGEN] CreateRuntimeFunctionDecl: 开始创建运行时函数声明 " << funcName << std::endl;
DEBUG_MSG("[DEBUG IRGEN] CreateRuntimeFunctionDecl: 开始创建运行时函数声明 " << funcName);
// 根据常见运行时函数名创建对应的函数类型
if (funcName == "getint" || funcName == "getch") {
@ -792,7 +792,7 @@ ir::Function* IRGenImpl::CreateRuntimeFunctionDecl(const std::string& funcName)
}
std::any IRGenImpl::visitUnaryExp(SysYParser::UnaryExpContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitUnaryExp: 开始处理一元表达式 " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitUnaryExp: 开始处理一元表达式 " << (ctx ? ctx->getText() : "<null>"));
if (!ctx) {
throw std::runtime_error(FormatError("irgen", "非法一元表达式"));
}
@ -852,7 +852,7 @@ std::any IRGenImpl::visitUnaryExp(SysYParser::UnaryExpContext* ctx) {
// 实现函数调用
std::any IRGenImpl::visitFuncRParams(SysYParser::FuncRParamsContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitFuncRParams: 开始处理函数参数 " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitFuncRParams: 开始处理函数参数 " << (ctx ? ctx->getText() : "<null>"));
if (!ctx) return std::vector<ir::Value*>{};
std::vector<ir::Value*> args;
for (auto* exp : ctx->exp()) {
@ -863,7 +863,7 @@ std::any IRGenImpl::visitFuncRParams(SysYParser::FuncRParamsContext* ctx) {
// visitConstExp - 处理常量表达式
std::any IRGenImpl::visitConstExp(SysYParser::ConstExpContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitConstExp: 开始处理常量表达式 " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitConstExp: 开始处理常量表达式 " << (ctx ? ctx->getText() : "<null>"));
if (!ctx || !ctx->addExp()) {
throw std::runtime_error(FormatError("irgen", "非法常量表达式"));
}
@ -884,7 +884,7 @@ std::any IRGenImpl::visitConstExp(SysYParser::ConstExpContext* ctx) {
// visitConstInitVal - 处理常量初始化值
std::any IRGenImpl::visitConstInitVal(SysYParser::ConstInitValContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitConstInitVal: 开始处理常量初始化值 " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitConstInitVal: 开始处理常量初始化值 " << (ctx ? ctx->getText() : "<null>"));
if (!ctx) {
throw std::runtime_error(FormatError("irgen", "非法常量初始化值"));
}
@ -929,7 +929,7 @@ std::any IRGenImpl::visitConstInitVal(SysYParser::ConstInitValContext* ctx) {
}
std::any IRGenImpl::visitRelExp(SysYParser::RelExpContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitRelExp: 开始处理关系表达式 " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitRelExp: 开始处理关系表达式 " << (ctx ? ctx->getText() : "<null>"));
if (!ctx) {
throw std::runtime_error(FormatError("irgen", "非法关系表达式"));
}
@ -940,10 +940,10 @@ std::any IRGenImpl::visitRelExp(SysYParser::RelExpContext* ctx) {
auto* lhs = std::any_cast<ir::Value*>(left_any);
auto* rhs = std::any_cast<ir::Value*>(right_any);
std::cerr << "[DEBUG] visitRelExp: left=" << (void*)lhs
DEBUG_MSG("[DEBUG] visitRelExp: left=" << (void*)lhs
<< ", type=" << (lhs->GetType()->IsFloat() ? "float" : "int")
<< ", right=" << (void*)rhs
<< ", type=" << (rhs->GetType()->IsFloat() ? "float" : "int") << std::endl;
<< ", type=" << (rhs->GetType()->IsFloat() ? "float" : "int"));
// 处理类型转换:如果操作数类型不同,需要进行类型转换
if (lhs->GetType()->IsFloat() != rhs->GetType()->IsFloat()) {
@ -1004,7 +1004,7 @@ std::any IRGenImpl::visitRelExp(SysYParser::RelExpContext* ctx) {
}
std::any IRGenImpl::visitEqExp(SysYParser::EqExpContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitEqExp: 开始处理相等表达式 " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitEqExp: 开始处理相等表达式 " << (ctx ? ctx->getText() : "<null>"));
if (!ctx) {
throw std::runtime_error(FormatError("irgen", "非法相等表达式"));
}
@ -1015,10 +1015,10 @@ std::any IRGenImpl::visitEqExp(SysYParser::EqExpContext* ctx) {
auto* lhs = std::any_cast<ir::Value*>(left_any);
auto* rhs = std::any_cast<ir::Value*>(right_any);
std::cerr << "[DEBUG] visitEqExp: left=" << (void*)lhs
DEBUG_MSG("[DEBUG] visitEqExp: left=" << (void*)lhs
<< ", type=" << (lhs->GetType()->IsFloat() ? "float" : "int")
<< ", right=" << (void*)rhs
<< ", type=" << (rhs->GetType()->IsFloat() ? "float" : "int") << std::endl;
<< ", type=" << (rhs->GetType()->IsFloat() ? "float" : "int"));
// 处理类型转换:如果操作数类型不同,需要进行类型转换
if (lhs->GetType()->IsFloat() != rhs->GetType()->IsFloat()) {
@ -1062,8 +1062,8 @@ std::any IRGenImpl::visitEqExp(SysYParser::EqExpContext* ctx) {
ir::Value* IRGenImpl::EvalAssign(SysYParser::StmtContext* ctx) {
std::cerr << "[DEBUG IRGEN] EvalAssign: 开始处理赋值语句 " << (ctx ? ctx->getText() : "<null>") << std::endl;
std::cout << "[DEBUG IRGEN] visitCond: " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] EvalAssign: 开始处理赋值语句 " << (ctx ? ctx->getText() : "<null>"));
DEBUG_MSG("[DEBUG IRGEN] visitCond: " << (ctx ? ctx->getText() : "<null>"));
if (!ctx || !ctx->lVal() || !ctx->exp()) {
throw std::runtime_error(FormatError("irgen", "非法赋值语句"));
}
@ -1127,12 +1127,12 @@ ir::Value* IRGenImpl::EvalAssign(SysYParser::StmtContext* ctx) {
} else {
// 普通标量赋值
// 调试输出指针类型
std::cerr << "[DEBUG] base_ptr type: " << base_ptr->GetType() << std::endl;
std::cerr << "[DEBUG] rhs type: " << rhs->GetType()<< std::endl;
DEBUG_MSG("[DEBUG] base_ptr type: " << base_ptr->GetType());
DEBUG_MSG("[DEBUG] rhs type: " << rhs->GetType());
// 如果 base_ptr 不是标量指针类型,可能需要特殊处理
if (!base_ptr->GetType()->IsPtrInt32() && !base_ptr->GetType()->IsPtrFloat()) {
std::cerr << "[ERROR] base_ptr is not a pointer type!" << std::endl;
DEBUG_MSG("[ERROR] base_ptr is not a pointer type!");
throw std::runtime_error("尝试存储到非指针类型");
}
rhs = convert_for_store(rhs, base_ptr);

@ -54,7 +54,7 @@ IRGenImpl::IRGenImpl(ir::Module& module,
}
void IRGenImpl::AddRuntimeFunctions() {
std::cerr << "[DEBUG IRGEN] 添加运行时库函数声明" << std::endl;
DEBUG_MSG("[DEBUG IRGEN] 添加运行时库函数声明");
// 输入函数(返回 int
module_.CreateFunction("getint",
@ -155,21 +155,21 @@ void IRGenImpl::AddRuntimeFunctions() {
ir::Type::GetVoidType(),
{ir::Type::GetPtrFloatType(), ir::Type::GetInt32Type()}));
std::cerr << "[DEBUG IRGEN] 运行时库函数声明完成" << std::endl;
DEBUG_MSG("[DEBUG IRGEN] 运行时库函数声明完成");
}
// 修正:没有 mainFuncDef通过函数名找到 main
std::any IRGenImpl::visitCompUnit(SysYParser::CompUnitContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitCompUnit" << std::endl;
std::cerr << "[DEBUG] IRGen: 符号表地址 = " << &symbol_table_ << std::endl;
std::cerr << "[DEBUG] IRGen: 开始生成 IR" << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitCompUnit");
DEBUG_MSG("[DEBUG] IRGen: 符号表地址 = " << &symbol_table_);
DEBUG_MSG("[DEBUG] IRGen: 开始生成 IR");
// 尝试查找 main 函数
const Symbol* main_sym = symbol_table_.lookup("main");
if (main_sym) {
std::cerr << "[DEBUG] IRGen: 找到 main 函数符号" << std::endl;
DEBUG_MSG("[DEBUG] IRGen: 找到 main 函数符号");
} else {
std::cerr << "[DEBUG] IRGen: 未找到 main 函数符号" << std::endl;
DEBUG_MSG("[DEBUG] IRGen: 未找到 main 函数符号");
}
if (!ctx) {
throw std::runtime_error(FormatError("irgen", "缺少编译单元"));
@ -193,7 +193,7 @@ std::any IRGenImpl::visitCompUnit(SysYParser::CompUnitContext* ctx) {
}
std::any IRGenImpl::visitFuncDef(SysYParser::FuncDefContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitFuncDef: " << (ctx && ctx->Ident() ? ctx->Ident()->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitFuncDef: " << (ctx && ctx->Ident() ? ctx->Ident()->getText() : "<null>"));
if (!ctx) {
throw std::runtime_error(FormatError("irgen", "缺少函数定义"));
}
@ -255,25 +255,25 @@ std::any IRGenImpl::visitFuncDef(SysYParser::FuncDefContext* ctx) {
auto func_type = ir::Type::GetFunctionType(ret_type, param_types);
// 调试输出
std::cerr << "[DEBUG] visitFuncDef: 创建函数 " << funcName
DEBUG_MSG("[DEBUG] visitFuncDef: 创建函数 " << funcName
<< ",返回类型: " << (ret_type->IsVoid() ? "void" : ret_type->IsFloat() ? "float" : "int")
<< ",参数数量: " << param_types.size() << std::endl;
<< ",参数数量: " << param_types.size());
// 创建函数对象
func_ = module_.CreateFunction(funcName, func_type);
// 检查函数是否成功创建
if (!func_) {
std::cerr << "[ERROR] visitFuncDef: 创建函数失败func_ 为 nullptr!" << std::endl;
DEBUG_MSG("[ERROR] visitFuncDef: 创建函数失败func_ 为 nullptr!");
throw std::runtime_error(FormatError("irgen", "创建函数失败: " + funcName));
}
std::cerr << "[DEBUG] visitFuncDef: 函数对象地址: " << (void*)func_ << std::endl;
DEBUG_MSG("[DEBUG] visitFuncDef: 函数对象地址: " << (void*)func_);
// 设置插入点
auto* entry_block = func_->GetEntry();
if (!entry_block) {
std::cerr << "[ERROR] visitFuncDef: 函数入口基本块为空!" << std::endl;
DEBUG_MSG("[ERROR] visitFuncDef: 函数入口基本块为空!");
throw std::runtime_error(FormatError("irgen", "函数入口基本块为空: " + funcName));
}
@ -324,15 +324,15 @@ std::any IRGenImpl::visitFuncDef(SysYParser::FuncDefContext* ctx) {
// 检查函数对象是否有效
if (!func_) {
std::cerr << "[ERROR] visitFuncDef: func_ 在添加参数时变为 nullptr!" << std::endl;
DEBUG_MSG("[ERROR] visitFuncDef: func_ 在添加参数时变为 nullptr!");
throw std::runtime_error(FormatError("irgen", "函数对象无效"));
}
std::cerr << "[DEBUG] visitFuncDef: 为函数 " << funcName
DEBUG_MSG("[DEBUG] visitFuncDef: 为函数 " << funcName
<< " 添加参数 " << name << ",类型: "
<< (param_ty->IsInt32() ? "int32" : param_ty->IsFloat() ? "float" :
param_ty->IsPtrInt32() ? "ptr_int32" : param_ty->IsPtrFloat() ? "ptr_float" : "other")
<< std::endl;
);
// 创建参数并添加到函数
auto arg = std::make_unique<ir::Argument>(param_ty, name);
@ -344,7 +344,7 @@ std::any IRGenImpl::visitFuncDef(SysYParser::FuncDefContext* ctx) {
auto* added_arg = func_->AddArgument(std::move(arg));
if (!added_arg) {
std::cerr << "[ERROR] visitFuncDef: AddArgument 返回 nullptr!" << std::endl;
DEBUG_MSG("[ERROR] visitFuncDef: AddArgument 返回 nullptr!");
throw std::runtime_error(FormatError("irgen", "添加参数失败: " + name));
}
@ -371,17 +371,17 @@ std::any IRGenImpl::visitFuncDef(SysYParser::FuncDefContext* ctx) {
pointer_param_names_.erase(name);
}
std::cerr << "[DEBUG] visitFuncDef: 参数 " << name << " 处理完成" << std::endl;
DEBUG_MSG("[DEBUG] visitFuncDef: 参数 " << name << " 处理完成");
}
}
// 生成函数体
std::cerr << "[DEBUG] visitFuncDef: 开始生成函数体" << std::endl;
DEBUG_MSG("[DEBUG] visitFuncDef: 开始生成函数体");
ctx->block()->accept(this);
// 如果当前插入块没有终止指令,添加默认返回
if (auto* cur = builder_.GetInsertBlock(); cur && !cur->HasTerminator()) {
std::cerr << "[DEBUG] visitFuncDef: 函数体没有终止指令,添加默认返回" << std::endl;
DEBUG_MSG("[DEBUG] visitFuncDef: 函数体没有终止指令,添加默认返回");
if (function_cleanup_block_) {
if (ret_type->IsFloat()) {
builder_.CreateStore(builder_.CreateConstFloat(0.0f), function_return_slot_);
@ -416,11 +416,11 @@ std::any IRGenImpl::visitFuncDef(SysYParser::FuncDefContext* ctx) {
try {
VerifyFunctionStructure(*func_);
} catch (const std::exception& e) {
std::cerr << "[ERROR] visitFuncDef: 验证函数结构失败: " << e.what() << std::endl;
DEBUG_MSG("[ERROR] visitFuncDef: 验证函数结构失败: " << e.what());
throw;
}
std::cerr << "[DEBUG] visitFuncDef: 函数 " << funcName << " 生成完成" << std::endl;
DEBUG_MSG("[DEBUG] visitFuncDef: 函数 " << funcName << " 生成完成");
func_ = nullptr;
current_function_name_.clear();
current_function_is_recursive_ = false;
@ -467,7 +467,7 @@ ir::AllocaInst* IRGenImpl::CreateEntryAllocaFloat(const std::string& name) {
std::any IRGenImpl::visitBlock(SysYParser::BlockContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitBlock: " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitBlock: " << (ctx ? ctx->getText() : "<null>"));
if (!ctx) {
throw std::runtime_error(FormatError("irgen", "缺少语句块"));
}
@ -482,8 +482,8 @@ std::any IRGenImpl::visitBlock(SysYParser::BlockContext* ctx) {
}
auto* cur = builder_.GetInsertBlock();
std::cerr << "[DEBUG] current insert block: "
<< (cur ? cur->GetName() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG] current insert block: "
<< (cur ? cur->GetName() : "<null>"));
if (cur && cur->HasTerminator()) {
break;
}
@ -500,7 +500,7 @@ IRGenImpl::BlockFlow IRGenImpl::VisitBlockItemResult(
}
// 用于遍历块内项,返回是否继续访问后续项(如遇到 return/break/continue 则终止访问)
std::any IRGenImpl::visitBlockItem(SysYParser::BlockItemContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitBlockItem: " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitBlockItem: " << (ctx ? ctx->getText() : "<null>"));
if (!ctx) {
throw std::runtime_error(FormatError("irgen", "缺少块内项"));
}

@ -16,7 +16,7 @@
// - 空语句、块语句嵌套分发之外的更多语句形态
std::any IRGenImpl::visitStmt(SysYParser::StmtContext* ctx) {
std::cerr << "[DEBUG IRGEN] visitStmt: " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] visitStmt: " << (ctx ? ctx->getText() : "<null>"));
if (!ctx) {
throw std::runtime_error(FormatError("irgen", "缺少语句"));
}
@ -65,7 +65,7 @@ std::any IRGenImpl::visitStmt(SysYParser::StmtContext* ctx) {
// 修改 HandleReturnStmt 函数
IRGenImpl::BlockFlow IRGenImpl::HandleReturnStmt(SysYParser::StmtContext* ctx) {
std::cerr << "[DEBUG IRGEN] HandleReturnStmt: " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] HandleReturnStmt: " << (ctx ? ctx->getText() : "<null>"));
if (!ctx) {
throw std::runtime_error(FormatError("irgen", "缺少 return 语句"));
}
@ -132,7 +132,7 @@ IRGenImpl::BlockFlow IRGenImpl::HandleReturnStmt(SysYParser::StmtContext* ctx) {
// if语句待实现
IRGenImpl::BlockFlow IRGenImpl::HandleIfStmt(SysYParser::StmtContext* ctx) {
std::cerr << "[DEBUG IRGEN] HandleIfStmt: " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] HandleIfStmt: " << (ctx ? ctx->getText() : "<null>"));
auto* cond = ctx->cond();
auto* thenStmt = ctx->stmt(0);
@ -148,11 +148,11 @@ IRGenImpl::BlockFlow IRGenImpl::HandleIfStmt(SysYParser::StmtContext* ctx) {
auto* elseBlock = (ctx->Else() && elseStmt) ? func_->CreateBlock(uniq("else")) : nullptr;
auto* mergeBlock = func_->CreateBlock(uniq("merge"));
std::cerr << "[DEBUG IF] thenBlock: " << thenBlock->GetName() << std::endl;
if (elseBlock) std::cerr << "[DEBUG IF] elseBlock: " << elseBlock->GetName() << std::endl;
std::cerr << "[DEBUG IF] mergeBlock: " << mergeBlock->GetName() << std::endl;
std::cerr << "[DEBUG IF] current insert block before cond: "
<< builder_.GetInsertBlock()->GetName() << std::endl;
DEBUG_MSG("[DEBUG IF] thenBlock: " << thenBlock->GetName());
if (elseBlock) DEBUG_MSG("[DEBUG IF] elseBlock: " << elseBlock->GetName());
DEBUG_MSG("[DEBUG IF] mergeBlock: " << mergeBlock->GetName());
DEBUG_MSG("[DEBUG IF] current insert block before cond: "
<< builder_.GetInsertBlock()->GetName());
// 生成条件
auto* condValue = EvalCond(*cond);
@ -168,74 +168,74 @@ IRGenImpl::BlockFlow IRGenImpl::HandleIfStmt(SysYParser::StmtContext* ctx) {
// 创建条件跳转
if (elseBlock) {
std::cerr << "[DEBUG IF] Creating condbr: " << condValue->GetName()
<< " -> " << thenBlock->GetName() << ", " << elseBlock->GetName() << std::endl;
DEBUG_MSG("[DEBUG IF] Creating condbr: " << condValue->GetName()
<< " -> " << thenBlock->GetName() << ", " << elseBlock->GetName());
builder_.CreateCondBr(condValue, thenBlock, elseBlock);
} else {
std::cerr << "[DEBUG IF] Creating condbr: " << condValue->GetName()
<< " -> " << thenBlock->GetName() << ", " << mergeBlock->GetName() << std::endl;
DEBUG_MSG("[DEBUG IF] Creating condbr: " << condValue->GetName()
<< " -> " << thenBlock->GetName() << ", " << mergeBlock->GetName());
builder_.CreateCondBr(condValue, thenBlock, mergeBlock);
}
// 生成 then 分支
std::cerr << "[DEBUG IF] Generating then branch in block: " << thenBlock->GetName() << std::endl;
DEBUG_MSG("[DEBUG IF] Generating then branch in block: " << thenBlock->GetName());
builder_.SetInsertPoint(thenBlock);
auto thenResult = thenStmt->accept(this);
bool thenTerminated = (std::any_cast<BlockFlow>(thenResult) == BlockFlow::Terminated);
std::cerr << "[DEBUG IF] then branch terminated: " << thenTerminated << std::endl;
DEBUG_MSG("[DEBUG IF] then branch terminated: " << thenTerminated);
if (!thenTerminated) {
std::cerr << "[DEBUG IF] Adding br to merge block from then" << std::endl;
DEBUG_MSG("[DEBUG IF] Adding br to merge block from then");
builder_.CreateBr(mergeBlock);
}
std::cerr << "[DEBUG IF] then block has terminator: " << thenBlock->HasTerminator() << std::endl;
DEBUG_MSG("[DEBUG IF] then block has terminator: " << thenBlock->HasTerminator());
// 生成 else 分支
bool elseTerminated = false;
if (elseBlock) {
std::cout << "[DEBUG IF] Generating else branch in block: " << elseBlock->GetName() << std::endl;
DEBUG_MSG("[DEBUG IF] Generating else branch in block: " << elseBlock->GetName());
builder_.SetInsertPoint(elseBlock);
auto elseResult = elseStmt->accept(this);
elseTerminated = (std::any_cast<BlockFlow>(elseResult) == BlockFlow::Terminated);
std::cout << "[DEBUG IF] else branch terminated: " << elseTerminated << std::endl;
DEBUG_MSG("[DEBUG IF] else branch terminated: " << elseTerminated);
if (!elseTerminated) {
std::cout << "[DEBUG IF] Adding br to merge block from else" << std::endl;
DEBUG_MSG("[DEBUG IF] Adding br to merge block from else");
builder_.CreateBr(mergeBlock);
}
std::cout << "[DEBUG IF] else block has terminator: " << elseBlock->HasTerminator() << std::endl;
DEBUG_MSG("[DEBUG IF] else block has terminator: " << elseBlock->HasTerminator());
}
// 决定后续插入点
std::cout << "[DEBUG IF] thenTerminated=" << thenTerminated
<< ", elseTerminated=" << elseTerminated << std::endl;
DEBUG_MSG("[DEBUG IF] thenTerminated=" << thenTerminated
<< ", elseTerminated=" << elseTerminated);
if (elseBlock) {
std::cout << "[DEBUG IF] Setting insert point to merge block: "
<< mergeBlock->GetName() << std::endl;
DEBUG_MSG("[DEBUG IF] Setting insert point to merge block: "
<< mergeBlock->GetName());
builder_.SetInsertPoint(mergeBlock);
} else {
std::cout << "[DEBUG IF] No else, setting insert point to merge block: "
<< mergeBlock->GetName() << std::endl;
DEBUG_MSG("[DEBUG IF] No else, setting insert point to merge block: "
<< mergeBlock->GetName());
builder_.SetInsertPoint(mergeBlock);
}
std::cout << "[DEBUG IF] Final insert block: "
<< builder_.GetInsertBlock()->GetName() << std::endl;
DEBUG_MSG("[DEBUG IF] Final insert block: "
<< builder_.GetInsertBlock()->GetName());
return BlockFlow::Continue;
}
// while语句待实现IRGenImpl::BlockFlow IRGenImpl::HandleWhileStmt(SysYParser::StmtContext* ctx) {
IRGenImpl::BlockFlow IRGenImpl::HandleWhileStmt(SysYParser::StmtContext* ctx) {
std::cout << "[DEBUG IRGEN] HandleWhileStmt: " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] HandleWhileStmt: " << (ctx ? ctx->getText() : "<null>"));
if (!ctx || !ctx->cond() || !ctx->stmt(0)) {
throw std::runtime_error(FormatError("irgen", "非法 while 语句"));
}
std::cout << "[DEBUG WHILE] Current insert block before while: "
<< builder_.GetInsertBlock()->GetName() << std::endl;
DEBUG_MSG("[DEBUG WHILE] Current insert block before while: "
<< builder_.GetInsertBlock()->GetName());
auto uniq = [&](const std::string& prefix) {
std::string t = module_.GetContext().NextTemp();
@ -246,18 +246,18 @@ IRGenImpl::BlockFlow IRGenImpl::HandleWhileStmt(SysYParser::StmtContext* ctx) {
auto* bodyBlock = func_->CreateBlock(uniq("while.body"));
auto* exitBlock = func_->CreateBlock(uniq("while.exit"));
std::cout << "[DEBUG WHILE] condBlock: " << condBlock->GetName() << std::endl;
std::cout << "[DEBUG WHILE] bodyBlock: " << bodyBlock->GetName() << std::endl;
std::cout << "[DEBUG WHILE] exitBlock: " << exitBlock->GetName() << std::endl;
DEBUG_MSG("[DEBUG WHILE] condBlock: " << condBlock->GetName());
DEBUG_MSG("[DEBUG WHILE] bodyBlock: " << bodyBlock->GetName());
DEBUG_MSG("[DEBUG WHILE] exitBlock: " << exitBlock->GetName());
std::cout << "[DEBUG WHILE] Adding br to condBlock from current block" << std::endl;
DEBUG_MSG("[DEBUG WHILE] Adding br to condBlock from current block");
builder_.CreateBr(condBlock);
loopStack_.push_back({condBlock, bodyBlock, exitBlock});
std::cout << "[DEBUG WHILE] loopStack size: " << loopStack_.size() << std::endl;
DEBUG_MSG("[DEBUG WHILE] loopStack size: " << loopStack_.size());
// 条件块
std::cout << "[DEBUG WHILE] Generating condition in block: " << condBlock->GetName() << std::endl;
DEBUG_MSG("[DEBUG WHILE] Generating condition in block: " << condBlock->GetName());
builder_.SetInsertPoint(condBlock);
auto* condValue = EvalCond(*ctx->cond());
if (!condValue->GetType()->IsInt1()) {
@ -270,45 +270,45 @@ IRGenImpl::BlockFlow IRGenImpl::HandleWhileStmt(SysYParser::StmtContext* ctx) {
}
}
builder_.CreateCondBr(condValue, bodyBlock, exitBlock);
std::cout << "[DEBUG WHILE] condBlock has terminator: " << condBlock->HasTerminator() << std::endl;
DEBUG_MSG("[DEBUG WHILE] condBlock has terminator: " << condBlock->HasTerminator());
// 循环体
std::cout << "[DEBUG WHILE] Generating body in block: " << bodyBlock->GetName() << std::endl;
DEBUG_MSG("[DEBUG WHILE] Generating body in block: " << bodyBlock->GetName());
builder_.SetInsertPoint(bodyBlock);
auto bodyResult = ctx->stmt(0)->accept(this);
bool bodyTerminated = (std::any_cast<BlockFlow>(bodyResult) == BlockFlow::Terminated);
std::cout << "[DEBUG WHILE] body terminated: " << bodyTerminated << std::endl;
DEBUG_MSG("[DEBUG WHILE] body terminated: " << bodyTerminated);
if (!bodyTerminated) {
std::cout << "[DEBUG WHILE] Adding br to condBlock from body" << std::endl;
DEBUG_MSG("[DEBUG WHILE] Adding br to condBlock from body");
builder_.CreateBr(condBlock);
}
std::cout << "[DEBUG WHILE] bodyBlock has terminator: " << bodyBlock->HasTerminator() << std::endl;
DEBUG_MSG("[DEBUG WHILE] bodyBlock has terminator: " << bodyBlock->HasTerminator());
loopStack_.pop_back();
std::cout << "[DEBUG WHILE] loopStack size after pop: " << loopStack_.size() << std::endl;
DEBUG_MSG("[DEBUG WHILE] loopStack size after pop: " << loopStack_.size());
// 设置插入点为 exitBlock
std::cout << "[DEBUG WHILE] Setting insert point to exitBlock: " << exitBlock->GetName() << std::endl;
DEBUG_MSG("[DEBUG WHILE] Setting insert point to exitBlock: " << exitBlock->GetName());
builder_.SetInsertPoint(exitBlock);
std::cout << "[DEBUG WHILE] exitBlock has terminator before return: "
<< exitBlock->HasTerminator() << std::endl;
DEBUG_MSG("[DEBUG WHILE] exitBlock has terminator before return: "
<< exitBlock->HasTerminator());
return BlockFlow::Continue;
}
// break语句待实现
IRGenImpl::BlockFlow IRGenImpl::HandleBreakStmt(SysYParser::StmtContext* ctx) {
std::cout << "[DEBUG IRGEN] HandleBreakStmt: " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] HandleBreakStmt: " << (ctx ? ctx->getText() : "<null>"));
if (loopStack_.empty()) {
throw std::runtime_error(FormatError("irgen", "break 语句不在循环中"));
}
std::cout << "[DEBUG BREAK] Current insert block before break: "
<< builder_.GetInsertBlock()->GetName() << std::endl;
std::cout << "[DEBUG BREAK] Breaking to exitBlock: "
<< loopStack_.back().exitBlock->GetName() << std::endl;
DEBUG_MSG("[DEBUG BREAK] Current insert block before break: "
<< builder_.GetInsertBlock()->GetName());
DEBUG_MSG("[DEBUG BREAK] Breaking to exitBlock: "
<< loopStack_.back().exitBlock->GetName());
// 跳转到循环退出块
builder_.CreateBr(loopStack_.back().exitBlock);
@ -318,16 +318,16 @@ IRGenImpl::BlockFlow IRGenImpl::HandleBreakStmt(SysYParser::StmtContext* ctx) {
}
IRGenImpl::BlockFlow IRGenImpl::HandleContinueStmt(SysYParser::StmtContext* ctx) {
std::cout << "[DEBUG IRGEN] HandleContinueStmt: " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] HandleContinueStmt: " << (ctx ? ctx->getText() : "<null>"));
if (loopStack_.empty()) {
throw std::runtime_error(FormatError("irgen", "continue 语句不在循环中"));
}
std::cout << "[DEBUG CONTINUE] Current insert block before continue: "
<< builder_.GetInsertBlock()->GetName() << std::endl;
std::cout << "[DEBUG CONTINUE] Continuing to condBlock: "
<< loopStack_.back().condBlock->GetName() << std::endl;
DEBUG_MSG("[DEBUG CONTINUE] Current insert block before continue: "
<< builder_.GetInsertBlock()->GetName());
DEBUG_MSG("[DEBUG CONTINUE] Continuing to condBlock: "
<< loopStack_.back().condBlock->GetName());
// 跳转到循环条件块
builder_.CreateBr(loopStack_.back().condBlock);
@ -340,7 +340,7 @@ IRGenImpl::BlockFlow IRGenImpl::HandleContinueStmt(SysYParser::StmtContext* ctx)
// 赋值语句
// 赋值语句
IRGenImpl::BlockFlow IRGenImpl::HandleAssignStmt(SysYParser::StmtContext* ctx) {
std::cout << "[DEBUG IRGEN] HandleAssignStmt: " << (ctx ? ctx->getText() : "<null>") << std::endl;
DEBUG_MSG("[DEBUG IRGEN] HandleAssignStmt: " << (ctx ? ctx->getText() : "<null>"));
if (!ctx || !ctx->lVal() || !ctx->exp()) {
throw std::runtime_error(FormatError("irgen", "非法赋值语句"));
@ -354,7 +354,7 @@ IRGenImpl::BlockFlow IRGenImpl::HandleAssignStmt(SysYParser::StmtContext* ctx) {
auto* lval = ctx->lVal();
std::string varName = lval->Ident()->getText();
std::cerr << "[DEBUG] HandleAssignStmt: assigning to " << varName << std::endl;
DEBUG_MSG("[DEBUG] HandleAssignStmt: assigning to " << varName);
// 1. 检查是否为常量(不能给常量赋值)
auto* const_decl = sema_.ResolveConstUse(lval);
@ -372,8 +372,8 @@ IRGenImpl::BlockFlow IRGenImpl::HandleAssignStmt(SysYParser::StmtContext* ctx) {
auto it = storage_map_.find(var_decl);
if (it != storage_map_.end()) {
base_ptr = it->second;
std::cerr << "[DEBUG] HandleAssignStmt: found in storage_map_ for " << varName
<< ", ptr = " << (void*)base_ptr << std::endl;
DEBUG_MSG("[DEBUG] HandleAssignStmt: found in storage_map_ for " << varName
<< ", ptr = " << (void*)base_ptr);
}
}
@ -382,8 +382,8 @@ IRGenImpl::BlockFlow IRGenImpl::HandleAssignStmt(SysYParser::StmtContext* ctx) {
auto it2 = param_map_.find(varName);
if (it2 != param_map_.end()) {
base_ptr = it2->second;
std::cerr << "[DEBUG] HandleAssignStmt: found in param_map_ for " << varName
<< ", ptr = " << (void*)base_ptr << std::endl;
DEBUG_MSG("[DEBUG] HandleAssignStmt: found in param_map_ for " << varName
<< ", ptr = " << (void*)base_ptr);
}
}
@ -392,8 +392,8 @@ IRGenImpl::BlockFlow IRGenImpl::HandleAssignStmt(SysYParser::StmtContext* ctx) {
auto it3 = global_map_.find(varName);
if (it3 != global_map_.end()) {
base_ptr = it3->second;
std::cerr << "[DEBUG] HandleAssignStmt: found in global_map_ for " << varName
<< ", ptr = " << (void*)base_ptr << std::endl;
DEBUG_MSG("[DEBUG] HandleAssignStmt: found in global_map_ for " << varName
<< ", ptr = " << (void*)base_ptr);
}
}
@ -402,8 +402,8 @@ IRGenImpl::BlockFlow IRGenImpl::HandleAssignStmt(SysYParser::StmtContext* ctx) {
auto it4 = local_var_map_.find(varName);
if (it4 != local_var_map_.end()) {
base_ptr = it4->second;
std::cerr << "[DEBUG] HandleAssignStmt: found in local_var_map_ for " << varName
<< ", ptr = " << (void*)base_ptr << std::endl;
DEBUG_MSG("[DEBUG] HandleAssignStmt: found in local_var_map_ for " << varName
<< ", ptr = " << (void*)base_ptr);
}
}
@ -497,21 +497,21 @@ IRGenImpl::BlockFlow IRGenImpl::HandleAssignStmt(SysYParser::StmtContext* ctx) {
builder_.CreateStore(rhs, elem_ptr);
} else {
// 普通标量赋值
std::cerr << "[DEBUG] HandleAssignStmt: scalar assignment to " << varName
DEBUG_MSG("[DEBUG] HandleAssignStmt: scalar assignment to " << varName
<< ", ptr = " << (void*)base_ptr
<< ", rhs = " << (void*)rhs << std::endl;
<< ", rhs = " << (void*)rhs);
// 在 HandleAssignStmt 中,存储前添加类型调试
if (base_ptr && base_ptr->GetType()) {
std::cerr << "[DEBUG] Is int32: " << base_ptr->GetType()->IsInt32() << std::endl;
std::cerr << "[DEBUG] Is float: " << base_ptr->GetType()->IsFloat() << std::endl;
std::cerr << "[DEBUG] Is ptr int32: " << base_ptr->GetType()->IsPtrInt32() << std::endl;
std::cerr << "[DEBUG] Is ptr float: " << base_ptr->GetType()->IsPtrFloat() << std::endl;
std::cerr << "[DEBUG] Is array: " << base_ptr->GetType()->IsArray() << std::endl;
DEBUG_MSG("[DEBUG] Is int32: " << base_ptr->GetType()->IsInt32());
DEBUG_MSG("[DEBUG] Is float: " << base_ptr->GetType()->IsFloat());
DEBUG_MSG("[DEBUG] Is ptr int32: " << base_ptr->GetType()->IsPtrInt32());
DEBUG_MSG("[DEBUG] Is ptr float: " << base_ptr->GetType()->IsPtrFloat());
DEBUG_MSG("[DEBUG] Is array: " << base_ptr->GetType()->IsArray());
}
if (rhs && rhs->GetType()) {
std::cerr << "[DEBUG] Value is int32: " << rhs->GetType()->IsInt32() << std::endl;
DEBUG_MSG("[DEBUG] Value is int32: " << rhs->GetType()->IsInt32());
}
if (base_ptr->GetType()->IsPtrFloat() && rhs->GetType()->IsInt32()) {
rhs = builder_.CreateSIToFP(rhs, ir::Type::GetFloatType(),

@ -4,11 +4,21 @@
#include <stdexcept>
#include <string>
#include <sstream>
#include <iostream>
#include "SysYBaseVisitor.h"
#include "sem/SymbolTable.h"
#include "utils/Log.h"
//#define DEBUG_SEMA
#ifdef DEBUG_SEMA
#include <iostream>
#define DEBUG_MSG(msg) std::cerr << "[Sema Debug] " << msg << std::endl
#else
#define DEBUG_MSG(msg)
#endif
namespace {
// 获取左值名称的辅助函数
@ -67,10 +77,9 @@ public:
} else {
return_type = ir::Type::GetInt32Type();
}
std::cout << "[DEBUG] 进入函数: " << name
DEBUG_MSG("[DEBUG] 进入函数: " << name
<< " 返回类型: " << (return_type->IsInt32() ? "int" :
return_type->IsFloat() ? "float" : "void")
<< std::endl;
return_type->IsFloat() ? "float" : "void"));
// 记录当前函数返回类型(用于 return 检查)
current_func_return_type_ = return_type;
@ -83,10 +92,9 @@ public:
if (ctx->block()) { // 处理函数体
ctx->block()->accept(this);
}
std::cout << "[DEBUG] 函数 " << name
DEBUG_MSG("[DEBUG] 函数 " << name
<< " has_return: " << current_func_has_return_
<< " return_type_is_void: " << return_type->IsVoid()
<< std::endl;
<< " return_type_is_void: " << return_type->IsVoid());
if (!return_type->IsVoid() && !current_func_has_return_) { // 检查非 void 函数是否有 return
throw std::runtime_error(FormatError("sema", "非 void 函数 " + name + " 缺少 return 语句"));
}
@ -170,10 +178,10 @@ public:
std::vector<int> dims;
bool is_array = !ctx->constExp().empty();
// 调试输出
std::cout << "[DEBUG] CheckVarDef: " << name
DEBUG_MSG("[DEBUG] CheckVarDef: " << name
<< " base_type: " << (base_type->IsInt32() ? "int" : base_type->IsFloat() ? "float" : "unknown")
<< " is_array: " << is_array
<< " dim_count: " << ctx->constExp().size() << std::endl;
<< " dim_count: " << ctx->constExp().size());
if (is_array) {
// 处理数组维度
for (auto* dim_exp : ctx->constExp()) {
@ -185,26 +193,24 @@ public:
throw std::runtime_error(FormatError("sema", "数组维度必须为正整数"));
}
dims.push_back(dim);
std::cout << "[DEBUG] dim[" << dims.size() - 1 << "] = " << dim << std::endl;
DEBUG_MSG("[DEBUG] dim[" << dims.size() - 1 << "] = " << dim);
}
// 创建数组类型
type = ir::Type::GetArrayType(base_type, dims);
std::cout << "[DEBUG] 创建数组类型完成" << std::endl;
std::cout << "[DEBUG] type->IsArray(): " << type->IsArray() << std::endl;
std::cout << "[DEBUG] type->GetKind(): " << (int)type->GetKind() << std::endl;
DEBUG_MSG("[DEBUG] 创建数组类型完成");
DEBUG_MSG("[DEBUG] type->IsArray(): " << type->IsArray());
DEBUG_MSG("[DEBUG] type->GetKind(): " << (int)type->GetKind());
// 验证数组类型
if (type->IsArray()) {
auto* arr_type = dynamic_cast<ir::ArrayType*>(type.get());
if (arr_type) {
std::cout << "[DEBUG] ArrayType dimensions: ";
DEBUG_MSG("[DEBUG] ArrayType dimensions: ");
for (int d : arr_type->GetDimensions()) {
std::cout << d << " ";
DEBUG_MSG(d << " ");
}
std::cout << std::endl;
std::cout << "[DEBUG] Element type: "
DEBUG_MSG("[DEBUG] Element type: "
<< (arr_type->GetElementType()->IsInt32() ? "int" :
arr_type->GetElementType()->IsFloat() ? "float" : "unknown")
<< std::endl;
arr_type->GetElementType()->IsFloat() ? "float" : "unknown"));
}
}
}
@ -230,10 +236,10 @@ public:
sym.param_types.clear(); // 确保不混淆
}
table_.addSymbol(sym); // 添加到符号表
std::cout << "[DEBUG] 符号添加完成: " << name
DEBUG_MSG("[DEBUG] 符号添加完成: " << name
<< " type_kind: " << (int)sym.type->GetKind()
<< " is_array: " << sym.type->IsArray()
<< std::endl;
);
}
void CheckConstDef(SysYParser::ConstDefContext* ctx,
@ -250,10 +256,10 @@ public:
std::shared_ptr<ir::Type> type = base_type;
std::vector<int> dims;
bool is_array = !ctx->constExp().empty();
std::cout << "[DEBUG] CheckConstDef: " << name
DEBUG_MSG("[DEBUG] CheckConstDef: " << name
<< " base_type: " << (base_type->IsInt32() ? "int" : base_type->IsFloat() ? "float" : "unknown")
<< " is_array: " << is_array
<< " dim_count: " << ctx->constExp().size() << std::endl;
<< " dim_count: " << ctx->constExp().size());
if (is_array) {
for (auto* dim_exp : ctx->constExp()) {
@ -262,10 +268,10 @@ public:
throw std::runtime_error(FormatError("sema", "数组维度必须为正整数"));
}
dims.push_back(dim);
std::cout << "[DEBUG] dim[" << dims.size() - 1 << "] = " << dim << std::endl;
DEBUG_MSG("[DEBUG] dim[" << dims.size() - 1 << "] = " << dim);
}
type = ir::Type::GetArrayType(base_type, dims);
std::cout << "[DEBUG] 创建数组类型完成IsArray: " << type->IsArray() << std::endl;
DEBUG_MSG("[DEBUG] 创建数组类型完成IsArray: " << type->IsArray());
}
// ========== 绑定维度表达式 ==========
@ -280,7 +286,7 @@ public:
BindConstInitVal(ctx->constInitVal());
init_values = table_.EvaluateConstInitVal(ctx->constInitVal(), dims, base_type);
std::cout << "[DEBUG] 初始化值数量: " << init_values.size() << std::endl;
DEBUG_MSG("[DEBUG] 初始化值数量: " << init_values.size());
}
// 计算期望的元素数量
@ -288,12 +294,12 @@ public:
if (is_array) {
expected_count = 1;
for (int d : dims) expected_count *= d;
std::cout << "[DEBUG] 期望元素数量: " << expected_count << std::endl;
DEBUG_MSG("[DEBUG] 期望元素数量: " << expected_count);
}
// 如果初始化值不足,补零
if (is_array && init_values.size() < expected_count) {
std::cout << "[DEBUG] 初始化值不足,补零" << std::endl;
DEBUG_MSG("[DEBUG] 初始化值不足,补零");
SymbolTable::ConstValue zero;
if (base_type->IsInt32()) {
zero.kind = SymbolTable::ConstValue::INT;
@ -314,13 +320,13 @@ public:
Symbol sym;
sym.name = name;
sym.kind = SymbolKind::Constant;
std::cout << "CheckConstDef: before addSymbol, sym.kind = " << (int)sym.kind << std::endl;
DEBUG_MSG("CheckConstDef: before addSymbol, sym.kind = " << (int)sym.kind);
sym.type = type;
sym.scope_level = table_.currentScopeLevel();
sym.is_initialized = true;
sym.var_def_ctx = nullptr;
sym.const_def_ctx = ctx;
std::cout << "保存常量定义上下文: " << name << ", ctx: " << ctx << std::endl;
DEBUG_MSG("保存常量定义上下文: " << name << ", ctx: " << ctx);
// ========== 存储常量值 ==========
if (is_array) {
@ -338,19 +344,19 @@ public:
sym.array_const_values.push_back(cv);
}
std::cout << "[DEBUG] 存储数组常量,共 " << sym.array_const_values.size()
<< " 个元素" << std::endl;
DEBUG_MSG("[DEBUG] 存储数组常量,共 " << sym.array_const_values.size()
<< " 个元素");
} else if (!init_values.empty()) {
// 存储标量常量
if (base_type->IsInt32() && init_values[0].kind == SymbolTable::ConstValue::INT) {
sym.is_int_const = true;
sym.const_value.i32 = init_values[0].int_val;
std::cout << "[DEBUG] 存储整型常量: " << init_values[0].int_val << std::endl;
DEBUG_MSG("[DEBUG] 存储整型常量: " << init_values[0].int_val);
} else if (base_type->IsFloat() && init_values[0].kind == SymbolTable::ConstValue::FLOAT) {
sym.is_int_const = false;
sym.const_value.f32 = init_values[0].float_val;
std::cout << "[DEBUG] 存储浮点常量: " << init_values[0].float_val << std::endl;
DEBUG_MSG("[DEBUG] 存储浮点常量: " << init_values[0].float_val);
} else if (base_type->IsInt32() && init_values[0].kind == SymbolTable::ConstValue::FLOAT) {
// 整型常量用浮点数初始化(需要检查是否为整数)
float f = init_values[0].float_val;
@ -361,30 +367,30 @@ public:
}
sym.is_int_const = true;
sym.const_value.i32 = i;
std::cout << "[DEBUG] 浮点转整型常量: " << f << " -> " << i << std::endl;
DEBUG_MSG("[DEBUG] 浮点转整型常量: " << f << " -> " << i);
} else if (base_type->IsFloat() && init_values[0].kind == SymbolTable::ConstValue::INT) {
// 浮点常量用整型初始化,隐式转换
sym.is_int_const = false;
sym.const_value.f32 = static_cast<float>(init_values[0].int_val);
std::cout << "[DEBUG] 整型转浮点常量: " << init_values[0].int_val
<< " -> " << static_cast<float>(init_values[0].int_val) << std::endl;
DEBUG_MSG("[DEBUG] 整型转浮点常量: " << init_values[0].int_val
<< " -> " << static_cast<float>(init_values[0].int_val));
}
} else {
// 没有初始化值,对于标量常量这是错误的
if (!is_array) {
throw std::runtime_error(FormatError("sema", "常量必须有初始化值: " + name));
}
std::cout << "[DEBUG] 数组常量无初始化器,将全部补零" << std::endl;
DEBUG_MSG("[DEBUG] 数组常量无初始化器,将全部补零");
}
table_.addSymbol(sym);
std::cout << "CheckConstDef: after addSymbol, sym.kind = " << (int)sym.kind << std::endl;
DEBUG_MSG("CheckConstDef: after addSymbol, sym.kind = " << (int)sym.kind);
auto* stored = table_.lookup(name);
std::cout << "CheckConstDef: after addSymbol, stored const_def_ctx = " << stored->const_def_ctx << std::endl;
DEBUG_MSG("CheckConstDef: after addSymbol, stored const_def_ctx = " << stored->const_def_ctx);
std::cout << "[DEBUG] 常量符号添加完成: " << name
DEBUG_MSG("[DEBUG] 常量符号添加完成: " << name
<< " is_array_const: " << sym.is_array_const
<< " element_count: " << sym.array_const_values.size() << std::endl;
<< " element_count: " << sym.array_const_values.size());
}
// ==================== 常量声明 ====================
@ -407,20 +413,19 @@ public:
std::any visitStmt(SysYParser::StmtContext* ctx) override {
if (!ctx) return {};
// 调试输出
std::cout << "[DEBUG] visitStmt: ";
if (ctx->Return()) std::cout << "Return ";
if (ctx->If()) std::cout << "If ";
if (ctx->While()) std::cout << "While ";
if (ctx->Break()) std::cout << "Break ";
if (ctx->Continue()) std::cout << "Continue ";
if (ctx->lVal() && ctx->Assign()) std::cout << "Assign ";
if (ctx->exp() && ctx->Semi()) std::cout << "ExpStmt ";
if (ctx->block()) std::cout << "Block ";
std::cout << std::endl;
DEBUG_MSG("[DEBUG] visitStmt: ");
if (ctx->Return()) DEBUG_MSG("Return ");
if (ctx->If()) DEBUG_MSG("If ");
if (ctx->While()) DEBUG_MSG("While ");
if (ctx->Break()) DEBUG_MSG("Break ");
if (ctx->Continue()) DEBUG_MSG("Continue ");
if (ctx->lVal() && ctx->Assign()) DEBUG_MSG("Assign ");
if (ctx->exp() && ctx->Semi()) DEBUG_MSG("ExpStmt ");
if (ctx->block()) DEBUG_MSG("Block ");
// 判断语句类型 - 注意Return() 返回的是 TerminalNode*
if (ctx->Return() != nullptr) {
// return 语句
std::cout << "[DEBUG] 检测到 return 语句" << std::endl;
DEBUG_MSG("[DEBUG] 检测到 return 语句");
return visitReturnStmtInternal(ctx);
} else if (ctx->lVal() != nullptr && ctx->Assign() != nullptr) {
// 赋值语句
@ -449,14 +454,14 @@ public:
// return 语句内部实现
std::any visitReturnStmtInternal(SysYParser::StmtContext* ctx) {
std::cout << "[DEBUG] visitReturnStmtInternal 被调用" << std::endl;
DEBUG_MSG("[DEBUG] visitReturnStmtInternal 被调用");
std::shared_ptr<ir::Type> expected = current_func_return_type_;
if (!expected) {
throw std::runtime_error(FormatError("sema", "return 语句不在函数体内"));
}
if (ctx->exp() != nullptr) {
// 有返回值的 return
std::cout << "[DEBUG] 有返回值的 return" << std::endl;
DEBUG_MSG("[DEBUG] 有返回值的 return");
ExprInfo ret_val = CheckExp(ctx->exp());
if (expected->IsVoid()) {
throw std::runtime_error(FormatError("sema", "void 函数不能返回值"));
@ -469,23 +474,23 @@ public:
}
// 设置 has_return 标志
current_func_has_return_ = true;
std::cout << "[DEBUG] 设置 current_func_has_return_ = true" << std::endl;
DEBUG_MSG("[DEBUG] 设置 current_func_has_return_ = true");
} else {
// 无返回值的 return
std::cout << "[DEBUG] 无返回值的 return" << std::endl;
DEBUG_MSG("[DEBUG] 无返回值的 return");
if (!expected->IsVoid()) {
throw std::runtime_error(FormatError("sema", "非 void 函数必须返回值"));
}
// 设置 has_return 标志
current_func_has_return_ = true;
std::cout << "[DEBUG] 设置 current_func_has_return_ = true" << std::endl;
DEBUG_MSG("[DEBUG] 设置 current_func_has_return_ = true");
}
return {};
}
// 左值表达式(变量引用)
std::any visitLVal(SysYParser::LValContext* ctx) override {
std::cout << "[DEBUG] visitLVal: " << ctx->getText() << std::endl;
DEBUG_MSG("[DEBUG] visitLVal: " << ctx->getText());
if (!ctx || !ctx->Ident()) {
throw std::runtime_error(FormatError("sema", "非法变量引用"));
}
@ -496,17 +501,17 @@ public:
}
// 检查数组访问
bool is_array_access = !ctx->exp().empty();
std::cout << "[DEBUG] name: " << name
DEBUG_MSG("[DEBUG] name: " << name
<< ", is_array_access: " << is_array_access
<< ", subscript_count: " << ctx->exp().size() << std::endl;
<< ", subscript_count: " << ctx->exp().size());
ExprInfo result;
// 判断是否为数组类型或指针类型(数组参数)
bool is_array_or_ptr = false;
if (sym->type) {
is_array_or_ptr = sym->type->IsArray() || sym->type->IsPtrInt32() || sym->type->IsPtrFloat();
std::cout << "[DEBUG] type_kind: " << (int)sym->type->GetKind()
DEBUG_MSG("[DEBUG] type_kind: " << (int)sym->type->GetKind()
<< ", is_array: " << sym->type->IsArray()
<< ", is_ptr: " << (sym->type->IsPtrInt32() || sym->type->IsPtrFloat()) << std::endl;
<< ", is_ptr: " << (sym->type->IsPtrInt32() || sym->type->IsPtrFloat()));
}
if (is_array_or_ptr) {
@ -517,7 +522,7 @@ public:
if (auto* arr_type = dynamic_cast<ir::ArrayType*>(sym->type.get())) {
dim_count = arr_type->GetDimensions().size();
elem_type = arr_type->GetElementType();
std::cout << "[DEBUG] 数组维度: " << dim_count << std::endl;
DEBUG_MSG("[DEBUG] 数组维度: " << dim_count);
}
} else if (sym->type->IsPtrInt32() || sym->type->IsPtrFloat()) {
dim_count = 1;
@ -526,12 +531,12 @@ public:
} else if (sym->type->IsPtrFloat()) {
elem_type = ir::Type::GetFloatType();
}
std::cout << "[DEBUG] 指针类型, dim_count: 1" << std::endl;
DEBUG_MSG("[DEBUG] 指针类型, dim_count: 1");
}
if (is_array_access) {
std::cout << "[DEBUG] 有下标访问,期望维度: " << dim_count
<< ", 实际下标数: " << ctx->exp().size() << std::endl;
DEBUG_MSG("[DEBUG] 有下标访问,期望维度: " << dim_count
<< ", 实际下标数: " << ctx->exp().size());
if (ctx->exp().size() != dim_count) {
throw std::runtime_error(FormatError("sema", "数组下标个数不匹配"));
}
@ -545,9 +550,9 @@ public:
result.is_lvalue = true;
result.is_const = false;
} else {
std::cout << "[DEBUG] 无下标访问" << std::endl;
DEBUG_MSG("[DEBUG] 无下标访问");
if (sym->type->IsArray()) {
std::cout << "[DEBUG] 数组名作为地址,转换为指针" << std::endl;
DEBUG_MSG("[DEBUG] 数组名作为地址,转换为指针");
if (auto* arr_type = dynamic_cast<ir::ArrayType*>(sym->type.get())) {
if (arr_type->GetElementType()->IsInt32()) {
result.type = ir::Type::GetPtrInt32Type();
@ -669,7 +674,7 @@ public:
// 主表达式
std::any visitPrimaryExp(SysYParser::PrimaryExpContext* ctx) override {
std::cout << "[DEBUG] visitPrimaryExp: " << ctx->getText() << std::endl;
DEBUG_MSG("[DEBUG] visitPrimaryExp: " << ctx->getText());
ExprInfo result;
if (ctx->lVal()) { // 左值表达式
result = CheckLValue(ctx->lVal());
@ -701,14 +706,14 @@ public:
// 一元表达式
std::any visitUnaryExp(SysYParser::UnaryExpContext* ctx) override {
std::cout << "[DEBUG] visitUnaryExp: " << ctx->getText() << std::endl;
DEBUG_MSG("[DEBUG] visitUnaryExp: " << ctx->getText());
ExprInfo result;
if (ctx->primaryExp()) {
ctx->primaryExp()->accept(this);
auto* info = sema_.GetExprType(ctx->primaryExp());
if (info) result = *info;
} else if (ctx->Ident() && ctx->L_PAREN()) { // 函数调用
std::cout << "[DEBUG] 函数调用: " << ctx->Ident()->getText() << std::endl;
DEBUG_MSG("[DEBUG] 函数调用: " << ctx->Ident()->getText());
result = CheckFuncCall(ctx);
} else if (ctx->unaryOp()) { // 一元运算
ctx->unaryExp()->accept(this);
@ -1074,8 +1079,8 @@ public:
// 新增:同时返回两者
SemaResult TakeResult() {
std::cerr << "[DEBUG] TakeResult 前: 符号表作用域数量 = "
<< table_.getScopeCount() << std::endl;
DEBUG_MSG("[DEBUG] TakeResult 前: 符号表作用域数量 = "
<< table_.getScopeCount());
// 可选:打印符号表内容
// table_.dump();
@ -1084,8 +1089,8 @@ public:
result.context = std::move(sema_);
result.symbol_table = std::move(table_);
std::cerr << "[DEBUG] TakeResult 后: 符号表作用域数量 = "
<< result.symbol_table.getScopeCount() << std::endl;
DEBUG_MSG("[DEBUG] TakeResult 后: 符号表作用域数量 = "
<< result.symbol_table.getScopeCount());
return result;
}
@ -1106,7 +1111,7 @@ private:
if (!ctx || !ctx->addExp()) {
throw std::runtime_error(FormatError("sema", "无效表达式"));
}
std::cout << "[DEBUG] CheckExp: " << ctx->getText() << std::endl;
DEBUG_MSG("[DEBUG] CheckExp: " << ctx->getText());
ctx->addExp()->accept(this);
auto* info = sema_.GetExprType(ctx->addExp());
if (!info) {
@ -1157,21 +1162,21 @@ private:
if (!sym) {
throw std::runtime_error(FormatError("sema", "未定义的变量: " + name));
}
std::cout << "CheckLValue: found sym->name = " << sym->name
<< ", sym->kind = " << (int)sym->kind << std::endl;
DEBUG_MSG("CheckLValue: found sym->name = " << sym->name
<< ", sym->kind = " << (int)sym->kind);
if (sym->kind == SymbolKind::Variable && sym->var_def_ctx) {
sema_.BindVarUse(ctx, sym->var_def_ctx);
std::cout << "绑定变量: " << name << " -> VarDefContext" << std::endl;
DEBUG_MSG("绑定变量: " << name << " -> VarDefContext");
}
else if (sym->kind == SymbolKind::Constant && sym->const_def_ctx) {
sema_.BindConstUse(ctx, sym->const_def_ctx);
std::cout << "绑定常量: " << name << " -> ConstDefContext" << std::endl;
DEBUG_MSG("绑定常量: " << name << " -> ConstDefContext");
}
std::cout << "CheckLValue 绑定变量: " << name
DEBUG_MSG("CheckLValue 绑定变量: " << name
<< ", sym->kind: " << (int)sym->kind
<< ", sym->var_def_ctx: " << sym->var_def_ctx
<< ", sym->const_def_ctx: " << sym->const_def_ctx << std::endl;
<< ", sym->const_def_ctx: " << sym->const_def_ctx);
bool is_array_access = !ctx->exp().empty();
bool is_const = (sym->kind == SymbolKind::Constant);
@ -1203,9 +1208,8 @@ private:
} else if (sym->type->IsPtrFloat()) {
elem_type = ir::Type::GetFloatType();
}
std::cout << "数组参数维度: " << dim_count << " 维, dims: ";
for (int d : dims) std::cout << d << " ";
std::cout << std::endl;
DEBUG_MSG("数组参数维度: " << dim_count << " 维, dims: ");
for (int d : dims) DEBUG_MSG(d << " ");
} else if (sym->type && (sym->type->IsPtrInt32() || sym->type->IsPtrFloat())) {
// 普通指针,只能有一个下标
dim_count = 1;
@ -1218,7 +1222,7 @@ private:
size_t subscript_count = ctx->exp().size();
std::cout << "dim_count: " << dim_count << ", subscript_count: " << subscript_count << std::endl;
DEBUG_MSG("dim_count: " << dim_count << ", subscript_count: " << subscript_count);
if (dim_count > 0 || sym->is_array_param || sym->type->IsArray() ||
sym->type->IsPtrInt32() || sym->type->IsPtrFloat()) {
@ -1239,11 +1243,11 @@ private:
if (subscript_count == dim_count) {
// 完全索引,返回元素类型
std::cout << "完全索引,返回元素类型" << std::endl;
DEBUG_MSG("完全索引,返回元素类型");
return {elem_type, true, false};
} else {
// 部分索引,返回子数组的指针类型
std::cout << "部分索引,返回指针类型" << std::endl;
DEBUG_MSG("部分索引,返回指针类型");
// 计算剩余维度的指针类型
if (elem_type->IsInt32()) {
return {ir::Type::GetPtrInt32Type(), false, false};
@ -1257,7 +1261,7 @@ private:
// 没有下标访问
if (sym->type && sym->type->IsArray()) {
// 数组名作为地址
std::cout << "数组名作为地址" << std::endl;
DEBUG_MSG("数组名作为地址");
if (auto* arr_type = dynamic_cast<ir::ArrayType*>(sym->type.get())) {
if (arr_type->GetElementType()->IsInt32()) {
return {ir::Type::GetPtrInt32Type(), false, true};
@ -1268,7 +1272,7 @@ private:
return {ir::Type::GetPtrInt32Type(), false, true};
} else if (sym->is_array_param) {
// 数组参数名作为地址
std::cout << "数组参数名作为地址" << std::endl;
DEBUG_MSG("数组参数名作为地址");
if (sym->type->IsPtrInt32()) {
return {ir::Type::GetPtrInt32Type(), false, true};
} else {
@ -1292,14 +1296,14 @@ private:
throw std::runtime_error(FormatError("sema", "非法函数调用"));
}
std::string func_name = ctx->Ident()->getText();
std::cout << "[DEBUG] CheckFuncCall: " << func_name << std::endl;
DEBUG_MSG("[DEBUG] CheckFuncCall: " << func_name);
auto* func_sym = table_.lookup(func_name);
if (!func_sym || func_sym->kind != SymbolKind::Function) {
throw std::runtime_error(FormatError("sema", "未定义的函数: " + func_name));
}
std::vector<ExprInfo> args;
if (ctx->funcRParams()) {
std::cout << "[DEBUG] 处理函数调用参数:" << std::endl;
DEBUG_MSG("[DEBUG] 处理函数调用参数:");
for (auto* exp : ctx->funcRParams()->exp()) {
if (exp) {
args.push_back(CheckExp(exp));
@ -1310,8 +1314,8 @@ private:
throw std::runtime_error(FormatError("sema", "参数个数不匹配"));
}
for (size_t i = 0; i < std::min(args.size(), func_sym->param_types.size()); ++i) {
std::cout << "[DEBUG] 检查参数 " << i << ": 实参类型 " << (int)args[i].type->GetKind()
<< " 形参类型 " << (int)func_sym->param_types[i]->GetKind() << std::endl;
DEBUG_MSG("[DEBUG] 检查参数 " << i << ": 实参类型 " << (int)args[i].type->GetKind()
<< " 形参类型 " << (int)func_sym->param_types[i]->GetKind());
if (!IsTypeCompatible(args[i].type, func_sym->param_types[i])) {
throw std::runtime_error(FormatError("sema", "参数类型不匹配"));
}
@ -1511,10 +1515,8 @@ private:
sym.array_dims = dims;
table_.addSymbol(sym);
std::cout << "[DEBUG] 添加参数: " << name << " type_kind: " << (int)param_type->GetKind()
<< " is_array: " << is_array << " dims: ";
for (int d : dims) std::cout << d << " ";
std::cout << std::endl;
DEBUG_MSG("[DEBUG] 添加参数: " << name << " type_kind: " << (int)param_type->GetKind());
for (int d : dims) DEBUG_MSG(d << " ");
}
}

@ -6,7 +6,7 @@
#include <cmath>
#include <functional>
#define DEBUG_SYMBOL_TABLE
//#define DEBUG_SYMBOL_TABLE
#ifdef DEBUG_SYMBOL_TABLE
#include <iostream>
@ -48,10 +48,9 @@ bool SymbolTable::addSymbol(const Symbol& sym) {
// 立即验证存储的符号
const auto& stored = current_scope[sym.name];
std::cout << "SymbolTable::addSymbol: stored " << sym.name
DEBUG_MSG("SymbolTable::addSymbol: stored " << sym.name
<< " with kind=" << (int)stored.kind
<< ", const_def_ctx=" << stored.const_def_ctx
<< std::endl;
<< ", const_def_ctx=" << stored.const_def_ctx);
return true;
}
@ -69,11 +68,10 @@ const Symbol* SymbolTable::lookup(const std::string& name) const {
const auto& scope = scopes_[*it];
auto found = scope.find(name);
if (found != scope.end()) {
std::cout << "SymbolTable::lookup: found " << name
DEBUG_MSG("SymbolTable::lookup: found " << name
<< " in active scope index " << *it
<< ", kind=" << (int)found->second.kind
<< ", const_def_ctx=" << found->second.const_def_ctx
<< std::endl;
<< ", const_def_ctx=" << found->second.const_def_ctx);
return &found->second;
}
}

Loading…
Cancel
Save