file.cpp 22 KB

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