module Carbon::Concrete::Item::Base

A “base” for other items. This just has some common methods that all items need to share to be compatible with the index. This module is the bases of the build system in the {Concrete} module.

Attributes

dependencies[R]

The dependencies that this item is based on. These are requests because we request the item from the index as a dependency. Requests contain the module name that it builds and the generics it requires.

@api semipublic @example

item.dependencies # => Set[#<Carbon::Concrete::Request T>]

@return [Set<Request>]

generics[R]

Returns the generic information associated with this item. This is used internally for generic substitution later on.

@api semipublic @example

item.generics # => [#<Carbon::Concrcete::Type::Generic T>]

@return [<Type::Generic>]

name[R]

Returns the full name of the item. This can include generic information.

@example

item.name # => "Carbon::Pointer<T>"

@return [::String]

type[R]

Returns the type of the item. This is the full {Concrete::Type}.

@api public @example For a module.

item.name # => "Carbon::Pointer<T>"
item.type # => #<Carbon::Type Carbon::Pointer<T>>

@example For a function.

item.name
  # => "Carbon::Pointer<T>.+(Carbon::Pointer<T>, Carbon::Int32)"
item.intern
  # => #<Carbon::Type Carbon::Pointer.+(Carbon::Pointer, Carbon::Int32)>

@return [Concrete::Type] The type.

Public Class Methods

from(type) click to toggle source

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

@api private @param type [Concrete::Type] The type. @return [{::Symbol => ::Object}]

# File lib/carbon/concrete/item/base.rb, line 60
def self.from(type)
  { type: type }
end

Public Instance Methods

==(other) click to toggle source

Compares this item to another object. If the other object is this item, then it returns true; otherwise, it returns false.

@api public @example

first.type # => "Carbon::Pointer<T>"
second.type # => "Carbon::Pointer<T>"
first == first # => true
first == second # => true
second == second # => true
second == first # => true

@param other [Base, ::Object] The object to compare. @return [::Boolean] The result of comparison.

# File lib/carbon/concrete/item/base.rb, line 77
def ==(other)
  equal?(other)
end
Also aliased as: eql?
call(build, generics) click to toggle source

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`.

@api private @param build [Concrete::Build] The build information for the item. @param generics [{Concrete::Type => Concrete::Type}] The generics

to apply for the item.

@return [void] @raise [NotImplementedError] If it is not implemented.

# File lib/carbon/concrete/item/base.rb, line 103
def call(build, generics)
  fail NotImplementedError, "Could not build #{self.class}"
end
corrected_dependencies(request, &block) click to toggle source

Modifies the dependencies of this item so that they conform to the given request. This should resolve all of our dependencies so that they no longer hold any sort of generic class.

@api private @param request [Request] The request. @yield [dep] The corrected dependencies. @yieldparam dep [Request] The corrected dependency. @return [::Enumerable] An enumerator of the corrected dependencies,

if no block was given.

@return [void] If a block was given.

# File lib/carbon/concrete/item/base.rb, line 129
def corrected_dependencies(request, &block)
  return to_enum(:corrected_dependencies, request) unless block_given?
  return dependencies.each(&block) if generics.empty?

  forced_corrected_dependencies(request, &block)
end
define(index) click to toggle source

Performs modification to the item. This is done after all items are loaded into the index, so that an item can load outside information if need be.

@param index [Concrete::Index] The index. @return [Concrete::Item::Base] A modified version of the item, or

self.
# File lib/carbon/concrete/item/base.rb, line 114
def define(index)
  self
end
eql?(other)
Alias for: ==
hash() click to toggle source

Creates a hash of the item. This is used mostly in the Hash class.

@api private @return [Numeric]

# File lib/carbon/concrete/item/base.rb, line 87
def hash
  type.hash
end

Private Instance Methods

forced_corrected_dependencies(request, &block) click to toggle source
# File lib/carbon/concrete/item/base.rb, line 138
def forced_corrected_dependencies(request, &block)
  # Array<Type::Generic> -> Array<(Type::Generic, Numeric)> ->
  # Array<(Type, Type)> -> {Type => Type}
  mapping = generics
            .each_with_index
            .map { |gen, i| [gen.name, request.generics[i].name] }
            .to_h

  dependencies.map { |dep| dep.sub(mapping) }.each(&block)
end