semantics_handle_array.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 {
  9. auto SemanticsHandleArrayExpressionStart(SemanticsContext& /*context*/,
  10. ParseTree::Node /*parse_node*/)
  11. -> bool {
  12. return true;
  13. }
  14. auto SemanticsHandleArrayExpressionSemi(SemanticsContext& context,
  15. ParseTree::Node parse_node) -> bool {
  16. context.node_stack().Push(parse_node);
  17. return true;
  18. }
  19. auto SemanticsHandleArrayExpression(SemanticsContext& context,
  20. ParseTree::Node parse_node) -> bool {
  21. // TODO: Handle array type with undefined bound.
  22. if (context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
  23. ParseNodeKind::ArrayExpressionSemi) {
  24. context.node_stack().PopAndIgnore();
  25. context.node_stack().PopAndIgnore();
  26. return context.TODO(parse_node, "HandleArrayExpressionWithoutBounds");
  27. }
  28. auto bound_node_id = context.node_stack().PopExpression();
  29. context.node_stack()
  30. .PopAndDiscardSoloParseNode<ParseNodeKind::ArrayExpressionSemi>();
  31. auto element_type_node_id = context.node_stack().PopExpression();
  32. auto bound_node = context.semantics_ir().GetNode(bound_node_id);
  33. if (bound_node.kind() == SemanticsNodeKind::IntegerLiteral) {
  34. auto bound_value = context.semantics_ir().GetIntegerLiteral(
  35. bound_node.GetAsIntegerLiteral());
  36. if (!bound_value.isNegative()) {
  37. context.AddNodeAndPush(
  38. parse_node,
  39. SemanticsNode::ArrayType::Make(
  40. parse_node, SemanticsTypeId::TypeType, bound_node_id,
  41. context.ExpressionAsType(parse_node, element_type_node_id)));
  42. return true;
  43. }
  44. }
  45. CARBON_DIAGNOSTIC(InvalidArrayExpression, Error, "Invalid array expression.");
  46. context.emitter().Emit(parse_node, InvalidArrayExpression);
  47. context.node_stack().Push(parse_node, SemanticsNodeId::BuiltinError);
  48. return true;
  49. }
  50. } // namespace Carbon