xref: /llvm-project/mlir/lib/Query/Matcher/RegistryManager.h (revision 58b44c8102afb0e76d1cb70d4a5d089f70d2f657)
1 //===--- RegistryManager.h - Matcher registry -------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // RegistryManager to manage registry of all known matchers.
10 //
11 // The registry provides a generic interface to construct any matcher by name.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef MLIR_TOOLS_MLIRQUERY_MATCHER_REGISTRYMANAGER_H
16 #define MLIR_TOOLS_MLIRQUERY_MATCHER_REGISTRYMANAGER_H
17 
18 #include "Diagnostics.h"
19 #include "mlir/Query/Matcher/Marshallers.h"
20 #include "mlir/Query/Matcher/Registry.h"
21 #include "mlir/Query/Matcher/VariantValue.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/ADT/StringRef.h"
25 #include <string>
26 
27 namespace mlir::query::matcher {
28 
29 using MatcherCtor = const internal::MatcherDescriptor *;
30 
31 struct MatcherCompletion {
32   MatcherCompletion() = default;
MatcherCompletionMatcherCompletion33   MatcherCompletion(llvm::StringRef typedText, llvm::StringRef matcherDecl)
34       : typedText(typedText.str()), matcherDecl(matcherDecl.str()) {}
35 
36   bool operator==(const MatcherCompletion &other) const {
37     return typedText == other.typedText && matcherDecl == other.matcherDecl;
38   }
39 
40   // The text to type to select this matcher.
41   std::string typedText;
42 
43   // The "declaration" of the matcher, with type information.
44   std::string matcherDecl;
45 };
46 
47 class RegistryManager {
48 public:
49   RegistryManager() = delete;
50 
51   static std::optional<MatcherCtor>
52   lookupMatcherCtor(llvm::StringRef matcherName,
53                     const Registry &matcherRegistry);
54 
55   static std::vector<ArgKind> getAcceptedCompletionTypes(
56       llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> context);
57 
58   static std::vector<MatcherCompletion>
59   getMatcherCompletions(ArrayRef<ArgKind> acceptedTypes,
60                         const Registry &matcherRegistry);
61 
62   static VariantMatcher constructMatcher(MatcherCtor ctor,
63                                          internal::SourceRange nameRange,
64                                          llvm::StringRef functionName,
65                                          ArrayRef<ParserValue> args,
66                                          internal::Diagnostics *error);
67 };
68 
69 } // namespace mlir::query::matcher
70 
71 #endif // MLIR_TOOLS_MLIRQUERY_MATCHER_REGISTRYMANAGER_H
72