mangler.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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_SEM_IR_MANGLER_H_
  5. #define CARBON_TOOLCHAIN_SEM_IR_MANGLER_H_
  6. #include <string>
  7. #include "clang/AST/Mangle.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::SemIR {
  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 `File`.
  19. Mangler(const SemIR::File& sem_ir, int total_ir_count)
  20. : sem_ir_(sem_ir), fingerprinter_(total_ir_count) {}
  21. // Produce a deterministically unique mangled name for the function specified
  22. // by `function_id` and `specific_id`.
  23. auto Mangle(SemIR::FunctionId function_id, SemIR::SpecificId specific_id)
  24. -> std::string;
  25. // Produce a deterministically unique mangled name for the given global
  26. // variable pattern, or an empty string if the variable doesn't bind any
  27. // names, in which case it can't be referenced from another file and should be
  28. // given internal linkage.
  29. auto MangleGlobalVariable(SemIR::InstId pattern_id) -> std::string;
  30. // Produce a deterministically unique mangled name for the specified class's
  31. // vtable.
  32. auto MangleVTable(const SemIR::Class& class_info,
  33. SemIR::SpecificId specific_id) -> std::string;
  34. private:
  35. // Mangle this `NameId` as an individual name component.
  36. auto MangleNameId(llvm::raw_ostream& os, SemIR::NameId name_id) -> void;
  37. // Mangle this `SpecificId`, or nothing if it is `SpecificId::None`.
  38. auto MangleSpecificId(llvm::raw_ostream& os, SemIR::SpecificId specific_id)
  39. -> void;
  40. // Mangle this qualified name with inner scope first, working outwards. This
  41. // may reduce the incidence of common prefixes in the name mangling. (i.e.:
  42. // every standard library name won't have a common prefix that has to be
  43. // skipped and compared before getting to the interesting part)
  44. auto MangleInverseQualifiedNameScope(llvm::raw_ostream& os,
  45. SemIR::NameScopeId name_scope_id)
  46. -> void;
  47. // Mangle the unqualified name of the specified `Class`.
  48. auto MangleUnqualifiedClass(llvm::raw_ostream& os,
  49. const SemIR::Class& class_info,
  50. SemIR::SpecificId specific_id) -> 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 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. const SemIR::File& sem_ir_;
  61. SemIR::InstFingerprinter fingerprinter_;
  62. };
  63. } // namespace Carbon::SemIR
  64. #endif // CARBON_TOOLCHAIN_SEM_IR_MANGLER_H_