handle_index.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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::InterfaceType>(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 interface_type =
  54. context.types().TryGetAs<SemIR::InterfaceType>(impl_constraint_type_id);
  55. if (!interface_type) {
  56. continue;
  57. }
  58. if (index_with_interface->interface_id != interface_type->interface_id) {
  59. continue;
  60. }
  61. return context.inst_blocks().GetOrEmpty(
  62. context.specifics().Get(interface_type->specific_id).args_id);
  63. }
  64. return std::nullopt;
  65. }
  66. // Performs an index with base expression `operand_inst_id` and
  67. // `operand_type_id` for types that are not an array. This checks if
  68. // the base expression implements the `IndexWith` interface; if so, uses the
  69. // `At` associative method, otherwise prints a diagnostic.
  70. static auto PerformIndexWith(Context& context, Parse::NodeId node_id,
  71. SemIR::InstId operand_inst_id,
  72. SemIR::TypeId operand_type_id,
  73. SemIR::InstId index_inst_id) -> SemIR::InstId {
  74. auto args = GetIndexWithArgs(context, node_id, operand_type_id);
  75. // If the type does not implement the `IndexWith` interface, then return
  76. // an error.
  77. if (!args) {
  78. CARBON_DIAGNOSTIC(TypeNotIndexable, Error,
  79. "type {0} does not support indexing", SemIR::TypeId);
  80. context.emitter().Emit(node_id, TypeNotIndexable, operand_type_id);
  81. return SemIR::InstId::BuiltinError;
  82. }
  83. Operator op{
  84. .interface_name = "IndexWith",
  85. .interface_args_ref = *args,
  86. .op_name = "At",
  87. };
  88. // IndexWith is defined without generic arguments.
  89. if (args->empty()) {
  90. return BuildBinaryOperator(context, node_id, op, operand_inst_id,
  91. index_inst_id);
  92. }
  93. // The first argument of the `IndexWith` interface corresponds to the
  94. // `SubscriptType`, so first cast `index_inst_id` to that type.
  95. auto subscript_type_id = context.GetTypeIdForTypeInst((*args)[0]);
  96. auto cast_index_id =
  97. ConvertToValueOfType(context, node_id, index_inst_id, subscript_type_id);
  98. return BuildBinaryOperator(context, node_id, op, operand_inst_id,
  99. cast_index_id);
  100. }
  101. auto HandleParseNode(Context& context, Parse::IndexExprId node_id) -> bool {
  102. auto index_inst_id = context.node_stack().PopExpr();
  103. auto operand_inst_id = context.node_stack().PopExpr();
  104. operand_inst_id = ConvertToValueOrRefExpr(context, operand_inst_id);
  105. auto operand_inst = context.insts().Get(operand_inst_id);
  106. auto operand_type_id = operand_inst.type_id();
  107. CARBON_KIND_SWITCH(context.types().GetAsInst(operand_type_id)) {
  108. case CARBON_KIND(SemIR::ArrayType array_type): {
  109. auto index_loc_id = context.insts().GetLocId(index_inst_id);
  110. auto cast_index_id = ConvertToValueOfType(
  111. context, index_loc_id, index_inst_id,
  112. context.GetBuiltinType(SemIR::BuiltinInstKind::IntType));
  113. auto array_cat =
  114. SemIR::GetExprCategory(context.sem_ir(), operand_inst_id);
  115. if (array_cat == SemIR::ExprCategory::Value) {
  116. // If the operand is an array value, convert it to an ephemeral
  117. // reference to an array so we can perform a primitive indexing into it.
  118. operand_inst_id = context.AddInst<SemIR::ValueAsRef>(
  119. node_id, {.type_id = operand_type_id, .value_id = operand_inst_id});
  120. }
  121. // Constant evaluation will perform a bounds check on this array indexing
  122. // if the index is constant.
  123. auto elem_id = context.AddInst<SemIR::ArrayIndex>(
  124. node_id, {.type_id = array_type.element_type_id,
  125. .array_id = operand_inst_id,
  126. .index_id = cast_index_id});
  127. if (array_cat != SemIR::ExprCategory::DurableRef) {
  128. // Indexing a durable reference gives a durable reference expression.
  129. // Indexing anything else gives a value expression.
  130. // TODO: This should be replaced by a choice between using `IndexWith`
  131. // and `IndirectIndexWith`.
  132. elem_id = ConvertToValueExpr(context, elem_id);
  133. }
  134. context.node_stack().Push(node_id, elem_id);
  135. return true;
  136. }
  137. default: {
  138. auto elem_id = SemIR::InstId::BuiltinError;
  139. if (operand_type_id != SemIR::TypeId::Error) {
  140. elem_id = PerformIndexWith(context, node_id, operand_inst_id,
  141. operand_type_id, index_inst_id);
  142. }
  143. context.node_stack().Push(node_id, elem_id);
  144. return true;
  145. }
  146. }
  147. }
  148. } // namespace Carbon::Check