file.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  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 if (step.index == 1) {
  231. out << " in ";
  232. steps.push_back(step.Next());
  233. push_inst_id(sem_ir.types().GetInstId(inst.interface_type_id));
  234. } else {
  235. out << ">";
  236. }
  237. break;
  238. }
  239. case BindAlias::Kind:
  240. case BindSymbolicName::Kind:
  241. case ExportDecl::Kind: {
  242. auto name_id =
  243. untyped_inst.As<AnyBindNameOrExportDecl>().entity_name_id;
  244. out << sem_ir.names().GetFormatted(
  245. sem_ir.entity_names().Get(name_id).name_id);
  246. break;
  247. }
  248. case CARBON_KIND(ClassType inst): {
  249. auto class_name_id = sem_ir.classes().Get(inst.class_id).name_id;
  250. out << sem_ir.names().GetFormatted(class_name_id);
  251. break;
  252. }
  253. case CARBON_KIND(ConstType inst): {
  254. if (step.index == 0) {
  255. out << "const ";
  256. // Add parentheses if required.
  257. auto inner_type_inst_id = sem_ir.types().GetInstId(inst.inner_id);
  258. if (GetTypePrecedence(sem_ir.insts().Get(inner_type_inst_id).kind()) <
  259. GetTypePrecedence(SemIR::ConstType::Kind)) {
  260. out << "(";
  261. steps.push_back(step.Next());
  262. }
  263. push_inst_id(inner_type_inst_id);
  264. } else if (step.index == 1) {
  265. out << ")";
  266. }
  267. break;
  268. }
  269. case CARBON_KIND(FacetTypeAccess inst): {
  270. // Print `T as type` as simply `T`.
  271. push_inst_id(inst.facet_id);
  272. break;
  273. }
  274. case CARBON_KIND(FloatType inst): {
  275. // TODO: Is this okay?
  276. if (step.index == 1) {
  277. out << ")";
  278. } else if (auto width_value =
  279. sem_ir.insts().TryGetAs<IntLiteral>(inst.bit_width_id)) {
  280. out << "f";
  281. sem_ir.ints().Get(width_value->int_id).print(out, /*isSigned=*/false);
  282. } else {
  283. out << "Core.Float(";
  284. steps.push_back(step.Next());
  285. push_inst_id(inst.bit_width_id);
  286. }
  287. break;
  288. }
  289. case CARBON_KIND(FunctionType inst): {
  290. auto fn_name_id = sem_ir.functions().Get(inst.function_id).name_id;
  291. out << "<type of " << sem_ir.names().GetFormatted(fn_name_id) << ">";
  292. break;
  293. }
  294. case CARBON_KIND(GenericClassType inst): {
  295. auto class_name_id = sem_ir.classes().Get(inst.class_id).name_id;
  296. out << "<type of " << sem_ir.names().GetFormatted(class_name_id) << ">";
  297. break;
  298. }
  299. case CARBON_KIND(GenericInterfaceType inst): {
  300. auto interface_name_id =
  301. sem_ir.interfaces().Get(inst.interface_id).name_id;
  302. out << "<type of " << sem_ir.names().GetFormatted(interface_name_id)
  303. << ">";
  304. break;
  305. }
  306. case CARBON_KIND(InterfaceType inst): {
  307. auto interface_name_id =
  308. sem_ir.interfaces().Get(inst.interface_id).name_id;
  309. out << sem_ir.names().GetFormatted(interface_name_id);
  310. break;
  311. }
  312. case CARBON_KIND(IntType inst): {
  313. if (step.index == 1) {
  314. out << ")";
  315. } else if (auto width_value =
  316. sem_ir.insts().TryGetAs<IntLiteral>(inst.bit_width_id)) {
  317. out << (inst.int_kind.is_signed() ? "i" : "u");
  318. sem_ir.ints().Get(width_value->int_id).print(out, /*isSigned=*/false);
  319. } else {
  320. out << (inst.int_kind.is_signed() ? "Core.Int(" : "Core.UInt(");
  321. steps.push_back(step.Next());
  322. push_inst_id(inst.bit_width_id);
  323. }
  324. break;
  325. }
  326. case CARBON_KIND(NameRef inst): {
  327. out << sem_ir.names().GetFormatted(inst.name_id);
  328. break;
  329. }
  330. case CARBON_KIND(PointerType inst): {
  331. if (step.index == 0) {
  332. steps.push_back(step.Next());
  333. push_inst_id(sem_ir.types().GetInstId(inst.pointee_id));
  334. } else if (step.index == 1) {
  335. out << "*";
  336. }
  337. break;
  338. }
  339. case CARBON_KIND(StructType inst): {
  340. auto refs = sem_ir.inst_blocks().Get(inst.fields_id);
  341. if (refs.empty()) {
  342. out << "{}";
  343. break;
  344. } else if (step.index == 0) {
  345. out << "{";
  346. } else if (step.index < static_cast<int>(refs.size())) {
  347. out << ", ";
  348. } else {
  349. out << "}";
  350. break;
  351. }
  352. steps.push_back(step.Next());
  353. push_inst_id(refs[step.index]);
  354. break;
  355. }
  356. case CARBON_KIND(StructTypeField inst): {
  357. out << "." << sem_ir.names().GetFormatted(inst.name_id) << ": ";
  358. push_inst_id(sem_ir.types().GetInstId(inst.field_type_id));
  359. break;
  360. }
  361. case CARBON_KIND(TupleType inst): {
  362. auto refs = sem_ir.type_blocks().Get(inst.elements_id);
  363. if (refs.empty()) {
  364. out << "()";
  365. break;
  366. } else if (step.index == 0) {
  367. out << "(";
  368. } else if (step.index < static_cast<int>(refs.size())) {
  369. out << ", ";
  370. } else {
  371. // A tuple of one element has a comma to disambiguate from an
  372. // expression.
  373. if (step.index == 1) {
  374. out << ",";
  375. }
  376. out << ")";
  377. break;
  378. }
  379. steps.push_back(step.Next());
  380. push_inst_id(sem_ir.types().GetInstId(refs[step.index]));
  381. break;
  382. }
  383. case CARBON_KIND(UnboundElementType inst): {
  384. if (step.index == 0) {
  385. out << "<unbound element of class ";
  386. steps.push_back(step.Next());
  387. push_inst_id(sem_ir.types().GetInstId(inst.class_type_id));
  388. } else {
  389. out << ">";
  390. }
  391. break;
  392. }
  393. case AdaptDecl::Kind:
  394. case AddrOf::Kind:
  395. case AddrPattern::Kind:
  396. case ArrayIndex::Kind:
  397. case ArrayInit::Kind:
  398. case AsCompatible::Kind:
  399. case Assign::Kind:
  400. case AssociatedConstantDecl::Kind:
  401. case AssociatedEntity::Kind:
  402. case BaseDecl::Kind:
  403. case BindName::Kind:
  404. case BindValue::Kind:
  405. case BlockArg::Kind:
  406. case BoolLiteral::Kind:
  407. case BoundMethod::Kind:
  408. case Branch::Kind:
  409. case BranchIf::Kind:
  410. case BranchWithArg::Kind:
  411. case BuiltinInst::Kind:
  412. case Call::Kind:
  413. case ClassDecl::Kind:
  414. case ClassElementAccess::Kind:
  415. case ClassInit::Kind:
  416. case Converted::Kind:
  417. case Deref::Kind:
  418. case FieldDecl::Kind:
  419. case FloatLiteral::Kind:
  420. case FunctionDecl::Kind:
  421. case ImplDecl::Kind:
  422. case ImportDecl::Kind:
  423. case ImportRefLoaded::Kind:
  424. case ImportRefUnloaded::Kind:
  425. case InitializeFrom::Kind:
  426. case SpecificConstant::Kind:
  427. case InterfaceDecl::Kind:
  428. case InterfaceWitness::Kind:
  429. case InterfaceWitnessAccess::Kind:
  430. case IntLiteral::Kind:
  431. case Namespace::Kind:
  432. case Param::Kind:
  433. case Return::Kind:
  434. case ReturnExpr::Kind:
  435. case SpliceBlock::Kind:
  436. case StringLiteral::Kind:
  437. case StructAccess::Kind:
  438. case StructLiteral::Kind:
  439. case StructInit::Kind:
  440. case StructValue::Kind:
  441. case Temporary::Kind:
  442. case TemporaryStorage::Kind:
  443. case TupleAccess::Kind:
  444. case TupleIndex::Kind:
  445. case TupleLiteral::Kind:
  446. case TupleInit::Kind:
  447. case TupleValue::Kind:
  448. case UnaryOperatorNot::Kind:
  449. case ValueAsRef::Kind:
  450. case ValueOfInitializer::Kind:
  451. case VarStorage::Kind:
  452. // We don't need to handle stringification for instructions that don't
  453. // show up in errors, but make it clear what's going on so that it's
  454. // clearer when stringification is needed.
  455. out << "<cannot stringify " << step.inst_id << ">";
  456. break;
  457. }
  458. }
  459. return str;
  460. }
  461. auto File::StringifyType(TypeId type_id) const -> std::string {
  462. return StringifyTypeExprImpl(*this, types().GetInstId(type_id));
  463. }
  464. auto File::StringifyType(ConstantId type_const_id) const -> std::string {
  465. return StringifyTypeExprImpl(*this,
  466. constant_values().GetInstId(type_const_id));
  467. }
  468. auto File::StringifyTypeExpr(InstId outer_inst_id) const -> std::string {
  469. return StringifyTypeExprImpl(*this, outer_inst_id);
  470. }
  471. auto GetExprCategory(const File& file, InstId inst_id) -> ExprCategory {
  472. const File* ir = &file;
  473. // The overall expression category if the current instruction is a value
  474. // expression.
  475. ExprCategory value_category = ExprCategory::Value;
  476. while (true) {
  477. auto untyped_inst = ir->insts().Get(inst_id);
  478. CARBON_KIND_SWITCH(untyped_inst) {
  479. case AdaptDecl::Kind:
  480. case Assign::Kind:
  481. case BaseDecl::Kind:
  482. case Branch::Kind:
  483. case BranchIf::Kind:
  484. case BranchWithArg::Kind:
  485. case FieldDecl::Kind:
  486. case FunctionDecl::Kind:
  487. case ImplDecl::Kind:
  488. case Namespace::Kind:
  489. case Return::Kind:
  490. case ReturnExpr::Kind:
  491. case StructTypeField::Kind:
  492. return ExprCategory::NotExpr;
  493. case ImportRefUnloaded::Kind:
  494. case ImportRefLoaded::Kind: {
  495. auto import_ir_inst = ir->import_ir_insts().Get(
  496. untyped_inst.As<SemIR::AnyImportRef>().import_ir_inst_id);
  497. ir = ir->import_irs().Get(import_ir_inst.ir_id).sem_ir;
  498. inst_id = import_ir_inst.inst_id;
  499. continue;
  500. }
  501. case CARBON_KIND(AsCompatible inst): {
  502. inst_id = inst.source_id;
  503. continue;
  504. }
  505. case CARBON_KIND(BindAlias inst): {
  506. inst_id = inst.value_id;
  507. continue;
  508. }
  509. case CARBON_KIND(ExportDecl inst): {
  510. inst_id = inst.value_id;
  511. continue;
  512. }
  513. case CARBON_KIND(NameRef inst): {
  514. inst_id = inst.value_id;
  515. continue;
  516. }
  517. case CARBON_KIND(Converted inst): {
  518. inst_id = inst.result_id;
  519. continue;
  520. }
  521. case CARBON_KIND(SpecificConstant inst): {
  522. inst_id = inst.inst_id;
  523. continue;
  524. }
  525. case AddrOf::Kind:
  526. case AddrPattern::Kind:
  527. case ArrayType::Kind:
  528. case AssociatedConstantDecl::Kind:
  529. case AssociatedEntity::Kind:
  530. case AssociatedEntityType::Kind:
  531. case BindSymbolicName::Kind:
  532. case BindValue::Kind:
  533. case BlockArg::Kind:
  534. case BoolLiteral::Kind:
  535. case BoundMethod::Kind:
  536. case ClassDecl::Kind:
  537. case ClassType::Kind:
  538. case ConstType::Kind:
  539. case FacetTypeAccess::Kind:
  540. case FloatLiteral::Kind:
  541. case FloatType::Kind:
  542. case FunctionType::Kind:
  543. case GenericClassType::Kind:
  544. case GenericInterfaceType::Kind:
  545. case ImportDecl::Kind:
  546. case InterfaceDecl::Kind:
  547. case InterfaceType::Kind:
  548. case InterfaceWitness::Kind:
  549. case InterfaceWitnessAccess::Kind:
  550. case IntLiteral::Kind:
  551. case IntType::Kind:
  552. case Param::Kind:
  553. case PointerType::Kind:
  554. case StringLiteral::Kind:
  555. case StructValue::Kind:
  556. case StructType::Kind:
  557. case TupleValue::Kind:
  558. case TupleType::Kind:
  559. case UnaryOperatorNot::Kind:
  560. case UnboundElementType::Kind:
  561. case ValueOfInitializer::Kind:
  562. return value_category;
  563. case CARBON_KIND(BuiltinInst inst): {
  564. if (inst.builtin_inst_kind == BuiltinInstKind::Error) {
  565. return ExprCategory::Error;
  566. }
  567. return value_category;
  568. }
  569. case CARBON_KIND(BindName inst): {
  570. inst_id = inst.value_id;
  571. continue;
  572. }
  573. case CARBON_KIND(ArrayIndex inst): {
  574. inst_id = inst.array_id;
  575. continue;
  576. }
  577. case CARBON_KIND(ClassElementAccess inst): {
  578. inst_id = inst.base_id;
  579. // A value of class type is a pointer to an object representation.
  580. // Therefore, if the base is a value, the result is an ephemeral
  581. // reference.
  582. value_category = ExprCategory::EphemeralRef;
  583. continue;
  584. }
  585. case CARBON_KIND(StructAccess inst): {
  586. inst_id = inst.struct_id;
  587. continue;
  588. }
  589. case CARBON_KIND(TupleAccess inst): {
  590. inst_id = inst.tuple_id;
  591. continue;
  592. }
  593. case CARBON_KIND(TupleIndex inst): {
  594. inst_id = inst.tuple_id;
  595. continue;
  596. }
  597. case CARBON_KIND(SpliceBlock inst): {
  598. inst_id = inst.result_id;
  599. continue;
  600. }
  601. case StructLiteral::Kind:
  602. case TupleLiteral::Kind:
  603. return ExprCategory::Mixed;
  604. case ArrayInit::Kind:
  605. case Call::Kind:
  606. case InitializeFrom::Kind:
  607. case ClassInit::Kind:
  608. case StructInit::Kind:
  609. case TupleInit::Kind:
  610. return ExprCategory::Initializing;
  611. case Deref::Kind:
  612. case VarStorage::Kind:
  613. return ExprCategory::DurableRef;
  614. case Temporary::Kind:
  615. case TemporaryStorage::Kind:
  616. case ValueAsRef::Kind:
  617. return ExprCategory::EphemeralRef;
  618. }
  619. }
  620. }
  621. } // namespace Carbon::SemIR