示例#1
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)):
      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) or mojom.IsPendingRemoteKind(kind):
      return name + "Proxy"
    if mojom.IsInterfaceRequestKind(kind) or mojom.IsPendingReceiverKind(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)
示例#2
0
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.IsMapKind(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"
示例#3
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.IsUnionKind(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 GetDartType(kind)
示例#4
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.IsArrayKind(kind):
        array_type = "NullableArrayOf" if mojom.IsNullableKind(
            kind) else "ArrayOf"
        array_length = "" if kind.length is None else ", %d" % kind.length
        element_type = "codec.PackedBool" if mojom.IsBoolKind(kind.kind) \
            else CodecType(kind.kind)
        return "new codec.%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
示例#5
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)
示例#6
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.IsUnionKind(kind):
        return "%s_Data" % GetNameForKind(kind, internal=True)
    if mojom.IsInterfaceKind(kind):
        return "mojo::internal::Interface_Data"
    if 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]
示例#7
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):
        return "mojo::Array<%s> " % 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 "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]
示例#8
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)
示例#9
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>'
示例#10
0
    def _GetImportsForKind(self, kind):
        qualified_name = self._GetNameInJsModule(kind)

        def make_import(name, suffix=''):
            class ImportInfo(object):
                def __init__(self, name, alias):
                    self.name = name
                    self.alias = alias

            return ImportInfo(name + suffix, qualified_name + suffix)

        if (mojom.IsEnumKind(kind) or mojom.IsStructKind(kind)
                or mojom.IsUnionKind(kind)):
            return [make_import(kind.name), make_import(kind.name, 'Spec')]
        if mojom.IsInterfaceKind(kind):
            return [
                make_import(kind.name, 'Remote'),
                make_import(kind.name, 'PendingReceiver')
            ]
        assert False, kind.name
示例#11
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)
示例#12
0
def GetJavaType(context, kind, boxed=False, with_generics=True):
  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):
    if with_generics:
      return 'java.util.Map<%s, %s>' % (
          GetBoxedJavaType(context, kind.key_kind),
          GetBoxedJavaType(context, kind.value_kind))
    else:
      return 'java.util.Map'
  if mojom.IsArrayKind(kind):
    return '%s[]' % GetJavaType(context, kind.kind, boxed, with_generics)
  if mojom.IsEnumKind(kind):
    return 'int'
  return _spec_to_java_type[kind.spec]
示例#13
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.IsInterfaceRequestKind(kind):
         return "codec.%s" % ("NullableInterfaceRequest" if mojom.
                              IsNullableKind(kind) else "InterfaceRequest")
     if mojom.IsAssociatedInterfaceKind(kind):
         return "codec.%s" % ("NullableAssociatedInterfacePtrInfo"
                              if mojom.IsNullableKind(kind) else
                              "AssociatedInterfacePtrInfo")
     if mojom.IsAssociatedInterfaceRequestKind(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)
示例#14
0
def GetCppFieldType(kind):
    if mojom.IsStructKind(kind):
        return ("mojo::internal::StructPointer<%s_Data>" %
                GetNameForKind(kind, internal=True))
    if mojom.IsUnionKind(kind):
        return "%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):
        return "mojo::internal::Interface_Data"
    if mojom.IsInterfaceRequestKind(kind):
        return "mojo::MessagePipeHandle"
    if mojom.IsEnumKind(kind):
        return GetNameForKind(kind)
    if mojom.IsStringKind(kind):
        return "mojo::internal::StringPointer"
    return GetCppTypeForKind(kind)
示例#15
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.IsUnionKind(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"
示例#16
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)
示例#17
0
 def _GetObjCWrapperType(self, kind, objectType=False):
     if self._IsMojomStruct(kind):
         return "%s%s *" % (self.class_prefix, kind.name)
     if mojom.IsEnumKind(kind):
         return "%s%s" % (self.class_prefix, kind.name)
     if mojom.IsInterfaceKind(kind):
         return "id<%s%s>" % (self.class_prefix, kind.name)
     if mojom.IsStringKind(kind):
         return "NSString *"
     if mojom.IsArrayKind(kind):
         return "NSArray<%s> *" % self._GetObjCWrapperType(kind.kind, True)
     if mojom.IsMapKind(kind):
         return "NSDictionary<%s, %s> *" % \
         (self._GetObjCWrapperType(kind.key_kind, True),
          self._GetObjCWrapperType(kind.value_kind, True))
     if self._IsObjCNumberKind(kind):
         if objectType:
             return "NSNumber *"
         else:
             return _kind_to_objc_type[kind]
     raise Exception("Unrecognized kind %s" % kind)
