handle_class.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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/lex/token_kind.h"
  6. namespace Carbon::Check {
  7. auto HandleClassIntroducer(Context& context, Parse::Node parse_node) -> bool {
  8. // Create an instruction block to hold the instructions created as part of the
  9. // class signature, such as generic parameters.
  10. context.inst_block_stack().Push();
  11. // Push the bracketing node.
  12. context.node_stack().Push(parse_node);
  13. // A name should always follow.
  14. context.decl_name_stack().PushScopeAndStartName();
  15. return true;
  16. }
  17. static auto BuildClassDecl(Context& context)
  18. -> std::tuple<SemIR::ClassId, SemIR::InstId> {
  19. auto name_context = context.decl_name_stack().FinishName();
  20. auto class_keyword =
  21. context.node_stack()
  22. .PopForSoloParseNode<Parse::NodeKind::ClassIntroducer>();
  23. auto decl_block_id = context.inst_block_stack().Pop();
  24. // Add the class declaration.
  25. auto class_decl =
  26. SemIR::ClassDecl{class_keyword, SemIR::ClassId::Invalid, decl_block_id};
  27. auto class_decl_id = context.AddInst(class_decl);
  28. // Check whether this is a redeclaration.
  29. auto existing_id =
  30. context.decl_name_stack().LookupOrAddName(name_context, class_decl_id);
  31. if (existing_id.is_valid()) {
  32. if (auto existing_class_decl =
  33. context.insts().Get(existing_id).TryAs<SemIR::ClassDecl>()) {
  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.classes().Add(
  47. {.name_id =
  48. name_context.state == DeclNameStack::NameContext::State::Unresolved
  49. ? name_context.unresolved_name_id
  50. : SemIR::NameId::Invalid,
  51. // `.self_type_id` depends on `class_id`, so is set below.
  52. .self_type_id = SemIR::TypeId::Invalid,
  53. .decl_id = class_decl_id});
  54. // Build the `Self` type.
  55. auto& class_info = context.classes().Get(class_decl.class_id);
  56. class_info.self_type_id =
  57. context.CanonicalizeType(context.AddInst(SemIR::ClassType{
  58. class_keyword, context.GetBuiltinType(SemIR::BuiltinKind::TypeType),
  59. class_decl.class_id}));
  60. }
  61. // Write the class ID into the ClassDecl.
  62. context.insts().Set(class_decl_id, class_decl);
  63. return {class_decl.class_id, class_decl_id};
  64. }
  65. auto HandleClassDecl(Context& context, Parse::Node /*parse_node*/) -> bool {
  66. BuildClassDecl(context);
  67. context.decl_name_stack().PopScope();
  68. return true;
  69. }
  70. auto HandleClassDefinitionStart(Context& context, Parse::Node parse_node)
  71. -> bool {
  72. auto [class_id, class_decl_id] = BuildClassDecl(context);
  73. auto& class_info = context.classes().Get(class_id);
  74. // Track that this declaration is the definition.
  75. if (class_info.definition_id.is_valid()) {
  76. CARBON_DIAGNOSTIC(ClassRedefinition, Error, "Redefinition of class {0}.",
  77. llvm::StringRef);
  78. CARBON_DIAGNOSTIC(ClassPreviousDefinition, Note,
  79. "Previous definition was here.");
  80. context.emitter()
  81. .Build(parse_node, ClassRedefinition,
  82. context.names().GetFormatted(class_info.name_id))
  83. .Note(context.insts().Get(class_info.definition_id).parse_node(),
  84. ClassPreviousDefinition)
  85. .Emit();
  86. } else {
  87. class_info.definition_id = class_decl_id;
  88. class_info.scope_id = context.name_scopes().Add();
  89. }
  90. // Enter the class scope.
  91. context.PushScope(class_decl_id, class_info.scope_id);
  92. // Introduce `Self`.
  93. context.AddNameToLookup(
  94. parse_node, SemIR::NameId::SelfType,
  95. context.sem_ir().GetTypeAllowBuiltinTypes(class_info.self_type_id));
  96. context.inst_block_stack().Push();
  97. context.node_stack().Push(parse_node, class_id);
  98. context.args_type_info_stack().Push();
  99. // TODO: Handle the case where there's control flow in the class body. For
  100. // example:
  101. //
  102. // class C {
  103. // var v: if true then i32 else f64;
  104. // }
  105. //
  106. // We may need to track a list of instruction blocks here, as we do for a
  107. // function.
  108. class_info.body_block_id = context.inst_block_stack().PeekOrAdd();
  109. return true;
  110. }
  111. auto HandleClassDefinition(Context& context, Parse::Node parse_node) -> bool {
  112. auto fields_id = context.args_type_info_stack().Pop();
  113. auto class_id =
  114. context.node_stack().Pop<Parse::NodeKind::ClassDefinitionStart>();
  115. context.inst_block_stack().Pop();
  116. context.PopScope();
  117. context.decl_name_stack().PopScope();
  118. // The class type is now fully defined.
  119. auto& class_info = context.classes().Get(class_id);
  120. class_info.object_representation_id =
  121. context.CanonicalizeStructType(parse_node, fields_id);
  122. return true;
  123. }
  124. } // namespace Carbon::Check