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 provides base functionality for systems to generate | 6 """This module provides base functionality for systems to generate |
| 7 Dart APIs from the IDL database.""" | 7 Dart APIs from the IDL database.""" |
| 8 | 8 |
| 9 import os | 9 import os |
| 10 #import re | 10 from generator import * |
|
Anton Muhin
2012/06/06 14:53:41
may we use qualified import here?
| |
| 11 import generator | |
| 12 | 11 |
| 13 def MassagePath(path): | 12 def MassagePath(path): |
| 14 # The most robust way to emit path separators is to use / always. | 13 # The most robust way to emit path separators is to use / always. |
| 15 return path.replace('\\', '/') | 14 return path.replace('\\', '/') |
| 16 | 15 |
| 17 class System(object): | 16 class System(object): |
| 18 """A System generates all the files for one implementation. | 17 """A System generates all the files for one implementation. |
| 19 | 18 |
| 20 This is a base class for all the specific systems. | 19 This is a base class for all the specific systems. |
| 21 The life-cycle of a System is: | 20 The life-cycle of a System is: |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 | 88 |
| 90 | 89 |
| 91 def _BaseDefines(self, interface): | 90 def _BaseDefines(self, interface): |
| 92 """Returns a set of names (strings) for members defined in a base class. | 91 """Returns a set of names (strings) for members defined in a base class. |
| 93 """ | 92 """ |
| 94 def WalkParentChain(interface): | 93 def WalkParentChain(interface): |
| 95 if interface.parents: | 94 if interface.parents: |
| 96 # Only consider primary parent, secondary parents are not on the | 95 # Only consider primary parent, secondary parents are not on the |
| 97 # implementation class inheritance chain. | 96 # implementation class inheritance chain. |
| 98 parent = interface.parents[0] | 97 parent = interface.parents[0] |
| 99 if generator.IsDartCollectionType(parent.type.id): | 98 if IsDartCollectionType(parent.type.id): |
| 100 return | 99 return |
| 101 if self._database.HasInterface(parent.type.id): | 100 if self._database.HasInterface(parent.type.id): |
| 102 parent_interface = self._database.GetInterface(parent.type.id) | 101 parent_interface = self._database.GetInterface(parent.type.id) |
| 103 for attr in parent_interface.attributes: | 102 for attr in parent_interface.attributes: |
| 104 result.add(attr.id) | 103 result.add(attr.id) |
| 105 for op in parent_interface.operations: | 104 for op in parent_interface.operations: |
| 106 result.add(op.id) | 105 result.add(op.id) |
| 107 WalkParentChain(parent_interface) | 106 WalkParentChain(parent_interface) |
| 108 | 107 |
| 109 result = set() | 108 result = set() |
| 110 WalkParentChain(interface) | 109 WalkParentChain(interface) |
| 111 return result; | 110 return result; |
| 111 | |
| 112 class BaseGenerator(object): | |
|
Anton Muhin
2012/06/06 14:53:41
just a move, should I double check?
| |
| 113 def __init__(self, database): | |
| 114 self._database = database | |
| 115 | |
| 116 def AddMembers(self, interface): | |
| 117 for const in sorted(interface.constants, ConstantOutputOrder): | |
| 118 self.AddConstant(const) | |
| 119 | |
| 120 attributes = [attr for attr in interface.attributes | |
| 121 if attr.type.id != 'EventListener'] | |
| 122 for (getter, setter) in _PairUpAttributes(attributes): | |
| 123 self.AddAttribute(getter, setter) | |
| 124 | |
| 125 # The implementation should define an indexer if the interface directly | |
| 126 # extends List. | |
| 127 (element_type, requires_indexer) = ListImplementationInfo( | |
| 128 interface, self._database) | |
| 129 if element_type: | |
| 130 if requires_indexer: | |
| 131 self.AddIndexer(element_type) | |
| 132 else: | |
| 133 self.AmendIndexer(element_type) | |
| 134 # Group overloaded operations by id | |
| 135 operationsById = {} | |
| 136 for operation in interface.operations: | |
| 137 if operation.id not in operationsById: | |
| 138 operationsById[operation.id] = [] | |
| 139 operationsById[operation.id].append(operation) | |
| 140 | |
| 141 # Generate operations | |
| 142 for id in sorted(operationsById.keys()): | |
| 143 operations = operationsById[id] | |
| 144 info = AnalyzeOperation(interface, operations) | |
| 145 if info.IsStatic(): | |
| 146 self.AddStaticOperation(info) | |
| 147 else: | |
| 148 self.AddOperation(info) | |
| 149 | |
| 150 def AddSecondaryMembers(self, interface, secondary_parents): | |
| 151 # With multiple inheritance, attributes and operations of non-first | |
| 152 # interfaces need to be added. Sometimes the attribute or operation is | |
| 153 # defined in the current interface as well as a parent. In that case we | |
| 154 # avoid making a duplicate definition and pray that the signatures match. | |
| 155 | |
| 156 for parent_interface in secondary_parents: | |
| 157 if isinstance(parent_interface, str): # IsDartCollectionType(parent_inter face) | |
| 158 continue | |
| 159 attributes = [attr for attr in parent_interface.attributes | |
| 160 if not FindMatchingAttribute(interface, attr)] | |
| 161 for (getter, setter) in _PairUpAttributes(attributes): | |
| 162 self.AddSecondaryAttribute(parent_interface, getter, setter) | |
| 163 | |
| 164 # Group overloaded operations by id | |
| 165 operationsById = {} | |
| 166 for operation in parent_interface.operations: | |
| 167 if operation.id not in operationsById: | |
| 168 operationsById[operation.id] = [] | |
| 169 operationsById[operation.id].append(operation) | |
| 170 | |
| 171 # Generate operations | |
| 172 for id in sorted(operationsById.keys()): | |
| 173 if not any(op.id == id for op in interface.operations): | |
| 174 operations = operationsById[id] | |
| 175 info = AnalyzeOperation(interface, operations) | |
| 176 self.AddSecondaryOperation(parent_interface, info) | |
| 177 | |
| 178 def AddConstant(self, constant): | |
| 179 pass | |
| 180 | |
| 181 def AddAttribute(self, getter, setter): | |
| 182 pass | |
| 183 | |
| 184 def AddIndexer(self, element_type): | |
| 185 pass | |
| 186 | |
| 187 def AmendIndexer(self, element_type): | |
| 188 pass | |
| 189 | |
| 190 def AddOperation(self, info): | |
| 191 pass | |
| 192 | |
| 193 def AddStaticOperation(self, info): | |
| 194 pass | |
| 195 | |
| 196 def AddSecondaryAttribute(self, interface, getter, setter): | |
| 197 pass | |
| 198 | |
| 199 def AddSecondaryOperation(self, interface, attr): | |
| 200 pass | |
| 201 | |
| 202 | |
| 203 def _PairUpAttributes(attributes): | |
| 204 """Returns a list of (getter, setter) pairs sorted by name. | |
| 205 | |
| 206 One element of the pair may be None. | |
| 207 """ | |
| 208 names = sorted(set(attr.id for attr in attributes)) | |
| 209 getters = {} | |
| 210 setters = {} | |
| 211 for attr in attributes: | |
| 212 if attr.is_fc_getter: | |
| 213 getters[attr.id] = attr | |
| 214 elif attr.is_fc_setter and 'Replaceable' not in attr.ext_attrs: | |
| 215 setters[attr.id] = attr | |
| 216 return [(getters.get(id), setters.get(id)) for id in names] | |
| OLD | NEW |