| 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 provides base functionality for systems to generate | |
| 7 Dart APIs from the IDL database.""" | |
| 8 | |
| 9 import os | |
| 10 from generator import * | |
| 11 | |
| 12 class BaseGenerator(object): | |
| 13 def __init__(self, database, type_registry, interface): | |
| 14 self._database = database | |
| 15 self._type_registry = type_registry | |
| 16 self._interface = interface | |
| 17 | |
| 18 def Generate(self): | |
| 19 if 'Callback' in self._interface.ext_attrs: | |
| 20 handlers = [operation for operation in self._interface.operations | |
| 21 if operation.id == 'handleEvent'] | |
| 22 info = AnalyzeOperation(self._interface, handlers) | |
| 23 self.GenerateCallback(info) | |
| 24 return | |
| 25 | |
| 26 self.StartInterface() | |
| 27 self.AddMembers(self._interface) | |
| 28 self.AddSecondaryMembers(self._interface) | |
| 29 self.FinishInterface() | |
| 30 | |
| 31 def GenerateCallback(self, info): | |
| 32 pass | |
| 33 | |
| 34 def AddMembers(self, interface): | |
| 35 for const in sorted(interface.constants, ConstantOutputOrder): | |
| 36 self.AddConstant(const) | |
| 37 | |
| 38 for attr in sorted(interface.attributes, ConstantOutputOrder): | |
| 39 if attr.type.id != 'EventListener': | |
| 40 self.AddAttribute(attr) | |
| 41 | |
| 42 # The implementation should define an indexer if the interface directly | |
| 43 # extends List. | |
| 44 (element_type, requires_indexer) = ListImplementationInfo( | |
| 45 interface, self._database) | |
| 46 if element_type: | |
| 47 if requires_indexer: | |
| 48 self.AddIndexer(element_type) | |
| 49 else: | |
| 50 self.AmendIndexer(element_type) | |
| 51 # Group overloaded operations by id | |
| 52 operationsById = {} | |
| 53 for operation in interface.operations: | |
| 54 if operation.id not in operationsById: | |
| 55 operationsById[operation.id] = [] | |
| 56 operationsById[operation.id].append(operation) | |
| 57 | |
| 58 # Generate operations | |
| 59 for id in sorted(operationsById.keys()): | |
| 60 operations = operationsById[id] | |
| 61 info = AnalyzeOperation(interface, operations) | |
| 62 self.AddOperation(info) | |
| 63 | |
| 64 def AddSecondaryMembers(self, interface): | |
| 65 # With multiple inheritance, attributes and operations of non-first | |
| 66 # interfaces need to be added. Sometimes the attribute or operation is | |
| 67 # defined in the current interface as well as a parent. In that case we | |
| 68 # avoid making a duplicate definition and pray that the signatures match. | |
| 69 secondary_parents = self._TransitiveSecondaryParents(interface) | |
| 70 for parent_interface in secondary_parents: | |
| 71 if isinstance(parent_interface, str): # IsDartCollectionType(parent_inter
face) | |
| 72 continue | |
| 73 for attr in sorted(parent_interface.attributes, ConstantOutputOrder): | |
| 74 if not FindMatchingAttribute(interface, attr): | |
| 75 self.AddSecondaryAttribute(parent_interface, attr) | |
| 76 | |
| 77 # Group overloaded operations by id | |
| 78 operationsById = {} | |
| 79 for operation in parent_interface.operations: | |
| 80 if operation.id not in operationsById: | |
| 81 operationsById[operation.id] = [] | |
| 82 operationsById[operation.id].append(operation) | |
| 83 | |
| 84 # Generate operations | |
| 85 for id in sorted(operationsById.keys()): | |
| 86 if not any(op.id == id for op in interface.operations): | |
| 87 operations = operationsById[id] | |
| 88 info = AnalyzeOperation(interface, operations) | |
| 89 self.AddSecondaryOperation(parent_interface, info) | |
| 90 | |
| 91 def AddConstant(self, constant): | |
| 92 pass | |
| 93 | |
| 94 def AddAttribute(self, attribute): | |
| 95 pass | |
| 96 | |
| 97 def AddIndexer(self, element_type): | |
| 98 pass | |
| 99 | |
| 100 def AmendIndexer(self, element_type): | |
| 101 pass | |
| 102 | |
| 103 def AddOperation(self, info): | |
| 104 pass | |
| 105 | |
| 106 def AddSecondaryAttribute(self, interface, attribute): | |
| 107 pass | |
| 108 | |
| 109 def AddSecondaryOperation(self, interface, info): | |
| 110 pass | |
| 111 | |
| 112 def _TransitiveSecondaryParents(self, interface): | |
| 113 """Returns a list of all non-primary parents. | |
| 114 | |
| 115 The list contains the interface objects for interfaces defined in the | |
| 116 database, and the name for undefined interfaces. | |
| 117 """ | |
| 118 def walk(parents): | |
| 119 for parent in parents: | |
| 120 if IsDartCollectionType(parent.type.id): | |
| 121 result.append(parent.type.id) | |
| 122 continue | |
| 123 if self._database.HasInterface(parent.type.id): | |
| 124 parent_interface = self._database.GetInterface(parent.type.id) | |
| 125 result.append(parent_interface) | |
| 126 walk(parent_interface.parents) | |
| 127 | |
| 128 result = [] | |
| 129 if interface.parents: | |
| 130 parent = interface.parents[0] | |
| 131 if IsPureInterface(parent.type.id): | |
| 132 walk(interface.parents) | |
| 133 else: | |
| 134 walk(interface.parents[1:]) | |
| 135 return result | |
| 136 | |
| 137 def _TypeInfo(self, type_name): | |
| 138 return self._type_registry.TypeInfo(type_name) | |
| 139 | |
| 140 def _DartType(self, type_name): | |
| 141 return self._type_registry.DartType(type_name) | |
| 142 | |
| 143 | |
| 144 class GeneratorOptions(object): | |
| 145 def __init__(self, templates, database, type_registry, renamer): | |
| 146 self.templates = templates | |
| 147 self.database = database | |
| 148 self.type_registry = type_registry | |
| 149 self.renamer = renamer | |
| 150 | |
| 151 | |
| 152 def IsReadOnly(attribute): | |
| 153 return attribute.is_read_only or 'Replaceable' in attribute.ext_attrs | |
| OLD | NEW |