Class: Duby::JVM::Types::ArrayType

Inherits:
Type show all
Defined in:
lib/duby/jvm/types.rb,
lib/duby/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, #meta

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

#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, #get_method, #init_value, #inner_class_getter, #inspect, #interface?, #intrinsics, #is_parent, #java_method, #jvm_type, #load, #log, #meta, #meta?, #newarray, #prefix, #primitive?, #return, #store, #to_source, #unmeta, #void?, #wide?

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

#==, #block?, #compatible?, #eql?, #error?, #hash, #is_parent, #meta?, #narrow, #null?, #primitive?, #to_s, #unmeta, #unreachable?

Methods included from AST::Named

#to_s

Methods inherited from AST::Node

#<<, ===, #[], #_set_parent, child, child_name, #each, #empty?, #expr?, #initialize_copy, #insert, #inspect, #line_number, #log, #precompile, #resolve_if, #resolved!, #resolved?, #simple_name, #temp, #to_s

Constructor Details

#initialize(component_type) ⇒ ArrayType

Returns a new instance of ArrayType.



257
258
259
260
261
262
263
264
265
266
# File 'lib/duby/jvm/types.rb', line 257

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.



255
256
257
# File 'lib/duby/jvm/types.rb', line 255

def component_type
  @component_type
end

Instance Method Details

#add_intrinsicsObject



112
113
114
115
116
117
118
119
120
121
122
123
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
# File 'lib/duby/jvm/types/intrinsics.rb', line 112

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', Duby::AST.block_type) do |transformer, call|
    Duby::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
      [
        Duby::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:



268
269
270
# File 'lib/duby/jvm/types.rb', line 268

def array?
  true
end

#basic_typeObject



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

def basic_type
  component_type.basic_type
end

#inner_class?Boolean

Returns:



276
277
278
# File 'lib/duby/jvm/types.rb', line 276

def inner_class?
  basic_type.inner_class?
end

#interfacesObject



288
289
290
# File 'lib/duby/jvm/types.rb', line 288

def interfaces
  []
end

#iterable?Boolean

Returns:



272
273
274
# File 'lib/duby/jvm/types.rb', line 272

def iterable?
  true
end

#superclassObject



284
285
286
# File 'lib/duby/jvm/types.rb', line 284

def superclass
  Object
end