Class: Carbon::Concrete::Item::Function Private

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

A function definition. This contains all of the information needed to build a proper LLVM function. This is stored within an index and serialized as needed, in order to defer compilation. There are two main types of functions: "normal," or "extern." "Normal" functions have an actual definition (i.e. Tacky::Function), and are written in Ruby/Carbon. "Extern" functions do not have a definition; they map a Carbon function to a C/low-level function. Instead of a definition, they have a function name, that maps to the C/low-level function. This is normally specified with an :extern directive.

Instance Attribute Summary collapse

Attributes included from Base

#dependencies, #generics, #name, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#==, #corrected_dependencies, #hash

Constructor Details

#initialize(data) ⇒ Function

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.

Returns a new instance of Function.



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/carbon/concrete/item/function.rb', line 38

def initialize(data)
  @type = data.fetch(:type)
  @internal = data.fetch(:internal) { @type.function.name }
  @parameters = data.fetch(:parameters) { @type.function.parameters }
  @definition = data.fetch(:definition)
  @generics = data.fetch(:generics) { @type.generics }
  @return = data.fetch(:return)
  @extern = data[:extern]
  @name = @type.to_s

  derive_dependencies
  deep_freeze!
end

Instance Attribute Details

#definitionObject (readonly)

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.



25
26
27
# File 'lib/carbon/concrete/item/function.rb', line 25

def definition
  @definition
end

#returnObject (readonly)

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.



24
25
26
# File 'lib/carbon/concrete/item/function.rb', line 24

def return
  @return
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
31
32
33
34
35
36
# File 'lib/carbon/concrete/item/function.rb', line 28

def self.from(type)
  {
    type: type,
    generics: type.generics,
    internal: type.function.name,
    parameters: type.function.parameters,
    definition: Tacky::Function.new(type.function.parameters)
  }
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.



62
63
64
65
66
67
68
69
70
71
# File 'lib/carbon/concrete/item/function.rb', line 62

def call(build, generics)
  case @definition
  when Tacky::Function
    build_function_intern(build, generics)
  when ::String, ::Symbol
    build_function_extern(build, generics)
  else fail ArgumentError,
    "Unknown definition #{@definition.class}"
  end
end

#define(index) ⇒ 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.



52
53
54
55
56
57
58
59
# File 'lib/carbon/concrete/item/function.rb', line 52

def define(index)
  if @definition.is_a?(::Proc)
    Function.new(Function.from(@type)
      .merge(definition: @definition.call(index), return: @return))
  else
    self
  end
end