diff --git a/src/main/java/org/jcnc/snow/compiler/semantic/type/StructType.java b/src/main/java/org/jcnc/snow/compiler/semantic/type/StructType.java index 31bd504..c4e7139 100644 --- a/src/main/java/org/jcnc/snow/compiler/semantic/type/StructType.java +++ b/src/main/java/org/jcnc/snow/compiler/semantic/type/StructType.java @@ -23,6 +23,10 @@ public class StructType implements Type { * 结构体类型名称(如 "Person") */ private final String name; + /** + * 形参个数 → 构造函数签名 + */ + private final Map 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 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 getFields() { return fields; } /** - * 获取所有方法签名(方法名 → 函数类型) + * 获取所有方法签名(方法名 → 函数类型)。 + * + * @return 方法签名表 */ public Map getMethods() { return methods; } /** - * 判断类型兼容性: + * 判断类型兼容性。 *
    *
  • 仅模块名与结构体名都相等才视为兼容。
  • *
  • 跨模块同名 struct 不兼容。
  • @@ -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() {