Class: Duby::AST::MethodDefinition

Inherits:
Node
  • Object
show all
Includes:
Annotated, Binding, Named, Scope
Defined in:
lib/duby/compiler.rb,
lib/duby/ast/method.rb

Instance Attribute Summary collapse

Attributes included from Scope

#static_scope

Attributes included from Annotated

#annotations

Attributes inherited from Node

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

Instance Method Summary collapse

Methods included from Binding

#binding_type, #binding_type=, #has_binding?

Methods included from Named

#to_s

Methods included from Annotated

#annotation

Methods inherited from 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(parent, line_number, name, annotations = [], &block) ⇒ MethodDefinition

Returns a new instance of MethodDefinition.



127
128
129
130
131
# File 'lib/duby/ast/method.rb', line 127

def initialize(parent, line_number, name, annotations=[], &block)
  @annotations = annotations
  super(parent, line_number, &block)
  @name = name
end

Instance Attribute Details

#defining_classObject

Returns the value of attribute defining_class.



125
126
127
# File 'lib/duby/ast/method.rb', line 125

def defining_class
  @defining_class
end

Instance Method Details

#abstract?Boolean

Returns:



186
187
188
189
190
191
192
# File 'lib/duby/ast/method.rb', line 186

def abstract?
  node = parent
  while node && !node.kind_of?(Scope)
    node = node.parent
  end
  InterfaceDeclaration === node
end

#compile(compiler, expression) ⇒ Object



149
150
151
152
# File 'lib/duby/compiler.rb', line 149

def compile(compiler, expression)
  # TODO: what does it mean for a method to be an expression?
  compiler.define_method(self)
end

#infer(typer) ⇒ Object



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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/duby/ast/method.rb', line 137

def infer(typer)
  @defining_class ||= typer.self_type
  typer.infer(arguments)
  typer.infer_signature(self)
  forced_type = signature[:return]
  inferred_type = body ? typer.infer(body) : typer.no_type

  if !(inferred_type && arguments.inferred_type.all?)
    typer.defer(self)
  else
    actual_type = if forced_type.nil?
      inferred_type
    else
      forced_type
    end
    if actual_type.unreachable?
      actual_type = typer.no_type
    end

    if !abstract? &&
        forced_type != typer.no_type &&
        !actual_type.is_parent(inferred_type)
      raise Duby::Typer::InferenceError.new(
          "Inferred return type %s is incompatible with declared %s" %
          [inferred_type, actual_type], self)
    end

    @inferred_type = typer.learn_method_type(defining_class, name, arguments.inferred_type, actual_type, signature[:throws])

    # learn the other overloads as well
    args_for_opt = []
    if arguments.args
      arguments.args.each do |arg|
        if OptionalArgument === arg
          arg_types_for_opt = args_for_opt.map do |arg_for_opt|
            arg_for_opt.infer(typer)
          end
          typer.learn_method_type(defining_class, name, arg_types_for_opt, actual_type, signature[:throws])
        end
        args_for_opt << arg
      end
    end

    signature[:return] = @inferred_type
  end

  @inferred_type
end

#nameObject



133
134
135
# File 'lib/duby/ast/method.rb', line 133

def name
  super
end

#static?Boolean

Returns:



194
195
196
# File 'lib/duby/ast/method.rb', line 194

def static?
  false
end