mangler.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. namespace Carbon::Lower {
  8. auto Mangler::MangleInverseQualifiedNameScope(llvm::raw_ostream& os,
  9. SemIR::NameScopeId name_scope_id)
  10. -> void {
  11. // Maintain a stack of names for delayed rendering of interface impls.
  12. struct NameEntry {
  13. SemIR::NameScopeId name_scope_id;
  14. // The prefix emitted before this name component. If '\0', no prefix will be
  15. // emitted.
  16. // - Namespace components are separated by '.'.
  17. // - The two components of an interface are separated by ':'.
  18. char prefix;
  19. };
  20. llvm::SmallVector<NameEntry> names_to_render;
  21. names_to_render.push_back({.name_scope_id = name_scope_id, .prefix = '.'});
  22. while (!names_to_render.empty()) {
  23. auto [name_scope_id, prefix] = names_to_render.pop_back_val();
  24. if (prefix) {
  25. os << prefix;
  26. }
  27. if (name_scope_id == SemIR::NameScopeId::Package) {
  28. if (auto package_id = sem_ir().package_id(); package_id.is_valid()) {
  29. os << sem_ir().identifiers().Get(package_id);
  30. } else {
  31. os << "Main";
  32. }
  33. continue;
  34. }
  35. const auto& name_scope = sem_ir().name_scopes().Get(name_scope_id);
  36. CARBON_KIND_SWITCH(sem_ir().insts().Get(name_scope.inst_id)) {
  37. case CARBON_KIND(SemIR::ImplDecl impl_decl): {
  38. const auto& impl = sem_ir().impls().Get(impl_decl.impl_id);
  39. auto interface_type = insts().GetAs<SemIR::InterfaceType>(
  40. constant_values().GetConstantInstId(impl.constraint_id));
  41. const auto& interface =
  42. sem_ir().interfaces().Get(interface_type.interface_id);
  43. names_to_render.push_back(
  44. {.name_scope_id = interface.scope_id, .prefix = ':'});
  45. CARBON_KIND_SWITCH(insts().Get(constant_values().GetConstantInstId(
  46. impl.self_id))) {
  47. case CARBON_KIND(SemIR::ClassType class_type): {
  48. auto next_name_scope_id =
  49. sem_ir().classes().Get(class_type.class_id).scope_id;
  50. names_to_render.push_back(
  51. {.name_scope_id = next_name_scope_id, .prefix = '\0'});
  52. break;
  53. }
  54. case CARBON_KIND(SemIR::BuiltinInst builtin_inst): {
  55. os << builtin_inst.builtin_inst_kind.label();
  56. break;
  57. }
  58. default:
  59. CARBON_FATAL("Attempting to mangle unsupported SemIR.");
  60. break;
  61. }
  62. // Skip the tail of the loop that adds the parent name scope to the
  63. // stack - the scope in which the impl was defined is not part of the
  64. // mangling, the constraint and interface alone uniquelify identify an
  65. // impl.
  66. continue;
  67. }
  68. case CARBON_KIND(SemIR::ClassDecl class_decl): {
  69. os << names().GetAsStringIfIdentifier(
  70. sem_ir().classes().Get(class_decl.class_id).name_id);
  71. break;
  72. }
  73. case CARBON_KIND(SemIR::InterfaceDecl interface_decl): {
  74. os << names().GetAsStringIfIdentifier(
  75. sem_ir().interfaces().Get(interface_decl.interface_id).name_id);
  76. break;
  77. }
  78. case SemIR::Namespace::Kind: {
  79. os << names().GetAsStringIfIdentifier(name_scope.name_id);
  80. break;
  81. }
  82. default:
  83. CARBON_FATAL("Attempting to mangle unsupported SemIR.");
  84. break;
  85. }
  86. if (!name_scope.is_imported_package()) {
  87. names_to_render.push_back(
  88. {.name_scope_id = name_scope.parent_scope_id, .prefix = '.'});
  89. }
  90. }
  91. }
  92. auto Mangler::Mangle(SemIR::FunctionId function_id,
  93. SemIR::SpecificId specific_id) -> std::string {
  94. const auto& function = sem_ir().functions().Get(function_id);
  95. if (SemIR::IsEntryPoint(sem_ir(), function_id)) {
  96. CARBON_CHECK(!specific_id.is_valid(), "entry point should not be generic");
  97. return "main";
  98. }
  99. std::string result;
  100. llvm::raw_string_ostream os(result);
  101. os << "_C";
  102. os << names().GetAsStringIfIdentifier(function.name_id);
  103. MangleInverseQualifiedNameScope(os, function.parent_scope_id);
  104. // TODO: Add proper support for generic entities. The ID we emit here will not
  105. // be consistent across object files.
  106. if (specific_id.is_valid()) {
  107. os << "." << specific_id.index;
  108. }
  109. return os.str();
  110. }
  111. } // namespace Carbon::Lower