Esempio n. 1
0
def GetUnionGetterReturnType(kind):
    if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind)
            or mojom.IsArrayKind(kind) or mojom.IsMapKind(kind)
            or mojom.IsAnyHandleKind(kind) or mojom.IsInterfaceKind(kind)
            or mojom.IsAssociatedKind(kind)):
        return "%s&" % GetCppWrapperType(kind)
    return GetCppResultWrapperType(kind)
Esempio n. 2
0
 def IsBasicKind(kind):
     return (mojom.IsIntegralKind(kind) or mojom.IsStringKind(kind)
             or mojom.IsDoubleKind(kind) or mojom.IsFloatKind(kind)
             or mojom.IsAnyHandleKind(kind)
             or mojom.IsInterfaceKind(kind)
             or mojom.IsInterfaceRequestKind(kind)
             or mojom.IsAssociatedKind(kind))
    def AddKind(kind):
      if (mojom.IsIntegralKind(kind) or mojom.IsStringKind(kind) or
          mojom.IsDoubleKind(kind) or mojom.IsFloatKind(kind) or
          mojom.IsAnyHandleKind(kind) or
          mojom.IsInterfaceKind(kind) or
          mojom.IsInterfaceRequestKind(kind) or
          mojom.IsAssociatedKind(kind) or
          mojom.IsPendingRemoteKind(kind) or
          mojom.IsPendingReceiverKind(kind)):
        pass
      elif mojom.IsArrayKind(kind):
        AddKind(kind.kind)
      elif mojom.IsMapKind(kind):
        AddKind(kind.key_kind)
        AddKind(kind.value_kind)
      else:
        name = self._GetFullMojomNameForKind(kind)
        if name in seen_types:
          return
        seen_types.add(name)

        typemap = self.typemap.get(name, None)
        if typemap:
          used_typemaps.append(typemap)
        if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind):
          for field in kind.fields:
            AddKind(field.kind)
def JavaScriptDefaultValue(field):
  if field.default:
    if mojom.IsStructKind(field.kind):
      assert field.default == "default"
      return "new %s()" % JavaScriptType(field.kind)
    return ExpressionToText(field.default)
  if field.kind in mojom.PRIMITIVES:
    return _kind_to_javascript_default_value[field.kind]
  if mojom.IsStructKind(field.kind):
    return "null"
  if mojom.IsUnionKind(field.kind):
    return "null"
  if mojom.IsArrayKind(field.kind):
    return "null"
  if mojom.IsMapKind(field.kind):
    return "null"
  if mojom.IsInterfaceKind(field.kind):
    return "new %sPtr()" % JavaScriptType(field.kind)
  if mojom.IsInterfaceRequestKind(field.kind):
    return "new bindings.InterfaceRequest()"
  if mojom.IsAssociatedKind(field.kind):
    return "null"
  if mojom.IsEnumKind(field.kind):
    return "0"
  raise Exception("No valid default: %s" % field)
