示例#1
0
    def EnumValueFromKey(self, enum_type_key, enum_value_index):
        """Takes an enum type key and an enum value index (together these
    form a key to a mojom_enum_value) and returns a module.EnumValue referring
    the module equivalent enum value

    Args:
      enum_type_key: {str} the type key of a mojom_enum
      enum_value_index: {int} the 0-based index into the |values| array of
        the mojom_enum

    Returns:
      {module.EnumValue} translated from mojom_enum_value.
    """
        enum_value_key = (enum_type_key, enum_value_index)
        if enum_value_key in self._value_cache:
            return self._value_cache[enum_value_key]

        mojom_enum = self._graph.resolved_types[enum_type_key].enum_type
        mojom_enum_value = mojom_enum.values[enum_value_index]

        # We need to create and cache the EnumValue object just in case later calls
        # require the creation of that same EnumValue object.
        enum_value = module.EnumValue()
        self._value_cache[enum_value_key] = enum_value

        enum = self.UserDefinedFromTypeKey(enum_type_key)
        enum_value.enum = enum
        self.PopulateModuleOrImportedFrom(enum_value, mojom_enum_value)
        enum_value.namespace = enum_value.module.namespace
        enum_value.parent_kind = enum.parent_kind
        enum_value.name = mojom_enum_value.decl_data.short_name

        return enum_value
示例#2
0
def _EnumField(module, enum, parsed_field, parent_kind):
    """
  Args:
    module: {mojom.Module} Module currently being constructed.
    enum: {mojom.Enum} Enum this field belongs to.
    parsed_field: {ast.EnumValue} Parsed enum value.
    parent_kind: {mojom.Kind} The enclosing type.

  Returns:
    {mojom.EnumField} AST enum field.
  """
    field = mojom.EnumField()
    field.mojom_name = parsed_field.mojom_name
    # TODO(mpcomplete): FixupExpression should be done in the second pass,
    # so constants and enums can refer to each other.
    # TODO(mpcomplete): But then, what if constants are initialized to an enum? Or
    # vice versa?
    if parent_kind:
        field.value = _FixupExpression(
            module, parsed_field.value,
            (module.mojom_namespace, parent_kind.mojom_name), enum)
    else:
        field.value = _FixupExpression(module, parsed_field.value,
                                       (module.mojom_namespace, ), enum)
    field.attributes = _AttributeListToDict(parsed_field.attribute_list)
    value = mojom.EnumValue(module, enum, field)
    module.values[value.GetSpec()] = value
    return field
    def EnumValueFromMojom(self, mojom_enum_value):
        """Translates an mojom_types_mojom.EnumValue to a module.EnumValue.

    mojom_enum_value: {mojom_types_mojom.EnumValue} to be translated.

    Returns:
      {module.EnumValue} translated from mojom_enum_value.
    """
        enum_type_key = mojom_enum_value.enum_type_key
        name = mojom_enum_value.decl_data.short_name
        value_key = (enum_type_key, name)
        if value_key in self._value_cache:
            return self._value_cache[value_key]

        # We need to create and cache the EnumValue object just in case later calls
        # require the creation of that same EnumValue object.
        enum_value = module.EnumValue()
        self._value_cache[value_key] = enum_value

        enum = self.UserDefinedFromTypeKey(enum_type_key)
        enum_value.enum = enum
        self.PopulateModuleOrImportedFrom(enum_value, mojom_enum_value)
        enum_value.namespace = enum_value.module.namespace
        enum_value.parent_kind = enum.parent_kind
        enum_value.name = name

        return enum_value
示例#4
0
def EnumFieldFromData(module, enum, data, parent_kind):
  field = mojom.EnumField()
  field.name = data['name']
  # TODO(mpcomplete): FixupExpression should be done in the second pass,
  # so constants and enums can refer to each other.
  # TODO(mpcomplete): But then, what if constants are initialized to an enum? Or
  # vice versa?
  if parent_kind:
    field.value = FixupExpression(
        module, data['value'], (module.namespace, parent_kind.name))
  else:
    field.value = FixupExpression(
        module, data['value'], (module.namespace, ))
  value = mojom.EnumValue(module, enum, field)
  module.values[value.GetSpec()] = value
  return field