semantics_handle_struct.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 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.
  70. auto refs = context.semantics().GetNodeBlock(refs_id);
  71. auto type_id = context.AddNode(
  72. SemanticsNode::StructType::Make(parse_node, type_block_id));
  73. auto value_id = context.AddNode(
  74. SemanticsNode::StructValue::Make(parse_node, type_id, refs_id));
  75. context.node_stack().Push(parse_node, value_id);
  76. return true;
  77. }
  78. auto SemanticsHandleStructLiteralOrStructTypeLiteralStart(
  79. SemanticsContext& context, ParseTree::Node parse_node) -> bool {
  80. context.PushScope();
  81. context.node_stack().Push(parse_node);
  82. // At this point we aren't sure whether this will be a value or type literal,
  83. // so we push onto args irrespective. It just won't be used for a type
  84. // literal.
  85. context.args_type_info_stack().Push();
  86. context.ParamOrArgStart();
  87. return true;
  88. }
  89. auto SemanticsHandleStructTypeLiteral(SemanticsContext& context,
  90. ParseTree::Node parse_node) -> bool {
  91. auto refs_id = context.ParamOrArgEnd(
  92. /*for_args=*/false, ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  93. context.PopScope();
  94. context.node_stack().PopAndDiscardSoloParseNode(
  95. ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  96. // This is only used for value literals.
  97. context.args_type_info_stack().Pop();
  98. CARBON_CHECK(refs_id != SemanticsNodeBlockId::Empty)
  99. << "{} is handled by StructLiteral.";
  100. auto type_id =
  101. context.AddNode(SemanticsNode::StructType::Make(parse_node, refs_id));
  102. context.node_stack().Push(parse_node, type_id);
  103. return true;
  104. }
  105. } // namespace Carbon