handle_index.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 the
  15. // tuple size. Returns the index on success, or nullptr on failure.
  16. static auto ValidateTupleIndex(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(
  23. TupleIndexOutOfBounds, Error,
  24. "Tuple element index `{0}` is past the end of type `{1}`.",
  25. llvm::APSInt, SemIR::TypeId);
  26. context.emitter().Emit(parse_node, TupleIndexOutOfBounds,
  27. llvm::APSInt(index_val, /*isUnsigned=*/true),
  28. 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 operand_inst_id = context.node_stack().PopExpr();
  36. operand_inst_id = ConvertToValueOrRefExpr(context, operand_inst_id);
  37. auto operand_inst = context.insts().Get(operand_inst_id);
  38. auto operand_type_id = operand_inst.type_id();
  39. auto operand_type_inst = context.types().GetAsInst(operand_type_id);
  40. switch (operand_type_inst.kind()) {
  41. case SemIR::ArrayType::Kind: {
  42. auto array_type = operand_type_inst.As<SemIR::ArrayType>();
  43. auto index_parse_node = context.insts().GetParseNode(index_inst_id);
  44. auto cast_index_id = ConvertToValueOfType(
  45. context, index_parse_node, index_inst_id,
  46. context.GetBuiltinType(SemIR::BuiltinKind::IntType));
  47. auto array_cat =
  48. SemIR::GetExprCategory(context.sem_ir(), operand_inst_id);
  49. if (array_cat == SemIR::ExprCategory::Value) {
  50. // If the operand is an array value, convert it to an ephemeral
  51. // reference to an array so we can perform a primitive indexing into it.
  52. operand_inst_id = context.AddInst(
  53. {parse_node, SemIR::ValueAsRef{operand_type_id, operand_inst_id}});
  54. }
  55. // Constant evaluation will perform a bounds check on this array indexing
  56. // if the index is constant.
  57. auto elem_id = context.AddInst(
  58. {parse_node, SemIR::ArrayIndex{array_type.element_type_id,
  59. operand_inst_id, cast_index_id}});
  60. if (array_cat != SemIR::ExprCategory::DurableRef) {
  61. // Indexing a durable reference gives a durable reference expression.
  62. // Indexing anything else gives a value expression.
  63. // TODO: This should be replaced by a choice between using `IndexWith`
  64. // and `IndirectIndexWith`.
  65. elem_id = ConvertToValueExpr(context, elem_id);
  66. }
  67. context.node_stack().Push(parse_node, elem_id);
  68. return true;
  69. }
  70. case SemIR::TupleType::Kind: {
  71. SemIR::TypeId element_type_id = SemIR::TypeId::Error;
  72. auto index_parse_node = context.insts().GetParseNode(index_inst_id);
  73. index_inst_id = ConvertToValueOfType(
  74. context, index_parse_node, index_inst_id,
  75. context.GetBuiltinType(SemIR::BuiltinKind::IntType));
  76. auto index_const_id = context.constant_values().Get(index_inst_id);
  77. if (index_const_id == SemIR::ConstantId::Error) {
  78. index_inst_id = SemIR::InstId::BuiltinError;
  79. } else if (!index_const_id.is_template()) {
  80. // TODO: Decide what to do if the index is a symbolic constant.
  81. CARBON_DIAGNOSTIC(TupleIndexNotConstant, Error,
  82. "Tuple index must be a constant.");
  83. context.emitter().Emit(parse_node, TupleIndexNotConstant);
  84. index_inst_id = SemIR::InstId::BuiltinError;
  85. } else {
  86. auto index_literal =
  87. context.insts().GetAs<SemIR::IntLiteral>(index_const_id.inst_id());
  88. auto type_block = context.type_blocks().Get(
  89. operand_type_inst.As<SemIR::TupleType>().elements_id);
  90. if (const auto* index_val =
  91. ValidateTupleIndex(context, parse_node, operand_inst,
  92. index_literal, type_block.size())) {
  93. element_type_id = type_block[index_val->getZExtValue()];
  94. } else {
  95. index_inst_id = SemIR::InstId::BuiltinError;
  96. }
  97. }
  98. context.AddInstAndPush(
  99. {parse_node,
  100. SemIR::TupleIndex{element_type_id, operand_inst_id, index_inst_id}});
  101. return true;
  102. }
  103. default: {
  104. if (operand_type_id != SemIR::TypeId::Error) {
  105. CARBON_DIAGNOSTIC(TypeNotIndexable, Error,
  106. "Type `{0}` does not support indexing.",
  107. SemIR::TypeId);
  108. context.emitter().Emit(parse_node, TypeNotIndexable, operand_type_id);
  109. }
  110. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinError);
  111. return true;
  112. }
  113. }
  114. }
  115. } // namespace Carbon::Check