Repository navigation
Expand file tree
/
Copy pathstring_matcher.cpp
More file actions
396 lines (345 loc) · 9.79 KB
/
Copy pathstring_matcher.cpp
File metadata and controls
396 lines (345 loc) · 9.79 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.
#include <algorithm>
#include <cassert>
#include <cctype>
#include <string_view>
#include "converter/converter_lib.h"
#include "converter/printer.h"
#include "converter/rules/matcher.h"
#include "converter/rules/registry.h"
namespace cpp2rust::Matcher {
namespace {
// Attempts to unify an instantiated C++ type or function signature with a
// corresponding template pattern. If the two match structurally, it returns
// a mapping from template parameter names (e.g., "T1") to their concrete
// instantiated types (e.g., "int"). If no match is possible, returns nullopt.
//
// Example:
// template_str = "std::vector<T1>::vector()"
// instantiated = "std::vector<int>::vector()"
// result = { "int" }
std::optional<Bindings> matchTemplate(const std::string &template_str,
const std::string &instantiated) {
auto matchLiteralAt = [&](const std::string &input_str, size_t pos,
std::string_view literal, size_t &end_pos) -> bool {
size_t i = pos;
size_t j = 0;
while (true) {
while (i < input_str.size() && std::isspace(input_str[i])) {
i++;
}
while (j < literal.size() && std::isspace(literal[j])) {
j++;
}
if (j == literal.size()) {
end_pos = i;
return true;
}
if (i >= input_str.size()) {
return false;
}
if (input_str[i] != literal[j]) {
return false;
}
i++;
j++;
}
};
auto findNextLiteralSameDepth = [&](const std::string &s, size_t start,
std::string_view lit) -> size_t {
int ang = 0;
int par = 0;
int sq = 0;
for (size_t i = 0; i < s.size() && i < start; i++) {
switch (s[i]) {
case '<': {
ang++;
break;
}
case '>': {
ang--;
break;
}
case '(': {
par++;
break;
}
case ')': {
par--;
break;
}
case '[': {
sq++;
break;
}
case ']': {
sq--;
break;
}
default:
break;
}
assert(ang >= 0 && par >= 0 && sq >= 0 && "Unbalanced ang, par or sq");
}
int base_ang = ang;
int base_par = par;
int base_sq = sq;
for (size_t i = start; i <= s.size(); i++) {
if (ang == base_ang && par == base_par && sq == base_sq) {
size_t end_i = 0;
if (matchLiteralAt(s, i, lit, end_i)) {
return i;
}
}
if (i == s.size()) {
break;
}
char c = s[i];
switch (c) {
case '<': {
ang++;
break;
}
case '>': {
ang--;
break;
}
case '(': {
par++;
break;
}
case ')': {
par--;
break;
}
case '[': {
sq++;
break;
}
case ']': {
sq--;
break;
}
default:
break;
}
if (ang < 0 || par < 0 || sq < 0) {
return std::string::npos;
}
}
return std::string::npos;
};
Bindings captured;
size_t ti = 0;
size_t si = 0;
while (ti < template_str.size()) {
if (template_str[ti] == 'T' && ti + 1 < template_str.size() &&
std::isdigit(template_str[ti + 1])) {
size_t tj = ti + 2;
while (tj < template_str.size() && std::isdigit(template_str[tj])) {
tj++;
}
size_t type_idx = std::stoi(&template_str[ti + 1]) - 1;
assert(type_idx < TranslationRule::kMaxGenerics &&
"template placeholder exceeds kMaxGenerics");
ti = tj;
std::string_view nextLit;
size_t scan = ti;
while (scan < template_str.size()) {
if (template_str[scan] == 'T' && scan + 1 < template_str.size() &&
std::isdigit(template_str[scan + 1])) {
break;
}
scan++;
}
nextLit = std::string_view(template_str).substr(ti, scan - ti);
captured.resize(std::max(captured.size(), type_idx + 1));
auto &repl = captured[type_idx];
if (repl.has_value()) {
size_t end_pos = 0;
if (!matchLiteralAt(instantiated, si, *repl, end_pos)) {
return std::nullopt;
}
si = end_pos;
} else {
if (!nextLit.empty()) {
size_t k = findNextLiteralSameDepth(instantiated, si, nextLit);
if (k == std::string::npos) {
return std::nullopt;
}
size_t a = si;
size_t b = k;
while (a < b && std::isspace(instantiated[a])) {
a++;
}
while (b > a && std::isspace(instantiated[b - 1])) {
b--;
}
repl = instantiated.substr(a, b - a);
si = k;
} else {
size_t a = si;
size_t b = instantiated.size();
while (a < b && std::isspace(instantiated[a])) {
a++;
}
while (b > a && std::isspace(instantiated[b - 1])) {
b--;
}
repl = instantiated.substr(a, b - a);
si = instantiated.size();
}
}
if (!nextLit.empty()) {
size_t end_pos = 0;
if (!matchLiteralAt(instantiated, si, nextLit, end_pos)) {
return std::nullopt;
}
si = end_pos;
ti += nextLit.size();
}
} else {
size_t tj = ti;
while (tj < template_str.size()) {
if (template_str[tj] == 'T' && tj + 1 < template_str.size() &&
std::isdigit(template_str[tj + 1])) {
break;
}
++tj;
}
auto lit = std::string_view(template_str).substr(ti, tj - ti);
size_t end_pos = 0;
if (!matchLiteralAt(instantiated, si, lit, end_pos)) {
return std::nullopt;
}
si = end_pos;
ti = tj;
}
}
while (si < instantiated.size() && std::isspace(instantiated[si])) {
si++;
}
if (si != instantiated.size()) {
return std::nullopt;
}
return captured;
}
std::string exprKey(const std::string &str) {
// Extract the function name from something like
// const T1 & std::foo<T1, T2>::fn_name(args)
auto n = str.find_first_of('(');
if (n == std::string::npos) {
n = str.size();
}
// Walk backwards from '(' tracking <> depth:
// - skip characters inside template arguments (depth > 0)
// - stop at the first space outside all angle brackets
std::string result;
int depth = 0;
for (int i = (int)n - 1; i >= 0; --i) {
char c = str[i];
if (c == '>')
++depth;
else if (c == '<')
--depth;
else if (c == ' ' && depth == 0)
break;
else if (depth == 0)
result += c;
}
std::reverse(result.begin(), result.end());
return result;
}
std::string typeKey(const std::string &str) {
auto n = str.find_first_of("<[");
if (n == std::string::npos || str[n] == '<') {
return str.substr(0, n);
}
// something like int[][] or T1[] -> []
return str.substr(n + 1);
}
template <typename Rule, typename Candidates>
Match<Rule> search(Candidates candidates, const std::string &txt) {
Rule *rule = nullptr;
Bindings subs;
for (auto &[_, this_rule] : candidates) {
auto this_subs = matchTemplate(this_rule.src, txt);
if (!this_subs) {
continue;
}
// tie breaker: prefer more specific rules (usually the longer ones)
if (!rule || this_rule.src.size() > rule->src.size()) {
rule = &this_rule;
subs = *std::move(this_subs);
}
}
return {rule, std::move(subs)};
}
Match<TranslationRule::ExprRule> searchExpr(const std::string &txt) {
return search<TranslationRule::ExprRule>(
RuleRegistry::ExprCandidates(exprKey(txt)), txt);
}
Match<TranslationRule::TypeRule> searchType(const std::string &txt) {
return search<TranslationRule::TypeRule>(
RuleRegistry::TypeCandidates(typeKey(txt)), txt);
}
std::string mapTypeString(const std::string &cpp_type) {
auto [rule, subs] = searchType(cpp_type);
if (!rule) {
llvm::errs() << "cpp_type: " << cpp_type << '\n';
assert(0 && "Type is not present in the registry");
}
for (auto &ty : subs) {
if (ty) {
ty = mapTypeString(*ty);
}
}
return InstantiateTgt(subs, rule->type_info.type);
}
} // namespace
std::string Key(const TranslationRule::ExprRule &rule) {
return exprKey(rule.src);
}
std::string Key(const TranslationRule::TypeRule &rule) {
return typeKey(rule.src);
}
Match<TranslationRule::ExprRule> Find(clang::ASTContext &ctx,
const clang::Expr *expr) {
auto qualified_name = Printer::ToString(ctx, expr);
auto res = searchExpr(qualified_name);
log() << "search expr " << qualified_name << ", result:\n";
if (res.first) {
res.first->dump();
} else {
log() << "None\n";
}
return res;
}
Match<TranslationRule::TypeRule> Find(clang::ASTContext &ctx,
clang::QualType type) {
auto sugared = Printer::ToString(ctx, type, Printer::ScalarSugar::kPreserve);
if (auto res = searchType(sugared); res.first) {
log() << "search type " << sugared
<< ", result: " << res.first->type_info.type << '\n';
return res;
}
auto desugared = Printer::ToString(ctx, type);
if (desugared == sugared) {
log() << "search type " << desugared << ", result: None\n";
return {};
}
auto res = searchType(desugared);
log() << "search type " << desugared
<< ", result: " << (res.first ? res.first->type_info.type : "None")
<< '\n';
return res;
}
bool HasRuleNamed(clang::ASTContext &ctx, const clang::FunctionDecl *decl) {
return !RuleRegistry::ExprCandidates(exprKey(Printer::ToString(ctx, decl)))
.empty();
}
std::string MapBinding(const Bindings &bindings, unsigned n) {
return mapTypeString(bindings.at(n).value());
}
} // namespace cpp2rust::Matcher