file.cpp 23 KB

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