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 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 # to a dart argument. | 230 # to a dart argument. |
231 info = OperationInfo() | 231 info = OperationInfo() |
232 info.operations = operations | 232 info.operations = operations |
233 info.overloads = split_operations | 233 info.overloads = split_operations |
234 info.declared_name = operations[0].id | 234 info.declared_name = operations[0].id |
235 info.name = operations[0].ext_attrs.get('DartName', info.declared_name) | 235 info.name = operations[0].ext_attrs.get('DartName', info.declared_name) |
236 info.constructor_name = None | 236 info.constructor_name = None |
237 info.js_name = info.declared_name | 237 info.js_name = info.declared_name |
238 info.type_name = operations[0].type.id # TODO: widen. | 238 info.type_name = operations[0].type.id # TODO: widen. |
239 info.param_infos = _BuildArguments([op.arguments for op in split_operations],
interface) | 239 info.param_infos = _BuildArguments([op.arguments for op in split_operations],
interface) |
| 240 info.requires_named_arguments = False |
240 return info | 241 return info |
241 | 242 |
242 | 243 |
243 def AnalyzeConstructor(interface): | 244 def AnalyzeConstructor(interface): |
244 """Returns an OperationInfo object for the constructor. | 245 """Returns an OperationInfo object for the constructor. |
245 | 246 |
246 Returns None if the interface has no Constructor. | 247 Returns None if the interface has no Constructor. |
247 """ | 248 """ |
248 if 'Constructor' in interface.ext_attrs: | 249 if 'Constructor' in interface.ext_attrs: |
249 name = None | 250 name = None |
(...skipping 13 matching lines...) Expand all Loading... |
263 | 264 |
264 info = OperationInfo() | 265 info = OperationInfo() |
265 info.overloads = None | 266 info.overloads = None |
266 info.idl_args = idl_args | 267 info.idl_args = idl_args |
267 info.declared_name = name | 268 info.declared_name = name |
268 info.name = name | 269 info.name = name |
269 info.constructor_name = None | 270 info.constructor_name = None |
270 info.js_name = name | 271 info.js_name = name |
271 info.type_name = interface.id | 272 info.type_name = interface.id |
272 info.param_infos = args | 273 info.param_infos = args |
| 274 info.requires_named_arguments = False |
273 return info | 275 return info |
274 | 276 |
275 def IsDartListType(type): | 277 def IsDartListType(type): |
276 return type == 'List' or type.startswith('sequence<') | 278 return type == 'List' or type.startswith('sequence<') |
277 | 279 |
278 def IsDartCollectionType(type): | 280 def IsDartCollectionType(type): |
279 return IsDartListType(type) | 281 return IsDartListType(type) |
280 | 282 |
281 def FindMatchingAttribute(interface, attr1): | 283 def FindMatchingAttribute(interface, attr1): |
282 matches = [attr2 for attr2 in interface.attributes | 284 matches = [attr2 for attr2 in interface.attributes |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 | 334 |
333 Attributes: | 335 Attributes: |
334 overloads: A list of IDL operation overloads with the same name. | 336 overloads: A list of IDL operation overloads with the same name. |
335 name: A string, the simple name of the operation. | 337 name: A string, the simple name of the operation. |
336 constructor_name: A string, the name of the constructor iff the constructor | 338 constructor_name: A string, the name of the constructor iff the constructor |
337 is named, e.g. 'fromList' in Int8Array.fromList(list). | 339 is named, e.g. 'fromList' in Int8Array.fromList(list). |
338 type_name: A string, the name of the return type of the operation. | 340 type_name: A string, the name of the return type of the operation. |
339 param_infos: A list of ParamInfo. | 341 param_infos: A list of ParamInfo. |
340 """ | 342 """ |
341 | 343 |
342 def ParametersInterfaceDeclaration(self, rename_type): | 344 def ParametersInterfaceDeclaration(self, rename_type, force_optional=False): |
343 """Returns a formatted string declaring the parameters for the interface.""" | 345 """Returns a formatted string declaring the parameters for the interface.""" |
344 return self._FormatParams(self.param_infos, rename_type, True) | 346 return self._FormatParams(self.param_infos, rename_type, True, |
| 347 force_optional=force_optional) |
345 | 348 |
346 def ParametersImplementationDeclaration(self, rename_type): | 349 def ParametersImplementationDeclaration(self, rename_type): |
347 """Returns a formatted string declaring the parameters for the | 350 """Returns a formatted string declaring the parameters for the |
348 implementation. | 351 implementation. |
349 | 352 |
350 Args: | 353 Args: |
351 rename_type: A function that allows the types to be renamed. | 354 rename_type: A function that allows the types to be renamed. |
352 The function is applied to the parameter's dart_type. | 355 The function is applied to the parameter's dart_type. |
353 """ | 356 """ |
354 return self._FormatParams(self.param_infos, rename_type, False) | 357 return self._FormatParams(self.param_infos, rename_type, False) |
355 | 358 |
356 def ParametersAsArgumentList(self, parameter_count = None): | 359 def ParametersAsArgumentList(self, parameter_count = None): |
357 """Returns a string of the parameter names suitable for passing the | 360 """Returns a string of the parameter names suitable for passing the |
358 parameters as arguments. | 361 parameters as arguments. |
359 """ | 362 """ |
360 if parameter_count is None: | 363 if parameter_count is None: |
361 parameter_count = len(self.param_infos) | 364 parameter_count = len(self.param_infos) |
362 return ', '.join(map( | 365 return ', '.join(map( |
363 lambda param_info: param_info.name, | 366 lambda param_info: param_info.name, |
364 self.param_infos[:parameter_count])) | 367 self.param_infos[:parameter_count])) |
365 | 368 |
366 def _FormatParams(self, params, rename_type, provide_comments): | 369 def _FormatParams(self, params, rename_type, provide_comments, |
| 370 force_optional=False): |
367 def FormatParam(param): | 371 def FormatParam(param): |
368 dart_type = rename_type(param.type_id) if param.type_id else 'Dynamic' | 372 dart_type = rename_type(param.type_id) if param.type_id else 'Dynamic' |
369 type = TypeOrNothing(dart_type, param.type_id if provide_comments else Non
e) | 373 type = TypeOrNothing(dart_type, param.type_id if provide_comments else Non
e) |
370 return '%s%s' % (type, param.name) | 374 return '%s%s' % (type, param.name) |
371 | 375 |
372 required = [] | 376 required = [] |
373 optional = [] | 377 optional = [] |
374 for param_info in params: | 378 for param_info in params: |
375 if param_info.is_optional: | 379 if param_info.is_optional: |
376 optional.append(param_info) | 380 optional.append(param_info) |
377 else: | 381 else: |
378 if optional: | 382 if optional: |
379 raise Exception('Optional parameters cannot precede required ones: ' | 383 raise Exception('Optional parameters cannot precede required ones: ' |
380 + str(params)) | 384 + str(params)) |
381 required.append(param_info) | 385 required.append(param_info) |
382 argtexts = map(FormatParam, required) | 386 argtexts = map(FormatParam, required) |
383 if optional: | 387 if optional: |
384 argtexts.append('[' + ', '.join(map(FormatParam, optional)) + ']') | 388 needs_named = self.requires_named_arguments and not force_optional |
| 389 left_bracket, right_bracket = '{}' if needs_named else '[]' |
| 390 argtexts.append( |
| 391 left_bracket + |
| 392 ', '.join(map(FormatParam, optional)) + |
| 393 right_bracket) |
385 return ', '.join(argtexts) | 394 return ', '.join(argtexts) |
386 | 395 |
387 def IsStatic(self): | 396 def IsStatic(self): |
388 is_static = self.overloads[0].is_static | 397 is_static = self.overloads[0].is_static |
389 assert any([is_static == o.is_static for o in self.overloads]) | 398 assert any([is_static == o.is_static for o in self.overloads]) |
390 return is_static | 399 return is_static |
391 | 400 |
392 def _ConstructorFullName(self, rename_type): | 401 def _ConstructorFullName(self, rename_type): |
393 if self.constructor_name: | 402 if self.constructor_name: |
394 return rename_type(self.type_name) + '.' + self.constructor_name | 403 return rename_type(self.type_name) + '.' + self.constructor_name |
(...skipping 692 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1087 self._database.GetInterface(type_name)) | 1096 self._database.GetInterface(type_name)) |
1088 else: | 1097 else: |
1089 dart_interface_name = type_name | 1098 dart_interface_name = type_name |
1090 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name) | 1099 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name) |
1091 | 1100 |
1092 if type_data.clazz == 'ListLike': | 1101 if type_data.clazz == 'ListLike': |
1093 return ListLikeIDLTypeInfo(type_name, type_data, self.TypeInfo(type_data.i
tem_type)) | 1102 return ListLikeIDLTypeInfo(type_name, type_data, self.TypeInfo(type_data.i
tem_type)) |
1094 | 1103 |
1095 class_name = '%sIDLTypeInfo' % type_data.clazz | 1104 class_name = '%sIDLTypeInfo' % type_data.clazz |
1096 return globals()[class_name](type_name, type_data) | 1105 return globals()[class_name](type_name, type_data) |
OLD | NEW |