| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491 |
- // 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/sem_ir/formatter.h"
- #include <string>
- #include <utility>
- #include "common/ostream.h"
- #include "llvm/ADT/Sequence.h"
- #include "llvm/ADT/StringExtras.h"
- #include "llvm/Support/SaveAndRestore.h"
- #include "toolchain/base/fixed_size_value_store.h"
- #include "toolchain/base/kind_switch.h"
- #include "toolchain/base/shared_value_stores.h"
- #include "toolchain/lex/tokenized_buffer.h"
- #include "toolchain/parse/tree.h"
- #include "toolchain/parse/tree_and_subtrees.h"
- #include "toolchain/sem_ir/builtin_function_kind.h"
- #include "toolchain/sem_ir/constant.h"
- #include "toolchain/sem_ir/entity_with_params_base.h"
- #include "toolchain/sem_ir/expr_info.h"
- #include "toolchain/sem_ir/function.h"
- #include "toolchain/sem_ir/ids.h"
- #include "toolchain/sem_ir/name_scope.h"
- #include "toolchain/sem_ir/typed_insts.h"
- #include "toolchain/sem_ir/vtable.h"
- // TODO: Consider addressing recursion here, although it's not critical because
- // the formatter isn't required to work on arbitrary code. Still, it may help
- // in the future to debug complex code.
- // NOLINTBEGIN(misc-no-recursion)
- namespace Carbon::SemIR {
- Formatter::Formatter(
- const File* sem_ir, int total_ir_count,
- Parse::GetTreeAndSubtreesFn get_tree_and_subtrees,
- const FixedSizeValueStore<SemIR::CheckIRId, bool>* include_ir_in_dumps,
- bool use_dump_sem_ir_ranges)
- : sem_ir_(sem_ir),
- inst_namer_(sem_ir_, total_ir_count),
- get_tree_and_subtrees_(get_tree_and_subtrees),
- include_ir_in_dumps_(include_ir_in_dumps),
- use_dump_sem_ir_ranges_(use_dump_sem_ir_ranges),
- // Create a placeholder visible chunk and assign it to all instructions
- // that don't have a chunk of their own.
- tentative_inst_chunks_(sem_ir_->insts(), AddChunkNoFlush(true)) {
- if (use_dump_sem_ir_ranges_) {
- ComputeNodeParents();
- }
- // Create empty placeholder chunks for instructions that we output lazily.
- for (auto inst_id : llvm::concat<const InstId>(
- sem_ir_->constants().array_ref(),
- sem_ir_->inst_blocks().Get(InstBlockId::Imports))) {
- tentative_inst_chunks_.Set(inst_id, AddChunkNoFlush(false));
- }
- // Create a real chunk for the start of the output.
- AddChunkNoFlush(true);
- }
- auto Formatter::Format() -> void {
- out_ << "--- " << sem_ir_->filename() << "\n";
- FormatTopLevelScopeIfUsed(InstNamer::ScopeId::Constants,
- sem_ir_->constants().array_ref(),
- /*use_tentative_output_scopes=*/true);
- FormatTopLevelScopeIfUsed(InstNamer::ScopeId::Imports,
- sem_ir_->inst_blocks().Get(InstBlockId::Imports),
- /*use_tentative_output_scopes=*/true);
- FormatTopLevelScopeIfUsed(
- InstNamer::ScopeId::File,
- sem_ir_->inst_blocks().GetOrEmpty(sem_ir_->top_inst_block_id()),
- /*use_tentative_output_scopes=*/false);
- for (const auto& [id, interface] : sem_ir_->interfaces().enumerate()) {
- FormatInterface(id, interface);
- }
- for (const auto& [id, assoc_const] :
- sem_ir_->associated_constants().enumerate()) {
- FormatAssociatedConstant(id, assoc_const);
- }
- for (const auto& [id, impl] : sem_ir_->impls().enumerate()) {
- FormatImpl(id, impl);
- }
- for (const auto& [id, class_info] : sem_ir_->classes().enumerate()) {
- FormatClass(id, class_info);
- }
- for (const auto& [id, vtable] : sem_ir_->vtables().enumerate()) {
- FormatVtable(id, vtable);
- }
- for (const auto& [id, function] : sem_ir_->functions().enumerate()) {
- FormatFunction(id, function);
- }
- for (const auto& [id, specific] : sem_ir_->specifics().enumerate()) {
- FormatSpecific(id, specific);
- }
- out_ << "\n";
- }
- auto Formatter::ComputeNodeParents() -> void {
- CARBON_CHECK(!node_parents_);
- node_parents_ = NodeParentStore::MakeWithExplicitSize(
- sem_ir_->parse_tree().size(), Parse::NodeId::None);
- for (auto n : sem_ir_->parse_tree().postorder()) {
- for (auto child : get_tree_and_subtrees_().children(n)) {
- node_parents_->Set(child, n);
- }
- }
- }
- auto Formatter::Write(llvm::raw_ostream& out) -> void {
- FlushChunk();
- for (const auto& chunk : output_chunks_) {
- if (chunk.include_in_output) {
- out << chunk.chunk;
- }
- }
- }
- auto Formatter::FlushChunk() -> void {
- CARBON_CHECK(output_chunks_.back().chunk.empty());
- output_chunks_.back().chunk = std::move(buffer_);
- buffer_.clear();
- }
- auto Formatter::AddChunkNoFlush(bool include_in_output) -> size_t {
- CARBON_CHECK(buffer_.empty());
- output_chunks_.push_back({.include_in_output = include_in_output});
- return output_chunks_.size() - 1;
- }
- auto Formatter::AddChunk(bool include_in_output) -> size_t {
- FlushChunk();
- return AddChunkNoFlush(include_in_output);
- }
- auto Formatter::IncludeChunkInOutput(size_t chunk) -> void {
- if (chunk == output_chunks_.size() - 1) {
- return;
- }
- if (auto& current_chunk = output_chunks_.back();
- !current_chunk.include_in_output) {
- current_chunk.dependencies.push_back(chunk);
- return;
- }
- llvm::SmallVector<size_t> to_add = {chunk};
- while (!to_add.empty()) {
- auto& chunk = output_chunks_[to_add.pop_back_val()];
- if (chunk.include_in_output) {
- continue;
- }
- chunk.include_in_output = true;
- to_add.append(chunk.dependencies);
- chunk.dependencies.clear();
- }
- }
- auto Formatter::ShouldIncludeInstByIR(InstId inst_id) -> bool {
- const auto* import_ir = GetCanonicalFileAndInstId(sem_ir_, inst_id).first;
- return include_ir_in_dumps_->Get(import_ir->check_ir_id());
- }
- // Returns true for a `DefinitionStart` node.
- static auto IsDefinitionStart(Parse::NodeKind node_kind) -> bool {
- switch (node_kind) {
- case Parse::NodeKind::BuiltinFunctionDefinitionStart:
- case Parse::NodeKind::ChoiceDefinitionStart:
- case Parse::NodeKind::ClassDefinitionStart:
- case Parse::NodeKind::FunctionDefinitionStart:
- case Parse::NodeKind::ImplDefinitionStart:
- case Parse::NodeKind::InterfaceDefinitionStart:
- case Parse::NodeKind::NamedConstraintDefinitionStart:
- return true;
- default:
- return false;
- }
- }
- auto Formatter::ShouldFormatEntity(InstId decl_id) -> bool {
- if (!decl_id.has_value()) {
- return true;
- }
- if (!ShouldIncludeInstByIR(decl_id)) {
- return false;
- }
- if (!use_dump_sem_ir_ranges_) {
- return true;
- }
- // When there are dump ranges, ignore imported instructions.
- auto loc_id = sem_ir_->insts().GetCanonicalLocId(decl_id);
- if (loc_id.kind() != LocId::Kind::NodeId) {
- return false;
- }
- const auto& tree_and_subtrees = get_tree_and_subtrees_();
- // This takes the earliest token from either the node or its first postorder
- // child. The first postorder child isn't necessarily the earliest token in
- // the subtree (for example, it can miss modifiers), but finding the earliest
- // token requires walking *all* children, whereas this approach is
- // constant-time.
- auto begin_node_id = *tree_and_subtrees.postorder(loc_id.node_id()).begin();
- // Non-defining declarations will be associated with a `Decl` node.
- // Definitions will have a `DefinitionStart` for which we can use the parent
- // to find the `Definition`, giving a range that includes the definition's
- // body.
- auto end_node_id = loc_id.node_id();
- if (IsDefinitionStart(sem_ir_->parse_tree().node_kind(end_node_id))) {
- end_node_id = node_parents_->Get(end_node_id);
- }
- Lex::InclusiveTokenRange range = {
- .begin = sem_ir_->parse_tree().node_token(begin_node_id),
- .end = sem_ir_->parse_tree().node_token(end_node_id)};
- return sem_ir_->parse_tree().tokens().OverlapsWithDumpSemIRRange(range);
- }
- auto Formatter::ShouldFormatEntity(const EntityWithParamsBase& entity) -> bool {
- return ShouldFormatEntity(entity.latest_decl_id());
- }
- auto Formatter::ShouldFormatInst(InstId inst_id) -> bool {
- if (!use_dump_sem_ir_ranges_) {
- return true;
- }
- // When there are dump ranges, ignore imported instructions.
- auto loc_id = sem_ir_->insts().GetCanonicalLocId(inst_id);
- if (loc_id.kind() != LocId::Kind::NodeId) {
- return false;
- }
- auto token = sem_ir_->parse_tree().node_token(loc_id.node_id());
- return sem_ir_->parse_tree().tokens().OverlapsWithDumpSemIRRange(
- Lex::InclusiveTokenRange{.begin = token, .end = token});
- }
- auto Formatter::OpenBrace() -> void {
- // Put the constant value of an instruction before any braced block, rather
- // than at the end.
- FormatPendingConstantValue(AddSpace::After);
- // Put the imported-from library name before the definition of the entity.
- FormatPendingImportedFrom(AddSpace::After);
- out_ << '{';
- indent_ += 2;
- after_open_brace_ = true;
- }
- auto Formatter::CloseBrace() -> void {
- indent_ -= 2;
- if (!after_open_brace_) {
- Indent();
- }
- out_ << '}';
- after_open_brace_ = false;
- }
- auto Formatter::Semicolon() -> void {
- FormatPendingImportedFrom(AddSpace::Before);
- out_ << ';';
- }
- auto Formatter::Indent(int offset) -> void {
- if (after_open_brace_) {
- out_ << '\n';
- after_open_brace_ = false;
- }
- out_.indent(indent_ + offset);
- }
- auto Formatter::IndentLabel() -> void {
- CARBON_CHECK(indent_ >= 2);
- if (!after_open_brace_) {
- out_ << '\n';
- }
- Indent(-2);
- }
- auto Formatter::FormatTopLevelScopeIfUsed(InstNamer::ScopeId scope_id,
- llvm::ArrayRef<InstId> block,
- bool use_tentative_output_scopes)
- -> void {
- if (!use_tentative_output_scopes && use_dump_sem_ir_ranges_) {
- // Don't format the scope if no instructions are in a dump range.
- block = block.drop_while(
- [&](InstId inst_id) { return !ShouldFormatInst(inst_id); });
- }
- if (block.empty()) {
- return;
- }
- llvm::SaveAndRestore scope(scope_, scope_id);
- // Note, we don't use OpenBrace() / CloseBrace() here because we always want
- // a newline to avoid misformatting if the first instruction is omitted.
- out_ << "\n" << inst_namer_.GetScopeName(scope_id) << " {\n";
- indent_ += 2;
- for (const InstId inst_id : block) {
- // Format instructions when needed, but do nothing for elided entries;
- // unlike normal code blocks, scopes are non-sequential so skipped
- // instructions are assumed to be uninteresting.
- if (use_tentative_output_scopes) {
- // This is for constants and imports. These use tentative logic to
- // determine whether an instruction is printed.
- TentativeOutputScope scope(*this, tentative_inst_chunks_.Get(inst_id));
- FormatInst(inst_id);
- } else if (ShouldFormatInst(inst_id)) {
- // This is for the file scope. It uses only the range-based filtering.
- FormatInst(inst_id);
- }
- }
- out_ << "}\n";
- indent_ -= 2;
- }
- auto Formatter::FormatClass(ClassId id, const Class& class_info) -> void {
- if (!ShouldFormatEntity(class_info)) {
- return;
- }
- FormatEntityStart("class", class_info, id);
- llvm::SaveAndRestore class_scope(scope_, inst_namer_.GetScopeFor(id));
- if (class_info.scope_id.has_value()) {
- out_ << ' ';
- OpenBrace();
- FormatCodeBlock(class_info.body_block_id);
- Indent();
- out_ << "complete_type_witness = ";
- FormatName(class_info.complete_type_witness_id);
- out_ << "\n";
- if (class_info.vtable_decl_id.has_value()) {
- Indent();
- out_ << "vtable_decl = ";
- FormatName(class_info.vtable_decl_id);
- out_ << "\n";
- }
- FormatNameScope(class_info.scope_id, "!members:\n");
- CloseBrace();
- } else {
- Semicolon();
- }
- out_ << '\n';
- FormatEntityEnd(class_info.generic_id);
- }
- auto Formatter::FormatVtable(VtableId id, const Vtable& vtable_info) -> void {
- out_ << '\n';
- Indent();
- out_ << "vtable ";
- FormatName(id);
- out_ << ' ';
- OpenBrace();
- for (auto function_id :
- sem_ir_->inst_blocks().Get(vtable_info.virtual_functions_id)) {
- Indent();
- FormatArg(function_id);
- out_ << '\n';
- }
- CloseBrace();
- out_ << '\n';
- }
- auto Formatter::FormatInterface(InterfaceId id, const Interface& interface_info)
- -> void {
- if (!ShouldFormatEntity(interface_info)) {
- return;
- }
- FormatEntityStart("interface", interface_info, id);
- llvm::SaveAndRestore interface_scope(scope_, inst_namer_.GetScopeFor(id));
- if (interface_info.scope_id.has_value()) {
- out_ << ' ';
- OpenBrace();
- FormatCodeBlock(interface_info.body_block_id);
- // Always include the !members label because we always list the witness in
- // this section.
- IndentLabel();
- out_ << "!members:\n";
- FormatNameScope(interface_info.scope_id);
- Indent();
- out_ << "witness = ";
- FormatArg(interface_info.associated_entities_id);
- out_ << "\n";
- CloseBrace();
- } else {
- Semicolon();
- }
- out_ << '\n';
- FormatEntityEnd(interface_info.generic_id);
- }
- auto Formatter::FormatAssociatedConstant(AssociatedConstantId id,
- const AssociatedConstant& assoc_const)
- -> void {
- if (!ShouldFormatEntity(assoc_const.decl_id)) {
- return;
- }
- FormatEntityStart("assoc_const", assoc_const.decl_id, assoc_const.generic_id,
- id);
- llvm::SaveAndRestore assoc_const_scope(scope_, inst_namer_.GetScopeFor(id));
- out_ << " ";
- FormatName(assoc_const.name_id);
- out_ << ":! ";
- FormatTypeOfInst(assoc_const.decl_id);
- if (assoc_const.default_value_id.has_value()) {
- out_ << " = ";
- FormatArg(assoc_const.default_value_id);
- }
- out_ << ";\n";
- FormatEntityEnd(assoc_const.generic_id);
- }
- auto Formatter::FormatImpl(ImplId id, const Impl& impl_info) -> void {
- if (!ShouldFormatEntity(impl_info)) {
- return;
- }
- FormatEntityStart("impl", impl_info, id);
- llvm::SaveAndRestore impl_scope(scope_, inst_namer_.GetScopeFor(id));
- out_ << ": ";
- FormatName(impl_info.self_id);
- out_ << " as ";
- FormatName(impl_info.constraint_id);
- if (impl_info.is_complete()) {
- out_ << ' ';
- OpenBrace();
- FormatCodeBlock(impl_info.body_block_id);
- // Print the !members label even if the name scope is empty because we
- // always list the witness in this section.
- IndentLabel();
- out_ << "!members:\n";
- if (impl_info.scope_id.has_value()) {
- FormatNameScope(impl_info.scope_id);
- }
- Indent();
- out_ << "witness = ";
- FormatArg(impl_info.witness_id);
- out_ << "\n";
- CloseBrace();
- } else {
- Semicolon();
- }
- out_ << '\n';
- FormatEntityEnd(impl_info.generic_id);
- }
- auto Formatter::FormatFunction(FunctionId id, const Function& fn) -> void {
- if (!ShouldFormatEntity(fn)) {
- return;
- }
- std::string function_start;
- switch (fn.virtual_modifier) {
- case FunctionFields::VirtualModifier::Virtual:
- function_start += "virtual ";
- break;
- case FunctionFields::VirtualModifier::Abstract:
- function_start += "abstract ";
- break;
- case FunctionFields::VirtualModifier::Override:
- function_start += "override ";
- break;
- case FunctionFields::VirtualModifier::None:
- break;
- }
- if (fn.is_extern) {
- function_start += "extern ";
- }
- function_start += "fn";
- FormatEntityStart(function_start, fn, id);
- llvm::SaveAndRestore function_scope(scope_, inst_namer_.GetScopeFor(id));
- auto return_type_info = ReturnTypeInfo::ForFunction(*sem_ir_, fn);
- FormatParamList(fn.call_params_id, return_type_info.is_valid() &&
- return_type_info.has_return_slot());
- if (fn.builtin_function_kind() != BuiltinFunctionKind::None) {
- out_ << " = \""
- << FormatEscaped(fn.builtin_function_kind().name(),
- /*use_hex_escapes=*/true)
- << "\"";
- }
- if (fn.thunk_decl_id().has_value()) {
- out_ << " [thunk ";
- FormatArg(fn.thunk_decl_id());
- out_ << "]";
- }
- if (!fn.body_block_ids.empty()) {
- out_ << ' ';
- OpenBrace();
- for (auto block_id : fn.body_block_ids) {
- IndentLabel();
- FormatLabel(block_id);
- out_ << ":\n";
- FormatCodeBlock(block_id);
- }
- CloseBrace();
- } else {
- Semicolon();
- }
- out_ << '\n';
- FormatEntityEnd(fn.generic_id);
- }
- auto Formatter::FormatSpecificRegion(const Generic& generic,
- const Specific& specific,
- GenericInstIndex::Region region,
- llvm::StringRef region_name) -> void {
- if (!specific.GetValueBlock(region).has_value()) {
- return;
- }
- if (!region_name.empty()) {
- IndentLabel();
- out_ << "!" << region_name << ":\n";
- }
- for (auto [generic_inst_id, specific_inst_id] : llvm::zip_longest(
- sem_ir_->inst_blocks().GetOrEmpty(generic.GetEvalBlock(region)),
- sem_ir_->inst_blocks().GetOrEmpty(specific.GetValueBlock(region)))) {
- Indent();
- if (generic_inst_id) {
- FormatName(*generic_inst_id);
- } else {
- out_ << "<missing>";
- }
- out_ << " => ";
- if (specific_inst_id) {
- FormatName(*specific_inst_id);
- } else {
- out_ << "<missing>";
- }
- out_ << "\n";
- }
- }
- auto Formatter::FormatSpecific(SpecificId id, const Specific& specific)
- -> void {
- const auto& generic = sem_ir_->generics().Get(specific.generic_id);
- if (!ShouldFormatEntity(generic.decl_id)) {
- // Omit specifics if we also omitted the generic.
- return;
- }
- if (specific.IsUnresolved()) {
- // Omit specifics that were never resolved. Such specifics exist only to
- // track the way the arguments were spelled, and that information is
- // conveyed entirely by the name of the specific. These specifics may also
- // not be referenced by any SemIR that we format, so including them adds
- // clutter and possibly emits references to instructions we didn't name.
- return;
- }
- llvm::SaveAndRestore generic_scope(
- scope_, inst_namer_.GetScopeFor(specific.generic_id));
- out_ << "\n";
- out_ << "specific ";
- FormatName(id);
- out_ << " ";
- OpenBrace();
- FormatSpecificRegion(generic, specific, GenericInstIndex::Region::Declaration,
- "");
- FormatSpecificRegion(generic, specific, GenericInstIndex::Region::Definition,
- "definition");
- CloseBrace();
- out_ << "\n";
- }
- auto Formatter::FormatGenericStart(llvm::StringRef entity_kind,
- GenericId generic_id) -> void {
- const auto& generic = sem_ir_->generics().Get(generic_id);
- out_ << "\n";
- Indent();
- out_ << "generic " << entity_kind << " ";
- FormatName(generic_id);
- llvm::SaveAndRestore generic_scope(scope_,
- inst_namer_.GetScopeFor(generic_id));
- FormatParamList(generic.bindings_id);
- out_ << " ";
- OpenBrace();
- FormatCodeBlock(generic.decl_block_id);
- if (generic.definition_block_id.has_value()) {
- IndentLabel();
- out_ << "!definition:\n";
- FormatCodeBlock(generic.definition_block_id);
- }
- }
- auto Formatter::FormatEntityEnd(GenericId generic_id) -> void {
- if (generic_id.has_value()) {
- CloseBrace();
- out_ << '\n';
- }
- }
- auto Formatter::FormatParamList(InstBlockId params_id, bool has_return_slot)
- -> void {
- if (!params_id.has_value()) {
- // TODO: This happens for imported functions, for which we don't currently
- // import the call parameters list.
- return;
- }
- llvm::StringLiteral close = ")";
- out_ << "(";
- llvm::ListSeparator sep;
- for (InstId param_id : sem_ir_->inst_blocks().Get(params_id)) {
- auto is_out_param = sem_ir_->insts().Is<OutParam>(param_id);
- if (is_out_param) {
- // TODO: An input parameter following an output parameter is formatted a
- // bit strangely. For example, alternating input and output parameters
- // produces:
- //
- // fn @F(%in1: %t) -> %out1: %t, %in2: %t -> %out2: %t
- //
- // This doesn't actually happen right now, though.
- out_ << std::exchange(close, llvm::StringLiteral(""));
- out_ << " -> ";
- } else {
- out_ << sep;
- }
- if (!param_id.has_value()) {
- out_ << "invalid";
- continue;
- }
- // Don't include the name of the return slot parameter if the function
- // doesn't have a return slot; the name won't be used for anything in that
- // case.
- // TODO: Should the call parameter even exist in that case? There isn't a
- // corresponding argument in a `call` instruction.
- if (!is_out_param || has_return_slot) {
- FormatName(param_id);
- out_ << ": ";
- }
- FormatTypeOfInst(param_id);
- }
- out_ << close;
- }
- auto Formatter::FormatCodeBlock(InstBlockId block_id) -> void {
- bool elided = false;
- for (const InstId inst_id : sem_ir_->inst_blocks().GetOrEmpty(block_id)) {
- if (ShouldFormatInst(inst_id)) {
- FormatInst(inst_id);
- elided = false;
- } else if (!elided) {
- // When formatting a block, leave a hint that instructions were elided.
- Indent();
- out_ << "<elided>\n";
- elided = true;
- }
- }
- }
- auto Formatter::FormatTrailingBlock(InstBlockId block_id) -> void {
- out_ << ' ';
- OpenBrace();
- FormatCodeBlock(block_id);
- CloseBrace();
- }
- auto Formatter::FormatNameScope(NameScopeId id, llvm::StringRef label) -> void {
- const auto& scope = sem_ir_->name_scopes().Get(id);
- if (scope.entries().empty() && scope.extended_scopes().empty() &&
- scope.import_ir_scopes().empty() && !scope.is_cpp_scope() &&
- !scope.has_error()) {
- // Name scope is empty.
- return;
- }
- if (!label.empty()) {
- IndentLabel();
- out_ << label;
- }
- for (auto [name_id, result] : scope.entries()) {
- Indent();
- out_ << ".";
- FormatName(name_id);
- switch (result.access_kind()) {
- case AccessKind::Public:
- break;
- case AccessKind::Protected:
- out_ << " [protected]";
- break;
- case AccessKind::Private:
- out_ << " [private]";
- break;
- }
- out_ << " = ";
- if (result.is_poisoned()) {
- out_ << "<poisoned>";
- } else {
- FormatName(result.is_found() ? result.target_inst_id() : InstId::None);
- }
- out_ << "\n";
- }
- for (auto extended_scope_id : scope.extended_scopes()) {
- Indent();
- out_ << "extend ";
- FormatName(extended_scope_id);
- out_ << "\n";
- }
- // This is used to cluster all "Core//prelude/..." imports, but not
- // "Core//prelude" itself. This avoids unrelated churn in test files when we
- // add or remove an unused prelude file, but is intended to still show the
- // existence of indirect imports.
- bool has_prelude_components = false;
- for (auto [import_ir_id, unused] : scope.import_ir_scopes()) {
- auto label = GetImportIRLabel(import_ir_id);
- if (label.starts_with("Core//prelude/")) {
- if (has_prelude_components) {
- // Only print the existence once.
- continue;
- } else {
- has_prelude_components = true;
- label = "Core//prelude/...";
- }
- }
- Indent();
- out_ << "import " << label << "\n";
- }
- if (scope.is_cpp_scope()) {
- Indent();
- out_ << "import Cpp//...\n";
- }
- if (scope.has_error()) {
- Indent();
- out_ << "has_error\n";
- }
- }
- auto Formatter::FormatInst(InstId inst_id) -> void {
- if (!inst_id.has_value()) {
- Indent();
- out_ << "none\n";
- return;
- }
- if (!in_terminator_sequence_) {
- Indent();
- }
- auto inst = sem_ir_->insts().GetWithAttachedType(inst_id);
- CARBON_KIND_SWITCH(inst) {
- case CARBON_KIND(Branch branch): {
- out_ << Branch::Kind.ir_name() << " ";
- FormatLabel(branch.target_id);
- out_ << "\n";
- in_terminator_sequence_ = false;
- return;
- }
- case CARBON_KIND(BranchIf branch_if): {
- out_ << "if ";
- FormatName(branch_if.cond_id);
- out_ << " " << Branch::Kind.ir_name() << " ";
- FormatLabel(branch_if.target_id);
- out_ << " else ";
- in_terminator_sequence_ = true;
- return;
- }
- case CARBON_KIND(BranchWithArg branch_with_arg): {
- out_ << BranchWithArg::Kind.ir_name() << " ";
- FormatLabel(branch_with_arg.target_id);
- out_ << "(";
- FormatName(branch_with_arg.arg_id);
- out_ << ")\n";
- in_terminator_sequence_ = false;
- return;
- }
- default: {
- FormatInstLhs(inst_id, inst);
- out_ << inst.kind().ir_name();
- // Add constants for everything except `ImportRefUnloaded`.
- if (!inst.Is<ImportRefUnloaded>()) {
- pending_constant_value_ =
- sem_ir_->constant_values().GetAttached(inst_id);
- pending_constant_value_is_self_ =
- sem_ir_->constant_values().GetInstIdIfValid(
- pending_constant_value_) == inst_id;
- }
- FormatInstRhs(inst);
- // This usually prints the constant, but when `FormatInstRhs` prints it
- // first (or for `ImportRefUnloaded`), this does nothing.
- FormatPendingConstantValue(AddSpace::Before);
- out_ << "\n";
- return;
- }
- }
- }
- auto Formatter::FormatPendingImportedFrom(AddSpace space_where) -> void {
- if (pending_imported_from_.empty()) {
- return;
- }
- if (space_where == AddSpace::Before) {
- out_ << ' ';
- }
- out_ << "[from \"" << FormatEscaped(pending_imported_from_) << "\"]";
- if (space_where == AddSpace::After) {
- out_ << ' ';
- }
- pending_imported_from_ = llvm::StringRef();
- }
- auto Formatter::FormatPendingConstantValue(AddSpace space_where) -> void {
- if (pending_constant_value_ == ConstantId::NotConstant) {
- return;
- }
- if (space_where == AddSpace::Before) {
- out_ << ' ';
- }
- out_ << '[';
- if (pending_constant_value_.has_value()) {
- switch (sem_ir_->constant_values().GetDependence(pending_constant_value_)) {
- case ConstantDependence::None:
- out_ << "concrete";
- break;
- case ConstantDependence::PeriodSelf:
- out_ << "symbolic_self";
- break;
- // TODO: Consider renaming this. This will cause a lot of SemIR churn.
- case ConstantDependence::Checked:
- out_ << "symbolic";
- break;
- case ConstantDependence::Template:
- out_ << "template";
- break;
- }
- if (!pending_constant_value_is_self_) {
- out_ << " = ";
- FormatConstant(pending_constant_value_);
- }
- } else {
- out_ << pending_constant_value_;
- }
- out_ << ']';
- if (space_where == AddSpace::After) {
- out_ << ' ';
- }
- pending_constant_value_ = ConstantId::NotConstant;
- }
- auto Formatter::FormatInstLhs(InstId inst_id, Inst inst) -> void {
- // Every typed instruction is named, and there are some untyped instructions
- // that have names (such as `ImportRefUnloaded`). When there's a typed
- // instruction with no name, it means an instruction is incorrectly not named
- // -- but should be printed as such.
- bool has_name = inst_namer_.has_name(inst_id);
- if (!has_name && !inst.kind().has_type()) {
- return;
- }
- FormatName(inst_id);
- if (inst.kind().has_type()) {
- out_ << ": ";
- switch (GetExprCategory(*sem_ir_, inst_id)) {
- case ExprCategory::NotExpr:
- case ExprCategory::Error:
- case ExprCategory::Value:
- case ExprCategory::Mixed:
- break;
- case ExprCategory::DurableRef:
- case ExprCategory::EphemeralRef:
- out_ << "ref ";
- break;
- case ExprCategory::Initializing:
- out_ << "init ";
- break;
- }
- FormatTypeOfInst(inst_id);
- }
- out_ << " = ";
- }
- auto Formatter::FormatInstArgAndKind(Inst::ArgAndKind arg_and_kind) -> void {
- GetFormatArgFn(arg_and_kind.kind())(*this, arg_and_kind.value());
- }
- auto Formatter::FormatInstRhs(Inst inst) -> void {
- CARBON_KIND_SWITCH(inst) {
- case SemIR::InstKind::ArrayInit:
- case SemIR::InstKind::StructInit:
- case SemIR::InstKind::TupleInit: {
- auto init = inst.As<AnyAggregateInit>();
- FormatArgs(init.elements_id);
- FormatReturnSlotArg(init.dest_id);
- return;
- }
- case SemIR::InstKind::ImportRefLoaded:
- case SemIR::InstKind::ImportRefUnloaded:
- FormatImportRefRhs(inst.As<AnyImportRef>());
- return;
- case SemIR::InstKind::OutParam:
- case SemIR::InstKind::RefParam:
- case SemIR::InstKind::ValueParam: {
- auto param = inst.As<AnyParam>();
- FormatArgs(param.index);
- // Omit pretty_name because it's an implementation detail of
- // pretty-printing.
- return;
- }
- case CARBON_KIND(AssociatedConstantDecl decl): {
- FormatArgs(decl.assoc_const_id);
- llvm::SaveAndRestore scope(scope_,
- inst_namer_.GetScopeFor(decl.assoc_const_id));
- FormatTrailingBlock(decl.decl_block_id);
- return;
- }
- case CARBON_KIND(BindSymbolicName bind): {
- // A BindSymbolicName with no value is a purely symbolic binding, such as
- // the `Self` in an interface. Don't print out `none` for the value.
- if (bind.value_id.has_value()) {
- FormatArgs(bind.entity_name_id, bind.value_id);
- } else {
- FormatArgs(bind.entity_name_id);
- }
- return;
- }
- case CARBON_KIND(BlockArg block): {
- out_ << " ";
- FormatLabel(block.block_id);
- return;
- }
- case CARBON_KIND(Call call): {
- FormatCallRhs(call);
- return;
- }
- case CARBON_KIND(ClassDecl decl): {
- FormatDeclRhs(decl.class_id,
- sem_ir_->classes().Get(decl.class_id).pattern_block_id,
- decl.decl_block_id);
- return;
- }
- case CARBON_KIND(CustomLayoutType type): {
- out_ << " {";
- auto layout = sem_ir_->custom_layouts().Get(type.layout_id);
- out_ << "size=" << layout[CustomLayoutId::SizeIndex]
- << ", align=" << layout[CustomLayoutId::AlignIndex];
- for (auto [field, offset] :
- llvm::zip(sem_ir_->struct_type_fields().Get(type.fields_id),
- layout.drop_front(CustomLayoutId::FirstFieldIndex))) {
- out_ << ", .";
- FormatName(field.name_id);
- out_ << "@" << offset << ": ";
- FormatInstAsType(field.type_inst_id);
- }
- out_ << "}";
- return;
- }
- case CARBON_KIND(FloatValue value): {
- llvm::SmallVector<char, 16> buffer;
- sem_ir_->floats().Get(value.float_id).toString(buffer);
- out_ << " " << buffer;
- return;
- }
- case CARBON_KIND(FunctionDecl decl): {
- FormatDeclRhs(decl.function_id,
- sem_ir_->functions().Get(decl.function_id).pattern_block_id,
- decl.decl_block_id);
- return;
- }
- case InstKind::ImportCppDecl: {
- FormatImportCppDeclRhs();
- return;
- }
- case CARBON_KIND(ImplDecl decl): {
- FormatDeclRhs(decl.impl_id,
- sem_ir_->impls().Get(decl.impl_id).pattern_block_id,
- decl.decl_block_id);
- return;
- }
- case CARBON_KIND(InitializeFrom init): {
- FormatArgs(init.src_id);
- FormatReturnSlotArg(init.dest_id);
- return;
- }
- case CARBON_KIND(InstValue inst): {
- out_ << ' ';
- OpenBrace();
- // TODO: Should we use a more compact representation in the case where the
- // inst is a SpliceBlock?
- FormatInst(inst.inst_id);
- CloseBrace();
- return;
- }
- case CARBON_KIND(InterfaceDecl decl): {
- FormatDeclRhs(
- decl.interface_id,
- sem_ir_->interfaces().Get(decl.interface_id).pattern_block_id,
- decl.decl_block_id);
- return;
- }
- case CARBON_KIND(IntValue value): {
- out_ << " ";
- sem_ir_->ints()
- .Get(value.int_id)
- .print(out_, sem_ir_->types().IsSignedInt(value.type_id));
- return;
- }
- case CARBON_KIND(NameBindingDecl name): {
- FormatTrailingBlock(name.pattern_block_id);
- return;
- }
- case CARBON_KIND(Namespace ns): {
- if (ns.import_id.has_value()) {
- FormatArgs(ns.import_id, ns.name_scope_id);
- } else {
- FormatArgs(ns.name_scope_id);
- }
- return;
- }
- case CARBON_KIND(ReturnExpr ret): {
- FormatArgs(ret.expr_id);
- if (ret.dest_id.has_value()) {
- FormatReturnSlotArg(ret.dest_id);
- }
- return;
- }
- case CARBON_KIND(ReturnSlot ret): {
- // Omit inst.type_inst_id because it's not semantically significant.
- FormatArgs(ret.storage_id);
- return;
- }
- case InstKind::ReturnSlotPattern:
- // No-op because type_id is the only semantically significant field,
- // and it's handled separately.
- return;
- case CARBON_KIND(SpliceBlock splice): {
- FormatArgs(splice.result_id);
- FormatTrailingBlock(splice.block_id);
- return;
- }
- case CARBON_KIND(StructType struct_type): {
- out_ << " {";
- llvm::ListSeparator sep;
- for (auto field :
- sem_ir_->struct_type_fields().Get(struct_type.fields_id)) {
- out_ << sep << ".";
- FormatName(field.name_id);
- out_ << ": ";
- FormatInstAsType(field.type_inst_id);
- }
- out_ << "}";
- return;
- }
- case CARBON_KIND(WhereExpr where): {
- FormatArgs(where.period_self_id);
- FormatTrailingBlock(where.requirements_id);
- return;
- }
- default:
- FormatInstRhsDefault(inst);
- return;
- }
- }
- auto Formatter::FormatInstRhsDefault(Inst inst) -> void {
- auto arg0 = inst.arg0_and_kind();
- if (arg0.kind() == IdKind::None) {
- return;
- }
- out_ << " ";
- FormatInstArgAndKind(arg0);
- auto arg1 = inst.arg1_and_kind();
- if (arg1.kind() == IdKind::None) {
- return;
- }
- // Several instructions have a second operand that's a specific ID. We
- // don't include it in the argument list if there is no corresponding
- // specific, that is, when we're not in a generic context.
- if (auto arg1_specific_id = arg1.TryAs<SpecificId>();
- arg1_specific_id && !arg1_specific_id->has_value()) {
- return;
- }
- out_ << ", ";
- FormatInstArgAndKind(arg1);
- }
- auto Formatter::FormatCallRhs(Call inst) -> void {
- out_ << " ";
- FormatArg(inst.callee_id);
- if (!inst.args_id.has_value()) {
- out_ << "(<none>)";
- return;
- }
- llvm::ArrayRef<InstId> args = sem_ir_->inst_blocks().Get(inst.args_id);
- auto return_info = ReturnTypeInfo::ForType(*sem_ir_, inst.type_id);
- if (!return_info.is_valid()) {
- out_ << "(<invalid return info>)";
- return;
- }
- bool has_return_slot = return_info.has_return_slot();
- InstId return_slot_arg_id = InstId::None;
- if (has_return_slot) {
- return_slot_arg_id = args.consume_back();
- }
- llvm::ListSeparator sep;
- out_ << '(';
- for (auto inst_id : args) {
- out_ << sep;
- FormatArg(inst_id);
- }
- out_ << ')';
- if (has_return_slot) {
- FormatReturnSlotArg(return_slot_arg_id);
- }
- }
- auto Formatter::FormatImportCppDeclRhs() -> void {
- out_ << " ";
- OpenBrace();
- for (ImportCpp import_cpp : sem_ir_->import_cpps().values()) {
- Indent();
- out_ << "import Cpp ";
- if (import_cpp.library_id.has_value()) {
- out_ << "\""
- << FormatEscaped(
- sem_ir_->string_literal_values().Get(import_cpp.library_id))
- << "\"";
- } else {
- out_ << "inline";
- }
- out_ << "\n";
- }
- CloseBrace();
- }
- auto Formatter::FormatImportRefRhs(AnyImportRef inst) -> void {
- out_ << " ";
- auto import_ir_inst = sem_ir_->import_ir_insts().Get(inst.import_ir_inst_id);
- FormatArg(import_ir_inst.ir_id());
- out_ << ", ";
- if (inst.entity_name_id.has_value()) {
- // Prefer to show the entity name when possible.
- FormatArg(inst.entity_name_id);
- } else {
- // Show a name based on the location when possible, or the numeric
- // instruction as a last resort.
- const auto& import_ir = sem_ir_->import_irs().Get(import_ir_inst.ir_id());
- auto loc_id =
- import_ir.sem_ir->insts().GetCanonicalLocId(import_ir_inst.inst_id());
- switch (loc_id.kind()) {
- case LocId::Kind::None: {
- out_ << import_ir_inst.inst_id() << " [no loc]";
- break;
- }
- case LocId::Kind::ImportIRInstId: {
- // TODO: Probably don't want to format each indirection, but maybe
- // reuse GetCanonicalImportIRInst?
- out_ << import_ir_inst.inst_id() << " [indirect]";
- break;
- }
- case LocId::Kind::NodeId: {
- // Formats a NodeId from the import.
- const auto& tree = import_ir.sem_ir->parse_tree();
- auto token = tree.node_token(loc_id.node_id());
- out_ << "loc" << tree.tokens().GetLineNumber(token) << "_"
- << tree.tokens().GetColumnNumber(token);
- break;
- }
- case LocId::Kind::InstId:
- CARBON_FATAL("Unexpected LocId: {0}", loc_id);
- }
- }
- out_ << ", "
- << (inst.kind == InstKind::ImportRefLoaded ? "loaded" : "unloaded");
- }
- auto Formatter::FormatArg(EntityNameId id) -> void {
- if (!id.has_value()) {
- out_ << "_";
- return;
- }
- const auto& info = sem_ir_->entity_names().Get(id);
- FormatName(info.name_id);
- if (info.bind_index().has_value()) {
- out_ << ", " << info.bind_index().index;
- }
- if (info.is_template) {
- out_ << ", template";
- }
- }
- auto Formatter::FormatArg(FacetTypeId id) -> void {
- const auto& info = sem_ir_->facet_types().Get(id);
- // Nothing output to indicate that this is a facet type since this is only
- // used as the argument to a `facet_type` instruction.
- out_ << "<";
- llvm::ListSeparator sep(" & ");
- if (info.extend_constraints.empty()) {
- out_ << "type";
- } else {
- for (auto interface : info.extend_constraints) {
- out_ << sep;
- FormatName(interface.interface_id);
- if (interface.specific_id.has_value()) {
- out_ << ", ";
- FormatName(interface.specific_id);
- }
- }
- }
- if (info.other_requirements || !info.self_impls_constraints.empty() ||
- !info.rewrite_constraints.empty()) {
- out_ << " where ";
- llvm::ListSeparator and_sep(" and ");
- if (!info.self_impls_constraints.empty()) {
- out_ << and_sep << ".Self impls ";
- llvm::ListSeparator amp_sep(" & ");
- for (auto interface : info.self_impls_constraints) {
- out_ << amp_sep;
- FormatName(interface.interface_id);
- if (interface.specific_id.has_value()) {
- out_ << ", ";
- FormatName(interface.specific_id);
- }
- }
- }
- for (auto rewrite : info.rewrite_constraints) {
- out_ << and_sep;
- FormatArg(rewrite.lhs_id);
- out_ << " = ";
- FormatArg(rewrite.rhs_id);
- }
- if (info.other_requirements) {
- out_ << and_sep << "TODO";
- }
- }
- out_ << ">";
- }
- auto Formatter::FormatArg(ImportIRId id) -> void {
- if (id.has_value()) {
- out_ << GetImportIRLabel(id);
- } else {
- out_ << id;
- }
- }
- auto Formatter::FormatArg(IntId id) -> void {
- // We don't know the signedness to use here. Default to unsigned.
- sem_ir_->ints().Get(id).print(out_, /*isSigned=*/false);
- }
- auto Formatter::FormatArg(NameScopeId id) -> void {
- OpenBrace();
- FormatNameScope(id);
- CloseBrace();
- }
- auto Formatter::FormatArg(InstBlockId id) -> void {
- if (!id.has_value()) {
- out_ << "invalid";
- return;
- }
- out_ << '(';
- llvm::ListSeparator sep;
- for (auto inst_id : sem_ir_->inst_blocks().Get(id)) {
- out_ << sep;
- FormatArg(inst_id);
- }
- out_ << ')';
- }
- auto Formatter::FormatArg(AbsoluteInstBlockId id) -> void {
- FormatArg(static_cast<InstBlockId>(id));
- }
- auto Formatter::FormatArg(RealId id) -> void {
- // TODO: Format with a `.` when the exponent is near zero.
- const auto& real = sem_ir_->reals().Get(id);
- real.mantissa.print(out_, /*isSigned=*/false);
- out_ << (real.is_decimal ? 'e' : 'p') << real.exponent;
- }
- auto Formatter::FormatArg(StringLiteralValueId id) -> void {
- out_ << '"'
- << FormatEscaped(sem_ir_->string_literal_values().Get(id),
- /*use_hex_escapes=*/true)
- << '"';
- }
- auto Formatter::FormatReturnSlotArg(InstId dest_id) -> void {
- out_ << " to ";
- FormatArg(dest_id);
- }
- auto Formatter::FormatName(NameId id) -> void {
- out_ << sem_ir_->names().GetFormatted(id);
- }
- auto Formatter::FormatName(InstId id) -> void {
- if (id.has_value()) {
- IncludeChunkInOutput(tentative_inst_chunks_.Get(id));
- }
- out_ << inst_namer_.GetNameFor(scope_, id);
- }
- auto Formatter::FormatName(SpecificId id) -> void {
- const auto& specific = sem_ir_->specifics().Get(id);
- FormatName(specific.generic_id);
- FormatArg(specific.args_id);
- }
- auto Formatter::FormatName(SpecificInterfaceId id) -> void {
- const auto& interface = sem_ir_->specific_interfaces().Get(id);
- FormatName(interface.interface_id);
- if (interface.specific_id.has_value()) {
- out_ << ", ";
- FormatArg(interface.specific_id);
- }
- }
- auto Formatter::FormatLabel(InstBlockId id) -> void {
- out_ << inst_namer_.GetLabelFor(scope_, id);
- }
- auto Formatter::FormatConstant(ConstantId id) -> void {
- if (!id.has_value()) {
- out_ << "<not constant>";
- return;
- }
- auto inst_id = GetInstWithConstantValue(*sem_ir_, id);
- FormatName(inst_id);
- // For an attached constant, also list the unattached constant.
- if (id.is_symbolic() && sem_ir_->constant_values()
- .GetSymbolicConstant(id)
- .generic_id.has_value()) {
- // TODO: Skip printing this if it's the same as `inst_id`.
- auto unattached_inst_id = sem_ir_->constant_values().GetInstId(id);
- out_ << " (";
- FormatName(unattached_inst_id);
- out_ << ")";
- }
- }
- auto Formatter::FormatInstAsType(InstId id) -> void {
- if (!id.has_value()) {
- out_ << "invalid";
- return;
- }
- // Types are formatted in the `constants` scope because they typically refer
- // to constants.
- llvm::SaveAndRestore file_scope(scope_, InstNamer::ScopeId::Constants);
- if (auto const_id = sem_ir_->constant_values().GetAttached(id);
- const_id.has_value()) {
- FormatConstant(const_id);
- } else {
- // Type instruction didn't have a constant value. Fall back to printing
- // the instruction name.
- FormatArg(id);
- }
- }
- auto Formatter::FormatTypeOfInst(InstId id) -> void {
- auto type_id = sem_ir_->insts().GetAttachedType(id);
- if (!type_id.has_value()) {
- out_ << "invalid";
- return;
- }
- // Types are formatted in the `constants` scope because they typically refer
- // to constants.
- llvm::SaveAndRestore file_scope(scope_, InstNamer::ScopeId::Constants);
- FormatConstant(sem_ir_->types().GetConstantId(type_id));
- }
- auto Formatter::GetImportIRLabel(ImportIRId id) -> std::string {
- CARBON_CHECK(id.has_value(),
- "Callers are responsible for checking `id.has_value`");
- const auto& import_ir = *sem_ir_->import_irs().Get(id).sem_ir;
- CARBON_CHECK(import_ir.library_id().has_value());
- auto package_id = import_ir.package_id();
- llvm::StringRef package_name =
- package_id.AsIdentifierId().has_value()
- ? import_ir.identifiers().Get(package_id.AsIdentifierId())
- : package_id.AsSpecialName();
- llvm::StringRef library_name =
- (import_ir.library_id() != LibraryNameId::Default)
- ? import_ir.string_literal_values().Get(
- import_ir.library_id().AsStringLiteralValueId())
- : "default";
- return llvm::formatv("{0}//{1}", package_name, library_name);
- }
- } // namespace Carbon::SemIR
- // NOLINTEND(misc-no-recursion)
|