| 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 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 path = GetPackagePath(imported['module']) | 300 path = GetPackagePath(imported['module']) |
| 301 if path in imports: | 301 if path in imports: |
| 302 return | 302 return |
| 303 name = GetPackageName(imported['module']) | 303 name = GetPackageName(imported['module']) |
| 304 while name in imports.values(): # This avoids repeated names. | 304 while name in imports.values(): # This avoids repeated names. |
| 305 name += '_' | 305 name += '_' |
| 306 imported['go_name'] = name | 306 imported['go_name'] = name |
| 307 imports[path] = name | 307 imports[path] = name |
| 308 mojom_imports[path] = name | 308 mojom_imports[path] = name |
| 309 | 309 |
| 310 def GetIdentifier(kind): | 310 # Get the mojom type's identifier suffix. |
| 311 """Use the kind's module to determine the package and name.""" | 311 def GetMojomTypeIdentifier(kind): |
| 312 """Get the mojom type's unique identifier from the kind's package and name.""" |
| 312 # Note: InterfaceRequest's should use the Interface inside them. | 313 # Note: InterfaceRequest's should use the Interface inside them. |
| 313 if hasattr(kind, 'module'): | 314 if hasattr(kind, 'module'): |
| 314 package = GetPackageName(kind.module) | 315 package = GetPackageName(kind.module) |
| 315 name = kind.name | 316 name = kind.name |
| 316 elif mojom.IsInterfaceRequestKind(kind): | 317 elif mojom.IsInterfaceRequestKind(kind): |
| 317 package = GetPackageName(kind.kind.module) | 318 package = GetPackageName(kind.kind.module) |
| 318 name = kind.kind.name | 319 name = kind.kind.name |
| 319 else: | 320 else: |
| 320 # These kinds (e.g., simple kinds, maps, and arrays) lack identifiers. | 321 # These kinds (e.g., simple kinds, maps, and arrays) lack identifiers. |
| 321 raise Exception('Unexpected kind: %s' % kind) | 322 raise Exception('Unexpected kind: %s' % kind) |
| 322 | 323 return "%s_%s__" % (package, name) |
| 323 return '%s_%s' % (package, name) | |
| 324 | |
| 325 # Get the mojom type's identifier suffix. | |
| 326 def GetMojomTypeIdentifier(kind): | |
| 327 # Since this should be unique, it is based on the type's identifier. | |
| 328 return "%s__" % GetIdentifier(kind) | |
| 329 | 324 |
| 330 class Generator(generator.Generator): | 325 class Generator(generator.Generator): |
| 331 go_filters = { | 326 go_filters = { |
| 332 'array': lambda kind: mojom.Array(kind), | 327 'array': lambda kind: mojom.Array(kind), |
| 333 'bit_size': GetBitSize, | 328 'bit_size': GetBitSize, |
| 334 'decode_suffix': DecodeSuffix, | 329 'decode_suffix': DecodeSuffix, |
| 335 'encode_suffix': EncodeSuffix, | 330 'encode_suffix': EncodeSuffix, |
| 336 'go_type': GetGoType, | 331 'go_type': GetGoType, |
| 337 'expression_to_text': ExpressionToText, | 332 'expression_to_text': ExpressionToText, |
| 338 'has_response': lambda method: method.response_parameters is not None, | 333 'has_response': lambda method: method.response_parameters is not None, |
| 339 'identifier': GetIdentifier, | |
| 340 'is_array': mojom.IsArrayKind, | 334 'is_array': mojom.IsArrayKind, |
| 341 'is_enum': mojom.IsEnumKind, | 335 'is_enum': mojom.IsEnumKind, |
| 342 'is_handle': mojom.IsAnyHandleKind, | 336 'is_handle': mojom.IsAnyHandleKind, |
| 343 'is_interface': mojom.IsInterfaceKind, | 337 'is_interface': mojom.IsInterfaceKind, |
| 344 'is_interface_request': mojom.IsInterfaceRequestKind, | 338 'is_interface_request': mojom.IsInterfaceRequestKind, |
| 345 'is_map': mojom.IsMapKind, | 339 'is_map': mojom.IsMapKind, |
| 346 'is_none_or_empty': lambda array: array is None or len(array) == 0, | 340 'is_none_or_empty': lambda array: array is None or len(array) == 0, |
| 347 'is_nullable': mojom.IsNullableKind, | 341 'is_nullable': mojom.IsNullableKind, |
| 348 'is_pointer': IsPointer, | 342 'is_pointer': IsPointer, |
| 349 'is_object': mojom.IsObjectKind, | 343 'is_object': mojom.IsObjectKind, |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 487 # Overrides the implementation from the base class in order to customize the | 481 # Overrides the implementation from the base class in order to customize the |
| 488 # struct and field names. | 482 # struct and field names. |
| 489 def _GetResponseStructFromMethod(self, method): | 483 def _GetResponseStructFromMethod(self, method): |
| 490 params_class = "%s_%s_ResponseParams" % ( | 484 params_class = "%s_%s_ResponseParams" % ( |
| 491 GetNameForElement(method.interface), GetNameForElement(method)) | 485 GetNameForElement(method.interface), GetNameForElement(method)) |
| 492 struct = mojom.Struct(params_class, module=method.interface.module) | 486 struct = mojom.Struct(params_class, module=method.interface.module) |
| 493 for param in method.response_parameters: | 487 for param in method.response_parameters: |
| 494 struct.AddField("out%s" % GetNameForElement(param), | 488 struct.AddField("out%s" % GetNameForElement(param), |
| 495 param.kind, param.ordinal, attributes=param.attributes) | 489 param.kind, param.ordinal, attributes=param.attributes) |
| 496 return self._AddStructComputedData(False, struct) | 490 return self._AddStructComputedData(False, struct) |
| OLD | NEW |