feat: 添加用户自定义结构体类型支持

- 新增 StructType 类,用于描述结构体的静态类型信息
- 实现了结构体类型的唯一标识、字段定义、方法签名和构造函数等功能
- 支持跨模块同名结构体的类型检查和兼容性判断
This commit is contained in:
Luke 2025-08-29 18:01:38 +08:00
parent 3942a22e67
commit 49cb89c9f2

View File

@ -0,0 +1,135 @@
package org.jcnc.snow.compiler.semantic.type;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
/**
* {@code StructType}
* <p>
* 表示用户自定义结构体类型在类型系统中用于描述结构体实例的静态类型信息
* <ul>
* <li> <b>(moduleName, structName)</b> 唯一标识支持跨模块同名 struct</li>
* <li>包含字段定义方法签名构造函数等全部类型元信息</li>
* </ul>
* </p>
*/
public class StructType implements Type {
/**
* 所属模块名称用于唯一标识支持跨模块同名结构体
*/
private final String moduleName;
/**
* 结构体类型名称 "Person"
*/
private final String name;
/**
* 字段定义表字段名 -> 字段类型
*/
private final Map<String, Type> fields = new HashMap<>();
/**
* 方法签名表方法名 -> 函数类型
*/
private final Map<String, FunctionType> methods = new HashMap<>();
/**
* 构造函数init的函数类型可为 null表示无参默认构造
*/
private FunctionType constructor;
/**
* 构造函数创建结构体类型描述
*
* @param moduleName 所属模块名不可为空
* @param name 结构体名称不可为空
*/
public StructType(String moduleName, String name) {
this.moduleName = moduleName;
this.name = name;
}
/**
* 获取结构体所属模块名
*/
public String moduleName() {
return moduleName;
}
/**
* 获取结构体名称
*/
@Override
public String name() {
return name;
}
/**
* 获取构造函数签名可为 null表示默认无参构造
*/
public FunctionType getConstructor() {
return constructor;
}
/**
* 设置构造函数签名
*/
public void setConstructor(FunctionType ctor) {
this.constructor = ctor;
}
/**
* 获取所有字段定义字段名 字段类型
*/
public Map<String, Type> getFields() {
return fields;
}
/**
* 获取所有方法签名方法名 函数类型
*/
public Map<String, FunctionType> getMethods() {
return methods;
}
/**
* 判断类型兼容性
* <ul>
* <li>仅模块名与结构体名都相等才视为兼容</li>
* <li>跨模块同名 struct 不兼容</li>
* </ul>
*
* @param other 另一个类型
* @return 类型兼容返回 true否则 false
*/
@Override
public boolean isCompatible(Type other) {
if (!(other instanceof StructType s)) return false;
return Objects.equals(this.name, s.name)
&& Objects.equals(this.moduleName, s.moduleName);
}
/**
* 判断类型相等模块名+结构体名全等
*/
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof StructType s)) return false;
return Objects.equals(name, s.name) && Objects.equals(moduleName, s.moduleName);
}
/**
* 哈希值定义 equals 保持一致用于 Map/Set 索引
*/
@Override
public int hashCode() {
return Objects.hash(moduleName, name);
}
/**
* 字符串表示返回结构体名调试用
*/
@Override
public String toString() {
return name;
}
}