Class: IDL::RubyStubWriter

Inherits:
RubyWriterBase show all
Defined in:
lib/ridlbe/ruby/walker.rb

Instance Method Summary collapse

Methods inherited from RubyWriterBase

#indent, #nest, #print, #printi, #printiln, #println, #visit_nodes

Constructor Details

#initialize(output = STDOUT, params = {}, indent = ' ') ⇒ RubyStubWriter

Returns a new instance of RubyStubWriter.



52
53
54
# File 'lib/ridlbe/ruby/walker.rb', line 52

def initialize(output = STDOUT, params = {}, indent = '  ')
  super
end

Instance Method Details

#declare_interface(node) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/ridlbe/ruby/walker.rb', line 115

def declare_interface(node)
  name = node.rubyname
  if not @params[:class_interfaces].nil? and @params[:class_interfaces].include?(name)
    printiln("class #{name}; end  ## interface forward")
  else
    printiln("module #{name}; end  ## interface forward")
  end
end

#declare_struct(node) ⇒ Object



871
# File 'lib/ridlbe/ruby/walker.rb', line 871

def declare_struct(node); end

#declare_union(node) ⇒ Object



934
# File 'lib/ridlbe/ruby/walker.rb', line 934

def declare_union(node); end

#declare_valuetype(node) ⇒ Object



255
# File 'lib/ridlbe/ruby/walker.rb', line 255

def declare_valuetype(node); end

#enter_exception(node) ⇒ Object



923
924
925
926
927
928
# File 'lib/ridlbe/ruby/walker.rb', line 923

def enter_exception(node)
  println
  name = node.rubyname
  printiln("class #{name} < CORBA::UserException")
  @nest += 1
end

#enter_include(node) ⇒ Object



91
92
93
94
95
# File 'lib/ridlbe/ruby/walker.rb', line 91

def enter_include(node)
  printiln('## include')
  printiln("CORBA.implement('#{node.filename}', {}, CORBA::IDL::CLIENT_STUB) {")
  println
end

#enter_interface(node) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/ridlbe/ruby/walker.rb', line 124

def enter_interface(node)
  println
  name = node.rubyname
  if node.is_pseudo?
    if not @params[:class_interfaces].nil? and @params[:class_interfaces].include?(name)
      printiln("class #{name}  ## pseudo object interface")
    else
      printiln("module #{name}  ## pseudo object interface")
    end
    @nest += 1
  else
    if not @params[:class_interfaces].nil? and @params[:class_interfaces].include?(name)
      printiln("class #{node.rubyname}  ## interface")
    else
      printiln("module #{node.rubyname}  ## interface")
    end
    println
    @nest += 1

    if node.bases.size > 0
      node.bases.each do |n|
        printiln("include #{n.scoped_rubyname}")
      end
      println
    end

    printiln(format("Id = '%s'.freeze", node.repository_id))
    printi('Ids = [ Id')
    if node.is_abstract?
      println(',')
      printi("        'IDL:omg.org/CORBA/AbstractBase:1.0'")
    end
    if node.bases.size > 0
      node.bases.each do |n|
        println(',')
        printi("        #{n.scoped_rubyname}::Id")
      end
    end
    println(' ].freeze')

    println
    unless node.is_abstract?
      printiln(format("def %s._tc; @@tc_%s ||= CORBA::TypeCode::ObjectRef.new(Id, '%s', self); end",
                      node.rubyname, node.rubyname, node.rubyname))
    else
      printiln(format("def %s._tc; @@tc_%s ||= CORBA::TypeCode::AbstractInterface.new(Id, '%s', self); end",
                      node.rubyname, node.rubyname, node.rubyname))
    end
    printiln('self._tc  # register typecode');

    println
    printiln("def #{name}._narrow(obj)")
    nest {
      printiln('return nil if CORBA.is_nil(obj)')
      if node.is_local?
        printiln('return CORBA::Stub.create_stub(obj)._unchecked_narrow!(self)')
      else
        printiln('return CORBA::Stub.create_stub(obj)._narrow!(self)')
      end
    }
    printiln('end')
    println
    printiln("def #{name}._duplicate(obj)")
    nest {
      if node.is_local?
        printiln('obj');
      else
        printiln('return nil if CORBA.is_nil(obj)')
        printiln('return CORBA::Stub.create_stub(super(obj))._narrow!(self)')
      end
    }
    printiln('end')
    println
    printiln('def _interface_repository_id')
    nest {
      printiln("#{node.scoped_rubyname}::Id")
    }
    printiln('end')
  end
  println
