handle_array.cpp 2.0 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/check/convert.h"
  6. #include "toolchain/parse/node_kind.h"
  7. namespace Carbon::Check {
  8. auto HandleArrayExprStart(Context& /*context*/,
  9. Parse::ArrayExprStartId /*parse_node*/) -> bool {
  10. return true;
  11. }
  12. auto HandleArrayExprSemi(Context& context, Parse::ArrayExprSemiId parse_node)
  13. -> bool {
  14. context.node_stack().Push(parse_node);
  15. return true;
  16. }
  17. auto HandleArrayExpr(Context& context, Parse::ArrayExprId parse_node) -> bool {
  18. // TODO: Handle array type with undefined bound.
  19. if (context.node_stack()
  20. .PopAndDiscardSoloParseNodeIf<Parse::NodeKind::ArrayExprSemi>()) {
  21. context.node_stack().PopAndIgnore();
  22. return context.TODO(parse_node, "HandleArrayExprWithoutBounds");
  23. }
  24. auto bound_inst_id = context.node_stack().PopExpr();
  25. context.node_stack()
  26. .PopAndDiscardSoloParseNode<Parse::NodeKind::ArrayExprSemi>();
  27. auto [element_type_node_id, element_type_inst_id] =
  28. context.node_stack().PopExprWithParseNode();
  29. // The array bound must be a constant.
  30. //
  31. // TODO: Should we support runtime-phase bounds in cases such as:
  32. // comptime fn F(n: i32) -> type { return [i32; n]; }
  33. auto bound_inst = context.constant_values().Get(bound_inst_id);
  34. if (!bound_inst.is_constant()) {
  35. CARBON_DIAGNOSTIC(InvalidArrayExpr, Error,
  36. "Array bound is not a constant.");
  37. context.emitter().Emit(bound_inst_id, InvalidArrayExpr);
  38. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinError);
  39. return true;
  40. }
  41. context.AddInstAndPush(
  42. {parse_node, SemIR::ArrayType{SemIR::TypeId::TypeType, bound_inst_id,
  43. ExprAsType(context, element_type_node_id,
  44. element_type_inst_id)}});
  45. return true;
  46. }
  47. } // namespace Carbon::Check