diff --git a/scripts/lldbinit.py b/scripts/lldbinit.py index 5b89b78060b41..82f5f289545ce 100644 --- a/scripts/lldbinit.py +++ b/scripts/lldbinit.py @@ -44,13 +44,16 @@ def print_usage() -> None: Dumps the value of an associated ID, using the C++ Dump() functions. Usage: - dump [|-- || ] + dump [|-- |[.]] Args: CONTEXT is the dump context, such a SemIR::Context reference, a SemIR::File, a Parse::Context, or a Lex::TokenizeBuffer. EXPR is a C++ expression such as a variable name. Use `--` to prevent it from being treated as a TYPE and ID. + IR is the CheckIRId(N) in the form `irN`. Not all ID types have an `ir` + prefix, the ones which do are dumped with that prefix, such as for + `inst` as `ir1.inst2`. TYPE can be `inst`, `constant`, `generic`, `impl`, `entity_name`, etc. See the `Label` string in `IdBase` classes to find possible TYPE names, though only Id types that have a matching `Make...Id()` function are @@ -74,8 +77,13 @@ def print_usage() -> None: context = args[0] - # The set of "Make" functions in dump.cpp. + # The set of "Make" functions in dump.cpp. These use ADL to find the factory + # function in `check/` or in `sem_ir/`. id_types = { + "inst": "MakeInstId", + } + # These id types don't have an IdTag embedded in them. + untagged_id_types = { "class": "SemIR::MakeClassId", "constant": "SemIR::MakeConstantId", "symbolic_constant": "SemIR::MakeSymbolicConstantId", @@ -85,7 +93,6 @@ def print_usage() -> None: "generic": "SemIR::MakeGenericId", "impl": "SemIR::MakeImplId", "inst_block": "SemIR::MakeInstBlockId", - "inst": "SemIR::MakeInstId", "interface": "SemIR::MakeInterfaceId", "name": "SemIR::MakeNameId", "name_scope": "SemIR::MakeNameScopeId", @@ -110,27 +117,31 @@ def print_dump(context: str, expr: str) -> None: # Try to find a type + id from the input args. If not, the id will be passed # through directly to C++, as it can be a variable name. - id_type = None + found_id_type = False - # Look for as a single argument. - if m := re.fullmatch("([a-z_]+)(\\d+)", args[1]): - if m[1] in id_types: + # Look for . as a single argument. + if m := re.fullmatch("ir(\\d+)\\.([a-z_]+)(\\d+)", args[1]): + if m[2] in id_types: if len(args) != 2: print_usage() return - id_type = m[1] - print_dump(context, f"{id_types[id_type]}({m[2]})") + id_type = m[2] + print_dump( + context, f"{id_types[id_type]}({context}, {m[1]}, {m[3]})" + ) + found_id_type = True - # Look for as two arguments. - if not id_type: - if args[1] in id_types: - if len(args) != 3: + # Look for as a single argument. + if m := re.fullmatch("([a-z_]+)(\\d+)", args[1]): + if m[1] in untagged_id_types: + if len(args) != 2: print_usage() return - id_type = args[1] - print_dump(context, f"{id_types[id_type]}({args[2]})") + id_type = m[1] + print_dump(context, f"{untagged_id_types[id_type]}({m[2]})") + found_id_type = True - if not id_type: + if not found_id_type: # Use `--` to escape a variable name like `inst22`. if args[1] == "--": expr = " ".join(args[2:]) diff --git a/toolchain/check/BUILD b/toolchain/check/BUILD index 1454203448656..caadbb41ada6c 100644 --- a/toolchain/check/BUILD +++ b/toolchain/check/BUILD @@ -180,6 +180,7 @@ cc_library( "//toolchain/parse:tree", "//toolchain/sem_ir:dump", "//toolchain/sem_ir:file", + "//toolchain/sem_ir:typed_insts", ], # Always link dump methods. alwayslink = 1, diff --git a/toolchain/check/context.h b/toolchain/check/context.h index 07e4ae427f57f..fa9ac2f1c5cd1 100644 --- a/toolchain/check/context.h +++ b/toolchain/check/context.h @@ -243,6 +243,7 @@ class Context { // Directly expose SemIR::File data accessors for brevity in calls. // -------------------------------------------------------------------------- + auto check_ir_id() -> SemIR::CheckIRId { return sem_ir().check_ir_id(); } auto identifiers() -> SharedValueStores::IdentifierStore& { return sem_ir().identifiers(); } diff --git a/toolchain/check/dump.cpp b/toolchain/check/dump.cpp index efc10643ae197..f1a9126fc106a 100644 --- a/toolchain/check/dump.cpp +++ b/toolchain/check/dump.cpp @@ -25,6 +25,7 @@ #include "toolchain/parse/tree.h" #include "toolchain/sem_ir/dump.h" #include "toolchain/sem_ir/file.h" +#include "toolchain/sem_ir/ids.h" namespace Carbon::Check { @@ -138,6 +139,17 @@ LLVM_DUMP_METHOD static auto Dump(const Context& context, SemIR::TypeId type_id) return SemIR::Dump(context.sem_ir(), type_id); } +LLVM_DUMP_METHOD static auto MakeInstId(Context& context, int check_ir_id, + int id) -> SemIR::InstId { + if (SemIR::CheckIRId(check_ir_id) == context.check_ir_id()) { + return SemIR::MakeInstId(context.sem_ir(), check_ir_id, id); + } else { + const auto& import_ir = context.import_irs().Get( + context.check_ir_map().Get(SemIR::CheckIRId(check_ir_id))); + return SemIR::MakeInstId(*import_ir.sem_ir, check_ir_id, id); + } +} + } // namespace Carbon::Check #endif // NDEBUG diff --git a/toolchain/sem_ir/dump.cpp b/toolchain/sem_ir/dump.cpp index f687c2f1a2568..5cb6c9446bf97 100644 --- a/toolchain/sem_ir/dump.cpp +++ b/toolchain/sem_ir/dump.cpp @@ -452,7 +452,16 @@ LLVM_DUMP_METHOD static auto MakeImplId(int id) -> ImplId { return ImplId(id); } LLVM_DUMP_METHOD static auto MakeInstBlockId(int id) -> InstBlockId { return InstBlockId(id); } -LLVM_DUMP_METHOD static auto MakeInstId(int id) -> InstId { return InstId(id); } +LLVM_DUMP_METHOD auto MakeInstId(const SemIR::File& file, int check_ir_id, + int id) -> InstId { + if (file.check_ir_id() != SemIR::CheckIRId(check_ir_id)) { + llvm::errs() + << "SemIR::MakeInstId given a CheckIRId for the wrong SemIR::File. " + "Maybe you want to dump from a Check::Context.\n"; + return SemIR::InstId::None; + } + return InstId(file.insts().GetIdTag().Apply(id)); +} LLVM_DUMP_METHOD static auto MakeInterfaceId(int id) -> InterfaceId { return InterfaceId(id); } diff --git a/toolchain/sem_ir/dump.h b/toolchain/sem_ir/dump.h index e90bb72c74d25..8a65dede7a870 100644 --- a/toolchain/sem_ir/dump.h +++ b/toolchain/sem_ir/dump.h @@ -42,6 +42,8 @@ auto Dump(const File& file, StructTypeFieldsId struct_type_fields_id) -> std::string; auto Dump(const File& file, TypeId type_id) -> std::string; +auto MakeInstId(const SemIR::File& file, int check_ir_id, int id) -> InstId; + } // namespace Carbon::SemIR #endif // NDEBUG