end

#enter_module(node) ⇒ Object



103
104
105
106
107
# File 'lib/ridlbe/ruby/walker.rb', line 103

def enter_module(node)
  printiln('module ' + node.rubyname)
  println
  @nest += 1
end

#enter_struct(node) ⇒ Object



873
874
875
876
877
878
# File 'lib/ridlbe/ruby/walker.rb', line 873

def enter_struct(node)
  println
  name = node.rubyname
  printiln("class #{name} < CORBA::Portable::Struct")
  @nest += 1
end

#enter_union(node) ⇒ Object



936
937
938
939
940
941
# File 'lib/ridlbe/ruby/walker.rb', line 936

def enter_union(node)
  println
  name = node.rubyname
  printiln("class #{name} < CORBA::Portable::Union")
  @nest += 1
end

#enter_valuetype(node) ⇒ Object



257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/ridlbe/ruby/walker.rb', line 257

def enter_valuetype(node)
  println
  name = node.rubyname
  unless node.is_abstract?
    printiln("class #{name}")
  else
    printiln("module #{name}")
  end
  @nest += 1

  unless node.is_abstract?
    if node.is_custom?
      printiln('include CORBA::Portable::CustomValueBase')
    else
      printiln('include CORBA::ValueBase')
    end
  else
    printiln('include CORBA::AbstractValueBase')
  end

  unless node.is_abstract?
    trunc_ids = node.truncatable_ids
    println
    printi('TRUNCATABLE_IDS = [')
    print("'#{trunc_ids.shift}'")
    unless trunc_ids.empty?
      nest {
        trunc_ids.each do |trunc_id|
          println(',')
          printi("'#{trunc_id}'")
        end
      }
    end
    println(' ].freeze')
  end

  println
  printiln(format('def %s._tc', node.rubyname))
  nest {
    printi("@@tc_#{node.rubyname} ||= ")
    print_valuetype_typecode(node)
    println
  }
  printiln('end')
  printiln('self._tc  # register typecode')
end

#expression_to_s(exp) ⇒ Object



764
765
766
767
768
769
770
771
772
773
774
775
# File 'lib/ridlbe/ruby/walker.rb', line 764

def expression_to_s(exp)
  case exp
  when Expression::Value
    value_to_s(exp)
  when Expression::Operation
    operation_to_s(exp)
  when Expression::ScopedName, Expression::Enumerator
    exp.node.scoped_rubyname
  else
    raise "unknown expression type: #{exp.class.name}"
  end
end

#get_arg_type(_idl_argtype) ⇒ Object



753
754
755
756
757
758
759
760
761
762
# File 'lib/ridlbe/ruby/walker.rb', line 753

def get_arg_type(_idl_argtype)
  case _idl_argtype
  when IDL::AST::Parameter::IN
    'CORBA::ARG_IN'
  when IDL::AST::Parameter::OUT
    'CORBA::ARG_OUT'
  else
    'CORBA::ARG_INOUT'
  end
end

#get_typecode(_type) ⇒ Object



688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
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
# File 'lib/ridlbe/ruby/walker.rb', line 688

def get_typecode(_type)
  case _type
  when Type::Octet,
       Type::UShort, Type::Short,
       Type::ULong, Type::Long,
       Type::ULongLong, Type::LongLong,
       Type::Boolean, Type::Char, Type::WChar,
       Type::Float, Type::Double, Type::LongDouble,
       Type::Void, Type::Any
    s = _type.class.name.split('::') # IDL::Type::Octet -> [IDL, Type, Octet]
    s = s[s.length - 1]
    s.downcase!
    format('CORBA._tc_%s', s)

  when Type::Object
    'CORBA._tc_Object'

  when Type::String
    if not _type.length.nil?
      format('CORBA::TypeCode::String.new(%d)', _type.length)
    else
      'CORBA._tc_string'
    end

  when Type::WString
    if not _type.length.nil?
      format('CORBA::TypeCode::WString.new(%d)', _type.length)
    else
      'CORBA._tc_wstring'
    end

  when Type::ScopedName
    scoped_type = _type.node.idltype
    if scoped_type.is_a?(IDL::Type::Interface) && scoped_type.node.is_forward?
      node = scoped_type.node
      "(CORBA::TypeCode.typecode_for_id('#{node.repository_id}') || " +
        "CORBA::TypeCode::ObjectRef.new('#{node.repository_id}', '#{node.rubyname}', #{node.scoped_rubyname}))"
    else
      _type.node.scoped_rubyname + '._tc'
    end

  when Type::Array
    sep = ''
    tc = 'CORBA::TypeCode::Array.new(' +
        get_typecode(_type.basetype) + ', '
    _type.sizes.each do |sz|
      tc += "#{sep}#{sz.to_s}"
      sep = ', '
    end
    tc + ')'

  when Type::Sequence
    if _type.is_recursive?
      "CORBA::TypeCode::Sequence.new(CORBA::TypeCode::Recursive.new('#{_type.basetype.resolved_type.node.repository_id}'))"
    else
      'CORBA::TypeCode::Sequence.new(' +
            get_typecode(_type.basetype) +
            if not _type.length.nil? then format(', %d)', _type.length) else ')' end
    end

  else
    raise "invalid type for (un)marshalling: #{_type.typename}"
  end
