fix(tpv): 泛型特化名分裂——嵌套泛型实参递归 SHA1 规范化(_Type.md 问题 7)
- _norm_type_arg_name 顶层 '|' 分割改括号感知(list[A|B] 不再误分)
- 新增嵌套泛型实参递归:list[list[AST]] → list[list[{sha1}.AST]],
内部按顶层 ',' 分割逐个 SHA1 规范化
- 所有 module 表示统一为 Hash Namespace({SHA1}.Class),消除跨模块
特化名分裂(undefined symbol)
This commit is contained in:
1 parent
4415f1f38b
commit
2610f361fd
1 file changed
+104
-2
@@ -230,12 +230,22 @@ class TypeGenResolve(TypeGenBase.TypeGenBase):
|
||||
"""
|
||||
if type_name is None:
|
||||
return None
|
||||
# ---- 1. 联合类型 "A|B" ----
|
||||
# ---- 1. 顶层联合 "A|B"('|' 仅在括号外才分割)----
|
||||
# 括号感知:list[A|B] 的 '|' 在 '[' 内不是顶层联合,应留给第 1.5 步
|
||||
# 泛型 slice 递归处理;list[A]|t.CPtr 的 '|' 在括号外才是联合分割。
|
||||
seg_len: t.CSizeT = string.strlen(type_name)
|
||||
if seg_len == 0:
|
||||
return type_name
|
||||
pipe_idx: t.CSizeT = seg_len
|
||||
p_depth: int = 0
|
||||
pi: t.CSizeT = 0
|
||||
while pi < seg_len:
|
||||
if type_name[pi] == '|':
|
||||
ch_p: int = type_name[pi]
|
||||
if ch_p == '[':
|
||||
p_depth += 1
|
||||
elif ch_p == ']':
|
||||
p_depth -= 1
|
||||
elif ch_p == '|' and p_depth == 0:
|
||||
pipe_idx = pi
|
||||
break
|
||||
pi += 1
|
||||
@@ -266,6 +276,98 @@ class TypeGenResolve(TypeGenBase.TypeGenBase):
|
||||
string.strcat(out, "|")
|
||||
string.strcat(out, r_n)
|
||||
return out
|
||||
# ---- 1.5 嵌套泛型实参 "Name[A,B]...":内部参数递归规范化 ----
|
||||
# 场景:一侧 list[list[AST]]、另一侧 list[list[ast.AST]] → 特化名分裂。
|
||||
# 顶层 '['(深度 0 首个)且整体以 ']' 结尾才按泛型表示处理("list[A]|B"
|
||||
# 顶层联合已在第 1 步分割,此处 type_name 是纯泛型表示)。内部参数按
|
||||
# 顶层 ',' 分割后逐个递归 _norm_type_arg_name,再重组 "Name[arg1,arg2]"
|
||||
# ——保证所有 module 表示统一为 Hash Namespace({SHA1}.Class)。
|
||||
lb_idx: t.CSizeT = seg_len
|
||||
ld: int = 0
|
||||
li2: t.CSizeT = 0
|
||||
while li2 < seg_len:
|
||||
if type_name[li2] == '[':
|
||||
if ld == 0:
|
||||
lb_idx = li2
|
||||
break
|
||||
ld += 1
|
||||
elif type_name[li2] == ']':
|
||||
ld -= 1
|
||||
li2 += 1
|
||||
if lb_idx < seg_len and type_name[seg_len - 1] == ']':
|
||||
container_g: str = pool.alloc(lb_idx + 1)
|
||||
if container_g is not None:
|
||||
string.strncpy(container_g, type_name, lb_idx)
|
||||
container_g[lb_idx] = '\0'
|
||||
inner_len_g: t.CSizeT = seg_len - lb_idx - 2
|
||||
inner_buf_g: str = pool.alloc(inner_len_g + 1)
|
||||
if inner_buf_g is not None:
|
||||
string.strncpy(inner_buf_g, type_name + lb_idx + 1, inner_len_g)
|
||||
inner_buf_g[inner_len_g] = '\0'
|
||||
# 数参数个数(顶层 ',' 分割,跳过嵌套 '[' ']')
|
||||
arg_cnt_g: t.CSizeT = 1
|
||||
cur_dep_g: int = 0
|
||||
ci_g: t.CSizeT = 0
|
||||
while ci_g < inner_len_g:
|
||||
if inner_buf_g[ci_g] == '[':
|
||||
cur_dep_g += 1
|
||||
elif inner_buf_g[ci_g] == ']':
|
||||
cur_dep_g -= 1
|
||||
elif inner_buf_g[ci_g] == ',' and cur_dep_g == 0:
|
||||
arg_cnt_g += 1
|
||||
ci_g += 1
|
||||
arg_list_g: list[str] | t.CPtr = list[str](pool, arg_cnt_g)
|
||||
if arg_list_g is not None:
|
||||
arg_start_g: t.CSizeT = 0
|
||||
cur_dep_g = 0
|
||||
ci_g = 0
|
||||
while ci_g <= inner_len_g:
|
||||
is_sep_g: int = 0
|
||||
if ci_g < inner_len_g:
|
||||
if inner_buf_g[ci_g] == '[':
|
||||
cur_dep_g += 1
|
||||
elif inner_buf_g[ci_g] == ']':
|
||||
cur_dep_g -= 1
|
||||
elif inner_buf_g[ci_g] == ',' and cur_dep_g == 0:
|
||||
is_sep_g = 1
|
||||
else:
|
||||
is_sep_g = 1 # 结尾
|
||||
if is_sep_g != 0:
|
||||
alen_g: t.CSizeT = ci_g - arg_start_g
|
||||
abuf_g: str = pool.alloc(alen_g + 1)
|
||||
if abuf_g is not None:
|
||||
string.strncpy(abuf_g, inner_buf_g + arg_start_g, alen_g)
|
||||
abuf_g[alen_g] = '\0'
|
||||
anorm_g: str = self._norm_type_arg_name(pool, abuf_g, from_imports)
|
||||
if anorm_g is not None:
|
||||
arg_list_g.append(anorm_g)
|
||||
else:
|
||||
arg_list_g.append(abuf_g)
|
||||
arg_start_g = ci_g + 1
|
||||
ci_g += 1
|
||||
# 重组 container[arg1,arg2,...]
|
||||
g_total: t.CSizeT = string.strlen(container_g) + 2
|
||||
gi_g: t.CSizeT
|
||||
for gi_g in range(len(arg_list_g)):
|
||||
if gi_g > 0:
|
||||
g_total += 1
|
||||
g_arg: str = arg_list_g.get(gi_g)
|
||||
if g_arg is not None:
|
||||
g_total += string.strlen(g_arg)
|
||||
g_total += 1
|
||||
g_out: str = pool.alloc(g_total)
|
||||
if g_out is not None:
|
||||
g_out[0] = '\0'
|
||||
string.strcat(g_out, container_g)
|
||||
string.strcat(g_out, "[")
|
||||
for gi_g in range(len(arg_list_g)):
|
||||
if gi_g > 0:
|
||||
string.strcat(g_out, ",")
|
||||
g_arg2: str = arg_list_g.get(gi_g)
|
||||
if g_arg2 is not None:
|
||||
string.strcat(g_out, g_arg2)
|
||||
string.strcat(g_out, "]")
|
||||
return g_out
|
||||
# ---- 2. 已规范化(SHA1 前缀)----
|
||||
if self._is_sha1_prefixed(type_name) != 0:
|
||||
return type_name
|
||||
|
||||
Reference in new issue
Block a user