| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/python | |
| 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 | |
| 4 # BSD-style license that can be found in the LICENSE file. | |
| 5 | |
| 6 """This module providesfunctionality for systems to generate | |
| 7 Dart interfaces from the IDL database.""" | |
| 8 | |
| 9 import os | |
| 10 import systembase | |
| 11 from generator import * | |
| 12 | |
| 13 class InterfacesSystem(systembase.System): | |
| 14 | |
| 15 def __init__(self, options): | |
| 16 super(InterfacesSystem, self).__init__(options) | |
| 17 self._dart_interface_file_paths = [] | |
| 18 | |
| 19 | |
| 20 def ProcessInterface(self, interface): | |
| 21 """.""" | |
| 22 interface_name = interface.id | |
| 23 dart_interface_file_path = self._FilePathForDartInterface(interface_name) | |
| 24 | |
| 25 self._dart_interface_file_paths.append(dart_interface_file_path) | |
| 26 | |
| 27 dart_interface_code = self._emitters.FileEmitter(dart_interface_file_path) | |
| 28 | |
| 29 template_file = 'interface_%s.darttemplate' % interface_name | |
| 30 template = self._templates.TryLoad(template_file) | |
| 31 if not template: | |
| 32 template = self._templates.Load('interface.darttemplate') | |
| 33 | |
| 34 DartInterfaceGenerator( | |
| 35 self, interface, dart_interface_code, template).Generate() | |
| 36 | |
| 37 def ProcessCallback(self, interface, info): | |
| 38 """Generates a typedef for the callback interface.""" | |
| 39 interface_name = interface.id | |
| 40 file_path = self._FilePathForDartInterface(interface_name) | |
| 41 self._ProcessCallback(interface, info, file_path) | |
| 42 | |
| 43 def GenerateLibraries(self): | |
| 44 pass | |
| 45 | |
| 46 | |
| 47 def _FilePathForDartInterface(self, interface_name): | |
| 48 """Returns the file path of the Dart interface definition.""" | |
| 49 return os.path.join(self._output_dir, 'src', 'interface', | |
| 50 '%s.dart' % interface_name) | |
| 51 | |
| 52 # ------------------------------------------------------------------------------ | |
| 53 | |
| 54 class DartInterfaceGenerator(systembase.BaseGenerator): | |
| 55 """Generates Dart Interface definition for one DOM IDL interface.""" | |
| 56 | |
| 57 def __init__(self, system, interface, emitter, template): | |
| 58 """Generates Dart code for the given interface. | |
| 59 | |
| 60 Args: | |
| 61 interface -- an IDLInterface instance. It is assumed that all types have | |
| 62 been converted to Dart types (e.g. int, String), unless they are in the | |
| 63 same package as the interface. | |
| 64 super_interface -- the name of the common interface that this interface | |
| 65 implements, if any. | |
| 66 """ | |
| 67 super(DartInterfaceGenerator, self).__init__(system._database, interface) | |
| 68 self._system = system | |
| 69 self._emitter = emitter | |
| 70 self._template = template | |
| 71 self._super_interface = interface.ext_attrs.get( | |
| 72 'synthesizedSuperInterfaceName', None) | |
| 73 | |
| 74 def StartInterface(self): | |
| 75 if self._super_interface: | |
| 76 typename = self._super_interface | |
| 77 else: | |
| 78 typename = self._interface.id | |
| 79 | |
| 80 | |
| 81 extends = [] | |
| 82 suppressed_extends = [] | |
| 83 | |
| 84 for parent in self._interface.parents: | |
| 85 # TODO(vsm): Remove source_filter. | |
| 86 if MatchSourceFilter(parent): | |
| 87 # Parent is a DOM type. | |
| 88 extends.append(self._DartType(parent.type.id)) | |
| 89 elif '<' in parent.type.id: | |
| 90 # Parent is a Dart collection type. | |
| 91 # TODO(vsm): Make this check more robust. | |
| 92 extends.append(parent.type.id) | |
| 93 else: | |
| 94 suppressed_extends.append('%s.%s' % | |
| 95 (self._common_prefix, parent.type.id)) | |
| 96 | |
| 97 comment = ' extends' | |
| 98 extends_str = '' | |
| 99 if extends: | |
| 100 extends_str += ' extends ' + ', '.join(extends) | |
| 101 comment = ',' | |
| 102 if suppressed_extends: | |
| 103 extends_str += ' /*%s %s */' % (comment, ', '.join(suppressed_extends)) | |
| 104 | |
| 105 factory_provider = None | |
| 106 constructor_info = AnalyzeConstructor(self._interface) | |
| 107 if constructor_info: | |
| 108 factory_provider = '_' + typename + 'FactoryProvider' | |
| 109 | |
| 110 if typename in interface_factories: | |
| 111 factory_provider = interface_factories[typename] | |
| 112 | |
| 113 if factory_provider: | |
| 114 extends_str += ' default ' + factory_provider | |
| 115 | |
| 116 # TODO(vsm): Add appropriate package / namespace syntax. | |
| 117 (self._members_emitter, | |
| 118 self._top_level_emitter) = self._emitter.Emit( | |
| 119 self._template + '$!TOP_LEVEL', | |
| 120 ID=typename, | |
| 121 EXTENDS=extends_str) | |
| 122 | |
| 123 if constructor_info: | |
| 124 self._members_emitter.Emit( | |
| 125 '\n' | |
| 126 ' $CTOR($PARAMS);\n', | |
| 127 CTOR=typename, | |
| 128 PARAMS=constructor_info.ParametersInterfaceDeclaration(self._DartType)
) | |
| 129 | |
| 130 element_type = MaybeTypedArrayElementTypeInHierarchy( | |
| 131 self._interface, self._system._database) | |
| 132 if element_type: | |
| 133 self._members_emitter.Emit( | |
| 134 '\n' | |
| 135 ' $CTOR(int length);\n' | |
| 136 '\n' | |
| 137 ' $CTOR.fromList(List<$TYPE> list);\n' | |
| 138 '\n' | |
| 139 ' $CTOR.fromBuffer(ArrayBuffer buffer,' | |
| 140 ' [int byteOffset, int length]);\n', | |
| 141 CTOR=self._interface.id, | |
| 142 TYPE=self._DartType(element_type)) | |
| 143 | |
| 144 | |
| 145 def FinishInterface(self): | |
| 146 # TODO(vsm): Use typedef if / when that is supported in Dart. | |
| 147 # Define variant as subtype. | |
| 148 if (self._super_interface and | |
| 149 self._interface.id is not self._super_interface): | |
| 150 consts_emitter = self._top_level_emitter.Emit( | |
| 151 '\n' | |
| 152 'interface $NAME extends $BASE {\n' | |
| 153 '$!CONSTS' | |
| 154 '}\n', | |
| 155 NAME=self._interface.id, | |
| 156 BASE=self._super_interface) | |
| 157 for const in sorted(self._interface.constants, ConstantOutputOrder): | |
| 158 self._EmitConstant(consts_emitter, const) | |
| 159 | |
| 160 def AddConstant(self, constant): | |
| 161 if (not self._super_interface or | |
| 162 self._interface.id is self._super_interface): | |
| 163 self._EmitConstant(self._members_emitter, constant) | |
| 164 | |
| 165 def _EmitConstant(self, emitter, constant): | |
| 166 emitter.Emit('\n static const $TYPE$NAME = $VALUE;\n', | |
| 167 NAME=constant.id, | |
| 168 TYPE=TypeOrNothing(self._DartType(constant.type.id), | |
| 169 constant.type.id), | |
| 170 VALUE=constant.value) | |
| 171 | |
| 172 def AddAttribute(self, attribute): | |
| 173 getter = attribute | |
| 174 setter = attribute if not systembase.IsReadOnly(attribute) else None | |
| 175 if getter and setter and getter.type.id == setter.type.id: | |
| 176 self._members_emitter.Emit('\n $TYPE $NAME;\n', | |
| 177 NAME=DartDomNameOfAttribute(getter), | |
| 178 TYPE=TypeOrVar(self._DartType(getter.type.id), | |
| 179 getter.type.id)) | |
| 180 return | |
| 181 if getter and not setter: | |
| 182 self._members_emitter.Emit('\n final $TYPE$NAME;\n', | |
| 183 NAME=DartDomNameOfAttribute(getter), | |
| 184 TYPE=TypeOrNothing(self._DartType(getter.type.i
d), | |
| 185 getter.type.id)) | |
| 186 return | |
| 187 raise Exception('Unexpected getter/setter combination %s %s' % | |
| 188 (getter, setter)) | |
| 189 | |
| 190 def AddOperation(self, info): | |
| 191 """ | |
| 192 Arguments: | |
| 193 operations - contains the overloads, one or more operations with the same | |
| 194 name. | |
| 195 """ | |
| 196 self._members_emitter.Emit('\n' | |
| 197 ' $TYPE $NAME($PARAMS);\n', | |
| 198 TYPE=self._DartType(info.type_name), | |
| 199 NAME=info.name, | |
| 200 PARAMS=info.ParametersInterfaceDeclaration(self._
DartType)) | |
| OLD | NEW |