name.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #include "toolchain/sem_ir/name.h"
  5. #include "llvm/ADT/StringSwitch.h"
  6. #include "toolchain/sem_ir/file.h"
  7. #include "toolchain/sem_ir/inst_profile.h"
  8. namespace Carbon::SemIR {
  9. // Get the spelling to use for a special name.
  10. static auto GetSpecialName(NameId name_id, bool for_ir) -> llvm::StringRef {
  11. switch (name_id.index) {
  12. case NameId::Invalid.index:
  13. return for_ir ? "" : "<invalid>";
  14. case NameId::SelfValue.index:
  15. return "self";
  16. case NameId::SelfType.index:
  17. return "Self";
  18. case NameId::ReturnSlot.index:
  19. return for_ir ? "return" : "<return slot>";
  20. case NameId::PackageNamespace.index:
  21. return "package";
  22. case NameId::Base.index:
  23. return "base";
  24. default:
  25. CARBON_FATAL() << "Unknown special name";
  26. }
  27. }
  28. auto NameStoreWrapper::GetFormatted(NameId name_id) const -> llvm::StringRef {
  29. // If the name is an identifier name with a keyword spelling, format it with
  30. // an `r#` prefix. Format any other identifier name as just the identifier.
  31. if (auto string_name = GetAsStringIfIdentifier(name_id)) {
  32. return llvm::StringSwitch<llvm::StringRef>(*string_name)
  33. #define CARBON_KEYWORD_TOKEN(Name, Spelling) .Case(Spelling, "r#" Spelling)
  34. #include "toolchain/lex/token_kind.def"
  35. .Default(*string_name);
  36. }
  37. return GetSpecialName(name_id, /*for_ir=*/false);
  38. }
  39. auto NameStoreWrapper::GetIRBaseName(NameId name_id) const -> llvm::StringRef {
  40. if (auto string_name = GetAsStringIfIdentifier(name_id)) {
  41. return *string_name;
  42. }
  43. return GetSpecialName(name_id, /*for_ir=*/true);
  44. }
  45. } // namespace Carbon::SemIR