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 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 return UpperCamelCase(name) | 135 return UpperCamelCase(name) |
136 # Leave '_' symbols for unexported names. | 136 # Leave '_' symbols for unexported names. |
137 return name[0].lower() + name[1:] | 137 return name[0].lower() + name[1:] |
138 | 138 |
139 # Returns full name of an imported element based on prebuilt dict |_imports|. | 139 # Returns full name of an imported element based on prebuilt dict |_imports|. |
140 # If the |element| is not imported returns formatted name of it. | 140 # If the |element| is not imported returns formatted name of it. |
141 # |element| should have attr 'name'. |exported| argument is used to make | 141 # |element| should have attr 'name'. |exported| argument is used to make |
142 # |FormatName()| calls only. | 142 # |FormatName()| calls only. |
143 def GetFullName(element, exported=True): | 143 def GetFullName(element, exported=True): |
144 return GetQualifiedName( | 144 return GetQualifiedName( |
145 element.name, GetPackageNameForElement(element), exported) | 145 element.name, GetPackageNameForElement(element), exported) |
| 146 |
| 147 # Returns a string of the form package.path.TypeName - the full identifier |
| 148 # for an element. |
| 149 def GetFullIdentifier(element, exported=True): |
| 150 return '%s.%s' % (element.module.namespace, GetNameForElement(element)) |
146 | 151 |
147 def GetUnqualifiedNameForElement(element, exported=True): | 152 def GetUnqualifiedNameForElement(element, exported=True): |
148 return FormatName(element.name, exported) | 153 return FormatName(element.name, exported) |
149 | 154 |
150 # Returns a name for nested elements like enum field or constant. | 155 # Returns a name for nested elements like enum field or constant. |
151 # The returned name consists of camel-cased parts separated by '_'. | 156 # The returned name consists of camel-cased parts separated by '_'. |
152 def GetNameForNestedElement(element): | 157 def GetNameForNestedElement(element): |
153 if element.parent_kind: | 158 if element.parent_kind: |
154 return "%s_%s" % (GetNameForElement(element.parent_kind), | 159 return "%s_%s" % (GetNameForElement(element.parent_kind), |
155 FormatName(element.name)) | 160 FormatName(element.name)) |
(...skipping 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 'is_interface': mojom.IsInterfaceKind, | 355 'is_interface': mojom.IsInterfaceKind, |
351 'is_interface_request': mojom.IsInterfaceRequestKind, | 356 'is_interface_request': mojom.IsInterfaceRequestKind, |
352 'is_map': mojom.IsMapKind, | 357 'is_map': mojom.IsMapKind, |
353 'is_none_or_empty': lambda array: array is None or len(array) == 0, | 358 'is_none_or_empty': lambda array: array is None or len(array) == 0, |
354 'is_nullable': mojom.IsNullableKind, | 359 'is_nullable': mojom.IsNullableKind, |
355 'is_pointer': IsPointer, | 360 'is_pointer': IsPointer, |
356 'is_object': mojom.IsObjectKind, | 361 'is_object': mojom.IsObjectKind, |
357 'is_struct': mojom.IsStructKind, | 362 'is_struct': mojom.IsStructKind, |
358 'is_union': mojom.IsUnionKind, | 363 'is_union': mojom.IsUnionKind, |
359 'qualified': GetQualifiedName, | 364 'qualified': GetQualifiedName, |
| 365 'fullidentifier': GetFullIdentifier, |
360 'mojom_type': GetMojomTypeValue, | 366 'mojom_type': GetMojomTypeValue, |
361 'mojom_type_identifier': GetMojomTypeIdentifier, | 367 'mojom_type_identifier': GetMojomTypeIdentifier, |
362 'name': GetNameForElement, | 368 'name': GetNameForElement, |
363 'unqualified_name': GetUnqualifiedNameForElement, | 369 'unqualified_name': GetUnqualifiedNameForElement, |
364 'package': GetPackageNameForElement, | 370 'package': GetPackageNameForElement, |
365 'tab_indent': lambda s, size = 1: ('\n' + '\t' * size).join(s.splitlines()) | 371 'tab_indent': lambda s, size = 1: ('\n' + '\t' * size).join(s.splitlines()) |
366 } | 372 } |
367 | 373 |
368 # TODO: This value should be settable via arguments. If False, then mojom type | 374 # TODO: This value should be settable via arguments. If False, then mojom type |
369 # information will not be generated. | 375 # information will not be generated. |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 # Overrides the implementation from the base class in order to customize the | 500 # Overrides the implementation from the base class in order to customize the |
495 # struct and field names. | 501 # struct and field names. |
496 def _GetResponseStructFromMethod(self, method): | 502 def _GetResponseStructFromMethod(self, method): |
497 params_class = "%s_%s_ResponseParams" % ( | 503 params_class = "%s_%s_ResponseParams" % ( |
498 GetNameForElement(method.interface), GetNameForElement(method)) | 504 GetNameForElement(method.interface), GetNameForElement(method)) |
499 struct = mojom.Struct(params_class, module=method.interface.module) | 505 struct = mojom.Struct(params_class, module=method.interface.module) |
500 for param in method.response_parameters: | 506 for param in method.response_parameters: |
501 struct.AddField("out%s" % GetNameForElement(param), | 507 struct.AddField("out%s" % GetNameForElement(param), |
502 param.kind, param.ordinal, attributes=param.attributes) | 508 param.kind, param.ordinal, attributes=param.attributes) |
503 return self._AddStructComputedData(False, struct) | 509 return self._AddStructComputedData(False, struct) |
OLD | NEW |