ids.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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/ids.h"
  5. #include "llvm/Support/ConvertUTF.h"
  6. #include "llvm/Support/NativeFormatting.h"
  7. #include "toolchain/base/value_ids.h"
  8. #include "toolchain/sem_ir/singleton_insts.h"
  9. #include "toolchain/sem_ir/typed_insts.h"
  10. namespace Carbon::SemIR {
  11. auto InstId::Print(llvm::raw_ostream& out) const -> void {
  12. if (IsSingletonInstId(*this)) {
  13. out << Label << "(" << SingletonInstKinds[index] << ")";
  14. } else {
  15. IdBase::Print(out);
  16. }
  17. }
  18. auto ConstantId::Print(llvm::raw_ostream& out, bool disambiguate) const
  19. -> void {
  20. if (!has_value()) {
  21. IdBase::Print(out);
  22. return;
  23. }
  24. if (is_concrete()) {
  25. if (disambiguate) {
  26. out << "concrete_constant(";
  27. }
  28. out << concrete_inst_id();
  29. if (disambiguate) {
  30. out << ")";
  31. }
  32. } else if (is_symbolic()) {
  33. out << symbolic_id();
  34. } else {
  35. CARBON_CHECK(!is_constant());
  36. out << "runtime";
  37. }
  38. }
  39. auto GenericInstIndex::Print(llvm::raw_ostream& out) const -> void {
  40. out << "generic_inst";
  41. if (has_value()) {
  42. out << (region() == Declaration ? "_in_decl" : "_in_def") << index();
  43. } else {
  44. out << "<none>";
  45. }
  46. }
  47. auto BoolValue::Print(llvm::raw_ostream& out) const -> void {
  48. if (*this == False) {
  49. out << "false";
  50. } else if (*this == True) {
  51. out << "true";
  52. } else {
  53. CARBON_FATAL("Invalid bool value {0}", index);
  54. }
  55. }
  56. auto CharId::Print(llvm::raw_ostream& out) const -> void {
  57. // TODO: If we switch to C++23, `std::format("`{:?}`")` might be a better
  58. // choice.
  59. out << "U+";
  60. llvm::write_hex(out, index, llvm::HexPrintStyle::Upper, 4);
  61. }
  62. auto IntKind::Print(llvm::raw_ostream& out) const -> void {
  63. if (*this == Unsigned) {
  64. out << "unsigned";
  65. } else if (*this == Signed) {
  66. out << "signed";
  67. } else {
  68. CARBON_FATAL("Invalid int kind value {0}", index);
  69. }
  70. }
  71. static auto FloatKindToStringLiteral(FloatKind kind) -> llvm::StringLiteral {
  72. switch (kind.index) {
  73. case FloatKind::None.index:
  74. return "<none>";
  75. case FloatKind::Binary16.index:
  76. return "f16";
  77. case FloatKind::Binary32.index:
  78. return "f32";
  79. case FloatKind::Binary64.index:
  80. return "f64";
  81. case FloatKind::Binary128.index:
  82. return "f128";
  83. case FloatKind::BFloat16.index:
  84. return "f16_brain";
  85. case FloatKind::X87Float80.index:
  86. return "f80_x87";
  87. case FloatKind::PPCFloat128.index:
  88. return "f128_ppc";
  89. default:
  90. return "<invalid>";
  91. }
  92. }
  93. auto FloatKind::Print(llvm::raw_ostream& out) const -> void {
  94. out << FloatKindToStringLiteral(*this);
  95. }
  96. auto FloatKind::Semantics() const -> const llvm::fltSemantics& {
  97. switch (this->index) {
  98. case Binary16.index:
  99. return llvm::APFloat::IEEEhalf();
  100. case Binary32.index:
  101. return llvm::APFloat::IEEEsingle();
  102. case Binary64.index:
  103. return llvm::APFloat::IEEEdouble();
  104. case Binary128.index:
  105. return llvm::APFloat::IEEEquad();
  106. case BFloat16.index:
  107. return llvm::APFloat::BFloat();
  108. case X87Float80.index:
  109. return llvm::APFloat::x87DoubleExtended();
  110. case PPCFloat128.index:
  111. return llvm::APFloat::PPCDoubleDouble();
  112. default:
  113. CARBON_FATAL("Unexpected float kind {0}", *this);
  114. }
  115. }
  116. // Double-check the special value mapping and constexpr evaluation.
  117. static_assert(NameId::SpecialNameId::Vptr == *NameId::Vptr.AsSpecialNameId());
  118. auto NameId::ForIdentifier(IdentifierId id) -> NameId {
  119. if (id.index >= 0) {
  120. return NameId(id.index);
  121. } else if (!id.has_value()) {
  122. return NameId::None;
  123. } else {
  124. CARBON_FATAL("Unexpected identifier ID {0}", id);
  125. }
  126. }
  127. auto NameId::ForPackageName(PackageNameId id) -> NameId {
  128. if (auto identifier_id = id.AsIdentifierId(); identifier_id.has_value()) {
  129. return ForIdentifier(identifier_id);
  130. } else if (id == PackageNameId::Core) {
  131. return NameId::Core;
  132. } else if (!id.has_value()) {
  133. return NameId::None;
  134. } else {
  135. CARBON_FATAL("Unexpected package ID {0}", id);
  136. }
  137. }
  138. auto NameId::Print(llvm::raw_ostream& out) const -> void {
  139. if (!has_value() || index >= 0) {
  140. IdBase::Print(out);
  141. return;
  142. }
  143. out << Label << "(";
  144. auto special_name_id = AsSpecialNameId();
  145. CARBON_CHECK(special_name_id, "Unknown index {0}", index);
  146. switch (*special_name_id) {
  147. #define CARBON_SPECIAL_NAME_ID_FOR_PRINT(Name) \
  148. case SpecialNameId::Name: \
  149. out << #Name; \
  150. break;
  151. CARBON_SPECIAL_NAME_ID(CARBON_SPECIAL_NAME_ID_FOR_PRINT)
  152. #undef CARBON_SPECIAL_NAME_ID_FOR_PRINT
  153. }
  154. out << ")";
  155. }
  156. auto InstBlockId::Print(llvm::raw_ostream& out) const -> void {
  157. if (*this == Unreachable) {
  158. out << "unreachable";
  159. } else if (*this == Empty) {
  160. out << Label << "_empty";
  161. } else if (*this == Exports) {
  162. out << "exports";
  163. } else if (*this == Imports) {
  164. out << "imports";
  165. } else if (*this == GlobalInit) {
  166. out << "global_init";
  167. } else {
  168. IdBase::Print(out);
  169. }
  170. }
  171. auto TypeId::Print(llvm::raw_ostream& out) const -> void {
  172. out << Label << "(";
  173. if (*this == TypeType::TypeId) {
  174. out << "TypeType";
  175. } else if (*this == AutoType::TypeId) {
  176. out << "AutoType";
  177. } else if (*this == ErrorInst::TypeId) {
  178. out << "Error";
  179. } else {
  180. AsConstantId().Print(out, /*disambiguate=*/false);
  181. }
  182. out << ")";
  183. }
  184. auto LibraryNameId::ForStringLiteralValueId(StringLiteralValueId id)
  185. -> LibraryNameId {
  186. CARBON_CHECK(id.index >= NoneIndex, "Unexpected library name ID {0}", id);
  187. if (id == StringLiteralValueId::None) {
  188. // Prior to SemIR, we use `None` to indicate `default`.
  189. return LibraryNameId::Default;
  190. } else {
  191. return LibraryNameId(id.index);
  192. }
  193. }
  194. auto LibraryNameId::Print(llvm::raw_ostream& out) const -> void {
  195. if (*this == Default) {
  196. out << Label << "Default";
  197. } else if (*this == Error) {
  198. out << Label << "<error>";
  199. } else {
  200. IdBase::Print(out);
  201. }
  202. }
  203. auto LocId::Print(llvm::raw_ostream& out) const -> void {
  204. switch (kind()) {
  205. case Kind::None:
  206. IdBase::Print(out);
  207. break;
  208. case Kind::ImportIRInstId:
  209. out << Label << "_" << import_ir_inst_id();
  210. break;
  211. case Kind::InstId:
  212. out << Label << "_" << inst_id();
  213. break;
  214. case Kind::NodeId:
  215. out << Label << "_" << node_id();
  216. if (is_desugared()) {
  217. out << "_desugared";
  218. }
  219. break;
  220. }
  221. }
  222. } // namespace Carbon::SemIR