end

#leave_exception(node) ⇒ Object



930
931
932
# File 'lib/ridlbe/ruby/walker.rb', line 930

def leave_exception(node)
  leave_struct(node)
end

#leave_include(node) ⇒ Object



97
98
99
100
101
# File 'lib/ridlbe/ruby/walker.rb', line 97

def leave_include(node)
  println
  printiln("} ## end of include '#{node.filename}'")
  println
end

#leave_interface(node) ⇒ Object



206
207
208
209
210
211
# File 'lib/ridlbe/ruby/walker.rb', line 206

def leave_interface(node)
  name = node.rubyname
  @nest -= 1
  printiln("end #of interface #{name}")
  println
end

#leave_module(node) ⇒ Object



109
110
111
112
113
# File 'lib/ridlbe/ruby/walker.rb', line 109

def leave_module(node)
  @nest -= 1
  printiln("end #of module #{node.rubyname}")
  println
end

#leave_struct(node) ⇒ Object



880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
# File 'lib/ridlbe/ruby/walker.rb', line 880

def leave_struct(node)
  tc_ = if node.is_a? IDL::AST::Exception then 'Except' else 'Struct' end
  println
  printiln(format('def %s._tc', node.rubyname))
  struct_members_ = node.members
  nest {
    printi(format("@@tc_%s ||= CORBA::TypeCode::%s.new('%s'.freeze, '%s'",
                  node.rubyname, tc_, node.repository_id, node.rubyname))
    unless struct_members_.empty?
      pfx = '   ['
      struct_members_.each do |m|
        println(',')
        printi(pfx)
        pfx = '    '
        print(format("['%s', %s]", m.rubyname, get_typecode(m.idltype)))
      end
      println('], self)')
    else
      println(', self)')
    end
  }
  printiln('end')
  printiln('self._tc  # register typecode');
  struct_members_.each do |m|
    printiln(format('attr_accessor :%s', m.rubyname))
  end

  if struct_members_.size > 0
    printiln('def initialize(*param_)')
    nest {
      struct_members_.each do |m|
        printiln("@#{m.rubyname} = param_.shift")
      end
    }
    printiln('end')
    println
  end

  name = node.rubyname
  @nest -= 1
  printiln("end #of #{if node.is_a? IDL::AST::Exception then "exception" else "struct" end} #{name}")
end

#leave_union(node) ⇒ Object



943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
# File 'lib/ridlbe/ruby/walker.rb', line 943

def leave_union(node)
  println
  printiln(format('def %s._tc', node.rubyname))
  nest {
    printiln(format("@@tc_%s ||= CORBA::TypeCode::Union.new('%s'.freeze, '%s',",
                  node.rubyname, node.repository_id, node.rubyname))
    printi("    #{get_typecode(node.switchtype)}")
    if node.members.size > 0
      pfx = '   ['
      node.members.each do |m|
        m.labels.each do |lbl|
          println(',')
          printi(pfx)
          pfx = '    '
          labeltxt = if lbl == :default then ':default'; else expression_to_s(lbl); end
          print(format("[%s, '%s', %s]", labeltxt, m.rubyname, get_typecode(m.idltype)))
        end
      end
      print('], self')
    else
      print(', self')
    end
    if (!node.has_default?) && node.default_label # has implicit default?
      println(", #{expression_to_s(node.default_label)})")
    else
      println(')')
    end
  }
  printiln('end')
  printiln('self._tc  # register typecode');
  ix = 0
  if node.members.size > 0
    node.members.each do |m|
      printiln(format('def %s; @value; end', m.rubyname))
      printiln(format('def %s=(val); _set_value(%d, val); end', m.rubyname, ix))
      ix += m.labels.size
    end
  end

  if (!node.has_default?) && node.default_label # has implicit default?
    def_label = expression_to_s(node.default_label)
    printiln("def _default; @discriminator = #{def_label}; @value = nil; end")
  end

  name = node.rubyname
  @nest -= 1
  printiln("end #of union #{name}")
