Class: Mirah::AST::ConstructorDefinition

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

Instance Attribute Summary collapse

Attributes inherited from MethodDefinition

#abstract, #defining_class, #exceptions, #return_type, #visibility

Attributes included from Scope

#static_scope, #type_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, #resolve_if, #static?

Methods included from Binding

#binding_type, #binding_type=, #has_binding?

Methods included from ClassScoped

#class_scope

Methods included from Scoped

#containing_scope, #scope

Methods included from Named

#string_value, #to_s, #validate_name

Methods included from Annotated

#annotation

Methods inherited from Node

#<<, ===, #[], #[]=, #_dump, _load, #_set_parent, child, child_name, #child_nodes, #each, #empty?, #expr?, #inferred_type!, #initialize_copy, #insert, #inspect, #inspect_children, #line_number, #log, #precompile, #resolve_if, #resolved!, #resolved?, #simple_name, #string_value, #temp, #to_s, #top_level?, #validate_child

Constructor Details

#initialize(*args) ⇒ ConstructorDefinition

Returns a new instance of ConstructorDefinition.



336
337
338
339
# File 'lib/mirah/ast/method.rb', line 336

def initialize(*args)
  super
  extract_delegate_constructor
end

Instance Attribute Details

#calls_superObject

Returns the value of attribute calls_super.



334
335
336
# File 'lib/mirah/ast/method.rb', line 334

def calls_super
  @calls_super
end

#delegate_argsObject

Returns the value of attribute delegate_args.



334
335
336
# File 'lib/mirah/ast/method.rb', line 334

def delegate_args
  @delegate_args
end

Instance Method Details

#compile(compiler, expression) ⇒ Object



37
38
39
40
41
# File 'lib/mirah/compiler/method.rb', line 37

def compile(compiler, expression)
  compiler.constructor(self)
rescue Exception => ex
  raise Mirah::InternalCompilerError.wrap(ex, self)
end

#extract_delegate_constructorObject



365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/mirah/ast/method.rb', line 365

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



348
349
350
351
352
353
354
# File 'lib/mirah/ast/method.rb', line 348

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

#first_node=(new_node) ⇒ Object



356
357
358
359
360
361
362
363
# File 'lib/mirah/ast/method.rb', line 356

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, expression) ⇒ Object



384
385
386
387
388
389
# File 'lib/mirah/ast/method.rb', line 384

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

#validate_childrenObject



341
342
343
344
345
346
# File 'lib/mirah/ast/method.rb', line 341

def validate_children
  super
  if @delegate_args
    @delegate_args.each {|arg| arg.parent = self}
  end
end