core_identifier.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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_CORE_IDENTIFIER_H_
  5. #define CARBON_TOOLCHAIN_CHECK_CORE_IDENTIFIER_H_
  6. #include "common/enum_base.h"
  7. #include "toolchain/base/shared_value_stores.h"
  8. #include "toolchain/base/value_ids.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. namespace Carbon::Check {
  11. CARBON_DEFINE_RAW_ENUM_CLASS(CoreIdentifier, uint8_t) {
  12. #define CARBON_CORE_IDENTIFIER(Name) CARBON_RAW_ENUM_ENUMERATOR(Name)
  13. #include "toolchain/check/core_identifier.def"
  14. };
  15. // An identifier in `Core` that's significant for the language (typically used
  16. // by desugaring) but not a builtin or keyword. Note this includes non-top-level
  17. // identifiers, such as both `AddWith` and `Op` in `Core.AddWith.Op`.
  18. class CoreIdentifier : public CARBON_ENUM_BASE(CoreIdentifier) {
  19. public:
  20. #define CARBON_CORE_IDENTIFIER(Name) CARBON_ENUM_CONSTANT_DECL(Name)
  21. #include "toolchain/check/core_identifier.def"
  22. private:
  23. // Exposes `AsInt`.
  24. friend class CoreIdentifierCache;
  25. };
  26. #define CARBON_CORE_IDENTIFIER(Name) \
  27. CARBON_ENUM_CONSTANT_DEFINITION(CoreIdentifier, Name)
  28. #include "toolchain/check/core_identifier.def"
  29. // A cache of added `Core` identifiers. These are added to the identifier
  30. // store on first use.
  31. class CoreIdentifierCache {
  32. public:
  33. explicit CoreIdentifierCache(SharedValueStores::IdentifierStore* identifiers)
  34. : identifiers_(identifiers) {}
  35. // Returns the `NameId` for a `CoreIdentifier`.
  36. auto AddNameId(CoreIdentifier identifier) -> SemIR::NameId {
  37. auto& value = cache_[identifier.AsInt()];
  38. if (!value.has_value()) {
  39. value =
  40. SemIR::NameId::ForIdentifier(identifiers_->Add(identifier.name()));
  41. }
  42. return value;
  43. }
  44. private:
  45. // The number of cache entries.
  46. static constexpr int CacheSize = 0
  47. #define CARBON_CORE_IDENTIFIER(Name) +1
  48. #include "toolchain/check/core_identifier.def"
  49. ;
  50. // A pointer for adding identifiers.
  51. SharedValueStores::IdentifierStore* identifiers_;
  52. // The cache of added identifiers. These are stored as a `NameId` because the
  53. // `IdentifierId` isn't directly used.
  54. SemIR::NameId cache_[CacheSize] = {
  55. #define CARBON_CORE_IDENTIFIER(Name) SemIR::NameId::None,
  56. #include "toolchain/check/core_identifier.def"
  57. };
  58. };
  59. } // namespace Carbon::Check
  60. #endif // CARBON_TOOLCHAIN_CHECK_CORE_IDENTIFIER_H_