handle_array.cpp 1.9 KB

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