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

Side by Side Diff: mojo/public/tools/bindings/pylib/mojom/generate/translate.py

Issue 2864753002: Mojo code generator: simplify how imported types and values are handled. (Closed)
Patch Set: . Created 3 years, 7 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
« no previous file with comments | « mojo/public/tools/bindings/pylib/mojom/generate/module.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 """Convert parse tree to AST. 5 """Convert parse tree to AST.
6 6
7 This module converts the parse tree to the AST we use for code generation. The 7 This module converts the parse tree to the AST we use for code generation. The
8 main entry point is OrderedModule, which gets passed the parser 8 main entry point is OrderedModule, which gets passed the parser
9 representation of a mojom file. When called it's assumed that all imports have 9 representation of a mojom file. When called it's assumed that all imports have
10 already been parsed and converted to ASTs before. 10 already been parsed and converted to ASTs before.
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 second_kind = spec[key_end+2:-1] 215 second_kind = spec[key_end+2:-1]
216 216
217 kind = mojom.Map(_Kind(kinds, first_kind, scope), 217 kind = mojom.Map(_Kind(kinds, first_kind, scope),
218 _Kind(kinds, second_kind, scope)) 218 _Kind(kinds, second_kind, scope))
219 else: 219 else:
220 kind = mojom.Kind(spec) 220 kind = mojom.Kind(spec)
221 221
222 kinds[spec] = kind 222 kinds[spec] = kind
223 return kind 223 return kind
224 224
225 def _KindFromImport(original_kind, imported_from):
226 """Used with 'import module' - clones the kind imported from the given
227 module's namespace. Only used with Structs, Unions, Interfaces and Enums."""
228 kind = copy.copy(original_kind)
229 # |shared_definition| is used to store various properties (see
230 # |AddSharedProperty()| in module.py), including |imported_from|. We don't
231 # want the copy to share these with the original, so copy it if necessary.
232 if hasattr(original_kind, 'shared_definition'):
233 kind.shared_definition = copy.copy(original_kind.shared_definition)
234 kind.imported_from = imported_from
235 return kind
236
237 def _Import(module, import_module): 225 def _Import(module, import_module):
238 import_item = {}
239 import_item['module_name'] = import_module.name
240 import_item['namespace'] = import_module.namespace
241 import_item['module'] = import_module
242
243 # Copy the struct kinds from our imports into the current module. 226 # Copy the struct kinds from our imports into the current module.
244 importable_kinds = (mojom.Struct, mojom.Union, mojom.Enum, mojom.Interface) 227 importable_kinds = (mojom.Struct, mojom.Union, mojom.Enum, mojom.Interface)
245 for kind in import_module.kinds.itervalues(): 228 for kind in import_module.kinds.itervalues():
246 if (isinstance(kind, importable_kinds) and 229 if (isinstance(kind, importable_kinds) and
247 kind.imported_from is None): 230 kind.module.path == import_module.path):
248 kind = _KindFromImport(kind, import_item)
249 module.kinds[kind.spec] = kind 231 module.kinds[kind.spec] = kind
250 # Ditto for values. 232 # Ditto for values.
251 for value in import_module.values.itervalues(): 233 for value in import_module.values.itervalues():
252 if value.imported_from is None: 234 if value.module.path == import_module.path:
253 # Values don't have shared definitions (since they're not nullable), so no
254 # need to do anything special.
255 value = copy.copy(value)
256 value.imported_from = import_item
257 module.values[value.GetSpec()] = value 235 module.values[value.GetSpec()] = value
258 236
259 return import_item 237 return import_module
260 238
261 def _Struct(module, parsed_struct): 239 def _Struct(module, parsed_struct):
262 """ 240 """
263 Args: 241 Args:
264 module: {mojom.Module} Module currently being constructed. 242 module: {mojom.Module} Module currently being constructed.
265 parsed_struct: {ast.Struct} Parsed struct. 243 parsed_struct: {ast.Struct} Parsed struct.
266 244
267 Returns: 245 Returns:
268 {mojom.Struct} AST struct. 246 {mojom.Struct} AST struct.
269 """ 247 """
(...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after
630 {mojom.Module} An AST for the mojom. 608 {mojom.Module} An AST for the mojom.
631 """ 609 """
632 module = _Module(tree, name, imports) 610 module = _Module(tree, name, imports)
633 for interface in module.interfaces: 611 for interface in module.interfaces:
634 next_ordinal = 0 612 next_ordinal = 0
635 for method in interface.methods: 613 for method in interface.methods:
636 if method.ordinal is None: 614 if method.ordinal is None:
637 method.ordinal = next_ordinal 615 method.ordinal = next_ordinal
638 next_ordinal = method.ordinal + 1 616 next_ordinal = method.ordinal + 1
639 return module 617 return module
OLDNEW
« no previous file with comments | « mojo/public/tools/bindings/pylib/mojom/generate/module.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698