generic.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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_GENERIC_H_
  5. #define CARBON_TOOLCHAIN_CHECK_GENERIC_H_
  6. #include "toolchain/check/context.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. namespace Carbon::Check {
  9. // Start processing a declaration or definition that might be a generic entity.
  10. auto StartGenericDecl(Context& context) -> void;
  11. // Start processing a declaration or definition that might be a generic entity.
  12. auto StartGenericDefinition(Context& context) -> void;
  13. // Discard the information about the current generic entity. This should be
  14. // called instead of `FinishGenericDecl` if the corresponding `Generic` object
  15. // would not actually be used, or when recovering from an error.
  16. auto DiscardGenericDecl(Context& context) -> void;
  17. // Finish processing a potentially generic declaration and produce a
  18. // corresponding generic object. Returns SemIR::GenericId::Invalid if this
  19. // declaration is not actually generic.
  20. auto FinishGenericDecl(Context& context, SemIR::InstId decl_id)
  21. -> SemIR::GenericId;
  22. // Merge a redeclaration of an entity that might be a generic into the original
  23. // declaration.
  24. auto FinishGenericRedecl(Context& context, SemIR::InstId decl_id,
  25. SemIR::GenericId generic_id) -> void;
  26. // Finish processing a potentially generic definition.
  27. auto FinishGenericDefinition(Context& context, SemIR::GenericId generic_id)
  28. -> void;
  29. // Builds and returns an eval block, given the list of canonical symbolic
  30. // constants that the instructions in the eval block should produce. This is
  31. // used when importing a generic.
  32. auto RebuildGenericEvalBlock(Context& context, SemIR::GenericId generic_id,
  33. SemIR::GenericInstIndex::Region region,
  34. llvm::ArrayRef<SemIR::InstId> const_ids)
  35. -> SemIR::InstBlockId;
  36. // Builds a new specific, or finds an existing one if this generic has already
  37. // been referenced with these arguments. Performs substitution into the
  38. // declaration, but not the definition, of the generic.
  39. //
  40. // `args_id` should be a canonical instruction block referring to constants.
  41. auto MakeSpecific(Context& context, SemIR::GenericId generic_id,
  42. SemIR::InstBlockId args_id) -> SemIR::SpecificId;
  43. // Builds a new specific if the given generic is valid. Otherwise returns an
  44. // invalid specific.
  45. inline auto MakeSpecificIfGeneric(Context& context, SemIR::GenericId generic_id,
  46. SemIR::InstBlockId args_id)
  47. -> SemIR::SpecificId {
  48. return generic_id.is_valid() ? MakeSpecific(context, generic_id, args_id)
  49. : SemIR::SpecificId::Invalid;
  50. }
  51. // Builds the specific that describes how the generic should refer to itself.
  52. // For example, for a generic `G(T:! type)`, this is the specific `G(T)`. For an
  53. // invalid `generic_id`, returns an invalid specific ID.
  54. auto MakeSelfSpecific(Context& context, SemIR::GenericId generic_id)
  55. -> SemIR::SpecificId;
  56. // Attempts to resolve the definition of the given specific, by evaluating the
  57. // eval block of the corresponding generic and storing a corresponding value
  58. // block in the specific. Returns false if a definition is not available.
  59. auto ResolveSpecificDefinition(Context& context, SemIR::SpecificId specific_id)
  60. -> bool;
  61. } // namespace Carbon::Check
  62. #endif // CARBON_TOOLCHAIN_CHECK_GENERIC_H_