handle_index.cpp 7.1 KB

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