generic.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. // Finish processing a potentially generic declaration and produce a
  14. // corresponding generic object. Returns SemIR::GenericId::Invalid if this
  15. // declaration is not actually generic.
  16. auto FinishGenericDecl(Context& context, SemIR::InstId decl_id)
  17. -> SemIR::GenericId;
  18. // Merge a redeclaration of an entity that might be a generic into the original
  19. // declaration.
  20. auto FinishGenericRedecl(Context& context, SemIR::InstId decl_id,
  21. SemIR::GenericId generic_id) -> void;
  22. // Finish processing a potentially generic definition.
  23. auto FinishGenericDefinition(Context& context, SemIR::GenericId generic_id)
  24. -> void;
  25. // Builds and returns an eval block, given the list of canonical symbolic
  26. // constants that the instructions in the eval block should produce. This is
  27. // used when importing a generic.
  28. auto RebuildGenericEvalBlock(Context& context, SemIR::GenericId generic_id,
  29. SemIR::GenericInstIndex::Region region,
  30. llvm::ArrayRef<SemIR::InstId> const_ids)
  31. -> SemIR::InstBlockId;
  32. // Builds a new specific, or finds an existing one if this generic has already
  33. // been referenced with these arguments. Performs substitution into the
  34. // declaration, but not the definition, of the generic.
  35. //
  36. // `args_id` should be a canonical instruction block referring to constants.
  37. auto MakeSpecific(Context& context, SemIR::GenericId generic_id,
  38. SemIR::InstBlockId args_id) -> SemIR::SpecificId;
  39. // Builds a new specific if the given generic is valid. Otherwise returns an
  40. // invalid specific.
  41. inline auto MakeSpecificIfGeneric(Context& context, SemIR::GenericId generic_id,
  42. SemIR::InstBlockId args_id)
  43. -> SemIR::SpecificId {
  44. return generic_id.is_valid() ? MakeSpecific(context, generic_id, args_id)
  45. : SemIR::SpecificId::Invalid;
  46. }
  47. // Builds the specific that describes how the generic should refer to itself.
  48. // For example, for a generic `G(T:! type)`, this is the specific `G(T)`. For an
  49. // invalid `generic_id`, returns an invalid specific ID.
  50. auto MakeSelfSpecific(Context& context, SemIR::GenericId generic_id)
  51. -> SemIR::SpecificId;
  52. // Attempts to resolve the definition of the given specific, by evaluating the
  53. // eval block of the corresponding generic and storing a corresponding value
  54. // block in the specific. Returns false if a definition is not available.
  55. auto ResolveSpecificDefinition(Context& context, SemIR::SpecificId specific_id)
  56. -> bool;
  57. // Requires that a param block only contains generics. Diagnoses and updates the
  58. // block otherwise. This will typically be called once for each of implicit and
  59. // explicit parameters, and must occur before constant evaluation of the
  60. // parameterized instruction.
  61. auto RequireGenericParams(Context& context, SemIR::InstBlockId block_id)
  62. -> void;
  63. } // namespace Carbon::Check
  64. #endif // CARBON_TOOLCHAIN_CHECK_GENERIC_H_