handle_struct.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 HandleStructTypeLiteralStart(Context& context,
  8. Parse::StructTypeLiteralStartId node_id)
  9. -> bool {
  10. context.scope_stack().Push();
  11. context.node_stack().Push(node_id);
  12. context.param_and_arg_refs_stack().Push();
  13. return true;
  14. }
  15. auto HandleStructLiteralStart(Context& context,
  16. Parse::StructLiteralStartId node_id) -> bool {
  17. context.scope_stack().Push();
  18. context.node_stack().Push(node_id);
  19. context.args_type_info_stack().Push();
  20. context.param_and_arg_refs_stack().Push();
  21. return true;
  22. }
  23. auto HandleStructFieldDesignator(Context& context,
  24. Parse::StructFieldDesignatorId /*node_id*/)
  25. -> bool {
  26. // This leaves the designated name on top because the `.` isn't interesting.
  27. CARBON_CHECK(context.node_stack().PeekIsName());
  28. return true;
  29. }
  30. auto HandleStructComma(Context& context, Parse::StructCommaId /*node_id*/)
  31. -> bool {
  32. context.param_and_arg_refs_stack().ApplyComma();
  33. return true;
  34. }
  35. auto HandleStructField(Context& context, Parse::StructFieldId node_id) -> bool {
  36. auto value_inst_id = context.node_stack().PopExpr();
  37. auto [name_node, name_id] = context.node_stack().PopNameWithNodeId();
  38. // Store the name for the type.
  39. context.args_type_info_stack().AddInstId(
  40. context.AddInstInNoBlock<SemIR::StructTypeField>(
  41. name_node,
  42. {.name_id = name_id,
  43. .field_type_id = context.insts().Get(value_inst_id).type_id()}));
  44. // Push the value back on the stack as an argument.
  45. context.node_stack().Push(node_id, value_inst_id);
  46. return true;
  47. }
  48. auto HandleStructTypeField(Context& context, Parse::StructTypeFieldId node_id)
  49. -> bool {
  50. auto [type_node, type_id] = context.node_stack().PopExprWithNodeId();
  51. SemIR::TypeId cast_type_id = ExprAsType(context, type_node, type_id);
  52. auto [name_node, name_id] = context.node_stack().PopNameWithNodeId();
  53. auto inst_id = context.AddInst<SemIR::StructTypeField>(
  54. name_node, {.name_id = name_id, .field_type_id = cast_type_id});
  55. context.node_stack().Push(node_id, inst_id);
  56. return true;
  57. }
  58. static auto DiagnoseDuplicateNames(Context& context,
  59. SemIR::InstBlockId type_block_id,
  60. llvm::StringRef construct) -> bool {
  61. auto& sem_ir = context.sem_ir();
  62. auto fields = sem_ir.inst_blocks().Get(type_block_id);
  63. llvm::SmallDenseMap<SemIR::NameId, SemIR::InstId> names;
  64. auto& insts = sem_ir.insts();
  65. for (SemIR::InstId field_inst_id : fields) {
  66. auto field_inst = insts.GetAs<SemIR::StructTypeField>(field_inst_id);
  67. auto [it, added] = names.insert({field_inst.name_id, field_inst_id});
  68. if (!added) {
  69. CARBON_DIAGNOSTIC(StructNameDuplicate, Error,
  70. "Duplicated field name `{1}` in {0}.", std::string,
  71. SemIR::NameId);
  72. CARBON_DIAGNOSTIC(StructNamePrevious, Note,
  73. "Field with the same name here.");
  74. context.emitter()
  75. .Build(field_inst_id, StructNameDuplicate, construct.str(),
  76. field_inst.name_id)
  77. .Note(it->second, StructNamePrevious)
  78. .Emit();
  79. return true;
  80. }
  81. }
  82. return false;
  83. }
  84. auto HandleStructLiteral(Context& context, Parse::StructLiteralId node_id)
  85. -> bool {
  86. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  87. Parse::NodeKind::StructLiteralStart);
  88. context.scope_stack().Pop();
  89. context.node_stack()
  90. .PopAndDiscardSoloNodeId<Parse::NodeKind::StructLiteralStart>();
  91. auto type_block_id = context.args_type_info_stack().Pop();
  92. if (DiagnoseDuplicateNames(context, type_block_id, "struct literal")) {
  93. context.node_stack().Push(node_id, SemIR::InstId::BuiltinError);
  94. return true;
  95. }
  96. auto type_id = context.GetStructType(type_block_id);
  97. auto value_id = context.AddInst<SemIR::StructLiteral>(
  98. node_id, {.type_id = type_id, .elements_id = refs_id});
  99. context.node_stack().Push(node_id, value_id);
  100. return true;
  101. }
  102. auto HandleStructTypeLiteral(Context& context,
  103. Parse::StructTypeLiteralId node_id) -> bool {
  104. auto refs_id = context.param_and_arg_refs_stack().EndAndPop(
  105. Parse::NodeKind::StructTypeLiteralStart);
  106. context.scope_stack().Pop();
  107. context.node_stack()
  108. .PopAndDiscardSoloNodeId<Parse::NodeKind::StructTypeLiteralStart>();
  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(node_id, SemIR::InstId::BuiltinError);
  113. return true;
  114. }
  115. context.AddInstAndPush<SemIR::StructType>(
  116. node_id, {.type_id = SemIR::TypeId::TypeType, .fields_id = refs_id});
  117. return true;
  118. }
  119. } // namespace Carbon::Check