handle_struct.cpp 4.9 KB

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