expr_info.cpp 8.1 KB

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