Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Unified Diff: mojo/public/tools/bindings/pylib/mojom/generate/generator.py

Issue 1826063002: Generated dart code should import transitive imports. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/public/tools/bindings/pylib/mojom/generate/generator.py
diff --git a/mojo/public/tools/bindings/pylib/mojom/generate/generator.py b/mojo/public/tools/bindings/pylib/mojom/generate/generator.py
index 1d57528d3582852a19ce12238bfe5b283ed4a015..0e6258a875816ed72c33ef1410b438a2f0e700a6 100644
--- a/mojo/public/tools/bindings/pylib/mojom/generate/generator.py
+++ b/mojo/public/tools/bindings/pylib/mojom/generate/generator.py
@@ -5,6 +5,7 @@
"""Code shared by the various language-specific code generators."""
from functools import partial
+from itertools import chain
import os.path
import re
@@ -62,6 +63,77 @@ class Generator(object):
def GetInterfaces(self):
return map(self._AddInterfaceComputedData, self.module.interfaces)
+ def GetUsedImports(self, module):
+ """GetUsedImports computes the imports that are used in the provided module.
+
+ An import being used means that a type or constant defined in the import is
+ referenced in the provided module.
+
+ Args:
+ module: {module.Module} The module whose used imports are to be computed.
+
+ Returns:
+ {dict<str, dict>} A dictionary of the used imports. The key is the file
+ name as defined in the import's Module.path. The value is a dictionary.
+ The contents of the dictionary is identical to that found in the
+ imported_from field of mojom elements.
+ """
+ used = {}
+
+ def AddImport(element):
+ """AddImport is a utility function that adds the import of the provided
+ element to the used dictionary defined above.
+ """
+ # Only named values or kinds could be imported.
+ if (not isinstance(element, mojom.Kind) and
+ not isinstance(element, mojom.NamedValue)):
+ return
+
+ if mojom.IsArrayKind(element) or mojom.IsInterfaceRequestKind(element):
+ AddImport(element.kind)
+ return
+ if mojom.IsMapKind(element):
+ AddImport(element.key_kind)
+ AddImport(element.value_kind)
+ return
+ if not hasattr(element, 'imported_from') or not element.imported_from:
+ return
+
+ imported_from = element.imported_from
+ used[imported_from['module'].path] = imported_from
+
+ # We want to collect the structs that represent method input and output
+ # parameters.
+ all_structs = list(module.structs)
+ for interface in module.interfaces:
+ for method in interface.methods:
+ all_structs.append(self._GetStructFromMethod(method))
+ if method.response_parameters:
+ all_structs.append(self._GetResponseStructFromMethod(method))
+
+ for struct in all_structs:
+ for field in struct.fields:
+ AddImport(field.kind)
+ if field.default:
+ AddImport(field.default)
+
+ # Enums can be defined in the module, in structs or in interfaces.
+ enum_containers = [module] + module.structs + module.interfaces
+ enums = [c.enums for c in enum_containers]
+ for enum in chain.from_iterable(enums):
+ for field in enum.fields:
+ if field.value:
+ AddImport(field.value)
+
+ for union in module.unions:
+ for field in union.fields:
+ AddImport(field.kind)
+
+ for constant in module.constants:
+ AddImport(constant.value)
+
+ return used
+
# Prepend the filename with a directory that matches the directory of the
# original .mojom file, relative to the import root.
def MatchMojomFilePath(self, filename):
« no previous file with comments | « mojo/public/tools/bindings/generators/mojom_dart_generator.py ('k') | mojo/services/ui/views/interfaces/view_provider.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698