mangler.cpp 6.5 KB

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