end

#leave_valuetype(node) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# File 'lib/ridlbe/ruby/walker.rb', line 304

def leave_valuetype(node)
  println
  printiln('module Intf')
  nest {
    intfs_ = node.interfaces
    unless intfs_.empty?
      printiln('## supported interfaces')
      intfs_.each do |intf|
        printiln("include #{intf.scoped_rubyname}")
      end
      printiln('undef :_interface_repository_id')
    end

    bases_ = Array.new(node.bases)
    if node.has_concrete_base?
      printiln('## concrete base type')
      printiln("include #{bases_.shift.scoped_rubyname}::Intf")
    end
    unless bases_.empty?
      printiln('## abstract base types')
      bases_.each do |base|
        printiln("include #{base.scoped_rubyname}::Intf")
      end
    end

    if node.has_concrete_base? || !bases_.empty? || !intfs_.empty?
      println
    end

    intf_members_ = node.interface_members
    [IDL::AST::Operation, IDL::AST::Attribute].each do |m_type|
      has_type = false
      intf_members_.each do |m|
        if m.is_a?(m_type)
          if m.is_a?(IDL::AST::Operation)
            printi('## operations') unless has_type
            visit_operation(m)
            has_type = true
          else
            printi('## attributes') unless has_type
            visit_attribute(m)
            has_type = true
          end
        end
      end
      println if has_type
    end

    unless node.is_abstract?
      state_members_ = node.state_members
      has_type = false
      state_members_.each do |m|
        if m.visibility == :public
          printiln('## public value state') unless has_type
          printiln(format('attr_accessor :%s', m.rubyname))
          has_type = true
        end
      end
      println if has_type
      has_type = false
      state_members_.each do |m|
        unless m.visibility == :public
          unless has_type
            printiln('## private value state')
            printiln('protected')
          end
          printiln(format('attr_accessor :%s', m.rubyname))
          has_type = true
        end
      end
      println if has_type

      printiln('public')
      println

      unless node.is_custom?
        printiln('def self.marshal(vt, __os__)')
        nest {
          printiln("#{node.bases.first.scoped_rubyname}::Intf.marshal(vt, __os__)") if node.has_concrete_base?
          unless state_members_.empty?
            printiln('vt._marshal_with(__os__) do')
            nest {
              state_members_.each_with_index do |m, i|
                printiln("__os__.write_member(#{node.scoped_rubyname}._tc.member_type(#{i}),")
                printiln("                    self.#{m.rubyname})")
              end
            }
            printiln('end')
          end
        }
        printiln('end')
        println

        printiln('def self.unmarshal(vt, __is__)')
        nest {
          printiln("#{node.bases.first.scoped_rubyname}::Intf.unmarshal(vt, __is__)") if node.has_concrete_base?
          unless state_members_.empty?
            printiln('vt._unmarshal_with(__is__) do')
            nest {
              state_members_.each_with_index do |m, i|
                printi("self.#{m.rubyname} = ")
                println("__is__.read_member(#{node.scoped_rubyname}._tc.member_type(#{i}))")
              end
            }
            printiln('end')
          end
        }
        printiln('end')
        println

        printiln('def marshal(os)')
        nest {
          printiln("#{node.scoped_rubyname}::Intf.marshal(self, os)")
        }
        printiln('end')
        println

        printiln('def unmarshal(is)')
        nest {
          printiln("#{node.scoped_rubyname}::Intf.unmarshal(self, is)")
        }
        printiln('end')
        println
      end
    end
  }
  printiln('end # of Intf')
  println
  printiln('include Intf')
  println

  @nest -= 1
  if node.is_a?(IDL::AST::Eventtype)
    printiln("end #of eventtype #{node.rubyname}")
  else
    printiln("end #of valuetype #{node.rubyname}")
  end

  unless node.is_abstract?
    println
    initializers = node.initializers
    printiln("class #{node.rubyname}Factory < CORBA::Portable::ValueFactoryBase")
    nest {
      printiln("VALUE_ID = '#{node.repository_id}'.freeze")
      if initializers.empty?
        unless node.has_operations_or_attributes?(false)
          # valuetype has only state, create the default factory
          printiln('def _create_default')
          nest {
            printiln("#{node.scoped_rubyname}.new")
          }
          printiln('end')
        end
      else
        println
        initializers.each do |init|
          printiln("def #{init.rubyname}(#{init.params.collect { |p| p.rubyname }.join(',')})")
          nest {
            printiln("raise RuntimeError, 'unimplemented local operation called'")
          }
          printiln('end')
        end
      end
    }
    printiln("end # of #{node.rubyname}Factory")
  end
