Class: Mirah::JVM::Types::Type

Inherits:
AST::TypeReference show all
Includes:
Java::DubyLangCompiler::Class, MethodLookup
Defined in:
lib/mirah/jvm/types/type.rb,
lib/mirah/jvm/types/enumerable.rb,
lib/mirah/jvm/types/extensions.rb,
lib/mirah/jvm/source_generator/builder.rb,
lib/mirah/jvm/types/intrinsics.rb,
lib/mirah/jvm/types/methods.rb

Constant Summary

Constants inherited from AST::TypeReference

AST::TypeReference::BlockType, AST::TypeReference::ErrorType, AST::TypeReference::NoType, AST::TypeReference::NullType, AST::TypeReference::UnreachableType

Instance Attribute Summary collapse

Attributes inherited from AST::TypeReference

#array

Attributes included from AST::Named

#name

Attributes inherited from AST::Node

#children, #inferred_type, #newline, #parent, #position

Instance Method Summary collapse

Methods included from MethodLookup

#each_is_exact, #each_is_exact_or_subtype_or_convertible, #field_lookup, #find_jls, #find_method, #inner_class, #is_more_specific?, #phase1, #phase2, #phase3, #primitive_convertible?

Methods inherited from AST::TypeReference

#==, #_dump, _load, #block?, #eql?, #error?, #hash, #narrow, #null?, #to_s, #type_reference, #unreachable?

Methods included from AST::Named

#string_value, #to_s, #validate_name

Methods inherited from AST::Node

#<<, ===, #[], #[]=, #_dump, _load, #_set_parent, child, child_name, #child_nodes, #each, #empty?, #expr?, #inferred_type!, #initialize_copy, #insert, #inspect_children, #line_number, #precompile, #resolve_if, #resolved!, #resolved?, #simple_name, #string_value, #temp, #to_s, #top_level?, #validate_child, #validate_children

Constructor Details

#initialize(mirror_or_name) ⇒ Type

Returns a new instance of Type.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
# File 'lib/mirah/jvm/types/type.rb', line 14

def initialize(mirror_or_name)
  if mirror_or_name.kind_of?(BiteScript::ASM::ClassMirror)
    @type = mirror_or_name
    name = mirror_or_name.type.class_name
  else
    name = mirror_or_name.to_s
  end
  super(name, false, false)
  raise ArgumentError, "Bad type #{mirror_or_name}" if name =~ /Java::/
end

Instance Attribute Details

#inner_class=(value) ⇒ Object (writeonly)

Sets the attribute inner_class

Parameters:

  • value

    the value to set the attribute inner_class to.



8
9
10
# File 'lib/mirah/jvm/types/type.rb', line 8

def inner_class=(value)
  @inner_class = value
end

Instance Method Details

#add_compiled_macro(klass, name, arg_types) ⇒ Object



75
76
77
78
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
105
106
107
108
109
# File 'lib/mirah/jvm/types/intrinsics.rb', line 75

def add_compiled_macro(klass, name, arg_types)
  add_macro(name, *arg_types) do |mirah, call|
    # Ick. We need to preserve the scope of the arguments to the macro.
    # However the only way to do that is to wrap them in a ScopedBody.
    # It'd be better if we didn't have to expose this wrapper node to
    # the user code.
    #
    if call.block && call.name != 'quote'
      call.block.body = wrap_with_scoped_body call, call.block.body
    end
    
    call.parameters = call.parameters.map do |arg|
      wrap_with_scoped_body call, arg
    end
    
    expander = klass.constructors[0].newInstance(mirah, call)
    ast = expander.expand
    if ast
      body = Mirah::AST::ScopedBody.new(call.parent, call.position)
      body << ast
      
      # if the macro was called on something, and that something
      # was not a class only containing macros, reassign self for
      # the body.
      if call.target && !call.target.is_a?(Mirah::AST::Builtin)
        body.static_scope.self_type = call.target.inferred_type!
        body.static_scope.self_node = call.target
      end
      
      body
    else
      Mirah::AST::Noop.new(call.parent, call.position)
    end
  end
end

#add_enumerable_macrosObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/mirah/jvm/types/enumerable.rb', line 24

