Skip to content

Commit 8792efe

Browse files
authored
Merge pull request swiftlang#77115 from kubamracek/embedded-mangling-prefix
[Mangling] [NFC] Prepare for a new mangling prefix for Embedded Swift: $e
2 parents db39d05 + b760541 commit 8792efe

File tree

115 files changed

+639
-388
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+639
-388
lines changed

docs/ABI/Mangling.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Mangling
1212
mangled-name ::= '@__swiftmacro_' global // Swift mangling for filenames
1313
mangled-name ::= '_T0' global // Swift 4.0
1414
mangled-name ::= '$S' global // Swift 4.2
15+
mangled-name ::= '$e' global // Embedded Swift (unstable)
1516

1617
All Swift-mangled names begin with a common prefix. Since Swift 4.0, the
1718
compiler has used variations of the mangling described in this document, though

docs/EmbeddedSwift/ABI.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ The ABI of code generated by Embedded Swift is not currently stable. For a concr
1212

1313
Similarly, do not mix Embedded Swift code with full Swift code, as the ABIs are different. Details are described in the following sections.
1414

15+
## Symbol mangling under Embedded Swift
16+
17+
Since Swift 5.0, the stable ABI mangling schema uses the `$s` prefix on all Swift symbols. Because Embedded Swift's ABI differs from the stable ABI, and furthermore because it's not expected to be stable, Embedded Swift uses a `$e` mangling prefix. The logic and structure of the mangling stays the same, the only difference is the prefix.
18+
1519
## Calling convention of Embedded Swift
1620

1721
As of today, Embedded Swift has identical calling convention to full Swift. However, this does not need to continue in the future, and there should not be expectations that the ABI of Embedded Swift is compatible with full Swift.

include/swift/AST/ASTDemangler.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#ifndef SWIFT_AST_ASTDEMANGLER_H
2323
#define SWIFT_AST_ASTDEMANGLER_H
2424