end

#operation_to_s(exp) ⇒ Object



835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
# File 'lib/ridlbe/ruby/walker.rb', line 835

def operation_to_s(exp)
  s = nil
  op = exp.operands
  case exp
  when Expression::Operation::UnaryPlus
    s = expression_to_s(op[0])
  when Expression::Operation::UnaryMinus
    s = '-' + expression_to_s(op[0])
  when Expression::Operation::UnaryNot
    s = '~' + expression_to_s(op[0])
  when Expression::Operation::Xor
    s = expression_to_s(op[0]) + ' ^ ' + expression_to_s(op[1])
  when Expression::Operation::Or
    s = expression_to_s(op[0]) + ' | ' + expression_to_s(op[1])
  when Expression::Operation::And
    s = expression_to_s(op[0]) + ' & ' + expression_to_s(op[1])
  when Expression::Operation::LShift
    s = expression_to_s(op[0]) + ' << ' + expression_to_s(op[1])
  when Expression::Operation::RShift
    s = expression_to_s(op[0]) + ' >> ' + expression_to_s(op[1])
  when Expression::Operation::Add
    s = expression_to_s(op[0]) + ' + ' + expression_to_s(op[1])
  when Expression::Operation::Minus
    s = expression_to_s(op[0]) + ' - ' + expression_to_s(op[1])
  when Expression::Operation::Mult
    s = expression_to_s(op[0]) + ' * ' + expression_to_s(op[1])
  when Expression::Operation::Div
    s = expression_to_s(op[0]) + ' / ' + expression_to_s(op[1])
  when Expression::Operation::Mod
    s = expression_to_s(op[0]) + ' % ' + expression_to_s(op[1])
  else
    raise "unknown operation: #{exp.type.name}"
  end
  '(' + s + ')'
end

#post_visit(parser) ⇒ Object



77
78
79
80
81
82
83
84
# File 'lib/ridlbe/ruby/walker.rb', line 77

def post_visit(parser)
  idleval = @params[:idl_eval] || false
  unless idleval
    printiln("} ## end of '#{@params[:idlfile]}'")
  end
  leave_module(parser.root_namespace) unless parser.root_namespace.nil?
  println('# -*- END -*-')
end

#pre_visit(parser) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ridlbe/ruby/walker.rb', line 56

def pre_visit(parser)
  print(
%Q{# -*- Ruby -*-
#
# ****  Code generated by the R2CORBA IDL Compiler ****
# R2CORBA has been developed by:
#        Remedy IT Expertise BV
#        The Netherlands
#        https://www.remedy.nl
#
})
  println("require 'corba'") if @params[:libinit]
  println
  enter_module(parser.root_namespace) unless parser.root_namespace.nil?
  idleval = @params[:idl_eval] || false
  unless idleval
    printiln("CORBA.implement('#{@params[:idlfile]}', {}, CORBA::IDL::CLIENT_STUB) {")
    println
  end
end

#value_to_s(exp) ⇒ Object



777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
# File 'lib/ridlbe/ruby/walker.rb', line 777

def value_to_s(exp)
  s = nil
  v = exp.value
  case exp.idltype
  when Type::Void
    s = 'nil'
  when Type::Char
    s = "'#{v.chr}'"
  when Type::Integer,
    Type::Boolean,
    Type::Octet,
    Type::Float
    s = v.to_s
  when Type::WChar
    s = (case v.first
    when :char, :esc_ch
      v.last.unpack('c')
    when :esc
      IDL::Scanner::ESCTBL[v.last]
    when :oct
      v.last.oct
    when :hex2, :hex4
      v.last.hex
    else
      0
    end).to_s
  when Type::Enum
    v = exp.idltype.narrow(v)
    s = exp.idltype.node.enumerators[v].scoped_rubyname
  when Type::String
    s = "'#{v.to_s}'"
  when Type::WString
    v = (v.collect do |(elt, elv)|
      case elt
      when :char, :esc_ch
        elv.unpack('c')
      when :esc
        IDL::Scanner::ESCTBL[elv]
      when :oct
        elv.oct
      when :hex2, :hex4
        elv.hex
      else
        nil
      end
    end).compact
    s = "[#{v.join(',')}]"
  # when Type::Fixed
  # when Type::Any
  # when Type::Object
  when Type::ScopedName
    s = value_to_s(Expression::Value.new(exp.idltype.node.idltype, v))
  else
    raise "#{exp.typename}'s not been supported yet."
  end
  s
