mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
The primary change in this PR is to split the `Initializing` expression category into separate `ReprInitializing` and `InPlaceInitializing` categories, depending on whether initialization uses the types initializing representation, or is guaranteed to be in place. It also rationalizes and documents the SemIR-level semantics of those categories (including where #5545's "ephemeral entire reference" category will fit), and introduces two new inst kinds to close gaps exposed in the process. Some additional secondary changes: - Consistently format the storage arguments of initializers with `to`, regardless of whether initialization is in-place, and document the `to` notation. - Rename some inst kinds and functions, and restructure some of the code, for clarity and consistency with the new documentation. - Resolve a TODO to handle more category conversions in `CategoryConverter`, in order to make it easier to reason about category conversions. See #6588 and the review history of this PR for background. --------- Co-authored-by: Richard Smith <richard@metafoo.co.uk>
85 lines
3.4 KiB
C++
85 lines
3.4 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
|
|
|
|
#include "toolchain/lower/function_context.h"
|
|
#include "toolchain/sem_ir/expr_info.h"
|
|
#include "toolchain/sem_ir/file.h"
|
|
|
|
namespace Carbon::Lower {
|
|
|
|
auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
|
|
SemIR::AcquireValue inst) -> void {
|
|
auto inst_type = context.GetTypeIdOfInst(inst_id);
|
|
switch (context.GetValueRepr(inst_type).repr.kind) {
|
|
case SemIR::ValueRepr::Unknown:
|
|
CARBON_FATAL(
|
|
"Value acquisition for type with incomplete value representation");
|
|
case SemIR::ValueRepr::Dependent:
|
|
CARBON_FATAL(
|
|
"Value acquisition for type with dependent value representation");
|
|
case SemIR::ValueRepr::None:
|
|
// Nothing should use this value, but StubRef needs a value to
|
|
// propagate.
|
|
// TODO: Remove this now the StubRefs are gone.
|
|
context.SetLocal(inst_id,
|
|
llvm::PoisonValue::get(context.GetType(inst_type)));
|
|
break;
|
|
case SemIR::ValueRepr::Copy:
|
|
context.SetLocal(
|
|
inst_id,
|
|
context.LoadObject(inst_type, context.GetValue(inst.value_id)));
|
|
break;
|
|
case SemIR::ValueRepr::Pointer:
|
|
context.SetLocal(inst_id, context.GetValue(inst.value_id));
|
|
break;
|
|
case SemIR::ValueRepr::Custom:
|
|
CARBON_FATAL("TODO: Add support for AcquireValue with custom value rep");
|
|
}
|
|
}
|
|
|
|
auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
|
|
SemIR::MarkInPlaceInit inst) -> void {
|
|
context.SetLocal(inst_id, context.GetValue(inst.dest_id));
|
|
}
|
|
|
|
auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
|
|
SemIR::Temporary inst) -> void {
|
|
if (SemIR::GetExprCategory(context.sem_ir(), inst.init_id) !=
|
|
SemIR::ExprCategory::InPlaceInitializing) {
|
|
context.InitializeStorage(context.GetTypeIdOfInst(inst_id), inst.storage_id,
|
|
inst.init_id);
|
|
}
|
|
context.SetLocal(inst_id, context.GetValue(inst.storage_id));
|
|
}
|
|
|
|
auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
|
|
SemIR::TemporaryStorage /*inst*/) -> void {
|
|
context.SetLocal(
|
|
inst_id, context.CreateAlloca(context.GetTypeOfInst(inst_id), "temp"));
|
|
}
|
|
|
|
auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
|
|
SemIR::ValueAsRef inst) -> void {
|
|
CARBON_CHECK(SemIR::GetExprCategory(context.sem_ir(), inst.value_id) ==
|
|
SemIR::ExprCategory::Value);
|
|
auto inst_type = context.GetTypeIdOfInst(inst_id);
|
|
auto value_repr = context.GetValueRepr(inst_type);
|
|
CARBON_CHECK(value_repr.repr.kind == SemIR::ValueRepr::Pointer);
|
|
context.SetLocal(inst_id, context.GetValue(inst.value_id));
|
|
}
|
|
|
|
auto HandleInst(FunctionContext& context, SemIR::InstId inst_id,
|
|
SemIR::ValueOfInitializer inst) -> void {
|
|
CARBON_CHECK(SemIR::GetExprCategory(context.sem_ir(), inst.init_id) ==
|
|
SemIR::ExprCategory::ReprInitializing);
|
|
auto inst_type = context.GetTypeIdOfInst(inst_id);
|
|
auto value_repr = context.GetValueRepr(inst_type);
|
|
auto init_repr = context.GetInitRepr(inst_type);
|
|
CARBON_CHECK(value_repr.repr.kind == SemIR::ValueRepr::Copy);
|
|
CARBON_CHECK(init_repr.kind == SemIR::InitRepr::ByCopy);
|
|
context.SetLocal(inst_id, context.GetValue(inst.init_id));
|
|
}
|
|
|
|
} // namespace Carbon::Lower
|