Esempio n. 5
0
        def get_type_name(kind):
            if kind == mojom.INT64 or kind == mojom.UINT64:
                return "bigint"
            if kind in mojom.PRIMITIVES:
                return _kind_to_closure_type[kind]
            if mojom.IsArrayKind(kind):
                return "Array<%s>" % recurse_with_nullability(kind.kind)
            if mojom.IsMapKind(kind) and self._IsStringableKind(kind.key_kind):
                return "Object<%s, %s>" % (recurse_with_nullability(
                    kind.key_kind), recurse_with_nullability(kind.value_kind))
            if mojom.IsMapKind(kind):
                return "Map<%s, %s>" % (recurse_with_nullability(
                    kind.key_kind), recurse_with_nullability(kind.value_kind))

            if (mojom.IsAssociatedKind(kind)
                    or mojom.IsInterfaceRequestKind(kind)
                    or mojom.IsPendingRemoteKind(kind)
                    or mojom.IsPendingReceiverKind(kind)
                    or mojom.IsPendingAssociatedRemoteKind(kind)
                    or mojom.IsPendingAssociatedReceiverKind(kind)):
                named_kind = kind.kind
            else:
                named_kind = kind

            name = []
            qualified = (not for_module) or (self.module
                                             is not named_kind.module)
            if qualified and named_kind.module:
                name.append(named_kind.module.namespace)
            if named_kind.parent_kind:
                name.append(named_kind.parent_kind.name)

            if mojom.IsEnumKind(kind) and named_kind.parent_kind:
                name = ".".join(name)
                name += "_" + named_kind.name
            else:
                name.append("" + named_kind.name)
                name = ".".join(name)

            if for_module:
                name = name.replace(".", "_")

            if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind)
                    or mojom.IsEnumKind(kind)):
                return name
            if mojom.IsInterfaceKind(kind) or mojom.IsPendingRemoteKind(kind):
                return name + "Remote"
            if mojom.IsInterfaceRequestKind(
                    kind) or mojom.IsPendingReceiverKind(kind):
                return name + "PendingReceiver"
            # TODO(calamity): Support associated interfaces properly.
            if (mojom.IsAssociatedInterfaceKind(kind)
                    or mojom.IsPendingAssociatedRemoteKind(kind)):
                return "Object"
            # TODO(calamity): Support associated interface requests properly.
            if (mojom.IsAssociatedInterfaceRequestKind(kind)
                    or mojom.IsPendingAssociatedReceiverKind(kind)):
                return "Object"
            raise Exception("No valid closure type: %s" % kind)
Esempio n. 6
0
        def get_spec(kind):
            if self._IsPrimitiveKind(kind):
                return _kind_to_lite_js_type[kind]
            if mojom.IsArrayKind(kind):
                return "mojo.internal.Array(%s, %s)" % (get_spec(
                    kind.kind), "true" if mojom.IsNullableKind(kind.kind) else
                                                        "false")
            if mojom.IsMapKind(kind):
                return "mojo.internal.Map(%s, %s, %s)" % (
                    get_spec(kind.key_kind), get_spec(kind.value_kind), "true"
                    if mojom.IsNullableKind(kind.value_kind) else "false")

            if (mojom.IsAssociatedKind(kind)
                    or mojom.IsInterfaceRequestKind(kind)
                    or mojom.IsPendingRemoteKind(kind)
                    or mojom.IsPendingReceiverKind(kind)
                    or mojom.IsPendingAssociatedRemoteKind(kind)
                    or mojom.IsPendingAssociatedReceiverKind(kind)):
                named_kind = kind.kind
            else:
                named_kind = kind

            name = []
            qualified = (not for_module) or (self.module
                                             is not named_kind.module)
            if qualified and named_kind.module:
                name.append(named_kind.module.namespace)
            if named_kind.parent_kind:
                parent_name = named_kind.parent_kind.name
                if mojom.IsStructKind(
                        named_kind.parent_kind) and not for_module:
                    parent_name += "Spec"
                name.append(parent_name)
            name.append(named_kind.name)
            name = ".".join(name)
            if for_module:
                name = name.replace(".", "_")

            if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind)
                    or mojom.IsEnumKind(kind)):
                return "%sSpec.$" % name
            if mojom.IsInterfaceKind(kind) or mojom.IsPendingRemoteKind(kind):
                return "mojo.internal.InterfaceProxy(%sRemote)" % name
            if mojom.IsInterfaceRequestKind(
                    kind) or mojom.IsPendingReceiverKind(kind):
                return "mojo.internal.InterfaceRequest(%sPendingReceiver)" % name
            if (mojom.IsAssociatedInterfaceKind(kind)
                    or mojom.IsPendingAssociatedRemoteKind(kind)):
                # TODO(rockot): Implement associated interfaces.
                return "mojo.internal.AssociatedInterfaceProxy(%sRemote)" % (
                    name)
            if (mojom.IsAssociatedInterfaceRequestKind(kind)
                    or mojom.IsPendingAssociatedReceiverKind(kind)):
                return "mojo.internal.AssociatedInterfaceRequest(%sPendingReceiver)" % (
                    name)

            return name
