| 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 shared functionality for systems to generate | 6 """This module provides shared functionality for systems to generate |
| 7 Dart APIs from the IDL database.""" | 7 Dart APIs from the IDL database.""" |
| 8 | 8 |
| 9 import copy | 9 import copy |
| 10 import re | 10 import re |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 def MatchSourceFilter(thing): | 141 def MatchSourceFilter(thing): |
| 142 return 'WebKit' in thing.annotations or 'Dart' in thing.annotations | 142 return 'WebKit' in thing.annotations or 'Dart' in thing.annotations |
| 143 | 143 |
| 144 | 144 |
| 145 class ParamInfo(object): | 145 class ParamInfo(object): |
| 146 """Holder for various information about a parameter of a Dart operation. | 146 """Holder for various information about a parameter of a Dart operation. |
| 147 | 147 |
| 148 Attributes: | 148 Attributes: |
| 149 name: Name of parameter. | 149 name: Name of parameter. |
| 150 type_id: Original type id. None for merged types. | 150 type_id: Original type id. None for merged types. |
| 151 dart_type: DartType of parameter. | |
| 152 is_optional: Parameter optionality. | 151 is_optional: Parameter optionality. |
| 153 """ | 152 """ |
| 154 def __init__(self, name, type_id, dart_type, is_optional): | 153 def __init__(self, name, type_id, is_optional): |
| 155 self.name = name | 154 self.name = name |
| 156 self.type_id = type_id | 155 self.type_id = type_id |
| 157 self.dart_type = dart_type | |
| 158 self.is_optional = is_optional | 156 self.is_optional = is_optional |
| 159 | 157 |
| 160 def Copy(self): | 158 def Copy(self): |
| 161 return ParamInfo(self.name, self.type_id, self.dart_type, self.is_optional) | 159 return ParamInfo(self.name, self.type_id, self.is_optional) |
| 162 | 160 |
| 163 def __repr__(self): | 161 def __repr__(self): |
| 164 content = 'name = %s, type_id = %s, dart_type = %s, is_optional = %s' % ( | 162 content = 'name = %s, type_id = %s, is_optional = %s' % ( |
| 165 self.name, self.type_id, self.dart_type, self.is_optional) | 163 self.name, self.type_id, self.is_optional) |
| 166 return '<ParamInfo(%s)>' % content | 164 return '<ParamInfo(%s)>' % content |
| 167 | 165 |
| 168 | 166 |
| 169 # Given a list of overloaded arguments, render dart arguments. | 167 # Given a list of overloaded arguments, render dart arguments. |
| 170 def _BuildArguments(args, interface, constructor=False): | 168 def _BuildArguments(args, interface, constructor=False): |
| 171 def IsOptional(argument): | 169 def IsOptional(argument): |
| 172 if 'Callback' in argument.ext_attrs: | 170 if 'Callback' in argument.ext_attrs: |
| 173 # Callbacks with 'Optional=XXX' are treated as optional arguments. | 171 # Callbacks with 'Optional=XXX' are treated as optional arguments. |
| 174 return 'Optional' in argument.ext_attrs | 172 return 'Optional' in argument.ext_attrs |
| 175 if constructor: | 173 if constructor: |
| 176 # FIXME: Constructors with 'Optional=XXX' shouldn't be treated as | 174 # FIXME: Constructors with 'Optional=XXX' shouldn't be treated as |
| 177 # optional arguments. | 175 # optional arguments. |
| 178 return 'Optional' in argument.ext_attrs | 176 return 'Optional' in argument.ext_attrs |
| 179 return False | 177 return False |
| 180 | 178 |
| 181 # Given a list of overloaded arguments, choose a suitable name. | 179 # Given a list of overloaded arguments, choose a suitable name. |
| 182 def OverloadedName(args): | 180 def OverloadedName(args): |
| 183 return '_OR_'.join(sorted(set(arg.id for arg in args))) | 181 return '_OR_'.join(sorted(set(arg.id for arg in args))) |
| 184 | 182 |
| 185 def DartType(idl_type_name): | 183 def DartType(idl_type_name): |
| 186 if idl_type_name in _idl_type_registry: | 184 if idl_type_name in _idl_type_registry: |
| 187 return _idl_type_registry[idl_type_name].dart_type or idl_type_name | 185 return _idl_type_registry[idl_type_name].dart_type or idl_type_name |
| 188 return idl_type_name | 186 return idl_type_name |
| 189 | 187 |
| 190 # Given a list of overloaded arguments, choose a suitable type. | 188 # Given a list of overloaded arguments, choose a suitable type. |
| 191 def OverloadedType(args): | 189 def OverloadedType(args): |
| 192 type_ids = sorted(set(arg.type.id for arg in args)) | 190 type_ids = sorted(set(arg.type.id for arg in args)) |
| 193 if len(set(DartType(arg.type.id) for arg in args)) == 1: | 191 if len(set(DartType(arg.type.id) for arg in args)) == 1: |
| 194 if len(type_ids) == 1: | 192 return type_ids[0] |
| 195 return (type_ids[0], type_ids[0]) | |
| 196 else: | |
| 197 return (None, type_ids[0]) | |
| 198 else: | 193 else: |
| 199 return (None, TypeName(type_ids, interface)) | 194 return None |
| 200 | 195 |
| 201 result = [] | 196 result = [] |
| 202 | 197 |
| 203 is_optional = False | 198 is_optional = False |
| 204 for arg_tuple in map(lambda *x: x, *args): | 199 for arg_tuple in map(lambda *x: x, *args): |
| 205 is_optional = is_optional or any(arg is None or IsOptional(arg) for arg in a
rg_tuple) | 200 is_optional = is_optional or any(arg is None or IsOptional(arg) for arg in a
rg_tuple) |
| 206 | 201 |
| 207 filtered = filter(None, arg_tuple) | 202 filtered = filter(None, arg_tuple) |
| 208 (type_id, dart_type) = OverloadedType(filtered) | 203 type_id = OverloadedType(filtered) |
| 209 name = OverloadedName(filtered) | 204 name = OverloadedName(filtered) |
| 210 result.append(ParamInfo(name, type_id, dart_type, is_optional)) | 205 result.append(ParamInfo(name, type_id, is_optional)) |
| 211 | 206 |
| 212 return result | 207 return result |
| 213 | 208 |
| 214 def IsOptional(argument): | 209 def IsOptional(argument): |
| 215 return ('Optional' in argument.ext_attrs and | 210 return ('Optional' in argument.ext_attrs and |
| 216 argument.ext_attrs['Optional'] == None) | 211 argument.ext_attrs['Optional'] == None) |
| 217 | 212 |
| 218 def AnalyzeOperation(interface, operations): | 213 def AnalyzeOperation(interface, operations): |
| 219 """Makes operation calling convention decision for a set of overloads. | 214 """Makes operation calling convention decision for a set of overloads. |
| 220 | 215 |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 name: A string, the simple name of the operation. | 335 name: A string, the simple name of the operation. |
| 341 constructor_name: A string, the name of the constructor iff the constructor | 336 constructor_name: A string, the name of the constructor iff the constructor |
| 342 is named, e.g. 'fromList' in Int8Array.fromList(list). | 337 is named, e.g. 'fromList' in Int8Array.fromList(list). |
| 343 type_name: A string, the name of the return type of the operation. | 338 type_name: A string, the name of the return type of the operation. |
| 344 param_infos: A list of ParamInfo. | 339 param_infos: A list of ParamInfo. |
| 345 """ | 340 """ |
| 346 | 341 |
| 347 def ParametersInterfaceDeclaration(self, rename_type): | 342 def ParametersInterfaceDeclaration(self, rename_type): |
| 348 """Returns a formatted string declaring the parameters for the interface.""" | 343 """Returns a formatted string declaring the parameters for the interface.""" |
| 349 def type_function(param): | 344 def type_function(param): |
| 350 # TODO(podivilov): replace param.dart_type field with param.is_optional | 345 dart_type = rename_type(param.type_id) if param.type_id else 'Dynamic' |
| 351 dart_type = param.dart_type | |
| 352 if dart_type != 'Dynamic': | |
| 353 dart_type = rename_type(dart_type) | |
| 354 return TypeOrNothing(dart_type, param.type_id) | 346 return TypeOrNothing(dart_type, param.type_id) |
| 355 return self._FormatParams(self.param_infos, None, type_function) | 347 return self._FormatParams(self.param_infos, None, type_function) |
| 356 | 348 |
| 357 def ParametersImplementationDeclaration(self, rename_type): | 349 def ParametersImplementationDeclaration(self, rename_type): |
| 358 """Returns a formatted string declaring the parameters for the | 350 """Returns a formatted string declaring the parameters for the |
| 359 implementation. | 351 implementation. |
| 360 | 352 |
| 361 Args: | 353 Args: |
| 362 rename_type: A function that allows the types to be renamed. | 354 rename_type: A function that allows the types to be renamed. |
| 363 The function is applied to the parameter's dart_type. | 355 The function is applied to the parameter's dart_type. |
| 364 """ | 356 """ |
| 365 def type_function(param): | 357 def type_function(param): |
| 366 # TODO(podivilov): replace param.dart_type field with param.is_optional | 358 dart_type = rename_type(param.type_id) if param.type_id else 'Dynamic' |
| 367 dart_type = param.dart_type | |
| 368 if dart_type != 'Dynamic': | |
| 369 dart_type = rename_type(dart_type) | |
| 370 return TypeOrNothing(dart_type) | 359 return TypeOrNothing(dart_type) |
| 371 return self._FormatParams(self.param_infos, 'null', type_function) | 360 return self._FormatParams(self.param_infos, 'null', type_function) |
| 372 | 361 |
| 373 def ParametersAsArgumentList(self, parameter_count = None): | 362 def ParametersAsArgumentList(self, parameter_count = None): |
| 374 """Returns a string of the parameter names suitable for passing the | 363 """Returns a string of the parameter names suitable for passing the |
| 375 parameters as arguments. | 364 parameters as arguments. |
| 376 """ | 365 """ |
| 377 if parameter_count is None: | 366 if parameter_count is None: |
| 378 parameter_count = len(self.param_infos) | 367 parameter_count = len(self.param_infos) |
| 379 return ', '.join(map( | 368 return ', '.join(map( |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 462 if param_info.is_optional: | 451 if param_info.is_optional: |
| 463 EmitOptionalParameterInvocation(index) | 452 EmitOptionalParameterInvocation(index) |
| 464 | 453 |
| 465 | 454 |
| 466 def CopyAndWidenDefaultParameters(self): | 455 def CopyAndWidenDefaultParameters(self): |
| 467 """Returns equivalent OperationInfo, but default parameters are Dynamic.""" | 456 """Returns equivalent OperationInfo, but default parameters are Dynamic.""" |
| 468 info = copy.copy(self) | 457 info = copy.copy(self) |
| 469 info.param_infos = [param.Copy() for param in self.param_infos] | 458 info.param_infos = [param.Copy() for param in self.param_infos] |
| 470 for param in info.param_infos: | 459 for param in info.param_infos: |
| 471 if param.is_optional: | 460 if param.is_optional: |
| 472 param.dart_type = 'Dynamic' | 461 param.type_id = None |
| 473 return info | 462 return info |
| 474 | 463 |
| 475 | 464 |
| 476 def ConstantOutputOrder(a, b): | 465 def ConstantOutputOrder(a, b): |
| 477 """Canonical output ordering for constants.""" | 466 """Canonical output ordering for constants.""" |
| 478 if a.id < b.id: return -1 | 467 if a.id < b.id: return -1 |
| 479 if a.id > b.id: return 1 | 468 if a.id > b.id: return 1 |
| 480 return 0 | 469 return 0 |
| 481 | 470 |
| 482 | 471 |
| (...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1109 self._database.GetInterface(type_name)) | 1098 self._database.GetInterface(type_name)) |
| 1110 else: | 1099 else: |
| 1111 dart_interface_name = type_name | 1100 dart_interface_name = type_name |
| 1112 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name) | 1101 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name) |
| 1113 | 1102 |
| 1114 if type_data.clazz == 'ListLike': | 1103 if type_data.clazz == 'ListLike': |
| 1115 return ListLikeIDLTypeInfo(type_name, type_data, self.TypeInfo(type_data.i
tem_type)) | 1104 return ListLikeIDLTypeInfo(type_name, type_data, self.TypeInfo(type_data.i
tem_type)) |
| 1116 | 1105 |
| 1117 class_name = '%sIDLTypeInfo' % type_data.clazz | 1106 class_name = '%sIDLTypeInfo' % type_data.clazz |
| 1118 return globals()[class_name](type_name, type_data) | 1107 return globals()[class_name](type_name, type_data) |
| OLD | NEW |