handle_index.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/node.h"
  8. #include "toolchain/sem_ir/node_kind.h"
  9. namespace Carbon::Check {
  10. auto HandleIndexExpressionStart(Context& /*context*/,
  11. Parse::Node /*parse_node*/) -> bool {
  12. // Leave the expression on the stack for IndexExpression.
  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::Node operand_node,
  20. SemIR::IntegerLiteral index_node,
  21. int size) -> const llvm::APInt* {
  22. const auto& index_val = context.integers().Get(index_node.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_node.type_id()));
  31. return nullptr;
  32. }
  33. return &index_val;
  34. }
  35. auto HandleIndexExpression(Context& context, Parse::Node parse_node) -> bool {
  36. auto index_node_id = context.node_stack().PopExpression();
  37. auto index_node = context.nodes().Get(index_node_id);
  38. auto operand_node_id = context.node_stack().PopExpression();
  39. operand_node_id =
  40. ConvertToValueOrReferenceExpression(context, operand_node_id);
  41. auto operand_node = context.nodes().Get(operand_node_id);
  42. auto operand_type_id = operand_node.type_id();
  43. auto operand_type_node = context.nodes().Get(
  44. context.sem_ir().GetTypeAllowBuiltinTypes(operand_type_id));
  45. switch (operand_type_node.kind()) {
  46. case SemIR::ArrayType::Kind: {
  47. auto array_type = operand_type_node.As<SemIR::ArrayType>();
  48. // We can check whether integers are in-bounds, although it doesn't affect
  49. // the IR for an array.
  50. if (auto index_literal = index_node.TryAs<SemIR::IntegerLiteral>();
  51. index_literal &&
  52. !ValidateIntegerLiteralBound(
  53. context, parse_node, operand_node, *index_literal,
  54. context.sem_ir().GetArrayBoundValue(array_type.bound_id))) {
  55. index_node_id = SemIR::NodeId::BuiltinError;
  56. }
  57. auto cast_index_id = ConvertToValueOfType(
  58. context, index_node.parse_node(), index_node_id,
  59. context.GetBuiltinType(SemIR::BuiltinKind::IntegerType));
  60. auto array_cat =
  61. SemIR::GetExpressionCategory(context.sem_ir(), operand_node_id);
  62. if (array_cat == SemIR::ExpressionCategory::Value) {
  63. // If the operand is an array value, convert it to an ephemeral
  64. // reference to an array so we can perform a primitive indexing into it.
  65. operand_node_id = context.AddNode(SemIR::ValueAsReference{
  66. parse_node, operand_type_id, operand_node_id});
  67. }
  68. auto elem_id = context.AddNode(
  69. SemIR::ArrayIndex{parse_node, array_type.element_type_id,
  70. operand_node_id, cast_index_id});
  71. if (array_cat != SemIR::ExpressionCategory::DurableReference) {
  72. // Indexing a durable reference gives a durable reference expression.
  73. // Indexing anything else gives a value expression.
  74. // TODO: This should be replaced by a choice between using `IndexWith`
  75. // and `IndirectIndexWith`.
  76. elem_id = ConvertToValueExpression(context, elem_id);
  77. }
  78. context.node_stack().Push(parse_node, elem_id);
  79. return true;
  80. }
  81. case SemIR::TupleType::Kind: {
  82. SemIR::TypeId element_type_id = SemIR::TypeId::Error;
  83. if (auto index_literal = index_node.TryAs<SemIR::IntegerLiteral>()) {
  84. auto type_block = context.type_blocks().Get(
  85. operand_type_node.As<SemIR::TupleType>().elements_id);
  86. if (const auto* index_val = ValidateIntegerLiteralBound(
  87. context, parse_node, operand_node, *index_literal,
  88. type_block.size())) {
  89. element_type_id = type_block[index_val->getZExtValue()];
  90. } else {
  91. index_node_id = SemIR::NodeId::BuiltinError;
  92. }
  93. } else if (index_node.type_id() != SemIR::TypeId::Error) {
  94. CARBON_DIAGNOSTIC(TupleIndexIntegerLiteral, Error,
  95. "Tuples indices must be integer literals.");
  96. context.emitter().Emit(parse_node, TupleIndexIntegerLiteral);
  97. index_node_id = SemIR::NodeId::BuiltinError;
  98. }
  99. context.AddNodeAndPush(parse_node,
  100. SemIR::TupleIndex{parse_node, element_type_id,
  101. operand_node_id, index_node_id});
  102. return true;
  103. }
  104. default: {
  105. if (operand_type_id != SemIR::TypeId::Error) {
  106. CARBON_DIAGNOSTIC(TypeNotIndexable, Error,
  107. "`{0}` does not support indexing.", std::string);
  108. context.emitter().Emit(parse_node, TypeNotIndexable,
  109. context.sem_ir().StringifyType(operand_type_id));
  110. }
  111. context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinError);
  112. return true;
  113. }
  114. }
  115. }
  116. } // namespace Carbon::Check