Esempio n. 7
0
    def _LiteJavaScriptType(self, kind):
        if self._IsPrimitiveKind(kind):
            return _kind_to_lite_js_type[kind]
        if mojom.IsArrayKind(kind):
            return "mojo.internal.Array(%s, %s)" % (self._LiteJavaScriptType(
                kind.kind), "true" if mojom.IsNullableKind(kind.kind) else
                                                    "false")
        if mojom.IsMapKind(kind):
            return "mojo.internal.Map(%s, %s, %s)" % (self._LiteJavaScriptType(
                kind.key_kind), self._LiteJavaScriptType(
                    kind.value_kind), "true" if mojom.IsNullableKind(
                        kind.value_kind) else "false")

        if (mojom.IsAssociatedKind(kind) or mojom.IsInterfaceRequestKind(kind)
                or mojom.IsPendingRemoteKind(kind)
                or mojom.IsPendingReceiverKind(kind)
                or mojom.IsPendingAssociatedRemoteKind(kind)
                or mojom.IsPendingAssociatedReceiverKind(kind)):
            named_kind = kind.kind
        else:
            named_kind = kind

        name = []
        if named_kind.module:
            name.append(named_kind.module.namespace)
        if named_kind.parent_kind:
            parent_name = named_kind.parent_kind.name
            if mojom.IsStructKind(named_kind.parent_kind):
                parent_name += "Spec"
            name.append(parent_name)
        name.append(named_kind.name)
        name = ".".join(name)

        if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind)
                or mojom.IsEnumKind(kind)):
            return "%sSpec.$" % name
        if mojom.IsInterfaceKind(kind) or mojom.IsPendingRemoteKind(kind):
            remote_name = name + self._GetPrimitivesNames()["remote"]
            return "mojo.internal.InterfaceProxy(%s)" % remote_name
        if mojom.IsInterfaceRequestKind(kind) or mojom.IsPendingReceiverKind(
                kind):
            request_name = name + self._GetPrimitivesNames(
            )["pending_receiver"]
            return "mojo.internal.InterfaceRequest(%s)" % request_name
        if (mojom.IsAssociatedInterfaceKind(kind)
                or mojom.IsPendingAssociatedRemoteKind(kind)):
            remote_name = name + self._GetPrimitivesNames()["remote"]
            # TODO(rockot): Implement associated interfaces.
            return "mojo.internal.AssociatedInterfaceProxy(%s)" % (remote_name)
        if (mojom.IsAssociatedInterfaceRequestKind(kind)
                or mojom.IsPendingAssociatedReceiverKind(kind)):
            request_name = name + self._GetPrimitivesNames(
            )["pending_receiver"]
            return "mojo.internal.AssociatedInterfaceRequest(%s)" % request_name

        return name
Esempio n. 8
0
    def _LiteClosureType(self, kind):
        if kind in mojom.PRIMITIVES:
            return _kind_to_closure_type[kind]
        if mojom.IsArrayKind(kind):
            return "Array<%s>" % self._LiteClosureTypeWithNullability(
                kind.kind)
        if mojom.IsMapKind(kind) and self._IsStringableKind(kind.key_kind):
            return "Object<%s, %s>" % (
                self._LiteClosureTypeWithNullability(kind.key_kind),
                self._LiteClosureTypeWithNullability(kind.value_kind))
        if mojom.IsMapKind(kind):
            return "Map<%s, %s>" % (
                self._LiteClosureTypeWithNullability(kind.key_kind),
                self._LiteClosureTypeWithNullability(kind.value_kind))

        if (mojom.IsAssociatedKind(kind) or mojom.IsInterfaceRequestKind(kind)
                or mojom.IsPendingRemoteKind(kind)
                or mojom.IsPendingReceiverKind(kind)
                or mojom.IsPendingAssociatedRemoteKind(kind)
                or mojom.IsPendingAssociatedReceiverKind(kind)):
            named_kind = kind.kind
        else:
            named_kind = kind

        name = []
        if named_kind.module:
            name.append(named_kind.module.namespace)
        if named_kind.parent_kind:
            name.append(named_kind.parent_kind.name)

        if mojom.IsEnumKind(kind) and named_kind.parent_kind:
            name = ".".join(name)
            name += "_" + named_kind.name
        else:
            name.append("" + named_kind.name)
            name = ".".join(name)

        if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind)
                or mojom.IsEnumKind(kind)):
            return name
        if mojom.IsInterfaceKind(kind) or mojom.IsPendingRemoteKind(kind):
            return name + self._GetPrimitivesNames()["remote"]
        if mojom.IsInterfaceRequestKind(kind) or mojom.IsPendingReceiverKind(
                kind):
            return name + self._GetPrimitivesNames()["pending_receiver"]
        # TODO(calamity): Support associated interfaces properly.
        if (mojom.IsAssociatedInterfaceKind(kind)
                or mojom.IsPendingAssociatedRemoteKind(kind)):
            return "Object"
        # TODO(calamity): Support associated interface requests properly.
        if (mojom.IsAssociatedInterfaceRequestKind(kind)
                or mojom.IsPendingAssociatedReceiverKind(kind)):
            return "Object"

        raise Exception("No valid closure type: %s" % kind)
