| OLD | NEW |
| 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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 mod.imports = [] | 66 mod.imports = [] |
| 67 self._transitive_imports = self.GetTransitiveImports(mojom_file) | 67 self._transitive_imports = self.GetTransitiveImports(mojom_file) |
| 68 mod.transitive_imports = self._transitive_imports.values() | 68 mod.transitive_imports = self._transitive_imports.values() |
| 69 if mojom_file.imports: | 69 if mojom_file.imports: |
| 70 mod.imports = [ | 70 mod.imports = [ |
| 71 self._transitive_imports[imp] for imp in mojom_file.imports] | 71 self._transitive_imports[imp] for imp in mojom_file.imports] |
| 72 | 72 |
| 73 if mojom_file.declared_mojom_objects: | 73 if mojom_file.declared_mojom_objects: |
| 74 if mojom_file.declared_mojom_objects.top_level_constants: | 74 if mojom_file.declared_mojom_objects.top_level_constants: |
| 75 mod.constants = [ | 75 mod.constants = [ |
| 76 self.ConstantFromValueKey(key) | 76 self.ConstantFromKey(key) |
| 77 for key in mojom_file.declared_mojom_objects.top_level_constants] | 77 for key in mojom_file.declared_mojom_objects.top_level_constants] |
| 78 | 78 |
| 79 user_defined_types = ['interfaces', 'structs', 'unions'] | 79 user_defined_types = ['interfaces', 'structs', 'unions'] |
| 80 for user_defined_type in user_defined_types: | 80 for user_defined_type in user_defined_types: |
| 81 if getattr(mojom_file.declared_mojom_objects, user_defined_type): | 81 if getattr(mojom_file.declared_mojom_objects, user_defined_type): |
| 82 setattr(mod, user_defined_type, [self.UserDefinedFromTypeKey(key) | 82 setattr(mod, user_defined_type, [self.UserDefinedFromTypeKey(key) |
| 83 for key in getattr( | 83 for key in getattr( |
| 84 mojom_file.declared_mojom_objects, user_defined_type)]) | 84 mojom_file.declared_mojom_objects, user_defined_type)]) |
| 85 if mojom_file.declared_mojom_objects.top_level_enums: | 85 if mojom_file.declared_mojom_objects.top_level_enums: |
| 86 mod.enums = [self.UserDefinedFromTypeKey(key) | 86 mod.enums = [self.UserDefinedFromTypeKey(key) |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 329 if not contained_declarations: | 329 if not contained_declarations: |
| 330 return | 330 return |
| 331 | 331 |
| 332 if contained_declarations.enums: | 332 if contained_declarations.enums: |
| 333 for enum_key in contained_declarations.enums: | 333 for enum_key in contained_declarations.enums: |
| 334 enum = self.UserDefinedFromTypeKey(enum_key) | 334 enum = self.UserDefinedFromTypeKey(enum_key) |
| 335 parent_kind.enums.append(enum) | 335 parent_kind.enums.append(enum) |
| 336 | 336 |
| 337 if contained_declarations.constants: | 337 if contained_declarations.constants: |
| 338 for const_key in contained_declarations.constants: | 338 for const_key in contained_declarations.constants: |
| 339 const = self.ConstantFromValueKey(const_key) | 339 const = self.ConstantFromKey(const_key) |
| 340 parent_kind.constants.append(const) | 340 parent_kind.constants.append(const) |
| 341 | 341 |
| 342 def EnumFromMojom(self, enum, mojom_type): | 342 def EnumFromMojom(self, enum, mojom_type): |
| 343 """Populates a module.Enum based on a MojomEnum. | 343 """Populates a module.Enum based on a MojomEnum. |
| 344 | 344 |
| 345 Args: | 345 Args: |
| 346 enum: {module.Enum} to be populated. | 346 enum: {module.Enum} to be populated. |
| 347 mojom_type: {mojom_types_mojom.Type} referring to the MojomEnum to be | 347 mojom_type: {mojom_types_mojom.Type} referring to the MojomEnum to be |
| 348 translated. | 348 translated. |
| 349 """ | 349 """ |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 504 # translated struct to a value that the code generators are expecting. | 504 # translated struct to a value that the code generators are expecting. |
| 505 method.response_param_struct.name = "%s_%s_ResponseParams" % ( | 505 method.response_param_struct.name = "%s_%s_ResponseParams" % ( |
| 506 method.interface.name, method.name) | 506 method.interface.name, method.name) |
| 507 method.response_parameters = [self.ParamFromMojom(param) | 507 method.response_parameters = [self.ParamFromMojom(param) |
| 508 for param in mojom_method.response_params.fields] | 508 for param in mojom_method.response_params.fields] |
| 509 | 509 |
| 510 method.min_version=mojom_method.min_version | 510 method.min_version=mojom_method.min_version |
| 511 | 511 |
| 512 return method | 512 return method |
| 513 | 513 |
| 514 def ConstantFromValueKey(self, value_key): | 514 def ConstantFromKey(self, constant_key): |
| 515 """Takes a value key into a graph.resolved_values referring to a constant | 515 """Takes a key into the map graph.resolved_constants and returns the module |
| 516 and returns the module equivalent. | 516 equivalent constant. |
| 517 | 517 |
| 518 Args: | 518 Args: |
| 519 value_key: {str} the value key referring to the value to be returned. | 519 constant_key: {str} the key referring to the constant whose translation |
| 520 is to be returned. |
| 520 | 521 |
| 521 Returns: | 522 Returns: |
| 522 {module.Constant} translated. | 523 {module.Constant} translated. |
| 523 """ | 524 """ |
| 524 if value_key in self._constant_cache: | 525 if constant_key in self._constant_cache: |
| 525 return self._constant_cache[value_key] | 526 return self._constant_cache[constant_key] |
| 526 | 527 |
| 527 mojom_const = self._graph.resolved_values[value_key].declared_constant | 528 mojom_const = self._graph.resolved_constants[constant_key] |
| 528 const = module.Constant() | 529 const = module.Constant() |
| 529 self._constant_cache[value_key] = const | 530 self._constant_cache[constant_key] = const |
| 530 | 531 |
| 531 self.ConstantFromMojom(const, mojom_const) | 532 self.ConstantFromMojom(const, mojom_const) |
| 532 return const | 533 return const |
| 533 | 534 |
| 534 def ConstantFromMojom(self, const, mojom_const): | 535 def ConstantFromMojom(self, const, mojom_const): |
| 535 """Populates a module.Constant based on a DeclaredConstant. | 536 """Populates a module.Constant based on a DeclaredConstant. |
| 536 | 537 |
| 537 Args: | 538 Args: |
| 538 const: {module.Constant} to be populated. | 539 const: {module.Constant} to be populated. |
| 539 mojom_const: {mojom_types_mojom.DeclaredConstant} to be translated. | 540 mojom_const: {mojom_types_mojom.DeclaredConstant} to be translated. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 591 'double.NEGATIVE_INFINITY', | 592 'double.NEGATIVE_INFINITY', |
| 592 mojom_types_mojom.BuiltinConstantValue.DOUBLE_NAN: | 593 mojom_types_mojom.BuiltinConstantValue.DOUBLE_NAN: |
| 593 'double.NAN', | 594 'double.NAN', |
| 594 mojom_types_mojom.BuiltinConstantValue.FLOAT_INFINITY: | 595 mojom_types_mojom.BuiltinConstantValue.FLOAT_INFINITY: |
| 595 'float.INFINITY', | 596 'float.INFINITY', |
| 596 mojom_types_mojom.BuiltinConstantValue.FLOAT_NEGATIVE_INFINITY: | 597 mojom_types_mojom.BuiltinConstantValue.FLOAT_NEGATIVE_INFINITY: |
| 597 'float.NEGATIVE_INFINITY', | 598 'float.NEGATIVE_INFINITY', |
| 598 mojom_types_mojom.BuiltinConstantValue.FLOAT_NAN: 'float.NAN', | 599 mojom_types_mojom.BuiltinConstantValue.FLOAT_NAN: 'float.NAN', |
| 599 } | 600 } |
| 600 return module.BuiltinValue(mojom_to_builtin[value.builtin_value]) | 601 return module.BuiltinValue(mojom_to_builtin[value.builtin_value]) |
| 602 return self.FromUserValueReference(value) |
| 601 | 603 |
| 602 assert value.tag == mojom_types_mojom.Value.Tags.user_value_reference | 604 def FromUserValueReference(self, value): |
| 603 return self.UserDefinedFromValueKey(value.user_value_reference.value_key) | 605 """Translates a mojom_types.EnumValueReference or ConstantReference into the |
| 604 | 606 module equivalent. |
| 605 def UserDefinedFromValueKey(self, value_key): | |
| 606 """Takes a value key into graph.resolved_values and returns the module | |
| 607 equivalent. | |
| 608 | 607 |
| 609 Args: | 608 Args: |
| 610 value_key: {str} the value key referring to the value to be returned. | 609 value: {mojom_types_mojom.Value} the value ref to be translated. It |
| 610 must be of type EnumValueReference or ConstantReference. |
| 611 | 611 |
| 612 Returns: | 612 Returns: |
| 613 {module.EnumValue|module.ConstantValue} translated. | 613 {module.EnumValue|module.ConstantValue} translated. |
| 614 """ | 614 """ |
| 615 if value_key in self._value_cache: | 615 if value.tag == mojom_types_mojom.Value.Tags.constant_reference: |
| 616 return self._value_cache[value_key] | 616 return self.ConstantValueFromKey(value.constant_reference.constant_key) |
| 617 assert value.tag == mojom_types_mojom.Value.Tags.enum_value_reference |
| 618 return self.EnumValueFromKey(value.enum_value_reference.enum_type_key, |
| 619 value.enum_value_reference.enum_value_index) |
| 617 | 620 |
| 618 value = self._graph.resolved_values[value_key] | 621 def ConstantValueFromKey(self, constant_key): |
| 619 if value.tag == mojom_types_mojom.UserDefinedValue.Tags.enum_value: | 622 """Takes a key into graph.resolved_constants referring to a |
| 620 return self.EnumValueFromMojom(value.enum_value) | 623 mojom declared_constant and returns a module.ConstantValue referring to the |
| 621 return self.ConstantValueFromValueKey(value_key) | 624 module equivalent constant. |
| 622 | |
| 623 def ConstantValueFromValueKey(self, value_key): | |
| 624 """Takes a value key into graph.resolved_values referring to a declared | |
| 625 constant and returns the module equivalent. | |
| 626 | 625 |
| 627 Args: | 626 Args: |
| 628 value_key: {str} the value key referring to the value to be returned. | 627 constant_key: {str} the constant key referring to a constant. |
| 629 | 628 |
| 630 Returns: | 629 Returns: |
| 631 {module.ConstantValue} translated. | 630 {module.ConstantValue} translated. |
| 632 """ | 631 """ |
| 632 if constant_key in self._value_cache: |
| 633 return self._value_cache[constant_key] |
| 634 |
| 633 const_value = module.ConstantValue() | 635 const_value = module.ConstantValue() |
| 634 self._value_cache[value_key] = const_value | 636 self._value_cache[constant_key] = const_value |
| 635 | 637 |
| 636 const = self.ConstantFromValueKey(value_key) | 638 const = self.ConstantFromKey(constant_key) |
| 637 const_value.constant = const | 639 const_value.constant = const |
| 638 const_value.name = const.name | 640 const_value.name = const.name |
| 639 const_value.parent_kind = const.parent_kind | 641 const_value.parent_kind = const.parent_kind |
| 640 self.PopulateModuleOrImportedFrom(const_value, | 642 self.PopulateModuleOrImportedFrom(const_value, |
| 641 self._graph.resolved_values[value_key].declared_constant) | 643 self._graph.resolved_constants[constant_key]) |
| 642 const_value.namespace = const_value.module.namespace | 644 const_value.namespace = const_value.module.namespace |
| 643 return const_value | 645 return const_value |
| 644 | 646 |
| 645 def EnumValueFromMojom(self, mojom_enum_value): | 647 def EnumValueFromKey(self, enum_type_key, enum_value_index): |
| 646 """Translates an mojom_types_mojom.EnumValue to a module.EnumValue. | 648 """Takes an enum type key and an enum value index (together these |
| 649 form a key to a mojom_enum_value) and returns a module.EnumValue referring |
| 650 the module equivalent enum value |
| 647 | 651 |
| 648 mojom_enum_value: {mojom_types_mojom.EnumValue} to be translated. | 652 Args: |
| 653 enum_type_key: {str} the type key of a mojom_enum |
| 654 enum_value_index: {int} the 0-based index into the |values| array of |
| 655 the mojom_enum |
| 649 | 656 |
| 650 Returns: | 657 Returns: |
| 651 {module.EnumValue} translated from mojom_enum_value. | 658 {module.EnumValue} translated from mojom_enum_value. |
| 652 """ | 659 """ |
| 653 enum_type_key = mojom_enum_value.enum_type_key | 660 enum_value_key = (enum_type_key, enum_value_index) |
| 654 name = mojom_enum_value.decl_data.short_name | 661 if enum_value_key in self._value_cache: |
| 655 value_key = (enum_type_key, name) | 662 return self._value_cache[enum_value_key] |
| 656 if value_key in self._value_cache: | 663 |
| 657 return self._value_cache[value_key] | 664 mojom_enum = self._graph.resolved_types[enum_type_key].enum_type |
| 665 mojom_enum_value = mojom_enum.values[enum_value_index] |
| 658 | 666 |
| 659 # We need to create and cache the EnumValue object just in case later calls | 667 # We need to create and cache the EnumValue object just in case later calls |
| 660 # require the creation of that same EnumValue object. | 668 # require the creation of that same EnumValue object. |
| 661 enum_value = module.EnumValue() | 669 enum_value = module.EnumValue() |
| 662 self._value_cache[value_key] = enum_value | 670 self._value_cache[enum_value_key] = enum_value |
| 663 | 671 |
| 664 enum = self.UserDefinedFromTypeKey(enum_type_key) | 672 enum = self.UserDefinedFromTypeKey(enum_type_key) |
| 665 enum_value.enum = enum | 673 enum_value.enum = enum |
| 666 self.PopulateModuleOrImportedFrom(enum_value, mojom_enum_value) | 674 self.PopulateModuleOrImportedFrom(enum_value, mojom_enum_value) |
| 667 enum_value.namespace = enum_value.module.namespace | 675 enum_value.namespace = enum_value.module.namespace |
| 668 enum_value.parent_kind = enum.parent_kind | 676 enum_value.parent_kind = enum.parent_kind |
| 669 enum_value.name = name | 677 enum_value.name = mojom_enum_value.decl_data.short_name |
| 670 | 678 |
| 671 return enum_value | 679 return enum_value |
| 672 | 680 |
| 673 def KindFromMojom(self, mojom_type): | 681 def KindFromMojom(self, mojom_type): |
| 674 """Translates a mojom_types_mojom.Type to its equivalent module type. | 682 """Translates a mojom_types_mojom.Type to its equivalent module type. |
| 675 | 683 |
| 676 It is guaranteed that two calls to KindFromMojom with the same input yield | 684 It is guaranteed that two calls to KindFromMojom with the same input yield |
| 677 the same object. | 685 the same object. |
| 678 | 686 |
| 679 Args: | 687 Args: |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 884 | 892 |
| 885 Args: | 893 Args: |
| 886 graph: {mojom_types_mojom.MojomFileGraph} to be translated. | 894 graph: {mojom_types_mojom.MojomFileGraph} to be translated. |
| 887 | 895 |
| 888 Return: | 896 Return: |
| 889 {dict<str, module.Module>} mapping the file's name to its module.Module | 897 {dict<str, module.Module>} mapping the file's name to its module.Module |
| 890 translation for all files in graph.files. | 898 translation for all files in graph.files. |
| 891 """ | 899 """ |
| 892 return {file_name: FileTranslator(graph, file_name).Translate() | 900 return {file_name: FileTranslator(graph, file_name).Translate() |
| 893 for file_name in graph.files} | 901 for file_name in graph.files} |
| OLD | NEW |