Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(198)

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

Issue 1847683004: Mojom compiler backend: Stop re-computing interface version numbers and consume method declaration o (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Rebasing. Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
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 436 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 mojom_type: {UserDefinedType} referring to the MojomInterface to be 447 mojom_type: {UserDefinedType} referring to the MojomInterface to be
448 translated. 448 translated.
449 """ 449 """
450 assert (mojom_type.tag 450 assert (mojom_type.tag
451 == mojom_types_mojom.UserDefinedType.Tags.interface_type) 451 == mojom_types_mojom.UserDefinedType.Tags.interface_type)
452 mojom_interface = mojom_type.interface_type 452 mojom_interface = mojom_type.interface_type
453 interface.attributes = self.AttributesFromMojom(mojom_interface) 453 interface.attributes = self.AttributesFromMojom(mojom_interface)
454 self.PopulateModuleOrImportedFrom(interface, mojom_interface) 454 self.PopulateModuleOrImportedFrom(interface, mojom_interface)
455 interface.name = mojom_interface.decl_data.short_name 455 interface.name = mojom_interface.decl_data.short_name
456 interface.spec = interface.name 456 interface.spec = interface.name
457 interface.version = mojom_interface.current_version
457 interface.service_name = mojom_interface.service_name 458 interface.service_name = mojom_interface.service_name
458 if interface.attributes: 459 if interface.attributes:
459 assert interface.service_name == interface.attributes.get( 460 assert interface.service_name == interface.attributes.get(
460 'ServiceName', None), interface.service_name 461 'ServiceName', None), interface.service_name
461 else: 462 else:
462 assert interface.service_name is None, interface.service_name 463 assert interface.service_name is None, interface.service_name
463 464
464 465
465 # Translate the dictionary of methods into a list of module.Methods. 466 # Translate the dictionary of methods into a list of module.Methods.
466 # In order to have a deterministic ordering we sort by method ordinal.
467 # TODO(rudominer) Consider ordering by declaration order instead once
468 # this field is populated by the front-end.
469 interface.methods = [self.MethodFromMojom(mojom_method, interface) 467 interface.methods = [self.MethodFromMojom(mojom_method, interface)
470 for ordinal, mojom_method in sorted(mojom_interface.methods.iteritems(), 468 for ordinal, mojom_method in mojom_interface.methods.iteritems()]
471 key=operator.itemgetter(0))] 469 # We want the methods in an interface to be in some deterministic order
470 # and we choose declaration order (i.e. lexical order within the
471 # .mojom file.)
472 interface.methods.sort(key=lambda method: method.declaration_order)
472 self.PopulateContainedDeclarationsFromMojom( 473 self.PopulateContainedDeclarationsFromMojom(
473 interface, mojom_interface.decl_data.contained_declarations) 474 interface, mojom_interface.decl_data.contained_declarations)
474 475
475 def MethodFromMojom(self, mojom_method, interface): 476 def MethodFromMojom(self, mojom_method, interface):
476 """Translates a mojom_types_mojom.MojomMethod to a module.Method. 477 """Translates a mojom_types_mojom.MojomMethod to a module.Method.
477 478
478 Args: 479 Args:
479 mojom_method: {mojom_types_mojom.MojomMethod} to be translated. 480 mojom_method: {mojom_types_mojom.MojomMethod} to be translated.
480 interface: {module.Interface} the method is a member of. 481 interface: {module.Interface} the method is a member of.
481 482
482 Returns: 483 Returns:
483 {module.Method} translated from mojom_method. 484 {module.Method} translated from mojom_method.
484 """ 485 """
485 method = module.Method(interface, mojom_method.decl_data.short_name) 486 method = module.Method(interface, mojom_method.decl_data.short_name)
486 method.ordinal = mojom_method.ordinal 487 method.ordinal = mojom_method.ordinal
488 method.declaration_order = mojom_method.decl_data.declaration_order
487 method.param_struct = module.Struct() 489 method.param_struct = module.Struct()
488 self.StructFromMojomStruct(method.param_struct, mojom_method.parameters) 490 self.StructFromMojomStruct(method.param_struct, mojom_method.parameters)
489 # The name of a synthetic request parameter struct is not guaranteed by 491 # 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 492 # 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. 493 # translated struct to a value that the code generators are expecting.
492 method.param_struct.name = "%s_%s_Params" % ( 494 method.param_struct.name = "%s_%s_Params" % (
493 method.interface.name, method.name) 495 method.interface.name, method.name)
494 method.parameters = [self.ParamFromMojom(param) 496 method.parameters = [self.ParamFromMojom(param)
495 for param in mojom_method.parameters.fields] 497 for param in mojom_method.parameters.fields]
496 if mojom_method.response_params is not None: 498 if mojom_method.response_params is not None:
497 method.response_param_struct = module.Struct() 499 method.response_param_struct = module.Struct()
498 self.StructFromMojomStruct(method.response_param_struct, 500 self.StructFromMojomStruct(method.response_param_struct,
499 mojom_method.response_params) 501 mojom_method.response_params)
500 # The name of a synthetic response parameter struct is not guaranteed by 502 # 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 503 # 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. 504 # translated struct to a value that the code generators are expecting.
503 method.response_param_struct.name = "%s_%s_ResponseParams" % ( 505 method.response_param_struct.name = "%s_%s_ResponseParams" % (
504 method.interface.name, method.name) 506 method.interface.name, method.name)
505 method.response_parameters = [self.ParamFromMojom(param) 507 method.response_parameters = [self.ParamFromMojom(param)
506 for param in mojom_method.response_params.fields] 508 for param in mojom_method.response_params.fields]
507 509
508 # Set the min_version attribute on the method. 510 method.min_version=mojom_method.min_version
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 511
516 return method 512 return method
517 513
518 def ConstantFromValueKey(self, value_key): 514 def ConstantFromValueKey(self, value_key):
519 """Takes a value key into a graph.resolved_values referring to a constant 515 """Takes a value key into a graph.resolved_values referring to a constant
520 and returns the module equivalent. 516 and returns the module equivalent.
521 517
522 Args: 518 Args:
523 value_key: {str} the value key referring to the value to be returned. 519 value_key: {str} the value key referring to the value to be returned.
524 520
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after
888 884
889 Args: 885 Args:
890 graph: {mojom_types_mojom.MojomFileGraph} to be translated. 886 graph: {mojom_types_mojom.MojomFileGraph} to be translated.
891 887
892 Return: 888 Return:
893 {dict<str, module.Module>} mapping the file's name to its module.Module 889 {dict<str, module.Module>} mapping the file's name to its module.Module
894 translation for all files in graph.files. 890 translation for all files in graph.files.
895 """ 891 """
896 return {file_name: FileTranslator(graph, file_name).Translate() 892 return {file_name: FileTranslator(graph, file_name).Translate()
897 for file_name in graph.files} 893 for file_name in graph.files}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698