impl.h 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. // Checks that any `require` declarations in the interface being implemented by
  55. // `impl` are satisfied. Otherwise, a diagnostic is issued and the `impl` is
  56. // made invalid.
  57. auto CheckRequireDeclsSatisfied(Context& context, SemIR::LocId loc_id,
  58. SemIR::Impl& impl) -> void;
  59. // Sets all unset members of the witness for `impl` to the error instruction and
  60. // sets the witness id in the `Impl` to an error.
  61. auto FillImplWitnessWithErrors(Context& context, SemIR::Impl& impl) -> void;
  62. // Returns whether the impl is either `final` explicitly, or implicitly due to
  63. // being concrete.
  64. auto IsImplEffectivelyFinal(Context& context, const SemIR::Impl& impl) -> bool;
  65. // Checks that `impl_function_id` is a valid implementation of the function
  66. // described in the interface as `interface_function_id`. Returns the value to
  67. // put into the corresponding slot in the witness table, which can be
  68. // `ErrorInst::InstId` if the function is not usable.
  69. auto CheckAssociatedFunctionImplementation(
  70. Context& context, SemIR::FunctionType interface_function_type,
  71. SemIR::SpecificId enclosing_specific_id, SemIR::InstId impl_decl_id,
  72. bool defer_thunk_definition) -> SemIR::InstId;
  73. // Checks that the constraint specified for the impl is valid and identified.
  74. // Returns the interface that the impl implements. On error, issues a diagnostic
  75. // and returns `None`.
  76. auto CheckConstraintIsInterface(Context& context, SemIR::InstId impl_decl_id,
  77. SemIR::InstId self_id,
  78. SemIR::TypeInstId constraint_id)
  79. -> SemIR::SpecificInterface;
  80. } // namespace Carbon::Check
  81. #endif // CARBON_TOOLCHAIN_CHECK_IMPL_H_