inst.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/sem_ir/inst.h"
  5. namespace Carbon::SemIR {
  6. auto Inst::Print(llvm::raw_ostream& out) const -> void {
  7. out << "{kind: " << kind();
  8. auto print_args = [&](auto info) {
  9. using Info = decltype(info);
  10. if constexpr (Info::NumArgs > 0) {
  11. out << ", arg0: " << FromRaw<typename Info::template ArgType<0>>(arg0_);
  12. }
  13. if constexpr (Info::NumArgs > 1) {
  14. out << ", arg1: " << FromRaw<typename Info::template ArgType<1>>(arg1_);
  15. }
  16. };
  17. switch (kind()) {
  18. #define CARBON_SEM_IR_INST_KIND(Name) \
  19. case Name::Kind: \
  20. print_args(Internal::InstLikeTypeInfo<Name>()); \
  21. break;
  22. #include "toolchain/sem_ir/inst_kind.def"
  23. }
  24. if (type_id_.has_value()) {
  25. out << ", type: " << type_id_;
  26. }
  27. out << "}";
  28. }
  29. // Returns the IdKind of an instruction's argument, or None if there is no
  30. // argument with that index.
  31. template <typename InstKind, int ArgIndex>
  32. static constexpr auto IdKindFor() -> IdKind {
  33. using Info = Internal::InstLikeTypeInfo<InstKind>;
  34. if constexpr (ArgIndex < Info::NumArgs) {
  35. return IdKind::For<typename Info::template ArgType<ArgIndex>>;
  36. } else {
  37. return IdKind::None;
  38. }
  39. }
  40. const std::pair<IdKind, IdKind> Inst::ArgKindTable[] = {
  41. #define CARBON_SEM_IR_INST_KIND(Name) \
  42. {IdKindFor<Name, 0>(), IdKindFor<Name, 1>()},
  43. #include "toolchain/sem_ir/inst_kind.def"
  44. };
  45. } // namespace Carbon::SemIR