Class: Duby::Compiler::JavaSource

Inherits:
JVMCompilerBase show all
Defined in:
lib/duby/jvm/source_compiler.rb,
lib/duby/jvm/source_generator/loops.rb

Direct Known Subclasses

ClosureCompiler

Defined Under Namespace

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Attributes inherited from JVMCompilerBase

#class, #filename, #method, #static

Instance Method Summary collapse

Methods inherited from JVMCompilerBase

#begin_main, #compile, #declare_argument, #declared_captures, #define_class, #define_main, #finish_main, #generate, #get_binding, #import, #log, #scoped_local_name, #toplevel_class, #with

Constructor Details

#initialize(filename) ⇒ JavaSource

Returns a new instance of JavaSource.



32
33
34
35
# File 'lib/duby/jvm/source_compiler.rb', line 32

def initialize(filename)
  super
  @file = Duby::JavaSource::Builder.new(filename, self)
end

Instance Attribute Details

#lvalueObject

Returns the value of attribute lvalue.



19
20
21
# File 'lib/duby/jvm/source_compiler.rb', line 19

def lvalue
  @lvalue
end

Instance Method Details

#_raise(node) ⇒ Object



141
142
143
144
145
146
147
148
149
# File 'lib/duby/jvm/source_compiler.rb', line 141

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



61
62
63
# File 'lib/duby/jvm/source_compiler.rb', line 61

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

#array(node, expression) ⇒ Object



596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/duby/jvm/source_compiler.rb', line 596

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



445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/duby/jvm/source_compiler.rb', line 445

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



279
280
281
282
# File 'lib/duby/jvm/source_compiler.rb', line 279

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

#binding_referenceObject



650
651
652
# File 'lib/duby/jvm/source_compiler.rb', line 650

def binding_reference
  @method.print '$binding'
end

#body(body, expression) ⇒ Object



292
293
294
295
296
# File 'lib/duby/jvm/source_compiler.rb', line 292

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

#boolean(value) ⇒ Object



586
587
588
# File 'lib/duby/jvm/source_compiler.rb', line 586

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

#branch(node, expression) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/duby/jvm/source_compiler.rb', line 321

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



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/duby/jvm/source_compiler.rb', line 304

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



468
469
470
# File 'lib/duby/jvm/source_compiler.rb', line 468

def break(node)
  @loop.break
end

#build_string(orig_nodes, expression) ⇒ Object



619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
# File 'lib/duby/jvm/source_compiler.rb', line 619

def build_string(orig_nodes, expression)
  if expression
    nodes = precompile_nodes(orig_nodes)
    simple = nodes.equal?(orig_nodes)
    if !simple
      @method.print(lvalue)
    end
    first = true
    unless nodes[0].kind_of?(Duby::AST::String)
      @method.print '""'
      first = false
    end
    nodes.each do |node|
      @method.print ' + ' unless first
      first = false
      node.compile(self, true)
    end
    @method.puts ';' unless simple
  else
    orig_nodes.each {|n| n.compile(self, false)}
  end
end

#call(call, expression) ⇒ Object



426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# File 'lib/duby/jvm/source_compiler.rb', line 426

def call(call, expression)
  if Duby::AST::Constant === call.target
    target = call.target.inferred_type.to_source
  else
    target = call.target.precompile(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



253
254
255
256
# File 'lib/duby/jvm/source_compiler.rb', line 253

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

#captured_local_assign(node, expression) ⇒ Object



258
259
260
261
262
263
# File 'lib/duby/jvm/source_compiler.rb', line 258

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



246
247
248
249
250
251
# File 'lib/duby/jvm/source_compiler.rb', line 246

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

#compile_args(call) ⇒ Object



398
399
400
# File 'lib/duby/jvm/source_compiler.rb', line 398

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

#compile_selfObject



654
655
656
# File 'lib/duby/jvm/source_compiler.rb', line 654

def compile_self
  @method.print 'this'
end

#constructor(node) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/duby/jvm/source_compiler.rb', line 79

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
        node.body.compile(self, false) if node.body
      end
      method.stop
    end
  end
end

#declare_field(name, type, annotations) ⇒ Object



181
182
183
# File 'lib/duby/jvm/source_compiler.rb', line 181

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

#declare_local(name, type) ⇒ Object



177
178
179
# File 'lib/duby/jvm/source_compiler.rb', line 177

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

#define_closure(class_def, expression) ⇒ Object



122
123
124
125
# File 'lib/duby/jvm/source_compiler.rb', line 122

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

#define_method(node) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/duby/jvm/source_compiler.rb', line 41

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

      prepare_binding(node) do
        unless @method.type.nil? || @method.type.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



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/duby/jvm/source_compiler.rb', line 65

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.type.nil? || @method.type.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.children[0].value.compile(self, true)

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

#empty_array(type, size) ⇒ Object



575
576
577
578
579
580
# File 'lib/duby/jvm/source_compiler.rb', line 575

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



165
166
167
168
169
170
171
172
# File 'lib/duby/jvm/source_compiler.rb', line 165

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)


