xref: /llvm-project/flang/lib/Optimizer/Support/InternalNames.cpp (revision 95b4128c6a87e9b894aa75524e63be147cca790b)
15339029fSEric Schweitz //===-- InternalNames.cpp -------------------------------------------------===//
25339029fSEric Schweitz //
35339029fSEric Schweitz // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45339029fSEric Schweitz // See https://llvm.org/LICENSE.txt for license information.
55339029fSEric Schweitz // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
65339029fSEric Schweitz //
75339029fSEric Schweitz //===----------------------------------------------------------------------===//
8c68d2895SEric Schweitz //
9c68d2895SEric Schweitz // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10c68d2895SEric Schweitz //
11c68d2895SEric Schweitz //===----------------------------------------------------------------------===//
125339029fSEric Schweitz 
135339029fSEric Schweitz #include "flang/Optimizer/Support/InternalNames.h"
14e090182fSEric Schweitz #include "flang/Optimizer/Dialect/FIRType.h"
15e090182fSEric Schweitz #include "mlir/IR/BuiltinTypes.h"
165339029fSEric Schweitz #include "mlir/IR/Diagnostics.h"
175339029fSEric Schweitz #include "llvm/Support/CommandLine.h"
184d4d4785SKazu Hirata #include <optional>
19cfd4c180SSlava Zakharin #include <regex>
205339029fSEric Schweitz 
215339029fSEric Schweitz static llvm::cl::opt<std::string> mainEntryName(
225339029fSEric Schweitz     "main-entry-name",
235339029fSEric Schweitz     llvm::cl::desc("override the name of the default PROGRAM entry (may be "
245339029fSEric Schweitz                    "helpful for using other runtimes)"));
255339029fSEric Schweitz 
261e96c4b5SValentin Clement constexpr std::int64_t badValue = -1;
275339029fSEric Schweitz 
285339029fSEric Schweitz inline std::string prefix() { return "_Q"; }
295339029fSEric Schweitz 
302c143345SV Donaldson /// Generate a mangling prefix from module, submodule, procedure, and
312c143345SV Donaldson /// statement function names, plus an (innermost) block scope id.
322c143345SV Donaldson static std::string doAncestors(llvm::ArrayRef<llvm::StringRef> modules,
332c143345SV Donaldson                                llvm::ArrayRef<llvm::StringRef> procs,
342c143345SV Donaldson                                std::int64_t blockId = 0) {
352c143345SV Donaldson   std::string prefix;
362c143345SV Donaldson   const char *tag = "M";
372c143345SV Donaldson   for (auto mod : modules) {
382c143345SV Donaldson     prefix.append(tag).append(mod.lower());
392c143345SV Donaldson     tag = "S";
405339029fSEric Schweitz   }
412c143345SV Donaldson   for (auto proc : procs)
422c143345SV Donaldson     prefix.append("F").append(proc.lower());
432c143345SV Donaldson   if (blockId)
442c143345SV Donaldson     prefix.append("B").append(std::to_string(blockId));
452c143345SV Donaldson   return prefix;
465339029fSEric Schweitz }
475339029fSEric Schweitz 
4864d7d5a5SValentin Clement inline llvm::SmallVector<llvm::StringRef>
495339029fSEric Schweitz convertToStringRef(llvm::ArrayRef<std::string> from) {
505339029fSEric Schweitz   return {from.begin(), from.end()};
515339029fSEric Schweitz }
525339029fSEric Schweitz 
53c0921586SKazu Hirata inline std::optional<llvm::StringRef>
54c0921586SKazu Hirata convertToStringRef(const std::optional<std::string> &from) {
55c0921586SKazu Hirata   std::optional<llvm::StringRef> to;
5686b8c1d9SKazu Hirata   if (from)
57009ab172SKazu Hirata     to = *from;
585339029fSEric Schweitz   return to;
595339029fSEric Schweitz }
605339029fSEric Schweitz 
615339029fSEric Schweitz static std::string readName(llvm::StringRef uniq, std::size_t &i,
625339029fSEric Schweitz                             std::size_t init, std::size_t end) {
63cfd4c180SSlava Zakharin   // Allow 'X' to be part of the mangled name, which
64cfd4c180SSlava Zakharin   // can happen after the special symbols are replaced
65cfd4c180SSlava Zakharin   // in the mangled names by CompilerGeneratedNamesConversionPass.
66cfd4c180SSlava Zakharin   for (i = init; i < end && (uniq[i] < 'A' || uniq[i] > 'Z' || uniq[i] == 'X');
67cfd4c180SSlava Zakharin        ++i) {
685339029fSEric Schweitz     // do nothing
695339029fSEric Schweitz   }
7030db1776SEric Schweitz   return uniq.substr(init, i - init).str();
715339029fSEric Schweitz }
725339029fSEric Schweitz 
735339029fSEric Schweitz static std::int64_t readInt(llvm::StringRef uniq, std::size_t &i,
745339029fSEric Schweitz                             std::size_t init, std::size_t end) {
755339029fSEric Schweitz   for (i = init; i < end && uniq[i] >= '0' && uniq[i] <= '9'; ++i) {
765339029fSEric Schweitz     // do nothing
775339029fSEric Schweitz   }
781e96c4b5SValentin Clement   std::int64_t result = badValue;
7930db1776SEric Schweitz   if (uniq.substr(init, i - init).getAsInteger(10, result))
801e96c4b5SValentin Clement     return badValue;
815339029fSEric Schweitz   return result;
825339029fSEric Schweitz }
835339029fSEric Schweitz 
845339029fSEric Schweitz std::string fir::NameUniquer::toLower(llvm::StringRef name) {
855339029fSEric Schweitz   return name.lower();
865339029fSEric Schweitz }
875339029fSEric Schweitz 
885339029fSEric Schweitz std::string fir::NameUniquer::intAsString(std::int64_t i) {
895339029fSEric Schweitz   assert(i >= 0);
905339029fSEric Schweitz   return std::to_string(i);
915339029fSEric Schweitz }
925339029fSEric Schweitz 
935339029fSEric Schweitz std::string fir::NameUniquer::doKind(std::int64_t kind) {
945339029fSEric Schweitz   std::string result = "K";
955339029fSEric Schweitz   if (kind < 0)
965339029fSEric Schweitz     return result.append("N").append(intAsString(-kind));
975339029fSEric Schweitz   return result.append(intAsString(kind));
985339029fSEric Schweitz }
995339029fSEric Schweitz 
1005339029fSEric Schweitz std::string fir::NameUniquer::doKinds(llvm::ArrayRef<std::int64_t> kinds) {
1015339029fSEric Schweitz   std::string result;
1025339029fSEric Schweitz   for (auto i : kinds)
1035339029fSEric Schweitz     result.append(doKind(i));
1045339029fSEric Schweitz   return result;
1055339029fSEric Schweitz }
1065339029fSEric Schweitz 
1075339029fSEric Schweitz std::string fir::NameUniquer::doCommonBlock(llvm::StringRef name) {
1086f7a3b07SV Donaldson   return prefix().append("C").append(toLower(name));
109e090182fSEric Schweitz }
110e090182fSEric Schweitz 
1115339029fSEric Schweitz std::string
1125339029fSEric Schweitz fir::NameUniquer::doConstant(llvm::ArrayRef<llvm::StringRef> modules,
1132c143345SV Donaldson                              llvm::ArrayRef<llvm::StringRef> procs,
1142c143345SV Donaldson                              std::int64_t blockId, llvm::StringRef name) {
1156f7a3b07SV Donaldson   return prefix()
1166f7a3b07SV Donaldson       .append(doAncestors(modules, procs, blockId))
1176f7a3b07SV Donaldson       .append("EC")
1186f7a3b07SV Donaldson       .append(toLower(name));
1195339029fSEric Schweitz }
1205339029fSEric Schweitz 
1215339029fSEric Schweitz std::string
1225339029fSEric Schweitz fir::NameUniquer::doDispatchTable(llvm::ArrayRef<llvm::StringRef> modules,
1232c143345SV Donaldson                                   llvm::ArrayRef<llvm::StringRef> procs,
1242c143345SV Donaldson                                   std::int64_t blockId, llvm::StringRef name,
1255339029fSEric Schweitz                                   llvm::ArrayRef<std::int64_t> kinds) {
1266f7a3b07SV Donaldson   return prefix()
1276f7a3b07SV Donaldson       .append(doAncestors(modules, procs, blockId))
1286f7a3b07SV Donaldson       .append("DT")
1296f7a3b07SV Donaldson       .append(toLower(name))
1306f7a3b07SV Donaldson       .append(doKinds(kinds));
1315339029fSEric Schweitz }
1325339029fSEric Schweitz 
1335339029fSEric Schweitz std::string fir::NameUniquer::doGenerated(llvm::StringRef name) {
1346f7a3b07SV Donaldson   return prefix().append("Q").append(name);
1356f7a3b07SV Donaldson }
1366f7a3b07SV Donaldson 
1376f7a3b07SV Donaldson std::string
1386f7a3b07SV Donaldson fir::NameUniquer::doGenerated(llvm::ArrayRef<llvm::StringRef> modules,
1396f7a3b07SV Donaldson                               llvm::ArrayRef<llvm::StringRef> procs,
1406f7a3b07SV Donaldson                               std::int64_t blockId, llvm::StringRef name) {
1416f7a3b07SV Donaldson   return prefix()
1426f7a3b07SV Donaldson       .append("Q")
1436f7a3b07SV Donaldson       .append(doAncestors(modules, procs, blockId))
1446f7a3b07SV Donaldson       .append(name);
1455339029fSEric Schweitz }
1465339029fSEric Schweitz 
1475339029fSEric Schweitz std::string fir::NameUniquer::doIntrinsicTypeDescriptor(
1485339029fSEric Schweitz     llvm::ArrayRef<llvm::StringRef> modules,
1492c143345SV Donaldson     llvm::ArrayRef<llvm::StringRef> procs, std::int64_t blockId,
1502c143345SV Donaldson     IntrinsicType type, std::int64_t kind) {
1515339029fSEric Schweitz   const char *name = nullptr;
1525339029fSEric Schweitz   switch (type) {
1535339029fSEric Schweitz   case IntrinsicType::CHARACTER:
1545339029fSEric Schweitz     name = "character";
1555339029fSEric Schweitz     break;
1565339029fSEric Schweitz   case IntrinsicType::COMPLEX:
1575339029fSEric Schweitz     name = "complex";
1585339029fSEric Schweitz     break;
1595339029fSEric Schweitz   case IntrinsicType::INTEGER:
1605339029fSEric Schweitz     name = "integer";
1615339029fSEric Schweitz     break;
1625339029fSEric Schweitz   case IntrinsicType::LOGICAL:
1635339029fSEric Schweitz     name = "logical";
1645339029fSEric Schweitz     break;
1655339029fSEric Schweitz   case IntrinsicType::REAL:
1665339029fSEric Schweitz     name = "real";
1675339029fSEric Schweitz     break;
1685339029fSEric Schweitz   }
1695339029fSEric Schweitz   assert(name && "unknown intrinsic type");
1706f7a3b07SV Donaldson   return prefix()
1716f7a3b07SV Donaldson       .append(doAncestors(modules, procs, blockId))
1726f7a3b07SV Donaldson       .append("YI")
1736f7a3b07SV Donaldson       .append(name)
1746f7a3b07SV Donaldson       .append(doKind(kind));
1755339029fSEric Schweitz }
1765339029fSEric Schweitz 
1775339029fSEric Schweitz std::string
1785339029fSEric Schweitz fir::NameUniquer::doProcedure(llvm::ArrayRef<llvm::StringRef> modules,
1792c143345SV Donaldson                               llvm::ArrayRef<llvm::StringRef> procs,
1805339029fSEric Schweitz                               llvm::StringRef name) {
1816f7a3b07SV Donaldson   return prefix()
1826f7a3b07SV Donaldson       .append(doAncestors(modules, procs))
1836f7a3b07SV Donaldson       .append("P")
1846f7a3b07SV Donaldson       .append(toLower(name));
1855339029fSEric Schweitz }
1865339029fSEric Schweitz 
1875339029fSEric Schweitz std::string fir::NameUniquer::doType(llvm::ArrayRef<llvm::StringRef> modules,
1882c143345SV Donaldson                                      llvm::ArrayRef<llvm::StringRef> procs,
1892c143345SV Donaldson                                      std::int64_t blockId, llvm::StringRef name,
1905339029fSEric Schweitz                                      llvm::ArrayRef<std::int64_t> kinds) {
1916f7a3b07SV Donaldson   return prefix()
1926f7a3b07SV Donaldson       .append(doAncestors(modules, procs, blockId))
1936f7a3b07SV Donaldson       .append("T")
1946f7a3b07SV Donaldson       .append(toLower(name))
1956f7a3b07SV Donaldson       .append(doKinds(kinds));
1965339029fSEric Schweitz }
1975339029fSEric Schweitz 
1985339029fSEric Schweitz std::string
1995339029fSEric Schweitz fir::NameUniquer::doTypeDescriptor(llvm::ArrayRef<llvm::StringRef> modules,
2002c143345SV Donaldson                                    llvm::ArrayRef<llvm::StringRef> procs,
2012c143345SV Donaldson                                    std::int64_t blockId, llvm::StringRef name,
2025339029fSEric Schweitz                                    llvm::ArrayRef<std::int64_t> kinds) {
2036f7a3b07SV Donaldson   return prefix()
2046f7a3b07SV Donaldson       .append(doAncestors(modules, procs, blockId))
2056f7a3b07SV Donaldson       .append("CT")
2066f7a3b07SV Donaldson       .append(toLower(name))
2076f7a3b07SV Donaldson       .append(doKinds(kinds));
2085339029fSEric Schweitz }
2095339029fSEric Schweitz 
2102c143345SV Donaldson std::string
2112c143345SV Donaldson fir::NameUniquer::doTypeDescriptor(llvm::ArrayRef<std::string> modules,
2122c143345SV Donaldson                                    llvm::ArrayRef<std::string> procs,
2132c143345SV Donaldson                                    std::int64_t blockId, llvm::StringRef name,
2142c143345SV Donaldson                                    llvm::ArrayRef<std::int64_t> kinds) {
2155339029fSEric Schweitz   auto rmodules = convertToStringRef(modules);
2162c143345SV Donaldson   auto rprocs = convertToStringRef(procs);
2172c143345SV Donaldson   return doTypeDescriptor(rmodules, rprocs, blockId, name, kinds);
2185339029fSEric Schweitz }
2195339029fSEric Schweitz 
2205339029fSEric Schweitz std::string
2215339029fSEric Schweitz fir::NameUniquer::doVariable(llvm::ArrayRef<llvm::StringRef> modules,
2222c143345SV Donaldson                              llvm::ArrayRef<llvm::StringRef> procs,
2232c143345SV Donaldson                              std::int64_t blockId, llvm::StringRef name) {
2246f7a3b07SV Donaldson   return prefix()
2256f7a3b07SV Donaldson       .append(doAncestors(modules, procs, blockId))
2266f7a3b07SV Donaldson       .append("E")
2276f7a3b07SV Donaldson       .append(toLower(name));
2285339029fSEric Schweitz }
2295339029fSEric Schweitz 
23062cc6b0dSValentin Clement std::string
23162cc6b0dSValentin Clement fir::NameUniquer::doNamelistGroup(llvm::ArrayRef<llvm::StringRef> modules,
2322c143345SV Donaldson                                   llvm::ArrayRef<llvm::StringRef> procs,
23362cc6b0dSValentin Clement                                   llvm::StringRef name) {
2346f7a3b07SV Donaldson   return prefix()
2356f7a3b07SV Donaldson       .append(doAncestors(modules, procs))
2366f7a3b07SV Donaldson       .append("N")
2376f7a3b07SV Donaldson       .append(toLower(name));
23862cc6b0dSValentin Clement }
23962cc6b0dSValentin Clement 
2405339029fSEric Schweitz llvm::StringRef fir::NameUniquer::doProgramEntry() {
2415339029fSEric Schweitz   if (mainEntryName.size())
2425339029fSEric Schweitz     return mainEntryName;
2435339029fSEric Schweitz   return "_QQmain";
2445339029fSEric Schweitz }
2455339029fSEric Schweitz 
2465339029fSEric Schweitz std::pair<fir::NameUniquer::NameKind, fir::NameUniquer::DeconstructedName>
2475339029fSEric Schweitz fir::NameUniquer::deconstruct(llvm::StringRef uniq) {
248c373f581SjeanPerier   uniq = fir::NameUniquer::dropTypeConversionMarkers(uniq);
24911efcceaSKazu Hirata   if (uniq.starts_with("_Q")) {
2501e96c4b5SValentin Clement     llvm::SmallVector<std::string> modules;
2512c143345SV Donaldson     llvm::SmallVector<std::string> procs;
2522c143345SV Donaldson     std::int64_t blockId = 0;
2535339029fSEric Schweitz     std::string name;
2541e96c4b5SValentin Clement     llvm::SmallVector<std::int64_t> kinds;
2555339029fSEric Schweitz     NameKind nk = NameKind::NOT_UNIQUED;
2565339029fSEric Schweitz     for (std::size_t i = 2, end{uniq.size()}; i != end;) {
2575339029fSEric Schweitz       switch (uniq[i]) {
2582c143345SV Donaldson       case 'B': // Block
2592c143345SV Donaldson         blockId = readInt(uniq, i, i + 1, end);
2602c143345SV Donaldson         break;
2612c143345SV Donaldson       case 'C': // Common block
2625339029fSEric Schweitz         nk = NameKind::COMMON;
2635339029fSEric Schweitz         name = readName(uniq, i, i + 1, end);
2645339029fSEric Schweitz         break;
2652c143345SV Donaldson       case 'D': // Dispatch table
2665339029fSEric Schweitz         nk = NameKind::DISPATCH_TABLE;
2675339029fSEric Schweitz         assert(uniq[i + 1] == 'T');
2685339029fSEric Schweitz         name = readName(uniq, i, i + 2, end);
2695339029fSEric Schweitz         break;
2705339029fSEric Schweitz       case 'E':
2712c143345SV Donaldson         if (uniq[i + 1] == 'C') { // Constant Entity
2725339029fSEric Schweitz           nk = NameKind::CONSTANT;
2735339029fSEric Schweitz           name = readName(uniq, i, i + 2, end);
2742c143345SV Donaldson         } else { // variable Entity
2755339029fSEric Schweitz           nk = NameKind::VARIABLE;
2765339029fSEric Schweitz           name = readName(uniq, i, i + 1, end);
2775339029fSEric Schweitz         }
2785339029fSEric Schweitz         break;
2792c143345SV Donaldson       case 'F': // procedure/Function ancestor component of a mangled prefix
2802c143345SV Donaldson         procs.push_back(readName(uniq, i, i + 1, end));
2812c143345SV Donaldson         break;
2822c143345SV Donaldson       case 'K':
2832c143345SV Donaldson         if (uniq[i + 1] == 'N') // Negative Kind
2842c143345SV Donaldson           kinds.push_back(-readInt(uniq, i, i + 2, end));
2852c143345SV Donaldson         else // [positive] Kind
2862c143345SV Donaldson           kinds.push_back(readInt(uniq, i, i + 1, end));
2872c143345SV Donaldson         break;
2882c143345SV Donaldson       case 'M': // Module
2892c143345SV Donaldson       case 'S': // Submodule
2902c143345SV Donaldson         modules.push_back(readName(uniq, i, i + 1, end));
2912c143345SV Donaldson         break;
2922c143345SV Donaldson       case 'N': // Namelist group
2932c143345SV Donaldson         nk = NameKind::NAMELIST_GROUP;
294e090182fSEric Schweitz         name = readName(uniq, i, i + 1, end);
295e090182fSEric Schweitz         break;
2962c143345SV Donaldson       case 'P': // Procedure/function (itself)
2975339029fSEric Schweitz         nk = NameKind::PROCEDURE;
2985339029fSEric Schweitz         name = readName(uniq, i, i + 1, end);
2995339029fSEric Schweitz         break;
3002c143345SV Donaldson       case 'Q': // UniQue mangle name tag
3015339029fSEric Schweitz         nk = NameKind::GENERATED;
3025339029fSEric Schweitz         name = uniq;
3035339029fSEric Schweitz         i = end;
3045339029fSEric Schweitz         break;
3052c143345SV Donaldson       case 'T': // derived Type
3065339029fSEric Schweitz         nk = NameKind::DERIVED_TYPE;
3075339029fSEric Schweitz         name = readName(uniq, i, i + 1, end);
3085339029fSEric Schweitz         break;
3092c143345SV Donaldson       case 'Y':
3102c143345SV Donaldson         if (uniq[i + 1] == 'I') { // tYpe descriptor for an Intrinsic type
3112c143345SV Donaldson           nk = NameKind::INTRINSIC_TYPE_DESC;
31262cc6b0dSValentin Clement           name = readName(uniq, i, i + 1, end);
3132c143345SV Donaldson         } else { // tYpe descriptor
3142c143345SV Donaldson           nk = NameKind::TYPE_DESC;
3152c143345SV Donaldson           name = readName(uniq, i, i + 2, end);
3162c143345SV Donaldson         }
31762cc6b0dSValentin Clement         break;
3185339029fSEric Schweitz       default:
3195339029fSEric Schweitz         assert(false && "unknown uniquing code");
3205339029fSEric Schweitz         break;
3215339029fSEric Schweitz       }
3225339029fSEric Schweitz     }
3232c143345SV Donaldson     return {nk, DeconstructedName(modules, procs, blockId, name, kinds)};
3245339029fSEric Schweitz   }
3255339029fSEric Schweitz   return {NameKind::NOT_UNIQUED, DeconstructedName(uniq)};
3265339029fSEric Schweitz }
327fc66dbbaSValentin Clement 
328fc66dbbaSValentin Clement bool fir::NameUniquer::isExternalFacingUniquedName(
329fc66dbbaSValentin Clement     const std::pair<fir::NameUniquer::NameKind,
330fc66dbbaSValentin Clement                     fir::NameUniquer::DeconstructedName> &deconstructResult) {
331fc66dbbaSValentin Clement   return (deconstructResult.first == NameKind::PROCEDURE ||
332fc66dbbaSValentin Clement           deconstructResult.first == NameKind::COMMON) &&
333fc66dbbaSValentin Clement          deconstructResult.second.modules.empty() &&
3342c143345SV Donaldson          deconstructResult.second.procs.empty();
335fc66dbbaSValentin Clement }
336fc66dbbaSValentin Clement 
337fc66dbbaSValentin Clement bool fir::NameUniquer::needExternalNameMangling(llvm::StringRef uniquedName) {
338fc66dbbaSValentin Clement   auto result = fir::NameUniquer::deconstruct(uniquedName);
339fc66dbbaSValentin Clement   return result.first != fir::NameUniquer::NameKind::NOT_UNIQUED &&
340fc66dbbaSValentin Clement          fir::NameUniquer::isExternalFacingUniquedName(result);
341fc66dbbaSValentin Clement }
3427dd7ccd2SJean Perier 
3437dd7ccd2SJean Perier bool fir::NameUniquer::belongsToModule(llvm::StringRef uniquedName,
3447dd7ccd2SJean Perier                                        llvm::StringRef moduleName) {
3457dd7ccd2SJean Perier   auto result = fir::NameUniquer::deconstruct(uniquedName);
3467dd7ccd2SJean Perier   return !result.second.modules.empty() &&
3477dd7ccd2SJean Perier          result.second.modules[0] == moduleName;
3487dd7ccd2SJean Perier }
349013160f6SJean Perier 
350013160f6SJean Perier static std::string
351013160f6SJean Perier mangleTypeDescriptorKinds(llvm::ArrayRef<std::int64_t> kinds) {
352013160f6SJean Perier   if (kinds.empty())
353013160f6SJean Perier     return "";
354a8c294d6SKazu Hirata   std::string result;
355013160f6SJean Perier   for (std::int64_t kind : kinds)
356cfd4c180SSlava Zakharin     result += (fir::kNameSeparator + std::to_string(kind)).str();
357013160f6SJean Perier   return result;
358013160f6SJean Perier }
359013160f6SJean Perier 
36087b2d1d0SValentin Clement static std::string getDerivedTypeObjectName(llvm::StringRef mangledTypeName,
36187b2d1d0SValentin Clement                                             const llvm::StringRef separator) {
362c373f581SjeanPerier   mangledTypeName =
363c373f581SjeanPerier       fir::NameUniquer::dropTypeConversionMarkers(mangledTypeName);
36487b2d1d0SValentin Clement   auto result = fir::NameUniquer::deconstruct(mangledTypeName);
36587b2d1d0SValentin Clement   if (result.first != fir::NameUniquer::NameKind::DERIVED_TYPE)
366013160f6SJean Perier     return "";
36787b2d1d0SValentin Clement   std::string varName = separator.str() + result.second.name +
368013160f6SJean Perier                         mangleTypeDescriptorKinds(result.second.kinds);
369013160f6SJean Perier   llvm::SmallVector<llvm::StringRef> modules;
370013160f6SJean Perier   for (const std::string &mod : result.second.modules)
371013160f6SJean Perier     modules.push_back(mod);
3722c143345SV Donaldson   llvm::SmallVector<llvm::StringRef> procs;
3732c143345SV Donaldson   for (const std::string &proc : result.second.procs)
3742c143345SV Donaldson     procs.push_back(proc);
3752c143345SV Donaldson   return fir::NameUniquer::doVariable(modules, procs, result.second.blockId,
3762c143345SV Donaldson                                       varName);
37787b2d1d0SValentin Clement }
37887b2d1d0SValentin Clement 
37987b2d1d0SValentin Clement std::string
38087b2d1d0SValentin Clement fir::NameUniquer::getTypeDescriptorName(llvm::StringRef mangledTypeName) {
381cfd4c180SSlava Zakharin   return getDerivedTypeObjectName(mangledTypeName,
382cfd4c180SSlava Zakharin                                   fir::kTypeDescriptorSeparator);
383cfd4c180SSlava Zakharin }
384cfd4c180SSlava Zakharin 
385cfd4c180SSlava Zakharin std::string fir::NameUniquer::getTypeDescriptorAssemblyName(
386cfd4c180SSlava Zakharin     llvm::StringRef mangledTypeName) {
387cfd4c180SSlava Zakharin   return replaceSpecialSymbols(getTypeDescriptorName(mangledTypeName));
38887b2d1d0SValentin Clement }
38987b2d1d0SValentin Clement 
39087b2d1d0SValentin Clement std::string fir::NameUniquer::getTypeDescriptorBindingTableName(
39187b2d1d0SValentin Clement     llvm::StringRef mangledTypeName) {
392cfd4c180SSlava Zakharin   return getDerivedTypeObjectName(mangledTypeName, fir::kBindingTableSeparator);
393013160f6SJean Perier }
394c373f581SjeanPerier 
39566d5ca2aSjeanPerier std::string
39666d5ca2aSjeanPerier fir::NameUniquer::getComponentInitName(llvm::StringRef mangledTypeName,
39766d5ca2aSjeanPerier                                        llvm::StringRef componentName) {
39866d5ca2aSjeanPerier 
39966d5ca2aSjeanPerier   std::string prefix =
400cfd4c180SSlava Zakharin       getDerivedTypeObjectName(mangledTypeName, fir::kComponentInitSeparator);
401cfd4c180SSlava Zakharin   return (prefix + fir::kNameSeparator + componentName).str();
40266d5ca2aSjeanPerier }
40366d5ca2aSjeanPerier 
404c373f581SjeanPerier llvm::StringRef
405c373f581SjeanPerier fir::NameUniquer::dropTypeConversionMarkers(llvm::StringRef mangledTypeName) {
406cfd4c180SSlava Zakharin   if (mangledTypeName.ends_with(fir::boxprocSuffix))
407cfd4c180SSlava Zakharin     return mangledTypeName.drop_back(fir::boxprocSuffix.size());
408c373f581SjeanPerier   return mangledTypeName;
409c373f581SjeanPerier }
410cfd4c180SSlava Zakharin 
411cfd4c180SSlava Zakharin std::string fir::NameUniquer::replaceSpecialSymbols(const std::string &name) {
412cfd4c180SSlava Zakharin   return std::regex_replace(name, std::regex{"\\."}, "X");
413cfd4c180SSlava Zakharin }
414*95b4128cSAbid Qadeer 
415*95b4128cSAbid Qadeer bool fir::NameUniquer::isSpecialSymbol(llvm::StringRef name) {
416*95b4128cSAbid Qadeer   return !name.empty() && (name[0] == '.' || name[0] == 'X');
417*95b4128cSAbid Qadeer }
418