Class: Duby::AST::ConstructorDefinition

Inherits:
MethodDefinition show all
Defined in:
lib/duby/compiler.rb,
lib/duby/ast/method.rb

Instance Attribute Summary collapse

Attributes inherited from MethodDefinition

#defining_class

Attributes included from Scope

#static_scope

Attributes included from Named

#name

Attributes included from Annotated

#annotations

Attributes inherited from Node

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

Instance Method Summary collapse

Methods inherited from MethodDefinition

#abstract?, #name, #static?

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(*args) ⇒ ConstructorDefinition

Returns a new instance of ConstructorDefinition.



212
213
214
215
# File 'lib/duby/ast/method.rb', line 212

def initialize(*args)
  super
  extract_delegate_constructor
end

Instance Attribute Details

#calls_superObject

Returns the value of attribute calls_super.



210
211
212
# File 'lib/duby/ast/method.rb', line 210

def calls_super
  @calls_super
end

#delegate_argsObject

Returns the value of attribute delegate_args.



210
211
212
# File 'lib/duby/ast/method.rb', line 210

def delegate_args
  @delegate_args
end

Instance Method Details

#compile(compiler, expression) ⇒ Object



156
157
158
# File 'lib/duby/compiler.rb', line 156

def compile(compiler, expression)
  compiler.constructor(self)
end

#extract_delegate_constructorObject



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/duby/ast/method.rb', line 234

def extract_delegate_constructor
  # TODO verify that this constructor exists during type inference.
  possible_delegate = first_node
  if FunctionalCall === possible_delegate &&
      possible_delegate.name == 'initialize'
    @delegate_args = possible_delegate.parameters
  elsif Super === possible_delegate
    @calls_super = true
    @delegate_args = possible_delegate.parameters
    unless @delegate_args
      args = arguments.children.map {|x| x || []}
      @delegate_args = args.flatten.map do |arg|
        Local.new(self, possible_delegate.position, arg.name)
      end
    end
  end
  self.first_node = Noop.new(self, position) if @delegate_args
end

#first_nodeObject



217
218
219
220
221
222
223
# File 'lib/duby/ast/method.rb', line 217

def first_node
  if body.kind_of? Body
    body.children[0]
  else
    body
  end
end

#first_node=(new_node) ⇒ Object



225
226
227
228
229
230
231
232
# File 'lib/duby/ast/method.rb', line 225

def first_node=(new_node)
  if body.kind_of? Body
    new_node.parent = body
    body.children[0] = new_node
  else
    self.body = new_node
  end
end

#infer(typer) ⇒ Object



253
254
255
256
257
258
# File 'lib/duby/ast/method.rb', line 253

def infer(typer)
  unless @inferred_type
    delegate_args.each {|a| typer.infer(a)} if delegate_args
  end
  super
end