handle_struct.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. namespace Carbon::Check {
  7. auto HandleStructComma(Context& context, Parse::Node /*parse_node*/) -> bool {
  8. context.ParamOrArgComma();
  9. return true;
  10. }
  11. auto HandleStructFieldDesignator(Context& context, Parse::Node /*parse_node*/)
  12. -> bool {
  13. // This leaves the designated name on top because the `.` isn't interesting.
  14. CARBON_CHECK(
  15. context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
  16. Parse::NodeKind::Name);
  17. return true;
  18. }
  19. auto HandleStructFieldType(Context& context, Parse::Node parse_node) -> bool {
  20. auto [type_node, type_id] = context.node_stack().PopExpressionWithParseNode();
  21. SemIR::TypeId cast_type_id = ExpressionAsType(context, type_node, type_id);
  22. auto [name_node, name_id] =
  23. context.node_stack().PopWithParseNode<Parse::NodeKind::Name>();
  24. context.AddNodeAndPush(
  25. parse_node, SemIR::StructTypeField(name_node, name_id, cast_type_id));
  26. return true;
  27. }
  28. auto HandleStructFieldUnknown(Context& context, Parse::Node parse_node)
  29. -> bool {
  30. return context.TODO(parse_node, "HandleStructFieldUnknown");
  31. }
  32. auto HandleStructFieldValue(Context& context, Parse::Node parse_node) -> bool {
  33. auto [value_parse_node, value_node_id] =
  34. context.node_stack().PopExpressionWithParseNode();
  35. SemIR::StringId name_id = context.node_stack().Pop<Parse::NodeKind::Name>();
  36. // Store the name for the type.
  37. context.args_type_info_stack().AddNode(SemIR::StructTypeField(
  38. parse_node, name_id,
  39. context.semantics_ir().GetNode(value_node_id).type_id()));
  40. // Push the value back on the stack as an argument.
  41. context.node_stack().Push(parse_node, value_node_id);
  42. return true;
  43. }
  44. auto HandleStructLiteral(Context& context, Parse::Node parse_node) -> bool {
  45. auto refs_id = context.ParamOrArgEnd(
  46. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart);
  47. context.PopScope();
  48. context.node_stack()
  49. .PopAndDiscardSoloParseNode<
  50. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart>();
  51. auto type_block_id = context.args_type_info_stack().Pop();
  52. auto type_id = context.CanonicalizeStructType(parse_node, type_block_id);
  53. auto value_id =
  54. context.AddNode(SemIR::StructLiteral(parse_node, type_id, refs_id));
  55. context.node_stack().Push(parse_node, value_id);
  56. return true;
  57. }
  58. auto HandleStructLiteralOrStructTypeLiteralStart(Context& context,
  59. Parse::Node parse_node)
  60. -> bool {
  61. context.PushScope();
  62. context.node_stack().Push(parse_node);
  63. // At this point we aren't sure whether this will be a value or type literal,
  64. // so we push onto args irrespective. It just won't be used for a type
  65. // literal.
  66. context.args_type_info_stack().Push();
  67. context.ParamOrArgStart();
  68. return true;
  69. }
  70. auto HandleStructTypeLiteral(Context& context, Parse::Node parse_node) -> bool {
  71. auto refs_id = context.ParamOrArgEnd(
  72. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart);
  73. context.PopScope();
  74. context.node_stack()
  75. .PopAndDiscardSoloParseNode<
  76. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart>();
  77. // This is only used for value literals.
  78. context.args_type_info_stack().Pop();
  79. CARBON_CHECK(refs_id != SemIR::NodeBlockId::Empty)
  80. << "{} is handled by StructLiteral.";
  81. context.AddNodeAndPush(
  82. parse_node,
  83. SemIR::StructType(parse_node, SemIR::TypeId::TypeType, refs_id));
  84. return true;
  85. }
  86. } // namespace Carbon::Check