name.cpp 1.8 KB

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