Class: Carbon::Concrete::Item::Internal Private

Inherits:
Object
  • Object
show all
Includes:
Data
Defined in:
lib/carbon/concrete/item/internal.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.

Note:

This class is frozen upon initialization. This means that any attempt to modify it will result in an error. In most cases, the attributes on this class will also be frozen, as well.

An internal data type. In most cases, this is just integers and pointers that can't be easily serialized because of links to LLVM and FFI.

Constant Summary collapse

TYPE =

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

Retreives the LLVM type information for the given integer size. This cannot be serialized by marshal or any other serialization library.

Returns:

  • (Proc<Numeric, LLVM::Type>)
proc { |size| LLVM.const_get("Int#{size}").type }

Instance Attribute Summary

Attributes included from Data

#implements

Attributes included from Base

#dependencies, #generics, #name, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#==, #corrected_dependencies, #define, #hash

Constructor Details

#initialize(data) ⇒ Internal

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 internal data type. Internal data types may not have generic information, and have no dependencies, but can still implement traits.

Parameters:

  • data (::Hash)

    The options for the internal data type.

Options Hash (data):

  • :type (Type)

    The module of the internal data type.

  • :kind (::String, ::Symbol)

    The kind of the internal data type. Valid values are :integer, :pointer, :void, or the string version of any of those. This controls the underlying LLVM type of the internal data type.

  • :size (::String, ::Numeric, #to_i)

    The size of the internal data type. For integers, it is the number of bits in the integer; for pointers, it is the number of bits of the element it points to.

  • :implements (<Type>) — default: []

    The types that the internal data type implements.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/carbon/concrete/item/internal.rb', line 48

def initialize(data)
  @type = data.fetch(:type)
  @kind = data.fetch(:kind).to_s
  @size = data.fetch(:size, 8)
  @extern = data[:extern]

  @generics = @type.generics
  @implements = Set.new(data.fetch(:implements, []))
  @dependencies = Set.new(@generics.map(&:name))
  @name = @module.to_s

  deep_freeze!
end

Class Method Details

.from(type) ⇒ {::Symbol => ::Object}

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.

Creates a hash from a given Type that can be used to intialize an instance of this object. This is mostly used for Carbon::Concrete::Index#define and shouldn't be used anywhere else.

Parameters:

Returns:

  • ({::Symbol => ::Object})


28
29
30
# File 'lib/carbon/concrete/item/internal.rb', line 28

def self.from(type)
  { type: type, implements: [] }
end

Instance Method Details

#call(build, generics) ⇒ 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.

Performs compilation for the item. This converts the item into an LLVM-based value or type, to be used for compiling. If unimplemented, it raises a NotImplementedError.

Parameters:

Raises:

  • (NotImplementedError)

    If it is not implemented.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/carbon/concrete/item/internal.rb', line 63

def call(build, generics)
  full = @type.sub(generics)
  value = case @kind.to_s
          when "integer" then TYPE.call(@size)
          when "void"    then ::LLVM::Type.void
          when "opaque"
            ::LLVM::Type.struct([], false, @extern || full.to_s)
          when "pointer"
            type = build.fetch(generics.fetch(Carbon::Type("T")))
            type.last.pointer
          else fail ArgumentError, "Unknown kind #{@kind}"
          end

  build.items[@type.sub(generics)] = [self, value]
end