handle_index.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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::Node index_node, int size)
  21. -> const llvm::APInt* {
  22. const auto& index_val = context.semantics_ir().GetIntegerLiteral(
  23. index_node.GetAsIntegerLiteral());
  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::NodeKind::ArrayType: {
  48. auto [bound_id, element_type_id] = operand_type_node.GetAsArrayType();
  49. // We can check whether integers are in-bounds, although it doesn't affect
  50. // the IR for an array.
  51. if (index_node.kind() == SemIR::NodeKind::IntegerLiteral &&
  52. !ValidateIntegerLiteralBound(
  53. context, parse_node, operand_node, index_node,
  54. context.semantics_ir().GetArrayBoundValue(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.CanonicalizeType(SemIR::NodeId::BuiltinIntegerType));
  60. context.AddNodeAndPush(parse_node, SemIR::Node::ArrayIndex::Make(
  61. parse_node, element_type_id,
  62. operand_node_id, cast_index_id));
  63. return true;
  64. }
  65. case SemIR::NodeKind::TupleType: {
  66. SemIR::TypeId element_type_id = SemIR::TypeId::Error;
  67. if (index_node.kind() == SemIR::NodeKind::IntegerLiteral) {
  68. auto type_block = context.semantics_ir().GetTypeBlock(
  69. operand_type_node.GetAsTupleType());
  70. if (const auto* index_val =
  71. ValidateIntegerLiteralBound(context, parse_node, operand_node,
  72. index_node, type_block.size())) {
  73. element_type_id = type_block[index_val->getZExtValue()];
  74. } else {
  75. index_node_id = SemIR::NodeId::BuiltinError;
  76. }
  77. } else {
  78. CARBON_DIAGNOSTIC(TupleIndexIntegerLiteral, Error,
  79. "Tuples indices must be integer literals.");
  80. context.emitter().Emit(parse_node, TupleIndexIntegerLiteral);
  81. index_node_id = SemIR::NodeId::BuiltinError;
  82. }
  83. context.AddNodeAndPush(parse_node, SemIR::Node::TupleIndex::Make(
  84. parse_node, element_type_id,
  85. operand_node_id, index_node_id));
  86. return true;
  87. }
  88. default: {
  89. if (operand_type_id != SemIR::TypeId::Error) {
  90. CARBON_DIAGNOSTIC(TypeNotIndexable, Error,
  91. "`{0}` does not support indexing.", std::string);
  92. context.emitter().Emit(
  93. parse_node, TypeNotIndexable,
  94. context.semantics_ir().StringifyType(operand_type_id));
  95. }
  96. context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinError);
  97. return true;
  98. }
  99. }
  100. }
  101. } // namespace Carbon::Check