| 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 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 def GetAllConstants(module): | 218 def GetAllConstants(module): |
| 219 data = [module] + module.structs + module.interfaces | 219 data = [module] + module.structs + module.interfaces |
| 220 constants = [x.constants for x in data] | 220 constants = [x.constants for x in data] |
| 221 return [i for i in chain.from_iterable(constants)] | 221 return [i for i in chain.from_iterable(constants)] |
| 222 | 222 |
| 223 def GetAllEnums(module): | 223 def GetAllEnums(module): |
| 224 data = [module] + module.structs + module.interfaces | 224 data = [module] + module.structs + module.interfaces |
| 225 enums = [x.enums for x in data] | 225 enums = [x.enums for x in data] |
| 226 return [i for i in chain.from_iterable(enums)] | 226 return [i for i in chain.from_iterable(enums)] |
| 227 | 227 |
| 228 def GetSerializedRuntimeTypeInfoLiteral(module, enabled): | |
| 229 """ Constructs a string that represents a literal definition in Go of | |
| 230 an array of bytes corresponding to |module.serialized_runtime_type_info|. | |
| 231 | |
| 232 Args: | |
| 233 module: {mojom.Module} the module being processed. | |
| 234 enabled: {bool} Is this feature enabled. | |
| 235 | |
| 236 Returns: A string of the form '{b0, b1, b2,...}' where the 'bi' are | |
| 237 the decimal representation of the bytes of | |
| 238 |module.serialized_runtime_type_info| or the string '{}' if either | |
| 239 |enabled| is false or |module.serialized_runtime_type_info| is None. | |
| 240 Furthermore the returned string will have embedded newline characters inserted | |
| 241 every 1000 characters to make the generated source code more tractable. | |
| 242 """ | |
| 243 if not enabled or not module.serialized_runtime_type_info: | |
| 244 return '{}' | |
| 245 return '{%s}' % ','.join('%s%d' % | |
| 246 ('\n' if index > 0 and index%1000 == 0 else '', b) | |
| 247 for index, b in enumerate(module.serialized_runtime_type_info)) | |
| 248 | |
| 249 def AddImport(imports, mojom_imports, module, element): | 228 def AddImport(imports, mojom_imports, module, element): |
| 250 """Adds an import required to use the provided element. | 229 """Adds an import required to use the provided element. |
| 251 | 230 |
| 252 The required import is stored in the imports parameter. | 231 The required import is stored in the imports parameter. |
| 253 The corresponding mojom import is stored in the mojom_imports parameter. | 232 The corresponding mojom import is stored in the mojom_imports parameter. |
| 254 Each import is also updated to include a 'go_name' entry. The 'go_name' entry | 233 Each import is also updated to include a 'go_name' entry. The 'go_name' entry |
| 255 is the name by which the imported module will be referred to in the generated | 234 is the name by which the imported module will be referred to in the generated |
| 256 code. Because the import dictionary is accessible from the element's | 235 code. Because the import dictionary is accessible from the element's |
| 257 imported_from field this allows us to generate the qualified name for the | 236 imported_from field this allows us to generate the qualified name for the |
| 258 element. | 237 element. |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 'imports': imports, | 313 'imports': imports, |
| 335 'interfaces': self.GetInterfaces(), | 314 'interfaces': self.GetInterfaces(), |
| 336 'mojom_imports': mojom_imports, | 315 'mojom_imports': mojom_imports, |
| 337 'package': package, | 316 'package': package, |
| 338 'structs': self.GetStructs(), | 317 'structs': self.GetStructs(), |
| 339 'descpkg': '%s.' % _service_describer_pkg_short \ | 318 'descpkg': '%s.' % _service_describer_pkg_short \ |
| 340 if package != _service_describer_pkg_short else '', | 319 if package != _service_describer_pkg_short else '', |
| 341 'typepkg': '%s.' % _mojom_types_pkg_short \ | 320 'typepkg': '%s.' % _mojom_types_pkg_short \ |
| 342 if package != _mojom_types_pkg_short else '', | 321 if package != _mojom_types_pkg_short else '', |
| 343 'unions': self.GetUnions(), | 322 'unions': self.GetUnions(), |
| 344 'serialized_runtime_type_info_literal' : ( | |
| 345 GetSerializedRuntimeTypeInfoLiteral(self.module, | |
| 346 self.should_gen_mojom_types)) | |
| 347 } | 323 } |
| 348 | 324 |
| 349 @UseJinja('go_templates/source.tmpl', filters=go_filters) | 325 @UseJinja('go_templates/source.tmpl', filters=go_filters) |
| 350 def GenerateSource(self): | 326 def GenerateSource(self): |
| 351 return self.GetParameters() | 327 return self.GetParameters() |
| 352 | 328 |
| 353 def GenerateFiles(self, args): | 329 def GenerateFiles(self, args): |
| 354 self.should_gen_mojom_types = "--generate_type_info" in args | 330 self.should_gen_mojom_types = "--generate_type_info" in args |
| 355 | 331 |
| 356 self.Write(self.GenerateSource(), os.path.join("go", "src", | 332 self.Write(self.GenerateSource(), os.path.join("go", "src", |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 for enum in GetAllEnums(self.module): | 390 for enum in GetAllEnums(self.module): |
| 415 for field in enum.fields: | 391 for field in enum.fields: |
| 416 if field.value: | 392 if field.value: |
| 417 AddImport(imports, mojom_imports, self.module, field.value) | 393 AddImport(imports, mojom_imports, self.module, field.value) |
| 418 | 394 |
| 419 # Mojom Type generation requires additional imports. | 395 # Mojom Type generation requires additional imports. |
| 420 defInterface = len(self.module.interfaces) > 0 | 396 defInterface = len(self.module.interfaces) > 0 |
| 421 defOtherType = len(self.module.unions) + len(all_structs) + \ | 397 defOtherType = len(self.module.unions) + len(all_structs) + \ |
| 422 len(GetAllEnums(self.module)) > 0 | 398 len(GetAllEnums(self.module)) > 0 |
| 423 | 399 |
| 400 if self.should_gen_mojom_types: |
| 401 imports['bytes'] = 'bytes' |
| 402 imports['compress/gzip'] = 'gzip' |
| 403 imports['encoding/base64'] = 'base64' |
| 404 imports['io/ioutil'] = 'ioutil' |
| 405 |
| 424 if GetPackageName(self.module) != _mojom_types_pkg_short: | 406 if GetPackageName(self.module) != _mojom_types_pkg_short: |
| 425 if defInterface: | 407 if defInterface: |
| 426 # Each Interface has a service description that uses this. | 408 # Each Interface has a service description that uses this. |
| 427 imports[_mojom_types_pkg] = _mojom_types_pkg_short | 409 imports[_mojom_types_pkg] = _mojom_types_pkg_short |
| 428 if defOtherType and self.should_gen_mojom_types: | 410 if defOtherType and self.should_gen_mojom_types: |
| 429 # This import is needed only if generating mojom type definitions. | 411 # This import is needed only if generating mojom type definitions. |
| 430 imports[_mojom_types_pkg] = _mojom_types_pkg_short | 412 imports[_mojom_types_pkg] = _mojom_types_pkg_short |
| 431 | 413 |
| 432 if GetPackageName(self.module) != _service_describer_pkg_short and \ | 414 if GetPackageName(self.module) != _service_describer_pkg_short and \ |
| 433 defInterface: | 415 defInterface: |
| (...skipping 20 matching lines...) Expand all Loading... |
| 454 # Overrides the implementation from the base class in order to customize the | 436 # Overrides the implementation from the base class in order to customize the |
| 455 # struct and field names. | 437 # struct and field names. |
| 456 def _GetResponseStructFromMethod(self, method): | 438 def _GetResponseStructFromMethod(self, method): |
| 457 params_class = "%s_%s_ResponseParams" % ( | 439 params_class = "%s_%s_ResponseParams" % ( |
| 458 GetNameForElement(method.interface), GetNameForElement(method)) | 440 GetNameForElement(method.interface), GetNameForElement(method)) |
| 459 struct = mojom.Struct(params_class, module=method.interface.module) | 441 struct = mojom.Struct(params_class, module=method.interface.module) |
| 460 for param in method.response_parameters: | 442 for param in method.response_parameters: |
| 461 struct.AddField("out%s" % GetNameForElement(param), | 443 struct.AddField("out%s" % GetNameForElement(param), |
| 462 param.kind, param.ordinal, attributes=param.attributes) | 444 param.kind, param.ordinal, attributes=param.attributes) |
| 463 return self._AddStructComputedData(False, struct) | 445 return self._AddStructComputedData(False, struct) |
| OLD | NEW |