handle_index.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. // Performs an index with base expression `operand_inst_id` and
  25. // `operand_type_id` for types that are not an array. This checks if
  26. // the base expression implements the `IndexWith` interface; if so, uses the
  27. // `At` associative method, otherwise prints a diagnostic.
  28. static auto PerformIndexWith(Context& context, Parse::NodeId node_id,
  29. SemIR::InstId operand_inst_id,
  30. SemIR::InstId index_inst_id) -> SemIR::InstId {
  31. SemIR::InstId args[] = {context.types().GetTypeInstId(
  32. context.insts().Get(index_inst_id).type_id())};
  33. Operator op{.interface_name = CoreIdentifier::IndexWith,
  34. .interface_args_ref = args,
  35. .op_name = CoreIdentifier::At};
  36. return BuildBinaryOperator(context, node_id, op, operand_inst_id,
  37. index_inst_id);
  38. }
  39. auto HandleParseNode(Context& context, Parse::IndexExprId node_id) -> bool {
  40. auto index_inst_id = context.node_stack().PopExpr();
  41. auto operand_inst_id = context.node_stack().PopExpr();
  42. operand_inst_id = ConvertToValueOrRefExpr(context, operand_inst_id);
  43. auto operand_inst = context.insts().Get(operand_inst_id);
  44. auto operand_type_id = operand_inst.type_id();
  45. CARBON_KIND_SWITCH(context.types().GetAsInst(operand_type_id)) {
  46. case CARBON_KIND(SemIR::ArrayType array_type): {
  47. auto cast_index_id = ConvertToValueOfType(
  48. context, SemIR::LocId(index_inst_id), index_inst_id,
  49. // TODO: Replace this with impl lookup rather than hardcoding `i32`.
  50. MakeIntType(context, node_id, SemIR::IntKind::Signed,
  51. context.ints().Add(32)));
  52. auto array_cat =
  53. SemIR::GetExprCategory(context.sem_ir(), operand_inst_id);
  54. if (array_cat == SemIR::ExprCategory::Value) {
  55. // If the operand is an array value, convert it to an ephemeral
  56. // reference to an array so we can perform a primitive indexing into it.
  57. operand_inst_id = AddInst<SemIR::ValueAsRef>(
  58. context, node_id,
  59. {.type_id = operand_type_id, .value_id = operand_inst_id});
  60. }
  61. // Constant evaluation will perform a bounds check on this array indexing
  62. // if the index is constant.
  63. auto elem_id = AddInst<SemIR::ArrayIndex>(
  64. context, node_id,
  65. {.type_id = context.types().GetTypeIdForTypeInstId(
  66. array_type.element_type_inst_id),
  67. .array_id = operand_inst_id,
  68. .index_id = cast_index_id});
  69. if (array_cat != SemIR::ExprCategory::DurableRef) {
  70. // Indexing a durable reference gives a durable reference expression.
  71. // Indexing anything else gives a value expression.
  72. // TODO: This should be replaced by a choice between using `IndexWith`
  73. // and `IndirectIndexWith`.
  74. elem_id = ConvertToValueExpr(context, elem_id);
  75. }
  76. context.node_stack().Push(node_id, elem_id);
  77. return true;
  78. }
  79. default: {
  80. auto elem_id =
  81. PerformIndexWith(context, node_id, operand_inst_id, index_inst_id);
  82. context.node_stack().Push(node_id, elem_id);
  83. return true;
  84. }
  85. }
  86. }
  87. } // namespace Carbon::Check