mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-15 13:38:29 +00:00
When identifying a facet type, defer evaluation of LookupImplWitnesses by redirecting them to evaluate into IdentifiedWitness. Then the caller of identify will need to substitute the IdentifiedWitnesses, either with witnesses of their own, or with LookupImplWitness to cause a lookup operation. This allows: - An `impl as` on a named constraint can subst the witnesses in designators from the named constraint with a witness to the impl itself. This prevents them from looking for and finding some other overlapping impl. (This PR) - Impl lookup to collect rewrite/equality requirements from all named constraints and then use the impls it has already found to evaluate any designators in those requirements instead of performing impl lookup again (creating a cycle). (Done in https://github.com/carbon-language/carbon-lang/pull/7944) - Similarly, an `impl as` can collect impls and equality requirements from all named constraints and verify them correctly by replacing witnesses that should refer to the impl with the correct witness instead of performing a lookup and finding an overlapping impl. (Done in https://github.com/carbon-language/carbon-lang/pull/7937) - ImplWitnessAccess to be able to find rewrites from a named constraint, not just from the initial DeclaredFacetType. (TBD) To do this we need to introduce the concept of "identified" specifics. In order to identify a facet type, we must construct a specific for each require decl in a named constraint. These specifics will have IdentifiedWitnesses inside them, but their args would otherwise match a specific for the require decl constructed in another context. To avoid polluting the canonical specific namespace, we introduce a second namespace of specifics that are "identified" since they are created by identifying. As we have introduced a new global state that modifies eval behaviour, we must disable that state when resolving a specific (as per https://github.com/carbon-language/carbon-lang/pull/7923). But inside an "identified" specific, we preserve the behaviour so that the witnesses modified by identify (through SubstPeriodSelf or through evaluating a require specific) have their evaluation deferred.
106 lines
4.1 KiB
C++
106 lines
4.1 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/base/kind_switch.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,
|
|
llvm::raw_ostream* vlog_stream, bool mangle_string_fingerprint)
|
|
: emitter_(emitter),
|
|
tree_and_subtrees_getter_(tree_and_subtrees_getter),
|
|
sem_ir_(sem_ir),
|
|
total_ir_count_(total_ir_count),
|
|
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_(*this),
|
|
deferred_definition_worklist_(vlog_stream),
|
|
generic_region_stack_(vlog_stream),
|
|
vtable_stack_("vtable_stack_", *sem_ir, vlog_stream),
|
|
check_ir_map_(CheckIRToImportIRStore::MakeWithExplicitSize(
|
|
total_ir_count_, SemIR::ImportIRId::None)),
|
|
global_init_(this),
|
|
region_stack_([this](SemIR::LocId loc_id, std::string label) {
|
|
TODO(loc_id, label);
|
|
}),
|
|
core_identifiers_(&identifiers()),
|
|
mangle_string_fingerprint_(mangle_string_fingerprint) {
|
|
// 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());
|
|
CARBON_CHECK(declaring_impl_decls_.empty());
|
|
CARBON_CHECK(eval_impl_lookup_to_identified_witness_ == 0);
|
|
CARBON_CHECK(return_form_expr_ == std::nullopt);
|
|
|
|
#ifndef NDEBUG
|
|
if (auto verify = sem_ir_->Verify(); !verify.ok()) {
|
|
CARBON_FATAL("Built invalid SemIR: {1}\n", 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
|