handle_index.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "llvm/ADT/APSInt.h"
  5. #include "toolchain/check/context.h"
  6. #include "toolchain/check/convert.h"
  7. #include "toolchain/sem_ir/inst.h"
  8. #include "toolchain/sem_ir/inst_kind.h"
  9. namespace Carbon::Check {
  10. auto HandleIndexExprStart(Context& /*context*/, Parse::Node /*parse_node*/)
  11. -> bool {
  12. // Leave the expression on the stack for IndexExpr.
  13. return true;
  14. }
  15. // Validates that the index (required to be an IntegerLiteral) is valid within
  16. // the array or tuple size. Returns the index on success, or nullptr on failure.
  17. static auto ValidateIntegerLiteralBound(Context& context,
  18. Parse::Node parse_node,
  19. SemIR::Inst operand_inst,
  20. SemIR::IntegerLiteral index_inst,
  21. int size) -> const llvm::APInt* {
  22. const auto& index_val = context.integers().Get(index_inst.integer_id);
  23. if (index_val.uge(size)) {
  24. CARBON_DIAGNOSTIC(IndexOutOfBounds, Error,
  25. "Index `{0}` is past the end of `{1}`.", llvm::APSInt,
  26. std::string);
  27. context.emitter().Emit(
  28. parse_node, IndexOutOfBounds,
  29. llvm::APSInt(index_val, /*isUnsigned=*/true),
  30. context.sem_ir().StringifyType(operand_inst.type_id()));
  31. return nullptr;
  32. }
  33. return &index_val;
  34. }
  35. auto HandleIndexExpr(Context& context, Parse::Node parse_node) -> bool {
  36. auto index_inst_id = context.node_stack().PopExpr();
  37. auto index_inst = context.insts().Get(index_inst_id);
  38. auto operand_inst_id = context.node_stack().PopExpr();
  39. operand_inst_id = ConvertToValueOrRefExpr(context, operand_inst_id);
  40. auto operand_inst = context.insts().Get(operand_inst_id);
  41. auto operand_type_id = operand_inst.type_id();
  42. auto operand_type_inst = context.insts().Get(
  43. context.sem_ir().GetTypeAllowBuiltinTypes(operand_type_id));
  44. switch (operand_type_inst.kind()) {
  45. case SemIR::ArrayType::Kind: {
  46. auto array_type = operand_type_inst.As<SemIR::ArrayType>();
  47. // We can check whether integers are in-bounds, although it doesn't affect
  48. // the IR for an array.
  49. if (auto index_literal = index_inst.TryAs<SemIR::IntegerLiteral>();
  50. index_literal &&
  51. !ValidateIntegerLiteralBound(
  52. context, parse_node, operand_inst, *index_literal,
  53. context.sem_ir().GetArrayBoundValue(array_type.bound_id))) {
  54. index_inst_id = SemIR::InstId::BuiltinError;
  55. }
  56. auto cast_index_id = ConvertToValueOfType(
  57. context, index_inst.parse_node(), index_inst_id,
  58. context.GetBuiltinType(SemIR::BuiltinKind::IntegerType));
  59. auto array_cat =
  60. SemIR::GetExprCategory(context.sem_ir(), operand_inst_id);
  61. if (array_cat == SemIR::ExprCategory::Value) {
  62. // If the operand is an array value, convert it to an ephemeral
  63. // reference to an array so we can perform a primitive indexing into it.
  64. operand_inst_id = context.AddInst(
  65. SemIR::ValueAsRef{parse_node, operand_type_id, operand_inst_id});
  66. }
  67. auto elem_id = context.AddInst(
  68. SemIR::ArrayIndex{parse_node, array_type.element_type_id,
  69. operand_inst_id, cast_index_id});
  70. if (array_cat != SemIR::ExprCategory::DurableRef) {
  71. // Indexing a durable reference gives a durable reference expression.
  72. // Indexing anything else gives a value expression.
  73. // TODO: This should be replaced by a choice between using `IndexWith`
  74. // and `IndirectIndexWith`.
  75. elem_id = ConvertToValueExpr(context, elem_id);
  76. }
  77. context.node_stack().Push(parse_node, elem_id);
  78. return true;
  79. }
  80. case SemIR::TupleType::Kind: {
  81. SemIR::TypeId element_type_id = SemIR::TypeId::Error;
  82. if (auto index_literal = index_inst.TryAs<SemIR::IntegerLiteral>()) {
  83. auto type_block = context.type_blocks().Get(
  84. operand_type_inst.As<SemIR::TupleType>().elements_id);
  85. if (const auto* index_val = ValidateIntegerLiteralBound(
  86. context, parse_node, operand_inst, *index_literal,
  87. type_block.size())) {
  88. element_type_id = type_block[index_val->getZExtValue()];
  89. } else {
  90. index_inst_id = SemIR::InstId::BuiltinError;
  91. }
  92. } else if (index_inst.type_id() != SemIR::TypeId::Error) {
  93. CARBON_DIAGNOSTIC(TupleIndexIntegerLiteral, Error,
  94. "Tuples indices must be integer literals.");
  95. context.emitter().Emit(parse_node, TupleIndexIntegerLiteral);
  96. index_inst_id = SemIR::InstId::BuiltinError;
  97. }
  98. context.AddInstAndPush(parse_node,
  99. SemIR::TupleIndex{parse_node, element_type_id,
  100. operand_inst_id, index_inst_id});
  101. return true;
  102. }
  103. default: {
  104. if (operand_type_id != SemIR::TypeId::Error) {
  105. CARBON_DIAGNOSTIC(TypeNotIndexable, Error,
  106. "`{0}` does not support indexing.", std::string);
  107. context.emitter().Emit(parse_node, TypeNotIndexable,
  108. context.sem_ir().StringifyType(operand_type_id));
  109. }
  110. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinError);
  111. return true;
  112. }
  113. }
  114. }
  115. } // namespace Carbon::Check