Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. | 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 '''Generates Go source files from a mojom.Module.''' | 5 '''Generates Go source files from a mojom.Module.''' |
| 6 | 6 |
| 7 from itertools import chain | 7 from itertools import chain |
| 8 import os | 8 import os |
| 9 import re | 9 import re |
| 10 | 10 |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 247 def GetAllConstants(module): | 247 def GetAllConstants(module): |
| 248 data = [module] + module.structs + module.interfaces | 248 data = [module] + module.structs + module.interfaces |
| 249 constants = [x.constants for x in data] | 249 constants = [x.constants for x in data] |
| 250 return [i for i in chain.from_iterable(constants)] | 250 return [i for i in chain.from_iterable(constants)] |
| 251 | 251 |
| 252 def GetAllEnums(module): | 252 def GetAllEnums(module): |
| 253 data = [module] + module.structs + module.interfaces | 253 data = [module] + module.structs + module.interfaces |
| 254 enums = [x.enums for x in data] | 254 enums = [x.enums for x in data] |
| 255 return [i for i in chain.from_iterable(enums)] | 255 return [i for i in chain.from_iterable(enums)] |
| 256 | 256 |
| 257 def GetSerializedRuntimeTypeInfoLiteral(module, enabled): | |
| 258 """ Constructs a string that represents a literal definition in Go of | |
| 259 an array of bytes corresponding to |module.serialized_runtime_type_info|. | |
| 260 | |
| 261 Args: | |
| 262 module: {mojom.Module} the module being processed. | |
| 263 enabled: {bool} Is this feature enabled. | |
| 264 | |
| 265 Returns: A string of the form '{b0, b1, b2,...}' where the 'bi' are | |
| 266 the decimal representation of the bytes of | |
| 267 |module.serialized_runtime_type_info| or the string '{}' if either | |
| 268 |enabled| is false or |module.serialized_runtime_type_info| is None. | |
| 269 Furthermore the returned string will have embedded newline characters inserted | |
| 270 every 1000 characters to make the generated source code more tractable. | |
| 271 """ | |
| 272 if not enabled or not module.serialized_runtime_type_info: | |
| 273 return '{}' | |
| 274 return '{%s}' % ','.join('%s%d' % | |
| 275 ('\n' if index > 0 and index%1000 == 0 else '', b) | |
| 276 for index, b in enumerate(module.serialized_runtime_type_info)) | |
| 277 | |
| 257 def AddImport(imports, mojom_imports, module, element): | 278 def AddImport(imports, mojom_imports, module, element): |
| 258 """Adds an import required to use the provided element. | 279 """Adds an import required to use the provided element. |
| 259 | 280 |
| 260 The required import is stored in the imports parameter. | 281 The required import is stored in the imports parameter. |
| 261 The corresponding mojom import is stored in the mojom_imports parameter. | 282 The corresponding mojom import is stored in the mojom_imports parameter. |
| 262 Each import is also updated to include a 'go_name' entry. The 'go_name' entry | 283 Each import is also updated to include a 'go_name' entry. The 'go_name' entry |
| 263 is the name by which the imported module will be referred to in the generated | 284 is the name by which the imported module will be referred to in the generated |
| 264 code. Because the import dictionary is accessible from the element's | 285 code. Because the import dictionary is accessible from the element's |
| 265 imported_from field this allows us to generate the qualified name for the | 286 imported_from field this allows us to generate the qualified name for the |
| 266 element. | 287 element. |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 'is_interface_request': mojom.IsInterfaceRequestKind, | 339 'is_interface_request': mojom.IsInterfaceRequestKind, |
| 319 'is_map': mojom.IsMapKind, | 340 'is_map': mojom.IsMapKind, |
| 320 'is_none_or_empty': lambda array: array is None or len(array) == 0, | 341 'is_none_or_empty': lambda array: array is None or len(array) == 0, |
| 321 'is_nullable': mojom.IsNullableKind, | 342 'is_nullable': mojom.IsNullableKind, |
| 322 'is_pointer': IsPointer, | 343 'is_pointer': IsPointer, |
| 323 'is_object': mojom.IsObjectKind, | 344 'is_object': mojom.IsObjectKind, |
| 324 'is_struct': mojom.IsStructKind, | 345 'is_struct': mojom.IsStructKind, |
| 325 'is_union': mojom.IsUnionKind, | 346 'is_union': mojom.IsUnionKind, |
| 326 'qualified': GetQualifiedName, | 347 'qualified': GetQualifiedName, |
| 327 'fullidentifier': mojom.GetMojomTypeFullIdentifier, | 348 'fullidentifier': mojom.GetMojomTypeFullIdentifier, |
| 328 'mojom_type': GetMojomTypeValue, | 349 'mojom_type': GetMojomTypeValue, |
|
alexfandrianto
2016/02/23 21:19:47
I think a lot of these filters don't need to be he
rudominer
2016/02/24 02:06:53
Done.
| |
| 329 'mojom_type_identifier': mojom.GetMojomTypeIdentifier, | |
| 330 'name': GetNameForElement, | 350 'name': GetNameForElement, |
| 331 'unqualified_name': GetUnqualifiedNameForElement, | 351 'unqualified_name': GetUnqualifiedNameForElement, |
| 332 'package': GetPackageNameForElement, | 352 'package': GetPackageNameForElement, |
| 333 'tab_indent': lambda s, size = 1: ('\n' + '\t' * size).join(s.splitlines()) | 353 'tab_indent': lambda s, size = 1: ('\n' + '\t' * size).join(s.splitlines()) |
| 334 } | 354 } |
| 335 | 355 |
| 336 # If set to True, then mojom type information will be generated. | 356 # If set to True, then mojom type information will be generated. |
| 337 should_gen_mojom_types = False | 357 should_gen_mojom_types = False |
| 338 | 358 |
| 339 def GetParameters(self): | 359 def GetParameters(self): |
| 340 package = GetPackageName(self.module) | 360 package = GetPackageName(self.module) |
| 341 imports, mojom_imports = self.GetImports() | 361 imports, mojom_imports = self.GetImports() |
| 342 return { | 362 return { |
| 343 'enums': GetAllEnums(self.module), | 363 'enums': GetAllEnums(self.module), |
| 344 'imports': imports, | 364 'imports': imports, |
| 345 'interfaces': self.GetInterfaces(), | 365 'interfaces': self.GetInterfaces(), |
| 346 'mojom_imports': mojom_imports, | 366 'mojom_imports': mojom_imports, |
| 347 'package': package, | 367 'package': package, |
| 348 'structs': self.GetStructs(), | 368 'structs': self.GetStructs(), |
| 349 'descpkg': '%s.' % _service_describer_pkg_short \ | 369 'descpkg': '%s.' % _service_describer_pkg_short \ |
| 350 if package != _service_describer_pkg_short else '', | 370 if package != _service_describer_pkg_short else '', |
| 351 'typepkg': '%s.' % _mojom_types_pkg_short \ | 371 'typepkg': '%s.' % _mojom_types_pkg_short \ |
| 352 if package != _mojom_types_pkg_short else '', | 372 if package != _mojom_types_pkg_short else '', |
| 353 'unions': self.GetUnions() | 373 'unions': self.GetUnions(), |
| 374 'serialized_runtime_type_info_literal' : ( | |
| 375 GetSerializedRuntimeTypeInfoLiteral(self.module, | |
| 376 self.should_gen_mojom_types)) | |
| 354 } | 377 } |
| 355 | 378 |
| 356 @UseJinja('go_templates/source.tmpl', filters=go_filters) | 379 @UseJinja('go_templates/source.tmpl', filters=go_filters) |
| 357 def GenerateSource(self): | 380 def GenerateSource(self): |
| 358 return self.GetParameters() | 381 return self.GetParameters() |
| 359 | 382 |
| 360 def GenerateFiles(self, args): | 383 def GenerateFiles(self, args): |
| 361 self.should_gen_mojom_types = "--generate_type_info" in args | 384 self.should_gen_mojom_types = "--generate_type_info" in args |
| 362 | 385 |
| 363 self.Write(self.GenerateSource(), os.path.join("go", "src", | 386 self.Write(self.GenerateSource(), os.path.join("go", "src", |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 461 # Overrides the implementation from the base class in order to customize the | 484 # Overrides the implementation from the base class in order to customize the |
| 462 # struct and field names. | 485 # struct and field names. |
| 463 def _GetResponseStructFromMethod(self, method): | 486 def _GetResponseStructFromMethod(self, method): |
| 464 params_class = "%s_%s_ResponseParams" % ( | 487 params_class = "%s_%s_ResponseParams" % ( |
| 465 GetNameForElement(method.interface), GetNameForElement(method)) | 488 GetNameForElement(method.interface), GetNameForElement(method)) |
| 466 struct = mojom.Struct(params_class, module=method.interface.module) | 489 struct = mojom.Struct(params_class, module=method.interface.module) |
| 467 for param in method.response_parameters: | 490 for param in method.response_parameters: |
| 468 struct.AddField("out%s" % GetNameForElement(param), | 491 struct.AddField("out%s" % GetNameForElement(param), |
| 469 param.kind, param.ordinal, attributes=param.attributes) | 492 param.kind, param.ordinal, attributes=param.attributes) |
| 470 return self._AddStructComputedData(False, struct) | 493 return self._AddStructComputedData(False, struct) |
| OLD | NEW |