interface.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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/interface.h"
  5. #include "toolchain/check/context.h"
  6. #include "toolchain/sem_ir/ids.h"
  7. #include "toolchain/sem_ir/inst.h"
  8. #include "toolchain/sem_ir/typed_insts.h"
  9. namespace Carbon::Check {
  10. auto BuildAssociatedEntity(Context& context, SemIR::InterfaceId interface_id,
  11. SemIR::InstId decl_id) -> SemIR::InstId {
  12. auto& interface_info = context.interfaces().Get(interface_id);
  13. if (!interface_info.is_being_defined()) {
  14. // This should only happen if the interface is erroneously defined more than
  15. // once.
  16. // TODO: Find a way to CHECK this.
  17. return SemIR::InstId::BuiltinError;
  18. }
  19. // The interface type is the type of `Self`.
  20. auto self_type_id =
  21. context.insts().Get(interface_info.self_param_id).type_id();
  22. // Register this declaration as declaring an associated entity.
  23. auto index = SemIR::ElementIndex(
  24. context.args_type_info_stack().PeekCurrentBlockContents().size());
  25. context.args_type_info_stack().AddInstId(decl_id);
  26. // Name lookup for the declaration's name should name the associated entity,
  27. // not the declaration itself.
  28. auto type_id = context.GetAssociatedEntityType(
  29. self_type_id, context.insts().Get(decl_id).type_id());
  30. return context.AddInstReusingLoc<SemIR::AssociatedEntity>(
  31. context.insts().GetLocId(decl_id),
  32. {.type_id = type_id, .index = index, .decl_id = decl_id});
  33. }
  34. } // namespace Carbon::Check