semantics_handle_struct.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/semantics/semantics_context.h"
  5. namespace Carbon {
  6. auto SemanticsHandleStructComma(SemanticsContext& context,
  7. ParseTree::Node /*parse_node*/) -> bool {
  8. context.ParamOrArgComma(
  9. /*for_args=*/context.parse_tree().node_kind(
  10. context.node_stack().PeekParseNode()) !=
  11. ParseNodeKind::StructFieldType);
  12. return true;
  13. }
  14. auto SemanticsHandleStructFieldDesignator(SemanticsContext& context,
  15. ParseTree::Node /*parse_node*/)
  16. -> bool {
  17. // This leaves the designated name on top because the `.` isn't interesting.
  18. CARBON_CHECK(
  19. context.parse_tree().node_kind(context.node_stack().PeekParseNode()) ==
  20. ParseNodeKind::DesignatedName);
  21. return true;
  22. }
  23. auto SemanticsHandleStructFieldType(SemanticsContext& context,
  24. ParseTree::Node parse_node) -> bool {
  25. auto [type_node, type_id] = context.node_stack().PopForParseNodeAndNodeId();
  26. SemanticsNodeId cast_type_id = context.ImplicitAsRequired(
  27. type_node, type_id, SemanticsNodeId::BuiltinTypeType);
  28. auto [name_node, name_id] = context.node_stack().PopForParseNodeAndNameId(
  29. ParseNodeKind::DesignatedName);
  30. context.AddNode(
  31. SemanticsNode::StructTypeField::Make(name_node, cast_type_id, name_id));
  32. context.node_stack().Push(parse_node);
  33. return true;
  34. }
  35. auto SemanticsHandleStructFieldUnknown(SemanticsContext& context,
  36. ParseTree::Node parse_node) -> bool {
  37. return context.TODO(parse_node, "HandleStructFieldUnknown");
  38. }
  39. auto SemanticsHandleStructFieldValue(SemanticsContext& context,
  40. ParseTree::Node parse_node) -> bool {
  41. auto [value_parse_node, value_node_id] =
  42. context.node_stack().PopForParseNodeAndNodeId();
  43. auto [_, name_id] = context.node_stack().PopForParseNodeAndNameId(
  44. ParseNodeKind::DesignatedName);
  45. // Store the name for the type.
  46. auto type_block_id = context.args_type_info_stack().PeekForAdd();
  47. context.semantics().AddNode(
  48. type_block_id,
  49. SemanticsNode::StructTypeField::Make(
  50. parse_node, context.semantics().GetNode(value_node_id).type_id(),
  51. name_id));
  52. // Push the value back on the stack as an argument.
  53. context.node_stack().Push(parse_node, value_node_id);
  54. return true;
  55. }
  56. auto SemanticsHandleStructLiteral(SemanticsContext& context,
  57. ParseTree::Node parse_node) -> bool {
  58. auto [ir_id, refs_id] = context.ParamOrArgEnd(
  59. /*for_args=*/true, ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  60. context.PopScope();
  61. context.node_stack().PopAndDiscardSoloParseNode(
  62. ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  63. auto type_block_id = context.args_type_info_stack().Pop();
  64. // Special-case `{}`.
  65. if (refs_id == SemanticsNodeBlockId::Empty) {
  66. context.node_stack().Push(parse_node, SemanticsNodeId::BuiltinEmptyStruct);
  67. return true;
  68. }
  69. // Construct a type for the literal. Each field is one node, so ir_id and
  70. // refs_id match.
  71. auto refs = context.semantics().GetNodeBlock(refs_id);
  72. auto type_id = context.AddNode(SemanticsNode::StructType::Make(
  73. parse_node, type_block_id, type_block_id));
  74. auto value_id = context.AddNode(
  75. SemanticsNode::StructValue::Make(parse_node, type_id, ir_id, refs_id));
  76. context.node_stack().Push(parse_node, value_id);
  77. return true;
  78. }
  79. auto SemanticsHandleStructLiteralOrStructTypeLiteralStart(
  80. SemanticsContext& context, ParseTree::Node parse_node) -> bool {
  81. context.PushScope();
  82. context.node_stack().Push(parse_node);
  83. // At this point we aren't sure whether this will be a value or type literal,
  84. // so we push onto args irrespective. It just won't be used for a type
  85. // literal.
  86. context.args_type_info_stack().Push();
  87. context.ParamOrArgStart();
  88. return true;
  89. }
  90. auto SemanticsHandleStructTypeLiteral(SemanticsContext& context,
  91. ParseTree::Node parse_node) -> bool {
  92. auto [ir_id, refs_id] = context.ParamOrArgEnd(
  93. /*for_args=*/false, ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  94. context.PopScope();
  95. context.node_stack().PopAndDiscardSoloParseNode(
  96. ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  97. // This is only used for value literals.
  98. context.args_type_info_stack().Pop();
  99. CARBON_CHECK(refs_id != SemanticsNodeBlockId::Empty)
  100. << "{} is handled by StructLiteral.";
  101. auto type_id = context.AddNode(
  102. SemanticsNode::StructType::Make(parse_node, ir_id, refs_id));
  103. context.node_stack().Push(parse_node, type_id);
  104. return true;
  105. }
  106. } // namespace Carbon