def ShouldInlineStruct(struct): # TODO(darin): Base this on the size of the wrapper class. if len(struct.fields) > 4: return False for field in struct.fields: if mojom.IsReferenceKind(field.kind) and not mojom.IsStringKind(field.kind): return False return True
def AppendEncodeParams(initial_params, kind, bit): """ Appends standard parameters shared between encode and decode calls. """ params = list(initial_params) if (kind == mojom.BOOL): params.append(str(bit)) if mojom.IsReferenceKind(kind): if mojom.IsArrayKind(kind): params.append(GetArrayNullabilityFlags(kind)) else: params.append(GetDartTrueFalse(mojom.IsNullableKind(kind))) if mojom.IsArrayKind(kind): params.append(GetArrayExpectedLength(kind)) return params
def GetUnionTraitGetterReturnType(kind): """Get field type used in UnionTraits template specialization. The type may be qualified as UnionTraits specializations live outside the namespace where e.g. structs are defined. Args: kind: {Kind} The type of the field. Returns: {str} The C++ type to use for the field. """ if mojom.IsReferenceKind(kind): return "%s&" % GetCppWrapperType(kind, add_same_module_namespaces=True) return GetCppWrapperType(kind, add_same_module_namespaces=True)
def AppendEncodeDecodeParams(initial_params, context, kind, bit): """ Appends standard parameters shared between encode and decode calls. """ params = list(initial_params) if (kind == mojom.BOOL): params.append(str(bit)) if mojom.IsReferenceKind(kind): if mojom.IsArrayKind(kind): params.append(GetArrayNullabilityFlags(kind)) else: params.append(GetJavaTrueFalse(mojom.IsNullableKind(kind))) if mojom.IsArrayKind(kind): params.append(GetArrayExpectedLength(kind)) if mojom.IsInterfaceKind(kind): params.append('%s.MANAGER' % GetJavaType(context, kind)) if mojom.IsArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): params.append('%s.MANAGER' % GetJavaType(context, kind.kind)) return params
def GetNameForElement(element): # structs if (mojom.IsEnumKind(element) or mojom.IsInterfaceKind(element) or mojom.IsStructKind(element)): return element.mojom_name # vectors if (mojom.IsArrayKind(element)): elem_name = GetFullNameForElement(element.kind) return f'std::vector<{elem_name}>' # maps if (mojom.IsMapKind(element)): key_name = GetFullNameForElement(element.key_kind) value_name = GetFullNameForElement(element.value_kind) return f'std::map<{key_name}, {value_name}>' # struct fields and function parameters if isinstance(element, (mojom.Field, mojom.Method, mojom.Parameter)): # maps and vectors if (mojom.IsArrayKind(element.kind) or mojom.IsMapKind(element.kind)): return GetNameForElement(element.kind) # strings if (mojom.IsReferenceKind(element.kind) and element.kind.spec == 's'): return 'std::string' # PODs if element.kind in _kind_to_cpp_type: return _kind_to_cpp_type[element.kind] # structs and enums return element.kind.mojom_name # PODs that are members of vectors/maps if (hasattr(element, '__hash__') and element in _kind_to_cpp_type): return _kind_to_cpp_type[element] if (hasattr(element, 'spec')): # strings that are members of vectors/maps if (element.spec == 's'): return 'std::string' # structs that aren't defined in mojom that are members of vectors/maps if (element.spec[0] == 'x'): return element.spec.replace('x:', '').replace('.', '::') if (mojom.IsInterfaceRequestKind(element) or mojom.IsAssociatedKind(element) or mojom.IsPendingRemoteKind(element) or mojom.IsPendingReceiverKind(element) or mojom.IsUnionKind(element)): raise Exception('Unsupported element: %s' % element) raise Exception('Unexpected element: %s' % element)
def AppendDecodeParams(initial_params, kind, bit): """ Appends standard parameters for decode calls. """ params = list(initial_params) if (kind == mojom.BOOL): params.append(str(bit)) if mojom.IsReferenceKind(kind): if mojom.IsArrayKind(kind): params.append(GetArrayNullabilityFlags(kind)) else: params.append(GetDartTrueFalse(mojom.IsNullableKind(kind))) if mojom.IsInterfaceKind(kind): params.append('%sProxy.newFromEndpoint' % GetDartType(kind)) if mojom.IsArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): params.append('%sProxy.newFromEndpoint' % GetDartType(kind.kind)) if mojom.IsInterfaceRequestKind(kind): params.append('%sStub.newFromEndpoint' % GetDartType(kind.kind)) if mojom.IsArrayKind(kind) and mojom.IsInterfaceRequestKind(kind.kind): params.append('%sStub.newFromEndpoint' % GetDartType(kind.kind.kind)) if mojom.IsArrayKind(kind): params.append(GetArrayExpectedLength(kind)) return params
def AppendEncodeDecodeParams(initial_params, context, kind, bit): """ Appends standard parameters shared between encode and decode calls. """ params = list(initial_params) if (kind == mojom.BOOL): params.append(str(bit)) if mojom.IsReferenceKind(kind): if mojom.IsAnyArrayKind(kind): params.append(GetArrayNullabilityFlags(kind)) else: params.append(GetJavaTrueFalse(mojom.IsNullableKind(kind))) if mojom.IsAnyArrayKind(kind): if mojom.IsFixedArrayKind(kind): params.append(str(kind.length)) else: params.append( 'org.chromium.mojo.bindings.BindingsHelper.UNSPECIFIED_ARRAY_LENGTH' ) if mojom.IsInterfaceKind(kind): params.append('%s.MANAGER' % GetJavaType(context, kind)) if mojom.IsAnyArrayKind(kind) and mojom.IsInterfaceKind(kind.kind): params.append('%s.MANAGER' % GetJavaType(context, kind.kind)) return params
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 _GetUnionGetterReturnType(self, kind): if mojom.IsReferenceKind(kind): return "%s&" % self._GetCppWrapperType(kind) return self._GetCppWrapperType(kind)
def _ShouldPassParamByValue(self, kind): return ((not mojom.IsReferenceKind(kind)) or self._IsMoveOnlyKind(kind) or self._IsCopyablePassByValue(kind))
def ShouldInlineUnion(union): return not any( mojom.IsReferenceKind(field.kind) and not mojom.IsStringKind(field.kind) for field in union.fields)
def RequiresContextForDataView(kind): for field in kind.fields: if mojom.IsReferenceKind(field.kind): return True return False
def GetUnionGetterReturnType(kind): if mojom.IsReferenceKind(kind): return "%s&" % GetCppWrapperType(kind) return GetCppWrapperType(kind)
def __init__(self, struct): self.struct = struct # |packed_fields| contains all the fields, in increasing offset order. self.packed_fields = [] # |packed_fields_in_ordinal_order| refers to the same fields as # |packed_fields|, but in ordinal order. self.packed_fields_in_ordinal_order = [] # No fields. if (len(struct.fields) == 0): return # Start by sorting by ordinal. src_fields = self.packed_fields_in_ordinal_order ordinal = 0 for index, field in enumerate(struct.fields): if field.ordinal is not None: ordinal = field.ordinal src_fields.append(PackedField(field, index, ordinal)) ordinal += 1 src_fields.sort(key=lambda field: field.ordinal) # Set |min_version| for each field. next_min_version = 0 for packed_field in src_fields: if packed_field.field.min_version is None: assert next_min_version == 0 else: assert packed_field.field.min_version >= next_min_version next_min_version = packed_field.field.min_version packed_field.min_version = next_min_version if (packed_field.min_version != 0 and mojom.IsReferenceKind(packed_field.field.kind) and not packed_field.field.kind.is_nullable): raise Exception("Non-nullable fields are only allowed in version 0 of " "a struct. %s.%s is defined with [MinVersion=%d]." % (self.struct.name, packed_field.field.name, packed_field.min_version)) src_field = src_fields[0] src_field.offset = 0 src_field.bit = 0 dst_fields = self.packed_fields dst_fields.append(src_field) # Then find first slot that each field will fit. for src_field in src_fields[1:]: last_field = dst_fields[0] for i in range(1, len(dst_fields)): next_field = dst_fields[i] offset, bit = GetFieldOffset(src_field, last_field) if offset + src_field.size <= next_field.offset: # Found hole. src_field.offset = offset src_field.bit = bit dst_fields.insert(i, src_field) break last_field = next_field if src_field.offset is None: # Add to end src_field.offset, src_field.bit = GetFieldOffset(src_field, last_field) dst_fields.append(src_field)
def ShouldPassParamByValue(kind): return (not mojom.IsReferenceKind(kind)) or IsMoveOnlyKind(kind)