Class: TypedRb::Model::TmSend

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

Overview

message send

Instance Attribute Summary collapse

Attributes inherited from Expr

#col, #line, #node, #type

Instance Method Summary collapse

Constructor Details

#initialize(receiver, message, args, node) ⇒ TmSend

Returns a new instance of TmSend.



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

def initialize(receiver, message, args, node)
  super(node)
  @receiver = receiver
  @message = message
  @args = args
  @block = nil
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



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

def args
  @args
end

#blockObject

Returns the value of attribute block.



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

def block
  @block
end

#messageObject

Returns the value of attribute message.



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

def message
  @message
end

#receiverObject

Returns the value of attribute receiver.



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

def receiver
  @receiver
end

Instance Method Details

#cast?(function_klass_type) ⇒ Boolean

Returns:

  • (Boolean)


265
266
267
# File 'lib/typed/model/tm_send.rb', line 265

def cast?(function_klass_type)
  function_klass_type == BasicObject && message == :cast
end

#check_application(receiver_type, function_type, context) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/typed/model/tm_send.rb', line 163

def check_application(receiver_type, function_type, context)
  if function_type.is_a?(Types::TyDynamicFunction)
    function_type.to
  else
    if function_type.generic?
      function_type.local_typing_context.parent = Types::TypingContext.type_variables_register
      return_type = function_type.materialize do |materialized_function|
        check_application(receiver_type, materialized_function, context)
      end.to
      return_type.respond_to?(:as_object_type) ? return_type.as_object_type : return_type
    else
      formal_parameters = function_type.from
      parameters_info = function_type.parameters_info
      TypedRb.log(binding, :debug, "Checking function application #{receiver_type}::#{message}( #{parameters_info} )")
      check_args_application(parameters_info, formal_parameters, args, context)
      if @block
        block_type = @block.check_type(context)
        # TODO:
        # Unification is run here
        # Algorithm is failing:
        # G > String,
        # G < E
        # ========
        # G = [String, ?]
        # -----
        # G = [String, E]
        # E = [String, ?]
        block_return_type = if function_type.block_type
                              # materialization and unification will happen in this invocation
                              block_type.compatible?(function_type.block_type, :lt)
                            else
                              block_type.to
                            end
        if block_return_type.to.stack_jump?
          break_type = block_return_type.to.wrapped_type.check_type(context)
          unless break_type.compatible?(function_type.to, :lt)
            error_message = "Incompatible 'break' type, expected #{function_type.to}, found #{break_type}"
            fail error_message, block_return_type.to.node
          end
        elsif block_return_type.to.either?
          max_type = block_return_type.check_type(context, [:return, :break, :normal])
          unless max_type.compatible?(function_type.to, :lt)
            error_message = "Incompatible either max type, expected #{function_type.to}, found #{max_type}"
            fail error_message, block_return_type.to.node
          end
        end
      end
      return_type = function_type.to
      return_type.respond_to?(:as_object_type) ? return_type.as_object_type : return_type
    end
  end
end

#check_args_application(parameters_info, formal_parameters, actual_arguments, context) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/typed/model/tm_send.rb', line 220

def check_args_application(parameters_info, formal_parameters, actual_arguments, context)
  #binding.pry if actual_arguments.size == 1 && actual_arguments.first.class == TypedRb::Model::TmVar && actual_arguments.first.val == "klass" && actual_arguments.first.col == 36
  parameters_info.each_with_index do |(require_info, arg_name), index|
    actual_argument = actual_arguments[index]
    formal_parameter_type = formal_parameters[index]
    if formal_parameter_type.nil? && !require_info == :block
      fail TypeCheckError.new("Error type checking message sent '#{message}': Missing information about argument #{arg_name} in #{receiver}##{message}", node)
    end
    if actual_argument.nil? && require_info != :opt && require_info != :rest && require_info != :block
      fail TypeCheckError.new("Error type checking message sent '#{message}': Missing mandatory argument #{arg_name} in #{receiver}##{message}", node)
    else
      if require_info == :rest
        break if actual_argument.nil? # invocation without any of the optional arguments
        rest_type = formal_parameter_type.type_vars.first
        formal_parameter_type = if rest_type.respond_to?(:bound)
                                  rest_type.bound
                                else
                                  rest_type
                                end
        actual_arguments[index..-1].each do |actual_argument|
          actual_argument_type = actual_argument.check_type(context)
          unless actual_argument_type.compatible?(formal_parameter_type, :lt)
            error_message = "Error type checking message sent '#{message}': #{formal_parameter_type} expected, #{actual_argument_type} found"
            fail TypeCheckError.new(error_message, node)
          end
        end
        break
      else
        unless actual_argument.nil? # opt or block if this is nil
          actual_argument_type = actual_argument.check_type(context)
          fail TypeCheckError.new("Error type checking message sent '#{message}': Missing type information for argument '#{arg_name}'", node) if formal_parameter_type.nil?
          begin
            unless actual_argument_type.compatible?(formal_parameter_type, :lt)
              error_message = "Error type checking message sent '#{message}': #{formal_parameter_type} expected, #{actual_argument_type} found"
              fail TypeCheckError.new(error_message, node)
            end
          rescue Types::UncomparableTypes, ArgumentError
            raise Types::UncomparableTypes.new(actual_argument_type, formal_parameter_type, node)
          end
        end
      end
    end
  end