def add_enumerable_macros
  all_proc = proc do |transformer, call|
    if !call.block
      # We need to create a block that just returns the item passed to it
      # First get a new temp for the block argument
      var = transformer.tmp
      # Parse a fake function call to create a block. Then pull of the
      # block and attach it to the real call.
      call.block = transformer.eval("foo {|#{var}| #{var}}").block
    end
    
    # Now that we've got a block we can transform it into a Loop.
    forloop = expand_each(transformer, call)
    
    # Start adding stuff to the loop.
    # At the beginning of the loop we create a temp initialized to true
    all = transformer.tmp
    forloop.init << transformer.eval("#{all} = true")
    
    # Now we want to wrap the body of the loop. Start off by using
    # foo as a placeholder.
    body = transformer.eval(
        "unless foo;#{all} = false;break;end", '', forloop)
    # Then replace foo with the real body.
    body.condition.predicate = call.block.body
    # And finally patch the new body back into the forloop.
    forloop.body = call.block.body.parent = body
    
    # Loops don't have a return value, so we need somewhere to
    # put the result.
    result = Mirah::AST::Body.new(call.parent, call.position)
    result << forloop << transformer.eval("#{all}", '', nil, all)
  end
  add_macro('all?', &all_proc)
  add_macro('all?', Mirah::AST.block_type, &all_proc)

  any_proc = proc do |transformer, call|
    if !call.block
      var = transformer.tmp
      call.block = transformer.eval("foo {|#{var}| #{var}}").block
    end
    forloop = expand_each(transformer, call)
    any = transformer.tmp
    forloop.init << transformer.eval("#{any} = false")
    body = transformer.eval(
        "if foo;#{any} = true;break;end", '', forloop)
    body.condition.predicate = call.block.body
    forloop.body = call.block.body.parent = body
    
    result = Mirah::AST::Body.new(call.parent, call.position)
    result << forloop << transformer.eval("#{any}", '', nil, any)
  end
  add_macro('any?', &any_proc)
  add_macro('any?', Mirah::AST.block_type, &any_proc)
end

#add_intrinsicsObject



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
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/mirah/jvm/types/intrinsics.rb', line 166

def add_intrinsics
  add_method('nil?', [], Boolean) do |compiler, call, expression|
    if expression
      call.target.compile(compiler, true)
      compiler.method.op_to_bool do |target|
        compiler.method.ifnull(target)
      end
    end
  end

  add_method('==', [Object], Boolean) do |compiler, call, expression|
    # Should this call Object.equals for consistency with Ruby?
    if expression
      call.target.compile(compiler, true)
      call.parameters[0].compile(compiler, true)
      compiler.method.op_to_bool do |target|
        compiler.method.if_acmpeq(target)
      end
    end
  end

  add_method('!=', [Object], Boolean) do |compiler, call, expression|
    # Should this call Object.equals for consistency with Ruby?
    if expression
      call.target.compile(compiler, true)
      call.parameters[0].compile(compiler, true)
      compiler.method.op_to_bool do |target|
        compiler.method.if_acmpne(target)
      end
    end
  end

  add_macro('kind_of?', ClassType) do |transformer, call|
    klass, object = call.parameters[0], call.target
    Mirah::AST::Call.new(call.parent, call.position, 'isInstance') do |call2|
      klass.parent = object.parent = call2
      [
        klass,
        [object]
      ]
    end
  end

  add_method('kind_of?', [Object.meta], Boolean) do |compiler, call, expression|
    call.target.compile(compiler, expression)
    if expression
      klass = call.parameters[0].inferred_type!
      compiler.method.instanceof(klass.unmeta)
    end
  end
end

#add_macro(name, *args, &block) ⇒ Object



68
69
70
71
72
73
# File 'lib/mirah/jvm/types/intrinsics.rb', line 68

def add_macro(name, *args, &block)
  type = Mirah::AST::InlineCode.new(&block)
  intrinsics[name][args] = Intrinsic.new(self, name, args, type) do
    raise "Macro should be expanded, no called!"
  end
end

#add_method(name, args, method_or_type = nil, &block) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/mirah/jvm/types/intrinsics.rb', line 60

def add_method(name, args, method_or_type=nil, &block)
  if block_given?
    method_or_type = Intrinsic.new(self, name, args,
                                   method_or_type, &block)
  end
  intrinsics[name][args] = method_or_type
end

#aload(builder) ⇒ Object



163
164
165
166
167
168
169
# File 'lib/mirah/jvm/types/type.rb', line 163

def aload(builder)
  if primitive?
    builder.send "#{name[0,1]}aload"
  else
    builder.aaload
  end
end

#array?Boolean

Returns:



42
43
44
# File 'lib/mirah/jvm/types/type.rb', line 42

def array?
  false
end

#array_typeObject



109
110
111
# File 'lib/mirah/jvm/types/type.rb', line 109

