Class: Ikra::Types::StructType

Inherits:
Object
  • Object
show all
Includes:
RubyType
Defined in:
lib/types/types/struct_type.rb,
lib/translator/struct_type.rb

Overview

This type represents a C++ struct. It has fields, each of which has a type.

Direct Known Subclasses

ZipStructType

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?, #should_generate_self_arg?, #to_array_type, #to_str, #to_union_type

Constructor Details

#initialize(fields) ⇒ StructType

Returns a new instance of StructType.



46
47
48
49
50
51
52
53
54
# File 'lib/types/types/struct_type.rb', line 46

def initialize(fields)
    fields.each do |key, value|
        if not value.is_union_type?
            raise AssertionError.new("Union type expected for field #{key}")
        end
    end

    @fields = fields
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



12
13
14
# File 'lib/types/types/struct_type.rb', line 12

def fields
  @fields
end

Class Method Details

.identifier_from_hash(fields) ⇒ Object

Generates a unique type identifier based on the types of the struct fields.



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/types/types/struct_type.rb', line 29

def identifier_from_hash(fields)
    identifier = "indexed_struct_#{fields.size}_lt_"

    type_parts = fields.map do |key, value|
        value.to_c_type
    end

    identifier = identifier + type_parts.join("_")

    return identifier + "_gt_t"
end

.new(fields) ⇒ Object

Ensure singleton per class



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/types/types/struct_type.rb', line 16

def new(fields)
    if @cache == nil
        @cache = {} 
    end

    if not @cache.include?(identifier_from_hash(fields))
        @cache[identifier_from_hash(fields)] = super(fields)
    end

    @cache[identifier_from_hash(fields)]
end

Instance Method Details

#==(other) ⇒ Object



42
43
44
# File 'lib/types/types/struct_type.rb', line 42

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

#generate_definitionObject

Raises:

  • (NotImplementedError)


4
5
6
# File 'lib/translator/struct_type.rb', line 4

def generate_definition
    raise NotImplementedError.new("ZipStructType is the only implementation")
end

#to_c_typeObject



56
57
58
# File 'lib/types/types/struct_type.rb', line 56

def to_c_type
    return StructType.identifier_from_hash(@fields)
end

#to_ffi_typeObject



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

def to_ffi_type
    # TODO: Support transfering zipped data back from GPU
    return to_ruby_type
end

#to_ruby_typeObject

Raises:

  • (NotImplementedError)


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

def to_ruby_type
    # TODO: general structs
    raise NotImplementedError.new
end