end

#check_casting(context) ⇒ Object



269
270
271
272
273
274
275
# File 'lib/typed/model/tm_send.rb', line 269

def check_casting(context)
  from_type = args[0].check_type(context)
  to = parse_type_application_arguments([args[1]], context).first
  to_type = to.is_a?(Types::TyObject) ? to.as_object_type : to
  TypedRb.log(binding, :info, "Casting #{from_type} into #{to_type}")
  to_type
end

#check_instantiation(context) ⇒ Object

we received new, but we look for initialize in the class, not the singleton class. we then run the regular application, but we return the class type instead of the return type for the constructor application (should be unit/nil).



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/typed/model/tm_send.rb', line 52

def check_instantiation(context)
  self_type = singleton_object_type(receiver, context).as_object_type
  function_klass_type, function_type = self_type.find_function_type(:initialize, args.size, @block)
  TypedRb.log_dynamic_warning(node, self_type, :initialize) if function_type.dynamic?

  # function application
  @message = :initialize
  begin
    check_application(self_type, function_type, context)
  rescue TypeCheckError => error
    raise error if function_klass_type == self_type.ruby_type
  end
  self_type
end

#check_lambda_application(lambda_type, context) ⇒ Object



216
217
218
# File 'lib/typed/model/tm_send.rb', line 216

def check_lambda_application(lambda_type, context)
  lambda_type.check_args_application(args, context).to
end

#check_module_inclusions(self_type, context) ⇒ Object



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'lib/typed/model/tm_send.rb', line 281

def check_module_inclusions(self_type, context)
  args.map do |arg|
    arg.check_type(context)
  end.each do |module_type|
    if module_type.is_a?(Types::TyExistentialType)
      if module_type.local_typing_context
        module_type.check_inclusion(self_type)
      else
        # TODO: report warning about missing module information
        TypedRb.log(binding, :debug,  "Not type checking module #{module_type.ruby_type} inclusion due to lack of module information")
      end
    else
      error_message = "Error type checking message sent '#{message}': Module type expected for inclusion in #{self_type}, #{module_type} found"
      fail TypeCheckError.new(error_message, node)
    end
  end
  self_type
end

#check_type(context) ⇒ Object



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

def check_type(context)
  @context = context
  TypedRb.log(binding, :debug,  "Type checking message sent: #{message} at line #{node.loc.line}")
  if receiver.nil? && message == :ts
    # ignore, => type annotation
    Types::TyUnit.new(node)
  elsif message == :new && !singleton_object_type(receiver, context).nil? # clean this!
    check_instantiation(context)
  elsif receiver == :self || receiver.nil?
    # self.m(args), m(args), m
    check_type_no_explicit_receiver(context)
  else
    # x.m(args)
    check_type_explicit_receiver(context)
  end
end

#check_type_application_to_generic(generic_type, args) ⇒ Object



159
160
161
# File 'lib/typed/model/tm_send.rb', line 159

def check_type_application_to_generic(generic_type, args)
  generic_type.materialize(args)
end

#check_type_explicit_receiver(context) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/typed/model/tm_send.rb', line 85

