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. #include "toolchain/sem_ir/inst.h"
  8. namespace Carbon::Check {
  9. auto HandleArrayExprStart(Context& /*context*/,
  10. Parse::ArrayExprStartId /*parse_node*/) -> bool {
  11. return true;
  12. }
  13. auto HandleArrayExprSemi(Context& context, Parse::ArrayExprSemiId parse_node)
  14. -> bool {
  15. context.node_stack().Push(parse_node);
  16. return true;
  17. }
  18. auto HandleArrayExpr(Context& context, Parse::ArrayExprId parse_node) -> bool {
  19. // TODO: Handle array type with undefined bound.
  20. if (context.node_stack()
  21. .PopAndDiscardSoloParseNodeIf<Parse::NodeKind::ArrayExprSemi>()) {
  22. context.node_stack().PopAndIgnore();
  23. return context.TODO(parse_node, "HandleArrayExprWithoutBounds");
  24. }
  25. auto bound_inst_id = context.node_stack().PopExpr();
  26. context.node_stack()
  27. .PopAndDiscardSoloParseNode<Parse::NodeKind::ArrayExprSemi>();
  28. auto element_type_inst_id = context.node_stack().PopExpr();
  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{
  43. SemIR::TypeId::TypeType, bound_inst_id,
  44. ExprAsType(context, parse_node, element_type_inst_id)}});
  45. return true;
  46. }
  47. } // namespace Carbon::Check