Chromium Code Reviews| 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 generates Dart APIs from the IDL database.""" | 6 """This module generates Dart APIs from the IDL database.""" |
| 7 | 7 |
| 8 import emitter | 8 import emitter |
| 9 import idlnode | 9 import idlnode |
| 10 import logging | 10 import logging |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 if HasAnnotations(interface): | 179 if HasAnnotations(interface): |
| 180 interface.constants = filter(HasAnnotations, interface.constants) | 180 interface.constants = filter(HasAnnotations, interface.constants) |
| 181 interface.attributes = filter(HasAnnotations, interface.attributes) | 181 interface.attributes = filter(HasAnnotations, interface.attributes) |
| 182 interface.operations = filter(HasAnnotations, interface.operations) | 182 interface.operations = filter(HasAnnotations, interface.operations) |
| 183 interface.parents = filter(HasAnnotations, interface.parents) | 183 interface.parents = filter(HasAnnotations, interface.parents) |
| 184 else: | 184 else: |
| 185 database.DeleteInterface(interface.id) | 185 database.DeleteInterface(interface.id) |
| 186 | 186 |
| 187 self.FilterMembersWithUnidentifiedTypes(database) | 187 self.FilterMembersWithUnidentifiedTypes(database) |
| 188 | 188 |
| 189 def Generate(self, database, system, source_filter=None, super_database=None, | 189 def Generate(self, database, system, super_database=None, webkit_renames={}): |
| 190 common_prefix=None, webkit_renames={}): | |
| 191 self._database = database | 190 self._database = database |
| 192 | 191 |
| 193 # Collect interfaces | 192 # Collect interfaces |
| 194 interfaces = [] | 193 interfaces = [] |
| 195 for interface in database.GetInterfaces(): | 194 for interface in database.GetInterfaces(): |
| 196 if not MatchSourceFilter(source_filter, interface): | 195 if not MatchSourceFilter(interface): |
| 197 # Skip this interface since it's not present in the required source | 196 # Skip this interface since it's not present in the required source |
| 198 _logger.info('Omitting interface - %s' % interface.id) | 197 _logger.info('Omitting interface - %s' % interface.id) |
| 199 continue | 198 continue |
| 200 interfaces.append(interface) | 199 interfaces.append(interface) |
| 201 | 200 |
| 202 # TODO(sra): Use this list of exception names to generate information to | 201 # TODO(sra): Use this list of exception names to generate information to |
| 203 # tell Frog which exceptions can be passed from JS to Dart code. | 202 # tell Frog which exceptions can be passed from JS to Dart code. |
| 204 exceptions = self._CollectExceptions(interfaces) | 203 exceptions = self._CollectExceptions(interfaces) |
| 205 | 204 |
| 206 super_map = dict((v, k) for k, v in webkit_renames.iteritems()) | 205 super_map = dict((v, k) for k, v in webkit_renames.iteritems()) |
| 207 | 206 |
| 208 # Render all interfaces into Dart and save them in files. | 207 # Render all interfaces into Dart and save them in files. |
| 209 for interface in self._PreOrderInterfaces(interfaces): | 208 for interface in self._PreOrderInterfaces(interfaces): |
| 210 | 209 |
| 211 super_interface = None | |
| 212 super_name = interface.id | 210 super_name = interface.id |
| 213 | 211 |
| 214 if super_name in super_map: | 212 if super_name in super_map: |
| 215 super_name = super_map[super_name] | 213 super_name = super_map[super_name] |
| 216 | 214 |
| 217 if (super_database is not None and | 215 if (super_database is not None and |
| 218 super_database.HasInterface(super_name)): | 216 super_database.HasInterface(super_name)): |
| 219 super_interface = super_name | 217 interface.ext_attrs['synthesizedSuperInterfaceName'] = super_name |
|
Anton Muhin
2012/06/27 18:13:55
I am really uneasy about this method. I can under
podivilov
2012/06/27 18:48:07
It's a bit hacky, but synthesized annotations fit
| |
| 220 | 218 |
| 221 interface_name = interface.id | 219 interface_name = interface.id |
| 222 auxiliary_file = self._auxiliary_files.get(interface_name) | 220 auxiliary_file = self._auxiliary_files.get(interface_name) |
| 223 if auxiliary_file is not None: | 221 if auxiliary_file is not None: |
| 224 _logger.info('Skipping %s because %s exists' % ( | 222 _logger.info('Skipping %s because %s exists' % ( |
| 225 interface_name, auxiliary_file)) | 223 interface_name, auxiliary_file)) |
| 226 continue | 224 continue |
| 227 | 225 |
| 228 info = RecognizeCallback(interface) | 226 info = RecognizeCallback(interface) |
| 229 if info: | 227 if info: |
| 230 system.ProcessCallback(interface, info) | 228 system.ProcessCallback(interface, info) |
| 231 else: | 229 else: |
| 232 if 'Callback' in interface.ext_attrs: | 230 if 'Callback' in interface.ext_attrs: |
| 233 _logger.info('Malformed callback: %s' % interface.id) | 231 _logger.info('Malformed callback: %s' % interface.id) |
| 234 self._ProcessInterface(system, interface, super_interface, | 232 _logger.info('Generating %s' % interface.id) |
| 235 source_filter, common_prefix) | 233 system.ProcessInterface(interface) |
| 236 | 234 |
| 237 system.GenerateLibraries() | 235 system.GenerateLibraries() |
| 238 system.Finish() | 236 system.Finish() |
| 239 | 237 |
| 240 def _PreOrderInterfaces(self, interfaces): | 238 def _PreOrderInterfaces(self, interfaces): |
| 241 """Returns the interfaces in pre-order, i.e. parents first.""" | 239 """Returns the interfaces in pre-order, i.e. parents first.""" |
| 242 seen = set() | 240 seen = set() |
| 243 ordered = [] | 241 ordered = [] |
| 244 def visit(interface): | 242 def visit(interface): |
| 245 if interface.id in seen: | 243 if interface.id in seen: |
| 246 return | 244 return |
| 247 seen.add(interface.id) | 245 seen.add(interface.id) |
| 248 for parent in interface.parents: | 246 for parent in interface.parents: |
| 249 if IsDartCollectionType(parent.type.id): | 247 if IsDartCollectionType(parent.type.id): |
| 250 continue | 248 continue |
| 251 if self._database.HasInterface(parent.type.id): | 249 if self._database.HasInterface(parent.type.id): |
| 252 parent_interface = self._database.GetInterface(parent.type.id) | 250 parent_interface = self._database.GetInterface(parent.type.id) |
| 253 visit(parent_interface) | 251 visit(parent_interface) |
| 254 ordered.append(interface) | 252 ordered.append(interface) |
| 255 | 253 |
| 256 for interface in interfaces: | 254 for interface in interfaces: |
| 257 visit(interface) | 255 visit(interface) |
| 258 return ordered | 256 return ordered |
| 259 | 257 |
| 260 | |
| 261 def _ProcessInterface(self, system, interface, super_interface_name, | |
| 262 source_filter, | |
| 263 common_prefix): | |
| 264 """.""" | |
| 265 _logger.info('Generating %s' % interface.id) | |
| 266 | |
| 267 generator = system.InterfaceGenerator(interface, | |
| 268 common_prefix, | |
| 269 super_interface_name, | |
| 270 source_filter) | |
| 271 if not generator: | |
| 272 return | |
| 273 | |
| 274 generator.StartInterface() | |
| 275 generator.AddMembers(interface) | |
| 276 secondary_parents = self._TransitiveSecondaryParents(interface) | |
| 277 generator.AddSecondaryMembers(interface, secondary_parents) | |
| 278 generator.FinishInterface() | |
| 279 | |
| 280 def _TransitiveSecondaryParents(self, interface): | |
| 281 """Returns a list of all non-primary parents. | |
| 282 | |
| 283 The list contains the interface objects for interfaces defined in the | |
| 284 database, and the name for undefined interfaces. | |
| 285 """ | |
| 286 def walk(parents): | |
| 287 for parent in parents: | |
| 288 if IsDartCollectionType(parent.type.id): | |
| 289 result.append(parent.type.id) | |
| 290 continue | |
| 291 if self._database.HasInterface(parent.type.id): | |
| 292 parent_interface = self._database.GetInterface(parent.type.id) | |
| 293 result.append(parent_interface) | |
| 294 walk(parent_interface.parents) | |
| 295 | |
| 296 result = [] | |
| 297 if interface.parents: | |
| 298 parent = interface.parents[0] | |
| 299 if IsPureInterface(parent.type.id): | |
| 300 walk(interface.parents) | |
| 301 else: | |
| 302 walk(interface.parents[1:]) | |
| 303 return result; | |
| 304 | |
| 305 | |
| 306 def _CollectExceptions(self, interfaces): | 258 def _CollectExceptions(self, interfaces): |
| 307 """Returns the names of all exception classes raised.""" | 259 """Returns the names of all exception classes raised.""" |
| 308 exceptions = set() | 260 exceptions = set() |
| 309 for interface in interfaces: | 261 for interface in interfaces: |
| 310 for attribute in interface.attributes: | 262 for attribute in interface.attributes: |
| 311 if attribute.get_raises: | 263 if attribute.get_raises: |
| 312 exceptions.add(attribute.get_raises.id) | 264 exceptions.add(attribute.get_raises.id) |
| 313 if attribute.set_raises: | 265 if attribute.set_raises: |
| 314 exceptions.add(attribute.set_raises.id) | 266 exceptions.add(attribute.set_raises.id) |
| 315 for operation in interface.operations: | 267 for operation in interface.operations: |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 337 | 289 |
| 338 def __init__(self, templates, database, emitters, output_dir): | 290 def __init__(self, templates, database, emitters, output_dir): |
| 339 super(DummyImplementationSystem, self).__init__( | 291 super(DummyImplementationSystem, self).__init__( |
| 340 templates, database, emitters, output_dir) | 292 templates, database, emitters, output_dir) |
| 341 factory_providers_file = os.path.join(self._output_dir, 'src', 'dummy', | 293 factory_providers_file = os.path.join(self._output_dir, 'src', 'dummy', |
| 342 'RegularFactoryProviders.dart') | 294 'RegularFactoryProviders.dart') |
| 343 self._factory_providers_emitter = self._emitters.FileEmitter( | 295 self._factory_providers_emitter = self._emitters.FileEmitter( |
| 344 factory_providers_file) | 296 factory_providers_file) |
| 345 self._impl_file_paths = [factory_providers_file] | 297 self._impl_file_paths = [factory_providers_file] |
| 346 | 298 |
| 347 def InterfaceGenerator(self, | 299 def ProcessInterface(self, interface): |
| 348 interface, | 300 generator = DummyInterfaceGenerator(self, interface) |
|
Anton Muhin
2012/06/27 18:13:55
nit: make in oneliner?
podivilov
2012/06/27 18:48:07
Done.
| |
| 349 common_prefix, | 301 generator.Generate() |
| 350 super_interface_name, | |
| 351 source_filter): | |
| 352 return DummyInterfaceGenerator(self, interface) | |
| 353 | 302 |
| 354 def ProcessCallback(self, interface, info): | 303 def ProcessCallback(self, interface, info): |
| 355 pass | 304 pass |
| 356 | 305 |
| 357 def GenerateLibraries(self): | 306 def GenerateLibraries(self): |
| 358 # Library generated for implementation. | 307 # Library generated for implementation. |
| 359 self._GenerateLibFile( | 308 self._GenerateLibFile( |
| 360 'dom_dummy.darttemplate', | 309 'dom_dummy.darttemplate', |
| 361 os.path.join(self._output_dir, 'dom_dummy.dart'), | 310 os.path.join(self._output_dir, 'dom_dummy.dart'), |
| 362 (self._interface_system._dart_interface_file_paths + | 311 (self._interface_system._dart_interface_file_paths + |
| 363 self._interface_system._dart_callback_file_paths + | 312 self._interface_system._dart_callback_file_paths + |
| 364 self._impl_file_paths)) | 313 self._impl_file_paths)) |
| 365 | 314 |
| 366 | 315 |
| 367 # ------------------------------------------------------------------------------ | 316 # ------------------------------------------------------------------------------ |
| 368 | 317 |
| 369 class DummyInterfaceGenerator(systembase.BaseGenerator): | 318 class DummyInterfaceGenerator(systembase.BaseGenerator): |
| 370 """Generates dummy implementation.""" | 319 """Generates dummy implementation.""" |
| 371 | 320 |
| 372 def __init__(self, system, interface): | 321 def __init__(self, system, interface): |
| 373 super(DummyInterfaceGenerator, self).__init__(system._database) | 322 super(DummyInterfaceGenerator, self).__init__(system._database, interface) |
| 374 self._system = system | 323 self._system = system |
| 375 self._interface = interface | |
| 376 | 324 |
| 377 def StartInterface(self): | 325 def StartInterface(self): |
| 378 # There is no implementation to match the interface, but there might be a | 326 # There is no implementation to match the interface, but there might be a |
| 379 # factory constructor for the Dart interface. | 327 # factory constructor for the Dart interface. |
| 380 constructor_info = AnalyzeConstructor(self._interface) | 328 constructor_info = AnalyzeConstructor(self._interface) |
| 381 if constructor_info: | 329 if constructor_info: |
| 382 dart_interface_name = self._interface.id | 330 dart_interface_name = self._interface.id |
| 383 self._EmitFactoryProvider(dart_interface_name, constructor_info) | 331 self._EmitFactoryProvider(dart_interface_name, constructor_info) |
| 384 | 332 |
| 385 def _EmitFactoryProvider(self, interface_name, constructor_info): | 333 def _EmitFactoryProvider(self, interface_name, constructor_info): |
| 386 factory_provider = '_' + interface_name + 'FactoryProvider' | 334 factory_provider = '_' + interface_name + 'FactoryProvider' |
| 387 self._system._factory_providers_emitter.Emit( | 335 self._system._factory_providers_emitter.Emit( |
| 388 self._system._templates.Load('factoryprovider.darttemplate'), | 336 self._system._templates.Load('factoryprovider.darttemplate'), |
| 389 FACTORYPROVIDER=factory_provider, | 337 FACTORYPROVIDER=factory_provider, |
| 390 CONSTRUCTOR=interface_name, | 338 CONSTRUCTOR=interface_name, |
| 391 PARAMETERS=constructor_info.ParametersImplementationDeclaration()) | 339 PARAMETERS=constructor_info.ParametersImplementationDeclaration()) |
| 392 | 340 |
| 393 def FinishInterface(self): | 341 def FinishInterface(self): |
| 394 pass | 342 pass |
| 395 | 343 |
| 396 def AddTypedArrayConstructors(self, element_type): | 344 def AddTypedArrayConstructors(self, element_type): |
| 397 pass | 345 pass |
| 398 | 346 |
| 399 def AddEventAttributes(self, event_attrs): | 347 def AddEventAttributes(self, event_attrs): |
| 400 pass | 348 pass |
| OLD | NEW |