示例#1
0
def GetIdentifier(kind):
  """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)
 def _DecodeMethodName(kind):
     if mojom.IsArrayKind(kind):
         return _DecodeMethodName(kind.kind) + 's'
     if mojom.IsEnumKind(kind):
         return _DecodeMethodName(mojom.INT32)
     if mojom.IsInterfaceRequestKind(kind):
         return 'readInterfaceRequest'
     if mojom.IsInterfaceKind(kind):
         return 'readServiceInterface'
     if mojom.IsAssociatedInterfaceRequestKind(kind):
         return 'readAssociatedInterfaceRequestNotSupported'
     if mojom.IsAssociatedInterfaceKind(kind):
         return 'readAssociatedServiceInterfaceNotSupported'
     return _spec_to_decode_method[kind.spec]
示例#3
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 []
示例#4
0
def GetFieldType(kind, field=None):
    if mojom.IsArrayKind(kind):
        arguments = []
        if kind.kind in _kind_to_typecode_for_native_array:
            arguments.append('%r' %
                             _kind_to_typecode_for_native_array[kind.kind])
        elif kind.kind != mojom.BOOL:
            arguments.append(GetFieldType(kind.kind))
        if mojom.IsNullableKind(kind):
            arguments.append('nullable=True')
        if kind.length is not None:
            arguments.append('length=%d' % kind.length)
        array_type = 'GenericArrayType'
        if kind.kind == mojom.BOOL:
            array_type = 'BooleanArrayType'
        elif kind.kind in _kind_to_typecode_for_native_array:
            array_type = 'NativeArrayType'
        return '_descriptor.%s(%s)' % (array_type, ', '.join(arguments))

    if mojom.IsMapKind(kind):
        arguments = [
            GetFieldType(kind.key_kind),
            GetFieldType(kind.value_kind),
        ]
        if mojom.IsNullableKind(kind):
            arguments.append('nullable=True')
        return '_descriptor.MapType(%s)' % ', '.join(arguments)

    if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind):
        arguments = ['lambda: %s' % GetFullyQualifiedName(kind)]
        if mojom.IsNullableKind(kind):
            arguments.append('nullable=True')
        return '_descriptor.StructType(%s)' % ', '.join(arguments)

    if mojom.IsEnumKind(kind):
        return GetFieldType(mojom.INT32)

    if mojom.IsInterfaceKind(kind):
        arguments = ['lambda: %s' % GetFullyQualifiedName(kind)]
        if mojom.IsNullableKind(kind):
            arguments.append('nullable=True')
        return '_descriptor.InterfaceType(%s)' % ', '.join(arguments)

    if mojom.IsInterfaceRequestKind(kind):
        arguments = []
        if mojom.IsNullableKind(kind):
            arguments.append('nullable=True')
        return '_descriptor.InterfaceRequestType(%s)' % ', '.join(arguments)

    return _kind_to_type[kind]
示例#5
0
def GetNonNullableGoType(kind):
  if mojom.IsStructKind(kind):
    return '%s' % GetFullName(kind)
  if mojom.IsArrayKind(kind):
    if kind.length:
      return '[%s]%s' % (kind.length, GetGoType(kind.kind))
    return '[]%s' % GetGoType(kind.kind)
  if mojom.IsMapKind(kind):
    return 'map[%s]%s' % (GetGoType(kind.key_kind), GetGoType(kind.value_kind))
  if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
    return GetGoType(mojom.MSGPIPE)
  if mojom.IsEnumKind(kind):
    return GetNameForNestedElement(kind)
  return _kind_infos[kind].go_type
示例#6
0
 def _EncodeMethodName(kind):
     if mojom.IsStructKind(kind):
         return 'encodeStruct'
     if mojom.IsUnionKind(kind):
         return 'encodeUnion'
     if mojom.IsArrayKind(kind):
         return _EncodeMethodName(kind.kind) + 'Array'
     if mojom.IsEnumKind(kind):
         return 'encodeEnum'
     if mojom.IsInterfaceRequestKind(kind):
         return 'encodeInterfaceRequest'
     if mojom.IsInterfaceKind(kind):
         return 'encodeInterfaceHandle'
     return _spec_to_encode_method[kind.spec]
def CodecType(kind):
    if kind in mojom.PRIMITIVES:
        return _kind_to_codec_type[kind]
    if mojom.IsStructKind(kind):
        return "new codec.PointerTo(%s)" % CodecType(kind.name)
    if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind):
        return "new codec.ArrayOf(new codec.ArrayOf(codec.PackedBool))"
    if mojom.IsArrayKind(kind):
        return "new codec.ArrayOf(%s)" % CodecType(kind.kind)
    if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
        return CodecType(mojom.MSGPIPE)
    if mojom.IsEnumKind(kind):
        return _kind_to_codec_type[mojom.INT32]
    return kind
示例#8
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
示例#9
0
def GetIdentifier(kind):
  # Use the kind's module to determine the package name.
  if hasattr(kind, 'module'):
    package = GetPackageName(kind.module)
  elif mojom.IsInterfaceRequestKind(kind):
    package = GetPackageName(kind.kind.module)
  else:
    return ''

  # Most kinds have a name, but those that don't should rely on their spec.
  # Since spec can have : and ? characters, these must be replaced. Since ? is
  # replaced with '', the caller must keep track of optionality on its own.
  name_or_spec = (kind.name if hasattr(kind, 'name') else kind.spec)
  package_unique = name_or_spec.replace(':', '_').replace('?', '')
  return '%s_%s' % (package, package_unique)
示例#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):
        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)
示例#11
0
def GetCppType(kind):
    if mojom.IsArrayKind(kind):
        return "mojo::internal::Array_Data<%s>*" % GetCppType(kind.kind)
    if mojom.IsMapKind(kind):
        return "mojo::internal::Map_Data<%s, %s>*" % (GetCppType(
            kind.key_kind), GetCppType(kind.value_kind))
    if mojom.IsStructKind(kind):
        return "%s_Data*" % GetNameForKind(kind, internal=True)
    if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
        return "mojo::MessagePipeHandle"
    if mojom.IsEnumKind(kind):
        return "int32_t"
    if mojom.IsStringKind(kind):
        return "mojo::internal::String_Data*"
    return _kind_to_cpp_type[kind]
示例#12
0
 def _CodecType(self, kind):
     if kind in mojom.PRIMITIVES:
         return _kind_to_codec_type[kind]
     if mojom.IsStructKind(kind):
         pointer_type = "NullablePointerTo" if mojom.IsNullableKind(kind) \
             else "PointerTo"
         return "new codec.%s(%s)" % (pointer_type,
                                      self._JavaScriptType(kind))
     if mojom.IsUnionKind(kind):
         return self._JavaScriptType(kind)
     if mojom.IsArrayKind(kind):
         array_type = ("NullableArrayOf"
                       if mojom.IsNullableKind(kind) else "ArrayOf")
         array_length = "" if kind.length is None else ", %d" % kind.length
         element_type = self._ElementCodecType(kind.kind)
         return "new codec.%s(%s%s)" % (array_type, element_type,
                                        array_length)
     if mojom.IsInterfaceKind(kind):
         return "new codec.%s(%sPtr)" % (
             "NullableInterface" if mojom.IsNullableKind(kind) else
             "Interface", self._JavaScriptType(kind))
     if mojom.IsPendingRemoteKind(kind):
         return "new codec.%s(%sPtr)" % (
             "NullableInterface" if mojom.IsNullableKind(kind) else
             "Interface", self._JavaScriptType(kind.kind))
     if mojom.IsInterfaceRequestKind(kind) or mojom.IsPendingReceiverKind(
             kind):
         return "codec.%s" % ("NullableInterfaceRequest" if mojom.
                              IsNullableKind(kind) else "InterfaceRequest")
     if (mojom.IsAssociatedInterfaceKind(kind)
             or mojom.IsPendingAssociatedRemoteKind(kind)):
         return "codec.%s" % ("NullableAssociatedInterfacePtrInfo"
                              if mojom.IsNullableKind(kind) else
                              "AssociatedInterfacePtrInfo")
     if (mojom.IsAssociatedInterfaceRequestKind(kind)
             or mojom.IsPendingAssociatedReceiverKind(kind)):
         return "codec.%s" % ("NullableAssociatedInterfaceRequest"
                              if mojom.IsNullableKind(kind) else
                              "AssociatedInterfaceRequest")
     if mojom.IsEnumKind(kind):
         return "new codec.Enum(%s)" % self._JavaScriptType(kind)
     if mojom.IsMapKind(kind):
         map_type = "NullableMapOf" if mojom.IsNullableKind(
             kind) else "MapOf"
         key_type = self._ElementCodecType(kind.key_kind)
         value_type = self._ElementCodecType(kind.value_kind)
         return "new codec.%s(%s, %s)" % (map_type, key_type, value_type)
     raise Exception("No codec type for %s" % kind)
示例#13
0
    def _GetCppDataViewType(self, kind, qualified=False):
        def _GetName(input_kind):
            return _NameFormatter(input_kind, None).FormatForCpp(
                omit_namespace_for_module=(None if qualified else self.module),
                flatten_nested_kind=True)

        if mojom.IsEnumKind(kind):
            return _GetName(kind)
        if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind):
            return "%sDataView" % _GetName(kind)
        if mojom.IsArrayKind(kind):
            return "mojo::ArrayDataView<%s>" % (self._GetCppDataViewType(
                kind.kind, qualified))
        if mojom.IsMapKind(kind):
            return ("mojo::MapDataView<%s, %s>" %
                    (self._GetCppDataViewType(kind.key_kind, qualified),
                     self._GetCppDataViewType(kind.value_kind, qualified)))
        if mojom.IsStringKind(kind):
            return "mojo::StringDataView"
        if mojom.IsInterfaceKind(kind):
            return "%sPtrDataView" % _GetName(kind)
        if mojom.IsInterfaceRequestKind(kind):
            return "%sRequestDataView" % _GetName(kind.kind)
        if mojom.IsPendingRemoteKind(kind):
            return ("mojo::InterfacePtrDataView<%sInterfaceBase>" %
                    _GetName(kind.kind))
        if mojom.IsPendingReceiverKind(kind):
            return ("mojo::InterfaceRequestDataView<%sInterfaceBase>" %
                    _GetName(kind.kind))
        if (mojom.IsAssociatedInterfaceKind(kind)
                or mojom.IsPendingAssociatedRemoteKind(kind)):
            return "%sAssociatedPtrInfoDataView" % _GetName(kind.kind)
        if (mojom.IsAssociatedInterfaceRequestKind(kind)
                or mojom.IsPendingAssociatedReceiverKind(kind)):
            return "%sAssociatedRequestDataView" % _GetName(kind.kind)
        if mojom.IsGenericHandleKind(kind):
            return "mojo::ScopedHandle"
        if mojom.IsDataPipeConsumerKind(kind):
            return "mojo::ScopedDataPipeConsumerHandle"
        if mojom.IsDataPipeProducerKind(kind):
            return "mojo::ScopedDataPipeProducerHandle"
        if mojom.IsMessagePipeKind(kind):
            return "mojo::ScopedMessagePipeHandle"
        if mojom.IsSharedBufferKind(kind):
            return "mojo::ScopedSharedBufferHandle"
        if mojom.IsPlatformHandleKind(kind):
            return "mojo::PlatformHandle"
        return _kind_to_cpp_type[kind]
示例#14
0
def DartDecodeSnippet(kind):
  if kind in mojom.PRIMITIVES:
    return "decodeStruct(%s)" % CodecType(kind)
  if mojom.IsStructKind(kind):
    return "decodeStructPointer(%s)" % DartType(kind)
  if mojom.IsMapKind(kind):
    return "decodeMapPointer(%s, %s)" % \
        (MapCodecType(kind.key_kind), MapCodecType(kind.value_kind))
  if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind):
    return "decodeArrayPointer(bindings.PackedBool)"
  if mojom.IsArrayKind(kind):
    return "decodeArrayPointer(%s)" % CodecType(kind.kind)
  if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
    return DartDecodeSnippet(mojom.MSGPIPE)
  if mojom.IsEnumKind(kind):
    return DartDecodeSnippet(mojom.INT32)
示例#15
0
def JavaScriptEncodeSnippet(kind):
    if kind in mojom.PRIMITIVES:
        return "encodeStruct(%s, " % CodecType(kind)
    if mojom.IsStructKind(kind):
        return "encodeStructPointer(%s, " % JavaScriptType(kind)
    if mojom.IsMapKind(kind):
        return "encodeMapPointer(%s, %s, " % \
            (MapCodecType(kind.key_kind), MapCodecType(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.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
        return JavaScriptEncodeSnippet(mojom.MSGPIPE)
    if mojom.IsEnumKind(kind):
        return JavaScriptEncodeSnippet(mojom.INT32)
示例#16
0
def GetCppFieldType(kind):
  if mojom.IsStructKind(kind):
    return ("mojo::internal::StructPointer<%s_Data>" %
        GetNameForKind(kind, internal=True))
  if mojom.IsArrayKind(kind):
    return "mojo::internal::ArrayPointer<%s>" % GetCppType(kind.kind)
  if mojom.IsMapKind(kind):
    return ("mojo::internal::StructPointer<mojo::internal::Map_Data<%s, %s>>" %
            (GetCppType(kind.key_kind), GetCppType(kind.value_kind)))
  if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
    return "mojo::MessagePipeHandle"
  if mojom.IsEnumKind(kind):
    return GetNameForKind(kind)
  if mojom.IsStringKind(kind):
    return "mojo::internal::StringPointer"
  return _kind_to_cpp_type[kind]
示例#17
0
  def _GetCppWrapperProtoType(self, kind, add_same_module_namespaces=False):
    if (mojom.IsEnumKind(kind) or mojom.IsStructKind(kind)
        or mojom.IsUnionKind(kind)):
      return self._GetCppProtoNameForKind(
          kind, add_same_module_namespaces=add_same_module_namespaces)
    elif mojom.IsInterfaceKind(kind):
      return '%s::Ptr' % self._GetCppProtoNameForKind(
          kind, add_same_module_namespaces=add_same_module_namespaces)
    elif mojom.IsInterfaceRequestKind(kind):
      return '%s::Request' % self._GetCppProtoNameForKind(
          kind.kind, add_same_module_namespaces=add_same_module_namespaces)
    elif mojom.IsAssociatedInterfaceKind(kind):
      return '%s::AssociatedPtr' % self._GetCppProtoNameForKind(
          kind.kind, add_same_module_namespaces=add_same_module_namespaces)
    elif mojom.IsAssociatedInterfaceRequestKind(kind):
      return '%s::AssociatedRequest' % self._GetCppProtoNameForKind(
          kind.kind, add_same_module_namespaces=add_same_module_namespaces)
    elif mojom.IsPendingRemoteKind(kind):
      return "%s::PendingRemote" % self._GetCppProtoNameForKind(
          kind.kind, add_same_module_namespaces=add_same_module_namespaces)
    elif mojom.IsPendingReceiverKind(kind):
      return "%s::PendingReceiver" % self._GetCppProtoNameForKind(
          kind.kind, add_same_module_namespaces=add_same_module_namespaces)
    elif mojom.IsPendingAssociatedRemoteKind(kind):
      return "%s::PendingAssociatedRemote" % self._GetCppProtoNameForKind(
          kind.kind, add_same_module_namespaces=add_same_module_namespaces)
    elif mojom.IsPendingAssociatedReceiverKind(kind):
      return "%s::PendingAssociatedReceiver" % self._GetCppProtoNameForKind(
          kind.kind, add_same_module_namespaces=add_same_module_namespaces)
    elif mojom.IsStringKind(kind):
      return "std::string"
    elif mojom.IsGenericHandleKind(kind):
      return "mojolpm::Handle"
    elif mojom.IsDataPipeConsumerKind(kind):
      return "mojolpm::DataPipeConsumerHandle"
    elif mojom.IsDataPipeProducerKind(kind):
      return "mojolpm::DataPipeProducerHandle"
    elif mojom.IsMessagePipeKind(kind):
      return "mojolpm::MessagePipeHandle"
    elif mojom.IsSharedBufferKind(kind):
      return "mojolpm::SharedBufferHandle"
    elif mojom.IsPlatformHandleKind(kind):
      return "mojolpm::PlatformHandle"

    if not kind in _kind_to_cpp_proto_type:
      raise Exception("Unrecognized kind %s" % kind.spec)
    return _kind_to_cpp_proto_type[kind]
示例#18
0
def AddImport(imports, mojom_imports, module, element):
  """Adds an import required to use the provided element.

  The required import is stored in the imports parameter.
  The corresponding mojom import is stored in the mojom_imports parameter.
  Each import is also updated to include a 'go_name' entry. The 'go_name' entry
  is the name by which the imported module will be referred to in the generated
  code. Because the import dictionary is accessible from the element's
  imported_from field this allows us to generate the qualified name for the
  element.

  Args:
    imports: {dict<str, str>} The key is the path to the import and the value
      is the go name.
    mojom_imports: {dict<str, str>} The key is the path to the import and the
      value is the go name.
    module: {module.Module} the module being processed.
    element: {module.Kind} the element whose import is to be tracked.
  """
  if not isinstance(element, mojom.Kind):
    return

  if mojom.IsArrayKind(element) or mojom.IsInterfaceRequestKind(element):
    AddImport(imports, mojom_imports, module, element.kind)
    return
  if mojom.IsMapKind(element):
    AddImport(imports, mojom_imports, module, element.key_kind)
    AddImport(imports, mojom_imports, module, element.value_kind)
    return
  if mojom.IsAnyHandleKind(element):
    imports['mojo/public/go/system'] = 'system'
    return

  if not hasattr(element, 'imported_from') or not element.imported_from:
    return
  imported = element.imported_from
  if GetPackagePath(imported['module']) == GetPackagePath(module):
    return
  path = GetPackagePath(imported['module'])
  if path in imports:
    return
  name = GetPackageName(imported['module'])
  while name in imports.values(): # This avoids repeated names.
    name += '_'
  imported['go_name'] = name
  imports[path] = name
  mojom_imports[path] = name
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.IsArrayKind(field.kind):
        return "null"
    if mojom.IsInterfaceKind(field.kind) or \
       mojom.IsInterfaceRequestKind(field.kind):
        return _kind_to_javascript_default_value[mojom.MSGPIPE]
    if mojom.IsEnumKind(field.kind):
        return "0"
示例#20
0
def DartDeclType(kind):
    if kind in mojom.PRIMITIVES:
        return _kind_to_dart_decl_type[kind]
    if mojom.IsStructKind(kind):
        return GetDartType(kind)
    if mojom.IsArrayKind(kind):
        array_type = DartDeclType(kind.kind)
        return "List<" + array_type + ">"
    if mojom.IsMapKind(kind):
        key_type = DartDeclType(kind.key_kind)
        value_type = DartDeclType(kind.value_kind)
        return "Map<" + key_type + ", " + value_type + ">"
    if mojom.IsInterfaceKind(kind) or \
       mojom.IsInterfaceRequestKind(kind):
        return "Object"
    if mojom.IsEnumKind(kind):
        return "int"
示例#21
0
def GetJavaType(context, kind, boxed=False):
    if boxed:
        return GetBoxedJavaType(context, kind)
    if mojom.IsStructKind(kind) or mojom.IsInterfaceKind(kind):
        return GetNameForKind(context, kind)
    if mojom.IsInterfaceRequestKind(kind):
        return ('org.chromium.mojo.bindings.InterfaceRequest<%s>' %
                GetNameForKind(context, kind.kind))
    if mojom.IsMapKind(kind):
        return 'java.util.Map<%s, %s>' % (GetBoxedJavaType(
            context, kind.key_kind), GetBoxedJavaType(context,
                                                      kind.value_kind))
    if mojom.IsArrayKind(kind):
        return '%s[]' % GetJavaType(context, kind.kind)
    if mojom.IsEnumKind(kind):
        return 'int'
    return _spec_to_java_type[kind.spec]
示例#22
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)
示例#23
0
def CodecType(kind):
    if kind in mojom.PRIMITIVES:
        return _kind_to_codec_type[kind]
    if mojom.IsStructKind(kind):
        pointer_type = "NullablePointerTo" if mojom.IsNullableKind(kind) \
            else "PointerTo"
        return "new codec.%s(%s)" % (pointer_type, JavaScriptType(kind))
    if mojom.IsAnyArrayKind(kind):
        array_type = "NullableArrayOf" if mojom.IsNullableKind(
            kind) else "ArrayOf"
        element_type = "codec.PackedBool" if mojom.IsBoolKind(kind.kind) \
            else CodecType(kind.kind)
        return "new codec.%s(%s)" % (array_type, element_type)
    if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
        return CodecType(mojom.MSGPIPE)
    if mojom.IsEnumKind(kind):
        return _kind_to_codec_type[mojom.INT32]
    return kind
示例#24
0
def CodecType(kind):
  if kind in mojom.PRIMITIVES:
    return _kind_to_codec_type[kind]
  if mojom.IsStructKind(kind):
    pointer_type = "NullablePointerTo" if mojom.IsNullableKind(kind) \
        else "PointerTo"
    return "new bindings.%s(%s)" % (pointer_type, DartType(kind))
  if mojom.IsArrayKind(kind):
    array_type = "NullableArrayOf" if mojom.IsNullableKind(kind) else "ArrayOf"
    array_length = "" if kind.length is None else ", %d" % kind.length
    element_type = "bindings.PackedBool" if mojom.IsBoolKind(kind.kind) \
        else CodecType(kind.kind)
    return "new bindings.%s(%s%s)" % (array_type, element_type, array_length)
  if mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind):
    return CodecType(mojom.MSGPIPE)
  if mojom.IsEnumKind(kind):
    return _kind_to_codec_type[mojom.INT32]
  return kind
示例#25
0
def GetCppArrayArgWrapperType(kind):
    if mojom.IsStructKind(kind) and kind.native_only:
        if IsTypemappedKind(kind):
            return GetNativeTypeName(kind)
        else:
            # Without a relevant typemap to apply, a native-only struct can only be
            # exposed as a blob of bytes.
            return "mojo::Array<uint8_t>"
    if IsTypemappedKind(kind):
        raise Exception(
            "Cannot serialize containers of non-native typemapped structs yet!"
        )
    if mojom.IsEnumKind(kind):
        return GetNameForKind(kind)
    if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind):
        return "%sPtr" % GetNameForKind(kind)
    if mojom.IsArrayKind(kind):
        pattern = "mojo::WTFArray<%s>" if _for_blink else "mojo::Array<%s>"
        return pattern % GetCppArrayArgWrapperType(kind.kind)
    if mojom.IsMapKind(kind):
        return "mojo::Map<%s, %s> " % (GetCppArrayArgWrapperType(
            kind.key_kind), GetCppArrayArgWrapperType(kind.value_kind))
    if mojom.IsInterfaceKind(kind):
        raise Exception("Arrays of interfaces not yet supported!")
    if mojom.IsInterfaceRequestKind(kind):
        raise Exception("Arrays of interface requests not yet supported!")
    if mojom.IsAssociatedInterfaceKind(kind):
        raise Exception("Arrays of associated interfaces not yet supported!")
    if mojom.IsAssociatedInterfaceRequestKind(kind):
        raise Exception("Arrays of associated interface requests not yet "
                        "supported!")
    if mojom.IsStringKind(kind):
        return "WTF::String" if _for_blink else "mojo::String"
    if mojom.IsGenericHandleKind(kind):
        return "mojo::ScopedHandle"
    if mojom.IsDataPipeConsumerKind(kind):
        return "mojo::ScopedDataPipeConsumerHandle"
    if mojom.IsDataPipeProducerKind(kind):
        return "mojo::ScopedDataPipeProducerHandle"
    if mojom.IsMessagePipeKind(kind):
        return "mojo::ScopedMessagePipeHandle"
    if mojom.IsSharedBufferKind(kind):
        return "mojo::ScopedSharedBufferHandle"
    return _kind_to_cpp_type[kind]
示例#26
0
def DartDefaultValue(field):
    if field.default:
        if mojom.IsStructKind(field.kind):
            assert field.default == "default"
            return "new %s()" % GetDartType(field.kind)
        return ExpressionToText(field.default)
    if field.kind in mojom.PRIMITIVES:
        return _kind_to_dart_default_value[field.kind]
    if mojom.IsStructKind(field.kind):
        return "null"
    if mojom.IsArrayKind(field.kind):
        return "null"
    if mojom.IsMapKind(field.kind):
        return "null"
    if mojom.IsInterfaceKind(field.kind) or \
       mojom.IsInterfaceRequestKind(field.kind):
        return "null"
    if mojom.IsEnumKind(field.kind):
        return "0"
示例#27
0
def GetCppType(kind):
  if mojom.IsArrayKind(kind):
    return "::fidl::internal::Array_Data<%s>*" % GetCppType(kind.kind)
  if mojom.IsMapKind(kind):
    return "::fidl::internal::Map_Data<%s, %s>*" % (
      GetCppType(kind.key_kind), GetCppType(kind.value_kind))
  if mojom.IsStructKind(kind):
    return "%s_Data*" % GetNameForKind(kind, internal=True)
  if mojom.IsUnionKind(kind):
    return "%s_Data" % GetNameForKind(kind, internal=True)
  if mojom.IsInterfaceKind(kind):
    return "::fidl::internal::Interface_Data"
  if mojom.IsInterfaceRequestKind(kind):
    return "::fidl::internal::WrappedHandle"
  if mojom.IsEnumKind(kind):
    return "int32_t"
  if mojom.IsStringKind(kind):
    return "::fidl::internal::String_Data*"
  return GetCppTypeForKind(kind)
示例#28
0
def GetCppResultWrapperType(kind):
    if IsTypemappedKind(kind):
        return "const %s&" % GetNativeTypeName(kind)
    if mojom.IsStructKind(kind) and kind.native_only:
        return "mojo::Array<uint8_t>"
    if mojom.IsEnumKind(kind):
        return GetNameForKind(kind)
    if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind):
        return "%sPtr" % GetNameForKind(kind)
    if mojom.IsArrayKind(kind):
        pattern = "mojo::WTFArray<%s>" if _for_blink else "mojo::Array<%s>"
        return pattern % GetCppArrayArgWrapperType(kind.kind)
    if mojom.IsMapKind(kind):
        return "mojo::Map<%s, %s>" % (GetCppArrayArgWrapperType(
            kind.key_kind), GetCppArrayArgWrapperType(kind.value_kind))
    if mojom.IsInterfaceKind(kind):
        return "%sPtr" % GetNameForKind(kind)
    if mojom.IsInterfaceRequestKind(kind):
        return "%sRequest" % GetNameForKind(kind.kind)
    if mojom.IsAssociatedInterfaceKind(kind):
        return "%sAssociatedPtrInfo" % GetNameForKind(kind.kind)
    if mojom.IsAssociatedInterfaceRequestKind(kind):
        return "%sAssociatedRequest" % GetNameForKind(kind.kind)
    if mojom.IsStringKind(kind):
        return "WTF::String" if _for_blink else "mojo::String"
    if mojom.IsGenericHandleKind(kind):
        return "mojo::ScopedHandle"
    if mojom.IsDataPipeConsumerKind(kind):
        return "mojo::ScopedDataPipeConsumerHandle"
    if mojom.IsDataPipeProducerKind(kind):
        return "mojo::ScopedDataPipeProducerHandle"
    if mojom.IsMessagePipeKind(kind):
        return "mojo::ScopedMessagePipeHandle"
    if mojom.IsSharedBufferKind(kind):
        return "mojo::ScopedSharedBufferHandle"
    # TODO(rudominer) After improvements to compiler front end have landed,
    # revisit strategy used below for emitting a useful error message when an
    # undefined identifier is referenced.
    val = _kind_to_cpp_type.get(kind)
    if (val is not None):
        return val
    raise Exception("Unrecognized kind %s" % kind.spec)
示例#29
0
 def _FuzzHandleName(self, kind):
     if mojom.IsInterfaceRequestKind(kind):
         return '{0}.{1}Request'.format(kind.kind.module.namespace,
                                        kind.kind.name)
     elif mojom.IsInterfaceKind(kind):
         return '{0}.{1}Ptr'.format(kind.module.namespace, kind.name)
     elif mojom.IsAssociatedInterfaceRequestKind(kind):
         return '{0}.{1}AssociatedRequest'.format(
             kind.kind.module.namespace, kind.kind.name)
     elif mojom.IsAssociatedInterfaceKind(kind):
         return '{0}.{1}AssociatedPtr'.format(kind.kind.module.namespace,
                                              kind.kind.name)
     elif mojom.IsSharedBufferKind(kind):
         return 'handle<shared_buffer>'
     elif mojom.IsDataPipeConsumerKind(kind):
         return 'handle<data_pipe_consumer>'
     elif mojom.IsDataPipeProducerKind(kind):
         return 'handle<data_pipe_producer>'
     elif mojom.IsMessagePipeKind(kind):
         return 'handle<message_pipe>'
示例#30
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)