Module: Carbon::Core::Integer::Misc Private

Included in:
Carbon::Core::Integer
Defined in:
lib/carbon/core/integer/misc.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Defines a set of miscellaneous functions. These functions are various functions that don't fit in any of the other category. These functions include:

  • .null(): self
  • .next(self): self
  • .succ(self): self
  • .abs(self): self
  • .prev(self): self
  • .pred(self): self
  • .to-bool(self): self
  • .size(): Carbon::UInt32
  • .size(self): Carbon::UInt32

These functions are defined on all of the integer types except boolean. However, .size and .null are also defined on boolean.

Instance Method Summary collapse

Instance Method Details

#define_bool_function(int) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Defines the "to boolean" function for the given integer type. This just returns the result of comparison of the number to zero (returns 0 if the number is zero, 1 otherwise).

Parameters:



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/carbon/core/integer/misc.rb', line 32

def define_bool_function(int)
  function_name = int.name.call("to-bool", [int.name])
  Core.define(function: function_name) do |function|
    function[:return] = int.name
    function[:definition].add("entry").build do |b|
      this = function[:definition].params[0]
      this.name = "self"
      b.ret(b.icmp(:ne, this, 0).as(Carbon::Boolean))
    end
  end
end

#define_iabs_function(int) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Define the absolute value function. For signed integers, it returns the absolute (positive) value of an integer; for unsigned integers, it returns itself.

Parameters:



99
100
101
102
103
104
105
# File 'lib/carbon/core/integer/misc.rb', line 99

def define_iabs_function(int)
  function_name = int.name.call("abs", [int.name])
  Core.define(function: function_name) do |function|
    function[:return] = int.name
    define_iabs_definition(int, function[:definition])
  end
end

#define_next_function(int) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Define the next functions for the given integer type. These functions just return the given number incremented by one. This is implemented without any calls. This function defines both the .next and .succ functions.

Parameters:



66
67
68
69
70
71
72
73
74
# File 'lib/carbon/core/integer/misc.rb', line 66

def define_next_function(int)
  %w(next succ).each do |name|
    function_name = int.name.call(name, [int.name])
    Core.define(function: function_name) do |function|
      function[:return] = int.name
      define_next_definition(function[:definition])
    end
  end
end

#define_null_function(int) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Defines the null function. This defines a function that returns a null (zero) integer value.

Parameters:



49
50
51
52
53
54
55
56
57
# File 'lib/carbon/core/integer/misc.rb', line 49

def define_null_function(int)
  function_name = int.name.call(:null, [])
  Core.define(function: function_name) do |function|
    function[:return] = int.name
    function[:definition].add("entry").build do |b|
      b.ret(b._null(int.name).as(int.name))
    end
  end
end

#define_prev_function(int) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Define the previous functions for the given integer type. These functions just return the given number decremented by one. This is implemented without any calls. This function defines both the .prev and .pred functions.

Parameters:



83
84
85
86
87
88
89
90
91
# File 'lib/carbon/core/integer/misc.rb', line 83

def define_prev_function(int)
  %w(prev pred).each do |name|
    function_name = int.name.call(name, [int.name])
    Core.define(function: function_name) do |function|
      function[:return] = int.name
      define_prev_definition(function[:definition])
    end
  end
end

#define_size_function(int) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Defines the size function. This returns the size of the integer, in bits.

Parameters:

  • int (Core::Int)

    The (receiver) integer type.



112
113
114
115
116
117
118
119
120
# File 'lib/carbon/core/integer/misc.rb', line 112

def define_size_function(int)
  [[], [int.name]].each do |args|
    function_name = int.name.call("size", args)
    Core.define(function: function_name) do |function|
      function[:return] = Carbon::Type("Carbon::UInt32")
      define_size_definition(int, function[:definition])
    end
  end
end