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