feat: 支持结构体多个构造函数

- 修改 SignatureRegistrar 类以支持结构体的多个构造函数
- 通过参数个数区分重载的构造函数
- 为每个构造函数解析参数类型并设置返回类型为 void
This commit is contained in:
Luke 2025-09-01 16:51:58 +08:00
parent 5367fc39f6
commit 6dc651d305

View File

@ -54,20 +54,22 @@ public record SignatureRegistrar(Context ctx) {
StructType st = new StructType(mod.name(), stn.name());
mi.getStructs().put(stn.name(), st);
// --- 2.1 构造函数 init ---
if (stn.init() != null) {
List<Type> ptypes = new ArrayList<>();
for (ParameterNode p : stn.init().parameters()) {
// 解析参数类型不存在则报错降级为 int
Type t = ctx.parseType(p.type());
if (t == null) {
ctx.errors().add(new SemanticError(p, "未知类型: " + p.type()));
t = BuiltinType.INT;
// --- 2.1 多个构造函数 init重载按参数个数区分 ---
if (stn.inits() != null) {
for (FunctionNode initFn : stn.inits()) {
List<Type> ptypes = new ArrayList<>();
for (ParameterNode p : initFn.parameters()) {
// 解析参数类型不存在则报错降级为 int
Type t = ctx.parseType(p.type());
if (t == null) {
ctx.errors().add(new SemanticError(p, "未知类型: " + p.type()));
t = BuiltinType.INT;
}
ptypes.add(t);
}
ptypes.add(t);
// 构造函数返回类型固定为 void
st.addConstructor(new FunctionType(ptypes, BuiltinType.VOID));
}
// 构造函数返回类型固定为 void
st.setConstructor(new FunctionType(ptypes, BuiltinType.VOID));
}
// --- 2.2 结构体方法签名 ---