handle_index.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <optional>
  5. #include "toolchain/base/kind_switch.h"
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/check/convert.h"
  8. #include "toolchain/check/handle.h"
  9. #include "toolchain/check/inst.h"
  10. #include "toolchain/check/literal.h"
  11. #include "toolchain/check/name_lookup.h"
  12. #include "toolchain/check/operator.h"
  13. #include "toolchain/check/type.h"
  14. #include "toolchain/diagnostics/diagnostic.h"
  15. #include "toolchain/sem_ir/expr_info.h"
  16. #include "toolchain/sem_ir/inst.h"
  17. #include "toolchain/sem_ir/typed_insts.h"
  18. namespace Carbon::Check {
  19. auto HandleParseNode(Context& /*context*/, Parse::IndexExprStartId /*node_id*/)
  20. -> bool {
  21. // Leave the expression on the stack for IndexExpr.
  22. return true;
  23. }
  24. // Returns the argument values of the `IndexWith` interface. Arguments
  25. // correspond to the `SubscriptType` and the `ElementType`. If no arguments are
  26. // used to define `IndexWith`, this returns an empty array reference. If the
  27. // class does not implement the said interface, this returns a `std::nullopt`.
  28. // TODO: Switch to using an associated type instead of a parameter for the
  29. // `ElementType`.
  30. static auto GetIndexWithArgs(Context& context, Parse::NodeId node_id,
  31. SemIR::TypeId self_id)
  32. -> std::optional<llvm::ArrayRef<SemIR::InstId>> {
  33. auto index_with_inst_id = LookupNameInCore(context, node_id, "IndexWith");
  34. // If the `IndexWith` interface doesn't have generic arguments then return an
  35. // empty reference.
  36. if (context.insts().Is<SemIR::FacetType>(index_with_inst_id)) {
  37. return llvm::ArrayRef<SemIR::InstId>();
  38. }
  39. auto index_with_inst =
  40. context.insts().TryGetAsIfValid<SemIR::StructValue>(index_with_inst_id);
  41. if (!index_with_inst) {
  42. return std::nullopt;
  43. }
  44. auto index_with_interface =
  45. context.types().TryGetAs<SemIR::GenericInterfaceType>(
  46. index_with_inst->type_id);
  47. if (!index_with_interface) {
  48. return std::nullopt;
  49. }
  50. for (const auto& impl : context.impls().values()) {
  51. auto impl_self_type_id =
  52. context.types().GetTypeIdForTypeInstId(impl.self_id);
  53. auto impl_constraint_type_id =
  54. context.types().GetTypeIdForTypeInstId(impl.constraint_id);
  55. if (impl_self_type_id != self_id) {
  56. continue;
  57. }
  58. auto facet_type =
  59. context.types().TryGetAs<SemIR::FacetType>(impl_constraint_type_id);
  60. if (!facet_type) {
  61. continue;
  62. }
  63. const auto& facet_type_info =
  64. context.facet_types().Get(facet_type->facet_type_id);
  65. auto interface_type = facet_type_info.TryAsSingleInterface();
  66. if (!interface_type) {
  67. continue;
  68. }
  69. if (index_with_interface->interface_id != interface_type->interface_id) {
  70. continue;
  71. }
  72. return context.inst_blocks().GetOrEmpty(
  73. context.specifics().Get(interface_type->specific_id).args_id);
  74. }
  75. return std::nullopt;
  76. }
  77. // Performs an index with base expression `operand_inst_id` and
  78. // `operand_type_id` for types that are not an array. This checks if
  79. // the base expression implements the `IndexWith` interface; if so, uses the
  80. // `At` associative method, otherwise prints a diagnostic.
  81. static auto PerformIndexWith(Context& context, Parse::NodeId node_id,
  82. SemIR::InstId operand_inst_id,
  83. SemIR::TypeId operand_type_id,
  84. SemIR::InstId index_inst_id) -> SemIR::InstId {
  85. auto args = GetIndexWithArgs(context, node_id, operand_type_id);
  86. // If the type does not implement the `IndexWith` interface, then return
  87. // an error.
  88. if (!args) {
  89. CARBON_DIAGNOSTIC(TypeNotIndexable, Error,
  90. "type {0} does not support indexing", SemIR::TypeId);
  91. context.emitter().Emit(node_id, TypeNotIndexable, operand_type_id);
  92. return SemIR::ErrorInst::InstId;
  93. }
  94. Operator op{
  95. .interface_name = "IndexWith",
  96. .interface_args_ref = *args,
  97. .op_name = "At",
  98. };
  99. // IndexWith is defined without generic arguments.
  100. if (args->empty()) {
  101. return BuildBinaryOperator(context, node_id, op, operand_inst_id,
  102. index_inst_id);
  103. }
  104. // The first argument of the `IndexWith` interface corresponds to the
  105. // `SubscriptType`, so first cast `index_inst_id` to that type.
  106. auto subscript_type_id = context.types().GetTypeIdForTypeInstId((*args)[0]);
  107. auto cast_index_id =
  108. ConvertToValueOfType(context, node_id, index_inst_id, subscript_type_id);
  109. return BuildBinaryOperator(context, node_id, op, operand_inst_id,
  110. cast_index_id);
  111. }
  112. auto HandleParseNode(Context& context, Parse::IndexExprId node_id) -> bool {
  113. auto index_inst_id = context.node_stack().PopExpr();
  114. auto operand_inst_id = context.node_stack().PopExpr();
  115. operand_inst_id = ConvertToValueOrRefExpr(context, operand_inst_id);
  116. auto operand_inst = context.insts().Get(operand_inst_id);
  117. auto operand_type_id = operand_inst.type_id();
  118. CARBON_KIND_SWITCH(context.types().GetAsInst(operand_type_id)) {
  119. case CARBON_KIND(SemIR::ArrayType array_type): {
  120. auto cast_index_id = ConvertToValueOfType(
  121. context, SemIR::LocId(index_inst_id), index_inst_id,
  122. // TODO: Replace this with impl lookup rather than hardcoding `i32`.
  123. MakeIntType(context, node_id, SemIR::IntKind::Signed,
  124. context.ints().Add(32)));
  125. auto array_cat =
  126. SemIR::GetExprCategory(context.sem_ir(), operand_inst_id);
  127. if (array_cat == SemIR::ExprCategory::Value) {
  128. // If the operand is an array value, convert it to an ephemeral
  129. // reference to an array so we can perform a primitive indexing into it.
  130. operand_inst_id = AddInst<SemIR::ValueAsRef>(
  131. context, node_id,
  132. {.type_id = operand_type_id, .value_id = operand_inst_id});
  133. }
  134. // Constant evaluation will perform a bounds check on this array indexing
  135. // if the index is constant.
  136. auto elem_id = AddInst<SemIR::ArrayIndex>(
  137. context, node_id,
  138. {.type_id = context.types().GetTypeIdForTypeInstId(
  139. array_type.element_type_inst_id),
  140. .array_id = operand_inst_id,
  141. .index_id = cast_index_id});
  142. if (array_cat != SemIR::ExprCategory::DurableRef) {
  143. // Indexing a durable reference gives a durable reference expression.
  144. // Indexing anything else gives a value expression.
  145. // TODO: This should be replaced by a choice between using `IndexWith`
  146. // and `IndirectIndexWith`.
  147. elem_id = ConvertToValueExpr(context, elem_id);
  148. }
  149. context.node_stack().Push(node_id, elem_id);
  150. return true;
  151. }
  152. default: {
  153. auto elem_id = SemIR::ErrorInst::InstId;
  154. if (operand_type_id != SemIR::ErrorInst::TypeId) {
  155. elem_id = PerformIndexWith(context, node_id, operand_inst_id,
  156. operand_type_id, index_inst_id);
  157. }
  158. context.node_stack().Push(node_id, elem_id);
  159. return true;
  160. }
  161. }
  162. }
  163. } // namespace Carbon::Check