handle_array.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 "toolchain/check/context.h"
  5. #include "toolchain/check/convert.h"
  6. #include "toolchain/parse/node_kind.h"
  7. #include "toolchain/sem_ir/inst.h"
  8. namespace Carbon::Check {
  9. auto HandleArrayExprStart(Context& /*context*/,
  10. Parse::ArrayExprStartId /*parse_node*/) -> bool {
  11. return true;
  12. }
  13. auto HandleArrayExprSemi(Context& context, Parse::ArrayExprSemiId parse_node)
  14. -> bool {
  15. context.node_stack().Push(parse_node);
  16. return true;
  17. }
  18. auto HandleArrayExpr(Context& context, Parse::ArrayExprId parse_node) -> bool {
  19. // TODO: Handle array type with undefined bound.
  20. if (context.node_stack()
  21. .PopAndDiscardSoloParseNodeIf<Parse::NodeKind::ArrayExprSemi>()) {
  22. context.node_stack().PopAndIgnore();
  23. return context.TODO(parse_node, "HandleArrayExprWithoutBounds");
  24. }
  25. auto bound_inst_id = context.node_stack().PopExpr();
  26. context.node_stack()
  27. .PopAndDiscardSoloParseNode<Parse::NodeKind::ArrayExprSemi>();
  28. auto element_type_inst_id = context.node_stack().PopExpr();
  29. auto bound_inst = context.insts().Get(bound_inst_id);
  30. if (auto literal = bound_inst.TryAs<SemIR::IntLiteral>()) {
  31. const auto& bound_value = context.ints().Get(literal->int_id);
  32. // TODO: Produce an error if the array type is too large.
  33. if (bound_value.getActiveBits() <= 64) {
  34. context.AddInstAndPush(
  35. parse_node,
  36. SemIR::ArrayType{
  37. parse_node, SemIR::TypeId::TypeType, bound_inst_id,
  38. ExprAsType(context, parse_node, element_type_inst_id)});
  39. return true;
  40. }
  41. }
  42. CARBON_DIAGNOSTIC(InvalidArrayExpr, Error, "Invalid array expression.");
  43. context.emitter().Emit(parse_node, InvalidArrayExpr);
  44. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinError);
  45. return true;
  46. }
  47. } // namespace Carbon::Check