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 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 332 | 332 |
| 333 Attributes: | 333 Attributes: |
| 334 overloads: A list of IDL operation overloads with the same name. | 334 overloads: A list of IDL operation overloads with the same name. |
| 335 name: A string, the simple name of the operation. | 335 name: A string, the simple name of the operation. |
| 336 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 |
| 337 is named, e.g. 'fromList' in Int8Array.fromList(list). | 337 is named, e.g. 'fromList' in Int8Array.fromList(list). |
| 338 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. |
| 339 param_infos: A list of ParamInfo. | 339 param_infos: A list of ParamInfo. |
| 340 """ | 340 """ |
| 341 | 341 |
| 342 def ParametersInterfaceDeclaration(self, rename_type): | 342 def ParametersDeclaration(self, rename_type): |
| 343 """Returns a formatted string declaring the parameters for the interface.""" | |
| 344 return self._FormatParams(self.param_infos, rename_type, True) | |
| 345 | |
| 346 def ParametersImplementationDeclaration(self, rename_type): | |
| 347 """Returns a formatted string declaring the parameters for the | |
| 348 implementation. | |
| 349 | |
| 350 Args: | |
| 351 rename_type: A function that allows the types to be renamed. | |
| 352 The function is applied to the parameter's dart_type. | |
| 353 """ | |
| 354 return self._FormatParams(self.param_infos, rename_type, False) | |
| 355 | |
| 356 def ParametersAsArgumentList(self, parameter_count = None): | |
| 357 """Returns a string of the parameter names suitable for passing the | |
| 358 parameters as arguments. | |
| 359 """ | |
| 360 if parameter_count is None: | |
| 361 parameter_count = len(self.param_infos) | |
| 362 return ', '.join(map( | |
| 363 lambda param_info: param_info.name, | |
| 364 self.param_infos[:parameter_count])) | |
| 365 | |
| 366 def _FormatParams(self, params, rename_type, provide_comments): | |
| 367 def FormatParam(param): | 343 def FormatParam(param): |
| 368 dart_type = rename_type(param.type_id) if param.type_id else 'Dynamic' | 344 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) | 345 return '%s%s' % (TypeOrNothing(dart_type, param.type_id), param.name) |
| 370 return '%s%s' % (type, param.name) | |
| 371 | 346 |
| 372 required = [] | 347 required = [] |
| 373 optional = [] | 348 optional = [] |
| 374 for param_info in params: | 349 for param_info in self.param_infos: |
| 375 if param_info.is_optional: | 350 if param_info.is_optional: |
| 376 optional.append(param_info) | 351 optional.append(param_info) |
| 377 else: | 352 else: |
| 378 if optional: | 353 if optional: |
| 379 raise Exception('Optional parameters cannot precede required ones: ' | 354 raise Exception('Optional parameters cannot precede required ones: ' |
| 380 + str(params)) | 355 + str(params)) |
| 381 required.append(param_info) | 356 required.append(param_info) |
| 382 argtexts = map(FormatParam, required) | 357 argtexts = map(FormatParam, required) |
| 383 if optional: | 358 if optional: |
| 384 argtexts.append('[' + ', '.join(map(FormatParam, optional)) + ']') | 359 argtexts.append('[' + ', '.join(map(FormatParam, optional)) + ']') |
| 385 return ', '.join(argtexts) | 360 return ', '.join(argtexts) |
| 386 | 361 |
| 362 def ParametersAsArgumentList(self, parameter_count = None): | |
|
podivilov
2012/10/17 14:21:16
Please move back to reduce the diff.
Anton Muhin
2012/10/17 14:26:30
Cannot, it's a single function :)
| |
| 363 """Returns a string of the parameter names suitable for passing the | |
| 364 parameters as arguments. | |
| 365 """ | |
| 366 if parameter_count is None: | |
| 367 parameter_count = len(self.param_infos) | |
| 368 return ', '.join(map( | |
| 369 lambda param_info: param_info.name, | |
| 370 self.param_infos[:parameter_count])) | |
| 371 | |
| 387 def IsStatic(self): | 372 def IsStatic(self): |
| 388 is_static = self.overloads[0].is_static | 373 is_static = self.overloads[0].is_static |
| 389 assert any([is_static == o.is_static for o in self.overloads]) | 374 assert any([is_static == o.is_static for o in self.overloads]) |
| 390 return is_static | 375 return is_static |
| 391 | 376 |
| 392 def _ConstructorFullName(self, rename_type): | 377 def _ConstructorFullName(self, rename_type): |
| 393 if self.constructor_name: | 378 if self.constructor_name: |
| 394 return rename_type(self.type_name) + '.' + self.constructor_name | 379 return rename_type(self.type_name) + '.' + self.constructor_name |
| 395 else: | 380 else: |
| 396 return rename_type(self.type_name) | 381 return rename_type(self.type_name) |
| 397 | 382 |
| 398 def ConstructorFactoryName(self, rename_type): | 383 def ConstructorFactoryName(self, rename_type): |
| 399 return 'create' + self._ConstructorFullName(rename_type).replace('.', '_') | 384 return 'create' + self._ConstructorFullName(rename_type).replace('.', '_') |
| 400 | 385 |
| 401 def GenerateFactoryInvocation(self, rename_type, emitter, factory_provider): | 386 def GenerateFactoryInvocation(self, rename_type, emitter, factory_provider): |
| 402 has_optional = any(param_info.is_optional | 387 has_optional = any(param_info.is_optional |
| 403 for param_info in self.param_infos) | 388 for param_info in self.param_infos) |
| 404 | 389 |
| 405 factory_name = self.ConstructorFactoryName(rename_type) | 390 factory_name = self.ConstructorFactoryName(rename_type) |
| 406 if not has_optional: | 391 if not has_optional: |
| 407 emitter.Emit( | 392 emitter.Emit( |
| 408 '\n' | 393 '\n' |
| 409 ' factory $CTOR($PARAMS) => ' | 394 ' factory $CTOR($PARAMS) => ' |
| 410 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n', | 395 '$FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n', |
| 411 CTOR=self._ConstructorFullName(rename_type), | 396 CTOR=self._ConstructorFullName(rename_type), |
| 412 PARAMS=self.ParametersInterfaceDeclaration(rename_type), | 397 PARAMS=self.ParametersDeclaration(rename_type), |
| 413 FACTORY=factory_provider, | 398 FACTORY=factory_provider, |
| 414 CTOR_FACTORY_NAME=factory_name, | 399 CTOR_FACTORY_NAME=factory_name, |
| 415 FACTORY_PARAMS=self.ParametersAsArgumentList()) | 400 FACTORY_PARAMS=self.ParametersAsArgumentList()) |
| 416 return | 401 return |
| 417 | 402 |
| 418 dispatcher_emitter = emitter.Emit( | 403 dispatcher_emitter = emitter.Emit( |
| 419 '\n' | 404 '\n' |
| 420 ' factory $CTOR($PARAMS) {\n' | 405 ' factory $CTOR($PARAMS) {\n' |
| 421 '$!DISPATCHER' | 406 '$!DISPATCHER' |
| 422 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' | 407 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' |
| 423 ' }\n', | 408 ' }\n', |
| 424 CTOR=self._ConstructorFullName(rename_type), | 409 CTOR=self._ConstructorFullName(rename_type), |
| 425 PARAMS=self.ParametersInterfaceDeclaration(rename_type), | 410 PARAMS=self.ParametersDeclaration(rename_type), |
| 426 FACTORY=factory_provider, | 411 FACTORY=factory_provider, |
| 427 CTOR_FACTORY_NAME=factory_name, | 412 CTOR_FACTORY_NAME=factory_name, |
| 428 FACTORY_PARAMS=self.ParametersAsArgumentList()) | 413 FACTORY_PARAMS=self.ParametersAsArgumentList()) |
| 429 | 414 |
| 430 # If we have optional parameters, check to see if they are set | 415 # If we have optional parameters, check to see if they are set |
| 431 # and call the appropriate factory method. | 416 # and call the appropriate factory method. |
| 432 def EmitOptionalParameterInvocation(index): | 417 def EmitOptionalParameterInvocation(index): |
| 433 dispatcher_emitter.Emit( | 418 dispatcher_emitter.Emit( |
| 434 ' if (!?$OPT_PARAM_NAME) {\n' | 419 ' if (!?$OPT_PARAM_NAME) {\n' |
| 435 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' | 420 ' return $FACTORY.$CTOR_FACTORY_NAME($FACTORY_PARAMS);\n' |
| (...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1087 self._database.GetInterface(type_name)) | 1072 self._database.GetInterface(type_name)) |
| 1088 else: | 1073 else: |
| 1089 dart_interface_name = type_name | 1074 dart_interface_name = type_name |
| 1090 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name) | 1075 return InterfaceIDLTypeInfo(type_name, type_data, dart_interface_name) |
| 1091 | 1076 |
| 1092 if type_data.clazz == 'ListLike': | 1077 if type_data.clazz == 'ListLike': |
| 1093 return ListLikeIDLTypeInfo(type_name, type_data, self.TypeInfo(type_data.i tem_type)) | 1078 return ListLikeIDLTypeInfo(type_name, type_data, self.TypeInfo(type_data.i tem_type)) |
| 1094 | 1079 |
| 1095 class_name = '%sIDLTypeInfo' % type_data.clazz | 1080 class_name = '%sIDLTypeInfo' % type_data.clazz |
| 1096 return globals()[class_name](type_name, type_data) | 1081 return globals()[class_name](type_name, type_data) |
| OLD | NEW |