|
|
|
|
@ -85,25 +85,25 @@ SysYParser::VarDefContext* SymbolTable::Lookup(const std::string& name) const {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---------- 辅助函数:从语法树节点构造 Type ----------
|
|
|
|
|
std::shared_ptr<Type> SymbolTable::getTypeFromVarDef(SysYParser::VarDefContext* ctx) {
|
|
|
|
|
std::shared_ptr<ir::Type> SymbolTable::getTypeFromVarDef(SysYParser::VarDefContext* ctx) {
|
|
|
|
|
// 这里需要根据实际的语法树结构来提取类型。
|
|
|
|
|
// 假设 ctx 中包含类型节点和数组维度信息。
|
|
|
|
|
// 以下代码为示意,具体实现需根据你的 SysY 语法定义调整。
|
|
|
|
|
|
|
|
|
|
// 1. 获取基本类型
|
|
|
|
|
std::shared_ptr<Type> base_type;
|
|
|
|
|
std::shared_ptr<ir::Type> base_type;
|
|
|
|
|
// 假设 ctx 中有 type() 方法返回类型节点,且类型节点有 INT()、FLOAT() 等方法
|
|
|
|
|
if (ctx->type()) {
|
|
|
|
|
if (ctx->type()->INT()) {
|
|
|
|
|
base_type = Type::GetInt32Type();
|
|
|
|
|
base_type = ir::Type::GetInt32Type();
|
|
|
|
|
} else if (ctx->type()->FLOAT()) {
|
|
|
|
|
base_type = Type::GetFloatType();
|
|
|
|
|
base_type = ir::Type::GetFloatType();
|
|
|
|
|
} else {
|
|
|
|
|
// 默认为 int
|
|
|
|
|
base_type = Type::GetInt32Type();
|
|
|
|
|
base_type = ir::Type::GetInt32Type();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
base_type = Type::GetInt32Type(); // 默认
|
|
|
|
|
base_type = ir::Type::GetInt32Type(); // 默认
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. 处理数组维度
|
|
|
|
|
@ -121,44 +121,44 @@ std::shared_ptr<Type> SymbolTable::getTypeFromVarDef(SysYParser::VarDefContext*
|
|
|
|
|
} else {
|
|
|
|
|
// 数组类型:递归构建数组类型
|
|
|
|
|
// 注意:Type::GetArrayType 需要元素类型和维度列表
|
|
|
|
|
return Type::GetArrayType(base_type, dims);
|
|
|
|
|
return ir::Type::GetArrayType(base_type, dims);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<Type> SymbolTable::getTypeFromFuncDef(SysYParser::FuncDefContext* ctx) {
|
|
|
|
|
std::shared_ptr<ir::Type> SymbolTable::getTypeFromFuncDef(SysYParser::FuncDefContext* ctx) {
|
|
|
|
|
// 提取返回类型
|
|
|
|
|
std::shared_ptr<Type> ret_type;
|
|
|
|
|
std::shared_ptr<ir::Type> ret_type;
|
|
|
|
|
if (ctx->type()) {
|
|
|
|
|
if (ctx->type()->VOID()) {
|
|
|
|
|
ret_type = Type::GetVoidType();
|
|
|
|
|
ret_type = ir::Type::GetVoidType();
|
|
|
|
|
} else if (ctx->type()->INT()) {
|
|
|
|
|
ret_type = Type::GetInt32Type();
|
|
|
|
|
ret_type = ir::Type::GetInt32Type();
|
|
|
|
|
} else if (ctx->type()->FLOAT()) {
|
|
|
|
|
ret_type = Type::GetFloatType();
|
|
|
|
|
ret_type = ir::Type::GetFloatType();
|
|
|
|
|
} else {
|
|
|
|
|
ret_type = Type::GetInt32Type(); // 默认
|
|
|
|
|
ret_type = ir::Type::GetInt32Type(); // 默认
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ret_type = Type::GetInt32Type(); // 默认
|
|
|
|
|
ret_type = ir::Type::GetInt32Type(); // 默认
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 提取参数类型列表
|
|
|
|
|
std::vector<std::shared_ptr<Type>> param_types;
|
|
|
|
|
std::vector<std::shared_ptr<ir::Type>> param_types;
|
|
|
|
|
if (ctx->paramList()) {
|
|
|
|
|
for (auto param : ctx->paramList()->param()) {
|
|
|
|
|
// 根据参数声明构建类型
|
|
|
|
|
// 这里假设每个参数有类型节点,并且可能包含数组维度
|
|
|
|
|
std::shared_ptr<Type> param_type;
|
|
|
|
|
std::shared_ptr<ir::Type> param_type;
|
|
|
|
|
if (param->type()) {
|
|
|
|
|
if (param->type()->INT()) {
|
|
|
|
|
param_type = Type::GetInt32Type();
|
|
|
|
|
param_type = ir::Type::GetInt32Type();
|
|
|
|
|
} else if (param->type()->FLOAT()) {
|
|
|
|
|
param_type = Type::GetFloatType();
|
|
|
|
|
param_type = ir::Type::GetFloatType();
|
|
|
|
|
} else {
|
|
|
|
|
param_type = Type::GetInt32Type();
|
|
|
|
|
param_type = ir::Type::GetInt32Type();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
param_type = Type::GetInt32Type();
|
|
|
|
|
param_type = ir::Type::GetInt32Type();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理数组参数(如果存在维度)
|
|
|
|
|
@ -169,5 +169,5 @@ std::shared_ptr<Type> SymbolTable::getTypeFromFuncDef(SysYParser::FuncDefContext
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建函数类型
|
|
|
|
|
return Type::GetFunctionType(ret_type, param_types);
|
|
|
|
|
return ir::Type::GetFunctionType(ret_type, param_types);
|
|
|
|
|
}
|