Class: Mirah::JVM::Compiler::JavaSource

Inherits:
Base
  • Object
show all
Defined in:
lib/mirah/plugin/gwt.rb,
lib/mirah/jvm/compiler/java_source.rb,
lib/mirah/jvm/source_generator/loops.rb

Direct Known Subclasses

ClosureCompiler

Defined Under Namespace

Modules: Redoable Classes: ClosureCompiler, ComplexWhileLoop, ImplicitReturn, SimpleWhileLoop

Constant Summary collapse

JVMTypes =
Mirah::JVM::Types
Operators =
[
  '+', '-', '+@', '-@', '/', '%', '*', '<',
  '<=', '==', '!=', '>=', '>',
  '<<', '>>', '>>>', '|', '&', '^', '~'
]
ArrayOps =
[
  '[]', '[]=', 'length'
]

Instance Attribute Summary collapse

Attributes inherited from Base

#class, #filename, #method, #static

Instance Method Summary collapse

Methods inherited from Base

#base_define_method, #begin_main, #compile, #compile_self, #create_method_builder, #declare_argument, #declared_captures, #define_main, #error, #finish_main, #fixnum, #generate, #get_binding, #import, #log, #original_create_method_builder, #scoped_local_name, #toplevel_class, #with

Constructor Details

#initializeJavaSource

Returns a new instance of JavaSource.



48
49
50
# File 'lib/mirah/jvm/compiler/java_source.rb', line 48

def initialize
  super
end

Instance Attribute Details

#lvalueObject

Returns the value of attribute lvalue.



35
36
37
# File 'lib/mirah/jvm/compiler/java_source.rb', line 35

def lvalue
  @lvalue
end

Instance Method Details

#_raise(node) ⇒ Object



180
181
182
183
184
185
186
187
188
# File 'lib/mirah/jvm/compiler/java_source.rb', line 180

def _raise(node)
  if node.expr?(self)
    @method.print 'throw '
    node.compile(self, true)
    @method.puts ';'
  else
    store_value('throw ', node)
  end
end

#annotate(node, annotations) ⇒ Object



91
92
93
# File 'lib/mirah/jvm/compiler/java_source.rb', line 91

def annotate(node, annotations)
  node.annotate(annotations)
end

#array(node, expression) ⇒ Object



682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
# File 'lib/mirah/jvm/compiler/java_source.rb', line 682

def array(node, expression)
  if expression
    # create unmodifiable list from array (simplest way to do this in Java source)
    @method.print "java.util.Collections.unmodifiableList(java.util.Arrays.asList("

    # elements, as expressions
    comma = false
    node.children.each do |n|
      @method.print ", " if comma
      n.compile(self, true)
      comma = true
    end

    @method.print("))")
  else
    # elements, as non-expressions
    # TODO: ensure they're all reference types!
    node.children.each do |n|
      n.compile(self, false)
    end
  end
end

#array_op(target, name, args, expression) ⇒ Object



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
# File 'lib/mirah/jvm/compiler/java_source.rb', line 523

def array_op(target, name, args, expression)
  simple = expr?(target, args)
  index, value = args
  if expression && !simple
    @method.print @lvalue
  end
  target.compile(self, true)
  if name == 'length'
    @method.print '.length'
  else
    @method.print '['
    index.compile(self, true)
    @method.print ']'
    if name == '[]='
      @method.print " = "
      value.compile(self, true)
    end
  end
  unless simple && expression
    @method.puts ';'
  end
end

#assign(name, value) ⇒ Object



342
343
344
345
# File 'lib/mirah/jvm/compiler/java_source.rb', line 342

def assign(name, value)
  store_value("#{name} = ", value)
  name
end

#binding_referenceObject



738
739
740
# File 'lib/mirah/jvm/compiler/java_source.rb', line 738

def binding_reference
  @method.print '$binding'
end

#body(body, expression) ⇒ Object



355
356
357
358
359
# File 'lib/mirah/jvm/compiler/java_source.rb', line 355

def body(body, expression)
  super(body, expression) do |last|
    maybe_store(last, expression)
  end
end

#boolean(value) ⇒ Object



672
673
674
# File 'lib/mirah/jvm/compiler/java_source.rb', line 672

def boolean(value)
  @method.print value ? 'true' : 'false'
end

#branch(node, expression) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
# File 'lib/mirah/jvm/compiler/java_source.rb', line 388

