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, " % \ (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.IsInterfaceKind(kind): return "encodeStruct(%s, " % CodecType(kind) if mojom.IsInterfaceRequestKind(kind): return JavaScriptEncodeSnippet(mojom.MSGPIPE) if mojom.IsEnumKind(kind): return JavaScriptEncodeSnippet(mojom.INT32)
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
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
def GetArrayValidateParamsCtorArgs(kind): if mojom.IsStringKind(kind) or (mojom.IsStructKind(kind) and kind.native_only): expected_num_elements = 0 element_is_nullable = False element_validate_params = "nullptr" elif mojom.IsMapKind(kind): expected_num_elements = 0 element_is_nullable = mojom.IsNullableKind(kind.value_kind) element_validate_params = GetNewArrayValidateParams(kind.value_kind) else: expected_num_elements = generator.ExpectedArraySize(kind) or 0 element_is_nullable = mojom.IsNullableKind(kind.kind) element_validate_params = GetNewArrayValidateParams(kind.kind) return "%d, %s, %s" % (expected_num_elements, "true" if element_is_nullable else "false", element_validate_params)
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)
def _JavaScriptEncodeSnippet(self, kind): if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or mojom.IsAnyInterfaceKind(kind)): return "encodeStruct(%s, " % self._CodecType(kind) if mojom.IsUnionKind(kind): return "encodeStruct(%s, " % self._JavaScriptType(kind) if mojom.IsStructKind(kind): return "encodeStructPointer(%s, " % self._JavaScriptType(kind) if mojom.IsMapKind(kind): return "encodeMapPointer(%s, %s, " % (self._ElementCodecType( kind.key_kind), self._ElementCodecType(kind.value_kind)) if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): return "encodeArrayPointer(codec.PackedBool, " if mojom.IsArrayKind(kind): return "encodeArrayPointer(%s, " % self._CodecType(kind.kind) if mojom.IsEnumKind(kind): return self._JavaScriptEncodeSnippet(mojom.INT32) raise Exception("No encode snippet for %s" % kind)
def _KindMustBeSerialized(self, kind, processed_kinds=None): if not processed_kinds: processed_kinds = set() if kind in processed_kinds: return False if (self._IsTypemappedKind(kind) and self.typemap[self._GetFullMojomNameForKind(kind)]["force_serialize"]): return True processed_kinds.add(kind) if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): return any(self._KindMustBeSerialized(field.kind, processed_kinds=processed_kinds) for field in kind.fields) return False
def JavaScriptDecodeSnippet(kind): if (kind in mojom.PRIMITIVES or mojom.IsUnionKind(kind) or mojom.IsAnyInterfaceKind(kind)): return "decodeStruct(%s)" % CodecType(kind) if mojom.IsStructKind(kind): return "decodeStructPointer(%s)" % JavaScriptType(kind) if mojom.IsMapKind(kind): return "decodeMapPointer(%s, %s)" % \ (ElementCodecType(kind.key_kind), ElementCodecType(kind.value_kind)) if mojom.IsArrayKind(kind) and mojom.IsBoolKind(kind.kind): return "decodeArrayPointer(codec.PackedBool)" if mojom.IsArrayKind(kind): return "decodeArrayPointer(%s)" % CodecType(kind.kind) if mojom.IsUnionKind(kind): return "decodeUnion(%s)" % CodecType(kind) if mojom.IsEnumKind(kind): return JavaScriptDecodeSnippet(mojom.INT32) raise Exception("No decode snippet for %s" % kind)
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)
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): 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]
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 "(Map<%s, %s>|Object<%s, %s>)" % ( self._LiteClosureTypeWithNullability(kind.key_kind), self._LiteClosureTypeWithNullability(kind.value_kind), 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): 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)
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 "int"
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)
def _IsFullHeaderRequiredForImport(self, imported_module): """Determines whether a given import module requires a full header include, or if the forward header is sufficient.""" # Type-mapped kinds may not have forward declarations, and nested kinds # cannot be forward declared. if any(kind.module == imported_module and ((self._IsTypemappedKind( kind) and not self._GetTypemappedForwardDeclaration(kind)) or kind.parent_kind != None) for kind in self.module.imported_kinds.values()): return True # For most kinds, whether or not a full definition is needed depends on how # the kind is used. for kind in self.module.structs + self.module.unions: for field in kind.fields: # Peel array kinds. kind = field.kind while mojom.IsArrayKind(kind): kind = kind.kind if kind.module == imported_module: # Need full def for struct/union fields, even when not inlined. if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): return True for kind in self.module.kinds.values(): if mojom.IsMapKind(kind): if kind.key_kind.module == imported_module: # Map keys need the full definition. return True if self.for_blink and kind.value_kind.module == imported_module: # For Blink, map values need the full definition for tracing. return True for constant in self.module.constants: # Constants referencing enums need the full definition. if mojom.IsEnumKind(constant.kind ) and constant.value.module == imported_module: return True return False
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]
def _GetProtoFieldType(self, kind, quantified=True): # TODO(markbrand): This will not handle array<array> or array<map> # TODO(markbrand): This also will not handle array<x, 10> unquantified = '' if (mojom.IsEnumKind(kind) or mojom.IsStructKind(kind) or mojom.IsUnionKind(kind)): unquantified = self._GetProtoNameForKind(kind) elif mojom.IsArrayKind(kind): return "repeated %sEntry" % self._GetProtoFieldType( kind.kind, quantified=False) elif mojom.IsMapKind(kind): return ( "map<%sKey, %sValue>" % (self._GetProtoFieldType(kind.key_kind, quantified=False), self._GetProtoFieldType(kind.value_kind, quantified=False))) elif (mojom.IsPendingRemoteKind(kind) or mojom.IsPendingReceiverKind(kind) or mojom.IsPendingAssociatedRemoteKind(kind) or mojom.IsPendingAssociatedReceiverKind(kind)): unquantified = "uint32" elif mojom.IsStringKind(kind): unquantified = "string" elif mojom.IsGenericHandleKind(kind): unquantified = "mojolpm.Handle" elif mojom.IsDataPipeConsumerKind(kind): unquantified = "mojolpm.DataPipeConsumerHandle" elif mojom.IsDataPipeProducerKind(kind): unquantified = "mojolpm.DataPipeProducerHandle" elif mojom.IsMessagePipeKind(kind): unquantified = "mojolpm.MessagePipeHandle" elif mojom.IsSharedBufferKind(kind): unquantified = "mojolpm.SharedBufferHandle" elif mojom.IsPlatformHandleKind(kind): unquantified = "mojolpm.PlatformHandle" else: unquantified = _kind_to_proto_type[kind] if quantified and mojom.IsNullableKind(kind): return 'optional %s' % unquantified elif quantified: return 'required %s' % unquantified else: return unquantified
def _GetDefaultValue(self, field, for_module=False): if field.default: if mojom.IsStructKind(field.kind): assert field.default == "default" return "null" if ((field.kind == mojom.INT64 or field.kind == mojom.UINT64) and not isinstance(field.default, (mojom.EnumValue, mojom.NamedValue, mojom.BuiltinValue))): return "BigInt('{}')".format(int(field.default, 0)) return self._ExpressionToTextLite(field.default, for_module=for_module) if field.kind == mojom.INT64 or field.kind == mojom.UINT64: return "BigInt(0)" if field.kind in mojom.PRIMITIVES: return _kind_to_javascript_default_value[field.kind] if mojom.IsEnumKind(field.kind): return "0" return "null"
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]
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)
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
def AddKind(kind): if IsBasicKind(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 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)
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)
def GetCppResultWrapperType(kind): if IsTypemappedKind(kind): return "const %s&" % 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): 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)
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
def _GetExpectedCppParamType(self, kind): def is_move_only_kind(kind): if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): return True if mojom.IsArrayKind(kind): return is_move_only_kind(kind.kind) if mojom.IsMapKind(kind): return (is_move_only_kind(kind.value_kind) or is_move_only_kind(kind.key_kind)) if mojom.IsAnyHandleOrInterfaceKind(kind): return True return False should_pass_param_by_value = ((not mojom.IsReferenceKind(kind)) or is_move_only_kind(kind)) typemap = MojoTypemapForKind(kind, False) typestring = typemap.ExpectedCppType() if mojom.IsNullableKind(kind) and not mojom.IsStructKind(kind): typestring = "absl::optional<%s>" % typestring if should_pass_param_by_value: return typestring return "const %s&" % typestring
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 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)
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): 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) return _kind_to_type.get(kind, '_descriptor.TYPE_NONE')
def GetCppDataViewType(kind, qualified=False): def _GetName(input_kind): return _NameFormatter(input_kind, None).FormatForCpp( add_same_module_namespaces=qualified, 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>" % GetCppDataViewType( kind.kind, qualified) if mojom.IsMapKind(kind): return ("mojo::MapDataView<%s, %s>" % (GetCppDataViewType(kind.key_kind, qualified), 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]