Esempio n. 9
0
 def extract_referenced_user_kinds(kind):
   if mojom.IsArrayKind(kind):
     return extract_referenced_user_kinds(kind.kind)
   if mojom.IsMapKind(kind):
     return (extract_referenced_user_kinds(kind.key_kind) +
         extract_referenced_user_kinds(kind.value_kind))
   if mojom.IsInterfaceRequestKind(kind) or mojom.IsAssociatedKind(kind):
     return [kind.kind]
   if mojom.IsStructKind(kind):
     return [kind]
   if (mojom.IsInterfaceKind(kind) or mojom.IsEnumKind(kind) or
       mojom.IsUnionKind(kind)):
     return [kind]
   return []
Esempio n. 10
0
def GetNameForElement(element):
    if (mojom.IsEnumKind(element) or mojom.IsInterfaceKind(element)
            or mojom.IsStructKind(element) or mojom.IsUnionKind(element)):
        return UpperCamelCase(element.name)
    if mojom.IsInterfaceRequestKind(element) or mojom.IsAssociatedKind(
            element):
        return GetNameForElement(element.kind)
    if isinstance(element, (mojom.Method, mojom.Parameter, mojom.Field)):
        return CamelCase(element.name)
    if isinstance(element, mojom.EnumValue):
        return (GetNameForElement(element.enum) + '.' +
                ConstantStyle(element.name))
    if isinstance(element,
                  (mojom.NamedValue, mojom.Constant, mojom.EnumField)):
        return ConstantStyle(element.name)
    raise Exception('Unexpected element: %s' % element)
Esempio n. 11
0
def GetNameForElement(element):
    # structs
    if (mojom.IsEnumKind(element) or
        mojom.IsInterfaceKind(element) or
        mojom.IsStructKind(element)):
        return element.mojom_name
    # vectors
    if (mojom.IsArrayKind(element)):
        elem_name = GetFullNameForElement(element.kind)
        return f'std::vector<{elem_name}>'
    # maps
    if (mojom.IsMapKind(element)):
        key_name = GetFullNameForElement(element.key_kind)
        value_name = GetFullNameForElement(element.value_kind)
        return f'std::map<{key_name}, {value_name}>'
    # struct fields and function parameters
    if isinstance(element, (mojom.Field, mojom.Method, mojom.Parameter)):
        # maps and vectors
        if (mojom.IsArrayKind(element.kind) or mojom.IsMapKind(element.kind)):
            return GetNameForElement(element.kind)
        # strings
        if (mojom.IsReferenceKind(element.kind) and element.kind.spec == 's'):
            return 'std::string'
        # PODs
        if element.kind in _kind_to_cpp_type:
            return _kind_to_cpp_type[element.kind]
        # structs and enums
        return element.kind.mojom_name
    # PODs that are members of vectors/maps
    if (hasattr(element, '__hash__') and element in _kind_to_cpp_type):
        return _kind_to_cpp_type[element]
    if (hasattr(element, 'spec')):
        # strings that are members of vectors/maps
        if (element.spec == 's'):
            return 'std::string'
        # structs that aren't defined in mojom that are members of vectors/maps
        if (element.spec[0] == 'x'):
            return element.spec.replace('x:', '').replace('.', '::')
    if (mojom.IsInterfaceRequestKind(element) or
        mojom.IsAssociatedKind(element) or
        mojom.IsPendingRemoteKind(element) or
        mojom.IsPendingReceiverKind(element) or
        mojom.IsUnionKind(element)):
        raise Exception('Unsupported element: %s' % element)
    raise Exception('Unexpected element: %s' % element)