def branch(node, expression)
  if expression && node.expr?(self)
    return branch_expression(node)
  end
  predicate = node.condition.predicate.precompile(self)
  @method.print 'if ('
  predicate.compile(self, true)
  @method.block ")" do
    if node.body
      maybe_store(node.body, expression)
    elsif expression
      store_value(@lvalue, @method.init_value(node.inferred_type))
    end
  end
  if node.else || expression
    @method.block 'else' do
      if node.else
        maybe_store(node.else, expression)
      else
        store_value(@lvalue, @method.init_value(node.inferred_type))
      end
    end
  end
end

#branch_expression(node) ⇒ Object



371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
# File 'lib/mirah/jvm/compiler/java_source.rb', line 371

def branch_expression(node)
  node.condition.compile(self, true)
  @method.print ' ? ('
  if node.body
    node.body.compile(self, true)
  else
    @method.print @method.init_value(node.inferred_type)
  end
  @method.print ') : ('
  if node.else
    node.else.compile(self, true)
  else
    @method.print @method.init_value(node.inferred_type)
  end
  @method.print ')'
end

#break(node) ⇒ Object



546
547
548
549
# File 'lib/mirah/jvm/compiler/java_source.rb', line 546

def break(node)
  error("break outside of loop", node) unless @loop
  @loop.break
end

#build_string(orig_nodes, expression) ⇒ Object



705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
# File 'lib/mirah/jvm/compiler/java_source.rb', line 705

def build_string(orig_nodes, expression)
  if expression
    nodes = precompile_nodes(orig_nodes)
    simple = nodes.equal?(orig_nodes)

    unless simple
      @method.print(lvalue)
    end

    @method.print '"" + ' unless nodes.first.kind_of?(Mirah::AST::String)

    nodes.first.compile(self, true)

    nodes[1..-1].each do |node|
      @method.print ' + '
      node.compile(self, true)
    end
    unless simple
      @method.puts ';'
    end
  else
    orig_nodes.each {|n| n.compile(self, false)}
  end
end

#call(call, expression) ⇒ Object



503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/mirah/jvm/compiler/java_source.rb', line 503

def call(call, expression)
  return cast(call, expression) if call.cast?
  if Mirah::AST::Constant === call.target || Mirah::AST::Colon2 === call.target
    target = call.target.inferred_type.to_source
  else
    target = call.precompile_target(self)
  end
  params = compile_args(call)

  if Operators.include? call.name
    operator(target, call.name, params, expression)
  elsif call.target.inferred_type.array? && ArrayOps.include?(call.name)
    array_op(target, call.name, params, expression)
  elsif call.name == 'nil?'
    operator(target, '==', ['null'], expression)
  else
    method_call(target, call, params, expression)
  end
end

#captured_local(scope, name, type) ⇒ Object



316
317
318
319
# File 'lib/mirah/jvm/compiler/java_source.rb', line 316

def captured_local(scope, name, type)
  captured_local_declare(scope, name, type)
  @method.print "$binding.#{name}"
end

#captured_local_assign(node, expression) ⇒ Object



321
322
323
324
325
326
# File 'lib/mirah/jvm/compiler/java_source.rb', line 321

def captured_local_assign(node, expression)
  scope, name, type = node.containing_scope, node.name, node.inferred_type
  captured_local_declare(scope, name, type)
  lvalue = "#{@lvalue if expression}$binding.#{name} = "
  store_value(lvalue, node.value)
end

#captured_local_declare(scope, name, type) ⇒ Object



309
310
311
312
313
314
# File 'lib/mirah/jvm/compiler/java_source.rb', line 309

def captured_local_declare(scope, name, type)
  unless declared_captures[name]
    declared_captures[name] = type
    @binding.declare_field(name, type, false, '')
  end
end

#cast(call, expression) ⇒ Object



479
480
481
482
483
484
485
486
487
# File 'lib/mirah/jvm/compiler/java_source.rb', line 479

def cast(call, expression)
  args = compile_args(call)
  simple = call.expr?(self)
  @method.print @lvalue if expression && !simple
  @method.print "((#{call.inferred_type.to_source})("
  args.each{|arg| arg.compile(self, true)}
  @method.print "))"
  @method.puts ';' unless simple && expression
end

#compile_args(call) ⇒ Object



465
466
467
# File 'lib/mirah/jvm/compiler/java_source.rb', line 465

def compile_args(call)
  precompile_nodes(call.parameters)
end

