def _IsMoveOnlyKind(self, kind): if self._IsTypemappedKind(kind): if mojom.IsEnumKind(kind): return False return self.typemap[self._GetFullMojomNameForKind( kind)]["move_only"] if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): return True if mojom.IsArrayKind(kind): return self._IsMoveOnlyKind(kind.kind) if mojom.IsMapKind(kind): return (self._IsMoveOnlyKind(kind.value_kind) or self._IsMoveOnlyKind(kind.key_kind)) if mojom.IsAnyHandleOrInterfaceKind(kind): return True return False
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) or mojom.IsPendingRemoteKind(kind) or mojom.IsPendingReceiverKind(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 []
def get_type_name(kind): if mojom.IsEnumKind(kind): return "number" prefix = "" if mojom.IsNullableKind(kind) else "!" if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): return prefix + "Object" if mojom.IsArrayKind(kind): return prefix + ("Array<%s>" % get_type_name(kind.kind)) if mojom.IsMapKind(kind): return "%sMap<%s, %s>|%sObject<%s, %s>" % ( prefix, get_type_name( kind.key_kind), get_type_name(kind.value_kind), prefix, get_type_name(kind.key_kind), get_type_name( kind.value_kind)) return prefix + self._GetTypeNameForNewBindings( kind, for_module=for_module)
def GetArrayValidateParamsCtorArgs(kind): if mojom.IsStringKind(kind): 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 GetAllTypes(element): if mojom.IsArrayKind(element): return GetAllTypes(element.kind) if mojom.IsMapKind(element): return GetAllTypes(element.key_kind) + GetAllTypes(element.value_kind) if isinstance(element, mojom.Parameter): return GetAllTypes(element.kind) if mojom.IsEnumKind(element): return [element.mojom_name] if not mojom.IsStructKind(element): return [element.spec] if len(element.fields) == 0: return [element.mojom_name] ret = [GetAllTypes(x.kind) for x in element.fields] ret = [x for sublist in ret for x in sublist] return list(set(ret))
def GetNonNullableGoType(kind): if mojom.IsStructKind(kind) or mojom.IsUnionKind(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): return '%s_Pointer' % GetFullName(kind) if mojom.IsInterfaceRequestKind(kind): return '%s_Request' % GetFullName(kind.kind) if mojom.IsEnumKind(kind): return GetNameForNestedElement(kind) return _kind_infos[kind].go_type
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)
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 _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): unquantified = "%s.PendingRemote" % self._GetProtoNameForKind(kind.kind) elif mojom.IsPendingReceiverKind(kind): unquantified = "%s.PendingReceiver" % self._GetProtoNameForKind(kind.kind) elif mojom.IsPendingAssociatedRemoteKind(kind): unquantified = ("%s.PendingAssociatedRemote" % self._GetProtoNameForKind(kind.kind)) elif mojom.IsPendingAssociatedReceiverKind(kind): unquantified = ("%s.PendingAssociatedReceiver" % self._GetProtoNameForKind(kind.kind)) 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 _LiteClosureParamType(self, kind): if mojom.IsEnumKind(kind): return "number" prefix = "" if mojom.IsNullableKind(kind) else "!" if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): return prefix + "Object" if mojom.IsArrayKind(kind): return prefix + ("Array<%s>" % self._LiteClosureParamType(kind.kind)) if mojom.IsMapKind(kind): return "%sMap<%s, %s>|%sObject<%s, %s>" % ( prefix, self._LiteClosureParamType(kind.key_kind), self._LiteClosureParamType(kind.value_kind), prefix, self._LiteClosureParamType(kind.key_kind), self._LiteClosureParamType(kind.value_kind)) return prefix + self._LiteClosureType(kind)
def _LiteJavaScriptType(self, kind): if self._IsPrimitiveKind(kind): return _kind_to_lite_js_type[kind] if mojom.IsArrayKind(kind): return "mojo.internal.Array(%s, %s)" % (self._LiteJavaScriptType( kind.kind), "true" if mojom.IsNullableKind(kind.kind) else "false") if mojom.IsMapKind(kind): return "mojo.internal.Map(%s, %s, %s)" % (self._LiteJavaScriptType( kind.key_kind), self._LiteJavaScriptType( kind.value_kind), "true" if mojom.IsNullableKind( kind.value_kind) else "false") if (mojom.IsAssociatedKind(kind) or mojom.IsInterfaceRequestKind(kind) or mojom.IsPendingRemoteKind(kind) or mojom.IsPendingReceiverKind(kind)): named_kind = kind.kind else: named_kind = kind name = [] if named_kind.module: name.append(named_kind.module.namespace) if named_kind.parent_kind: parent_name = named_kind.parent_kind.name if mojom.IsStructKind(named_kind.parent_kind): parent_name += "Spec" name.append(parent_name) name.append(named_kind.name) name = ".".join(name) if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind) or mojom.IsEnumKind(kind)): return "%sSpec.$" % name if mojom.IsInterfaceKind(kind) or mojom.IsPendingRemoteKind(kind): return "mojo.internal.InterfaceProxy(%sProxy)" % name if mojom.IsInterfaceRequestKind(kind) or mojom.IsPendingReceiverKind( kind): return "mojo.internal.InterfaceRequest(%sRequest)" % name if mojom.IsAssociatedInterfaceKind(kind): # TODO(rockot): Implement associated interfaces. return "mojo.internal.AssociatedInterfaceProxy(%sProxy)" % (name) if mojom.IsAssociatedInterfaceRequestKind(kind): return "mojo.internal.AssociatedInterfaceRequest(%s)" % name return name
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): 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)
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]
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"
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]
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" return _kind_to_cpp_type[kind]
def _GetObjCPropertyDefaultValue(self, field): kind = field.kind if mojom.IsNullableKind(kind) and not field.default: return 'nil' if self._IsObjCNumberKind(kind): return '0' if not field.default else field.default if mojom.IsEnumKind(kind): value = '0' if not field.default else field.default.field.value return 'static_cast<%s%s>(%s)' % (self.class_prefix, kind.name, value) if mojom.IsStringKind(kind): return '@""' if not field.default else '@%s' % field.default if mojom.IsArrayKind(kind): return '@[]' if mojom.IsMapKind(kind): return '@{}' if kind in self.module.structs: return "[[%s%s alloc] init]" % (self.class_prefix, kind.name) raise Exception("Unrecognized kind %s" % kind.spec)
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 _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 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]
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 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"
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 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 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)
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 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): return ("bindings.InterfaceHandle<%s>" % GetDartType(kind)) if mojom.IsInterfaceRequestKind(kind): return ("bindings.InterfaceRequest<%s>" % GetDartType(kind.kind)) if mojom.IsEnumKind(kind): return GetDartType(kind)
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 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"
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)