Esempio n. 12
0
    def _LiteClosureType(self, kind):
        if kind in mojom.PRIMITIVES:
            return _kind_to_closure_type[kind]
        if mojom.IsArrayKind(kind):
            return "Array<%s>" % self._ClosureType(kind.kind)
        if mojom.IsMapKind(kind) and self._IsStringableKind(kind.key_kind):
            return "Map<%s, %s>|Object<%s, %s>" % (self._LiteClosureType(
                kind.key_kind), self._LiteClosureType(
                    kind.value_kind), self._LiteClosureType(
                        kind.key_kind), self._LiteClosureType(kind.value_kind))
        if mojom.IsMapKind(kind):
            return "Map<%s, %s>" % (self._LiteClosureType(
                kind.key_kind), self._LiteClosureType(kind.value_kind))

        if mojom.IsAssociatedKind(kind) or mojom.IsInterfaceRequestKind(kind):
            named_kind = kind.kind
        else:
            named_kind = kind

        name = []
        if named_kind.module:
            name.append(named_kind.module.namespace)
        if named_kind.parent_kind:
            name.append(named_kind.parent_kind.name)
        name.append("" + named_kind.name)
        name = ".".join(name)

        if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind)
                or mojom.IsEnumKind(kind)):
            return name
        if mojom.IsInterfaceKind(kind):
            return name + "Proxy"
        if mojom.IsInterfaceRequestKind(kind):
            return name + "Request"
        # TODO(calamity): Support associated interfaces properly.
        if mojom.IsAssociatedInterfaceKind(kind):
            return "Object"
        # TODO(calamity): Support associated interface requests properly.
        if mojom.IsAssociatedInterfaceRequestKind(kind):
            return "Object"

        raise Exception("No valid closure type: %s" % kind)
Esempio n. 13
0
def JavaScriptEncodeSnippet(kind):
    if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind)
            or mojom.IsInterfaceKind(kind) or mojom.IsAssociatedKind(kind)):
        return "encodeStruct(%s, " % CodecType(kind)
    if mojom.IsUnionKind(kind):
        return "encodeStruct(%s, " % JavaScriptType(kind)
    if mojom.IsStructKind(kind):
        return "encodeStructPointer(%s, " % JavaScriptType(kind)
    if mojom.IsMapKind(kind):
        return "encodeMapPointer(%s, %s, " % \
            (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind))
    if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind):
        return "encodeArrayPointer(codec.PackedBool, "
    if mojom.IsArrayKind(kind):
        return "encodeArrayPointer(%s, " % CodecType(kind.kind)
    if mojom.IsInterfaceRequestKind(kind):
        return JavaScriptEncodeSnippet(mojom.MSGPIPE)
    if mojom.IsEnumKind(kind):
        return JavaScriptEncodeSnippet(mojom.INT32)
    raise Exception("No encode snippet for %s" % kind)
