handle_index.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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*/,
  10. Parse::IndexExprStartId /*parse_node*/) -> 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::IndexExprId 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.types().GetAsInst(operand_type_id);
  41. switch (operand_type_inst.kind()) {
  42. case SemIR::ArrayType::Kind: {
  43. auto array_type = operand_type_inst.As<SemIR::ArrayType>();
  44. // We can check whether integers are in-bounds, although it doesn't affect
  45. // the IR for an array.
  46. if (auto index_literal = index_inst.TryAs<SemIR::IntLiteral>();
  47. index_literal &&
  48. !ValidateIntLiteralBound(
  49. context, parse_node, operand_inst, *index_literal,
  50. context.sem_ir().GetArrayBoundValue(array_type.bound_id))) {
  51. index_inst_id = SemIR::InstId::BuiltinError;
  52. }
  53. auto cast_index_id = ConvertToValueOfType(
  54. context, index_inst.parse_node(), index_inst_id,
  55. context.GetBuiltinType(SemIR::BuiltinKind::IntType));
  56. auto array_cat =
  57. SemIR::GetExprCategory(context.sem_ir(), operand_inst_id);
  58. if (array_cat == SemIR::ExprCategory::Value) {
  59. // If the operand is an array value, convert it to an ephemeral
  60. // reference to an array so we can perform a primitive indexing into it.
  61. operand_inst_id = context.AddInst(
  62. SemIR::ValueAsRef{parse_node, operand_type_id, operand_inst_id});
  63. }
  64. auto elem_id = context.AddInst(
  65. SemIR::ArrayIndex{parse_node, array_type.element_type_id,
  66. operand_inst_id, cast_index_id});
  67. if (array_cat != SemIR::ExprCategory::DurableRef) {
  68. // Indexing a durable reference gives a durable reference expression.
  69. // Indexing anything else gives a value expression.
  70. // TODO: This should be replaced by a choice between using `IndexWith`
  71. // and `IndirectIndexWith`.
  72. elem_id = ConvertToValueExpr(context, elem_id);
  73. }
  74. context.node_stack().Push(parse_node, elem_id);
  75. return true;
  76. }
  77. case SemIR::TupleType::Kind: {
  78. SemIR::TypeId element_type_id = SemIR::TypeId::Error;
  79. if (auto index_literal = index_inst.TryAs<SemIR::IntLiteral>()) {
  80. auto type_block = context.type_blocks().Get(
  81. operand_type_inst.As<SemIR::TupleType>().elements_id);
  82. if (const auto* index_val =
  83. ValidateIntLiteralBound(context, parse_node, operand_inst,
  84. *index_literal, type_block.size())) {
  85. element_type_id = type_block[index_val->getZExtValue()];
  86. } else {
  87. index_inst_id = SemIR::InstId::BuiltinError;
  88. }
  89. } else if (index_inst.type_id() != SemIR::TypeId::Error) {
  90. CARBON_DIAGNOSTIC(TupleIndexIntLiteral, Error,
  91. "Tuples indices must be integer literals.");
  92. context.emitter().Emit(parse_node, TupleIndexIntLiteral);
  93. index_inst_id = SemIR::InstId::BuiltinError;
  94. }
  95. context.AddInstAndPush(parse_node,
  96. SemIR::TupleIndex{parse_node, element_type_id,
  97. operand_inst_id, index_inst_id});
  98. return true;
  99. }
  100. default: {
  101. if (operand_type_id != SemIR::TypeId::Error) {
  102. CARBON_DIAGNOSTIC(TypeNotIndexable, Error,
  103. "Type `{0}` does not support indexing.", std::string);
  104. context.emitter().Emit(parse_node, TypeNotIndexable,
  105. context.sem_ir().StringifyType(operand_type_id));
  106. }
  107. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinError);
  108. return true;
  109. }
  110. }
  111. }
  112. } // namespace Carbon::Check