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 /// \brief 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 "clang/Basic/LLVM.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/Optional.h"
25 #include "llvm/ADT/StringRef.h"
28 namespace ast_matchers {
32 class MatcherDescriptor;
35 typedef const internal::MatcherDescriptor *MatcherCtor;
37 struct MatcherCompletion {
38 MatcherCompletion() {}
39 MatcherCompletion(StringRef TypedText, StringRef MatcherDecl,
41 : TypedText(TypedText), MatcherDecl(MatcherDecl),
42 Specificity(Specificity) {}
44 /// \brief The text to type to select this matcher.
45 std::string TypedText;
47 /// \brief The "declaration" of the matcher, with type information.
48 std::string MatcherDecl;
50 /// \brief Value corresponding to the "specificity" of the converted matcher.
52 /// Zero specificity indicates that this conversion would produce a trivial
53 /// matcher that will either always or never match.
54 /// Such matchers are excluded from code completion results.
57 bool operator==(const MatcherCompletion &Other) const {
58 return TypedText == Other.TypedText && MatcherDecl == Other.MatcherDecl;
64 /// \brief Look up a matcher in the registry by name,
66 /// \return An opaque value which may be used to refer to the matcher
67 /// constructor, or Optional<MatcherCtor>() if not found.
68 static llvm::Optional<MatcherCtor> lookupMatcherCtor(StringRef MatcherName);
70 /// \brief Compute the list of completion types for \p Context.
72 /// Each element of \p Context represents a matcher invocation, going from
73 /// outermost to innermost. Elements are pairs consisting of a reference to
74 /// the matcher constructor and the index of the next element in the
75 /// argument list of that matcher (or for the last element, the index of
76 /// the completion point in the argument list). An empty list requests
77 /// completion for the root matcher.
78 static std::vector<ArgKind> getAcceptedCompletionTypes(
79 llvm::ArrayRef<std::pair<MatcherCtor, unsigned>> Context);
81 /// \brief Compute the list of completions that match any of
84 /// \param AcceptedTypes All types accepted for this completion.
86 /// \return All completions for the specified types.
87 /// Completions should be valid when used in \c lookupMatcherCtor().
88 /// The matcher constructed from the return of \c lookupMatcherCtor()
89 /// should be convertible to some type in \p AcceptedTypes.
90 static std::vector<MatcherCompletion>
91 getMatcherCompletions(ArrayRef<ArgKind> AcceptedTypes);
93 /// \brief Construct a matcher from the registry.
95 /// \param Ctor The matcher constructor to instantiate.
97 /// \param NameRange The location of the name in the matcher source.
98 /// Useful for error reporting.
100 /// \param Args The argument list for the matcher. The number and types of the
101 /// values must be valid for the matcher requested. Otherwise, the function
102 /// will return an error.
104 /// \return The matcher object constructed if no error was found.
105 /// A null matcher if the number of arguments or argument types do not match
106 /// the signature. In that case \c Error will contain the description of
108 static VariantMatcher constructMatcher(MatcherCtor Ctor,
109 const SourceRange &NameRange,
110 ArrayRef<ParserValue> Args,
113 /// \brief Construct a matcher from the registry and bind it.
115 /// Similar the \c constructMatcher() above, but it then tries to bind the
116 /// matcher to the specified \c BindID.
117 /// If the matcher is not bindable, it sets an error in \c Error and returns
119 static VariantMatcher constructBoundMatcher(MatcherCtor Ctor,
120 const SourceRange &NameRange,
122 ArrayRef<ParserValue> Args,
129 } // namespace dynamic
130 } // namespace ast_matchers
133 #endif // LLVM_CLANG_AST_MATCHERS_DYNAMIC_REGISTRY_H