Class: Carbon::Concrete::Build

Inherits:
Object
  • Object
show all
Defined in:
lib/carbon/concrete/build.rb

Overview

Handles building requests. This is the step after all requests are determined from the dependency build stage. The entire purpose of this class is to consolidate state information for LLVM module building.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(requests, index) ⇒ Build

Initialize the build process. This only initializes instance variables.

Parameters:

  • requests (::Enumerable<Concrete::Type>)

    An enumerable over each request that needs to be built. The requests should be resolved, i.e. the generics for the request should resolve to a valid type.

  • index (Concrete::Index)

    The index that is being built from.



38
39
40
41
# File 'lib/carbon/concrete/build.rb', line 38

def initialize(requests, index)
  @requests = requests
  @index = index
end

Instance Attribute Details

#indexConcrete::Index (readonly)

The index associated with this build.

Returns:



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

def index
  @index
end

#items{Concrete::Type => (Concrete::Item, ::LLVM::Value)} (readonly)

The types that have been defined. This is a mapping of the Type to an arry of the Item and the corresponding LLVM::Type. This is so that no information is lost when converting the item to the type.

Returns:



18
19
20
# File 'lib/carbon/concrete/build.rb', line 18

def items
  @items
end

#module::LLVM::Module (readonly)

The LLVM module that is being used for compilation.

Returns:

  • (::LLVM::Module)


23
24
25
# File 'lib/carbon/concrete/build.rb', line 23

def module
  @module
end

Instance Method Details

#call::LLVM::Module

Performs the build process. Builds each request in order by calling Item::Base#call on each.

Returns:

  • (::LLVM::Module)

    The built module.



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/carbon/concrete/build.rb', line 56

def call
  @items = {}
  @module = ::LLVM::Module.new("__carbon_main")

  @requests.each do |request|
    item = @index.fetch(request).first
    generics = item.generics.map(&:name)
      .zip(request.generics.map(&:name)).to_h
    item.call(self, generics)
  end

  @module
end

#fetch(name, default = nil) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/carbon/concrete/build.rb', line 43

def fetch(name, default = nil)
  result = @items.find { |(k, _v)| k.match?(name) }

  return result[1] if result
  return default if default
  yield if block_given?
  fail ItemNotFoundError, "Could not find item with name #{name}"
end