mangler.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. if (auto package_id = sem_ir().package_id(); package_id.has_value()) {
  31. os << sem_ir().identifiers().Get(package_id);
  32. } else {
  33. os << "Main";
  34. }
  35. continue;
  36. }
  37. const auto& name_scope = sem_ir().name_scopes().Get(name_scope_id);
  38. CARBON_KIND_SWITCH(sem_ir().insts().Get(name_scope.inst_id())) {
  39. case CARBON_KIND(SemIR::ImplDecl impl_decl): {
  40. const auto& impl = sem_ir().impls().Get(impl_decl.impl_id);
  41. auto facet_type = insts().GetAs<SemIR::FacetType>(
  42. constant_values().GetConstantInstId(impl.constraint_id));
  43. const auto& facet_type_info =
  44. sem_ir().facet_types().Get(facet_type.facet_type_id);
  45. auto interface_type = facet_type_info.TryAsSingleInterface();
  46. CARBON_CHECK(interface_type,
  47. "Mangling of an impl of something other than a single "
  48. "interface is not yet supported.");
  49. const auto& interface =
  50. sem_ir().interfaces().Get(interface_type->interface_id);
  51. names_to_render.push_back(
  52. {.name_scope_id = interface.scope_id, .prefix = ':'});
  53. auto self_inst =
  54. insts().Get(constant_values().GetConstantInstId(impl.self_id));
  55. CARBON_KIND_SWITCH(self_inst) {
  56. case CARBON_KIND(SemIR::ClassType class_type): {
  57. auto next_name_scope_id =
  58. sem_ir().classes().Get(class_type.class_id).scope_id;
  59. names_to_render.push_back(
  60. {.name_scope_id = next_name_scope_id, .prefix = '\0'});
  61. break;
  62. }
  63. case SemIR::AutoType::Kind:
  64. case SemIR::BoolType::Kind:
  65. case SemIR::BoundMethodType::Kind:
  66. case SemIR::IntLiteralType::Kind:
  67. case SemIR::LegacyFloatType::Kind:
  68. case SemIR::NamespaceType::Kind:
  69. case SemIR::SpecificFunctionType::Kind:
  70. case SemIR::StringType::Kind:
  71. case SemIR::TypeType::Kind:
  72. case SemIR::VtableType::Kind:
  73. case SemIR::WitnessType::Kind: {
  74. os << self_inst.kind().ir_name();
  75. break;
  76. }
  77. case CARBON_KIND(SemIR::IntType int_type): {
  78. os << (int_type.int_kind == SemIR::IntKind::Signed ? "i" : "u")
  79. << sem_ir().ints().Get(
  80. sem_ir()
  81. .insts()
  82. .GetAs<SemIR::IntValue>(int_type.bit_width_id)
  83. .int_id);
  84. break;
  85. }
  86. default:
  87. CARBON_FATAL("Attempting to mangle unsupported SemIR.");
  88. break;
  89. }
  90. // Skip the tail of the loop that adds the parent name scope to the
  91. // stack - the scope in which the impl was defined is not part of the
  92. // mangling, the constraint and interface alone uniquelify identify an
  93. // impl.
  94. continue;
  95. }
  96. case CARBON_KIND(SemIR::ClassDecl class_decl): {
  97. os << names().GetAsStringIfIdentifier(
  98. sem_ir().classes().Get(class_decl.class_id).name_id);
  99. break;
  100. }
  101. case CARBON_KIND(SemIR::InterfaceDecl interface_decl): {
  102. os << names().GetAsStringIfIdentifier(
  103. sem_ir().interfaces().Get(interface_decl.interface_id).name_id);
  104. break;
  105. }
  106. case SemIR::Namespace::Kind: {
  107. os << names().GetAsStringIfIdentifier(name_scope.name_id());
  108. break;
  109. }
  110. default:
  111. CARBON_FATAL("Attempting to mangle unsupported SemIR.");
  112. break;
  113. }
  114. if (!name_scope.is_imported_package()) {
  115. names_to_render.push_back(
  116. {.name_scope_id = name_scope.parent_scope_id(), .prefix = '.'});
  117. }
  118. }
  119. }
  120. auto Mangler::Mangle(SemIR::FunctionId function_id,
  121. SemIR::SpecificId specific_id) -> std::string {
  122. const auto& function = sem_ir().functions().Get(function_id);
  123. if (SemIR::IsEntryPoint(sem_ir(), function_id)) {
  124. CARBON_CHECK(!specific_id.has_value(), "entry point should not be generic");
  125. return "main";
  126. }
  127. RawStringOstream os;
  128. os << "_C";
  129. os << names().GetAsStringIfIdentifier(function.name_id);
  130. MangleInverseQualifiedNameScope(os, function.parent_scope_id);
  131. // TODO: Add proper support for mangling generic entities. For now we use a
  132. // fingerprint of the specific arguments, which should be stable across files,
  133. // but isn't necessarily stable across toolchain changes.
  134. if (specific_id.has_value()) {
  135. os << ".";
  136. llvm::write_hex(
  137. os,
  138. fingerprinter_.GetOrCompute(
  139. &sem_ir(), sem_ir().specifics().Get(specific_id).args_id),
  140. llvm::HexPrintStyle::Lower, 16);
  141. }
  142. return os.TakeStr();
  143. }
  144. } // namespace Carbon::Lower