mangler.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 =
  40. types().GetAs<SemIR::InterfaceType>(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(types().GetAsInst(impl.self_id)) {
  46. case CARBON_KIND(SemIR::ClassType class_type): {
  47. auto next_name_scope_id =
  48. sem_ir().classes().Get(class_type.class_id).scope_id;
  49. names_to_render.push_back(
  50. {.name_scope_id = next_name_scope_id, .prefix = '\0'});
  51. break;
  52. }
  53. case CARBON_KIND(SemIR::BuiltinInst builtin_inst): {
  54. os << builtin_inst.builtin_inst_kind.label();
  55. break;
  56. }
  57. default:
  58. CARBON_FATAL() << "Attempting to mangle unsupported SemIR.";
  59. break;
  60. }
  61. // Skip the tail of the loop that adds the parent name scope to the
  62. // stack - the scope in which the impl was defined is not part of the
  63. // mangling, the constraint and interface alone uniquelify identify an
  64. // impl.
  65. continue;
  66. }
  67. case CARBON_KIND(SemIR::ClassDecl class_decl): {
  68. os << names().GetAsStringIfIdentifier(
  69. sem_ir().classes().Get(class_decl.class_id).name_id);
  70. break;
  71. }
  72. case CARBON_KIND(SemIR::InterfaceDecl interface_decl): {
  73. os << names().GetAsStringIfIdentifier(
  74. sem_ir().interfaces().Get(interface_decl.interface_id).name_id);
  75. break;
  76. }
  77. case SemIR::Namespace::Kind: {
  78. os << names().GetAsStringIfIdentifier(name_scope.name_id);
  79. break;
  80. }
  81. default:
  82. CARBON_FATAL() << "Attempting to mangle unsupported SemIR.";
  83. break;
  84. }
  85. names_to_render.push_back(
  86. {.name_scope_id = name_scope.parent_scope_id, .prefix = '.'});
  87. }
  88. }
  89. auto Mangler::Mangle(SemIR::FunctionId function_id) -> std::string {
  90. // FIXME: Add support for generic entities.
  91. const auto& function = sem_ir().functions().Get(function_id);
  92. if (SemIR::IsEntryPoint(sem_ir(), function_id)) {
  93. return "main";
  94. }
  95. std::string result;
  96. llvm::raw_string_ostream os(result);
  97. os << "_C";
  98. os << names().GetAsStringIfIdentifier(function.name_id);
  99. MangleInverseQualifiedNameScope(os, function.parent_scope_id);
  100. return os.str();
  101. }
  102. } // namespace Carbon::Lower