mangler.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. CARBON_CHECK(facet_type_info.extend_constraints.size() == 1,
  51. "Mangling of an impl of something other than a single "
  52. "interface is not yet supported.");
  53. auto interface_type = facet_type_info.extend_constraints.front();
  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_const_inst_id =
  59. constant_values().GetConstantInstId(impl.self_id);
  60. auto self_inst = insts().Get(self_const_inst_id);
  61. CARBON_KIND_SWITCH(self_inst) {
  62. case CARBON_KIND(SemIR::ClassType class_type): {
  63. auto next_name_scope_id =
  64. sem_ir().classes().Get(class_type.class_id).scope_id;
  65. names_to_render.push_back(
  66. {.name_scope_id = next_name_scope_id, .prefix = '\0'});
  67. break;
  68. }
  69. case SemIR::AutoType::Kind:
  70. case SemIR::BoolType::Kind:
  71. case SemIR::BoundMethodType::Kind:
  72. case SemIR::IntLiteralType::Kind:
  73. case SemIR::LegacyFloatType::Kind:
  74. case SemIR::NamespaceType::Kind:
  75. case SemIR::SpecificFunctionType::Kind:
  76. case SemIR::StringType::Kind:
  77. case SemIR::TypeType::Kind:
  78. case SemIR::VtableType::Kind:
  79. case SemIR::WitnessType::Kind: {
  80. os << self_inst.kind().ir_name();
  81. break;
  82. }
  83. case CARBON_KIND(SemIR::IntType int_type): {
  84. os << (int_type.int_kind == SemIR::IntKind::Signed ? "i" : "u")
  85. << sem_ir().ints().Get(
  86. sem_ir()
  87. .insts()
  88. .GetAs<SemIR::IntValue>(int_type.bit_width_id)
  89. .int_id);
  90. break;
  91. }
  92. default: {
  93. // Fall back to including a fingerprint.
  94. llvm::write_hex(
  95. os, fingerprinter_.GetOrCompute(&sem_ir(), self_const_inst_id),
  96. llvm::HexPrintStyle::Lower, 16);
  97. break;
  98. }
  99. }
  100. // Skip the tail of the loop that adds the parent name scope to the
  101. // stack - the scope in which the impl was defined is not part of the
  102. // mangling, the constraint and interface alone uniquelify identify an
  103. // impl.
  104. continue;
  105. }
  106. case CARBON_KIND(SemIR::ClassDecl class_decl): {
  107. os << names().GetAsStringIfIdentifier(
  108. sem_ir().classes().Get(class_decl.class_id).name_id);
  109. break;
  110. }
  111. case CARBON_KIND(SemIR::InterfaceDecl interface_decl): {
  112. os << names().GetAsStringIfIdentifier(
  113. sem_ir().interfaces().Get(interface_decl.interface_id).name_id);
  114. break;
  115. }
  116. case SemIR::Namespace::Kind: {
  117. os << names().GetIRBaseName(name_scope.name_id());
  118. break;
  119. }
  120. default:
  121. CARBON_FATAL("Attempting to mangle unsupported SemIR.");
  122. break;
  123. }
  124. if (!name_scope.is_imported_package()) {
  125. names_to_render.push_back(
  126. {.name_scope_id = name_scope.parent_scope_id(), .prefix = '.'});
  127. }
  128. }
  129. }
  130. auto Mangler::Mangle(SemIR::FunctionId function_id,
  131. SemIR::SpecificId specific_id) -> std::string {
  132. const auto& function = sem_ir().functions().Get(function_id);
  133. if (SemIR::IsEntryPoint(sem_ir(), function_id)) {
  134. CARBON_CHECK(!specific_id.has_value(), "entry point should not be generic");
  135. return "main";
  136. }
  137. if (function.cpp_decl) {
  138. return MangleCppClang(function.cpp_decl);
  139. }
  140. RawStringOstream os;
  141. os << "_C";
  142. os << names().GetAsStringIfIdentifier(function.name_id);
  143. // For a special function, add a marker to disambiguate.
  144. switch (function.special_function_kind) {
  145. case SemIR::Function::SpecialFunctionKind::None:
  146. break;
  147. case SemIR::Function::SpecialFunctionKind::Thunk:
  148. os << ":thunk";
  149. break;
  150. }
  151. MangleInverseQualifiedNameScope(os, function.parent_scope_id);
  152. // TODO: Add proper support for mangling generic entities. For now we use a
  153. // fingerprint of the specific arguments, which should be stable across files,
  154. // but isn't necessarily stable across toolchain changes.
  155. if (specific_id.has_value()) {
  156. os << ".";
  157. llvm::write_hex(
  158. os,
  159. fingerprinter_.GetOrCompute(
  160. &sem_ir(), sem_ir().specifics().Get(specific_id).args_id),
  161. llvm::HexPrintStyle::Lower, 16);
  162. }
  163. return os.TakeStr();
  164. }
  165. auto Mangler::MangleCppClang(const clang::NamedDecl* decl) -> std::string {
  166. CARBON_CHECK(
  167. cpp_mangle_context_,
  168. "Mangling of a C++ imported declaration without a Clang `MangleContext`");
  169. RawStringOstream cpp_mangled_name;
  170. cpp_mangle_context_->mangleName(decl, cpp_mangled_name);
  171. return cpp_mangled_name.TakeStr();
  172. }
  173. auto Mangler::MangleVTable(const SemIR::Class& class_info) -> std::string {
  174. RawStringOstream os;
  175. os << "_C";
  176. os << names().GetAsStringIfIdentifier(class_info.name_id);
  177. MangleInverseQualifiedNameScope(os, class_info.parent_scope_id);
  178. os << ".$vtable";
  179. return os.TakeStr();
  180. }
  181. } // namespace Carbon::Lower