forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RemanglerBase.h
179 lines (142 loc) · 5.1 KB
/
RemanglerBase.h
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
//===--- Demangler.h - String to Node-Tree Demangling -----------*- C++ -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file contains shared code between the old and new remanglers.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_DEMANGLING_BASEREMANGLER_H
#define SWIFT_DEMANGLING_BASEREMANGLER_H
#include "swift/Demangling/Demangler.h"
#include "swift/Demangling/NamespaceMacros.h"
#include <unordered_map>
using namespace swift::Demangle;
using llvm::StringRef;
namespace swift {
namespace Demangle {
SWIFT_BEGIN_INLINE_NAMESPACE
#define RETURN_IF_ERROR(x) \
do { \
ManglingError err = (x); \
if (!err.isSuccess()) \
return err; \
} while (0)
// An entry in the remangler's substitution map.
class SubstitutionEntry {
Node *TheNode = nullptr;
size_t StoredHash = 0;
bool treatAsIdentifier = false;
public:
void setNode(Node *node, bool treatAsIdentifier) {
this->treatAsIdentifier = treatAsIdentifier;
TheNode = node;
deepHash(node);
}
struct Hasher {
size_t operator()(const SubstitutionEntry &entry) const {
return entry.StoredHash;
}
};
private:
friend bool operator==(const SubstitutionEntry &lhs,
const SubstitutionEntry &rhs) {
if (lhs.StoredHash != rhs.StoredHash)
return false;
if (lhs.treatAsIdentifier != rhs.treatAsIdentifier)
return false;
if (lhs.treatAsIdentifier) {
return identifierEquals(lhs.TheNode, rhs.TheNode);
}
return lhs.deepEquals(lhs.TheNode, rhs.TheNode);
}
static bool identifierEquals(Node *lhs, Node *rhs);
void combineHash(size_t newValue) {
StoredHash = 33 * StoredHash + newValue;
}
void deepHash(Node *node);
bool deepEquals(Node *lhs, Node *rhs) const;
};
/// The output string for the Remangler.
///
/// It's allocating the string with the provided Factory.
class RemanglerBuffer {
CharVector Stream;
NodeFactory &Factory;
public:
RemanglerBuffer(NodeFactory &Factory) : Factory(Factory) {
Stream.init(Factory, 32);
}
void reset(size_t toPos) { Stream.resetSize(toPos); }
StringRef strRef() const { return Stream.str(); }
RemanglerBuffer &operator<<(char c) & {
Stream.push_back(c, Factory);
return *this;
}
RemanglerBuffer &operator<<(llvm::StringRef Value) & {
Stream.append(Value, Factory);
return *this;
}
RemanglerBuffer &operator<<(int n) & {
Stream.append(n, Factory);
return *this;
}
RemanglerBuffer &operator<<(unsigned n) & {
Stream.append((unsigned long long)n, Factory);
return *this;
}
RemanglerBuffer &operator<<(unsigned long n) & {
Stream.append((unsigned long long)n, Factory);
return *this;
}
RemanglerBuffer &operator<<(unsigned long long n) & {
Stream.append(n, Factory);
return *this;
}
};
/// The base class for the old and new remangler.
class RemanglerBase {
protected:
// Used to allocate temporary nodes and the output string (in Buffer).
NodeFactory &Factory;
// An efficient hash-map implementation in the spirit of llvm's SmallPtrSet:
// The first 16 substitutions are stored in an inline-allocated array to avoid
// malloc calls in the common case.
// Lookup is still reasonable fast because there are max 16 elements in the
// array.
static const size_t InlineSubstCapacity = 16;
SubstitutionEntry InlineSubstitutions[InlineSubstCapacity];
size_t NumInlineSubsts = 0;
// The "overflow" for InlineSubstitutions. Only if InlineSubstitutions is
// full, new substitutions are stored in OverflowSubstitutions.
std::unordered_map<SubstitutionEntry, unsigned, SubstitutionEntry::Hasher>
OverflowSubstitutions;
RemanglerBuffer Buffer;
protected:
RemanglerBase(NodeFactory &Factory) : Factory(Factory), Buffer(Factory) { }
/// Find a substitution and return its index.
/// Returns -1 if no substitution is found.
int findSubstitution(const SubstitutionEntry &entry);
/// Adds a substitution.
void addSubstitution(const SubstitutionEntry &entry);
/// Resets the output string buffer to \p toPos.
void resetBuffer(size_t toPos) { Buffer.reset(toPos); }
public:
/// Append a custom string to the output buffer.
void append(StringRef str) { Buffer << str; }
StringRef getBufferStr() const { return Buffer.strRef(); }
std::string str() {
return getBufferStr().str();
}
};
SWIFT_END_INLINE_NAMESPACE
} // end namespace Demangle
} // end namespace swift
#endif // SWIFT_DEMANGLING_BASEREMANGLER_H