357
358
359
# File 'lib/duby/jvm/source_compiler.rb', line 357

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

#field(name, type, annotations) ⇒ Object



190
191
192
193
194
# File 'lib/duby/jvm/source_compiler.rb', line 190

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

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



239
240
241
242
243
244
# File 'lib/duby/jvm/source_compiler.rb', line 239

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

#field_declare(name, type, annotations) ⇒ Object



229
230
231
232
# File 'lib/duby/jvm/source_compiler.rb', line 229

def field_declare(name, type, annotations)
  name = name[1..-1]
  declare_field(name, type, annotations)
end

#line(num) ⇒ Object



174
175
# File 'lib/duby/jvm/source_compiler.rb', line 174

def line(num)
end

#local(scope, name, type) ⇒ Object



185
186
187
188
# File 'lib/duby/jvm/source_compiler.rb', line 185

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

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



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/duby/jvm/source_compiler.rb', line 200

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



234
235
236
237
# File 'lib/duby/jvm/source_compiler.rb', line 234

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

#loop(loop, expression) ⇒ Object



346
347
348
349
350
351
352
353
354
355
# File 'lib/duby/jvm/source_compiler.rb', line 346

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



284
285
286
287
288
289
290
# File 'lib/duby/jvm/source_compiler.rb', line 284

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



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
# File 'lib/duby/jvm/source_compiler.rb', line 513

def method_call(target, call, params, expression)
  simple = call.expr?(self)
  method = call.method(self)
  unless simple || method.actual_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
  else
    target.compile(self, true)
    @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.actual_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



472
473
474
# File 'lib/duby/jvm/source_compiler.rb', line 472

def next(node)
  @loop.next
end

#nullObject



646
647
648
# File 'lib/duby/jvm/source_compiler.rb', line 646

def null
  @method.print 'null'
end

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



361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/duby/jvm/source_compiler.rb', line 361

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



37
38
39
# File 'lib/duby/jvm/source_compiler.rb', line 37

def output_type
  "source files"
end

#precompile_nodes(nodes) ⇒ Object



384
385
386
387
388
389
390
391
392
393
394
395
396
# File 'lib/duby/jvm/source_compiler.rb', line 384

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?(Duby::AST::Literal)
        tempval = node.temp(self)
      end
      tempval
    end
  end
end

#prepare_binding(scope) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/duby/jvm/source_compiler.rb', line 106

def prepare_binding(scope)
  if scope.has_binding?
    type = scope.binding_type
    @binding = @bindings[type]
    @method.puts "#{type.name} $binding = new #{type.name}();"
  end
  begin
    yield
  ensure
    if scope.has_binding?
      @binding.stop
      @binding = nil
    end
  end
end


658
659
660
661
662
663
664
665
666
667
668
# File 'lib/duby/jvm/source_compiler.rb', line 658

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

#redo(node) ⇒ Object



476
477
478
# File 'lib/duby/jvm/source_compiler.rb', line 476

def redo(node)
  @loop.redo
end

#regexp(value, flags = 0) ⇒ Object



590
591
592
593
594
# File 'lib/duby/jvm/source_compiler.rb', line 590

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

#rescue(node, expression) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/duby/jvm/source_compiler.rb', line 151

def rescue(node, expression)
  @method.block 'try' do
    maybe_store(node.body, expression)
  end
  node.clauses.each do |clause|
    clause.types.each do |type|
      name = clause.name || 'tmp$ex'
      @method.block "catch (#{type.to_source} #{name})" do
        maybe_store(clause.body, expression)
      end
    end
  end
end

#return(node) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/duby/jvm/source_compiler.rb', line 127

def return(node)
  if @method.type.nil? || @method.type.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



298
299
300
301
302
# File 'lib/duby/jvm/source_compiler.rb', line 298

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

#self_call(call, expression) ⇒ Object



412
413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/duby/jvm/source_compiler.rb', line 412

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

#self_typeObject



402
403
404
405
406
# File 'lib/duby/jvm/source_compiler.rb', line 402

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

#store_value(lvalue, value) ⇒ Object



265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/duby/jvm/source_compiler.rb', line 265

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



582
583
584
# File 'lib/duby/jvm/source_compiler.rb', line 582

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

#super_call(call, expression) ⇒ Object



408
409
410
# File 'lib/duby/jvm/source_compiler.rb', line 408

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



481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/duby/jvm/source_compiler.rb', line 481

def super_method_call(target, call, params, expression)
  simple = call.expr?(self)
  method = call.method(self)
  unless simple || method.actual_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.actual_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



563
564
565
566
567
568
569
570
571
572
573
# File 'lib/duby/jvm/source_compiler.rb', line 563

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

#thisObject



196
197
198
# File 'lib/duby/jvm/source_compiler.rb', line 196

def this
  @static ? @class.class_name : 'this'
end

#to_string(body, expression) ⇒ Object



642
643
644
# File 'lib/duby/jvm/source_compiler.rb', line 642

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