handle_array.cpp 2.1 KB

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