generic.h 4.1 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_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 BuildGeneric(Context& context, SemIR::InstId decl_id) -> SemIR::GenericId;
  21. // Builds eval block for the declaration.
  22. auto FinishGenericDecl(Context& context, SemIRLoc loc,
  23. SemIR::GenericId generic_id) -> void;
  24. // BuildGeneric() and FinishGenericDecl() combined. Normally you would call this
  25. // function unless the caller has work to do between the two steps.
  26. auto BuildGenericDecl(Context& context, SemIR::InstId decl_id)
  27. -> SemIR::GenericId;
  28. // Merge a redeclaration of an entity that might be a generic into the original
  29. // declaration.
  30. auto FinishGenericRedecl(Context& context, SemIR::InstId decl_id,
  31. SemIR::GenericId generic_id) -> void;
  32. // Finish processing a potentially generic definition.
  33. auto FinishGenericDefinition(Context& context, SemIR::GenericId generic_id)
  34. -> void;
  35. // Builds and returns an eval block, given the list of canonical symbolic
  36. // constants that the instructions in the eval block should produce. This is
  37. // used when importing a generic.
  38. auto RebuildGenericEvalBlock(Context& context, SemIR::GenericId generic_id,
  39. SemIR::GenericInstIndex::Region region,
  40. llvm::ArrayRef<SemIR::InstId> const_ids)
  41. -> SemIR::InstBlockId;
  42. // Builds a new specific, or finds an existing one if this generic has already
  43. // been referenced with these arguments. Performs substitution into the
  44. // declaration, but not the definition, of the generic.
  45. //
  46. // `args_id` should be a canonical instruction block referring to constants.
  47. auto MakeSpecific(Context& context, SemIRLoc loc, SemIR::GenericId generic_id,
  48. SemIR::InstBlockId args_id) -> SemIR::SpecificId;
  49. // Builds a new specific if the given generic is valid. Otherwise returns an
  50. // invalid specific.
  51. inline auto MakeSpecificIfGeneric(Context& context, SemIRLoc loc,
  52. SemIR::GenericId generic_id,
  53. SemIR::InstBlockId args_id)
  54. -> SemIR::SpecificId {
  55. return generic_id.is_valid() ? MakeSpecific(context, loc, generic_id, args_id)
  56. : SemIR::SpecificId::Invalid;
  57. }
  58. // Builds the specific that describes how the generic should refer to itself.
  59. // For example, for a generic `G(T:! type)`, this is the specific `G(T)`. For an
  60. // invalid `generic_id`, returns an invalid specific ID.
  61. auto MakeSelfSpecific(Context& context, SemIRLoc loc,
  62. SemIR::GenericId generic_id) -> SemIR::SpecificId;
  63. // Attempts to resolve the definition of the given specific, by evaluating the
  64. // eval block of the corresponding generic and storing a corresponding value
  65. // block in the specific. Returns false if a definition is not available.
  66. auto ResolveSpecificDefinition(Context& context, SemIRLoc loc,
  67. SemIR::SpecificId specific_id) -> bool;
  68. // Returns an instruction describing the entity named by the given specific.
  69. auto GetInstForSpecific(Context& context, SemIR::SpecificId specific_id)
  70. -> SemIR::InstId;
  71. } // namespace Carbon::Check
  72. #endif // CARBON_TOOLCHAIN_CHECK_GENERIC_H_