mangler.h 2.2 KB

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