Class: IDL::AST::Union

Inherits:
Node show all
Defined in:
lib/ridl/node.rb,
lib/ridl/node.rb

Overview

Member

Constant Summary collapse

NAMETYPE =
:class
DEFINABLE =
[IDL::AST::UnionMember, IDL::AST::Struct, IDL::AST::Union, IDL::AST::Enum, IDL::AST::Enumerator]

Instance Attribute Summary collapse

Attributes inherited from Leaf

#annotations, #enclosure, #intern, #lm_name, #name, #prefix, #scopes

Instance Method Summary collapse

Methods inherited from Node

#define, #introduce, #is_definable?, #match_members, #redefine, #replace_prefix, #resolve, #undo_introduction

Methods inherited from Leaf

#_set_prefix, #has_annotations?, #is_template?, #lm_name_for_scope, #parsed_name_scope, #replace_prefix, #repo_scopes, #repository_id, #resolve, #scoped_lm_name, #scoped_name, #set_repo_id, #set_repo_version, #typename

Constructor Details

#initialize(_name, _enclosure, params) ⇒ Union



2587
2588
2589
2590
2591
2592
2593
2594
# File 'lib/ridl/node.rb', line 2587

def initialize(_name, _enclosure, params)
  @defined = false
  @recursive = false
  @forward = params[:forward] ? true : false
  @switchtype = nil
  super(_name, _enclosure)
  @idltype = IDL::Type::Union.new(self)
end

Instance Attribute Details

#idltypeObject (readonly)

Returns the value of attribute idltype.



2585
2586
2587
# File 'lib/ridl/node.rb', line 2585

def idltype
  @idltype
end

#switchtypeObject

Returns the value of attribute switchtype.



2586
2587
2588
# File 'lib/ridl/node.rb', line 2586

def switchtype
  @switchtype
end

Instance Method Details

#default_labelObject



2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
# File 'lib/ridl/node.rb', line 2627

def default_label
  swtype = @switchtype.resolved_type
  lbls = members.collect { |m| m.labels.include?(:default) ? [] : m.labels.collect { |l| l.value } }.flatten
  lbls = lbls.sort unless IDL::Type::Boolean === swtype ## work around bug in Ruby 1.9.2
  def_lbl = swtype.min
  while swtype.in_range?(def_lbl)
    return IDL::Expression::Value.new(@switchtype, def_lbl) unless lbls.include?(def_lbl)
    return nil if def_lbl == swtype.max
    def_lbl = swtype.next(def_lbl)
  end
  nil
end

#defined=(f) ⇒ Object



2601
# File 'lib/ridl/node.rb', line 2601

def defined=(f); @defined = f; end

#has_default?Boolean



2623
2624
2625
# File 'lib/ridl/node.rb', line 2623

def has_default?
  members.any? { |m| m.labels.include?(:default) }
end

#instantiate(_context, _enclosure) ⇒ Object



2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
# File 'lib/ridl/node.rb', line 2685

def instantiate(_context, _enclosure)
  _params = {
    :forward => @forward
  }
  _u = super(_context, _enclosure, _params)
  _u.set_switchtype(@switchtype.instantiate(_context))
  _u.validate_labels
  _u.defined = self.is_defined?
  _u
end

#is_defined?Boolean



2600
# File 'lib/ridl/node.rb', line 2600

def is_defined?; @defined; end

#is_forward?Boolean



2602
# File 'lib/ridl/node.rb', line 2602

def is_forward?; @forward; end

#is_local?(recurstk = []) ⇒ Boolean



2614
2615
2616
2617
2618
2619
2620
2621
# File 'lib/ridl/node.rb', line 2614

def is_local?(recurstk = [])
  # not local if forward decl or recursion detected
  return false if is_forward? || recurstk.include?(self)
  recurstk.push self   # track root node to detect recursion
  ret = members.any? { |m| m.is_local?(recurstk) }
  recurstk.pop
  ret
end

#is_recursive?Boolean



2603
# File 'lib/ridl/node.rb', line 2603

def is_recursive?; @recursive end

#marshal_dumpObject



2672
2673
2674
# File 'lib/ridl/node.rb', line 2672

def marshal_dump
  super() << @defined << @recursive << @forward << @idltype << @switchtype
end

#marshal_load(vars) ⇒ Object



2676
2677
2678
2679
2680
2681
2682
2683
# File 'lib/ridl/node.rb', line 2676

def marshal_load(vars)
  @switchtype = vars.pop
  @idltype = vars.pop
  @forward = vars.pop
  @recursive = vars.pop
  @defined = vars.pop
  super(vars)
end

#membersObject



2610
2611
2612
# File 'lib/ridl/node.rb', line 2610

def members
  @children.find_all { |c| c.is_a? IDL::AST::UnionMember }
end

#recursive=(f) ⇒ Object



2604
# File 'lib/ridl/node.rb', line 2604

def recursive=(f); @recursive = f end

#set_switchtype(_switchtype) ⇒ Object



2596
2597
2598
# File 'lib/ridl/node.rb', line 2596

def set_switchtype(_switchtype)
  @switchtype = _switchtype
end

#validate_labelsObject



2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
# File 'lib/ridl/node.rb', line 2640

def validate_labels
  return if self.is_template?
  labelvals = []
  default_ = false
  members.each { |m|
    ## check union case labels for validity
    m.labels.each { |lbl|
      if lbl == :default
        raise RuntimeError,
              "duplicate case label 'default' for #{typename} #{lm_name}" if default_
        default_ = true
      else
        # correct type
        lv = @switchtype.resolved_type.narrow(lbl.value)
        # doubles
        if labelvals.include? lv
          raise RuntimeError,
                "duplicate case label #{lv.to_s} for #{typename} #{lm_name}"
        end
        labelvals << lv
      end
    }
  }
  ## check if default allowed if defined
  if default_
    if @switchtype.resolved_type.range_length == labelvals.size
      raise RuntimeError,
            "'default' case label superfluous for #{typename} #{lm_name}"
    end
  end
end

#walk_members(&block) ⇒ Object



2606
2607
2608
# File 'lib/ridl/node.rb', line 2606

def walk_members(&block)
  @children.each { |m| yield(m) if not m.is_a? IDL::AST::UnionMember }
end