def check_type_explicit_receiver(context)
  if receiver_type.is_a?(Types::Polymorphism::TypeVariable)
    # Existential type (Module) if receiver_type is self
    # TODO: what can we do if this is the inclusion of a module?
    arg_types = args.map { |arg| arg.check_type(context) }
    receiver_type.add_message_constraint(message, arg_types)
  elsif receiver_type.is_a?(Types::TyGenericSingletonObject) && (message == :call)
    # Application of types accept a type class or a string with a type description
    arg_types = parse_type_application_arguments(args, context)
    check_type_application_to_generic(receiver_type, arg_types)
  elsif receiver_type.is_a?(Types::TyFunction) && (message == :[] || message == :call)
    check_lambda_application(receiver_type, context)
  else
    function_klass_type, function_type = receiver_type.find_function_type(message, args.size, @block)
    TypedRb.log_dynamic_warning(node, receiver_type, message) if function_type.dynamic?
    # begin
    if function_type.nil?
      error_message = "Error type checking message sent '#{message}': Type information for #{receiver_type}:#{message} not found."
      fail TypeCheckError.new(error_message, node)
    elsif cast?(function_klass_type)
      check_casting(context)
    elsif module_include_implementation?(function_klass_type)
      check_module_inclusions(receiver_type, context)
    else
      # function application
      check_application(receiver_type, function_type, context)
    end
    # rescue TypeCheckError => error
    #  if function_klass_type != receiver_type.ruby_type
    #    Types::TyDynamic.new(Object, node)
    #  else
    #    raise error
    #  end
    # end
  end
end

#check_type_no_explicit_receiver(context) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/typed/model/tm_send.rb', line 67

def check_type_no_explicit_receiver(context)
  if message == :yield
    check_yield_application(context)
  else
    @receiver_type = context.get_type_for(:self) # check message in self type -> application
    check_type_explicit_receiver(context)
  end
end

#check_yield_application(context) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/typed/model/tm_send.rb', line 76

def check_yield_application(context)
  yield_abs_type = context.get_type_for(:yield)
  if yield_abs_type
    check_lambda_application(yield_abs_type, context)
  else
    fail TypeCheckError.new("Error type checking message sent '#{message}': Cannot find yield function defined in typing context", node)
  end
end

#module_include_implementation?(function_klass_type) ⇒ Boolean

Returns:

  • (Boolean)


277
278
279
# File 'lib/typed/model/tm_send.rb', line 277

def module_include_implementation?(function_klass_type)
  function_klass_type == Module && message == :include
end

#parse_type_application_argument(type) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/typed/model/tm_send.rb', line 145

def parse_type_application_argument(type)
  # TODO: do this recursively in the case of nested generic type
  # TODO: do we need it at all?
  klass = if type.is_a?(Hash) && type[:kind] == :generic_type
            Class.for_name(type[:type])
          end
  Runtime::TypeParser.parse(type, klass)
end

#parse_type_application_arguments(arguments, context) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/typed/model/tm_send.rb', line 122

def parse_type_application_arguments(arguments, context)
  arguments.map do |argument|
    if argument.is_a?(Model::TmString)
      type_var_signature = argument.node.children.first
      maybe_generic_method_var = Types::TypingContext.vars_info(:method)[type_var_signature]
      maybe_generic_class_var = Types::TypingContext.vars_info(:class)[type_var_signature]
      maybe_generic_module_var = Types::TypingContext.vars_info(:module)[type_var_signature]
      if maybe_generic_method_var || maybe_generic_class_var || maybe_generic_module_var
        maybe_generic_method_var || maybe_generic_class_var || maybe_generic_module_var
      else
        parsed_types = TypeSignature::Parser.parse(type_var_signature)
        if parsed_types.is_a?(Array)
          parsed_types.map { |parsed_type| parse_type_application_argument(parsed_type) }
        else
          parse_type_application_argument(parsed_types)
        end
      end
    else
      argument.check_type(context)
    end
  end.flatten
end

#receiver_typeObject



300
301
302
# File 'lib/typed/model/tm_send.rb', line 300

def receiver_type
  @receiver_type ||= receiver.check_type(@context)
end

#singleton_object_type(receiver, context) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/typed/model/tm_send.rb', line 38

def singleton_object_type(receiver, context)
  parsed_receiver_type = if receiver.nil? || receiver == :self
                           context.get_type_for(:self)
                         else
                           receiver_type
                         end
  return parsed_receiver_type if parsed_receiver_type.is_a?(Types::TySingletonObject)
end

#type_application_counterObject



154
155
156
157
# File 'lib/typed/model/tm_send.rb', line 154

def type_application_counter
  @type_application_counter ||= 0
  @type_application_counter += 1
end

#with_block(block) ⇒ Object



17
18
19
# File 'lib/typed/model/tm_send.rb', line 17

def with_block(block)
  @block = block
end