1 //===- Registry.h - Matcher registry ----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// Registry of all known matchers.
13 /// The registry provides a generic interface to construct any matcher by name.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_CLANG_ASTMATCHERS_DYNAMIC_REGISTRY_H
18 #define LLVM_CLANG_ASTMATCHERS_DYNAMIC_REGISTRY_H
20 #include "clang/ASTMatchers/Dynamic/Diagnostics.h"
21 #include "clang/ASTMatchers/Dynamic/VariantValue.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/ADT/Optional.h"
24 #include "llvm/ADT/StringRef.h"
30 namespace ast_matchers {
35 class MatcherDescriptor;
37 } // namespace internal
39 using MatcherCtor = const internal::MatcherDescriptor *;
41 struct MatcherCompletion {
42 MatcherCompletion() = default;
43 MatcherCompletion(StringRef TypedText, StringRef MatcherDecl,
45 : TypedText(TypedText), MatcherDecl(MatcherDecl),
46 Specificity(Specificity) {}
48 bool operator==(const MatcherCompletion &Other) const {
49 return TypedText == Other.TypedText && MatcherDecl == Other.MatcherDecl;
52 /// The text to type to select this matcher.
53 std::string TypedText;
55 /// The "declaration" of the matcher, with type information.
56 std::string MatcherDecl;
58 /// Value corresponding to the "specificity" of the converted matcher.
60 /// Zero specificity indicates that this conversion would produce a trivial
61 /// matcher that will either always or never match.
62 /// Such matchers are excluded from code completion results.
70 /// Look up a matcher in the registry by name,
72 /// \return An opaque value which may be used to refer to the matcher
73 /// constructor, or Optional<MatcherCtor>() if not found.
74 static llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName);
76 /// Compute the list of completion types for \p Context.
78 /// Each element of \p Context represents a matcher invocation, going from
79 /// outermost to innermost. Elements are pairs consisting of a reference to
80 /// the matcher constructor and the index of the next element in the
81 /// argument list of that matcher (or for the last element, the index of
82 /// the completion point in the argument list). An empty list requests
83 /// completion for the root matcher.
84 static std::vector<ArgKind> getAcceptedCompletionTypes(
85 llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> Context);
87 /// Compute the list of completions that match any of
90 /// \param AcceptedTypes All types accepted for this completion.
92 /// \return All completions for the specified types.
93 /// Completions should be valid when used in \c lookupMatcherCtor().
94 /// The matcher constructed from the return of \c lookupMatcherCtor()
95 /// should be convertible to some type in \p AcceptedTypes.
96 static std::vector<MatcherCompletion>
97 getMatcherCompletions(ArrayRef<ArgKind> AcceptedTypes);
99 /// Construct a matcher from the registry.
101 /// \param Ctor The matcher constructor to instantiate.
103 /// \param NameRange The location of the name in the matcher source.
104 /// Useful for error reporting.
106 /// \param Args The argument list for the matcher. The number and types of the
107 /// values must be valid for the matcher requested. Otherwise, the function
108 /// will return an error.
110 /// \return The matcher object constructed if no error was found.
111 /// A null matcher if the number of arguments or argument types do not match
112 /// the signature. In that case \c Error will contain the description of
114 static VariantMatcher constructMatcher(MatcherCtor Ctor,
115 SourceRange NameRange,
116 ArrayRef<ParserValue> Args,
119 /// Construct a matcher from the registry and bind it.
121 /// Similar the \c constructMatcher() above, but it then tries to bind the
122 /// matcher to the specified \c BindID.
123 /// If the matcher is not bindable, it sets an error in \c Error and returns
125 static VariantMatcher constructBoundMatcher(MatcherCtor Ctor,
126 SourceRange NameRange,
128 ArrayRef<ParserValue> Args,
132 } // namespace dynamic
133 } // namespace ast_matchers
136 #endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_REGISTRY_H