end

#visit_attribute(node, from_valuetype = false) ⇒ Object



606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
# File 'lib/ridlbe/ruby/walker.rb', line 606

def visit_attribute(node, from_valuetype = false)
  _intf = node.enclosure
  println
  printiln("def #{node.rubyname}()")
  nest do
    if _intf.is_a?(IDL::AST::Valuetype) or from_valuetype
      printiln("@#{node.name}")
    elsif _intf.is_local?
      printiln('raise ::CORBA::NO_IMPLEMENT.new(')
      printiln("         'unimplemented attribute on local interface',")
      printiln('         1, ::CORBA::COMPLETED_NO)')
    else
      ## check if this is a proper object reference
      printiln('raise ::CORBA::NO_IMPLEMENT unless self.respond_to?(:_invoke, true)')

      printiln("_ret = self._invoke('_get_#{node.name}', {")
      nest { printi(":result_type => #{get_typecode(node.idltype)}") }
      if node.get_raises.size > 0
        println(',')
        nest {
          printi(':exc_list => [')
          pfx = ''
          nest {
            node.get_raises.each { |ex|
              println(pfx)
              pfx = ','
              printi(get_typecode(ex))
            }
            print(']')
          }
        }
      end
      println('})')

      printiln('_ret')
    end
  end
  printiln("end #of attribute #{node.name} getter")
  unless node.readonly
    printiln("def #{node.rubyname}=(val)")
    nest do
      if _intf.is_a?(IDL::AST::Valuetype) or from_valuetype
        printiln("@#{node.name} = val")
      elsif _intf.is_local?
        printiln('raise ::CORBA::NO_IMPLEMENT.new(')
        printiln("         'unimplemented attribute on local interface',")
        printiln('         1, ::CORBA::COMPLETED_NO)')
      else
        ## check if this is a proper object reference
        printiln('raise ::CORBA::NO_IMPLEMENT unless self.respond_to?(:_invoke, true)')

        ## validate IN arg
        printiln("val = #{get_typecode(node.idltype)}.validate(val)")
        ## invoke operation
        printiln("self._invoke('_set_#{node.name}', {")
        nest {
          printiln(':arg_list => [')
          nest {
            printiln("['val', CORBA::ARG_IN, #{get_typecode(node.idltype)}, val]],");
          }
          printi(':result_type => CORBA._tc_void')
          if node.set_raises.size > 0
            println(',')
            printi(':exc_list => [')
            pfx = ''
            nest {
              node.set_raises.each { |ex|
                println(pfx)
                pfx = ','
                printi(get_typecode(ex))
              }
              print(']')
            }
          end
          println('})')
        }
      end
    end
    printiln("end #of attribute #{node.name} setter")
  end
end

#visit_const(node) ⇒ Object



496
497
498
499
500
# File 'lib/ridlbe/ruby/walker.rb', line 496

def visit_const(node)
  # v = Expression::Value.new(node.idltype, node.value)
  s = node.rubyname + ' = ' + expression_to_s(node.expression)
  printiln(s)
end

#visit_enum(node) ⇒ Object



992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
# File 'lib/ridlbe/ruby/walker.rb', line 992

def visit_enum(node)
  printiln(format('class %s < ::R2CORBA::FIXNUM_KLASS', node.rubyname))
  nest {
    printiln(format('def %s._tc', node.rubyname))
    nest {
      printi(format("@@tc_%s ||= CORBA::TypeCode::Enum.new('%s'.freeze, '%s', [",
                    node.rubyname, node.repository_id, node.rubyname))
      pfx = ''
      node.enumerators.each { |e|
        println(pfx)
        pfx = ','
        printi("    '#{e.rubyname}'")
      }
      println('])')
    }
    printiln('end')
    printiln('self._tc  # register typecode');
  }
  printiln(format('end # enum %s', node.rubyname))
end

#visit_enumerator(node) ⇒ Object



