handle_struct.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. 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, context.nodes().Get(value_node_id).type_id()});
  39. // Push the value back on the stack as an argument.
  40. context.node_stack().Push(parse_node, value_node_id);
  41. return true;
  42. }
  43. auto HandleStructLiteral(Context& context, Parse::Node parse_node) -> bool {
  44. auto refs_id = context.ParamOrArgEnd(
  45. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart);
  46. context.PopScope();
  47. context.node_stack()
  48. .PopAndDiscardSoloParseNode<
  49. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart>();
  50. auto type_block_id = context.args_type_info_stack().Pop();
  51. auto type_id = context.CanonicalizeStructType(parse_node, type_block_id);
  52. auto value_id =
  53. context.AddNode(SemIR::StructLiteral{parse_node, type_id, refs_id});
  54. context.node_stack().Push(parse_node, value_id);
  55. return true;
  56. }
  57. auto HandleStructLiteralOrStructTypeLiteralStart(Context& context,
  58. Parse::Node parse_node)
  59. -> bool {
  60. context.PushScope();
  61. context.node_stack().Push(parse_node);
  62. // At this point we aren't sure whether this will be a value or type literal,
  63. // so we push onto args irrespective. It just won't be used for a type
  64. // literal.
  65. context.args_type_info_stack().Push();
  66. context.ParamOrArgStart();
  67. return true;
  68. }
  69. auto HandleStructTypeLiteral(Context& context, Parse::Node parse_node) -> bool {
  70. auto refs_id = context.ParamOrArgEnd(
  71. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart);
  72. context.PopScope();
  73. context.node_stack()
  74. .PopAndDiscardSoloParseNode<
  75. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart>();
  76. // This is only used for value literals.
  77. context.args_type_info_stack().Pop();
  78. CARBON_CHECK(refs_id != SemIR::NodeBlockId::Empty)
  79. << "{} is handled by StructLiteral.";
  80. context.AddNodeAndPush(
  81. parse_node,
  82. SemIR::StructType{parse_node, SemIR::TypeId::TypeType, refs_id});
  83. return true;
  84. }
  85. } // namespace Carbon::Check