inst.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #include <utility>
  6. #include "toolchain/sem_ir/file.h"
  7. namespace Carbon::SemIR {
  8. auto Inst::Print(llvm::raw_ostream& out) const -> void {
  9. out << "{kind: " << kind();
  10. auto print_args = [&](auto info) {
  11. using Info = decltype(info);
  12. if constexpr (Info::NumArgs > 0) {
  13. out << ", arg0: " << FromRaw<typename Info::template ArgType<0>>(arg0_);
  14. }
  15. if constexpr (Info::NumArgs > 1) {
  16. out << ", arg1: " << FromRaw<typename Info::template ArgType<1>>(arg1_);
  17. }
  18. };
  19. switch (kind()) {
  20. #define CARBON_SEM_IR_INST_KIND(Name) \
  21. case Name::Kind: \
  22. print_args(Internal::InstLikeTypeInfo<Name>()); \
  23. break;
  24. #include "toolchain/sem_ir/inst_kind.def"
  25. }
  26. if (type_id_.has_value()) {
  27. out << ", type: " << type_id_;
  28. }
  29. out << "}";
  30. }
  31. // Returns the IdKind of an instruction's argument, or None if there is no
  32. // argument with that index.
  33. template <typename InstKind, int ArgIndex>
  34. static constexpr auto IdKindFor() -> IdKind {
  35. using Info = Internal::InstLikeTypeInfo<InstKind>;
  36. if constexpr (ArgIndex < Info::NumArgs) {
  37. return IdKind::For<typename Info::template ArgType<ArgIndex>>;
  38. } else {
  39. return IdKind::None;
  40. }
  41. }
  42. const std::pair<IdKind, IdKind> Inst::ArgKindTable[] = {
  43. #define CARBON_SEM_IR_INST_KIND(Name) \
  44. {IdKindFor<Name, 0>(), IdKindFor<Name, 1>()},
  45. #include "toolchain/sem_ir/inst_kind.def"
  46. };
  47. InstStore::InstStore(File* file, int32_t reserved_inst_ids)
  48. : file_(file), values_(file->check_ir_id(), reserved_inst_ids) {}
  49. auto InstStore::GetUnattachedType(TypeId type_id) const -> TypeId {
  50. return file_->types().GetUnattachedType(type_id);
  51. }
  52. } // namespace Carbon::SemIR