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 return '*' + javascript_binding_name | 161 return '*' + javascript_binding_name |
162 | 162 |
163 | 163 |
164 def MatchSourceFilter(thing): | 164 def MatchSourceFilter(thing): |
165 return 'WebKit' in thing.annotations or 'Dart' in thing.annotations | 165 return 'WebKit' in thing.annotations or 'Dart' in thing.annotations |
166 | 166 |
167 | 167 |
168 def DartType(idl_type_name): | 168 def DartType(idl_type_name): |
169 return GetIDLTypeInfo(idl_type_name).dart_type() | 169 return GetIDLTypeInfo(idl_type_name).dart_type() |
170 | 170 |
| 171 def DartInterfaceName(interface): |
| 172 return IDLTypeInfo.interface_renames.get(interface.id, interface.id) |
171 | 173 |
172 class ParamInfo(object): | 174 class ParamInfo(object): |
173 """Holder for various information about a parameter of a Dart operation. | 175 """Holder for various information about a parameter of a Dart operation. |
174 | 176 |
175 Attributes: | 177 Attributes: |
176 name: Name of parameter. | 178 name: Name of parameter. |
177 type_id: Original type id. None for merged types. | 179 type_id: Original type id. None for merged types. |
178 dart_type: DartType of parameter. | 180 dart_type: DartType of parameter. |
179 is_optional: Parameter optionality. | 181 is_optional: Parameter optionality. |
180 """ | 182 """ |
(...skipping 14 matching lines...) Expand all Loading... |
195 # Given a list of overloaded arguments, choose a suitable name. | 197 # Given a list of overloaded arguments, choose a suitable name. |
196 def OverloadedName(args): | 198 def OverloadedName(args): |
197 return '_OR_'.join(sorted(set(arg.id for arg in args))) | 199 return '_OR_'.join(sorted(set(arg.id for arg in args))) |
198 | 200 |
199 # Given a list of overloaded arguments, choose a suitable type. | 201 # Given a list of overloaded arguments, choose a suitable type. |
200 def OverloadedType(args): | 202 def OverloadedType(args): |
201 type_ids = sorted(set(arg.type.id for arg in args)) | 203 type_ids = sorted(set(arg.type.id for arg in args)) |
202 dart_types = sorted(set(DartType(arg.type.id) for arg in args)) | 204 dart_types = sorted(set(DartType(arg.type.id) for arg in args)) |
203 if len(dart_types) == 1: | 205 if len(dart_types) == 1: |
204 if len(type_ids) == 1: | 206 if len(type_ids) == 1: |
205 return (type_ids[0], dart_types[0]) | 207 return (type_ids[0], type_ids[0]) |
206 else: | 208 else: |
207 return (None, dart_types[0]) | 209 return (None, type_ids[0]) |
208 else: | 210 else: |
209 return (None, TypeName(type_ids, interface)) | 211 return (None, TypeName(type_ids, interface)) |
210 | 212 |
211 def IsOptional(argument): | 213 def IsOptional(argument): |
212 if not argument: | 214 if not argument: |
213 return True | 215 return True |
214 if 'Callback' in argument.ext_attrs: | 216 if 'Callback' in argument.ext_attrs: |
215 # Callbacks with 'Optional=XXX' are treated as optional arguments. | 217 # Callbacks with 'Optional=XXX' are treated as optional arguments. |
216 return 'Optional' in argument.ext_attrs | 218 return 'Optional' in argument.ext_attrs |
217 if constructor: | 219 if constructor: |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 args = map(lambda *args: _DartArg(args, interface), | 253 args = map(lambda *args: _DartArg(args, interface), |
252 *(op.arguments for op in split_operations)) | 254 *(op.arguments for op in split_operations)) |
253 | 255 |
254 info = OperationInfo() | 256 info = OperationInfo() |
255 info.operations = operations | 257 info.operations = operations |
256 info.overloads = split_operations | 258 info.overloads = split_operations |
257 info.declared_name = operations[0].id | 259 info.declared_name = operations[0].id |
258 info.name = operations[0].ext_attrs.get('DartName', info.declared_name) | 260 info.name = operations[0].ext_attrs.get('DartName', info.declared_name) |
259 info.constructor_name = None | 261 info.constructor_name = None |
260 info.js_name = info.declared_name | 262 info.js_name = info.declared_name |
261 info.type_name = DartType(operations[0].type.id) # TODO: widen. | 263 info.type_name = operations[0].type.id # TODO: widen. |
262 info.param_infos = args | 264 info.param_infos = args |
263 return info | 265 return info |
264 | 266 |
265 | 267 |
266 def AnalyzeConstructor(interface): | 268 def AnalyzeConstructor(interface): |
267 """Returns an OperationInfo object for the constructor. | 269 """Returns an OperationInfo object for the constructor. |
268 | 270 |
269 Returns None if the interface has no Constructor. | 271 Returns None if the interface has no Constructor. |
270 """ | 272 """ |
271 def GetArgs(func_value): | 273 def GetArgs(func_value): |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 | 375 |
374 Attributes: | 376 Attributes: |
375 overloads: A list of IDL operation overloads with the same name. | 377 overloads: A list of IDL operation overloads with the same name. |
376 name: A string, the simple name of the operation. | 378 name: A string, the simple name of the operation. |
377 constructor_name: A string, the name of the constructor iff the constructor | 379 constructor_name: A string, the name of the constructor iff the constructor |
378 is named, e.g. 'fromList' in Int8Array.fromList(list). | 380 is named, e.g. 'fromList' in Int8Array.fromList(list). |
379 type_name: A string, the name of the return type of the operation. | 381 type_name: A string, the name of the return type of the operation. |
380 param_infos: A list of ParamInfo. | 382 param_infos: A list of ParamInfo. |
381 """ | 383 """ |
382 | 384 |
383 def ParametersInterfaceDeclaration(self, rename_type=lambda x: x): | 385 def ParametersInterfaceDeclaration(self, rename_type=DartType): |
384 """Returns a formatted string declaring the parameters for the interface.""" | 386 """Returns a formatted string declaring the parameters for the interface.""" |
385 return self._FormatParams( | 387 return self._FormatParams( |
386 self.param_infos, None, | 388 self.param_infos, None, |
387 lambda param: TypeOrNothing(rename_type(param.dart_type), param.type_id)
) | 389 lambda param: TypeOrNothing(rename_type(param.dart_type), param.type_id)
) |
388 | 390 |
389 def ParametersImplementationDeclaration( | 391 def ParametersImplementationDeclaration( |
390 self, rename_type=None, default_value='null'): | 392 self, rename_type=DartType, default_value='null'): |
391 """Returns a formatted string declaring the parameters for the | 393 """Returns a formatted string declaring the parameters for the |
392 implementation. | 394 implementation. |
393 | 395 |
394 Args: | 396 Args: |
395 rename_type: A function that allows the types to be renamed. | 397 rename_type: A function that allows the types to be renamed. |
396 The function is applied to the parameter's dart_type. | 398 The function is applied to the parameter's dart_type. |
397 """ | 399 """ |
398 if rename_type: | 400 return self._FormatParams( |
399 def renamer(param_info): | 401 self.param_infos, default_value, |
400 return TypeOrNothing(rename_type(param_info.dart_type)) | 402 lambda param: TypeOrNothing(rename_type(param.dart_type))) |
401 return self._FormatParams(self.param_infos, default_value, renamer) | |
402 else: | |
403 def type_fn(param_info): | |
404 if param_info.dart_type == 'Dynamic': | |
405 if param_info.type_id: | |
406 # It is more informative to use a comment IDL type. | |
407 return '/*%s*/' % param_info.type_id | |
408 else: | |
409 return 'var' | |
410 else: | |
411 return param_info.dart_type | |
412 return self._FormatParams( | |
413 self.param_infos, default_value, | |
414 lambda param: TypeOrNothing(param.dart_type, param.type_id)) | |
415 | 403 |
416 def ParametersAsArgumentList(self): | 404 def ParametersAsArgumentList(self): |
417 """Returns a string of the parameter names suitable for passing the | 405 """Returns a string of the parameter names suitable for passing the |
418 parameters as arguments. | 406 parameters as arguments. |
419 """ | 407 """ |
420 return ', '.join(map(lambda param_info: param_info.name, self.param_infos)) | 408 return ', '.join(map(lambda param_info: param_info.name, self.param_infos)) |
421 | 409 |
422 def _FormatParams(self, params, default_value, type_fn): | 410 def _FormatParams(self, params, default_value, type_fn): |
423 def FormatParam(param): | 411 def FormatParam(param): |
424 """Returns a parameter declaration fragment for an ParamInfo.""" | 412 """Returns a parameter declaration fragment for an ParamInfo.""" |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 self._dart_type = dart_type | 486 self._dart_type = dart_type |
499 self._native_type = native_type | 487 self._native_type = native_type |
500 self._custom_to_native = custom_to_native | 488 self._custom_to_native = custom_to_native |
501 self._custom_to_dart = custom_to_dart | 489 self._custom_to_dart = custom_to_dart |
502 self._conversion_includes = conversion_includes + [idl_type] | 490 self._conversion_includes = conversion_includes + [idl_type] |
503 | 491 |
504 def idl_type(self): | 492 def idl_type(self): |
505 return self._idl_type | 493 return self._idl_type |
506 | 494 |
507 def dart_type(self): | 495 def dart_type(self): |
508 return self._dart_type or self._idl_type | 496 dart_type = self._dart_type or self._idl_type |
| 497 return self.interface_renames.get(dart_type, dart_type) |
509 | 498 |
510 def native_type(self): | 499 def native_type(self): |
511 return self._native_type or self._idl_type | 500 return self._native_type or self._idl_type |
512 | 501 |
513 def emit_to_native(self, emitter, idl_node, name, handle, interface_name): | 502 def emit_to_native(self, emitter, idl_node, name, handle, interface_name): |
514 if 'Callback' in idl_node.ext_attrs: | 503 if 'Callback' in idl_node.ext_attrs: |
515 if set(['Optional', 'Callback']).issubset(idl_node.ext_attrs.keys()): | 504 if set(['Optional', 'Callback']).issubset(idl_node.ext_attrs.keys()): |
516 flag = 'DartUtilities::ConvertNullToDefaultValue' | 505 flag = 'DartUtilities::ConvertNullToDefaultValue' |
517 else: | 506 else: |
518 flag = 'DartUtilities::ConvertNone' | 507 flag = 'DartUtilities::ConvertNone' |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
590 | 579 |
591 def to_native_includes(self): | 580 def to_native_includes(self): |
592 return ['"Dart%s.h"' % self.idl_type()] | 581 return ['"Dart%s.h"' % self.idl_type()] |
593 | 582 |
594 def to_dart_conversion(self, value, interface_name=None, attributes=None): | 583 def to_dart_conversion(self, value, interface_name=None, attributes=None): |
595 return 'Dart%s::toDart(%s)' % (self._idl_type, value) | 584 return 'Dart%s::toDart(%s)' % (self._idl_type, value) |
596 | 585 |
597 def custom_to_dart(self): | 586 def custom_to_dart(self): |
598 return self._custom_to_dart | 587 return self._custom_to_dart |
599 | 588 |
| 589 interface_renames = {} |
| 590 |
600 | 591 |
601 class SequenceIDLTypeInfo(IDLTypeInfo): | 592 class SequenceIDLTypeInfo(IDLTypeInfo): |
602 def __init__(self, idl_type, item_info): | 593 def __init__(self, idl_type, item_info): |
603 super(SequenceIDLTypeInfo, self).__init__(idl_type) | 594 super(SequenceIDLTypeInfo, self).__init__(idl_type) |
604 self._item_info = item_info | 595 self._item_info = item_info |
605 | 596 |
606 def dart_type(self): | 597 def dart_type(self): |
607 return 'List<%s>' % self._item_info.dart_type() | 598 return 'List<%s>' % self._item_info.dart_type() |
608 | 599 |
609 def to_dart_conversion(self, value, interface_name=None, attributes=None): | 600 def to_dart_conversion(self, value, interface_name=None, attributes=None): |
(...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
825 | 816 |
826 match = re.match(r'sequence<(\w+)>$', idl_type_name) | 817 match = re.match(r'sequence<(\w+)>$', idl_type_name) |
827 if match: | 818 if match: |
828 return SequenceIDLTypeInfo(idl_type_name, GetIDLTypeInfo(match.group(1))) | 819 return SequenceIDLTypeInfo(idl_type_name, GetIDLTypeInfo(match.group(1))) |
829 | 820 |
830 match = re.match(r'(\w+)\[\]$', idl_type_name) | 821 match = re.match(r'(\w+)\[\]$', idl_type_name) |
831 if match: | 822 if match: |
832 return SequenceIDLTypeInfo(idl_type_name, GetIDLTypeInfo(match.group(1))) | 823 return SequenceIDLTypeInfo(idl_type_name, GetIDLTypeInfo(match.group(1))) |
833 | 824 |
834 return IDLTypeInfo(idl_type_name) | 825 return IDLTypeInfo(idl_type_name) |
OLD | NEW |