xdrgen: Add generator code for XDR width macros

Introduce logic in the code generators to emit maxsize (XDR
width) definitions. In C, these are pre-processor macros.

Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
This commit is contained in:
Chuck Lever 2024-10-03 14:54:45 -04:00
parent ce5a75d993
commit e9e1e7e75a
15 changed files with 107 additions and 6 deletions

View File

@ -111,3 +111,7 @@ class SourceGenerator:
def emit_encoder(self, node: _XdrAst) -> None:
"""Emit one encoder function for this XDR type"""
raise NotImplementedError("Encoder generation not supported")
def emit_maxsize(self, node: _XdrAst) -> None:
"""Emit one maxsize macro for this XDR type"""
raise NotImplementedError("Maxsize macro generation not supported")

View File

@ -4,7 +4,7 @@
"""Generate code to handle XDR enum types"""
from generators import SourceGenerator, create_jinja2_environment
from xdr_ast import _XdrEnum, public_apis, big_endian
from xdr_ast import _XdrEnum, public_apis, big_endian, get_header_name
class XdrEnumGenerator(SourceGenerator):
@ -51,3 +51,14 @@ class XdrEnumGenerator(SourceGenerator):
else:
template = self.environment.get_template("encoder/enum.j2")
print(template.render(name=node.name))
def emit_maxsize(self, node: _XdrEnum) -> None:
"""Emit one maxsize macro for an XDR enum type"""
macro_name = get_header_name().upper() + "_" + node.name + "_sz"
template = self.environment.get_template("maxsize/enum.j2")
print(
template.render(
macro=macro_name,
width=" + ".join(node.symbolic_width()),
)
)

View File

@ -12,7 +12,7 @@ from xdr_ast import _XdrBasic, _XdrString
from xdr_ast import _XdrFixedLengthOpaque, _XdrVariableLengthOpaque
from xdr_ast import _XdrFixedLengthArray, _XdrVariableLengthArray
from xdr_ast import _XdrOptionalData, _XdrPointer, _XdrDeclaration
from xdr_ast import public_apis
from xdr_ast import public_apis, get_header_name
def emit_pointer_declaration(environment: Environment, node: _XdrPointer) -> None:
@ -247,6 +247,18 @@ def emit_pointer_encoder(environment: Environment, node: _XdrPointer) -> None:
print(template.render())
def emit_pointer_maxsize(environment: Environment, node: _XdrPointer) -> None:
"""Emit one maxsize macro for an XDR pointer type"""
macro_name = get_header_name().upper() + "_" + node.name + "_sz"
template = get_jinja2_template(environment, "maxsize", "pointer")
print(
template.render(
macro=macro_name,
width=" + ".join(node.symbolic_width()),
)
)
class XdrPointerGenerator(SourceGenerator):
"""Generate source code for XDR pointer"""
@ -270,3 +282,7 @@ class XdrPointerGenerator(SourceGenerator):
def emit_encoder(self, node: _XdrPointer) -> None:
"""Emit one encoder function for an XDR pointer type"""
emit_pointer_encoder(self.environment, node)
def emit_maxsize(self, node: _XdrPointer) -> None:
"""Emit one maxsize macro for an XDR pointer type"""
emit_pointer_maxsize(self.environment, node)

View File

@ -12,7 +12,7 @@ from xdr_ast import _XdrBasic, _XdrString
from xdr_ast import _XdrFixedLengthOpaque, _XdrVariableLengthOpaque
from xdr_ast import _XdrFixedLengthArray, _XdrVariableLengthArray
from xdr_ast import _XdrOptionalData, _XdrStruct, _XdrDeclaration
from xdr_ast import public_apis
from xdr_ast import public_apis, get_header_name
def emit_struct_declaration(environment: Environment, node: _XdrStruct) -> None:
@ -247,6 +247,18 @@ def emit_struct_encoder(environment: Environment, node: _XdrStruct) -> None:
print(template.render())
def emit_struct_maxsize(environment: Environment, node: _XdrStruct) -> None:
"""Emit one maxsize macro for an XDR struct type"""
macro_name = get_header_name().upper() + "_" + node.name + "_sz"
template = get_jinja2_template(environment, "maxsize", "struct")
print(
template.render(
macro=macro_name,
width=" + ".join(node.symbolic_width()),
)
)
class XdrStructGenerator(SourceGenerator):
"""Generate source code for XDR structs"""
@ -270,3 +282,7 @@ class XdrStructGenerator(SourceGenerator):
def emit_encoder(self, node: _XdrStruct) -> None:
"""Emit one encoder function for an XDR struct type"""
emit_struct_encoder(self.environment, node)
def emit_maxsize(self, node: _XdrStruct) -> None:
"""Emit one maxsize macro for an XDR struct type"""
emit_struct_maxsize(self.environment, node)

