Class: Carbon::Core::Int Private

Inherits:
Object
  • Object
show all
Defined in:
lib/carbon/core/int.rb

Overview

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

An integer type in Carbon. The integer types are kept in here in order to keep track of the properties of each integer type.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, cast, sign, size) ⇒ Int

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.

Initialize the int.

Parameters:

  • name (Carbon::Concrete::Type)

    The type of the integer.

  • cast (::String)

    The cast function name.

  • sign (::Symbol)

    How the integer is signed.

  • size (::Integer)

    The size of the integer, in bits.



61
62
63
64
65
66
67
68
# File 'lib/carbon/core/int.rb', line 61

def initialize(name, cast, sign, size)
  @name = name
  @cast = cast
  @sign = sign
  @size = size

  deep_freeze!
end

Instance Attribute Details

#cast::String (readonly)

The name of the function that casts another integer to this one.

Examples:

int.cast # => "to-bool"

Returns:

  • (::String)

    The cast function name.



25
26
27
# File 'lib/carbon/core/int.rb', line 25

def cast
  @cast
end

#nameCarbon::Concrete::Type (readonly)

The name of the integer type.

Examples:

int.name # => #<Carbon::Concrete::Type Carbon::Boolean>

Returns:



17
18
19
# File 'lib/carbon/core/int.rb', line 17

def name
  @name
end

#sign::Symbol (readonly)

The signage of the integer type. This is either :signed or :unsigned. :signed implies two's compliment.

Examples:

int.sign # => :unsigned

Returns:

  • (::Symbol)

    How the integer is signed.



34
35
36
# File 'lib/carbon/core/int.rb', line 34

def sign
  @sign
end

#size::Integer (readonly)

The number of bits in the integer. This is normally a power of two.

Examples:

int.size # => 1

Returns:

  • (::Integer)

    The size of the integer.



42
43
44
# File 'lib/carbon/core/int.rb', line 42

def size
  @size
end

Class Method Details

.find(size:, sign:) ⇒ Int?

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.

Finds a specific integer with the given size and signage. Uses Carbon::Core::Ints as the base.

Parameters:

  • size (::Integer)

    The size of the integer.

  • sign (::Symbol)

    How the integer should be signed.

Returns:

  • (Int)

    If an integer with the given size and signage is found.

  • (nil)

    Otherwise.



51
52
53
# File 'lib/carbon/core/int.rb', line 51

def self.find(size:, sign:)
  Ints.find { |i| i.size == size && i.sign == sign }
end