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 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 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 339 overloads: A list of IDL operation overloads with the same name. | 339 overloads: A list of IDL operation overloads with the same name. |
| 340 name: A string, the simple name of the operation. | 340 name: A string, the simple name of the operation. |
| 341 constructor_name: A string, the name of the constructor iff the constructor | 341 constructor_name: A string, the name of the constructor iff the constructor |
| 342 is named, e.g. 'fromList' in Int8Array.fromList(list). | 342 is named, e.g. 'fromList' in Int8Array.fromList(list). |
| 343 type_name: A string, the name of the return type of the operation. | 343 type_name: A string, the name of the return type of the operation. |
| 344 param_infos: A list of ParamInfo. | 344 param_infos: A list of ParamInfo. |
| 345 """ | 345 """ |
| 346 | 346 |
| 347 def ParametersInterfaceDeclaration(self, rename_type): | 347 def ParametersInterfaceDeclaration(self, rename_type): |
| 348 """Returns a formatted string declaring the parameters for the interface.""" | 348 """Returns a formatted string declaring the parameters for the interface.""" |
| 349 def type_function(param): | 349 return self._FormatParams(self.param_infos, rename_type, True) |
| 350 # TODO(podivilov): replace param.dart_type field with param.is_optional | |
| 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) | |
| 355 return self._FormatParams(self.param_infos, type_function) | |
| 356 | 350 |
| 357 def ParametersImplementationDeclaration(self, rename_type): | 351 def ParametersImplementationDeclaration(self, rename_type): |
| 358 """Returns a formatted string declaring the parameters for the | 352 """Returns a formatted string declaring the parameters for the |
| 359 implementation. | 353 implementation. |
| 360 | 354 |
| 361 Args: | 355 Args: |
| 362 rename_type: A function that allows the types to be renamed. | 356 rename_type: A function that allows the types to be renamed. |
| 363 The function is applied to the parameter's dart_type. | 357 The function is applied to the parameter's dart_type. |
| 364 """ | 358 """ |
| 365 def type_function(param): | 359 return self._FormatParams(self.param_infos, rename_type, False) |
| 366 # TODO(podivilov): replace param.dart_type field with param.is_optional | |
| 367 dart_type = param.dart_type | |
| 368 if dart_type != 'Dynamic': | |
| 369 dart_type = rename_type(dart_type) | |
| 370 return TypeOrNothing(dart_type) | |
| 371 return self._FormatParams(self.param_infos, type_function) | |
| 372 | 360 |
| 373 def ParametersAsArgumentList(self, parameter_count = None): | 361 def ParametersAsArgumentList(self, parameter_count = None): |
| 374 """Returns a string of the parameter names suitable for passing the | 362 """Returns a string of the parameter names suitable for passing the |
| 375 parameters as arguments. | 363 parameters as arguments. |
| 376 """ | 364 """ |
| 377 if parameter_count is None: | 365 if parameter_count is None: |
| 378 parameter_count = len(self.param_infos) | 366 parameter_count = len(self.param_infos) |
| 379 return ', '.join(map( | 367 return ', '.join(map( |
| 380 lambda param_info: param_info.name, | 368 lambda param_info: param_info.name, |
| 381 self.param_infos[:parameter_count])) | 369 self.param_infos[:parameter_count])) |
| 382 | 370 |
| 383 def _FormatParams(self, params, type_fn): | 371 def _FormatParams(self, params, rename_type, provide_comments): |
|
Anton Muhin
2012/10/17 12:30:16
we can simplify logic even further if we unify int
podivilov
2012/10/17 12:38:04
I agree.
| |
| 384 def FormatParam(param): | 372 def FormatParam(param): |
| 385 """Returns a parameter declaration fragment for an ParamInfo.""" | 373 # TODO(podivilov): replace param.dart_type field with param.is_optional |
| 386 return '%s%s' % (type_fn(param), param.name) | 374 dart_type = param.dart_type |
| 375 if dart_type != 'Dynamic': | |
| 376 dart_type = rename_type(dart_type) | |
| 377 type = TypeOrNothing(dart_type, param.type_id if provide_comments else Non e) | |
| 378 return '%s%s' % (type, param.name) | |
| 387 | 379 |
| 388 required = [] | 380 required = [] |
| 389 optional = [] | 381 optional = [] |
| 390 for param_info in params: | 382 for param_info in params: |
| 391 if param_info.is_optional: | 383 if param_info.is_optional: |
| 392 optional.append(param_info) | 384 optional.append(param_info) |
| 393 else: | 385 else: |
| 394 if optional: | 386 if optional: |
| 395 raise Exception('Optional parameters cannot precede required ones: ' | 387 raise Exception('Optional parameters cannot precede required ones: ' |
| 396 + str(params)) | 388 + str(params)) |
| (...skipping 706 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1103 self._database.GetInterface(type_name)) | 1095 self._database.GetInterface(type_name)) |
| 1104 else: | 1096 else: |
| 1105 dart_interface_name = type_name | 1097 dart_interface_name = type_name |
| 1106 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name) | 1098 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name) |
| 1107 | 1099 |
| 1108 if type_data.clazz == 'ListLike': | 1100 if type_data.clazz == 'ListLike': |
| 1109 return ListLikeIDLTypeInfo(type_name, type_data, self.TypeInfo(type_data.i tem_type)) | 1101 return ListLikeIDLTypeInfo(type_name, type_data, self.TypeInfo(type_data.i tem_type)) |
| 1110 | 1102 |
| 1111 class_name = '%sIDLTypeInfo' % type_data.clazz | 1103 class_name = '%sIDLTypeInfo' % type_data.clazz |
| 1112 return globals()[class_name](type_name, type_data) | 1104 return globals()[class_name](type_name, type_data) |
| OLD | NEW |