Chromium Code Reviews

Side by Side Diff: mojo/public/tools/bindings/pylib/mojom/generate/mojom_translator.py

Issue 1824263002: Mojom backend: Stop re-computing version info and field packing data. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
OLDNEW
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 # Copyright 2015 The Chromium Authors. All rights reserved. 2 # Copyright 2015 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 # 5 #
6 # This module is responsible for translating a MojomFileGraph (see 6 # This module is responsible for translating a MojomFileGraph (see
7 # mojom_files.mojom) to one or more module.Module. 7 # mojom_files.mojom) to one or more module.Module.
8 # 8 #
9 # This module takes the output of the mojom parser, a MojomFileGraph and 9 # This module takes the output of the mojom parser, a MojomFileGraph and
10 # translates it to the input of the code generators, a module.Module object. 10 # translates it to the input of the code generators, a module.Module object.
(...skipping 11 matching lines...)
22 # NOTE: This module assumes that the python path contains the generated modules 22 # NOTE: This module assumes that the python path contains the generated modules
23 # for mojom_files.mojom and mojom_types.mojom as well as their dependencies. 23 # for mojom_files.mojom and mojom_types.mojom as well as their dependencies.
24 # It is the responsibility of the module's loader to handle this. 24 # It is the responsibility of the module's loader to handle this.
25 25
26 import os 26 import os
27 27
28 from generated import mojom_files_mojom 28 from generated import mojom_files_mojom
29 from generated import mojom_types_mojom 29 from generated import mojom_types_mojom
30 import module 30 import module
31 import operator 31 import operator
32 import pack
32 33
33 34
34 class FileTranslator(object): 35 class FileTranslator(object):
35 """FileTranslator translates a MojomFile to a module.Module.""" 36 """FileTranslator translates a MojomFile to a module.Module."""
36 def __init__(self, graph, file_name): 37 def __init__(self, graph, file_name):
37 """Initializes a FileTranslator. 38 """Initializes a FileTranslator.
38 39
39 Args: 40 Args:
40 graph: {mojom_files_mojom.MojomFileGraph} containing the file to be 41 graph: {mojom_files_mojom.MojomFileGraph} containing the file to be
41 translated. 42 translated.
(...skipping 143 matching lines...)
185 def StructFromMojom(self, struct, mojom_type): 186 def StructFromMojom(self, struct, mojom_type):
186 """Populates a module.Struct based on a MojomStruct. 187 """Populates a module.Struct based on a MojomStruct.
187 188
188 Args: 189 Args:
189 struct: {module.Struct} to be populated. 190 struct: {module.Struct} to be populated.
190 mojom_type: {UserDefinedType} referring to the MojomStruct to be 191 mojom_type: {UserDefinedType} referring to the MojomStruct to be
191 translated. 192 translated.
192 """ 193 """
193 assert mojom_type.tag == mojom_types_mojom.UserDefinedType.Tags.struct_type 194 assert mojom_type.tag == mojom_types_mojom.UserDefinedType.Tags.struct_type
194 mojom_struct = mojom_type.struct_type 195 mojom_struct = mojom_type.struct_type
196 self.StructFromMojomStruct(struct, mojom_struct)
197
198 def StructFromMojomStruct(self, struct, mojom_struct):
199 """Populates a module.Struct based on a MojomStruct.
200
201 Args:
202 struct: {module.Struct} to be populated.
203 mojom_struct: {mojom_types.MojomStruct} to be translated.
204 """
195 self.PopulateUserDefinedType(struct, mojom_struct) 205 self.PopulateUserDefinedType(struct, mojom_struct)
196 # mojom_struct.fields is indexed by the field ordinals. We want 206 # mojom_struct.fields is indexed by the field ordinals.
197 # to capture these ordinals but sort struct.fields by declaration_order. 207 struct.fields_in_ordinal_order = [self.StructFieldFromMojom(ordinal, f)
198 struct.fields = [self.StructFieldFromMojom(ordinal, f) for (ordinal, f) in 208 for (ordinal, f) in enumerate(mojom_struct.fields)]
199 enumerate(mojom_struct.fields)] 209 # We also want a list of fields sorted in declaration_order.
210 struct.fields = [f for f in struct.fields_in_ordinal_order]
200 struct.fields.sort(key=lambda field: field.declaration_order) 211 struct.fields.sort(key=lambda field: field.declaration_order)
212
213 assert mojom_struct.version_info
214 struct.versions = [self.VersionInfoFromMojom(version) for version in
215 mojom_struct.version_info]
201 self.PopulateContainedDeclarationsFromMojom( 216 self.PopulateContainedDeclarationsFromMojom(
202 struct, mojom_struct.decl_data.contained_declarations) 217 struct, mojom_struct.decl_data.contained_declarations)
203 218
204 def UnionFieldFromMojom(self, mojom_field): 219 def UnionFieldFromMojom(self, mojom_field):
205 """Translates a mojom_types_mojom.UnionField to a module.UnionField. 220 """Translates a mojom_types_mojom.UnionField to a module.UnionField.
206 221
207 Args: 222 Args:
208 mojom_field: {mojom_types_mojom.UnionField} to be translated. 223 mojom_field: {mojom_types_mojom.UnionField} to be translated.
209 224
210 Returns: 225 Returns:
(...skipping 21 matching lines...)
232 ordinal: {int} The 0-based ordinal position of the field within the 247 ordinal: {int} The 0-based ordinal position of the field within the
233 struct. Note that this is not necessarily the same as the lexical 248 struct. Note that this is not necessarily the same as the lexical
234 order or the packing order. 249 order or the packing order.
235 mojom_field: {mojom_types_mojom.StructField} to be translated. 250 mojom_field: {mojom_types_mojom.StructField} to be translated.
236 251
237 Returns: 252 Returns:
238 {module.StructField} translated from mojom_field. 253 {module.StructField} translated from mojom_field.
239 """ 254 """
240 struct_field = module.StructField() 255 struct_field = module.StructField()
241 self.PopulateCommonFieldValues(struct_field, mojom_field) 256 self.PopulateCommonFieldValues(struct_field, mojom_field)
257 struct_field.computed_offset = mojom_field.offset
258 struct_field.computed_bit = mojom_field.bit
259 struct_field.computed_min_version = mojom_field.min_version
242 # Note that the |ordinal| attribute of |struct_field| records only the 260 # Note that the |ordinal| attribute of |struct_field| records only the
243 # *declared* ordinal and as such is not defined for every field whereas 261 # *declared* ordinal and as such is not defined for every field whereas
244 # the |computed_ordinal| attribute is defined for every field. If 262 # the |computed_ordinal| attribute is defined for every field. If
245 # |ordinal| is defined then it is equal to |computed_ordinal|. 263 # |ordinal| is defined then it is equal to |computed_ordinal|.
246 struct_field.ordinal = self.OrdinalFromMojom(mojom_field) 264 struct_field.ordinal = self.OrdinalFromMojom(mojom_field)
247 struct_field.computed_ordinal = ordinal 265 struct_field.computed_ordinal = ordinal
248 struct_field.declaration_order = mojom_field.decl_data.declaration_order 266 struct_field.declaration_order = mojom_field.decl_data.declaration_order
249 if mojom_field.default_value: 267 if mojom_field.default_value:
250 if (mojom_field.default_value.tag == 268 if (mojom_field.default_value.tag ==
251 mojom_types_mojom.DefaultFieldValue.Tags.default_keyword): 269 mojom_types_mojom.DefaultFieldValue.Tags.default_keyword):
252 struct_field.default = 'default' 270 struct_field.default = 'default'
253 else: 271 else:
254 struct_field.default = self.ValueFromMojom( 272 struct_field.default = self.ValueFromMojom(
255 mojom_field.default_value.value) 273 mojom_field.default_value.value)
256 274
257 return struct_field 275 return struct_field
258 276
277 def VersionInfoFromMojom(self, mojom_version):
278 """Translates a mojom_types_mojom.StructVersion to a pack.VersionInfo
279 Args:
280 mojom_version: {mojom_types_mojom.StructVersion} to be translated.
281
282 Returns:
283 {pack.VersionInfo} translated from |mojom_version|.
284 """
285 return pack.VersionInfo(mojom_version.version_number,
286 mojom_version.num_fields, mojom_version.num_bytes)
287
288
259 def ParamFromMojom(self, mojom): 289 def ParamFromMojom(self, mojom):
260 """Translates a mojom_types_mojom.StructField to a module.Parameter. 290 """Translates a mojom_types_mojom.StructField to a module.Parameter.
261 291
262 The parameters passed to and returned from a method are expressed as a 292 The parameters passed to and returned from a method are expressed as a
263 struct. The struct fields in the struct are the parameters. 293 struct. The struct fields in the struct are the parameters.
264 294
265 Args: 295 Args:
266 mojom: {mojom_types_mojom.StructField} representing a method parameter. 296 mojom: {mojom_types_mojom.StructField} representing a method parameter.
267 297
268 Returns: 298 Returns:
(...skipping 178 matching lines...)
447 477
448 Args: 478 Args:
449 mojom_method: {mojom_types_mojom.MojomMethod} to be translated. 479 mojom_method: {mojom_types_mojom.MojomMethod} to be translated.
450 interface: {module.Interface} the method is a member of. 480 interface: {module.Interface} the method is a member of.
451 481
452 Returns: 482 Returns:
453 {module.Method} translated from mojom_method. 483 {module.Method} translated from mojom_method.
454 """ 484 """
455 method = module.Method(interface, mojom_method.decl_data.short_name) 485 method = module.Method(interface, mojom_method.decl_data.short_name)
456 method.ordinal = mojom_method.ordinal 486 method.ordinal = mojom_method.ordinal
487 method.param_struct = module.Struct()
488 self.StructFromMojomStruct(method.param_struct, mojom_method.parameters)
489 # The name of a synthetic request parameter struct is not guaranteed by
490 # the frontend to be anything in particular so we set the name of the
491 # translated struct to a value that the code generators are expecting.
492 method.param_struct.name = "%s_%s_Params" % (
493 method.interface.name, method.name)
457 method.parameters = [self.ParamFromMojom(param) 494 method.parameters = [self.ParamFromMojom(param)
458 for param in mojom_method.parameters.fields] 495 for param in mojom_method.parameters.fields]
459 if mojom_method.response_params is not None: 496 if mojom_method.response_params is not None:
497 method.response_param_struct = module.Struct()
498 self.StructFromMojomStruct(method.response_param_struct,
499 mojom_method.response_params)
500 # The name of a synthetic response parameter struct is not guaranteed by
501 # the frontend to be anything in particular so we set the name of the
502 # translated struct to a value that the code generators are expecting.
503 method.response_param_struct.name = "%s_%s_ResponseParams" % (
504 method.interface.name, method.name)
460 method.response_parameters = [self.ParamFromMojom(param) 505 method.response_parameters = [self.ParamFromMojom(param)
461 for param in mojom_method.response_params.fields] 506 for param in mojom_method.response_params.fields]
507
508 # Set the min_version attribute on the method.
509 method.min_version=None
510 # TODO(rudominer) For now we parse the "MinVersion" attribute here but
511 # after we add a min_version field to mojom_types_mojom.MojomMethod then
512 # we should take the value from there instead.
513 if method.attributes:
514 method.min_version=method.get('MinVersion')
515
462 return method 516 return method
463 517
464 def ConstantFromValueKey(self, value_key): 518 def ConstantFromValueKey(self, value_key):
465 """Takes a value key into a graph.resolved_values referring to a constant 519 """Takes a value key into a graph.resolved_values referring to a constant
466 and returns the module equivalent. 520 and returns the module equivalent.
467 521
468 Args: 522 Args:
469 value_key: {str} the value key referring to the value to be returned. 523 value_key: {str} the value key referring to the value to be returned.
470 524
471 Returns: 525 Returns:
(...skipping 362 matching lines...)
834 888
835 Args: 889 Args:
836 graph: {mojom_types_mojom.MojomFileGraph} to be translated. 890 graph: {mojom_types_mojom.MojomFileGraph} to be translated.
837 891
838 Return: 892 Return:
839 {dict<str, module.Module>} mapping the file's name to its module.Module 893 {dict<str, module.Module>} mapping the file's name to its module.Module
840 translation for all files in graph.files. 894 translation for all files in graph.files.
841 """ 895 """
842 return {file_name: FileTranslator(graph, file_name).Translate() 896 return {file_name: FileTranslator(graph, file_name).Translate()
843 for file_name in graph.files} 897 for file_name in graph.files}
OLDNEW

Powered by Google App Engine