Files
carbon-lang/toolchain/check/context.cpp
T
Jon Ross-PerkinsandGeoff Romer 7209ad7c9f Generate Destroy impls for classes (#5873)
Although this focused on `Destroy` support, some choices here around
`implicit_type_impls` are because copy/move will likely follow a similar
approach. I'm trying not to predict too much about how we'll structure
those, but I'm putting `Destroy` impl logic in a file that could perhaps
be shared with those. They'd likely be interested in similar things,
e.g. traversing members of types (particularly class, struct literal,
tuple literal).

At present this sets the destroy function as `no_op` which is consistent
with current logic, but has a TODO to correctly define.

Constant importing for functions changes slightly due to some issues I
was having with `GetFunctionType`. zygoloid suggested this approach to
avoid `EvalInst` logic.

Adds a flag for controlling whether to generating these impls. While
this does generation for `class`, as noted above this'll also need to be
done for tuples and struct literals, which would leave the `none.carbon`
min_prelude unable to use any types. Note if destruction *would* occur,
it'll still look up `Core.Destroy` for that and fail, but that's already
true of any test using `none.carbon`. I'm trying to use the flag to see
if we can keep `none.carbon` working mostly-consistently.

I'd tried separating out the flag to #5852, but that got a lot of
pushback over whether the behavior was appropriate. I'm hoping that the
interactions here make it clearer why the particular approach -- the
goal is not to enable advanced testing, or create some new end-user
behavior that we really support, it's just to keep no-prelude tests
functional. The main question raised there was why not just keep
generating `impl T as Core.Destroy` if `fn destroy` is present -- but I
think here it should be apparent that would require additional
complexity, as the generation of `impl T as Core.Destroy` is not
currently conditioned based on the implementation of `fn destroy`. I'd
rather add complexity to this flag only if it's enabling interesting
test functionality.

---------

Co-authored-by: Geoff Romer <gromer@google.com>
2025-08-04 19:39:22 +00:00

101 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
#include "toolchain/check/context.h"
#include <string>
#include <utility>
#include "common/check.h"
#include "toolchain/check/deferred_definition_worklist.h"
#include "toolchain/sem_ir/ids.h"
namespace Carbon::Check {
Context::Context(DiagnosticEmitterBase* emitter,
Parse::GetTreeAndSubtreesFn tree_and_subtrees_getter,
SemIR::File* sem_ir, int imported_ir_count, int total_ir_count,
bool gen_implicit_type_impls, llvm::raw_ostream* vlog_stream)
: emitter_(emitter),
tree_and_subtrees_getter_(tree_and_subtrees_getter),
sem_ir_(sem_ir),
gen_implicit_type_impls_(gen_implicit_type_impls),
vlog_stream_(vlog_stream),
node_stack_(sem_ir->parse_tree(), vlog_stream),
inst_block_stack_("inst_block_stack_", *sem_ir, vlog_stream),
pattern_block_stack_("pattern_block_stack_", *sem_ir, vlog_stream),
param_and_arg_refs_stack_(*sem_ir, vlog_stream, node_stack_),
args_type_info_stack_("args_type_info_stack_", *sem_ir, vlog_stream),
decl_name_stack_(this),
scope_stack_(sem_ir_),
deferred_definition_worklist_(vlog_stream),
vtable_stack_("vtable_stack_", *sem_ir, vlog_stream),
check_ir_map_(
FixedSizeValueStore<SemIR::CheckIRId, SemIR::ImportIRId>::
MakeWithExplicitSize(total_ir_count, SemIR::ImportIRId::None)),
global_init_(this),
region_stack_([this](SemIR::LocId loc_id, std::string label) {
TODO(loc_id, label);
}) {
// Prepare fields which relate to the number of IRs available for import.
import_irs().Reserve(imported_ir_count);
import_ir_constant_values_.reserve(imported_ir_count);
}
auto Context::TODO(SemIR::LocId loc_id, std::string label) -> bool {
CARBON_DIAGNOSTIC(SemanticsTodo, Error, "semantics TODO: `{0}`", std::string);
emitter_->Emit(loc_id, SemanticsTodo, std::move(label));
return false;
}
auto Context::TODO(SemIR::InstId loc_inst_id, std::string label) -> bool {
return TODO(SemIR::LocId(loc_inst_id), label);
}
auto Context::VerifyOnFinish() const -> void {
// Information in all the various context objects should be cleaned up as
// various pieces of context go out of scope. At this point, nothing should
// remain, so we verify stacks are empty. `node_stack_` is an exception
// because it ends containing all top-level entities.
inst_block_stack_.VerifyOnFinish();
pattern_block_stack_.VerifyOnFinish();
param_and_arg_refs_stack_.VerifyOnFinish();
args_type_info_stack_.VerifyOnFinish();
CARBON_CHECK(struct_type_fields_stack_.empty());
CARBON_CHECK(field_decls_stack_.empty());
decl_name_stack_.VerifyOnFinish();
decl_introducer_state_stack_.VerifyOnFinish();
scope_stack_.VerifyOnFinish();
generic_region_stack_.VerifyOnFinish();
vtable_stack_.VerifyOnFinish();
region_stack_.VerifyOnFinish();
CARBON_CHECK(impl_lookup_stack_.empty());
#ifndef NDEBUG
if (auto verify = sem_ir_->Verify(); !verify.ok()) {
CARBON_FATAL("{0}Built invalid semantics IR: {1}\n", sem_ir_,
verify.error());
}
#endif
}
auto Context::PrintForStackDump(llvm::raw_ostream& output) const -> void {
output << "Check::Context\n";
// In a stack dump, this is probably indented by a tab. We treat that as 8
// spaces then add a couple to indent past the Context label.
constexpr int Indent = 10;
output.indent(Indent);
output << "filename: " << tokens().source().filename() << "\n";
node_stack_.PrintForStackDump(Indent, output);
inst_block_stack_.PrintForStackDump(Indent, output);
pattern_block_stack_.PrintForStackDump(Indent, output);
param_and_arg_refs_stack_.PrintForStackDump(Indent, output);
args_type_info_stack_.PrintForStackDump(Indent, output);
}
} // namespace Carbon::Check