handle_class.cpp 4.9 KB

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