handle_index.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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/inst.h"
  8. namespace Carbon::Check {
  9. auto HandleIndexExprStart(Context& /*context*/, Parse::NodeId /*parse_node*/)
  10. -> bool {
  11. // Leave the expression on the stack for IndexExpr.
  12. return true;
  13. }
  14. // Validates that the index (required to be an IntLiteral) is valid within
  15. // the array or tuple size. Returns the index on success, or nullptr on failure.
  16. static auto ValidateIntLiteralBound(Context& context, Parse::NodeId parse_node,
  17. SemIR::Inst operand_inst,
  18. SemIR::IntLiteral index_inst, int size)
  19. -> const llvm::APInt* {
  20. const auto& index_val = context.ints().Get(index_inst.int_id);
  21. if (index_val.uge(size)) {
  22. CARBON_DIAGNOSTIC(IndexOutOfBounds, Error,
  23. "Index `{0}` is past the end of type `{1}`.",
  24. llvm::APSInt, std::string);
  25. context.emitter().Emit(
  26. parse_node, IndexOutOfBounds,
  27. llvm::APSInt(index_val, /*isUnsigned=*/true),
  28. context.sem_ir().StringifyType(operand_inst.type_id()));
  29. return nullptr;
  30. }
  31. return &index_val;
  32. }
  33. auto HandleIndexExpr(Context& context, Parse::NodeId parse_node) -> bool {
  34. auto index_inst_id = context.node_stack().PopExpr();
  35. auto index_inst = context.insts().Get(index_inst_id);
  36. auto operand_inst_id = context.node_stack().PopExpr();
  37. operand_inst_id = ConvertToValueOrRefExpr(context, operand_inst_id);
  38. auto operand_inst = context.insts().Get(operand_inst_id);
  39. auto operand_type_id = operand_inst.type_id();
  40. auto operand_type_inst = context.insts().Get(
  41. context.sem_ir().GetTypeAllowBuiltinTypes(operand_type_id));
  42. switch (operand_type_inst.kind()) {
  43. case SemIR::ArrayType::Kind: {
  44. auto array_type = operand_type_inst.As<SemIR::ArrayType>();
  45. // We can check whether integers are in-bounds, although it doesn't affect
  46. // the IR for an array.
  47. if (auto index_literal = index_inst.TryAs<SemIR::IntLiteral>();
  48. index_literal &&
  49. !ValidateIntLiteralBound(
  50. context, parse_node, operand_inst, *index_literal,
  51. context.sem_ir().GetArrayBoundValue(array_type.bound_id))) {
  52. index_inst_id = SemIR::InstId::BuiltinError;
  53. }
  54. auto cast_index_id = ConvertToValueOfType(
  55. context, index_inst.parse_node(), index_inst_id,
  56. context.GetBuiltinType(SemIR::BuiltinKind::IntType));
  57. auto array_cat =
  58. SemIR::GetExprCategory(context.sem_ir(), operand_inst_id);
  59. if (array_cat == SemIR::ExprCategory::Value) {
  60. // If the operand is an array value, convert it to an ephemeral
  61. // reference to an array so we can perform a primitive indexing into it.
  62. operand_inst_id = context.AddInst(
  63. SemIR::ValueAsRef{parse_node, operand_type_id, operand_inst_id});
  64. }
  65. auto elem_id = context.AddInst(
  66. SemIR::ArrayIndex{parse_node, array_type.element_type_id,
  67. operand_inst_id, cast_index_id});
  68. if (array_cat != SemIR::ExprCategory::DurableRef) {
  69. // Indexing a durable reference gives a durable reference expression.
  70. // Indexing anything else gives a value expression.
  71. // TODO: This should be replaced by a choice between using `IndexWith`
  72. // and `IndirectIndexWith`.
  73. elem_id = ConvertToValueExpr(context, elem_id);
  74. }
  75. context.node_stack().Push(parse_node, elem_id);
  76. return true;
  77. }
  78. case SemIR::TupleType::Kind: {
  79. SemIR::TypeId element_type_id = SemIR::TypeId::Error;
  80. if (auto index_literal = index_inst.TryAs<SemIR::IntLiteral>()) {
  81. auto type_block = context.type_blocks().Get(
  82. operand_type_inst.As<SemIR::TupleType>().elements_id);
  83. if (const auto* index_val =
  84. ValidateIntLiteralBound(context, parse_node, operand_inst,
  85. *index_literal, type_block.size())) {
  86. element_type_id = type_block[index_val->getZExtValue()];
  87. } else {
  88. index_inst_id = SemIR::InstId::BuiltinError;
  89. }
  90. } else if (index_inst.type_id() != SemIR::TypeId::Error) {
  91. CARBON_DIAGNOSTIC(TupleIndexIntLiteral, Error,
  92. "Tuples indices must be integer literals.");
  93. context.emitter().Emit(parse_node, TupleIndexIntLiteral);
  94. index_inst_id = SemIR::InstId::BuiltinError;
  95. }
  96. context.AddInstAndPush(parse_node,
  97. SemIR::TupleIndex{parse_node, element_type_id,
  98. operand_inst_id, index_inst_id});
  99. return true;
  100. }
  101. default: {
  102. if (operand_type_id != SemIR::TypeId::Error) {
  103. CARBON_DIAGNOSTIC(TypeNotIndexable, Error,
  104. "Type `{0}` does not support indexing.", std::string);
  105. context.emitter().Emit(parse_node, TypeNotIndexable,
  106. context.sem_ir().StringifyType(operand_type_id));
  107. }
  108. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinError);
  109. return true;
  110. }
  111. }
  112. }
  113. } // namespace Carbon::Check