semantics_handle_index.cpp 4.7 KB

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