View File

@ -12,7 +12,7 @@ from xdr_ast import _XdrBasic, _XdrTypedef, _XdrString
from xdr_ast import _XdrFixedLengthOpaque, _XdrVariableLengthOpaque
from xdr_ast import _XdrFixedLengthArray, _XdrVariableLengthArray
from xdr_ast import _XdrOptionalData, _XdrVoid, _XdrDeclaration
from xdr_ast import public_apis
from xdr_ast import public_apis, get_header_name
def emit_typedef_declaration(environment: Environment, node: _XdrDeclaration) -> None:
@ -230,6 +230,18 @@ def emit_typedef_encoder(environment: Environment, node: _XdrDeclaration) -> Non
raise NotImplementedError("typedef: type not recognized")
def emit_typedef_maxsize(environment: Environment, node: _XdrDeclaration) -> None:
"""Emit a maxsize macro for an XDR typedef"""
macro_name = get_header_name().upper() + "_" + node.name + "_sz"
template = get_jinja2_template(environment, "maxsize", node.template)
print(
template.render(
macro=macro_name,
width=" + ".join(node.symbolic_width()),
)
)
class XdrTypedefGenerator(SourceGenerator):
"""Generate source code for XDR typedefs"""
@ -253,3 +265,7 @@ class XdrTypedefGenerator(SourceGenerator):
def emit_encoder(self, node: _XdrTypedef) -> None:
"""Emit one encoder function for an XDR typedef"""
emit_typedef_encoder(self.environment, node.declaration)
def emit_maxsize(self, node: _XdrTypedef) -> None:
"""Emit one maxsize macro for an XDR typedef"""
emit_typedef_maxsize(self.environment, node.declaration)

View File

@ -8,8 +8,8 @@ from jinja2 import Environment
from generators import SourceGenerator
from generators import create_jinja2_environment, get_jinja2_template
from xdr_ast import _XdrBasic, _XdrUnion, _XdrVoid, big_endian
from xdr_ast import _XdrDeclaration, _XdrCaseSpec, public_apis
from xdr_ast import _XdrBasic, _XdrUnion, _XdrVoid, get_header_name
from xdr_ast import _XdrDeclaration, _XdrCaseSpec, public_apis, big_endian
def emit_union_declaration(environment: Environment, node: _XdrUnion) -> None:
@ -234,6 +234,18 @@ def emit_union_encoder(environment, node: _XdrUnion) -> None:
print(template.render())
def emit_union_maxsize(environment: Environment, node: _XdrUnion) -> None:
"""Emit one maxsize macro for an XDR union type"""
macro_name = get_header_name().upper() + "_" + node.name + "_sz"
template = get_jinja2_template(environment, "maxsize", "union")
print(
template.render(
macro=macro_name,
width=" + ".join(node.symbolic_width()),
)
)
class XdrUnionGenerator(SourceGenerator):
"""Generate source code for XDR unions"""
@ -257,3 +269,7 @@ class XdrUnionGenerator(SourceGenerator):
def emit_encoder(self, node: _XdrUnion) -> None:
"""Emit one encoder function for an XDR union"""
emit_union_encoder(self.environment, node)
def emit_maxsize(self, node: _XdrUnion) -> None:
"""Emit one maxsize macro for an XDR union"""
emit_union_maxsize(self.environment, node)

View File

@ -0,0 +1,2 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} ({{ width }})

View File

@ -0,0 +1,3 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} \
({{ width }})

View File

@ -0,0 +1,3 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} \
({{ width }})

View File

@ -0,0 +1,3 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} \
({{ width }})

View File

@ -0,0 +1,2 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} ({{ width }})

View File

@ -0,0 +1,2 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} ({{ width }})

View File

@ -0,0 +1,2 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} ({{ width }})

View File

@ -0,0 +1,2 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} ({{ width }})

View File

@ -0,0 +1,3 @@
{# SPDX-License-Identifier: GPL-2.0 #}
#define {{ '{:<31}'.format(macro) }} \
({{ width }})