generic.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_SEM_IR_GENERIC_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_GENERIC_H_
  6. #include "common/set.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. namespace Carbon::SemIR {
  9. // Information for a generic entity, such as a generic class, a generic
  10. // interface, or generic function.
  11. //
  12. // Note that this includes both checked generics and template generics.
  13. struct Generic : public Printable<Generic> {
  14. auto Print(llvm::raw_ostream& out) const -> void {
  15. out << "{decl: " << decl_id << ", bindings: " << bindings_id << "}";
  16. }
  17. // The following members always have values, and do not change throughout the
  18. // lifetime of the generic.
  19. // The first declaration of the generic entity.
  20. InstId decl_id;
  21. // A block containing the IDs of compile time bindings in this generic scope.
  22. // The index in this block will match the `bind_index` in the name binding
  23. // instruction's `BindNameInfo`.
  24. InstBlockId bindings_id;
  25. };
  26. // An instance of a generic entity, such as an instance of a generic function.
  27. // For each construct that depends on a compile-time parameter in the generic
  28. // entity, this contains the corresponding non-generic value. This includes
  29. // values for the compile-time parameters themselves.
  30. struct GenericInstance : Printable<GenericInstance> {
  31. auto Print(llvm::raw_ostream& out) const -> void {
  32. out << "{generic: " << generic_id << ", args: " << args_id << "}";
  33. }
  34. // The generic that this is an instance of.
  35. GenericId generic_id;
  36. // Argument values, corresponding to the bindings in `Generic::bindings_id`.
  37. InstBlockId args_id;
  38. };
  39. // Provides storage for deduplicated instances of generics.
  40. class GenericInstanceStore : public Yaml::Printable<GenericInstanceStore> {
  41. public:
  42. // Adds a new generic instance, or gets the existing generic instance for a
  43. // specified generic and argument list. Returns the ID of the generic
  44. // instance. The argument IDs must be for instructions in the constant block,
  45. // and must be a canonical instruction block ID.
  46. auto GetOrAdd(GenericId generic_id, InstBlockId args_id) -> GenericInstanceId;
  47. // Gets the specified generic instance.
  48. auto Get(GenericInstanceId instance_id) const -> const GenericInstance& {
  49. return generic_instances_.Get(instance_id);
  50. }
  51. // Gets the specified generic instance.
  52. auto Get(GenericInstanceId instance_id) -> GenericInstance& {
  53. return generic_instances_.Get(instance_id);
  54. }
  55. // These are to support printable structures, and are not guaranteed.
  56. auto OutputYaml() const -> Yaml::OutputMapping {
  57. return generic_instances_.OutputYaml();
  58. }
  59. private:
  60. // Context for hashing keys.
  61. class KeyContext;
  62. ValueStore<GenericInstanceId> generic_instances_;
  63. Carbon::Set<GenericInstanceId, 0, KeyContext> lookup_table_;
  64. };
  65. } // namespace Carbon::SemIR
  66. #endif // CARBON_TOOLCHAIN_SEM_IR_GENERIC_H_