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

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

Issue 2863353002: Mojo code generator: change where to add computed data to mojom definitions (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 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 0e64af78a12b6db2fb6adfc863e081099d87081d..04850d71025953807451fb7b5d80dc048054a214 100644
--- a/mojo/public/tools/bindings/pylib/mojom/generate/generator.py
+++ b/mojo/public/tools/bindings/pylib/mojom/generate/generator.py
@@ -12,17 +12,25 @@ import module as mojom
import mojom.fileutil as fileutil
import pack
+
def ExpectedArraySize(kind):
if mojom.IsArrayKind(kind):
return kind.length
return None
-def StudlyCapsToCamel(studly):
- return studly[0].lower() + studly[1:]
-def UnderToCamel(under):
- """Converts underscore_separated strings to CamelCase strings."""
- return ''.join(word.capitalize() for word in under.split('_'))
+def ToCamel(identifier, lower_initial=False, dilimiter='_'):
+ """Splits |identifier| using |dilimiter|, makes the first character of each
+ word uppercased (but makes the first character of the first word lowercased
+ if |lower_initial| is set to True), and joins the words. Please note that for
+ each word, all the characters except the first one are untouched.
+ """
+ result = ''.join(
+ word[0].upper() + word[1:] for word in identifier.split(dilimiter))
+ if lower_initial:
+ result = result[0].lower() + result[1:]
+ return result
+
def WriteFile(contents, full_path):
# Make sure the containing directory exists.
@@ -33,121 +41,115 @@ def WriteFile(contents, full_path):
with open(full_path, "w+") as f:
f.write(contents)
-class Generator(object):
- # Pass |output_dir| to emit files to disk. Omit |output_dir| to echo all
- # files to stdout.
- def __init__(self, module, output_dir=None, typemap=None, variant=None,
- bytecode_path=None, for_blink=False, use_once_callback=False,
- use_new_js_bindings=False, export_attribute=None,
- export_header=None, generate_non_variant_code=False):
- self.module = module
- self.output_dir = output_dir
- self.typemap = typemap or {}
- self.variant = variant
- self.bytecode_path = bytecode_path
- self.for_blink = for_blink
- self.use_once_callback = use_once_callback
- self.use_new_js_bindings = use_new_js_bindings
- self.export_attribute = export_attribute
- self.export_header = export_header
- self.generate_non_variant_code = generate_non_variant_code
-
- def GetStructsFromMethods(self):
- result = []
- for interface in self.module.interfaces:
- for method in interface.methods:
- result.append(self._GetStructFromMethod(method))
- if method.response_parameters != None:
- result.append(self._GetResponseStructFromMethod(method))
- return result
-
- def GetStructs(self):
- return map(partial(self._AddStructComputedData, True), self.module.structs)
-
- def GetUnions(self):
- return map(self._AddUnionComputedData, self.module.unions)
-
- def GetInterfaces(self):
- return map(self._AddInterfaceComputedData, self.module.interfaces)
- # Prepend the filename with a directory that matches the directory of the
- # original .mojom file, relative to the import root.
- def MatchMojomFilePath(self, filename):
- return os.path.join(os.path.dirname(self.module.path), filename)
-
- def Write(self, contents, filename):
- if self.output_dir is None:
- print contents
- return
- full_path = os.path.join(self.output_dir, filename)
- WriteFile(contents, full_path)
-
- def GenerateFiles(self, args):
- raise NotImplementedError("Subclasses must override/implement this method")
-
- def GetJinjaParameters(self):
- """Returns default constructor parameters for the jinja environment."""
- return {}
+def AddComputedData(module):
+ """Adds computed data to the given module. The data is computed once and
+ used repeatedly in the generation process."""
- def GetGlobals(self):
- """Returns global mappings for the template generation."""
- return {}
-
- def _AddStructComputedData(self, exported, struct):
- """Adds computed data to the given struct. The data is computed once and
- used repeatedly in the generation process."""
+ def _AddStructComputedData(exported, struct):
struct.packed = pack.PackedStruct(struct)
struct.bytes = pack.GetByteLayout(struct.packed)
struct.versions = pack.GetVersionInfo(struct.packed)
struct.exported = exported
- return struct
- def _AddUnionComputedData(self, union):
- """Adds computed data to the given union. The data is computed once and
- used repeatedly in the generation process."""
+ def _AddUnionComputedData(union):
ordinal = 0
for field in union.fields:
if field.ordinal is not None:
ordinal = field.ordinal
field.ordinal = ordinal
ordinal += 1
- return union
- def _AddInterfaceComputedData(self, interface):
- """Adds computed data to the given interface. The data is computed once and
- used repeatedly in the generation process."""
+ def _AddInterfaceComputedData(interface):
+ next_ordinal = 0
interface.version = 0
for method in interface.methods:
+ if method.ordinal is None:
+ method.ordinal = next_ordinal
+ next_ordinal = method.ordinal + 1
+
if method.min_version is not None:
interface.version = max(interface.version, method.min_version)
- method.param_struct = self._GetStructFromMethod(method)
+ method.param_struct = _GetStructFromMethod(method)
interface.version = max(interface.version,
method.param_struct.versions[-1].version)
if method.response_parameters is not None:
- method.response_param_struct = self._GetResponseStructFromMethod(method)
+ method.response_param_struct = _GetResponseStructFromMethod(method)
interface.version = max(
interface.version,
method.response_param_struct.versions[-1].version)
else:
method.response_param_struct = None
- return interface
- def _GetStructFromMethod(self, method):
+ def _GetStructFromMethod(method):
"""Converts a method's parameters into the fields of a struct."""
params_class = "%s_%s_Params" % (method.interface.name, method.name)
struct = mojom.Struct(params_class, module=method.interface.module)
for param in method.parameters:
struct.AddField(param.name, param.kind, param.ordinal,
attributes=param.attributes)
- return self._AddStructComputedData(False, struct)
+ _AddStructComputedData(False, struct)
+ return struct
- def _GetResponseStructFromMethod(self, method):
+ def _GetResponseStructFromMethod(method):
"""Converts a method's response_parameters into the fields of a struct."""
params_class = "%s_%s_ResponseParams" % (method.interface.name, method.name)
struct = mojom.Struct(params_class, module=method.interface.module)
for param in method.response_parameters:
struct.AddField(param.name, param.kind, param.ordinal,
attributes=param.attributes)
- return self._AddStructComputedData(False, struct)
+ _AddStructComputedData(False, struct)
+ return struct
+
+ for struct in module.structs:
+ _AddStructComputedData(True, struct)
+ for union in module.unions:
+ _AddUnionComputedData(union)
+ for interface in module.interfaces:
+ _AddInterfaceComputedData(interface)
+
+
+class Generator(object):
+ # Pass |output_dir| to emit files to disk. Omit |output_dir| to echo all
+ # files to stdout.
+ def __init__(self, module, output_dir=None, typemap=None, variant=None,
+ bytecode_path=None, for_blink=False, use_once_callback=False,
+ use_new_js_bindings=False, export_attribute=None,
+ export_header=None, generate_non_variant_code=False):
+ self.module = module
+ self.output_dir = output_dir
+ self.typemap = typemap or {}
+ self.variant = variant
+ self.bytecode_path = bytecode_path
+ self.for_blink = for_blink
+ self.use_once_callback = use_once_callback
+ self.use_new_js_bindings = use_new_js_bindings
+ self.export_attribute = export_attribute
+ self.export_header = export_header
+ self.generate_non_variant_code = generate_non_variant_code
+
+ # Prepend the filename with a directory that matches the directory of the
+ # original .mojom file, relative to the import root.
+ def MatchMojomFilePath(self, filename):
+ return os.path.join(os.path.dirname(self.module.path), filename)
+
+ def Write(self, contents, filename):
+ if self.output_dir is None:
+ print contents
+ return
+ full_path = os.path.join(self.output_dir, filename)
+ WriteFile(contents, full_path)
+
+ def GenerateFiles(self, args):
+ raise NotImplementedError("Subclasses must override/implement this method")
+
+ def GetJinjaParameters(self):
+ """Returns default constructor parameters for the jinja environment."""
+ return {}
+
+ def GetGlobals(self):
+ """Returns global mappings for the template generation."""
+ return {}
+

Powered by Google App Engine
This is Rietveld 408576698