def array_type
  @array_type ||= Mirah::JVM::Types::ArrayType.new(self)
end

#assignable_from?(other) ⇒ Boolean

Returns:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/mirah/jvm/types/type.rb', line 72

def assignable_from?(other)
  return false if other.nil?
  return true if !primitive? && other == Null
  return true if other == self
  return true if other.error? || other.unreachable?

  # TODO should we allow more here?
  return interface? if other.block?

  return true if jvm_type && (jvm_type == other.jvm_type)

  assignable_from?(other.superclass) ||
      other.interfaces.any? {|i| assignable_from?(i)}
end

#astore(builder) ⇒ Object



155
156
157
158
159
160
161
# File 'lib/mirah/jvm/types/type.rb', line 155

def astore(builder)
  if primitive?
    builder.send "#{name[0,1]}astore"
  else
    builder.aastore
  end
end

#basic_typeObject



105
106
107
# File 'lib/mirah/jvm/types/type.rb', line 105

def basic_type
  self
end

#compatible?(other) ⇒ Boolean

Returns:



68
69
70
# File 'lib/mirah/jvm/types/type.rb', line 68

def compatible?(other)
  assignable_from?(other)
end

#component_typeObject



93
94
95
# File 'lib/mirah/jvm/types/type.rb', line 93

def component_type
  AST.type(nil, 'java.lang.Object') if iterable?
end

#constructor(*types) ⇒ Object

Raises:

  • (NameError)


406
407
408
409
410
411
412
413
414
415
# File 'lib/mirah/jvm/types/methods.rb', line 406

def constructor(*types)
  begin
    descriptors = types.map {|type| BiteScript::Signature.class_id(type)}
    constructor = jvm_type.getConstructor(*descriptors)
    return JavaConstructor.new(constructor) if constructor
  rescue => ex
    log(ex.message)
  end
  raise NameError, "No constructor #{name}(#{types.join ', '})"
end

#declared_class_methods(name = nil) ⇒ Object



460
461
462
463
464
465
466
467
468
# File 'lib/mirah/jvm/types/methods.rb', line 460

def declared_class_methods(name=nil)
  methods = []
  if jvm_type && !unmeta.array?
    jvm_type.getDeclaredMethods(name).each do |method|
      methods << JavaStaticMethod.new(method) if method.static?
    end
  end
  methods.concat(meta.declared_intrinsics(name))
end

#declared_constructorsObject



470
471
472
473
474
# File 'lib/mirah/jvm/types/methods.rb', line 470

def declared_constructors
  jvm_type.getConstructors.map do |method|
    JavaConstructor.new(method)
  end
end

#declared_instance_methods(name = nil) ⇒ Object



450
451
452
453
454
455
456
457
458
# File 'lib/mirah/jvm/types/methods.rb', line 450

def declared_instance_methods(name=nil)
  methods = []
  if jvm_type && !array?
    jvm_type.getDeclaredMethods(name).each do |method|
      methods << JavaMethod.new(method) unless method.static?
    end
  end
  methods.concat((meta? ? unmeta : self).declared_intrinsics(name))
end

#declared_intrinsics(name = nil) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/mirah/jvm/types/intrinsics.rb', line 117

def declared_intrinsics(name=nil)
  methods = []
  all_intrinsics = if name.nil?
    intrinsics
  else
    [[name, intrinsics[name]]]
  end
  all_intrinsics.each do |name, group|
    group.each do |args, method|
      methods << method
    end
  end
  interfaces.each do |interface|
    methods.concat(interface.declared_intrinsics(name))
  end
  methods
end

#dynamic?Boolean

Returns:



56
57
58
# File 'lib/mirah/jvm/types/type.rb', line 56

def dynamic?
  false
end

#expand_each(transformer, call) ⇒ Object



18
19
20
21
22
# File 'lib/mirah/jvm/types/enumerable.rb', line 18

def expand_each(transformer, call)
  arg_types = [Mirah::AST.block_type]
  code = intrinsics['each'][arg_types].return_type
  code.inline(transformer, call)
end

#field_getter(name) ⇒ Object



476
477
478
479
480
481
482
483
# File 'lib/mirah/jvm/types/methods.rb', line 476

def field_getter(name)
  if jvm_type
    field = jvm_type.getField(name)
    JavaFieldGetter.new(field) if field
  else
    nil
  end
end

#field_setter(name) ⇒ Object



485
486
487
488
489
490
491
492
# File 'lib/mirah/jvm/types/methods.rb', line 485

