handle_index.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/sem_ir/inst.h"
  8. namespace Carbon::Check {
  9. auto HandleIndexExprStart(Context& /*context*/,
  10. Parse::IndexExprStartId /*node_id*/) -> bool {
  11. // Leave the expression on the stack for IndexExpr.
  12. return true;
  13. }
  14. // Validates that the index (required to be an IntLiteral) is valid within the
  15. // tuple size. Returns the index on success, or nullptr on failure.
  16. static auto ValidateTupleIndex(Context& context, Parse::NodeId node_id,
  17. SemIR::Inst operand_inst,
  18. SemIR::IntLiteral index_inst, int size)
  19. -> const llvm::APInt* {
  20. const auto& index_val = context.ints().Get(index_inst.int_id);
  21. if (index_val.uge(size)) {
  22. CARBON_DIAGNOSTIC(
  23. TupleIndexOutOfBounds, Error,
  24. "Tuple element index `{0}` is past the end of type `{1}`.", TypedInt,
  25. SemIR::TypeId);
  26. context.emitter().Emit(node_id, TupleIndexOutOfBounds,
  27. TypedInt{index_inst.type_id, index_val},
  28. operand_inst.type_id());
  29. return nullptr;
  30. }
  31. return &index_val;
  32. }
  33. auto HandleIndexExpr(Context& context, Parse::IndexExprId node_id) -> bool {
  34. auto index_inst_id = context.node_stack().PopExpr();
  35. auto operand_inst_id = context.node_stack().PopExpr();
  36. operand_inst_id = ConvertToValueOrRefExpr(context, operand_inst_id);
  37. auto operand_inst = context.insts().Get(operand_inst_id);
  38. auto operand_type_id = operand_inst.type_id();
  39. CARBON_KIND_SWITCH(context.types().GetAsInst(operand_type_id)) {
  40. case CARBON_KIND(SemIR::ArrayType array_type): {
  41. auto index_node_id = context.insts().GetLocId(index_inst_id);
  42. auto cast_index_id = ConvertToValueOfType(
  43. context, index_node_id, index_inst_id,
  44. context.GetBuiltinType(SemIR::BuiltinKind::IntType));
  45. auto array_cat =
  46. SemIR::GetExprCategory(context.sem_ir(), operand_inst_id);
  47. if (array_cat == SemIR::ExprCategory::Value) {
  48. // If the operand is an array value, convert it to an ephemeral
  49. // reference to an array so we can perform a primitive indexing into it.
  50. operand_inst_id = context.AddInst(
  51. {node_id, SemIR::ValueAsRef{operand_type_id, operand_inst_id}});
  52. }
  53. // Constant evaluation will perform a bounds check on this array indexing
  54. // if the index is constant.
  55. auto elem_id = context.AddInst(
  56. {node_id, SemIR::ArrayIndex{array_type.element_type_id,
  57. operand_inst_id, cast_index_id}});
  58. if (array_cat != SemIR::ExprCategory::DurableRef) {
  59. // Indexing a durable reference gives a durable reference expression.
  60. // Indexing anything else gives a value expression.
  61. // TODO: This should be replaced by a choice between using `IndexWith`
  62. // and `IndirectIndexWith`.
  63. elem_id = ConvertToValueExpr(context, elem_id);
  64. }
  65. context.node_stack().Push(node_id, elem_id);
  66. return true;
  67. }
  68. case CARBON_KIND(SemIR::TupleType tuple_type): {
  69. SemIR::TypeId element_type_id = SemIR::TypeId::Error;
  70. auto index_node_id = context.insts().GetLocId(index_inst_id);
  71. index_inst_id = ConvertToValueOfType(
  72. context, index_node_id, index_inst_id,
  73. context.GetBuiltinType(SemIR::BuiltinKind::IntType));
  74. auto index_const_id = context.constant_values().Get(index_inst_id);
  75. if (index_const_id == SemIR::ConstantId::Error) {
  76. index_inst_id = SemIR::InstId::BuiltinError;
  77. } else if (!index_const_id.is_template()) {
  78. // TODO: Decide what to do if the index is a symbolic constant.
  79. CARBON_DIAGNOSTIC(TupleIndexNotConstant, Error,
  80. "Tuple index must be a constant.");
  81. context.emitter().Emit(node_id, TupleIndexNotConstant);
  82. index_inst_id = SemIR::InstId::BuiltinError;
  83. } else {
  84. auto index_literal =
  85. context.insts().GetAs<SemIR::IntLiteral>(index_const_id.inst_id());
  86. auto type_block = context.type_blocks().Get(tuple_type.elements_id);
  87. if (const auto* index_val =
  88. ValidateTupleIndex(context, node_id, operand_inst,
  89. index_literal, type_block.size())) {
  90. element_type_id = type_block[index_val->getZExtValue()];
  91. } else {
  92. index_inst_id = SemIR::InstId::BuiltinError;
  93. }
  94. }
  95. context.AddInstAndPush(
  96. {node_id,
  97. SemIR::TupleIndex{element_type_id, operand_inst_id, index_inst_id}});
  98. return true;
  99. }
  100. default: {
  101. if (operand_type_id != SemIR::TypeId::Error) {
  102. CARBON_DIAGNOSTIC(TypeNotIndexable, Error,
  103. "Type `{0}` does not support indexing.",
  104. SemIR::TypeId);
  105. context.emitter().Emit(node_id, TypeNotIndexable, operand_type_id);
  106. }
  107. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  108. return true;
  109. }
  110. }
  111. }
  112. } // namespace Carbon::Check