file.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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/file.h"
  5. #include "common/check.h"
  6. #include "llvm/ADT/STLExtras.h"
  7. #include "llvm/ADT/SmallVector.h"
  8. #include "toolchain/base/kind_switch.h"
  9. #include "toolchain/base/value_store.h"
  10. #include "toolchain/base/yaml.h"
  11. #include "toolchain/parse/node_ids.h"
  12. #include "toolchain/sem_ir/builtin_inst_kind.h"
  13. #include "toolchain/sem_ir/ids.h"
  14. #include "toolchain/sem_ir/inst.h"
  15. #include "toolchain/sem_ir/inst_kind.h"
  16. #include "toolchain/sem_ir/typed_insts.h"
  17. namespace Carbon::SemIR {
  18. File::File(CheckIRId check_ir_id, IdentifierId package_id,
  19. LibraryNameId library_id, SharedValueStores& value_stores,
  20. std::string filename)
  21. : check_ir_id_(check_ir_id),
  22. package_id_(package_id),
  23. library_id_(library_id),
  24. value_stores_(&value_stores),
  25. filename_(std::move(filename)),
  26. impls_(*this),
  27. type_blocks_(allocator_),
  28. name_scopes_(&insts_),
  29. constant_values_(ConstantId::NotConstant),
  30. inst_blocks_(allocator_),
  31. constants_(*this) {
  32. // `type` and the error type are both complete types.
  33. types_.SetValueRepr(TypeId::TypeType,
  34. {.kind = ValueRepr::Copy, .type_id = TypeId::TypeType});
  35. types_.SetValueRepr(TypeId::Error,
  36. {.kind = ValueRepr::Copy, .type_id = TypeId::Error});
  37. insts_.Reserve(BuiltinInstKind::ValidCount);
  38. // Error uses a self-referential type so that it's not accidentally treated as
  39. // a normal type. Every other builtin is a type, including the
  40. // self-referential TypeType.
  41. #define CARBON_SEM_IR_BUILTIN_INST_KIND(Name, ...) \
  42. insts_.AddInNoBlock(LocIdAndInst::NoLoc<BuiltinInst>( \
  43. {.type_id = BuiltinInstKind::Name == BuiltinInstKind::Error \
  44. ? TypeId::Error \
  45. : TypeId::TypeType, \
  46. .builtin_inst_kind = BuiltinInstKind::Name}));
  47. #include "toolchain/sem_ir/builtin_inst_kind.def"
  48. CARBON_CHECK(insts_.size() == BuiltinInstKind::ValidCount,
  49. "Builtins should produce {0} insts, actual: {1}",
  50. BuiltinInstKind::ValidCount, insts_.size());
  51. for (auto i : llvm::seq(BuiltinInstKind::ValidCount)) {
  52. auto builtin_id = SemIR::InstId(i);
  53. constant_values_.Set(builtin_id,
  54. SemIR::ConstantId::ForTemplateConstant(builtin_id));
  55. }
  56. }
  57. auto File::Verify() const -> ErrorOr<Success> {
  58. // Invariants don't necessarily hold for invalid IR.
  59. if (has_errors_) {
  60. return Success();
  61. }
  62. // Check that every code block has a terminator sequence that appears at the
  63. // end of the block.
  64. for (const Function& function : functions_.array_ref()) {
  65. for (InstBlockId block_id : function.body_block_ids) {
  66. TerminatorKind prior_kind = TerminatorKind::NotTerminator;
  67. for (InstId inst_id : inst_blocks().Get(block_id)) {
  68. TerminatorKind inst_kind =
  69. insts().Get(inst_id).kind().terminator_kind();
  70. if (prior_kind == TerminatorKind::Terminator) {
  71. return Error(llvm::formatv("Inst {0} in block {1} follows terminator",
  72. inst_id, block_id));
  73. }
  74. if (prior_kind > inst_kind) {
  75. return Error(
  76. llvm::formatv("Non-terminator inst {0} in block {1} follows "
  77. "terminator sequence",
  78. inst_id, block_id));
  79. }
  80. prior_kind = inst_kind;
  81. }
  82. if (prior_kind != TerminatorKind::Terminator) {
  83. return Error(llvm::formatv("No terminator in block {0}", block_id));
  84. }
  85. }
  86. }
  87. // TODO: Check that an instruction only references other instructions that are
  88. // either global or that dominate it.
  89. return Success();
  90. }
  91. auto File::OutputYaml(bool include_builtins) const -> Yaml::OutputMapping {
  92. return Yaml::OutputMapping([this,
  93. include_builtins](Yaml::OutputMapping::Map map) {
  94. map.Add("filename", filename_);
  95. map.Add(
  96. "sem_ir", Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  97. map.Add("import_irs", import_irs_.OutputYaml());
  98. map.Add("import_ir_insts", import_ir_insts_.OutputYaml());
  99. map.Add("name_scopes", name_scopes_.OutputYaml());
  100. map.Add("entity_names", entity_names_.OutputYaml());
  101. map.Add("functions", functions_.OutputYaml());
  102. map.Add("classes", classes_.OutputYaml());
  103. map.Add("generics", generics_.OutputYaml());
  104. map.Add("specifics", specifics_.OutputYaml());
  105. map.Add("types", types_.OutputYaml());
  106. map.Add("type_blocks", type_blocks_.OutputYaml());
  107. map.Add(
  108. "insts", Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  109. int start = include_builtins ? 0 : BuiltinInstKind::ValidCount;
  110. for (int i : llvm::seq(start, insts_.size())) {
  111. auto id = InstId(i);
  112. map.Add(PrintToString(id),
  113. Yaml::OutputScalar(insts_.Get(id)));
  114. }
  115. }));
  116. map.Add("constant_values",
  117. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  118. int start =
  119. include_builtins ? 0 : BuiltinInstKind::ValidCount;
  120. for (int i : llvm::seq(start, insts_.size())) {
  121. auto id = InstId(i);
  122. auto value = constant_values_.Get(id);
  123. if (!value.is_valid() || value.is_constant()) {
  124. map.Add(PrintToString(id), Yaml::OutputScalar(value));
  125. }
  126. }
  127. }));
  128. map.Add(
  129. "symbolic_constants",
  130. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  131. for (const auto& [i, symbolic] :
  132. llvm::enumerate(constant_values().symbolic_constants())) {
  133. map.Add(
  134. PrintToString(ConstantId::ForSymbolicConstantIndex(i)),
  135. Yaml::OutputScalar(symbolic));
  136. }
  137. }));
  138. map.Add("inst_blocks", inst_blocks_.OutputYaml());
  139. }));
  140. });
  141. }
  142. auto File::CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  143. -> void {
  144. mem_usage.Add(MemUsage::ConcatLabel(label, "allocator_"), allocator_);
  145. mem_usage.Collect(MemUsage::ConcatLabel(label, "entity_names_"),
  146. entity_names_);
  147. mem_usage.Collect(MemUsage::ConcatLabel(label, "functions_"), functions_);
  148. mem_usage.Collect(MemUsage::ConcatLabel(label, "classes_"), classes_);
  149. mem_usage.Collect(MemUsage::ConcatLabel(label, "interfaces_"), interfaces_);
  150. mem_usage.Collect(MemUsage::ConcatLabel(label, "impls_"), impls_);
  151. mem_usage.Collect(MemUsage::ConcatLabel(label, "generics_"), generics_);
  152. mem_usage.Collect(MemUsage::ConcatLabel(label, "specifics_"), specifics_);
  153. mem_usage.Collect(MemUsage::ConcatLabel(label, "import_irs_"), import_irs_);
  154. mem_usage.Collect(MemUsage::ConcatLabel(label, "import_ir_insts_"),
  155. import_ir_insts_);
  156. mem_usage.Collect(MemUsage::ConcatLabel(label, "type_blocks_"), type_blocks_);
  157. mem_usage.Collect(MemUsage::ConcatLabel(label, "insts_"), insts_);
  158. mem_usage.Collect(MemUsage::ConcatLabel(label, "name_scopes_"), name_scopes_);
  159. mem_usage.Collect(MemUsage::ConcatLabel(label, "constant_values_"),
  160. constant_values_);
  161. mem_usage.Collect(MemUsage::ConcatLabel(label, "inst_blocks_"), inst_blocks_);
  162. mem_usage.Collect(MemUsage::ConcatLabel(label, "constants_"), constants_);
  163. mem_usage.Collect(MemUsage::ConcatLabel(label, "types_"), types_);
  164. }
  165. // Map an instruction kind representing a type into an integer describing the
  166. // precedence of that type's syntax. Higher numbers correspond to higher
  167. // precedence.
  168. static auto GetTypePrecedence(InstKind kind) -> int {
  169. CARBON_CHECK(kind.is_type() != InstIsType::Never,
  170. "Only called for kinds which can define a type.");
  171. if (kind == ConstType::Kind) {
  172. return -1;
  173. }
  174. if (kind == PointerType::Kind) {
  175. return -2;
  176. }
  177. return 0;
  178. }
  179. // Implements File::StringifyTypeExpr. Static to prevent accidental use of
  180. // member functions while traversing IRs.
  181. static auto StringifyTypeExprImpl(const SemIR::File& outer_sem_ir,
  182. InstId outer_inst_id) {
  183. std::string str;
  184. llvm::raw_string_ostream out(str);
  185. struct Step {
  186. // The instruction's file.
  187. const File& sem_ir;
  188. // The instruction to print.
  189. InstId inst_id;
  190. // The index into inst_id to print. Not used by all types.
  191. int index = 0;
  192. auto Next() const -> Step {
  193. return {.sem_ir = sem_ir, .inst_id = inst_id, .index = index + 1};
  194. }
  195. };
  196. llvm::SmallVector<Step> steps = {
  197. Step{.sem_ir = outer_sem_ir, .inst_id = outer_inst_id}};
  198. while (!steps.empty()) {
  199. auto step = steps.pop_back_val();
  200. if (!step.inst_id.is_valid()) {
  201. out << "<invalid type>";
  202. continue;
  203. }
  204. // Builtins have designated labels.
  205. if (step.inst_id.is_builtin()) {
  206. out << step.inst_id.builtin_inst_kind().label();
  207. continue;
  208. }
  209. const auto& sem_ir = step.sem_ir;
  210. // Helper for instructions with the current sem_ir.
  211. auto push_inst_id = [&](InstId inst_id) {
  212. steps.push_back({.sem_ir = sem_ir, .inst_id = inst_id});
  213. };
  214. auto untyped_inst = sem_ir.insts().Get(step.inst_id);
  215. CARBON_KIND_SWITCH(untyped_inst) {
  216. case CARBON_KIND(ArrayType inst): {
  217. if (step.index == 0) {
  218. out << "[";
  219. steps.push_back(step.Next());
  220. push_inst_id(sem_ir.types().GetInstId(inst.element_type_id));
  221. } else if (step.index == 1) {
  222. out << "; " << sem_ir.GetArrayBoundValue(inst.bound_id) << "]";
  223. }
  224. break;
  225. }
  226. case CARBON_KIND(AssociatedEntityType inst): {
  227. if (step.index == 0) {
  228. out << "<associated ";
  229. steps.push_back(step.Next());
  230. push_inst_id(sem_ir.types().GetInstId(inst.entity_type_id));
  231. } else if (step.index == 1) {
  232. out << " in ";
  233. steps.push_back(step.Next());
  234. push_inst_id(sem_ir.types().GetInstId(inst.interface_type_id));
  235. } else {
  236. out << ">";
  237. }
  238. break;
  239. }
  240. case BindAlias::Kind:
  241. case BindSymbolicName::Kind:
  242. case ExportDecl::Kind: {
  243. auto name_id =
  244. untyped_inst.As<AnyBindNameOrExportDecl>().entity_name_id;
  245. out << sem_ir.names().GetFormatted(
  246. sem_ir.entity_names().Get(name_id).name_id);
  247. break;
  248. }
  249. case CARBON_KIND(ClassType inst): {
  250. auto class_name_id = sem_ir.classes().Get(inst.class_id).name_id;
  251. out << sem_ir.names().GetFormatted(class_name_id);
  252. break;
  253. }
  254. case CARBON_KIND(ConstType inst): {
  255. if (step.index == 0) {
  256. out << "const ";
  257. // Add parentheses if required.
  258. auto inner_type_inst_id = sem_ir.types().GetInstId(inst.inner_id);
  259. if (GetTypePrecedence(sem_ir.insts().Get(inner_type_inst_id).kind()) <
  260. GetTypePrecedence(SemIR::ConstType::Kind)) {
  261. out << "(";
  262. steps.push_back(step.Next());
  263. }
  264. push_inst_id(inner_type_inst_id);
  265. } else if (step.index == 1) {
  266. out << ")";
  267. }
  268. break;
  269. }
  270. case CARBON_KIND(FacetTypeAccess inst): {
  271. // Print `T as type` as simply `T`.
  272. push_inst_id(inst.facet_id);
  273. break;
  274. }
  275. case CARBON_KIND(FloatType inst): {
  276. // TODO: Is this okay?
  277. if (step.index == 1) {
  278. out << ")";
  279. } else if (auto width_value =
  280. sem_ir.insts().TryGetAs<IntLiteral>(inst.bit_width_id)) {
  281. out << "f";
  282. sem_ir.ints().Get(width_value->int_id).print(out, /*isSigned=*/false);
  283. } else {
  284. out << "Core.Float(";
  285. steps.push_back(step.Next());
  286. push_inst_id(inst.bit_width_id);
  287. }
  288. break;
  289. }
  290. case CARBON_KIND(FunctionType inst): {
  291. auto fn_name_id = sem_ir.functions().Get(inst.function_id).name_id;
  292. out << "<type of " << sem_ir.names().GetFormatted(fn_name_id) << ">";
  293. break;
  294. }
  295. case CARBON_KIND(GenericClassType inst): {
  296. auto class_name_id = sem_ir.classes().Get(inst.class_id).name_id;
  297. out << "<type of " << sem_ir.names().GetFormatted(class_name_id) << ">";
  298. break;
  299. }
  300. case CARBON_KIND(GenericInterfaceType inst): {
  301. auto interface_name_id =
  302. sem_ir.interfaces().Get(inst.interface_id).name_id;
  303. out << "<type of " << sem_ir.names().GetFormatted(interface_name_id)
  304. << ">";
  305. break;
  306. }
  307. case CARBON_KIND(InterfaceType inst): {
  308. auto interface_name_id =
  309. sem_ir.interfaces().Get(inst.interface_id).name_id;
  310. out << sem_ir.names().GetFormatted(interface_name_id);
  311. break;
  312. }
  313. case CARBON_KIND(IntType inst): {
  314. if (step.index == 1) {
  315. out << ")";
  316. } else if (auto width_value =
  317. sem_ir.insts().TryGetAs<IntLiteral>(inst.bit_width_id)) {
  318. out << (inst.int_kind.is_signed() ? "i" : "u");
  319. sem_ir.ints().Get(width_value->int_id).print(out, /*isSigned=*/false);
  320. } else {
  321. out << (inst.int_kind.is_signed() ? "Core.Int(" : "Core.UInt(");
  322. steps.push_back(step.Next());
  323. push_inst_id(inst.bit_width_id);
  324. }
  325. break;
  326. }
  327. case CARBON_KIND(NameRef inst): {
  328. out << sem_ir.names().GetFormatted(inst.name_id);
  329. break;
  330. }
  331. case CARBON_KIND(PointerType inst): {
  332. if (step.index == 0) {
  333. steps.push_back(step.Next());
  334. push_inst_id(sem_ir.types().GetInstId(inst.pointee_id));
  335. } else if (step.index == 1) {
  336. out << "*";
  337. }
  338. break;
  339. }
  340. case CARBON_KIND(StructType inst): {
  341. auto refs = sem_ir.inst_blocks().Get(inst.fields_id);
  342. if (refs.empty()) {
  343. out << "{}";
  344. break;
  345. } else if (step.index == 0) {
  346. out << "{";
  347. } else if (step.index < static_cast<int>(refs.size())) {
  348. out << ", ";
  349. } else {
  350. out << "}";
  351. break;
  352. }
  353. steps.push_back(step.Next());
  354. push_inst_id(refs[step.index]);
  355. break;
  356. }
  357. case CARBON_KIND(StructTypeField inst): {
  358. out << "." << sem_ir.names().GetFormatted(inst.name_id) << ": ";
  359. push_inst_id(sem_ir.types().GetInstId(inst.field_type_id));
  360. break;
  361. }
  362. case CARBON_KIND(TupleType inst): {
  363. auto refs = sem_ir.type_blocks().Get(inst.elements_id);
  364. if (refs.empty()) {
  365. out << "()";
  366. break;
  367. } else if (step.index == 0) {
  368. out << "(";
  369. } else if (step.index < static_cast<int>(refs.size())) {
  370. out << ", ";
  371. } else {
  372. // A tuple of one element has a comma to disambiguate from an
  373. // expression.
  374. if (step.index == 1) {
  375. out << ",";
  376. }
  377. out << ")";
  378. break;
  379. }
  380. steps.push_back(step.Next());
  381. push_inst_id(sem_ir.types().GetInstId(refs[step.index]));
  382. break;
  383. }
  384. case CARBON_KIND(UnboundElementType inst): {
  385. if (step.index == 0) {
  386. out << "<unbound element of class ";
  387. steps.push_back(step.Next());
  388. push_inst_id(sem_ir.types().GetInstId(inst.class_type_id));
  389. } else {
  390. out << ">";
  391. }
  392. break;
  393. }
  394. case CARBON_KIND(WhereExpr inst): {
  395. if (step.index == 0) {
  396. out << "<where restriction on ";
  397. steps.push_back(step.Next());
  398. TypeId type_id = sem_ir.insts().Get(inst.period_self_id).type_id();
  399. push_inst_id(sem_ir.types().GetInstId(type_id));
  400. // TODO: also output restrictions from the inst block
  401. // inst.requirements_id
  402. } else {
  403. out << ">";
  404. }
  405. break;
  406. }
  407. case AdaptDecl::Kind:
  408. case AddrOf::Kind:
  409. case AddrPattern::Kind:
  410. case ArrayIndex::Kind:
  411. case ArrayInit::Kind:
  412. case AsCompatible::Kind:
  413. case Assign::Kind:
  414. case AssociatedConstantDecl::Kind:
  415. case AssociatedEntity::Kind:
  416. case BaseDecl::Kind:
  417. case BindingPattern::Kind:
  418. case BindName::Kind:
  419. case BindValue::Kind:
  420. case BlockArg::Kind:
  421. case BoolLiteral::Kind:
  422. case BoundMethod::Kind:
  423. case Branch::Kind:
  424. case BranchIf::Kind:
  425. case BranchWithArg::Kind:
  426. case BuiltinInst::Kind:
  427. case Call::Kind:
  428. case ClassDecl::Kind:
  429. case ClassElementAccess::Kind:
  430. case ClassInit::Kind:
  431. case CompleteTypeWitness::Kind:
  432. case Converted::Kind:
  433. case Deref::Kind:
  434. case FieldDecl::Kind:
  435. case FloatLiteral::Kind:
  436. case FunctionDecl::Kind:
  437. case ImplDecl::Kind:
  438. case ImportDecl::Kind:
  439. case ImportRefLoaded::Kind:
  440. case ImportRefUnloaded::Kind:
  441. case InitializeFrom::Kind:
  442. case SpecificConstant::Kind:
  443. case InterfaceDecl::Kind:
  444. case InterfaceWitness::Kind:
  445. case InterfaceWitnessAccess::Kind:
  446. case IntLiteral::Kind:
  447. case Namespace::Kind:
  448. case Param::Kind:
  449. case RequirementEquivalent::Kind:
  450. case RequirementImpls::Kind:
  451. case RequirementRewrite::Kind:
  452. case Return::Kind:
  453. case ReturnExpr::Kind:
  454. case SpliceBlock::Kind:
  455. case StringLiteral::Kind:
  456. case StructAccess::Kind:
  457. case StructLiteral::Kind:
  458. case StructInit::Kind:
  459. case StructValue::Kind:
  460. case SymbolicBindingPattern::Kind:
  461. case Temporary::Kind:
  462. case TemporaryStorage::Kind:
  463. case TupleAccess::Kind:
  464. case TupleLiteral::Kind:
  465. case TupleInit::Kind:
  466. case TupleValue::Kind:
  467. case UnaryOperatorNot::Kind:
  468. case ValueAsRef::Kind:
  469. case ValueOfInitializer::Kind:
  470. case VarStorage::Kind:
  471. // We don't know how to print this instruction, but it might have a
  472. // constant value that we can print.
  473. auto const_inst_id =
  474. sem_ir.constant_values().GetConstantInstId(step.inst_id);
  475. if (const_inst_id.is_valid() && const_inst_id != step.inst_id) {
  476. push_inst_id(const_inst_id);
  477. break;
  478. }
  479. // We don't need to handle stringification for instructions that don't
  480. // show up in errors, but make it clear what's going on so that it's
  481. // clearer when stringification is needed.
  482. out << "<cannot stringify " << step.inst_id << ">";
  483. break;
  484. }
  485. }
  486. return str;
  487. }
  488. auto File::StringifyType(TypeId type_id) const -> std::string {
  489. return StringifyTypeExprImpl(*this, types().GetInstId(type_id));
  490. }
  491. auto File::StringifyType(ConstantId type_const_id) const -> std::string {
  492. return StringifyTypeExprImpl(*this,
  493. constant_values().GetInstId(type_const_id));
  494. }
  495. auto File::StringifyTypeExpr(InstId outer_inst_id) const -> std::string {
  496. return StringifyTypeExprImpl(*this, outer_inst_id);
  497. }
  498. auto GetExprCategory(const File& file, InstId inst_id) -> ExprCategory {
  499. const File* ir = &file;
  500. // The overall expression category if the current instruction is a value
  501. // expression.
  502. ExprCategory value_category = ExprCategory::Value;
  503. while (true) {
  504. auto untyped_inst = ir->insts().Get(inst_id);
  505. CARBON_KIND_SWITCH(untyped_inst) {
  506. case AdaptDecl::Kind:
  507. case Assign::Kind:
  508. case BaseDecl::Kind:
  509. case BindingPattern::Kind:
  510. case Branch::Kind:
  511. case BranchIf::Kind:
  512. case BranchWithArg::Kind:
  513. case FieldDecl::Kind:
  514. case FunctionDecl::Kind:
  515. case ImplDecl::Kind:
  516. case Namespace::Kind:
  517. case RequirementEquivalent::Kind:
  518. case RequirementImpls::Kind:
  519. case RequirementRewrite::Kind:
  520. case Return::Kind:
  521. case ReturnExpr::Kind:
  522. case StructTypeField::Kind:
  523. case SymbolicBindingPattern::Kind:
  524. return ExprCategory::NotExpr;
  525. case ImportRefUnloaded::Kind:
  526. case ImportRefLoaded::Kind: {
  527. auto import_ir_inst = ir->import_ir_insts().Get(
  528. untyped_inst.As<SemIR::AnyImportRef>().import_ir_inst_id);
  529. ir = ir->import_irs().Get(import_ir_inst.ir_id).sem_ir;
  530. inst_id = import_ir_inst.inst_id;
  531. continue;
  532. }
  533. case CARBON_KIND(AsCompatible inst): {
  534. inst_id = inst.source_id;
  535. continue;
  536. }
  537. case CARBON_KIND(BindAlias inst): {
  538. inst_id = inst.value_id;
  539. continue;
  540. }
  541. case CARBON_KIND(ExportDecl inst): {
  542. inst_id = inst.value_id;
  543. continue;
  544. }
  545. case CARBON_KIND(NameRef inst): {
  546. inst_id = inst.value_id;
  547. continue;
  548. }
  549. case CARBON_KIND(Converted inst): {
  550. inst_id = inst.result_id;
  551. continue;
  552. }
  553. case CARBON_KIND(SpecificConstant inst): {
  554. inst_id = inst.inst_id;
  555. continue;
  556. }
  557. case AddrOf::Kind:
  558. case AddrPattern::Kind:
  559. case ArrayType::Kind:
  560. case AssociatedConstantDecl::Kind:
  561. case AssociatedEntity::Kind:
  562. case AssociatedEntityType::Kind:
  563. case BindSymbolicName::Kind:
  564. case BindValue::Kind:
  565. case BlockArg::Kind:
  566. case BoolLiteral::Kind:
  567. case BoundMethod::Kind:
  568. case ClassDecl::Kind:
  569. case ClassType::Kind:
  570. case CompleteTypeWitness::Kind:
  571. case ConstType::Kind:
  572. case FacetTypeAccess::Kind:
  573. case FloatLiteral::Kind:
  574. case FloatType::Kind:
  575. case FunctionType::Kind:
  576. case GenericClassType::Kind:
  577. case GenericInterfaceType::Kind:
  578. case ImportDecl::Kind:
  579. case InterfaceDecl::Kind:
  580. case InterfaceType::Kind:
  581. case InterfaceWitness::Kind:
  582. case InterfaceWitnessAccess::Kind:
  583. case IntLiteral::Kind:
  584. case IntType::Kind:
  585. case Param::Kind:
  586. case PointerType::Kind:
  587. case StringLiteral::Kind:
  588. case StructValue::Kind:
  589. case StructType::Kind:
  590. case TupleValue::Kind:
  591. case TupleType::Kind:
  592. case UnaryOperatorNot::Kind:
  593. case UnboundElementType::Kind:
  594. case ValueOfInitializer::Kind:
  595. case WhereExpr::Kind:
  596. return value_category;
  597. case CARBON_KIND(BuiltinInst inst): {
  598. if (inst.builtin_inst_kind == BuiltinInstKind::Error) {
  599. return ExprCategory::Error;
  600. }
  601. return value_category;
  602. }
  603. case CARBON_KIND(BindName inst): {
  604. inst_id = inst.value_id;
  605. continue;
  606. }
  607. case CARBON_KIND(ArrayIndex inst): {
  608. inst_id = inst.array_id;
  609. continue;
  610. }
  611. case CARBON_KIND(ClassElementAccess inst): {
  612. inst_id = inst.base_id;
  613. // A value of class type is a pointer to an object representation.
  614. // Therefore, if the base is a value, the result is an ephemeral
  615. // reference.
  616. value_category = ExprCategory::EphemeralRef;
  617. continue;
  618. }
  619. case CARBON_KIND(StructAccess inst): {
  620. inst_id = inst.struct_id;
  621. continue;
  622. }
  623. case CARBON_KIND(TupleAccess inst): {
  624. inst_id = inst.tuple_id;
  625. continue;
  626. }
  627. case CARBON_KIND(SpliceBlock inst): {
  628. inst_id = inst.result_id;
  629. continue;
  630. }
  631. case StructLiteral::Kind:
  632. case TupleLiteral::Kind:
  633. return ExprCategory::Mixed;
  634. case ArrayInit::Kind:
  635. case Call::Kind:
  636. case InitializeFrom::Kind:
  637. case ClassInit::Kind:
  638. case StructInit::Kind:
  639. case TupleInit::Kind:
  640. return ExprCategory::Initializing;
  641. case Deref::Kind:
  642. case VarStorage::Kind:
  643. return ExprCategory::DurableRef;
  644. case Temporary::Kind:
  645. case TemporaryStorage::Kind:
  646. case ValueAsRef::Kind:
  647. return ExprCategory::EphemeralRef;
  648. }
  649. }
  650. }
  651. } // namespace Carbon::SemIR