semantics_handle_struct.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.ExpressionAsType(type_node, type_id);
  27. auto [name_node, name_id] = context.node_stack().PopForParseNodeAndNameId(
  28. ParseNodeKind::DesignatedName);
  29. context.AddNode(
  30. SemanticsNode::StructTypeField::Make(name_node, cast_type_id, name_id));
  31. context.node_stack().Push(parse_node);
  32. return true;
  33. }
  34. auto SemanticsHandleStructFieldUnknown(SemanticsContext& context,
  35. ParseTree::Node parse_node) -> bool {
  36. return context.TODO(parse_node, "HandleStructFieldUnknown");
  37. }
  38. auto SemanticsHandleStructFieldValue(SemanticsContext& context,
  39. ParseTree::Node parse_node) -> bool {
  40. auto [value_parse_node, value_node_id] =
  41. context.node_stack().PopForParseNodeAndNodeId();
  42. auto [_, name_id] = context.node_stack().PopForParseNodeAndNameId(
  43. ParseNodeKind::DesignatedName);
  44. // Store the name for the type.
  45. auto type_block_id = context.args_type_info_stack().PeekForAdd();
  46. context.semantics().AddNode(
  47. type_block_id,
  48. SemanticsNode::StructTypeField::Make(
  49. parse_node, context.semantics().GetNode(value_node_id).type_id(),
  50. name_id));
  51. // Push the value back on the stack as an argument.
  52. context.node_stack().Push(parse_node, value_node_id);
  53. return true;
  54. }
  55. auto SemanticsHandleStructLiteral(SemanticsContext& context,
  56. ParseTree::Node parse_node) -> bool {
  57. auto refs_id = context.ParamOrArgEnd(
  58. /*for_args=*/true, ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  59. context.PopScope();
  60. context.node_stack().PopAndDiscardSoloParseNode(
  61. ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  62. auto type_block_id = context.args_type_info_stack().Pop();
  63. // Construct a type for the literal.
  64. // TODO: This should try to canonicalize the struct form before adding the
  65. // node.
  66. auto refs = context.semantics().GetNodeBlock(refs_id);
  67. auto type_id =
  68. context.CanonicalizeType(context.AddNode(SemanticsNode::StructType::Make(
  69. parse_node,
  70. context.CanonicalizeType(SemanticsNodeId::BuiltinTypeType),
  71. type_block_id)));
  72. auto value_id = context.AddNode(
  73. SemanticsNode::StructValue::Make(parse_node, type_id, refs_id));
  74. context.node_stack().Push(parse_node, value_id);
  75. return true;
  76. }
  77. auto SemanticsHandleStructLiteralOrStructTypeLiteralStart(
  78. SemanticsContext& context, ParseTree::Node parse_node) -> bool {
  79. context.PushScope();
  80. context.node_stack().Push(parse_node);
  81. // At this point we aren't sure whether this will be a value or type literal,
  82. // so we push onto args irrespective. It just won't be used for a type
  83. // literal.
  84. context.args_type_info_stack().Push();
  85. context.ParamOrArgStart();
  86. return true;
  87. }
  88. auto SemanticsHandleStructTypeLiteral(SemanticsContext& context,
  89. ParseTree::Node parse_node) -> bool {
  90. auto refs_id = context.ParamOrArgEnd(
  91. /*for_args=*/false, ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  92. context.PopScope();
  93. context.node_stack().PopAndDiscardSoloParseNode(
  94. ParseNodeKind::StructLiteralOrStructTypeLiteralStart);
  95. // This is only used for value literals.
  96. context.args_type_info_stack().Pop();
  97. CARBON_CHECK(refs_id != SemanticsNodeBlockId::Empty)
  98. << "{} is handled by StructLiteral.";
  99. auto type_id = context.AddNode(SemanticsNode::StructType::Make(
  100. parse_node, context.CanonicalizeType(SemanticsNodeId::BuiltinTypeType),
  101. refs_id));
  102. context.node_stack().Push(parse_node, type_id);
  103. return true;
  104. }
  105. } // namespace Carbon