Class: Duby::Typer::JVM

Inherits:
Simple show all
Includes:
JVM::Types
Defined in:
lib/duby/jvm/typer.rb

Direct Known Subclasses

JavaSource

Constant Summary

Constants included from JVM::Types

JVM::Types::Boolean, JVM::Types::Byte, JVM::Types::Char, JVM::Types::Double, JVM::Types::Float, JVM::Types::Int, JVM::Types::Long, JVM::Types::Null, JVM::Types::Object, JVM::Types::Short, JVM::Types::String, JVM::Types::TYPE_ORDERING, JVM::Types::Void

Constants included from Duby

Duby::TransformError, VERSION

Instance Attribute Summary

Attributes inherited from Simple

#errors, #known_types

Instance Method Summary collapse

Methods inherited from Simple

#alias_type, #boolean_type, #cycle, #cycling=, #cycling?, #default_type, #defer, #deferred_nodes, #define_type, #error, #field_type, #field_type_hash, #field_types, #fixnum_type, #float_type, #get_method_type_hash, #infer, #learn_field_type, #learn_local_type, #local_type, #local_type_hash, #local_types, #method_type, #method_types, #plugins, #resolve, #self_type, #string_type

Methods inherited from BaseTyper

#log, #to_s

Methods included from Duby

compile, parse, run, typer_plugins

Constructor Details

#initialize(filename) ⇒ JVM

Returns a new instance of JVM.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/duby/jvm/typer.rb', line 10

def initialize(filename)
  @factory = AST.type_factory
  unless @factory.kind_of? TypeFactory
    raise "TypeFactory not installed"
  end
  @known_types = @factory.known_types
  classname = File.basename(filename, '.duby')
  main_class = @factory.declare_type(classname)
  @known_types['self'] = main_class.meta
  @errors = []
end

Instance Method Details

#alias_types(short, long) ⇒ Object



26
27
28
# File 'lib/duby/jvm/typer.rb', line 26

def alias_types(short, long)
  @known_types[short] = type_reference(long)
end

#infer_signature(method_def) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
110
111
112
113
114
115
# File 'lib/duby/jvm/typer.rb', line 59

def infer_signature(method_def)
  signature = method_def.signature
  sig_args = signature.dup
  return_type = sig_args.delete(:return)
  exceptions = sig_args.delete(:throws)
  args = method_def.arguments.args || []
  static = method_def.kind_of? Duby::AST::StaticMethodDefinition
  if sig_args.size != args.size
    # If the superclass declares one method with the same name and
    # same number of arguments, assume we're overriding it.
    found = nil
    ambiguous = false
    classes = [self_type.superclass] + self_type.interfaces
    while classes.size > 0
      cls = classes.pop
      if static
        methods = cls.declared_class_methods
      else
        methods = cls.declared_instance_methods
      end
      methods.each do |method|
        if method.name == method_def.name &&
           method.argument_types.size == args.size
          if found && found.argument_types != method.argument_types
            ambiguous = true
          else
            found ||= method
          end
        end
      end
      classes << cls.superclass if cls.superclass
    end
    if found && !ambiguous
      signature[:return] = found.actual_return_type
      signature[:throws] = found.exceptions
      args.zip(found.argument_types) do |arg, type|
        signature[arg.name.intern] = type
      end
    end
  elsif signature[:return].nil? && !static
    arg_types = args.map do |arg|
      signature[arg.name.intern]
    end
    method = self_type.find_method(
        self_type, method_def.name, arg_types, false)
    interfaces = self_type.interfaces.dup
    until method || interfaces.empty?
      interface = interfaces.pop
      method = interface.find_method(
          interface, method_def.name, arg_types, false)
    end
    if method
      signature[:return] = method.actual_return_type
      signature[:throws] = method.exceptions
    end
  end
end

#learn_method_type(target_type, name, parameter_types, type, exceptions) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/duby/jvm/typer.rb', line 46

def learn_method_type(target_type, name, parameter_types, type, exceptions)
  static = target_type.meta?
  unless target_type.unmeta.kind_of?(TypeDefinition)
    raise "Method defined on #{target_type}"
  end
  if static
    target_type.unmeta.declare_static_method(name, parameter_types, type, exceptions)
  else
    target_type.declare_method(name, parameter_types, type, exceptions)
  end
  super
end

#nameObject



30
31
32
# File 'lib/duby/jvm/typer.rb', line 30

def name
  "JVM"
end

#no_typeObject



42
43
44
# File 'lib/duby/jvm/typer.rb', line 42

def no_type
  Void
end

#null_typeObject



38
39
40
# File 'lib/duby/jvm/typer.rb', line 38

def null_type
  Null
end

#type_definition(name, superclass, interfaces) ⇒ Object



34
35
36
# File 'lib/duby/jvm/typer.rb', line 34

def type_definition(name, superclass, interfaces)
  @known_types[name]
end

#type_reference(name, array = false, meta = false) ⇒ Object



22
23
24
# File 'lib/duby/jvm/typer.rb', line 22

def type_reference(name, array=false, meta=false)
  @factory.type(name, array, meta)
end