handle_class.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. namespace Carbon::Check {
  6. auto HandleClassIntroducer(Context& context, Parse::Node parse_node) -> bool {
  7. // Create a node block to hold the nodes created as part of the class
  8. // signature, such as generic parameters.
  9. context.node_block_stack().Push();
  10. // Push the bracketing node.
  11. context.node_stack().Push(parse_node);
  12. // A name should always follow.
  13. context.declaration_name_stack().Push();
  14. return true;
  15. }
  16. static auto BuildClassDeclaration(Context& context)
  17. -> std::tuple<SemIR::ClassId, SemIR::NodeId> {
  18. auto name_context = context.declaration_name_stack().Pop();
  19. auto class_keyword =
  20. context.node_stack()
  21. .PopForSoloParseNode<Parse::NodeKind::ClassIntroducer>();
  22. auto decl_block_id = context.node_block_stack().Pop();
  23. // Add the class declaration.
  24. auto class_decl =
  25. SemIR::ClassDeclaration{class_keyword, SemIR::TypeId::TypeType,
  26. SemIR::ClassId::Invalid, decl_block_id};
  27. auto class_decl_id = context.AddNode(class_decl);
  28. // Check whether this is a redeclaration.
  29. auto existing_id = context.declaration_name_stack().LookupOrAddName(
  30. name_context, class_decl_id);
  31. if (existing_id.is_valid()) {
  32. if (auto existing_class_decl = context.semantics_ir()
  33. .GetNode(existing_id)
  34. .TryAs<SemIR::ClassDeclaration>()) {
  35. // This is a redeclaration of an existing class.
  36. class_decl.class_id = existing_class_decl->class_id;
  37. } else {
  38. // This is a redeclaration of something other than a class.
  39. context.DiagnoseDuplicateName(name_context.parse_node, existing_id);
  40. }
  41. }
  42. // Create a new class if this isn't a valid redeclaration.
  43. if (!class_decl.class_id.is_valid()) {
  44. // TODO: If this is an invalid redeclaration of a non-class entity or there
  45. // was an error in the qualifier, we will have lost track of the class name
  46. // here. We should keep track of it even if the name is invalid.
  47. class_decl.class_id = context.semantics_ir().AddClass(
  48. {.name_id = name_context.state ==
  49. DeclarationNameStack::NameContext::State::Unresolved
  50. ? name_context.unresolved_name_id
  51. : StringId::Invalid});
  52. }
  53. // Write the class ID into the ClassDeclaration.
  54. context.semantics_ir().ReplaceNode(class_decl_id, class_decl);
  55. return {class_decl.class_id, class_decl_id};
  56. }
  57. auto HandleClassDeclaration(Context& context, Parse::Node /*parse_node*/)
  58. -> bool {
  59. BuildClassDeclaration(context);
  60. return true;
  61. }
  62. auto HandleClassDefinitionStart(Context& context, Parse::Node parse_node)
  63. -> bool {
  64. auto [class_id, class_decl_id] = BuildClassDeclaration(context);
  65. auto& class_info = context.semantics_ir().GetClass(class_id);
  66. // Track that this declaration is the definition.
  67. if (class_info.definition_id.is_valid()) {
  68. CARBON_DIAGNOSTIC(ClassRedefinition, Error, "Redefinition of class {0}.",
  69. llvm::StringRef);
  70. CARBON_DIAGNOSTIC(ClassPreviousDefinition, Note,
  71. "Previous definition was here.");
  72. context.emitter()
  73. .Build(parse_node, ClassRedefinition,
  74. context.semantics_ir().strings().Get(class_info.name_id))
  75. .Note(context.semantics_ir()
  76. .GetNode(class_info.definition_id)
  77. .parse_node(),
  78. ClassPreviousDefinition)
  79. .Emit();
  80. } else {
  81. class_info.definition_id = class_decl_id;
  82. class_info.scope_id = context.semantics_ir().AddNameScope();
  83. // TODO: Introduce `Self`.
  84. }
  85. // Enter the class scope.
  86. context.PushScope(class_info.scope_id);
  87. context.node_block_stack().Push();
  88. // TODO: Handle the case where there's control flow in the class body. For
  89. // example:
  90. //
  91. // class C {
  92. // var v: if true then i32 else f64;
  93. // }
  94. //
  95. // We may need to track a list of node blocks here, as we do for a function.
  96. class_info.body_block_id = context.node_block_stack().PeekOrAdd();
  97. return true;
  98. }
  99. auto HandleClassDefinition(Context& context, Parse::Node /*parse_node*/)
  100. -> bool {
  101. context.node_block_stack().Pop();
  102. context.PopScope();
  103. // TODO: Mark the class as a complete type.
  104. return true;
  105. }
  106. } // namespace Carbon::Check