handle_index.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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<SemIR::ValueAsRef>(
  51. node_id, {.type_id = operand_type_id, .value_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<SemIR::ArrayIndex>(
  56. node_id, {.type_id = array_type.element_type_id,
  57. .array_id = operand_inst_id,
  58. .index_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 CARBON_KIND(SemIR::TupleType tuple_type): {
  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(tuple_type.elements_id);
  88. if (const auto* index_val =
  89. ValidateTupleIndex(context, node_id, operand_inst,
  90. index_literal, type_block.size())) {
  91. element_type_id = type_block[index_val->getZExtValue()];
  92. } else {
  93. index_inst_id = SemIR::InstId::BuiltinError;
  94. }
  95. }
  96. context.AddInstAndPush<SemIR::TupleIndex>(node_id,
  97. {.type_id = element_type_id,
  98. .tuple_id = operand_inst_id,
  99. .index_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