#constructor(node) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/mirah/jvm/compiler/java_source.rb', line 109

def constructor(node)
  super(node, false) do |method, _|
    with :method => method do
      @method.start
      if node.delegate_args
        delegate = if node.calls_super
          "super"
        else
          "this"
        end
        method.print "#{delegate}("
        node.delegate_args.each_with_index do |arg, index|
          method.print ', ' unless index == 0
          raise "Invalid constructor argument #{arg}" unless arg.expr?(self)
          arg.compile(self, true)
        end
        method.puts ");"
      end

      prepare_binding(node) do
        declare_locals(node.static_scope)
        node.body.compile(self, false) if node.body
      end
      method.stop
    end
  end
end

#declare_field(name, type, annotations, static_field) ⇒ Object



229
230
231
# File 'lib/mirah/jvm/compiler/java_source.rb', line 229

def declare_field(name, type, annotations, static_field)
  @class.declare_field(name, type, @static || static_field, 'private', annotations)
end

#declare_local(name, type) ⇒ Object



225
226
227
# File 'lib/mirah/jvm/compiler/java_source.rb', line 225

def declare_local(name, type)
  @method.declare_local(type, name)
end

#declare_locals(scope) ⇒ Object



254
255
256
257
258
259
260
261
# File 'lib/mirah/jvm/compiler/java_source.rb', line 254

def declare_locals(scope)
  scope.locals.each do |name|
    full_name = scoped_local_name(name, scope)
    unless scope.captured?(name) || method.local?(full_name)
      declare_local(full_name, scope.local_type(name))
    end
  end
end

#define_class(class_def, expression) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/mirah/jvm/compiler/java_source.rb', line 60

def define_class(class_def, expression)
  with(:type => class_def.inferred_type,
  :class => class_def.inferred_type.define(@file),
  :static => false) do
    annotate(@class, class_def.annotations)
    class_def.body.compile(self, false) if class_def.body
    @class.stop unless @method && @method.name == 'main' && @class == @method.klass
  end
end

#define_closure(class_def, expression) ⇒ Object



161
162
163
164
# File 'lib/mirah/jvm/compiler/java_source.rb', line 161

def define_closure(class_def, expression)
  compiler = ClosureCompiler.new(@file, @type, self)
  compiler.define_class(class_def, expression)
end

#define_jsni_method(node) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/mirah/plugin/gwt.rb', line 83

def define_jsni_method(node)
  base_define_method(node, false) do |method, arg_types|
    with :method => method do
      log "Starting new JSNI method #{node.name}"
      @method.start

      @method.puts node.body.literal.chomp

      log "JSNI method #{node.name} complete!"
      @method.stop
    end
  end
end

#define_method(node) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mirah/jvm/compiler/java_source.rb', line 70

def define_method(node)
  base_define_method(node, false) do |method, _|
    with :method => method do
      log "Starting new method #{node.name}"
      @method.start

      prepare_binding(node) do
        declare_locals(node.static_scope)
        unless method.returns_void?
          self.return(ImplicitReturn.new(node.body))
        else
          node.body.compile(self, false) if node.body
        end
      end

      log "Method #{node.name} complete!"
      @method.stop
    end
  end
end

#define_optarg_chain(name, arg, return_type, args_for_opt, arg_types_for_opt) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/mirah/jvm/compiler/java_source.rb', line 95

def define_optarg_chain(name, arg, return_type,
  args_for_opt, arg_types_for_opt)
  # declare all args so they get their values
  @method.print "return " unless method.returns_void?
  @method.print "this." unless @static
  @method.print "#{name}("
  @method.print args_for_opt.map(&:name).join(', ')
  @method.print ', 'if args_for_opt.size > 0
  arg.value.compile(self, true)

  # invoke the next one in the chain
  @method.print ");\n"
end

#empty_array(type, size) ⇒ Object



661
662
663
664
665
666
# File 'lib/mirah/jvm/compiler/java_source.rb', line 661

def empty_array(type, size)
  sizevar = size.precompile(self)
  @method.print "#{@lvalue unless size.expr?(self)}new #{type.name}["
  sizevar.compile(self, true)
  @method.print ']'
end

#ensure(node, expression) ⇒ Object



213
214
215
216
217
218
219
220
# File 'lib/mirah/jvm/compiler/java_source.rb', line 213

def ensure(node, expression)
  @method.block 'try' do
    maybe_store(node.body, expression)
  end
  @method.block 'finally' do
    node.clause.compile(self, false)
  end
