mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
Remove calls to `InstStore::GetLocId()` to build a LocId from an InstId now that they can be constructed directly from the InstId. Most uses of LocId are just plumbing, so this does not affect them. However places that want to look inside the LocId do not want to work with the InstId form. In these places, introduce `InstStore::GetResolvedLocId()` which converts a LocId (or an InstId as an optimization) into a LocId which is not backed by an InstId. These locations can be printed (they have a line and column when they are a NodeId), they can have flags added to them (`ToImplicit`, `ToTokenOnly`), they can be converted to an underlying ImportIRInstId, or they may be `None`. `Dump()` is made to print a resolved location instead of printing the InstId in the location, since (at least in my experience) the resolved location is what is interesting in debugging, and this saves manual `MakeInstId` steps in the debugger every time a location is of interest. The LocId constructor from InstId is made `explicit` to add clarity to function calls passing an `inst_id` now directly instead of calling `context.insts().GetLocId(inst_id)`. To avoid needing to construct `SemIR::LocId(...)` explicitly in all cases though, the diagnostics code in Check uses `DiagnosticLocId` as its template parameter which accepts InstId as well and does the construction of LocId from it. Because LocId now requires an explicit construction from InstId, any callers to `AddInst()` functions will have to explicitly convert to LocId if they had an InstId, but not if they pass a NodeId. To make this difference clear to callers, we `requires` that the input type can be converted to LocId. This ensures that passing an InstId results in an error at the callsite where the InstId is passed, instead of generating a compiler error when trying to construct `LocIdAndInst` inside `AddInst()`, which is less clear about what went wrong and doesn't seem entirely intentional. --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
92 lines
3.5 KiB
C++
92 lines
3.5 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"
|
|
|
|
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)
|
|
: emitter_(emitter),
|
|
tree_and_subtrees_getter_(tree_and_subtrees_getter),
|
|
sem_ir_(sem_ir),
|
|
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_),
|
|
vtable_stack_("vtable_stack_", *sem_ir, vlog_stream),
|
|
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);
|
|
check_ir_map_.resize(total_ir_count, SemIR::ImportIRId::None);
|
|
}
|
|
|
|
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;
|
|
|
|
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
|