Class: Ikra::Types::ClassType

Inherits:
Object
  • Object
show all
Includes:
RubyType
Defined in:
lib/types/types/class_type.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RubyType

#class_id, #eql?, #hash, #inspect, #is_primitive?, #is_union_type?, #to_array_type, #to_str, #to_union_type

Constructor Details

#initialize(cls) ⇒ ClassType

Returns a new instance of ClassType.



34
35
36
37
38
39
40
41
42
43
# 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

Instance Attribute Details

#clsObject (readonly)

Returns the value of attribute cls.



13
14
15
# File 'lib/types/types/class_type.rb', line 13

def cls
  @cls
end

#inst_vars_typesObject (readonly)

Returns the value of attribute inst_vars_types.



14
15
16
# File 'lib/types/types/class_type.rb', line 14

def inst_vars_types
  @inst_vars_types
end

Class Method Details

.new(cls) ⇒ Object

Ensure singleton per class



18
19
20
21
22
23
24
25
26
27
# 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

Instance Method Details

#==(other) ⇒ Object



30
31
32
# File 'lib/types/types/class_type.rb', line 30

def ==(other)
    return other.class == self.class && other.cls == self.cls
end

#accessed_inst_varsObject



61
62
63
# File 'lib/types/types/class_type.rb', line 61

def accessed_inst_vars
    @inst_vars_read + @inst_vars_written
end

#c_sizeObject



127
128
129
130
# File 'lib/types/types/class_type.rb', line 127

def c_size
    # IDs are 4 byte integers
    4
end

#class_nameObject

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

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



83
84
85
86
# 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) ⇒ Object



92
93
94
95
96
97
98
# 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) ⇒ Object



45
46
47
# 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) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
# 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) ⇒ Object



57
58
59
# 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) ⇒ Object



49
50
51
# 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) ⇒ Object



88
89
90
# File 'lib/types/types/class_type.rb', line 88

def mangled_method_name(selector)
    "_method_#{class_name}_#{selector}_"
end

#method_ast(selector) ⇒ Object



100
101
102
103
# 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) ⇒ Object



105
106
107
108
# 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) ⇒ Object



110
111
112
113
114
115
116
# 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_nameObject



69
70
71
# File 'lib/types/types/class_type.rb', line 69

def ruby_name
    @cls.to_s
end

#should_generate_self_arg?Boolean

Returns:

  • (Boolean)


118
119
120
121
# 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_typeObject



73
74
75
76
# 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_typeObject



65
66
67
# File 'lib/types/types/class_type.rb', line 65

def to_ruby_type
    @cls
end

#to_sObject



123
124
125
# File 'lib/types/types/class_type.rb', line 123

def to_s
    "<class: #{class_name}>"
end