Repository: carbon-language/carbon-lang
File: toolchain/check/import_cpp.cpp (C++)

Code before:
       .pattern_block_id = SemIR::InstBlockId::None,
       .implicit_param_patterns_id = SemIR::InstBlockId::None,
       .param_patterns_id = SemIR::InstBlockId::None,
       .is_extern = false,
       .extern_library_id = SemIR::LibraryNameId::None,
       .non_owning_decl_id = SemIR::InstId::None,
       .first_owning_decl_id = class_decl_id},
      {// `.self_type_id` depends on the ClassType, so is set below.
       .self_type_id = SemIR::TypeId::None,
       // TODO: Support Dynamic classes.
       // TODO: Support Final classes.
       .inheritance_kind = SemIR::Class::Base}};

  class_decl.class_id = context.classes().Add(class_info);

  // Write the class ID into the ClassDecl.
  ReplaceInstBeforeConstantUse(context, class_decl_id, class_decl);

  SetClassSelfType(context, class_decl.class_id);

  return {class_decl.class_id, class_decl_id};
}

// Creates a class definition for the given based on the information in the
// given Clang declaration, which is assumed to be for a class definition.
// Returns the new class id and instruction id.
static auto BuildClassDefinition(Context& context,
                                 clang::CXXRecordDecl* clang_decl)
    -> std::tuple<SemIR::ClassId, SemIR::InstId> {
  auto [class_id, class_inst_id] =
      BuildClassDecl(context, GetParentNameScopeId(context, clang_decl),
                     AddIdentifierName(context, clang_decl->getName()));
  auto& class_info = context.classes().Get(class_id);
  StartClassDefinition(context, class_info, class_inst_id);

  context.name_scopes()
      .Get(class_info.scope_id)
      .set_clang_decl_context_id(context.sem_ir().clang_decls().Add(
          {.decl = clang_decl, .inst_id = class_inst_id}));

  return {class_id, class_inst_id};
}

// Mark the given `Decl` as failed in `clang_decls`.
static auto MarkFailedDecl(Context& context, clang::Decl* clang_decl) {
  context.sem_ir().clang_decls().Add(
      {.decl = clang_decl, .inst_id = SemIR::ErrorInst::InstId});
}

// Imports a record declaration from Clang to Carbon. If successful, returns
// the new Carbon class declaration `InstId`.

Review comment (suggestion):
```suggestion
// Creates a class definition based on the information in the
// given Clang declaration, which is assumed to be for a class definition.
// Returns the new class id and instruction id.
```

Code after:
       .pattern_block_id = SemIR::InstBlockId::None,
       .implicit_param_patterns_id = SemIR::InstBlockId::None,
       .param_patterns_id = SemIR::InstBlockId::None,
       .is_extern = false,
       .extern_library_id = SemIR::LibraryNameId::None,
       .non_owning_decl_id = SemIR::InstId::None,
       .first_owning_decl_id = class_decl_id},
      {// `.self_type_id` depends on the ClassType, so is set below.
       .self_type_id = SemIR::TypeId::None,
       // TODO: Support Dynamic classes.
       // TODO: Support Final classes.
       .inheritance_kind = SemIR::Class::Base}};

  class_decl.class_id = context.classes().Add(class_info);

  // Write the class ID into the ClassDecl.
  ReplaceInstBeforeConstantUse(context, class_decl_id, class_decl);

  SetClassSelfType(context, class_decl.class_id);

  return {class_decl.class_id, class_decl_id};
}

// Creates a class definition based on the information in the given Clang
// declaration, which is assumed to be for a class definition. Returns the new
// class id and instruction id.
static auto BuildClassDefinition(Context& context,
                                 clang::CXXRecordDecl* clang_decl)
    -> std::tuple<SemIR::ClassId, SemIR::InstId> {
  auto [class_id, class_inst_id] =
      BuildClassDecl(context, GetParentNameScopeId(context, clang_decl),
                     AddIdentifierName(context, clang_decl->getName()));
  auto& class_info = context.classes().Get(class_id);
  StartClassDefinition(context, class_info, class_inst_id);

  context.name_scopes()
      .Get(class_info.scope_id)
      .set_clang_decl_context_id(context.sem_ir().clang_decls().Add(
          {.decl = clang_decl, .inst_id = class_inst_id}));

  return {class_id, class_inst_id};
}

// Mark the given `Decl` as failed in `clang_decls`.
static auto MarkFailedDecl(Context& context, clang::Decl* clang_decl) {
  context.sem_ir().clang_decls().Add(
      {.decl = clang_decl, .inst_id = SemIR::ErrorInst::InstId});
}

// Imports a record declaration from Clang to Carbon. If successful, returns
// the new Carbon class declaration `InstId`.