file.cpp 22 KB

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