Sha256: f6139f47cdafec9d34b229f6bedb8764dd4421e05e785420658846507b2f85a0

Contents?: true

Size: 1.65 KB

Versions: 3

Compression:

Stored size: 1.65 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module Carbon
  module Core
    module Pointer
      # Performs casting for pointers.  This converts them into an integer
      # for use in the program.  This defines the following functions on the
      # pointer type:
      #
      # - `.to-int64(self): Carbon::Int64`
      # - `.to-uint64(self): Carbon::UInt64`
      # - `.to-int32(self): Carbon::Int32`
      # - `.to-uint32(self): Carbon::UInt32`
      # - `.to-int16(self): Carbon::Int16`
      # - `.to-uint16(self): Carbon::UInt16`
      # - `.to-int8(self): Carbon::Int8`
      # - `.to-uint8(self): Carbon::UInt8`
      module Cast
        # Defines all of the cast functions for all of the integer types.
        #
        # @see #define_cast_function
        # @return [void]
        def define_cast_functions
          Ints.each do |int|
            next if int.size == 1
            define_cast_function(int)
          end
        end

        # Defines a cast function for a given integer type.
        #
        # @param int [Core::Int] The integer to cast the pointer to.
        # @return [void]
        def define_cast_function(int)
          function_name = PTYPE.call(int.cast, [PTYPE])
          Core.define(function: function_name) do |function|
            function[:return] = int.name
            define_cast_definition(int, function[:definition])
          end
        end

      private

        def define_cast_definition(int, definition)
          entry = definition.add("entry").build
          this = definition.params[0]
          this.name = "self"

          entry.ret(entry.ptr2int(this, int.name).as(int.name))
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
carbon-core-0.2.1 lib/carbon/core/pointer/cast.rb
carbon-core-0.2.0 lib/carbon/core/pointer/cast.rb
carbon-core-0.1.1 lib/carbon/core/pointer/cast.rb