handle_array.cpp 2.0 KB

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