name.cpp 1.7 KB

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