def field_setter(name)
  if jvm_type
    field = jvm_type.getField(name)
    JavaFieldSetter.new(field) if field
  else
    nil
  end
end

#full_nameObject



25
26
27
28
# File 'lib/mirah/jvm/types/type.rb', line 25

def full_name
  desc = BiteScript::Signature.class_id(self)
  BiteScript::ASM::Type.get_type(desc).class_name
end

#get_method(name, args) ⇒ Object



393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/mirah/jvm/types/methods.rb', line 393

def get_method(name, args)
  method = find_method(self, name, args, meta?)
  unless method
    # Allow constant narrowing for assignment methods
    if name =~ /=$/ && args[-1].respond_to?(:narrow!)
      if args[-1].narrow!
        method = find_method(self, name, args, meta?)
      end
    end
  end
  method
end

#include(type) ⇒ Object



97
98
99
# File 'lib/mirah/jvm/types/extensions.rb', line 97

def include(type)
  ExtendedType.new(self).include(type)
end

#init_value(builder) ⇒ Object



48
49
50
# File 'lib/mirah/jvm/types/intrinsics.rb', line 48

def init_value(builder)
  builder.aconst_null
end

#inner_class?Boolean

Returns:



60
61
62
# File 'lib/mirah/jvm/types/type.rb', line 60

def inner_class?
  @inner_class
end

#inner_class_getter(name) ⇒ Object



494
495
496
497
498
499
500
501
502
503
# File 'lib/mirah/jvm/types/methods.rb', line 494

def inner_class_getter(name)
  full_name = "#{self.name}$#{name}"
  inner_class = Mirah::AST.type(nil, full_name) rescue nil
  return unless inner_class
  inner_class.inner_class = true
  add_macro(name) do |transformer, call|
    Mirah::AST::Constant.new(call.parent, call.position, full_name)
  end
  intrinsics[name][[]]
end

#inspect(indent = 0) ⇒ Object



122
123
124
# File 'lib/mirah/jvm/types/type.rb', line 122

def inspect(indent=0)
  "#{' ' * indent}#<#{self.class.name} #{name}>"
end

#interface?Boolean

Returns:



50
51
52
53
54
# File 'lib/mirah/jvm/types/type.rb', line 50

def interface?
  # FIXME: Don't do rescue nil. Figure out a cleaner way to handle
  # mirrors for all incoming types without blowing up on e.g. 'boolean' or 'int'
  (@type || BiteScript::ASM::ClassMirror.for_name(@name)).interface? rescue nil
end

#interfacesObject



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/mirah/jvm/types/type.rb', line 143

def interfaces
  raise "Incomplete type #{self} (#{self.class})" unless jvm_type
  @interfaces ||= begin
    interfaces = jvm_type.interfaces.map {|i| AST.type(nil, i)}.to_set
    if superclass
      interfaces |= superclass.interfaces
    end
    interfaces.to_a
  end
  @interfaces
end

#intrinsicsObject



52
53
54
55
56
57
58
# File 'lib/mirah/jvm/types/intrinsics.rb', line 52

def intrinsics
  @intrinsics ||= begin
    @intrinsics = Hash.new {|h, k| h[k] = {}}
    add_intrinsics
    @intrinsics
  end
end

#is_parent(other) ⇒ Object



64
65
66
# File 'lib/mirah/jvm/types/type.rb', line 64

def is_parent(other)
  assignable_from?(other)
end

#iterable?Boolean

Returns:



87
88
89
90
91
# File 'lib/mirah/jvm/types/type.rb', line 87

def iterable?
  ['java.lang.Iterable',
   'java.util.Iterator',
   'java.util.Enumeration'].any? {|n| AST.type(nil, n).assignable_from(self)}
end

#java_method(name, *types) ⇒ Object

Raises:

  • (NameError)


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
# File 'lib/mirah/jvm/types/methods.rb', line 417

def java_method(name, *types)
  intrinsic = intrinsics[name][types]
  return intrinsic if intrinsic
  jvm_types = types.map {|type| type.jvm_type}

  return JavaDynamicMethod.new(name, *jvm_types) if dynamic?

  begin
    descriptors = types.map {|type| BiteScript::Signature.class_id(type)}
    method = jvm_type.getDeclaredMethod(name, *descriptors) if jvm_type

    if method.nil? && superclass
      method = superclass.java_method(name, *types) rescue nil
    end

    if method.nil? && jvm_type.abstract?
      interfaces.each do |interface|
        method = interface.java_method(name, *types) rescue nil
        break if method
      end
    end

    return method if method.kind_of?(JavaCallable)
    if method && method.static? == meta?
      return JavaStaticMethod.new(method) if method.static?
      return JavaMethod.new(method)
    end
  rescue   => ex
    log(ex.message)
  end
  raise NameError, "No method #{self.name}.#{name}(#{types.join ', '})"
