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

Side by Side 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, 8 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 unified diff | Download patch
OLDNEW
1 # Copyright 2013 The Chromium Authors. All rights reserved. 1 # Copyright 2013 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 """Code shared by the various language-specific code generators.""" 5 """Code shared by the various language-specific code generators."""
6 6
7 from functools import partial 7 from functools import partial
8 from itertools import chain
8 import os.path 9 import os.path
9 import re 10 import re
10 11
11 import module as mojom 12 import module as mojom
12 import mojom.fileutil as fileutil 13 import mojom.fileutil as fileutil
13 import pack 14 import pack
14 15
15 def ExpectedArraySize(kind): 16 def ExpectedArraySize(kind):
16 if mojom.IsArrayKind(kind): 17 if mojom.IsArrayKind(kind):
17 return kind.length 18 return kind.length
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
55 56
56 def GetStructs(self): 57 def GetStructs(self):
57 return map(partial(self._AddStructComputedData, True), self.module.structs) 58 return map(partial(self._AddStructComputedData, True), self.module.structs)
58 59
59 def GetUnions(self): 60 def GetUnions(self):
60 return self.module.unions 61 return self.module.unions
61 62
62 def GetInterfaces(self): 63 def GetInterfaces(self):
63 return map(self._AddInterfaceComputedData, self.module.interfaces) 64 return map(self._AddInterfaceComputedData, self.module.interfaces)
64 65
66 def GetUsedImports(self, module):
67 """GetUsedImports computes the imports that are used in the provided module.
68
69 An import being used means that a type or constant defined in the import is
70 referenced in the provided module.
71
72 Args:
73 module: {module.Module} The module whose used imports are to be computed.
74
75 Returns:
76 {dict<str, dict>} A dictionary of the used imports. The key is the file
77 name as defined in the import's Module.path. The value is a dictionary.
78 The contents of the dictionary is identical to that found in the
79 imported_from field of mojom elements.
80 """
81 used = {}
82
83 def AddImport(element):
84 """AddImport is a utility function that adds the import of the provided
85 element to the used dictionary defined above.
86 """
87 # Only named values or kinds could be imported.
88 if (not isinstance(element, mojom.Kind) and
89 not isinstance(element, mojom.NamedValue)):
90 return
91
92 if mojom.IsArrayKind(element) or mojom.IsInterfaceRequestKind(element):
93 AddImport(element.kind)
94 return
95 if mojom.IsMapKind(element):
96 AddImport(element.key_kind)
97 AddImport(element.value_kind)
98 return
99 if not hasattr(element, 'imported_from') or not element.imported_from:
100 return
101
102 imported_from = element.imported_from
103 used[imported_from['module'].path] = imported_from
104
105 # We want to collect the structs that represent method input and output
106 # parameters.
107 all_structs = list(module.structs)
108 for interface in module.interfaces:
109 for method in interface.methods:
110 all_structs.append(self._GetStructFromMethod(method))
111 if method.response_parameters:
112 all_structs.append(self._GetResponseStructFromMethod(method))
113
114 for struct in all_structs:
115 for field in struct.fields:
116 AddImport(field.kind)
117 if field.default:
118 AddImport(field.default)
119
120 # Enums can be defined in the module, in structs or in interfaces.
121 enum_containers = [module] + module.structs + module.interfaces
122 enums = [c.enums for c in enum_containers]
123 for enum in chain.from_iterable(enums):
124 for field in enum.fields:
125 if field.value:
126 AddImport(field.value)
127
128 for union in module.unions:
129 for field in union.fields:
130 AddImport(field.kind)
131
132 for constant in module.constants:
133 AddImport(constant.value)
134
135 return used
136
65 # Prepend the filename with a directory that matches the directory of the 137 # Prepend the filename with a directory that matches the directory of the
66 # original .mojom file, relative to the import root. 138 # original .mojom file, relative to the import root.
67 def MatchMojomFilePath(self, filename): 139 def MatchMojomFilePath(self, filename):
68 return os.path.join(os.path.dirname(self.module.path), filename) 140 return os.path.join(os.path.dirname(self.module.path), filename)
69 141
70 def Write(self, contents, filename): 142 def Write(self, contents, filename):
71 if self.output_dir is None: 143 if self.output_dir is None:
72 print contents 144 print contents
73 return 145 return
74 full_path = os.path.join(self.output_dir, filename) 146 full_path = os.path.join(self.output_dir, filename)
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 method.response_param_struct = None 192 method.response_param_struct = None
121 return interface 193 return interface
122 194
123 def _GetStructFromMethod(self, method): 195 def _GetStructFromMethod(self, method):
124 """Returns a method's parameters as a struct.""" 196 """Returns a method's parameters as a struct."""
125 return self._AddStructComputedData(False, method.param_struct) 197 return self._AddStructComputedData(False, method.param_struct)
126 198
127 def _GetResponseStructFromMethod(self, method): 199 def _GetResponseStructFromMethod(self, method):
128 """Returns a method's response_parameters as a struct.""" 200 """Returns a method's response_parameters as a struct."""
129 return self._AddStructComputedData(False, method.response_param_struct) 201 return self._AddStructComputedData(False, method.response_param_struct)
OLDNEW
« 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