end

#expr?(target, params) ⇒ Boolean

Returns:

  • (Boolean)


424
425
426
# File 'lib/mirah/jvm/compiler/java_source.rb', line 424

def expr?(target, params)
  !([target] + params).any? {|x| x.kind_of? Mirah::AST::TempValue}
end

#field(name, type, annotations, static_field) ⇒ Object



238
239
240
241
242
# File 'lib/mirah/jvm/compiler/java_source.rb', line 238

def field(name, type, annotations, static_field)
  name = name[1..-1] if name =~ /^@/
  declare_field(name, type, annotations, static_field)
  @method.print "#{this}.#{name}"
end

#field_assign(name, type, expression, value, annotations, static_field) ⇒ Object



302
303
304
305
306
307
# File 'lib/mirah/jvm/compiler/java_source.rb', line 302

def field_assign(name, type, expression, value, annotations, static_field)
  name = name[1..-1] if name =~ /^@/
  declare_field(name, type, annotations, static_field)
  lvalue = "#{@lvalue if expression}#{this}.#{name} = "
  store_value(lvalue, value)
end

#field_declare(name, type, annotations) ⇒ Object



292
293
294
295
# File 'lib/mirah/jvm/compiler/java_source.rb', line 292

def field_declare(name, type, annotations)
  name = name[1..-1] if name =~ /^@/
  declare_field(name, type, annotations)
end

#file_builder(filename) ⇒ Object



52
53
54
# File 'lib/mirah/jvm/compiler/java_source.rb', line 52

def file_builder(filename)
  Mirah::JavaSource::Builder.new(filename, self)
end

#line(num) ⇒ Object



222
223
# File 'lib/mirah/jvm/compiler/java_source.rb', line 222

def line(num)
end

#local(scope, name, type) ⇒ Object



233
234
235
236
# File 'lib/mirah/jvm/compiler/java_source.rb', line 233

def local(scope, name, type)
  name = scoped_local_name(name, scope)
  @method.print name
end

#local_assign(scope, name, type, expression, value) ⇒ Object



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
# File 'lib/mirah/jvm/compiler/java_source.rb', line 263

def local_assign(scope, name, type, expression, value)
  simple = value.expr?(self)
  value = value.precompile(self)
  name = scoped_local_name(name, scope)
  if method.local?(name)
    if expression
      if simple
        @method.print '('
      else
        @method.print @lvalue
      end
    end
    @method.print "#{name} = "
    value.compile(self, true)
    if simple && expression
      @method.print ')'
    else
      @method.puts ';'
    end
  else
    @method.declare_local(type, name) do
      value.compile(self, true)
    end
    if expression
      @method.puts "#{@lvalue}#{name};"
    end
  end
end

#local_declare(scope, name, type) ⇒ Object



297
298
299
300
# File 'lib/mirah/jvm/compiler/java_source.rb', line 297

def local_declare(scope, name, type)
  name = scoped_local_name(name, scope)
  declare_local(name, type)
end

#loop(loop, expression) ⇒ Object



413
414
415
416
417
418
419
420
421
422
# File 'lib/mirah/jvm/compiler/java_source.rb', line 413

def loop(loop, expression)
  if loop.redo? || loop.post || !loop.condition.predicate.expr?(self)
    loop = ComplexWhileLoop.new(loop, self)
  else
    loop = SimpleWhileLoop.new(loop, self)
  end
  with(:loop => loop) do
    loop.compile(expression)
  end
end

#maybe_store(value, expression) ⇒ Object



347
348
349
350
351
352
353
# File 'lib/mirah/jvm/compiler/java_source.rb', line 347

def maybe_store(value, expression)
  if expression
    store_value(@lvalue, value)
  else
    value.compile(self, false)
  end
end

#method_call(target, call, params, expression) ⇒ Object



594
595
596
597
598
599
600
601
602
603
604
605
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
# File 'lib/mirah/jvm/compiler/java_source.rb', line 594

