|
|
|
|
@ -270,8 +270,14 @@ std::any IRGenImpl::visitLVal(SysYParser::LValContext* ctx) {
|
|
|
|
|
if (!ctx || !ctx->ID()) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "非法左值"));
|
|
|
|
|
}
|
|
|
|
|
ir::Value* addr = GetLValAddress(ctx);
|
|
|
|
|
BoundDecl bound = sema_.ResolveVarUse(ctx);
|
|
|
|
|
if ((bound.kind == BoundDecl::Kind::Var && !bound.var_decl) ||
|
|
|
|
|
(bound.kind == BoundDecl::Kind::Const && !bound.const_decl) ||
|
|
|
|
|
(bound.kind == BoundDecl::Kind::Param && !bound.param_decl)) {
|
|
|
|
|
throw std::runtime_error(
|
|
|
|
|
FormatError("irgen", "左值缺少语义绑定: " + ctx->getText()));
|
|
|
|
|
}
|
|
|
|
|
ir::Value* addr = GetLValAddress(ctx);
|
|
|
|
|
const TypeDesc* ty = nullptr;
|
|
|
|
|
if (bound.kind == BoundDecl::Kind::Var && bound.var_decl) {
|
|
|
|
|
ty = sema_.GetVarType(bound.var_decl);
|
|
|
|
|
@ -280,52 +286,6 @@ std::any IRGenImpl::visitLVal(SysYParser::LValContext* ctx) {
|
|
|
|
|
} else if (bound.kind == BoundDecl::Kind::Param && bound.param_decl) {
|
|
|
|
|
ty = sema_.GetParamType(bound.param_decl);
|
|
|
|
|
}
|
|
|
|
|
if (!ty && ctx->ID()) {
|
|
|
|
|
const auto name = ctx->ID()->getText();
|
|
|
|
|
for (const auto& kv : var_storage_) {
|
|
|
|
|
auto* def = const_cast<SysYParser::VarDefContext*>(kv.first);
|
|
|
|
|
if (def && def->ID() && def->ID()->getText() == name) {
|
|
|
|
|
ty = sema_.GetVarType(kv.first);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!ty) {
|
|
|
|
|
for (const auto& kv : const_storage_) {
|
|
|
|
|
auto* def = const_cast<SysYParser::ConstDefContext*>(kv.first);
|
|
|
|
|
if (def && def->ID() && def->ID()->getText() == name) {
|
|
|
|
|
ty = sema_.GetConstType(kv.first);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!ty) {
|
|
|
|
|
for (const auto& kv : param_storage_) {
|
|
|
|
|
auto* def = const_cast<SysYParser::FuncFParamContext*>(kv.first);
|
|
|
|
|
if (def && def->ID() && def->ID()->getText() == name) {
|
|
|
|
|
ty = sema_.GetParamType(kv.first);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!ty) {
|
|
|
|
|
for (const auto& kv : global_var_storage_) {
|
|
|
|
|
auto* def = const_cast<SysYParser::VarDefContext*>(kv.first);
|
|
|
|
|
if (def && def->ID() && def->ID()->getText() == name) {
|
|
|
|
|
ty = sema_.GetVarType(kv.first);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!ty) {
|
|
|
|
|
for (const auto& kv : global_const_storage_) {
|
|
|
|
|
auto* def = const_cast<SysYParser::ConstDefContext*>(kv.first);
|
|
|
|
|
if (def && def->ID() && def->ID()->getText() == name) {
|
|
|
|
|
ty = sema_.GetConstType(kv.first);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!ty) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "无法解析左值类型"));
|
|
|
|
|
}
|
|
|
|
|
@ -514,13 +474,22 @@ ir::Value* IRGenImpl::GetLValAddress(SysYParser::LValContext* ctx) {
|
|
|
|
|
const TypeDesc* ty = nullptr;
|
|
|
|
|
if (bound.kind == BoundDecl::Kind::Var && bound.var_decl) {
|
|
|
|
|
ty = sema_.GetVarType(bound.var_decl);
|
|
|
|
|
base_ptr = var_storage_[bound.var_decl];
|
|
|
|
|
auto it = var_storage_.find(bound.var_decl);
|
|
|
|
|
if (it != var_storage_.end()) {
|
|
|
|
|
base_ptr = it->second;
|
|
|
|
|
}
|
|
|
|
|
} else if (bound.kind == BoundDecl::Kind::Const && bound.const_decl) {
|
|
|
|
|
ty = sema_.GetConstType(bound.const_decl);
|
|
|
|
|
base_ptr = const_storage_[bound.const_decl];
|
|
|
|
|
auto it = const_storage_.find(bound.const_decl);
|
|
|
|
|
if (it != const_storage_.end()) {
|
|
|
|
|
base_ptr = it->second;
|
|
|
|
|
}
|
|
|
|
|
} else if (bound.kind == BoundDecl::Kind::Param && bound.param_decl) {
|
|
|
|
|
ty = sema_.GetParamType(bound.param_decl);
|
|
|
|
|
base_ptr = param_storage_[bound.param_decl];
|
|
|
|
|
auto it = param_storage_.find(bound.param_decl);
|
|
|
|
|
if (it != param_storage_.end()) {
|
|
|
|
|
base_ptr = it->second;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!base_ptr && bound.kind == BoundDecl::Kind::Var && bound.var_decl) {
|
|
|
|
|
auto it = global_var_storage_.find(bound.var_decl);
|
|
|
|
|
@ -536,57 +505,6 @@ ir::Value* IRGenImpl::GetLValAddress(SysYParser::LValContext* ctx) {
|
|
|
|
|
base_ptr = it->second;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!base_ptr && ctx && ctx->ID()) {
|
|
|
|
|
const auto name = ctx->ID()->getText();
|
|
|
|
|
for (const auto& kv : var_storage_) {
|
|
|
|
|
auto* def = const_cast<SysYParser::VarDefContext*>(kv.first);
|
|
|
|
|
if (def && def->ID() && def->ID()->getText() == name) {
|
|
|
|
|
ty = sema_.GetVarType(kv.first);
|
|
|
|
|
base_ptr = kv.second;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!base_ptr) {
|
|
|
|
|
for (const auto& kv : const_storage_) {
|
|
|
|
|
auto* def = const_cast<SysYParser::ConstDefContext*>(kv.first);
|
|
|
|
|
if (def && def->ID() && def->ID()->getText() == name) {
|
|
|
|
|
ty = sema_.GetConstType(kv.first);
|
|
|
|
|
base_ptr = kv.second;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!base_ptr) {
|
|
|
|
|
for (const auto& kv : param_storage_) {
|
|
|
|
|
auto* def = const_cast<SysYParser::FuncFParamContext*>(kv.first);
|
|
|
|
|
if (def && def->ID() && def->ID()->getText() == name) {
|
|
|
|
|
ty = sema_.GetParamType(kv.first);
|
|
|
|
|
base_ptr = kv.second;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!base_ptr) {
|
|
|
|
|
for (const auto& kv : global_var_storage_) {
|
|
|
|
|
auto* def = const_cast<SysYParser::VarDefContext*>(kv.first);
|
|
|
|
|
if (def && def->ID() && def->ID()->getText() == name) {
|
|
|
|
|
ty = sema_.GetVarType(kv.first);
|
|
|
|
|
base_ptr = kv.second;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!base_ptr) {
|
|
|
|
|
for (const auto& kv : global_const_storage_) {
|
|
|
|
|
auto* def = const_cast<SysYParser::ConstDefContext*>(kv.first);
|
|
|
|
|
if (def && def->ID() && def->ID()->getText() == name) {
|
|
|
|
|
ty = sema_.GetConstType(kv.first);
|
|
|
|
|
base_ptr = kv.second;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!base_ptr || !ty) {
|
|
|
|
|
throw std::runtime_error(FormatError("irgen", "左值未绑定"));
|
|
|
|
|
}
|
|
|
|
|
|