impl.h 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #ifndef CARBON_TOOLCHAIN_CHECK_IMPL_H_
  5. #define CARBON_TOOLCHAIN_CHECK_IMPL_H_
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. namespace Carbon::Check {
  9. struct RedeclaredImpl {
  10. // The previous Impl which the query Impl is redeclaring.
  11. SemIR::ImplId prev_impl_id;
  12. };
  13. struct NewImpl {
  14. // The lookup bucket for the query Impl where it should be added once an
  15. // ImplId is known.
  16. SemIR::ImplStore::LookupBucketRef lookup_bucket;
  17. // Indicates the query Impl is not a redeclaration but an error was diagnosed.
  18. // The caller should avoid diagnosing more errors in the query impl.
  19. bool find_had_error;
  20. };
  21. // Finds an existing impl if the `query_impl` is a redeclaration, and returns
  22. // its `ImplId`. This ensures all (valid) redeclarations share the same
  23. // `ImplId`. Otherwise, returns the bucket where a new `ImplId` should be added.
  24. auto FindImplId(Context& context, const SemIR::Impl& query_impl)
  25. -> std::variant<RedeclaredImpl, NewImpl>;
  26. // Adds an impl to the ImplStore, and returns a new `ImplId`.
  27. //
  28. // If the impl is modified with `extend` then the parent's scope is extended
  29. // with it.
  30. auto AddImpl(Context& context, const SemIR::Impl& impl,
  31. SemIR::ImplStore::LookupBucketRef lookup_bucket,
  32. Parse::NodeId extend_node, SemIR::LocId implicit_params_loc_id)
  33. -> SemIR::ImplId;
  34. // Creates and returns an impl witness instruction for an impl declaration.
  35. //
  36. // If there are no rewrites into a name of the interface being implemented, a
  37. // placeholder witness table is created, to be replaced in the impl definition.
  38. //
  39. // Adds and returns an `ImplWitness` instruction (created with location set to
  40. // `loc_id`) that shows the "`Self` type" (from a facet in `impl.self_id`)
  41. // implements an identified interface (from a facet type in
  42. // `impl.constraint_id`). This witness reflects the values assigned to
  43. // associated constant members of that interface by rewrite constraints in the
  44. // constraint facet type. `self_specific_id` will be the `specific_id` of the
  45. // resulting witness.
  46. auto AddImplWitnessForDeclaration(Context& context, SemIR::LocId loc_id,
  47. const SemIR::Impl& impl,
  48. SemIR::SpecificId self_specific_id)
  49. -> SemIR::InstId;
  50. // Update `impl`'s witness at the start of a definition.
  51. auto ImplWitnessStartDefinition(Context& context, SemIR::Impl& impl) -> void;
  52. // Adds the function members to the witness for `impl`.
  53. auto FinishImplWitness(Context& context, const SemIR::Impl& impl_id) -> void;
  54. // Sets all unset members of the witness for `impl` to the error instruction and
  55. // sets the witness id in the `Impl` to an error.
  56. auto FillImplWitnessWithErrors(Context& context, SemIR::Impl& impl) -> void;
  57. // Returns whether the impl is either `final` explicitly, or implicitly due to
  58. // being concrete.
  59. auto IsImplEffectivelyFinal(Context& context, const SemIR::Impl& impl) -> bool;
  60. // Checks that `impl_function_id` is a valid implementation of the function
  61. // described in the interface as `interface_function_id`. Returns the value to
  62. // put into the corresponding slot in the witness table, which can be
  63. // `ErrorInst::InstId` if the function is not usable.
  64. auto CheckAssociatedFunctionImplementation(
  65. Context& context, SemIR::FunctionType interface_function_type,
  66. SemIR::InstId impl_decl_id, SemIR::TypeId self_type_id,
  67. SemIR::InstId witness_inst_id, bool defer_thunk_definition)
  68. -> SemIR::InstId;
  69. // Checks that the constraint specified for the impl is valid and identified.
  70. // Returns the interface that the impl implements. On error, issues a diagnostic
  71. // and returns `None`.
  72. auto CheckConstraintIsInterface(Context& context, SemIR::InstId impl_decl_id,
  73. SemIR::TypeInstId constraint_id)
  74. -> SemIR::SpecificInterface;
  75. } // namespace Carbon::Check
  76. #endif // CARBON_TOOLCHAIN_CHECK_IMPL_H_