Class: TypedRb::Model::TmFun

Inherits:
Expr show all
Defined in:
lib/typed/model/tm_fun.rb

Overview

A instance/class function definition expression

Instance Attribute Summary collapse

Attributes inherited from Expr

#col, #line, #node, #type

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, name, args, body, node) ⇒ TmFun

Returns a new instance of TmFun.



12
13
14
15
16
17
18
19
20
# File 'lib/typed/model/tm_fun.rb', line 12

def initialize(owner, name, args, body, node)
  super(node)
  @owner = parse_owner(owner)
  @name = name
  @args = args
  @body = body
  @arg_count = args.count { |arg| arg.first != :blockarg }
  @has_block = args.detect { |arg| arg.first == :blockarg }
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



9
10
11
# File 'lib/typed/model/tm_fun.rb', line 9

def args
  @args
end

#bodyObject

Returns the value of attribute body.



9
10
11
# File 'lib/typed/model/tm_fun.rb', line 9

def body
  @body
end

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/typed/model/tm_fun.rb', line 9

def name
  @name
end

#ownerObject

Returns the value of attribute owner.



9
10
11
# File 'lib/typed/model/tm_fun.rb', line 9

def owner
  @owner
end

#owner_typeObject (readonly)

Returns the value of attribute owner_type.



10
11
12
# File 'lib/typed/model/tm_fun.rb', line 10

def owner_type
  @owner_type
end

Class Method Details

.with_fresh_bindings(klass, function_type) ⇒ Object

TODO: 1 Find free type variables for the generic function. 2 Create a new local typing context for the generic function 3 Add free type variables to the typing context



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/typed/model/tm_fun.rb', line 45

def self.with_fresh_bindings(klass, function_type)
  if function_type.generic?
    Types::TypingContext.push_context(:method)
    function_type.free_type_variables(klass).each do |type_var|
      # This will add the variable to the context
      Types::TypingContext.type_variable_for_function_type(type_var)
    end

    yield if block_given?

    # # Since every single time we find the generic type the same instance
    # # will be returned, the local_typing_context will still be associated.
    # # This is the reason we need to build a new typing context cloning this
    # # one while type materialization.
    function_type.local_typing_context = Types::TypingContext.pop_context
  else
    yield if block_given?
  end
  function_type
end

Instance Method Details

#check_type(context) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/typed/model/tm_fun.rb', line 22

def check_type(context)
  compute_owner_type(context)

  function_klass_type, function_type = owner_type.find_function_type(name, @arg_count, @has_block)

  if function_type.nil? || function_type.dynamic?
    TypedRb.log_dynamic_warning(node, owner_type, name)
    return Types::TyUnit.new(node)
  end

  context = setup_context(context, function_type)
  # check the body with the new bindings for the args
  TmFun.with_fresh_bindings(function_klass_type, function_type) do
    body_return_type = body.check_type(context)
    TypedRb::Types::TypingContext.function_context_pop
    check_return_type(context, function_type, body_return_type)
  end
end