handle_array.cpp 2.1 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/check/context.h"
  5. #include "toolchain/check/convert.h"
  6. #include "toolchain/parse/node_kind.h"
  7. #include "toolchain/sem_ir/node.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_node_id = context.node_stack().PopExpression();
  28. context.node_stack()
  29. .PopAndDiscardSoloParseNode<Parse::NodeKind::ArrayExpressionSemi>();
  30. auto element_type_node_id = context.node_stack().PopExpression();
  31. auto bound_node = context.semantics_ir().GetNode(bound_node_id);
  32. if (auto literal = bound_node.TryAs<SemIR::IntegerLiteral>()) {
  33. const auto& bound_value =
  34. context.semantics_ir().integers().Get(literal->integer_id);
  35. // TODO: Produce an error if the array type is too large.
  36. if (bound_value.getActiveBits() <= 64) {
  37. context.AddNodeAndPush(
  38. parse_node,
  39. SemIR::ArrayType{
  40. parse_node, SemIR::TypeId::TypeType, bound_node_id,
  41. ExpressionAsType(context, 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, SemIR::NodeId::BuiltinError);
  48. return true;
  49. }
  50. } // namespace Carbon::Check