1013
1014
1015
1016
1017
# File 'lib/ridlbe/ruby/walker.rb', line 1013

def visit_enumerator(node)
  v = Expression::Value.new(node.idltype, node.value)
  s = node.rubyname + ' = ' + expression_to_s(v)
  printiln(s)
end

#visit_include(node) ⇒ Object



86
87
88
89
# File 'lib/ridlbe/ruby/walker.rb', line 86

def visit_include(node)
  printiln(format("require '%s'", node.filename.sub(/\.[^\.]*$/, @params[:stub_pfx])))
  println
end

#visit_operation(node, from_valuetype = false) ⇒ Object



502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/ridlbe/ruby/walker.rb', line 502

def visit_operation(node, from_valuetype = false)
  _parm = node.params
  _in = node.in_params
  _out = node.out_params
  _intf = node.enclosure

  println
  printi("def #{node.rubyname}(")
  print(_in.collect{ |p| p.rubyname }.join(', '))
  if node.oneway
    println(')    # oneway')
  else
    println(')')
  end

  nest do
    if _intf.is_a?(IDL::AST::Valuetype) or from_valuetype
      printiln("raise RuntimeError, 'unimplemented local operation called'")
    elsif _intf.is_local?
      printiln('raise ::CORBA::NO_IMPLEMENT.new(')
      printiln("         'unimplemented operation on local interface',")
      printiln('         1, ::CORBA::COMPLETED_NO)')
    else
      ## check if this is a proper object reference
      printiln('raise ::CORBA::NO_IMPLEMENT unless self.respond_to?(:_invoke, true)')

      ##  validate data for IN/INOUT args
      ##
      if _parm.size > 0
        _parm.each do |p|
          if p.attribute != IDL::AST::Parameter::OUT
            printiln("#{p.rubyname} = #{get_typecode(p.idltype)}.validate(#{p.rubyname})")
          end
        end
      end

      ##  invoke operation
      ##
      if not node.oneway
        printi("_ret = self._invoke('#{node.name}', {")
      else
        printi("self._invoke('#{node.name}', {")
      end

      newln = ''
      if _parm.size > 0
        println(newln)
        nest do
          printi(':arg_list => [')
          nest {
            pfx = ''
            _parm.each do |p|
              println(pfx)
              printi("['#{p.rubyname}', #{get_arg_type(p.attribute)}, ");
              print(get_typecode(p.idltype))
              if p.attribute != IDL::AST::Parameter::OUT
                print(", #{p.rubyname}]")
              else
                print(']')
              end
              pfx = ','
            end
            print(']')
          }
        end
        newln = ','
      end

      unless node.oneway
        println(newln)
        nest { printi(":result_type => #{get_typecode(node.idltype)}") }
        newln = ','
      end

      if node.raises.size > 0
        println(newln)
        nest {
          printi(':exc_list => [')
          pfx = ''
          nest {
            node.raises.each { |ex|
              println(pfx)
              pfx = ','
              printi(get_typecode(ex))
            }
            print(']')
          }
        }
      end

      println('})')

      unless node.oneway
        returns_void = (node.idltype.is_a? Type::Void)
        size = _out.size
        size += 1 unless returns_void
        printiln('_ret')
      end
    end
  end

  printiln("end #of operation #{node.rubyname}")
end

#visit_typedef(node) ⇒ Object



1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
# File 'lib/ridlbe/ruby/walker.rb', line 1019

