interface.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. // Register this declaration as declaring an associated entity.
  20. auto index = SemIR::ElementIndex(
  21. context.args_type_info_stack().PeekCurrentBlockContents().size());
  22. context.args_type_info_stack().AddInstId(decl_id);
  23. // Name lookup for the declaration's name should name the associated entity,
  24. // not the declaration itself.
  25. auto type_id = context.GetAssociatedEntityType(
  26. interface_id, context.insts().Get(decl_id).type_id());
  27. return context.AddInst<SemIR::AssociatedEntity>(
  28. context.insts().GetLocId(decl_id),
  29. {.type_id = type_id, .index = index, .decl_id = decl_id});
  30. }
  31. } // namespace Carbon::Check