ids.cpp 7.1 KB

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