| 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 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 return name[0].lower() + name[1:] | 132 return name[0].lower() + name[1:] |
| 133 | 133 |
| 134 # Returns full name of an imported element. | 134 # Returns full name of an imported element. |
| 135 # If the |element| is not imported returns formatted name of it. | 135 # If the |element| is not imported returns formatted name of it. |
| 136 # |element| should have attr 'name'. |exported| argument is used to make | 136 # |element| should have attr 'name'. |exported| argument is used to make |
| 137 # |FormatName()| calls only. | 137 # |FormatName()| calls only. |
| 138 def GetFullName(element, exported=True): | 138 def GetFullName(element, exported=True): |
| 139 return GetQualifiedName( | 139 return GetQualifiedName( |
| 140 element.name, GetPackageNameForElement(element), exported) | 140 element.name, GetPackageNameForElement(element), exported) |
| 141 | 141 |
| 142 # Returns a string of the form package.path.TypeName - the full identifier | |
| 143 # for an element. | |
| 144 def GetFullIdentifier(element, exported=True): | |
| 145 return '%s.%s' % (element.module.namespace, GetNameForElement(element)) | |
| 146 | |
| 147 def GetUnqualifiedNameForElement(element, exported=True): | 142 def GetUnqualifiedNameForElement(element, exported=True): |
| 148 return FormatName(element.name, exported) | 143 return FormatName(element.name, exported) |
| 149 | 144 |
| 150 # Returns a name for nested elements like enum field or constant. | 145 # Returns a name for nested elements like enum field or constant. |
| 151 # The returned name consists of camel-cased parts separated by '_'. | 146 # The returned name consists of camel-cased parts separated by '_'. |
| 152 def GetNameForNestedElement(element): | 147 def GetNameForNestedElement(element): |
| 153 if element.parent_kind: | 148 if element.parent_kind: |
| 154 return "%s_%s" % (GetNameForElement(element.parent_kind), | 149 return "%s_%s" % (GetNameForElement(element.parent_kind), |
| 155 FormatName(element.name)) | 150 FormatName(element.name)) |
| 156 return GetFullName(element) | 151 return GetFullName(element) |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 path = GetPackagePath(imported['module']) | 295 path = GetPackagePath(imported['module']) |
| 301 if path in imports: | 296 if path in imports: |
| 302 return | 297 return |
| 303 name = GetPackageName(imported['module']) | 298 name = GetPackageName(imported['module']) |
| 304 while name in imports.values(): # This avoids repeated names. | 299 while name in imports.values(): # This avoids repeated names. |
| 305 name += '_' | 300 name += '_' |
| 306 imported['go_name'] = name | 301 imported['go_name'] = name |
| 307 imports[path] = name | 302 imports[path] = name |
| 308 mojom_imports[path] = name | 303 mojom_imports[path] = name |
| 309 | 304 |
| 310 def GetIdentifier(kind): | |
| 311 """Use the kind's module to determine the package and name.""" | |
| 312 # Note: InterfaceRequest's should use the Interface inside them. | |
| 313 if hasattr(kind, 'module'): | |
| 314 package = GetPackageName(kind.module) | |
| 315 name = kind.name | |
| 316 elif mojom.IsInterfaceRequestKind(kind): | |
| 317 package = GetPackageName(kind.kind.module) | |
| 318 name = kind.kind.name | |
| 319 else: | |
| 320 # These kinds (e.g., simple kinds, maps, and arrays) lack identifiers. | |
| 321 raise Exception('Unexpected kind: %s' % kind) | |
| 322 | |
| 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 | |
| 330 class Generator(generator.Generator): | 305 class Generator(generator.Generator): |
| 331 go_filters = { | 306 go_filters = { |
| 332 'array': lambda kind: mojom.Array(kind), | 307 'array': lambda kind: mojom.Array(kind), |
| 333 'bit_size': GetBitSize, | 308 'bit_size': GetBitSize, |
| 334 'decode_suffix': DecodeSuffix, | 309 'decode_suffix': DecodeSuffix, |
| 335 'encode_suffix': EncodeSuffix, | 310 'encode_suffix': EncodeSuffix, |
| 336 'go_type': GetGoType, | 311 'go_type': GetGoType, |
| 337 'expression_to_text': ExpressionToText, | 312 'expression_to_text': ExpressionToText, |
| 338 'has_response': lambda method: method.response_parameters is not None, | 313 'has_response': lambda method: method.response_parameters is not None, |
| 339 'identifier': GetIdentifier, | |
| 340 'is_array': mojom.IsArrayKind, | 314 'is_array': mojom.IsArrayKind, |
| 341 'is_enum': mojom.IsEnumKind, | 315 'is_enum': mojom.IsEnumKind, |
| 342 'is_handle': mojom.IsAnyHandleKind, | 316 'is_handle': mojom.IsAnyHandleKind, |
| 343 'is_interface': mojom.IsInterfaceKind, | 317 'is_interface': mojom.IsInterfaceKind, |
| 344 'is_interface_request': mojom.IsInterfaceRequestKind, | 318 'is_interface_request': mojom.IsInterfaceRequestKind, |
| 345 'is_map': mojom.IsMapKind, | 319 'is_map': mojom.IsMapKind, |
| 346 'is_none_or_empty': lambda array: array is None or len(array) == 0, | 320 'is_none_or_empty': lambda array: array is None or len(array) == 0, |
| 347 'is_nullable': mojom.IsNullableKind, | 321 'is_nullable': mojom.IsNullableKind, |
| 348 'is_pointer': IsPointer, | 322 'is_pointer': IsPointer, |
| 349 'is_object': mojom.IsObjectKind, | 323 'is_object': mojom.IsObjectKind, |
| 350 'is_struct': mojom.IsStructKind, | 324 'is_struct': mojom.IsStructKind, |
| 351 'is_union': mojom.IsUnionKind, | 325 'is_union': mojom.IsUnionKind, |
| 352 'qualified': GetQualifiedName, | 326 'qualified': GetQualifiedName, |
| 353 'fullidentifier': GetFullIdentifier, | 327 'fullidentifier': mojom.GetMojomTypeFullIdentifier, |
| 354 'mojom_type': GetMojomTypeValue, | 328 'mojom_type': GetMojomTypeValue, |
| 355 'mojom_type_identifier': GetMojomTypeIdentifier, | 329 'mojom_type_identifier': mojom.GetMojomTypeIdentifier, |
| 356 'name': GetNameForElement, | 330 'name': GetNameForElement, |
| 357 'unqualified_name': GetUnqualifiedNameForElement, | 331 'unqualified_name': GetUnqualifiedNameForElement, |
| 358 'package': GetPackageNameForElement, | 332 'package': GetPackageNameForElement, |
| 359 'tab_indent': lambda s, size = 1: ('\n' + '\t' * size).join(s.splitlines()) | 333 'tab_indent': lambda s, size = 1: ('\n' + '\t' * size).join(s.splitlines()) |
| 360 } | 334 } |
| 361 | 335 |
| 362 # If set to True, then mojom type information will be generated. | 336 # If set to True, then mojom type information will be generated. |
| 363 should_gen_mojom_types = False | 337 should_gen_mojom_types = False |
| 364 | 338 |
| 365 def GetParameters(self): | 339 def GetParameters(self): |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 487 # Overrides the implementation from the base class in order to customize the | 461 # Overrides the implementation from the base class in order to customize the |
| 488 # struct and field names. | 462 # struct and field names. |
| 489 def _GetResponseStructFromMethod(self, method): | 463 def _GetResponseStructFromMethod(self, method): |
| 490 params_class = "%s_%s_ResponseParams" % ( | 464 params_class = "%s_%s_ResponseParams" % ( |
| 491 GetNameForElement(method.interface), GetNameForElement(method)) | 465 GetNameForElement(method.interface), GetNameForElement(method)) |
| 492 struct = mojom.Struct(params_class, module=method.interface.module) | 466 struct = mojom.Struct(params_class, module=method.interface.module) |
| 493 for param in method.response_parameters: | 467 for param in method.response_parameters: |
| 494 struct.AddField("out%s" % GetNameForElement(param), | 468 struct.AddField("out%s" % GetNameForElement(param), |
| 495 param.kind, param.ordinal, attributes=param.attributes) | 469 param.kind, param.ordinal, attributes=param.attributes) |
| 496 return self._AddStructComputedData(False, struct) | 470 return self._AddStructComputedData(False, struct) |
| OLD | NEW |