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
|
//===- RegistryManager.cpp - Matcher registry -----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Registry map populated at static initialization time.
//
//===----------------------------------------------------------------------===//
#include "RegistryManager.h"
#include "mlir/Query/Matcher/Registry.h"
#include <set>
#include <utility>
namespace mlir::query::matcher {
namespace {
// Enum to string for autocomplete.
static std::string asArgString(ArgKind kind) {
switch (kind) {
case ArgKind::Boolean:
return "Boolean";
case ArgKind::Matcher:
return "Matcher";
case ArgKind::Signed:
return "Signed";
case ArgKind::String:
return "String";
}
llvm_unreachable("Unhandled ArgKind");
}
} // namespace
void Registry::registerMatcherDescriptor(
llvm::StringRef matcherName,
std::unique_ptr<internal::MatcherDescriptor> callback) {
assert(!constructorMap.contains(matcherName));
constructorMap[matcherName] = std::move(callback);
}
std::optional<MatcherCtor>
RegistryManager::lookupMatcherCtor(llvm::StringRef matcherName,
const Registry &matcherRegistry) {
auto it = matcherRegistry.constructors().find(matcherName);
return it == matcherRegistry.constructors().end()
? std::optional<MatcherCtor>()
: it->second.get();
}
std::vector<ArgKind> RegistryManager::getAcceptedCompletionTypes(
llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> context) {
// Starting with the above seed of acceptable top-level matcher types, compute
// the acceptable type set for the argument indicated by each context element.
std::set<ArgKind> typeSet;
typeSet.insert(ArgKind::Matcher);
for (const auto &ctxEntry : context) {
MatcherCtor ctor = ctxEntry.first;
unsigned argNumber = ctxEntry.second;
std::vector<ArgKind> nextTypeSet;
if (ctor->isVariadic() || argNumber < ctor->getNumArgs())
ctor->getArgKinds(argNumber, nextTypeSet);
typeSet.insert(nextTypeSet.begin(), nextTypeSet.end());
}
return std::vector<ArgKind>(typeSet.begin(), typeSet.end());
}
std::vector<MatcherCompletion>
RegistryManager::getMatcherCompletions(llvm::ArrayRef<ArgKind> acceptedTypes,
const Registry &matcherRegistry) {
std::vector<MatcherCompletion> completions;
// Search the registry for acceptable matchers.
for (const auto &m : matcherRegistry.constructors()) {
const internal::MatcherDescriptor &matcher = *m.getValue();
llvm::StringRef name = m.getKey();
unsigned numArgs = matcher.isVariadic() ? 1 : matcher.getNumArgs();
std::vector<std::vector<ArgKind>> argKinds(numArgs);
for (const ArgKind &kind : acceptedTypes) {
if (kind != ArgKind::Matcher)
continue;
for (unsigned arg = 0; arg != numArgs; ++arg)
matcher.getArgKinds(arg, argKinds[arg]);
}
std::string decl;
llvm::raw_string_ostream os(decl);
std::string typedText = std::string(name);
os << "Matcher: " << name << "(";
for (const std::vector<ArgKind> &arg : argKinds) {
if (&arg != &argKinds[0])
os << ", ";
bool firstArgKind = true;
// Two steps. First all non-matchers, then matchers only.
for (const ArgKind &argKind : arg) {
if (!firstArgKind)
os << "|";
firstArgKind = false;
os << asArgString(argKind);
}
}
if (matcher.isVariadic())
os << ",...";
os << ")";
typedText += "(";
if (argKinds.empty())
typedText += ")";
else if (argKinds[0][0] == ArgKind::String)
typedText += "\"";
completions.emplace_back(typedText, decl);
}
return completions;
}
VariantMatcher RegistryManager::constructMatcher(
MatcherCtor ctor, internal::SourceRange nameRange,
llvm::StringRef functionName, llvm::ArrayRef<ParserValue> args,
internal::Diagnostics *error) {
VariantMatcher out = ctor->create(nameRange, args, error);
if (functionName.empty() || out.isNull())
return out;
if (std::optional<DynMatcher> result = out.getDynMatcher()) {
result->setFunctionName(functionName);
return VariantMatcher::SingleMatcher(*result);
}
error->addError(nameRange, internal::ErrorType::RegistryNotBindable);
return {};
}
} // namespace mlir::query::matcher
|