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

Unified Diff: mojo/public/tools/bindings/generators/mojom_dart_generator.py

Issue 1433183002: Generate Mojom Types for Dart (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Simplify identifier_store for Go and Dart Created 5 years, 1 month 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/generators/mojom_dart_generator.py
diff --git a/mojo/public/tools/bindings/generators/mojom_dart_generator.py b/mojo/public/tools/bindings/generators/mojom_dart_generator.py
index 034588680ec55e6a4a00a4cedc2f2b8a4e1cfe89..0eab1b9d9fbf2bc7f3c744da83272de9e67a370f 100644
--- a/mojo/public/tools/bindings/generators/mojom_dart_generator.py
+++ b/mojo/public/tools/bindings/generators/mojom_dart_generator.py
@@ -4,6 +4,7 @@
"""Generates dart source files from a mojom.Module."""
+from collections import defaultdict
zra 2015/11/24 17:34:44 I'm not familiar with python conventions for impor
azani 2015/11/24 20:02:02 The prescribed style is that you import the module
alexfandrianto 2015/12/18 01:53:19 Not needed anymore. No more identifiercache
import os
import re
import shutil
@@ -70,6 +71,32 @@ _kind_to_dart_decl_type = {
mojom.NULLABLE_STRING: "String"
}
+_kind_to_mojom_type = {
+ mojom.BOOL: "BOOL",
+ mojom.INT8: "INT8",
+ mojom.UINT8: "UINT8",
+ mojom.INT16: "INT16",
+ mojom.UINT16: "UINT16",
+ mojom.INT32: "INT32",
+ mojom.UINT32: "UINT32",
+ mojom.FLOAT: "FLOAT",
+ mojom.HANDLE: "UNSPECIFIED",
+ mojom.DCPIPE: "DATA_PIPE_CONSUMER",
+ mojom.DPPIPE: "DATA_PIPE_PRODUCER",
+ mojom.MSGPIPE: "MESSAGE_PIPE",
+ mojom.SHAREDBUFFER: "SHARED_BUFFER",
+ mojom.NULLABLE_HANDLE: "UNSPECIFIED",
+ mojom.NULLABLE_DCPIPE: "DATA_PIPE_CONSUMER",
+ mojom.NULLABLE_DPPIPE: "DATA_PIPE_PRODUCER",
+ mojom.NULLABLE_MSGPIPE: "MESSAGE_PIPE",
+ mojom.NULLABLE_SHAREDBUFFER: "SHARED_BUFFER",
+ mojom.INT64: "INT64",
+ mojom.UINT64: "UINT64",
+ mojom.DOUBLE: "DOUBLE",
+ mojom.STRING: "STRING",
+ mojom.NULLABLE_STRING: "STRING"
+}
+
_spec_to_decode_method = {
mojom.BOOL.spec: 'decodeBool',
mojom.DCPIPE.spec: 'decodeConsumerHandle',
@@ -122,6 +149,15 @@ _spec_to_encode_method = {
mojom.UINT8.spec: 'encodeUint8',
}
+# The mojom_types.mojom and service_describer.mojom files are special because
zra 2015/11/24 17:34:44 It looks like you might be trying to avoid importi
alexfandrianto 2015/12/18 01:53:19 Done.
+# they are used to generate mojom Type's and ServiceDescription implementations.
+_service_describer_pkg_short = "service_describer"
+_service_describer_pkg = "package:mojo/mojo/%s.mojom.dart" % \
+ _service_describer_pkg_short
+_mojom_types_pkg_short = "mojom_types"
+_mojom_types_pkg = "package:mojo/mojo/%s.mojom.dart" % \
+ _mojom_types_pkg_short
+
def GetDartType(kind):
if kind.imported_from:
return kind.imported_from["unique_name"] + "." + GetNameForElement(kind)
@@ -172,6 +208,27 @@ def DartDeclType(kind):
if mojom.IsEnumKind(kind):
return GetDartType(kind)
+def GetMojomTypeValue(kind, typepkg=''):
+ if not kind in _kind_to_mojom_type:
+ return ''
+
+ nullable = 'true' if mojom.IsNullableKind(kind) else 'false'
+ w = _kind_to_mojom_type[kind]
+ if kind == mojom.BOOL or kind == mojom.FLOAT or kind == mojom.DOUBLE or \
+ mojom.IsIntegralKind(kind):
+
+ return 'new %sType()..simpleType = %sSimpleType.%s' % (typepkg, typepkg, w)
+ elif mojom.IsAnyHandleKind(kind):
+ return ('new %sType()\n..handleType = (new %sHandleType()' +
+ '\n..kind = %sHandleTypeKind.%s' +
+ '\n..nullable = %s)') % \
+ (typepkg, typepkg, typepkg, w, nullable)
+ elif mojom.IsStringKind(kind):
+ return 'new %sType()\n..stringType = (new %sStringType()..nullable = %s)' \
+ % (typepkg, typepkg, nullable)
+ else:
+ raise Exception('Missing case for kind: %s' % kind)
+
def NameToComponent(name):
# insert '_' between anything and a Title name (e.g, HTTPEntry2FooBar ->
# HTTP_Entry2_FooBar)
@@ -382,6 +439,9 @@ def IsPointerArrayKind(kind):
def IsEnumArrayKind(kind):
return mojom.IsArrayKind(kind) and mojom.IsEnumKind(kind.kind)
+def IsImportedKind(kind):
+ return hasattr(kind, 'imported_from') and kind.imported_from
+
def ParseStringAttribute(attribute):
assert isinstance(attribute, basestring)
return attribute
@@ -392,12 +452,45 @@ def GetPackage(module):
# Default package.
return 'mojom'
+def GetPackageName(module):
+ return module.name.split('.')[0]
+
def GetImportUri(module):
package = GetPackage(module);
elements = module.namespace.split('.')
elements.append("%s" % module.name)
return os.path.join(package, *elements)
+# The identifier_store tracks identifiers that have been used so that code
zra 2015/11/24 17:34:44 Is this needed for the new code generation you're
alexfandrianto 2015/12/18 01:53:19 Removed
+# 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 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 ''
+
+ return '%s_%s' % (package, name)
+
+def StoreIdentifier(identifier):
+ identifier_store[identifier] = True
+ return ''
+
+def CheckIdentifier(identifier):
+ return identifier in identifier_store
+
+def GetMojomTypeIdentifier(kind):
+ """Get the mojom type's identifier suffix."""
+ # Since this should be unique, it is based on the type's identifier.
+ return "_%s__" % GetIdentifier(kind)
+
class Generator(generator.Generator):
dart_filters = {
@@ -407,6 +500,13 @@ class Generator(generator.Generator):
'default_value': DartDefaultValue,
'encode_method': EncodeMethod,
'expression_to_text': ExpressionToText,
+ 'identifier': GetIdentifier,
+ 'identifier_check': CheckIdentifier,
+ 'identifier_store': StoreIdentifier,
+ 'mojom_type_value': GetMojomTypeValue,
+ 'mojom_type_identifier': GetMojomTypeIdentifier,
+ 'is_imported_kind': IsImportedKind,
+ 'is_array_kind': mojom.IsArrayKind,
'is_map_kind': mojom.IsMapKind,
'is_nullable_kind': mojom.IsNullableKind,
'is_pointer_array_kind': IsPointerArrayKind,
@@ -414,6 +514,8 @@ class Generator(generator.Generator):
'is_struct_kind': mojom.IsStructKind,
'is_union_kind': mojom.IsUnionKind,
'is_enum_kind': mojom.IsEnumKind,
+ 'is_interface_kind': mojom.IsInterfaceKind,
+ 'is_interface_request_kind': mojom.IsInterfaceRequestKind,
'dart_true_false': GetDartTrueFalse,
'dart_type': DartDeclType,
'name': GetNameForElement,
@@ -421,10 +523,16 @@ class Generator(generator.Generator):
'interface_response_name': GetInterfaceResponseName,
'dot_to_underscore': DotToUnderscore,
'is_cloneable_kind': mojom.IsCloneableKind,
+ 'upper_camel': UpperCamelCase,
}
+ # If set to True, then mojom type information will be generated.
+ should_gen_mojom_types = False
+
def GetParameters(self, args):
- return {
+ package = GetPackageName(self.module)
+
+ parameters = {
"namespace": self.module.namespace,
"imports": self.GetImports(args),
"kinds": self.module.kinds,
@@ -435,6 +543,33 @@ class Generator(generator.Generator):
"interfaces": self.GetInterfaces(),
"imported_interfaces": self.GetImportedInterfaces(),
"imported_from": self.ImportedFrom(),
+ "typepkg": '%s.' % _mojom_types_pkg_short,
+ "descpkg": '%s.' % _service_describer_pkg_short,
+ "mojom_types_import": 'import \'%s\' as %s;' % \
+ (_mojom_types_pkg, _mojom_types_pkg_short),
+ "service_describer_import": 'import \'%s\' as %s;' % \
+ (_service_describer_pkg, _service_describer_pkg_short),
+ }
+
+ # If this is the mojom types package, clear the import-related params.
+ if package == _mojom_types_pkg_short:
+ parameters["typepkg"] = ""
+ parameters["mojom_types_import"] = ""
+
+ # If this is the service describer package, clear the import-related params.
+ if package == _service_describer_pkg_short:
+ parameters["descpkg"] = ""
+ parameters["service_describer_import"] = ""
+
+ # If no interfaces were defined, the service describer import isn't needed.
+ if len(self.module.interfaces) == 0:
+ parameters["service_describer_import"] = ""
+
+ return parameters
+
+ def GetGlobals(self):
+ return {
+ 'should_gen_mojom_types': self.should_gen_mojom_types,
}
@UseJinja("dart_templates/module.lib.tmpl", filters=dart_filters)
@@ -443,6 +578,9 @@ class Generator(generator.Generator):
def GenerateFiles(self, args):
+ if "--dart_gen_types" in args:
+ self.should_gen_mojom_types = True
+
elements = self.module.namespace.split('.')
elements.append("%s.dart" % self.module.name)

Powered by Google App Engine
This is Rietveld 408576698