expr_info.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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/expr_info.h"
  5. #include <concepts>
  6. #include "common/check.h"
  7. #include "toolchain/base/kind_switch.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. #include "toolchain/sem_ir/inst_kind.h"
  10. #include "toolchain/sem_ir/typed_insts.h"
  11. namespace Carbon::SemIR {
  12. // Returns the InstId represented by an instruction operand.
  13. static auto AsAnyInstId(Inst::ArgAndKind arg) -> InstId {
  14. if (auto inst_id = arg.TryAs<SemIR::InstId>()) {
  15. return *inst_id;
  16. }
  17. return arg.As<SemIR::AbsoluteInstId>();
  18. }
  19. static auto GetExprCategoryImpl(const File* ir, InstId inst_id)
  20. -> ExprCategory {
  21. // The overall expression category if the current instruction is a value
  22. // expression.
  23. ExprCategory value_category = ExprCategory::Value;
  24. while (true) {
  25. auto untyped_inst = ir->insts().Get(inst_id);
  26. auto category_from_kind = untyped_inst.kind().expr_category();
  27. // If this instruction kind has a fixed category, return it.
  28. if (auto fixed_category = category_from_kind.TryAsFixedCategory()) {
  29. return *fixed_category == ExprCategory::Value ? value_category
  30. : *fixed_category;
  31. }
  32. // Handle any special cases that use
  33. // ComputedExprCategory::DependsOnOperands.
  34. auto handle_special_case =
  35. [&]<typename TypedInstT>(
  36. TypedInstT inst) -> std::optional<ExprCategory> {
  37. if constexpr (std::same_as<TypedInstT, ClassElementAccess>) {
  38. inst_id = inst.base_id;
  39. // A value of class type is a pointer to an object representation.
  40. // Therefore, if the base is a value, the result is an ephemeral
  41. // reference.
  42. value_category = ExprCategory::EphemeralRef;
  43. return std::nullopt;
  44. } else if constexpr (std::same_as<TypedInstT, ImportRefLoaded> ||
  45. std::same_as<TypedInstT, ImportRefUnloaded>) {
  46. auto import_ir_inst = ir->import_ir_insts().Get(inst.import_ir_inst_id);
  47. ir = ir->import_irs().Get(import_ir_inst.ir_id()).sem_ir;
  48. inst_id = import_ir_inst.inst_id();
  49. return std::nullopt;
  50. } else if constexpr (std::same_as<TypedInstT, Call>) {
  51. auto callee = GetCallee(*ir, inst.callee_id);
  52. CARBON_KIND_SWITCH(callee) {
  53. case CARBON_KIND(SemIR::CalleeError _): {
  54. return ExprCategory::Error;
  55. }
  56. case CARBON_KIND(SemIR::CalleeFunction callee_function): {
  57. const auto& function =
  58. ir->functions().Get(callee_function.function_id);
  59. auto return_form_id = function.GetDeclaredReturnForm(
  60. *ir, callee_function.resolved_specific_id);
  61. if (!return_form_id.has_value()) {
  62. // Treat as equivalent to `-> ()`.
  63. return ExprCategory::ReprInitializing;
  64. }
  65. auto return_form = ir->insts().Get(return_form_id);
  66. CARBON_KIND_SWITCH(return_form) {
  67. case CARBON_KIND(InitForm _):
  68. return ExprCategory::ReprInitializing;
  69. case CARBON_KIND(RefForm _):
  70. return ExprCategory::DurableRef;
  71. case CARBON_KIND(ValueForm _):
  72. return ExprCategory::Value;
  73. case CARBON_KIND(ErrorInst _):
  74. return ExprCategory::Error;
  75. default:
  76. CARBON_FATAL("Unexpected inst kind: {0}", return_form);
  77. }
  78. }
  79. case CARBON_KIND(SemIR::CalleeNonFunction _): {
  80. return ExprCategory::NotExpr;
  81. }
  82. case CARBON_KIND(SemIR::CalleeCppOverloadSet _): {
  83. // TODO: support `ref` returns from C++.
  84. return ExprCategory::ReprInitializing;
  85. }
  86. }
  87. } else if constexpr (std::same_as<TypedInstT, FormBinding>) {
  88. auto form_id = ir->entity_names().Get(inst.entity_name_id).form_id;
  89. auto form_inst = ir->insts().Get(form_id);
  90. CARBON_KIND_SWITCH(form_inst) {
  91. case InitForm::Kind:
  92. // A `var` binding pattern produces a `ref` binding.
  93. case RefForm::Kind:
  94. return ExprCategory::DurableRef;
  95. case ValueForm::Kind:
  96. return ExprCategory::Value;
  97. case ErrorInst::Kind:
  98. return ExprCategory::Error;
  99. default:
  100. CARBON_FATAL("Unexpected kind for form inst {0}", form_inst);
  101. }
  102. } else {
  103. static_assert(
  104. TypedInstT::Kind.expr_category().TryAsComputedCategory() !=
  105. ComputedExprCategory::DependsOnOperands,
  106. "Missing expression category computation for type");
  107. }
  108. CARBON_FATAL("Unreachable");
  109. };
  110. // If the category depends on the operands of the instruction, determine it.
  111. // Usually this means the category is the same as the category of an
  112. // operand.
  113. switch (*category_from_kind.TryAsComputedCategory()) {
  114. case ComputedExprCategory::ValueIfHasType: {
  115. return untyped_inst.kind().has_type() ? value_category
  116. : ExprCategory::NotExpr;
  117. }
  118. case ComputedExprCategory::SameAsFirstOperand: {
  119. inst_id = AsAnyInstId(untyped_inst.arg0_and_kind());
  120. break;
  121. }
  122. case ComputedExprCategory::SameAsSecondOperand: {
  123. inst_id = AsAnyInstId(untyped_inst.arg1_and_kind());
  124. break;
  125. }
  126. case ComputedExprCategory::DependsOnOperands: {
  127. switch (untyped_inst.kind()) {
  128. #define CARBON_SEM_IR_INST_KIND(TypedInstT) \
  129. case TypedInstT::Kind: { \
  130. auto category = handle_special_case(untyped_inst.As<TypedInstT>()); \
  131. if (category.has_value()) { \
  132. return *category; \
  133. } \
  134. break; \
  135. }
  136. #include "toolchain/sem_ir/inst_kind.def"
  137. }
  138. }
  139. }
  140. }
  141. }
  142. auto GetExprCategory(const File& file, InstId inst_id) -> ExprCategory {
  143. return GetExprCategoryImpl(&file, inst_id);
  144. }
  145. auto FindStorageArgForInitializer(const File& sem_ir, InstId init_id,
  146. bool allow_transitive) -> InstId {
  147. while (true) {
  148. Inst init_untyped = sem_ir.insts().Get(init_id);
  149. CARBON_KIND_SWITCH(init_untyped) {
  150. case CARBON_KIND(AsCompatible init): {
  151. if (!allow_transitive) {
  152. return InstId::None;
  153. }
  154. init_id = init.source_id;
  155. continue;
  156. }
  157. case CARBON_KIND(Converted init): {
  158. if (!allow_transitive) {
  159. return InstId::None;
  160. }
  161. init_id = init.result_id;
  162. continue;
  163. }
  164. case CARBON_KIND(UpdateInit init): {
  165. if (!allow_transitive) {
  166. return InstId::None;
  167. }
  168. init_id = init.base_init_id;
  169. continue;
  170. }
  171. case CARBON_KIND(ArrayInit init): {
  172. return init.dest_id;
  173. }
  174. case CARBON_KIND(ClassInit init): {
  175. return init.dest_id;
  176. }
  177. case CARBON_KIND(StructInit init): {
  178. return init.dest_id;
  179. }
  180. case CARBON_KIND(TupleInit init): {
  181. return init.dest_id;
  182. }
  183. case CARBON_KIND(InPlaceInit init): {
  184. return init.dest_id;
  185. }
  186. case CARBON_KIND(MarkInPlaceInit init): {
  187. return init.dest_id;
  188. }
  189. case CARBON_KIND(Call call): {
  190. auto callee_function = GetCalleeAsFunction(sem_ir, call.callee_id);
  191. const auto& function =
  192. sem_ir.functions().Get(callee_function.function_id);
  193. if (!function.return_form_inst_id.has_value()) {
  194. return InstId::None;
  195. }
  196. auto return_form_constant_id = GetConstantValueInSpecific(
  197. sem_ir, callee_function.resolved_specific_id,
  198. function.return_form_inst_id);
  199. auto return_form = sem_ir.insts().Get(
  200. sem_ir.constant_values().GetInstId(return_form_constant_id));
  201. CARBON_KIND_SWITCH(return_form) {
  202. case CARBON_KIND(InitForm init_form): {
  203. auto type_id = sem_ir.types().GetTypeIdForTypeInstId(
  204. init_form.type_component_inst_id);
  205. if (!InitRepr::ForType(sem_ir, type_id).MightBeInPlace()) {
  206. return InstId::None;
  207. }
  208. if (!call.args_id.has_value()) {
  209. // Argument initialization failed, so we have no return slot.
  210. return InstId::None;
  211. }
  212. CARBON_CHECK(function.call_param_ranges.return_size() == 1,
  213. "Unexpected number of output parameters on function");
  214. return sem_ir.inst_blocks().Get(
  215. call.args_id)[function.call_param_ranges.return_begin().index];
  216. }
  217. case CARBON_KIND(RefForm _): {
  218. return InstId::None;
  219. }
  220. default:
  221. CARBON_FATAL("Unexpected inst kind: {0}", return_form);
  222. }
  223. }
  224. case CARBON_KIND(ErrorInst _): {
  225. return InstId::None;
  226. }
  227. default:
  228. CARBON_FATAL("Initialization from unexpected inst {0}", init_untyped);
  229. }
  230. }
  231. }
  232. } // namespace Carbon::SemIR