示例#18
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
示例#19
0
def GetCppConstWrapperType(kind):
  if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind):
    return "%sPtr" % GetNameForKind(kind)
  if mojom.IsArrayKind(kind):
    return "::fidl::Array<%s>" % GetCppArrayArgWrapperType(kind.kind)
  if mojom.IsMapKind(kind):
    return "::fidl::Map<%s, %s>" % (GetCppArrayArgWrapperType(kind.key_kind),
                                  GetCppArrayArgWrapperType(kind.value_kind))
  if mojom.IsInterfaceKind(kind):
    return "::fidl::InterfaceHandle<%s>" % GetNameForKind(kind)
  if mojom.IsInterfaceRequestKind(kind):
    return "::fidl::InterfaceRequest<%s>" % GetNameForKind(kind.kind)
  if mojom.IsEnumKind(kind):
    return GetNameForKind(kind)
  if mojom.IsStringKind(kind):
    return "const ::fidl::String&"
  if mojom.IsGenericHandleKind(kind):
    return "zx::handle"
  if mojom.IsChannelKind(kind):
    return "zx::channel"
  if mojom.IsVMOKind(kind):
    return "zx::vmo"
  if mojom.IsProcessKind(kind):
    return "zx::process"
  if mojom.IsThreadKind(kind):
    return "zx::thread"
  if mojom.IsEventKind(kind):
    return "zx::event"
  if mojom.IsPortKind(kind):
    return "zx::port"
  if mojom.IsJobKind(kind):
    return "zx::job"
  if mojom.IsSocketKind(kind):
    return "zx::socket"
  if mojom.IsEventPairKind(kind):
    return "zx::eventpair"
  if not kind in _kind_to_cpp_type:
    print "missing:", kind.spec
  return GetCppTypeForKind(kind)
示例#20
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.IsArrayKind(kind):
    array_type = "NullableArrayOf" if mojom.IsNullableKind(kind) else "ArrayOf"
    array_length = "" if kind.length is None else ", %d" % kind.length
    element_type = ElementCodecType(kind.kind)
    return "new codec.%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]
  if mojom.IsMapKind(kind):
    map_type = "NullableMapOf" if mojom.IsNullableKind(kind) else "MapOf"
    key_type = ElementCodecType(kind.key_kind)
    value_type = ElementCodecType(kind.value_kind)
    return "new codec.%s(%s, %s)" % (map_type, key_type, value_type)
  return kind
    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.IsAssociatedInterfaceKind(kind):
            return "%sAssociatedPtrInfoDataView" % _GetName(kind.kind)
        if mojom.IsAssociatedInterfaceRequestKind(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"
        return _kind_to_cpp_type[kind]
示例#22
0
def GetCppConstWrapperType(kind):
    if IsTypemappedKind(kind):
        return "const %s&" % GetNativeTypeName(kind)
    if mojom.IsStructKind(kind) and kind.native_only:
        return "mojo::Array<uint8_t>"
    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.IsEnumKind(kind):
        return GetNameForKind(kind)
    if mojom.IsStringKind(kind):
        return "const WTF::String&" if _for_blink else "const 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"
    if not kind in _kind_to_cpp_type:
        print "missing:", kind.spec
    return _kind_to_cpp_type[kind]
示例#23
0
def GetNameForElement(element):
  if (mojom.IsInterfaceKind(element) or mojom.IsStructKind(element) or
      mojom.IsUnionKind(element)):
    return UpperCamelCase(element.name)
  if mojom.IsEnumKind(element):
    if element.parent_kind:
      return ("%s%s" % (UpperCamelCase(element.parent_kind.name),
                        UpperCamelCase(element.name)))
    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)
示例#24
0
def GetCppWrapperType(kind):
    if IsTypemappedKind(kind):
        return GetNativeTypeName(kind)
    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):
        pattern = "mojo::WTFMap<%s, %s>" if _for_blink else "mojo::Map<%s, %s>"
        return pattern % (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"
    if not kind in _kind_to_cpp_type:
        raise Exception("Unrecognized kind %s" % kind.spec)
    return _kind_to_cpp_type[kind]
示例#25
0
def GetCppArrayArgWrapperType(kind):
    if IsTypemappedKind(kind):
        return GetNativeTypeName(kind)
    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):
        pattern = "mojo::WTFMap<%s, %s>" if _for_blink else "mojo::Map<%s, %s>"
        return pattern % (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]
