| OLD | NEW |
| 1 #!/usr/bin/python | 1 #!/usr/bin/python |
| 2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 2 # Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 3 # for details. All rights reserved. Use of this source code is governed by a | 3 # for details. All rights reserved. Use of this source code is governed by a |
| 4 # BSD-style license that can be found in the LICENSE file. | 4 # BSD-style license that can be found in the LICENSE file. |
| 5 | 5 |
| 6 """This module providesfunctionality for systems to generate | 6 """This module providesfunctionality for systems to generate |
| 7 Dart interfaces from the IDL database.""" | 7 Dart interfaces from the IDL database.""" |
| 8 | 8 |
| 9 import os | 9 import os |
| 10 import systembase | 10 import systembase |
| 11 from generator import * | 11 from generator import * |
| 12 | 12 |
| 13 class InterfacesSystem(systembase.System): | 13 class InterfacesSystem(systembase.System): |
| 14 | 14 |
| 15 def __init__(self, templates, database, emitters, output_dir): | 15 def __init__(self, templates, database, emitters, output_dir): |
| 16 super(InterfacesSystem, self).__init__( | 16 super(InterfacesSystem, self).__init__( |
| 17 templates, database, emitters, output_dir) | 17 templates, database, emitters, output_dir) |
| 18 self._dart_interface_file_paths = [] | 18 self._dart_interface_file_paths = [] |
| 19 | 19 |
| 20 | 20 |
| 21 def InterfaceGenerator(self, | 21 def ProcessInterface(self, interface): |
| 22 interface, | |
| 23 common_prefix, | |
| 24 super_interface_name, | |
| 25 source_filter): | |
| 26 """.""" | 22 """.""" |
| 27 interface_name = interface.id | 23 interface_name = interface.id |
| 28 dart_interface_file_path = self._FilePathForDartInterface(interface_name) | 24 dart_interface_file_path = self._FilePathForDartInterface(interface_name) |
| 29 | 25 |
| 30 self._dart_interface_file_paths.append(dart_interface_file_path) | 26 self._dart_interface_file_paths.append(dart_interface_file_path) |
| 31 | 27 |
| 32 dart_interface_code = self._emitters.FileEmitter(dart_interface_file_path) | 28 dart_interface_code = self._emitters.FileEmitter(dart_interface_file_path) |
| 33 | 29 |
| 34 template_file = 'interface_%s.darttemplate' % interface_name | 30 template_file = 'interface_%s.darttemplate' % interface_name |
| 35 template = self._templates.TryLoad(template_file) | 31 template = self._templates.TryLoad(template_file) |
| 36 if not template: | 32 if not template: |
| 37 template = self._templates.Load('interface.darttemplate') | 33 template = self._templates.Load('interface.darttemplate') |
| 38 | 34 |
| 39 return DartInterfaceGenerator( | 35 generator = DartInterfaceGenerator( |
| 40 self, interface, dart_interface_code, | 36 self, interface, dart_interface_code, template) |
| 41 template, | 37 generator.Generate() |
| 42 common_prefix, super_interface_name, | |
| 43 source_filter) | |
| 44 | 38 |
| 45 def ProcessCallback(self, interface, info): | 39 def ProcessCallback(self, interface, info): |
| 46 """Generates a typedef for the callback interface.""" | 40 """Generates a typedef for the callback interface.""" |
| 47 interface_name = interface.id | 41 interface_name = interface.id |
| 48 file_path = self._FilePathForDartInterface(interface_name) | 42 file_path = self._FilePathForDartInterface(interface_name) |
| 49 self._ProcessCallback(interface, info, file_path) | 43 self._ProcessCallback(interface, info, file_path) |
| 50 | 44 |
| 51 def GenerateLibraries(self): | 45 def GenerateLibraries(self): |
| 52 pass | 46 pass |
| 53 | 47 |
| 54 | 48 |
| 55 def _FilePathForDartInterface(self, interface_name): | 49 def _FilePathForDartInterface(self, interface_name): |
| 56 """Returns the file path of the Dart interface definition.""" | 50 """Returns the file path of the Dart interface definition.""" |
| 57 return os.path.join(self._output_dir, 'src', 'interface', | 51 return os.path.join(self._output_dir, 'src', 'interface', |
| 58 '%s.dart' % interface_name) | 52 '%s.dart' % interface_name) |
| 59 | 53 |
| 60 # ------------------------------------------------------------------------------ | 54 # ------------------------------------------------------------------------------ |
| 61 | 55 |
| 62 class DartInterfaceGenerator(systembase.BaseGenerator): | 56 class DartInterfaceGenerator(systembase.BaseGenerator): |
| 63 """Generates Dart Interface definition for one DOM IDL interface.""" | 57 """Generates Dart Interface definition for one DOM IDL interface.""" |
| 64 | 58 |
| 65 def __init__(self, system, interface, emitter, template, | 59 def __init__(self, system, interface, emitter, template): |
| 66 common_prefix, super_interface, source_filter): | |
| 67 """Generates Dart code for the given interface. | 60 """Generates Dart code for the given interface. |
| 68 | 61 |
| 69 Args: | 62 Args: |
| 70 interface -- an IDLInterface instance. It is assumed that all types have | 63 interface -- an IDLInterface instance. It is assumed that all types have |
| 71 been converted to Dart types (e.g. int, String), unless they are in the | 64 been converted to Dart types (e.g. int, String), unless they are in the |
| 72 same package as the interface. | 65 same package as the interface. |
| 73 common_prefix -- the prefix for the common library, if any. | |
| 74 super_interface -- the name of the common interface that this interface | 66 super_interface -- the name of the common interface that this interface |
| 75 implements, if any. | 67 implements, if any. |
| 76 source_filter -- if specified, rewrites the names of any superinterfaces | |
| 77 that are not from these sources to use the common prefix. | |
| 78 """ | 68 """ |
| 79 super(DartInterfaceGenerator, self).__init__(system._database) | 69 super(DartInterfaceGenerator, self).__init__(system._database, interface) |
| 80 self._system = system | 70 self._system = system |
| 81 self._interface = interface | |
| 82 self._emitter = emitter | 71 self._emitter = emitter |
| 83 self._template = template | 72 self._template = template |
| 84 self._common_prefix = common_prefix | 73 self._super_interface = interface.ext_attrs.get( |
| 85 self._super_interface = super_interface | 74 'synthesizedSuperInterfaceName', None) |
| 86 self._source_filter = source_filter | |
| 87 | |
| 88 | 75 |
| 89 def StartInterface(self): | 76 def StartInterface(self): |
| 90 if self._super_interface: | 77 if self._super_interface: |
| 91 typename = self._super_interface | 78 typename = self._super_interface |
| 92 else: | 79 else: |
| 93 typename = self._interface.id | 80 typename = self._interface.id |
| 94 | 81 |
| 95 | 82 |
| 96 extends = [] | 83 extends = [] |
| 97 suppressed_extends = [] | 84 suppressed_extends = [] |
| 98 | 85 |
| 99 for parent in self._interface.parents: | 86 for parent in self._interface.parents: |
| 100 # TODO(vsm): Remove source_filter. | 87 # TODO(vsm): Remove source_filter. |
| 101 if MatchSourceFilter(self._source_filter, parent): | 88 if MatchSourceFilter(parent): |
| 102 # Parent is a DOM type. | 89 # Parent is a DOM type. |
| 103 extends.append(DartType(parent.type.id)) | 90 extends.append(DartType(parent.type.id)) |
| 104 elif '<' in parent.type.id: | 91 elif '<' in parent.type.id: |
| 105 # Parent is a Dart collection type. | 92 # Parent is a Dart collection type. |
| 106 # TODO(vsm): Make this check more robust. | 93 # TODO(vsm): Make this check more robust. |
| 107 extends.append(parent.type.id) | 94 extends.append(parent.type.id) |
| 108 else: | 95 else: |
| 109 suppressed_extends.append('%s.%s' % | 96 suppressed_extends.append('%s.%s' % |
| 110 (self._common_prefix, parent.type.id)) | 97 (self._common_prefix, parent.type.id)) |
| 111 | 98 |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 """ | 191 """ |
| 205 Arguments: | 192 Arguments: |
| 206 operations - contains the overloads, one or more operations with the same | 193 operations - contains the overloads, one or more operations with the same |
| 207 name. | 194 name. |
| 208 """ | 195 """ |
| 209 self._members_emitter.Emit('\n' | 196 self._members_emitter.Emit('\n' |
| 210 ' $TYPE $NAME($PARAMS);\n', | 197 ' $TYPE $NAME($PARAMS);\n', |
| 211 TYPE=info.type_name, | 198 TYPE=info.type_name, |
| 212 NAME=info.name, | 199 NAME=info.name, |
| 213 PARAMS=info.ParametersInterfaceDeclaration()) | 200 PARAMS=info.ParametersInterfaceDeclaration()) |
| OLD | NEW |