Repository navigation
Expand file tree
/
Copy pathfunctions.cpp
More file actions
160 lines (139 loc) · 4.56 KB
/
Copy pathfunctions.cpp
File metadata and controls
160 lines (139 loc) · 4.56 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
#include "functions.h"
namespace bpftrace {
namespace {
std::string arg_types_str(const std::vector<SizedType> &arg_types)
{
std::string str = "(";
bool first = true;
for (const SizedType &arg_type : arg_types) {
if (!first)
str += ", ";
str += typestr(arg_type);
first = false;
}
str += ")";
return str;
}
std::string param_types_str(const std::vector<Param> ¶ms)
{
std::string str = "(";
bool first = true;
for (const Param ¶m : params) {
if (!first)
str += ", ";
str += typestr(param.type());
first = false;
}
str += ")";
return str;
}
} // namespace
const Function *FunctionRegistry::add(Function::Origin origin,
std::string_view name,
const SizedType &return_type,
const std::vector<Param> ¶ms)
{
return add(origin, {}, name, return_type, params);
}
const Function *FunctionRegistry::add(Function::Origin origin,
std::string_view ns,
std::string_view name,
const SizedType &return_type,
const std::vector<Param> ¶ms)
{
FqName fq_name{
.ns = std::string{ ns },
.name = std::string{ name },
};
// Check for duplicate function definitions
// The assumption is that builtin functions are all added to the registry
// before any user-defined functions.
// Builtin functions can be duplicated. Other functions can not.
for (const Function &func : funcs_by_fq_name_[fq_name]) {
if (func.origin() != Function::Origin::Builtin) {
return nullptr;
}
}
all_funcs_.push_back(std::make_unique<Function>(
origin, std::string{ name }, return_type, params));
Function &new_func = *all_funcs_.back().get();
funcs_by_fq_name_[fq_name].emplace_back(new_func);
return &new_func;
}
namespace {
bool can_implicit_cast(const SizedType &from, const SizedType &to)
{
if (from.FitsInto(to))
return true;
if (from.IsStringTy() && to.IsPtrTy() && to.GetPointeeTy().IsIntTy() &&
to.GetPointeeTy().GetSize() == 1) {
// Allow casting from string to int8* or uint8*
return true;
}
// Builtin and script functions do not care about string sizes. External
// functions cannot be defined to accept string types (they'd take char*)
if (from.IsStringTy() && to.IsStringTy())
return true;
return false;
}
} // namespace
// Find the best function by name for the given argument types.
//
// Returns either a single function or nullptr, when no such function exists.
//
// When there are multiple candidate functions with the same name, prefer the
// non-builtin over the builtin function.
//
// Valid functions have the correct name and all arguments can be implicitly
// casted into all parameter types.
const Function *FunctionRegistry::get(std::string_view ns,
std::string_view name,
const std::vector<SizedType> &arg_types,
const ast::Node &node) const
{
FqName fq_name = {
.ns = std::string{ ns },
.name = std::string{ name },
};
auto it = funcs_by_fq_name_.find(fq_name);
if (it == funcs_by_fq_name_.end()) {
node.addError() << "Function not found: '" << name << "'";
return nullptr;
}
const auto &candidates = it->second;
// We disallow duplicate functions other than for builtins, so expect at most
// two exact matches.
assert(candidates.size() <= 2);
// No candidates => no match
// 1 candidate => use it
// 2 candidates => use non-builtin candidate
const Function *candidate = nullptr;
for (const Function &func : candidates) {
candidate = &func;
if (candidate->origin() != Function::Origin::Builtin)
break;
}
// Validate that the candidate's parameters can take our arguments
if (candidate) {
bool valid = true;
if (candidate->params().size() != arg_types.size()) {
valid = false;
} else {
for (size_t i = 0; i < arg_types.size(); i++) {
if (!can_implicit_cast(arg_types[i], candidate->params()[i].type())) {
valid = false;
break;
}
}
}
if (valid)
return candidate;
}
auto &err = node.addError();
err << "Cannot call function '" << name
<< "' using argument types: " << arg_types_str(arg_types);
err.addHint() << "Candidate function:\n " << candidate->name()
<< param_types_str(candidate->params());
return nullptr;
}
} // namespace bpftrace