Class: Yadriggy::RubyTypeInferer

Inherits:
RubyTypeChecker show all
Defined in:
lib/yadriggy/ruby_typeinfer.rb

Overview

Type checker for Ruby with type inference. The type system is slightly different from Ruby's one. For example, once an integer is assigned to a local variable, assigning a String value to it causes a type error.

Direct Known Subclasses

C::ClangTypeChecker

Constant Summary

Constants included from Yadriggy

DynType, Undef, VERSION, Void

Instance Method Summary collapse

Methods inherited from RubyTypeChecker

#get_call_expr_type, #get_name_type, #initialize, #lookup_ruby_classes, #type_args_and_block, #type_assert_params, #type_assert_subsume, #type_parameters

Methods inherited from TypeChecker

#add_typedef, #error_group, #initialize, #make_base_env, #type, #type?, #type_as, #type_assert, #type_assert_false, #type_assert_later, #type_env, #typecheck, #typedef

Methods inherited from Checker

#ast, #ast_env, #check, #check_all, #check_later, #error!, #error_found!, #error_messages, #errors?, #initialize, #last_error, #make_base_env, #proceed, rule

Methods included from Yadriggy

debug, debug=, define_syntax, reify, reset_pry

Constructor Details

This class inherits a constructor from Yadriggy::RubyTypeChecker

Instance Method Details

#bind_local_var(env, ast, var_type, is_def = true) ⇒ Object

Binds a local variable name to a type.

Parameters:

  • env (TypeEnv)

    a type environment.

  • ast (ASTnode)

    a local variable name.

  • var_type (Type)

    a type.

  • is_def (Boolean) (defaults to: true)

    true if the variable is initialized there.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/yadriggy/ruby_typeinfer.rb', line 20

def bind_local_var(env, ast, var_type, is_def=true)
  unless var_type.nil?
    t = if UnionType.role(var_type)
          ts = UnionType.role(var_type).types
          UnionType.new(ts.map {|t| to_non_instance_type(t) })
        else
          ins_t = InstanceType.role(var_type)
          to_non_instance_type(var_type)
        end
    lvt = LocalVarType.new(t.copy(OptionalRole), is_def ? ast : nil)
    env.bind_name(ast, lvt)
    @typetable[ast] = lvt
  end
end

#get_instance_variable_type(key, ivar, is_valid_type, value_type) ⇒ Type

Obtains the type of the given instance variable ivar declared in the given class (i.e. module) or the instance object key. If the type of ivar is not defined, value_type is recorded as its type.

Parameters:

  • key (Module|Object)

    the key when looking into the typedef table.

  • ivar (InstanceVariable)

    an instance variable.

  • is_valid_type (Boolean)

    true if value_type is valid.

  • value_type (Type)

    the type suggested for ivar.

Returns:

  • (Type)

    the type of ivar.



193
194
195
196
197
198
199
200
201
202
203
# File 'lib/yadriggy/ruby_typeinfer.rb', line 193

def get_instance_variable_type(key, ivar, is_valid_type, value_type)
  td = add_typedef(key)
  ivar_t = td[ivar]
  if ivar_t.nil?
    td[ivar] = value_type
  else
    type_assert_subsume(ivar_t, value_type,
              "bad type value for #{ivar.name}") if is_valid_type
    ivar_t
  end
end