|
|
|
|
@ -398,7 +398,122 @@ public:
|
|
|
|
|
|
|
|
|
|
// 左值表达式(变量引用)
|
|
|
|
|
std::any visitLVal(SysYParser::LValContext* ctx) override {
|
|
|
|
|
ExprInfo result = CheckLValue(ctx);
|
|
|
|
|
std::cout << "[DEBUG] visitLVal: " << ctx->getText() << std::endl;
|
|
|
|
|
if (!ctx || !ctx->Ident()) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "非法变量引用"));
|
|
|
|
|
}
|
|
|
|
|
std::string name = ctx->Ident()->getText();
|
|
|
|
|
auto* sym = table_.lookup(name);
|
|
|
|
|
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
|
|
|
|
|
<< ", is_array_access: " << is_array_access
|
|
|
|
|
<< ", subscript_count: " << ctx->exp().size() << std::endl;
|
|
|
|
|
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()
|
|
|
|
|
<< ", is_array: " << sym->type->IsArray()
|
|
|
|
|
<< ", is_ptr: " << (sym->type->IsPtrInt32() || sym->type->IsPtrFloat()) << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_array_or_ptr) {
|
|
|
|
|
// 获取维度信息
|
|
|
|
|
size_t dim_count = 0;
|
|
|
|
|
std::shared_ptr<ir::Type> elem_type = sym->type;
|
|
|
|
|
if (sym->type->IsArray()) {
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
} else if (sym->type->IsPtrInt32() || sym->type->IsPtrFloat()) {
|
|
|
|
|
dim_count = 1;
|
|
|
|
|
if (sym->type->IsPtrInt32()) {
|
|
|
|
|
elem_type = ir::Type::GetInt32Type();
|
|
|
|
|
} else if (sym->type->IsPtrFloat()) {
|
|
|
|
|
elem_type = ir::Type::GetFloatType();
|
|
|
|
|
}
|
|
|
|
|
std::cout << "[DEBUG] 指针类型, dim_count: 1" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_array_access) {
|
|
|
|
|
std::cout << "[DEBUG] 有下标访问,期望维度: " << dim_count
|
|
|
|
|
<< ", 实际下标数: " << ctx->exp().size() << std::endl;
|
|
|
|
|
if (ctx->exp().size() != dim_count) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "数组下标个数不匹配"));
|
|
|
|
|
}
|
|
|
|
|
for (auto* idx_exp : ctx->exp()) {
|
|
|
|
|
ExprInfo idx = CheckExp(idx_exp);
|
|
|
|
|
if (!idx.type->IsInt32()) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "数组下标必须是 int 类型"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
result.type = elem_type;
|
|
|
|
|
result.is_lvalue = true;
|
|
|
|
|
result.is_const = false;
|
|
|
|
|
} else {
|
|
|
|
|
std::cout << "[DEBUG] 无下标访问" << std::endl;
|
|
|
|
|
if (sym->type->IsArray()) {
|
|
|
|
|
std::cout << "[DEBUG] 数组名作为地址,转换为指针" << std::endl;
|
|
|
|
|
if (auto* arr_type = dynamic_cast<ir::ArrayType*>(sym->type.get())) {
|
|
|
|
|
if (arr_type->GetElementType()->IsInt32()) {
|
|
|
|
|
result.type = ir::Type::GetPtrInt32Type();
|
|
|
|
|
} else if (arr_type->GetElementType()->IsFloat()) {
|
|
|
|
|
result.type = ir::Type::GetPtrFloatType();
|
|
|
|
|
} else {
|
|
|
|
|
result.type = ir::Type::GetPtrInt32Type();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
result.type = ir::Type::GetPtrInt32Type();
|
|
|
|
|
}
|
|
|
|
|
result.is_lvalue = false;
|
|
|
|
|
result.is_const = true;
|
|
|
|
|
} else {
|
|
|
|
|
result.type = sym->type;
|
|
|
|
|
result.is_lvalue = true;
|
|
|
|
|
result.is_const = (sym->kind == SymbolKind::Constant);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (is_array_access) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "非数组变量不能使用下标: " + name));
|
|
|
|
|
}
|
|
|
|
|
result.type = sym->type;
|
|
|
|
|
result.is_lvalue = true;
|
|
|
|
|
result.is_const = (sym->kind == SymbolKind::Constant);
|
|
|
|
|
if (result.is_const && sym->type && !sym->type->IsArray()) {
|
|
|
|
|
if (sym->is_int_const) {
|
|
|
|
|
result.is_const_int = true;
|
|
|
|
|
result.const_int_value = sym->const_value.i32;
|
|
|
|
|
} else {
|
|
|
|
|
result.const_float_value = sym->const_value.f32;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
sema_.SetExprType(ctx, result);
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
@ -956,33 +1071,30 @@ private:
|
|
|
|
|
if (!sym) {
|
|
|
|
|
throw std::runtime_error(FormatError("sema", "未定义的变量: " + name));
|
|
|
|
|
}
|
|
|
|
|
// ========== 添加绑定 ==========
|
|
|
|
|
if (sym->var_def_ctx) {
|
|
|
|
|
std::cout << "[DEBUG] CheckLValue 绑定变量: " << name << std::endl;
|
|
|
|
|
sema_.BindVarUse(ctx, sym->var_def_ctx);
|
|
|
|
|
}
|
|
|
|
|
// ============================
|
|
|
|
|
|
|
|
|
|
bool is_array_access = !ctx->exp().empty();
|
|
|
|
|
bool is_const = (sym->kind == SymbolKind::Constant);
|
|
|
|
|
bool is_array_or_ptr = false;
|
|
|
|
|
|
|
|
|
|
if (sym->type) {
|
|
|
|
|
is_array_or_ptr = sym->type->IsArray() || sym->type->IsPtrInt32() || sym->type->IsPtrFloat();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t dim_count = 0;
|
|
|
|
|
std::shared_ptr<ir::Type> elem_type = sym->type;
|
|
|
|
|
std::vector<int> dims;
|
|
|
|
|
|
|
|
|
|
// 获取维度信息
|
|
|
|
|
if (sym->type && sym->type->IsArray()) {
|
|
|
|
|
if (auto* arr_type = dynamic_cast<ir::ArrayType*>(sym->type.get())) {
|
|
|
|
|
dims = arr_type->GetDimensions();
|
|
|
|
|
dim_count = dims.size();
|
|
|
|
|
dim_count = arr_type->GetDimensions().size();
|
|
|
|
|
elem_type = arr_type->GetElementType();
|
|
|
|
|
}
|
|
|
|
|
} else if (sym->is_array_param) {
|
|
|
|
|
// 数组参数,使用保存的维度信息
|
|
|
|
|
dims = sym->array_dims;
|
|
|
|
|
dim_count = dims.size();
|
|
|
|
|
// 元素类型是基本类型
|
|
|
|
|
if (sym->type->IsPtrInt32()) {
|
|
|
|
|
elem_type = ir::Type::GetInt32Type();
|
|
|
|
|
} else if (sym->type->IsPtrFloat()) {
|
|
|
|
|
elem_type = ir::Type::GetFloatType();
|
|
|
|
|
}
|
|
|
|
|
} else if (sym->type && (sym->type->IsPtrInt32() || sym->type->IsPtrFloat())) {
|
|
|
|
|
// 普通指针,只能有一个下标
|
|
|
|
|
dim_count = 1;
|
|
|
|
|
if (sym->type->IsPtrInt32()) {
|
|
|
|
|
elem_type = ir::Type::GetInt32Type();
|
|
|
|
|
@ -1408,4 +1520,4 @@ SemanticContext RunSema(SysYParser::CompUnitContext& comp_unit) {
|
|
|
|
|
SemaVisitor visitor;
|
|
|
|
|
comp_unit.accept(&visitor);
|
|
|
|
|
return visitor.TakeSemanticContext();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|