handle_index.cpp 6.7 KB

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