handle_index.cpp 5.6 KB

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