feat: 为 StructType 类添加构造函数相关方法

- 添加了 addConstructor、getConstructor 和 getConstructors 方法
- 优化了类属性的注释,明确了方法的用途和参数含义
- 保持了原有的类结构和方法名称不变
This commit is contained in:
Luke 2025-09-01 17:05:24 +08:00
parent ba436cb91c
commit ef956fb930

View File

@ -23,6 +23,10 @@ public class StructType implements Type {
* 结构体类型名称 "Person"
*/
private final String name;
/**
* 形参个数 构造函数签名
*/
private final Map<Integer, FunctionType> constructors = new HashMap<>();
/**
* 字段定义表字段名 -> 字段类型
*/
@ -48,14 +52,46 @@ public class StructType implements Type {
}
/**
* 获取结构体所属模块名
* 获取结构体所属模块名
*
* @return 模块名称
*/
public String moduleName() {
return moduleName;
}
/**
* 获取结构体名称
* 添加构造函数签名
*
* @param ctor 构造函数的 {@link FunctionType} 实例
*/
public void addConstructor(FunctionType ctor) {
constructors.put(ctor.paramTypes().size(), ctor);
}
/**
* 根据参数个数获取对应构造函数
*
* @param argc 参数个数
* @return 对应的构造函数签名找不到则返回 null
*/
public FunctionType getConstructor(int argc) {
return constructors.get(argc);
}
/**
* 获取所有构造函数签名不可变视图
*
* @return 形参个数 构造函数签名的映射
*/
public Map<Integer, FunctionType> getConstructors() {
return Map.copyOf(constructors);
}
/**
* 获取结构体名称
*
* @return 结构体类型名
*/
@Override
public String name() {
@ -63,35 +99,43 @@ public class StructType implements Type {
}
/**
* 获取构造函数签名可为 null表示默认无参构造
* 获取默认构造函数签名
*
* @return 默认构造函数可能为 null表示无参构造
*/
public FunctionType getConstructor() {
return constructor;
}
/**
* 设置构造函数签名
* 设置默认构造函数签名
*
* @param ctor 构造函数类型
*/
public void setConstructor(FunctionType ctor) {
this.constructor = ctor;
}
/**
* 获取所有字段定义字段名 字段类型
* 获取所有字段定义字段名 字段类型
*
* @return 字段定义表
*/
public Map<String, Type> getFields() {
return fields;
}
/**
* 获取所有方法签名方法名 函数类型
* 获取所有方法签名方法名 函数类型
*
* @return 方法签名表
*/
public Map<String, FunctionType> getMethods() {
return methods;
}
/**
* 判断类型兼容性
* 判断类型兼容性
* <ul>
* <li>仅模块名与结构体名都相等才视为兼容</li>
* <li>跨模块同名 struct 不兼容</li>
@ -109,6 +153,9 @@ public class StructType implements Type {
/**
* 判断类型相等模块名+结构体名全等
*
* @param o 另一个对象
* @return 相等返回 true否则 false
*/
@Override
public boolean equals(Object o) {
@ -118,7 +165,9 @@ public class StructType implements Type {
}
/**
* 哈希值定义 equals 保持一致用于 Map/Set 索引
* 哈希值定义 equals 保持一致用于 Map/Set 索引
*
* @return 哈希码
*/
@Override
public int hashCode() {
@ -126,7 +175,9 @@ public class StructType implements Type {
}
/**
* 字符串表示返回结构体名调试用
* 字符串表示返回结构体名调试用
*
* @return 结构体名字符串
*/
@Override
public String toString() {