generic.h 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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, SemIRLoc loc, 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, SemIRLoc loc,
  46. SemIR::GenericId generic_id,
  47. SemIR::InstBlockId args_id)
  48. -> SemIR::SpecificId {
  49. return generic_id.is_valid() ? MakeSpecific(context, loc, generic_id, args_id)
  50. : SemIR::SpecificId::Invalid;
  51. }
  52. // Builds the specific that describes how the generic should refer to itself.
  53. // For example, for a generic `G(T:! type)`, this is the specific `G(T)`. For an
  54. // invalid `generic_id`, returns an invalid specific ID.
  55. auto MakeSelfSpecific(Context& context, SemIRLoc loc,
  56. SemIR::GenericId generic_id) -> SemIR::SpecificId;
  57. // Attempts to resolve the definition of the given specific, by evaluating the
  58. // eval block of the corresponding generic and storing a corresponding value
  59. // block in the specific. Returns false if a definition is not available.
  60. auto ResolveSpecificDefinition(Context& context, SemIRLoc loc,
  61. SemIR::SpecificId specific_id) -> bool;
  62. // Returns an instruction describing the entity named by the given specific.
  63. auto GetInstForSpecific(Context& context, SemIR::SpecificId specific_id)
  64. -> SemIR::InstId;
  65. } // namespace Carbon::Check
  66. #endif // CARBON_TOOLCHAIN_CHECK_GENERIC_H_