def method_call(target, call, params, expression)
  simple = call.expr?(self)
  method = call.method(self)
  unless simple || method.return_type.void?
    @method.print @lvalue if expression
  end

  # preamble
  if method.constructor?
    @method.print "new "
    target.compile(self, true)
    @method.print '('
  elsif method.field?
    target.compile(self, true)
    @method.print ".#{method.name}"
    if method.argument_types.size == 1
      @method.print " = ("
    end
  elsif Mirah::JVM::Types::Intrinsic === method
    method.call(self, call, expression)
    return
  else
    @method.print "("             if Mirah::AST::StringConcat === target
    target.compile(self, true)
    @method.print ")"             if Mirah::AST::StringConcat === target
    @method.print ".#{method.name}("
  end

  # args
  params.each_with_index do |param, index|
    @method.print ', ' unless index == 0
    param.compile(self, true)
  end

  # postamble
  if !method.field? || (method.field? && method.argument_types.size == 1)
    if simple && expression
      @method.print ')'
    else
      @method.puts ');'
    end
  end

  # cleanup
  if method.return_type.void? && expression
    @method.print @lvalue
    if method.static?
      @method.puts 'null;'
    else
      target.compile(self, true)
      @method.puts ';'
    end
  end
end

#next(node) ⇒ Object



551
552
553
554
# File 'lib/mirah/jvm/compiler/java_source.rb', line 551

def next(node)
  error("next outside of loop", node) unless @loop
  @loop.next
end

#nullObject



734
735
736
# File 'lib/mirah/jvm/compiler/java_source.rb', line 734

def null
  @method.print 'null'
end

#operator(target, op, params, expression) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/mirah/jvm/compiler/java_source.rb', line 428

def operator(target, op, params, expression)
  simple = expr?(target, params)
  if expression && !simple
    @method.print @lvalue
  end
  if params.size == 0
    # unary operator
    op = op[0,1]
    @method.print op
    target.compile(self, true)
  else
    @method.print '('
    other = params[0]
    target.compile(self, true)
    @method.print " #{op} "
    other.compile(self, true)
    @method.print ')'
  end
  unless expression && simple
    @method.puts ';'
  end
end

#output_typeObject



56
57
58
# File 'lib/mirah/jvm/compiler/java_source.rb', line 56

def output_type
  "source files"
end

#precompile_nodes(nodes) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
462
463
# File 'lib/mirah/jvm/compiler/java_source.rb', line 451

def precompile_nodes(nodes)
  if nodes.all? {|n| n.expr?(self)}
    nodes
  else
    nodes.map do |node|
      tempval = node.precompile(self)
      if node == tempval && !node.kind_of?(Mirah::AST::Literal)
        tempval = node.temp(self)
      end
      tempval
    end
  end
end

#prepare_binding(scope) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/mirah/jvm/compiler/java_source.rb', line 137

def prepare_binding(scope)
  if scope.has_binding?
    type = scope.binding_type
    @binding = @bindings[type]
    @method.puts "#{type.to_source} $binding = new #{type.to_source}();"
    if scope.respond_to? :arguments
      scope.arguments.args.each do |param|
        if scope.static_scope.captured?(param.name)
          captured_local_declare(scope, param.name, param.inferred_type)
          @method.puts "$binding.#{param.name} = #{param.name};"
        end
      end
    end
  end
  begin
    yield
  ensure
    if scope.has_binding?
      @binding.stop
      @binding = nil
    end
  end
end


746
747
748
749
750
751
752
753
754
755
756
# File 'lib/mirah/jvm/compiler/java_source.rb', line 746

def print(node)
  value = node.parameters[0]
  value = value && value.precompile(self)
  if node.println
    @method.print "System.out.println("
  else
    @method.print "System.out.print("
  end
  value.compile(self, true) if value
  @method.puts ');'
end

#real_selfObject



742
743
744
# File 'lib/mirah/jvm/compiler/java_source.rb', line 742

def real_self
  @method.print 'this'
end

#redo(node) ⇒ Object



556
557
558
559
# File 'lib/mirah/jvm/compiler/java_source.rb', line 556

def redo(node)
  error("redo outside of loop", node) unless @loop
  @loop.redo
end

#regexp(value, flags = 0) ⇒ Object



676
677
678
679
680
# File 'lib/mirah/jvm/compiler/java_source.rb', line 676

def regexp(value, flags = 0)
  @method.print "java.util.regex.Pattern.compile("
  @method.print value.inspect
  @method.print ")"
end

#rescue(node, expression) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/mirah/jvm/compiler/java_source.rb', line 190

