semantics_handle_index.cpp 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. auto HandleIndexExpression(Context& context, ParseTree::Node parse_node)
  15. -> bool {
  16. CARBON_DIAGNOSTIC(OutOfBoundsAccess, Error,
  17. "Index `{0}` is past the end of `{1}`.", llvm::APSInt,
  18. std::string);
  19. auto index_node_id = context.node_stack().PopExpression();
  20. auto index_node = context.semantics_ir().GetNode(index_node_id);
  21. auto name_node_id = context.node_stack().PopExpression();
  22. auto name_node = context.semantics_ir().GetNode(name_node_id);
  23. auto name_type_id =
  24. context.semantics_ir().GetTypeAllowBuiltinTypes(name_node.type_id());
  25. auto name_type_node = context.semantics_ir().GetNode(name_type_id);
  26. if (name_type_node.kind() == SemIR::NodeKind::ArrayType) {
  27. auto [bound_id, type_id] = name_type_node.GetAsArrayType();
  28. if (index_node.kind() == SemIR::NodeKind::IntegerLiteral) {
  29. const auto& index_val = context.semantics_ir().GetIntegerLiteral(
  30. index_node.GetAsIntegerLiteral());
  31. if (index_val.uge(context.semantics_ir().GetArrayBoundValue(bound_id))) {
  32. context.emitter().Emit(
  33. parse_node, OutOfBoundsAccess,
  34. llvm::APSInt(index_val, /*isUnsigned=*/true),
  35. context.semantics_ir().StringifyType(name_node.type_id()));
  36. } else {
  37. context.AddNodeAndPush(
  38. parse_node, SemIR::Node::ArrayIndex::Make(
  39. parse_node, type_id, name_node_id, index_node_id));
  40. return true;
  41. }
  42. } else if (context.ImplicitAsRequired(
  43. index_node.parse_node(), index_node_id,
  44. context.CanonicalizeType(
  45. SemIR::NodeId::BuiltinIntegerType)) !=
  46. SemIR::NodeId::BuiltinError) {
  47. context.AddNodeAndPush(
  48. parse_node, SemIR::Node::ArrayIndex::Make(
  49. parse_node, type_id, name_node_id, index_node_id));
  50. return true;
  51. }
  52. } else if (name_type_node.kind() == SemIR::NodeKind::TupleType) {
  53. if (index_node.kind() == SemIR::NodeKind::IntegerLiteral) {
  54. const auto& index_val = context.semantics_ir().GetIntegerLiteral(
  55. index_node.GetAsIntegerLiteral());
  56. auto type_block =
  57. context.semantics_ir().GetTypeBlock(name_type_node.GetAsTupleType());
  58. if (index_val.uge(static_cast<uint64_t>(type_block.size()))) {
  59. context.emitter().Emit(
  60. parse_node, OutOfBoundsAccess,
  61. llvm::APSInt(index_val, /*isUnsigned=*/true),
  62. context.semantics_ir().StringifyType(name_node.type_id()));
  63. } else {
  64. context.AddNodeAndPush(
  65. parse_node, SemIR::Node::TupleIndex::Make(
  66. parse_node, type_block[index_val.getZExtValue()],
  67. name_node_id, index_node_id));
  68. return true;
  69. }
  70. } else {
  71. CARBON_DIAGNOSTIC(NondeterministicType, Error,
  72. "Type cannot be determined at compile time.");
  73. context.emitter().Emit(parse_node, NondeterministicType);
  74. }
  75. } else if (name_type_id != SemIR::NodeId::BuiltinError) {
  76. CARBON_DIAGNOSTIC(InvalidIndexExpression, Error,
  77. "Invalid index expression.");
  78. context.emitter().Emit(parse_node, InvalidIndexExpression);
  79. }
  80. context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinError);
  81. return true;
  82. }
  83. } // namespace Carbon::Check