ids.cpp 6.9 KB

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