handle_array.cpp 2.1 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/parse/node_kind.h"
  6. #include "toolchain/sem_ir/node.h"
  7. #include "toolchain/sem_ir/node_kind.h"
  8. namespace Carbon::Check {
  9. auto HandleArrayExpressionStart(Context& /*context*/,
  10. Parse::Node /*parse_node*/) -> bool {
  11. return true;
  12. }
  13. auto HandleArrayExpressionSemi(Context& context, Parse::Node parse_node)
  14. -> bool {
  15. context.node_stack().Push(parse_node);
  16. return true;
  17. }
  18. auto HandleArrayExpression(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::ArrayExpressionSemi) {
  22. context.node_stack().PopAndIgnore();
  23. context.node_stack().PopAndIgnore();
  24. return context.TODO(parse_node, "HandleArrayExpressionWithoutBounds");
  25. }
  26. auto bound_node_id = context.node_stack().PopExpression();
  27. context.node_stack()
  28. .PopAndDiscardSoloParseNode<Parse::NodeKind::ArrayExpressionSemi>();
  29. auto element_type_node_id = context.node_stack().PopExpression();
  30. auto bound_node = context.semantics_ir().GetNode(bound_node_id);
  31. if (bound_node.kind() == SemIR::NodeKind::IntegerLiteral) {
  32. auto bound_value = context.semantics_ir().GetIntegerLiteral(
  33. bound_node.GetAsIntegerLiteral());
  34. if (!bound_value.isNegative()) {
  35. context.AddNodeAndPush(
  36. parse_node,
  37. SemIR::Node::ArrayType::Make(
  38. parse_node, SemIR::TypeId::TypeType, bound_node_id,
  39. context.ExpressionAsType(parse_node, element_type_node_id)));
  40. return true;
  41. }
  42. }
  43. CARBON_DIAGNOSTIC(InvalidArrayExpression, Error, "Invalid array expression.");
  44. context.emitter().Emit(parse_node, InvalidArrayExpression);
  45. context.node_stack().Push(parse_node, SemIR::NodeId::BuiltinError);
  46. return true;
  47. }
  48. } // namespace Carbon::Check