1 #!/usr/bin/env python3 1 #!/usr/bin/env python3 2 # ex: set filetype=python: 2 # ex: set filetype=python: 3 3 4 """Generate code to handle XDR struct types""" 4 """Generate code to handle XDR struct types""" 5 5 6 from jinja2 import Environment 6 from jinja2 import Environment 7 7 8 from generators import SourceGenerator, kernel 8 from generators import SourceGenerator, kernel_c_type 9 from generators import create_jinja2_environme 9 from generators import create_jinja2_environment, get_jinja2_template 10 10 11 from xdr_ast import _XdrBasic, _XdrVariableLen 11 from xdr_ast import _XdrBasic, _XdrVariableLengthString 12 from xdr_ast import _XdrFixedLengthOpaque, _Xd 12 from xdr_ast import _XdrFixedLengthOpaque, _XdrVariableLengthOpaque 13 from xdr_ast import _XdrFixedLengthArray, _Xdr 13 from xdr_ast import _XdrFixedLengthArray, _XdrVariableLengthArray 14 from xdr_ast import _XdrOptionalData, _XdrStru 14 from xdr_ast import _XdrOptionalData, _XdrStruct, _XdrDeclaration 15 from xdr_ast import public_apis 15 from xdr_ast import public_apis 16 16 17 17 18 def emit_struct_declaration(environment: Envir 18 def emit_struct_declaration(environment: Environment, node: _XdrStruct) -> None: 19 """Emit one declaration pair for an XDR st 19 """Emit one declaration pair for an XDR struct type""" 20 if node.name in public_apis: 20 if node.name in public_apis: 21 template = get_jinja2_template(environ 21 template = get_jinja2_template(environment, "declaration", "close") 22 print(template.render(name=node.name)) 22 print(template.render(name=node.name)) 23 23 24 24 25 def emit_struct_member_definition( 25 def emit_struct_member_definition( 26 environment: Environment, field: _XdrDecla 26 environment: Environment, field: _XdrDeclaration 27 ) -> None: 27 ) -> None: 28 """Emit a definition for one field in an X 28 """Emit a definition for one field in an XDR struct""" 29 if isinstance(field, _XdrBasic): 29 if isinstance(field, _XdrBasic): 30 template = get_jinja2_template(environ 30 template = get_jinja2_template(environment, "definition", field.template) 31 print( 31 print( 32 template.render( 32 template.render( 33 name=field.name, 33 name=field.name, 34 type=kernel_c_type(field.spec) 34 type=kernel_c_type(field.spec), 35 classifier=field.spec.c_classi 35 classifier=field.spec.c_classifier, 36 ) 36 ) 37 ) 37 ) 38 elif isinstance(field, _XdrFixedLengthOpaq 38 elif isinstance(field, _XdrFixedLengthOpaque): 39 template = get_jinja2_template(environ 39 template = get_jinja2_template(environment, "definition", field.template) 40 print( 40 print( 41 template.render( 41 template.render( 42 name=field.name, 42 name=field.name, 43 size=field.size, 43 size=field.size, 44 ) 44 ) 45 ) 45 ) 46 elif isinstance(field, _XdrVariableLengthO 46 elif isinstance(field, _XdrVariableLengthOpaque): 47 template = get_jinja2_template(environ 47 template = get_jinja2_template(environment, "definition", field.template) 48 print(template.render(name=field.name) 48 print(template.render(name=field.name)) 49 elif isinstance(field, _XdrVariableLengthS 49 elif isinstance(field, _XdrVariableLengthString): 50 template = get_jinja2_template(environ 50 template = get_jinja2_template(environment, "definition", field.template) 51 print(template.render(name=field.name) 51 print(template.render(name=field.name)) 52 elif isinstance(field, _XdrFixedLengthArra 52 elif isinstance(field, _XdrFixedLengthArray): 53 template = get_jinja2_template(environ 53 template = get_jinja2_template(environment, "definition", field.template) 54 print( 54 print( 55 template.render( 55 template.render( 56 name=field.name, 56 name=field.name, 57 type=kernel_c_type(field.spec) 57 type=kernel_c_type(field.spec), 58 size=field.size, 58 size=field.size, 59 ) 59 ) 60 ) 60 ) 61 elif isinstance(field, _XdrVariableLengthA 61 elif isinstance(field, _XdrVariableLengthArray): 62 template = get_jinja2_template(environ 62 template = get_jinja2_template(environment, "definition", field.template) 63 print( 63 print( 64 template.render( 64 template.render( 65 name=field.name, 65 name=field.name, 66 type=kernel_c_type(field.spec) 66 type=kernel_c_type(field.spec), 67 classifier=field.spec.c_classi 67 classifier=field.spec.c_classifier, 68 ) 68 ) 69 ) 69 ) 70 elif isinstance(field, _XdrOptionalData): 70 elif isinstance(field, _XdrOptionalData): 71 template = get_jinja2_template(environ 71 template = get_jinja2_template(environment, "definition", field.template) 72 print( 72 print( 73 template.render( 73 template.render( 74 name=field.name, 74 name=field.name, 75 type=kernel_c_type(field.spec) 75 type=kernel_c_type(field.spec), 76 classifier=field.spec.c_classi 76 classifier=field.spec.c_classifier, 77 ) 77 ) 78 ) 78 ) 79 79 80 80 81 def emit_struct_definition(environment: Enviro 81 def emit_struct_definition(environment: Environment, node: _XdrStruct) -> None: 82 """Emit one definition for an XDR struct t 82 """Emit one definition for an XDR struct type""" 83 template = get_jinja2_template(environment 83 template = get_jinja2_template(environment, "definition", "open") 84 print(template.render(name=node.name)) 84 print(template.render(name=node.name)) 85 85 86 for field in node.fields: 86 for field in node.fields: 87 emit_struct_member_definition(environm 87 emit_struct_member_definition(environment, field) 88 88 89 template = get_jinja2_template(environment 89 template = get_jinja2_template(environment, "definition", "close") 90 print(template.render(name=node.name)) 90 print(template.render(name=node.name)) 91 91 92 92 93 def emit_struct_member_decoder( 93 def emit_struct_member_decoder( 94 environment: Environment, field: _XdrDecla 94 environment: Environment, field: _XdrDeclaration 95 ) -> None: 95 ) -> None: 96 """Emit a decoder for one field in an XDR 96 """Emit a decoder for one field in an XDR struct""" 97 if isinstance(field, _XdrBasic): 97 if isinstance(field, _XdrBasic): 98 template = get_jinja2_template(environ 98 template = get_jinja2_template(environment, "decoder", field.template) 99 print( 99 print( 100 template.render( 100 template.render( 101 name=field.name, 101 name=field.name, 102 type=field.spec.type_name, 102 type=field.spec.type_name, 103 classifier=field.spec.c_classi 103 classifier=field.spec.c_classifier, 104 ) 104 ) 105 ) 105 ) 106 elif isinstance(field, _XdrFixedLengthOpaq 106 elif isinstance(field, _XdrFixedLengthOpaque): 107 template = get_jinja2_template(environ 107 template = get_jinja2_template(environment, "decoder", field.template) 108 print( 108 print( 109 template.render( 109 template.render( 110 name=field.name, 110 name=field.name, 111 size=field.size, 111 size=field.size, 112 ) 112 ) 113 ) 113 ) 114 elif isinstance(field, _XdrVariableLengthO 114 elif isinstance(field, _XdrVariableLengthOpaque): 115 template = get_jinja2_template(environ 115 template = get_jinja2_template(environment, "decoder", field.template) 116 print( 116 print( 117 template.render( 117 template.render( 118 name=field.name, 118 name=field.name, 119 maxsize=field.maxsize, 119 maxsize=field.maxsize, 120 ) 120 ) 121 ) 121 ) 122 elif isinstance(field, _XdrVariableLengthS 122 elif isinstance(field, _XdrVariableLengthString): 123 template = get_jinja2_template(environ 123 template = get_jinja2_template(environment, "decoder", field.template) 124 print( 124 print( 125 template.render( 125 template.render( 126 name=field.name, 126 name=field.name, 127 maxsize=field.maxsize, 127 maxsize=field.maxsize, 128 ) 128 ) 129 ) 129 ) 130 elif isinstance(field, _XdrFixedLengthArra 130 elif isinstance(field, _XdrFixedLengthArray): 131 template = get_jinja2_template(environ 131 template = get_jinja2_template(environment, "decoder", field.template) 132 print( 132 print( 133 template.render( 133 template.render( 134 name=field.name, 134 name=field.name, 135 type=field.spec.type_name, 135 type=field.spec.type_name, 136 size=field.size, 136 size=field.size, 137 classifier=field.spec.c_classi 137 classifier=field.spec.c_classifier, 138 ) 138 ) 139 ) 139 ) 140 elif isinstance(field, _XdrVariableLengthA 140 elif isinstance(field, _XdrVariableLengthArray): 141 template = get_jinja2_template(environ 141 template = get_jinja2_template(environment, "decoder", field.template) 142 print( 142 print( 143 template.render( 143 template.render( 144 name=field.name, 144 name=field.name, 145 type=field.spec.type_name, 145 type=field.spec.type_name, 146 maxsize=field.maxsize, 146 maxsize=field.maxsize, 147 classifier=field.spec.c_classi 147 classifier=field.spec.c_classifier, 148 ) 148 ) 149 ) 149 ) 150 elif isinstance(field, _XdrOptionalData): 150 elif isinstance(field, _XdrOptionalData): 151 template = get_jinja2_template(environ 151 template = get_jinja2_template(environment, "decoder", field.template) 152 print( 152 print( 153 template.render( 153 template.render( 154 name=field.name, 154 name=field.name, 155 type=field.spec.type_name, 155 type=field.spec.type_name, 156 classifier=field.spec.c_classi 156 classifier=field.spec.c_classifier, 157 ) 157 ) 158 ) 158 ) 159 159 160 160 161 def emit_struct_decoder(environment: Environme 161 def emit_struct_decoder(environment: Environment, node: _XdrStruct) -> None: 162 """Emit one decoder function for an XDR st 162 """Emit one decoder function for an XDR struct type""" 163 template = get_jinja2_template(environment 163 template = get_jinja2_template(environment, "decoder", "open") 164 print(template.render(name=node.name)) 164 print(template.render(name=node.name)) 165 165 166 for field in node.fields: 166 for field in node.fields: 167 emit_struct_member_decoder(environment 167 emit_struct_member_decoder(environment, field) 168 168 169 template = get_jinja2_template(environment 169 template = get_jinja2_template(environment, "decoder", "close") 170 print(template.render()) 170 print(template.render()) 171 171 172 172 173 def emit_struct_member_encoder( 173 def emit_struct_member_encoder( 174 environment: Environment, field: _XdrDecla 174 environment: Environment, field: _XdrDeclaration 175 ) -> None: 175 ) -> None: 176 """Emit an encoder for one field in an XDR 176 """Emit an encoder for one field in an XDR struct""" 177 if isinstance(field, _XdrBasic): 177 if isinstance(field, _XdrBasic): 178 template = get_jinja2_template(environ 178 template = get_jinja2_template(environment, "encoder", field.template) 179 print( 179 print( 180 template.render( 180 template.render( 181 name=field.name, 181 name=field.name, 182 type=field.spec.type_name, 182 type=field.spec.type_name, 183 ) 183 ) 184 ) 184 ) 185 elif isinstance(field, _XdrFixedLengthOpaq 185 elif isinstance(field, _XdrFixedLengthOpaque): 186 template = get_jinja2_template(environ 186 template = get_jinja2_template(environment, "encoder", field.template) 187 print( 187 print( 188 template.render( 188 template.render( 189 name=field.name, 189 name=field.name, 190 size=field.size, 190 size=field.size, 191 ) 191 ) 192 ) 192 ) 193 elif isinstance(field, _XdrVariableLengthO 193 elif isinstance(field, _XdrVariableLengthOpaque): 194 template = get_jinja2_template(environ 194 template = get_jinja2_template(environment, "encoder", field.template) 195 print( 195 print( 196 template.render( 196 template.render( 197 name=field.name, 197 name=field.name, 198 maxsize=field.maxsize, 198 maxsize=field.maxsize, 199 ) 199 ) 200 ) 200 ) 201 elif isinstance(field, _XdrVariableLengthS 201 elif isinstance(field, _XdrVariableLengthString): 202 template = get_jinja2_template(environ 202 template = get_jinja2_template(environment, "encoder", field.template) 203 print( 203 print( 204 template.render( 204 template.render( 205 name=field.name, 205 name=field.name, 206 maxsize=field.maxsize, 206 maxsize=field.maxsize, 207 ) 207 ) 208 ) 208 ) 209 elif isinstance(field, _XdrFixedLengthArra 209 elif isinstance(field, _XdrFixedLengthArray): 210 template = get_jinja2_template(environ 210 template = get_jinja2_template(environment, "encoder", field.template) 211 print( 211 print( 212 template.render( 212 template.render( 213 name=field.name, 213 name=field.name, 214 type=field.spec.type_name, 214 type=field.spec.type_name, 215 size=field.size, 215 size=field.size, 216 ) 216 ) 217 ) 217 ) 218 elif isinstance(field, _XdrVariableLengthA 218 elif isinstance(field, _XdrVariableLengthArray): 219 template = get_jinja2_template(environ 219 template = get_jinja2_template(environment, "encoder", field.template) 220 print( 220 print( 221 template.render( 221 template.render( 222 name=field.name, 222 name=field.name, 223 type=field.spec.type_name, 223 type=field.spec.type_name, 224 maxsize=field.maxsize, 224 maxsize=field.maxsize, 225 ) 225 ) 226 ) 226 ) 227 elif isinstance(field, _XdrOptionalData): 227 elif isinstance(field, _XdrOptionalData): 228 template = get_jinja2_template(environ 228 template = get_jinja2_template(environment, "encoder", field.template) 229 print( 229 print( 230 template.render( 230 template.render( 231 name=field.name, 231 name=field.name, 232 type=field.spec.type_name, 232 type=field.spec.type_name, 233 classifier=field.spec.c_classi 233 classifier=field.spec.c_classifier, 234 ) 234 ) 235 ) 235 ) 236 236 237 237 238 def emit_struct_encoder(environment: Environme 238 def emit_struct_encoder(environment: Environment, node: _XdrStruct) -> None: 239 """Emit one encoder function for an XDR st 239 """Emit one encoder function for an XDR struct type""" 240 template = get_jinja2_template(environment 240 template = get_jinja2_template(environment, "encoder", "open") 241 print(template.render(name=node.name)) 241 print(template.render(name=node.name)) 242 242 243 for field in node.fields: 243 for field in node.fields: 244 emit_struct_member_encoder(environment 244 emit_struct_member_encoder(environment, field) 245 245 246 template = get_jinja2_template(environment 246 template = get_jinja2_template(environment, "encoder", "close") 247 print(template.render()) 247 print(template.render()) 248 248 249 249 250 class XdrStructGenerator(SourceGenerator): 250 class XdrStructGenerator(SourceGenerator): 251 """Generate source code for XDR structs""" 251 """Generate source code for XDR structs""" 252 252 253 def __init__(self, language: str, peer: st 253 def __init__(self, language: str, peer: str): 254 """Initialize an instance of this clas 254 """Initialize an instance of this class""" 255 self.environment = create_jinja2_envir 255 self.environment = create_jinja2_environment(language, "struct") 256 self.peer = peer 256 self.peer = peer 257 257 258 def emit_declaration(self, node: _XdrStruc 258 def emit_declaration(self, node: _XdrStruct) -> None: 259 """Emit one declaration pair for an XD 259 """Emit one declaration pair for an XDR struct type""" 260 emit_struct_declaration(self.environme 260 emit_struct_declaration(self.environment, node) 261 261 262 def emit_definition(self, node: _XdrStruct 262 def emit_definition(self, node: _XdrStruct) -> None: 263 """Emit one definition for an XDR stru 263 """Emit one definition for an XDR struct type""" 264 emit_struct_definition(self.environmen 264 emit_struct_definition(self.environment, node) 265 265 266 def emit_decoder(self, node: _XdrStruct) - 266 def emit_decoder(self, node: _XdrStruct) -> None: 267 """Emit one decoder function for an XD 267 """Emit one decoder function for an XDR struct type""" 268 emit_struct_decoder(self.environment, 268 emit_struct_decoder(self.environment, node) 269 269 270 def emit_encoder(self, node: _XdrStruct) - 270 def emit_encoder(self, node: _XdrStruct) -> None: 271 """Emit one encoder function for an XD 271 """Emit one encoder function for an XDR struct type""" 272 emit_struct_encoder(self.environment, 272 emit_struct_encoder(self.environment, node)
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.