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/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. // TODO: Produce an error if the array type is too large.
  35. if (bound_value.getBitWidth() <= 64) {
  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