class Ikra::Types::ClassType

Attributes

cls[R]
inst_vars_types[R]

Public Class Methods

new(cls) click to toggle source

Ensure singleton per class

Calls superclass method
# File lib/types/types/class_type.rb, line 18
def new(cls)
    if @cache == nil
        @cache = {}
        @cache.default_proc = Proc.new do |hash, key|
            hash[key] = super(key)
        end
    end

    @cache[cls]
end
new(cls) click to toggle source
# File lib/types/types/class_type.rb, line 34
def initialize(cls)
    @cls = cls
    @inst_vars_read = Set.new
    @inst_vars_written = Set.new

    @inst_vars_types = Hash.new
    @inst_vars_types.default_proc = Proc.new do |hash, key|
        hash[key] = UnionType.new
    end
end

Public Instance Methods

==(other) click to toggle source
# File lib/types/types/class_type.rb, line 30
def ==(other)
    return other.class == self.class && other.cls == self.cls
end
accessed_inst_vars() click to toggle source
# File lib/types/types/class_type.rb, line 61
def accessed_inst_vars
    @inst_vars_read + @inst_vars_written
end
c_size() click to toggle source
# File lib/types/types/class_type.rb, line 127
def c_size
    # IDs are 4 byte integers
    4
end
class_name() click to toggle source

Generates a class name for [@cls], which is a valid C++ identifier.

For example: A –> A #<Class: A> –> singleton_A

# File lib/types/types/class_type.rb, line 83
def class_name
    # Handle name generation for singleton classes
    return ruby_name.gsub("\#<Class:", "singleton_").gsub(">", "")
end
inst_var_array_name(inst_var_name) click to toggle source
# File lib/types/types/class_type.rb, line 92
def inst_var_array_name(inst_var_name)
    if inst_var_name.to_s[0] != "@"
        raise AssertionError.new("Expected instance variable identifier")
    end

    "_iv_#{class_name}_#{inst_var_name.to_s[1..-1]}_"
end
inst_var_read!(inst_var_name) click to toggle source
# File lib/types/types/class_type.rb, line 45
def inst_var_read!(inst_var_name)
    @inst_vars_read.add(inst_var_name)
end
inst_var_read?(inst_var_name) click to toggle source
# File lib/types/types/class_type.rb, line 53
def inst_var_read?(inst_var_name)
    @inst_var_read.include?(inst_var_name)
end
inst_var_written(inst_var_name) click to toggle source
# File lib/types/types/class_type.rb, line 57
def inst_var_written(inst_var_name)
    @inst_var_written.include?(inst_var_name)
end
inst_var_written!(inst_var_name) click to toggle source
# File lib/types/types/class_type.rb, line 49
def inst_var_written!(inst_var_name)
    @inst_var_written.add(inst_var_name)
end
mangled_method_name(selector) click to toggle source
# File lib/types/types/class_type.rb, line 88
def mangled_method_name(selector)
    "_method_#{class_name}_#{selector}_"
end
method_ast(selector) click to toggle source
# File lib/types/types/class_type.rb, line 100
def method_ast(selector)
    source = Parsing.parse_method(cls.instance_method(selector))
    return AST::Builder.from_parser_ast(source)
end
method_binding(selector) click to toggle source
# File lib/types/types/class_type.rb, line 105
def method_binding(selector)
    # TODO: Fix binding
    return cls.instance_method(selector).send(:binding)
end
method_parameters(selector) click to toggle source
# File lib/types/types/class_type.rb, line 110
def method_parameters(selector)
    # returns names
    # TODO: handle optional params, kwargs, etc.
    to_ruby_type.instance_method(selector).parameters.map do |param|
        param[1]
    end
end
ruby_name() click to toggle source
# File lib/types/types/class_type.rb, line 69
def ruby_name
    @cls.to_s
end
should_generate_self_arg?() click to toggle source
# File lib/types/types/class_type.rb, line 118
def should_generate_self_arg?
    # Do not generate type for singleton classes
    return !to_ruby_type.is_a?(Module.singleton_class)
end
to_c_type() click to toggle source
# File lib/types/types/class_type.rb, line 73
def to_c_type
    # TODO: sometimes this should be a union type struct
    "obj_id_t"
end
to_ruby_type() click to toggle source
# File lib/types/types/class_type.rb, line 65
def to_ruby_type
    @cls
end
to_s() click to toggle source
# File lib/types/types/class_type.rb, line 123
def to_s
    "<class: #{class_name}>"
end