Class: Mirah::JVM::Types::ArrayType

Inherits:
Type show all
Defined in:
lib/mirah/jvm/types/array_type.rb,
lib/mirah/jvm/types/intrinsics.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 Type

#inner_class

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 inherited from Type

#abstract?, #add_compiled_macro, #add_enumerable_macros, #add_macro, #add_method, #aload, #array_type, #assignable_from?, #astore, #compatible?, #constructor, #declared_class_methods, #declared_constructors, #declared_instance_methods, #declared_intrinsics, #dynamic?, #expand_each, #field_getter, #field_setter, #full_name, #get_method, #include, #init_value, #inner_class_getter, #inspect, #interface?, #intrinsics, #is_parent, #java_method, #jvm_type, #load, #load_extensions, #log, #meta?, #newarray, #pop, #prefix, #primitive?, #return, #store, #to_source, #unmeta, #void?, #wide?, #wrap_with_scoped_body

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?, #log, #phase1, #phase2, #phase3, #primitive_convertible?

Methods inherited from AST::TypeReference

#==, #_dump, _load, #block?, #compatible?, #eql?, #error?, #full_name, #hash, #is_parent, #meta?, #narrow, #null?, #primitive?, #to_s, #type_reference, #unmeta, #unreachable?, #void?

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, #inspect_children, #line_number, #log, #precompile, #resolve_if, #resolved!, #resolved?, #simple_name, #string_value, #temp, #to_s, #top_level?, #validate_child, #validate_children

Constructor Details

#initialize(component_type) ⇒ ArrayType

Returns a new instance of ArrayType.



7
8
9
10
11
12
13
14
15
16
# File 'lib/mirah/jvm/types/array_type.rb', line 7

def initialize(component_type)
  @component_type = component_type
  if @component_type.jvm_type
    #@type = java.lang.reflect.Array.newInstance(@component_type.jvm_type, 0).class
  else
    # FIXME: THIS IS WRONG, but I don't know how to fix it
    #@type = @component_type
  end
  @name = component_type.name
end

Instance Attribute Details

#component_typeObject (readonly)

Returns the value of attribute component_type.



5
6
7
# File 'lib/mirah/jvm/types/array_type.rb', line 5

def component_type
  @component_type
end

Instance Method Details

#add_intrinsicsObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/mirah/jvm/types/intrinsics.rb', line 208

def add_intrinsics
  super
  add_enumerable_macros

  add_method(
      '[]', [Int], component_type) do |compiler, call, expression|
    if expression
      call.target.compile(compiler, true)
      call.parameters[0].compile(compiler, true)
      component_type.aload(compiler.method)
    end
  end

  add_method('[]=',
             [Int, component_type],
             component_type) do |compiler, call, expression|
    call.target.compile(compiler, true)
    convert_args(compiler, call.parameters, [Int, component_type])
    component_type.astore(compiler.method)
    if expression
      call.parameters[1].compile(compiler, true)
    end
  end

  add_method('length', [], Int) do |compiler, call, expression|
    call.target.compile(compiler, true)
    compiler.method.arraylength
  end

  add_macro('each', Mirah::AST.block_type) do |transformer, call|
    Mirah::AST::Loop.new(call.parent,
                        call.position, true, false) do |forloop|
      index = transformer.tmp
      array = transformer.tmp

      init = transformer.eval("#{index} = 0;#{array} = nil")
      array_assignment = init.children[-1]
      array_assignment.value = call.target
      call.target.parent = array_assignment
      forloop.init << init

      var = call.block.args.args[0]
      if var
        forloop.pre << transformer.eval(
            "#{var.name} = #{array}[#{index}]", '', forloop, index, array)
      end
      forloop.post << transformer.eval("#{index} += 1")
      call.block.body.parent = forloop if call.block.body
      [
        Mirah::AST::Condition.new(forloop, call.position) do |c|
          [transformer.eval("#{index} < #{array}.length",
                            '', forloop, index, array)]
        end,
        call.block.body
      ]
    end
  end
end

#array?Boolean

Returns:



18
19
20
# File 'lib/mirah/jvm/types/array_type.rb', line 18

def array?
  true
end

#basic_typeObject



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

def basic_type
  component_type.basic_type
end

#inner_class?Boolean

Returns:



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

def inner_class?
  basic_type.inner_class?
end

#interfacesObject



50
51
52
# File 'lib/mirah/jvm/types/array_type.rb', line 50

def interfaces
  []
end

#iterable?Boolean

Returns:



22
23
24
# File 'lib/mirah/jvm/types/array_type.rb', line 22

def iterable?
  true
end

#metaObject



54
55
56
# File 'lib/mirah/jvm/types/array_type.rb', line 54

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

#superclassObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mirah/jvm/types/array_type.rb', line 34

def superclass
  if component_type.primitive?
    Object
  elsif component_type.array?
    # fix covariance here for arrays of arrays
    # see #55
    Object
  else
    if component_type == Object
      Object
    else
      component_type.superclass.array_type
    end
  end
end