end

#jvm_typeObject



30
31
32
# File 'lib/mirah/jvm/types/type.rb', line 30

def jvm_type
  @type
end

#load(builder, index) ⇒ Object



36
37
38
# File 'lib/mirah/jvm/types/intrinsics.rb', line 36

def load(builder, index)
  builder.send "#{prefix}load", index
end

#load_extensions(klass = nil) ⇒ Object



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
# File 'lib/mirah/jvm/types/intrinsics.rb', line 135

def load_extensions(klass=nil)
  mirror = nil
  if klass
    factory = Mirah::AST.type_factory
    mirror = factory.get_mirror(klass.getName)
  elsif jvm_type
    mirror = jvm_type
  end
  if mirror
    extensions = mirror.getDeclaredAnnotation('duby.anno.Extensions')
    return self if extensions.nil?
    macros = extensions['macros']
    return self if macros.nil?
    macros.each do |macro|
      macro_name = macro['name']
      class_name = macro['class']
      types = BiteScript::ASM::Type.get_argument_types(macro['signature'])
      args = types.map do |type|
        if type.class_name == 'duby.lang.compiler.Block'
          Mirah::AST::TypeReference::BlockType
        else
          Mirah::AST.type(nil, type)
        end
      end
      klass = JRuby.runtime.jruby_class_loader.loadClass(class_name)
      add_compiled_macro(klass, macro_name, args)
    end
  end
  self
end

#log(message) ⇒ Object



10
11
12
# File 'lib/mirah/jvm/types/type.rb', line 10

def log(message)
  puts "* [JVM::Types] #{message}" if Mirah::JVM::Compiler::JVMBytecode.verbose
end

#metaObject



97
98
99
# File 'lib/mirah/jvm/types/type.rb', line 97

def meta
  @meta ||= MetaType.new(self)
end

#meta?Boolean

Returns:



38
39
40
# File 'lib/mirah/jvm/types/type.rb', line 38

def meta?
  false
end

#newarray(method) ⇒ Object



126
127
128
# File 'lib/mirah/jvm/types/type.rb', line 126

def newarray(method)
  method.anewarray(self)
end

#pop(method) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/mirah/jvm/types/type.rb', line 130

def pop(method)
  if wide?
    method.pop2
  else
    method.pop
  end
end

#prefixObject



113
114
115
# File 'lib/mirah/jvm/types/type.rb', line 113

def prefix
  'a'
end

#primitive?Boolean

Returns:



46
47
48
# File 'lib/mirah/jvm/types/type.rb', line 46

def primitive?
  false
end

#return(builder) ⇒ Object



44
45
46
# File 'lib/mirah/jvm/types/intrinsics.rb', line 44

def return(builder)
  builder.send "#{prefix}return"
end

#store(builder, index) ⇒ Object



40
41
42
# File 'lib/mirah/jvm/types/intrinsics.rb', line 40

def store(builder, index)
  builder.send "#{prefix}store", index
end

#superclassObject



138
139
140
141
# File 'lib/mirah/jvm/types/type.rb', line 138

def superclass
  raise "Incomplete type #{self}" unless jvm_type
  AST.type(nil, jvm_type.superclass) if jvm_type.superclass
end

#to_sourceObject



20
21
22
23
24
# File 'lib/mirah/jvm/source_generator/builder.rb', line 20

def to_source
  java_name = name
  java_name = java_name.tr('$', '.')
  "#{java_name}#{'[]' if array?}"
end

#unmetaObject



101
102
103
# File 'lib/mirah/jvm/types/type.rb', line 101

def unmeta
  self
end

#void?Boolean

Returns:



34
35
36
# File 'lib/mirah/jvm/types/type.rb', line 34

def void?
  false
end

#wide?Boolean

is this a 64 bit type?

Returns:



118
119
120
# File 'lib/mirah/jvm/types/type.rb', line 118

def wide?
  false
end

#wrap_with_scoped_body(call, node) ⇒ Object



111
112
113
114
115
# File 'lib/mirah/jvm/types/intrinsics.rb', line 111

def wrap_with_scoped_body call, node
  wrapper = Mirah::AST::ScopedBody.new(call.parent, call.position)
  wrapper.static_scope = call.scope.static_scope
  wrapper << node
end