mangler.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #include "toolchain/lower/mangler.h"
  5. #include <string>
  6. #include "common/raw_string_ostream.h"
  7. #include "toolchain/base/kind_switch.h"
  8. #include "toolchain/sem_ir/entry_point.h"
  9. #include "toolchain/sem_ir/ids.h"
  10. namespace Carbon::Lower {
  11. auto Mangler::MangleInverseQualifiedNameScope(llvm::raw_ostream& os,
  12. SemIR::NameScopeId name_scope_id)
  13. -> void {
  14. // Maintain a stack of names for delayed rendering of interface impls.
  15. struct NameEntry {
  16. SemIR::NameScopeId name_scope_id;
  17. // The prefix emitted before this name component. If '\0', no prefix will be
  18. // emitted.
  19. // - Namespace components are separated by '.'.
  20. // - The two components of an interface are separated by ':'.
  21. char prefix;
  22. };
  23. llvm::SmallVector<NameEntry> names_to_render;
  24. names_to_render.push_back({.name_scope_id = name_scope_id, .prefix = '.'});
  25. while (!names_to_render.empty()) {
  26. auto [name_scope_id, prefix] = names_to_render.pop_back_val();
  27. if (prefix) {
  28. os << prefix;
  29. }
  30. if (name_scope_id == SemIR::NameScopeId::Package) {
  31. auto package_id = sem_ir().package_id();
  32. if (auto ident_id = package_id.AsIdentifierId(); ident_id.has_value()) {
  33. os << sem_ir().identifiers().Get(ident_id);
  34. } else {
  35. // TODO: Handle name conflicts between special package names and raw
  36. // identifier package names. Note that any change here will also require
  37. // a change to namespace mangling for imported packages.
  38. os << package_id.AsSpecialName();
  39. }
  40. continue;
  41. }
  42. const auto& name_scope = sem_ir().name_scopes().Get(name_scope_id);
  43. CARBON_KIND_SWITCH(sem_ir().insts().Get(name_scope.inst_id())) {
  44. case CARBON_KIND(SemIR::ImplDecl impl_decl): {
  45. const auto& impl = sem_ir().impls().Get(impl_decl.impl_id);
  46. auto facet_type = insts().GetAs<SemIR::FacetType>(
  47. constant_values().GetConstantInstId(impl.constraint_id));
  48. const auto& facet_type_info =
  49. sem_ir().facet_types().Get(facet_type.facet_type_id);
  50. auto interface_type = facet_type_info.TryAsSingleInterface();
  51. CARBON_CHECK(interface_type,
  52. "Mangling of an impl of something other than a single "
  53. "interface is not yet supported.");
  54. const auto& interface =
  55. sem_ir().interfaces().Get(interface_type->interface_id);
  56. names_to_render.push_back(
  57. {.name_scope_id = interface.scope_id, .prefix = ':'});
  58. auto self_inst =
  59. insts().Get(constant_values().GetConstantInstId(impl.self_id));
  60. CARBON_KIND_SWITCH(self_inst) {
  61. case CARBON_KIND(SemIR::ClassType class_type): {
  62. auto next_name_scope_id =
  63. sem_ir().classes().Get(class_type.class_id).scope_id;
  64. names_to_render.push_back(
  65. {.name_scope_id = next_name_scope_id, .prefix = '\0'});
  66. break;
  67. }
  68. case SemIR::AutoType::Kind:
  69. case SemIR::BoolType::Kind:
  70. case SemIR::BoundMethodType::Kind:
  71. case SemIR::IntLiteralType::Kind:
  72. case SemIR::LegacyFloatType::Kind:
  73. case SemIR::NamespaceType::Kind:
  74. case SemIR::SpecificFunctionType::Kind:
  75. case SemIR::StringType::Kind:
  76. case SemIR::TypeType::Kind:
  77. case SemIR::VtableType::Kind:
  78. case SemIR::WitnessType::Kind: {
  79. os << self_inst.kind().ir_name();
  80. break;
  81. }
  82. case CARBON_KIND(SemIR::IntType int_type): {
  83. os << (int_type.int_kind == SemIR::IntKind::Signed ? "i" : "u")
  84. << sem_ir().ints().Get(
  85. sem_ir()
  86. .insts()
  87. .GetAs<SemIR::IntValue>(int_type.bit_width_id)
  88. .int_id);
  89. break;
  90. }
  91. default:
  92. CARBON_FATAL("Attempting to mangle unsupported SemIR.");
  93. break;
  94. }
  95. // Skip the tail of the loop that adds the parent name scope to the
  96. // stack - the scope in which the impl was defined is not part of the
  97. // mangling, the constraint and interface alone uniquelify identify an
  98. // impl.
  99. continue;
  100. }
  101. case CARBON_KIND(SemIR::ClassDecl class_decl): {
  102. os << names().GetAsStringIfIdentifier(
  103. sem_ir().classes().Get(class_decl.class_id).name_id);
  104. break;
  105. }
  106. case CARBON_KIND(SemIR::InterfaceDecl interface_decl): {
  107. os << names().GetAsStringIfIdentifier(
  108. sem_ir().interfaces().Get(interface_decl.interface_id).name_id);
  109. break;
  110. }
  111. case SemIR::Namespace::Kind: {
  112. os << names().GetIRBaseName(name_scope.name_id());
  113. break;
  114. }
  115. default:
  116. CARBON_FATAL("Attempting to mangle unsupported SemIR.");
  117. break;
  118. }
  119. if (!name_scope.is_imported_package()) {
  120. names_to_render.push_back(
  121. {.name_scope_id = name_scope.parent_scope_id(), .prefix = '.'});
  122. }
  123. }
  124. }
  125. auto Mangler::Mangle(SemIR::FunctionId function_id,
  126. SemIR::SpecificId specific_id) -> std::string {
  127. const auto& function = sem_ir().functions().Get(function_id);
  128. if (SemIR::IsEntryPoint(sem_ir(), function_id)) {
  129. CARBON_CHECK(!specific_id.has_value(), "entry point should not be generic");
  130. return "main";
  131. }
  132. if (function.cpp_decl) {
  133. return MangleCppClang(function.cpp_decl);
  134. }
  135. RawStringOstream os;
  136. os << "_C";
  137. os << names().GetAsStringIfIdentifier(function.name_id);
  138. MangleInverseQualifiedNameScope(os, function.parent_scope_id);
  139. // TODO: Add proper support for mangling generic entities. For now we use a
  140. // fingerprint of the specific arguments, which should be stable across files,
  141. // but isn't necessarily stable across toolchain changes.
  142. if (specific_id.has_value()) {
  143. os << ".";
  144. llvm::write_hex(
  145. os,
  146. fingerprinter_.GetOrCompute(
  147. &sem_ir(), sem_ir().specifics().Get(specific_id).args_id),
  148. llvm::HexPrintStyle::Lower, 16);
  149. }
  150. return os.TakeStr();
  151. }
  152. auto Mangler::MangleCppClang(const clang::NamedDecl* decl) -> std::string {
  153. CARBON_CHECK(
  154. cpp_mangle_context_,
  155. "Mangling of a C++ imported declaration without a Clang `MangleContext`");
  156. RawStringOstream cpp_mangled_name;
  157. cpp_mangle_context_->mangleName(decl, cpp_mangled_name);
  158. return cpp_mangled_name.TakeStr();
  159. }
  160. auto Mangler::MangleVTable(const SemIR::Class& class_info) -> std::string {
  161. RawStringOstream os;
  162. os << "_C";
  163. os << names().GetAsStringIfIdentifier(class_info.name_id);
  164. MangleInverseQualifiedNameScope(os, class_info.parent_scope_id);
  165. os << ".$vtable";
  166. return os.TakeStr();
  167. }
  168. } // namespace Carbon::Lower