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_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. constant_values_(ConstantId::NotConstant),
  62. inst_blocks_(allocator_),
  63. constants_(*this, allocator_) {
  64. insts_.Reserve(BuiltinKind::ValidCount);
  65. // Error uses a self-referential type so that it's not accidentally treated as
  66. // a normal type. Every other builtin is a type, including the
  67. // self-referential TypeType.
  68. #define CARBON_SEM_IR_BUILTIN_KIND(Name, ...) \
  69. insts_.AddInNoBlock(LocIdAndInst::NoLoc( \
  70. Builtin{BuiltinKind::Name == BuiltinKind::Error ? TypeId::Error \
  71. : TypeId::TypeType, \
  72. BuiltinKind::Name}));
  73. #include "toolchain/sem_ir/builtin_kind.def"
  74. CARBON_CHECK(insts_.size() == BuiltinKind::ValidCount)
  75. << "Builtins should produce " << BuiltinKind::ValidCount
  76. << " insts, actual: " << insts_.size();
  77. for (auto i : llvm::seq(BuiltinKind::ValidCount)) {
  78. auto builtin_id = SemIR::InstId(i);
  79. constant_values_.Set(builtin_id,
  80. SemIR::ConstantId::ForTemplateConstant(builtin_id));
  81. }
  82. }
  83. auto File::Verify() const -> ErrorOr<Success> {
  84. // Invariants don't necessarily hold for invalid IR.
  85. if (has_errors_) {
  86. return Success();
  87. }
  88. // Check that every code block has a terminator sequence that appears at the
  89. // end of the block.
  90. for (const Function& function : functions_.array_ref()) {
  91. for (InstBlockId block_id : function.body_block_ids) {
  92. TerminatorKind prior_kind = TerminatorKind::NotTerminator;
  93. for (InstId inst_id : inst_blocks().Get(block_id)) {
  94. TerminatorKind inst_kind =
  95. insts().Get(inst_id).kind().terminator_kind();
  96. if (prior_kind == TerminatorKind::Terminator) {
  97. return Error(llvm::formatv("Inst {0} in block {1} follows terminator",
  98. inst_id, block_id));
  99. }
  100. if (prior_kind > inst_kind) {
  101. return Error(
  102. llvm::formatv("Non-terminator inst {0} in block {1} follows "
  103. "terminator sequence",
  104. inst_id, block_id));
  105. }
  106. prior_kind = inst_kind;
  107. }
  108. if (prior_kind != TerminatorKind::Terminator) {
  109. return Error(llvm::formatv("No terminator in block {0}", block_id));
  110. }
  111. }
  112. }
  113. // TODO: Check that an instruction only references other instructions that are
  114. // either global or that dominate it.
  115. return Success();
  116. }
  117. auto File::OutputYaml(bool include_builtins) const -> Yaml::OutputMapping {
  118. return Yaml::OutputMapping([this,
  119. include_builtins](Yaml::OutputMapping::Map map) {
  120. map.Add("filename", filename_);
  121. map.Add(
  122. "sem_ir", Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  123. map.Add("import_irs_size", Yaml::OutputScalar(import_irs_.size()));
  124. map.Add("name_scopes", name_scopes_.OutputYaml());
  125. map.Add("bind_names", bind_names_.OutputYaml());
  126. map.Add("functions", functions_.OutputYaml());
  127. map.Add("classes", classes_.OutputYaml());
  128. map.Add("types", types_.OutputYaml());
  129. map.Add("type_blocks", type_blocks_.OutputYaml());
  130. map.Add("insts",
  131. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  132. int start = include_builtins ? 0 : BuiltinKind::ValidCount;
  133. for (int i : llvm::seq(start, insts_.size())) {
  134. auto id = InstId(i);
  135. map.Add(PrintToString(id),
  136. Yaml::OutputScalar(insts_.Get(id)));
  137. }
  138. }));
  139. map.Add("constant_values",
  140. Yaml::OutputMapping([&](Yaml::OutputMapping::Map map) {
  141. int start = include_builtins ? 0 : BuiltinKind::ValidCount;
  142. for (int i : llvm::seq(start, insts_.size())) {
  143. auto id = InstId(i);
  144. auto value = constant_values_.Get(id);
  145. if (!value.is_valid() || value.is_constant()) {
  146. map.Add(PrintToString(id), Yaml::OutputScalar(value));
  147. }
  148. }
  149. }));
  150. map.Add("inst_blocks", inst_blocks_.OutputYaml());
  151. }));
  152. });
  153. }
  154. // Map an instruction kind representing a type into an integer describing the
  155. // precedence of that type's syntax. Higher numbers correspond to higher
  156. // precedence.
  157. static auto GetTypePrecedence(InstKind kind) -> int {
  158. switch (kind) {
  159. case ArrayType::Kind:
  160. case AssociatedEntityType::Kind:
  161. case BindSymbolicName::Kind:
  162. case Builtin::Kind:
  163. case ClassType::Kind:
  164. case FloatType::Kind:
  165. case FunctionType::Kind:
  166. case GenericClassType::Kind:
  167. case InterfaceType::Kind:
  168. case InterfaceWitnessAccess::Kind:
  169. case IntType::Kind:
  170. case StructType::Kind:
  171. case TupleType::Kind:
  172. case UnboundElementType::Kind:
  173. return 0;
  174. case ConstType::Kind:
  175. return -1;
  176. case PointerType::Kind:
  177. return -2;
  178. #define CARBON_SEM_IR_INST_KIND_TYPE_ALWAYS(...)
  179. #define CARBON_SEM_IR_INST_KIND_TYPE_MAYBE(...)
  180. #define CARBON_SEM_IR_INST_KIND(Name) case SemIR::Name::Kind:
  181. #include "toolchain/sem_ir/inst_kind.def"
  182. CARBON_FATAL() << "GetTypePrecedence for non-type inst kind " << kind;
  183. }
  184. }
  185. // Implements File::StringifyTypeExpr. Static to prevent accidental use of
  186. // member functions while traversing IRs.
  187. static auto StringifyTypeExprImpl(const SemIR::File& outer_sem_ir,
  188. InstId outer_inst_id) {
  189. std::string str;
  190. llvm::raw_string_ostream out(str);
  191. struct Step {
  192. // The instruction's file.
  193. const File& sem_ir;
  194. // The instruction to print.
  195. InstId inst_id;
  196. // The index into inst_id to print. Not used by all types.
  197. int index = 0;
  198. auto Next() const -> Step {
  199. return {.sem_ir = sem_ir, .inst_id = inst_id, .index = index + 1};
  200. }
  201. };
  202. llvm::SmallVector<Step> steps = {
  203. Step{.sem_ir = outer_sem_ir, .inst_id = outer_inst_id}};
  204. while (!steps.empty()) {
  205. auto step = steps.pop_back_val();
  206. if (!step.inst_id.is_valid()) {
  207. out << "<invalid type>";
  208. continue;
  209. }
  210. // Builtins have designated labels.
  211. if (step.inst_id.is_builtin()) {
  212. out << step.inst_id.builtin_kind().label();
  213. continue;
  214. }
  215. const auto& sem_ir = step.sem_ir;
  216. // Helper for instructions with the current sem_ir.
  217. auto push_inst_id = [&](InstId inst_id) {
  218. steps.push_back({.sem_ir = sem_ir, .inst_id = inst_id});
  219. };
  220. auto untyped_inst = sem_ir.insts().Get(step.inst_id);
  221. CARBON_KIND_SWITCH(untyped_inst) {
  222. case CARBON_KIND(ArrayType inst): {
  223. if (step.index == 0) {
  224. out << "[";
  225. steps.push_back(step.Next());
  226. push_inst_id(sem_ir.types().GetInstId(inst.element_type_id));
  227. } else if (step.index == 1) {
  228. out << "; " << sem_ir.GetArrayBoundValue(inst.bound_id) << "]";
  229. }
  230. break;
  231. }
  232. case CARBON_KIND(AssociatedEntityType inst): {
  233. if (step.index == 0) {
  234. out << "<associated ";
  235. steps.push_back(step.Next());
  236. push_inst_id(sem_ir.types().GetInstId(inst.entity_type_id));
  237. } else {
  238. auto interface_name_id =
  239. sem_ir.interfaces().Get(inst.interface_id).name_id;
  240. out << " in " << sem_ir.names().GetFormatted(interface_name_id)
  241. << ">";
  242. }
  243. break;
  244. }
  245. case BindAlias::Kind:
  246. case BindSymbolicName::Kind:
  247. case ExportDecl::Kind: {
  248. auto name_id = untyped_inst.As<AnyBindNameOrExportDecl>().bind_name_id;
  249. out << sem_ir.names().GetFormatted(
  250. sem_ir.bind_names().Get(name_id).name_id);
  251. break;
  252. }
  253. case CARBON_KIND(ClassType inst): {
  254. auto class_name_id = sem_ir.classes().Get(inst.class_id).name_id;
  255. out << sem_ir.names().GetFormatted(class_name_id);
  256. break;
  257. }
  258. case CARBON_KIND(ConstType inst): {
  259. if (step.index == 0) {
  260. out << "const ";
  261. // Add parentheses if required.
  262. auto inner_type_inst_id = sem_ir.types().GetInstId(inst.inner_id);
  263. if (GetTypePrecedence(sem_ir.insts().Get(inner_type_inst_id).kind()) <
  264. GetTypePrecedence(SemIR::ConstType::Kind)) {
  265. out << "(";
  266. steps.push_back(step.Next());
  267. }
  268. push_inst_id(inner_type_inst_id);
  269. } else if (step.index == 1) {
  270. out << ")";
  271. }
  272. break;
  273. }
  274. case CARBON_KIND(FacetTypeAccess inst): {
  275. // Print `T as type` as simply `T`.
  276. push_inst_id(inst.facet_id);
  277. break;
  278. }
  279. case CARBON_KIND(FloatType inst): {
  280. // TODO: Is this okay?
  281. if (step.index == 1) {
  282. out << ")";
  283. } else if (auto width_value =
  284. sem_ir.insts().TryGetAs<IntLiteral>(inst.bit_width_id)) {
  285. out << "f";
  286. sem_ir.ints().Get(width_value->int_id).print(out, /*isSigned=*/false);
  287. } else {
  288. out << "Core.Float(";
  289. steps.push_back(step.Next());
  290. push_inst_id(inst.bit_width_id);
  291. }
  292. break;
  293. }
  294. case CARBON_KIND(FunctionType inst): {
  295. auto fn_name_id = sem_ir.functions().Get(inst.function_id).name_id;
  296. // TODO: Consider formatting as `typeof(F)` instead.
  297. out << sem_ir.names().GetFormatted(fn_name_id);
  298. break;
  299. }
  300. case CARBON_KIND(GenericClassType inst): {
  301. auto class_name_id = sem_ir.classes().Get(inst.class_id).name_id;
  302. // TODO: Consider formatting as `typeof(C)` instead.
  303. out << sem_ir.names().GetFormatted(class_name_id);
  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 Builtin::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 ImportRefLoaded::Kind:
  423. case ImportRefUnloaded::Kind:
  424. case InitializeFrom::Kind:
  425. case InterfaceDecl::Kind:
  426. case InterfaceWitness::Kind:
  427. case InterfaceWitnessAccess::Kind:
  428. case IntLiteral::Kind:
  429. case Namespace::Kind:
  430. case Param::Kind:
  431. case RealLiteral::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::StringifyTypeExpr(InstId outer_inst_id) const -> std::string {
  464. return StringifyTypeExprImpl(*this, outer_inst_id);
  465. }
  466. auto GetExprCategory(const File& file, InstId inst_id) -> ExprCategory {
  467. const File* ir = &file;
  468. // The overall expression category if the current instruction is a value
  469. // expression.
  470. ExprCategory value_category = ExprCategory::Value;
  471. while (true) {
  472. auto untyped_inst = ir->insts().Get(inst_id);
  473. CARBON_KIND_SWITCH(untyped_inst) {
  474. case AdaptDecl::Kind:
  475. case Assign::Kind:
  476. case BaseDecl::Kind:
  477. case Branch::Kind:
  478. case BranchIf::Kind:
  479. case BranchWithArg::Kind:
  480. case FieldDecl::Kind:
  481. case FunctionDecl::Kind:
  482. case ImplDecl::Kind:
  483. case ImportRefUnloaded::Kind:
  484. case Namespace::Kind:
  485. case Return::Kind:
  486. case ReturnExpr::Kind:
  487. case StructTypeField::Kind:
  488. return ExprCategory::NotExpr;
  489. case CARBON_KIND(ImportRefLoaded inst): {
  490. auto import_ir_inst = ir->import_ir_insts().Get(inst.import_ir_inst_id);
  491. ir = ir->import_irs().Get(import_ir_inst.ir_id).sem_ir;
  492. inst_id = import_ir_inst.inst_id;
  493. continue;
  494. }
  495. case CARBON_KIND(AsCompatible inst): {
  496. inst_id = inst.source_id;
  497. continue;
  498. }
  499. case CARBON_KIND(BindAlias inst): {
  500. inst_id = inst.value_id;
  501. continue;
  502. }
  503. case CARBON_KIND(ExportDecl inst): {
  504. inst_id = inst.value_id;
  505. continue;
  506. }
  507. case CARBON_KIND(NameRef inst): {
  508. inst_id = inst.value_id;
  509. continue;
  510. }
  511. case CARBON_KIND(Converted inst): {
  512. inst_id = inst.result_id;
  513. continue;
  514. }
  515. case AddrOf::Kind:
  516. case AddrPattern::Kind:
  517. case ArrayType::Kind:
  518. case AssociatedConstantDecl::Kind:
  519. case AssociatedEntity::Kind:
  520. case AssociatedEntityType::Kind:
  521. case BindSymbolicName::Kind:
  522. case BindValue::Kind:
  523. case BlockArg::Kind:
  524. case BoolLiteral::Kind:
  525. case BoundMethod::Kind:
  526. case ClassDecl::Kind:
  527. case ClassType::Kind:
  528. case ConstType::Kind:
  529. case FacetTypeAccess::Kind:
  530. case FloatLiteral::Kind:
  531. case FloatType::Kind:
  532. case FunctionType::Kind:
  533. case GenericClassType::Kind:
  534. case InterfaceDecl::Kind:
  535. case InterfaceType::Kind:
  536. case InterfaceWitness::Kind:
  537. case InterfaceWitnessAccess::Kind:
  538. case IntLiteral::Kind:
  539. case IntType::Kind:
  540. case Param::Kind:
  541. case PointerType::Kind:
  542. case RealLiteral::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(Builtin inst): {
  553. if (inst.builtin_kind == BuiltinKind::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