semantics_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/parser/parse_node_kind.h"
  5. #include "toolchain/semantics/semantics_context.h"
  6. #include "toolchain/semantics/semantics_node.h"
  7. #include "toolchain/semantics/semantics_node_kind.h"
  8. namespace Carbon::Check {
  9. auto HandleArrayExpressionStart(Context& /*context*/,
  10. ParseTree::Node /*parse_node*/) -> bool {
  11. return true;
  12. }
  13. auto HandleArrayExpressionSemi(Context& context, ParseTree::Node parse_node)
  14. -> bool {
  15. context.node_stack().Push(parse_node);
  16. return true;
  17. }
  18. auto HandleArrayExpression(Context& context, ParseTree::Node parse_node)
  19. -> bool {
  20. // TODO: Handle array type with undefined bound.
  21. if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
  22. ParseNodeKind::ArrayExpressionSemi) {
  23. context.node_stack().PopAndIgnore();
  24. context.node_stack().PopAndIgnore();
  25. return context.TODO(parse_node, "HandleArrayExpressionWithoutBounds");
  26. }
  27. auto bound_node_id = context.node_stack().PopExpression();
  28. context.node_stack()
  29. .PopAndDiscardSoloParseNode<ParseNodeKind::ArrayExpressionSemi>();
  30. auto element_type_node_id = context.node_stack().PopExpression();
  31. auto bound_node = context.semantics_ir().GetNode(bound_node_id);
  32. if (bound_node.kind() == SemIR::NodeKind::IntegerLiteral) {
  33. auto bound_value = context.semantics_ir().GetIntegerLiteral(
  34. bound_node.GetAsIntegerLiteral());
  35. if (!bound_value.isNegative()) {
  36. context.AddNodeAndPush(
  37. parse_node,
  38. SemIR::Node::ArrayType::Make(
  39. parse_node, SemIR::TypeId::TypeType, bound_node_id,
  40. context.ExpressionAsType(parse_node, element_type_node_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::NodeId::BuiltinError);
  47. return true;
  48. }
  49. } // namespace Carbon::Check