mangler.h 2.0 KB

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