mangler.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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_LOWER_MANGLER_H_
  5. #define CARBON_TOOLCHAIN_LOWER_MANGLER_H_
  6. #include <string>
  7. #include "toolchain/lower/file_context.h"
  8. #include "toolchain/sem_ir/ids.h"
  9. namespace Carbon::Lower {
  10. // A class for producing mangled (deterministically unique, at least partially
  11. // human readable) names for externally referenceable entities such as
  12. // functions.
  13. class Mangler {
  14. public:
  15. // Initialize a new Mangler instance for mangling entities within the
  16. // specified `FileContext`.
  17. explicit Mangler(FileContext& file_context) : file_context_(file_context) {}
  18. // Produce a deterministically unique mangled name for the function specified
  19. // by `function_id`.
  20. auto Mangle(SemIR::FunctionId function_id) -> std::string;
  21. private:
  22. // Mangle this qualified name with inner scope first, working outwards. This
  23. // may reduce the incidence of common prefixes in the name mangling. (i.e.:
  24. // every standard library name won't have a common prefix that has to be
  25. // skipped and compared before getting to the interesting part)
  26. auto MangleInverseQualifiedNameScope(llvm::raw_ostream& os,
  27. SemIR::NameScopeId name_scope_id)
  28. -> void;
  29. auto sem_ir() const -> const SemIR::File& { return file_context_.sem_ir(); }
  30. auto names() const -> SemIR::NameStoreWrapper { return sem_ir().names(); }
  31. auto types() const -> SemIR::TypeStore { return sem_ir().types(); }
  32. FileContext& file_context_;
  33. };
  34. } // namespace Carbon::Lower
  35. #endif // CARBON_TOOLCHAIN_LOWER_MANGLER_H_