handle_index.cpp 5.5 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/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 HandleIndexExprStart(Context& /*context*/,
  11. Parse::IndexExprStartId /*node_id*/) -> bool {
  12. // Leave the expression on the stack for IndexExpr.
  13. return true;
  14. }
  15. // Validates that the index (required to be an IntLiteral) is valid within the
  16. // tuple size. Returns the index on success, or nullptr on failure.
  17. static auto ValidateTupleIndex(Context& context, Parse::NodeId node_id,
  18. SemIR::Inst operand_inst,
  19. SemIR::IntLiteral index_inst, int size)
  20. -> const llvm::APInt* {
  21. const auto& index_val = context.ints().Get(index_inst.int_id);
  22. if (index_val.uge(size)) {
  23. CARBON_DIAGNOSTIC(
  24. TupleIndexOutOfBounds, Error,
  25. "Tuple element index `{0}` is past the end of type `{1}`.", TypedInt,
  26. SemIR::TypeId);
  27. context.emitter().Emit(node_id, TupleIndexOutOfBounds,
  28. {.type = index_inst.type_id, .value = index_val},
  29. operand_inst.type_id());
  30. return nullptr;
  31. }
  32. return &index_val;
  33. }
  34. auto HandleIndexExpr(Context& context, Parse::IndexExprId node_id) -> bool {
  35. auto index_inst_id = context.node_stack().PopExpr();
  36. auto operand_inst_id = context.node_stack().PopExpr();
  37. operand_inst_id = ConvertToValueOrRefExpr(context, operand_inst_id);
  38. auto operand_inst = context.insts().Get(operand_inst_id);
  39. auto operand_type_id = operand_inst.type_id();
  40. CARBON_KIND_SWITCH(context.types().GetAsInst(operand_type_id)) {
  41. case CARBON_KIND(SemIR::ArrayType array_type): {
  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<SemIR::ValueAsRef>(
  52. node_id, {.type_id = operand_type_id, .value_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<SemIR::ArrayIndex>(
  57. node_id, {.type_id = array_type.element_type_id,
  58. .array_id = operand_inst_id,
  59. .index_id = cast_index_id});
  60. if (array_cat != SemIR::ExprCategory::DurableRef) {
  61. // Indexing a durable reference gives a durable reference expression.
  62. // Indexing anything else gives a value expression.
  63. // TODO: This should be replaced by a choice between using `IndexWith`
  64. // and `IndirectIndexWith`.
  65. elem_id = ConvertToValueExpr(context, elem_id);
  66. }
  67. context.node_stack().Push(node_id, elem_id);
  68. return true;
  69. }
  70. case CARBON_KIND(SemIR::TupleType tuple_type): {
  71. SemIR::TypeId element_type_id = SemIR::TypeId::Error;
  72. auto index_node_id = context.insts().GetLocId(index_inst_id);
  73. index_inst_id = ConvertToValueOfType(
  74. context, index_node_id, index_inst_id,
  75. context.GetBuiltinType(SemIR::BuiltinKind::IntType));
  76. auto index_const_id = context.constant_values().Get(index_inst_id);
  77. if (index_const_id == SemIR::ConstantId::Error) {
  78. index_inst_id = SemIR::InstId::BuiltinError;
  79. } else if (!index_const_id.is_template()) {
  80. // TODO: Decide what to do if the index is a symbolic constant.
  81. CARBON_DIAGNOSTIC(TupleIndexNotConstant, Error,
  82. "Tuple index must be a constant.");
  83. context.emitter().Emit(node_id, TupleIndexNotConstant);
  84. index_inst_id = SemIR::InstId::BuiltinError;
  85. } else {
  86. auto index_literal = context.insts().GetAs<SemIR::IntLiteral>(
  87. context.constant_values().GetInstId(index_const_id));
  88. auto type_block = context.type_blocks().Get(tuple_type.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<SemIR::TupleIndex>(node_id,
  98. {.type_id = element_type_id,
  99. .tuple_id = operand_inst_id,
  100. .index_id = index_inst_id});
  101. return true;
  102. }
  103. default: {
  104. if (operand_type_id != SemIR::TypeId::Error) {
  105. CARBON_DIAGNOSTIC(TypeNotIndexable, Error,
  106. "Type `{0}` does not support indexing.",
  107. SemIR::TypeId);
  108. context.emitter().Emit(node_id, TypeNotIndexable, operand_type_id);
  109. }
  110. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  111. return true;
  112. }
  113. }
  114. }
  115. } // namespace Carbon::Check