def _GetCppFieldType(self, kind): if mojom.IsStructKind(kind): return ("mojo::internal::Pointer<%s>" % self._GetNameForKind(kind, internal=True)) if mojom.IsUnionKind(kind): return "%s" % self._GetNameForKind(kind, internal=True) if mojom.IsArrayKind(kind): return ("mojo::internal::Pointer<mojo::internal::Array_Data<%s>>" % self._GetCppFieldType(kind.kind)) if mojom.IsMapKind(kind): return ("mojo::internal::Pointer<mojo::internal::Map_Data<%s, %s>>" % (self._GetCppFieldType(kind.key_kind), self._GetCppFieldType(kind.value_kind))) if mojom.IsInterfaceKind(kind) or mojom.IsPendingRemoteKind(kind): return "mojo::internal::Interface_Data" if mojom.IsInterfaceRequestKind(kind) or mojom.IsPendingReceiverKind(kind): return "mojo::internal::Handle_Data" if (mojom.IsAssociatedInterfaceKind(kind) or mojom.IsPendingAssociatedRemoteKind(kind)): return "mojo::internal::AssociatedInterface_Data" if (mojom.IsAssociatedInterfaceRequestKind(kind) or mojom.IsPendingAssociatedReceiverKind(kind)): return "mojo::internal::AssociatedEndpointHandle_Data" if mojom.IsEnumKind(kind): return "int32_t" if mojom.IsStringKind(kind): return "mojo::internal::Pointer<mojo::internal::String_Data>" if mojom.IsAnyHandleKind(kind): return "mojo::internal::Handle_Data" return _kind_to_cpp_type[kind]
def AddKind(kind): if (mojom.IsIntegralKind(kind) or mojom.IsStringKind(kind) or mojom.IsDoubleKind(kind) or mojom.IsFloatKind(kind) or mojom.IsAnyHandleKind(kind) or mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind) or mojom.IsAssociatedKind(kind) or mojom.IsPendingRemoteKind(kind) or mojom.IsPendingReceiverKind(kind)): pass elif mojom.IsArrayKind(kind): AddKind(kind.kind) elif mojom.IsMapKind(kind): AddKind(kind.key_kind) AddKind(kind.value_kind) else: name = self._GetFullMojomNameForKind(kind) if name in seen_types: return seen_types.add(name) typemap = self.typemap.get(name, None) if typemap: used_typemaps.append(typemap) if mojom.IsStructKind(kind) or mojom.IsUnionKind(kind): for field in kind.fields: AddKind(field.kind)
def GetCppFieldType(kind): if mojom.IsStructKind(kind): return ("mojo::internal::Pointer<%s>" % GetNameForKind(kind, internal=True)) if mojom.IsUnionKind(kind): return "%s" % GetNameForKind(kind, internal=True) if mojom.IsArrayKind(kind): return ("mojo::internal::Pointer<mojo::internal::Array_Data<%s>>" % GetCppType(kind.kind)) if mojom.IsMapKind(kind): return ("mojo::internal::Pointer<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::internal::Handle_Data" if mojom.IsAssociatedInterfaceKind(kind): return "mojo::internal::AssociatedInterface_Data" if mojom.IsAssociatedInterfaceRequestKind(kind): return "mojo::internal::AssociatedInterfaceRequest_Data" if mojom.IsEnumKind(kind): return "int32_t" if mojom.IsStringKind(kind): return "mojo::internal::Pointer<mojo::internal::String_Data>" if mojom.IsAnyHandleKind(kind): return "mojo::internal::Handle_Data" return _kind_to_cpp_type[kind]
def Check(kind): if kind.spec in checked: return True checked.add(kind.spec) if mojom.IsNullableKind(kind): return False elif mojom.IsStructKind(kind): if (IsTypemappedKind(kind) and not _current_typemap[ GetFullMojomNameForKind(kind)]["hashable"]): return False return all(Check(field.kind) for field in kind.fields) elif mojom.IsEnumKind(kind): return not IsTypemappedKind(kind) or _current_typemap[ GetFullMojomNameForKind(kind)]["hashable"] elif mojom.IsUnionKind(kind): return all(Check(field.kind) for field in kind.fields) elif mojom.IsAnyHandleKind(kind): return False elif mojom.IsAnyInterfaceKind(kind): return False # TODO(tibell): Arrays and maps could be made hashable. We just don't have a # use case yet. elif mojom.IsArrayKind(kind): return False elif mojom.IsMapKind(kind): return False else: return True
def GetMojomTypeValue(kind, typepkg=''): if not kind in _kind_infos: return '' nullable = 'true' if mojom.IsNullableKind(kind) else 'false' if kind == mojom.BOOL or kind == mojom.FLOAT or kind == mojom.DOUBLE or \ mojom.IsIntegralKind(kind): kind_name = UpperCamelCase(_kind_infos[kind].decode_suffix.upper()) if kind == mojom.FLOAT: kind_name = "Float" elif kind == mojom.DOUBLE: kind_name = "Double" return '%sTypeSimpleType{%sSimpleType_%s}' % (typepkg, typepkg, kind_name) elif mojom.IsAnyHandleKind(kind): kind_name = 'Unspecified' if kind == mojom.DCPIPE: kind_name = 'DataPipeConsumer' elif kind == mojom.DPPIPE: kind_name = 'DataPipeProducer' elif kind == mojom.MSGPIPE: kind_name = 'MessagePipe' elif kind == mojom.SHAREDBUFFER: kind_name = 'SharedBuffer' return '%sTypeHandleType{%sHandleType{' \ 'Nullable: %s, Kind: %sHandleType_Kind_%s}}' % \ (typepkg, typepkg, nullable, typepkg, kind_name) elif mojom.IsStringKind(kind): return '%sTypeStringType{%sStringType{%s}}' % (typepkg, typepkg, nullable) else: raise Exception('Missing case for kind: %s' % kind)
def IsBasicKind(kind): return (mojom.IsIntegralKind(kind) or mojom.IsStringKind(kind) or mojom.IsDoubleKind(kind) or mojom.IsFloatKind(kind) or mojom.IsAnyHandleKind(kind) or mojom.IsInterfaceKind(kind) or mojom.IsInterfaceRequestKind(kind) or mojom.IsAssociatedKind(kind))
def AddImport(module, element): if not isinstance(element, mojom.Kind): return if mojom.IsArrayKind(element) or mojom.IsInterfaceRequestKind(element): AddImport(module, element.kind) return if mojom.IsMapKind(element): AddImport(module, element.key_kind) AddImport(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(): name += '_' _imports[path] = name
def AddKind(kind): if (mojom.IsIntegralKind(kind) or mojom.IsStringKind(kind) or mojom.IsDoubleKind(kind) or mojom.IsFloatKind(kind)): pass elif (mojom.IsAnyHandleKind(kind)): self.needs_mojolpm_proto = True elif mojom.IsArrayKind(kind): AddKind(kind.kind) elif mojom.IsMapKind(kind): AddKind(kind.key_kind) AddKind(kind.value_kind) elif (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind) or mojom.IsEnumKind(kind) or mojom.IsInterfaceKind(kind)): name = self._GetFullMojomNameForKind(kind) if name in seen_types: return seen_types.add(name) if kind.module in all_imports: seen_imports.add(kind.module) elif (mojom.IsInterfaceRequestKind(kind) or mojom.IsAssociatedInterfaceKind(kind) or mojom.IsAssociatedInterfaceRequestKind(kind) or mojom.IsPendingRemoteKind(kind) or mojom.IsPendingReceiverKind(kind) or mojom.IsPendingAssociatedRemoteKind(kind) or mojom.IsPendingAssociatedReceiverKind(kind)): AddKind(kind.kind)
def Check(kind): if kind.spec in checked: return True checked.add(kind.spec) if mojom.IsNullableKind(kind): return False elif mojom.IsStructKind(kind): if kind.native_only: return False if (self._IsTypemappedKind(kind) and not self.typemap[self._GetFullMojomNameForKind(kind)]["hashable"]): return False return all(Check(field.kind) for field in kind.fields) elif mojom.IsEnumKind(kind): return not self._IsTypemappedKind(kind) or self.typemap[ self._GetFullMojomNameForKind(kind)]["hashable"] elif mojom.IsUnionKind(kind): return all(Check(field.kind) for field in kind.fields) elif mojom.IsAnyHandleKind(kind): return False elif mojom.IsAnyInterfaceKind(kind): return False # TODO(crbug.com/735301): Arrays and maps could be made hashable. We just # don't have a use case yet. elif mojom.IsArrayKind(kind): return False elif mojom.IsMapKind(kind): return False else: return True
def GetUnionGetterReturnType(kind): if (mojom.IsStructKind(kind) or mojom.IsUnionKind(kind) or mojom.IsArrayKind(kind) or mojom.IsMapKind(kind) or mojom.IsAnyHandleKind(kind) or mojom.IsInterfaceKind(kind) or mojom.IsAssociatedKind(kind)): return "%s&" % GetCppWrapperType(kind) return GetCppResultWrapperType(kind)
def GetCppUnionFieldType(kind): if mojom.IsAnyHandleKind(kind): return "MojoHandle" if mojom.IsEnumKind(kind): return "int32_t" if mojom.IsUnionKind(kind): return ("mojo::internal::UnionPointer<%s_Data>" % GetNameForKind(kind, internal=True)) return GetCppFieldType(kind)
def GetCppUnionFieldType(kind): if mojom.IsAnyHandleKind(kind): return "MojoHandle" if mojom.IsInterfaceKind(kind): return "uint64_t" if mojom.IsUnionKind(kind): return ("mojo::internal::UnionPointer<%s>" % GetNameForKind(kind, internal=True)) return GetCppFieldType(kind)
def GetCppUnionFieldType(kind): if mojom.IsAnyHandleKind(kind): return "fidl::internal::WrappedHandle" if mojom.IsInterfaceKind(kind): return "uint64_t" if mojom.IsEnumKind(kind): return "int32_t" if mojom.IsUnionKind(kind): return ("::fidl::internal::UnionPointer<%s_Data>" % GetNameForKind(kind, internal=True)) return GetCppFieldType(kind)
def AddImport(module, element): if not hasattr(element, 'imported_from') or not element.imported_from: return if isinstance(element, mojom.Kind) and mojom.IsAnyHandleKind(element): return imported = element.imported_from if imported['namespace'] == module.namespace: return path = 'gen/mojom' name = 'mojom' if imported['namespace']: path = '/'.join([path] + imported['namespace'].split('.')) name = '_'.join([name] + imported['namespace'].split('.')) while (name in _imports.values() and _imports[path] != path): name += '_' _imports[path] = name
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 IsHandleField(field): return mojom.IsAnyHandleKind(field.kind)
def IsStructWithHandles(struct): for pf in struct.packed.packed_fields: if mojom.IsAnyHandleKind(pf.field.kind): return True return False