semantics_handle_index.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/semantics/semantics_context.h"
  6. #include "toolchain/semantics/semantics_node.h"
  7. #include "toolchain/semantics/semantics_node_kind.h"
  8. namespace Carbon::Check {
  9. auto HandleIndexExpressionStart(Context& /*context*/,
  10. Parse::Node /*parse_node*/) -> bool {
  11. // Leave the expression on the stack for IndexExpression.
  12. return true;
  13. }
  14. // Validates that the index (required to be an IntegerLiteral) is valid within
  15. // the array or tuple size. Returns the index on success, or nullptr on failure.
  16. static auto ValidateIntegerLiteralBound(Context& context,
  17. Parse::Node parse_node,
  18. SemIR::Node operand_node,
  19. SemIR::Node index_node, int size)
  20. -> const llvm::APInt* {
  21. const auto& index_val = context.semantics_ir().GetIntegerLiteral(
  22. index_node.GetAsIntegerLiteral());
  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.semantics_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.semantics_ir().GetNode(index_node_id);
  38. auto operand_node_id = context.node_stack().PopExpression();
  39. operand_node_id = context.MaterializeIfInitializing(operand_node_id);
  40. auto operand_node = context.semantics_ir().GetNode(operand_node_id);
  41. auto operand_type_id = operand_node.type_id();
  42. auto operand_type_node = context.semantics_ir().GetNode(
  43. context.semantics_ir().GetTypeAllowBuiltinTypes(operand_type_id));
  44. switch (operand_type_node.kind()) {
  45. case SemIR::NodeKind::ArrayType: {
  46. auto [bound_id, element_type_id] = operand_type_node.GetAsArrayType();
  47. // We can check whether integers are in-bounds, although it doesn't affect
  48. // the IR for an array.
  49. if (index_node.kind() == SemIR::NodeKind::IntegerLiteral &&
  50. !ValidateIntegerLiteralBound(
  51. context, parse_node, operand_node, index_node,
  52. context.semantics_ir().GetArrayBoundValue(bound_id))) {
  53. index_node_id = SemIR::NodeId::BuiltinError;
  54. }
  55. auto cast_index_id = context.ImplicitAsRequired(
  56. index_node.parse_node(), index_node_id,
  57. context.CanonicalizeType(SemIR::NodeId::BuiltinIntegerType));
  58. context.AddNodeAndPush(parse_node, SemIR::Node::ArrayIndex::Make(
  59. parse_node, element_type_id,
  60. operand_node_id, cast_index_id));
  61. return true;
  62. }
  63. case SemIR::NodeKind::TupleType: {
  64. SemIR::TypeId element_type_id = SemIR::TypeId::Error;
  65. if (index_node.kind() == SemIR::NodeKind::IntegerLiteral) {
  66. auto type_block = context.semantics_ir().GetTypeBlock(
  67. operand_type_node.GetAsTupleType());
  68. if (const auto* index_val =
  69. ValidateIntegerLiteralBound(context, parse_node, operand_node,
  70. index_node, type_block.size())) {
  71. element_type_id = type_block[index_val->getZExtValue()];
  72. } else {
  73. index_node_id = SemIR::NodeId::BuiltinError;
  74. }
  75. } else {
  76. CARBON_DIAGNOSTIC(TupleIndexIntegerLiteral, Error,
  77. "Tuples indices must be integer literals.");
  78. context.emitter().Emit(parse_node, TupleIndexIntegerLiteral);
  79. index_node_id = SemIR::NodeId::BuiltinError;
  80. }
  81. context.AddNodeAndPush(parse_node, SemIR::Node::TupleIndex::Make(
  82. parse_node, element_type_id,
  83. operand_node_id, index_node_id));
  84. return true;
  85. }
  86. default: {
  87. if (operand_type_id != SemIR::TypeId::Error) {
  88. CARBON_DIAGNOSTIC(TypeNotIndexable, Error,
  89. "`{0}` does not support indexing.", std::string);
  90. context.emitter().Emit(
  91. parse_node, TypeNotIndexable,
  92. context.semantics_ir().StringifyType(operand_type_id));
  93. }
  94. context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinError);
  95. return true;
  96. }
  97. }
  98. }
  99. } // namespace Carbon::Check