Class: TypedRb::Model::TmModule

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

Overview

Module expression

Instance Attribute Summary collapse

Attributes inherited from Expr

#col, #line, #node, #type

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(module_name, body, node) ⇒ TmModule

Returns a new instance of TmModule.



10
11
12
13
14
# File 'lib/typed/model/tm_module.rb', line 10

def initialize(module_name, body, node)
  super(node)
  @module_name = module_name
  @body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



8
9
10
# File 'lib/typed/model/tm_module.rb', line 8

def body
  @body
end

#module_nameObject (readonly)

Returns the value of attribute module_name.



8
9
10
# File 'lib/typed/model/tm_module.rb', line 8

def module_name
  @module_name
end

Class Method Details

.with_local_context(module_type, node) {|module_self_variable| ... } ⇒ Object

Yields:

  • (module_self_variable)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/typed/model/tm_module.rb', line 36

def self.with_local_context(module_type, node)
  Types::TypingContext.push_context(:module)
  # Deal with upper/lower bounds here if required
  module_self_variable = Types::TypingContext.type_variable_for(module_type.ruby_type, :module_self, [module_type.ruby_type])
  module_self_variable.node = node
  module_self_variable.module_type = module_type
  module_type.self_variable = module_self_variable

  if(module_type.is_a?(TypedRb::Types::TyGenericExistentialType))
    module_type.type_vars.each do |type_var|
      type_var = Types::TypingContext.type_variable_for_generic_type(type_var)
      type_var.node = node

      if type_var.upper_bound
        type_var.compatible?(type_var.upper_bound, :lt)
      end

      if type_var.lower_bound
        type_var.compatible?(type_var.lower_bound, :gt)
      end
    end
  end
  yield(module_self_variable)

  # 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.
  Types::TypingContext.pop_context
end

Instance Method Details

#check_type(context) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/typed/model/tm_module.rb', line 16

def check_type(context)
  module_ruby_type = Types::TypingContext.find_namespace(module_name)
  module_type = Runtime::TypeParser.parse_existential_object_type(module_ruby_type.name)
  Types::TypingContext.namespace_push(module_name)
  module_type.node = node
  module_typing_context = TmModule.with_local_context(module_type, node) do |_module_self_variable|
    context = context.add_binding(:self, module_type)
    body.check_type(context) if body
  end
  Types::TypingContext.namespace_pop
  module_type.local_typing_context = module_typing_context
  unification = Types::Polymorphism::Unification.new(module_type.local_typing_context.all_constraints,
                                                     :allow_unbound_receivers => true)
  unification.run
  if(module_type.is_a?(TypedRb::Types::TyGenericExistentialType))
    module_type.clean_dynamic_bindings
  end
  module_type
end