file.cpp 22 KB

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