handle_index.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 "toolchain/base/kind_switch.h"
  5. #include "toolchain/check/context.h"
  6. #include "toolchain/check/convert.h"
  7. #include "toolchain/check/handle.h"
  8. #include "toolchain/sem_ir/inst.h"
  9. namespace Carbon::Check {
  10. auto HandleParseNode(Context& /*context*/, Parse::IndexExprStartId /*node_id*/)
  11. -> bool {
  12. // Leave the expression on the stack for IndexExpr.
  13. return true;
  14. }
  15. auto HandleParseNode(Context& context, Parse::IndexExprId node_id) -> bool {
  16. auto index_inst_id = context.node_stack().PopExpr();
  17. auto operand_inst_id = context.node_stack().PopExpr();
  18. operand_inst_id = ConvertToValueOrRefExpr(context, operand_inst_id);
  19. auto operand_inst = context.insts().Get(operand_inst_id);
  20. auto operand_type_id = operand_inst.type_id();
  21. CARBON_KIND_SWITCH(context.types().GetAsInst(operand_type_id)) {
  22. case CARBON_KIND(SemIR::ArrayType array_type): {
  23. auto index_node_id = context.insts().GetLocId(index_inst_id);
  24. auto cast_index_id = ConvertToValueOfType(
  25. context, index_node_id, index_inst_id,
  26. context.GetBuiltinType(SemIR::BuiltinInstKind::IntType));
  27. auto array_cat =
  28. SemIR::GetExprCategory(context.sem_ir(), operand_inst_id);
  29. if (array_cat == SemIR::ExprCategory::Value) {
  30. // If the operand is an array value, convert it to an ephemeral
  31. // reference to an array so we can perform a primitive indexing into it.
  32. operand_inst_id = context.AddInst<SemIR::ValueAsRef>(
  33. node_id, {.type_id = operand_type_id, .value_id = operand_inst_id});
  34. }
  35. // Constant evaluation will perform a bounds check on this array indexing
  36. // if the index is constant.
  37. auto elem_id = context.AddInst<SemIR::ArrayIndex>(
  38. node_id, {.type_id = array_type.element_type_id,
  39. .array_id = operand_inst_id,
  40. .index_id = cast_index_id});
  41. if (array_cat != SemIR::ExprCategory::DurableRef) {
  42. // Indexing a durable reference gives a durable reference expression.
  43. // Indexing anything else gives a value expression.
  44. // TODO: This should be replaced by a choice between using `IndexWith`
  45. // and `IndirectIndexWith`.
  46. elem_id = ConvertToValueExpr(context, elem_id);
  47. }
  48. context.node_stack().Push(node_id, elem_id);
  49. return true;
  50. }
  51. default: {
  52. if (operand_type_id != SemIR::TypeId::Error) {
  53. CARBON_DIAGNOSTIC(TypeNotIndexable, Error,
  54. "type {0} does not support indexing", TypeOfInstId);
  55. context.emitter().Emit(node_id, TypeNotIndexable, operand_inst_id);
  56. }
  57. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  58. return true;
  59. }
  60. }
  61. }
  62. } // namespace Carbon::Check