summaryrefslogtreecommitdiff
path: root/lib9p/protogen/c_validate.py
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2025-03-28 10:33:39 -0600
committerLuke T. Shumaker <lukeshu@lukeshu.com>2025-03-28 10:54:57 -0600
commit703c99776fdfcf0e6bd5edda572de644eba3a452 (patch)
treef7612df82af97130e81bc53ff897be4928e434c4 /lib9p/protogen/c_validate.py
parent2c3dc0a67b3a64ca23645a8b524e1c977e145402 (diff)
lib9p: idl: Allow for const .cnt
Diffstat (limited to 'lib9p/protogen/c_validate.py')
-rw-r--r--lib9p/protogen/c_validate.py24
1 files changed, 18 insertions, 6 deletions
diff --git a/lib9p/protogen/c_validate.py b/lib9p/protogen/c_validate.py
index e315b60..7d0c69e 100644
--- a/lib9p/protogen/c_validate.py
+++ b/lib9p/protogen/c_validate.py
@@ -132,21 +132,33 @@ def gen_c_validate(versions: set[str], typs: list[idl.UserType]) -> str:
if should_save_offset(parent, child):
ret += f"{'\t'*indent_lvl()}uint32_t offsetof{''.join('_'+m.membname for m in path.elems)} = net_offset + {incr_buf};\n"
if child.cnt:
- assert child.cnt.typ.static_size
- cnt_path = path.parent().add(child.cnt)
- incr_flush()
+ if isinstance(child.cnt, int):
+ cnt_str = str(child.cnt)
+ cnt_typ = "size_t"
+ else:
+ assert child.cnt.typ.static_size
+ incr_flush()
+ cnt_str = f"LAST_U{child.cnt.typ.static_size*8}LE()"
+ cnt_typ = c9util.typename(child.cnt.typ)
if child.membname == "utf8": # SPECIAL (string)
+ assert child.typ.static_size == 1
# Yes, this is content-validation and "belongs" in
# gen_validate_content(), not here. But it's just
# easier this way.
- ret += f"{'\t'*indent_lvl()}VALIDATE_NET_UTF8(LAST_U{child.cnt.typ.static_size*8}LE());\n"
+ incr_flush()
+ ret += f"{'\t'*indent_lvl()}VALIDATE_NET_UTF8({cnt_str});\n"
return
if child.typ.static_size == 1: # SPECIAL (zerocopy)
- ret += f"{'\t'*indent_lvl()}VALIDATE_NET_BYTES(LAST_U{child.cnt.typ.static_size*8}LE());\n"
+ if isinstance(child.cnt, int):
+ incr_buf += child.cnt
+ return
+ incr_flush()
+ ret += f"{'\t'*indent_lvl()}VALIDATE_NET_BYTES({cnt_str});\n"
return
loopdepth = sum(1 for elem in path.elems if elem.cnt)
loopvar = chr(ord("i") + loopdepth - 1)
- ret += f"{'\t'*indent_lvl()}for ({c9util.typename(child.cnt.typ)} {loopvar} = 0, cnt = LAST_U{child.cnt.typ.static_size*8}LE(); {loopvar} < cnt; {loopvar}++) {{\n"
+ incr_flush()
+ ret += f"{'\t'*indent_lvl()}for ({cnt_typ} {loopvar} = 0, cnt = {cnt_str}; {loopvar} < cnt; {loopvar}++) {{\n"
indent_stack.append(IndentLevel(ifdef=False))
ret += f"{'\t'*indent_lvl()}RESERVE_HOST_BYTES(sizeof({c9util.typename(child.typ)}));\n"
if not isinstance(child.typ, idl.Struct):