Repository navigation
Expand file tree
/
Copy pathmapper.cpp
More file actions
80 lines (67 loc) · 2.47 KB
/
Copy pathmapper.cpp
File metadata and controls
80 lines (67 loc) · 2.47 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
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.
#include "converter/mapper.h"
#include <clang/AST/ExprCXX.h>
#include <cstdlib>
#include <format>
#include <optional>
#include <utility>
#include <vector>
#include "converter/converter_lib.h"
#include "converter/rules/matcher.h"
#include "converter/rules/registry.h"
#include "converter/translation_rule.h"
namespace cpp2rust::Mapper {
bool Contains(clang::ASTContext &ctx, clang::QualType qual_type) {
return RuleRegistry::Search(ctx, qual_type).first != nullptr;
}
bool Contains(clang::ASTContext &ctx, const clang::Expr *expr) {
return RuleRegistry::Search(ctx, expr).first != nullptr;
}
std::string MapFunctionName(clang::ASTContext &ctx,
const clang::FunctionDecl *decl) {
assert(decl);
if (!IsUserDefinedDecl(decl) && Matcher::HasRuleNamed(ctx, decl)) {
return std::format("libcc2rs::{}_{}", decl->getNameAsString(),
RuleRegistry::CurrentModel() == Model::kRefCount
? "refcount"
: "unsafe");
}
return GetNamedDeclAsString(decl->getCanonicalDecl());
}
std::string InstantiateTemplate(clang::ASTContext &ctx, const clang::Expr *expr,
unsigned n) {
auto [rule, subs] = RuleRegistry::Search(ctx, expr);
auto text = std::format("T{}", n);
if (!rule) {
return text;
}
auto &ty = subs.at(n - 1);
if (ty) {
ty = Matcher::MapBinding(subs, n - 1);
}
return Matcher::InstantiateTgt(subs, text);
}
std::string Map(clang::ASTContext &ctx, clang::QualType qual_type) {
auto [rule, subs] = RuleRegistry::Search(ctx, qual_type);
if (rule) {
return Matcher::InstantiateTgt(Matcher::MapBindings(subs),
rule->type_info.type);
}
return {};
}
std::string MapInitializer(clang::ASTContext &ctx, clang::QualType qual_type) {
auto [rule, subs] = RuleRegistry::Search(ctx, qual_type);
if (rule && !rule->initializer.empty()) {
return Matcher::InstantiateTgt(Matcher::MapBindings(subs),
rule->initializer);
}
return {};
}
std::string GetParamType(clang::ASTContext &ctx, const clang::Expr *expr,
unsigned index) {
auto [rule, subs] = RuleRegistry::Search(ctx, expr);
return Matcher::InstantiateTgt(Matcher::MapBindings(subs),
rule->params.at(index).type);
}
} // namespace cpp2rust::Mapper