def visit_typedef(node)
  # tc_ = node.enclosure.rubyname + '._tc_' + node.rubyname
  # typ_ = node.enclosure.rubyname + '.' + node.rubyname
  case t = node.idltype
  when Type::ScopedName
    if Type::Interface === t.resolved_type
      printiln(format('%s = %s # typedef %s', node.rubyname, t.node.scoped_rubyname, node.rubyname))
    else
      printiln(format('class %s < %s', node.rubyname, t.node.scoped_rubyname))
      nest {
        printi(format('def %s._tc; ', node.rubyname))
        print(format("@@tc_%s ||= CORBA::TypeCode::Alias.new('%s', '%s',", node.rubyname, node.repository_id, node.rubyname))
        println(format('%s, self); end', get_typecode(t)))
      }
      printiln(format('end # typedef %s', node.rubyname))
    end

  when IDL::Type::Native
    printiln("class #{node.rubyname}; end  ## 'native' type");

  when Type::Any, Type::Octet,
      Type::UShort, Type::Short,
      Type::ULong, Type::Long,
      Type::ULongLong, Type::LongLong,
      Type::Boolean, Type::Char, Type::WChar,
      Type::Float, Type::Double, Type::LongDouble
    s = t.class.name.split('::') # IDL::Type::Octet -> [IDL, Type, Octet]
    s = s[s.length - 1]
    s.downcase!
    printiln(format('class %s < CORBA::_tc_%s.get_type', node.rubyname, s))
    nest {
      printiln(format("def %s._tc; @@tc_%s ||= CORBA::TypeCode::Alias.new('%s', '%s', CORBA::_tc_%s, self); end",
                      node.rubyname, node.rubyname, node.repository_id, node.rubyname, s))
    }
    printiln(format('end # typedef %s', node.rubyname))

  when Type::String
    printiln(format('class %s < String', node.rubyname))
    nest {
      if not t.length.nil?
        printiln(format("def %s._tc; @@tc_%s ||= CORBA::TypeCode::Alias.new('%s', '%s', CORBA::TypeCode::String.new(%d), self); end",
                        node.rubyname, node.rubyname, node.repository_id, node.rubyname, t.length))
      else
        printiln(format("def %s._tc; @@tc_%s ||= CORBA::TypeCode::Alias.new('%s', '%s', CORBA::_tc_string, self); end",
                        node.rubyname, node.rubyname, node.repository_id, node.rubyname))
      end
    }
    printiln(format('end # typedef %s', node.rubyname))

  when Type::WString
    printiln(format('class %s < Array', node.rubyname))
    nest {
      if not t.length.nil?
        printiln(format("def %s._tc; @@tc_%s ||= CORBA::TypeCode::Alias.new('%s', '%s', CORBA::TypeCode::WString.new(%d), self); end",
                        node.rubyname, node.rubyname, node.repository_id, node.rubyname, t.length))
      else
        printiln(format("def %s._tc; @@tc_%s ||= CORBA::TypeCode::Alias.new('%s', '%s', CORBA::_tc_wstring, self); end",
                        node.rubyname, node.rubyname, node.repository_id, node.rubyname))
      end
    }
    printiln(format('end # typedef %s', node.rubyname))

  when IDL::Type::Array
    printiln(format('class %s < Array', node.rubyname))
    nest {
      printiln(format('def %s._tc', node.rubyname))
      nest {
        printiln(format("@@tc_%s ||= CORBA::TypeCode::Alias.new('%s', '%s',", node.rubyname, node.repository_id, node.rubyname))
        nest { printiln(format('%s, self)', get_typecode(t))) }
      }
      printiln('end')
    }
    printiln(format('end # typedef %s', node.rubyname))

  when IDL::Type::Sequence
    case t.basetype.resolved_type
    when IDL::Type::Char, IDL::Type::Octet
      printiln(format('class %s < String', node.rubyname))
    else
      printiln(format('class %s < Array', node.rubyname))
    end
    nest {
      printiln(format('def %s._tc', node.rubyname))
      nest {
        printiln(format("@@tc_%s ||= CORBA::TypeCode::Alias.new('%s', '%s',", node.rubyname, node.repository_id, node.rubyname))
        nest { printiln(format('%s, self)', get_typecode(t))) }
      }
      printiln('end')
    }
    printiln(format('end # typedef %s', node.rubyname))

  when IDL::Type::Object
    printiln(format("%s = CORBA::Object # typedef %s\n", node.rubyname, node.rubyname))

  else
    raise "unsupported typedef for #{t.class.name}."
  end
end

#visit_valuebox(node) ⇒ Object



472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/ridlbe/ruby/walker.rb', line 472

def visit_valuebox(node)
  println
  name = node.rubyname
  printiln("class #{name}")
  nest {
    printiln('include CORBA::Portable::BoxedValueBase')
    println
    printiln("TRUNCATABLE_IDS = [ '#{node.repository_id}' ].freeze")
    println
    printiln(format('def %s._tc', node.rubyname))
    nest {
      printiln(format("@@tc_%s ||= CORBA::TypeCode::Valuebox.new('%s'.freeze, '%s',",
                      node.rubyname, node.repository_id, node.rubyname))
      printiln("   #{get_typecode(node.boxed_type)}, self)")
    }
    printiln('end')
    printiln('self._tc  # register typecode');
    printiln('attr_accessor :value')
    printiln('def initialize(val = nil); @value = val; end')
  }
  printiln("end #of valuebox #{name}")
  println
end