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