handle_struct.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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::NodeId /*parse_node*/) -> bool {
  8. context.ParamOrArgComma();
  9. return true;
  10. }
  11. auto HandleStructFieldDesignator(Context& context, Parse::NodeId /*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::NodeId parse_node) -> bool {
  20. auto [type_node, type_id] = context.node_stack().PopExprWithParseNode();
  21. SemIR::TypeId cast_type_id = ExprAsType(context, type_node, type_id);
  22. auto [name_node, name_id] =
  23. context.node_stack().PopWithParseNode<Parse::NodeKind::Name>();
  24. context.AddInstAndPush(
  25. parse_node, SemIR::StructTypeField{name_node, name_id, cast_type_id});
  26. return true;
  27. }
  28. auto HandleStructFieldUnknown(Context& context, Parse::NodeId parse_node)
  29. -> bool {
  30. return context.TODO(parse_node, "HandleStructFieldUnknown");
  31. }
  32. auto HandleStructFieldValue(Context& context, Parse::NodeId parse_node)
  33. -> bool {
  34. auto value_inst_id = context.node_stack().PopExpr();
  35. auto [name_node, name_id] =
  36. context.node_stack().PopWithParseNode<Parse::NodeKind::Name>();
  37. // Store the name for the type.
  38. context.args_type_info_stack().AddInst(SemIR::StructTypeField{
  39. name_node, name_id, context.insts().Get(value_inst_id).type_id()});
  40. // Push the value back on the stack as an argument.
  41. context.node_stack().Push(parse_node, value_inst_id);
  42. return true;
  43. }
  44. static auto DiagnoseDuplicateNames(Context& context,
  45. SemIR::InstBlockId type_block_id,
  46. llvm::StringRef construct) -> bool {
  47. auto& sem_ir = context.sem_ir();
  48. auto fields = sem_ir.inst_blocks().Get(type_block_id);
  49. llvm::SmallDenseMap<SemIR::NameId, Parse::NodeId> names;
  50. auto& insts = sem_ir.insts();
  51. for (SemIR::InstId field_inst_id : fields) {
  52. auto field_inst = insts.GetAs<SemIR::StructTypeField>(field_inst_id);
  53. auto [it, added] =
  54. names.insert({field_inst.name_id, field_inst.parse_node});
  55. if (!added) {
  56. CARBON_DIAGNOSTIC(StructNameDuplicate, Error,
  57. "Duplicated field name `{1}` in {0}.", std::string,
  58. std::string);
  59. CARBON_DIAGNOSTIC(StructNamePrevious, Note,
  60. "Field with the same name here.");
  61. context.emitter()
  62. .Build(field_inst.parse_node, StructNameDuplicate, construct.str(),
  63. sem_ir.names().GetFormatted(field_inst.name_id).str())
  64. .Note(it->second, StructNamePrevious)
  65. .Emit();
  66. return true;
  67. }
  68. }
  69. return false;
  70. }
  71. auto HandleStructLiteral(Context& context, Parse::NodeId parse_node) -> bool {
  72. auto refs_id = context.ParamOrArgEnd(
  73. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart);
  74. context.PopScope();
  75. context.node_stack()
  76. .PopAndDiscardSoloParseNode<
  77. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart>();
  78. auto type_block_id = context.args_type_info_stack().Pop();
  79. if (DiagnoseDuplicateNames(context, type_block_id, "struct literal")) {
  80. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinError);
  81. return true;
  82. }
  83. auto type_id = context.CanonicalizeStructType(parse_node, type_block_id);
  84. auto value_id =
  85. context.AddInst(SemIR::StructLiteral{parse_node, type_id, refs_id});
  86. context.node_stack().Push(parse_node, value_id);
  87. return true;
  88. }
  89. auto HandleStructLiteralOrStructTypeLiteralStart(Context& context,
  90. Parse::NodeId parse_node)
  91. -> bool {
  92. context.PushScope();
  93. context.node_stack().Push(parse_node);
  94. // At this point we aren't sure whether this will be a value or type literal,
  95. // so we push onto args irrespective. It just won't be used for a type
  96. // literal.
  97. context.args_type_info_stack().Push();
  98. context.ParamOrArgStart();
  99. return true;
  100. }
  101. auto HandleStructTypeLiteral(Context& context, Parse::NodeId parse_node)
  102. -> bool {
  103. auto refs_id = context.ParamOrArgEnd(
  104. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart);
  105. context.PopScope();
  106. context.node_stack()
  107. .PopAndDiscardSoloParseNode<
  108. Parse::NodeKind::StructLiteralOrStructTypeLiteralStart>();
  109. // This is only used for value literals.
  110. context.args_type_info_stack().Pop();
  111. CARBON_CHECK(refs_id != SemIR::InstBlockId::Empty)
  112. << "{} is handled by StructLiteral.";
  113. if (DiagnoseDuplicateNames(context, refs_id, "struct type literal")) {
  114. context.node_stack().Push(parse_node, SemIR::InstId::BuiltinError);
  115. return true;
  116. }
  117. context.AddInstAndPush(
  118. parse_node,
  119. SemIR::StructType{parse_node, SemIR::TypeId::TypeType, refs_id});
  120. return true;
  121. }
  122. } // namespace Carbon::Check