ids.cpp 6.8 KB

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