Class: SFRP::Mono::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/sfrp/mono/type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(str, vconst_strs = nil, static = false, native_str = nil) ⇒ Type

Returns a new instance of Type.



6
7
8
9
10
11
# File 'lib/sfrp/mono/type.rb', line 6

def initialize(str, vconst_strs = nil, static = false, native_str = nil)
  @str = str
  @vconst_strs = vconst_strs
  @static = static
  @native_str = native_str
end

Instance Attribute Details

#strObject (readonly)

Returns the value of attribute str.



4
5
6
# File 'lib/sfrp/mono/type.rb', line 4

def str
  @str
end

Instance Method Details

#==(other) ⇒ Object



17
18
19
# File 'lib/sfrp/mono/type.rb', line 17

def ==(other)
  comp == other.comp
end

#all_pattern_examples(set) ⇒ Object



59
60
61
62
63
64
# File 'lib/sfrp/mono/type.rb', line 59

def all_pattern_examples(set)
  return [Pattern::PatternExample.new(nil, [])] if infinite?
  @vconst_strs.flat_map do |vc_str|
    set.vconst(vc_str).all_pattern_examples(set)
  end
end

#compObject



13
14
15
# File 'lib/sfrp/mono/type.rb', line 13

def comp
  [@str, @vconst_strs, @static, @native_str]
end

#gen(src_set, dest_set) ⇒ Object

Generate C’s elements for this type.



113
114
115
116
117
118
119
# File 'lib/sfrp/mono/type.rb', line 113

def gen(src_set, dest_set)
  gen_struct(src_set, dest_set)
  gen_typedef(src_set, dest_set)
  gen_constructor(src_set, dest_set)
  gen_allocator(src_set, dest_set)
  gen_mark_function(src_set, dest_set)
end

#gen_mark_cleanup_stmt(src_set, stmts) ⇒ Object

Generate statement to clean up objects of this types.



122
123
124
125
126
# File 'lib/sfrp/mono/type.rb', line 122

def gen_mark_cleanup_stmt(src_set, stmts)
  return unless need_mark?(src_set)
  return if src_set.memory(@str) == 0
  stmts << L.stmt("#{low_allocator_str}(1)")
end

#has_meta_in_struct?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/sfrp/mono/type.rb', line 55

def has_meta_in_struct?
  !(static? && single_vconst?)
end

#infinite?Boolean

Does this type has infinite amount of vconsts?

Returns:

  • (Boolean)


28
29
30
# File 'lib/sfrp/mono/type.rb', line 28

def infinite?
  @vconst_strs.nil?
end

#linear?(set) ⇒ Boolean

Does this type has single vconst of native type parameters e.g. Tuple3(Int, Int, Int)

Returns:

  • (Boolean)


39
40
41
# File 'lib/sfrp/mono/type.rb', line 39

def linear?(set)
  single_vconst? && set.vconst(@vconst_strs[0]).native_args?(set)
end

#low_allocator_strObject



83
84
85
# File 'lib/sfrp/mono/type.rb', line 83

def low_allocator_str
  "alloc_#{@str}"
end

#low_mark_func_strObject



87
88
89
# File 'lib/sfrp/mono/type.rb', line 87

def low_mark_func_str
  "mark_#{@str}"
end

#low_member_pointers_for_single_vconst(set, receiver_str) ⇒ Object



107
108
109
110
# File 'lib/sfrp/mono/type.rb', line 107

def low_member_pointers_for_single_vconst(set, receiver_str)
  raise unless single_vconst?
  set.vconst(@vconst_strs[0]).low_member_pointers(self, receiver_str)
end

#low_type_strObject



79
80
81
# File 'lib/sfrp/mono/type.rb', line 79

def low_type_str
  @native_str ? @native_str : @str
end

#low_typedef_for_alias(alias_str) ⇒ Object



75
76
77
# File 'lib/sfrp/mono/type.rb', line 75

def low_typedef_for_alias(alias_str)
  L.typedef("#{low_type_str} #{alias_str}")
end

#memory(set) ⇒ Object

Return max memory size to hold an instance of this type.



67
68
69
70
71
72
73
# File 'lib/sfrp/mono/type.rb', line 67

def memory(set)
  return Memory.one(@str) if infinite?
  x = @vconst_strs.reduce(Memory.empty) do |m, v_str|
    m.or(set.vconst(v_str).memory(set))
  end
  Memory.one(@str).and(x)
end

#meta_access_str(receiver_str) ⇒ Object



91
92
93
# File 'lib/sfrp/mono/type.rb', line 91

def meta_access_str(receiver_str)
  "#{receiver_str}#{static? ? '.' : '->'}meta"
end

#native?Boolean

Is this type native type?

Returns:

  • (Boolean)


33
34
35
# File 'lib/sfrp/mono/type.rb', line 33

def native?
  @native_str
end

#need_mark?(set) ⇒ Boolean

Do objects of this type need to be passed to mark-function?

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/sfrp/mono/type.rb', line 49

def need_mark?(set)
  return true unless static?
  return false if infinite?
  @vconst_strs.any? { |v_str| set.vconst(v_str).param_needing_mark?(set) }
end

#single_vconst?Boolean

Does this type has only one vconst?

Returns:

  • (Boolean)


44
45
46
# File 'lib/sfrp/mono/type.rb', line 44

def single_vconst?
  !infinite? && @vconst_strs.size == 1
end

#static?Boolean

Are objects of this type passed through value? Defalut is passing through referrence.

Returns:

  • (Boolean)


23
24
25
# File 'lib/sfrp/mono/type.rb', line 23

def static?
  @static
end

#term_id(vconst_str) ⇒ Object

Return term-id of given vconst of this type.



100
101
102
103
104
105
# File 'lib/sfrp/mono/type.rb', line 100

def term_id(vconst_str)
  raise "#{@str} is infinite" if infinite?
  res = @vconst_strs.index(vconst_str)
  raise "#{vconst_str} is not a vconst of #{@str}" unless res
  res
end

#terms_access_str(receiver_str) ⇒ Object



95
96
97
# File 'lib/sfrp/mono/type.rb', line 95

def terms_access_str(receiver_str)
  "#{receiver_str}#{static? ? '.' : '->'}terms"
end