Esempio n. 14
0
def GetNameForElement(element):
    if (mojom.IsEnumKind(element) or mojom.IsInterfaceKind(element)
            or mojom.IsStructKind(element) or mojom.IsUnionKind(element)):
        name = UpperCamelCase(element.name)
        if name in _java_reserved_types:
            return name + '_'
        return name
    if (mojom.IsInterfaceRequestKind(element)
            or mojom.IsAssociatedKind(element)
            or mojom.IsPendingRemoteKind(element)
            or mojom.IsPendingReceiverKind(element)):
        return GetNameForElement(element.kind)
    if isinstance(element, (mojom.Method, mojom.Parameter, mojom.Field)):
        return CamelCase(element.name)
    if isinstance(element, mojom.EnumValue):
        return (GetNameForElement(element.enum) + '.' +
                ConstantStyle(element.name))
    if isinstance(element,
                  (mojom.NamedValue, mojom.Constant, mojom.EnumField)):
        return ConstantStyle(element.name)
    raise Exception('Unexpected element: %s' % element)
Esempio n. 15
0
    def _LiteJavaScriptType(self, kind):
        if self._IsPrimitiveKind(kind):
            return _kind_to_lite_js_type[kind]
        if mojom.IsArrayKind(kind):
            return "mojo.internal.Array(%s, %s)" % (self._LiteJavaScriptType(
                kind.kind), "true" if mojom.IsNullableKind(kind.kind) else
                                                    "false")
        if mojom.IsMapKind(kind):
            return "mojo.internal.Map(%s, %s, %s)" % (self._LiteJavaScriptType(
                kind.key_kind), self._LiteJavaScriptType(
                    kind.value_kind), "true" if mojom.IsNullableKind(
                        kind.value_kind) else "false")

        if mojom.IsAssociatedKind(kind) or mojom.IsInterfaceRequestKind(kind):
            named_kind = kind.kind
        else:
            named_kind = kind

        name = []
        if named_kind.module:
            name.append(named_kind.module.namespace)
        if named_kind.parent_kind:
            name.append(named_kind.parent_kind.name)
        name.append(named_kind.name)
        name = ".".join(name)

        if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind)
                or mojom.IsEnumKind(kind)):
            return "%s.$" % name
        if mojom.IsInterfaceKind(kind):
            return "mojo.internal.InterfaceProxy(%sProxy)" % name
        if mojom.IsInterfaceRequestKind(kind):
            return "mojo.internal.InterfaceRequest(%sRequest)" % name
        if mojom.IsAssociatedInterfaceKind(kind):
            return "mojo.internal.AssociatedInterfaceProxy(%sAssociatedProxy)" % (
                name)
        if mojom.IsAssociatedInterfaceRequestKind(kind):
            return "mojo.internal.AssociatedInterfaceRequest(%s)" % name

        return name
Esempio n. 16
0
    def _LiteJavaScriptType(self, kind):
        if self._IsPrimitiveKind(kind):
            return _kind_to_lite_js_type[kind]
        if mojom.IsArrayKind(kind):
            return "mojo.mojom.Array(%s, %s)" % (self._LiteJavaScriptType(
                kind.kind), "true" if mojom.IsNullableKind(kind.kind) else
                                                 "false")
        if mojom.IsMapKind(kind):
            return "mojo.mojom.Map(%s, %s, %s)" % (self._LiteJavaScriptType(
                kind.key_kind), self._LiteJavaScriptType(
                    kind.value_kind), "true" if mojom.IsNullableKind(
                        kind.value_kind) else "false")

        if mojom.IsAssociatedKind(kind) or mojom.IsInterfaceRequestKind(kind):
            named_kind = kind.kind
        else:
            named_kind = kind

        name = []
        if named_kind.module and named_kind.module.path != self.module.path:
            name.append(named_kind.module.unique_name)
        if named_kind.parent_kind:
            name.append(named_kind.parent_kind.name)
        name.append(named_kind.name)
        name = ".".join(name)

        if mojom.IsInterfaceKind(kind):
            return "new mojo.mojom.InterfaceProxy(%sProxy)" % name
        if mojom.IsInterfaceRequestKind(kind):
            return "new mojo.mojom.InterfaceRequest(%sRequest)" % name
        if mojom.IsAssociatedInterfaceKind(kind):
            return "new mojo.mojom.AssociatedInterfaceProxy(%sAssociatedProxy)" % name
        if mojom.IsAssociatedInterfaceRequestKind(kind):
            return "new mojo.mojom.AssociatedInterfaceRequest(%s)" % name

        return name