handle_struct.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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::StructCommaId /*parse_node*/)
  8. -> bool {
  9. context.ParamOrArgComma();
  10. return true;
  11. }
  12. auto HandleStructFieldDesignator(Context& context,
  13. Parse::StructFieldDesignatorId /*parse_node*/)
  14. -> bool {
  15. // This leaves the designated name on top because the `.` isn't interesting.
  16. CARBON_CHECK(context.node_stack().PeekIsName());
  17. return true;
  18. }
  19. auto HandleStructFieldType(Context& context,
  20. Parse::StructFieldTypeId parse_node) -> bool {
  21. auto [type_node, type_id] = context.node_stack().PopExprWithParseNode();
  22. SemIR::TypeId cast_type_id = ExprAsType(context, type_node, type_id);
  23. auto [name_node, name_id] = context.node_stack().PopNameWithParseNode();
  24. context.AddInstAndPush(
  25. parse_node, SemIR::StructTypeField{name_node, name_id, cast_type_id});
  26. return true;
  27. }
  28. auto HandleStructFieldValue(Context& context,
  29. Parse::StructFieldValueId parse_node) -> bool {
  30. auto value_inst_id = context.node_stack().PopExpr();
  31. auto [name_node, name_id] = context.node_stack().PopNameWithParseNode();
  32. // Store the name for the type.
  33. context.args_type_info_stack().AddInst(SemIR::StructTypeField{
  34. name_node, name_id, context.insts().Get(value_inst_id).type_id()});
  35. // Push the value back on the stack as an argument.
  36. context.node_stack().Push(parse_node, value_inst_id);
  37. return true;
  38. }
  39. static auto DiagnoseDuplicateNames(Context& context,
  40. SemIR::InstBlockId type_block_id,
  41. llvm::StringRef construct) -> bool {
  42. auto& sem_ir = context.sem_ir();
  43. auto fields = sem_ir.inst_blocks().Get(type_block_id);
  44. llvm::SmallDenseMap<SemIR::NameId, Parse::NodeId> names;
  45. auto& insts = sem_ir.insts();
  46. for (SemIR::InstId field_inst_id : fields) {
  47. auto field_inst = insts.GetAs<SemIR::StructTypeField>(field_inst_id);
  48. auto [it, added] =
  49. names.insert({field_inst.name_id, field_inst.parse_node});
  50. if (!added) {
  51. CARBON_DIAGNOSTIC(StructNameDuplicate, Error,
  52. "Duplicated field name `{1}` in {0}.", std::string,
  53. std::string);
  54. CARBON_DIAGNOSTIC(StructNamePrevious, Note,
  55. "Field with the same name here.");
  56. context.emitter()
  57. .Build(field_inst.parse_node, StructNameDuplicate, construct.str(),
  58. sem_ir.names().GetFormatted(field_inst.name_id).str())
  59. .Note(it->second, StructNamePrevious)
  60. .Emit();
  61. return true;
  62. }
  63. }
  64. return false;
  65. }
  66. auto HandleStructLiteral(Context& context, Parse::StructLiteralId parse_node)
  67. -> 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(
  86. Context& context, Parse::StructLiteralOrStructTypeLiteralStartId 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,
  98. Parse::StructTypeLiteralId parse_node) -> 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