def GetCppResultWrapperType(kind):
    if mojom.IsEnumKind(kind):
        return GetNameForKind(kind)
    if mojom.IsStructKind(kind):
        return "%sPtr" % GetNameForKind(kind)
    if mojom.IsAnyArrayKind(kind):
        return "mojo::Array<%s>" % GetCppArrayArgWrapperType(kind.kind)
    if mojom.IsInterfaceKind(kind):
        return "%sPtr" % GetNameForKind(kind)
    if mojom.IsInterfaceRequestKind(kind):
        return "mojo::InterfaceRequest<%s>" % GetNameForKind(kind.kind)
    if mojom.IsStringKind(kind):
        return "mojo::String"
    if mojom.IsHandleKind(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]
def GetCppArrayArgWrapperType(kind):
    if mojom.IsEnumKind(kind):
        return GetNameForKind(kind)
    if mojom.IsStructKind(kind):
        return "%sPtr" % GetNameForKind(kind)
    if mojom.IsAnyArrayKind(kind):
        return "mojo::Array<%s> " % GetCppArrayArgWrapperType(kind.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.IsStringKind(kind):
        return "mojo::String"
    if mojom.IsHandleKind(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]
示例#28
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
示例#29
0
def _WriteInputParamForTracingImpl(generator, kind, cpp_parameter_name,
                                   output_context):
  """Generates lines of C++ to log a parameter into TracedValue
  |output_context.value|. Use |output_context.name| if |output_context| is of
  inhereted type from _OutputContext.

  Args:
    kind: {Kind} The kind of the parameter (corresponds to its C++ type).
    cpp_parameter_name: {string} The actual C++ variable name corresponding to
      the mojom parameter |parameter_name|. Can be a valid C++ expression
      (e.g., dereferenced variable |"(*var)"|).
    output_context: {_OutputContext} Represents the TracedValue* variable to be
      written into. Possibly also holds the mojo parameter name corresponding to
      |cpp_parameter_name|.

  Yields:
    {string} C++ lines of code that trace a |cpp_parameter_name| into
      |output_context.value|.
  """

  def _WrapIfNullable(inner_lines):
    """Check if kind is nullable if so yield code to check whether it has
    value.

    Args:
      inner_lines: {function} Function taking single argument and returning
        iterable. If kind is nullable, yield from this method with
        |cpp_parameter_name+'.value()'| otherwise yield with the parameter
        |cpp_parameter_name|.

    Args from the surrounding method:
      kind
      cpp_parameter_name
      output_context.AddSingleValue
    """
    if mojom.IsNullableKind(kind):
      yield 'if (%s.has_value()) {' % cpp_parameter_name
      for line in inner_lines(cpp_parameter_name + '.value()'):
        yield '  ' + line
      yield '} else {'
      yield '  ' + output_context.AddSingleValue('String', '"base::nullopt"')
      yield '}'
    else:
      # |yield from| is introduced in Python3.3.
      for line in inner_lines(cpp_parameter_name):
        yield line

  # TODO(crbug.com/1103623): Support more involved types.
  if mojom.IsEnumKind(kind):
    if generator._IsTypemappedKind(kind) or IsNativeOnlyKind(kind):
      yield output_context.AddSingleValue(
          'Integer', 'static_cast<int>(%s)' % cpp_parameter_name)
    else:
      yield output_context.AddSingleValue(
          'String', 'base::trace_event::ValueToString(%s)' % cpp_parameter_name)
    return
  if mojom.IsStringKind(kind):
    if generator.for_blink:
      # WTF::String is nullable on its own.
      yield output_context.AddSingleValue('String',
                                          '%s.Utf8()' % cpp_parameter_name)
      return
    # The type might be base::Optional<std::string> or std::string.
    for line in _WrapIfNullable(lambda cpp_parameter_name: [
        output_context.AddSingleValue('String', cpp_parameter_name)
    ]):
      yield line
    return
  if kind == mojom.BOOL:
    yield output_context.AddSingleValue('Boolean', cpp_parameter_name)
    return
  # TODO(crbug.com/1103623): Make TracedValue support int64_t, then move to
  # mojom.IsIntegralKind.
  if kind in [mojom.INT8, mojom.UINT8, mojom.INT16, mojom.UINT16, mojom.INT32]:
    # Parameter is representable as 32bit int.
    yield output_context.AddSingleValue('Integer', cpp_parameter_name)
    return
  if kind in [mojom.UINT32, mojom.INT64, mojom.UINT64]:
    yield output_context.AddSingleValue(
        'String', 'base::NumberToString(%s)' % cpp_parameter_name)
    return
  if mojom.IsFloatKind(kind) or mojom.IsDoubleKind(kind):
    yield output_context.AddSingleValue('Double', cpp_parameter_name)
    return
  if (mojom.IsStructKind(kind) and not generator._IsTypemappedKind(kind)
      and not IsNativeOnlyKind(kind)):
    yield 'if (%s.is_null()) {' % cpp_parameter_name
    yield '  ' + output_context.AddSingleValue('String', '"nullptr"')
    yield '} else {'
    yield '  ' + output_context.BeginContainer('Dictionary')
    yield '  ' + output_context.AsValueInto(cpp_parameter_name)
    yield '  ' + output_context.EndContainer('Dictionary')
    yield '}'
    return

  if mojom.IsArrayKind(kind):
    iterator_name = 'item'
    loop_body = _WriteInputParamForTracingImpl(generator=generator,
                                               kind=kind.kind,
                                               cpp_parameter_name=iterator_name,
                                               output_context=_ArrayItem(
                                                   output_context.value))
    loop_generator = lambda cpp_parameter_name: output_context.TraceContainer(
        container_type='Array',
        iterator_name=iterator_name,
        container_name=cpp_parameter_name,
        loop_body=loop_body)
    # Array might be a nullable kind.
    for line in _WrapIfNullable(loop_generator):
      yield line
    return

  def _TraceEventToString(cpp_parameter_name=cpp_parameter_name, kind=kind):
    return 'base::trace_event::ValueToString(%s, "<value of type %s>")' % (
        cpp_parameter_name, generator._GetCppWrapperParamType(kind))

  if mojom.IsMapKind(kind):
    iterator_name = 'item'
    if generator.for_blink:
      # WTF::HashMap<,>
      key_access = '.key'
      value_access = '.value'
    else:
      # base::flat_map<,>
      key_access = '.first'
      value_access = '.second'
    loop_body = _WriteInputParamForTracingImpl(
        generator=generator,
        kind=kind.value_kind,
        cpp_parameter_name=iterator_name + value_access,
        output_context=_DictionaryItemWithCopiedKey(
            value=output_context.value,
            name=_TraceEventToString(cpp_parameter_name=iterator_name +
                                     key_access,
                                     kind=kind.key_kind)))
    loop_generator = lambda cpp_parameter_name: output_context.TraceContainer(
        container_type="Dictionary",
        iterator_name=iterator_name,
        container_name=cpp_parameter_name,
        loop_body=loop_body)
    # Dictionary might be a nullable kind.
    for line in _WrapIfNullable(loop_generator):
      yield line
    return
  if (mojom.IsInterfaceRequestKind(kind)
      or mojom.IsAssociatedInterfaceRequestKind(kind)):
    yield output_context.AddSingleValue('Boolean',
                                        cpp_parameter_name + '.is_pending()')
    return
  if (mojom.IsAnyHandleOrInterfaceKind(kind)
      and not mojom.IsInterfaceKind(kind)):
    yield output_context.AddSingleValue('Boolean',
                                        cpp_parameter_name + '.is_valid()')
    return
  """ The case |mojom.IsInterfaceKind(kind)| is not covered.
  |mojom.IsInterfaceKind(kind) == True| for the following types:
  |mojo::InterfacePtrInfo|, |mojo::InterfacePtr|.
    There is |mojo::InterfacePtrInfo::is_valid|,
      but not |mojo::InterfacePtrInfo::is_bound|.
    There is |mojo::InterfacePtr::is_bound|,
      but not |mojo::InterfacePtr::is_valid|.

  Both |mojo::InterfacePtrInfo| and |mojo::InterfacePtr| are deprecated.
  """
  yield output_context.AddSingleValue('String', _TraceEventToString())
示例#30
0
 def _GetCppWrapperCallType(self, kind):
   # TODO: Remove this once interfaces are always passed as PtrInfo.
   if mojom.IsInterfaceKind(kind):
     return "%sPtr" % self._GetNameForKind(kind)
   return self._GetCppWrapperType(kind)