expr_info.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 {
  88. static_assert(
  89. TypedInstT::Kind.expr_category().TryAsComputedCategory() !=
  90. ComputedExprCategory::DependsOnOperands,
  91. "Missing expression category computation for type");
  92. }
  93. CARBON_FATAL("Unreachable");
  94. };
  95. // If the category depends on the operands of the instruction, determine it.
  96. // Usually this means the category is the same as the category of an
  97. // operand.
  98. switch (*category_from_kind.TryAsComputedCategory()) {
  99. case ComputedExprCategory::ValueIfHasType: {
  100. return untyped_inst.kind().has_type() ? value_category
  101. : ExprCategory::NotExpr;
  102. }
  103. case ComputedExprCategory::SameAsFirstOperand: {
  104. inst_id = AsAnyInstId(untyped_inst.arg0_and_kind());
  105. break;
  106. }
  107. case ComputedExprCategory::SameAsSecondOperand: {
  108. inst_id = AsAnyInstId(untyped_inst.arg1_and_kind());
  109. break;
  110. }
  111. case ComputedExprCategory::DependsOnOperands: {
  112. switch (untyped_inst.kind()) {
  113. #define CARBON_SEM_IR_INST_KIND(TypedInstT) \
  114. case TypedInstT::Kind: { \
  115. auto category = handle_special_case(untyped_inst.As<TypedInstT>()); \
  116. if (category.has_value()) { \
  117. return *category; \
  118. } \
  119. break; \
  120. }
  121. #include "toolchain/sem_ir/inst_kind.def"
  122. }
  123. }
  124. }
  125. }
  126. }
  127. auto GetExprCategory(const File& file, InstId inst_id) -> ExprCategory {
  128. return GetExprCategoryImpl(&file, inst_id);
  129. }
  130. auto FindStorageArgForInitializer(const File& sem_ir, InstId init_id,
  131. bool allow_transitive) -> InstId {
  132. while (true) {
  133. Inst init_untyped = sem_ir.insts().Get(init_id);
  134. CARBON_KIND_SWITCH(init_untyped) {
  135. case CARBON_KIND(AsCompatible init): {
  136. if (!allow_transitive) {
  137. return InstId::None;
  138. }
  139. init_id = init.source_id;
  140. continue;
  141. }
  142. case CARBON_KIND(Converted init): {
  143. if (!allow_transitive) {
  144. return InstId::None;
  145. }
  146. init_id = init.result_id;
  147. continue;
  148. }
  149. case CARBON_KIND(UpdateInit init): {
  150. if (!allow_transitive) {
  151. return InstId::None;
  152. }
  153. init_id = init.base_init_id;
  154. continue;
  155. }
  156. case CARBON_KIND(ArrayInit init): {
  157. return init.dest_id;
  158. }
  159. case CARBON_KIND(ClassInit init): {
  160. return init.dest_id;
  161. }
  162. case CARBON_KIND(StructInit init): {
  163. return init.dest_id;
  164. }
  165. case CARBON_KIND(TupleInit init): {
  166. return init.dest_id;
  167. }
  168. case CARBON_KIND(InPlaceInit init): {
  169. return init.dest_id;
  170. }
  171. case CARBON_KIND(MarkInPlaceInit init): {
  172. return init.dest_id;
  173. }
  174. case CARBON_KIND(Call call): {
  175. auto callee_function = GetCalleeAsFunction(sem_ir, call.callee_id);
  176. const auto& function =
  177. sem_ir.functions().Get(callee_function.function_id);
  178. if (!function.return_form_inst_id.has_value()) {
  179. return InstId::None;
  180. }
  181. auto return_form_constant_id = GetConstantValueInSpecific(
  182. sem_ir, callee_function.resolved_specific_id,
  183. function.return_form_inst_id);
  184. auto return_form = sem_ir.insts().Get(
  185. sem_ir.constant_values().GetInstId(return_form_constant_id));
  186. CARBON_KIND_SWITCH(return_form) {
  187. case CARBON_KIND(InitForm init_form): {
  188. auto type_id = sem_ir.types().GetTypeIdForTypeInstId(
  189. init_form.type_component_inst_id);
  190. if (!InitRepr::ForType(sem_ir, type_id).MightBeInPlace()) {
  191. return InstId::None;
  192. }
  193. if (!call.args_id.has_value()) {
  194. // Argument initialization failed, so we have no return slot.
  195. return InstId::None;
  196. }
  197. CARBON_CHECK(function.call_param_ranges.return_size() == 1,
  198. "Unexpected number of output parameters on function");
  199. return sem_ir.inst_blocks().Get(
  200. call.args_id)[function.call_param_ranges.return_begin().index];
  201. }
  202. case CARBON_KIND(RefForm _): {
  203. return InstId::None;
  204. }
  205. default:
  206. CARBON_FATAL("Unexpected inst kind: {0}", return_form);
  207. }
  208. }
  209. case CARBON_KIND(ErrorInst _): {
  210. return InstId::None;
  211. }
  212. default:
  213. CARBON_FATAL("Initialization from unexpected inst {0}", init_untyped);
  214. }
  215. }
  216. }
  217. } // namespace Carbon::SemIR