mangler.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_(file_context.cpp_ast()
  23. // Clang's createMangleContext is not
  24. // const-correct, but doesn't modify the AST.
  25. ? const_cast<clang::ASTContext&>(
  26. file_context.cpp_ast()->getASTContext())
  27. .createMangleContext()
  28. : nullptr) {}
  29. // Produce a deterministically unique mangled name for the function specified
  30. // by `function_id` and `specific_id`.
  31. auto Mangle(SemIR::FunctionId function_id, SemIR::SpecificId specific_id)
  32. -> std::string;
  33. // Produce a deterministically unique mangled name for the given global
  34. // variable pattern, or an empty string if the variable doesn't bind any
  35. // names, in which case it can't be referenced from another file and should be
  36. // given internal linkage.
  37. auto MangleGlobalVariable(SemIR::InstId pattern_id) -> std::string;
  38. // Produce a deterministically unique mangled name for the specified class's
  39. // vtable.
  40. auto MangleVTable(const SemIR::Class& class_info) -> std::string;
  41. private:
  42. // Mangle this `NameId` as an individual name component.
  43. auto MangleNameId(llvm::raw_ostream& os, SemIR::NameId name_id) -> void;
  44. // Mangle this qualified name with inner scope first, working outwards. This
  45. // may reduce the incidence of common prefixes in the name mangling. (i.e.:
  46. // every standard library name won't have a common prefix that has to be
  47. // skipped and compared before getting to the interesting part)
  48. auto MangleInverseQualifiedNameScope(llvm::raw_ostream& os,
  49. SemIR::NameScopeId name_scope_id)
  50. -> void;
  51. // Generates a mangled name using Clang mangling for imported C++ functions.
  52. auto MangleCppClang(const clang::NamedDecl* decl) -> std::string;
  53. auto sem_ir() const -> const SemIR::File& { return file_context_.sem_ir(); }
  54. auto names() const -> SemIR::NameStoreWrapper { return sem_ir().names(); }
  55. auto insts() const -> const SemIR::InstStore& { return sem_ir().insts(); }
  56. auto types() const -> const SemIR::TypeStore& { return sem_ir().types(); }
  57. auto constant_values() const -> const SemIR::ConstantValueStore& {
  58. return sem_ir().constant_values();
  59. }
  60. FileContext& file_context_;
  61. // TODO: If `file_context_` has an `InstNamer`, we could share its
  62. // fingerprinter.
  63. SemIR::InstFingerprinter fingerprinter_;
  64. // Clang Mangler lazily initialized when necessary. We create it once under
  65. // the assumption all declarations we need to mangle can use the same Mangler
  66. // (same AST Context).
  67. std::unique_ptr<clang::MangleContext> cpp_mangle_context_;
  68. };
  69. } // namespace Carbon::Lower
  70. #endif // CARBON_TOOLCHAIN_LOWER_MANGLER_H_