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 "toolchain/base/kind_switch.h"
  6. #include "toolchain/sem_ir/entry_point.h"
  7. #include "toolchain/sem_ir/ids.h"
  8. namespace Carbon::Lower {
  9. auto Mangler::MangleInverseQualifiedNameScope(llvm::raw_ostream& os,
  10. SemIR::NameScopeId name_scope_id)
  11. -> void {
  12. // Maintain a stack of names for delayed rendering of interface impls.
  13. struct NameEntry {
  14. SemIR::NameScopeId name_scope_id;
  15. // The prefix emitted before this name component. If '\0', no prefix will be
  16. // emitted.
  17. // - Namespace components are separated by '.'.
  18. // - The two components of an interface are separated by ':'.
  19. char prefix;
  20. };
  21. llvm::SmallVector<NameEntry> names_to_render;
  22. names_to_render.push_back({.name_scope_id = name_scope_id, .prefix = '.'});
  23. while (!names_to_render.empty()) {
  24. auto [name_scope_id, prefix] = names_to_render.pop_back_val();
  25. if (prefix) {
  26. os << prefix;
  27. }
  28. if (name_scope_id == SemIR::NameScopeId::Package) {
  29. if (auto package_id = sem_ir().package_id(); package_id.is_valid()) {
  30. os << sem_ir().identifiers().Get(package_id);
  31. } else {
  32. os << "Main";
  33. }
  34. continue;
  35. }
  36. const auto& name_scope = sem_ir().name_scopes().Get(name_scope_id);
  37. CARBON_KIND_SWITCH(sem_ir().insts().Get(name_scope.inst_id())) {
  38. case CARBON_KIND(SemIR::ImplDecl impl_decl): {
  39. const auto& impl = sem_ir().impls().Get(impl_decl.impl_id);
  40. auto facet_type = insts().GetAs<SemIR::FacetType>(
  41. constant_values().GetConstantInstId(impl.constraint_id));
  42. const auto& facet_type_info =
  43. sem_ir().facet_types().Get(facet_type.facet_type_id);
  44. auto interface_type = facet_type_info.TryAsSingleInterface();
  45. CARBON_CHECK(interface_type,
  46. "Mangling of an impl of something other than a single "
  47. "interface is not yet supported.");
  48. const auto& interface =
  49. sem_ir().interfaces().Get(interface_type->interface_id);
  50. names_to_render.push_back(
  51. {.name_scope_id = interface.scope_id, .prefix = ':'});
  52. auto self_inst =
  53. insts().Get(constant_values().GetConstantInstId(impl.self_id));
  54. CARBON_KIND_SWITCH(self_inst) {
  55. case CARBON_KIND(SemIR::ClassType class_type): {
  56. auto next_name_scope_id =
  57. sem_ir().classes().Get(class_type.class_id).scope_id;
  58. names_to_render.push_back(
  59. {.name_scope_id = next_name_scope_id, .prefix = '\0'});
  60. break;
  61. }
  62. case SemIR::AutoType::Kind:
  63. case SemIR::BoolType::Kind:
  64. case SemIR::BoundMethodType::Kind:
  65. case SemIR::IntLiteralType::Kind:
  66. case SemIR::LegacyFloatType::Kind:
  67. case SemIR::NamespaceType::Kind:
  68. case SemIR::SpecificFunctionType::Kind:
  69. case SemIR::StringType::Kind:
  70. case SemIR::TypeType::Kind:
  71. case SemIR::VtableType::Kind:
  72. case SemIR::WitnessType::Kind: {
  73. os << self_inst.kind().ir_name();
  74. break;
  75. }
  76. case CARBON_KIND(SemIR::IntType int_type): {
  77. os << (int_type.int_kind == SemIR::IntKind::Signed ? "i" : "u")
  78. << sem_ir().ints().Get(
  79. sem_ir()
  80. .insts()
  81. .GetAs<SemIR::IntValue>(int_type.bit_width_id)
  82. .int_id);
  83. break;
  84. }
  85. default:
  86. CARBON_FATAL("Attempting to mangle unsupported SemIR.");
  87. break;
  88. }
  89. // Skip the tail of the loop that adds the parent name scope to the
  90. // stack - the scope in which the impl was defined is not part of the
  91. // mangling, the constraint and interface alone uniquelify identify an
  92. // impl.
  93. continue;
  94. }
  95. case CARBON_KIND(SemIR::ClassDecl class_decl): {
  96. os << names().GetAsStringIfIdentifier(
  97. sem_ir().classes().Get(class_decl.class_id).name_id);
  98. break;
  99. }
  100. case CARBON_KIND(SemIR::InterfaceDecl interface_decl): {
  101. os << names().GetAsStringIfIdentifier(
  102. sem_ir().interfaces().Get(interface_decl.interface_id).name_id);
  103. break;
  104. }
  105. case SemIR::Namespace::Kind: {
  106. os << names().GetAsStringIfIdentifier(name_scope.name_id());
  107. break;
  108. }
  109. default:
  110. CARBON_FATAL("Attempting to mangle unsupported SemIR.");
  111. break;
  112. }
  113. if (!name_scope.is_imported_package()) {
  114. names_to_render.push_back(
  115. {.name_scope_id = name_scope.parent_scope_id(), .prefix = '.'});
  116. }
  117. }
  118. }
  119. auto Mangler::Mangle(SemIR::FunctionId function_id,
  120. SemIR::SpecificId specific_id) -> std::string {
  121. const auto& function = sem_ir().functions().Get(function_id);
  122. if (SemIR::IsEntryPoint(sem_ir(), function_id)) {
  123. CARBON_CHECK(!specific_id.is_valid(), "entry point should not be generic");
  124. return "main";
  125. }
  126. std::string result;
  127. llvm::raw_string_ostream os(result);
  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.is_valid()) {
  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.str();
  143. }
  144. } // namespace Carbon::Lower