class Xdrgen::AST::Definitions::Union

Public Instance Methods

discriminant_type() click to toggle source
# File lib/xdrgen/ast/definitions/union.rb, line 15
        def discriminant_type
  return nil unless discriminant.type.is_a?(Identifier)

  root.find_definition discriminant.type.name
end
nested_definitions() click to toggle source
# File lib/xdrgen/ast/definitions/union.rb, line 46
def nested_definitions
  arms.
    map(&:declaration).
    reject{|d| d.is_a?(Declarations::Void)}.
    map(&:type).
    select{|d| d.is_a?(Concerns::NestedDefinition)}
end
resolved_case(kase) click to toggle source
# File lib/xdrgen/ast/definitions/union.rb, line 21
def resolved_case(kase)
  if discriminant_type.nil? then
    # discriminant_type has not been found we need to search for the value in namespace's enum constants.
    # It's a case where union discriminant is a standard type (like `int`):
    #
    # enum StellarValueType
    # {
    #     STELLAR_VALUE_BASIC = 0,
    #     STELLAR_VALUE_SIGNED = 1
    # };
    #
    # union switch (int v)
    # {
    # case STELLAR_VALUE_BASIC:
    #     void;
    #     ...
    found = namespace.find_enum_value(kase.value_s)
    raise "Case error:  #{kase} (#{kase.value_s}) constant not found" if found.nil?
  else
    found = discriminant_type.members.find{|m| m.name == kase.value_s}
    raise "Case error:  #{kase} is not a member of #{discriminant_type.name}" if found.nil?
  end
  found
end