Chromium Code Reviews| Index: mojo/public/tools/bindings/generators/mojom_go_generator.py |
| diff --git a/mojo/public/tools/bindings/generators/mojom_go_generator.py b/mojo/public/tools/bindings/generators/mojom_go_generator.py |
| index 7b1c7ac8920cce3fc3d6ca2834880a4a8f1aa672..d2b7279ed01e5bb0b42184d1987488ff06fc4220 100644 |
| --- a/mojo/public/tools/bindings/generators/mojom_go_generator.py |
| +++ b/mojo/public/tools/bindings/generators/mojom_go_generator.py |
| @@ -57,8 +57,20 @@ _kind_infos = { |
| mojom.NULLABLE_STRING: KindInfo('string', 'String', 'String', 64), |
| } |
| +# _imports keeps track of the imports that the .go.mojom file needs to import. |
| _imports = {} |
| +# _mojom_imports keeps a list of the other .mojom files imported by this one. |
| +_mojom_imports = {} |
| + |
| +# The mojom_types.mojom and service_describer.mojom files are special because |
| +# they are used to generate mojom Type's and ServiceDescription implementations. |
| +_service_describer_pkg_short = "service_describer" |
| +_service_describer_pkg = "mojo/public/interfaces/bindings/%s" % \ |
| + _service_describer_pkg_short |
| +_mojom_types_pkg_short = "mojom_types" |
| +_mojom_types_pkg = "mojo/public/interfaces/bindings/%s" % _mojom_types_pkg_short |
| + |
| def GetBitSize(kind): |
| if isinstance(kind, (mojom.Union)): |
| return 128 |
| @@ -186,6 +198,36 @@ def EncodeSuffix(kind): |
| return EncodeSuffix(mojom.MSGPIPE) |
| return _kind_infos[kind].encode_suffix |
| +# This helper assists in the production of mojom_types.Type for simple kinds. |
| +# See _kind_infos above. |
| +def GetMojomTypeValue(kind, typepkg=''): |
| + if not kind in _kind_infos: |
| + return '' |
| + |
| + nullable = 'true' if mojom.IsNullableKind(kind) else 'false' |
| + if kind == mojom.BOOL or kind == mojom.FLOAT or kind == mojom.DOUBLE or mojom.IsIntegralKind(kind): |
| + kind_name = UpperCamelCase(_kind_infos[kind].decode_suffix.upper()) |
| + if kind == mojom.FLOAT: |
| + kind_name = "Float" |
| + elif kind == mojom.DOUBLE: |
| + kind_name = "Double" |
| + return '%sTypeSimpleType{%sSimpleType_%s}' % (typepkg, typepkg, kind_name) |
| + elif mojom.IsAnyHandleKind(kind): |
| + kind_name = 'Unspecified' |
| + if kind == mojom.DCPIPE: |
| + kind_name = 'DataPipeConsumer' |
| + elif kind == mojom.DPPIPE: |
| + kind_name = 'DataPipeProducer' |
| + elif kind == mojom.MSGPIPE: |
| + kind_name = 'MessagePipe' |
| + elif kind == mojom.SHAREDBUFFER: |
| + kind_name = 'SharedBuffer' |
| + return '%sTypeHandleType{%sHandleType{Nullable: %s, Kind: %sHandleType_Kind_%s}}' % (typepkg, typepkg, nullable, typepkg, kind_name) |
|
azani
2015/10/20 23:35:46
Line should be less than 80 chars long. (this appl
alexfandrianto
2015/10/21 00:12:46
I've wrapped those lines.
|
| + elif mojom.IsStringKind(kind): |
| + return '%sTypeStringType{%sStringType{%s}}' % (typepkg, typepkg, nullable) |
| + else: |
| + raise Exception('Missing case for kind: %s' % kind) |
| + |
| def GetPackageName(module): |
| return module.name.split('.')[0] |
| @@ -220,6 +262,7 @@ def GetAllEnums(module): |
| # Adds an import required to use the provided |element|. |
| # The required import is stored at '_imports'. |
| +# The mojom imports are also stored separately in '_mojom_imports'. |
| def AddImport(module, element): |
| if not isinstance(element, mojom.Kind): |
| return |
| @@ -244,10 +287,45 @@ def AddImport(module, element): |
| if path in _imports: |
| return |
| name = GetPackageName(imported['module']) |
| - while name in _imports.values(): |
| + while name in _imports.values(): # This avoids repeated names. |
| name += '_' |
| _imports[path] = name |
| + _mojom_imports[path] = name |
| + |
| +# The identifier cache is used by the Type generator to determine if a type has |
| +# already been generated or not. This prevents over-generation of the same type |
| +# when it is referred to in multiple ways. |
| +identifier_cache = {} |
| +def GetIdentifier(kind): |
| + # Use the kind's module to determine the package name. |
| + if hasattr(kind, 'module'): |
| + package = GetPackageName(kind.module) |
| + elif mojom.IsInterfaceRequestKind(kind): |
| + package = GetPackageName(kind.kind.module) |
| + else: |
| + return '' |
| + # Most kinds have a name, but those that don't should rely on their spec. |
| + # Since spec can have : and ? characters, these must be replaced. Since ? is |
| + # replaced with '', the caller must keep track of optionality on its own. |
| + package_unique = (kind.name if hasattr(kind, 'name') else kind.spec).replace(':', '_').replace('?', '') |
|
azani
2015/10/20 23:35:46
Line is too long. Maybe break this up into a multi
alexfandrianto
2015/10/21 00:12:46
I used two lines, but that would've worked too.
|
| + return '%s_%s' % (package, package_unique) |
| + |
| +def StoreIdentifier(identifier, cache_name): |
| + if not cache_name in identifier_cache: |
| + identifier_cache[cache_name] = {} |
| + identifier_cache[cache_name][identifier] = True |
| + return '' |
| + |
| +def CheckIdentifier(identifier, cache_name): |
| + if not cache_name in identifier_cache: |
| + identifier_cache[cache_name] = {} |
| + return identifier in identifier_cache[cache_name] |
| + |
| +# Get the mojom type's identifier suffix. |
| +def GetMojomTypeIdentifier(kind): |
| + # Since this should be unique, it is based on the type's identifier. |
| + return "%s__" % GetIdentifier(kind) |
| class Generator(generator.Generator): |
| go_filters = { |
| @@ -258,6 +336,9 @@ class Generator(generator.Generator): |
| 'go_type': GetGoType, |
| 'expression_to_text': ExpressionToText, |
| 'has_response': lambda method: method.response_parameters is not None, |
| + 'identifier': GetIdentifier, |
| + 'identifier_check': CheckIdentifier, |
| + 'identifier_store': StoreIdentifier, |
| 'is_array': mojom.IsArrayKind, |
| 'is_enum': mojom.IsEnumKind, |
| 'is_handle': mojom.IsAnyHandleKind, |
| @@ -271,6 +352,8 @@ class Generator(generator.Generator): |
| 'is_struct': mojom.IsStructKind, |
| 'is_union': mojom.IsUnionKind, |
| 'qualified': GetQualifiedName, |
| + 'mojom_type': GetMojomTypeValue, |
| + 'mojom_type_identifier': GetMojomTypeIdentifier, |
| 'name': GetNameForElement, |
| 'unqualified_name': GetUnqualifiedNameForElement, |
| 'package': GetPackageNameForElement, |
| @@ -278,13 +361,17 @@ class Generator(generator.Generator): |
| } |
| def GetParameters(self): |
| + package = GetPackageName(self.module) |
| return { |
| 'enums': GetAllEnums(self.module), |
| - 'imports': self.GetImports(), |
| + 'imports': self.GetImports()[0], |
| 'interfaces': self.GetInterfaces(), |
| - 'package': GetPackageName(self.module), |
| + 'mojom_imports': self.GetMojomImports(), |
| + 'package': package, |
| 'structs': self.GetStructs(), |
| - 'unions': self.GetUnions(), |
| + 'descpkg': '%s.' % _service_describer_pkg_short if package != _service_describer_pkg_short else '', |
| + 'typepkg': '%s.' % _mojom_types_pkg_short if package != _mojom_types_pkg_short else '', |
| + 'unions': self.GetUnions() |
| } |
| @UseJinja('go_templates/source.tmpl', filters=go_filters) |
| @@ -308,8 +395,11 @@ class Generator(generator.Generator): |
| } |
| # Scans |self.module| for elements that require imports and adds all found |
| - # imports to '_imports' dict. Returns a list of imports that should include |
| - # the generated go file. |
| + # imports to '_imports' dict. Mojom imports are stored in the '_mojom_imports' |
| + # dict. This operation is idempotent. |
| + # Returns a tuple: |
| + # - list of imports that should include the generated go file |
| + # - the dictionary of _mojom_imports |
| def GetImports(self): |
| # Imports can only be used in structs, constants, enums, interfaces. |
| all_structs = list(self.module.structs) |
| @@ -344,6 +434,17 @@ class Generator(generator.Generator): |
| if field.value: |
| AddImport(self.module, field.value) |
| + num_user_defined_types = len(self.module.interfaces) + \ |
| + len(self.module.unions) + len(all_structs) + len(GetAllEnums(self.module)) |
| + if num_user_defined_types > 0 \ |
| + and GetPackageName(self.module) != _mojom_types_pkg_short: |
| + _imports[_mojom_types_pkg] = _mojom_types_pkg_short |
| + |
| + if len(self.module.interfaces) > 0 \ |
| + and GetPackageName(self.module) != _mojom_types_pkg_short \ |
| + and GetPackageName(self.module) != _service_describer_pkg_short: |
| + _imports[_service_describer_pkg] = _service_describer_pkg_short |
| + |
| # TODO(rogulenko): add these after generating constants and struct defaults. |
| # for constant in GetAllConstants(self.module): |
| # AddImport(self.module, constant.value) |
| @@ -354,7 +455,11 @@ class Generator(generator.Generator): |
| imports_list.append('"%s"' % i) |
| else: |
| imports_list.append('%s "%s"' % (_imports[i], i)) |
| - return sorted(imports_list) |
| + return sorted(imports_list), _mojom_imports |
| + |
| + def GetMojomImports(self): |
| + # GetImports (idempotent) prepares the _imports and _mojom_imports maps. |
| + return self.GetImports()[1] |
| # Overrides the implementation from the base class in order to customize the |
| # struct and field names. |