Class: Duby::AST::FunctionalCall

Inherits:
Node show all
Includes:
Named
Defined in:
lib/duby/ast/call.rb,
lib/duby/compiler.rb,
lib/duby/jvm/compiler.rb,
lib/duby/jvm/source_generator/precompile.rb

Instance Attribute Summary collapse

Attributes included from Named

#name

Attributes inherited from Node

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

Instance Method Summary collapse

Methods included from Named

#to_s

Methods inherited from Node

#[], #each, #inspect, #line_number, #log, #precompile, #resolve_if, #resolved!, #resolved?, #simple_name, #temp, #to_s

Constructor Details

#initialize(parent, line_number, name, &kids) ⇒ FunctionalCall

Returns a new instance of FunctionalCall.



7
8
9
10
11
12
# File 'lib/duby/ast/call.rb', line 7

def initialize(parent, line_number, name, &kids)
  super(parent, line_number, &kids)
  @parameters, @block = children
  @name = name
  @cast = false
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



4
5
6
# File 'lib/duby/ast/call.rb', line 4

def block
  @block
end

#castObject Also known as: cast?

Returns the value of attribute cast.



4
5
6
# File 'lib/duby/ast/call.rb', line 4

def cast
  @cast
end

#parametersObject

Returns the value of attribute parameters.



4
5
6
# File 'lib/duby/ast/call.rb', line 4

def parameters
  @parameters
end

#targetObject

Returns the value of attribute target.



11
12
13
# File 'lib/duby/jvm/compiler.rb', line 11

def target
  @target
end

Instance Method Details

#compile(compiler, expression) ⇒ Object



139
140
141
142
# File 'lib/duby/compiler.rb', line 139

def compile(compiler, expression)
  compiler.line(line_number)
  compiler.self_call(self, expression)
end

#expr?(compiler) ⇒ Boolean

Returns:



92
93
94
95
# File 'lib/duby/jvm/source_generator/precompile.rb', line 92

def expr?(compiler)
  parameters.all? {|p| p.expr?(compiler)} &&
      (cast? || !method(compiler).actual_return_type.void?)
end

#infer(typer) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/duby/ast/call.rb', line 14

def infer(typer)
  @self_type ||= typer.self_type

  unless @inferred_type
    receiver_type = @self_type
    should_defer = false
    
    parameter_types = parameters.map do |param|
      typer.infer(param) || should_defer = true
    end
    
    unless should_defer
      if parameters.size == 1 && typer.known_types[name]
        # cast operation
        resolved!
        self.cast = true
        @inferred_type = typer.known_types[name]
      else
        @inferred_type = typer.method_type(receiver_type, name,
                                           parameter_types)
      end
    end
    
    @inferred_type ? resolved! : typer.defer(self)
  end
    
  @inferred_type
end

#method(compiler) ⇒ Object



85
86
87
88
89
90
# File 'lib/duby/jvm/source_generator/precompile.rb', line 85

def method(compiler)
  @method ||= begin
    arg_types = parameters.map {|p| p.inferred_type}
    compiler.self_type.get_method(name, arg_types)
  end
end