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 e9b6bfb9735de334c6ac5d52fe6ea575675654ad..638bb77260d2bb50aa56cb7705e1bd8a7367f130 100644 |
--- a/mojo/public/tools/bindings/generators/mojom_go_generator.py |
+++ b/mojo/public/tools/bindings/generators/mojom_go_generator.py |
@@ -4,6 +4,7 @@ |
'''Generates Go source files from a mojom.Module.''' |
+from collections import defaultdict |
from itertools import chain |
import os |
import re |
@@ -14,6 +15,8 @@ import mojom.generate.generator as generator |
import mojom.generate.module as mojom |
import mojom.generate.pack as pack |
+GENERATOR_PREFIX = 'go' |
+ |
class KindInfo(object): |
def __init__(self, go_type, encode_suffix, decode_suffix, bit_size): |
self.go_type = go_type |
@@ -296,36 +299,30 @@ def AddImport(module, element): |
_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 = {} |
+# The identifier_store tracks identifiers that have been used so that code |
+# generation can avoid over-generating code for the same identifier. |
+identifier_store = defaultdict(bool) |
def GetIdentifier(kind): |
- # Use the kind's module to determine the package name. |
+ # Use the kind's module to determine the package and name. |
+ # Note: InterfaceRequestKind's should use the Interface inside them. |
if hasattr(kind, 'module'): |
package = GetPackageName(kind.module) |
+ name = kind.name |
elif mojom.IsInterfaceRequestKind(kind): |
package = GetPackageName(kind.kind.module) |
+ name = kind.kind.name |
else: |
+ # This is for a type that should not be stored. Return an empty identifier. |
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. |
- name_or_spec = (kind.name if hasattr(kind, 'name') else kind.spec) |
- package_unique = name_or_spec.replace(':', '_').replace('?', '') |
- 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 '%s_%s' % (package, name) |
+ |
+def StoreIdentifier(identifier): |
+ identifier_store[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] |
+def CheckIdentifier(identifier): |
+ return identifier in identifier_store |
# Get the mojom type's identifier suffix. |
def GetMojomTypeIdentifier(kind): |
@@ -365,9 +362,8 @@ class Generator(generator.Generator): |
'tab_indent': lambda s, size = 1: ('\n' + '\t' * size).join(s.splitlines()) |
} |
- # TODO: This value should be settable via arguments. If False, then mojom type |
- # information will not be generated. |
- should_gen_mojom_types = True |
+ # If set to True, then mojom type information will be generated. |
+ should_gen_mojom_types = False |
def GetParameters(self): |
package = GetPackageName(self.module) |
@@ -390,6 +386,9 @@ class Generator(generator.Generator): |
return self.GetParameters() |
def GenerateFiles(self, args): |
+ if "--go_gen_types" in args: |
+ self.should_gen_mojom_types = True |
+ |
self.Write(self.GenerateSource(), os.path.join("go", "src", |
GetPackagePath(self.module), "%s.go" % self.module.name)) |