def rescue(node, expression)
  @method.block 'try' do
    if node.else_node.nil?
      maybe_store(node.body, expression) if node.body
    else
      node.body.compile(self, false) if node.body
    end
  end
  node.clauses.each do |clause|
    clause.types.each do |type|
      name = scoped_local_name(clause.name || 'tmp$ex', clause.static_scope)
      @method.declare_local(type, name, false)
      @method.block "catch (#{type.to_source} #{name})" do
        declare_locals(clause.static_scope)
        maybe_store(clause.body, expression)
      end
    end
  end
  if node.else_node
    maybe_store(node.else_node, expression)
  end
end

#return(node) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/mirah/jvm/compiler/java_source.rb', line 166

def return(node)
  if method.returns_void?
    @method.puts 'return;'
    return
  end
  if node.value.expr?(self)
    @method.print 'return '
    node.value.compile(self, true)
    @method.puts ';'
  else
    store_value('return ', node.value)
  end
end

#scoped_body(scope, expression) ⇒ Object



361
362
363
364
365
366
367
368
369
# File 'lib/mirah/jvm/compiler/java_source.rb', line 361

def scoped_body(scope, expression)
  if @method
    @method.block do
      super
    end
  else
    super
  end
end

#self_call(call, expression) ⇒ Object



489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/mirah/jvm/compiler/java_source.rb', line 489

def self_call(call, expression)
  if call.cast?
    cast(call, expression)
  else
    type = call.scope.static_scope.self_type
    type = type.meta if (@static && type == @type)
    params = call.parameters.map do |param|
      param.inferred_type
    end
    method = type.get_method(call.name, params)
    method_call(this(method), call, compile_args(call), expression)
  end
end

#self_typeObject



469
470
471
472
473
# File 'lib/mirah/jvm/compiler/java_source.rb', line 469

def self_type
  type = AST.type(nil, @class.name.tr('/', '.'))
  type = type.meta if @static
  type
end

#store_value(lvalue, value) ⇒ Object



328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/mirah/jvm/compiler/java_source.rb', line 328

def store_value(lvalue, value)
  if value.is_a? String
    @method.puts "#{lvalue}#{value};"
  elsif value.expr?(self)
    @method.print lvalue
    value.compile(self, true)
    @method.puts ';'
  else
    with :lvalue => lvalue do
      value.compile(self, true)
    end
  end
end

#string(value) ⇒ Object



668
669
670
# File 'lib/mirah/jvm/compiler/java_source.rb', line 668

def string(value)
  @method.print value.inspect
end

#super_call(call, expression) ⇒ Object



475
476
477
# File 'lib/mirah/jvm/compiler/java_source.rb', line 475

def super_call(call, expression)
  super_method_call(this, call, compile_args(call), expression)
end

#super_method_call(target, call, params, expression) ⇒ Object

TODO: merge cleanly with method_call logic



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
# File 'lib/mirah/jvm/compiler/java_source.rb', line 562

def super_method_call(target, call, params, expression)
  simple = call.expr?(self)
  method = call.method(self)
  unless simple || method.return_type.void?
    @method.print @lvalue if expression
  end
  if method.constructor?
    @method.print "super("
  else
    @method.print "super.#{method.name}("
  end
  params.each_with_index do |param, index|
    @method.print ', ' unless index == 0
    param.compile(self, true)
  end
  if simple && expression
    @method.print ')'
  else
    @method.puts ');'
  end
  if method.return_type.void? && expression
    @method.print @lvalue
    if method.static?
      @method.puts 'null;'
    else
      target.compile(self, true)
      @method.puts ';'
    end
  end

end

#temp(expression, value = nil) ⇒ Object



649
650
651
652
653
654
655
656
657
658
659
# File 'lib/mirah/jvm/compiler/java_source.rb', line 649

def temp(expression, value=nil)
  value ||= expression
  type = value.inferred_type
  if value.expr?(self)
    @method.tmp(type) do
      value.compile(self, true)
    end
  else
    assign(@method.tmp(type), value)
  end
end

#this(method = nil) ⇒ Object



244
245
246
247
248
249
250
251
252
# File 'lib/mirah/jvm/compiler/java_source.rb', line 244

def this(method=nil)
  if method && method.static?
    method.declaring_class.name
  elsif @self_scope && @self_scope.self_node && @self_scope.self_node != :self
    scoped_local_name('self', @self_scope)
  else
    @static ? @class.class_name : 'this'
  end
end

#to_string(body, expression) ⇒ Object



730
731
732
# File 'lib/mirah/jvm/compiler/java_source.rb', line 730

def to_string(body, expression)
  body.compile(self, expression)
end