xref: /linux/tools/net/sunrpc/xdrgen/generators/enum.py (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1*4b132aacSChuck Lever#!/usr/bin/env python3
2*4b132aacSChuck Lever# ex: set filetype=python:
3*4b132aacSChuck Lever
4*4b132aacSChuck Lever"""Generate code to handle XDR enum types"""
5*4b132aacSChuck Lever
6*4b132aacSChuck Leverfrom generators import SourceGenerator, create_jinja2_environment
7*4b132aacSChuck Leverfrom xdr_ast import _XdrEnum, public_apis
8*4b132aacSChuck Lever
9*4b132aacSChuck Lever
10*4b132aacSChuck Leverclass XdrEnumGenerator(SourceGenerator):
11*4b132aacSChuck Lever    """Generate source code for XDR enum types"""
12*4b132aacSChuck Lever
13*4b132aacSChuck Lever    def __init__(self, language: str, peer: str):
14*4b132aacSChuck Lever        """Initialize an instance of this class"""
15*4b132aacSChuck Lever        self.environment = create_jinja2_environment(language, "enum")
16*4b132aacSChuck Lever        self.peer = peer
17*4b132aacSChuck Lever
18*4b132aacSChuck Lever    def emit_declaration(self, node: _XdrEnum) -> None:
19*4b132aacSChuck Lever        """Emit one declaration pair for an XDR enum type"""
20*4b132aacSChuck Lever        if node.name in public_apis:
21*4b132aacSChuck Lever            template = self.environment.get_template("declaration/close.j2")
22*4b132aacSChuck Lever            print(template.render(name=node.name))
23*4b132aacSChuck Lever
24*4b132aacSChuck Lever    def emit_definition(self, node: _XdrEnum) -> None:
25*4b132aacSChuck Lever        """Emit one definition for an XDR enum type"""
26*4b132aacSChuck Lever        template = self.environment.get_template("definition/open.j2")
27*4b132aacSChuck Lever        print(template.render(name=node.name))
28*4b132aacSChuck Lever
29*4b132aacSChuck Lever        template = self.environment.get_template("definition/enumerator.j2")
30*4b132aacSChuck Lever        for enumerator in node.enumerators:
31*4b132aacSChuck Lever            print(template.render(name=enumerator.name, value=enumerator.value))
32*4b132aacSChuck Lever
33*4b132aacSChuck Lever        template = self.environment.get_template("definition/close.j2")
34*4b132aacSChuck Lever        print(template.render(name=node.name))
35*4b132aacSChuck Lever
36*4b132aacSChuck Lever    def emit_decoder(self, node: _XdrEnum) -> None:
37*4b132aacSChuck Lever        """Emit one decoder function for an XDR enum type"""
38*4b132aacSChuck Lever        template = self.environment.get_template("decoder/enum.j2")
39*4b132aacSChuck Lever        print(template.render(name=node.name))
40*4b132aacSChuck Lever
41*4b132aacSChuck Lever    def emit_encoder(self, node: _XdrEnum) -> None:
42*4b132aacSChuck Lever        """Emit one encoder function for an XDR enum type"""
43*4b132aacSChuck Lever        template = self.environment.get_template("encoder/enum.j2")
44*4b132aacSChuck Lever        print(template.render(name=node.name))
45