Chromium Code Reviews| 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 2adb77b363a42853aa88b45af4de1afd5e5315d8..61c28c6cf9b7a3de412d9fe30f751fc3db9165bb 100644 |
| --- a/mojo/public/tools/bindings/generators/mojom_dart_generator.py |
| +++ b/mojo/public/tools/bindings/generators/mojom_dart_generator.py |
| @@ -123,6 +123,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: "dataPipeConsumer", |
| + mojom.DPPIPE: "dataPipeProducer", |
| + mojom.MSGPIPE: "messagePipe", |
| + mojom.SHAREDBUFFER: "sharedBuffer", |
| + mojom.NULLABLE_HANDLE: "unspecified", |
| + mojom.NULLABLE_DCPIPE: "dataPipeConsumer", |
| + mojom.NULLABLE_DPPIPE: "dataPipeProducer", |
| + mojom.NULLABLE_MSGPIPE: "messagePipe", |
| + mojom.NULLABLE_SHAREDBUFFER: "sharedBuffer", |
| + 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', |
| @@ -175,6 +201,16 @@ _spec_to_encode_method = { |
| mojom.UINT8.spec: 'encodeUint8', |
| } |
| +# The mojom_types.mojom and service_describer.mojom files are special because |
| +# they are used to generate mojom Type's and ServiceDescription implementations. |
| +# They need to be imported, unless the file itself is being generated. |
| +_service_describer_pkg_short = "service_describer" |
| +_service_describer_pkg = "package:mojo/mojo/bindings/types/%s.mojom.dart" % \ |
| + _service_describer_pkg_short |
| +_mojom_types_pkg_short = "mojom_types" |
| +_mojom_types_pkg = "package:mojo/mojo/bindings/types/%s.mojom.dart" % \ |
| + _mojom_types_pkg_short |
| + |
| def GetDartType(kind): |
| if kind.imported_from: |
| return kind.imported_from["unique_name"] + "." + GetNameForElement(kind) |
| @@ -225,6 +261,27 @@ def DartDeclType(kind): |
| if mojom.IsEnumKind(kind): |
| return GetDartType(kind) |
| +def GetMojomTypeValue(kind, typepkg=''): |
|
zra
2016/01/15 21:01:33
I think it would be more consistent to move this l
alexfandrianto
2016/01/20 00:08:28
Pretty much all the languages use a map for the si
|
| + 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). Numbers terminate a string of lower-case characters. |
| @@ -426,6 +483,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 |
| @@ -436,12 +496,43 @@ 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) |
| +def GetIdentifier(kind): |
|
zra
2016/01/15 21:01:33
This name is too generic for what (I think) this f
alexfandrianto
2016/01/20 00:08:28
This was combined with GetMojomTypeIdentifier belo
|
| + """Use the kind's module to determine the package and name.""" |
| + # Note: InterfaceRequest'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: |
| + # These kinds (e.g., simple kinds, maps, and arrays) lack identifiers. |
| + raise Exception('Unexpected kind: %s' % kind) |
| + |
| + return '%s_%s' % (package, name) |
| + |
| +# Returns a string of the form package.path.TypeName - the full identifier |
| +# for an element. |
| +def GetFullIdentifier(element, exported=True): |
| + return '%s.%s' % (element.module.namespace, GetNameForElement(element)) |
| + |
| +def RaiseHelper(msg): |
| + raise Exception(msg) |
| + |
| +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) |
|
zra
2016/01/15 21:01:33
Why are there trailing underscores?
alexfandrianto
2016/01/20 00:08:28
This has to match between languages, and the Go on
|
| + |
| class Generator(generator.Generator): |
| dart_filters = { |
| @@ -450,6 +541,12 @@ class Generator(generator.Generator): |
| 'decode_method': DecodeMethod, |
| 'default_value': DartDefaultValue, |
| 'encode_method': EncodeMethod, |
| + 'fullidentifier': GetFullIdentifier, |
| + 'identifier': GetIdentifier, |
| + '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, |
| @@ -457,16 +554,25 @@ 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, |
| 'interface_response_name': GetInterfaceResponseName, |
| 'dot_to_underscore': DotToUnderscore, |
| 'is_cloneable_kind': mojom.IsCloneableKind, |
| + 'upper_camel': UpperCamelCase, |
| + 'raise': RaiseHelper, |
| } |
| + # 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, |
| @@ -477,6 +583,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) |
| @@ -485,6 +618,8 @@ class Generator(generator.Generator): |
| def GenerateFiles(self, args): |
| + self.should_gen_mojom_types = "--generate_type_info" in args |
| + |
| elements = self.module.namespace.split('.') |
| elements.append("%s.dart" % self.module.name) |