handle_struct.cpp 5.0 KB

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