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::Iterable, JVM::Types::Long, JVM::Types::Null, JVM::Types::Object, JVM::Types::Short, JVM::Types::String, JVM::Types::TYPE_ORDERING, JVM::Types::Void, JVM::Types::WIDENING_CONVERSIONS

Constants included from Duby

Duby::TransformError

Instance Attribute Summary collapse

Attributes inherited from Simple

#errors, #known_types, #last_chance

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, plugins, print_error, reset, run, typer_plugins

Constructor Details

#initialize(filename, transformer) ⇒ JVM

Returns a new instance of JVM.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/duby/jvm/typer.rb', line 12

def initialize(filename, transformer)
  @factory = AST.type_factory
  @transformer = transformer
  unless @factory.kind_of? TypeFactory
    raise "TypeFactory not installed"
  end
  @known_types = @factory.known_types
  classname = Duby::Compiler::JVM.classname_from_filename(filename)
  main_class = @factory.declare_type(classname)
  @known_types['self'] = main_class.meta
  @known_types['dynamic'] = DynamicType.new
  @errors = []
end

Instance Attribute Details

#transformerObject (readonly)

Returns the value of attribute transformer.



10
11
12
# File 'lib/duby/jvm/typer.rb', line 10

def transformer
  @transformer
end

Instance Method Details

#alias_types(short, long) ⇒ Object



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

def alias_types(short, long)
  @factory.alias(short, long)
end

#array_typeObject



50
51
52
53
# File 'lib/duby/jvm/typer.rb', line 50

def array_type
  # TODO: allow other types for pre-1.2 profiles
  type_reference("java.util.List")
end

#hash_typeObject



55
56
57
58
# File 'lib/duby/jvm/typer.rb', line 55

def hash_type
  # TODO: allow other types for pre-1.2 profiles
  type_reference("java.util.Map")
end

#infer_signature(method_def) ⇒ Object



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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/duby/jvm/typer.rb', line 81

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

#known_type(name) ⇒ Object



64
65
66
# File 'lib/duby/jvm/typer.rb', line 64

def known_type(name)
  @factory.known_type(name)
end

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



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/duby/jvm/typer.rb', line 68

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



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

def name
  "JVM"
end

#no_typeObject



46
47
48
# File 'lib/duby/jvm/typer.rb', line 46

def no_type
  Void
end

#null_typeObject



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

def null_type
  Null
end

#regexp_typeObject



60
61
62
# File 'lib/duby/jvm/typer.rb', line 60

def regexp_type
  type_reference("java.util.regex.Pattern")
end

#type_definition(name, superclass, interfaces) ⇒ Object



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

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

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



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

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