file.cpp 23 KB

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