mangler.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "clang/AST/Mangle.h"
  8. #include "toolchain/lower/file_context.h"
  9. #include "toolchain/sem_ir/constant.h"
  10. #include "toolchain/sem_ir/ids.h"
  11. #include "toolchain/sem_ir/inst_fingerprinter.h"
  12. namespace Carbon::Lower {
  13. // A class for producing mangled (deterministically unique, at least partially
  14. // human readable) names for externally referenceable entities such as
  15. // functions.
  16. class Mangler {
  17. public:
  18. // Initialize a new Mangler instance for mangling entities within the
  19. // specified `FileContext`.
  20. explicit Mangler(FileContext& file_context)
  21. : file_context_(file_context),
  22. cpp_mangle_context_(
  23. file_context.cpp_ast()
  24. ? file_context.cpp_ast()->getASTContext().createMangleContext()
  25. : nullptr) {}
  26. // Produce a deterministically unique mangled name for the function specified
  27. // by `function_id` and `specific_id`.
  28. auto Mangle(SemIR::FunctionId function_id, SemIR::SpecificId specific_id)
  29. -> std::string;
  30. // Produce a deterministically unique mangled name for the specified class's
  31. // vtable.
  32. auto MangleVTable(const SemIR::Class& class_info) -> std::string;
  33. private:
  34. // Mangle this qualified name with inner scope first, working outwards. This
  35. // may reduce the incidence of common prefixes in the name mangling. (i.e.:
  36. // every standard library name won't have a common prefix that has to be
  37. // skipped and compared before getting to the interesting part)
  38. auto MangleInverseQualifiedNameScope(llvm::raw_ostream& os,
  39. SemIR::NameScopeId name_scope_id)
  40. -> void;
  41. // Generates a mangled name using Clang mangling for imported C++ functions.
  42. auto MangleCppClang(const clang::NamedDecl* decl) -> std::string;
  43. auto sem_ir() const -> const SemIR::File& { return file_context_.sem_ir(); }
  44. auto names() const -> SemIR::NameStoreWrapper { return sem_ir().names(); }
  45. auto insts() const -> const SemIR::InstStore& { return sem_ir().insts(); }
  46. auto types() const -> const SemIR::TypeStore& { return sem_ir().types(); }
  47. auto constant_values() const -> const SemIR::ConstantValueStore& {
  48. return sem_ir().constant_values();
  49. }
  50. FileContext& file_context_;
  51. // TODO: If `file_context_` has an `InstNamer`, we could share its
  52. // fingerprinter.
  53. SemIR::InstFingerprinter fingerprinter_;
  54. // Clang Mangler lazily initialized when necessary. We create it once under
  55. // the assumption all declarations we need to mangle can use the same Mangler
  56. // (same AST Context).
  57. std::unique_ptr<clang::MangleContext> cpp_mangle_context_;
  58. };
  59. } // namespace Carbon::Lower
  60. #endif // CARBON_TOOLCHAIN_LOWER_MANGLER_H_