mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-25 16:21:11 +00:00
This adds `DefinitionInfo` for `Define`-based configuration so that
parameters are optional. It also makes it easier to provide the
equivalent functions on both `Definition` and `Define`.
A common pattern used here is to change from a `switch` with in-line
`case`s to instead have `case`s that call an overloaded function. What's
happening here is that the instruction type is used to select an
overload, and if an overload is not defined, a compiler error would
result. Meanwhile, clusters of overloads are being defined using
`requires`-based templating, so that equivalent implementations are not
copied. This addresses a limitation of a vanilla `switch` approach where
it's hard to have redundant cases using conditional logic, while also
getting compiler errors when adding new `InstKind` entries, which had
been a significant part of why we used macros previously.
This starts hitting some odd clang-format edge cases causing
`CARBON_KIND_SWITCH(inst){` (missing space), which I haven't seen
before. Adding `CARBON_KIND_SWITCH` to .clang-format works around it.
112 lines
3.9 KiB
C++
112 lines
3.9 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#ifndef CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_
|
|
#define CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_
|
|
|
|
#include "llvm/IR/Constants.h"
|
|
#include "llvm/IR/LLVMContext.h"
|
|
#include "llvm/IR/Module.h"
|
|
#include "toolchain/sem_ir/file.h"
|
|
#include "toolchain/sem_ir/inst_namer.h"
|
|
|
|
namespace Carbon::Lower {
|
|
|
|
// Context and shared functionality for lowering handlers.
|
|
class FileContext {
|
|
public:
|
|
explicit FileContext(llvm::LLVMContext& llvm_context,
|
|
llvm::StringRef module_name, const SemIR::File& sem_ir,
|
|
const SemIR::InstNamer* inst_namer,
|
|
llvm::raw_ostream* vlog_stream);
|
|
|
|
// Lowers the SemIR::File to LLVM IR. Should only be called once, and handles
|
|
// the main execution loop.
|
|
auto Run() -> std::unique_ptr<llvm::Module>;
|
|
|
|
// Gets a callable's function. Returns nullptr for a builtin.
|
|
auto GetFunction(SemIR::FunctionId function_id) -> llvm::Function* {
|
|
return functions_[function_id.index];
|
|
}
|
|
|
|
// Returns a lowered type for the given type_id.
|
|
auto GetType(SemIR::TypeId type_id) -> llvm::Type* {
|
|
// InvalidType should not be passed in.
|
|
if (type_id == SemIR::TypeId::TypeType) {
|
|
return GetTypeType();
|
|
}
|
|
CARBON_CHECK(type_id.index >= 0) << type_id;
|
|
CARBON_CHECK(types_[type_id.index]) << "Missing type " << type_id;
|
|
return types_[type_id.index];
|
|
}
|
|
|
|
// Returns a lowered value to use for a value of type `type`.
|
|
auto GetTypeAsValue() -> llvm::Constant* {
|
|
return llvm::ConstantStruct::get(GetTypeType());
|
|
}
|
|
|
|
// Returns a global value for the given instruction.
|
|
auto GetGlobal(SemIR::InstId inst_id) -> llvm::Value*;
|
|
|
|
// Returns the empty LLVM struct type used to represent the type `type`.
|
|
auto GetTypeType() -> llvm::StructType* {
|
|
if (!type_type_) {
|
|
// `type` is lowered to an empty LLVM StructType.
|
|
type_type_ = llvm::StructType::create(*llvm_context_, {}, "type");
|
|
}
|
|
return type_type_;
|
|
}
|
|
|
|
auto llvm_context() -> llvm::LLVMContext& { return *llvm_context_; }
|
|
auto llvm_module() -> llvm::Module& { return *llvm_module_; }
|
|
auto sem_ir() -> const SemIR::File& { return *sem_ir_; }
|
|
auto inst_namer() -> const SemIR::InstNamer* { return inst_namer_; }
|
|
|
|
private:
|
|
// Builds the declaration for the given function, which should then be cached
|
|
// by the caller.
|
|
auto BuildFunctionDecl(SemIR::FunctionId function_id) -> llvm::Function*;
|
|
|
|
// Builds the definition for the given function. If the function is only a
|
|
// declaration with no definition, does nothing.
|
|
auto BuildFunctionDefinition(SemIR::FunctionId function_id) -> void;
|
|
|
|
// Builds the type for the given instruction, which should then be cached by
|
|
// the caller.
|
|
auto BuildType(SemIR::InstId inst_id) -> llvm::Type*;
|
|
|
|
// State for building the LLVM IR.
|
|
llvm::LLVMContext* llvm_context_;
|
|
std::unique_ptr<llvm::Module> llvm_module_;
|
|
|
|
// The input SemIR.
|
|
const SemIR::File* const sem_ir_;
|
|
|
|
// The instruction namer, if given.
|
|
const SemIR::InstNamer* const inst_namer_;
|
|
|
|
// The optional vlog stream.
|
|
llvm::raw_ostream* vlog_stream_;
|
|
|
|
// Maps callables to lowered functions. SemIR treats callables as the
|
|
// canonical form of a function, so lowering needs to do the same.
|
|
// We resize this directly to the (often large) correct size.
|
|
llvm::SmallVector<llvm::Function*, 0> functions_;
|
|
|
|
// Provides lowered versions of types.
|
|
// We resize this directly to the (often large) correct size.
|
|
llvm::SmallVector<llvm::Type*, 0> types_;
|
|
|
|
// Lowered version of the builtin type `type`.
|
|
llvm::StructType* type_type_ = nullptr;
|
|
|
|
// Maps constants to their lowered values.
|
|
// We resize this directly to the (often large) correct size.
|
|
llvm::SmallVector<llvm::Constant*, 0> constants_;
|
|
};
|
|
|
|
} // namespace Carbon::Lower
|
|
|
|
#endif // CARBON_TOOLCHAIN_LOWER_FILE_CONTEXT_H_
|