# encoding: utf-8 # frozen_string_literal: true require "forwardable" module Carbon module Tacky # A "context." This contains all of the information needed to build a # function, such as instruction values (From LLVM), block mapping, # parameters, and the build ({Concrete::Build}). # # @api private class Context extend Forwardable # The values of instructions. This is used to properly map # {Tacky::Reference} values to the proper `LLVM::Value`s. # # @return [<::LLVM::Value>] attr_reader :instructions # The mapping for blocks. This is used by instructions to map a # {Tacky::Block} to the proper `LLVM::BasicBlock`, since many llvm # instructions take basic blocks as parameters. # # @return [{Tacky::Block => ::LLVM::BasicBlock}] attr_reader :blocks # The generics that the function is being built with. # # @return [{::String => Concrete::Type}] attr_reader :generics # The parameters that are passed from the function. This is used to # convert {Tacky::Parameter} references to `LLVM::Value`s. # # @return [<::LLVM::Value>] attr_reader :params # The actual build. # # @return [Concrete::Build] attr_reader :build # (see Concrete::Build#types) attr_reader :types def_delegator :@build, :types # (see Concrete::Build#functions) attr_reader :functions def_delegator :@build, :functions # Returns the function that this is being built for. # # @return [Tacky::Function] attr_reader :function # Initialize the context. # # @param function [Tacky::Function] # @param build [Concrete::Build] # @param generics [{::String => Concrete::Type}] def initialize(function, build, generics) @function = function @build = build @generics = generics @instructions = [] @blocks = {} @params = [] freeze end def find(name) name.function? ? functions.fetch(name).last : types.fetch(name).last end end end end