handle_index.cpp 7.0 KB

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