1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
//===-- Mangler.cpp -------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "flang/Lower/Mangler.h"
#include "flang/Common/reference.h"
#include "flang/Lower/Support/Utils.h"
#include "flang/Optimizer/Builder/Todo.h"
#include "flang/Optimizer/Dialect/FIRType.h"
#include "flang/Optimizer/Support/InternalNames.h"
#include "flang/Semantics/tools.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/MD5.h"
/// Return all ancestor module and submodule scope names; all host procedure
/// and statement function scope names; and the innermost blockId containing
/// \p scope, including scope itself.
static std::tuple<llvm::SmallVector<llvm::StringRef>,
llvm::SmallVector<llvm::StringRef>, std::int64_t>
ancestors(const Fortran::semantics::Scope &scope,
Fortran::lower::mangle::ScopeBlockIdMap &scopeBlockIdMap) {
llvm::SmallVector<const Fortran::semantics::Scope *> scopes;
for (auto *scp = &scope; !scp->IsGlobal(); scp = &scp->parent())
scopes.push_back(scp);
llvm::SmallVector<llvm::StringRef> modules;
llvm::SmallVector<llvm::StringRef> procs;
std::int64_t blockId = 0;
for (auto iter = scopes.rbegin(), rend = scopes.rend(); iter != rend;
++iter) {
auto *scp = *iter;
switch (scp->kind()) {
case Fortran::semantics::Scope::Kind::Module:
modules.emplace_back(toStringRef(scp->symbol()->name()));
break;
case Fortran::semantics::Scope::Kind::Subprogram:
procs.emplace_back(toStringRef(scp->symbol()->name()));
break;
case Fortran::semantics::Scope::Kind::MainProgram:
// Do not use the main program name, if any, because it may collide
// with a procedure of the same name in another compilation unit.
// This is nonconformant, but universally allowed.
procs.emplace_back(llvm::StringRef(""));
break;
case Fortran::semantics::Scope::Kind::BlockConstruct: {
auto it = scopeBlockIdMap.find(scp);
assert(it != scopeBlockIdMap.end() && it->second &&
"invalid block identifier");
blockId = it->second;
} break;
default:
break;
}
}
return {modules, procs, blockId};
}
/// Return all ancestor module and submodule scope names; all host procedure
/// and statement function scope names; and the innermost blockId containing
/// \p symbol.
static std::tuple<llvm::SmallVector<llvm::StringRef>,
llvm::SmallVector<llvm::StringRef>, std::int64_t>
ancestors(const Fortran::semantics::Symbol &symbol,
Fortran::lower::mangle::ScopeBlockIdMap &scopeBlockIdMap) {
return ancestors(symbol.owner(), scopeBlockIdMap);
}
/// Return a globally unique string for a compiler generated \p name.
std::string
Fortran::lower::mangle::mangleName(std::string &name,
const Fortran::semantics::Scope &scope,
ScopeBlockIdMap &scopeBlockIdMap) {
llvm::SmallVector<llvm::StringRef> modules;
llvm::SmallVector<llvm::StringRef> procs;
std::int64_t blockId;
std::tie(modules, procs, blockId) = ancestors(scope, scopeBlockIdMap);
return fir::NameUniquer::doGenerated(modules, procs, blockId, name);
}
// Mangle the name of \p symbol to make it globally unique.
std::string
Fortran::lower::mangle::mangleName(const Fortran::semantics::Symbol &symbol,
ScopeBlockIdMap &scopeBlockIdMap,
bool keepExternalInScope) {
// Resolve module and host associations before mangling.
const auto &ultimateSymbol = symbol.GetUltimate();
// The Fortran and BIND(C) namespaces are counterintuitive. A BIND(C) name is
// substituted early, and has precedence over the Fortran name. This allows
// multiple procedures or objects with identical Fortran names to legally
// coexist. The BIND(C) name is unique.
if (auto *overrideName = ultimateSymbol.GetBindName())
return *overrideName;
// TODO: A procedure that inherits BIND(C) through another interface
// (procedure(iface)) should be dealt with in GetBindName() or some wrapper.
if (!Fortran::semantics::IsPointer(ultimateSymbol) &&
Fortran::semantics::IsBindCProcedure(ultimateSymbol) &&
Fortran::semantics::ClassifyProcedure(symbol) !=
Fortran::semantics::ProcedureDefinitionClass::Internal)
return ultimateSymbol.name().ToString();
llvm::StringRef symbolName = toStringRef(ultimateSymbol.name());
llvm::SmallVector<llvm::StringRef> modules;
llvm::SmallVector<llvm::StringRef> procs;
std::int64_t blockId;
// mangle ObjectEntityDetails or AssocEntityDetails symbols.
auto mangleObject = [&]() -> std::string {
std::tie(modules, procs, blockId) =
ancestors(ultimateSymbol, scopeBlockIdMap);
if (Fortran::semantics::IsNamedConstant(ultimateSymbol))
return fir::NameUniquer::doConstant(modules, procs, blockId, symbolName);
return fir::NameUniquer::doVariable(modules, procs, blockId, symbolName);
};
return std::visit(
Fortran::common::visitors{
[&](const Fortran::semantics::MainProgramDetails &) {
return fir::NameUniquer::doProgramEntry().str();
},
[&](const Fortran::semantics::SubprogramDetails &subpDetails) {
// Mangle external procedure without any scope prefix.
if (!keepExternalInScope &&
Fortran::semantics::IsExternal(ultimateSymbol))
return fir::NameUniquer::doProcedure(std::nullopt, std::nullopt,
symbolName);
// A separate module procedure must be mangled according to its
// declaration scope, not its definition scope.
const Fortran::semantics::Symbol *interface = &ultimateSymbol;
if (interface->attrs().test(Fortran::semantics::Attr::MODULE) &&
interface->owner().IsSubmodule() && !subpDetails.isInterface())
interface = subpDetails.moduleInterface();
assert(interface && "Separate module procedure must be declared");
std::tie(modules, procs, blockId) =
ancestors(*interface, scopeBlockIdMap);
return fir::NameUniquer::doProcedure(modules, procs, symbolName);
},
[&](const Fortran::semantics::ProcEntityDetails &) {
// Mangle procedure pointers and dummy procedures as variables.
if (Fortran::semantics::IsPointer(ultimateSymbol) ||
Fortran::semantics::IsDummy(ultimateSymbol)) {
std::tie(modules, procs, blockId) =
ancestors(ultimateSymbol, scopeBlockIdMap);
return fir::NameUniquer::doVariable(modules, procs, blockId,
symbolName);
}
// Otherwise, this is an external procedure, with or without an
// explicit EXTERNAL attribute. Mangle it without any prefix.
return fir::NameUniquer::doProcedure(std::nullopt, std::nullopt,
symbolName);
},
[&](const Fortran::semantics::ObjectEntityDetails &) {
return mangleObject();
},
[&](const Fortran::semantics::AssocEntityDetails &) {
return mangleObject();
},
[&](const Fortran::semantics::NamelistDetails &) {
std::tie(modules, procs, blockId) =
ancestors(ultimateSymbol, scopeBlockIdMap);
return fir::NameUniquer::doNamelistGroup(modules, procs,
symbolName);
},
[&](const Fortran::semantics::CommonBlockDetails &) {
return fir::NameUniquer::doCommonBlock(symbolName);
},
[&](const Fortran::semantics::ProcBindingDetails &procBinding) {
return mangleName(procBinding.symbol(), scopeBlockIdMap,
keepExternalInScope);
},
[&](const Fortran::semantics::DerivedTypeDetails &) -> std::string {
// Derived type mangling must use mangleName(DerivedTypeSpec) so
// that kind type parameter values can be mangled.
llvm::report_fatal_error(
"only derived type instances can be mangled");
},
[](const auto &) -> std::string { TODO_NOLOC("symbol mangling"); },
},
ultimateSymbol.details());
}
std::string
Fortran::lower::mangle::mangleName(const Fortran::semantics::Symbol &symbol,
bool keepExternalInScope) {
assert((symbol.owner().kind() !=
Fortran::semantics::Scope::Kind::BlockConstruct ||
symbol.has<Fortran::semantics::SubprogramDetails>()) &&
"block object mangling must specify a scopeBlockIdMap");
ScopeBlockIdMap scopeBlockIdMap;
return mangleName(symbol, scopeBlockIdMap, keepExternalInScope);
}
std::string Fortran::lower::mangle::mangleName(
const Fortran::semantics::DerivedTypeSpec &derivedType,
ScopeBlockIdMap &scopeBlockIdMap) {
// Resolve module and host associations before mangling.
const Fortran::semantics::Symbol &ultimateSymbol =
derivedType.typeSymbol().GetUltimate();
llvm::StringRef symbolName = toStringRef(ultimateSymbol.name());
llvm::SmallVector<llvm::StringRef> modules;
llvm::SmallVector<llvm::StringRef> procs;
std::int64_t blockId;
std::tie(modules, procs, blockId) =
ancestors(ultimateSymbol, scopeBlockIdMap);
llvm::SmallVector<std::int64_t> kinds;
for (const auto ¶m :
Fortran::semantics::OrderParameterDeclarations(ultimateSymbol)) {
const auto ¶mDetails =
param->get<Fortran::semantics::TypeParamDetails>();
if (paramDetails.attr() == Fortran::common::TypeParamAttr::Kind) {
const Fortran::semantics::ParamValue *paramValue =
derivedType.FindParameter(param->name());
assert(paramValue && "derived type kind parameter value not found");
const Fortran::semantics::MaybeIntExpr paramExpr =
paramValue->GetExplicit();
assert(paramExpr && "derived type kind param not explicit");
std::optional<int64_t> init =
Fortran::evaluate::ToInt64(paramValue->GetExplicit());
assert(init && "derived type kind param is not constant");
kinds.emplace_back(*init);
}
}
return fir::NameUniquer::doType(modules, procs, blockId, symbolName, kinds);
}
std::string Fortran::lower::mangle::demangleName(llvm::StringRef name) {
auto result = fir::NameUniquer::deconstruct(name);
return result.second.name;
}
//===----------------------------------------------------------------------===//
// Array Literals Mangling
//===----------------------------------------------------------------------===//
static std::string typeToString(Fortran::common::TypeCategory cat, int kind,
llvm::StringRef derivedName) {
switch (cat) {
case Fortran::common::TypeCategory::Integer:
return "i" + std::to_string(kind);
case Fortran::common::TypeCategory::Real:
return "r" + std::to_string(kind);
case Fortran::common::TypeCategory::Complex:
return "z" + std::to_string(kind);
case Fortran::common::TypeCategory::Logical:
return "l" + std::to_string(kind);
case Fortran::common::TypeCategory::Character:
return "c" + std::to_string(kind);
case Fortran::common::TypeCategory::Derived:
return derivedName.str();
}
llvm_unreachable("bad TypeCategory");
}
std::string Fortran::lower::mangle::mangleArrayLiteral(
size_t size, const Fortran::evaluate::ConstantSubscripts &shape,
Fortran::common::TypeCategory cat, int kind,
Fortran::common::ConstantSubscript charLen, llvm::StringRef derivedName) {
std::string typeId;
for (Fortran::evaluate::ConstantSubscript extent : shape)
typeId.append(std::to_string(extent)).append("x");
if (charLen >= 0)
typeId.append(std::to_string(charLen)).append("x");
typeId.append(typeToString(cat, kind, derivedName));
std::string name =
fir::NameUniquer::doGenerated("ro."s.append(typeId).append("."));
if (!size)
name += "null.";
return name;
}
std::string Fortran::lower::mangle::globalNamelistDescriptorName(
const Fortran::semantics::Symbol &sym) {
std::string name = mangleName(sym);
return IsAllocatableOrPointer(sym) ? name : name + ".desc"s;
}
|