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 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 # is useful not only for browser compat, but to allow code that links | 161 # is useful not only for browser compat, but to allow code that links |
162 # against dart:dom_deprecated to load in a worker isolate. | 162 # against dart:dom_deprecated to load in a worker isolate. |
163 return '*' + javascript_binding_name | 163 return '*' + javascript_binding_name |
164 | 164 |
165 | 165 |
166 def MatchSourceFilter(thing): | 166 def MatchSourceFilter(thing): |
167 return 'WebKit' in thing.annotations or 'Dart' in thing.annotations | 167 return 'WebKit' in thing.annotations or 'Dart' in thing.annotations |
168 | 168 |
169 | 169 |
170 def DartType(idl_type_name): | 170 def DartType(idl_type_name): |
171 return TypeRegistry().TypeInfo(idl_type_name).dart_type() | 171 if idl_type_name in _idl_type_registry: |
| 172 return _idl_type_registry[idl_type_name].get('dart_type', idl_type_name) |
| 173 return idl_type_name |
172 | 174 |
173 | 175 |
174 class ParamInfo(object): | 176 class ParamInfo(object): |
175 """Holder for various information about a parameter of a Dart operation. | 177 """Holder for various information about a parameter of a Dart operation. |
176 | 178 |
177 Attributes: | 179 Attributes: |
178 name: Name of parameter. | 180 name: Name of parameter. |
179 type_id: Original type id. None for merged types. | 181 type_id: Original type id. None for merged types. |
180 dart_type: DartType of parameter. | 182 dart_type: DartType of parameter. |
181 is_optional: Parameter optionality. | 183 is_optional: Parameter optionality. |
(...skipping 15 matching lines...) Expand all Loading... |
197 # Given a list of overloaded arguments, choose a suitable name. | 199 # Given a list of overloaded arguments, choose a suitable name. |
198 def OverloadedName(args): | 200 def OverloadedName(args): |
199 return '_OR_'.join(sorted(set(arg.id for arg in args))) | 201 return '_OR_'.join(sorted(set(arg.id for arg in args))) |
200 | 202 |
201 # Given a list of overloaded arguments, choose a suitable type. | 203 # Given a list of overloaded arguments, choose a suitable type. |
202 def OverloadedType(args): | 204 def OverloadedType(args): |
203 type_ids = sorted(set(arg.type.id for arg in args)) | 205 type_ids = sorted(set(arg.type.id for arg in args)) |
204 dart_types = sorted(set(DartType(arg.type.id) for arg in args)) | 206 dart_types = sorted(set(DartType(arg.type.id) for arg in args)) |
205 if len(dart_types) == 1: | 207 if len(dart_types) == 1: |
206 if len(type_ids) == 1: | 208 if len(type_ids) == 1: |
207 return (type_ids[0], dart_types[0]) | 209 return (type_ids[0], type_ids[0]) |
208 else: | 210 else: |
209 return (None, dart_types[0]) | 211 return (None, type_ids[0]) |
210 else: | 212 else: |
211 return (None, TypeName(type_ids, interface)) | 213 return (None, TypeName(type_ids, interface)) |
212 | 214 |
213 def IsOptional(argument): | 215 def IsOptional(argument): |
214 if not argument: | 216 if not argument: |
215 return True | 217 return True |
216 if 'Callback' in argument.ext_attrs: | 218 if 'Callback' in argument.ext_attrs: |
217 # Callbacks with 'Optional=XXX' are treated as optional arguments. | 219 # Callbacks with 'Optional=XXX' are treated as optional arguments. |
218 return 'Optional' in argument.ext_attrs | 220 return 'Optional' in argument.ext_attrs |
219 if constructor: | 221 if constructor: |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 args = map(lambda *args: _DartArg(args, interface), | 255 args = map(lambda *args: _DartArg(args, interface), |
254 *(op.arguments for op in split_operations)) | 256 *(op.arguments for op in split_operations)) |
255 | 257 |
256 info = OperationInfo() | 258 info = OperationInfo() |
257 info.operations = operations | 259 info.operations = operations |
258 info.overloads = split_operations | 260 info.overloads = split_operations |
259 info.declared_name = operations[0].id | 261 info.declared_name = operations[0].id |
260 info.name = operations[0].ext_attrs.get('DartName', info.declared_name) | 262 info.name = operations[0].ext_attrs.get('DartName', info.declared_name) |
261 info.constructor_name = None | 263 info.constructor_name = None |
262 info.js_name = info.declared_name | 264 info.js_name = info.declared_name |
263 info.type_name = DartType(operations[0].type.id) # TODO: widen. | 265 info.type_name = operations[0].type.id # TODO: widen. |
264 info.param_infos = args | 266 info.param_infos = args |
265 return info | 267 return info |
266 | 268 |
267 | 269 |
268 def AnalyzeConstructor(interface): | 270 def AnalyzeConstructor(interface): |
269 """Returns an OperationInfo object for the constructor. | 271 """Returns an OperationInfo object for the constructor. |
270 | 272 |
271 Returns None if the interface has no Constructor. | 273 Returns None if the interface has no Constructor. |
272 """ | 274 """ |
273 def GetArgs(func_value): | 275 def GetArgs(func_value): |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 | 377 |
376 Attributes: | 378 Attributes: |
377 overloads: A list of IDL operation overloads with the same name. | 379 overloads: A list of IDL operation overloads with the same name. |
378 name: A string, the simple name of the operation. | 380 name: A string, the simple name of the operation. |
379 constructor_name: A string, the name of the constructor iff the constructor | 381 constructor_name: A string, the name of the constructor iff the constructor |
380 is named, e.g. 'fromList' in Int8Array.fromList(list). | 382 is named, e.g. 'fromList' in Int8Array.fromList(list). |
381 type_name: A string, the name of the return type of the operation. | 383 type_name: A string, the name of the return type of the operation. |
382 param_infos: A list of ParamInfo. | 384 param_infos: A list of ParamInfo. |
383 """ | 385 """ |
384 | 386 |
385 def ParametersInterfaceDeclaration(self, rename_type=lambda x: x): | 387 def ParametersInterfaceDeclaration(self, rename_type): |
386 """Returns a formatted string declaring the parameters for the interface.""" | 388 """Returns a formatted string declaring the parameters for the interface.""" |
387 return self._FormatParams( | 389 return self._FormatParams( |
388 self.param_infos, None, | 390 self.param_infos, None, |
389 lambda param: TypeOrNothing(rename_type(param.dart_type), param.type_id)
) | 391 lambda param: TypeOrNothing(rename_type(param.dart_type), param.type_id)
) |
390 | 392 |
391 def ParametersImplementationDeclaration( | 393 def ParametersImplementationDeclaration( |
392 self, rename_type=None, default_value='null'): | 394 self, rename_type, default_value='null'): |
393 """Returns a formatted string declaring the parameters for the | 395 """Returns a formatted string declaring the parameters for the |
394 implementation. | 396 implementation. |
395 | 397 |
396 Args: | 398 Args: |
397 rename_type: A function that allows the types to be renamed. | 399 rename_type: A function that allows the types to be renamed. |
398 The function is applied to the parameter's dart_type. | 400 The function is applied to the parameter's dart_type. |
399 """ | 401 """ |
400 if rename_type: | 402 return self._FormatParams( |
401 def renamer(param_info): | 403 self.param_infos, default_value, |
402 return TypeOrNothing(rename_type(param_info.dart_type)) | 404 lambda param: TypeOrNothing(rename_type(param.dart_type))) |
403 return self._FormatParams(self.param_infos, default_value, renamer) | |
404 else: | |
405 def type_fn(param_info): | |
406 if param_info.dart_type == 'Dynamic': | |
407 if param_info.type_id: | |
408 # It is more informative to use a comment IDL type. | |
409 return '/*%s*/' % param_info.type_id | |
410 else: | |
411 return 'var' | |
412 else: | |
413 return param_info.dart_type | |
414 return self._FormatParams( | |
415 self.param_infos, default_value, | |
416 lambda param: TypeOrNothing(param.dart_type, param.type_id)) | |
417 | 405 |
418 def ParametersAsArgumentList(self): | 406 def ParametersAsArgumentList(self): |
419 """Returns a string of the parameter names suitable for passing the | 407 """Returns a string of the parameter names suitable for passing the |
420 parameters as arguments. | 408 parameters as arguments. |
421 """ | 409 """ |
422 return ', '.join(map(lambda param_info: param_info.name, self.param_infos)) | 410 return ', '.join(map(lambda param_info: param_info.name, self.param_infos)) |
423 | 411 |
424 def _FormatParams(self, params, default_value, type_fn): | 412 def _FormatParams(self, params, default_value, type_fn): |
425 def FormatParam(param): | 413 def FormatParam(param): |
426 """Returns a parameter declaration fragment for an ParamInfo.""" | 414 """Returns a parameter declaration fragment for an ParamInfo.""" |
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
814 _svg_supplemental_includes = [ | 802 _svg_supplemental_includes = [ |
815 '"SVGAnimatedPropertyTearOff.h"', | 803 '"SVGAnimatedPropertyTearOff.h"', |
816 '"SVGAnimatedListPropertyTearOff.h"', | 804 '"SVGAnimatedListPropertyTearOff.h"', |
817 '"SVGStaticListPropertyTearOff.h"', | 805 '"SVGStaticListPropertyTearOff.h"', |
818 '"SVGAnimatedListPropertyTearOff.h"', | 806 '"SVGAnimatedListPropertyTearOff.h"', |
819 '"SVGTransformListPropertyTearOff.h"', | 807 '"SVGTransformListPropertyTearOff.h"', |
820 '"SVGPathSegListPropertyTearOff.h"', | 808 '"SVGPathSegListPropertyTearOff.h"', |
821 ] | 809 ] |
822 | 810 |
823 class TypeRegistry(object): | 811 class TypeRegistry(object): |
824 def __init__(self): | 812 def __init__(self, interface_renames): |
| 813 self._interface_renames = interface_renames |
825 self._cache = {} | 814 self._cache = {} |
826 | 815 |
827 def TypeInfo(self, type_name): | 816 def TypeInfo(self, type_name): |
828 if not type_name in self._cache: | 817 if not type_name in self._cache: |
829 self._cache[type_name] = self._TypeInfo(type_name) | 818 self._cache[type_name] = self._TypeInfo(type_name) |
830 return self._cache[type_name] | 819 return self._cache[type_name] |
831 | 820 |
| 821 def DartType(self, type_name): |
| 822 dart_type = self.TypeInfo(type_name).dart_type() |
| 823 return self._interface_renames.get(dart_type, dart_type) |
| 824 |
| 825 def InterfaceName(self, type_name): |
| 826 return self._interface_renames.get(type_name, type_name) |
| 827 |
832 def _TypeInfo(self, type_name): | 828 def _TypeInfo(self, type_name): |
833 match = re.match(r'(?:sequence<(\w+)>|(\w+)\[\])$', type_name) | 829 match = re.match(r'(?:sequence<(\w+)>|(\w+)\[\])$', type_name) |
834 if match: | 830 if match: |
835 if type_name == 'DOMString[]': | 831 if type_name == 'DOMString[]': |
836 return DOMStringArrayTypeInfo(self.TypeInfo('DOMString')) | 832 return DOMStringArrayTypeInfo(self.TypeInfo('DOMString')) |
837 return SequenceIDLTypeInfo(type_name, self.TypeInfo(match.group(1) or matc
h.group(2))) | 833 return SequenceIDLTypeInfo(type_name, self.TypeInfo(match.group(1) or matc
h.group(2))) |
838 if not type_name in _idl_type_registry: | 834 if not type_name in _idl_type_registry: |
839 return InterfaceIDLTypeInfo(type_name, {}) | 835 return InterfaceIDLTypeInfo(type_name, {}) |
840 type_data = _idl_type_registry.get(type_name) | 836 type_data = _idl_type_registry.get(type_name) |
841 class_name = '%sIDLTypeInfo' % type_data['type'] | 837 class_name = '%sIDLTypeInfo' % type_data['type'] |
842 return globals()[class_name](type_name, type_data) | 838 return globals()[class_name](type_name, type_data) |
OLD | NEW |