[midend]修复全局数组类型问题

This commit is contained in:
rain2133
2025-07-24 15:22:38 +08:00
parent 2556ab7315
commit c68b031c01

View File

@ -146,7 +146,11 @@ std::any SysYIRGenerator::visitGlobalVarDecl(SysYParser::GlobalVarDeclContext *c
delete root;
}
// 创建全局变量,并更新符号表
module->createGlobalValue(name, Type::getPointerType(type), dims, values);
Type* variableType = type;
if (!dims.empty()) { // 如果有维度,说明是数组
variableType = buildArrayType(type, dims); // 构建完整的 ArrayType
}
module->createGlobalValue(name, Type::getPointerType(variableType), dims, values);
}
return std::any();
}
@ -193,7 +197,7 @@ std::any SysYIRGenerator::visitVarDecl(SysYParser::VarDeclContext *ctx) {
// 对于数组alloca 的类型将是指针指向数组类型,例如 `int[2][3]*`
// 对于标量alloca 的类型将是指针指向标量类型,例如 `int*`
AllocaInst* alloca =
builder.createAllocaInst(Type::getPointerType(type), dims, name);
builder.createAllocaInst(Type::getPointerType(variableType), dims, name);
if (varDef->initVal() != nullptr) {
ValueCounter values;
@ -1232,6 +1236,7 @@ auto SysYIRGenerator::visitLOrExp(SysYParser::LOrExpContext *ctx) -> std::any {
return std::any();
}
// attention : 这里的type是数组元素的type
void Utils::tree2Array(Type *type, ArrayValueTree *root,
const std::vector<Value *> &dims, unsigned numDims,
ValueCounter &result, IRBuilder *builder) {