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 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 return GetFullName(element) | 151 return GetFullName(element) |
152 | 152 |
153 def GetNameForElement(element, exported=True): | 153 def GetNameForElement(element, exported=True): |
154 if (mojom.IsInterfaceKind(element) or mojom.IsStructKind(element) | 154 if (mojom.IsInterfaceKind(element) or mojom.IsStructKind(element) |
155 or mojom.IsUnionKind(element)): | 155 or mojom.IsUnionKind(element)): |
156 return GetFullName(element, exported) | 156 return GetFullName(element, exported) |
157 if isinstance(element, (mojom.EnumField, | 157 if isinstance(element, (mojom.EnumField, |
158 mojom.Field, | 158 mojom.Field, |
159 mojom.Method, | 159 mojom.Method, |
160 mojom.Parameter)): | 160 mojom.Parameter)): |
161 return FormatName(element.name, exported) | 161 element_name = (element.go_name if hasattr(element, "go_name") |
| 162 else element.name) |
| 163 return FormatName(element_name, exported) |
162 if isinstance(element, (mojom.Enum, | 164 if isinstance(element, (mojom.Enum, |
163 mojom.Constant, | 165 mojom.Constant, |
164 mojom.ConstantValue)): | 166 mojom.ConstantValue)): |
165 return GetNameForNestedElement(element) | 167 return GetNameForNestedElement(element) |
166 raise Exception('Unexpected element: %s' % element) | 168 raise Exception('Unexpected element: %s' % element) |
167 | 169 |
168 def ExpressionToText(token): | 170 def ExpressionToText(token): |
169 if isinstance(token, mojom.EnumValue): | 171 if isinstance(token, mojom.EnumValue): |
170 return "%s_%s" % (GetNameForNestedElement(token.enum), | 172 return "%s_%s" % (GetNameForNestedElement(token.enum), |
171 FormatName(token.name, True)) | 173 FormatName(token.name, True)) |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 # Each Interface has a service description that uses this. | 418 # Each Interface has a service description that uses this. |
417 imports[_service_describer_pkg] = _service_describer_pkg_short | 419 imports[_service_describer_pkg] = _service_describer_pkg_short |
418 | 420 |
419 # TODO(rogulenko): add these after generating constants and struct defaults. | 421 # TODO(rogulenko): add these after generating constants and struct defaults. |
420 # for constant in GetAllConstants(self.module): | 422 # for constant in GetAllConstants(self.module): |
421 # AddImport(imports, mojom_imports, self.module, constant.value) | 423 # AddImport(imports, mojom_imports, self.module, constant.value) |
422 | 424 |
423 return imports, mojom_imports | 425 return imports, mojom_imports |
424 | 426 |
425 # Overrides the implementation from the base class in order to customize the | 427 # Overrides the implementation from the base class in order to customize the |
426 # struct and field names. | 428 # struct and field names. Since the Python objects representing the struct |
| 429 # and fields are shared by all language generators we don't want to actually |
| 430 # modify the |name| property. Instead we add a |go_name| property. |
427 def _GetStructFromMethod(self, method): | 431 def _GetStructFromMethod(self, method): |
428 params_class = "%s_%s_Params" % (GetNameForElement(method.interface), | 432 self._AddStructComputedData(False, method.param_struct) |
429 GetNameForElement(method)) | 433 # Only generate the go_names if they have not already been generated. |
430 struct = mojom.Struct(params_class, module=method.interface.module) | 434 if not hasattr(method.param_struct, "go_name"): |
431 for param in method.parameters: | 435 method.param_struct.go_name = "%s_%s_Params" % ( |
432 struct.AddField("in%s" % GetNameForElement(param), | 436 GetNameForElement(method.interface), GetNameForElement(method)) |
433 param.kind, param.ordinal, attributes=param.attributes) | 437 for field in method.param_struct.fields: |
434 return self._AddStructComputedData(False, struct) | 438 field.go_name = "in%s" % GetNameForElement(field) |
| 439 return method.param_struct |
435 | 440 |
436 # Overrides the implementation from the base class in order to customize the | 441 # Overrides the implementation from the base class in order to customize the |
437 # struct and field names. | 442 # struct and field names. Since the Python objects representing the struct |
| 443 # and fields are shared by all language generators we don't want to actually |
| 444 # modify the |name| property. Instead we add a |go_name| property. |
438 def _GetResponseStructFromMethod(self, method): | 445 def _GetResponseStructFromMethod(self, method): |
439 params_class = "%s_%s_ResponseParams" % ( | 446 self._AddStructComputedData(False, method.response_param_struct) |
440 GetNameForElement(method.interface), GetNameForElement(method)) | 447 if not hasattr(method.response_param_struct, "go_name"): |
441 struct = mojom.Struct(params_class, module=method.interface.module) | 448 # Only generate the go_names if they have not already been generated. |
442 for param in method.response_parameters: | 449 method.response_param_struct.go_name = "%s_%s_ResponseParams" % ( |
443 struct.AddField("out%s" % GetNameForElement(param), | 450 GetNameForElement(method.interface), GetNameForElement(method)) |
444 param.kind, param.ordinal, attributes=param.attributes) | 451 for field in method.response_param_struct.fields: |
445 return self._AddStructComputedData(False, struct) | 452 field.go_name = "out%s" % GetNameForElement(field) |
| 453 return method.response_param_struct |
OLD | NEW |