interface.cpp 1.4 KB

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