mangler.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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) : file_context_(file_context) {}
  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. // Generates a mangled name using Clang mangling for imported C++ functions.
  48. auto MangleCppClang(const clang::NamedDecl* decl) -> std::string;
  49. auto sem_ir() const -> const SemIR::File& { return file_context_.sem_ir(); }
  50. auto names() const -> SemIR::NameStoreWrapper { return sem_ir().names(); }
  51. auto insts() const -> const SemIR::InstStore& { return sem_ir().insts(); }
  52. auto types() const -> const SemIR::TypeStore& { return sem_ir().types(); }
  53. auto constant_values() const -> const SemIR::ConstantValueStore& {
  54. return sem_ir().constant_values();
  55. }
  56. FileContext& file_context_;
  57. // TODO: If `file_context_` has an `InstNamer`, we could share its
  58. // fingerprinter.
  59. SemIR::InstFingerprinter fingerprinter_;
  60. };
  61. } // namespace Carbon::Lower
  62. #endif // CARBON_TOOLCHAIN_LOWER_MANGLER_H_