[midend]修复错误的RelExp类型转换

This commit is contained in:
rain2133
2025-07-31 13:55:59 +08:00
parent 6d60522ce2
commit fc7afdbb35

View File

@ -33,6 +33,9 @@ namespace sysy {
Value *SysYIRGenerator::promoteType(Value *value, Type *targetType) {
//如果是常量则直接返回相应的值
if (targetType == nullptr) {
return value; // 如果值为空,那就不需要转换
}
ConstantInteger* constInt = dynamic_cast<ConstantInteger *>(value);
ConstantFloating *constFloat = dynamic_cast<ConstantFloating *>(value);
if (constInt) {
@ -385,9 +388,9 @@ Value* SysYIRGenerator::computeExp(SysYParser::ExpContext *ctx, Type* targetType
BinaryExpLenStack.push_back(0); // 进入新的层次时Push 0
visitAddExp(ctx->addExp());
if(targetType == nullptr) {
targetType = Type::getIntType(); // 默认目标类型为int
}
// if(targetType == nullptr) {
// targetType = Type::getIntType(); // 默认目标类型为int
// }
compute();
// 最后一个Value应该是最终结果
@ -428,9 +431,9 @@ Value* SysYIRGenerator::computeAddExp(SysYParser::AddExpContext *ctx, Type* targ
visitMulExp(ctx->mulExp(i));
// BinaryValueStack.push_back(operand);
}
if(targetType == nullptr) {
targetType = Type::getIntType(); // 默认目标类型为int
}
// if(targetType == nullptr) {
// targetType = Type::getIntType(); // 默认目标类型为int
// }
// 根据后缀表达式的逻辑计算
compute();
// 最后一个Value应该是最终结果
@ -1620,13 +1623,13 @@ std::any SysYIRGenerator::visitAddExp(SysYParser::AddExpContext *ctx) {
}
std::any SysYIRGenerator::visitRelExp(SysYParser::RelExpContext *ctx) {
Value* result = computeAddExp(ctx->addExp(0), Type::getIntType());
Value* result = computeAddExp(ctx->addExp(0));
for (int i = 1; i < ctx->addExp().size(); i++) {
auto opNode = dynamic_cast<antlr4::tree::TerminalNode*>(ctx->children[2*i-1]);
int opType = opNode->getSymbol()->getType();
Value* operand = computeAddExp(ctx->addExp(i), Type::getIntType());
Value* operand = computeAddExp(ctx->addExp(i));
Type* resultType = result->getType();
Type* operandType = operand->getType();