forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIRGenMangler.cpp
183 lines (164 loc) · 5.97 KB
/
IRGenMangler.cpp
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
//===--- IRGenMangler.cpp - mangling of IRGen symbols ---------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "IRGenMangler.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/Demangling/ManglingMacros.h"
#include "swift/Demangling/Demangle.h"
#include "swift/ABI/MetadataValues.h"
#include "swift/ClangImporter/ClangModule.h"
#include "llvm/Support/SaveAndRestore.h"
using namespace swift;
using namespace irgen;
const char *getManglingForWitness(swift::Demangle::ValueWitnessKind kind) {
switch (kind) {
#define VALUE_WITNESS(MANGLING, NAME) \
case swift::Demangle::ValueWitnessKind::NAME: return #MANGLING;
#include "swift/Demangling/ValueWitnessMangling.def"
}
llvm_unreachable("not a function witness");
}
std::string IRGenMangler::mangleValueWitness(Type type, ValueWitness witness) {
beginMangling();
appendType(type);
const char *Code = nullptr;
switch (witness) {
#define GET_MANGLING(ID) \
case ValueWitness::ID: Code = getManglingForWitness(swift::Demangle::ValueWitnessKind::ID); break;
GET_MANGLING(InitializeBufferWithCopyOfBuffer) \
GET_MANGLING(Destroy) \
GET_MANGLING(InitializeWithCopy) \
GET_MANGLING(AssignWithCopy) \
GET_MANGLING(InitializeWithTake) \
GET_MANGLING(AssignWithTake) \
GET_MANGLING(GetEnumTagSinglePayload) \
GET_MANGLING(StoreEnumTagSinglePayload) \
GET_MANGLING(StoreExtraInhabitant) \
GET_MANGLING(GetExtraInhabitantIndex) \
GET_MANGLING(GetEnumTag) \
GET_MANGLING(DestructiveProjectEnumData) \
GET_MANGLING(DestructiveInjectEnumTag)
#undef GET_MANGLING
case ValueWitness::Size:
case ValueWitness::Flags:
case ValueWitness::Stride:
case ValueWitness::ExtraInhabitantFlags:
llvm_unreachable("not a function witness");
}
appendOperator("w", Code);
return finalize();
}
std::string IRGenMangler::manglePartialApplyForwarder(StringRef FuncName) {
if (FuncName.empty()) {
beginMangling();
} else {
if (FuncName.startswith(MANGLING_PREFIX_STR)) {
Buffer << FuncName;
} else {
beginMangling();
appendIdentifier(FuncName);
}
}
appendOperator("TA");
return finalize();
}
SymbolicMangling
IRGenMangler::mangleTypeForReflection(IRGenModule &IGM,
Type Ty) {
Mod = IGM.getSwiftModule();
OptimizeProtocolNames = false;
llvm::SaveAndRestore<std::function<bool (const DeclContext *)>>
SymbolicReferencesForLocalTypes(CanSymbolicReference);
if (IGM.CurSourceFile
&& !isa<ClangModuleUnit>(IGM.CurSourceFile)
&& !IGM.getOptions().IntegratedREPL) {
CanSymbolicReference = [&](const DeclContext *dc) -> bool {
// Symbolically reference types that are defined in the same file unit
// as we're referencing from.
//
// We could eventually improve this to reference any type that ends
// up with its nominal type descriptor in the same linked binary as us,
// but IRGen doesn't know that with much certainty currently.
return dc->getModuleScopeContext() == IGM.CurSourceFile
&& isa<NominalTypeDecl>(dc)
&& !isa<ProtocolDecl>(dc);
};
}
SymbolicReferences.clear();
appendType(Ty);
return {finalize(), std::move(SymbolicReferences)};
}
std::string IRGenMangler::mangleTypeForLLVMTypeName(CanType Ty) {
// To make LLVM IR more readable we always add a 'T' prefix so that type names
// don't start with a digit and don't need to be quoted.
Buffer << 'T';
if (auto P = dyn_cast<ProtocolType>(Ty)) {
appendProtocolName(P->getDecl(), /*allowStandardSubstitution=*/false);
appendOperator("P");
} else {
appendType(Ty);
}
return finalize();
}
std::string IRGenMangler::
mangleProtocolForLLVMTypeName(ProtocolCompositionType *type) {
ExistentialLayout layout = type->getExistentialLayout();
if (type->isAny()) {
Buffer << "Any";
} else if (layout.isAnyObject()) {
Buffer << "AnyObject";
} else {
// To make LLVM IR more readable we always add a 'T' prefix so that type names
// don't start with a digit and don't need to be quoted.
Buffer << 'T';
auto protocols = layout.getProtocols();
for (unsigned i = 0, e = protocols.size(); i != e; ++i) {
appendProtocolName(protocols[i]->getDecl());
if (i == 0)
appendOperator("_");
}
if (auto superclass = layout.explicitSuperclass) {
// We share type infos for different instantiations of a generic type
// when the archetypes have the same exemplars. We cannot mangle
// archetypes, and the mangling does not have to be unique, so we just
// mangle the unbound generic form of the type.
if (superclass->hasArchetype()) {
superclass = superclass->getClassOrBoundGenericClass()
->getDeclaredType();
}
appendType(CanType(superclass));
appendOperator("Xc");
} else if (layout.getLayoutConstraint()) {
appendOperator("Xl");
} else {
appendOperator("p");
}
}
return finalize();
}
std::string IRGenMangler::
mangleSymbolNameForSymbolicMangling(const SymbolicMangling &mangling) {
beginManglingWithoutPrefix();
static const char prefix[] = "symbolic ";
Buffer << prefix << mangling.String;
auto prefixLen = sizeof(prefix) - 1;
for (auto &symbol : mangling.SymbolicReferences) {
// Fill in the placeholder space with something printable.
auto dc = symbol.first;
auto offset = symbol.second;
Storage[prefixLen + offset] = Storage[prefixLen + offset+1] =
Storage[prefixLen + offset+2] = Storage[prefixLen + offset+3] = '_';
Buffer << ' ';
appendContext(dc);
}
return finalize();
}