[midend]IR修改常量类getint和getfloat逻辑,如果类型和方法不一致那么应用强制转换返回转换后的值

This commit is contained in:
rain2133
2025-07-29 23:52:37 +08:00
parent 42dce9820b
commit 31b6711d74

View File

@ -359,12 +359,25 @@ public:
// Helper methods to access constant values with appropriate casting
int getInt() const {
assert(getType()->isInt() && "Calling getInt() on non-integer type");
return std::get<int>(getVal());
auto val = getVal();
if (std::holds_alternative<int>(val)) {
return std::get<int>(val);
} else if (std::holds_alternative<float>(val)) {
return static_cast<int>(std::get<float>(val));
}
// Handle other possible types if needed
return 0; // Default fallback
}
float getFloat() const {
assert(getType()->isFloat() && "Calling getFloat() on non-float type");
return std::get<float>(getVal());
auto val = getVal();
if (std::holds_alternative<float>(val)) {
return std::get<float>(val);
} else if (std::holds_alternative<int>(val)) {
return static_cast<float>(std::get<int>(val));
}
// Handle other possible types if needed
return 0.0f; // Default fallback
}
template<typename T>