name.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. namespace Carbon::SemIR {
  7. // Get the spelling to use for a special name.
  8. static auto GetSpecialName(NameId name_id, bool for_ir) -> llvm::StringRef {
  9. switch (name_id.index) {
  10. case NameId::None.index:
  11. return for_ir ? "" : "<none>";
  12. case NameId::Base.index:
  13. return "base";
  14. case NameId::Core.index:
  15. return "Core";
  16. case NameId::PeriodSelf.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::SelfType.index:
  23. return "Self";
  24. case NameId::SelfValue.index:
  25. return "self";
  26. case NameId::Vptr.index:
  27. return for_ir ? "vptr" : "<vptr>";
  28. case NameId::ChoiceDiscriminant.index:
  29. return "discriminant";
  30. default:
  31. CARBON_FATAL("Unknown special name");
  32. }
  33. }
  34. auto NameStoreWrapper::GetFormatted(NameId name_id) const -> llvm::StringRef {
  35. // If the name is an identifier name with a keyword spelling, format it with
  36. // an `r#` prefix. Format any other identifier name as just the identifier.
  37. if (auto string_name = GetAsStringIfIdentifier(name_id)) {
  38. return llvm::StringSwitch<llvm::StringRef>(*string_name)
  39. #define CARBON_KEYWORD_TOKEN(Name, Spelling) .Case(Spelling, "r#" Spelling)
  40. #include "toolchain/lex/token_kind.def"
  41. .Default(*string_name);
  42. }
  43. return GetSpecialName(name_id, /*for_ir=*/false);
  44. }
  45. auto NameStoreWrapper::GetIRBaseName(NameId name_id) const -> llvm::StringRef {
  46. if (auto string_name = GetAsStringIfIdentifier(name_id)) {
  47. return *string_name;
  48. }
  49. return GetSpecialName(name_id, /*for_ir=*/true);
  50. }
  51. } // namespace Carbon::SemIR