Class: MilkTea::Types::Primitive

Inherits:
Base
  • Object
show all
Defined in:
lib/milk_tea/core/types/types.rb

Constant Summary collapse

POINTER_INTEGER_WIDTH =
Fiddle::SIZEOF_VOIDP * 8
INTEGER_NAMES =
%w[byte short int long ubyte ushort uint ulong ptr_int ptr_uint].freeze
FLOAT_NAMES =
%w[float double].freeze
FIXED_SIGNED_INTEGER_WIDTHS =
{
  "byte" => 8,
  "short" => 16,
  "int" => 32,
  "long" => 64,
}.freeze
FIXED_UNSIGNED_INTEGER_WIDTHS =
{
  "ubyte" => 8,
  "ushort" => 16,
  "uint" => 32,
  "ulong" => 64,
}.freeze
FLOAT_WIDTHS =
{
  "float" => 32,
  "double" => 64,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#accept, #children, #field_c_name, #sendable?

Constructor Details

#initialize(name) ⇒ Primitive

Returns a new instance of Primitive.



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/milk_tea/core/types/types.rb', line 137

def initialize(name)
  @name = name
  @hash = [self.class, name].hash

  # Precompute classification flags: `name` is immutable and these
  # predicates are called constantly during type checking, so resolve
  # them once instead of rescanning arrays/hashes on every call.
  @integer = INTEGER_NAMES.include?(name)
  @float = FLOAT_NAMES.include?(name)
  @numeric = @integer || @float
  @pointer_sized_integer = name == "ptr_int" || name == "ptr_uint"
  @signed_integer = FIXED_SIGNED_INTEGER_WIDTHS.key?(name) || name == "ptr_int"
  @unsigned_integer = FIXED_UNSIGNED_INTEGER_WIDTHS.key?(name) || name == "ptr_uint"
  @fixed_width_integer = FIXED_SIGNED_INTEGER_WIDTHS.key?(name) || FIXED_UNSIGNED_INTEGER_WIDTHS.key?(name) || @pointer_sized_integer
  @integer_width = FIXED_SIGNED_INTEGER_WIDTHS[name] || FIXED_UNSIGNED_INTEGER_WIDTHS[name] || (@pointer_sized_integer ? POINTER_INTEGER_WIDTH : nil)
  @float_width = FLOAT_WIDTHS[name]
  @boolean = name == "bool"
  @void = name == "void"

  freeze
end

Instance Attribute Details

#float_widthObject (readonly)

Returns the value of attribute float_width.



165
166
167
# File 'lib/milk_tea/core/types/types.rb', line 165

def float_width
  @float_width
end

#hashObject (readonly)

Returns the value of attribute hash.



165
166
167
# File 'lib/milk_tea/core/types/types.rb', line 165

def hash
  @hash
end

#integer_widthObject (readonly)

Returns the value of attribute integer_width.



165
166
167
# File 'lib/milk_tea/core/types/types.rb', line 165

def integer_width
  @integer_width
end

#nameObject (readonly)

Returns the value of attribute name.



135
136
137
# File 'lib/milk_tea/core/types/types.rb', line 135

def name
  @name
end

Instance Method Details

#bitwise?Boolean

Returns:

  • (Boolean)


171
172
173
# File 'lib/milk_tea/core/types/types.rb', line 171

def bitwise?
  @integer
end

#boolean?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/milk_tea/core/types/types.rb', line 195

def boolean?
  @boolean
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


159
160
161
# File 'lib/milk_tea/core/types/types.rb', line 159

def eql?(other)
  other.is_a?(Primitive) && other.name == name
end

#fixed_width_integer?Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/milk_tea/core/types/types.rb', line 191

def fixed_width_integer?
  @fixed_width_integer
end

#float?Boolean

Returns:

  • (Boolean)


179
180
181
# File 'lib/milk_tea/core/types/types.rb', line 179

def float?
  @float
end

#integer?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/milk_tea/core/types/types.rb', line 175

def integer?
  @integer
end

#numeric?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'lib/milk_tea/core/types/types.rb', line 167

def numeric?
  @numeric
end

#signed_integer?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/milk_tea/core/types/types.rb', line 183

def signed_integer?
  @signed_integer
end

#to_sObject



203
204
205
# File 'lib/milk_tea/core/types/types.rb', line 203

def to_s
  name
end

#unsigned_integer?Boolean

Returns:

  • (Boolean)


187
188
189
# File 'lib/milk_tea/core/types/types.rb', line 187

def unsigned_integer?
  @unsigned_integer
end

#void?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/milk_tea/core/types/types.rb', line 199

def void?
  @void
end