25+
#include "swift/AST/ASTContext.h"
2526
#include "swift/AST/Types.h"
2627
#include "swift/Demangling/Demangler.h"
2728
#include "swift/Demangling/NamespaceMacros.h"
@@ -53,6 +54,7 @@ TypeDecl *getTypeDeclForUSR(ASTContext &ctx,
5354
/// just finds and builds things in the AST.
5455
class ASTBuilder {
5556
ASTContext &Ctx;
57+
Mangle::ManglingFlavor ManglingFlavor;
5658
Demangle::NodeFactory Factory;
5759

5860
/// The notional context in which we're writing and type-checking code.
@@ -97,8 +99,11 @@ class ASTBuilder {
9799

98100
static constexpr bool needsToPrecomputeParentGenericContextShapes = false;
99101

100-
explicit ASTBuilder(ASTContext &ctx, GenericSignature genericSig)
101-
: Ctx(ctx) {
102+
explicit ASTBuilder(ASTContext &ctx, GenericSignature genericSig) : Ctx(ctx) {
103+
ManglingFlavor = ctx.LangOpts.hasFeature(Feature::Embedded)
104+
? Mangle::ManglingFlavor::Embedded
105+
: Mangle::ManglingFlavor::Default;
106+
102107
for (auto *paramTy : genericSig.getGenericParams()) {
103108
if (paramTy->isParameterPack())
104109
ParameterPacks.emplace_back(paramTy->getDepth(), paramTy->getIndex());
@@ -112,6 +117,7 @@ class ASTBuilder {
112117
}
113118

114119
ASTContext &getASTContext() { return Ctx; }
120+
Mangle::ManglingFlavor getManglingFlavor() { return ManglingFlavor; }
115121
DeclContext *getNotionalDC();
116122

117123
Demangle::NodeFactory &getNodeFactory() { return Factory; }

include/swift/AST/ASTMangler.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_AST_ASTMANGLER_H
1414
#define SWIFT_AST_ASTMANGLER_H
1515

16+
#include "swift/AST/ASTContext.h"
1617
#include "swift/AST/Decl.h"
1718
#include "swift/AST/FreestandingMacroExpansion.h"
1819
#include "swift/AST/SILThunkKind.h"
@@ -44,6 +45,9 @@ enum class DestructorKind {
4445
/// The mangler for AST declarations.
4546
class ASTMangler : public Mangler {
4647
protected:
48+
#if 0 // STAGING
49+
const ASTContext &Context;
50+
#endif
4751
ModuleDecl *Mod = nullptr;
4852

4953
/// Optimize out protocol names if a type only conforms to one protocol.
@@ -185,6 +189,7 @@ class ASTMangler : public Mangler {
185189
HasSymbolQuery,
186190
};
187191

192+
// STAGING: legacy constructor for LLDB
188193
/// lldb overrides the defaulted argument to 'true'.
189194
ASTMangler(bool DWARFMangling = false) {
190195
if (DWARFMangling) {
@@ -193,6 +198,24 @@ class ASTMangler : public Mangler {
193198
}
194199
}
195200

201+
/// lldb overrides the defaulted argument to 'true'.
202+
ASTMangler(const ASTContext &Ctx, bool DWARFMangling = false) : ASTMangler(DWARFMangling) {}
203+
204+
#if 0
205+
/// lldb overrides the defaulted argument to 'true'.
206+
ASTMangler(const ASTContext &Ctx, bool DWARFMangling = false) : Context(Ctx) {
207+
if (DWARFMangling) {
208+
DWARFMangling = true;
209+
RespectOriginallyDefinedIn = false;
210+
}
211+
Flavor = Ctx.LangOpts.hasFeature(Feature::Embedded)
212+
? ManglingFlavor::Embedded
213+
: ManglingFlavor::Default;
214+
}
215+
216+
const ASTContext &getASTContext() { return Context; }
217+
#endif
218+
196219
void addTypeSubstitution(Type type, GenericSignature sig) {
197220
type = dropProtocolsFromAssociatedTypes(type, sig);
198221
addSubstitution(type.getPointer());

include/swift/Basic/Mangler.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_BASIC_MANGLER_H
1414
#define SWIFT_BASIC_MANGLER_H
1515

16+
#include "swift/Demangling/ManglingFlavor.h"
1617
#include "swift/Demangling/ManglingUtils.h"
1718
#include "swift/Demangling/NamespaceMacros.h"
1819
#include "swift/Basic/Debug.h"
@@ -70,6 +71,8 @@ class Mangler {
7071

7172
size_t MaxNumWords = 26;
7273

74+
ManglingFlavor Flavor = ManglingFlavor::Default;
75+
7376
/// If enabled, non-ASCII names are encoded in modified Punycode.
7477
bool UsePunycode = true;
7578

@@ -135,7 +138,7 @@ class Mangler {
135138
void finalize(llvm::raw_ostream &stream);
136139

137140
/// Verify that demangling and remangling works.
138-
static void verify(StringRef mangledName);
141+
static void verify(StringRef mangledName, ManglingFlavor Flavor);
139142

140143
SWIFT_DEBUG_DUMP;
141144

include/swift/Demangling/Demangle.h

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define SWIFT_DEMANGLING_DEMANGLE_H
2121

2222
#include "swift/Demangling/Errors.h"
23+
#include "swift/Demangling/ManglingFlavor.h"
2324
#include "swift/Demangling/NamespaceMacros.h"
2425
#include "llvm/ADT/StringRef.h"
2526
#include "llvm/Support/Compiler.h"
@@ -673,24 +674,27 @@ class [[nodiscard]] ManglingErrorOr {
673674
};
674675

675676
/// Remangle a demangled parse tree.
676-
ManglingErrorOr<std::string> mangleNode(NodePointer root);
677+
ManglingErrorOr<std::string>
678+
mangleNode(NodePointer root,
679+
Mangle::ManglingFlavor Flavor = Mangle::ManglingFlavor::Default);
677680

678-
using SymbolicResolver =
679-
llvm::function_ref<Demangle::NodePointer (SymbolicReferenceKind,
680-
const void *)>;
681+
using SymbolicResolver = llvm::function_ref<Demangle::NodePointer(
682+
SymbolicReferenceKind, const void *)>;
681683

682684
/// Remangle a demangled parse tree, using a callback to resolve
683685
/// symbolic references.
684-
ManglingErrorOr<std::string> mangleNode(NodePointer root, SymbolicResolver resolver);
686+
ManglingErrorOr<std::string>
687+
mangleNode(NodePointer root, SymbolicResolver resolver,
688+
Mangle::ManglingFlavor Flavor = Mangle::ManglingFlavor::Default);
685689

686690
/// Remangle a demangled parse tree, using a callback to resolve
687691
/// symbolic references.
688692
///
689693
/// The returned string is owned by \p Factory. This means \p Factory must stay
690694
/// alive as long as the returned string is used.
691-
ManglingErrorOr<llvm::StringRef> mangleNode(NodePointer root,
692-
SymbolicResolver resolver,
693-
NodeFactory &Factory);
695+
ManglingErrorOr<llvm::StringRef>
696+
mangleNode(NodePointer root, SymbolicResolver resolver, NodeFactory &Factory,
697+
Mangle::ManglingFlavor Flavor = Mangle::ManglingFlavor::Default);
694698

695699
/// Remangle in the old mangling scheme.
696700
///
@@ -810,9 +814,9 @@ llvm::StringRef makeSymbolicMangledNameStringRef(const char *base);
810814

811815
/// Produce the mangled name for the nominal type descriptor of a type
812816
/// referenced by its module and type name.
813-
std::string mangledNameForTypeMetadataAccessor(llvm::StringRef moduleName,
814-
llvm::StringRef typeName,
815-
Node::Kind typeKind);
817+
std::string mangledNameForTypeMetadataAccessor(
818+
llvm::StringRef moduleName, llvm::StringRef typeName, Node::Kind typeKind,
819+
Mangle::ManglingFlavor Flavor = Mangle::ManglingFlavor::Default);
816820

817821
SWIFT_END_INLINE_NAMESPACE
818822
} // end namespace Demangle

include/swift/Demangling/Demangler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define SWIFT_DEMANGLING_DEMANGLER_H
2121

2222
#include "swift/Demangling/Demangle.h"
23+
#include "swift/Demangling/ManglingFlavor.h"
2324
#include "swift/Demangling/NamespaceMacros.h"
2425

2526
//#define NODE_FACTORY_DEBUGGING
@@ -405,6 +406,8 @@ class Demangler : public NodeFactory {
405406
/// as part of the name.
406407
bool IsOldFunctionTypeMangling = false;
407408

409+
Mangle::ManglingFlavor Flavor = Mangle::ManglingFlavor::Default;
410+
408411
Vector<NodePointer> NodeStack;
409412
Vector<NodePointer> Substitutions;
410413

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===--- ManglingFlavor.h - Swift name mangling -----------------*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_DEMANGLING_MANGLINGFLAVOR_H
14+
#define SWIFT_DEMANGLING_MANGLINGFLAVOR_H
15+
16+
#include "swift/Demangling/NamespaceMacros.h"
17+
18+
#include <cstdint>
19+
20+
namespace swift {
21+
namespace Mangle {
22+
SWIFT_BEGIN_INLINE_NAMESPACE
23+
24+
/// Which mangling style and prefix to use.
25+
enum class ManglingFlavor: uint8_t {
26+
/// Default mangling with the ABI stable $s prefix
27+
Default,
28+
/// Embedded Swift's mangling with $e prefix
29+
Embedded,
30+
};
31+
32+
SWIFT_END_INLINE_NAMESPACE
33+
} // end namespace Mangle
34+
} // end namespace swift
35+
36+
#endif // SWIFT_DEMANGLING_MANGLINGFLAVOR_H

include/swift/Demangling/ManglingMacros.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ _Pragma("clang diagnostic push")
2222
_Pragma("clang diagnostic ignored \"-Wdollar-in-identifier-extension\"")
2323
#endif
2424
#define MANGLING_PREFIX $s
25+
#define MANGLING_PREFIX_EMBEDDED $e
2526
#if defined(__clang__)
2627
_Pragma("clang diagnostic pop")
2728
#endif
2829

2930
#define MANGLING_PREFIX_STR MANGLE_AS_STRING(MANGLING_PREFIX)
31+
#define MANGLING_PREFIX_EMBEDDED_STR MANGLE_AS_STRING(MANGLING_PREFIX_EMBEDDED)
3032

3133
// The following macros help to create symbol manglings. They can be used
3234
// if a mangled name is needed at compile-time, e.g. for variable names in the

include/swift/Demangling/TypeDecoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ class TypeDecoder {
720720
return decodeMangledType(genericArgs->getChild(0), depth + 1);
721721
}
722722
case NodeKind::BuiltinTypeName: {
723-
auto mangling = Demangle::mangleNode(Node);
723+
auto mangling = Demangle::mangleNode(Node, Builder.getManglingFlavor());
724724
if (!mangling.isSuccess()) {
725725
return MAKE_NODE_TYPE_ERROR(Node,
726726
"failed to mangle node (%d:%u)",

0 commit comments

Comments
 (0)