handle_index.cpp 5.3 KB

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