# encoding: utf-8 # frozen_string_literal: true # rubocop:disable Metrics/LineLength module Carbon module Core module Pointer # Methods that perform accesses on pointers. This is essentially # dereferencing and indexing. This defines the following functions: # # - `Carbon::Pointer.value(self): T` # - `Carbon::Pointer.value=(self, value: T): T` # - `Carbon::Pointer.[](self, index: I): T` # - `Carbon::Pointer.[]=(self, index: I, value: T): T` # # @api private module Access # Define all of the access functions. This iterates over all of the # integers, defining the array get/set functions in terms of each # integer type. It then defines the value get/set functions. # # @return [void] def define_access_functions Ints.each do |int| next if int.size == 1 define_array_get_function(int) define_array_set_function(int) end define_value_get_function define_value_set_function end # Defines the value set function. This function (named `value=`) sets # the value at the pointer. This is the same as calling the array # set function with an index of zero (0). # # @return [void] def define_value_set_function function_name = PTYPE.call(:value=, [PTYPE, PTYPEGEN]) Core.define(function: function_name) do |function| function[:return] = PTYPEGEN define_value_set_definition(function[:definition]) end end # Defines the value get function. This function (named `value`) gets # the value at the pointer. This is the same as calling the array # get function with an index of zero (0). # # @return [void] def define_value_get_function function_name = PTYPE.call(:value, [PTYPE]) Core.define(function: function_name) do |function| function[:return] = PTYPEGEN define_value_get_definition(function[:definition]) end end # Defines the array set function. This function (named `[]=`) sets # the value a given distance away from the pointer. # # @param int [Core::Int] The integer type for the index. # @return [void] def define_array_set_function(int) function_name = PTYPE.call(:[]=, [PTYPE, int.name, PTYPEGEN]) Core.define(function: function_name) do |function| function[:return] = PTYPEGEN define_array_set_definition(int, function[:definition]) end end # Defines the array get function. This function (named `[]`) gets the # value at a given distance away from the pointer. # # @param int [Core::Int] The integer type for the index. # @return [void] def define_array_get_function(int) function_name = PTYPE.call(:[], [PTYPE, int.name]) Core.define(function: function_name) do |function| function[:return] = PTYPEGEN define_array_get_definition(int, function[:definition]) end end private def define_array_set_definition(_int, definition) entry = definition.add("entry").build this, index, value = definition.params this.name, index.name, value.name = %w(self index value) gep = entry.gep(this, index).as(Carbon::Type("Carbon::UInt64")) entry.store(gep, value) entry.ret(value) end def define_array_get_definition(_int, definition) entry = definition.add("entry").build this, index = definition.params this.name, index.name = %w(self index) gep = entry.gep(this, index).as(Carbon::Type("Carbon::UInt64")) load = entry.load(gep).as(PTYPEGEN) entry.ret(load) end def define_value_set_definition(definition) entry = definition.add("entry").build this, value = definition.params this.name, value.name = %w(self value) entry.store(this, value) entry.ret(value) end def define_value_get_definition(definition) entry = definition.add("entry").build this = definition.params[0] this.name = "self" entry.ret(entry.load(this).as(PTYPEGEN)) end end end end end