Class: Xdrgen::AST::Definitions::Union

Inherits:
Base
  • Object
show all
Extended by:
Memoist
Includes:
Concerns::Contained, Concerns::Named, Concerns::Namespace
Defined in:
lib/xdrgen/ast/definitions/union.rb

Direct Known Subclasses

NestedUnion

Instance Method Summary collapse

Methods included from Concerns::Named

#fully_qualified_name, #namespaces

Methods inherited from Base

#sub_type

Instance Method Details

#nested_definitionsObject



46
47
48
49
50
51
52
# 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) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 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