Class: IDL::AST::TemplateModule

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

Constant Summary collapse

DEFINABLE =
[
  IDL::AST::Include, IDL::AST::Module, IDL::AST::Interface, IDL::AST::Valuebox, IDL::AST::Valuetype,
  IDL::AST::Const, IDL::AST::Struct, IDL::AST::Union, IDL::AST::Enum, IDL::AST::Enumerator, IDL::AST::Typedef,
  IDL::AST::Home, IDL::AST::Porttype, IDL::AST::Component, IDL::AST::Connector,
  IDL::AST::TemplateParam, IDL::AST::TemplateModuleReference
]

Instance Attribute Summary collapse

Attributes inherited from Module

#anchor, #next, #template, #template_params

Attributes inherited from Leaf

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

Instance Method Summary collapse

Methods inherited from Module

#annotations, #has_anchor?, #is_templated?, #marshal_dump, #marshal_load, #redefine, #replace_prefix, #template_param, #undo_introduction

Methods inherited from Node

#introduce, #is_definable?, #marshal_dump, #marshal_load, #match_members, #redefine, #replace_prefix, #resolve, #select_members, #undo_introduction, #walk_members

Methods inherited from Leaf

#has_annotations?, #is_local?, #lm_name, #lm_scopes, #marshal_dump, #marshal_load, #replace_prefix, #repository_id, #resolve, #scoped_lm_name, #scoped_name, #set_repo_id, #set_repo_version, #typename, #unescaped_name

Constructor Details

#initialize(_name, _enclosure, _params) ⇒ TemplateModule

Returns a new instance of TemplateModule.



706
707
708
709
710
# File 'lib/ridl/node.rb', line 706

def initialize(_name, _enclosure, _params)
  super(_name, _enclosure, {})
  @idltype = IDL::Type::TemplateModule.new(self)
  @template_params = []
end

Instance Attribute Details

#idltypeObject (readonly)

Returns the value of attribute idltype.



704
705
706
# File 'lib/ridl/node.rb', line 704

def idltype
  @idltype
end

Instance Method Details

#define(*args) ⇒ Object



712
713
714
715
716
# File 'lib/ridl/node.rb', line 712

def define(*args)
  child = super(*args)
  @template_params << child if child.is_a?(IDL::AST::TemplateParam)
  child
end

#instantiate(_module_instance, instantiation_context = {}) ⇒ Object



726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'lib/ridl/node.rb', line 726

def instantiate(_module_instance, instantiation_context = {})
  # process concrete parameters
  @template_params.each_with_index do |_tp, _ix|
    raise "missing template parameter for #{typename} #{scoped_lm_name}: #{_tp.name}" unless _ix < _module_instance.template_params.size

    _cp = _module_instance.template_params[_ix]
    if _cp.is_a?(IDL::Type)
      raise "anonymous type definitions are not allowed!" if _cp.is_anonymous?
      # parameter should be a matching IDL::Type
      unless _tp.idltype.is_a?(IDL::Type::Any) || _tp.idltype.class === _cp.resolved_type
        raise "mismatched instantiation parameter \##{_ix} #{_cp.typename} for #{typename} #{scoped_lm_name}: expected #{_tp.idltype.typename} for #{_tp.name}"
      end

      # verify concrete parameter
      case _tp.idltype
        when IDL::Type::Any # 'typename'
          # no further checks
        when IDL::Type::Interface, # 'interface'
             IDL::Type::Eventtype, # 'eventtype'
             IDL::Type::Valuetype, # 'valuetype'
             IDL::Type::Struct, # 'struct'
             IDL::Type::Union, # 'union'
             IDL::Type::Exception, # 'exception'
             IDL::Type::Enum # 'enum'
             IDL::Type::BitMask# 'bitmask'
             IDL::Type::BitSet# 'bitset'
           # no further checks
        when IDL::Type::Sequence # 'sequence' or 'sequence<...>'
          _tptype = _tp.idltype
          unless _tptype.basetype.is_a?(IDL::Type::Void) # 'sequence'
            # check basetype
            unless _tptype.basetype.is_a?(IDL::Type::ScopedName) &&
                   _tptype.basetype.is_node?(IDL::AST::TemplateParam) &&
                   _tptype.basetype.node.concrete_matches?(_cp.resolved_type.basetype)
              raise "invalid sequence type as instantiation parameter for #{typename} #{scoped_lm_name}: expected #{_tp.idltype.typename} for #{_tp.name}"
            end
          end
      end
    elsif _cp.is_a?(IDL::Expression)
      # template param should be 'const <const_type>'
      unless _tp.idltype.is_a?(IDL::Type::Const)
        raise "unexpected expression as instantiation parameter for #{typename} #{scoped_lm_name}: expected #{_tp.idltype.typename} for #{_tp.name}"
      end

      # match constant type
      _tp.idltype.narrow(_cp.value)
    else
      raise "invalid instantiation parameter for #{typename} #{scoped_lm_name}: #{_cp.class.name}"
    end
    # if we  get here all is well -> store concrete param (either IDL type or expression)
    _tp.set_concrete_param(_cp)
  end
  # instantiate template by copying template module state to module instance
  _module_instance.copy_from(self, instantiation_context)
end

#is_template?Boolean

Returns:

  • (Boolean)


718
719
720
# File 'lib/ridl/node.rb', line 718

def is_template?
  true
end

#paramsObject



722
723
724
# File 'lib/ridl/node.rb', line 722

def params
  @template_params
end